Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display vertebral levels when provided with -perslice 1 #4004

Merged
merged 9 commits into from Jan 24, 2023
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')
sandrinebedard marked this conversation as resolved.
Show resolved Hide resolved

# 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]
sandrinebedard marked this conversation as resolved.
Show resolved Hide resolved
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
joshuacwnewton marked this conversation as resolved.
Show resolved Hide resolved
# 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.")
sandrinebedard marked this conversation as resolved.
Show resolved Hide resolved
perlevel = bool(arguments.perlevel)
slices = arguments.z
perslice = bool(arguments.perslice)
Expand Down