Skip to content

Commit

Permalink
Refactored phys2bids code.
Browse files Browse the repository at this point in the history
Restructure
  • Loading branch information
Stefano Moia authored Nov 28, 2019
2 parents 517442d + 42da7bd commit f2d7d52
Show file tree
Hide file tree
Showing 9 changed files with 770 additions and 199 deletions.
12 changes: 5 additions & 7 deletions phys2bids/cli/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ def _get_parser():
type=int,
help=('The number corresponding to the trigger channel.'
' Channel numbering starts with 0'),
default=1)
default=0)
optional.add_argument('-chsel', '--channel-selection',
dest='chsel',
nargs='*',
type=int,
help='The number corresponding to the channels to process.',
default=None)
optional.add_argument('-ntp', '--numtps',
dest='num_tps_expected',
dest='num_timepoints_expected',
type=int,
help='Number of expected timepoints.',
default=0)
Expand All @@ -95,14 +95,12 @@ def _get_parser():
type=float,
help='Threshold used for trigger detection.',
default=2.5)
optional.add_argument('-tbhd', '--table-header',
dest='table_header',
optional.add_argument('-chnames', '--channel-names',
dest='ch_name',
nargs='*',
type=str,
help='Columns header (for json file).',
# #!# Has to go to empty list
default=['time', 'respiratory_chest', 'trigger',
'cardiac', 'respiratory_CO2', 'respiratory_O2'])
default=[])

optional.add_argument('-v', '--version', action='version',
version=('%(prog)s ' + __version__))
Expand Down
10 changes: 3 additions & 7 deletions phys2bids/heuristics/heur.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,14 @@ def heur(physinfo, name, task='', acq='', direct='', rec='', run=''):
else:
# #!# Transform sys.exit in debug warnings or raiseexceptions!
# #!# Make all of the above a dictionary
print('File not found in heuristics!\nExiting')
sys.exit()
raise Warning(f'The heuristic {__file__} could not deal with {physinfo}')

if not task:
print('No "task" specified for this file!\nExiting')
sys.exit()
raise KeyError(f'No "task" attribute found')

name = name + '_task-' + task

# filename spec: sub-<label>[_ses-<label>]_task-<label>[_acq-<label>]
# [_ce-<label>][_dir-<label>][_rec-<label>]
# [_run-<index>][_recording-<label>]_physio
# filename spec: sub-<label>[_ses-<label>]_task-<label>[_acq-<label>][_ce-<label>][_dir-<label>][_rec-<label>][_run-<index>][_recording-<label>]_physio
if acq:
name = name + '_acq-' + acq

Expand Down
8 changes: 5 additions & 3 deletions phys2bids/heuristics/heur_ex.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,16 @@ def heur(physinfo, name, task='', acq='', direct='', rec='', run=''):
else:
# #!# Transform sys.exit in debug warnings or raiseexceptions!
# #!# Make all of the above a dictionary
sys.exit()
raise Warning(f'The heuristic {__file__} could not deal with {physinfo}')

if not task:
sys.exit()
raise KeyError(f'No "task" attribute found')

name = name + '_task-' + task

# filename spec: sub-<label>[_ses-<label>]_task-<label>[_acq-<label>][_ce-<label>][_dir-<label>][_rec-<label>][_run-<index>][_recording-<label>]_physio
# filename spec: sub-<label>[_ses-<label>]_task-<label>[_acq-<label>] ...
# ... [_ce-<label>][_dir-<label>][_rec-<label>] ...
# ... [_run-<index>][_recording-<label>]_physio
if acq:
name = name + '_acq-' + acq

Expand Down
55 changes: 55 additions & 0 deletions phys2bids/heuristics/heur_test_acq.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import fnmatch


def heur(physinfo, name, task='', acq='', direct='', rec='', run=''):
# ############################## #
# ## Modify here! ## #
# ## ## #
# ## Possible variables are: ## #
# ## -task (required) ## #
# ## -run ## #
# ## -rec ## #
# ## -acq ## #
# ## -direct ## #
# ## ## #
# ## ## #
# ## See example below ## #
# ############################## #

if fnmatch.fnmatchcase(physinfo, '*samefreq*'):
task = 'test'
run = '00'
rec = 'biopac'
elif physinfo == 'Example':
task = 'rest'
run = '01'
acq = 'resp'
# ############################## #
# ## Don't modify below this! ## #
# ############################## #
else:
# #!# Transform sys.exit in debug warnings or raiseexceptions!
# #!# Make all of the above a dictionary
raise Warning(f'The heuristic {__file__} could not deal with {physinfo}')

if not task:
raise KeyError(f'No "task" attribute found')

name = name + '_task-' + task

# filename spec: sub-<label>[_ses-<label>]_task-<label>[_acq-<label>] ...
# ... [_ce-<label>][_dir-<label>][_rec-<label>] ...
# ... [_run-<index>][_recording-<label>]_physio
if acq:
name = name + '_acq-' + acq

if direct:
name = name + '_dir-' + direct

if rec:
name = name + '_rec-' + rec

if run:
name = name + '_run-' + run

return name
32 changes: 32 additions & 0 deletions phys2bids/interfaces/acq.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
phys2bids interface for acqknowledge files.
"""
from bioread import read_file

from phys2bids.physio_obj import BlueprintInput


def populate_phys_input(filename, chtrig):
"""
Populate object phys_input
"""

data = read_file(filename).channels

freq = [data[chtrig].samples_per_second] * 2
timeseries = [data[chtrig].time_index, data[chtrig].data]
units = ['s', data[chtrig].units]
names = ['time', 'trigger']

for k, ch in enumerate(data):
if k != chtrig:
print(f'{k:02d}. {ch}')
timeseries.append(ch.data)
freq.append(ch.samples_per_second)
units.append(ch.units)
names.append(ch.name)

return BlueprintInput(timeseries, freq, names, units)
Loading

0 comments on commit f2d7d52

Please sign in to comment.