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

Keyword=CombinedLabels incompatible with combine=1 #4467

Closed
jcohenadad opened this issue May 3, 2024 · 0 comments · Fixed by #4469
Closed

Keyword=CombinedLabels incompatible with combine=1 #4467

jcohenadad opened this issue May 3, 2024 · 0 comments · Fixed by #4469
Assignees
Labels
bug category: fixes an error in the code sct_extract_metric context:
Milestone

Comments

@jcohenadad
Copy link
Member

Context

When running sct_extract_metric with a combination the flag -l that points to a label Keyword=CombinedLabels, i.e., with values 50 to 55, while using the flag -combine 1, the script crashes:

/results_20240503_134220/data_processed/sub-BB277/dwi $  /usr/bin/env /Users/julien/code/spinalcordtoolbox/python/bin/python /Users/julien/.vscode/extensions/ms-python.python-2024.4.1/python_files/lib/python/debugpy/adapter/../../debugpy/launcher 50935 -- /Users/julien/code/spinalcordtoolbox/spinalcordtoolbox/scripts/sct_extract_metric.py -i sub-BB277_chunk-1_DWI_moco_FA.nii.gz -f label_sub-BB277_chunk-1_DWI_moco/atlas -l 52 -combine 1 -vert 2:12 -vertfile label_sub-BB277_chunk-1_DWI_moco/template/PAM50_levels.nii.gz -o /Users/julien/code/spine-park/results_20240503_134220/results/DWI_FA_52.csv 

--
Spinal Cord Toolbox (git-jca/extract-metric-combine-72ff27030efda92431ee831b45f31e2858b8552d)

sct_extract_metric -i sub-BB277_chunk-1_DWI_moco_FA.nii.gz -f label_sub-BB277_chunk-1_DWI_moco/atlas -l 52 -combine 1 -vert 2:12 -vertfile label_sub-BB277_chunk-1_DWI_moco/template/PAM50_levels.nii.gz -o /Users/julien/code/spine-park/results_20240503_134220/results/DWI_FA_52.csv
--


Load metric image...
Estimation for label: 52
Traceback (most recent call last):
  File "/Users/julien/code/spinalcordtoolbox/spinalcordtoolbox/scripts/sct_extract_metric.py", line 390, in <module>
    main(sys.argv[1:])
  File "/Users/julien/code/spinalcordtoolbox/spinalcordtoolbox/scripts/sct_extract_metric.py", line 379, in main
    agg_metric = extract_metric(data, labels=labels, slices=slices, levels=levels, perslice=perslice,
  File "/Users/julien/code/spinalcordtoolbox/spinalcordtoolbox/aggregate_slicewise.py", line 440, in extract_metric
    labels_sum = np.sum(labels[..., label_struc[id_label].id], axis=labels.ndim-1)  # (nx, ny, nz, 1)
IndexError: index 52 is out of bounds for axis 3 with size 37

While when setting combine 0, the script runs:

julien-macbook:~/code/spine-park/results_20240503_134220/data_processed/sub-BB277/dwi $  cd /Users/julien/code/spine-park/results_20240503_134220/data_processed/sub-BB277/dwi ; /usr/bin/env /Users/julien/code/spinalcordtoolbox/python/bin/python /Users/julien/.vscode/extensions/ms-python.python-2024.4.1/python_files/lib/python/debugpy/adapter/../../debugpy/launcher 50940 -- /Users/julien/code/spinalcordtoolbox/spinalcordtoolbox/scripts/sct_extract_metric.py -i sub-BB277_chunk-1_DWI_moco_FA.nii.gz -f label_sub-BB277_chunk-1_DWI_moco/atlas -l 52 -combine 0 -vert 2:12 -vertfile label_sub-BB277_chunk-1_DWI_moco/template/PAM50_levels.nii.gz -o /Users/julien/code/spine-park/results_20240503_134220/results/DWI_FA_52.csv 

--
Spinal Cord Toolbox (git-jca/extract-metric-combine-72ff27030efda92431ee831b45f31e2858b8552d)

sct_extract_metric -i sub-BB277_chunk-1_DWI_moco_FA.nii.gz -f label_sub-BB277_chunk-1_DWI_moco/atlas -l 52 -combine 0 -vert 2:12 -vertfile label_sub-BB277_chunk-1_DWI_moco/template/PAM50_levels.nii.gz -o /Users/julien/code/spine-park/results_20240503_134220/results/DWI_FA_52.csv
--


Load metric image...
Estimation for label: gray matter

Done! To view results, type:
open /Users/julien/code/spine-park/results_20240503_134220/results/DWI_FA_52.csv

Although it does not really make sense to combine tracts that are already combined, this crash is not intuitive for the user.

Solution

Deal with the condition 'CombinedLabel' & combine=1.

@jcohenadad jcohenadad added bug category: fixes an error in the code sct_extract_metric context: labels May 3, 2024
@jcohenadad jcohenadad added this to the 6.4 milestone May 3, 2024
@jcohenadad jcohenadad self-assigned this May 3, 2024
@jcohenadad jcohenadad changed the title CombinedLabel incompatible with combine=1 Keyword=CombinedLabels incompatible with combine=1 May 3, 2024
jcohenadad added a commit that referenced this issue May 5, 2024
Fixes #4467

<!-- Hi, and thank you for submitting a Pull Request! The checklist
below is a brief summary of steps found in the NeuroPoly Contributing
Guidelines, which can be found here:
https://intranet.neuro.polymtl.ca/onboarding/software-development.html#software-development.
-->

## Checklist

#### GitHub

- [x] I've given this PR a concise, self-descriptive, and meaningful
title
- [x] I've linked relevant issues in the PR body
- [x] I've applied [the relevant
labels](https://intranet.neuro.polymtl.ca/onboarding/software-development.html#pr-labels)
to this PR
- [x] I've applied a [release
milestone](https://github.com/spinalcordtoolbox/spinalcordtoolbox/milestones)
(major, minor, patch) in line with [Semantic Versioning
guidelines](https://github.com/spinalcordtoolbox/spinalcordtoolbox/wiki/Misc%3A-Creating-a-new-release#convention-for-naming-releases)
- [ ] I've assigned a reviewer

<!-- For the title, please observe the following rules:
	- Provide a concise and self-descriptive title
- Do not include the applicable issue number in the title, do it in the
PR body
	- If the PR is not ready for review, convert it to a draft.
-->

#### PR contents

- [ ] I've consulted [SCT's internal developer
documentation](https://github.com/spinalcordtoolbox/spinalcordtoolbox/wiki)
to ensure my contribution is in line with any relevant design decisions
- [ ] I've added [relevant
tests](https://github.com/spinalcordtoolbox/spinalcordtoolbox/wiki/Testing)
for my contribution nope 😢 (see #4468)
- [ ] I've updated the [relevant
documentation](https://github.com/spinalcordtoolbox/spinalcordtoolbox/wiki/Documentation)
for my changes, including argparse descriptions, docstrings, and
ReadTheDocs tutorial pages

## Description
<!-- describe what the PR is about. Explain the approach and possible
drawbacks.It's ok to repeat some text from the related issue. -->

I'm having issues creating a test for it
(#4468), we
should add the following tests for `test_cli_sct_extract_metric.py`:

- Labels under `Keyword=IndivLabels`: `-l 2,3 -combine 1` --> it should
combine 2,3 into a single label
- Labels under `Keyword=IndivLabels`: `-l 2 -combine 1` --> it should
not crash, and extract in label 2
- Labels under `Keyword=CombinedLabels`: `-l 52 -combine 1` --> it
should not crash, and extract in label 52 (the test I was trying to
make)
- Labels under `Keyword=CombinedLabels`: `-l 51,52 -combine 1` --> Now
that's an interesting one. I'm now sure what will happen in the code to
be honest 😅 but ideally we would produce a label (99) that combines all
tracts within 51 and 52 (and deal with the redundancy)
 

## Linked issues
<!-- If the PR fixes any issues, indicate it here with issue-closing
keywords: e.g. Resolves #XX, Fixes #XX, Addresses #XX. Note that if you
want multiple issues to be autoclosed on PR merge, you must use the
issue-closing verb before each relevant issue: e.g. Resolves #1,
Resolves #2 -->

---------

Co-authored-by: Joshua Newton <joshuacwnewton@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug category: fixes an error in the code sct_extract_metric context:
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant