nifti2bids 0.0.9__tar.gz → 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of nifti2bids might be problematic. Click here for more details.

Files changed (27) hide show
  1. {nifti2bids-0.0.9 → nifti2bids-0.1.0}/PKG-INFO +2 -2
  2. {nifti2bids-0.0.9 → nifti2bids-0.1.0}/README.md +1 -1
  3. nifti2bids-0.1.0/nifti2bids/__init__.py +20 -0
  4. nifti2bids-0.0.9/nifti2bids/io.py → nifti2bids-0.1.0/nifti2bids/bids.py +36 -133
  5. nifti2bids-0.1.0/nifti2bids/io.py +135 -0
  6. nifti2bids-0.0.9/nifti2bids/utils.py → nifti2bids-0.1.0/nifti2bids/metadata.py +7 -23
  7. {nifti2bids-0.0.9 → nifti2bids-0.1.0}/nifti2bids.egg-info/PKG-INFO +2 -2
  8. {nifti2bids-0.0.9 → nifti2bids-0.1.0}/nifti2bids.egg-info/SOURCES.txt +7 -5
  9. nifti2bids-0.1.0/tests/test_bids.py +70 -0
  10. nifti2bids-0.1.0/tests/test_io.py +48 -0
  11. nifti2bids-0.0.9/tests/test_logger.py → nifti2bids-0.1.0/tests/test_logging.py +1 -1
  12. nifti2bids-0.0.9/tests/test_utils.py → nifti2bids-0.1.0/tests/test_metadata.py +31 -43
  13. nifti2bids-0.0.9/nifti2bids/__init__.py +0 -1
  14. nifti2bids-0.0.9/tests/test_io.py +0 -99
  15. {nifti2bids-0.0.9 → nifti2bids-0.1.0}/LICENSE +0 -0
  16. {nifti2bids-0.0.9 → nifti2bids-0.1.0}/nifti2bids/_decorators.py +0 -0
  17. {nifti2bids-0.0.9 → nifti2bids-0.1.0}/nifti2bids/_exceptions.py +0 -0
  18. {nifti2bids-0.0.9 → nifti2bids-0.1.0}/nifti2bids/_helpers.py +0 -0
  19. /nifti2bids-0.0.9/nifti2bids/logger.py → /nifti2bids-0.1.0/nifti2bids/logging.py +0 -0
  20. {nifti2bids-0.0.9 → nifti2bids-0.1.0}/nifti2bids/simulate.py +0 -0
  21. {nifti2bids-0.0.9 → nifti2bids-0.1.0}/nifti2bids.egg-info/dependency_links.txt +0 -0
  22. {nifti2bids-0.0.9 → nifti2bids-0.1.0}/nifti2bids.egg-info/requires.txt +0 -0
  23. {nifti2bids-0.0.9 → nifti2bids-0.1.0}/nifti2bids.egg-info/top_level.txt +0 -0
  24. {nifti2bids-0.0.9 → nifti2bids-0.1.0}/pyproject.toml +0 -0
  25. {nifti2bids-0.0.9 → nifti2bids-0.1.0}/setup.cfg +0 -0
  26. {nifti2bids-0.0.9 → nifti2bids-0.1.0}/tests/test_decorators.py +0 -0
  27. {nifti2bids-0.0.9 → nifti2bids-0.1.0}/tests/test_simulate.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: nifti2bids
3
- Version: 0.0.9
3
+ Version: 0.1.0
4
4
  Summary: Post-hoc BIDS conversion toolkit for NIfTI datasets without original DICOMs.
5
5
  Author-email: Donisha Smith <dsmit420@jhu.edu>
6
6
  License-Expression: MIT
@@ -45,7 +45,7 @@ Dynamic: license-file
45
45
  [![Test Status](https://github.com/donishadsmith/nifti2bids/actions/workflows/testing.yaml/badge.svg)](https://github.com/donishadsmith/nifti2bids/actions/workflows/testing.yaml)
46
46
  [![codecov](https://codecov.io/gh/donishadsmith/nifti2bids/graph/badge.svg?token=PCJ17NA627)](https://codecov.io/gh/donishadsmith/nifti2bids)
47
47
  [![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
48
- [![Documentation Status](https://readthedocs.org/projects/nifti2bids-py/badge/?version=latest)](http://nifti2bids.readthedocs.io/en/latest/?badge=latest)
48
+ [![Documentation Status](https://readthedocs.org/projects/nifti2bids/badge/?version=stable)](http://nifti2bids.readthedocs.io/en/stable/?badge=stable)
49
49
 
50
50
 
51
51
  A toolkit for post hoc BIDS-ification NIfTI datasets. Includes utilities for metadata extraction, file renaming, and JSON sidecar generation, designed primarily for datasets where the original DICOMs are unavailable.
@@ -7,7 +7,7 @@
7
7
  [![Test Status](https://github.com/donishadsmith/nifti2bids/actions/workflows/testing.yaml/badge.svg)](https://github.com/donishadsmith/nifti2bids/actions/workflows/testing.yaml)
8
8
  [![codecov](https://codecov.io/gh/donishadsmith/nifti2bids/graph/badge.svg?token=PCJ17NA627)](https://codecov.io/gh/donishadsmith/nifti2bids)
9
9
  [![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
10
- [![Documentation Status](https://readthedocs.org/projects/nifti2bids-py/badge/?version=latest)](http://nifti2bids.readthedocs.io/en/latest/?badge=latest)
10
+ [![Documentation Status](https://readthedocs.org/projects/nifti2bids/badge/?version=stable)](http://nifti2bids.readthedocs.io/en/stable/?badge=stable)
11
11
 
12
12
 
13
13
  A toolkit for post hoc BIDS-ification NIfTI datasets. Includes utilities for metadata extraction, file renaming, and JSON sidecar generation, designed primarily for datasets where the original DICOMs are unavailable.
@@ -0,0 +1,20 @@
1
+ """
2
+ Post-hoc BIDS conversion toolkit for NIfTI datasets without original DICOMs.
3
+ ----------------------------------------------------------------------------
4
+ Documentation can be found at https://nifti2bids.readthedocs.io.
5
+
6
+ Submodules
7
+ ----------
8
+ bids -- Operations related to initializing and creating BIDs compliant files
9
+
10
+ io -- Generic operations related to loading NIfTI data
11
+
12
+ logging -- Set up a logger using ``RichHandler`` as the default handler if a root or
13
+ module specific handler is not available
14
+
15
+ metadata -- Operations related to extracting metadata information from NIfTI images
16
+
17
+ simulate -- Simulate a basic NIfTI image for testing purposes
18
+ """
19
+
20
+ __version__ = "0.1.0"
@@ -1,139 +1,9 @@
1
- """Module for input/output operations."""
2
-
3
- import glob, json, os, shutil
1
+ import os, json
4
2
  from typing import Optional
5
3
 
6
- import nibabel as nib
7
-
8
-
9
- def load_nifti(
10
- nifti_file_or_img: str | nib.nifti1.Nifti1Image,
11
- ) -> nib.nifti1.Nifti1Image:
12
- """
13
- Loads a NIfTI image.
14
-
15
- Loads NIfTI image when not a ``Nifti1Image`` object or
16
- returns the image if already loaded in.
17
-
18
- Parameters
19
- ----------
20
- nifti_file_or_img: :obj:`str` or :obj:`Nifti1Image`
21
- Path to the NIfTI file or a NIfTI image.
22
-
23
- Returns
24
- -------
25
- nib.nifti1.Nifti1Image
26
- The loaded in NIfTI image.
27
- """
28
- nifti_img = (
29
- nifti_file_or_img
30
- if isinstance(nifti_file_or_img, nib.nifti1.Nifti1Image)
31
- else nib.load(nifti_file_or_img)
32
- )
33
-
34
- return nifti_img
35
-
36
-
37
- def compress_image(nifti_file: str, remove_src_file: bool = False) -> None:
38
- """
39
- Compresses a ".nii" image to a ".nii.gz" image.
40
-
41
- Parameters
42
- ----------
43
- nifti_file: :obj:`str`
44
- Path to the NIfTI image.
45
-
46
- remove_src_file: :obj:`bool`
47
- Deletes the original source image file.
48
-
49
- Returns
50
- -------
51
- None
52
- """
53
- img = nib.load(nifti_file)
54
- nib.save(img, nifti_file.replace(".nii", ".nii.gz"))
55
-
56
- if remove_src_file:
57
- os.remove(nifti_file)
58
-
59
-
60
- def glob_contents(src_dir: str, pattern: str) -> list[str]:
61
- """
62
- Use glob to get contents with specific patterns.
63
-
64
- Parameters
65
- ----------
66
- src_dir: :obj:`str`
67
- The source directory.
68
-
69
- ext: :obj:`str`
70
- The extension.
71
-
72
- Returns
73
- -------
74
- list[str]
75
- List of contents with the pattern specified by ``pattern``.
76
- """
77
- return glob.glob(os.path.join(src_dir, f"*{pattern}"))
78
-
79
-
80
- def get_nifti_header(nifti_file_or_img):
81
- """
82
- Get header from a NIfTI image.
83
-
84
- Parameters
85
- ----------
86
- nifti_file_or_img: :obj:`str` or :obj:`Nifti1Image`
87
- Path to the NIfTI file or a NIfTI image.
88
-
89
- Returns
90
- -------
91
- nib.nifti1.Nifti1Image
92
- The header from a NIfTI image.
93
- """
94
- return load_nifti(nifti_file_or_img).header
95
-
96
-
97
- def get_nifti_affine(nifti_file_or_img):
98
- """
99
- Get the affine matrix from a NIfTI image.
100
-
101
- Parameters
102
- ----------
103
- nifti_file_or_img: :obj:`str` or :obj:`Nifti1Image`
104
- Path to the NIfTI file or a NIfTI image.
105
-
106
- Returns
107
- -------
108
- nib.nifti1.Nifti1Image
109
- The header from a NIfTI image.
110
- """
111
- return load_nifti(nifti_file_or_img).affine
112
-
4
+ import pandas as pd
113
5
 
114
- def _copy_file(src_file: str, dst_file: str, remove_src_file: bool) -> None:
115
- """
116
- Copy a file and optionally remove the source file.
117
-
118
- Parameters
119
- ----------
120
- src_file: :obj:`str`
121
- The source file to be copied
122
-
123
- dst_file: :obj:`str`
124
- The new destination file.
125
-
126
- remove_src_file: :obj:`bool`
127
- Delete the source file if True.
128
-
129
- Returns
130
- -------
131
- None
132
- """
133
- shutil.copy(src_file, dst_file)
134
-
135
- if remove_src_file:
136
- os.remove(src_file)
6
+ from nifti2bids.io import _copy_file, glob_contents
137
7
 
138
8
 
139
9
  def create_bids_file(
@@ -285,3 +155,36 @@ def save_dataset_description(dataset_description: dict[str, str], dst_dir: str)
285
155
  os.path.join(dst_dir, "dataset_description.json"), "w", encoding="utf-8"
286
156
  ) as f:
287
157
  json.dump(dataset_description, f)
158
+
159
+
160
+ def create_participant_tsv(
161
+ bids_dir: str, save_df: bool = False, return_df: bool = True
162
+ ) -> pd.DataFrame | None:
163
+ """
164
+ Creates a basic participant dataframe for the "participants.tsv" file.
165
+
166
+ Parameters
167
+ ----------
168
+ bids_dir: :obj:`str`
169
+ The root of BIDS compliant directory.
170
+
171
+ save_df: :obj:`bool`, bool=False
172
+ Save the dataframe to the root of the BIDS compliant directory.
173
+
174
+ return_df: :obj:`str`
175
+ Whether or not to return the dataframe.
176
+
177
+ Returns
178
+ -------
179
+ pd.DataFrame or None
180
+ The dataframe if ``return_df`` is True.
181
+ """
182
+ participants = [
183
+ os.path.basename(folder) for folder in glob_contents(bids_dir, "*sub-*")
184
+ ]
185
+ df = pd.DataFrame({"participant_id": participants})
186
+
187
+ if save_df:
188
+ df.to_csv(os.path.join(bids_dir, "participants.tsv"), sep="\t", index=None)
189
+
190
+ return df if return_df else None
@@ -0,0 +1,135 @@
1
+ """Module for input/output operations."""
2
+
3
+ import glob, os, shutil
4
+
5
+ import nibabel as nib
6
+
7
+
8
+ def load_nifti(
9
+ nifti_file_or_img: str | nib.nifti1.Nifti1Image,
10
+ ) -> nib.nifti1.Nifti1Image:
11
+ """
12
+ Loads a NIfTI image.
13
+
14
+ Loads NIfTI image when not a ``Nifti1Image`` object or
15
+ returns the image if already loaded in.
16
+
17
+ Parameters
18
+ ----------
19
+ nifti_file_or_img: :obj:`str` or :obj:`Nifti1Image`
20
+ Path to the NIfTI file or a NIfTI image.
21
+
22
+ Returns
23
+ -------
24
+ nib.nifti1.Nifti1Image
25
+ The loaded in NIfTI image.
26
+ """
27
+ nifti_img = (
28
+ nifti_file_or_img
29
+ if isinstance(nifti_file_or_img, nib.nifti1.Nifti1Image)
30
+ else nib.load(nifti_file_or_img)
31
+ )
32
+
33
+ return nifti_img
34
+
35
+
36
+ def compress_image(nifti_file: str, remove_src_file: bool = False) -> None:
37
+ """
38
+ Compresses a ".nii" image to a ".nii.gz" image.
39
+
40
+ Parameters
41
+ ----------
42
+ nifti_file: :obj:`str`
43
+ Path to the NIfTI image.
44
+
45
+ remove_src_file: :obj:`bool`
46
+ Deletes the original source image file.
47
+
48
+ Returns
49
+ -------
50
+ None
51
+ """
52
+ img = nib.load(nifti_file)
53
+ nib.save(img, nifti_file.replace(".nii", ".nii.gz"))
54
+
55
+ if remove_src_file:
56
+ os.remove(nifti_file)
57
+
58
+
59
+ def glob_contents(src_dir: str, pattern: str) -> list[str]:
60
+ """
61
+ Use glob to get contents with specific patterns.
62
+
63
+ Parameters
64
+ ----------
65
+ src_dir: :obj:`str`
66
+ The source directory.
67
+
68
+ ext: :obj:`str`
69
+ The extension.
70
+
71
+ Returns
72
+ -------
73
+ list[str]
74
+ List of contents with the pattern specified by ``pattern``.
75
+ """
76
+ return glob.glob(os.path.join(src_dir, f"*{pattern}"))
77
+
78
+
79
+ def get_nifti_header(nifti_file_or_img):
80
+ """
81
+ Get header from a NIfTI image.
82
+
83
+ Parameters
84
+ ----------
85
+ nifti_file_or_img: :obj:`str` or :obj:`Nifti1Image`
86
+ Path to the NIfTI file or a NIfTI image.
87
+
88
+ Returns
89
+ -------
90
+ nib.nifti1.Nifti1Image
91
+ The header from a NIfTI image.
92
+ """
93
+ return load_nifti(nifti_file_or_img).header
94
+
95
+
96
+ def get_nifti_affine(nifti_file_or_img):
97
+ """
98
+ Get the affine matrix from a NIfTI image.
99
+
100
+ Parameters
101
+ ----------
102
+ nifti_file_or_img: :obj:`str` or :obj:`Nifti1Image`
103
+ Path to the NIfTI file or a NIfTI image.
104
+
105
+ Returns
106
+ -------
107
+ nib.nifti1.Nifti1Image
108
+ The header from a NIfTI image.
109
+ """
110
+ return load_nifti(nifti_file_or_img).affine
111
+
112
+
113
+ def _copy_file(src_file: str, dst_file: str, remove_src_file: bool) -> None:
114
+ """
115
+ Copy a file and optionally remove the source file.
116
+
117
+ Parameters
118
+ ----------
119
+ src_file: :obj:`str`
120
+ The source file to be copied
121
+
122
+ dst_file: :obj:`str`
123
+ The new destination file.
124
+
125
+ remove_src_file: :obj:`bool`
126
+ Delete the source file if True.
127
+
128
+ Returns
129
+ -------
130
+ None
131
+ """
132
+ shutil.copy(src_file, dst_file)
133
+
134
+ if remove_src_file:
135
+ os.remove(src_file)
@@ -3,12 +3,12 @@
3
3
  import datetime, os, re
4
4
  from typing import Any, Literal, Optional
5
5
 
6
- import nibabel as nib, numpy as np, pandas as pd
6
+ import nibabel as nib, numpy as np
7
7
 
8
8
  from ._exceptions import SliceAxisError, DataDimensionError
9
9
  from ._decorators import check_all_none
10
- from .io import load_nifti, get_nifti_header, glob_contents
11
- from .logger import setup_logger
10
+ from .io import load_nifti, get_nifti_header
11
+ from .logging import setup_logger
12
12
 
13
13
  LGR = setup_logger(__name__)
14
14
 
@@ -418,7 +418,7 @@ def is_valid_date(date_str: str, date_fmt: str) -> bool:
418
418
 
419
419
  Example
420
420
  -------
421
- >>> from nifti2bids.utils import is_valid_date
421
+ >>> from nifti2bids.metadata import is_valid_date
422
422
  >>> is_valid_date("241010", "%y%m%d")
423
423
  True
424
424
  """
@@ -451,7 +451,7 @@ def get_date_from_filename(filename: str, date_fmt: str) -> str | None:
451
451
 
452
452
  Example
453
453
  -------
454
- >>> from nifti2bids.utils import get_date_from_filename
454
+ >>> from nifti2bids.metadata import get_date_from_filename
455
455
  >>> get_date_from_filename("101_240820_mprage_32chan.nii", "%y%m%d")
456
456
  "240820"
457
457
  """
@@ -469,22 +469,6 @@ def get_date_from_filename(filename: str, date_fmt: str) -> str | None:
469
469
  return date_str
470
470
 
471
471
 
472
- def create_participant_tsv(bids_dir: str) -> None:
473
- """
474
- Creates a participant TSV file.
475
-
476
- Parameters
477
- ----------
478
- bids_dir: :obj:`str`
479
- The BIDS compliant directory.
480
- """
481
- participants = [
482
- os.path.basename(folder) for folder in glob_contents(bids_dir, "*sub-*")
483
- ]
484
- df = pd.DataFrame({"participant_id": participants})
485
- df.to_csv(os.path.join(bids_dir, "participants.tsv"), sep="\t", index=None)
486
-
487
-
488
472
  def get_entity_value(filename: str, entity: str) -> str | None:
489
473
  """
490
474
  Gets entity value of a BIDS compliant filename.
@@ -504,7 +488,7 @@ def get_entity_value(filename: str, entity: str) -> str | None:
504
488
 
505
489
  Example
506
490
  -------
507
- >>> from nifti2bids.utils import get_entity_value
491
+ >>> from nifti2bids.metadata import get_entity_value
508
492
  >>> get_entity_value("sub-01_task-flanker_bold.nii.gz", "task")
509
493
  "flanker"
510
494
  """
@@ -536,7 +520,7 @@ def infer_task_from_image(
536
520
  Example
537
521
  -------
538
522
  >>> from nifti2bids.io import simulate_nifti_image
539
- >>> from nifti2bids.utils import infer_task_from_image
523
+ >>> from nifti2bids.metadata import infer_task_from_image
540
524
  >>> img = simulate_nifti_image((100, 100, 100, 260))
541
525
  >>> volume_to_task_map = {300: "flanker", 260: "nback"}
542
526
  >>> infer_task_from_image(img, volume_to_task_map)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: nifti2bids
3
- Version: 0.0.9
3
+ Version: 0.1.0
4
4
  Summary: Post-hoc BIDS conversion toolkit for NIfTI datasets without original DICOMs.
5
5
  Author-email: Donisha Smith <dsmit420@jhu.edu>
6
6
  License-Expression: MIT
@@ -45,7 +45,7 @@ Dynamic: license-file
45
45
  [![Test Status](https://github.com/donishadsmith/nifti2bids/actions/workflows/testing.yaml/badge.svg)](https://github.com/donishadsmith/nifti2bids/actions/workflows/testing.yaml)
46
46
  [![codecov](https://codecov.io/gh/donishadsmith/nifti2bids/graph/badge.svg?token=PCJ17NA627)](https://codecov.io/gh/donishadsmith/nifti2bids)
47
47
  [![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
48
- [![Documentation Status](https://readthedocs.org/projects/nifti2bids-py/badge/?version=latest)](http://nifti2bids.readthedocs.io/en/latest/?badge=latest)
48
+ [![Documentation Status](https://readthedocs.org/projects/nifti2bids/badge/?version=stable)](http://nifti2bids.readthedocs.io/en/stable/?badge=stable)
49
49
 
50
50
 
51
51
  A toolkit for post hoc BIDS-ification NIfTI datasets. Includes utilities for metadata extraction, file renaming, and JSON sidecar generation, designed primarily for datasets where the original DICOMs are unavailable.
@@ -5,17 +5,19 @@ nifti2bids/__init__.py
5
5
  nifti2bids/_decorators.py
6
6
  nifti2bids/_exceptions.py
7
7
  nifti2bids/_helpers.py
8
+ nifti2bids/bids.py
8
9
  nifti2bids/io.py
9
- nifti2bids/logger.py
10
+ nifti2bids/logging.py
11
+ nifti2bids/metadata.py
10
12
  nifti2bids/simulate.py
11
- nifti2bids/utils.py
12
13
  nifti2bids.egg-info/PKG-INFO
13
14
  nifti2bids.egg-info/SOURCES.txt
14
15
  nifti2bids.egg-info/dependency_links.txt
15
16
  nifti2bids.egg-info/requires.txt
16
17
  nifti2bids.egg-info/top_level.txt
18
+ tests/test_bids.py
17
19
  tests/test_decorators.py
18
20
  tests/test_io.py
19
- tests/test_logger.py
20
- tests/test_simulate.py
21
- tests/test_utils.py
21
+ tests/test_logging.py
22
+ tests/test_metadata.py
23
+ tests/test_simulate.py
@@ -0,0 +1,70 @@
1
+ import glob, os
2
+
3
+ import pandas as pd, pytest
4
+
5
+ from nifti2bids.bids import (
6
+ create_bids_file,
7
+ create_participant_tsv,
8
+ create_dataset_description,
9
+ save_dataset_description,
10
+ )
11
+
12
+
13
+ @pytest.mark.parametrize("dst_dir, remove_src_file", ([None, True], [True, False]))
14
+ def test_create_bids_file(nifti_img_and_path, tmp_dir, dst_dir, remove_src_file):
15
+ """Test for ``create_bids_file``."""
16
+ _, img_path = nifti_img_and_path
17
+ dst_dir = None if not dst_dir else os.path.join(tmp_dir.name, "test")
18
+ if dst_dir:
19
+ os.makedirs(dst_dir)
20
+
21
+ bids_filename = create_bids_file(
22
+ img_path,
23
+ subj_id="01",
24
+ desc="bold",
25
+ remove_src_file=remove_src_file,
26
+ dst_dir=dst_dir,
27
+ return_bids_filename=True,
28
+ )
29
+ assert bids_filename
30
+ assert os.path.basename(bids_filename) == "sub-01_bold.nii"
31
+
32
+ if dst_dir:
33
+ dst_file = glob.glob(os.path.join(dst_dir, "*nii"))[0]
34
+ assert os.path.basename(dst_file) == "sub-01_bold.nii"
35
+
36
+ src_file = glob.glob(os.path.join(os.path.dirname(img_path), "*.nii"))[0]
37
+ assert os.path.basename(src_file) == "img.nii"
38
+ else:
39
+ files = glob.glob(os.path.join(os.path.dirname(img_path), "*.nii"))
40
+ assert len(files) == 1
41
+ assert os.path.basename(files[0]) == "sub-01_bold.nii"
42
+
43
+
44
+ def test_create_dataset_description():
45
+ """Test for ``create_dataset_description``."""
46
+ dataset_desc = create_dataset_description(dataset_name="test", bids_version="1.2.0")
47
+ assert dataset_desc.get("Name") == "test"
48
+ assert dataset_desc.get("BIDSVersion") == "1.2.0"
49
+
50
+
51
+ def test_save_dataset_description(tmp_dir):
52
+ """Test for ``save_dataset_description``."""
53
+ dataset_desc = create_dataset_description(dataset_name="test", bids_version="1.2.0")
54
+ save_dataset_description(dataset_desc, tmp_dir.name)
55
+ files = glob.glob(os.path.join(tmp_dir.name, "*.json"))
56
+ assert len(files) == 1
57
+ assert os.path.basename(files[0]) == "dataset_description.json"
58
+
59
+
60
+ def test_create_participant_tsv(tmp_dir):
61
+ """Test for ``create_participant_tsv``."""
62
+ os.makedirs(os.path.join(tmp_dir.name, "sub-01"))
63
+ df = create_participant_tsv(tmp_dir.name, save_df=True, return_df=True)
64
+ assert isinstance(df, pd.DataFrame)
65
+
66
+ filename = os.path.join(tmp_dir.name, "participants.tsv")
67
+ assert os.path.isfile(filename)
68
+
69
+ df = pd.read_csv(filename, sep="\t")
70
+ assert df["participant_id"].values[0] == "sub-01"
@@ -0,0 +1,48 @@
1
+ import glob, os
2
+ import nibabel as nib, pandas as pd, pytest
3
+ import nifti2bids.io as bids_io
4
+
5
+
6
+ def test_compress_image(nifti_img_and_path):
7
+ """Test for ``compress_image``."""
8
+ _, img_path = nifti_img_and_path
9
+
10
+ files = glob.glob(os.path.join(os.path.dirname(img_path), "*"))
11
+ assert len(files) == 1
12
+
13
+ file = files[0]
14
+ assert file.endswith(".nii")
15
+
16
+ bids_io.compress_image(img_path, remove_src_file=True)
17
+
18
+ files = glob.glob(os.path.join(os.path.dirname(img_path), "*"))
19
+ assert len(files) == 1
20
+
21
+ file = files[0]
22
+ assert file.endswith(".nii.gz")
23
+
24
+
25
+ def test_load_nifti(nifti_img_and_path):
26
+ """Test for ``load_nifti``."""
27
+ img, img_path = nifti_img_and_path
28
+ assert isinstance(bids_io.load_nifti(img), nib.nifti1.Nifti1Image)
29
+ assert isinstance(bids_io.load_nifti(img_path), nib.nifti1.Nifti1Image)
30
+
31
+
32
+ def test_glob_contents(nifti_img_and_path):
33
+ """Test for ``glob_contents``"""
34
+ _, img_path = nifti_img_and_path
35
+ files = bids_io.glob_contents(os.path.dirname(img_path), pattern=".nii")
36
+ assert len(files) == 1
37
+
38
+
39
+ def test_get_nifti_header(nifti_img_and_path):
40
+ """Test for ``get_nifti_header``."""
41
+ img, _ = nifti_img_and_path
42
+ assert isinstance(bids_io.get_nifti_header(img), nib.nifti1.Nifti1Header)
43
+
44
+
45
+ def test_get_nifti_affine(nifti_img_and_path):
46
+ """Test for ``get_nifti_affine``."""
47
+ img, _ = nifti_img_and_path
48
+ assert bids_io.get_nifti_affine(img).shape == (4, 4)
@@ -2,7 +2,7 @@ import logging
2
2
 
3
3
  import rich
4
4
 
5
- from nifti2bids.logger import setup_logger, _add_default_handler
5
+ from nifti2bids.logging import setup_logger, _add_default_handler
6
6
 
7
7
 
8
8
  def test_setup_logger(caplog):
@@ -1,6 +1,5 @@
1
- import os
2
- import nibabel as nib, numpy as np, pandas as pd, pytest
3
- import nifti2bids.utils as bids_utils
1
+ import nibabel as nib, numpy as np, pytest
2
+ import nifti2bids.metadata as bids_meta
4
3
 
5
4
 
6
5
  @pytest.mark.parametrize("return_header", (False, True))
@@ -10,14 +9,14 @@ def test_get_hdr_metadata(nifti_img_and_path, return_header):
10
9
  img.header["slice_end"] = 100
11
10
 
12
11
  if return_header:
13
- slice_end, hdr = bids_utils.get_hdr_metadata(
12
+ slice_end, hdr = bids_meta.get_hdr_metadata(
14
13
  metadata_name="slice_end",
15
14
  nifti_file_or_img=img,
16
15
  return_header=return_header,
17
16
  )
18
17
  assert isinstance(hdr, nib.nifti1.Nifti1Header)
19
18
  else:
20
- slice_end = bids_utils.get_hdr_metadata(
19
+ slice_end = bids_meta.get_hdr_metadata(
21
20
  metadata_name="slice_end",
22
21
  nifti_file_or_img=img,
23
22
  return_header=return_header,
@@ -31,23 +30,23 @@ def test_determine_slice_axis(nifti_img_and_path):
31
30
  img, _ = nifti_img_and_path
32
31
 
33
32
  with pytest.raises(ValueError):
34
- bids_utils.determine_slice_axis(img)
33
+ bids_meta.determine_slice_axis(img)
35
34
 
36
35
  # Subtract one to convert to index
37
36
  img.header["slice_end"] = img.get_fdata().shape[2] - 1
38
- assert bids_utils.determine_slice_axis(img) == 2
37
+ assert bids_meta.determine_slice_axis(img) == 2
39
38
 
40
39
 
41
40
  def test_get_n_volumes(nifti_img_and_path):
42
41
  """Test for ``get_n_volumes``."""
43
42
  img, _ = nifti_img_and_path
44
- assert bids_utils.get_n_volumes(img) == 5
43
+ assert bids_meta.get_n_volumes(img) == 5
45
44
 
46
45
  from nifti2bids.simulate import simulate_nifti_image
47
46
  from nifti2bids._exceptions import DataDimensionError
48
47
 
49
48
  with pytest.raises(DataDimensionError):
50
- bids_utils.get_n_volumes(simulate_nifti_image((10, 10, 10)))
49
+ bids_meta.get_n_volumes(simulate_nifti_image((10, 10, 10)))
51
50
 
52
51
 
53
52
  def test_get_n_slices(nifti_img_and_path):
@@ -59,23 +58,23 @@ def test_get_n_slices(nifti_img_and_path):
59
58
  img.header["slice_end"] = img.get_fdata().shape[2] - 1
60
59
 
61
60
  with pytest.raises(SliceAxisError):
62
- bids_utils.get_n_slices(img, slice_axis="x")
61
+ bids_meta.get_n_slices(img, slice_axis="x")
63
62
 
64
- assert bids_utils.get_n_slices(img, slice_axis="z") == img.header["slice_end"] + 1
65
- assert bids_utils.get_n_slices(img) == img.header["slice_end"] + 1
63
+ assert bids_meta.get_n_slices(img, slice_axis="z") == img.header["slice_end"] + 1
64
+ assert bids_meta.get_n_slices(img) == img.header["slice_end"] + 1
66
65
 
67
66
 
68
67
  def test_get_tr(nifti_img_and_path):
69
68
  """Test for ``get_tr``."""
70
69
  img, _ = nifti_img_and_path
71
70
  img.header["pixdim"][4] = 2.3
72
- assert bids_utils.get_tr(img) == 2.3
73
- assert not isinstance(bids_utils.get_tr(img), np.floating)
74
- assert not isinstance(bids_utils.get_tr(img), np.integer)
71
+ assert bids_meta.get_tr(img) == 2.3
72
+ assert not isinstance(bids_meta.get_tr(img), np.floating)
73
+ assert not isinstance(bids_meta.get_tr(img), np.integer)
75
74
 
76
75
  img.header["pixdim"][4] = 0
77
76
  with pytest.raises(ValueError):
78
- bids_utils.get_tr(img)
77
+ bids_meta.get_tr(img)
79
78
 
80
79
 
81
80
  @pytest.mark.parametrize("slice_acquisition_method", ("sequential", "interleaved"))
@@ -88,21 +87,21 @@ def test_create_slice_timing(slice_acquisition_method):
88
87
  img.header["slice_end"] = 3
89
88
 
90
89
  if slice_acquisition_method == "sequential":
91
- slice_timing_dict = bids_utils.create_slice_timing(
90
+ slice_timing_dict = bids_meta.create_slice_timing(
92
91
  nifti_file_or_img=img,
93
92
  slice_acquisition_method=slice_acquisition_method,
94
93
  ascending=True,
95
94
  )
96
95
  assert slice_timing_dict == [0, 0.5, 1, 1.5]
97
96
 
98
- slice_timing_dict = bids_utils.create_slice_timing(
97
+ slice_timing_dict = bids_meta.create_slice_timing(
99
98
  nifti_file_or_img=img,
100
99
  slice_acquisition_method=slice_acquisition_method,
101
100
  ascending=False,
102
101
  )
103
102
  assert slice_timing_dict == [1.5, 1, 0.5, 0]
104
103
  else:
105
- slice_timing_dict = bids_utils.create_slice_timing(
104
+ slice_timing_dict = bids_meta.create_slice_timing(
106
105
  nifti_file_or_img=img,
107
106
  slice_acquisition_method=slice_acquisition_method,
108
107
  ascending=True,
@@ -110,7 +109,7 @@ def test_create_slice_timing(slice_acquisition_method):
110
109
  )
111
110
  assert slice_timing_dict == [0, 1, 0.5, 1.5]
112
111
 
113
- slice_timing_dict = bids_utils.create_slice_timing(
112
+ slice_timing_dict = bids_meta.create_slice_timing(
114
113
  nifti_file_or_img=img,
115
114
  slice_acquisition_method=slice_acquisition_method,
116
115
  ascending=False,
@@ -118,7 +117,7 @@ def test_create_slice_timing(slice_acquisition_method):
118
117
  )
119
118
  assert slice_timing_dict == [1.5, 0.5, 1, 0]
120
119
 
121
- slice_timing_dict = bids_utils.create_slice_timing(
120
+ slice_timing_dict = bids_meta.create_slice_timing(
122
121
  nifti_file_or_img=img,
123
122
  slice_acquisition_method=slice_acquisition_method,
124
123
  ascending=True,
@@ -126,7 +125,7 @@ def test_create_slice_timing(slice_acquisition_method):
126
125
  )
127
126
  assert slice_timing_dict == [1, 0, 1.5, 0.5]
128
127
 
129
- slice_timing_dict = bids_utils.create_slice_timing(
128
+ slice_timing_dict = bids_meta.create_slice_timing(
130
129
  nifti_file_or_img=img,
131
130
  slice_acquisition_method=slice_acquisition_method,
132
131
  ascending=False,
@@ -135,7 +134,7 @@ def test_create_slice_timing(slice_acquisition_method):
135
134
  assert slice_timing_dict == [0.5, 1.5, 0, 1]
136
135
 
137
136
  with pytest.raises(ValueError):
138
- slice_timing_dict = bids_utils.create_slice_timing(
137
+ slice_timing_dict = bids_meta.create_slice_timing(
139
138
  nifti_file_or_img=img,
140
139
  slice_acquisition_method=slice_acquisition_method,
141
140
  ascending=False,
@@ -148,49 +147,38 @@ def test_is_3d_img(nifti_img_and_path):
148
147
  from nifti2bids.simulate import simulate_nifti_image
149
148
 
150
149
  img = simulate_nifti_image((10, 10, 10))
151
- assert bids_utils.is_3d_img(img)
150
+ assert bids_meta.is_3d_img(img)
152
151
 
153
152
  img, _ = nifti_img_and_path
154
- assert not bids_utils.is_3d_img(img)
153
+ assert not bids_meta.is_3d_img(img)
155
154
 
156
155
 
157
156
  def test_get_scanner_info(nifti_img_and_path):
158
157
  """Test for ``get_scanner_info``."""
159
158
  img, _ = nifti_img_and_path
160
159
  with pytest.raises(ValueError):
161
- bids_utils.get_scanner_info(img)
160
+ bids_meta.get_scanner_info(img)
162
161
 
163
162
  img.header["descrip"] = "Philips Ingenia Elition X 5.7.1"
164
- manufacturer_name, model_name = bids_utils.get_scanner_info(img)
163
+ manufacturer_name, model_name = bids_meta.get_scanner_info(img)
165
164
  assert manufacturer_name == "Philips"
166
165
  assert model_name == "Ingenia Elition X 5.7.1"
167
166
 
168
167
 
169
168
  def test_get_date_from_filename():
170
169
  """Test for ``get_date_from_filename``."""
171
- date = bids_utils.get_date_from_filename("101_240820_mprage_32chan.nii", "%y%m%d")
170
+ date = bids_meta.get_date_from_filename("101_240820_mprage_32chan.nii", "%y%m%d")
172
171
  assert date == "240820"
173
172
 
174
- date = bids_utils.get_date_from_filename("101_mprage_32chan.nii", "%y%m%d")
173
+ date = bids_meta.get_date_from_filename("101_mprage_32chan.nii", "%y%m%d")
175
174
  assert not date
176
175
 
177
176
 
178
- def test_create_participant_tsv(tmp_dir):
179
- """Test for ``get_date_from_filename``."""
180
- os.makedirs(os.path.join(tmp_dir.name, "sub-01"))
181
- bids_utils.create_participant_tsv(tmp_dir.name)
182
- filename = os.path.join(tmp_dir.name, "participants.tsv")
183
- assert os.path.isfile(filename)
184
-
185
- df = pd.read_csv(filename, sep="\t")
186
- assert df["participant_id"].values[0] == "sub-01"
187
-
188
-
189
177
  def test_get_entity_value():
190
178
  """Test for ``get_entity_value``."""
191
179
  filename = "sub-01_task-test_bold.nii.gz"
192
- assert bids_utils.get_entity_value(filename, "task") == "test"
193
- assert not bids_utils.get_entity_value(filename, "ses")
180
+ assert bids_meta.get_entity_value(filename, "task") == "test"
181
+ assert not bids_meta.get_entity_value(filename, "ses")
194
182
 
195
183
 
196
184
  def test_infer_task_from_image(nifti_img_and_path):
@@ -199,4 +187,4 @@ def test_infer_task_from_image(nifti_img_and_path):
199
187
 
200
188
  volume_to_task_map = {5: "flanker", 10: "nback"}
201
189
 
202
- assert bids_utils.infer_task_from_image(img, volume_to_task_map) == "flanker"
190
+ assert bids_meta.infer_task_from_image(img, volume_to_task_map) == "flanker"
@@ -1 +0,0 @@
1
- __version__ = "0.0.9"
@@ -1,99 +0,0 @@
1
- import glob, os
2
- import nibabel as nib, pytest
3
- import nifti2bids.io as bids_io
4
-
5
-
6
- def test_compress_image(nifti_img_and_path):
7
- """Test for ``compress_image``."""
8
- _, img_path = nifti_img_and_path
9
-
10
- files = glob.glob(os.path.join(os.path.dirname(img_path), "*"))
11
- assert len(files) == 1
12
-
13
- file = files[0]
14
- assert file.endswith(".nii")
15
-
16
- bids_io.compress_image(img_path, remove_src_file=True)
17
-
18
- files = glob.glob(os.path.join(os.path.dirname(img_path), "*"))
19
- assert len(files) == 1
20
-
21
- file = files[0]
22
- assert file.endswith(".nii.gz")
23
-
24
-
25
- def test_load_nifti(nifti_img_and_path):
26
- """Test for ``load_nifti``."""
27
- img, img_path = nifti_img_and_path
28
- assert isinstance(bids_io.load_nifti(img), nib.nifti1.Nifti1Image)
29
- assert isinstance(bids_io.load_nifti(img_path), nib.nifti1.Nifti1Image)
30
-
31
-
32
- def test_glob_contents(nifti_img_and_path):
33
- """Test for ``glob_contents``"""
34
- _, img_path = nifti_img_and_path
35
- files = bids_io.glob_contents(os.path.dirname(img_path), pattern=".nii")
36
- assert len(files) == 1
37
-
38
-
39
- def test_get_nifti_header(nifti_img_and_path):
40
- """Test for ``get_nifti_header``."""
41
- img, _ = nifti_img_and_path
42
- assert isinstance(bids_io.get_nifti_header(img), nib.nifti1.Nifti1Header)
43
-
44
-
45
- def test_get_nifti_affine(nifti_img_and_path):
46
- """Test for ``get_nifti_affine``."""
47
- img, _ = nifti_img_and_path
48
- assert bids_io.get_nifti_affine(img).shape == (4, 4)
49
-
50
-
51
- @pytest.mark.parametrize("dst_dir, remove_src_file", ([None, True], [True, False]))
52
- def test_create_bids_file(nifti_img_and_path, tmp_dir, dst_dir, remove_src_file):
53
- """Test for ``create_bids_file``."""
54
- _, img_path = nifti_img_and_path
55
- dst_dir = None if not dst_dir else os.path.join(tmp_dir.name, "test")
56
- if dst_dir:
57
- os.makedirs(dst_dir)
58
-
59
- bids_filename = bids_io.create_bids_file(
60
- img_path,
61
- subj_id="01",
62
- desc="bold",
63
- remove_src_file=remove_src_file,
64
- dst_dir=dst_dir,
65
- return_bids_filename=True,
66
- )
67
- assert bids_filename
68
- assert os.path.basename(bids_filename) == "sub-01_bold.nii"
69
-
70
- if dst_dir:
71
- dst_file = glob.glob(os.path.join(dst_dir, "*nii"))[0]
72
- assert os.path.basename(dst_file) == "sub-01_bold.nii"
73
-
74
- src_file = glob.glob(os.path.join(os.path.dirname(img_path), "*.nii"))[0]
75
- assert os.path.basename(src_file) == "img.nii"
76
- else:
77
- files = glob.glob(os.path.join(os.path.dirname(img_path), "*.nii"))
78
- assert len(files) == 1
79
- assert os.path.basename(files[0]) == "sub-01_bold.nii"
80
-
81
-
82
- def test_create_dataset_description():
83
- """Test for ``create_dataset_description``."""
84
- dataset_desc = bids_io.create_dataset_description(
85
- dataset_name="test", bids_version="1.2.0"
86
- )
87
- assert dataset_desc.get("Name") == "test"
88
- assert dataset_desc.get("BIDSVersion") == "1.2.0"
89
-
90
-
91
- def test_save_dataset_description(tmp_dir):
92
- """Test for ``save_dataset_description``."""
93
- dataset_desc = bids_io.create_dataset_description(
94
- dataset_name="test", bids_version="1.2.0"
95
- )
96
- bids_io.save_dataset_description(dataset_desc, tmp_dir.name)
97
- files = glob.glob(os.path.join(tmp_dir.name, "*.json"))
98
- assert len(files) == 1
99
- assert os.path.basename(files[0]) == "dataset_description.json"
File without changes
File without changes
File without changes