Skip to content

Commit

Permalink
Merge pull request #54 from vinferrer/txt
Browse files Browse the repository at this point in the history
Adding txt support for labchart
  • Loading branch information
Stefano Moia authored Dec 2, 2019
2 parents 27eb522 + d91682a commit 8a7ae2b
Show file tree
Hide file tree
Showing 5 changed files with 458,866 additions and 6 deletions.
80 changes: 80 additions & 0 deletions phys2bids/interfaces/txt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
phys2bids interface for txt files.
"""

import numpy as np
from phys2bids.physio_obj import BlueprintInput

def populate_phys_input(filename, chtrig):
"""
Populate object phys_input
for now this works only with labchart files
Input (Properties)
------------------
filename: str
path to the txt labchart file
chtrig : int
index of trigger channel
Output
------------------
BlueprintInput object for more see BlueprintInput docs
"""

header = []
channel_list = []
with open(filename,'r') as f:
header_l = 0
for line in f:
line=line.rstrip('\n').split('\t')
try:
float(line[0])
except ValueError:
header.append(line)
continue
line = [float(i) for i in line]
channel_list.append(line)
# get frequency
interval = header[0][1].split(" ")
if interval[-1] not in ['hr', 'min', 's','ms','µs']:
raise AttributeError(f'Interval unit "{interval[-1]}" is not in a valid LabChart time unit, '
'this probably means your file is not in labchart format')

if interval[-1] != 's':
print('Interval is not in seconds. Converting its value.')
if interval[-1] == 'hr':
interval[0] = float(interval)[0]*3600
interval[-1] = 's'
elif interval[-1] == 'min':
interval[0] = float(interval[0])*60
interval[-1] = 's'
elif interval[-1] == 'ms':
interval[0] = float(interval[0])/1000
interval[-1] = 's'
elif interval[-1] == 'µs':
interval[0] = float(interval[0])/1000000
interval[-1] = 's'
else:
interval[0] = float(interval[0])
# get units
range_list = header[5][1:]
units = []
for item in range_list:
units.append(item.split(' ')[1])
# get names
orig_names=header[4][1:]
names = ['time','trigger']
orig_names.pop(chtrig)
names=names+orig_names
# get channels
timeseries = np.matrix(channel_list).T.tolist()
freq = [1/interval[0]]*len(timeseries)
timeseries=[np.array(darray) for darray in timeseries]
ordered_timeseries=[timeseries[0],timeseries[chtrig]]
timeseries.pop(chtrig)
timeseries.pop(0)
ordered_timeseries=ordered_timeseries+timeseries
return BlueprintInput(ordered_timeseries, freq, names, units)
5 changes: 1 addition & 4 deletions phys2bids/phys2bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@
from phys2bids.cli.run import _get_parser
from phys2bids.physio_obj import BlueprintOutput

# #!# This is hardcoded until we find a better solution
HEADERLENGTH = 9


def print_summary(filename, ntp_expected, ntp_found, samp_freq, time_offset, outfile):
"""
Expand Down Expand Up @@ -205,7 +202,7 @@ def _main(argv=None):
if ftype == 'acq':
from phys2bids.interfaces.acq import populate_phys_input
elif ftype == 'txt':
raise NotImplementedError('txt not yet supported')
from phys2bids.interfaces.txt import populate_phys_input
else:
# #!# We should add a logger here.
raise NotImplementedError('Currently unsupported file type.')
Expand Down
2 changes: 1 addition & 1 deletion phys2bids/physio_obj.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def __init__(self, timeseries, freq, ch_name, units):
self.ch_amount = len(self.timeseries)
self.freq = has_size(is_valid(freq, list,
list_type=(int, float)),
self.ch_amount, 0)
self.ch_amount, 0.0)
self.ch_name = has_size(ch_name, self.ch_amount, 'unknown')
self.units = has_size(units, self.ch_amount, '[]')

Expand Down
Loading

0 comments on commit 8a7ae2b

Please sign in to comment.