Skip to content
Snippets Groups Projects
Commit a0764d25 authored by Alexander Kreft's avatar Alexander Kreft
Browse files

Merge branch 'improving-h5-scalar-attribute-handling' into 'dev'

Improved the handling of numpy scalars in cfoods.h5.h5_attr_to_property

See merge request caosdb/caosdb-advanced-user-tools!84
parents 08e13a67 f349f9a2
No related branches found
No related tags found
2 merge requests!22Release 0.3,!18Improved the handling of numpy scalars in cfoods.h5.h5_attr_to_property
Pipeline #14109 passed with warnings
...@@ -6,6 +6,8 @@ ...@@ -6,6 +6,8 @@
# Copyright (C) 2020 Daniel Hornung <d.hornung@indiscale.com> # Copyright (C) 2020 Daniel Hornung <d.hornung@indiscale.com>
# Copyright (C) 2021 Henrik tom Wörden <h.tomwoerden@indiscale.com> # Copyright (C) 2021 Henrik tom Wörden <h.tomwoerden@indiscale.com>
# Copyright (C) 2021 Alexander Kreft # Copyright (C) 2021 Alexander Kreft
# Copyright (C) 2021 Laboratory for Fluid Physics and Biocomplexity,
# Max-Planck-Insitute für Dynamik und Selbstorganisation <www.lfpn.ds.mpg.de>
# #
# This program is free software: you can redistribute it and/or modify # This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as # it under the terms of the GNU Affero General Public License as
...@@ -85,13 +87,13 @@ def h5_attr_to_property(val): ...@@ -85,13 +87,13 @@ def h5_attr_to_property(val):
if isinstance(val, np.ndarray): if isinstance(val, np.ndarray):
if val.ndim > 1: if val.ndim > 1:
return None, None return None, None
# The tolist method is on both numpy.ndarray and numpy.generic
if val.ndim == 0: # and properly converts scalars (including 0-dimensional
raise NotImplementedError( # numpy.ndarray) to Python scalars and 1D arrays to lists of
"Code assumes that scalar values " # Python scalars.
"will not be given as np.ndarray objects") if val.ndim != 0:
val = list(val)
dtype = db.LIST(dtype) dtype = db.LIST(dtype)
val = val.tolist()
# TODO this can eventually be removed # TODO this can eventually be removed
......
...@@ -181,4 +181,7 @@ class H5CFoodTest(unittest.TestCase): ...@@ -181,4 +181,7 @@ class H5CFoodTest(unittest.TestCase):
self.assertTupleEqual((None, None), h5_attr_to_property(test_integers_2d)) self.assertTupleEqual((None, None), h5_attr_to_property(test_integers_2d))
self.assertTupleEqual((None, None), h5_attr_to_property(test_floats_2d)) self.assertTupleEqual((None, None), h5_attr_to_property(test_floats_2d))
self.assertRaises(NotImplementedError, h5_attr_to_property, np.array(1)) # Test scalar values given as np.array
self.assertTupleEqual((1, db.INTEGER), h5_attr_to_property(np.array(1)))
self.assertTupleEqual((1.123, db.DOUBLE), h5_attr_to_property(np.array(1.123)))
self.assertTupleEqual(('Hello World', db.TEXT), h5_attr_to_property(np.array("Hello World")))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment