<s> import json <EOL> import warnings <EOL> import h5py <EOL> import numpy as np <EOL> from . . import options <EOL> from . . database import db <EOL> from . braindata import BrainData , VolumeData , VertexData , _hash <EOL> default_cmap = options . config . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> def normalize ( data ) : <EOL> if isinstance ( data , tuple ) : <EOL> if len ( data ) == <NUM_LIT:3> : <EOL> if data [ <NUM_LIT:0> ] . dtype == np . uint8 : <EOL> return VolumeRGB ( data [ <NUM_LIT:0> ] [ ... , <NUM_LIT:0> ] , data [ <NUM_LIT:0> ] [ ... , <NUM_LIT:1> ] , data [ <NUM_LIT:0> ] [ ... , <NUM_LIT:2> ] , * data [ <NUM_LIT:1> : ] ) <EOL> return Volume ( * data ) <EOL> elif len ( data ) == <NUM_LIT:2> : <EOL> return Vertex ( * data ) <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> elif isinstance ( data , Dataview ) : <EOL> return data <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> def _from_hdf_data ( h5 , name , xfmname = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> dnode = h5 . get ( "<STR_LIT>" % name ) <EOL> if dnode is None : <EOL> dnode = h5 . get ( name ) <EOL> subj = dnode . attrs [ '<STR_LIT>' ] <EOL> if xfmname is None and '<STR_LIT>' in dnode . attrs : <EOL> xfmname = dnode . attrs [ '<STR_LIT>' ] <EOL> mask = None <EOL> if "<STR_LIT>" in dnode . attrs : <EOL> if dnode . attrs [ '<STR_LIT>' ] . startswith ( "<STR_LIT>" ) : <EOL> mask = h5 [ '<STR_LIT>' % ( dnode . attrs [ '<STR_LIT>' ] , xfmname , dnode . attrs [ '<STR_LIT>' ] ) ] . value <EOL> else : <EOL> mask = dnode . attrs [ '<STR_LIT>' ] <EOL> if dnode . dtype == np . uint8 and dnode . shape [ - <NUM_LIT:1> ] in ( <NUM_LIT:3> , <NUM_LIT:4> ) : <EOL> alpha = None <EOL> if dnode . shape [ - <NUM_LIT:1> ] == <NUM_LIT:4> : <EOL> alpha = dnode [ ... , <NUM_LIT:3> ] <EOL> if xfmname is None : <EOL> return VertexRGB ( dnode [ ... , <NUM_LIT:0> ] , dnode [ ... , <NUM_LIT:1> ] , dnode [ ... , <NUM_LIT:2> ] , subj , <EOL> alpha = alpha , ** kwargs ) <EOL> return VolumeRGB ( dnode [ ... , <NUM_LIT:0> ] , dnode [ ... , <NUM_LIT:1> ] , dnode [ ... , <NUM_LIT:2> ] , subj , xfmname , <EOL> alpha = alpha , mask = mask , ** kwargs ) <EOL> if xfmname is None : <EOL> return Vertex ( dnode , subj , ** kwargs ) <EOL> return Volume ( dnode , subj , xfmname , mask = mask , ** kwargs ) <EOL> def _from_hdf_view ( h5 , data , xfmname = None , vmin = None , vmax = None , ** kwargs ) : <EOL> try : <EOL> basestring <EOL> strcls = ( unicode , str ) <EOL> except NameError : <EOL> strcls = str <EOL> if isinstance ( data , strcls ) : <EOL> return _from_hdf_data ( h5 , data , xfmname = xfmname , vmin = vmin , vmax = vmax , ** kwargs ) <EOL> if len ( data ) == <NUM_LIT:2> : <EOL> dim1 = _from_hdf_data ( h5 , data [ <NUM_LIT:0> ] , xfmname = xfmname [ <NUM_LIT:0> ] ) <EOL> dim2 = _from_hdf_data ( h5 , data [ <NUM_LIT:1> ] , xfmname = xfmname [ <NUM_LIT:1> ] ) <EOL> cls = Vertex2D if isinstance ( dim1 , Vertex ) else Volume2D <EOL> return cls ( dim1 , dim2 , vmin = vmin [ <NUM_LIT:0> ] , vmin2 = vmin [ <NUM_LIT:1> ] , <EOL> vmax = vmax [ <NUM_LIT:0> ] , vmax2 = vmax [ <NUM_LIT:1> ] , ** kwargs ) <EOL> elif len ( data ) == <NUM_LIT:4> : <EOL> red , green , blue = [ _from_hdf_data ( h5 , d , xfmname = xfmname ) for d in data [ : <NUM_LIT:3> ] ] <EOL> alpha = None <EOL> if data [ <NUM_LIT:3> ] is not None : <EOL> alpha = _from_hdf_data ( h5 , data [ <NUM_LIT:3> ] , xfmname = xfmname ) <EOL> cls = VertexRGB if isinstance ( red , Vertex ) else VolumeRGB <EOL> return cls ( red , green , blue , alpha = alpha , ** kwargs ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> class Dataview ( object ) : <EOL> def __init__ ( self , cmap = None , vmin = None , vmax = None , description = "<STR_LIT>" , state = None , <EOL> cvmin = None , cvmax = None , cvthr = False , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if self . __class__ == Dataview : <EOL> raise TypeError ( '<STR_LIT>' ) <EOL> self . cmap = cmap if cmap is not None else default_cmap <EOL> self . vmin = vmin <EOL> self . vmax = vmax <EOL> self . state = state <EOL> self . attrs = kwargs <EOL> if '<STR_LIT>' not in self . attrs : <EOL> self . attrs [ '<STR_LIT>' ] = <NUM_LIT:1> <EOL> self . description = description <EOL> def copy ( self , * args , ** kwargs ) : <EOL> kwargs . update ( self . attrs ) <EOL> return self . __class__ ( * args , <EOL> cmap = self . cmap , <EOL> vmin = self . vmin , <EOL> vmax = self . vmax , <EOL> description = self . description , <EOL> state = self . state , <EOL> ** kwargs ) <EOL> @ property <EOL> def priority ( self ) : <EOL> return self . attrs [ '<STR_LIT>' ] <EOL> @ priority . setter <EOL> def priority ( self , value ) : <EOL> self . attrs [ '<STR_LIT>' ] = value <EOL> def to_json ( self , simple = False ) : <EOL> if simple : <EOL> return dict ( ) <EOL> sdict = dict ( <EOL> state = self . state , <EOL> attrs = self . attrs . copy ( ) , <EOL> desc = self . description ) <EOL> try : <EOL> sdict . update ( dict ( <EOL> cmap = [ self . cmap ] , <EOL> vmin = [ self . vmin if self . vmin is not None else np . percentile ( np . nan_to_num ( self . data ) , <NUM_LIT:1> ) ] , <EOL> vmax = [ self . vmax if self . vmax is not None else np . percentile ( np . nan_to_num ( self . data ) , <NUM_LIT> ) ] <EOL> ) ) <EOL> except AttributeError : <EOL> pass <EOL> return sdict <EOL> @ staticmethod <EOL> def from_hdf ( node ) : <EOL> data = json . loads ( node [ <NUM_LIT:0> ] ) <EOL> desc = node [ <NUM_LIT:1> ] <EOL> try : <EOL> cmap = json . loads ( node [ <NUM_LIT:2> ] ) <EOL> except : <EOL> cmap = node [ <NUM_LIT:2> ] <EOL> vmin = json . loads ( node [ <NUM_LIT:3> ] ) <EOL> vmax = json . loads ( node [ <NUM_LIT:4> ] ) <EOL> state = json . loads ( node [ <NUM_LIT:5> ] ) <EOL> attrs = json . loads ( node [ <NUM_LIT:6> ] ) <EOL> try : <EOL> xfmname = json . loads ( node [ <NUM_LIT:7> ] ) <EOL> except ValueError : <EOL> xfmname = None <EOL> if not isinstance ( vmin , list ) : <EOL> vmin = [ vmin ] <EOL> if not isinstance ( vmax , list ) : <EOL> vmax = [ vmax ] <EOL> if not isinstance ( cmap , list ) : <EOL> cmap = [ cmap ] <EOL> if len ( data ) == <NUM_LIT:1> : <EOL> xfm = None if xfmname is None else xfmname [ <NUM_LIT:0> ] <EOL> return _from_hdf_view ( node . file , data [ <NUM_LIT:0> ] , xfmname = xfm , cmap = cmap [ <NUM_LIT:0> ] , description = desc , <EOL> vmin = vmin [ <NUM_LIT:0> ] , vmax = vmax [ <NUM_LIT:0> ] , state = state , ** attrs ) <EOL> else : <EOL> views = [ _from_hdf_view ( node . file , d , xfmname = x ) for d , x in zip ( data , xfname ) ] <EOL> raise NotImplementedError <EOL> def _write_hdf ( self , h5 , name = "<STR_LIT:data>" , data = None , xfmname = None ) : <EOL> views = h5 . require_group ( "<STR_LIT>" ) <EOL> view = views . require_dataset ( name , ( <NUM_LIT:8> , ) , h5py . special_dtype ( vlen = str ) ) <EOL> view [ <NUM_LIT:0> ] = json . dumps ( data ) <EOL> view [ <NUM_LIT:1> ] = self . description <EOL> try : <EOL> view [ <NUM_LIT:2> ] = json . dumps ( [ self . cmap ] ) <EOL> view [ <NUM_LIT:3> ] = json . dumps ( [ self . vmin ] ) <EOL> view [ <NUM_LIT:4> ] = json . dumps ( [ self . vmax ] ) <EOL> except AttributeError : <EOL> view [ <NUM_LIT:2> ] = None <EOL> view [ <NUM_LIT:3> : <NUM_LIT:5> ] = "<STR_LIT:null>" <EOL> view [ <NUM_LIT:5> ] = json . dumps ( self . state ) <EOL> view [ <NUM_LIT:6> ] = json . dumps ( self . attrs ) <EOL> view [ <NUM_LIT:7> ] = json . dumps ( xfmname ) <EOL> return view <EOL> @ property <EOL> def raw ( self ) : <EOL> from matplotlib import colors , cm , pyplot as plt <EOL> import glob , os <EOL> if isinstance ( self . cmap , ( str , unicode ) ) : <EOL> if not self . cmap in cm . __dict__ : <EOL> cmapdir = options . config . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> colormaps = glob . glob ( os . path . join ( cmapdir , "<STR_LIT>" ) ) <EOL> colormaps = dict ( ( ( os . path . split ( c ) [ <NUM_LIT:1> ] [ : - <NUM_LIT:4> ] , c ) for c in colormaps ) ) <EOL> if not self . cmap in colormaps : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> I = plt . imread ( colormaps [ self . cmap ] ) <EOL> cmap = colors . ListedColormap ( np . squeeze ( I ) ) <EOL> cm . register_cmap ( self . cmap , cmap ) <EOL> else : <EOL> cmap = cm . get_cmap ( self . cmap ) <EOL> elif isinstance ( self . cmap , colors . Colormap ) : <EOL> cmap = self . cmap <EOL> norm = colors . Normalize ( self . vmin , self . vmax ) <EOL> cmapper = cm . ScalarMappable ( norm = norm , cmap = cmap ) <EOL> color_data = cmapper . to_rgba ( self . data . flatten ( ) ) . reshape ( self . data . shape + ( <NUM_LIT:4> , ) ) <EOL> return np . rollaxis ( color_data , - <NUM_LIT:1> ) <EOL> class Multiview ( Dataview ) : <EOL> def __init__ ( self , views , description = "<STR_LIT>" ) : <EOL> for view in views : <EOL> if not isinstance ( view , Dataview ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> raise NotImplementedError <EOL> self . views = views <EOL> def uniques ( self , collapse = False ) : <EOL> for view in self . views : <EOL> for sv in view . uniques ( collapse = collapse ) : <EOL> yield sv <EOL> class Volume ( VolumeData , Dataview ) : <EOL> def __init__ ( self , data , subject , xfmname , mask = None , <EOL> cmap = None , vmin = None , vmax = None , description = "<STR_LIT>" , ** kwargs ) : <EOL> super ( Volume , self ) . __init__ ( data , subject , xfmname , mask = mask , <EOL> cmap = cmap , vmin = vmin , vmax = vmax , description = description , ** kwargs ) <EOL> def _write_hdf ( self , h5 , name = "<STR_LIT:data>" ) : <EOL> datanode = VolumeData . _write_hdf ( self , h5 ) <EOL> viewnode = Dataview . _write_hdf ( self , h5 , name = name , <EOL> data = [ self . name ] , xfmname = [ self . xfmname ] ) <EOL> return viewnode <EOL> @ property <EOL> def raw ( self ) : <EOL> r , g , b , a = super ( Volume , self ) . raw <EOL> return VolumeRGB ( r , g , b , self . subject , self . xfmname , a , <EOL> description = self . description , state = self . state , ** self . attrs ) <EOL> class Vertex ( VertexData , Dataview ) : <EOL> def __init__ ( self , data , subject , cmap = None , vmin = None , vmax = None , description = "<STR_LIT>" , ** kwargs ) : <EOL> super ( Vertex , self ) . __init__ ( data , subject , cmap = cmap , vmin = vmin , vmax = vmax , <EOL> description = description , ** kwargs ) <EOL> def _write_hdf ( self , h5 , name = "<STR_LIT:data>" ) : <EOL> datanode = VertexData . _write_hdf ( self , h5 ) <EOL> viewnode = Dataview . _write_hdf ( self , h5 , name = name , data = [ self . name ] ) <EOL> return viewnode <EOL> @ property <EOL> def raw ( self ) : <EOL> r , g , b , a = super ( Vertex , self ) . raw <EOL> return VertexRGB ( r , g , b , self . subject , a , <EOL> description = self . description , state = self . state , ** self . attrs ) <EOL> from . viewRGB import VolumeRGB , VertexRGB <EOL> from . view2D import Volume2D , Vertex2D </s>
