Skip to content

Commit

Permalink
remove warnings on construct for deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
stephprince committed Dec 21, 2024
1 parent 363d1fa commit bb2d82d
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 171 deletions.
6 changes: 4 additions & 2 deletions src/pynwb/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,15 @@ def __init__(self, **kwargs):

@property
def notes(self):
warn('Use of ScratchData.notes has been deprecated and will be removed in PyNWB 4.0. Use ScratchData.description instead.', DeprecationWarning)
warn(("Use of ScratchData.notes has been deprecated and will be removed in PyNWB 4.0. "
"Use ScratchData.description instead."), DeprecationWarning)
return self.description

@notes.setter
def notes(self, value):
self._error_on_new_pass_on_construct(
error_msg='Use of ScratchData.notes has been deprecated and will be removed in PyNWB 4.0. Use ScratchData.description instead.')
error_msg=("Use of ScratchData.notes has been deprecated and will be removed in PyNWB 4.0. "
"Use ScratchData.description instead."))
self.description = value


Expand Down
13 changes: 12 additions & 1 deletion src/pynwb/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,17 @@ def objects(self):
def epoch_tags(self):
return set(self.epochs.tags[:]) if self.epochs is not None else set()

@property
def icephys_filtering(self):
return self.fields.get('icephys_filtering')

@icephys_filtering.setter
def icephys_filtering(self, val):
if val is not None:
self._error_on_new_warn_on_construct("Use of icephys_filtering is deprecated. "
"Use the IntracellularElectrode.filtering field instead")
self.fields['icephys_filtering'] = val

def __check_epochs(self):
if self.epochs is None:
self.epochs = TimeIntervals(name='epochs', description='experimental epochs')
Expand Down Expand Up @@ -1017,7 +1028,7 @@ def add_scratch(self, **kwargs):
data, name, notes, table_description, description = getargs('data', 'name', 'notes', 'table_description',
'description', kwargs)
if notes is not None or table_description != '':
warn(('Use of the `notes` or `table_description` argument is deprecate and will be removed in PyNWB 4.0. '
warn(('Use of the `notes` or `table_description` argument is deprecated and will be removed in PyNWB 4.0. '
'Use the `description` argument instead.'), DeprecationWarning)
if description is not None:
raise ValueError('Cannot call add_scratch with (notes or table_description) and description')
Expand Down
37 changes: 14 additions & 23 deletions tests/integration/hdf5/test_ecephys.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,22 +184,19 @@ def setUpContainer(self):
Clustering(**kwargs)

# create object in construct mode, modeling the behavior of the ObjectMapper on read
# no warning should be raised
obj = Clustering.__new__(Clustering, in_construct_mode=True)
with self.assertWarnsWith(warn_type=UserWarning, exc_msg=error_msg):
obj.__init__(**kwargs)
obj.__init__(**kwargs)

return obj

def roundtripContainer(self, cache_spec=False):
# catch the DeprecationWarning raised when reading the Clustering object from file
error_msg = "The Clustering neurodata type is deprecated. Use pynwb.misc.Units or NWBFile.units instead"
with self.assertWarnsWith(UserWarning, error_msg):
return super().roundtripContainer(cache_spec)
# no warning or error should be raised when reading the Clustering object from file
return super().roundtripContainer(cache_spec)

def roundtripExportContainer(self, cache_spec=False):
error_msg = "The Clustering neurodata type is deprecated. Use pynwb.misc.Units or NWBFile.units instead"
with self.assertWarnsWith(UserWarning, error_msg):
return super().roundtripExportContainer(cache_spec)
# no warning or error should be raised when reading the Clustering object from file
return super().roundtripExportContainer(cache_spec)


class SpikeEventSeriesConstructor(NWBH5IOFlexMixin, TestCase):
Expand Down Expand Up @@ -240,11 +237,9 @@ def setUpContainer(self):
peak_over_rms = [5.3, 6.3]

# raise error on write
msg = "The Clustering neurodata type is deprecated. Use pynwb.misc.Units or NWBFile.units instead"
clust = Clustering.__new__(Clustering, in_construct_mode=True)
with self.assertWarnsWith(warn_type=UserWarning, exc_msg=msg):
clust.__init__('description', num, peak_over_rms, times)
self.clustering = clust
clust.__init__('description', num, peak_over_rms, times)
self.clustering = clust

means = [[7.3, 7.3]]
stdevs = [[8.3, 8.3]]
Expand All @@ -253,12 +248,12 @@ def setUpContainer(self):
cw = ClusterWaveforms(self.clustering, 'filtering', means, stdevs)

# create object in construct mode, modeling the behavior of the ObjectMapper on read
# no warning should be raised
cw = ClusterWaveforms.__new__(ClusterWaveforms,
container_source=None,
parent=None,
in_construct_mode=True)
with self.assertWarnsWith(warn_type=UserWarning, exc_msg=msg):
cw.__init__(self.clustering, 'filtering', means, stdevs)
cw.__init__(self.clustering, 'filtering', means, stdevs)

return cw

Expand All @@ -268,16 +263,12 @@ def addContainer(self, nwbfile):
nwbfile.add_acquisition(self.container)

def roundtripContainer(self, cache_spec=False):
# catch the DeprecationWarning raised when reading the Clustering object from file
msg = "The ClusterWaveforms neurodata type is deprecated. Use pynwb.misc.Units or NWBFile.units instead"
with self.assertWarnsWith(UserWarning, msg):
return super().roundtripContainer(cache_spec)
# no warning or error should be raised when reading the Clustering object from file
return super().roundtripContainer(cache_spec)

def roundtripExportContainer(self, cache_spec=False):
# catch the DeprecationWarning raised when reading the Clustering object from file
msg = "The ClusterWaveforms neurodata type is deprecated. Use pynwb.misc.Units or NWBFile.units instead"
with self.assertWarnsWith(UserWarning, msg):
return super().roundtripExportContainer(cache_spec)
# no warning or error should be raised when reading the Clustering object from file
return super().roundtripExportContainer(cache_spec)


class FeatureExtractionConstructor(NWBH5IOFlexMixin, TestCase):
Expand Down
40 changes: 14 additions & 26 deletions tests/integration/hdf5/test_icephys.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,11 @@ def setUpContainer(self):
stimulus_description="gotcha ya!", sweep_number=np.uint(4711))

# create the sweeptable in construct mode, modeling the behavior of the ObjectMapper on read
# no user warning or error should be raised
sweeptable = SweepTable.__new__(SweepTable)
sweeptable._in_construct_mode = True
msg = ("SweepTable is deprecated. Use the IntracellularRecordingsTable instead. "
"See also the NWBFile.add_intracellular_recordings function.")
with self.assertWarnsWith(warn_type=UserWarning, exc_msg=msg):
sweeptable.__init__(name='sweep_table')
sweeptable.__init__(name='sweep_table')
sweeptable._in_construct_mode = False

return sweeptable

Expand All @@ -181,17 +180,13 @@ def getContainer(self, nwbfile):

def roundtripContainer(self, cache_spec=False):
# catch the DeprecationWarning raised when reading the SweepTable object from file
msg = ("SweepTable is deprecated. Use the IntracellularRecordingsTable instead. "
"See also the NWBFile.add_intracellular_recordings function.")
with self.assertWarnsWith(UserWarning, msg):
return super().roundtripContainer(cache_spec)
# no warning or error message should be raised
return super().roundtripContainer(cache_spec)

def roundtripExportContainer(self, cache_spec=False):
# catch the DeprecationWarning raised when reading the SweepTable object from file
msg = ("SweepTable is deprecated. Use the IntracellularRecordingsTable instead. "
"See also the NWBFile.add_intracellular_recordings function.")
with self.assertWarnsWith(UserWarning, msg):
return super().roundtripExportContainer(cache_spec)
# no warning or error message should be raised
return super().roundtripExportContainer(cache_spec)

def test_container(self):
""" Test properties of the SweepTable read from file """
Expand Down Expand Up @@ -229,12 +224,11 @@ def setUpContainer(self):
stimulus_description="gotcha ya!", sweep_number=np.uint(4712))

# create the sweeptable in construct mode, modeling the behavior of the ObjectMapper on read
# no warning or error should be raised
sweeptable = SweepTable.__new__(SweepTable)
sweeptable._in_construct_mode = True
msg = ("SweepTable is deprecated. Use the IntracellularRecordingsTable instead. "
"See also the NWBFile.add_intracellular_recordings function.")
with self.assertWarnsWith(warn_type=UserWarning, exc_msg=msg):
sweeptable.__init__(name='sweep_table')
sweeptable.__init__(name='sweep_table')
sweeptable._in_construct_mode = False

return sweeptable

Expand All @@ -258,18 +252,12 @@ def getContainer(self, nwbfile):
return nwbfile.sweep_table

def roundtripContainer(self, cache_spec=False):
# catch the DeprecationWarning raised when reading the SweepTable object from file
msg = ("SweepTable is deprecated. Use the IntracellularRecordingsTable instead. "
"See also the NWBFile.add_intracellular_recordings function.")
with self.assertWarnsWith(UserWarning, msg):
return super().roundtripContainer(cache_spec)
# no warning or error should be raised when reading the SweepTable object from file
return super().roundtripContainer(cache_spec)

def roundtripExportContainer(self, cache_spec=False):
# catch the DeprecationWarning raised when reading the SweepTable object from file
msg = ("SweepTable is deprecated. Use the IntracellularRecordingsTable instead. "
"See also the NWBFile.add_intracellular_recordings function.")
with self.assertWarnsWith(UserWarning, msg):
return super().roundtripExportContainer(cache_spec)
# no warning or error should be raised when reading the SweepTable object from file
return super().roundtripExportContainer(cache_spec)

def test_container(self):
""" Test properties of the SweepTable read from file """
Expand Down
12 changes: 8 additions & 4 deletions tests/unit/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,17 @@ def test_add_data_interface(self):

def test_deprecated_add_data_interface(self):
ts = self._create_time_series()
msg = 'add_data_interface is deprecated and will be removed in PyNWB 4.0. Use add instead.'
with self.assertWarnsWith(warn_type=DeprecationWarning,
exc_msg="add_data_interface is deprecated. Use add instead."
exc_msg=msg
):
self.pm.add_data_interface(ts)

def test_deprecated_add_container(self):
ts = self._create_time_series()
msg = 'add_container is deprecated and will be removed in PyNWB 4.0. Use add instead.'
with self.assertWarnsWith(warn_type=DeprecationWarning,
exc_msg="add_container is deprecated. Use add instead."
exc_msg=msg
):
self.pm.add_container(ts)

Expand All @@ -68,17 +70,19 @@ def test_get_data_interface(self):
def test_deprecated_get_data_interface(self):
ts = self._create_time_series()
self.pm.add(ts)
msg = 'get_data_interface is deprecated and will be removed in PyNWB 4.0. Use get instead.'
with self.assertWarnsWith(warn_type=DeprecationWarning,
exc_msg="get_data_interface is deprecated. Use get instead."
exc_msg=msg
):
tmp = self.pm.get_data_interface("test_ts")
self.assertIs(tmp, ts)

def test_deprecated_get_container(self):
ts = self._create_time_series()
self.pm.add(ts)
msg = 'get_container is deprecated and will be removed in PyNWB 4.0. Use get instead.'
with self.assertWarnsWith(warn_type=DeprecationWarning,
exc_msg="get_container is deprecated. Use get instead."
exc_msg=msg
):
tmp = self.pm.get_container("test_ts")
self.assertIs(tmp, ts)
Expand Down
12 changes: 5 additions & 7 deletions tests/unit/test_ecephys.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,9 @@ def test_init(self):
cc = Clustering(**kwargs)

# create object in construct mode, modeling the behavior of the ObjectMapper on read
# no error or warning should be raised
cc = Clustering.__new__(Clustering, in_construct_mode=True)
with self.assertWarnsWith(warn_type=UserWarning, exc_msg=error_msg):
cc.__init__(**kwargs)
cc.__init__(**kwargs)

self.assertEqual(cc.description, 'description')
self.assertEqual(cc.num, num)
Expand All @@ -306,13 +306,11 @@ def test_init(self):
peak_over_rms = [5.3, 6.3]

# create object in construct mode, modeling the behavior of the ObjectMapper on read
error_msg = "The Clustering neurodata type is deprecated. Use pynwb.misc.Units or NWBFile.units instead"
cc = Clustering.__new__(Clustering,
container_source=None,
parent=None,
in_construct_mode=True)
with self.assertWarnsWith(warn_type=UserWarning, exc_msg=error_msg):
cc.__init__('description', num, peak_over_rms, times)
cc.__init__('description', num, peak_over_rms, times)

means = [[7.3, 7.3]]
stdevs = [[8.3, 8.3]]
Expand All @@ -321,12 +319,12 @@ def test_init(self):
cw = ClusterWaveforms(cc, 'filtering', means, stdevs)

# create object in construct mode, modeling the behavior of the ObjectMapper on read
# no error or warning should be raised
cw = ClusterWaveforms.__new__(ClusterWaveforms,
container_source=None,
parent=None,
in_construct_mode=True)
with self.assertWarnsWith(warn_type=UserWarning, exc_msg=error_msg):
cw.__init__(cc, 'filtering', means, stdevs)
cw.__init__(cc, 'filtering', means, stdevs)

self.assertEqual(cw.clustering_interface, cc)
self.assertEqual(cw.waveform_filtering, 'filtering')
Expand Down
43 changes: 11 additions & 32 deletions tests/unit/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,23 +126,14 @@ def test_access_group_after_io(self):

remove_test_file("electrodes_mwe.nwb")

def test_access_processing(self):
def test_access_processing_with_modules(self):
self.nwbfile.create_processing_module('test_mod', 'test_description')
msg = 'NWBFile.modules is deprecated. Use NWBFile.processing instead.'

# create object with deprecated argument
with self.assertRaisesWith(ValueError, msg):
msg = "'NWBFile' object has no attribute 'modules'"
with self.assertRaisesWith(AttributeError, msg):
self.nwbfile.modules['test_mod']

# create object in construct mode, modeling the behavior of the ObjectMapper on read
self.nwbfile._in_construct_mode = True
with self.assertWarnsWith(warn_type=UserWarning, exc_msg=msg):
modules = self.nwbfile.modules['test_mod']
self.assertIs(self.nwbfile.processing['test_mod'], modules)

# reset construct mode for other tests
self.nwbfile._in_construct_mode = False

def test_epoch_tags(self):
tags1 = ['t1', 't2']
tags2 = ['t3', 't4']
Expand Down Expand Up @@ -178,7 +169,7 @@ def test_add_stimulus(self):

def test_add_stimulus_timeseries_arg(self):
"""Test nwbfile.add_stimulus using the deprecated 'timeseries' keyword argument"""
msg = ("NWBFile.add_stimulus: unrecognized argument: 'timeseries'")
msg = ("NWBFile.add_stimulus: missing argument 'stimulus', unrecognized argument: 'timeseries'")
with self.assertRaisesWith(TypeError, msg):
self.nwbfile.add_stimulus(
timeseries=TimeSeries(
Expand All @@ -191,9 +182,9 @@ def test_add_stimulus_timeseries_arg(self):

def test_add_stimulus_no_stimulus_arg(self):
"""Test nwbfile.add_stimulus using the deprecated 'timeseries' keyword argument"""
msg = ("The 'stimulus' keyword argument is required.")
with self.assertRaisesWith(ValueError, msg):
self.nwbfile.add_stimulus(None)
msg = ("NWBFile.add_stimulus: missing argument 'stimulus'")
with self.assertRaisesWith(TypeError, msg):
self.nwbfile.add_stimulus()
self.assertEqual(len(self.nwbfile.stimulus), 0)

def test_add_stimulus_dynamic_table(self):
Expand Down Expand Up @@ -525,26 +516,14 @@ def test_ec_electrodes_deprecation(self):
nwbfile.add_electrode(location='loc1', group=elecgrp, id=0)

# test that NWBFile.ec_electrodes property warns or errors
msg = "NWBFile.ec_electrodes is deprecated. Use NWBFile.electrodes instead."
with self.assertRaisesWith(ValueError, msg):
msg = "'NWBFile' object has no attribute 'ec_electrodes'"
with self.assertRaisesWith(AttributeError, msg):
nwbfile.ec_electrodes

nwbfile._in_construct_mode = True
with self.assertWarnsWith(warn_type=UserWarning, exc_msg=msg):
nwbfile.ec_electrodes
self.assertEqual(nwbfile.ec_electrodes.location[0], 'loc1')
nwbfile._in_construct_mode = False

# test that NWBFile.ec_electrode_groups warns or errors
msg = "NWBFile.ec_electrode_groups is deprecated. Use NWBFile.electrode_groups instead."
with self.assertRaisesWith(ValueError, msg):
nwbfile.ec_electrode_groups

nwbfile._in_construct_mode = True
with self.assertWarnsWith(warn_type=UserWarning, exc_msg=msg):
msg = "'NWBFile' object has no attribute 'ec_electrode_groups'"
with self.assertRaisesWith(AttributeError, msg):
nwbfile.ec_electrode_groups
self.assertEqual(nwbfile.ec_electrode_groups['name'].description, 'desc')
nwbfile._in_construct_mode = False

class SubjectTest(TestCase):
def setUp(self):
Expand Down
Loading

0 comments on commit bb2d82d

Please sign in to comment.