Skip to content

Commit

Permalink
Updates and bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
laemtl authored and maltheism committed Aug 12, 2021
1 parent cd344f8 commit 24a8b4c
Show file tree
Hide file tree
Showing 12 changed files with 264 additions and 206 deletions.
21 changes: 9 additions & 12 deletions python/eeg2bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,17 @@ def connect(sid, environ):
return False # extra precaution.


def tarfile_bids_thread(data):
iEEG.TarFile(data)
def tarfile_bids_thread(bids_directory):
iEEG.TarFile(bids_directory)
response = {
'compression_time': 'example_5mins'
}
return eventlet.tpool.Proxy(response)


@sio.event
def tarfile_bids(sid, data):
# data = { bids_directory: '../path/to/bids/output', output_time: 'bids output time' }
response = eventlet.tpool.execute(tarfile_bids_thread, data)
def tarfile_bids(sid, bids_directory):
response = eventlet.tpool.execute(tarfile_bids_thread, bids_directory)
send = {
'compression_time': response['compression_time']
}
Expand Down Expand Up @@ -288,16 +287,14 @@ def edf_to_bids(sid, data):


@sio.event
def validate_bids(sid, data):
# data = { bids_directory: '../path/to/bids/output', output_time: 'bids output time' }
print('validate_bids: ', data)
def validate_bids(sid, bids_directory):
print('validate_bids: ', bids_directory)
error_messages = []
if 'bids_directory' not in data or not data['bids_directory']:
if not bids_directory:
error_messages.append('The BIDS output directory is missing.')
if 'output_time' not in data or not data['output_time']:
error_messages.append('The BIDS format is missing.')

if not error_messages:
BIDS.Validate(data)
BIDS.Validate(bids_directory)
response = {
'file_paths': BIDS.Validate.file_paths,
'result': BIDS.Validate.result
Expand Down
12 changes: 5 additions & 7 deletions python/libs/BIDS.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ class Validate:
file_paths = []
result = []

def __init__(self, data):
def __init__(self, bids_directory):
print('- Validate: init started.')
sep = os.path.sep
start_path = data['bids_directory'] + sep + data['output_time'] # current directory
file_paths = []
result = []
validator = BIDSValidator()
for path, dirs, files in os.walk(start_path):
for path, dirs, files in os.walk(bids_directory):
for filename in files:
if filename == '.bidsignore':
continue
Expand All @@ -25,9 +23,9 @@ def __init__(self, data):
continue

temp = os.path.join(path, filename)
file_paths.append(temp[len(start_path):len(temp)])
result.append(validator.is_bids(temp[len(start_path):len(temp)]))
# print(validator.is_bids(temp[len(start_path):len(temp)]))
file_paths.append(temp[len(bids_directory):len(temp)])
result.append(validator.is_bids(temp[len(bids_directory):len(temp)]))
# print(validator.is_bids(temp[len(bids_directory):len(temp)]))

self.set_file_paths(file_paths)
self.set_result(result)
Expand Down
36 changes: 18 additions & 18 deletions python/libs/Modifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def get_bids_root_path(self):

def get_eeg_path(self):
directory_path = 'sub-' + self.data['participantID'].replace('_', '').replace('-', '').replace(' ', '')

return os.path.join(
self.get_bids_root_path(),
directory_path,
Expand All @@ -40,29 +40,29 @@ def get_eeg_path(self):

def clean_dataset_files(self):
if len(self.data['edfData']['files']) > 0:
# for split recording, clean the duplicates _eeg.json and _channels.tsv
# for multiple run recording, clean the duplicates _eeg.json and _channels.tsv
channels_files = [f for f in os.listdir(self.get_eeg_path()) if f.endswith('_channels.tsv')]
for i in range(1, len(channels_files)):
filename = os.path.join(self.get_eeg_path(), channels_files[i])
os.remove(filename)
os.remove(filename)

sidecar_files = [f for f in os.listdir(self.get_eeg_path()) if f.endswith('eeg.json')]
for i in range(1, len(sidecar_files)):
filename = os.path.join(self.get_eeg_path(), sidecar_files[i])
os.remove(filename)
# remove the split suffix in the file names
os.remove(filename)

# remove the run suffix in the file names
fileOrig = os.path.join(self.get_eeg_path(), channels_files[0])
fileDest = os.path.join(
self.get_eeg_path(),
re.sub(r"_split-[0-9]+", '', channels_files[0])
re.sub(r"_run-[0-9]+", '', channels_files[0])
)
os.rename(fileOrig, fileDest)

fileOrig = os.path.join(self.get_eeg_path(), sidecar_files[0])
fileDest = os.path.join(
self.get_eeg_path(),
re.sub(r"_split-[0-9]+", '', sidecar_files[0])
re.sub(r"_run-[0-9]+", '', sidecar_files[0])
)
os.rename(fileOrig, fileDest)

Expand All @@ -84,7 +84,7 @@ def modify_dataset_description_json(self):
except IOError:
print("Could not read or write dataset_description.json file")


def modify_participants_tsv(self):
file_path = os.path.join(
self.get_bids_root_path(),
Expand Down Expand Up @@ -180,13 +180,13 @@ def copy_annotation_files(self):
for eegRun in self.data.get('eegRuns'):
edf_file = eegRun['edfBIDSBasename']
filename = os.path.join(self.get_eeg_path(), edf_file + '_annotations')

if eegRun['annotationsTSV']:
shutil.copyfile(
eegRun['annotationsTSV'],
os.path.join(self.get_eeg_path(), filename + '.tsv')
)

if eegRun['annotationsJSON']:
shutil.copyfile(
eegRun['annotationsJSON'],
Expand All @@ -198,7 +198,7 @@ def copy_event_files(self):
for eegRun in self.data.get('eegRuns'):
edf_file = eegRun['edfBIDSBasename']
filename = os.path.join(self.get_eeg_path(), edf_file + '_annotations')

if eegRun['eventFile']:
# events.tsv data collected:
output = []
Expand Down Expand Up @@ -228,7 +228,7 @@ def copy_event_files(self):
temp = os.path.join(path, filename)
if temp.endswith(eegRun['edfBIDSBasename'] + '_events.tsv'):
path_event_files = temp

# We now open BIDS events.tsv file if it exists
if path_event_files:
try:
Expand Down Expand Up @@ -272,7 +272,7 @@ def modify_eeg_json(self):
raise ValueError('Found more than one eeg.json file')

file_path = os.path.join(self.get_eeg_path(), eeg_json[0])

try:
with open(file_path, "r") as fp:
file_data = json.load(fp)
Expand All @@ -284,12 +284,12 @@ def modify_eeg_json(self):
referenceField = 'EGGReference'

file_data[referenceField] = self.data['reference']

if 'metadata' in self.data['bidsMetadata'] and 'invalid_keys' in self.data['bidsMetadata']:
for key in self.data['bidsMetadata']['metadata']:
if key not in self.data['bidsMetadata']['invalid_keys']:
if key not in self.data['bidsMetadata']['invalid_keys']:
file_data[key] = self.data['bidsMetadata']['metadata'][key]

with open(file_path, "w") as fp:
json.dump(file_data, fp, indent=4)

Expand Down
29 changes: 13 additions & 16 deletions python/libs/iEEG.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,23 +91,20 @@ class WriteError(PermissionError):

# TarFile - tarfile the BIDS data.
class TarFile:
# data = { bids_directory: '../path/to/bids/output', output_time: 'bids output time' }
def __init__(self, data):
def __init__(self, bids_directory):
import tarfile
import os.path
sep = os.path.sep
source_dir = data['bids_directory'] + sep + data['output_time'] # current directory
output_filename = data['bids_directory'] + sep + data['output_time'] + '.tar.gz'
output_filename = bids_directory + '.tar.gz'
with tarfile.open(output_filename, "w:gz") as tar:
tar.add(source_dir, arcname=os.path.basename(source_dir))
import platform
import subprocess
if platform.system() == 'Windows':
os.startfile(data['bids_directory'])
elif platform.system() == 'Darwin':
subprocess.Popen(['open', data['bids_directory']])
else:
subprocess.Popen(['xdg-open', data['bids_directory']])
tar.add(bids_directory, arcname=os.path.basename(bids_directory))

#import platform
#import subprocess
#if platform.system() == 'Windows':
# os.startfile(data['bids_directory'])
#elif platform.system() == 'Darwin':
# subprocess.Popen(['open', data['bids_directory']])
#else:
# subprocess.Popen(['xdg-open', data['bids_directory']])


# Anonymize - scrubs edf header data.
Expand Down Expand Up @@ -257,7 +254,7 @@ def to_bids(self,
bids_basename.update(session=session)

try:
write_raw_bids(raw, bids_basename, anonymize=dict(daysback=33630), overwrite=False, verbose=False)
write_raw_bids(raw, bids_basename, overwrite=False, verbose=False)
with open(bids_basename, 'r+b') as f:
f.seek(8) # id_info field starts 8 bytes in
f.write(bytes("X X X X".ljust(80), 'ascii'))
Expand Down
10 changes: 4 additions & 6 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
eventlet>=0.31.0
eventlet~=0.31.0
mne~=0.23.0
# MNE-BIDS 0.8 will support the .EDF extension
# Before its release we need to use the dev version
git+git://github.com/mne-tools/mne-bids@main#egg=mne-bids
mne-bids~=0.8
mne-features~=0.1
numpy~=1.19.5
python-socketio~=5.0.4
python-engineio~=4.0.0
bids-validator~=1.6.0
pybv>=0.4
requests>=2.25.0
pybv~=0.4
requests~=2.25.0
2 changes: 1 addition & 1 deletion src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const App = () => {
},
},
{
title: '4) Validator',
title: '4) Validate and package',
onClick: (e) => {
e.preventDefault();
setActiveMenuTab(3);
Expand Down
1 change: 1 addition & 0 deletions src/css/Authentication.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
cursor: default;
font-size: 12px;
background-color: #e9eaee;
width: 50%;
}
.loginMessage {
font-size: 14px;
Expand Down
7 changes: 6 additions & 1 deletion src/css/Configuration.css
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,9 @@ input[type="button"].primary-btn:disabled {
textarea {
vertical-align: top;
font-family: Arial;
}
}
.resetBtn {
text-align: right;
width: 50%;
background-color: #e9eaee;
}
Loading

0 comments on commit 24a8b4c

Please sign in to comment.