Skip to content

Commit 741e212

Browse files
committed
Implement dump and raise error for dumps
1 parent c0bdd6c commit 741e212

File tree

4 files changed

+28
-22
lines changed

4 files changed

+28
-22
lines changed

src/linkml_arrays/dumpers/hdf5_dumper.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,16 @@ def _iterate_element(
3939
class Hdf5Dumper(Dumper):
4040
"""Dumper class for LinkML models to HDF5 files."""
4141

42-
# TODO is this the right method to overwrite? it does not dump a string
43-
def dumps(
42+
def dump(
4443
self,
4544
element: Union[YAMLRoot, BaseModel],
45+
to_file: str,
4646
schemaview: SchemaView,
47-
output_file_path: Union[str, Path],
4847
**kwargs,
4948
):
5049
"""Dump the element to an HDF5 file."""
51-
with h5py.File(output_file_path, "w") as f:
50+
with h5py.File(to_file, "w") as f:
5251
_iterate_element(element, schemaview, f)
52+
53+
def dumps(self, element: Union[YAMLRoot, BaseModel], **kwargs):
54+
raise NotImplementedError("This method is not sensible for this dumper.")

src/linkml_arrays/dumpers/xarray_dumpers.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,32 +97,36 @@ def _iterate_element(
9797
class XarrayNetCDFDumper(Dumper):
9898
"""Dumper class for LinkML models to HDF5 files."""
9999

100-
# TODO is this the right method to overwrite? it does not dump a string
101-
def dumps(
100+
def dump(
102101
self,
103102
element: Union[YAMLRoot, BaseModel],
103+
to_file: str,
104104
schemaview: SchemaView,
105-
output_file_path: Union[str, Path],
106105
**kwargs,
107106
):
108107
"""Dump the element to an HDF5 file."""
109108
datatree = DataTree()
110109
datatree = _iterate_element(element, schemaview, datatree)
111-
datatree.to_netcdf(output_file_path, engine='h5netcdf')
110+
datatree.to_netcdf(to_file, engine='h5netcdf')
111+
112+
def dumps(self, element: Union[YAMLRoot, BaseModel], **kwargs):
113+
raise NotImplementedError("This method is not sensible for this dumper.")
112114

113115

114116
class XarrayZarrDumper(Dumper):
115117
"""Dumper class for LinkML models to HDF5 files."""
116118

117-
# TODO is this the right method to overwrite? it does not dump a string
118-
def dumps(
119+
def dump(
119120
self,
120121
element: Union[YAMLRoot, BaseModel],
122+
to_file: str,
121123
schemaview: SchemaView,
122-
output_file_path: Union[str, Path],
123124
**kwargs,
124125
):
125126
"""Dump the element to an HDF5 file."""
126127
datatree = DataTree()
127128
datatree = _iterate_element(element, schemaview, datatree)
128-
datatree.to_zarr(output_file_path)
129+
datatree.to_zarr(to_file)
130+
131+
def dumps(self, element: Union[YAMLRoot, BaseModel], **kwargs):
132+
raise NotImplementedError("This method is not sensible for this dumper.")

src/linkml_arrays/dumpers/zarr_directory_store_dumper.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,17 @@ def _iterate_element(
3939
class ZarrDirectoryStoreDumper(Dumper):
4040
"""Dumper class for LinkML models to Zarr directory stores."""
4141

42-
# TODO is this the right method to overwrite? it does not dump a string
43-
def dumps(
42+
def dump(
4443
self,
4544
element: Union[YAMLRoot, BaseModel],
45+
to_file: str,
4646
schemaview: SchemaView,
47-
output_file_path: Union[str, Path],
4847
**kwargs,
4948
):
5049
"""Dump the element to a Zarr directory store."""
51-
store = zarr.DirectoryStore(output_file_path)
50+
store = zarr.DirectoryStore(to_file)
5251
root = zarr.group(store=store, overwrite=True)
5352
_iterate_element(element, schemaview, root)
53+
54+
def dumps(self, element: Union[YAMLRoot, BaseModel], **kwargs):
55+
raise NotImplementedError("This method is not sensible for this dumper.")

tests/test_dumpers/test_dumpers.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def test_xarray_zarr_dumper(tmp_path):
155155
container = _create_container()
156156
schemaview = SchemaView(GROUND_TRUTH_DIR / "temperature_schema.yaml")
157157
output_file_path = tmp_path / "my_container_xarray.zarr"
158-
XarrayZarrDumper().dumps(container, schemaview=schemaview, output_file_path=output_file_path)
158+
XarrayZarrDumper().dump(container, to_file=output_file_path, schemaview=schemaview)
159159

160160
assert os.path.exists(output_file_path)
161161
root = zarr.group(store=output_file_path)
@@ -188,7 +188,7 @@ def test_xarray_netcdf_dumper(tmp_path):
188188
container = _create_container()
189189
schemaview = SchemaView(GROUND_TRUTH_DIR / "temperature_schema.yaml")
190190
output_file_path = tmp_path / "my_container.nc"
191-
XarrayNetCDFDumper().dumps(container, schemaview=schemaview, output_file_path=output_file_path)
191+
XarrayNetCDFDumper().dump(container, to_file=output_file_path, schemaview=schemaview)
192192

193193
assert os.path.exists(output_file_path)
194194
datatree = open_datatree(output_file_path, engine='h5netcdf')
@@ -219,7 +219,7 @@ def test_hdf5_dumper(tmp_path):
219219

220220
schemaview = SchemaView(GROUND_TRUTH_DIR / "temperature_schema.yaml")
221221
output_file_path = tmp_path / "my_container.h5"
222-
Hdf5Dumper().dumps(container, schemaview=schemaview, output_file_path=output_file_path)
222+
Hdf5Dumper().dump(container, to_file=output_file_path, schemaview=schemaview)
223223

224224
assert os.path.exists(output_file_path)
225225
with h5py.File(output_file_path, "r") as f:
@@ -249,9 +249,7 @@ def test_zarr_directory_store_dumper(tmp_path):
249249

250250
schemaview = SchemaView(GROUND_TRUTH_DIR / "temperature_schema.yaml")
251251
output_file_path = tmp_path / "my_container.zarr"
252-
ZarrDirectoryStoreDumper().dumps(
253-
container, schemaview=schemaview, output_file_path=output_file_path
254-
)
252+
ZarrDirectoryStoreDumper().dump(container, to_file=output_file_path, schemaview=schemaview)
255253

256254
assert os.path.exists(output_file_path)
257255

0 commit comments

Comments
 (0)