This repository has been archived by the owner on Dec 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #98 from teresamg/logosplit
ENH: Outsource leave-one-out splitter so it can be used across data types
- Loading branch information
Showing
5 changed files
with
154 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# 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]> | ||
# | ||
# 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/ | ||
# | ||
"""Data splitting helpers.""" | ||
from pathlib import Path | ||
import numpy as np | ||
import h5py | ||
|
||
|
||
def lovo_split(dataset, index, with_b0=False): | ||
""" | ||
Produce one fold of LOVO (leave-one-volume-out). | ||
Parameters | ||
---------- | ||
dataset : :obj:`eddymotion.data.dmri.DWI` | ||
DWI object | ||
index : :obj:`int` | ||
Index of the DWI orientation to be left out in this fold. | ||
Returns | ||
------- | ||
(train_data, train_gradients) : :obj:`tuple` | ||
Training DWI and corresponding gradients. | ||
Training data/gradients come **from the updated dataset**. | ||
(test_data, test_gradients) :obj:`tuple` | ||
Test 3D map (one DWI orientation) and corresponding b-vector/value. | ||
The test data/gradient come **from the original dataset**. | ||
""" | ||
|
||
if not Path(dataset.get_filename()).exists(): | ||
dataset.to_filename(dataset.get_filename()) | ||
|
||
# read original DWI data & b-vector | ||
with h5py.File(dataset.get_filename(), "r") as in_file: | ||
root = in_file["/0"] | ||
data = np.asanyarray(root["dataobj"]) | ||
gradients = np.asanyarray(root["gradients"]) | ||
|
||
# if the size of the mask does not match data, cache is stale | ||
mask = np.zeros(data.shape[-1], dtype=bool) | ||
mask[index] = True | ||
|
||
train_data = data[..., ~mask] | ||
train_gradients = gradients[..., ~mask] | ||
test_data = data[..., mask] | ||
test_gradients = gradients[..., mask] | ||
|
||
if with_b0: | ||
train_data = np.concatenate( | ||
(np.asanyarray(dataset.bzero)[..., np.newaxis], train_data), | ||
axis=-1, | ||
) | ||
b0vec = np.zeros((4, 1)) | ||
b0vec[0, 0] = 1 | ||
train_gradients = np.concatenate( | ||
(b0vec, train_gradients), | ||
axis=-1, | ||
) | ||
|
||
return ( | ||
(train_data, train_gradients), | ||
(test_data, test_gradients), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*- | ||
# vi: set ft=python sts=4 ts=4 sw=4 et: | ||
# | ||
# Copyright 2021 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/ | ||
# | ||
"""Unit test testing the lovo_split function.""" | ||
import numpy as np | ||
from eddymotion.data.dmri import DWI | ||
from eddymotion.data.splitting import lovo_split | ||
|
||
|
||
def test_lovo_split(datadir): | ||
""" | ||
Test the lovo_split function. | ||
Parameters: | ||
- datadir: The directory containing the test data. | ||
Returns: | ||
None | ||
""" | ||
data = DWI.from_filename(datadir / "dwi.h5") | ||
|
||
# Set zeros in dataobj and gradients of the dwi object | ||
data.dataobj[:] = 0 | ||
data.gradients[:] = 0 | ||
|
||
# Select a random index | ||
index = np.random.randint(len(data)) | ||
|
||
# Set 1 in dataobj and gradients of the dwi object at this specific index | ||
data.dataobj[..., index] = 1 | ||
data.gradients[..., index] = 1 | ||
|
||
# Apply the lovo_split function at the specified index | ||
(train_data, train_gradients), \ | ||
(test_data, test_gradients) = lovo_split(data, index) | ||
|
||
# Check if the test data contains only 1s | ||
# and the train data contains only 0s after the split | ||
assert np.all(test_data == 1) | ||
assert np.all(train_data == 0) | ||
assert np.all(test_gradients == 1) | ||
assert np.all(train_gradients == 0) | ||
|