Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add multi-run workflow synchronized with BIDS dataset #219

Closed
wants to merge 33 commits into from
Closed
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
65f314b
Start working on BIDS dataset-based multi-run converter.
tsalo May 8, 2020
69fecdb
Add some code. Not good code, but code.
tsalo May 8, 2020
7c169c5
Fix style issues.
tsalo May 8, 2020
1b198c4
Get synchronize_onsets basically working.
tsalo May 9, 2020
c0367c4
Add duration determination.
tsalo May 9, 2020
bd736e4
Minor refactor.
tsalo May 10, 2020
223adb1
More documentation.
tsalo May 12, 2020
7b262ba
Some more fixes.
tsalo May 12, 2020
7191a3e
Fix bugs and include echo keyword.
tsalo May 12, 2020
d15ad97
More flexible trigger channel selection for debugging.
tsalo May 18, 2020
eb46a71
More progress on multi-run conversion.
tsalo Jun 18, 2020
a5aad49
Merge remote-tracking branch 'physiopy/master' into enh/bids-multi-run
tsalo Jun 18, 2020
8591332
Merge pull request #1 from physiopy/master
tsalo Jul 3, 2020
9a5ce3b
Workflow is working now.
tsalo Jul 3, 2020
26f8aae
Fix style issues.
tsalo Jul 3, 2020
18f2831
Reorganize and improve documentation.
tsalo Jul 3, 2020
40fe26b
Fix bugs.
tsalo Jul 4, 2020
568d6aa
Use physio object instead of file.
tsalo Jul 4, 2020
4c3809c
Add requirements.
tsalo Jul 4, 2020
f0222cb
Add plotting function for QC.
tsalo Jul 4, 2020
b597880
fix.
tsalo Jul 4, 2020
d9d03fb
Reorganize functions and generalize update_bids_name.
tsalo Jul 8, 2020
92c99e8
Fix style issue.
tsalo Jul 8, 2020
17dfa07
Update.
tsalo Jul 19, 2020
40d792f
Merge branch 'master' into enh/bids-multi-run
tsalo Jul 19, 2020
cd1dea5
Get trigger update and resampling working.
tsalo Jul 19, 2020
233bb11
Update requirements.
tsalo Jul 19, 2020
8eaa407
Merge branch 'master' into enh/bids-multi-run
tsalo Oct 8, 2020
56f0ad0
Run black on files.
tsalo Oct 10, 2020
bb8166f
Move test of function I'd renamed.
tsalo Oct 10, 2020
428dd9f
Fix style issues.
tsalo Oct 10, 2020
bc7698a
Update conversion.py
tsalo Oct 10, 2020
2c7438f
Merge branch 'master' into enh/bids-multi-run
tsalo Jan 2, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions phys2bids/bids.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import os
import re
from csv import reader
from pathlib import Path

Expand Down Expand Up @@ -52,6 +53,86 @@
}


def update_bids_name(basename, **kwargs):
"""
Add entities, suffix, and/or extension to a BIDS filename while retaining
BIDS compatibility.

Parameters
----------
basename : str
Name of valid BIDS file.
kwargs : dict
Keyword arguments indicating entities and/or suffix and/or extension
to update/add to the BIDS filename. If a keyword's value is None, then
the entity will be removed from the filename.

Returns
-------
outname : str
Valid BIDS filename with updated entities/suffix/extension.
"""
# This is hardcoded, but would be nice to use the yaml-fied BIDS entity
# table when that's up and running.
ENTITY_ORDER = ['sub', 'ses', 'task', 'acq', 'ce', 'rec', 'dir', 'run',
'mod', 'echo', 'fa', 'inv', 'mt', 'part', 'ch',
'recording', 'proc', 'space', 'split']

outdir = os.path.dirname(basename)
outname = os.path.basename(basename)

# Determine scan suffix (should always be physio)
suffix = outname.split('_')[-1].split('.')[0]
extension = '.' + '.'.join(outname.split('_')[-1].split('.')[1:])
filetype = suffix + extension

for key, val in kwargs.items():
if key == 'suffix':
if not val.startswith('_'):
val = '_' + val

if not val.endswith('.'):
val = val + '.'

outname = outname.replace('_' + suffix + '.', val)
elif key == 'extension':
# add leading . if not ''
if val and not val.startswith('.'):
val = '.' + val
outname = outname.replace(extension, val)
else:
# entities
if key not in ENTITY_ORDER:
raise ValueError('Key {} not understood.'.format(key))

if val is None:
# remove entity from filename if kwarg val is None
regex = '_{}-[0-9a-zA-Z]+'.format(key)
outname = re.sub(regex, '', outname)
elif '_{}-{}'.format(key, val) in basename:
LGR.warning('Key-value pair {}/{} already found in '
'basename {}. Skipping.'.format(key, val, basename))
elif '_{}-'.format(key) in basename:
LGR.warning('Key {} already found in basename {}. '
'Overwriting.'.format(key, basename))
regex = '_{}-[0-9a-zA-Z]+'.format(key)
outname = re.sub(regex, '_{}-{}'.format(key, val), outname)
else:
loc = ENTITY_ORDER.index(key)
entities_to_check = ENTITY_ORDER[loc:]
entities_to_check = ['_{}-'.format(etc) for etc in entities_to_check]
entities_to_check.append('_{}'.format(filetype))
for etc in entities_to_check:
if etc in outname:
outname = outname.replace(
etc,
'_{}-{}{}'.format(key, val, etc)
)
break
outname = os.path.join(outdir, outname)
return outname


def bidsify_units(orig_unit):
"""
Read the input unit of measure and use the dictionary of aliases
Expand Down
Loading