diff --git a/src/caosadvancedtools/cfoods/h5.py b/src/caosadvancedtools/cfoods/h5.py index 543ac327871fb7f5c79d68e638af1a47b62d83f6..9defe77115db7687d3a6c5f27bf7f3d268e605fc 100644 --- a/src/caosadvancedtools/cfoods/h5.py +++ b/src/caosadvancedtools/cfoods/h5.py @@ -6,6 +6,8 @@ # Copyright (C) 2020 Daniel Hornung <d.hornung@indiscale.com> # Copyright (C) 2021 Henrik tom Wörden <h.tomwoerden@indiscale.com> # 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 # it under the terms of the GNU Affero General Public License as @@ -85,13 +87,13 @@ def h5_attr_to_property(val): if isinstance(val, np.ndarray): if val.ndim > 1: return None, None - - if val.ndim == 0: - raise NotImplementedError( - "Code assumes that scalar values " - "will not be given as np.ndarray objects") - val = list(val) + # The tolist method is on both numpy.ndarray and numpy.generic + # and properly converts scalars (including 0-dimensional + # numpy.ndarray) to Python scalars and 1D arrays to lists of + # Python scalars. + if val.ndim != 0: dtype = db.LIST(dtype) + val = val.tolist() # TODO this can eventually be removed diff --git a/unittests/test_h5.py b/unittests/test_h5.py index 12b04844e173ac2f778b34daafcd876fdf527a49..e5ae94686fe4542f6833e21e9a80f01e4257538d 100644 --- a/unittests/test_h5.py +++ b/unittests/test_h5.py @@ -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_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")))