Skip to content

Commit 6237fe3

Browse files
authored
NF,DOC improve docstring for show and make_static (#548)
* NF,DOC improve docstring for view and make_static * Remove cmap kwarg since it doesn't do anything
1 parent 372f20f commit 6237fe3

File tree

1 file changed

+154
-77
lines changed

1 file changed

+154
-77
lines changed

cortex/webgl/view.py

Lines changed: 154 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,27 @@
4040
colormaps = [(os.path.splitext(os.path.split(cm)[1])[0], serve.make_base64(cm))
4141
for cm in sorted(colormaps)]
4242

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+
):
4764
"""
4865
Creates a static webGL MRI viewer in your filesystem so that it can easily
4966
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"
7794
Labels for the listed layers will be set visible by default. Labels for
7895
layers not listed here will be hidden by default (but can be enabled in
7996
the viewer GUI). Default ('rois', )
80-
**kwargs
81-
All additional keyword arguments are passed to the template renderer.
8297
8398
Other parameters
8499
----------------
85100
types : tuple, optional
86101
Types of surfaces to include in addition to the original (fiducial, pial,
87102
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.
94103
html_embed : bool, optional
95104
Whether to embed the webgl resources in the html output. Default 'True'.
96105
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"
104113
a browser.
105114
layout : None or list of (int, int)
106115
The layout of the viewer subwindows for showing multiple subjects, passed to
107-
the template generator.
116+
the template generator.
108117
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.
109136
110137
Notes
111138
-----
112139
You will need a real web server to view this, since `file://` paths
113140
don't handle xsrf correctly
114141
"""
115142

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)
123145

124146
data = dataset.normalize(data)
125147
if not isinstance(data, dataset.Dataset):
@@ -130,10 +152,14 @@ def make_static(outpath, data, types=("inflated",), recache=False, cmap="RdBu_r"
130152
package = Package(data)
131153
subjects = list(package.subjects)
132154

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)
137163
package.reorder(ctms)
138164

139165
db.auxfile = None
@@ -144,15 +170,15 @@ def make_static(outpath, data, types=("inflated",), recache=False, cmap="RdBu_r"
144170
oldpath, fname = os.path.split(ctmfile)
145171
fname, ext = os.path.splitext(fname)
146172
if anonymize:
147-
newfname = "S%d"%i
173+
newfname = "S%d" % i
148174
submap[subj] = newfname
149175
else:
150176
newfname = fname
151-
ctms[subj] = newfname+".json"
177+
ctms[subj] = newfname + ".json"
152178

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))
156182
if os.path.exists(newfile):
157183
os.unlink(newfile)
158184

@@ -170,30 +196,31 @@ def make_static(outpath, data, types=("inflated",), recache=False, cmap="RdBu_r"
170196
ofh.close()
171197
if anonymize:
172198
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))
174200
if len(submap) == 0:
175201
submap = None
176202

177-
#Process the data
203+
# Process the data
178204
metadata = package.metadata(fmt="data/{name}_{frame}.png", submap=submap)
179205
images = package.images
180-
#Write out the PNGs
206+
# Write out the PNGs
181207
for name, imgs in images.items():
182208
impath = os.path.join(outpath, "data", "{name}_{frame}.png")
183209
for i, img in enumerate(imgs):
184210
with open(impath.format(name=name, frame=i), "wb") as binfile:
185211
binfile.write(img)
186212

187-
#Copy any stimulus files
213+
# Copy any stimulus files
188214
stimpath = os.path.join(outpath, "stim")
189215
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"]):
191217
if not os.path.exists(stimpath):
192218
os.makedirs(stimpath)
193-
shutil.copy2(view.attrs['stim'], stimpath)
219+
shutil.copy2(view.attrs["stim"], stimpath)
194220

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
196222
from . import htmlembed
223+
197224
if os.path.exists(template):
198225
## Load locally
199226
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"
206233
tpl = loader.load(templatefile)
207234

208235
# 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+
)
215259

216260
for sec in options.config.sections():
217-
if 'paths' in sec or 'labels' in sec:
261+
if "paths" in sec or "labels" in sec:
218262
my_viewopts[sec] = dict(options.config.items(sec))
219263

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+
)
230276
desthtml = os.path.join(outpath, "index.html")
231277
if html_embed:
232278
htmlembed.embed(html, desthtml, rootdirs)
@@ -235,15 +281,27 @@ def make_static(outpath, data, types=("inflated",), recache=False, cmap="RdBu_r"
235281
htmlfile.write(html)
236282

237283

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+
):
247305
"""
248306
Creates a webGL MRI viewer that is dynamically served by a tornado server
249307
running inside the current python process.
@@ -273,6 +331,10 @@ def show(data, types=("inflated", ), recache=False, cmap='RdBu_r', layout=None,
273331
Force recreation of CTM and SVG files for surfaces. Default False
274332
template : string, optional
275333
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).
276338
overlays_visible : tuple, optional
277339
The listed overlay layers will be set visible by default. Layers not listed
278340
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,
281343
Labels for the listed layers will be set visible by default. Labels for
282344
layers not listed here will be hidden by default (but can be enabled in
283345
the viewer GUI). Default ('rois', )
284-
**kwargs
285-
All additional keyword arguments are passed to the template renderer.
286346
287347
Other parameters
288348
----------------
289349
types : tuple, optional
290350
Types of surfaces to include in addition to the original (fiducial, pial,
291351
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?
295352
overlay_file : str or None, optional
296353
Custom overlays.svg file to use instead of the default one for this
297354
subject (if not None). Default None.
298355
curvature_brightness : float or None, optional
299356
Brightness of curvature overlay. Default None, which uses the value
300357
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.
304358
curvature_contrast : float or None, optional
305359
Contrast of curvature overlay. Default None, which uses the value
306360
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`.
307368
title : str, optional
308369
The title that is displayed on the viewer website when it is loaded in
309370
a browser.
310371
layout : None or list of (int, int), optional
311372
The layout of the viewer subwindows for showing multiple subjects, passed to
312373
the template generator.
313374
Default None, corresponding to no subwindows.
375+
**kwargs
376+
All additional keyword arguments are passed to the template renderer.
314377
"""
315378

316379
# populate default webshow args
@@ -361,12 +424,26 @@ def show(data, types=("inflated", ), recache=False, cmap='RdBu_r', layout=None,
361424
my_viewopts = dict(options.config.items('webgl_viewopts'))
362425
my_viewopts['overlays_visible'] = overlays_visible
363426
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+
)
370447

371448
for sec in options.config.sections():
372449
if 'paths' in sec or 'labels' in sec:
@@ -441,7 +518,7 @@ def get(self):
441518
self.set_header("Content-Type", "text/html")
442519
generated = html.generate(data=metadata,
443520
colormaps=colormaps,
444-
default_cmap=cmap,
521+
default_cmap="RdBu_r",
445522
python_interface=True,
446523
leapmotion=True,
447524
layout=layout,

0 commit comments

Comments
 (0)