diff --git a/dascore/core/attrs.py b/dascore/core/attrs.py index c4e978a8..d1640a3c 100644 --- a/dascore/core/attrs.py +++ b/dascore/core/attrs.py @@ -10,7 +10,6 @@ from pydantic import ConfigDict, Field, PlainValidator, model_validator from typing_extensions import Self -import dascore as dc from dascore.constants import ( VALID_DATA_CATEGORIES, VALID_DATA_TYPES, @@ -23,12 +22,11 @@ to_str, ) from dascore.utils.models import ( - StrTupleStrSerialized, - IntTupleStrSerialized, DascoreBaseModel, + IntTupleStrSerialized, + StrTupleStrSerialized, UnitQuantity, frozen_dict_serializer, - frozen_dict_validator, ) str_validator = PlainValidator(to_str) @@ -46,7 +44,7 @@ def _to_coord_summary(coord_dict) -> FrozenDict[str, CoordSummary]: for i, v in coord_dict.items(): if hasattr(v, "to_summary"): v = v.to_summary() - elif isinstance(v,CoordSummary): + elif isinstance(v, CoordSummary): pass else: v = CoordSummary(**v) @@ -133,7 +131,6 @@ class PatchAttrs(DascoreBaseModel): frozen_dict_serializer, ] = Field(default_factory=dict) - @model_validator(mode="before") @classmethod def _get_dims(cls, data: Any) -> Any: @@ -144,8 +141,8 @@ def _get_dims(cls, data: Any) -> Any: coords = data.get("coords", {}) dims = getattr(coords, "dims", None) if dims is None and isinstance(coords, dict): - dims = coords.get('dims', ()) - data['dims'] = dims + dims = coords.get("dims", ()) + data["dims"] = dims return data def __getitem__(self, item): diff --git a/dascore/core/coordmanager.py b/dascore/core/coordmanager.py index a53c1b96..ec9d6e98 100644 --- a/dascore/core/coordmanager.py +++ b/dascore/core/coordmanager.py @@ -289,7 +289,9 @@ def _divide_kwargs(kwargs): update_coords = update def update_from_attrs( - self, attrs: Mapping | dc.PatchAttrs, data=None, + self, + attrs: Mapping | dc.PatchAttrs, + data=None, ) -> tuple[Self, dc.PatchAttrs]: """ Update coordinates from attrs. diff --git a/dascore/core/coords.py b/dascore/core/coords.py index 931bd26e..5dbe7d06 100644 --- a/dascore/core/coords.py +++ b/dascore/core/coords.py @@ -47,9 +47,9 @@ from dascore.utils.models import ( ArrayLike, DascoreBaseModel, - UnitQuantity, IntTupleStrSerialized, StrTupleStrSerialized, + UnitQuantity, ) from dascore.utils.time import dtype_time_like, is_datetime64, is_timedelta64, to_float @@ -104,7 +104,7 @@ class CoordSummary(DascoreBaseModel): step: step_type | None = None units: UnitQuantity | None = None dims: StrTupleStrSerialized = () - name: str = '' + name: str = "" @model_serializer(when_used="json") def ser_model(self) -> dict[str, str]: @@ -710,7 +710,7 @@ def get_attrs_dict(self, name): out[f"{name}_units"] = self.units return out - def to_summary(self, dims=(), name='') -> CoordSummary: + def to_summary(self, dims=(), name="") -> CoordSummary: """Get the summary info about the coord.""" return CoordSummary( min=self.min(), @@ -718,7 +718,7 @@ def to_summary(self, dims=(), name='') -> CoordSummary: step=self.step, dtype=self.dtype, units=self.units, - shape = self.shape, + shape=self.shape, dims=dims, name=name, ) @@ -979,7 +979,7 @@ def change_length(self, length: int) -> Self: assert self.ndim == 1, "change_length only works on 1D coords." return get_coord(shape=(length,)) - def to_summary(self, dims=(), name='') -> CoordSummary: + def to_summary(self, dims=(), name="") -> CoordSummary: """Get the summary info about the coord.""" return CoordSummary( min=np.nan, diff --git a/dascore/core/patch.py b/dascore/core/patch.py index 9d07ccfa..22535c04 100644 --- a/dascore/core/patch.py +++ b/dascore/core/patch.py @@ -60,6 +60,7 @@ class Patch: if there is a conflict between information contained in both, the coords will be recalculated. """ + data: ArrayLike coords: CoordManager dims: tuple[str, ...] diff --git a/dascore/examples.py b/dascore/examples.py index b55cf8f4..8ddb13db 100644 --- a/dascore/examples.py +++ b/dascore/examples.py @@ -84,7 +84,7 @@ def random_patch( time_coord = dc.get_coord( data=time_array, step=time_step if time_array.size <= 1 else None, - units='s', + units="s", ) else: time_coord = dascore.core.get_coord( diff --git a/dascore/io/core.py b/dascore/io/core.py index 0a92422e..f7e8b1ab 100644 --- a/dascore/io/core.py +++ b/dascore/io/core.py @@ -40,9 +40,9 @@ from dascore.utils.mapping import FrozenDict from dascore.utils.misc import _iter_filesystem, cached_method, iterate, warn_or_raise from dascore.utils.models import ( - StrTupleStrSerialized, DascoreBaseModel, DateTime64, + StrTupleStrSerialized, TimeDelta64, ) from dascore.utils.pd import _model_list_to_df diff --git a/dascore/io/dasdae/core.py b/dascore/io/dasdae/core.py index 1d15f7c7..40bfc2b1 100644 --- a/dascore/io/dasdae/core.py +++ b/dascore/io/dasdae/core.py @@ -134,5 +134,5 @@ def scan(self, resource: H5Reader, **kwargs): A path to the file. """ file_format = self.name - version = resource.attrs['__DASDAE_version__'] + version = resource.attrs["__DASDAE_version__"] return _get_contents_from_patch_groups(resource, version, file_format) diff --git a/dascore/io/dasdae/utils.py b/dascore/io/dasdae/utils.py index 75d64354..dd9967ad 100644 --- a/dascore/io/dasdae/utils.py +++ b/dascore/io/dasdae/utils.py @@ -2,9 +2,6 @@ from __future__ import annotations -import pickle -from contextlib import suppress - import numpy as np from tables import NodeError @@ -12,10 +9,9 @@ from dascore.core.attrs import PatchAttrs from dascore.core.coordmanager import get_coord_manager from dascore.core.coords import get_coord -from dascore.utils.misc import suppress_warnings -from dascore.utils.time import to_int -from dascore.utils.misc import unbyte from dascore.utils.hdf5 import Empty +from dascore.utils.misc import suppress_warnings, unbyte +from dascore.utils.time import to_int # --- Functions for writing DASDAE format @@ -33,7 +29,7 @@ def _santize_pytables(some_dict): continue # Get rid of empty enum. if isinstance(val, Empty): - val = '' + val = "" out[i] = val return out @@ -90,15 +86,12 @@ def _save_array(data, name, group, h5): return array_node - - def _save_coords(patch, patch_group, h5): """Save coordinates.""" cm = patch.coords for name, coord in cm.coord_map.items(): - summary = ( - coord.to_summary(name=name, dims=cm.dims[name]) - .model_dump(exclude_defaults=True) + summary = coord.to_summary(name=name, dims=cm.dims[name]).model_dump( + exclude_defaults=True ) breakpoint() # First save coordinate arrays @@ -108,9 +101,6 @@ def _save_coords(patch, patch_group, h5): dataset.attrs.update(summary) - - - def _save_patch(patch, wave_group, h5, name): """Save the patch to disk.""" patch_group = _create_or_get_group(h5, wave_group, name) @@ -229,10 +219,10 @@ def _get_coord_info(info, group): attrs = _santize_pytables(dict(ds.attrs)) # Need to get old dimensions from c_dims in attrs. if "dims" not in attrs: - attrs['dims'] = info.get(f"_cdims_{name}", name) + attrs["dims"] = info.get(f"_cdims_{name}", name) # The summary info is not stored in attrs; need to read coord array. c_info = {} - if 'min' not in attrs: + if "min" not in attrs: c_summary = ( dc.core.get_coord(data=ds[:]) .to_summary() @@ -240,11 +230,12 @@ def _get_coord_info(info, group): ) c_info.update(c_summary) - c_info.update({ - "dtype": ds.dtype.str, - 'shape': ds.shape, - "name": name, - } + c_info.update( + { + "dtype": ds.dtype.str, + "shape": ds.shape, + "name": name, + } ) coords[name] = c_info return coords @@ -261,10 +252,10 @@ def _get_patch_content_from_group(group): value = np.atleast_1d(value)[0] out[new_key] = value # Add coord info. - out['coords'] = _get_coord_info(out, group) + out["coords"] = _get_coord_info(out, group) # Add data info. - out['shape'] = group['data'].shape - out['dtype'] = group['data'].dtype.str + out["shape"] = group["data"].shape + out["dtype"] = group["data"].dtype.str # rename dims out["dims"] = out.pop("_dims") return out diff --git a/dascore/io/h5simple/core.py b/dascore/io/h5simple/core.py index 669b5c98..5b0c0098 100644 --- a/dascore/io/h5simple/core.py +++ b/dascore/io/h5simple/core.py @@ -41,9 +41,7 @@ def read(self, resource: H5Reader, snap=True, **kwargs) -> SpoolType: patch = dc.Patch(coords=new_cm, data=new_data[:], attrs=attrs) return dc.spool([patch]) - def scan( - self, resource: H5Reader, snap=True, **kwargs - ) -> list[dc.PatchAttrs]: + def scan(self, resource: H5Reader, snap=True, **kwargs) -> list[dc.PatchAttrs]: """Get the attributes of a h5simple file.""" attrs, cm, data = _get_attrs_coords_and_data(resource, snap, self) attrs["coords"] = cm.to_summary_dict() diff --git a/dascore/utils/duck.py b/dascore/utils/duck.py index a1084cbf..653d0bdd 100644 --- a/dascore/utils/duck.py +++ b/dascore/utils/duck.py @@ -1,5 +1,3 @@ """ Utilities for working with DuckDB. """ - - diff --git a/dascore/utils/hdf5.py b/dascore/utils/hdf5.py index eb56535d..1f92dc63 100644 --- a/dascore/utils/hdf5.py +++ b/dascore/utils/hdf5.py @@ -19,7 +19,6 @@ import pandas as pd import tables from h5py import File as H5pyFile -from h5py import Empty from packaging.version import parse as get_version from pandas.io.common import stringify_path from tables import ClosedNodeError @@ -44,7 +43,6 @@ ) from dascore.utils.time import get_max_min_times, to_datetime64, to_int, to_timedelta64 - HDF5ExtError = tables.HDF5ExtError NoSuchNodeError = tables.NoSuchNodeError NodeError = tables.NodeError diff --git a/dascore/utils/models.py b/dascore/utils/models.py index 779938cf..898238e0 100644 --- a/dascore/utils/models.py +++ b/dascore/utils/models.py @@ -27,9 +27,10 @@ def _str_to_int_tuple(value): """Convert a string of ints to a tuple.""" if isinstance(value, str): - return tuple(int(x) for x in value.split(',')) + return tuple(int(x) for x in value.split(",")) return value + # A datetime64 DateTime64 = Annotated[ np.datetime64, @@ -70,7 +71,7 @@ def _str_to_int_tuple(value): IntTupleStrSerialized = Annotated[ tuple[int, ...], PlainValidator(_str_to_int_tuple), - PlainSerializer(lambda x: ",".join((str(y) for y in x))), + PlainSerializer(lambda x: ",".join(str(y) for y in x)), ] FrozenDictType = Annotated[ diff --git a/dascore/utils/pd.py b/dascore/utils/pd.py index 9dd6bcbe..aa8cacae 100644 --- a/dascore/utils/pd.py +++ b/dascore/utils/pd.py @@ -2,13 +2,11 @@ from __future__ import annotations - import fnmatch import os from collections import defaultdict -from collections.abc import Collection, Mapping, Sequence +from collections.abc import Collection, Iterable, Mapping, Sequence from functools import cache -from typing import Iterable import numpy as np import pandas as pd @@ -559,7 +557,7 @@ def rolling_df(df, window, step=None, axis=0, center=False): def get_attrs_coords_patch_table( - patch_or_attrs: Iterable[dc.PatchAttrs | dc.Patch | dc.BaseSpool], + patch_or_attrs: Iterable[dc.PatchAttrs | dc.Patch | dc.BaseSpool], ) -> tuple(pd.DataFrame, pd.DataFrame, pd.DataFrame): """ Get seperated attributes, coordinates, and patch tables from attrs. @@ -569,11 +567,12 @@ def get_attrs_coords_patch_table( patch_or_attrs An iterable with patch content. """ + def get_coord_dict(attr, num): """Get the coordinate information from the attrs.""" out = [] for coord in attr.values(): - coord['id'] = num + coord["id"] = num out.append(coord) return out @@ -588,11 +587,3 @@ def get_coord_dict(attr, num): breakpoint() coord_info.extend(get_coord_dict(attr.pop("coords", {}), num)) breakpoint() - - - - - - - - diff --git a/tests/test_core/test_attrs.py b/tests/test_core/test_attrs.py index 2fe62f94..2545f9f8 100644 --- a/tests/test_core/test_attrs.py +++ b/tests/test_core/test_attrs.py @@ -37,7 +37,10 @@ def attrs_coords_2() -> PatchAttrs: """Add non-standard coords to attrs.""" coords = {"depth": {"min": 10.0, "max": 12.0, "dtype": "