Skip to content

Commit ee2a063

Browse files
cmungallrly
andauthored
Reformatting code for flake8 config (#1)
Co-authored-by: Ryan Ly <[email protected]>
1 parent 37f5e93 commit ee2a063

24 files changed

+1048
-636
lines changed

docs/conf.py

+7-8
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@
44
# https://www.sphinx-doc.org/en/master/usage/configuration.html
55

66
import os
7-
import re
8-
import sys
97
from datetime import date
108
from linkml_arrays import __version__
9+
1110
# -- Project information -----------------------------------------------------
1211
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information
1312

14-
project = 'linkml-arrays'
13+
project = "linkml-arrays"
1514
copyright = f"{date.today().year}, Ryan Ly <[email protected]>"
16-
author = 'Ryan Ly <[email protected]>'
15+
author = "Ryan Ly <[email protected]>"
1716
release = __version__
1817

1918
# -- General configuration ---------------------------------------------------
@@ -25,7 +24,7 @@
2524
"sphinx_rtd_theme",
2625
"sphinx_click",
2726
"sphinx_autodoc_typehints",
28-
"myst_parser"
27+
"myst_parser",
2928
]
3029

3130
# generate autosummary pages
@@ -46,13 +45,13 @@
4645
# This pattern also affects html_static_path and html_extra_path.
4746
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]
4847

49-
templates_path = ['_templates']
48+
templates_path = ["_templates"]
5049

5150
# -- Options for HTML output -------------------------------------------------
5251
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output
5352

54-
html_theme = 'sphinx_rtd_theme'
55-
html_static_path = ['_static']
53+
html_theme = "sphinx_rtd_theme"
54+
html_static_path = ["_static"]
5655

5756
# The name of an image file (relative to this directory) to place at the top
5857
# of the sidebar.

poetry.lock

+718-427
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ readme = "README.md"
99
[tool.poetry.dependencies]
1010
python = "^3.9"
1111
#setuptools = "^65.5.0"
12-
#tox = "^3.25.1"
12+
tox = "^3.25.1"
1313
#click = "^8.1.3"
1414
#importlib-metadata = "^4.8.0"
1515
linkml-runtime = "^1.6.0"

src/linkml_arrays/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""linkml-arrays package."""
2+
23
import importlib_metadata
34

45
try:

src/linkml_arrays/cli.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
"""Command line interface for linkml-arrays."""
2-
import click
2+
33
import logging
44

5+
import click
6+
57
from linkml_arrays import __version__
68
from linkml_arrays.main import demo
79

@@ -11,6 +13,7 @@
1113

1214
logger = logging.getLogger(__name__)
1315

16+
1417
@click.group()
1518
@click.option("-v", "--verbose", count=True)
1619
@click.option("-q", "--quiet")
@@ -30,11 +33,11 @@ def main(verbose: int, quiet: bool):
3033
if quiet:
3134
logger.setLevel(level=logging.ERROR)
3235

36+
3337
@main.command()
3438
def run():
3539
"""Run the linkml-arrays's demo command."""
3640
demo()
37-
3841

3942

4043
if __name__ == "__main__":

src/linkml_arrays/dumpers/__init__.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
"""Dumper classes for linkml-arrays."""
2-
from .yaml_numpy_dumper import YamlNumpyDumper
3-
from .yaml_hdf5_dumper import YamlHdf5Dumper
2+
43
from .hdf5_dumper import Hdf5Dumper
5-
from .zarr_directory_store_dumper import ZarrDirectoryStoreDumper
4+
from .yaml_hdf5_dumper import YamlHdf5Dumper
5+
from .yaml_numpy_dumper import YamlNumpyDumper
6+
from .zarr_directory_store_dumper import ZarrDirectoryStoreDumper
7+
8+
__all__ = [
9+
"Hdf5Dumper",
10+
"YamlHdf5Dumper",
11+
"YamlNumpyDumper",
12+
"ZarrDirectoryStoreDumper",
13+
]
+21-14
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
1+
"""Class for dumping a LinkML model to an HDF5 file."""
2+
13
from typing import Union
24

35
import h5py
4-
from pydantic import BaseModel
5-
6+
from linkml_runtime import SchemaView
67
from linkml_runtime.dumpers.dumper_root import Dumper
78
from linkml_runtime.utils.yamlutils import YAMLRoot
8-
from linkml_runtime import SchemaView
9+
from pydantic import BaseModel
910

1011

11-
def iterate_element(
12-
element: Union[YAMLRoot, BaseModel],
13-
schemaview: SchemaView,
14-
group: h5py.Group = None
12+
def _iterate_element(
13+
element: Union[YAMLRoot, BaseModel], schemaview: SchemaView, group: h5py.Group = None
1514
):
15+
"""Recursively iterate through the elements of a LinkML model and save them.
16+
17+
Writes Pydantic BaseModel objects as groups, slots that implement "linkml:elements"
18+
as datasets, and other slots as attributes.
19+
"""
1620
# get the type of the element
1721
element_type = type(element).__name__
1822

@@ -25,23 +29,26 @@ def iterate_element(
2529
if isinstance(v, BaseModel):
2630
# create a subgroup and recurse
2731
subgroup = group.create_group(k)
28-
iterate_element(v, schemaview, subgroup)
32+
_iterate_element(v, schemaview, subgroup)
2933
else:
3034
# create an attribute on the group
3135
group.attrs[k] = v
3236

3337

3438
class Hdf5Dumper(Dumper):
39+
"""Dumper class for LinkML models to HDF5 files."""
40+
41+
# TODO is this the right method to overwrite? it does not dump a string
42+
def dumps(self, element: Union[YAMLRoot, BaseModel], schemaview: SchemaView, **kwargs):
43+
"""Dump the element to an HDF5 file.
3544
36-
def dumps(self, element: Union[YAMLRoot, BaseModel], schemaview: SchemaView, **kwargs) -> str:
37-
""" Return element formatted as a YAML string with paths to HDF5 files containing the arrays as datasets"""
45+
Raises:
46+
ValueError: If the class requires an identifier and it is not provided.
47+
"""
3848
id_slot = schemaview.get_identifier_slot(element.__class__.__name__)
3949
if id_slot is None:
4050
raise ValueError("The class requires an identifier.")
4151
id_value = getattr(element, id_slot.name)
4252
output_file_path = f"{id_value}.h5"
4353
with h5py.File(output_file_path, "w") as f:
44-
iterate_element(element, schemaview, f)
45-
46-
47-
54+
_iterate_element(element, schemaview, f)

src/linkml_arrays/dumpers/yaml_hdf5_dumper.py

+24-10
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,28 @@
1+
"""Class for dumping a LinkML model to a YAML file with paths to HDF5 files."""
2+
13
from typing import Union
24

35
import h5py
4-
from pydantic import BaseModel
56
import yaml
6-
7+
from linkml_runtime import SchemaView
78
from linkml_runtime.dumpers.dumper_root import Dumper
89
from linkml_runtime.utils.yamlutils import YAMLRoot
9-
from linkml_runtime import SchemaView
10+
from pydantic import BaseModel
11+
12+
13+
def _iterate_element(
14+
element: Union[YAMLRoot, BaseModel], schemaview: SchemaView, parent_identifier=None
15+
):
16+
"""Recursively iterate through the elements of a LinkML model and save them.
1017
18+
Returns a dictionary with the same structure as the input element, but with the slots
19+
that implement "linkml:elements" (arrays) are written to HDF5 files and the paths to these
20+
files are returned in the dictionary. Each array is written to an HDF5 dataset at path
21+
"/data" in a new HDF5 file.
1122
12-
def iterate_element(element: Union[YAMLRoot, BaseModel], schemaview: SchemaView, parent_identifier = None):
23+
Raises:
24+
ValueError: If the class requires an identifier and it is not provided.
25+
"""
1326
# get the type of the element
1427
element_type = type(element).__name__
1528

@@ -33,24 +46,25 @@ def iterate_element(element: Union[YAMLRoot, BaseModel], schemaview: SchemaView,
3346
output_file_path = f"{parent_identifier}.{found_class.name}.{found_slot.name}.h5"
3447
else:
3548
output_file_path = f"{found_class.name}.{found_slot.name}.h5"
36-
with h5py.File(output_file_path, "w") as f: # TODO do not assume that there is only one by this name
49+
with h5py.File(
50+
output_file_path, "w"
51+
) as f: # TODO do not assume that there is only one by this name
3752
f.create_dataset("data", data=v)
3853
ret_dict[k] = f"file:./{output_file_path}" # TODO make this nicer
3954
else:
4055
if isinstance(v, BaseModel):
41-
v2 = iterate_element(v, schemaview, id_value)
56+
v2 = _iterate_element(v, schemaview, id_value)
4257
ret_dict[k] = v2
4358
else:
4459
ret_dict[k] = v
4560
return ret_dict
4661

4762

4863
class YamlHdf5Dumper(Dumper):
64+
"""Class for dumping a LinkML model to a YAML file with paths to HDF5 files."""
4965

5066
def dumps(self, element: Union[YAMLRoot, BaseModel], schemaview: SchemaView, **kwargs) -> str:
51-
""" Return element formatted as a YAML string with paths to HDF5 files containing the arrays as datasets"""
52-
input = iterate_element(element, schemaview)
67+
"""Return element formatted as a YAML string."""
68+
input = _iterate_element(element, schemaview)
5369

5470
return yaml.dump(input)
55-
56-

src/linkml_arrays/dumpers/yaml_numpy_dumper.py

+21-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,28 @@
1+
"""Class for dumpling a LinkML model to a YAML file with paths to NumPy files."""
2+
13
from typing import Union
24

35
import numpy as np
4-
from pydantic import BaseModel
56
import yaml
6-
7+
from linkml_runtime import SchemaView
78
from linkml_runtime.dumpers.dumper_root import Dumper
89
from linkml_runtime.utils.yamlutils import YAMLRoot
9-
from linkml_runtime import SchemaView
10+
from pydantic import BaseModel
11+
12+
13+
def _iterate_element(
14+
element: Union[YAMLRoot, BaseModel], schemaview: SchemaView, parent_identifier=None
15+
):
16+
"""Recursively iterate through the elements of a LinkML model and save them.
1017
18+
Returns a dictionary with the same structure as the input element, but with the slots
19+
that implement "linkml:elements" (arrays) are written to HDF5 files and the paths to these
20+
files are returned in the dictionary. Each array is written to an HDF5 dataset at path
21+
"/data" in a new HDF5 file.
1122
12-
def iterate_element(element: Union[YAMLRoot, BaseModel], schemaview: SchemaView, parent_identifier = None):
23+
Raises:
24+
ValueError: If the class requires an identifier and it is not provided.
25+
"""
1326
# get the type of the element
1427
element_type = type(element).__name__
1528

@@ -37,19 +50,18 @@ def iterate_element(element: Union[YAMLRoot, BaseModel], schemaview: SchemaView,
3750
ret_dict[k] = f"file:./{output_file_path}" # TODO make this nicer
3851
else:
3952
if isinstance(v, BaseModel):
40-
v2 = iterate_element(v, schemaview, id_value)
53+
v2 = _iterate_element(v, schemaview, id_value)
4154
ret_dict[k] = v2
4255
else:
4356
ret_dict[k] = v
4457
return ret_dict
4558

4659

4760
class YamlNumpyDumper(Dumper):
61+
"""Dumper class for LinkML models to YAML files with paths to NumPy files."""
4862

4963
def dumps(self, element: Union[YAMLRoot, BaseModel], schemaview: SchemaView, **kwargs) -> str:
50-
""" Return element formatted as a YAML string with paths to numpy files containing the ndarrays"""
51-
input = iterate_element(element, schemaview)
64+
"""Return element formatted as a YAML string."""
65+
input = _iterate_element(element, schemaview)
5266

5367
return yaml.dump(input)
54-
55-
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
1+
"""Class for dumping a LinkML model to a Zarr directory store."""
2+
13
from typing import Union
24

35
import zarr
4-
from pydantic import BaseModel
5-
6+
from linkml_runtime import SchemaView
67
from linkml_runtime.dumpers.dumper_root import Dumper
78
from linkml_runtime.utils.yamlutils import YAMLRoot
8-
from linkml_runtime import SchemaView
9+
from pydantic import BaseModel
910

1011

11-
def iterate_element(
12-
element: Union[YAMLRoot, BaseModel],
13-
schemaview: SchemaView,
14-
group: zarr.hierarchy.Group = None
12+
def _iterate_element(
13+
element: Union[YAMLRoot, BaseModel], schemaview: SchemaView, group: zarr.hierarchy.Group = None
1514
):
15+
"""Recursively iterate through the elements of a LinkML model and save them.
16+
17+
Writes Pydantic BaseModel objects as groups, slots that implement "linkml:elements"
18+
as datasets, and other slots as attributes.
19+
"""
1620
# get the type of the element
1721
element_type = type(element).__name__
1822

@@ -25,24 +29,27 @@ def iterate_element(
2529
if isinstance(v, BaseModel):
2630
# create a subgroup and recurse
2731
subgroup = group.create_group(k)
28-
iterate_element(v, schemaview, subgroup)
32+
_iterate_element(v, schemaview, subgroup)
2933
else:
3034
# create an attribute on the group
3135
group.attrs[k] = v
3236

3337

3438
class ZarrDirectoryStoreDumper(Dumper):
39+
"""Dumper class for LinkML models to Zarr directory stores."""
40+
41+
# TODO is this the right method to overwrite? it does not dump a string
42+
def dumps(self, element: Union[YAMLRoot, BaseModel], schemaview: SchemaView, **kwargs):
43+
"""Dump the element to a Zarr directory store.
3544
36-
def dumps(self, element: Union[YAMLRoot, BaseModel], schemaview: SchemaView, **kwargs) -> str:
37-
""" Return element formatted as a YAML string with paths to HDF5 files containing the arrays as datasets"""
45+
Raises:
46+
ValueError: If the class requires an identifier and it is not provided.
47+
"""
3848
id_slot = schemaview.get_identifier_slot(element.__class__.__name__)
3949
if id_slot is None:
4050
raise ValueError("The class requires an identifier.")
4151
id_value = getattr(element, id_slot.name)
4252
output_file_path = f"{id_value}.zarr"
4353
store = zarr.DirectoryStore(output_file_path)
4454
root = zarr.group(store=store, overwrite=True)
45-
iterate_element(element, schemaview, root)
46-
47-
48-
55+
_iterate_element(element, schemaview, root)

src/linkml_arrays/loaders/__init__.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
"""Dumper classes for linkml-arrays."""
2-
from .yaml_numpy_loader import YamlNumpyLoader
3-
from .yaml_hdf5_loader import YamlHdf5Loader
2+
43
from .hdf5_loader import Hdf5Loader
5-
from .zarr_directory_store_loader import ZarrDirectoryStoreLoader
4+
from .yaml_hdf5_loader import YamlHdf5Loader
5+
from .yaml_numpy_loader import YamlNumpyLoader
6+
from .zarr_directory_store_loader import ZarrDirectoryStoreLoader
7+
8+
__all__ = [
9+
"Hdf5Loader",
10+
"YamlHdf5Loader",
11+
"YamlNumpyLoader",
12+
"ZarrDirectoryStoreLoader",
13+
]

0 commit comments

Comments
 (0)