|
10 | 10 | # Qianqian Fang <q.fang at neu.edu>
|
11 | 11 | ##############
|
12 | 12 |
|
13 |
| -__all__ = ['JMesh','read','write','default_header'] |
| 13 | +__all__ = ['JMesh', 'read', 'write', 'default_header'] |
14 | 14 |
|
15 |
| -from jdata import (load as jdload, save as jdsave) |
| 15 | +from jdata import load as jdload, save as jdsave |
16 | 16 | import numpy as np
|
17 | 17 | from ..filebasedimages import FileBasedImage
|
18 | 18 |
|
19 | 19 | default_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://.com/NeuroJSON/jnifty", |
30 |
| -"https://.com/NeuroJSON/jsonlab" |
31 |
| -], |
32 |
| -"JavaScript":"https://.com/NeuroJSON/jsdata", |
33 |
| -"CPP":"https://.com/NeuroJSON/json", |
34 |
| -"C":"https://.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://.com/NeuroJSON/jnifty', 'https://.com/NeuroJSON/jsonlab'], |
| 26 | +'JavaScript': 'https://.com/NeuroJSON/jsdata', |
| 27 | +'CPP': 'https://.com/NeuroJSON/json', |
| 28 | +'C': 'https://.com/NeuroJSON/ubj', |
| 29 | +}, |
36 | 30 | }
|
37 | 31 |
|
| 32 | + |
38 | 33 | class JMesh(FileBasedImage):
|
39 | 34 | """JMesh: a simple data structure representing a brain surface
|
40 | 35 |
|
@@ -62,28 +57,28 @@ class JMesh(FileBasedImage):
|
62 | 57 | raw : a dict
|
63 | 58 | The raw data loaded from the .jmsh or .bmsh file
|
64 | 59 | """
|
| 60 | + |
65 | 61 | valid_exts = ('.jmsh', '.bmsh')
|
66 | 62 | files_types = (('image', '.jmsh'), ('image', '.bmsh'))
|
67 | 63 | makeable = False
|
68 | 64 | rw = True
|
69 | 65 |
|
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): |
72 | 67 |
|
73 | 68 | self.raw = {}
|
74 |
| -if(not info is None): |
| 69 | +if not info is None: |
75 | 70 | self.raw['_DataInfo_'] = info
|
76 | 71 |
|
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}} |
79 | 74 | self.node = self.raw['MeshVertex3']['Data']
|
80 | 75 | self.nodelabel = self.raw['MeshVertex3']['Properties']['Tag']
|
81 | 76 | else:
|
82 | 77 | self.raw['MeshVertex3'] = node
|
83 | 78 | self.node = self.raw['MeshVertex3']
|
84 | 79 |
|
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}} |
87 | 82 | self.face = self.raw['MeshTri3']['Data']
|
88 | 83 | self.facelabel = self.raw['MeshTri3']['Properties']['Tag']
|
89 | 84 | else:
|
@@ -99,8 +94,9 @@ def from_filename(self, filename, opt={}, **kwargs):
|
99 | 94 | def to_filename(self, filename, opt={}, **kwargs):
|
100 | 95 | write(self, filename, opt, **kwargs)
|
101 | 96 |
|
| 97 | + |
102 | 98 | def 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 |
104 | 100 |
|
105 | 101 | Parameters
|
106 | 102 | ----------
|
@@ -116,73 +112,90 @@ def read(filename, opt={}, **kwargs):
|
116 | 112 | mesh : a JMesh object
|
117 | 113 | Return a JMesh object containing mesh data fields such as node, face, nodelabel etc
|
118 | 114 | """
|
119 |
| -opt.setdefault('ndarray',True) |
| 115 | +opt.setdefault('ndarray', True) |
120 | 116 |
|
121 | 117 | mesh = JMesh
|
122 | 118 | mesh.raw = jdload(filename, opt, **kwargs)
|
123 | 119 |
|
124 |
| -#-------------------------------------------------- |
| 120 | +# -------------------------------------------------- |
125 | 121 | # read metadata as `info`
|
126 |
| -#-------------------------------------------------- |
127 |
| -if('_DataInfo_' in mesh.raw): |
| 122 | +# -------------------------------------------------- |
| 123 | +if '_DataInfo_' in mesh.raw: |
128 | 124 | mesh.info = mesh.raw['_DataInfo_']
|
129 | 125 |
|
130 |
| -#-------------------------------------------------- |
| 126 | +# -------------------------------------------------- |
131 | 127 | # read vertices as `node` and `nodelabel`
|
132 |
| -#-------------------------------------------------- |
133 |
| -if('MeshVertex3' in mesh.raw): |
| 128 | +# -------------------------------------------------- |
| 129 | +if 'MeshVertex3' in mesh.raw: |
134 | 130 | mesh.node = mesh.raw['MeshVertex3']
|
135 |
| -elif('MeshNode' in mesh.raw): |
| 131 | +elif 'MeshNode' in mesh.raw: |
136 | 132 | mesh.node = mesh.raw['MeshNode']
|
137 | 133 | else:
|
138 | 134 | raise Exception('JMesh', 'JMesh surface must contain node (MeshVertex3 or MeshNode)')
|
139 | 135 |
|
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']): |
142 | 138 | mesh.nodelabel = mesh.node['Properties']['Tag']
|
143 |
| -if('Data' in mesh.node): |
| 139 | +if 'Data' in mesh.node: |
144 | 140 | 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:] |
147 | 143 | mesh.node = mesh.node[:, 0:3]
|
148 | 144 |
|
149 |
| -#-------------------------------------------------- |
| 145 | +# -------------------------------------------------- |
150 | 146 | # read triangles as `face` and `facelabel`
|
151 |
| -#-------------------------------------------------- |
152 |
| -if('MeshTri3' in mesh.raw): |
| 147 | +# -------------------------------------------------- |
| 148 | +if 'MeshTri3' in mesh.raw: |
153 | 149 | mesh.face = mesh.raw['MeshTri3']
|
154 |
| -elif('MeshSurf' in mesh.raw): |
| 150 | +elif 'MeshSurf' in mesh.raw: |
155 | 151 | mesh.face = mesh.raw['MeshSurf']
|
156 | 152 |
|
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']): |
159 | 155 | mesh.facelabel = mesh.face['Properties']['Tag']
|
160 |
| -if('Data' in mesh.face): |
| 156 | +if 'Data' in mesh.face: |
161 | 157 | 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:] |
164 | 160 | mesh.face = mesh.face[:, 0:3]
|
165 | 161 |
|
166 |
| -#-------------------------------------------------- |
| 162 | +# -------------------------------------------------- |
167 | 163 | # 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 | +): |
171 | 171 | mesh.node = np.array(mesh.node)
|
172 | 172 |
|
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 | +): |
174 | 178 | mesh.face = np.array(mesh.face)
|
175 | 179 |
|
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 | +): |
177 | 185 | mesh.nodelabel = np.array(mesh.nodelabel)
|
178 | 186 |
|
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 | +): |
180 | 192 | mesh.facelabel = np.array(mesh.facelabel)
|
181 | 193 |
|
182 | 194 | return mesh
|
183 | 195 |
|
| 196 | + |
184 | 197 | def write(mesh, filename, opt={}, **kwargs):
|
185 |
| -""" Save the current mesh to a new file |
| 198 | +"""Save the current mesh to a new file |
186 | 199 |
|
187 | 200 | Parameters
|
188 | 201 | ----------
|
@@ -207,20 +220,21 @@ def write(mesh, filename, opt={}, **kwargs):
|
207 | 220 | mesh.raw = {}
|
208 | 221 |
|
209 | 222 | if hasattr(mesh, 'info') and not mesh.info is None:
|
210 |
| -mesh.raw['_DataInfo_']=mesh.info |
| 223 | +mesh.raw['_DataInfo_'] = mesh.info |
211 | 224 | 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}} |
214 | 227 | else:
|
215 |
| -mesh.raw['MeshVertex3']=mesh.node |
| 228 | +mesh.raw['MeshVertex3'] = mesh.node |
216 | 229 |
|
217 | 230 | 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}} |
220 | 233 | else:
|
221 |
| -mesh.raw['MeshTri3']=mesh.face |
| 234 | +mesh.raw['MeshTri3'] = mesh.face |
222 | 235 |
|
223 | 236 | return jdsave(mesh.raw, filename, opt, **kwargs)
|
224 | 237 |
|
| 238 | + |
225 | 239 | load = read
|
226 | 240 | save = write
|
0 commit comments