Skip to content

Commit 262e384

Browse files
committed
ENH+RF: inform which unit value is actually overwritten (ignored and set)
Otherwise user is left wondering "what was ignored? it might have been millivolts and now it became volts?" and alike. While at it I RF code to avoid duplication (evil, tiresome to change) in the touched code
1 parent da4b29b commit 262e384

2 files changed

Lines changed: 23 additions & 21 deletions

File tree

src/pynwb/icephys.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,7 @@ class CurrentClampSeries(PatchClampSeries):
116116
'default': 'volts'})
117117
def __init__(self, **kwargs):
118118
name, data, unit, electrode, gain = popargs('name', 'data', 'unit', 'electrode', 'gain', kwargs)
119-
if unit != 'volts':
120-
warnings.warn("Unit for %s '%s' is ignored and will be set to 'volts' as per NWB 2.1.0."
121-
% (self.__class__.__name__, name))
122-
unit = 'volts'
119+
unit = ensure_unit(self, name, unit, 'volts', '2.1.0')
123120
bias_current, bridge_balance, capacitance_compensation = popargs(
124121
'bias_current', 'bridge_balance', 'capacitance_compensation', kwargs)
125122
super().__init__(name, data, unit, electrode, gain, **kwargs)
@@ -170,10 +167,7 @@ class CurrentClampStimulusSeries(PatchClampSeries):
170167
'default': 'amperes'})
171168
def __init__(self, **kwargs):
172169
name, data, unit, electrode, gain = popargs('name', 'data', 'unit', 'electrode', 'gain', kwargs)
173-
if unit != 'amperes':
174-
warnings.warn("Unit for %s '%s' is ignored and will be set to 'amperes' as per "
175-
"NWB 2.1.0." % (self.__class__.__name__, name))
176-
unit = 'amperes'
170+
unit = ensure_unit(self, name, unit, 'amperes', '2.1.0')
177171
super().__init__(name, data, unit, electrode, gain, **kwargs)
178172

179173

@@ -209,10 +203,7 @@ class VoltageClampSeries(PatchClampSeries):
209203
'default': 'amperes'})
210204
def __init__(self, **kwargs):
211205
name, data, unit, electrode, gain = popargs('name', 'data', 'unit', 'electrode', 'gain', kwargs)
212-
if unit != 'amperes':
213-
warnings.warn("Unit for %s '%s' is ignored and will be set to 'amperes' as per NWB 2.1.0."
214-
% (self.__class__.__name__, name))
215-
unit = 'amperes'
206+
unit = ensure_unit(self, name, unit, 'amperes', '2.1.0')
216207
capacitance_fast, capacitance_slow, resistance_comp_bandwidth, resistance_comp_correction, \
217208
resistance_comp_prediction, whole_cell_capacitance_comp, whole_cell_series_resistance_comp = popargs(
218209
'capacitance_fast', 'capacitance_slow', 'resistance_comp_bandwidth',
@@ -245,10 +236,7 @@ class VoltageClampStimulusSeries(PatchClampSeries):
245236
'default': 'volts'})
246237
def __init__(self, **kwargs):
247238
name, data, unit, electrode, gain = popargs('name', 'data', 'unit', 'electrode', 'gain', kwargs)
248-
if unit != 'volts':
249-
warnings.warn("Unit for %s '%s' is ignored and will be set to 'volts' as per "
250-
"NWB 2.1.0." % (self.__class__.__name__, name))
251-
unit = 'volts'
239+
unit = ensure_unit(self, name, unit, 'volts', '2.1.0')
252240
super().__init__(name, data, unit, electrode, gain, **kwargs)
253241

254242

@@ -309,3 +297,17 @@ def __get_row_ids(self, sweep_number):
309297
"""
310298

311299
return [index for index, elem in enumerate(self['sweep_number'].data) if elem == sweep_number]
300+
301+
302+
def ensure_unit(self, name, current_unit, unit, nwb_version):
303+
"""A helper to ensure correct unit used.
304+
305+
Issues a warning with details if `current_unit` is to be ignored, and
306+
`unit` to be used instead.
307+
"""
308+
if current_unit != unit:
309+
warnings.warn(
310+
"Unit '%s' for %s '%s' is ignored and will be set to '%s' "
311+
"as per NWB %s."
312+
% (current_unit, self.__class__.__name__, name, unit, nwb_version))
313+
return unit

tests/unit/test_icephys.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ def test_init(self):
205205
def test_unit_warning(self):
206206
electrode_name = GetElectrode()
207207

208-
msg = "Unit for CurrentClampSeries 'test_cCS' is ignored and will be set to 'volts' as per NWB 2.1.0."
208+
msg = "Unit 'unit' for CurrentClampSeries 'test_cCS' is ignored and will be set to 'volts' as per NWB 2.1.0."
209209
with self.assertWarnsWith(UserWarning, msg):
210210
cCS = CurrentClampSeries('test_cCS', list(), electrode_name, 1.0, "stimset", 2.0, 3.0, 4.0,
211211
timestamps=list(), unit='unit')
@@ -229,7 +229,7 @@ def test_init(self):
229229
def test_unit_warning(self):
230230
electrode_name = GetElectrode()
231231

232-
msg = "Unit for IZeroClampSeries 'test_iZCS' is ignored and will be set to 'volts' as per NWB 2.1.0."
232+
msg = "Unit 'unit' for IZeroClampSeries 'test_iZCS' is ignored and will be set to 'volts' as per NWB 2.1.0."
233233
with self.assertWarnsWith(UserWarning, msg):
234234
iZCS = IZeroClampSeries('test_iZCS', list(), electrode_name, 1.0, timestamps=list(), unit='unit')
235235
self.assertEqual(iZCS.unit, 'volts')
@@ -249,7 +249,7 @@ def test_init(self):
249249
def test_unit_warning(self):
250250
electrode_name = GetElectrode()
251251

252-
msg = ("Unit for CurrentClampStimulusSeries 'test_cCSS' is ignored and will be set to 'amperes' as per NWB "
252+
msg = ("Unit 'unit' for CurrentClampStimulusSeries 'test_cCSS' is ignored and will be set to 'amperes' as per NWB "
253253
"2.1.0.")
254254
with self.assertWarnsWith(UserWarning, msg):
255255
cCSS = CurrentClampStimulusSeries('test_cCSS', list(), electrode_name, 1.0, timestamps=list(), unit='unit')
@@ -279,7 +279,7 @@ def test_init(self):
279279
def test_unit_warning(self):
280280
electrode_name = GetElectrode()
281281

282-
msg = "Unit for VoltageClampSeries 'test_vCS' is ignored and will be set to 'amperes' as per NWB 2.1.0."
282+
msg = "Unit 'unit' for VoltageClampSeries 'test_vCS' is ignored and will be set to 'amperes' as per NWB 2.1.0."
283283
with self.assertWarnsWith(UserWarning, msg):
284284
vCS = VoltageClampSeries('test_vCS', list(), electrode_name,
285285
1.0, "stimset", 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, timestamps=list(), unit='unit')
@@ -299,7 +299,7 @@ def test_init(self):
299299
def test_unit_warning(self):
300300
electrode_name = GetElectrode()
301301

302-
msg = "Unit for VoltageClampStimulusSeries 'test_vCSS' is ignored and will be set to 'volts' as per NWB 2.1.0."
302+
msg = "Unit 'unit' for VoltageClampStimulusSeries 'test_vCSS' is ignored and will be set to 'volts' as per NWB 2.1.0."
303303
with self.assertWarnsWith(UserWarning, msg):
304304
vCSS = VoltageClampStimulusSeries('test_vCSS', list(), electrode_name, 1.0, timestamps=list(), unit='unit')
305305
self.assertEqual(vCSS.unit, 'volts')

0 commit comments

Comments
 (0)