Skip to content

Commit

Permalink
added curvature support in freeview and some other minor bug fix (#40)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
silencer1127 authored Sep 20, 2024
1 parent 7ca713d commit 1ae90ac
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
3 changes: 2 additions & 1 deletion surfa/io/framed.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ def save_framed_array(arr, filename, fmt=None, intent=FramedArrayIntents.mri):
iop = protocol.find_protocol_by_name(array_io_protocols, fmt)
if iop is None:
raise ValueError(f'unknown file format {fmt}')
filename = iop.enforce_extension(filename)
if iop.name != 'curv':
filename = iop.enforce_extension(filename)

# pass intent if iop() is an instance of MGHArrayIO
if (isinstance(iop(), MGHArrayIO)):
Expand Down
40 changes: 37 additions & 3 deletions surfa/vis/freeview.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def add_image(self, img, **kwargs):
# configure the corresponding freeview argument
self.arguments.append('-v ' + filename + _convert_kwargs_to_tags(kwargs))

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

# configure any curvatures
if curvature is not None:
curvature = [curvature] if not isinstance(curvature, (list, tuple)) else curvature
for c in curvature:
c = FreeviewCurvature(c) if not isinstance(c, FreeviewCurvature) else c
filename = _unique_filename(c.name, '.mgz', self.tempdir)
c.arr.save(filename)
if self.debug:
print(f'wrote curvature to {filename}')
tags += f':curvature={filename}' + c.tags()

# configure any overlays
if overlay is not None:
overlay = [overlay] if not isinstance(overlay, (list, tuple)) else overlay
Expand All @@ -135,6 +146,9 @@ def add_mesh(self, mesh, overlay=None, annot=None, **kwargs):
print(f'wrote annotation to {filename}')
tags += f':annot={filename}'

if name is not None:
tags += f':name={name}'

# configure the corresponding freeview argument
self.arguments.append('-f ' + mesh_filename + tags + _convert_kwargs_to_tags(kwargs))

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


class FreeviewCurvature:

def __init__(self, arr, name='curvature', method='binary'):
"""
Configuration for freeview curvature.
"""
self.arr = cast_overlay(arr, allow_none=False)
self.name = name
self.method = method

def tags(self):
tags = ''
tags += '' if self.method is None else f':curvature_method={self.method}'
return tags


class FreeviewOverlay:

def __init__(self, arr, name='overlay', threshold=None, opacity=None):
def __init__(self, arr, name='overlay', threshold=None, opacity=None, color=None, custom=None):
"""
Configuration for freeview overlays.
"""
self.arr = cast_overlay(arr, allow_none=False)
self.name = name
self.threshold = threshold
self.opacity = opacity
self.color = color
self.custom = custom

def tags(self):
tags = ''
tags += '' if self.threshold is None else f':overlay_threshold=' + ','.join(str(x) for x in config.threshold)
tags += '' if self.threshold is None else f':overlay_threshold=' + ','.join(str(x) for x in self.threshold)
tags += '' if self.opacity is None else f':overlay_opacity={self.opacity}'
tags += '' if self.color is None else f':overlay_color={self.color}'
tags += '' if self.custom is None else f':overlay_custom={self.custom}'
return tags


Expand Down

0 comments on commit 1ae90ac

Please sign in to comment.