h5rdmtoolbox 2.5.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (144) hide show
  1. h5rdmtoolbox/__init__.py +424 -0
  2. h5rdmtoolbox/_cfg.py +104 -0
  3. h5rdmtoolbox/_repr.py +747 -0
  4. h5rdmtoolbox/_version.py +6 -0
  5. h5rdmtoolbox/cli.py +61 -0
  6. h5rdmtoolbox/consts.py +3 -0
  7. h5rdmtoolbox/convention/__init__.py +41 -0
  8. h5rdmtoolbox/convention/cfg.py +2 -0
  9. h5rdmtoolbox/convention/consts.py +28 -0
  10. h5rdmtoolbox/convention/core.py +817 -0
  11. h5rdmtoolbox/convention/errors.py +34 -0
  12. h5rdmtoolbox/convention/generate.py +385 -0
  13. h5rdmtoolbox/convention/ontology/__init__.py +3 -0
  14. h5rdmtoolbox/convention/ontology/h5ontocls.py +133 -0
  15. h5rdmtoolbox/convention/ontology/hdf_datatypes.py +64 -0
  16. h5rdmtoolbox/convention/references.py +23 -0
  17. h5rdmtoolbox/convention/standard_attributes.py +383 -0
  18. h5rdmtoolbox/convention/toolbox_validators.py +175 -0
  19. h5rdmtoolbox/convention/utils.py +216 -0
  20. h5rdmtoolbox/data/def_icon.png +0 -0
  21. h5rdmtoolbox/data/fluid-v1.yml +36 -0
  22. h5rdmtoolbox/data/h5tbx.yaml +28 -0
  23. h5rdmtoolbox/data/id_icon.png +0 -0
  24. h5rdmtoolbox/data/iri_icon.png +0 -0
  25. h5rdmtoolbox/data/ld_icon.png +0 -0
  26. h5rdmtoolbox/data/piv-v1.yml +35 -0
  27. h5rdmtoolbox/data/style.css +216 -0
  28. h5rdmtoolbox/data/tutorial_convention.yaml +91 -0
  29. h5rdmtoolbox/data/tutorial_standard_name_table.yaml +390 -0
  30. h5rdmtoolbox/data/type_icon.png +0 -0
  31. h5rdmtoolbox/database/__init__.py +75 -0
  32. h5rdmtoolbox/database/hdfdb/__init__.py +4 -0
  33. h5rdmtoolbox/database/hdfdb/filedb.py +129 -0
  34. h5rdmtoolbox/database/hdfdb/objdb.py +520 -0
  35. h5rdmtoolbox/database/hdfdb/query.py +133 -0
  36. h5rdmtoolbox/database/hdfdb/utils.py +51 -0
  37. h5rdmtoolbox/database/interface.py +59 -0
  38. h5rdmtoolbox/database/lazy.py +183 -0
  39. h5rdmtoolbox/database/mongo.py +374 -0
  40. h5rdmtoolbox/errors.py +4 -0
  41. h5rdmtoolbox/extensions/__init__.py +0 -0
  42. h5rdmtoolbox/extensions/magnitude.py +67 -0
  43. h5rdmtoolbox/extensions/normalize.py +103 -0
  44. h5rdmtoolbox/extensions/onto.py +52 -0
  45. h5rdmtoolbox/extensions/units.py +48 -0
  46. h5rdmtoolbox/extensions/vector.py +138 -0
  47. h5rdmtoolbox/gui/__init__.py +0 -0
  48. h5rdmtoolbox/gui/fairify.py +446 -0
  49. h5rdmtoolbox/gui/src/__init__.py +0 -0
  50. h5rdmtoolbox/gui/src/main.py +131 -0
  51. h5rdmtoolbox/identifiers.py +257 -0
  52. h5rdmtoolbox/layout/__init__.py +4 -0
  53. h5rdmtoolbox/layout/core.py +624 -0
  54. h5rdmtoolbox/ld/__init__.py +129 -0
  55. h5rdmtoolbox/ld/globals.py +3 -0
  56. h5rdmtoolbox/ld/hdf/__init__.py +0 -0
  57. h5rdmtoolbox/ld/hdf/attributes.py +36 -0
  58. h5rdmtoolbox/ld/hdf/datasets.py +160 -0
  59. h5rdmtoolbox/ld/hdf/file.py +54 -0
  60. h5rdmtoolbox/ld/hdf/groups.py +47 -0
  61. h5rdmtoolbox/ld/rdf.py +883 -0
  62. h5rdmtoolbox/ld/shacl.py +221 -0
  63. h5rdmtoolbox/ld/user/__init__.py +0 -0
  64. h5rdmtoolbox/ld/user/attributes.py +241 -0
  65. h5rdmtoolbox/ld/user/datasets.py +22 -0
  66. h5rdmtoolbox/ld/user/file.py +58 -0
  67. h5rdmtoolbox/ld/user/groups.py +58 -0
  68. h5rdmtoolbox/ld/user/utils.py +8 -0
  69. h5rdmtoolbox/ld/utils.py +133 -0
  70. h5rdmtoolbox/plotting.py +168 -0
  71. h5rdmtoolbox/protected_attributes.py +9 -0
  72. h5rdmtoolbox/protocols.py +268 -0
  73. h5rdmtoolbox/repository/__init__.py +11 -0
  74. h5rdmtoolbox/repository/default_metamapper.py +55 -0
  75. h5rdmtoolbox/repository/interface.py +214 -0
  76. h5rdmtoolbox/repository/utils.py +131 -0
  77. h5rdmtoolbox/repository/zenodo/__init__.py +5 -0
  78. h5rdmtoolbox/repository/zenodo/core.py +616 -0
  79. h5rdmtoolbox/repository/zenodo/metadata.py +276 -0
  80. h5rdmtoolbox/repository/zenodo/tokens.py +118 -0
  81. h5rdmtoolbox/repository/zenodo/utils.py +16 -0
  82. h5rdmtoolbox/tutorial.py +374 -0
  83. h5rdmtoolbox/user.py +158 -0
  84. h5rdmtoolbox/utils.py +743 -0
  85. h5rdmtoolbox/wrapper/__init__.py +8 -0
  86. h5rdmtoolbox/wrapper/accessor.py +97 -0
  87. h5rdmtoolbox/wrapper/accessors/__init__.py +0 -0
  88. h5rdmtoolbox/wrapper/accessors/provenance.py +36 -0
  89. h5rdmtoolbox/wrapper/core.py +2528 -0
  90. h5rdmtoolbox/wrapper/ds_decoder.py +113 -0
  91. h5rdmtoolbox/wrapper/h5attr.py +519 -0
  92. h5rdmtoolbox/wrapper/h5ext.py +20 -0
  93. h5rdmtoolbox/wrapper/h5utils.py +32 -0
  94. h5rdmtoolbox/wrapper/h5yaml.py +116 -0
  95. h5rdmtoolbox/wrapper/jsonld.py +1189 -0
  96. h5rdmtoolbox/wrapper/xr2hdf.py +106 -0
  97. h5rdmtoolbox-2.5.3.dist-info/METADATA +379 -0
  98. h5rdmtoolbox-2.5.3.dist-info/RECORD +144 -0
  99. h5rdmtoolbox-2.5.3.dist-info/WHEEL +5 -0
  100. h5rdmtoolbox-2.5.3.dist-info/entry_points.txt +2 -0
  101. h5rdmtoolbox-2.5.3.dist-info/licenses/LICENSE +20 -0
  102. h5rdmtoolbox-2.5.3.dist-info/top_level.txt +2 -0
  103. tests/__init__.py +0 -0
  104. tests/clean_zenodo_sandbox.py +23 -0
  105. tests/conventions/__init__.py +0 -0
  106. tests/conventions/test_convention_vfuncs.py +98 -0
  107. tests/conventions/test_conventions.py +697 -0
  108. tests/conventions/test_engmeta.py +55 -0
  109. tests/conventions/test_references.py +22 -0
  110. tests/conventions/test_toolbox_validators.py +138 -0
  111. tests/conventions/test_utils.py +94 -0
  112. tests/database/__init__.py +0 -0
  113. tests/database/test_find_rdf.py +36 -0
  114. tests/database/test_hdfDB.py +619 -0
  115. tests/database/test_mongo.py +229 -0
  116. tests/layouts/__init__.py +0 -0
  117. tests/layouts/test_core.py +362 -0
  118. tests/layouts/test_docs.py +66 -0
  119. tests/ld/__init__.py +0 -0
  120. tests/ld/test_ld.py +904 -0
  121. tests/ld/test_rdf.py +688 -0
  122. tests/ld/test_shacl.py +223 -0
  123. tests/ld/test_user_graph.py +278 -0
  124. tests/ld/test_utils.py +13 -0
  125. tests/repository/__init__.py +0 -0
  126. tests/repository/test_zenodo.py +728 -0
  127. tests/test_cli.py +69 -0
  128. tests/test_config.py +80 -0
  129. tests/test_extensions.py +148 -0
  130. tests/test_identifiers.py +85 -0
  131. tests/test_plotting.py +155 -0
  132. tests/test_repr.py +114 -0
  133. tests/test_user.py +100 -0
  134. tests/test_utils.py +143 -0
  135. tests/test_version.py +72 -0
  136. tests/test_xarray_export.py +47 -0
  137. tests/wrapper/__init__.py +0 -0
  138. tests/wrapper/test_core.py +994 -0
  139. tests/wrapper/test_dump.py +77 -0
  140. tests/wrapper/test_file.py +411 -0
  141. tests/wrapper/test_h5ext.py +22 -0
  142. tests/wrapper/test_lazy.py +62 -0
  143. tests/wrapper/test_namespaces.py +46 -0
  144. tests/wrapper/test_xr2df.py +41 -0
tests/test_cli.py ADDED
@@ -0,0 +1,69 @@
1
+ import unittest
2
+
3
+ from click.testing import CliRunner
4
+
5
+ from h5rdmtoolbox import File
6
+ from h5rdmtoolbox.cli import h5tbx
7
+
8
+
9
+ class TestCLI(unittest.TestCase):
10
+
11
+ def test_help(self):
12
+ runner = CliRunner()
13
+ result = runner.invoke(h5tbx, ['--help', ])
14
+
15
+ self.assertIsNone(result.exception)
16
+ self.assertEqual(result.output,
17
+ r"""Usage: h5tbx [OPTIONS] COMMAND [ARGS]...
18
+
19
+ Options:
20
+ --fairify TEXT Starts the app helping you to make the file FAIRer
21
+ --help Show this message and exit.
22
+
23
+ Commands:
24
+ ld Linked-Data command
25
+ """)
26
+
27
+ def test_command_ld(self):
28
+ runner = CliRunner()
29
+ result = runner.invoke(h5tbx, ["ld", "--help"])
30
+ self.assertIsNone(result.exception)
31
+ self.assertEqual(result.output,
32
+ """Usage: h5tbx ld [OPTIONS] FILENAME
33
+
34
+ Linked-Data command
35
+
36
+ Options:
37
+ -o, --output PATH Filename to write the JSON-LD data to.
38
+ --format TEXT The output format, e.g. jsonld.
39
+ --graph Generates a graph and stores it in OUTPUT-graph.html. Uses
40
+ pyvis and kglab. Please Make sure it is installed
41
+ --help Show this message and exit.
42
+ """)
43
+
44
+ def test_ld_dump(self):
45
+ with File() as h5:
46
+ pass
47
+ runner = CliRunner()
48
+ result = runner.invoke(h5tbx, ["ld", f"{h5.hdf_filename}"])
49
+ self.assertIsNone(result.exception)
50
+ expected = """@prefix hdf: <http://purl.allotrope.org/ontologies/hdf5/1.8#> .
51
+
52
+ [] a hdf:File ;
53
+ hdf:rootGroup [ a hdf:Group ;
54
+ hdf:name "/" ] .
55
+
56
+
57
+ """
58
+ self.assertEqual(
59
+ expected,
60
+ result.output
61
+ )
62
+
63
+ # def test_fairify(self):
64
+ # with File() as h5:
65
+ # pass
66
+ # runner = CliRunner()
67
+ # result = runner.invoke(h5tbx, [f"--fairify=does-not-exist.hdf"])
68
+ # self.assertIsNotNone(result.exception)
69
+
tests/test_config.py ADDED
@@ -0,0 +1,80 @@
1
+ """Unit tests for h5rdmtoolbox.config"""
2
+ import unittest
3
+
4
+ import h5rdmtoolbox as h5tbx
5
+ from h5rdmtoolbox import File
6
+ from h5rdmtoolbox.wrapper import core
7
+
8
+
9
+ class TestConfig(unittest.TestCase):
10
+
11
+ def test_set_logger(self):
12
+ with h5tbx.set_config(init_logger_level='DEBUG'):
13
+ self.assertEqual(h5tbx.get_config()['init_logger_level'], 'DEBUG')
14
+ with h5tbx.set_config(init_logger_level=10):
15
+ self.assertEqual(h5tbx.get_config()['init_logger_level'], 10)
16
+ with self.assertRaises(ValueError):
17
+ with h5tbx.set_config(init_logger_level='invalid'):
18
+ pass
19
+ with self.assertRaises(ValueError):
20
+ with h5tbx.set_config(init_logger_level=100):
21
+ pass
22
+
23
+ with self.assertRaises(KeyError):
24
+ with h5tbx.set_config(invalid='invalid'):
25
+ pass
26
+
27
+ with self.assertRaises(ValueError):
28
+ with h5tbx.set_config(init_logger_level=3.4):
29
+ pass
30
+
31
+ def test_config_type(self):
32
+ self.assertIsInstance(h5tbx.get_config('xarray_unit_repr_in_plots'), str)
33
+ with self.assertRaises(KeyError):
34
+ h5tbx.get_config('does_not_exist')
35
+ with self.assertRaises(ValueError):
36
+ h5tbx.set_config(require_unit=4.3)
37
+ with self.assertRaises(ValueError):
38
+ h5tbx.set_config(xarray_unit_repr_in_plots=123)
39
+ h5tbx.set_config(xarray_unit_repr_in_plots='(')
40
+ h5tbx.set_config(xarray_unit_repr_in_plots='/')
41
+ with self.assertRaises(ValueError):
42
+ h5tbx.set_config(ureg_format=123)
43
+
44
+ def test_changing_ureg_format(self):
45
+ DEFAULT_FORMAT = h5tbx.get_config('ureg_format')
46
+ self.assertEqual(DEFAULT_FORMAT, 'C~')
47
+ ureg = h5tbx.get_ureg()
48
+ self.assertEqual(h5tbx.get_ureg().formatter.default_format, DEFAULT_FORMAT)
49
+ q = ureg('1 mm')
50
+ self.assertEqual(f'{q}', '1 mm')
51
+ h5tbx.set_config(ureg_format='Lx~')
52
+ self.assertEqual(h5tbx.get_config('ureg_format'), 'Lx~')
53
+ self.assertEqual(f'{q}', '\\SI[]{\\begin{pmatrix}\\end{pmatrix}}{\\milli\\meter}')
54
+ h5tbx.set_config(ureg_format='C~')
55
+ self.assertEqual(f'{q}', '1 mm')
56
+
57
+ q = ureg('2 mm')
58
+ h5tbx.set_config(ureg_format='Lx~')
59
+ self.assertEqual(f'{q}', '\\SI[]{\\begin{pmatrix}\\end{pmatrix}}{\\milli\\meter}')
60
+ self.assertEqual(f"{q:.2~Lx}", '\\SI[]{\\begin{pmatrix}.2\\end{pmatrix}}{\\milli\\meter}')
61
+ h5tbx.set_config(ureg_format=DEFAULT_FORMAT)
62
+
63
+ def test_set_parameter(self):
64
+ h5tbx.use(None)
65
+
66
+ with File(mode='w') as h5:
67
+ self.assertEqual(h5.__class__, core.File)
68
+
69
+ h5 = File()
70
+ self.assertEqual(h5.__class__, core.File)
71
+ h5.close()
72
+
73
+ h5tbx.use('h5tbx')
74
+
75
+ with File() as h5:
76
+ self.assertEqual(h5.__class__, h5tbx.File)
77
+
78
+ h5 = File()
79
+ self.assertEqual(h5.__class__, h5tbx.File)
80
+ h5.close()
@@ -0,0 +1,148 @@
1
+ import datetime
2
+ import numpy as np
3
+ import unittest
4
+ import xarray as xr
5
+
6
+ import h5rdmtoolbox as h5tbx
7
+ # noinspection PyUnresolvedReferences
8
+ from h5rdmtoolbox.extensions import normalize, vector, magnitude, units, onto
9
+
10
+
11
+ class TestExtension(unittest.TestCase):
12
+
13
+ def setUp(self) -> None:
14
+ h5tbx.use(None)
15
+
16
+ def test_onto(self):
17
+ with h5tbx.File() as h5:
18
+ h5.onto.create_person(orcid_id='https://orcid.org/0000-0001-8729-0482',
19
+ first_name='Matthias',
20
+ last_name='Probst')
21
+ self.assertEqual(h5['Matthias_Probst'].attrs['orcid_id'], 'https://orcid.org/0000-0001-8729-0482')
22
+ self.assertEqual(h5['Matthias_Probst'].attrs['first_name'], 'Matthias')
23
+ self.assertEqual(h5['Matthias_Probst'].attrs['last_name'], 'Probst')
24
+ self.assertEqual(h5['Matthias_Probst'].rdf.subject, 'https://orcid.org/0000-0001-8729-0482')
25
+
26
+ h5.dumps()
27
+
28
+ def test_Magnitude(self):
29
+ with h5tbx.File() as h5:
30
+ h5.create_dataset('x', data=[1, 2, 3], make_scale=True, attrs={'units': 'm'})
31
+ h5.create_dataset('y', data=[1, 2, 3, 4], make_scale=True, attrs={'units': 'm'})
32
+ h5.create_dataset('u', data=np.random.random((4, 3)), attach_scales=('y', 'x'))
33
+ h5.create_dataset('v', data=np.random.random((4, 3)), attach_scales=('y', 'x'))
34
+ mag_interface = h5.Magnitude('u', 'v')
35
+ self.assertIsInstance(mag_interface.datasets, dict)
36
+ mag = mag_interface[()]
37
+ self.assertEqual(mag.name, 'magnitude_of_u_and_v')
38
+ self.assertEqual(float(mag[0, 0]),
39
+ float(np.sqrt(h5['u'][0, 0] ** 2 + h5['v'][0, 0] ** 2)))
40
+ self.assertEqual(float(mag[1, 0]),
41
+ float(np.sqrt(h5['u'][1, 0] ** 2 + h5['v'][1, 0] ** 2)))
42
+ self.assertEqual(float(mag[0, 1]),
43
+ float(np.sqrt(h5['u'][0, 1] ** 2 + h5['v'][0, 1] ** 2)))
44
+ self.assertEqual(float(mag[3, 2]),
45
+ float(np.sqrt(h5['u'][3, 2] ** 2 + h5['v'][3, 2] ** 2)))
46
+
47
+ mag_interface = h5.Magnitude('u', 'v', name='uv')
48
+ mag = mag_interface[()]
49
+ self.assertEqual(mag.name, 'uv')
50
+
51
+ def test_Vector(self):
52
+ """Test the Vector special dataset"""
53
+ with h5tbx.File() as h5:
54
+ h5.create_dataset('u', data=[1, 2, 3])
55
+ h5.create_dataset('v', data=[2, 2, 2])
56
+
57
+ self.assertIsInstance(h5.Vector('u', 'v')[:], xr.Dataset)
58
+ vec = h5.Vector(h5['u'], h5['v'])[:]
59
+ self.assertIsInstance(vec, xr.Dataset)
60
+ self.assertTrue('u' in vec.data_vars)
61
+ self.assertTrue('v' in vec.data_vars)
62
+ vec = h5.Vector(uu=h5['u'], vv=h5['v'])[:]
63
+ self.assertIsInstance(vec, xr.Dataset)
64
+ self.assertTrue('uu' in vec.data_vars)
65
+ self.assertTrue('vv' in vec.data_vars)
66
+ with self.assertRaises(ValueError):
67
+ h5.Vector('u', 'v', u='u')
68
+ with self.assertRaises(TypeError):
69
+ h5.Vector('u', 6.5)
70
+ with self.assertRaises(TypeError):
71
+ h5.Vector(u='u', v=6.5)
72
+
73
+ vec = h5.Vector(uu='u', vv='v')[:]
74
+ self.assertListEqual(list(vec.data_vars), ['uu', 'vv'])
75
+ self.assertEqual(vec['uu'][0], 1)
76
+ self.assertEqual(vec['vv'][0], 2)
77
+
78
+ vec = h5.Vector(u='u', v='v').isel(dim_0=slice(None, None, None), dim_1=slice(None, None, None))
79
+ self.assertIsInstance(vec, xr.Dataset)
80
+ self.assertTrue('u' in vec.data_vars)
81
+ self.assertTrue('v' in vec.data_vars)
82
+ self.assertEqual(vec['u'][0], 1)
83
+ self.assertEqual(vec['v'][0], 2)
84
+
85
+ with self.assertRaises(KeyError):
86
+ h5.Vector(u='u', v='v').sel(dim_0=5, dim_1=2)
87
+
88
+ def test_normalize_issue_with_time_vector(self):
89
+ with h5tbx.File() as h5:
90
+ h5.create_dataset('x', data=[1, 2, 3], make_scale=True)
91
+ now = datetime.datetime.now()
92
+ h5.create_time_dataset('t',
93
+ data=[now, now + datetime.timedelta(seconds=1), now + datetime.timedelta(seconds=2)],
94
+ time_format='iso',
95
+ make_scale=True)
96
+ u = h5.create_dataset('u', data=[-4, 10, 0], attach_scales=[('x', 't'), ],
97
+ attrs=dict(units='m/s'))
98
+
99
+ with self.assertRaises(AssertionError):
100
+ u.normalize(10)
101
+
102
+ with self.assertRaises(TypeError):
103
+ u.normalize(ref={'t': 10})[()]
104
+
105
+ unorm = u.normalize(ref=10)[()]
106
+ np.testing.assert_almost_equal(unorm.values, u.values[()] / 10)
107
+ self.assertEqual(unorm.units, 'm/s')
108
+ self.assertEqual(unorm.name, 'u/ref')
109
+
110
+ with self.assertRaises(TypeError):
111
+ uxnorm = u.normalize(ref=10, name='uxnorm').x(xref={'t': 10})[()]
112
+ uxnorm = u.normalize(ref=10, name='uxnorm').x(xref=10)[()]
113
+ self.assertEqual(uxnorm.name, 'uxnorm')
114
+ np.testing.assert_almost_equal(uxnorm.values, u.values[()] / 10)
115
+ np.testing.assert_almost_equal(uxnorm['x/xref'].values, h5.x.values[()] / 10)
116
+ self.assertEqual(uxnorm.units, 'm/s')
117
+
118
+ uxnorm = u.normalize(ref=10, name='uxnorm').x(xref=10).isel(x=slice(None, None, None))
119
+ np.testing.assert_almost_equal(uxnorm.values, u.values[()] / 10)
120
+ np.testing.assert_almost_equal(uxnorm['x/xref'].values, h5.x.values[()] / 10)
121
+
122
+ uxnorm = u.normalize(ref=10, name='uxnorm').x(xref=10).sel(x=[1.1, 2.1, 3.1], method='nearest')
123
+ np.testing.assert_almost_equal(uxnorm.values, u.values[()] / 10)
124
+ np.testing.assert_almost_equal(uxnorm['x/xref'].values, h5.x.values[()] / 10)
125
+
126
+ uxnorm = u.normalize(ref='10 m/s').x(xref='10 m', name='xnorm')[()]
127
+ np.testing.assert_almost_equal(uxnorm.values, u.values[()] / 10)
128
+ np.testing.assert_almost_equal(uxnorm['xnorm'].values, h5.x.values[()] / 10)
129
+ self.assertEqual(uxnorm['xnorm'].units, '1/m')
130
+ self.assertEqual(uxnorm.units, '')
131
+
132
+ def test_units_to(self):
133
+ with h5tbx.File(mode='w') as h5:
134
+ ds = h5.create_dataset('x', data=[1, 2, 3], make_scale=True, attrs={'units': 'm'})
135
+ y = h5.create_dataset('y', data=[1, 0, 1], attach_scale='x', attrs={'units': 'mm'})
136
+ ds_cm = ds.to_units('cm')[()]
137
+ self.assertEqual('cm', ds_cm.attrs['units'])
138
+
139
+ y_cm = y.to_units('cm')[()]
140
+ self.assertEqual('cm', y_cm.attrs['units'])
141
+
142
+ y_xcm = y.to_units({'x': 'cm'})[()]
143
+ self.assertEqual('mm', y_xcm.attrs['units'])
144
+ self.assertEqual('cm', y_xcm.x.attrs['units'])
145
+
146
+ y_xcm = y.to_units(x='cm')[()]
147
+ self.assertEqual('mm', y_xcm.attrs['units'])
148
+ self.assertEqual('cm', y_xcm.x.attrs['units'])
@@ -0,0 +1,85 @@
1
+ """test h5rdmtoolbox.user.py"""
2
+ import unittest
3
+
4
+ from h5rdmtoolbox import identifiers
5
+
6
+
7
+ class TestOrcid(unittest.TestCase):
8
+
9
+ def test_urn(self):
10
+ self.assertTrue(identifiers.URN('urn:isbn:0143039431').validate())
11
+ self.assertTrue(identifiers.URN('urn:efg://wxyz').validate())
12
+ self.assertTrue(identifiers.URN('urn:issn:1476-4687').validate())
13
+ self.assertFalse(identifiers.URN('xyz:issn:1476-4687').validate())
14
+
15
+ def test_rorid(self):
16
+ self.assertTrue(identifiers.RORID('https://ror.org/04wxnsj81').validate())
17
+ self.assertFalse(identifiers.RORID('https://ror.org/?4wxnsj82').validate())
18
+ self.assertTrue(identifiers.RORID('04wxnsj81').validate())
19
+ self.assertEqual(str(identifiers.RORID('04wxnsj81')), 'https://ror.org/04wxnsj81')
20
+ self.assertFalse(identifiers.RORID('https://orcid.org/0000-0002-1825-0097').validate())
21
+
22
+ with self.assertRaises(NotImplementedError):
23
+ identifiers.RORID('04wxnsj81').check_checksum()
24
+
25
+ def test_from_url(self):
26
+ self.assertIsInstance(identifiers.from_url('https://orcid.org/0000-0002-1825-0097'),
27
+ identifiers.ORCID)
28
+ # self.assertIsInstance(identifiers.from_url('https://ror.org/04t3en479'),
29
+ # identifiers.RORID)
30
+
31
+ # def test_gnd(self):
32
+ # self.assertTrue(identifiers.GND('118540268').validate())
33
+ # self.assertFalse(identifiers.GND('118540269').validate())
34
+
35
+ def test_isbn(self):
36
+ self.assertTrue(identifiers.ISBN10('80-85892-15-4').validate())
37
+ self.assertFalse(identifiers.ISBN10('80/85892-15-4').validate())
38
+ self.assertFalse(identifiers.ISBN10('80-85892-15-8').validate())
39
+ self.assertFalse(identifiers.ISBN10('80-85892-15-X').validate())
40
+ self.assertTrue(identifiers.ISBN13('978-80-85892-15-4').validate())
41
+ self.assertFalse(identifiers.ISBN13('978-80-85892-15-8').validate())
42
+ self.assertFalse(identifiers.ISBN13('978/80-85892-15-4').validate())
43
+
44
+ def test_orcid(self):
45
+ self.assertTrue(identifiers.ORCID('https://orcid.org/0000-0002-1825-0097').exists())
46
+ self.assertFalse(identifiers.ORCID('https://orcid.org/1111-0002-1825-0097').exists())
47
+ self.assertTrue(identifiers.ORCID('https://orcid.org/0000-0002-1825-0097').validate())
48
+ self.assertFalse(identifiers.ORCID('https://orcid.org/0000-0002-1825-0096').validate())
49
+ self.assertTrue(identifiers.ORCID('0000-0002-1825-0097').validate())
50
+ self.assertTrue(identifiers.ORCID('https://orcid.org/0000-0001-5109-3700').validate())
51
+ self.assertTrue(identifiers.ORCID('https://orcid.org/0000-0002-1694-233X').validate())
52
+
53
+ def test_orcid_cache(self):
54
+ identifiers.KNOWN_ORCID_FILENAME.unlink(missing_ok=True)
55
+ self.assertEqual(len(identifiers.ORCID.get_existing_orcids()), 0)
56
+ identifiers.ORCID('https://orcid.org/0000-0002-1825-0097').exists()
57
+ print(identifiers.ORCID.get_existing_orcids())
58
+ self.assertEqual(len(identifiers.ORCID.get_existing_orcids()), 1)
59
+ self.assertEqual(identifiers.ORCID.get_existing_orcids()[0], 'https://orcid.org/0000-0002-1825-0097')
60
+
61
+ # def test_orcid_exists(self):
62
+ # o = h5tbx.orcid.KnownOrcids()
63
+ # o._filename = h5tbx.utils.generate_temporary_filename()
64
+ # o.load()
65
+ # self.assertEqual([], o._orcids)
66
+ # o._orcids = ['1', '2']
67
+ # o.save()
68
+ # self.assertTrue(o.filename.exists())
69
+ #
70
+ # o.add('3')
71
+ # self.assertEqual(['1', '2', '3'], o.orcids)
72
+ #
73
+ # o = h5tbx.orcid.ORCID([h5tbx.__author_orcid__])
74
+ # self.assertIsInstance(o, h5tbx.orcid.ORCID)
75
+ #
76
+ # o = h5tbx.orcid.ORCID([h5tbx.__author_orcid__, h5tbx.__author_orcid__])
77
+ # self.assertIsInstance(o, h5tbx.orcid.ORCIDS)
78
+ # orcids = h5tbx.orcid.ORCIDS([h5tbx.__author_orcid__])
79
+ # for o in orcids:
80
+ # self.assertIsInstance(o, h5tbx.orcid.ORCID)
81
+ # self.assertEqual(h5tbx.__author_orcid__, o)
82
+ # self.assertIsInstance(orcids[0], h5tbx.orcid.ORCID)
83
+ # self.assertEqual(h5tbx.__author_orcid__, orcids[0])
84
+ #
85
+ # print(o.validate())
tests/test_plotting.py ADDED
@@ -0,0 +1,155 @@
1
+ import matplotlib.pyplot as plt
2
+ import unittest
3
+
4
+ import h5rdmtoolbox as h5tbx
5
+ from h5rdmtoolbox import plotting
6
+
7
+
8
+ class TestPlotting(unittest.TestCase):
9
+
10
+ def test_decode_label(self):
11
+ self.assertTupleEqual(('test', ''), plotting.decode_label('test'))
12
+ self.assertEqual(('test ', 'm/s'), plotting.decode_label('test [m/s]'))
13
+
14
+ def test_build_label_unit_str(self):
15
+ label_str = plotting.build_label_unit_str('velocity', 'm/s', units_format='/')
16
+ self.assertEqual('velocity / $m/s$', label_str)
17
+
18
+ with self.assertRaises(ValueError):
19
+ plotting.build_label_unit_str('velocity', 'm/s', units_format='?')
20
+
21
+ label_str = plotting.build_label_unit_str('velocity', 'm/s', units_format='(')
22
+ self.assertEqual('velocity ($m/s$)', label_str)
23
+
24
+ label_str = plotting.build_label_unit_str('velocity', 'm/s', units_format='[')
25
+ self.assertEqual('velocity [$m/s$]', label_str)
26
+
27
+ with h5tbx.set_config(xarray_unit_repr_in_plots='in'):
28
+ label_str = plotting.build_label_unit_str('velocity', 'm/s', units_format=None)
29
+ self.assertEqual('velocity in $m/s$', label_str)
30
+
31
+ self.assertEqual("velocity [$m/s$]",
32
+ plotting.XarrayLabelManipulation._adjust_units_label('velocity [m/s]', units_format='['))
33
+
34
+ self.assertEqual("velocity ($m/s$)",
35
+ plotting.XarrayLabelManipulation._adjust_units_label('velocity [m/s]', units_format='('))
36
+
37
+ self.assertEqual("velocity",
38
+ plotting.XarrayLabelManipulation._adjust_units_label('velocity', units_format='('))
39
+
40
+ self.assertEqual("velocity",
41
+ plotting.XarrayLabelManipulation._adjust_units_label('velocity []', units_format='('))
42
+
43
+ with h5tbx.set_config(xarray_unit_repr_in_plots='in'):
44
+ self.assertEqual("velocity in $m/s$",
45
+ plotting.XarrayLabelManipulation._adjust_units_label('velocity [m/s]', units_format=None))
46
+
47
+ self.assertEqual(None,
48
+ plotting.XarrayLabelManipulation._adjust_units_label(None))
49
+
50
+ with h5tbx.use(None):
51
+ with h5tbx.File() as h5:
52
+ h5.create_dataset('x', data=[1, 2, 3],
53
+ make_scale=True,
54
+ attrs=dict(units='m',
55
+ long_name='x_long_name'))
56
+ h5.create_dataset('velocity', data=[5.3, 6.2, 7.1],
57
+ attrs=dict(units='m/s',
58
+ long_name='velocity'))
59
+ plt.figure()
60
+ ax = plt.gca()
61
+ with h5tbx.set_config(xarray_unit_repr_in_plots='['):
62
+ h5['velocity'][()].plot(ax=ax)
63
+ self.assertEqual(ax.get_xlabel(), f'dim_0')
64
+ self.assertEqual(ax.get_ylabel(), f'velocity [$m/s$]')
65
+ plt.close()
66
+
67
+ h5['velocity'].dims[0].attach_scale(h5['x'])
68
+ plt.figure()
69
+ ax = plt.gca()
70
+ with h5tbx.set_config(xarray_unit_repr_in_plots='['):
71
+ h5['velocity'][()].plot(ax=ax)
72
+ self.assertEqual(ax.get_xlabel(), f'x_long_name [$m$]')
73
+ self.assertEqual(ax.get_ylabel(), f'velocity [$m/s$]')
74
+ plt.close()
75
+
76
+ plt.figure()
77
+ ax = plt.gca()
78
+ with h5tbx.set_config(xarray_unit_repr_in_plots='('):
79
+ h5['velocity'][()].plot(ax=ax)
80
+ self.assertEqual(ax.get_xlabel(), f'x_long_name ($m$)')
81
+ self.assertEqual(ax.get_ylabel(), f'velocity ($m/s$)')
82
+ plt.close()
83
+
84
+ plt.figure()
85
+ ax = plt.gca()
86
+ with h5tbx.set_config(xarray_unit_repr_in_plots='('):
87
+ h5['velocity'][()].plot(ax=ax)
88
+ ax.set_xlabel('x [mm]')
89
+ ax.set_ylabel('velocity [km/s]')
90
+ self.assertEqual(ax.get_xlabel(), f'x ($mm$)')
91
+ self.assertEqual(ax.get_ylabel(), f'velocity ($km/s$)')
92
+
93
+ ax.set_xlabel(h5['x'][()])
94
+ ax.set_ylabel(h5['velocity'][()])
95
+ self.assertEqual(ax.get_xlabel(), f'x_long_name ($m$)')
96
+ self.assertEqual(ax.get_ylabel(), f'velocity ($m/s$)')
97
+
98
+ x = h5['x'][()]
99
+ x.attrs = {'units': 'm'}
100
+ v = h5['velocity'][()]
101
+ v.attrs = {'units': 'm/s'}
102
+ ax.set_xlabel(x)
103
+ ax.set_ylabel(v)
104
+ self.assertEqual(ax.get_xlabel(), f'x ($m$)')
105
+ self.assertEqual(ax.get_ylabel(), f'velocity ($m/s$)')
106
+ plt.close()
107
+
108
+ plt.figure()
109
+ ax = plt.gca()
110
+ with h5tbx.set_config(xarray_unit_repr_in_plots='('):
111
+ h5['velocity'][()].plot(ax=ax)
112
+ ax.set_xylabel('x', 'y')
113
+ self.assertEqual(ax.get_xlabel(), f'x')
114
+ self.assertEqual(ax.get_ylabel(), f'y')
115
+ plt.close()
116
+
117
+ def test_label(self):
118
+ h5tbx.use(None)
119
+ with h5tbx.File() as h5:
120
+ h5.create_dataset('x', data=[1, 2, 3],
121
+ make_scale=True,
122
+ attrs=dict(units='m/s',
123
+ long_name='x_long_name'))
124
+
125
+ h5tbx.set_config(xarray_unit_repr_in_plots='[]')
126
+ h5['x'][()].plot()
127
+ self.assertEqual("x_long_name [$m/s$]", plt.gca().get_ylabel())
128
+ plt.close()
129
+
130
+ h5tbx.set_config(xarray_unit_repr_in_plots='/')
131
+ h5['x'][()].plot()
132
+ self.assertEqual("x_long_name / $m/s$", plt.gca().get_ylabel())
133
+ plt.close()
134
+
135
+ h5tbx.set_config(xarray_unit_repr_in_plots='in')
136
+ h5['x'][()].plot()
137
+ self.assertEqual("x_long_name in $m/s$", plt.gca().get_ylabel())
138
+ plt.close()
139
+
140
+ h5tbx.set_config(xarray_unit_repr_in_plots='()')
141
+ h5['x'][()].plot()
142
+ self.assertEqual("x_long_name ($m/s$)", plt.gca().get_ylabel())
143
+ plt.close()
144
+
145
+ h5.create_dataset('y', data=[1, 2, 3],
146
+ attach_scale=h5['x'],
147
+ attrs=dict(units='Pa',
148
+ long_name='y_long_name', ))
149
+
150
+ plt.figure()
151
+ h5tbx.set_config(xarray_unit_repr_in_plots='()')
152
+ h5['y'][()].plot()
153
+ self.assertEqual("x_long_name ($m/s$)", plt.gca().get_xlabel())
154
+ self.assertEqual("y_long_name ($Pa$)", plt.gca().get_ylabel())
155
+ plt.close()
tests/test_repr.py ADDED
@@ -0,0 +1,114 @@
1
+ import numpy as np
2
+ import unittest
3
+
4
+ import h5rdmtoolbox as h5tbx
5
+ from h5rdmtoolbox import File
6
+ from h5rdmtoolbox import _repr
7
+ from h5rdmtoolbox._repr import process_string_for_link
8
+ # noinspection PyUnresolvedReferences
9
+ from h5rdmtoolbox.database import mongo
10
+
11
+
12
+ class TestRepr(unittest.TestCase):
13
+
14
+ def setUp(self) -> None:
15
+ h5tbx.use(None)
16
+
17
+ def test_hide_uri(self):
18
+ with h5tbx.File() as h5:
19
+ h5.attrs['orcid', 'https://example.ord/hasOrcid'] = h5tbx.__author_orcid__
20
+
21
+ ssr = _repr.HDF5StructureStrRepr()
22
+ ssr(h5)
23
+
24
+ s = ssr.__attrs__('orcid', h5['/'])
25
+ self.assertEqual(
26
+ s,
27
+ '\x1b[3ma: orcid (p=https://example.ord/hasOrcid)\x1b[0m: https://orcid.org/0000-0001-8729-0482'
28
+ )
29
+
30
+ ssr = _repr.HDF5StructureStrRepr(hide_uri=True)
31
+ ssr(h5)
32
+ self.assertEqual(
33
+ ssr.__attrs__('orcid', h5['/']),
34
+ '\x1b[3ma: orcid (p=https://example.ord/hasOrcid)\x1b[0m: https://orcid.org/0000-0001-8729-0482'
35
+ )
36
+
37
+ def test_process_string_for_link(self):
38
+ string_without_url = "This is a string without a web URL"
39
+ processed_string, has_url = process_string_for_link(string_without_url)
40
+ self.assertFalse(has_url)
41
+ self.assertEqual(processed_string, string_without_url)
42
+
43
+ zenodo_url = 'https://zenodo.org/record/10428822'
44
+ img_url = f'https://zenodo.org/badge/DOI/10.5281/zenodo.10428822.svg'
45
+ self.assertEqual(f'<a href="{zenodo_url}"><img src="{img_url}" alt="DOI"></a>',
46
+ process_string_for_link(zenodo_url)[0])
47
+
48
+ zenodo_url = 'https://doi.org/10.5281/zenodo.10428822'
49
+ img_url = f'https://zenodo.org/badge/DOI/10.5281/zenodo.10428822.svg'
50
+ self.assertEqual(f'<a href="{zenodo_url}"><img src="{img_url}" alt="DOI"></a>',
51
+ process_string_for_link('10.5281/zenodo.10428822')[0])
52
+
53
+ string_with_url = "This is a string with a web URL: https://www.example.com which goes on and on and on"
54
+ string_with_href = 'This is a string with a web URL: ' \
55
+ '<a href="https://www.example.com">https://www.example.com</a> which goes on and on and on'
56
+ processed_string, has_url = process_string_for_link(string_with_url)
57
+ self.assertTrue(has_url)
58
+ self.assertEqual(processed_string, string_with_href)
59
+
60
+ def test_dump_orcid(self):
61
+ # with File() as h5:
62
+ # h5.attrs['orcid'] = h5tbx.__author_orcid__
63
+ # h5.dump()
64
+ with File() as h5:
65
+ h5.attrs['orcid'] = [h5tbx.__author_orcid__, h5tbx.__author_orcid__, ]
66
+ h5.dump()
67
+
68
+ def test_repr_def(self):
69
+ with File() as h5:
70
+ h5.attrs['orcid'] = h5tbx.__author_orcid__
71
+ h5.rdf['orcid'].definition = 'https://example.com/hasOrcid'
72
+
73
+ ssr = _repr.HDF5StructureStrRepr()
74
+ ssr(h5)
75
+
76
+ s = ssr.__attrs__('orcid', h5['/'])
77
+ self.assertEqual(
78
+ s,
79
+ '\x1b[3ma: orcid (def=https://example.com/hasOrcid)\x1b[0m: https://orcid.org/0000-0001-8729-0482')
80
+
81
+ def test_repr(self):
82
+ with File(h5tbx.utils.generate_temporary_filename(), 'w') as h5:
83
+ h5.create_dataset('ds', data=3, dtype='int64')
84
+ h5.create_dataset('dsfloat', data=3.0, dtype='float64')
85
+ h5.create_dataset('str', data='str')
86
+ h5.create_dataset('bool', data=True)
87
+ h5.create_dataset('arr',
88
+ data=np.random.random((4, 5, 3)),
89
+ chunks=(4, 5, 1),
90
+ maxshape=(4, 5, 3))
91
+
92
+ ssr = _repr.HDF5StructureStrRepr()
93
+ ssr(h5, preamble='My preamble')
94
+
95
+ self.assertEqual(ssr.__0Ddataset__('ds0', h5['str']),
96
+ '\x1b[1mds0\x1b[0m ?type?, dtype: |S3')
97
+
98
+ s = ssr.__dataset__('ds', h5['ds'])
99
+ self.assertEqual(s, '\x1b[1mds\x1b[0m 3, dtype: int64')
100
+
101
+ s = ssr.__0Ddataset__('dsbool', h5['bool'])
102
+ self.assertEqual(s, '\x1b[1mdsbool\x1b[0m True, dtype: bool')
103
+
104
+ s = ssr.__dataset__('dsfloat', h5['dsfloat'])
105
+ self.assertEqual(s, '\x1b[1mdsfloat\x1b[0m 3.000000, dtype: float64')
106
+
107
+ s = ssr.__dataset__('str', h5['str'])
108
+ self.assertEqual(s, "\x1b[1mstr\x1b[0m: b'str'")
109
+
110
+ shr = _repr.HDF5StructureHTMLRepr()
111
+ shr(h5)
112
+
113
+ shr.__dataset__('ds', h5['ds'])
114
+ shr.__dataset__('dsfloat', h5['dsfloat'])