Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements to validation checks #189

Merged
merged 4 commits into from
May 3, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 15 additions & 13 deletions python/metrics/src/hydrotools/metrics/_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import numpy as np
import numpy.typing as npt
from typing import List, Tuple
from typing import List, Tuple, Sequence
import pandas as pd
import warnings
from pandas.api.types import CategoricalDtype
Expand Down Expand Up @@ -52,6 +52,12 @@ def __str__(self) -> str:
message = f"This array is not 1-dimensional\n {self._arr}"
return message


def _array_attr(seq: Sequence, attr: str) -> npt.ArrayLike:
if not hasattr(seq, attr):
return np.asarray(seq)
return seq

def raise_for_non_vector(
*arrays: List[npt.ArrayLike]
) -> None:
Expand All @@ -70,7 +76,7 @@ def raise_for_non_vector(
"""
# Check each array
for x in arrays:
if len(np.array(x).shape) != 1:
if _array_attr(x, "ndim").ndim != 1:
raise NonVectorError(arr=x)

def raise_for_inconsistent_shapes(
Expand All @@ -89,20 +95,16 @@ def raise_for_inconsistent_shapes(
InconsistentShapesError:
Raises if all of the arrays are not the same shape.
"""
# Convert to numpy arrays
arrays = [np.array(x) for x in arrays]

# Extract first array
x = arrays[0]
xshape = _array_attr(arrays[0], "shape").shape

# Check shape of each
for y in arrays:
# Test each array
test = all(i == j for i, j in zip(x.shape, y.shape))
if not test:
for y in arrays[1:]:
yshape = _array_attr(y, "shape").shape
if xshape != yshape:
raise InconsistentShapesError(
array_shape_1=x.shape,
array_shape_2=y.shape
array_shape_1=xshape,
array_shape_2=yshape
)

def convert_to_boolean_categorical_series(
Expand Down Expand Up @@ -137,4 +139,4 @@ def convert_to_boolean_categorical_series(
warnings.warn(message=message, category=UserWarning)

return s