From 2fa41e7db7c69172a73931e7d409e71b62b49a67 Mon Sep 17 00:00:00 2001 From: Thomas VINCENT Date: Thu, 14 Mar 2024 15:14:04 +0100 Subject: [PATCH] Fix numpy compatibility spotted by ruff --- examples/customHdf5TreeModel.py | 2 +- examples/hdf5widget.py | 2 +- src/silx/gui/data/RecordTableView.py | 2 +- src/silx/gui/data/TextFormatter.py | 4 ++-- src/silx/gui/hdf5/Hdf5Formatter.py | 4 ++-- src/silx/gui/hdf5/test/test_hdf5.py | 24 ++++++++++++------------ src/silx/gui/plot/items/image.py | 2 +- src/silx/io/fabioh5.py | 28 ++++++++++++++-------------- src/silx/io/octaveh5.py | 14 +++++++------- src/silx/io/test/test_fabioh5.py | 8 ++++---- 10 files changed, 45 insertions(+), 45 deletions(-) diff --git a/examples/customHdf5TreeModel.py b/examples/customHdf5TreeModel.py index 59c57507fc..63f014bda4 100644 --- a/examples/customHdf5TreeModel.py +++ b/examples/customHdf5TreeModel.py @@ -132,7 +132,7 @@ def get_hdf5_with_all_types(): g.create_dataset("int64", data=numpy.int64(10)) g.create_dataset("float32", data=numpy.float32(10)) g.create_dataset("float64", data=numpy.float64(10)) - g.create_dataset("string_", data=numpy.string_("Hi!")) + g.create_dataset("string_", data=numpy.bytes_("Hi!")) # g.create_dataset("string0",data=numpy.string0("Hi!\x00")) g.create_dataset("bool", data=True) diff --git a/examples/hdf5widget.py b/examples/hdf5widget.py index 187480ee66..0f7ffbf741 100755 --- a/examples/hdf5widget.py +++ b/examples/hdf5widget.py @@ -95,7 +95,7 @@ def get_hdf5_with_all_types(): g.create_dataset("int64", data=numpy.int64(10)) g.create_dataset("float32", data=numpy.float32(10)) g.create_dataset("float64", data=numpy.float64(10)) - g.create_dataset("string_", data=numpy.string_("Hi!")) + g.create_dataset("string_", data=numpy.bytes_("Hi!")) # g.create_dataset("string0",data=numpy.string0("Hi!\x00")) g.create_dataset("bool", data=True) diff --git a/src/silx/gui/data/RecordTableView.py b/src/silx/gui/data/RecordTableView.py index 8bf16833a4..bda3bea5f5 100644 --- a/src/silx/gui/data/RecordTableView.py +++ b/src/silx/gui/data/RecordTableView.py @@ -433,7 +433,7 @@ def setArrayData(self, data): sourceModel.setArrayData(data) if data is not None: - if issubclass(data.dtype.type, (numpy.string_, numpy.unicode_)): + if issubclass(data.dtype.type, (numpy.bytes_, numpy.str_)): # TODO it would be nice to also fix fields # but using it only for string array is already very useful self.setItemDelegateForColumn(0, self.__multilineView) diff --git a/src/silx/gui/data/TextFormatter.py b/src/silx/gui/data/TextFormatter.py index aee242744d..90ff0c34d1 100644 --- a/src/silx/gui/data/TextFormatter.py +++ b/src/silx/gui/data/TextFormatter.py @@ -311,9 +311,9 @@ def toString(self, data, dtype=None): ) return "(" + " ".join(text) + ")" return self.__formatBinary(data) - elif isinstance(data, (numpy.unicode_, str)): + elif isinstance(data, (numpy.str_, str)): return self.__formatText(data) - elif isinstance(data, (numpy.string_, bytes)): + elif isinstance(data, (numpy.bytes_, bytes)): if dtype is None and hasattr(data, "dtype"): dtype = data.dtype if dtype is not None: diff --git a/src/silx/gui/hdf5/Hdf5Formatter.py b/src/silx/gui/hdf5/Hdf5Formatter.py index 99e0bb6022..be48691f18 100644 --- a/src/silx/gui/hdf5/Hdf5Formatter.py +++ b/src/silx/gui/hdf5/Hdf5Formatter.py @@ -122,12 +122,12 @@ def humanReadableType(self, dataset, full=False): return self.humanReadableDType(dtype, full) def humanReadableDType(self, dtype, full=False): - if dtype == bytes or numpy.issubdtype(dtype, numpy.string_): + if dtype == bytes or numpy.issubdtype(dtype, numpy.bytes_): text = "string" if full: text = "ASCII " + text return text - elif dtype == str or numpy.issubdtype(dtype, numpy.unicode_): + elif dtype == str or numpy.issubdtype(dtype, numpy.str_): text = "string" if full: text = "UTF-8 " + text diff --git a/src/silx/gui/hdf5/test/test_hdf5.py b/src/silx/gui/hdf5/test/test_hdf5.py index 1271b48a04..2bb25304f6 100755 --- a/src/silx/gui/hdf5/test/test_hdf5.py +++ b/src/silx/gui/hdf5/test/test_hdf5.py @@ -502,9 +502,9 @@ def testNXentryStartTime(self): """Test NXentry with start_time""" model = hdf5.Hdf5TreeModel() h5 = commonh5.File("/foo/bar/1.mock", "w") - create_NXentry(h5, "a").create_dataset("start_time", data=numpy.string_("2015")) - create_NXentry(h5, "b").create_dataset("start_time", data=numpy.string_("2013")) - create_NXentry(h5, "c").create_dataset("start_time", data=numpy.string_("2014")) + create_NXentry(h5, "a").create_dataset("start_time", data=numpy.bytes_("2015")) + create_NXentry(h5, "b").create_dataset("start_time", data=numpy.bytes_("2013")) + create_NXentry(h5, "c").create_dataset("start_time", data=numpy.bytes_("2014")) model.insertH5pyObject(h5) proxy = hdf5.NexusSortFilterProxyModel() @@ -518,13 +518,13 @@ def testNXentryStartTimeInArray(self): model = hdf5.Hdf5TreeModel() h5 = commonh5.File("/foo/bar/1.mock", "w") create_NXentry(h5, "a").create_dataset( - "start_time", data=numpy.array([numpy.string_("2015")]) + "start_time", data=numpy.array([numpy.bytes_("2015")]) ) create_NXentry(h5, "b").create_dataset( - "start_time", data=numpy.array([numpy.string_("2013")]) + "start_time", data=numpy.array([numpy.bytes_("2013")]) ) create_NXentry(h5, "c").create_dataset( - "start_time", data=numpy.array([numpy.string_("2014")]) + "start_time", data=numpy.array([numpy.bytes_("2014")]) ) model.insertH5pyObject(h5) @@ -539,13 +539,13 @@ def testNXentryEndTimeInArray(self): model = hdf5.Hdf5TreeModel() h5 = commonh5.File("/foo/bar/1.mock", "w") create_NXentry(h5, "a").create_dataset( - "end_time", data=numpy.array([numpy.string_("2015")]) + "end_time", data=numpy.array([numpy.bytes_("2015")]) ) create_NXentry(h5, "b").create_dataset( - "end_time", data=numpy.array([numpy.string_("2013")]) + "end_time", data=numpy.array([numpy.bytes_("2013")]) ) create_NXentry(h5, "c").create_dataset( - "end_time", data=numpy.array([numpy.string_("2014")]) + "end_time", data=numpy.array([numpy.bytes_("2014")]) ) model.insertH5pyObject(h5) @@ -574,9 +574,9 @@ def testStartTime(self): """If it is not NXentry, start_time is not used""" model = hdf5.Hdf5TreeModel() h5 = commonh5.File("/foo/bar/1.mock", "w") - h5.create_group("a").create_dataset("start_time", data=numpy.string_("2015")) - h5.create_group("b").create_dataset("start_time", data=numpy.string_("2013")) - h5.create_group("c").create_dataset("start_time", data=numpy.string_("2014")) + h5.create_group("a").create_dataset("start_time", data=numpy.bytes_("2015")) + h5.create_group("b").create_dataset("start_time", data=numpy.bytes_("2013")) + h5.create_group("c").create_dataset("start_time", data=numpy.bytes_("2014")) model.insertH5pyObject(h5) proxy = hdf5.NexusSortFilterProxyModel() diff --git a/src/silx/gui/plot/items/image.py b/src/silx/gui/plot/items/image.py index 18310d92be..ee28c5764f 100644 --- a/src/silx/gui/plot/items/image.py +++ b/src/silx/gui/plot/items/image.py @@ -281,7 +281,7 @@ def getValueData(self, copy=True): else: dtype = numpy.float64 data = numpy.array(data, dtype=dtype, copy=True) - data[mask != 0] = numpy.NaN + data[mask != 0] = numpy.nan self.__valueDataCache = data return numpy.array(self.__valueDataCache, copy=copy) diff --git a/src/silx/io/fabioh5.py b/src/silx/io/fabioh5.py index 89e838ba80..acb4f49324 100755 --- a/src/silx/io/fabioh5.py +++ b/src/silx/io/fabioh5.py @@ -156,11 +156,11 @@ def _create_data(self): data = "\n".join(data) try: line = data.encode("ascii") - types.add(numpy.string_) + types.add(numpy.bytes_) except UnicodeEncodeError: try: line = data.encode("utf-8") - types.add(numpy.unicode_) + types.add(numpy.str_) except UnicodeEncodeError: # Fallback in void line = numpy.void(data) @@ -170,12 +170,12 @@ def _create_data(self): if numpy.void in types: dtype = numpy.void - elif numpy.unicode_ in types: - dtype = numpy.unicode_ + elif numpy.str_ in types: + dtype = numpy.str_ else: - dtype = numpy.string_ + dtype = numpy.bytes_ - if dtype == numpy.unicode_: + if dtype == numpy.str_: # h5py only support vlen unicode dtype = h5py.special_dtype(vlen=str) @@ -622,10 +622,10 @@ def _convert_metadata_vector(self, values): result_type = numpy.result_type(*types) - if issubclass(result_type.type, numpy.string_): + if issubclass(result_type.type, numpy.bytes_): # use the raw data to create the array result = values - elif issubclass(result_type.type, numpy.unicode_): + elif issubclass(result_type.type, numpy.str_): # use the raw data to create the array result = values else: @@ -707,7 +707,7 @@ def _convert_scalar_value(self, value): numpy_type = silx.utils.number.min_numerical_convertible_type(value) converted = numpy_type(value) except ValueError: - converted = numpy.string_(value) + converted = numpy.bytes_(value) return converted def _convert_list(self, value): @@ -726,19 +726,19 @@ def _convert_list(self, value): result_type = numpy.result_type(*types) - if issubclass(result_type.type, (numpy.string_, bytes)): + if issubclass(result_type.type, (numpy.bytes_, bytes)): # use the raw data to create the result - return numpy.string_(value) - elif issubclass(result_type.type, (numpy.unicode_, str)): + return numpy.bytes_(value) + elif issubclass(result_type.type, (numpy.str_, str)): # use the raw data to create the result - return numpy.unicode_(value) + return numpy.str_(value) else: if len(types) == 1: return numpy.array(numpy_values, dtype=result_type) else: return numpy.array(values, dtype=result_type) except ValueError: - return numpy.string_(value) + return numpy.bytes_(value) def has_sample_information(self): """Returns true if there is information about the sample in the diff --git a/src/silx/io/octaveh5.py b/src/silx/io/octaveh5.py index 5f5d81df1c..4ecd386cda 100644 --- a/src/silx/io/octaveh5.py +++ b/src/silx/io/octaveh5.py @@ -113,10 +113,10 @@ def get(self, struct_name): for key, val in iter(dict(gr_level2).items()): data_dict[str(key)] = list(val.items())[1][1][()] - if list(val.items())[0][1][()] != np.string_("sq_string"): + if list(val.items())[0][1][()] != np.bytes_("sq_string"): data_dict[str(key)] = float(data_dict[str(key)]) else: - if list(val.items())[0][1][()] == np.string_("sq_string"): + if list(val.items())[0][1][()] == np.bytes_("sq_string"): # in the case the string has been stored as an nd-array of char if type(data_dict[str(key)]) is np.ndarray: data_dict[str(key)] = "".join( @@ -145,26 +145,26 @@ def write(self, struct_name, data_dict): group_l1 = self.file.create_group(struct_name) group_l1.attrs["OCTAVE_GLOBAL"] = np.uint8(1) group_l1.attrs["OCTAVE_NEW_FORMAT"] = np.uint8(1) - group_l1.create_dataset("type", data=np.string_("scalar struct"), dtype="|S14") + group_l1.create_dataset("type", data=np.bytes_("scalar struct"), dtype="|S14") group_l2 = group_l1.create_group("value") for ftparams in data_dict: group_l3 = group_l2.create_group(ftparams) group_l3.attrs["OCTAVE_NEW_FORMAT"] = np.uint8(1) if type(data_dict[ftparams]) == str: group_l3.create_dataset( - "type", (), data=np.string_("sq_string"), dtype="|S10" + "type", (), data=np.bytes_("sq_string"), dtype="|S10" ) if self.octave_targetted_version < 3.8: group_l3.create_dataset( - "value", data=np.string_(data_dict[ftparams] + "0") + "value", data=np.bytes_(data_dict[ftparams] + "0") ) else: group_l3.create_dataset( - "value", data=np.string_(data_dict[ftparams]) + "value", data=np.bytes_(data_dict[ftparams]) ) else: group_l3.create_dataset( - "type", (), data=np.string_("scalar"), dtype="|S7" + "type", (), data=np.bytes_("scalar"), dtype="|S7" ) group_l3.create_dataset("value", data=data_dict[ftparams]) diff --git a/src/silx/io/test/test_fabioh5.py b/src/silx/io/test/test_fabioh5.py index 9c92f15aa7..01e7bf992b 100755 --- a/src/silx/io/test/test_fabioh5.py +++ b/src/silx/io/test/test_fabioh5.py @@ -169,8 +169,8 @@ def test_metadata_float(self): def test_metadata_string(self): dataset = self.h5_image["/scan_0/instrument/detector_0/others/string"] self.assertEqual(dataset.h5py_class, h5py.Dataset) - self.assertEqual(dataset[()], numpy.string_("hi!")) - self.assertEqual(dataset.dtype.type, numpy.string_) + self.assertEqual(dataset[()], numpy.bytes_("hi!")) + self.assertEqual(dataset.dtype.type, numpy.bytes_) self.assertEqual(dataset.shape, (1,)) def test_metadata_list_integer(self): @@ -194,8 +194,8 @@ def test_metadata_list_looks_like_list(self): "/scan_0/instrument/detector_0/others/string_looks_like_list" ] self.assertEqual(dataset.h5py_class, h5py.Dataset) - self.assertEqual(dataset[()], numpy.string_("2000 hi!")) - self.assertEqual(dataset.dtype.type, numpy.string_) + self.assertEqual(dataset[()], numpy.bytes_("2000 hi!")) + self.assertEqual(dataset.dtype.type, numpy.bytes_) self.assertEqual(dataset.shape, (1,)) def test_float_32(self):