Skip to content

Commit

Permalink
Display vertebral levels when provided with -perslice 1 (#4004)
Browse files Browse the repository at this point in the history
* change if condition for arg assignement

* add vertgroup for perslice

* remove deflault value of -vertfile

* remove blank line

* `sct_process_segmentation`: Add `default=''` to `-vert`

Without this, currently the default value of `argumnets.vert`
is `None`.

By adding this line, it makes it so that the default value of
`arguments.vert` will be an empty list (`[]`).

* `sct_process_segmentation`: Restore the default of `-vertfile`

This allows users to specify `-vert` by itself, assuming they've run
`sct_warp_template` and generated the level file in the working directory.

* `sct_process_segmentation`: Remove the `vertfile` conditional

Instead of manually setting `vert` to `[]` and `vertfile` to `None`,
we can just rely on their default values.

* `sct_process_segmentation`: Add existence check for `-vertfile`

Without this check, `aggregate_per_slice_or_level` will try to generate
vertebral level information on a nonexistent file.

Co-authored-by: Joshua Newton <joshuacwnewton@gmail.com>
  • Loading branch information
sandrinebedard and joshuacwnewton committed Jan 24, 2023
1 parent a3d052b commit 2d09af3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
8 changes: 7 additions & 1 deletion spinalcordtoolbox/aggregate_slicewise.py
Expand Up @@ -258,6 +258,8 @@ def aggregate_per_slice_or_level(metric, mask=None, slices=[], levels=[], distan
raise ValueError(f"Shape mismatch between vertfile [{vert_level_slices}] and metric [{metric_slices}]). "
f"Please verify that your vertfile has the same number of slices as your input image, "
f"and that your metric is RPI/LPI oriented.")
# Fetch fname_vert_level
im_vert_level = Image(fname_vert_level).change_orientation('RPI')

# If perslice is specified, put distance_pmj to None to prioritize perslice
if perslice:
Expand All @@ -277,7 +279,6 @@ def aggregate_per_slice_or_level(metric, mask=None, slices=[], levels=[], distan
# aggregation based on levels
vertgroups = None
if levels:
im_vert_level = Image(fname_vert_level).change_orientation('RPI')
# slicegroups = [(0, 1, 2), (3, 4, 5), (6, 7, 8)]
slicegroups = [tuple(get_slices_from_vertebral_levels(im_vert_level, level)) for level in levels]
# Intersection between specified slices and each element of slicegroups
Expand All @@ -301,6 +302,9 @@ def aggregate_per_slice_or_level(metric, mask=None, slices=[], levels=[], distan
if perslice:
# slicegroups = [(0,), (1,), (2,), (3,), (4,), (5,), (6,), (7,), (8,)]
slicegroups = [tuple([slice]) for slice in slices]
# vertgroups = [(2,), (2,), (2,), (3,), (3,), (3,), (4,), (4,), (4,)]
if fname_vert_level is not None:
vertgroups = [tuple([get_vertebral_level_from_slice(im_vert_level, i[0])]) for i in slicegroups]
else:
# slicegroups = [(0, 1, 2, 3, 4, 5, 6, 7, 8)]
slicegroups = [tuple(slices)]
Expand All @@ -324,6 +328,8 @@ def aggregate_per_slice_or_level(metric, mask=None, slices=[], levels=[], distan
agg_metric[slicegroup]['VertLevel'] = None
else:
agg_metric[slicegroup]['VertLevel'] = vertgroups[slicegroups.index(slicegroup)]
if agg_metric[slicegroup]['VertLevel'][0] is None:
agg_metric[slicegroup]['VertLevel'] = None
# Loop across functions (e.g.: MEAN, STD)
for (name, func) in group_funcs:
try:
Expand Down
16 changes: 10 additions & 6 deletions spinalcordtoolbox/scripts/sct_process_segmentation.py
Expand Up @@ -146,6 +146,7 @@ def get_parser():
'-vert',
metavar=Metavar.str,
type=parse_num_list,
default='',
help="Vertebral levels to compute the metrics across. Example: 2:9 for C2 to T2. If you also specify a range of "
"slices with flag `-z`, the intersection between the specified slices and vertebral levels will be "
"considered."
Expand Down Expand Up @@ -372,12 +373,15 @@ def main(argv: Sequence[str]):

file_out = os.path.abspath(arguments.o)
append = bool(arguments.append)
if arguments.vert is not None:
levels = arguments.vert
fname_vert_level = arguments.vertfile
else:
levels = []
fname_vert_level = None
levels = arguments.vert
fname_vert_level = arguments.vertfile
if not os.path.isfile(fname_vert_level):
logger.warning(f"Vertebral level file {fname_vert_level} does not exist. Vert level information will "
f"not be displayed. To use vertebral level information, you may need to run "
f"`sct_warp_template` to generate the appropriate level file in your working directory.")
fname_vert_level = None # Discard the default '-vertfile', so that we don't attempt to find vertebral levels
if levels:
raise FileNotFoundError("The vertebral level file must exist to use `-vert` to group by vertebral level.")
perlevel = bool(arguments.perlevel)
slices = arguments.z
perslice = bool(arguments.perslice)
Expand Down

0 comments on commit 2d09af3

Please sign in to comment.