summaryrefslogtreecommitdiff
path: root/src/silx/io/test
diff options
context:
space:
mode:
authorPicca Frédéric-Emmanuel <picca@debian.org>2022-11-03 10:02:44 +0100
committerPicca Frédéric-Emmanuel <picca@debian.org>2022-11-03 10:02:44 +0100
commit1c380bfeff1e13a9f7d506460336659502ca052d (patch)
tree48081d47748d4563eeaa76662287eb19638c8591 /src/silx/io/test
parent4e774db12d5ebe7a20eded6dd434a289e27999e5 (diff)
New upstream version 1.1.0+dfsg
Diffstat (limited to 'src/silx/io/test')
-rw-r--r--src/silx/io/test/__init__.py1
-rw-r--r--src/silx/io/test/test_commonh5.py1
-rw-r--r--src/silx/io/test/test_dictdump.py58
-rwxr-xr-xsrc/silx/io/test/test_fabioh5.py1
-rw-r--r--src/silx/io/test/test_fioh5.py1
-rw-r--r--src/silx/io/test/test_h5py_utils.py33
-rw-r--r--src/silx/io/test/test_nxdata.py147
-rw-r--r--src/silx/io/test/test_octaveh5.py1
-rw-r--r--src/silx/io/test/test_rawh5.py1
-rw-r--r--src/silx/io/test/test_specfile.py1
-rw-r--r--src/silx/io/test/test_specfilewrapper.py1
-rw-r--r--src/silx/io/test/test_spech5.py1
-rw-r--r--src/silx/io/test/test_spectoh5.py1
-rw-r--r--src/silx/io/test/test_url.py1
-rw-r--r--src/silx/io/test/test_utils.py35
-rw-r--r--src/silx/io/test/test_write_to_h5.py1
16 files changed, 260 insertions, 25 deletions
diff --git a/src/silx/io/test/__init__.py b/src/silx/io/test/__init__.py
index 244d090..3c723bb 100644
--- a/src/silx/io/test/__init__.py
+++ b/src/silx/io/test/__init__.py
@@ -1,4 +1,3 @@
-# coding: utf-8
# /*##########################################################################
# Copyright (C) 2016-2017 European Synchrotron Radiation Facility
#
diff --git a/src/silx/io/test/test_commonh5.py b/src/silx/io/test/test_commonh5.py
index 27f6e8c..d554d27 100644
--- a/src/silx/io/test/test_commonh5.py
+++ b/src/silx/io/test/test_commonh5.py
@@ -1,4 +1,3 @@
-# coding: utf-8
# /*##########################################################################
# Copyright (C) 2016-2017 European Synchrotron Radiation Facility
#
diff --git a/src/silx/io/test/test_dictdump.py b/src/silx/io/test/test_dictdump.py
index 4cafa9b..e31d7a8 100644
--- a/src/silx/io/test/test_dictdump.py
+++ b/src/silx/io/test/test_dictdump.py
@@ -1,6 +1,5 @@
-# coding: utf-8
# /*##########################################################################
-# Copyright (C) 2016-2021 European Synchrotron Radiation Facility
+# Copyright (C) 2016-2022 European Synchrotron Radiation Facility
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
@@ -27,15 +26,21 @@ __authors__ = ["P. Knobel"]
__license__ = "MIT"
__date__ = "17/01/2018"
-from collections import OrderedDict
-import numpy
+
+from collections import defaultdict, OrderedDict
+from copy import deepcopy
+from io import BytesIO
import os
import tempfile
import unittest
-import h5py
-from copy import deepcopy
-from collections import defaultdict
+import h5py
+import numpy
+try:
+ import pint
+except ImportError:
+ pint = None
+import pytest
from silx.utils.testutils import LoggingValidator
@@ -48,6 +53,13 @@ from ..utils import is_link
from ..utils import h5py_read_dataset
+@pytest.fixture
+def tmp_h5py_file():
+ with BytesIO() as buffer:
+ with h5py.File(buffer, mode="w") as h5file:
+ yield h5file
+
+
def tree():
"""Tree data structure as a recursive nested dictionary"""
return defaultdict(tree)
@@ -512,6 +524,22 @@ class TestDictToH5(H5DictTestCase):
assert_append("replace")
+@pytest.mark.skipif(pint is None, reason="Require pint")
+def test_dicttoh5_pint(tmp_h5py_file):
+ ureg = pint.UnitRegistry()
+ treedict = {
+ "array_mm": pint.Quantity([1, 2, 3], ureg.mm),
+ "value_kg": 3 * ureg.kg,
+ }
+
+ dicttoh5(treedict, tmp_h5py_file)
+
+ result = h5todict(tmp_h5py_file)
+ assert set(treedict.keys()) == set(result.keys())
+ for key, value in treedict.items():
+ assert numpy.array_equal(result[key], value.magnitude)
+
+
class TestH5ToDict(H5DictTestCase):
def setUp(self):
self.tempdir = tempfile.mkdtemp()
@@ -800,6 +828,22 @@ class TestDictToNx(H5DictTestCase):
assert_append("replace", add_nx_class=True)
+@pytest.mark.skipif(pint is None, reason="Require pint")
+def test_dicttonx_pint(tmp_h5py_file):
+ ureg = pint.UnitRegistry()
+ treedict = {
+ "array_mm": pint.Quantity([1, 2, 3], ureg.mm),
+ "value_kg": 3 * ureg.kg,
+ }
+
+ dictdump.dicttonx(treedict, tmp_h5py_file)
+
+ result = dictdump.nxtodict(tmp_h5py_file)
+ for key, value in treedict.items():
+ assert numpy.array_equal(result[key], value.magnitude)
+ assert result[f"{key}@units"] == f"{value.units:~C}"
+
+
class TestNxToDict(H5DictTestCase):
def setUp(self):
self.tempdir = tempfile.mkdtemp()
diff --git a/src/silx/io/test/test_fabioh5.py b/src/silx/io/test/test_fabioh5.py
index c410024..fdeb1c3 100755
--- a/src/silx/io/test/test_fabioh5.py
+++ b/src/silx/io/test/test_fabioh5.py
@@ -1,4 +1,3 @@
-# coding: utf-8
# /*##########################################################################
# Copyright (C) 2016-2018 European Synchrotron Radiation Facility
#
diff --git a/src/silx/io/test/test_fioh5.py b/src/silx/io/test/test_fioh5.py
index 8ffb4ad..18200c9 100644
--- a/src/silx/io/test/test_fioh5.py
+++ b/src/silx/io/test/test_fioh5.py
@@ -1,4 +1,3 @@
-# coding: utf-8
# /*##########################################################################
# Copyright (C) 2021 Timo Fuchs
#
diff --git a/src/silx/io/test/test_h5py_utils.py b/src/silx/io/test/test_h5py_utils.py
index ea46eca..0d10a78 100644
--- a/src/silx/io/test/test_h5py_utils.py
+++ b/src/silx/io/test/test_h5py_utils.py
@@ -1,4 +1,3 @@
-# coding: utf-8
# /*##########################################################################
# Copyright (C) 2016-2017 European Synchrotron Radiation Facility
#
@@ -449,3 +448,35 @@ class TestH5pyUtils(unittest.TestCase):
f.write("0")
with self.assertRaises(RetryTimeoutError):
top_level_names_test(txtfilename, filename, **kw)
+
+ @subtests
+ def test_retry_generator(self):
+ filename = self._new_filename()
+ ncausefailure = 3
+ faildelay = 0.1
+ sufficient_timeout = ncausefailure * (faildelay + 10)
+ insufficient_timeout = ncausefailure * faildelay * 0.5
+
+ @h5py_utils.retry()
+ def iter_data(filename, name, start_index=0):
+ nonlocal failcounter
+ if start_index <= 0:
+ with h5py_utils.File(filename) as h5file:
+ yield h5file[name][()]
+ if failcounter < ncausefailure:
+ time.sleep(faildelay)
+ failcounter += 1
+ raise RetryError
+ if start_index <= 1:
+ with h5py_utils.File(filename) as h5file:
+ yield not h5file[name][()]
+
+ failcounter = 0
+ kw = {"retry_timeout": sufficient_timeout}
+ data = list(iter_data(filename, "/check", **kw))
+ self.assertEqual(data, [True, False])
+
+ failcounter = 0
+ kw = {"retry_timeout": insufficient_timeout}
+ with self.assertRaises(RetryTimeoutError):
+ list(iter_data(filename, "/check", **kw))
diff --git a/src/silx/io/test/test_nxdata.py b/src/silx/io/test/test_nxdata.py
index 9025d6d..52a2b8a 100644
--- a/src/silx/io/test/test_nxdata.py
+++ b/src/silx/io/test/test_nxdata.py
@@ -1,6 +1,5 @@
-# coding: utf-8
# /*##########################################################################
-# Copyright (C) 2016-2021 European Synchrotron Radiation Facility
+# Copyright (C) 2016-2022 European Synchrotron Radiation Facility
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
@@ -30,10 +29,13 @@ __date__ = "24/03/2020"
import tempfile
import unittest
+
import h5py
import numpy
+import pytest
from .. import nxdata
+from ..dictdump import dicttoh5
text_dtype = h5py.special_dtype(vlen=str)
@@ -561,3 +563,144 @@ class TestSaveNXdata(unittest.TestCase):
self.assertTrue(numpy.array_equal(nxd.axes[0],
a0))
h5f.close()
+
+
+class TestGetDefault:
+ """Test silx.io.nxdata.get_default function"""
+
+ @pytest.fixture
+ def hdf5_file(self, tmp_path):
+ with h5py.File(tmp_path / "test_file.h5", "w") as h5f:
+ yield h5f
+
+ def testDirectPath(self, hdf5_file):
+ dicttoh5(
+ {
+ ("", "default"): "/nxentry/nxprocess/nxdata",
+ "nxentry": {
+ "nxprocess": {
+ "nxdata": {
+ ("", "NX_class"): "NXdata",
+ ("", "signal"): "data",
+ "data": (1, 2, 3),
+ }
+ }
+ }
+ },
+ hdf5_file)
+ default = nxdata.get_default(hdf5_file)
+ assert isinstance(default, nxdata.NXdata)
+ assert default.group.name == "/nxentry/nxprocess/nxdata"
+
+ def testAbsolutePath(self, hdf5_file):
+ dicttoh5(
+ {
+ ("", "default"): "/nxentry",
+ "nxentry": {
+ ("", "default"): "/nxentry/nxprocess/nxdata",
+ "nxprocess": {
+ "nxdata": {
+ ("", "NX_class"): "NXdata",
+ ("", "signal"): "data",
+ "data": (1, 2, 3),
+ }
+ }
+ }
+ },
+ hdf5_file)
+ default = nxdata.get_default(hdf5_file)
+ assert isinstance(default, nxdata.NXdata)
+ assert default.group.name == "/nxentry/nxprocess/nxdata"
+
+ def testRelativePath(self, hdf5_file):
+ dicttoh5(
+ {
+ ("", "default"): "nxentry",
+ "nxentry": {
+ ("", "default"): "nxdata",
+ "nxdata": {
+ ("", "NX_class"): "NXdata",
+ ("", "signal"): "data",
+ "data": (1, 2, 3),
+ }
+ }
+ },
+ hdf5_file)
+ default = nxdata.get_default(hdf5_file)
+ assert isinstance(default, nxdata.NXdata)
+ assert default.group.name == "/nxentry/nxdata"
+
+ def testRelativePathSubdir(self, hdf5_file):
+ dicttoh5(
+ {
+ ("", "default"): "nxentry",
+ "nxentry": {
+ ("", "default"): "nxprocess/nxdata",
+ "nxprocess": {
+ "nxdata": {
+ ("", "NX_class"): "NXdata",
+ ("", "signal"): "data",
+ "data": (1, 2, 3),
+ }
+ }
+ }
+ },
+ hdf5_file)
+ default = nxdata.get_default(hdf5_file)
+ assert isinstance(default, nxdata.NXdata)
+ assert default.group.name == "/nxentry/nxprocess/nxdata"
+
+ def testRecursiveAbsolutePath(self, hdf5_file):
+ dicttoh5(
+ {
+ ("", "default"): "/nxentry",
+ "nxentry": {
+ ("", "default"): "/nxentry/nxprocess",
+ "nxprocess": {
+ ("", "default"): "/nxentry/nxprocess/nxdata",
+ "nxdata": {
+ ("", "NX_class"): "NXdata",
+ ("", "signal"): "data",
+ "data": (1, 2, 3),
+ }
+ }
+ }
+ },
+ hdf5_file)
+ default = nxdata.get_default(hdf5_file)
+ assert isinstance(default, nxdata.NXdata)
+ assert default.group.name == "/nxentry/nxprocess/nxdata"
+
+ def testRecursiveRelativePath(self, hdf5_file):
+ dicttoh5(
+ {
+ ("", "default"): "nxentry",
+ "nxentry": {
+ ("", "default"): "nxprocess",
+ "nxprocess": {
+ ("", "default"): "nxdata",
+ "nxdata": {
+ ("", "NX_class"): "NXdata",
+ ("", "signal"): "data",
+ "data": (1, 2, 3),
+ }
+ }
+ }
+ },
+ hdf5_file)
+ default = nxdata.get_default(hdf5_file)
+ assert isinstance(default, nxdata.NXdata)
+ assert default.group.name == "/nxentry/nxprocess/nxdata"
+
+ def testLoop(self, hdf5_file):
+ """Infinite loop of @default"""
+ dicttoh5(
+ {
+ ("", "default"): "/nxentry",
+ "nxentry": {
+ ("", "default"): "/nxentry",
+ }
+ },
+ hdf5_file)
+ default = nxdata.get_default(hdf5_file)
+ assert default is None
diff --git a/src/silx/io/test/test_octaveh5.py b/src/silx/io/test/test_octaveh5.py
index 1c3b3e0..19b8ad6 100644
--- a/src/silx/io/test/test_octaveh5.py
+++ b/src/silx/io/test/test_octaveh5.py
@@ -1,4 +1,3 @@
-# coding: utf-8
# /*##########################################################################
# Copyright (C) 2016 European Synchrotron Radiation Facility
#
diff --git a/src/silx/io/test/test_rawh5.py b/src/silx/io/test/test_rawh5.py
index 236484d..947be0f 100644
--- a/src/silx/io/test/test_rawh5.py
+++ b/src/silx/io/test/test_rawh5.py
@@ -1,4 +1,3 @@
-# coding: utf-8
# /*##########################################################################
#
# Copyright (c) 2016 European Synchrotron Radiation Facility
diff --git a/src/silx/io/test/test_specfile.py b/src/silx/io/test/test_specfile.py
index 44cb08c..748e31c 100644
--- a/src/silx/io/test/test_specfile.py
+++ b/src/silx/io/test/test_specfile.py
@@ -1,4 +1,3 @@
-# coding: utf-8
# /*##########################################################################
# Copyright (C) 2016-2021 European Synchrotron Radiation Facility
#
diff --git a/src/silx/io/test/test_specfilewrapper.py b/src/silx/io/test/test_specfilewrapper.py
index a1ba5f4..7d2ce60 100644
--- a/src/silx/io/test/test_specfilewrapper.py
+++ b/src/silx/io/test/test_specfilewrapper.py
@@ -1,4 +1,3 @@
-# coding: utf-8
# /*##########################################################################
# Copyright (C) 2016 European Synchrotron Radiation Facility
#
diff --git a/src/silx/io/test/test_spech5.py b/src/silx/io/test/test_spech5.py
index 1e67961..456a538 100644
--- a/src/silx/io/test/test_spech5.py
+++ b/src/silx/io/test/test_spech5.py
@@ -1,4 +1,3 @@
-# coding: utf-8
# /*##########################################################################
# Copyright (C) 2016-2021 European Synchrotron Radiation Facility
#
diff --git a/src/silx/io/test/test_spectoh5.py b/src/silx/io/test/test_spectoh5.py
index 66bf8d6..5465ece 100644
--- a/src/silx/io/test/test_spectoh5.py
+++ b/src/silx/io/test/test_spectoh5.py
@@ -1,4 +1,3 @@
-# coding: utf-8
# /*##########################################################################
# Copyright (C) 2016-2019 European Synchrotron Radiation Facility
#
diff --git a/src/silx/io/test/test_url.py b/src/silx/io/test/test_url.py
index 7346391..8cbfb34 100644
--- a/src/silx/io/test/test_url.py
+++ b/src/silx/io/test/test_url.py
@@ -1,4 +1,3 @@
-# coding: utf-8
# /*##########################################################################
# Copyright (C) 2016-2017 European Synchrotron Radiation Facility
#
diff --git a/src/silx/io/test/test_utils.py b/src/silx/io/test/test_utils.py
index cc34100..b9fc3ab 100644
--- a/src/silx/io/test/test_utils.py
+++ b/src/silx/io/test/test_utils.py
@@ -1,6 +1,5 @@
-# coding: utf-8
# /*##########################################################################
-# Copyright (C) 2016-2019 European Synchrotron Radiation Facility
+# Copyright (C) 2016-2022 European Synchrotron Radiation Facility
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
@@ -30,7 +29,6 @@ import re
import shutil
import tempfile
import unittest
-import sys
from .. import utils
from ..._version import calc_hexversion
@@ -40,6 +38,7 @@ import h5py
from ..utils import h5ls
from silx.io import commonh5
+
import fabio
__authors__ = ["P. Knobel"]
@@ -921,3 +920,33 @@ def test_visitall_commonh5():
soft_link = visited_items["/group/soft_link"]
assert isinstance(soft_link, commonh5.SoftLink)
assert soft_link.path == "/group/dataset"
+
+
+def test_match_hdf5(tmp_path):
+ """Test match function with HDF5 file"""
+ with h5py.File(tmp_path / "test_match.h5", "w") as h5f:
+ h5f.create_group("entry_0000/group")
+ h5f["entry_0000/data"] = 0
+ h5f.create_group("entry_0001/group")
+ h5f["entry_0001/data"] = 1
+ h5f.create_group("entry_0002")
+ h5f["entry_0003"] = 3
+
+ result = list(utils.match(h5f, "/entry_*/*"))
+
+ assert sorted(result) == ['entry_0000/data', 'entry_0000/group', 'entry_0001/data', 'entry_0001/group']
+
+
+def test_match_commonh5():
+ """Test match function with commonh5 objects"""
+ with commonh5.File("filename.file", mode="w") as fobj:
+ fobj.create_group("entry_0000/group")
+ fobj["entry_0000/data"] = 0
+ fobj.create_group("entry_0001/group")
+ fobj["entry_0001/data"] = 1
+ fobj.create_group("entry_0002")
+ fobj["entry_0003"] = 3
+
+ result = list(utils.match(fobj, "/entry_*/*"))
+
+ assert sorted(result) == ['entry_0000/data', 'entry_0000/group', 'entry_0001/data', 'entry_0001/group']
diff --git a/src/silx/io/test/test_write_to_h5.py b/src/silx/io/test/test_write_to_h5.py
index 06149c9..fe855e1 100644
--- a/src/silx/io/test/test_write_to_h5.py
+++ b/src/silx/io/test/test_write_to_h5.py
@@ -1,4 +1,3 @@
-# coding: utf-8
# /*##########################################################################
# Copyright (C) 2021 European Synchrotron Radiation Facility
#