Skip to content

Commit db4fa63

Browse files
committed
Linting and cleanup
1 parent 43f300a commit db4fa63

File tree

6 files changed

+55
-123
lines changed

6 files changed

+55
-123
lines changed

src/mdio/core/v1/__init__.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@
44
"""
55

66
from ._overloads import mdio
7+
from ._serializer import make_coordinate
8+
from ._serializer import make_dataset
9+
from ._serializer import make_dataset_metadata
10+
from ._serializer import make_named_dimension
11+
from ._serializer import make_variable
712
from .builder import MDIODatasetBuilder
813
from .builder import write_mdio_metadata
9-
from ._serializer import (
10-
make_coordinate,
11-
make_dataset,
12-
make_dataset_metadata,
13-
make_named_dimension,
14-
make_variable,
15-
)
16-
from .factory import MDIOSchemaType
1714
from .factory import SCHEMA_TEMPLATE_MAP
15+
from .factory import MDIOSchemaType
16+
1817

1918
__all__ = [
2019
"MDIODatasetBuilder",

src/mdio/core/v1/_serializer.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44
to Zarr storage. This API is not considered stable and may change without notice.
55
"""
66

7-
from typing import Any, Dict, List, Optional
7+
from datetime import datetime
8+
from typing import Any
9+
from typing import Dict
10+
from typing import List
11+
from typing import Optional
812

913
import numpy as np
1014
from numcodecs import Blosc as NumcodecsBlosc
11-
from datetime import datetime
1215

1316
from mdio.core.v1._overloads import mdio
1417
from mdio.schema.compressors import ZFP

src/mdio/core/v1/builder.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,27 @@
1111

1212
from zarr.core.chunk_key_encodings import V2ChunkKeyEncoding # noqa: F401
1313

14+
from mdio.core.v1._overloads import mdio
1415
from mdio.schema.compressors import ZFP
1516
from mdio.schema.compressors import Blosc
1617
from mdio.schema.dimension import NamedDimension
1718
from mdio.schema.dtype import ScalarType
1819
from mdio.schema.dtype import StructuredType
1920
from mdio.schema.metadata import UserAttributes
2021
from mdio.schema.v1.dataset import Dataset
22+
from mdio.schema.v1.units import AllUnits
23+
from mdio.schema.v1.variable import Coordinate
24+
from mdio.schema.v1.variable import Variable
25+
from mdio.schema.v1.variable import VariableMetadata
2126

2227
# Import factory functions from serializer module
28+
from ._serializer import _construct_mdio_dataset
29+
from ._serializer import _convert_compressor
2330
from ._serializer import make_coordinate
2431
from ._serializer import make_dataset
2532
from ._serializer import make_dataset_metadata
2633
from ._serializer import make_named_dimension
2734
from ._serializer import make_variable
28-
from ._serializer import _convert_compressor
29-
from ._serializer import _construct_mdio_dataset
30-
from mdio.core.v1._overloads import mdio
31-
from mdio.schema.v1.units import AllUnits
32-
from mdio.schema.v1.variable import Coordinate
33-
from mdio.schema.v1.variable import Variable
34-
from mdio.schema.v1.variable import VariableMetadata
3535

3636

3737
class _BuilderState(Enum):
@@ -227,9 +227,7 @@ def build(self) -> Dataset:
227227
return make_dataset(all_variables, metadata)
228228

229229

230-
def write_mdio_metadata(
231-
mdio_ds: Dataset, store: str, **kwargs: Any
232-
) -> mdio.Dataset:
230+
def write_mdio_metadata(mdio_ds: Dataset, store: str, **kwargs: Any) -> mdio.Dataset:
233231
"""Write MDIO metadata to a Zarr store and return the constructed mdio.Dataset.
234232
235233
This function constructs an mdio.Dataset from the MDIO dataset and writes its metadata
@@ -238,7 +236,7 @@ def write_mdio_metadata(
238236
Args:
239237
mdio_ds: The MDIO dataset to serialize
240238
store: Path to the Zarr store
241-
**kwargs: Additional arguments to pass to to_zarr()
239+
kwargs: Additional arguments to pass to to_mdio()
242240
243241
Returns:
244242
The constructed xarray Dataset with MDIO extensions

src/mdio/core/v1/factory.py

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,18 @@
11
"""MDIO factories for seismic data."""
22

3+
# TODO(BrianMichell): Add implementations for other canonical datasets.
4+
35
from __future__ import annotations
46

5-
import importlib
6-
from datetime import UTC
7-
from datetime import datetime
87
from enum import Enum
98
from enum import auto
109
from typing import Any
11-
from typing import Dict
12-
from typing import List
13-
from typing import Optional
1410

1511
from mdio.core.v1.builder import MDIODatasetBuilder
1612
from mdio.schema.compressors import Blosc
1713
from mdio.schema.dtype import ScalarType
1814
from mdio.schema.dtype import StructuredType
1915
from mdio.schema.v1.dataset import Dataset
20-
from mdio.schema.v1.units import AllUnits
21-
from mdio.schema.v1.units import LengthUnitModel
2216

2317

2418
class MDIOSchemaType(Enum):
@@ -46,14 +40,14 @@ def __init__(self):
4640
def create(
4741
self,
4842
name: str,
49-
shape: List[int],
50-
header_fields: Dict[str, str],
43+
shape: list[int],
44+
header_fields: dict[str, str],
5145
create_coords: bool = False,
52-
sample_format: Optional[str] = None,
53-
chunks: Optional[List[int]] = None,
54-
sample_units: Optional[Dict[str, str]] = None,
55-
z_units: Optional[Dict[str, str]] = None,
56-
attributes: Optional[Dict[str, Any]] = None,
46+
sample_format: str | None = None,
47+
chunks: list[int] | None = None,
48+
sample_units: dict[str, str] | None = None,
49+
z_units: dict[str, str] | None = None,
50+
attributes: dict[str, Any] | None = None,
5751
) -> Dataset:
5852
"""Create a generic seismic dataset schema.
5953
@@ -80,7 +74,7 @@ def create(
8074
)
8175

8276
# Add dimensions
83-
for dim_name, dim_size in zip(self._dim_names, shape):
77+
for dim_name, dim_size in zip(self._dim_names, shape, strict=True):
8478
builder.add_dimension(
8579
name=dim_name,
8680
size=dim_size,
@@ -108,10 +102,12 @@ def create(
108102
)
109103

110104
# Add header variable with structured dtype
111-
header_dtype = StructuredType(fields=[
112-
{"name": field_name, "format": field_type}
113-
for field_name, field_type in header_fields.items()
114-
])
105+
header_dtype = StructuredType(
106+
fields=[
107+
{"name": field_name, "format": field_type}
108+
for field_name, field_type in header_fields.items()
109+
]
110+
)
115111
builder.add_variable(
116112
name="headers",
117113
data_type=header_dtype,
@@ -145,14 +141,14 @@ def __init__(self, domain: str):
145141
def create(
146142
self,
147143
name: str,
148-
shape: List[int],
149-
header_fields: Dict[str, str],
144+
shape: list[int],
145+
header_fields: dict[str, str],
150146
create_coords: bool = False,
151-
sample_format: Optional[str] = None,
152-
chunks: Optional[List[int]] = None,
153-
sample_units: Optional[Dict[str, str]] = None,
154-
z_units: Optional[Dict[str, str]] = None,
155-
attributes: Optional[Dict[str, Any]] = None,
147+
sample_format: str | None = None,
148+
chunks: list[int] | None = None,
149+
sample_units: dict[str, str] | None = None,
150+
z_units: dict[str, str] | None = None,
151+
attributes: dict[str, Any] | None = None,
156152
) -> Dataset:
157153
"""Create a seismic dataset schema with domain-specific attributes."""
158154
# Add seismic-specific attributes
@@ -197,14 +193,14 @@ def __init__(self, domain: str):
197193
def create(
198194
self,
199195
name: str,
200-
shape: List[int],
201-
header_fields: Dict[str, str],
196+
shape: list[int],
197+
header_fields: dict[str, str],
202198
create_coords: bool = False,
203-
sample_format: Optional[str] = None,
204-
chunks: Optional[List[int]] = None,
205-
sample_units: Optional[Dict[str, str]] = None,
206-
z_units: Optional[Dict[str, str]] = None,
207-
attributes: Optional[Dict[str, Any]] = None,
199+
sample_format: str | None = None,
200+
chunks: list[int] | None = None,
201+
sample_units: dict[str, str] | None = None,
202+
z_units: dict[str, str] | None = None,
203+
attributes: dict[str, Any] | None = None,
208204
) -> Dataset:
209205
"""Create a seismic dataset schema with pre-stack attributes."""
210206
# Add seismic-specific attributes

tests/integration/test_v1_serialization.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
import numpy as np
66

7-
from mdio.core.v1.builder import write_mdio_metadata
87
from mdio.core.v1._serializer import make_dataset
98
from mdio.core.v1._serializer import make_dataset_metadata
109
from mdio.core.v1._serializer import make_named_dimension
1110
from mdio.core.v1._serializer import make_variable
11+
from mdio.core.v1.builder import write_mdio_metadata
1212
from mdio.schema.compressors import ZFP
1313
from mdio.schema.compressors import Blosc
1414
from mdio.schema.dtype import ScalarType

tests/unit/test_template_factory.py

Lines changed: 3 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,24 @@
11
"""Unit tests for MDIO v1 factory."""
22

3-
# TODO(BrianMichell): Update this to use canonical factory functions.
3+
# TODO(BrianMichell): Update this to use canonical factory functions.
44

55
from datetime import datetime
66
from datetime import timezone
77

8-
import numpy as np
98
import pytest
10-
import xarray as xr
119
from pydantic import ValidationError
12-
from zarr import Array
1310

1411
from mdio.core.v1._serializer import make_coordinate
1512
from mdio.core.v1._serializer import make_dataset
1613
from mdio.core.v1._serializer import make_dataset_metadata
1714
from mdio.core.v1._serializer import make_named_dimension
1815
from mdio.core.v1._serializer import make_variable
19-
from mdio.core.v1.factory import MDIOSchemaType
16+
from mdio.core.v1.builder import write_mdio_metadata
2017
from mdio.core.v1.factory import SCHEMA_TEMPLATE_MAP
21-
from mdio.schema.compressors import ZFP
18+
from mdio.core.v1.factory import MDIOSchemaType
2219
from mdio.schema.compressors import Blosc
23-
from mdio.schema.dimension import NamedDimension
2420
from mdio.schema.dtype import ScalarType
2521
from mdio.schema.dtype import StructuredType
26-
from mdio.schema.v1.dataset import Dataset
27-
from mdio.core.v1.builder import write_mdio_metadata
28-
29-
# def test_make_named_dimension():
30-
# """Test that make_named_dimension returns a NamedDimension object."""
31-
# dim = make_named_dimension("time", 42)
32-
# assert isinstance(dim, NamedDimension)
33-
# assert dim.name == "time"
34-
# assert dim.size == 42
35-
36-
37-
# def test_make_coordinate_minimal():
38-
# """Test that make_coordinate returns a Coordinate object."""
39-
# dims = ["x"]
40-
# coord = make_coordinate(name="x", dimensions=dims, data_type=ScalarType.FLOAT32)
41-
# assert coord.name == "x"
42-
# assert coord.dimensions == dims
43-
# assert coord.data_type == ScalarType.FLOAT32
44-
# assert coord.metadata is None
45-
46-
47-
# def test_make_variable_minimal():
48-
# """Test that make_variable returns a Variable object."""
49-
# var = make_variable(
50-
# name="var",
51-
# dimensions=["x"],
52-
# data_type=ScalarType.FLOAT32,
53-
# compressor=None,
54-
# )
55-
# assert var.name == "var"
56-
# assert var.dimensions == ["x"]
57-
# assert var.data_type == ScalarType.FLOAT32
58-
# assert var.compressor is None
59-
# assert var.coordinates is None
60-
# assert var.metadata is None
61-
62-
63-
# def test_make_dataset_metadata_minimal():
64-
# """Test that make_dataset_metadata returns a DatasetMetadata object."""
65-
# ts = datetime.now(timezone.utc)
66-
# meta = make_dataset_metadata(name="ds", api_version="1", created_on=ts)
67-
# assert meta.name == "ds"
68-
# assert meta.api_version == "1"
69-
# assert meta.created_on == ts
70-
# assert meta.attributes is None
71-
72-
73-
# def test_make_dataset_minimal():
74-
# """Test that make_dataset returns a Dataset object."""
75-
# var = make_variable(
76-
# name="var",
77-
# dimensions=["x"],
78-
# data_type=ScalarType.FLOAT32,
79-
# compressor=None,
80-
# )
81-
# ts = datetime.now(timezone.utc)
82-
# meta = make_dataset_metadata(name="ds", api_version="1", created_on=ts)
83-
# ds = make_dataset([var], meta)
84-
# assert ds.variables == [var]
85-
# assert ds.metadata == meta
8622

8723

8824
def test_make_toy_dataset():

0 commit comments

Comments
 (0)