1010# Qianqian Fang <q.fang at neu.edu>
1111##############
1212
13- __all__ = ['JMesh' ,'read' ,'write' ,'default_header' ]
13+ __all__ = ['JMesh' , 'read' , 'write' , 'default_header' ]
1414
15- from jdata import ( load as jdload , save as jdsave )
15+ from jdata import load as jdload , save as jdsave
1616import numpy as np
1717from ..filebasedimages import FileBasedImage
1818
1919default_header = {
20- "JMeshVersion" :"0.5" ,
21- "Comment" :"Created by NiPy with NeuroJSON JMesh specification" ,
22- "AnnotationFormat" :"https://neurojson.org/jmesh/draft2" ,
23- "Parser" :{
24- "Python" :[
25- "https://pypi.org/project/jdata" ,
26- "https://pypi.org/project/bjdata"
27- ],
28- "MATLAB" :[
29- "https://github.com/NeuroJSON/jnifty" ,
30- "https://github.com/NeuroJSON/jsonlab"
31- ],
32- "JavaScript" :"https://github.com/NeuroJSON/jsdata" ,
33- "CPP" :"https://github.com/NeuroJSON/json" ,
34- "C" :"https://github.com/NeuroJSON/ubj"
35- }
20+ 'JMeshVersion' : '0.5' ,
21+ 'Comment' : 'Created by NiPy with NeuroJSON JMesh specification' ,
22+ 'AnnotationFormat' : 'https://neurojson.org/jmesh/draft2' ,
23+ 'Parser' : {
24+ 'Python' : ['https://pypi.org/project/jdata' , 'https://pypi.org/project/bjdata' ],
25+ 'MATLAB' : ['https://github.com/NeuroJSON/jnifty' , 'https://github.com/NeuroJSON/jsonlab' ],
26+ 'JavaScript' : 'https://github.com/NeuroJSON/jsdata' ,
27+ 'CPP' : 'https://github.com/NeuroJSON/json' ,
28+ 'C' : 'https://github.com/NeuroJSON/ubj' ,
29+ },
3630}
3731
32+
3833class JMesh (FileBasedImage ):
3934 """JMesh: a simple data structure representing a brain surface
4035
@@ -62,28 +57,28 @@ class JMesh(FileBasedImage):
6257 raw : a dict
6358 The raw data loaded from the .jmsh or .bmsh file
6459 """
60+
6561 valid_exts = ('.jmsh' , '.bmsh' )
6662 files_types = (('image' , '.jmsh' ), ('image' , '.bmsh' ))
6763 makeable = False
6864 rw = True
6965
70- def __init__ (self , info = None , node = None , nodelabel = None , face = None ,
71- facelabel = None ):
66+ def __init__ (self , info = None , node = None , nodelabel = None , face = None , facelabel = None ):
7267
7368 self .raw = {}
74- if ( not info is None ) :
69+ if not info is None :
7570 self .raw ['_DataInfo_' ] = info
7671
77- if ( not nodelabel is None ) :
78- self .raw ['MeshVertex3' ] = {'Data' : node , 'Properties' : {'Tag' : nodelabel } }
72+ if not nodelabel is None :
73+ self .raw ['MeshVertex3' ] = {'Data' : node , 'Properties' : {'Tag' : nodelabel }}
7974 self .node = self .raw ['MeshVertex3' ]['Data' ]
8075 self .nodelabel = self .raw ['MeshVertex3' ]['Properties' ]['Tag' ]
8176 else :
8277 self .raw ['MeshVertex3' ] = node
8378 self .node = self .raw ['MeshVertex3' ]
8479
85- if ( not facelabel is None ) :
86- self .raw ['MeshTri3' ] = {'Data' : face , 'Properties' : {'Tag' : facelabel } }
80+ if not facelabel is None :
81+ self .raw ['MeshTri3' ] = {'Data' : face , 'Properties' : {'Tag' : facelabel }}
8782 self .face = self .raw ['MeshTri3' ]['Data' ]
8883 self .facelabel = self .raw ['MeshTri3' ]['Properties' ]['Tag' ]
8984 else :
@@ -99,8 +94,9 @@ def from_filename(self, filename, opt={}, **kwargs):
9994 def to_filename (self , filename , opt = {}, ** kwargs ):
10095 write (self , filename , opt , ** kwargs )
10196
97+
10298def read (filename , opt = {}, ** kwargs ):
103- """ Load a JSON or binary JData (BJData) based JMesh file
99+ """Load a JSON or binary JData (BJData) based JMesh file
104100
105101 Parameters
106102 ----------
@@ -116,73 +112,90 @@ def read(filename, opt={}, **kwargs):
116112 mesh : a JMesh object
117113 Return a JMesh object containing mesh data fields such as node, face, nodelabel etc
118114 """
119- opt .setdefault ('ndarray' ,True )
115+ opt .setdefault ('ndarray' , True )
120116
121117 mesh = JMesh
122118 mesh .raw = jdload (filename , opt , ** kwargs )
123119
124- #--------------------------------------------------
120+ # --------------------------------------------------
125121 # read metadata as `info`
126- #--------------------------------------------------
127- if ( '_DataInfo_' in mesh .raw ) :
122+ # --------------------------------------------------
123+ if '_DataInfo_' in mesh .raw :
128124 mesh .info = mesh .raw ['_DataInfo_' ]
129125
130- #--------------------------------------------------
126+ # --------------------------------------------------
131127 # read vertices as `node` and `nodelabel`
132- #--------------------------------------------------
133- if ( 'MeshVertex3' in mesh .raw ) :
128+ # --------------------------------------------------
129+ if 'MeshVertex3' in mesh .raw :
134130 mesh .node = mesh .raw ['MeshVertex3' ]
135- elif ( 'MeshNode' in mesh .raw ) :
131+ elif 'MeshNode' in mesh .raw :
136132 mesh .node = mesh .raw ['MeshNode' ]
137133 else :
138134 raise Exception ('JMesh' , 'JMesh surface must contain node (MeshVertex3 or MeshNode)' )
139135
140- if ( isinstance (mesh .node , dict ) ):
141- if (( 'Properties' in mesh .node ) and ('Tag' in mesh .node ['Properties' ]) ):
136+ if isinstance (mesh .node , dict ):
137+ if ( 'Properties' in mesh .node ) and ('Tag' in mesh .node ['Properties' ]):
142138 mesh .nodelabel = mesh .node ['Properties' ]['Tag' ]
143- if ( 'Data' in mesh .node ) :
139+ if 'Data' in mesh .node :
144140 mesh .node = mesh .node ['Data' ]
145- if ( isinstance (mesh .node , np .ndarray ) and mesh .node .ndim == 2 and mesh .node .shape [1 ] > 3 ) :
146- mesh .nodelabel = mesh .node [:,3 :]
141+ if isinstance (mesh .node , np .ndarray ) and mesh .node .ndim == 2 and mesh .node .shape [1 ] > 3 :
142+ mesh .nodelabel = mesh .node [:, 3 :]
147143 mesh .node = mesh .node [:, 0 :3 ]
148144
149- #--------------------------------------------------
145+ # --------------------------------------------------
150146 # read triangles as `face` and `facelabel`
151- #--------------------------------------------------
152- if ( 'MeshTri3' in mesh .raw ) :
147+ # --------------------------------------------------
148+ if 'MeshTri3' in mesh .raw :
153149 mesh .face = mesh .raw ['MeshTri3' ]
154- elif ( 'MeshSurf' in mesh .raw ) :
150+ elif 'MeshSurf' in mesh .raw :
155151 mesh .face = mesh .raw ['MeshSurf' ]
156152
157- if ( isinstance (mesh .face , dict ) ):
158- if (( 'Properties' in mesh .face ) and ('Tag' in mesh .face ['Properties' ]) ):
153+ if isinstance (mesh .face , dict ):
154+ if ( 'Properties' in mesh .face ) and ('Tag' in mesh .face ['Properties' ]):
159155 mesh .facelabel = mesh .face ['Properties' ]['Tag' ]
160- if ( 'Data' in mesh .face ) :
156+ if 'Data' in mesh .face :
161157 mesh .face = mesh .face ['Data' ]
162- if ( isinstance (mesh .face , np .ndarray ) and mesh .face .ndim == 2 and mesh .face .shape [1 ] > 3 ) :
163- mesh .facelabel = mesh .face [:,3 :]
158+ if isinstance (mesh .face , np .ndarray ) and mesh .face .ndim == 2 and mesh .face .shape [1 ] > 3 :
159+ mesh .facelabel = mesh .face [:, 3 :]
164160 mesh .face = mesh .face [:, 0 :3 ]
165161
166- #--------------------------------------------------
162+ # --------------------------------------------------
167163 # convert to numpy ndarray
168- #--------------------------------------------------
169- if (opt ['ndarray' ]):
170- if hasattr (mesh , 'node' ) and (not mesh .node is None ) and (not isinstance (mesh .node , np .ndarray )):
164+ # --------------------------------------------------
165+ if opt ['ndarray' ]:
166+ if (
167+ hasattr (mesh , 'node' )
168+ and (not mesh .node is None )
169+ and (not isinstance (mesh .node , np .ndarray ))
170+ ):
171171 mesh .node = np .array (mesh .node )
172172
173- if hasattr (mesh , 'face' ) and (not mesh .face is None ) and (not isinstance (mesh .face , np .ndarray )):
173+ if (
174+ hasattr (mesh , 'face' )
175+ and (not mesh .face is None )
176+ and (not isinstance (mesh .face , np .ndarray ))
177+ ):
174178 mesh .face = np .array (mesh .face )
175179
176- if hasattr (mesh , 'nodelabel' ) and (not mesh .nodelabel is None ) and (not isinstance (mesh .nodelabel , np .ndarray )):
180+ if (
181+ hasattr (mesh , 'nodelabel' )
182+ and (not mesh .nodelabel is None )
183+ and (not isinstance (mesh .nodelabel , np .ndarray ))
184+ ):
177185 mesh .nodelabel = np .array (mesh .nodelabel )
178186
179- if hasattr (mesh , 'facelabel' ) and (not mesh .facelabel is None ) and (not isinstance (mesh .facelabel , np .ndarray )):
187+ if (
188+ hasattr (mesh , 'facelabel' )
189+ and (not mesh .facelabel is None )
190+ and (not isinstance (mesh .facelabel , np .ndarray ))
191+ ):
180192 mesh .facelabel = np .array (mesh .facelabel )
181193
182194 return mesh
183195
196+
184197def write (mesh , filename , opt = {}, ** kwargs ):
185- """ Save the current mesh to a new file
198+ """Save the current mesh to a new file
186199
187200 Parameters
188201 ----------
@@ -207,20 +220,21 @@ def write(mesh, filename, opt={}, **kwargs):
207220 mesh .raw = {}
208221
209222 if hasattr (mesh , 'info' ) and not mesh .info is None :
210- mesh .raw ['_DataInfo_' ]= mesh .info
223+ mesh .raw ['_DataInfo_' ] = mesh .info
211224 if hasattr (mesh , 'node' ) and not mesh .node is None :
212- if ( hasattr (mesh , 'facelabel' ) and not mesh .nodelabel is None ) :
213- mesh .raw ['MeshVertex3' ]= {'Data' : mesh .node , 'Properties' : {'Tag' : mesh .nodelabel }}
225+ if hasattr (mesh , 'facelabel' ) and not mesh .nodelabel is None :
226+ mesh .raw ['MeshVertex3' ] = {'Data' : mesh .node , 'Properties' : {'Tag' : mesh .nodelabel }}
214227 else :
215- mesh .raw ['MeshVertex3' ]= mesh .node
228+ mesh .raw ['MeshVertex3' ] = mesh .node
216229
217230 if hasattr (mesh , 'info' ) and not mesh .face is None :
218- if ( hasattr (mesh , 'facelabel' ) and not mesh .facelabel is None ) :
219- mesh .raw ['MeshTri3' ]= {'Data' : mesh .face , 'Properties' : {'Tag' : mesh .facelabel }}
231+ if hasattr (mesh , 'facelabel' ) and not mesh .facelabel is None :
232+ mesh .raw ['MeshTri3' ] = {'Data' : mesh .face , 'Properties' : {'Tag' : mesh .facelabel }}
220233 else :
221- mesh .raw ['MeshTri3' ]= mesh .face
234+ mesh .raw ['MeshTri3' ] = mesh .face
222235
223236 return jdsave (mesh .raw , filename , opt , ** kwargs )
224237
238+
225239load = read
226240save = write
0 commit comments