40
40
colormaps = [(os .path .splitext (os .path .split (cm )[1 ])[0 ], serve .make_base64 (cm ))
41
41
for cm in sorted (colormaps )]
42
42
43
- def make_static (outpath , data , types = ("inflated" ,), recache = False , cmap = "RdBu_r" ,
44
- template = "static.html" , layout = None , anonymize = False , overlays_available = None ,
45
- html_embed = True , overlays_visible = ('rois' , 'sulci' ), labels_visible = ('rois' , ),
46
- overlay_file = None , copy_ctmfiles = True , title = 'Brain' , ** kwargs ):
43
+ def make_static (
44
+ outpath ,
45
+ data ,
46
+ recache = False ,
47
+ template = "static.html" ,
48
+ anonymize = False ,
49
+ overlays_available = None ,
50
+ overlays_visible = ("rois" , "sulci" ),
51
+ labels_visible = ("rois" ,),
52
+ types = ("inflated" ,),
53
+ html_embed = True ,
54
+ copy_ctmfiles = True ,
55
+ title = "Brain" ,
56
+ layout = None ,
57
+ overlay_file = None ,
58
+ curvature_brightness = None ,
59
+ curvature_contrast = None ,
60
+ curvature_smoothness = None ,
61
+ surface_specularity = None ,
62
+ ** kwargs ,
63
+ ):
47
64
"""
48
65
Creates a static webGL MRI viewer in your filesystem so that it can easily
49
66
be posted publicly for sharing or just saved for later viewing.
@@ -77,20 +94,12 @@ def make_static(outpath, data, types=("inflated",), recache=False, cmap="RdBu_r"
77
94
Labels for the listed layers will be set visible by default. Labels for
78
95
layers not listed here will be hidden by default (but can be enabled in
79
96
the viewer GUI). Default ('rois', )
80
- **kwargs
81
- All additional keyword arguments are passed to the template renderer.
82
97
83
98
Other parameters
84
99
----------------
85
100
types : tuple, optional
86
101
Types of surfaces to include in addition to the original (fiducial, pial,
87
102
and white matter) and flat surfaces. Default ('inflated', )
88
- cmap : string, optional
89
- Name of default colormap. Default 'RdBu_r'
90
- TODO: DOES THIS DO ANYTHING ANYMORE?
91
- overlay_file : str, optional
92
- Custom overlays.svg file to use instead of the default one for this
93
- subject (if not None). Default None.
94
103
html_embed : bool, optional
95
104
Whether to embed the webgl resources in the html output. Default 'True'.
96
105
If 'False', the webgl resources must be served by your web server.
@@ -104,22 +113,35 @@ def make_static(outpath, data, types=("inflated",), recache=False, cmap="RdBu_r"
104
113
a browser.
105
114
layout : None or list of (int, int)
106
115
The layout of the viewer subwindows for showing multiple subjects, passed to
107
- the template generator.
116
+ the template generator.
108
117
Default to None, corresponding to no subwindows.
118
+ overlay_file : str or None, optional
119
+ Custom overlays.svg file to use instead of the default one for this
120
+ subject (if not None). Default None.
121
+ curvature_brightness : float or None, optional
122
+ Brightness of curvature overlay. Default None, which uses the value
123
+ specified in the config file.
124
+ curvature_contrast : float or None, optional
125
+ Contrast of curvature overlay. Default None, which uses the value
126
+ specified in the config file.
127
+ curvature_smoothness : float or None, optional
128
+ Smoothness of curvature overlay. Default None, which uses the value
129
+ specified in the config file.
130
+ surface_specularity : float or None, optional
131
+ Specularity of surfaces visualized with the WebGL viewer.
132
+ Default None, which uses the value specified in the config file under
133
+ `webgl_viewopts.specularity`.
134
+ **kwargs
135
+ All additional keyword arguments are passed to the template renderer.
109
136
110
137
Notes
111
138
-----
112
139
You will need a real web server to view this, since `file://` paths
113
140
don't handle xsrf correctly
114
141
"""
115
142
116
- outpath = os .path .abspath (os .path .expanduser (outpath )) # To handle ~ expansion
117
- if not os .path .exists (outpath ):
118
- os .makedirs (outpath )
119
- if not os .path .exists (os .path .join (outpath , 'data' )):
120
- # Don't lump together w/ outpath, because of edge cases
121
- # for which outpath exists but not sub-folder `data`
122
- os .makedirs (os .path .join (outpath , "data" ))
143
+ outpath = os .path .abspath (os .path .expanduser (outpath )) # To handle ~ expansion
144
+ os .makedirs (os .path .join (outpath , "data" ), exist_ok = True )
123
145
124
146
data = dataset .normalize (data )
125
147
if not isinstance (data , dataset .Dataset ):
@@ -130,10 +152,14 @@ def make_static(outpath, data, types=("inflated",), recache=False, cmap="RdBu_r"
130
152
package = Package (data )
131
153
subjects = list (package .subjects )
132
154
133
- ctmargs = dict (method = 'mg2' , level = 9 , recache = recache , external_svg = overlay_file ,
134
- overlays_available = overlays_available )
135
- ctms = dict ((subj , utils .get_ctmpack (subj , types , ** ctmargs ))
136
- for subj in subjects )
155
+ ctmargs = dict (
156
+ method = "mg2" ,
157
+ level = 9 ,
158
+ recache = recache ,
159
+ external_svg = overlay_file ,
160
+ overlays_available = overlays_available ,
161
+ )
162
+ ctms = dict ((subj , utils .get_ctmpack (subj , types , ** ctmargs )) for subj in subjects )
137
163
package .reorder (ctms )
138
164
139
165
db .auxfile = None
@@ -144,15 +170,15 @@ def make_static(outpath, data, types=("inflated",), recache=False, cmap="RdBu_r"
144
170
oldpath , fname = os .path .split (ctmfile )
145
171
fname , ext = os .path .splitext (fname )
146
172
if anonymize :
147
- newfname = "S%d" % i
173
+ newfname = "S%d" % i
148
174
submap [subj ] = newfname
149
175
else :
150
176
newfname = fname
151
- ctms [subj ] = newfname + ".json"
177
+ ctms [subj ] = newfname + ".json"
152
178
153
- for ext in [' json' , ' ctm' , ' svg' ]:
154
- srcfile = os .path .join (oldpath , "%s.%s" % (fname , ext ))
155
- newfile = os .path .join (outpath , "%s.%s" % (newfname , ext ))
179
+ for ext in [" json" , " ctm" , " svg" ]:
180
+ srcfile = os .path .join (oldpath , "%s.%s" % (fname , ext ))
181
+ newfile = os .path .join (outpath , "%s.%s" % (newfname , ext ))
156
182
if os .path .exists (newfile ):
157
183
os .unlink (newfile )
158
184
@@ -170,30 +196,31 @@ def make_static(outpath, data, types=("inflated",), recache=False, cmap="RdBu_r"
170
196
ofh .close ()
171
197
if anonymize :
172
198
old_subjects = sorted (list (ctms .keys ()))
173
- ctms = dict ((' S%d' % i , ctms [k ]) for i , k in enumerate (old_subjects ))
199
+ ctms = dict ((" S%d" % i , ctms [k ]) for i , k in enumerate (old_subjects ))
174
200
if len (submap ) == 0 :
175
201
submap = None
176
202
177
- #Process the data
203
+ # Process the data
178
204
metadata = package .metadata (fmt = "data/{name}_{frame}.png" , submap = submap )
179
205
images = package .images
180
- #Write out the PNGs
206
+ # Write out the PNGs
181
207
for name , imgs in images .items ():
182
208
impath = os .path .join (outpath , "data" , "{name}_{frame}.png" )
183
209
for i , img in enumerate (imgs ):
184
210
with open (impath .format (name = name , frame = i ), "wb" ) as binfile :
185
211
binfile .write (img )
186
212
187
- #Copy any stimulus files
213
+ # Copy any stimulus files
188
214
stimpath = os .path .join (outpath , "stim" )
189
215
for name , view in data :
190
- if ' stim' in view .attrs and os .path .exists (view .attrs [' stim' ]):
216
+ if " stim" in view .attrs and os .path .exists (view .attrs [" stim" ]):
191
217
if not os .path .exists (stimpath ):
192
218
os .makedirs (stimpath )
193
- shutil .copy2 (view .attrs [' stim' ], stimpath )
219
+ shutil .copy2 (view .attrs [" stim" ], stimpath )
194
220
195
- #Parse the html file and paste all the js and css files directly into the html
221
+ # Parse the html file and paste all the js and css files directly into the html
196
222
from . import htmlembed
223
+
197
224
if os .path .exists (template ):
198
225
## Load locally
199
226
templatedir , templatefile = os .path .split (os .path .abspath (template ))
@@ -206,27 +233,46 @@ def make_static(outpath, data, types=("inflated",), recache=False, cmap="RdBu_r"
206
233
tpl = loader .load (templatefile )
207
234
208
235
# Put together all view options
209
- my_viewopts = dict (options .config .items ('webgl_viewopts' ))
210
- my_viewopts ['overlays_visible' ] = overlays_visible
211
- my_viewopts ['labels_visible' ] = labels_visible
212
- my_viewopts ['brightness' ] = options .config .get ('curvature' , 'brightness' )
213
- my_viewopts ['smoothness' ] = options .config .get ('curvature' , 'webgl_smooth' )
214
- my_viewopts ['contrast' ] = options .config .get ('curvature' , 'contrast' )
236
+ my_viewopts = dict (options .config .items ("webgl_viewopts" ))
237
+ my_viewopts ["overlays_visible" ] = overlays_visible
238
+ my_viewopts ["labels_visible" ] = labels_visible
239
+ my_viewopts ["brightness" ] = (
240
+ options .config .get ("curvature" , "brightness" )
241
+ if curvature_brightness is None
242
+ else curvature_brightness
243
+ )
244
+ my_viewopts ["contrast" ] = (
245
+ options .config .get ("curvature" , "contrast" )
246
+ if curvature_contrast is None
247
+ else curvature_contrast
248
+ )
249
+ my_viewopts ["smoothness" ] = (
250
+ options .config .get ("curvature" , "webgl_smooth" )
251
+ if curvature_smoothness is None
252
+ else curvature_smoothness
253
+ )
254
+ my_viewopts ["specularity" ] = (
255
+ options .config .get ("webgl_viewopts" , "specularity" )
256
+ if surface_specularity is None
257
+ else surface_specularity
258
+ )
215
259
216
260
for sec in options .config .sections ():
217
- if ' paths' in sec or ' labels' in sec :
261
+ if " paths" in sec or " labels" in sec :
218
262
my_viewopts [sec ] = dict (options .config .items (sec ))
219
263
220
- html = tpl .generate (data = json .dumps (metadata ),
221
- colormaps = colormaps ,
222
- default_cmap = cmap ,
223
- python_interface = False ,
224
- leapmotion = True ,
225
- layout = layout ,
226
- subjects = json .dumps (ctms ),
227
- viewopts = json .dumps (my_viewopts ),
228
- title = title ,
229
- ** kwargs )
264
+ html = tpl .generate (
265
+ data = json .dumps (metadata ),
266
+ colormaps = colormaps ,
267
+ default_cmap = "RdBu_r" ,
268
+ python_interface = False ,
269
+ leapmotion = True ,
270
+ layout = layout ,
271
+ subjects = json .dumps (ctms ),
272
+ viewopts = json .dumps (my_viewopts ),
273
+ title = title ,
274
+ ** kwargs ,
275
+ )
230
276
desthtml = os .path .join (outpath , "index.html" )
231
277
if html_embed :
232
278
htmlembed .embed (html , desthtml , rootdirs )
@@ -235,15 +281,27 @@ def make_static(outpath, data, types=("inflated",), recache=False, cmap="RdBu_r"
235
281
htmlfile .write (html )
236
282
237
283
238
- def show (data , types = ("inflated" , ), recache = False , cmap = 'RdBu_r' , layout = None ,
239
- autoclose = None , open_browser = None , port = None , pickerfun = None ,
240
- template = "mixer.html" , overlays_available = None ,
241
- overlays_visible = ('rois' , 'sulci' ), labels_visible = ('rois' , ),
242
- overlay_file = None ,
243
- curvature_brightness = None ,
244
- curvature_smoothness = None ,
245
- curvature_contrast = None ,
246
- title = 'Brain' , ** kwargs ):
284
+ def show (
285
+ data ,
286
+ autoclose = None ,
287
+ open_browser = None ,
288
+ port = None ,
289
+ pickerfun = None ,
290
+ recache = False ,
291
+ template = "mixer.html" ,
292
+ overlays_available = None ,
293
+ overlays_visible = ("rois" , "sulci" ),
294
+ labels_visible = ("rois" ,),
295
+ types = ("inflated" ,),
296
+ overlay_file = None ,
297
+ curvature_brightness = None ,
298
+ curvature_contrast = None ,
299
+ curvature_smoothness = None ,
300
+ surface_specularity = None ,
301
+ title = "Brain" ,
302
+ layout = None ,
303
+ ** kwargs ,
304
+ ):
247
305
"""
248
306
Creates a webGL MRI viewer that is dynamically served by a tornado server
249
307
running inside the current python process.
@@ -273,6 +331,10 @@ def show(data, types=("inflated", ), recache=False, cmap='RdBu_r', layout=None,
273
331
Force recreation of CTM and SVG files for surfaces. Default False
274
332
template : string, optional
275
333
Name of template HTML file. Default 'mixer.html'
334
+ overlays_available : tuple, optional
335
+ Overlays available in the viewer. If None, then all overlay layers of the
336
+ svg file will be potentially available in the viewer (whether initially
337
+ visible or not).
276
338
overlays_visible : tuple, optional
277
339
The listed overlay layers will be set visible by default. Layers not listed
278
340
here will be hidden by default (but can be enabled in the viewer GUI).
@@ -281,36 +343,37 @@ def show(data, types=("inflated", ), recache=False, cmap='RdBu_r', layout=None,
281
343
Labels for the listed layers will be set visible by default. Labels for
282
344
layers not listed here will be hidden by default (but can be enabled in
283
345
the viewer GUI). Default ('rois', )
284
- **kwargs
285
- All additional keyword arguments are passed to the template renderer.
286
346
287
347
Other parameters
288
348
----------------
289
349
types : tuple, optional
290
350
Types of surfaces to include in addition to the original (fiducial, pial,
291
351
and white matter) and flat surfaces. Default ('inflated', )
292
- cmap : string, optional
293
- Name of default colormap. Default 'RdBu_r'
294
- TODO: DOES THIS DO ANYTHING ANYMORE?
295
352
overlay_file : str or None, optional
296
353
Custom overlays.svg file to use instead of the default one for this
297
354
subject (if not None). Default None.
298
355
curvature_brightness : float or None, optional
299
356
Brightness of curvature overlay. Default None, which uses the value
300
357
specified in the config file.
301
- curvature_smoothness : float or None, optional
302
- Smoothness of curvature overlay. Default None, which uses the value
303
- specified in the config file.
304
358
curvature_contrast : float or None, optional
305
359
Contrast of curvature overlay. Default None, which uses the value
306
360
specified in the config file.
361
+ curvature_smoothness : float or None, optional
362
+ Smoothness of curvature overlay. Default None, which uses the value
363
+ specified in the config file.
364
+ surface_specularity : float or None, optional
365
+ Specularity of surfaces visualized with the WebGL viewer.
366
+ Default None, which uses the value specified in the config file under
367
+ `webgl_viewopts.specularity`.
307
368
title : str, optional
308
369
The title that is displayed on the viewer website when it is loaded in
309
370
a browser.
310
371
layout : None or list of (int, int), optional
311
372
The layout of the viewer subwindows for showing multiple subjects, passed to
312
373
the template generator.
313
374
Default None, corresponding to no subwindows.
375
+ **kwargs
376
+ All additional keyword arguments are passed to the template renderer.
314
377
"""
315
378
316
379
# populate default webshow args
@@ -361,12 +424,26 @@ def show(data, types=("inflated", ), recache=False, cmap='RdBu_r', layout=None,
361
424
my_viewopts = dict (options .config .items ('webgl_viewopts' ))
362
425
my_viewopts ['overlays_visible' ] = overlays_visible
363
426
my_viewopts ['labels_visible' ] = labels_visible
364
- my_viewopts ['brightness' ] = options .config .get ('curvature' , 'brightness' ) \
365
- if curvature_brightness is None else curvature_brightness
366
- my_viewopts ['smoothness' ] = options .config .get ('curvature' , 'webgl_smooth' ) \
367
- if curvature_smoothness is None else curvature_smoothness
368
- my_viewopts ['contrast' ] = options .config .get ('curvature' , 'contrast' ) \
369
- if curvature_contrast is None else curvature_contrast
427
+ my_viewopts ["brightness" ] = (
428
+ options .config .get ("curvature" , "brightness" )
429
+ if curvature_brightness is None
430
+ else curvature_brightness
431
+ )
432
+ my_viewopts ["contrast" ] = (
433
+ options .config .get ("curvature" , "contrast" )
434
+ if curvature_contrast is None
435
+ else curvature_contrast
436
+ )
437
+ my_viewopts ["smoothness" ] = (
438
+ options .config .get ("curvature" , "webgl_smooth" )
439
+ if curvature_smoothness is None
440
+ else curvature_smoothness
441
+ )
442
+ my_viewopts ["specularity" ] = (
443
+ options .config .get ("webgl_viewopts" , "specularity" )
444
+ if surface_specularity is None
445
+ else surface_specularity
446
+ )
370
447
371
448
for sec in options .config .sections ():
372
449
if 'paths' in sec or 'labels' in sec :
@@ -441,7 +518,7 @@ def get(self):
441
518
self .set_header ("Content-Type" , "text/html" )
442
519
generated = html .generate (data = metadata ,
443
520
colormaps = colormaps ,
444
- default_cmap = cmap ,
521
+ default_cmap = "RdBu_r" ,
445
522
python_interface = True ,
446
523
leapmotion = True ,
447
524
layout = layout ,
0 commit comments