Skip to content

Commit

Permalink
Update emptyroom code to pull a secondary file if the original emptyr…
Browse files Browse the repository at this point in the history
…oom was not packaged correctly
  • Loading branch information
jstout211 committed Apr 22, 2024
1 parent 15b5b88 commit 3e6fac1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
11 changes: 9 additions & 2 deletions nih2mne/make_meg_bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,15 @@ def get_eroom(meg_fname, tmpdir=None):
print('Pulling and untar/unzip emptyroom')
pull_eroom(er_fname, tmpdir=tmpdir)
er_fname = op.join(tmpdir, op.basename(er_fname)).replace('.tgz','.ds')
logger.info(f'Using {er_fname} for emptyroom')
return er_fname
#Sometimes the compiled emptyroom file is not a functional file - add failover
if op.exists(er_fname):
logger.info(f'Using {er_fname} for emptyroom')
return er_fname
else:
er_fname = get_closest_eroom(meg_fname, failover=True)
pull_eroom(er_fname, tmpdir=tmpdir)
er_fname = op.join(tmpdir, op.basename(er_fname)).replace('.tgz','.ds')
return er_fname

def _check_markerfile(ds_fname):
'''
Expand Down
10 changes: 8 additions & 2 deletions nih2mne/utilities/emptyroom_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ def convert_meg_datetime(meg_fname):
i=meg_fname.split('_')[2]
return datetime.datetime(int(i[0:4]), int(i[4:6]), int(i[6:8]))

def get_closest_eroom(meg_fname, eroom_dict=None, eroom_location=None):
def get_closest_eroom(meg_fname, eroom_dict=None, eroom_location=None,
failover=False):
'''
Return the path of the emptyroom file that matches closest the date of
your current meg file.
Expand All @@ -51,6 +52,8 @@ def get_closest_eroom(meg_fname, eroom_dict=None, eroom_location=None):
Override if not using biowulf. The default is None.
eroom_location : TYPE, optional
Override if not using biowulf. The default is None.
failover : BOOL
Use this option to try an alternative (if the pulled tarfile doesnt unpack)
Returns
-------
Expand All @@ -69,12 +72,15 @@ def get_closest_eroom(meg_fname, eroom_dict=None, eroom_location=None):
assert len(eroom_dict) > 0
megdate = convert_meg_datetime(meg_fname)
res = min(eroom_dict.keys(), key=lambda curr: abs(curr - megdate))
if failover==True:
eroom_dict.pop(res)
res = min(eroom_dict.keys(), key=lambda curr: abs(curr - megdate))
closest_eroom = eroom_dict[res]
return closest_eroom

def pull_eroom(eroom_fname, tmpdir=None):
cmd=f'tar -xf {eroom_fname} -C {tmpdir}'
subprocess.run(cmd.split())
subprocess.run(cmd.split(), check=True)



Expand Down

0 comments on commit 3e6fac1

Please sign in to comment.