From ccd55222d1376825903b1d53c412feda4e72b8ac Mon Sep 17 00:00:00 2001 From: Adam Reeve Date: Mon, 16 Sep 2024 15:57:54 +1200 Subject: [PATCH] Fix tests for numpy 2 (#336) --- nptdms/export/hdf_export.py | 2 +- nptdms/test/test_thermocouples.py | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/nptdms/export/hdf_export.py b/nptdms/export/hdf_export.py index 77b9a5e..627bbbb 100644 --- a/nptdms/export/hdf_export.py +++ b/nptdms/export/hdf_export.py @@ -92,5 +92,5 @@ def _hdf_attr_value(value): """ Convert a value into a format suitable for an HDF attribute """ if isinstance(value, np.datetime64): - return np.string_(np.datetime_as_string(value, unit='us', timezone='UTC')) + return np.bytes_(np.datetime_as_string(value, unit='us', timezone='UTC')) return value diff --git a/nptdms/test/test_thermocouples.py b/nptdms/test/test_thermocouples.py index d901f06..a443cc5 100644 --- a/nptdms/test/test_thermocouples.py +++ b/nptdms/test/test_thermocouples.py @@ -193,3 +193,21 @@ def _test_voltage_to_temperature(reference_thermocouple, thermocouple, voltage, reference_temperature = reference_thermocouple.inverse_CmV(voltage, Tref=0.0) temperature = thermocouple.mv_to_celsius(voltage) assert abs(temperature - reference_temperature) < max_error + 1.0E-6 + + +@pytest.fixture(autouse=True) +def patch_thermocouples_reference(monkeypatch): + """ + thermocouples_reference creates an array with np.array(T, copy=False) + which causes an error on numpy 2 for non-array inputs. + We can't workaround this by only passing in numpy arrays as there are also + internal uses that cause this error, so monkeypatch to fix this. + """ + prev_call = thermocouples_reference.function_types.Polynomial_Gaussian_Piecewise_Function.__call__ + + def new_call(self, t, *args, **kwargs): + t = np.asarray(t) + return prev_call(self, t, *args, **kwargs) + + monkeypatch.setattr( + thermocouples_reference.function_types.Polynomial_Gaussian_Piecewise_Function, "__call__", new_call)