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

➕ Reorient resources to set orientation in config #2140

Merged
merged 39 commits into from
Oct 14, 2024

Conversation

birajstha
Copy link
Contributor

@birajstha birajstha commented Aug 16, 2024

Related to

Fixes #2138 by @sgiavasis

Description

This PR fixes the different orientation in CPAC output dir that were caused by the different orientation of templates being ingressed.

  • Added reorientation to the templates that are being ingressed and /or resampled (for different resolution) in CPAC.
  • Added desired_orientation as config key in blank-config and set it default to RPI
    With this PR the desired orientation of the outputs can be selected by modifying this key in the config.

Technical details

First of all,
The reorientation of templates are done as below
check_templates_orientation

  • resolve_resolution function is modified to take in orientation as extra input.
  • Orientation inputs are taken from the config and are added in the following spots
  1. resolve_resolution
  2. freesurfer_fs_brain_connector
  3. anatomical_init_T1
  4. anatomical_init_T2
  5. func_reorient

Tests

For testing you can run this branch of CPAC on one test subject / session with your config (set as desired_orientation) and check the orientations of the images in the output directory both anat and func

  1. cd into the anat or func directory.
  2. run for file in *.nii.gz; do orientation=$(3dinfo -orient "$file" 2>/dev/null); echo "$file: $orientation"; done

You should see the filename with orientations as below
anat :
anat

func :
func

Screenshots

Problem: Different orientations in the CPAC output dir

  1. anat dir:
    p_anat

  2. func dir:
    p_func

After fix:

  1. anat dir:
    anat

  2. func dir:
    func

Checklist

  • My pull request has a descriptive title (not a vague title like Update index.md).
  • My pull request targets the develop branch of the repository.
  • My commit messages follow best practices.
  • My code follows the established code style of the repository.
  • I added tests for the changes I made (if applicable).
  • I updated the changelog.
  • I added or updated documentation (if applicable).
  • I tried running the project locally and verified that there are no visible errors.

Developer Certificate of Origin

Developer Certificate of Origin
Developer Certificate of Origin
Version 1.1

Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
1 Letterman Drive
Suite D4700
San Francisco, CA, 94129

Everyone is permitted to copy and distribute verbatim copies of this
license document, but changing it is not allowed.


Developer's Certificate of Origin 1.1

By making a contribution to this project, I certify that:

(a) The contribution was created in whole or in part by me and I
    have the right to submit it under the open source license
    indicated in the file; or

(b) The contribution is based upon previous work that, to the best
    of my knowledge, is covered under an appropriate open source
    license and I have the right under that license to submit that
    work with modifications, whether created in whole or in part
    by me, under the same open source license (unless I am
    permitted to submit under a different license), as indicated
    in the file; or

(c) The contribution was provided directly to me by some other
    person who certified (a), (b) or (c) and I have not modified
    it.

(d) I understand and agree that this project and the contribution
    are public and that a record of the contribution (including all
    personal information I submit with it, including my sign-off) is
    maintained indefinitely and may be redistributed consistent with
    this project or the open source license(s) involved.

Copy link
Member

@shnizzedy shnizzedy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is my only substantive question & suggestion.


The rest is style that the ruff-format hook in pre-commit should have caught.

(I cherry-pick'd these changes locally and ran pre-commit run on them to catch these after I noticed a couple. Of

the errors that ruff found

CPAC/pipeline/engine.py:1:1: D100 Missing docstring in public module
CPAC/pipeline/engine.py:73:7: D101 Missing docstring in public class
CPAC/pipeline/engine.py:136:9: D105 Missing docstring in magic method
CPAC/pipeline/engine.py:144:9: D105 Missing docstring in magic method
CPAC/pipeline/engine.py:149:9: D102 Missing docstring in public method
CPAC/pipeline/engine.py:197:9: D102 Missing docstring in public method
CPAC/pipeline/engine.py:200:9: D102 Missing docstring in public method
CPAC/pipeline/engine.py:208:9: D102 Missing docstring in public method
CPAC/pipeline/engine.py:211:9: D102 Missing docstring in public method
CPAC/pipeline/engine.py:214:9: D102 Missing docstring in public method
CPAC/pipeline/engine.py:217:9: D102 Missing docstring in public method
CPAC/pipeline/engine.py:220:9: D102 Missing docstring in public method
CPAC/pipeline/engine.py:223:9: D102 Missing docstring in public method
CPAC/pipeline/engine.py:240:9: D102 Missing docstring in public method
CPAC/pipeline/engine.py:257:9: D102 Missing docstring in public method
CPAC/pipeline/engine.py:270:9: D102 Missing docstring in public method
CPAC/pipeline/engine.py:278:9: D102 Missing docstring in public method
CPAC/pipeline/engine.py:328:9: D102 Missing docstring in public method
CPAC/pipeline/engine.py:396:9: D102 Missing docstring in public method
CPAC/pipeline/engine.py:437:9: D102 Missing docstring in public method
CPAC/pipeline/engine.py:455:9: D102 Missing docstring in public method
CPAC/pipeline/engine.py:462:9: D102 Missing docstring in public method
CPAC/pipeline/engine.py:466:9: D102 Missing docstring in public method
CPAC/pipeline/engine.py:469:9: D102 Missing docstring in public method
CPAC/pipeline/engine.py:491:9: D102 Missing docstring in public method
CPAC/pipeline/engine.py:504:9: D102 Missing docstring in public method
CPAC/pipeline/engine.py:519:9: D102 Missing docstring in public method
CPAC/pipeline/engine.py:529:9: D102 Missing docstring in public method
CPAC/pipeline/engine.py:549:9: D102 Missing docstring in public method
CPAC/pipeline/engine.py:562:9: PLR0912 Too many branches (69 > 50)
CPAC/pipeline/engine.py:562:9: PLR0915 Too many statements (155 > 100)
CPAC/pipeline/engine.py:562:9: D102 Missing docstring in public method
CPAC/pipeline/engine.py:814:9: D102 Missing docstring in public method
CPAC/pipeline/engine.py:930:9: D102 Missing docstring in public method
CPAC/pipeline/engine.py:1073:9: PLR0912 Too many branches (69 > 50)
CPAC/pipeline/engine.py:1073:9: PLR0915 Too many statements (171 > 100)
CPAC/pipeline/engine.py:1073:9: D102 Missing docstring in public method
CPAC/pipeline/engine.py:1073:37: A002 Argument `all` is shadowing a Python builtin
CPAC/pipeline/engine.py:1420:7: D101 Missing docstring in public class
CPAC/pipeline/engine.py:1431:17: PLW2901 `for` loop variable `node_block_function` overwritten by assignment target
CPAC/pipeline/engine.py:1492:9: D102 Missing docstring in public method
CPAC/pipeline/engine.py:1495:9: D102 Missing docstring in public method
CPAC/pipeline/engine.py:1500:9: D102 Missing docstring in public method
CPAC/pipeline/engine.py:1509:9: D102 Missing docstring in public method
CPAC/pipeline/engine.py:1519:9: PLR0912 Too many branches (91 > 50)
CPAC/pipeline/engine.py:1519:9: PLR0915 Too many statements (186 > 100)
CPAC/pipeline/engine.py:1519:9: D102 Missing docstring in public method
CPAC/pipeline/engine.py:1831:37: PLW2901 `for` loop variable `connection` overwritten by assignment target
CPAC/pipeline/engine.py:1910:5: D103 Missing docstring in public function
CPAC/pipeline/engine.py:1955:5: D103 Missing docstring in public function
CPAC/pipeline/engine.py:2047:5: D103 Missing docstring in public function
CPAC/pipeline/engine.py:2093:5: D103 Missing docstring in public function
CPAC/pipeline/engine.py:2216:17: E722 Do not use bare `except`
CPAC/pipeline/engine.py:2240:5: D103 Missing docstring in public function
CPAC/pipeline/engine.py:2315:5: D103 Missing docstring in public function
CPAC/pipeline/engine.py:2383:5: D103 Missing docstring in public function
CPAC/pipeline/engine.py:2391:5: D103 Missing docstring in public function
CPAC/pipeline/engine.py:2411:5: D103 Missing docstring in public function
CPAC/pipeline/engine.py:2680:9: E722 Do not use bare `except`
CPAC/pipeline/engine.py:2697:5: D103 Missing docstring in public function
CPAC/pipeline/utils.py:29:5: D103 Missing docstring in public function
CPAC/pipeline/utils.py:39:12: RET504 Unnecessary assignment to `orientation` before `return` statement
CPAC/pipeline/utils.py:42:5: D103 Missing docstring in public function
Found 63 errors.

I'd add docstrings for

CPAC/pipeline/utils.py:29:5: D103 Missing docstring in public function
CPAC/pipeline/utils.py:42:5: D103 Missing docstring in public function

and change

orientation = subprocess.run(cmd_3dinfo, capture_output=True, text=True).stdout.strip().upper()
return orientation
to

    return subprocess.run(cmd_3dinfo, capture_output=True, text=True).stdout.strip().upper()

(or

    orientation = subprocess.run(cmd_3dinfo, capture_output=True, text=True).stdout.strip().upper() # noqa: RET504
    return orientation

to mark the style violation as intentional if you think it's that much more readable) for

CPAC/pipeline/utils.py:39:12: RET504 Unnecessary assignment to `orientation` before `return` statement

. Once all the remaining ruff violations are outside of the scope of the changes of this PR, I'd git commit --no-verify to accept those style violations as acceptable for now.)

CPAC/pipeline/engine.py Outdated Show resolved Hide resolved
CPAC/pipeline/engine.py Outdated Show resolved Hide resolved
CPAC/pipeline/engine.py Outdated Show resolved Hide resolved
CPAC/pipeline/engine.py Outdated Show resolved Hide resolved
CPAC/pipeline/engine.py Outdated Show resolved Hide resolved
CPAC/pipeline/utils.py Outdated Show resolved Hide resolved
CPAC/pipeline/utils.py Outdated Show resolved Hide resolved
CPAC/pipeline/utils.py Outdated Show resolved Hide resolved
CPAC/pipeline/utils.py Outdated Show resolved Hide resolved
CPAC/pipeline/utils.py Outdated Show resolved Hide resolved
@birajstha birajstha linked an issue Sep 20, 2024 that may be closed by this pull request
1 task
@birajstha birajstha changed the title ➕ orientation checks for resources added to PipeConfigs ➕ orientation checks for resources Sep 26, 2024
@birajstha birajstha changed the title ➕ orientation checks for resources ➕ Reorient resources to set orientation in config Sep 27, 2024
Copy link
Member

@shnizzedy shnizzedy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To fix:

The SSOTing by functionalizing the creation of resample nodes is totally optional, just a suggestion.

CPAC/pipeline/engine.py Outdated Show resolved Hide resolved
CPAC/utils/datasource.py Show resolved Hide resolved
CPAC/pipeline/engine.py Outdated Show resolved Hide resolved
CPAC/pipeline/engine.py Outdated Show resolved Hide resolved
CPAC/pipeline/engine.py Outdated Show resolved Hide resolved
CPAC/pipeline/engine.py Outdated Show resolved Hide resolved
CPAC/pipeline/engine.py Show resolved Hide resolved
CPAC/pipeline/engine.py Show resolved Hide resolved
CPAC/utils/datasource.py Outdated Show resolved Hide resolved
@birajstha birajstha force-pushed the feature/check_orientations branch 3 times, most recently from c9e71f2 to 40ea22c Compare October 1, 2024 18:42
@birajstha birajstha self-assigned this Oct 2, 2024
Copy link
Member

@shnizzedy shnizzedy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good, and all the tests are passing (including the smoke tests I manually triggered to check that all the preconfigs build with these changes)!

I have a few suggestions and a question for the CHANGELOG, and one more request to fix a style violation, but otherwise I'm happy with this fix. Nice work!

CHANGELOG.md Outdated Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
CPAC/resources/tests/test_templates.py Outdated Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
Copy link
Member

@shnizzedy shnizzedy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

@sgiavasis sgiavasis merged commit cda28cd into develop Oct 14, 2024
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

✨ Nifti header orientation checks and decisions for downstream resources
3 participants