1818 along with this library; if not, write to the Free Software Foundation, Inc.,
1919 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
2020"""
21+ from collections import defaultdict
2122import logging
2223from multiprocessing import Pool
2324import numpy
2930from bluepyefe .plotting import _save_fig
3031from matplotlib .backends .backend_pdf import PdfPages
3132
33+ from bluepyefe .recording import Recording
34+
3235logger = logging .getLogger (__name__ )
3336
3437
35- class Cell (object ):
38+ def extract_efeatures_helper (recording , efeatures , efeature_names , efel_settings ):
39+ """Helper function to compute efeatures for a single recording."""
40+ recording .compute_efeatures (
41+ efeatures , efeature_names , efel_settings )
42+ return recording
43+
44+
45+ class Cell :
3646
3747 """Contains the metadata related to a cell as well as the
3848 electrophysiological recordings once they are read"""
@@ -47,7 +57,7 @@ def __init__(self, name):
4757
4858 self .name = name
4959
50- self .recordings = []
60+ self .recordings : dict [ str , list [ Recording ]] = defaultdict ( list )
5161 self .rheobase = None
5262
5363 def reader (self , config_data , recording_reader = None ):
@@ -91,9 +101,8 @@ def reader(self, config_data, recording_reader=None):
91101 )
92102
93103 def get_protocol_names (self ):
94- """List of all the protocols available for the present cell."""
95-
96- return list (set ([rec .protocol_name for rec in self .recordings ]))
104+ """List of all the protocol names available for the present cell."""
105+ return list (self .recordings .keys ())
97106
98107 def get_recordings_by_protocol_name (self , protocol_name ):
99108 """List of all the recordings available for the present cell for a
@@ -103,27 +112,7 @@ def get_recordings_by_protocol_name(self, protocol_name):
103112 protocol_name (str): name of the protocol for which to get
104113 the recordings.
105114 """
106-
107- return [
108- rec
109- for rec in self .recordings
110- if rec .protocol_name == protocol_name
111- ]
112-
113- def get_recordings_id_by_protocol_name (self , protocol_name ):
114- """List of the indexes of the recordings available for the present
115- cell for a given protocol.
116-
117- Args:
118- protocol_name (str): name of the protocol for which to get
119- the recordings.
120- """
121-
122- return [
123- i
124- for i , trace in enumerate (self .recordings )
125- if trace .protocol_name == protocol_name
126- ]
115+ return self .recordings .get (protocol_name )
127116
128117 def read_recordings (
129118 self ,
@@ -164,7 +153,7 @@ def read_recordings(
164153 protocol_name ,
165154 efel_settings
166155 )
167- self .recordings .append (rec )
156+ self .recordings [ protocol_name ] .append (rec )
168157 break
169158 else :
170159 raise KeyError (
@@ -173,12 +162,6 @@ def read_recordings(
173162 f"the available stimuli names"
174163 )
175164
176- def extract_efeatures_helper (self , recording_id , efeatures , efeature_names , efel_settings ):
177- """Helper function to compute efeatures for a single recording."""
178- self .recordings [recording_id ].compute_efeatures (
179- efeatures , efeature_names , efel_settings )
180- return self .recordings [recording_id ]
181-
182165 def extract_efeatures (
183166 self ,
184167 protocol_name ,
@@ -199,26 +182,26 @@ def extract_efeatures(
199182 is to be extracted several time on different sections
200183 of the same recording.
201184 """
202- recording_ids = self .get_recordings_id_by_protocol_name (protocol_name )
185+ recordings_of_protocol : list [ Recording ] = self .recordings . get (protocol_name )
203186
204187 # Run in parallel via multiprocessing
205188 with Pool (maxtasksperchild = 1 ) as pool :
206189 tasks = [
207- (rec_id , efeatures , efeature_names , efel_settings )
208- for rec_id in recording_ids
190+ (recording , efeatures , efeature_names , efel_settings )
191+ for recording in recordings_of_protocol
209192 ]
210- results = pool .starmap (self . extract_efeatures_helper , tasks )
193+ results = pool .starmap (extract_efeatures_helper , tasks )
211194
212- self .recordings = results
195+ self .recordings [ protocol_name ] = results
213196
214197 def compute_relative_amp (self ):
215198 """Compute the relative current amplitude for all the recordings as a
216199 percentage of the rheobase."""
217200
218201 if self .rheobase not in (0.0 , None , False , numpy .nan ):
219-
220- for i in range ( len ( self . recordings )) :
221- self . recordings [ i ] .compute_relative_amp (self .rheobase )
202+ for _ , recordings_list in self . recordings . items ():
203+ for recording in recordings_list :
204+ recording .compute_relative_amp (self .rheobase )
222205
223206 else :
224207
0 commit comments