Skip to content

Commit 1ae90ac

Browse files
authored
added curvature support in freeview and some other minor bug fix (#40)
* added FreeviewCurvature support, added more options and minor bug fix in tags in FreeviewOverlay * bug fix: do not remove the extension when writing curv files to disk * added FreeviewCurvature support, added more options and minor bug fix in tags in FreeviewOverlay * bug fix: do not remove the extension when writing curv files to disk
1 parent 7ca713d commit 1ae90ac

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

surfa/io/framed.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ def save_framed_array(arr, filename, fmt=None, intent=FramedArrayIntents.mri):
158158
iop = protocol.find_protocol_by_name(array_io_protocols, fmt)
159159
if iop is None:
160160
raise ValueError(f'unknown file format {fmt}')
161-
filename = iop.enforce_extension(filename)
161+
if iop.name != 'curv':
162+
filename = iop.enforce_extension(filename)
162163

163164
# pass intent if iop() is an instance of MGHArrayIO
164165
if (isinstance(iop(), MGHArrayIO)):

surfa/vis/freeview.py

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def add_image(self, img, **kwargs):
8080
# configure the corresponding freeview argument
8181
self.arguments.append('-v ' + filename + _convert_kwargs_to_tags(kwargs))
8282

83-
def add_mesh(self, mesh, overlay=None, annot=None, **kwargs):
83+
def add_mesh(self, mesh, curvature=None, overlay=None, annot=None, name=None, **kwargs):
8484
"""
8585
Adds an image to the freeview window. Any key/value tags allowed as a `-v` option
8686
in the freeview command line can be provided as an additional argument.
@@ -113,6 +113,17 @@ def add_mesh(self, mesh, overlay=None, annot=None, **kwargs):
113113
# extra tags for the mesh
114114
tags = ''
115115

116+
# configure any curvatures
117+
if curvature is not None:
118+
curvature = [curvature] if not isinstance(curvature, (list, tuple)) else curvature
119+
for c in curvature:
120+
c = FreeviewCurvature(c) if not isinstance(c, FreeviewCurvature) else c
121+
filename = _unique_filename(c.name, '.mgz', self.tempdir)
122+
c.arr.save(filename)
123+
if self.debug:
124+
print(f'wrote curvature to {filename}')
125+
tags += f':curvature={filename}' + c.tags()
126+
116127
# configure any overlays
117128
if overlay is not None:
118129
overlay = [overlay] if not isinstance(overlay, (list, tuple)) else overlay
@@ -135,6 +146,9 @@ def add_mesh(self, mesh, overlay=None, annot=None, **kwargs):
135146
print(f'wrote annotation to {filename}')
136147
tags += f':annot={filename}'
137148

149+
if name is not None:
150+
tags += f':name={name}'
151+
138152
# configure the corresponding freeview argument
139153
self.arguments.append('-f ' + mesh_filename + tags + _convert_kwargs_to_tags(kwargs))
140154

@@ -193,21 +207,41 @@ def show(self, background=True, threads=None):
193207
run(command, background=background)
194208

195209

210+
class FreeviewCurvature:
211+
212+
def __init__(self, arr, name='curvature', method='binary'):
213+
"""
214+
Configuration for freeview curvature.
215+
"""
216+
self.arr = cast_overlay(arr, allow_none=False)
217+
self.name = name
218+
self.method = method
219+
220+
def tags(self):
221+
tags = ''
222+
tags += '' if self.method is None else f':curvature_method={self.method}'
223+
return tags
224+
225+
196226
class FreeviewOverlay:
197227

198-
def __init__(self, arr, name='overlay', threshold=None, opacity=None):
228+
def __init__(self, arr, name='overlay', threshold=None, opacity=None, color=None, custom=None):
199229
"""
200230
Configuration for freeview overlays.
201231
"""
202232
self.arr = cast_overlay(arr, allow_none=False)
203233
self.name = name
204234
self.threshold = threshold
205235
self.opacity = opacity
236+
self.color = color
237+
self.custom = custom
206238

207239
def tags(self):
208240
tags = ''
209-
tags += '' if self.threshold is None else f':overlay_threshold=' + ','.join(str(x) for x in config.threshold)
241+
tags += '' if self.threshold is None else f':overlay_threshold=' + ','.join(str(x) for x in self.threshold)
210242
tags += '' if self.opacity is None else f':overlay_opacity={self.opacity}'
243+
tags += '' if self.color is None else f':overlay_color={self.color}'
244+
tags += '' if self.custom is None else f':overlay_custom={self.custom}'
211245
return tags
212246

213247

0 commit comments

Comments
 (0)