Skip to content

Commit ea87fee

Browse files
committed
add eeg integration tests
1 parent 7545754 commit ea87fee

File tree

2 files changed

+112
-26
lines changed

2 files changed

+112
-26
lines changed

python/lib/eeg.py

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ def fetch_and_insert_eeg_files(self, derivatives=False, detect=True):
278278
with open(eegjson_file.path) as data_file:
279279
eeg_file_data = json.load(data_file)
280280

281-
eegjson_file_path = eegjson_file.path.replace(self.data_dir, '')
281+
eegjson_file_path = os.path.relpath(eegjson_file.path, self.data_dir)
282282
if self.loris_bids_root_dir:
283283
# copy the JSON file to the LORIS BIDS import directory
284284
eegjson_file_path = self.copy_file_to_loris_bids_dir(
@@ -317,7 +317,7 @@ def fetch_and_insert_eeg_files(self, derivatives=False, detect=True):
317317
# eeg_file_data dictionary
318318
fdt_file_path = None
319319
if file_type == 'set' and fdt_file:
320-
fdt_file_path = fdt_file.path.replace(self.data_dir, '')
320+
fdt_file_path = os.path.relpath(fdt_file, self.data_dir)
321321
if self.loris_bids_root_dir:
322322
# copy the fdt file to the LORIS BIDS import directory
323323
fdt_file_path = self.copy_file_to_loris_bids_dir(
@@ -344,7 +344,7 @@ def fetch_and_insert_eeg_files(self, derivatives=False, detect=True):
344344
# grep the modality ID from physiological_modality table
345345
modality_id = physiological_modality.grep_id_from_modality_value(self.data_type.name)
346346

347-
eeg_path = eeg_file.path.replace(self.data_dir, '')
347+
eeg_path = os.path.relpath(eeg_file.path, self.data_dir)
348348
if self.loris_bids_root_dir:
349349
# copy the eeg_file to the LORIS BIDS import directory
350350
eeg_path = self.copy_file_to_loris_bids_dir(
@@ -438,7 +438,7 @@ def fetch_and_insert_electrode_file(
438438
)
439439
if not result:
440440
electrode_data = utilities.read_tsv_file(electrode_file.path)
441-
electrode_path = electrode_file.path.replace(self.data_dir, '')
441+
electrode_path = os.path.relpath(electrode_file.path, self.data_dir)
442442
if self.loris_bids_root_dir:
443443
# copy the electrode file to the LORIS BIDS import directory
444444
electrode_path = self.copy_file_to_loris_bids_dir(
@@ -478,7 +478,7 @@ def fetch_and_insert_electrode_file(
478478
electrode_ids
479479
)
480480
else:
481-
electrode_metadata_path = coordsystem_metadata_file.path.replace(self.data_dir, '')
481+
electrode_metadata_path = os.path.relpath(coordsystem_metadata_file, self.data_dir)
482482
if self.loris_bids_root_dir:
483483
# copy the electrode metadata file to the LORIS BIDS import directory
484484
electrode_metadata_path = self.copy_file_to_loris_bids_dir(
@@ -547,7 +547,7 @@ def fetch_and_insert_channel_file(
547547
channel_path = result[0]['FilePath'] if result else None
548548
channel_data = utilities.read_tsv_file(channel_file.path)
549549
if not result:
550-
channel_path = channel_file.path.replace(self.data_dir, '')
550+
channel_path = os.path.relpath(channel_file.path, self.data_dir)
551551
if self.loris_bids_root_dir:
552552
# copy the channel file to the LORIS BIDS import directory
553553
channel_path = self.copy_file_to_loris_bids_dir(
@@ -632,7 +632,7 @@ def fetch_and_insert_event_files(
632632
'with physiological file ID ' + str(physiological_file_id)
633633
print(message)
634634
else:
635-
event_metadata_path = event_metadata_file.path.replace(self.data_dir, '')
635+
event_metadata_path = os.path.relpath(event_metadata_file.path, self.data_dir)
636636
if self.loris_bids_root_dir:
637637
# copy the event file to the LORIS BIDS import directory
638638
event_metadata_path = self.copy_file_to_loris_bids_dir(
@@ -657,7 +657,7 @@ def fetch_and_insert_event_files(
657657

658658
# get events.tsv file and insert
659659
event_data = utilities.read_tsv_file(event_data_file.path)
660-
event_path = event_data_file.path.replace(self.data_dir, '')
660+
event_path = os.path.relpath(event_data_file.path, self.data_dir)
661661
if self.loris_bids_root_dir:
662662
# copy the event file to the LORIS BIDS import directory
663663
event_path = self.copy_file_to_loris_bids_dir(
@@ -701,42 +701,39 @@ def copy_file_to_loris_bids_dir(self, file, derivatives=False, inheritance=False
701701
# Handle derivatives differently
702702
# Data path structure is unpredictable, so keep the same relative path
703703
if derivatives:
704-
copy_file = str.replace(
705-
file,
706-
self.bids_layout.root,
707-
""
708-
)
709-
704+
copy_file = os.path.relpath(file, self.bids_layout.root)
710705
copy_file = os.path.join(self.loris_bids_root_dir, copy_file)
711-
712-
# create derivative directories
713-
lib.utilities.create_dir(
714-
os.path.dirname(copy_file),
715-
self.verbose
716-
)
717706
else :
718707
# determine the path of the copied file
719708
copy_file = ""
720709
if not inheritance:
721710
copy_file = self.loris_bids_eeg_rel_dir
722711
if self.data_type.session.label:
723-
copy_file += os.path.basename(file)
712+
copy_file = os.path.join(copy_file, os.path.basename(file))
724713
else:
725714
# make sure the ses- is included in the new filename if using
726715
# default visit label from the LORIS config
727-
copy_file += str.replace(
728-
os.path.basename(file),
729-
"sub-" + self.data_type.subject.label,
730-
"sub-" + self.data_type.subject.label + "_ses-" + self.default_vl
716+
copy_file = os.path.join(
717+
copy_file,
718+
os.path.basename(file).replace(
719+
"sub-" + self.data_type.subject.label,
720+
"sub-" + self.data_type.subject.label + "_ses-" + self.default_vl
721+
)
731722
)
732723

733724
copy_file = os.path.join(self.loris_bids_root_dir, copy_file)
734725

726+
# create the archive directory if it does not exist
727+
lib.utilities.create_dir(
728+
os.path.dirname(copy_file),
729+
self.verbose
730+
)
731+
735732
# copy the file
736733
utilities.copy_file(file, copy_file, self.verbose)
737734

738735
# determine the relative path and return it
739-
relative_path = copy_file.replace(self.data_dir, "")
736+
relative_path = os.path.relpath(copy_file, self.data_dir)
740737

741738
return relative_path
742739

@@ -786,6 +783,12 @@ def create_and_insert_archive(self, files_to_archive, archive_rel_name,
786783
else:
787784
return
788785

786+
# create the file directory
787+
lib.utilities.create_dir(
788+
os.path.dirname(archive_full_path),
789+
self.verbose
790+
)
791+
789792
# create the archive file
790793
utilities.create_archive(files_to_archive, archive_full_path)
791794

@@ -841,6 +844,12 @@ def create_and_insert_event_archive(self, files_to_archive, archive_rel_name, ee
841844
else:
842845
return
843846

847+
# create the archive directory if it does not exist
848+
lib.utilities.create_dir(
849+
os.path.dirname(archive_full_path),
850+
self.verbose
851+
)
852+
844853
# create the archive file
845854
utilities.create_archive(files_to_archive, archive_full_path)
846855

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
from lib.db.queries.candidate import try_get_candidate_with_psc_id
2+
from lib.db.queries.config import set_config_with_setting_name
3+
from lib.db.queries.session import try_get_session_with_cand_id_visit_label
4+
from tests.util.database import get_integration_database_session
5+
from tests.util.file_system import check_file_tree
6+
from tests.util.run_integration_script import run_integration_script
7+
8+
9+
def test_import_eeg_bids_dataset():
10+
db = get_integration_database_session()
11+
12+
# Enable EEG chunking.
13+
set_config_with_setting_name(db, 'useEEGBrowserVisualizationComponents', 'true')
14+
db.commit()
15+
16+
process = run_integration_script([
17+
'import_bids_dataset.py',
18+
'--createcandidate', '--createsession',
19+
'--directory', '/data/loris/incoming/Face13',
20+
])
21+
22+
# Check the return code and standard error output.
23+
assert process.returncode == 0
24+
25+
# Check that the candidate and sessions are present in the database.
26+
candidate = try_get_candidate_with_psc_id(db, 'OTT166')
27+
assert candidate is not None
28+
session = try_get_session_with_cand_id_visit_label(db, candidate.cand_id, 'V1')
29+
assert session is not None
30+
31+
# TODO: Add EEG-specific database checks once the EEG-specific ORM models have been created.
32+
33+
# Check that the BIDS files have been copied.
34+
assert check_file_tree('/data/loris/bids_imports/', {
35+
'Face13_BIDSVersion_1.1.0': {
36+
'dataset_description.json': None,
37+
'participants.tsv': None,
38+
'README': None,
39+
'sub-OTT166': {
40+
'ses-V1': {
41+
'eeg': {
42+
'sub-OTT166_ses-V1_task-faceO_channels.tsv': None,
43+
'sub-OTT166_ses-V1_task-faceO_eeg.edf': None,
44+
'sub-OTT166_ses-V1_task-faceO_eeg.json': None,
45+
'sub-OTT166_ses-V1_task-faceO_electrodes.tsv': None,
46+
'sub-OTT166_ses-V1_task-faceO_events.tsv': None,
47+
}
48+
}
49+
}
50+
}
51+
})
52+
53+
# Check that the chunk files have been created.
54+
assert check_file_tree('/data/loris/bids_imports/', {
55+
'Face13_BIDSVersion_1.1.0_chunks': {
56+
'sub-OTT166_ses-V1_task-faceO_eeg.chunks': {
57+
'index.json': None,
58+
'raw': {
59+
'0': {
60+
str(i): {
61+
'0': {
62+
'0.buf': None,
63+
'1.buf': None,
64+
}
65+
} for i in range(0, 128)
66+
},
67+
'1': {
68+
str(i): {
69+
'0': {
70+
f'{j}.buf': None for j in range(0, 58)
71+
}
72+
} for i in range(1, 128)
73+
}
74+
}
75+
}
76+
}
77+
})

0 commit comments

Comments
 (0)