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
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
3343f34
enh: externalize sorting functions
esavary Apr 4, 2024
1e305d9
fix:import statement
esavary Apr 4, 2024
6a8a585
sty: fix style error
esavary Apr 4, 2024
4e199a9
sty: fix style error
esavary Apr 4, 2024
b13c975
sty: fix style errors
esavary Apr 4, 2024
148bb71
Apply suggestions from code review
esavary Apr 5, 2024
5155f9f
fix: revert stylistic changes
esavary Apr 5, 2024
9420ce5
fix: revert stylistic changes
esavary Apr 5, 2024
3d7afb1
fix: revert stylistic changes
esavary Apr 5, 2024
69611fa
Update src/eddymotion/utils.py
esavary Apr 5, 2024
31fa9d6
Update src/eddymotion/utils.py
esavary Apr 5, 2024
6320f66
Update src/eddymotion/utils.py
esavary Apr 5, 2024
4998afa
fix: revert changes in estimator.py
esavary Apr 5, 2024
a424582
Apply suggestions from code review
esavary Apr 5, 2024
88bea1d
fix: typos
esavary Apr 5, 2024
b58a708
enh: update args and test for bvalue_action
esavary Apr 5, 2024
4366683
fix: docstring
esavary Apr 5, 2024
ee95877
fix: remove unused import
esavary Apr 5, 2024
e84bddd
Apply suggestions from code review
esavary Apr 5, 2024
3c0b36d
fix: add link for new module documentation
esavary Apr 5, 2024
22bbb68
fix: add exeption
esavary Apr 5, 2024
43fe703
fix: bvalue_action implementation + typos
esavary Apr 5, 2024
11f52a4
sty: remove white space
esavary Apr 5, 2024
cdfe20a
fix: typos
esavary Apr 5, 2024
f858a6a
sty: remove white space
esavary Apr 5, 2024
90cf31c
Apply suggestions from code review
esavary Apr 8, 2024
fa43376
sty: change iterator names
esavary Apr 8, 2024
cdeb34f
sty: fix docstring
esavary Apr 8, 2024
965b3c5
Apply suggestions from code review
esavary Apr 8, 2024
a43fdde
fix: random seed and add test
esavary Apr 8, 2024
ae19816
fix: typos
esavary Apr 8, 2024
d78c500
enh: generalize docstring
esavary Apr 8, 2024
6aaa7d4
Update src/eddymotion/utils.py
esavary Apr 8, 2024
4e7a418
Apply suggestions from code review
esavary Apr 8, 2024
4163b0b
Apply suggestions from code review
esavary Apr 8, 2024
ed20ca8
fix: random generator test results
esavary Apr 8, 2024
d966c97
Apply suggestions from code review
esavary Apr 8, 2024
0057079
sty: add typing
esavary Apr 8, 2024
6f0d5fe
fix: remove unused import
esavary Apr 8, 2024
2cc923a
Apply suggestions from code review
esavary Apr 8, 2024
9bda539
fix: import position
esavary Apr 8, 2024
d1c9846
Apply suggestions from code review
esavary Apr 8, 2024
807a468
fix: iterator typing in docstring
esavary Apr 8, 2024
fee865d
sty: fix ruff errors
esavary Apr 8, 2024
5f0837d
Merge branch 'nipreps:main' into index-sorting
esavary Apr 8, 2024
caa72a5
sty: fix sty errors
esavary Apr 8, 2024
911a730
sty: fix sty errors
esavary Apr 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/developers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ Information on specific functions, classes, and methods.
api/eddymotion.data.dmri
api/eddymotion.estimator
api/eddymotion.model
api/eddymotion.utils
api/eddymotion.viz
163 changes: 163 additions & 0 deletions src/eddymotion/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
#
# Copyright 2022 The NiPreps Developers <[email protected]>
esavary marked this conversation as resolved.
Show resolved Hide resolved
#
# 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/
#
"""Utils to sort the DWI data volume indices"""
esavary marked this conversation as resolved.
Show resolved Hide resolved

import numpy as np
esavary marked this conversation as resolved.
Show resolved Hide resolved
esavary marked this conversation as resolved.
Show resolved Hide resolved
from itertools import chain, zip_longest


oesteban marked this conversation as resolved.
Show resolved Hide resolved
def linear_iterator(size=None, **kwargs):
"""
Sort the DWI data volume indices linearly
esavary marked this conversation as resolved.
Show resolved Hide resolved

Parameters
----------
size : :obj:`int`
Number of volumes in the dataset
(for instance, the number of orientations in a DWI)

Returns
-------
:obj:`~typing.Generator`
The sorted index order.

Examples
--------
>>> list(linear_iterator(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

"""
if size is None and 'bvals' in kwargs:
size = len(kwargs['bvals'])
if size is None:
raise TypeError("Cannot build iterator without size")

return range(size)
esavary marked this conversation as resolved.
Show resolved Hide resolved


def random_iterator(size=None, **kwargs):
"""Sort the DWI data volume indices.
esavary marked this conversation as resolved.
Show resolved Hide resolved

Parameters
----------
size : :obj:`int`
Number of volumes in the dataset
(for instance, the number of orientations in a DWI)
test_seed : :obj:`bool`
If True, return the seed value used for the random number generator.
esavary marked this conversation as resolved.
Show resolved Hide resolved

Returns
-------
:obj:`~typing.Generator`
The sorted index order.
oesteban marked this conversation as resolved.
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].

esavary marked this conversation as resolved.
Show resolved Hide resolved
[0, 13, 8, 4, 7, 2, 3, 5, 10, 1, 12, 6, 11, 14, 9]

"""

if size is None and 'bvals' in kwargs:
size = len(kwargs['bvals'])
if size is None:
raise TypeError("Cannot build iterator without size")

_seed = kwargs.get('seed', None)
_seed = 20210324 if _seed is True else _seed

rng = np.random.default_rng(
None if _seed is False else _seed,
)
esavary marked this conversation as resolved.
Show resolved Hide resolved

index_order = np.arange(size)
rng.shuffle(index_order)
esavary marked this conversation as resolved.
Show resolved Hide resolved
return index_order.tolist()
esavary marked this conversation as resolved.
Show resolved Hide resolved


def bvalue_iterator(size=None, **kwargs):
"""
Sort the DWI data volume indices in ascending order based on the last column of gradients.
esavary marked this conversation as resolved.
Show resolved Hide resolved

Parameters
----------
bvalues : :obj:`list`
List of b-values corresponding to all orientations of the dataset.

Returns
-------
:obj:`~typing.Generator`
The sorted index order.

Examples
--------
>>> list(bvalue_iterator(bvals=[0.0, 0.0, 1000.0, 1000.0, 700.0, 700.0, 2000.0, 2000.0, 0.0]))
[0, 1, 8, 4, 5, 2, 3, 6, 7]

"""
bvals = kwargs.get('bvals', None)
if bvals is None:
raise TypeError('Keyword argument bvals is required')
indexed_bvals = sorted([(round(b, 2), i) for i, b in enumerate(bvals)])
return (index[1] for index in indexed_bvals)


def centralsym_iterator(size=None, **kwargs):
"""
Sort the DWI data volume indices in a central symmetric manner.
esavary marked this conversation as resolved.
Show resolved Hide resolved

Parameters
----------
size : :obj:`int`
Number of volumes in the dataset
(for instance, the number of orientations in a DWI)

Returns
-------
:obj:`~typing.Generator`
The sorted index order.

Examples
--------
>>> list(centralsym_iterator(10))
[5, 4, 6, 3, 7, 2, 8, 1, 9, 0]
>>> list(centralsym_iterator(11))
[5, 4, 6, 3, 7, 2, 8, 1, 9, 0, 10]

esavary marked this conversation as resolved.
Show resolved Hide resolved
"""
if size is None and 'bvals' in kwargs:
size = len(kwargs['bvals'])
if size is None:
raise TypeError("Cannot build iterator without size")
linear = list(range(size))
esavary marked this conversation as resolved.
Show resolved Hide resolved
return (
x for x in chain.from_iterable(zip_longest(
linear[size // 2:],
reversed(linear[:size // 2]),
))
if x is not None
)
Loading