blissdata 2.0.1__tar.gz → 2.1.0__tar.gz

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 (97) hide show
  1. blissdata-2.1.0/LICENSE +166 -0
  2. {blissdata-2.0.1 → blissdata-2.1.0}/PKG-INFO +5 -2
  3. {blissdata-2.0.1 → blissdata-2.1.0}/pyproject.toml +4 -2
  4. blissdata-2.1.0/src/blissdata/h5map/__init__.py +21 -0
  5. blissdata-2.1.0/src/blissdata/h5map/data_items.py +184 -0
  6. blissdata-2.1.0/src/blissdata/h5map/hdf5_items.py +164 -0
  7. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/lima/client.py +1 -142
  8. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/redis_engine/event_stream.py +1 -8
  9. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/redis_engine/scan.py +1 -15
  10. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/redis_engine/store.py +1 -1
  11. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/streams/base.py +2 -10
  12. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/streams/lima/stream.py +5 -3
  13. blissdata-2.1.0/src/blissdata/streams/lima2/stream.py +253 -0
  14. blissdata-2.1.0/src/blissdata/tests/h5map/test_data_items.py +291 -0
  15. blissdata-2.1.0/src/blissdata/tests/h5map/test_hdf5_items.py +265 -0
  16. blissdata-2.1.0/src/blissdata/tests/plugin_streams/test_lima2_stream.py +168 -0
  17. blissdata-2.1.0/src/blissdata/tests/redis_engine/encoding/__init__.py +0 -0
  18. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/tests/redis_engine/encoding/test_numeric.py +1 -0
  19. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata.egg-info/PKG-INFO +5 -2
  20. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata.egg-info/SOURCES.txt +7 -0
  21. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata.egg-info/requires.txt +2 -1
  22. blissdata-2.0.1/src/blissdata/streams/lima2/stream.py +0 -71
  23. blissdata-2.0.1/src/blissdata/tests/plugin_streams/test_lima2_stream.py +0 -42
  24. {blissdata-2.0.1 → blissdata-2.1.0}/README.md +0 -0
  25. {blissdata-2.0.1 → blissdata-2.1.0}/setup.cfg +0 -0
  26. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/__init__.py +0 -0
  27. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/beacon/__init__.py +0 -0
  28. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/beacon/_base.py +0 -0
  29. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/beacon/_utils.py +0 -0
  30. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/beacon/config.py +0 -0
  31. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/beacon/data.py +0 -0
  32. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/beacon/files.py +0 -0
  33. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/h5api/__init__.py +0 -0
  34. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/h5api/abstract.py +0 -0
  35. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/h5api/dynamic_hdf5.py +0 -0
  36. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/h5api/file_arguments.py +0 -0
  37. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/h5api/redis_hdf5.py +0 -0
  38. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/h5api/scan_mapping.py +0 -0
  39. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/h5api/static_hdf5.py +0 -0
  40. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/h5api/utils/__init__.py +0 -0
  41. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/h5api/utils/bliss.py +0 -0
  42. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/h5api/utils/hdf5.py +0 -0
  43. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/h5api/utils/hdf5_retry.py +0 -0
  44. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/h5api/utils/lima.py +0 -0
  45. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/h5api/utils/types.py +0 -0
  46. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/lima/__init__.py +0 -0
  47. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/lima/image_utils.py +0 -0
  48. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/py.typed +0 -0
  49. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/redis_engine/__init__.py +0 -0
  50. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/redis_engine/encoding/__init__.py +0 -0
  51. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/redis_engine/encoding/json.py +0 -0
  52. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/redis_engine/encoding/numeric.py +0 -0
  53. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/redis_engine/exceptions.py +0 -0
  54. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/redis_engine/identities.py +0 -0
  55. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/redis_engine/memory_tracker.py +0 -0
  56. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/redis_engine/sink.py +0 -0
  57. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/scan.py +0 -0
  58. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/schemas/__init__.py +0 -0
  59. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/schemas/scan_info.py +0 -0
  60. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/schemas/scan_info_display.py +0 -0
  61. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/streams/__init__.py +0 -0
  62. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/streams/hdf5_fallback/__init__.py +0 -0
  63. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/streams/hdf5_fallback/stream.py +0 -0
  64. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/streams/lima/__init__.py +0 -0
  65. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/streams/lima2/__init__.py +0 -0
  66. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/streams/scan_sequence/__init__.py +0 -0
  67. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/streams/scan_sequence/stream.py +0 -0
  68. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/tests/__init__.py +0 -0
  69. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/tests/beacon/__init__.py +0 -0
  70. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/tests/beacon/test_config.py +0 -0
  71. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/tests/beacon/test_data.py +0 -0
  72. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/tests/beacon/test_files.py +0 -0
  73. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/tests/conftest.py +0 -0
  74. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/tests/h5api/__init__.py +0 -0
  75. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/tests/h5api/scanner.py +0 -0
  76. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/tests/h5api/test_dynamic_files.py +0 -0
  77. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/tests/h5api/test_h5api_lima_utils.py +0 -0
  78. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/tests/h5api/test_hdf5_handlers.py +0 -0
  79. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/tests/h5api/test_redis_files.py +0 -0
  80. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/tests/h5api/test_static_files.py +0 -0
  81. {blissdata-2.0.1/src/blissdata/tests/lima → blissdata-2.1.0/src/blissdata/tests/h5map}/__init__.py +0 -0
  82. {blissdata-2.0.1/src/blissdata/tests/plugin_streams → blissdata-2.1.0/src/blissdata/tests/lima}/__init__.py +0 -0
  83. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/tests/lima/test_image_utils.py +0 -0
  84. {blissdata-2.0.1/src/blissdata/tests/redis_engine → blissdata-2.1.0/src/blissdata/tests/plugin_streams}/__init__.py +0 -0
  85. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/tests/plugin_streams/test_file_backed_stream.py +0 -0
  86. {blissdata-2.0.1/src/blissdata/tests/redis_engine/encoding → blissdata-2.1.0/src/blissdata/tests/redis_engine}/__init__.py +0 -0
  87. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/tests/redis_engine/conftest.py +0 -0
  88. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/tests/redis_engine/encoding/test_json.py +0 -0
  89. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/tests/redis_engine/test_cursor.py +0 -0
  90. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/tests/redis_engine/test_event_stream.py +0 -0
  91. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/tests/redis_engine/test_memory_tracker.py +0 -0
  92. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/tests/redis_engine/test_scan.py +0 -0
  93. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/tests/redis_engine/test_search.py +0 -0
  94. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata/tests/redis_engine/utils.py +0 -0
  95. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata.egg-info/dependency_links.txt +0 -0
  96. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata.egg-info/entry_points.txt +0 -0
  97. {blissdata-2.0.1 → blissdata-2.1.0}/src/blissdata.egg-info/top_level.txt +0 -0
@@ -0,0 +1,166 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
166
+
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: blissdata
3
- Version: 2.0.1
3
+ Version: 2.1.0
4
4
  Summary: Bliss data streaming client
5
5
  Maintainer: BCU (ESRF)
6
6
  License-Expression: LGPL-3.0-or-later
@@ -12,11 +12,13 @@ Classifier: Operating System :: OS Independent
12
12
  Classifier: Programming Language :: Python :: 3
13
13
  Requires-Python: >=3.10
14
14
  Description-Content-Type: text/markdown
15
+ License-File: LICENSE
15
16
  Requires-Dist: beartype
16
17
  Requires-Dist: fabio>=0.12
17
18
  Requires-Dist: h5py>=2.9
18
19
  Requires-Dist: msgpack
19
20
  Requires-Dist: numpy>=1.20
21
+ Requires-Dist: numpydantic<1.6.10
20
22
  Requires-Dist: pydantic>=2.3.0
21
23
  Requires-Dist: redis>=4.6.0
22
24
  Requires-Dist: redis-om<1.0,>=0.3.1
@@ -25,7 +27,7 @@ Provides-Extra: tango
25
27
  Requires-Dist: pytango>=9.3.1; extra == "tango"
26
28
  Provides-Extra: lima2
27
29
  Requires-Dist: blissdata[tango]; extra == "lima2"
28
- Requires-Dist: lima2-client~=2.0; extra == "lima2"
30
+ Requires-Dist: lima2-client~=4.0; extra == "lima2"
29
31
  Provides-Extra: test
30
32
  Requires-Dist: gevent>=20.9.0; extra == "test"
31
33
  Requires-Dist: pytest>=7; extra == "test"
@@ -43,6 +45,7 @@ Requires-Dist: mkdocs-material; extra == "dev"
43
45
  Requires-Dist: mkdocstrings[python]>=0.18; extra == "dev"
44
46
  Provides-Extra: beacon
45
47
  Requires-Dist: ruamel.yaml; extra == "beacon"
48
+ Dynamic: license-file
46
49
 
47
50
  # Blissdata
48
51
 
@@ -1,9 +1,10 @@
1
1
  [project]
2
2
  name = "blissdata"
3
- version = "2.0.1"
3
+ version = "2.1.0"
4
4
  description = "Bliss data streaming client"
5
5
  readme = "README.md"
6
6
  license = "LGPL-3.0-or-later"
7
+ license-files = ["LICENSE"]
7
8
  maintainers = [{name = "BCU (ESRF)"}]
8
9
  classifiers = [
9
10
  "Intended Audience :: Science/Research",
@@ -17,6 +18,7 @@ dependencies = [
17
18
  "h5py>=2.9",
18
19
  "msgpack",
19
20
  "numpy>=1.20",
21
+ "numpydantic<1.6.10",
20
22
  "pydantic>=2.3.0", # (not a direct dep) redis-om 0.3.2 doesn't support lower versions
21
23
  "redis>=4.6.0",
22
24
  "redis-om>=0.3.1,<1.0",
@@ -32,7 +34,7 @@ Changes = "https://gitlab.esrf.fr/bliss/blissdata/-/blob/master/CHANGELOG.md"
32
34
  tango = ["pytango>=9.3.1"]
33
35
  lima2 = [
34
36
  "blissdata[tango]",
35
- "lima2-client~=2.0"
37
+ "lima2-client~=4.0"
36
38
  ]
37
39
  test = [
38
40
  "gevent>=20.9.0",
@@ -0,0 +1,21 @@
1
+ from .data_items import ( # noqa: F401
2
+ DataItem,
3
+ DataItemType,
4
+ InlineRaw,
5
+ Inline,
6
+ InlineBase64,
7
+ Stream,
8
+ )
9
+ from .hdf5_items import ( # noqa: F401
10
+ HDF5Item,
11
+ HDF5ItemType,
12
+ Group,
13
+ Dataset,
14
+ ExternalBinaryDataset,
15
+ ExternalBinaryFile,
16
+ SoftLink,
17
+ ExternalLink,
18
+ VirtualDataset,
19
+ VirtualSource,
20
+ SpaceSelection,
21
+ )
@@ -0,0 +1,184 @@
1
+ import numpy as np
2
+ from typing import Any, Annotated, Literal
3
+ from pydantic import (
4
+ BaseModel,
5
+ RootModel,
6
+ Base64Bytes,
7
+ Field,
8
+ field_validator,
9
+ model_serializer,
10
+ Discriminator,
11
+ Tag,
12
+ TypeAdapter,
13
+ )
14
+ from numpydantic import NDArray
15
+
16
+
17
+ class InlineRaw(RootModel):
18
+ """Literal values to be embedded into JSON directly (no 'type' field).
19
+ Accepts scalars or arrays of any shape with types that can be coerced using
20
+ the following table:
21
+ -------------- --------- ---------------
22
+ | Python input | Json | Python output |
23
+ -------------- --------- ---------------
24
+ | any integer | integer | np.int64 |
25
+ | any floating | decimal | np.float64 |
26
+ | any boolean | boolean | np.bool_ |
27
+ | any string | string | np.str_ |
28
+ -------------- --------- ---------------
29
+ For mixed types arrays, coercion follows this hierarchy (lowest to highest):
30
+ bool → int64 → float64 → str
31
+ Example:
32
+ [True, 123, 1.23] → [1.0, 123.0, 1.23]
33
+
34
+ Non-finite values (nan, -inf, inf) will be refused, one should use Inline
35
+ model instead.
36
+ """
37
+
38
+ root: NDArray[Any, np.bool_ | np.str_ | np.int64 | np.float64]
39
+
40
+ @field_validator("root", mode="before")
41
+ @classmethod
42
+ def coerce_numbers(cls, value):
43
+ array = np.asarray(value)
44
+ if np.issubdtype(array.dtype, np.floating):
45
+ if not np.isfinite(value).all():
46
+ raise ValueError("non-finite values (nan, -inf, inf) are not allowed.")
47
+ array = array.astype(np.float64)
48
+ elif np.issubdtype(array.dtype, np.integer):
49
+ array = array.astype(np.int64)
50
+ return array
51
+
52
+ @model_serializer(when_used="json", mode="wrap")
53
+ def fix_empty_string_serialization(self, nxt, info):
54
+ if np.issubdtype(self.root.dtype, np.str_) and not self.root.shape:
55
+ return str(self.root)
56
+ else:
57
+ return nxt(self, info)
58
+
59
+ def __eq__(self, other):
60
+ if other.__class__ is self.__class__:
61
+ return np.array_equal(self.root, other.root)
62
+ return NotImplemented
63
+
64
+ def decode(self):
65
+ value = self.model_dump()
66
+ if value.dtype.kind in "UT":
67
+ return value.tolist()
68
+ else:
69
+ return value
70
+
71
+
72
+ # Numpy dtype names (uint8, float16, complex128...), but limited to numbers and
73
+ # booleans.
74
+ _DType = Annotated[
75
+ str,
76
+ Field(
77
+ pattern=r"^(bool|(int|uint)(8|16|32|64)|float(16|32|64|128)|complex(64|128|256))$"
78
+ ),
79
+ ]
80
+
81
+
82
+ # Numpy's array interface protocol type strings, but limited to numbers
83
+ # and booleans. This notation contains both dtype and endianness.
84
+ # See Numpy array interface protocol definition here:
85
+ # https://numpy.org/doc/stable/reference/arrays.interface.html
86
+ _BinaryDType = Annotated[
87
+ str, Field(pattern=r"^(\|?[iub]1|[><]([iuf][248]|[fc](8|16)|c32))$")
88
+ ]
89
+
90
+
91
+ class _InlineValue(RootModel):
92
+ """Value for the 'Inline' model, can be any JSON value other than a mapping
93
+ or null value. Different from InlineRaw because it can be heterogeneous and
94
+ non-finite values (nan, inf, -inf) are coerced into string.
95
+ """
96
+
97
+ root: str | bool | int | float | list["_InlineValue"]
98
+
99
+ @field_validator("root", mode="before")
100
+ @classmethod
101
+ def coerce_numpy_values(cls, value):
102
+ def coerce_single(item):
103
+ if not np.issubdtype(type(item), np.number):
104
+ # str | bool
105
+ return item
106
+ elif not np.isfinite(item) or np.issubdtype(type(item), np.complexfloating):
107
+ # nan | inf | -inf | complex
108
+ return str(item)
109
+ elif isinstance(item, np.generic):
110
+ # numpy scalar
111
+ return item.item()
112
+ else:
113
+ # python scalar
114
+ return item
115
+
116
+ def coerce_all(value):
117
+ if isinstance(value, list):
118
+ return [coerce_all(i) for i in value]
119
+ elif isinstance(value, np.ndarray) and value.ndim > 1:
120
+ return value
121
+ elif isinstance(value, np.ndarray) and value.ndim > 0:
122
+ return [coerce_all(value[i]) for i in range(value.shape[0])]
123
+ else:
124
+ return coerce_single(value)
125
+
126
+ return coerce_all(value)
127
+
128
+
129
+ class Inline(BaseModel):
130
+ """Explicitly typed JSON value to produce specific HDF5 type (int8, float32, etc).
131
+ Can be used to transmit string as float like "nan" or "inf".
132
+ """
133
+
134
+ type: Literal["inline"] = "inline"
135
+ dtype: _DType
136
+ value: _InlineValue
137
+
138
+ def decode(self):
139
+ return np.array(self.value.model_dump(), dtype=self.dtype)
140
+
141
+
142
+ class InlineBase64(BaseModel):
143
+ """Base64 encoded binary value."""
144
+
145
+ type: Literal["inline_b64"] = "inline_b64"
146
+ dtype: _BinaryDType
147
+ shape: list[int] | None = None
148
+ value: Base64Bytes
149
+
150
+ def decode(self):
151
+ value = np.frombuffer(self.value, dtype=self.dtype)
152
+ if self.shape is not None:
153
+ return value.reshape(self.shape)
154
+ else:
155
+ return value
156
+
157
+
158
+ class Stream(BaseModel):
159
+ type: Literal["stream"] = "stream"
160
+ stream: str
161
+
162
+ def decode(self):
163
+ raise TypeError("Stream model is just a reference, can't use decode()")
164
+
165
+
166
+ def get_discriminator_value(v: Any) -> str:
167
+ if isinstance(v, dict):
168
+ return v.get("type")
169
+ if issubclass(type(v), BaseModel):
170
+ return getattr(v, "type", "raw")
171
+ return "raw"
172
+
173
+
174
+ DataItemType = Annotated[
175
+ (
176
+ Annotated[Inline, Tag("inline")]
177
+ | Annotated[InlineBase64, Tag("inline_b64")]
178
+ | Annotated[Stream, Tag("stream")]
179
+ | Annotated[InlineRaw, Tag("raw")]
180
+ ),
181
+ Discriminator(get_discriminator_value),
182
+ ]
183
+
184
+ DataItem = TypeAdapter(DataItemType)
@@ -0,0 +1,164 @@
1
+ from pydantic import (
2
+ BaseModel,
3
+ Field,
4
+ NonNegativeInt,
5
+ model_serializer,
6
+ model_validator,
7
+ field_validator,
8
+ Discriminator,
9
+ Tag,
10
+ TypeAdapter,
11
+ )
12
+ from typing import Any, Annotated, Literal
13
+ from .data_items import DataItemType, _BinaryDType
14
+
15
+
16
+ class Dataset(BaseModel):
17
+ type: Literal["dataset"] = "dataset"
18
+ attributes: dict[str, DataItemType] = Field(default_factory=dict)
19
+ value: DataItemType
20
+
21
+ @model_serializer(mode="wrap")
22
+ def serialize_value_only(self, _next):
23
+ if self.attributes:
24
+ return _next(self)
25
+ else:
26
+ return _next(self)["value"]
27
+
28
+ @model_validator(mode="before")
29
+ @classmethod
30
+ def validate_value_only(cls, data: Any) -> Any:
31
+ if isinstance(data, dict) and data.get("type", "dataset") == "dataset":
32
+ return data
33
+ else:
34
+ return {"type": "dataset", "value": data}
35
+
36
+
37
+ class ExternalBinaryFile(BaseModel):
38
+ name: str
39
+ offset: NonNegativeInt
40
+ size: NonNegativeInt | None = None # None means H5F_UNLIMITED
41
+
42
+
43
+ class ExternalBinaryDataset(BaseModel):
44
+ type: Literal["external_binary_dataset"] = "external_binary_dataset"
45
+ attributes: dict[str, DataItemType] = Field(default_factory=dict)
46
+ dtype: _BinaryDType
47
+ shape: list[int]
48
+ files: list[ExternalBinaryFile] = Field(min_length=1)
49
+
50
+ @model_serializer(mode="wrap")
51
+ def serialize(self, _next):
52
+ serialized = _next(self)
53
+ if not self.attributes:
54
+ serialized.pop("attributes", None)
55
+ return serialized
56
+
57
+
58
+ class SpaceSelection(BaseModel):
59
+ start: list[NonNegativeInt]
60
+ stride: list[NonNegativeInt] | None = None
61
+ count: list[NonNegativeInt]
62
+ block: list[NonNegativeInt] | None = None
63
+
64
+ @field_validator("stride", mode="after")
65
+ @classmethod
66
+ def hide_all_ones_stride(cls, value):
67
+ if value is None or all(x == 1 for x in value):
68
+ return None
69
+ else:
70
+ return value
71
+
72
+ @field_validator("block", mode="after")
73
+ @classmethod
74
+ def hide_all_ones_block(cls, value):
75
+ if value is None or all(x == 1 for x in value):
76
+ return None
77
+ else:
78
+ return value
79
+
80
+
81
+ class VirtualSource(BaseModel):
82
+ vspace: SpaceSelection
83
+ src_file: str
84
+ src_dataset: str
85
+ src_space: SpaceSelection
86
+
87
+
88
+ class VirtualDataset(BaseModel):
89
+ type: Literal["virtual_dataset"] = "virtual_dataset"
90
+ attributes: dict[str, DataItemType] = Field(default_factory=dict)
91
+ dtype: _BinaryDType
92
+ shape: list[int]
93
+ virtual_sources: list[VirtualSource] = Field(min_length=1)
94
+
95
+ @model_serializer(mode="wrap")
96
+ def serialize(self, _next):
97
+ serialized = _next(self)
98
+ if not self.attributes:
99
+ serialized.pop("attributes", None)
100
+ return serialized
101
+
102
+
103
+ class SoftLink(BaseModel):
104
+ type: Literal["soft_link"] = "soft_link"
105
+ target_path: str
106
+
107
+
108
+ class ExternalLink(BaseModel):
109
+ type: Literal["external_link"] = "external_link"
110
+ target_file: str
111
+ target_path: str
112
+
113
+
114
+ def get_discriminator_value(v: Any) -> str:
115
+ h5_type = {
116
+ "group",
117
+ "dataset",
118
+ "external_binary_dataset",
119
+ "soft_link",
120
+ "external_link",
121
+ "virtual_dataset",
122
+ }
123
+ # A dataset can be represented by its value only, therefore data_item types
124
+ # such as "inline" or "stream" may be end up here. Because of this we check
125
+ # wether or not type is an h5_type, otherwise it is a dataset.
126
+ if isinstance(v, dict):
127
+ type = v.get("type")
128
+ return type if type in h5_type else "dataset"
129
+ elif isinstance(v, BaseModel):
130
+ type = getattr(v, "type", None)
131
+ return type if type in h5_type else "dataset"
132
+ else:
133
+ return "dataset"
134
+
135
+
136
+ HDF5ItemType = Annotated[
137
+ (
138
+ Annotated["Group", Tag("group")]
139
+ | Annotated[Dataset, Tag("dataset")]
140
+ | Annotated[ExternalBinaryDataset, Tag("external_binary_dataset")]
141
+ | Annotated[SoftLink, Tag("soft_link")]
142
+ | Annotated[ExternalLink, Tag("external_link")]
143
+ | Annotated[VirtualDataset, Tag("virtual_dataset")]
144
+ ),
145
+ Discriminator(get_discriminator_value),
146
+ ]
147
+
148
+
149
+ class Group(BaseModel):
150
+ type: Literal["group"] = "group"
151
+ attributes: dict[str, DataItemType] = Field(default_factory=dict)
152
+ children: dict[str, HDF5ItemType] = Field(default_factory=dict)
153
+
154
+ @model_serializer(mode="wrap")
155
+ def skip_empty_fields(self, _next):
156
+ serialized = _next(self)
157
+ if not self.attributes:
158
+ serialized.pop("attributes", None)
159
+ if not self.children:
160
+ serialized.pop("children", None)
161
+ return serialized
162
+
163
+
164
+ HDF5Item = TypeAdapter(HDF5ItemType)