Skip to content

Commit

Permalink
Merge pull request #174 from AurelienJaquier/nwb-select
Browse files Browse the repository at this point in the history
select nwb traces according to v_file field
  • Loading branch information
AurelienJaquier authored Mar 21, 2024
2 parents e49d247 + c740a44 commit ddd5335
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 7 deletions.
3 changes: 2 additions & 1 deletion bluepyefe/cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ def reader(self, config_data, recording_reader=None):

if "v_file" in config_data:
filename = config_data["v_file"]
elif "filepath" in config_data:
# if both present: use filepath. e.g. for some nwb that 'contain' igor files
if "filepath" in config_data:
filename = config_data["filepath"]

if recording_reader:
Expand Down
46 changes: 41 additions & 5 deletions bluepyefe/nwbreader.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@


class NWBReader:
def __init__(self, content, target_protocols, repetition=None):
def __init__(self, content, target_protocols, repetition=None, v_file=None):
""" Init
Args:
content (h5.File): NWB file
target_protocols (list of str): list of the protocols to be read and returned
repetition (list of int): id of the repetition(s) to be read and returned
v_file (str): name of original file that can be retrieved in sweep's description
"""

self.content = content
self.target_protocols = target_protocols
self.repetition = repetition
self.v_file = v_file

def read(self):
""" Read the content of the NWB file
Expand Down Expand Up @@ -84,7 +86,10 @@ def read(self):
if not isinstance(protocol_name, str):
protocol_name = protocol_name.decode('UTF-8')

if self.target_protocols and protocol_name not in self.target_protocols:
if (
self.target_protocols and
protocol_name.lower() not in [prot.lower() for prot in self.target_protocols]
):
continue

data.append(self._format_nwb_trace(
Expand All @@ -111,7 +116,10 @@ def read(self):
key_current = sweep.replace('Series', 'StimulusSeries')
protocol_name = "Step"

if self.target_protocols and protocol_name not in self.target_protocols:
if (
self.target_protocols and
protocol_name.lower() not in [prot.lower() for prot in self.target_protocols]
):
continue

if key_current not in self.content['stimulus']['presentation']:
Expand Down Expand Up @@ -162,8 +170,21 @@ def read(self):
for ecode in self.target_protocols:
for cell_id in self.content["data_organization"].keys():
if ecode not in self.content["data_organization"][cell_id]:
logger.debug(f"No eCode {ecode} in nwb.")
continue
new_ecode = next(
iter(
ec
for ec in self.content["data_organization"][cell_id]
if ec.lower() == ecode.lower()
)
)
if new_ecode:
logger.debug(
f"Could not find {ecode} in nwb file, will use {new_ecode} instead"
)
ecode = new_ecode
else:
logger.debug(f"No eCode {ecode} in nwb.")
continue

ecode_content = self.content["data_organization"][cell_id][ecode]

Expand All @@ -185,11 +206,26 @@ def read(self):
logger.debug(f"Ignoring {key_current} not"
" present in the stimulus presentation")
continue

if trace_name not in self.content["acquisition"]:
logger.debug(f"Ignoring {trace_name} not"
" present in the acquisition")
continue

# if we have v_file, check that trace comes from this original file
if self.v_file is not None:
attrs = self.content["acquisition"][trace_name].attrs
if "description" not in attrs:
logger.warning(
"Ignoring %s because no description could be found.",
trace_name
)
continue
v_file_end = self.v_file.split("/")[-1]
if v_file_end != attrs.get("description", "").split("/")[-1]:
logger.debug(f"Ignoring {trace_name} not matching v_file")
continue

data.append(self._format_nwb_trace(
voltage=self.content["acquisition"][trace_name]["data"],
current=self.content["stimulus"]["presentation"][key_current][
Expand Down
7 changes: 6 additions & 1 deletion bluepyefe/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,12 @@ def nwb_reader(in_data):

with h5py.File(in_data["filepath"], "r") as content:
if "data_organization" in content:
reader = BBPNWBReader(content, target_protocols, in_data.get("repetition", None))
reader = BBPNWBReader(
content,
target_protocols,
in_data.get("repetition", None),
in_data.get("v_file", None)
)
elif "timeseries" in content["acquisition"].keys():
reader = AIBSNWBReader(content, target_protocols)
else:
Expand Down

0 comments on commit ddd5335

Please sign in to comment.