Skip to content

Commit

Permalink
Fix numpy compatibility spotted by ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
t20100 committed Mar 14, 2024
1 parent 197e253 commit 2fa41e7
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 45 deletions.
2 changes: 1 addition & 1 deletion examples/customHdf5TreeModel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion examples/hdf5widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/silx/gui/data/RecordTableView.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/silx/gui/data/TextFormatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions src/silx/gui/hdf5/Hdf5Formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 12 additions & 12 deletions src/silx/gui/hdf5/test/test_hdf5.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)

Expand All @@ -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)

Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion src/silx/gui/plot/items/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
28 changes: 14 additions & 14 deletions src/silx/io/fabioh5.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand All @@ -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
Expand Down
14 changes: 7 additions & 7 deletions src/silx/io/octaveh5.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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])

Expand Down
8 changes: 4 additions & 4 deletions src/silx/io/test/test_fabioh5.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand Down

0 comments on commit 2fa41e7

Please sign in to comment.