Skip to content

Commit 8065764

Browse files
committed
add modalities-specific bids modules
1 parent eb85570 commit 8065764

File tree

6 files changed

+114
-93
lines changed

6 files changed

+114
-93
lines changed

python/lib/eeg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from lib.database_lib.physiological_modality import PhysiologicalModality
1515
from lib.database_lib.physiological_output_type import PhysiologicalOutputType
1616
from lib.db.models.session import DbSession
17-
from lib.imaging_lib.bids.dataset import BIDSEEGDataType
17+
from lib.imaging_lib.bids.eeg.dataset import BIDSEEGDataType
1818
from lib.physiological import Physiological
1919
from lib.util.crypto import compute_file_blake2b_hash
2020

python/lib/imaging_lib/bids/dataset.py

Lines changed: 13 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,21 @@
22
from collections.abc import Iterator
33
from functools import cached_property
44
from pathlib import Path
5+
from typing import TYPE_CHECKING
56

67
from bids import BIDSLayout
78

89
from lib.imaging_lib.bids.dataset_description import BidsDatasetDescription
910
from lib.imaging_lib.bids.tsv_participants import BidsTsvParticipant, read_bids_participants_tsv_file
1011
from lib.imaging_lib.bids.tsv_scans import BidsTsvScan, read_bids_scans_tsv_file
11-
from lib.imaging_lib.nifti import find_dir_nifti_files
12-
from lib.util.fs import replace_file_extension, search_dir_file_with_regex
12+
from lib.util.fs import search_dir_file_with_regex
1313
from lib.util.iter import find
1414

15+
if TYPE_CHECKING:
16+
from lib.imaging_lib.bids.eeg.dataset import BIDSEEGDataType
17+
from lib.imaging_lib.bids.mri.dataset import BIDSMRIDataType, BIDSNifti
18+
19+
1520
PYBIDS_IGNORE = ['code', 'sourcedata', 'log', '.git']
1621

1722
PYBIDS_FORCE = [re.compile(r"_annotations\.(tsv|json)$")]
@@ -37,6 +42,7 @@ def data_types(self) -> Iterator['BIDSDataType']:
3742

3843
@property
3944
def niftis(self) -> Iterator['BIDSNifti']:
45+
from lib.imaging_lib.bids.mri.dataset import BIDSMRIDataType
4046
for data_type in self.data_types:
4147
if isinstance(data_type, BIDSMRIDataType):
4248
yield from data_type.niftis
@@ -159,6 +165,7 @@ def data_types(self) -> Iterator['BIDSDataType']:
159165

160166
@property
161167
def niftis(self) -> Iterator['BIDSNifti']:
168+
from lib.imaging_lib.bids.mri.dataset import BIDSMRIDataType
162169
for data_type in self.data_types:
163170
if isinstance(data_type, BIDSMRIDataType):
164171
yield from data_type.niftis
@@ -226,6 +233,8 @@ def mri_data_types(self) -> list['BIDSMRIDataType']:
226233
The MRI data type directories found in this session directory.
227234
"""
228235

236+
from lib.imaging_lib.bids.mri.dataset import BIDSMRIDataType
237+
229238
data_types: list[BIDSMRIDataType] = []
230239

231240
for data_type_name in ['anat', 'dwi', 'fmap', 'func']:
@@ -241,6 +250,8 @@ def eeg_data_types(self) -> list['BIDSEEGDataType']:
241250
The MRI data type directories found in this session directory.
242251
"""
243252

253+
from lib.imaging_lib.bids.eeg.dataset import BIDSEEGDataType
254+
244255
data_types: list[BIDSEEGDataType] = []
245256

246257
for data_type_name in ['eeg', 'ieeg']:
@@ -302,91 +313,3 @@ def root_dataset(self) -> BIDSDataset:
302313
@property
303314
def subject(self) -> BIDSSubject:
304315
return self.session.subject
305-
306-
307-
# TODO: Complete with EEG-specific content.
308-
class BIDSEEGDataType(BIDSDataType):
309-
pass
310-
311-
312-
class BIDSMRIDataType(BIDSDataType):
313-
@cached_property
314-
def niftis(self) -> list['BIDSNifti']:
315-
"""
316-
The NIfTI files found in this MRI data type directory.
317-
"""
318-
319-
niftis: list[BIDSNifti] = []
320-
321-
for nifti_path in find_dir_nifti_files(self.path):
322-
niftis.append(BIDSNifti(self, nifti_path.name))
323-
324-
return niftis
325-
326-
327-
class BIDSNifti:
328-
data_type: BIDSDataType
329-
path: Path
330-
suffix: str | None
331-
332-
def __init__(self, data_type: BIDSDataType, name: str):
333-
self.data_type = data_type
334-
self.path = data_type.path / name
335-
336-
suffix_match = re.search(r'_([a-zA-Z0-9]+)\.nii(\.gz)?$', self.name)
337-
if suffix_match is not None:
338-
self.suffix = suffix_match.group(1)
339-
else:
340-
self.suffix = None
341-
342-
@property
343-
def name(self) -> str:
344-
return self.path.name
345-
346-
@property
347-
def root_dataset(self) -> BIDSDataset:
348-
return self.data_type.root_dataset
349-
350-
@property
351-
def subject(self) -> BIDSSubject:
352-
return self.data_type.subject
353-
354-
@property
355-
def session(self) -> BIDSSession:
356-
return self.data_type.session
357-
358-
def get_json_path(self) -> Path | None:
359-
"""
360-
Get the JSON sidecar file path of this NIfTI file if it exists.
361-
"""
362-
363-
json_name = replace_file_extension(self.name, 'json')
364-
json_path = self.data_type.path / json_name
365-
if not json_path.exists():
366-
return None
367-
368-
return json_path
369-
370-
def get_bval_path(self) -> Path | None:
371-
"""
372-
Get the BVAL file path of this NIfTI file if it exists.
373-
"""
374-
375-
bval_name = replace_file_extension(self.name, 'bval')
376-
bval_path = self.data_type.path / bval_name
377-
if not bval_path.exists():
378-
return None
379-
380-
return bval_path
381-
382-
def get_bvec_path(self) -> Path | None:
383-
"""
384-
Get the BVEC file path of this NIfTI file if it exists.
385-
"""
386-
387-
bvec_name = replace_file_extension(self.name, 'bvec')
388-
bvec_path = self.data_type.path / bvec_name
389-
if not bvec_path.exists():
390-
return None
391-
392-
return bvec_path
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# TODO: Complete with EEG-specific content.
2+
from lib.imaging_lib.bids.dataset import BIDSDataType
3+
4+
5+
class BIDSEEGDataType(BIDSDataType):
6+
pass
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import re
2+
from functools import cached_property
3+
from pathlib import Path
4+
5+
from lib.imaging_lib.bids.dataset import BIDSDataset, BIDSDataType, BIDSSession, BIDSSubject
6+
from lib.imaging_lib.nifti import find_dir_nifti_files
7+
from lib.util.fs import replace_file_extension
8+
9+
10+
class BIDSMRIDataType(BIDSDataType):
11+
@cached_property
12+
def niftis(self) -> list['BIDSNifti']:
13+
"""
14+
The NIfTI files found in this MRI data type directory.
15+
"""
16+
17+
niftis: list[BIDSNifti] = []
18+
19+
for nifti_path in find_dir_nifti_files(self.path):
20+
niftis.append(BIDSNifti(self, nifti_path.name))
21+
22+
return niftis
23+
24+
25+
class BIDSNifti:
26+
data_type: BIDSDataType
27+
path: Path
28+
suffix: str | None
29+
30+
def __init__(self, data_type: BIDSDataType, name: str):
31+
self.data_type = data_type
32+
self.path = data_type.path / name
33+
34+
suffix_match = re.search(r'_([a-zA-Z0-9]+)\.nii(\.gz)?$', self.name)
35+
if suffix_match is not None:
36+
self.suffix = suffix_match.group(1)
37+
else:
38+
self.suffix = None
39+
40+
@property
41+
def name(self) -> str:
42+
return self.path.name
43+
44+
@property
45+
def root_dataset(self) -> BIDSDataset:
46+
return self.data_type.root_dataset
47+
48+
@property
49+
def subject(self) -> BIDSSubject:
50+
return self.data_type.subject
51+
52+
@property
53+
def session(self) -> BIDSSession:
54+
return self.data_type.session
55+
56+
def get_json_path(self) -> Path | None:
57+
"""
58+
Get the JSON sidecar file path of this NIfTI file if it exists.
59+
"""
60+
61+
json_name = replace_file_extension(self.name, 'json')
62+
json_path = self.data_type.path / json_name
63+
if not json_path.exists():
64+
return None
65+
66+
return json_path
67+
68+
def get_bval_path(self) -> Path | None:
69+
"""
70+
Get the BVAL file path of this NIfTI file if it exists.
71+
"""
72+
73+
bval_name = replace_file_extension(self.name, 'bval')
74+
bval_path = self.data_type.path / bval_name
75+
if not bval_path.exists():
76+
return None
77+
78+
return bval_path
79+
80+
def get_bvec_path(self) -> Path | None:
81+
"""
82+
Get the BVEC file path of this NIfTI file if it exists.
83+
"""
84+
85+
bvec_name = replace_file_extension(self.name, 'bvec')
86+
bvec_path = self.data_type.path / bvec_name
87+
if not bvec_path.exists():
88+
return None
89+
90+
return bvec_path

python/lib/import_bids_dataset/main.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
from lib.db.queries.session import try_get_session_with_cand_id_visit_label
1212
from lib.eeg import Eeg
1313
from lib.env import Env
14-
from lib.imaging_lib.bids.dataset import BIDSDataset, BIDSDataType, BIDSEEGDataType, BIDSMRIDataType, BIDSSession
14+
from lib.imaging_lib.bids.dataset import BIDSDataset, BIDSDataType, BIDSSession
1515
from lib.imaging_lib.bids.dataset_description import BidsDatasetDescriptionError
16+
from lib.imaging_lib.bids.eeg.dataset import BIDSEEGDataType
17+
from lib.imaging_lib.bids.mri.dataset import BIDSMRIDataType
1618
from lib.imaging_lib.bids.tsv_participants import (
1719
BidsTsvParticipant,
1820
merge_bids_tsv_participants,

python/lib/import_bids_dataset/mri.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
from lib.db.queries.file import try_get_file_with_hash, try_get_file_with_rel_path
88
from lib.db.queries.mri_scan_type import try_get_mri_scan_type_with_name
99
from lib.env import Env
10-
from lib.imaging_lib.bids.dataset import BIDSNifti
1110
from lib.imaging_lib.bids.json import add_bids_json_file_parameters
11+
from lib.imaging_lib.bids.mri.dataset import BIDSNifti
1212
from lib.imaging_lib.bids.tsv_scans import add_scan_tsv_file_parameters
1313
from lib.imaging_lib.bids.util import determine_bids_file_type
1414
from lib.imaging_lib.file import register_imaging_file

0 commit comments

Comments
 (0)