Skip to content

Commit

Permalink
add error on new pass on construct method
Browse files Browse the repository at this point in the history
  • Loading branch information
stephprince committed Dec 21, 2024
1 parent f971a1a commit 363d1fa
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 15 deletions.
19 changes: 16 additions & 3 deletions src/pynwb/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,19 @@ def _error_on_new_warn_on_construct(self, error_msg: str):
raise ValueError(error_msg)
warn(error_msg)

def _error_on_new_pass_on_construct(self, error_msg: str):
"""
Raise an error when a check is violated on instance creation.
When reading from a file, do nothing, ensuring that files with
invalid data or deprecated neurodata types can be read.
If error_msg is set to None the function will simply return
without further action.
"""
if error_msg is None:
return
if not self._in_construct_mode:
raise ValueError(error_msg)

def _get_type_map(self):
return get_type_map()

Expand Down Expand Up @@ -128,7 +141,7 @@ def __init__(self, **kwargs):
notes, description = popargs('notes', 'description', kwargs)
super().__init__(**kwargs)
if notes != '':
self._error_on_new_warn_on_construct(
self._error_on_new_pass_on_construct(
error_msg=("The `notes` argument of ScratchData.__init__ has been deprecated and will be removed in PyNWB 4.0. "
"Use description instead.")
)
Expand All @@ -137,7 +150,7 @@ def __init__(self, **kwargs):
'argument is recommended.')
description = notes
if not description:
self._error_on_new_warn_on_construct(error_msg='ScratchData.description is required.')
self._error_on_new_pass_on_construct(error_msg='ScratchData.description is required.')
self.description = description

@property
Expand All @@ -147,7 +160,7 @@ def notes(self):

@notes.setter
def notes(self, value):
self._error_on_new_warn_on_construct(
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.')
self.description = value

Expand Down
4 changes: 2 additions & 2 deletions src/pynwb/ecephys.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ class Clustering(NWBDataInterface):
'shape': (None,)},
{'name': 'name', 'type': str, 'doc': 'the name of this container', 'default': 'Clustering'})
def __init__(self, **kwargs):
self._error_on_new_warn_on_construct(
self._error_on_new_pass_on_construct(
error_msg='The Clustering neurodata type is deprecated. Use pynwb.misc.Units or NWBFile.units instead'
)
args_to_set = popargs_to_dict(('description', 'num', 'peak_over_rms', 'times'), kwargs)
Expand Down Expand Up @@ -263,7 +263,7 @@ class ClusterWaveforms(NWBDataInterface):
'doc': 'the standard deviations of waveforms for each cluster'},
{'name': 'name', 'type': str, 'doc': 'the name of this container', 'default': 'ClusterWaveforms'})
def __init__(self, **kwargs):
self._error_on_new_warn_on_construct(
self._error_on_new_pass_on_construct(
error_msg='The ClusterWaveforms neurodata type is deprecated. Use pynwb.misc.Units or NWBFile.units instead'
)
args_to_set = popargs_to_dict(('clustering_interface', 'waveform_filtering',
Expand Down
6 changes: 4 additions & 2 deletions src/pynwb/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,14 +492,14 @@ def __init__(self, **kwargs):
icephys_electrodes = args_to_set['icephys_electrodes']
ic_electrodes = args_to_set['ic_electrodes']
if icephys_electrodes is None and ic_electrodes is not None:
self._error_on_new_warn_on_construct(error_msg=("Use of the ic_electrodes parameter is deprecated. "
self._error_on_new_pass_on_construct(error_msg=("Use of the ic_electrodes parameter is deprecated. "
"Use the icephys_electrodes parameter instead"))
args_to_set['icephys_electrodes'] = ic_electrodes
args_to_set.pop('ic_electrodes') # do not set this arg

# backwards-compatibility for sweep table
if args_to_set['sweep_table'] is not None:
self._error_on_new_warn_on_construct(error_msg=("SweepTable is deprecated. Use the "
self._error_on_new_pass_on_construct(error_msg=("SweepTable is deprecated. Use the "
"IntracellularRecordingsTable instead. See also the "
"NWBFile.add_intracellular_recordings function."))

Expand Down Expand Up @@ -783,8 +783,10 @@ def _check_sweep_table(self):
"""
if self.sweep_table is None:
if self._in_construct_mode:
# Construct the SweepTable without triggering errors in construct mode because SweepTable has been deprecated
sweep_table = SweepTable.__new__(SweepTable, parent=self, in_construct_mode=True)
sweep_table.__init__(name='sweep_table')
sweep_table._in_construct_mode = False
else:
sweep_table = SweepTable(name='sweep_table')
self.sweep_table = sweep_table
Expand Down
1 change: 0 additions & 1 deletion src/pynwb/icephys.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,6 @@ def __init__(self, **kwargs):
'See also the NWBFile.add_intracellular_recordings function.')
if not self._in_construct_mode:
raise ValueError(error_msg)
warnings.warn(error_msg)

super().__init__(**kwargs)

Expand Down
9 changes: 5 additions & 4 deletions src/pynwb/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ def __init__(self, **kwargs):
if self._change_external_file_format():
self._error_on_new_warn_on_construct(error_msg=f"{self.__class__.__name__} '{self.name}': "
"The value for 'format' has been changed to 'external'. "
"Setting a default value for 'format' is deprecated.")
"If an external file is detected, setting a value for "
"'format' other than 'external' is deprecated.")

if not self._check_image_series_dimension():
warnings.warn(
Expand Down Expand Up @@ -194,7 +195,7 @@ def bits_per_pixel(self):
@bits_per_pixel.setter
def bits_per_pixel(self, val):
if val is not None:
self._error_on_new_warn_on_construct(error_msg="bits_per_pixel is deprecated")
self._error_on_new_pass_on_construct(error_msg="bits_per_pixel is deprecated")
self.fields['bits_per_pixel'] = val


Expand Down Expand Up @@ -251,13 +252,13 @@ class IndexSeries(TimeSeries):
def __init__(self, **kwargs):
indexed_timeseries, indexed_images = popargs('indexed_timeseries', 'indexed_images', kwargs)
if kwargs['unit'] and kwargs['unit'] != 'N/A':
self._error_on_new_warn_on_construct(error_msg=("The 'unit' field of IndexSeries is "
self._error_on_new_pass_on_construct(error_msg=("The 'unit' field of IndexSeries is "
"fixed to the value 'N/A'."))
if not indexed_timeseries and not indexed_images:
msg = "Either indexed_timeseries or indexed_images must be provided when creating an IndexSeries."
raise ValueError(msg)
if indexed_timeseries:
self._error_on_new_warn_on_construct("The indexed_timeseries field of IndexSeries is deprecated. "
self._error_on_new_pass_on_construct("The indexed_timeseries field of IndexSeries is deprecated. "
"Use the indexed_images field instead.")
kwargs['unit'] = 'N/A' # fixed value starting in NWB 2.5
super().__init__(**kwargs)
Expand Down
6 changes: 3 additions & 3 deletions src/pynwb/ophys.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,13 @@ def __init__(self, **kwargs):
args_to_set['optical_channel'] = [args_to_set['optical_channel']]
if args_to_set['manifold'] is not None:
error_msg = "The 'manifold' argument is deprecated in favor of 'origin_coords' and 'grid_spacing'."
self._error_on_new_warn_on_construct(error_msg=error_msg)
self._error_on_new_pass_on_construct(error_msg=error_msg)
if args_to_set['conversion'] != 1.0:
error_msg = "The 'conversion' argument is deprecated in favor of 'origin_coords' and 'grid_spacing'."
self._error_on_new_warn_on_construct(error_msg=error_msg)
self._error_on_new_pass_on_construct(error_msg=error_msg)
if args_to_set['unit'] != 'meters':
error_msg = "The 'unit' argument is deprecated in favor of 'origin_coords_unit' and 'grid_spacing_unit'."
self._error_on_new_warn_on_construct(error_msg=error_msg)
self._error_on_new_pass_on_construct(error_msg=error_msg)
for key, val in args_to_set.items():
setattr(self, key, val)

Expand Down

0 comments on commit 363d1fa

Please sign in to comment.