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
Branches
Tags
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 @@
# 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
......
......@@ -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")))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment