Skip to content

Commit a12d199

Browse files
committed
Add support for config prePackagedDownloadPath and EEGChunksPath
1 parent 938e8ea commit a12d199

File tree

6 files changed

+59
-40
lines changed

6 files changed

+59
-40
lines changed

python/bids_import.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def main():
3838

3939
long_options = [
4040
"help", "profile=", "directory=",
41-
"createcandidate", "createsession", "idsvalidation", "nobidsvalidation", "type", "verbose"
41+
"createcandidate", "createsession", "idsvalidation", "nobidsvalidation", "type=", "verbose"
4242
]
4343
usage = (
4444
'\n'
@@ -58,7 +58,7 @@ def main():
5858
)
5959

6060
try:
61-
opts, args = getopt.getopt(sys.argv[1:], 'hp:d:csiv', long_options)
61+
opts, args = getopt.getopt(sys.argv[1:], 'hp:d:csint:v', long_options)
6262
except getopt.GetoptError:
6363
print(usage)
6464
sys.exit(lib.exitcode.GETOPT_FAILURE)
@@ -88,15 +88,15 @@ def main():
8888
config_file = input_error_checking(profile, bids_dir, usage)
8989

9090
dataset_json = bids_dir + "/dataset_description.json"
91-
if not os.path.isfile(dataset_json):
91+
if not os.path.isfile(dataset_json) and not type:
9292
print('No dataset_description.json found. Please run with the --type option.')
9393
print(usage)
94-
sys.exit()
94+
sys.exit(lib.exitcode.MISSING_ARG)
9595

96-
if type not in ('raw', 'derivatives'):
96+
if type and type not in ('raw', 'derivatives'):
9797
print("--type must be one of 'raw', 'derivatives'")
9898
print(usage)
99-
sys.exit()
99+
sys.exit(lib.exitcode.MISSING_ARG)
100100

101101
# database connection
102102
db = Database(config_file.mysql, verbose)

python/ingest_eeg_bids_datasets.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -150,23 +150,23 @@ def main():
150150

151151
try:
152152
result = subprocess.run(command, shell = True, capture_output=True)
153-
153+
154154
if result.stdout:
155155
print(result.stdout.decode('utf-8'))
156156

157-
if not result.stderr:
157+
if result.stderr:
158+
print(
159+
f'ERROR: EEG Dataset with uploadID {uploadid} ingestion log:\n ' + result.stderr.decode('utf-8')
160+
)
161+
162+
if result.returncode == 0:
158163
db.update(
159164
"UPDATE electrophysiology_uploader SET Status = 'Ingested' WHERE UploadID = %s",
160165
(uploadid,)
161166
)
162167
print('EEG Dataset with uploadID ' + uploadid + ' successfully ingested')
163168
continue
164169

165-
print(
166-
f'ERROR: EEG Dataset with uploadID {uploadid} failed ingestion. \
167-
Error was:\n ' + result.stderr.decode('utf-8')
168-
)
169-
170170
except OSError:
171171
print('ERROR: ' + script + ' not found')
172172

python/lib/eeg.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from lib.database_lib.physiologicalannotationfile import PhysiologicalAnnotationFile
1515
from lib.database_lib.physiologicalannotationarchive import PhysiologicalAnnotationArchive
1616
from lib.database_lib.physiologicalannotationrel import PhysiologicalAnnotationRel
17-
17+
from lib.database_lib.config import Config
1818

1919
__license__ = "GPLv3"
2020

@@ -105,6 +105,9 @@ def __init__(self, bids_reader, bids_sub_id, bids_ses_id, bids_modality, db,
105105
:type type : string
106106
"""
107107

108+
# config
109+
self.config_db_obj = Config(db, verbose)
110+
108111
# load bids objects
109112
self.bids_reader = bids_reader
110113
self.bids_layout = bids_reader.bids_layout
@@ -948,9 +951,7 @@ def create_and_insert_archive(self, files_to_archive, archive_rel_name,
948951
else:
949952
return
950953

951-
# create the archive file
952-
archive_rel_name = archive_rel_name.replace(self.data_dir, '')
953-
utilities.create_archive(files_to_archive, archive_rel_name, self.data_dir)
954+
(archive_rel_name, archive_full_path) = self.create_archive(files_to_archive, archive_rel_name)
954955

955956
# insert the archive file in physiological_archive
956957
blake2 = utilities.compute_blake2b_hash(archive_full_path)
@@ -1003,9 +1004,22 @@ def create_and_insert_annotation_archive(self, files_to_archive, archive_rel_nam
10031004
else:
10041005
return
10051006

1006-
# create the archive file
1007-
utilities.create_archive(files_to_archive, archive_rel_name, self.data_dir)
1007+
(archive_rel_name, archive_full_path) = self.create_archive(files_to_archive, archive_rel_name)
10081008

10091009
# insert the archive into the physiological_annotation_archive table
10101010
blake2 = utilities.compute_blake2b_hash(archive_full_path)
10111011
physiological_annotation_archive_obj.insert(eeg_file_id, blake2, archive_rel_name)
1012+
1013+
def create_archive(self, files_to_archive, archive_rel_name):
1014+
# create the archive file
1015+
package_path = self.config_db_obj.get_config("prePackagedDownloadPath")
1016+
if package_path:
1017+
raw_package_dir = os.path.join(package_path, 'raw')
1018+
os.makedirs(raw_package_dir, exist_ok=True)
1019+
archive_rel_name = os.path.basename(archive_rel_name)
1020+
archive_full_path = os.path.join(raw_package_dir, archive_rel_name)
1021+
utilities.create_archive(files_to_archive, archive_full_path)
1022+
else:
1023+
archive_full_path = os.path.join(self.data_dir, archive_rel_name)
1024+
utilities.create_archive(files_to_archive, archive_full_path)
1025+
return (archive_rel_name, archive_full_path)

python/lib/physiological.py

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from lib.database_lib.physiologicalannotationparameter import PhysiologicalAnnotationParameter
1010
from lib.database_lib.physiologicalannotationlabel import PhysiologicalAnnotationLabel
1111
from lib.database_lib.physiologicalannotationinstance import PhysiologicalAnnotationInstance
12-
12+
from lib.database_lib.config import Config
1313

1414
__license__ = "GPLv3"
1515

@@ -56,6 +56,7 @@ def __init__(self, db, verbose):
5656

5757
self.db = db
5858
self.verbose = verbose
59+
self.config_db_obj = Config(self.db, self.verbose)
5960

6061
self.physiological_annotation_file_obj = PhysiologicalAnnotationFile(self.db, self.verbose)
6162
self.physiological_annotation_parameter_obj = PhysiologicalAnnotationParameter(self.db, self.verbose)
@@ -179,6 +180,7 @@ def insert_physio_parameter_file(self, physiological_file_id,
179180
parameter_file_values = (
180181
physiological_file_id, parameter_type_id, value
181182
)
183+
182184
self.db.insert(
183185
table_name='physiological_parameter_file',
184186
column_names=parameter_file_fields,
@@ -977,32 +979,32 @@ def create_chunks_for_visualization(self, physio_file_id, data_dir):
977979

978980
# check if chunks already exists for this PhysiologicalFileID
979981
results = self.grep_parameter_value_from_file_id(
980-
physio_file_id, 'electrophyiology_chunked_dataset_path'
982+
physio_file_id, 'electrophysiology_chunked_dataset_path'
981983
)
982984
chunk_path = results['Value'] if results else None
983985

984-
# determine which script to run based on the file type
985-
command = None
986-
script = None
986+
# No chunks found
987987
if not chunk_path:
988-
file_type = self.grep_file_type_from_file_id(physio_file_id)
989-
file_path = self.grep_file_path_from_file_id(physio_file_id)
990-
# the bids_rel_dir is the first two directories in file_path (
991-
# bids_imports/BIDS_dataset_name_BIDSVersion)
992-
bids_rel_dir = file_path.split('/')[0] + '/' + file_path.split('/')[1]
993-
chunk_root_dir = data_dir + bids_rel_dir + '_chunks' + '/'
994-
# the final chunk path will be /data/%PROJECT%/data/bids_imports
995-
# /BIDS_dataset_name_BIDSVersion_chunks/EEG_FILENAME.chunks
996-
chunk_path = chunk_root_dir + os.path.splitext(os.path.basename(file_path))[0] + '.chunks'
988+
script = None
989+
file_path = self.grep_file_path_from_file_id(physio_file_id)
990+
991+
chunk_root_dir = self.config_db_obj.get_config("EEGChunksPath")
992+
if not chunk_root_dir:
993+
# the bids_rel_dir is the first two directories in file_path (
994+
# bids_imports/BIDS_dataset_name_BIDSVersion)
995+
bids_rel_dir = file_path.split('/')[0] + '/' + file_path.split('/')[1]
996+
chunk_root_dir = data_dir + bids_rel_dir + '_chunks' + '/'
997+
998+
# determine which script to run based on the file type
999+
file_type = self.grep_file_type_from_file_id(physio_file_id)
9971000
if file_type == 'set':
9981001
script = os.environ['LORIS_MRI'] + '/python/react-series-data-viewer/eeglab_to_chunks.py'
9991002
command = 'python ' + script + ' ' + data_dir + file_path + ' --destination ' + chunk_root_dir
10001003
elif file_type == 'edf':
10011004
script = os.environ['LORIS_MRI'] + '/python/react-series-data-viewer/edf_to_chunks.py'
10021005
command = 'python ' + script + ' ' + data_dir + file_path + ' --destination ' + chunk_root_dir
10031006

1004-
# chunk the electrophysiology dataset if a command was determined above
1005-
if command:
1007+
# chunk the electrophysiology dataset if a command was determined above
10061008
try:
10071009
subprocess.call(
10081010
command,
@@ -1016,9 +1018,12 @@ def create_chunks_for_visualization(self, physio_file_id, data_dir):
10161018
print('ERROR: ' + script + ' not found')
10171019
sys.exit(lib.exitcode.CHUNK_CREATION_FAILURE)
10181020

1021+
# the final chunk path will be /data/%PROJECT%/data/bids_imports
1022+
# /BIDS_dataset_name_BIDSVersion_chunks/EEG_FILENAME.chunks
1023+
chunk_path = chunk_root_dir + os.path.splitext(os.path.basename(file_path))[0] + '.chunks'
10191024
if os.path.isdir(chunk_path):
10201025
self.insert_physio_parameter_file(
10211026
physiological_file_id = physio_file_id,
1022-
parameter_name = 'electrophyiology_chunked_dataset_path',
1027+
parameter_name = 'electrophysiology_chunked_dataset_path',
10231028
value = chunk_path.replace(data_dir, '')
10241029
)

python/lib/utilities.py

100644100755
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def create_dir(dir_name, verbose):
138138
return dir_name
139139

140140

141-
def create_archive(files_to_archive, archive_rel_name, data_dir):
141+
def create_archive(files_to_archive, archive_path):
142142
"""
143143
Creates an archive with the files listed in the files_to_archive tuple.
144144
@@ -151,8 +151,8 @@ def create_archive(files_to_archive, archive_rel_name, data_dir):
151151
"""
152152

153153
# if the archive does not already exists, create it
154-
if not os.path.isfile(data_dir + archive_rel_name):
155-
tar = tarfile.open(data_dir + archive_rel_name, "w:gz")
154+
if not os.path.isfile(archive_path):
155+
tar = tarfile.open(archive_path, "w:gz")
156156
for file in files_to_archive:
157157
filename = os.path.basename(file)
158158
tar.add(file, arcname=filename, recursive=False)

python/react-series-data-viewer/eeglab_to_chunks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def load_channels(path):
3030
args = parser.parse_args()
3131
for path in args.files:
3232
eeg = mne_eeglab._check_load_mat(path, None)
33-
eeglab_info = mne_eeglab._get_info(eeg, eog=(), montage_units="auto")
33+
eeglab_info = mne_eeglab._get_info(eeg, eog=())
3434
channel_names = eeglab_info[0]['ch_names']
3535

3636
if args.channel_index < 0:

0 commit comments

Comments
 (0)