Skip to content
This repository has been archived by the owner on Dec 20, 2024. It is now read-only.

ENH: Indexes sorting options #150

Merged
merged 47 commits into from
Apr 8, 2024
Merged

ENH: Indexes sorting options #150

merged 47 commits into from
Apr 8, 2024

Conversation

esavary
Copy link
Member

@esavary esavary commented Apr 2, 2024

Introduce iterators to rearrange the indexes used in logo_split. Sorting options include linear, random, in ascending order following gradient magnitude, and central symmetric.

closes #111

@esavary esavary requested a review from oesteban April 2, 2024 14:52
@esavary esavary changed the title ENH: Indexes sorting options ENH: Indexes sorting options -WIP Apr 2, 2024
@esavary esavary removed the request for review from oesteban April 2, 2024 14:57
@esavary esavary changed the title ENH: Indexes sorting options -WIP ENH: Indexes sorting options Apr 3, 2024
@esavary esavary requested review from oesteban and effigies April 3, 2024 09:19

"""

def handle_gradient_ordering():
Copy link
Collaborator

Choose a reason for hiding this comment

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

Re: #149 (comment)

All the methods here do not access any instance variable, and any IDE will suggest them becoming static, as they were prior to 8153649. However the actions dictionary was triggering errors because it was taking static methods. All this could be avoided if these methods were refactored into a utils.py (or similar) module that takes care of sorting the DWI volumes. In fact, (i) the estimator should not take care of this at all: it should receive the DWI volumes already sorted, and (ii) these methods may be useful elsewhere.

Related to this, the strategies could be put into an enum so that it becomes clear which are the sorting possibilities, and they are documented and available at a single place. However, I know that using enums is not very popular across many major scientific Python packages.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sounds like a great idea! I agree, I'll externalize the functions into a new module, util.py. Thanks for the suggestion!

@esavary esavary changed the title ENH: Indexes sorting options ENH: Indexes sorting options -WIP Apr 4, 2024
Copy link
Member

@oesteban oesteban left a comment

Choose a reason for hiding this comment

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

Some initial comments

src/eddymotion/utils.py Outdated Show resolved Hide resolved
Comment on lines 84 to 90
Parameters:
dwdata : :obj:`~eddymotion.dmri.DWI`
DWI dataset, represented by this tool's internal type.

Returns:
index_order : :obj:`numpy.ndarray`
The sorted index order.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Parameters:
dwdata : :obj:`~eddymotion.dmri.DWI`
DWI dataset, represented by this tool's internal type.
Returns:
index_order : :obj:`numpy.ndarray`
The sorted index order.
Parameters
----------
size : :obj:`int`
Number of volumes in dataset
(for instance, number of orientations in a DWI)
Returns
-------
:obj:`list` of :obj:`int`
The sorted index order.
Examples
--------
>>> linear_action(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

src/eddymotion/utils.py Outdated Show resolved Hide resolved
src/eddymotion/utils.py Outdated Show resolved Hide resolved
src/eddymotion/utils.py Outdated Show resolved Hide resolved
src/eddymotion/utils.py Show resolved Hide resolved
src/eddymotion/utils.py Outdated Show resolved Hide resolved
src/eddymotion/utils.py Outdated Show resolved Hide resolved
src/eddymotion/utils.py Outdated Show resolved Hide resolved
src/eddymotion/utils.py Outdated Show resolved Hide resolved
src/eddymotion/utils.py Show resolved Hide resolved
src/eddymotion/utils.py Outdated Show resolved Hide resolved
src/eddymotion/utils.py Outdated Show resolved Hide resolved
test/test_splitting.py Outdated Show resolved Hide resolved
test/test_model.py Outdated Show resolved Hide resolved
src/eddymotion/utils.py Outdated Show resolved Hide resolved
src/eddymotion/utils.py Outdated Show resolved Hide resolved
src/eddymotion/utils.py Outdated Show resolved Hide resolved
src/eddymotion/utils.py Outdated Show resolved Hide resolved
src/eddymotion/utils.py Outdated Show resolved Hide resolved
src/eddymotion/utils.py Outdated Show resolved Hide resolved
src/eddymotion/utils.py Outdated Show resolved Hide resolved

Examples
--------
>>> random_iterator(15, seed=0) # seed is 0
Copy link
Member Author

Choose a reason for hiding this comment

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

This test is failing on my computer locally. I'm getting a different value compared to yours @oesteban . Specifically, I'm getting [2, 11, 3, 10, 0, 4, 7, 5, 14, 12, 6, 9, 13, 8, 1]. Any idea why?

Copy link
Member

Choose a reason for hiding this comment

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

No idea - I might have copied the wrong order.

[12, 2, 7, 8, 14, 4, 9, 13, 6, 3, 5, 0, 1, 10, 11]
>>> random_iterator(15, seed=True) # seed is the default value 20210324
[13, 2, 8, 12, 10, 4, 9, 3, 0, 1, 14, 6, 11, 7, 5]
>>> random_iterator(15, seed=42) # seed is 42
Copy link
Member Author

Choose a reason for hiding this comment

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

Same here, I obtain [6, 7, 9, 3, 0, 11, 14, 10, 5, 2, 4, 12, 1, 13, 8].

Copy link
Member

@oesteban oesteban left a comment

Choose a reason for hiding this comment

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

Suggestions to avoid numpy for the time being. Also make the random range iterator a generator.

src/eddymotion/utils.py Outdated Show resolved Hide resolved
src/eddymotion/utils.py Outdated Show resolved Hide resolved
src/eddymotion/utils.py Outdated Show resolved Hide resolved
src/eddymotion/utils.py Outdated Show resolved Hide resolved
src/eddymotion/utils.py Outdated Show resolved Hide resolved
src/eddymotion/utils.py Outdated Show resolved Hide resolved
@oesteban
Copy link
Member

oesteban commented Apr 8, 2024

Suggestions to avoid numpy for the time being. Also make the random range iterator a generator.

These for sure will make the test values invalid - we'll need to re-write them.

@esavary
Copy link
Member Author

esavary commented Apr 8, 2024

Suggestions to avoid numpy for the time being. Also make the random range iterator a generator.

These for sure will make the test values invalid - we'll need to re-write them.

I changed them in ed20ca8

@esavary esavary requested a review from oesteban April 8, 2024 12:30
Copy link
Member

@oesteban oesteban left a comment

Choose a reason for hiding this comment

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

Looking good, I just have a few suggestions to improve the docstrings.

If you want to make this PR perfect, I'd go for type annotations on the four functions involved.

In the meantime, perhaps open a separate PR (make sure to branch from master, not from here) addressing #151? That PR should be fairly fast to merge and then we can update this one to resolve the ruff complaints.

src/eddymotion/utils.py Outdated Show resolved Hide resolved
src/eddymotion/utils.py Outdated Show resolved Hide resolved
src/eddymotion/utils.py Outdated Show resolved Hide resolved
src/eddymotion/utils.py Outdated Show resolved Hide resolved
src/eddymotion/utils.py Outdated Show resolved Hide resolved
src/eddymotion/utils.py Outdated Show resolved Hide resolved
Copy link
Member

@oesteban oesteban left a comment

Choose a reason for hiding this comment

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

The assistance tool for adding type annotations has introduced some problems (moved the imports to the top, before the license banner) and modified some docstrings that were better before.

src/eddymotion/utils.py Outdated Show resolved Hide resolved
src/eddymotion/utils.py Outdated Show resolved Hide resolved
src/eddymotion/utils.py Outdated Show resolved Hide resolved
src/eddymotion/utils.py Outdated Show resolved Hide resolved
src/eddymotion/utils.py Outdated Show resolved Hide resolved
src/eddymotion/utils.py Outdated Show resolved Hide resolved
src/eddymotion/utils.py Outdated Show resolved Hide resolved
src/eddymotion/utils.py Outdated Show resolved Hide resolved
Comment on lines 25 to 27
import random
from itertools import chain, zip_longest

Copy link
Member

Choose a reason for hiding this comment

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

Revert the removal of imports and add the Generation import here:

Suggested change
import random
from itertools import chain, zip_longest
import random
from itertools import chain, zip_longest
from typing import Generator

Comment on lines 1 to 5
import random
from itertools import chain, zip_longest
from typing import Generator

# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
Copy link
Member

Choose a reason for hiding this comment

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

Please, be careful, why these four lines here before the header?

Suggested change
import random
from itertools import chain, zip_longest
from typing import Generator
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-

Copy link
Member Author

Choose a reason for hiding this comment

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

I fixed the imports in 9bda539.

Comment on lines 1 to 27
import random
from itertools import chain, zip_longest
from typing import Generator

# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
#
# Copyright 2024 The NiPreps Developers <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# We support and encourage derived works from this project, please read
# about our expectations at
#
# https://www.nipreps.org/community/licensing/
#
"""Iterators to traverse the volumes in a 4D image."""
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
import random
from itertools import chain, zip_longest
from typing import Generator
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
#
# Copyright 2024 The NiPreps Developers <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# We support and encourage derived works from this project, please read
# about our expectations at
#
# https://www.nipreps.org/community/licensing/
#
"""Iterators to traverse the volumes in a 4D image."""
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
#
# Copyright 2024 The NiPreps Developers <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# We support and encourage derived works from this project, please read
# about our expectations at
#
# https://www.nipreps.org/community/licensing/
#
"""Iterators to traverse the volumes in a 4D image."""
import random
from itertools import chain, zip_longest
from typing import Iterator

src/eddymotion/utils.py Outdated Show resolved Hide resolved
src/eddymotion/utils.py Outdated Show resolved Hide resolved
src/eddymotion/utils.py Outdated Show resolved Hide resolved
src/eddymotion/utils.py Outdated Show resolved Hide resolved
src/eddymotion/utils.py Outdated Show resolved Hide resolved
@oesteban
Copy link
Member

oesteban commented Apr 8, 2024

Let's re-sync with main:

  1. Check you have the main repo as a remote:
    git remote -v
    
    And if not, add it:
    git remote add upstream [email protected]:nipreps/eddymotion.git
    
  2. Fetch the latest:
    git fetch upstream
    
  3. Merge main into this branch:
    git merge upstream/main
    

@esavary esavary requested a review from oesteban April 8, 2024 14:28
@oesteban oesteban merged commit 6128b37 into nipreps:main Apr 8, 2024
7 of 8 checks passed
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

LOGO split - strategies other than random to traverse all orientations
3 participants