Skip to content

Commit

Permalink
Add reader for LCCR csv (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilkilic authored Jan 16, 2024
1 parent ae362c5 commit 35a2cec
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 4 deletions.
2 changes: 2 additions & 0 deletions bluepyefe/cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ def reader(self, config_data, recording_reader=None):
return igor_reader(config_data)
if ".nwb" in filename:
return nwb_reader(config_data)
if ".txt" in filename:
return csv_lccr_reader(config_data)

raise Exception(
"The format of the ephys files is unknown and no custom reader"
Expand Down
3 changes: 1 addition & 2 deletions bluepyefe/ecode/step.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,7 @@ def interpret(self, t, current, config_data, reader_data):
smooth_current = scipy_signal2d(current, 85)
_ = numpy.abs(smooth_current[idx_buffer:] - self.hypamp)
self.ton = idx_buffer + numpy.argmax(_ > step_threshold)

else:
elif self.hypamp is None:
# Infer the base current hypamp
self.hypamp = base_current(current, idx_ton=self.ton)

Expand Down
87 changes: 87 additions & 0 deletions bluepyefe/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import numpy
import scipy.io
from neo import io
import os

from . import igorpy
from .nwbreader import BBPNWBReader, ScalaNWBReader, AIBSNWBReader
Expand Down Expand Up @@ -199,3 +200,89 @@ def nwb_reader(in_data):
data = reader.read()

return data


def csv_lccr_reader(in_data):
"""Reader to read .txt (csv_lccr)
Args:
in_data (dict): of the format
{
'filepath': "./XXX.txt",
'dt': 0.1,
'ton': 2000,
'toff': 2500,
'ljp': 14.0,
'amplitudes': [10 -10 20 -20 30 -30 40 -40 50 -50],
'hypamp': -20 # (units should match 'amplitudes'),
'remove_last_100ms': True,
'v_unit': 'mV',
't_unit': 'ms',
'i_unit': 'pA' # current unit for 'amplitudes' and 'hypamp'
}
"""
_check_metadata(
in_data,
csv_lccr_reader.__name__,
["filepath", "dt", "amplitudes", "v_unit", "t_unit", "i_unit", "ton", "toff", "hypamp"],
)

data = []

fln = os.path.join(in_data['filepath'])
if not os.path.isfile(fln):
raise FileNotFoundError(
"Please provide a string with the filename of the txt file; "
f"current path not found: {fln}"
)

dt = in_data['dt']
ton = in_data['ton']
toff = in_data['toff']
amplitudes = in_data['amplitudes']
hypamp = in_data['hypamp']

import csv
with open(fln, 'rt') as f:
reader = csv.reader(f, delimiter='\t')
columns = list(zip(*reader))
length = numpy.shape(columns)[1]

voltages = numpy.array([
[
float(string) if string not in ["-", ""] else 0
for string in column
]
for column in columns
])
t = numpy.arange(length) * dt

# Remove last 100 ms if needed
if in_data.get('remove_last_100ms', False):
slice_end = int(-100. / dt)
voltages = voltages[:, :slice_end]
t = t[:slice_end]

for amplitude, voltage in zip(amplitudes, voltages):
current = numpy.zeros_like(voltage)
ion, ioff = int(ton / dt), int(toff / dt)
current[:] = hypamp
current[ion:ioff] = amplitude + hypamp
trace_data = {
"filename": os.path.basename(in_data['filepath']),
"current": current,
"voltage": voltage,
"t": t,
"dt": numpy.float64(dt),
"ton": numpy.float64(ton),
"toff": numpy.float64(toff),
"amp": numpy.float64(amplitude),
"hypamp": numpy.float64(hypamp),
"ljp": in_data.get('ljp', 0),
"i_unit": in_data['i_unit'],
"v_unit": in_data['v_unit'],
"t_unit": in_data['t_unit'],
}

data.append(trace_data)

return data
3 changes: 1 addition & 2 deletions bluepyefe/recording.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,7 @@ def standardize_trace(self, config_data, reader_data):
# WARNING: the ljp is informed as a positive float but we substract it
# from the voltage
if "ljp" in config_data and config_data["ljp"] is not None:
voltage = voltage - config_data["ljp"]

voltage = numpy.array(voltage) - config_data["ljp"]
if "repetition" in reader_data:
self.repetition = reader_data["repetition"]

Expand Down

0 comments on commit 35a2cec

Please sign in to comment.