From 57d2092e28f507af78c98b2c7dac8e49dfefa265 Mon Sep 17 00:00:00 2001 From: ilkilic Date: Mon, 16 Sep 2024 14:07:11 +0200 Subject: [PATCH 1/6] repetitions support for ScalaNWBReader --- bluepyefe/nwbreader.py | 64 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 58 insertions(+), 6 deletions(-) diff --git a/bluepyefe/nwbreader.py b/bluepyefe/nwbreader.py index f6cb932..c9cf451 100644 --- a/bluepyefe/nwbreader.py +++ b/bluepyefe/nwbreader.py @@ -28,6 +28,22 @@ def read(self): raise NotImplementedError() + def _get_repetition_keys(self, content, request_repetitions=None): + """ + Retrieve the keys of the traces based on the requested repetitions. + + Args: + content (dict): The content of the NWB file for one sweep or protocol, containing repetition data. + request_repetitions (list of int or int, optional): Specific repetitions to retrieve. If None, all repetitions are returned. + + Returns: + list of str: The keys of the traces that correspond to the requested repetitions. + + Raises: + NotImplementedError: If the method is called in a subclass that does not support repetition handling. + """ + raise NotImplementedError("This NWBReader does not support repetitions.") + def _format_nwb_trace(self, voltage, current, start_time, trace_name=None, repetition=None): """ Format the data from the NWB file to the format used by BluePyEfe @@ -103,6 +119,29 @@ def read(self): class ScalaNWBReader(NWBReader): + + def _get_repetition_keys(self, content, request_repetitions=None): + """ + Retrieve the keys of the traces based on the requested repetitions. + + Args: + content (dict): The content of the NWB file for one sweep or protocol, containing repetition data. + request_repetitions (list of int or int, optional): Specific repetitions to retrieve. If None, all repetitions are returned. + + Returns: + list of str: The keys of the traces that correspond to the requested repetitions. + """ + if isinstance(request_repetitions, (int, str)): + request_repetitions = [int(request_repetitions)] + + reps = list(content.keys()) + reps_id = [int(rep.replace("repetition ", "")) for rep in reps] + + if request_repetitions: + return [reps[reps_id.index(i)] for i in request_repetitions if i in reps_id] + else: + return reps + def read(self): """ Read and format the content of the NWB file @@ -132,12 +171,25 @@ def read(self): if key_current not in self.content['stimulus']['presentation']: continue - data.append(self._format_nwb_trace( - voltage=self.content['acquisition'][sweep]['data'], - current=self.content['stimulus']['presentation'][key_current]['data'], - start_time=self.content["acquisition"][sweep]["starting_time"], - trace_name=sweep, - )) + repetitions_content = self.content["acquisition"][sweep].get("repetitions", None) + + if repetitions_content: + rep_iter = self._get_repetition_keys(repetitions_content, request_repetitions=self.repetition) + for rep in rep_iter: + data.append(self._format_nwb_trace( + voltage=repetitions_content[rep]['data'], + current=self.content['stimulus']['presentation'][key_current]['data'], + start_time=repetitions_content[rep]["starting_time"], + trace_name=f"{sweep}_repetition_{rep}", + repetition=int(rep.replace("repetition ", "")) + )) + else: + data.append(self._format_nwb_trace( + voltage=self.content['acquisition'][sweep]['data'], + current=self.content['stimulus']['presentation'][key_current]['data'], + start_time=self.content["acquisition"][sweep]["starting_time"], + trace_name=sweep, + )) return data From 44c4ae09224e1a681fbafc89e80a40dfd01552e9 Mon Sep 17 00:00:00 2001 From: ilkilic Date: Wed, 18 Sep 2024 09:36:03 +0200 Subject: [PATCH 2/6] add repetition support --- bluepyefe/nwbreader.py | 43 ++++++++++++------------------------------ bluepyefe/reader.py | 5 +++-- 2 files changed, 15 insertions(+), 33 deletions(-) diff --git a/bluepyefe/nwbreader.py b/bluepyefe/nwbreader.py index c9cf451..fdbae9b 100644 --- a/bluepyefe/nwbreader.py +++ b/bluepyefe/nwbreader.py @@ -120,28 +120,6 @@ def read(self): class ScalaNWBReader(NWBReader): - def _get_repetition_keys(self, content, request_repetitions=None): - """ - Retrieve the keys of the traces based on the requested repetitions. - - Args: - content (dict): The content of the NWB file for one sweep or protocol, containing repetition data. - request_repetitions (list of int or int, optional): Specific repetitions to retrieve. If None, all repetitions are returned. - - Returns: - list of str: The keys of the traces that correspond to the requested repetitions. - """ - if isinstance(request_repetitions, (int, str)): - request_repetitions = [int(request_repetitions)] - - reps = list(content.keys()) - reps_id = [int(rep.replace("repetition ", "")) for rep in reps] - - if request_repetitions: - return [reps[reps_id.index(i)] for i in request_repetitions if i in reps_id] - else: - return reps - def read(self): """ Read and format the content of the NWB file @@ -151,6 +129,11 @@ def read(self): data = [] + if self.repetition: + repetitions_content = self.content['general']['intracellular_ephys']['intracellular_recordings']['repetition'] + if isinstance(self.repetition, (int, str)): + self.repetition = [int(self.repetition)] + for sweep in list(self.content['acquisition'].keys()): key_current = sweep.replace('Series', 'StimulusSeries') try: @@ -171,17 +154,15 @@ def read(self): if key_current not in self.content['stimulus']['presentation']: continue - repetitions_content = self.content["acquisition"][sweep].get("repetitions", None) - - if repetitions_content: - rep_iter = self._get_repetition_keys(repetitions_content, request_repetitions=self.repetition) - for rep in rep_iter: + if self.repetition: + sweep_id = int(sweep.split("_")[-1]) + if (int(repetitions_content[sweep_id]) in self.repetition): data.append(self._format_nwb_trace( - voltage=repetitions_content[rep]['data'], + voltage=self.content['acquisition'][sweep]['data'], current=self.content['stimulus']['presentation'][key_current]['data'], - start_time=repetitions_content[rep]["starting_time"], - trace_name=f"{sweep}_repetition_{rep}", - repetition=int(rep.replace("repetition ", "")) + start_time=self.content['acquisition'][sweep]["starting_time"], + trace_name=sweep, + repetition=repetitions_content[sweep_id] )) else: data.append(self._format_nwb_trace( diff --git a/bluepyefe/reader.py b/bluepyefe/reader.py index e791f3a..39d9b6a 100644 --- a/bluepyefe/reader.py +++ b/bluepyefe/reader.py @@ -209,12 +209,13 @@ def nwb_reader(in_data): content, target_protocols, in_data.get("repetition", None), - in_data.get("v_file", None) + in_data.get("v_file", None), + request_repetitions=in_data.get("repetition", None), ) elif "timeseries" in content["acquisition"].keys(): reader = AIBSNWBReader(content, target_protocols) else: - reader = ScalaNWBReader(content, target_protocols) + reader = ScalaNWBReader(content, target_protocols, repetition=in_data.get("repetition", None)) data = reader.read() From b1406725518c5e456d96d79b0caf753dc07c7bae Mon Sep 17 00:00:00 2001 From: ilkilic Date: Wed, 18 Sep 2024 09:58:38 +0200 Subject: [PATCH 3/6] fix --- bluepyefe/reader.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/bluepyefe/reader.py b/bluepyefe/reader.py index 39d9b6a..ba250bf 100644 --- a/bluepyefe/reader.py +++ b/bluepyefe/reader.py @@ -206,11 +206,10 @@ 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), - in_data.get("v_file", None), - request_repetitions=in_data.get("repetition", None), + content=content, + target_protocols=target_protocols, + v_file=in_data.get("v_file", None), + repetition=in_data.get("repetition", None), ) elif "timeseries" in content["acquisition"].keys(): reader = AIBSNWBReader(content, target_protocols) From 65a6cd2f77a049ddac0e000033d167b1f5f04af1 Mon Sep 17 00:00:00 2001 From: ilkilic Date: Wed, 18 Sep 2024 10:01:37 +0200 Subject: [PATCH 4/6] minor fix --- bluepyefe/nwbreader.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bluepyefe/nwbreader.py b/bluepyefe/nwbreader.py index fdbae9b..2dfa410 100644 --- a/bluepyefe/nwbreader.py +++ b/bluepyefe/nwbreader.py @@ -162,7 +162,7 @@ def read(self): current=self.content['stimulus']['presentation'][key_current]['data'], start_time=self.content['acquisition'][sweep]["starting_time"], trace_name=sweep, - repetition=repetitions_content[sweep_id] + repetition=int(repetitions_content[sweep_id]) )) else: data.append(self._format_nwb_trace( From 6774731a19c4177957dd964a2279e4bee1e9d198 Mon Sep 17 00:00:00 2001 From: ilkilic Date: Wed, 18 Sep 2024 10:21:36 +0200 Subject: [PATCH 5/6] minor update --- bluepyefe/nwbreader.py | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/bluepyefe/nwbreader.py b/bluepyefe/nwbreader.py index 2dfa410..9682783 100644 --- a/bluepyefe/nwbreader.py +++ b/bluepyefe/nwbreader.py @@ -28,21 +28,6 @@ def read(self): raise NotImplementedError() - def _get_repetition_keys(self, content, request_repetitions=None): - """ - Retrieve the keys of the traces based on the requested repetitions. - - Args: - content (dict): The content of the NWB file for one sweep or protocol, containing repetition data. - request_repetitions (list of int or int, optional): Specific repetitions to retrieve. If None, all repetitions are returned. - - Returns: - list of str: The keys of the traces that correspond to the requested repetitions. - - Raises: - NotImplementedError: If the method is called in a subclass that does not support repetition handling. - """ - raise NotImplementedError("This NWBReader does not support repetitions.") def _format_nwb_trace(self, voltage, current, start_time, trace_name=None, repetition=None): """ Format the data from the NWB file to the format used by BluePyEfe From c16da67c317fc52a665d02ceed5e1736f75493c0 Mon Sep 17 00:00:00 2001 From: ilkilic Date: Wed, 18 Sep 2024 10:39:00 +0200 Subject: [PATCH 6/6] lint fix --- bluepyefe/nwbreader.py | 1 - 1 file changed, 1 deletion(-) diff --git a/bluepyefe/nwbreader.py b/bluepyefe/nwbreader.py index 9682783..e441fe2 100644 --- a/bluepyefe/nwbreader.py +++ b/bluepyefe/nwbreader.py @@ -28,7 +28,6 @@ def read(self): raise NotImplementedError() - def _format_nwb_trace(self, voltage, current, start_time, trace_name=None, repetition=None): """ Format the data from the NWB file to the format used by BluePyEfe