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

Adding txt support for labchart #54

Merged
merged 18 commits into from
Dec 2, 2019
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
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
69 changes: 69 additions & 0 deletions phys2bids/interfaces/txt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/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
"""
vinferrer marked this conversation as resolved.
Show resolved Hide resolved
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',orig_names[chtrig]]
smoia marked this conversation as resolved.
Show resolved Hide resolved
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