epymorph.util
epymorph general utility functions and classes.
acceptable_name
module-attribute
acceptable_name = compile('^[a-zA-Z][a-zA-Z0-9_-]*$')
A pattern that matches generally acceptable names for use across epymorph.
DateValueType
module-attribute
DateValueType = void
The numpy dtype used for structured arrays of date and value. numpy doesn't let us type these very explicitly, so this alias is used to express intention, but know it comes with few guarantees.
So basically it implies: [("date", "datetime64[D]"), ("value", DataT)]
ANSIColor
Bases: Enum
ANSIStyle
Bases: Enum
KeyValue
CovariantMapping
A type for covariant mappings, which restricts usage to only those methods which are safe under covariance. For many use-cases these limitations are acceptable and wind up simplifying type expression.
NumpyTypeError
Bases: Exception
Describes an error checking the type or shape of a numpy array.
Matcher
MatchAny
MatchEqual
MatchEqual(acceptable: T_contra)
MatchAnyIn
MatchDType
MatchDType(*acceptable: DTypeLike)
MatchDTypeCast
MatchDTypeCast(*acceptable: DTypeLike)
MatchShapeLiteral
MatchDimensions
MatchDimensions(acceptable: int)
Event
A typed pub-sub event.
has_subscribers
property
has_subscribers: bool
True if at least one listener is subscribed to this event.
subscribe
Subscribe a handler to this event. Returns an unsubscribe function.
Subscriber
Utility class to track a list of subscriptions for ease of unsubscription.
Consider using this via the subscriptions()
context.
Singleton
Bases: type
A metaclass for classes you want to treat as singletons.
StringBuilder
StringBuilder(indent: str = '')
block
block(
indent: str = " ",
*,
opener: str | None = None,
closer: str | None = None,
) -> Generator[StringBuilder, None, None]
normalize_list
Normalize a list of strings for permissive search.
ansi_stylize
Uses ANSI escape codes to stylize a given text, which may not work everywhere. Any applied color or style are reset after the text.
Parameters:
-
color
(AnsiColor
, default:WHITE
) –The text color to apply.
-
style
(AnsiStyle
, default:None
) –The text style to apply.
constant
A function which returns a constant value, regardless of what arguments its called with.
call_all
Given a list of no-arg functions, call all of the functions and return None.
cache_transparent
Decorates a function with functools.cache but maintains its signature.
index_where
Find the first index of it
where predicate
evaluates to True.
Return -1 if no such value exists.
index_of
Find the first index of it
where item
evaluates as equal.
Return -1 if no such value exists.
list_not_none
Convert an iterable to a list, skipping any entries that are None.
are_unique
Returns True if all items in the iterable are unique.
are_instances
TypeGuards a collection to check that all items are
instances of the given type (of_type
).
filter_unique
Convert an iterable to a list, keeping only the unique values and maintaining the order as first-seen.
filter_with_mask
filter_with_mask(
xs: Iterable[A],
predicate: Callable[[A], TypeGuard[B]],
) -> tuple[list[B], list[bool]]
Filters the given iterable for items which match predicate
, and also
returns a boolean mask the same length as the iterable with the results of
predicate
for each item.
zip_list
Zip (strict) two iterables together as a list.
map_values
Maps the values of a Mapping into a dict by applying the given function.
normalize
Normalize the values in an array by subtracting the min and dividing by the range.
row_normalize
row_normalize(
arr: NDArray[N],
row_sums: NDArray[N] | None = None,
dtype: DTypeLike = None,
) -> NDArray[N]
Assuming arr
is a 2D array, normalize values across each row by dividing
by the row sum. If you've already calculated row sums, you can pass those in,
otherwise they will be computed.
prefix
A vectorized operation to return the prefix of each value in an NDArray of strings.
mask
Creates a boolean mask of a given length
where all elements identified
by selection
are True and all others are False. The selection can be
a slice or a list of indices.
pairwise_haversine
pairwise_haversine(
coordinates: NDArray
| tuple[NDArray[float64], NDArray[float64]],
*,
units: Literal["miles", "kilometers"] = "miles",
radius: float | None = None,
) -> NDArray[float64]
Compute the distances between all pairs of coordinates.
Parameters:
-
coordinates
(NDArray | tuple[NDArray, NDArray]
) –The coordinates, given in one of two forms: either a structured numpy array with dtype
[("longitude", np.float64), ("latitude", np.float64)]
or a tuple of two numpy arrays, the first containing longitudes and the second latitudes. The coordinates must be given in degrees. -
units
(Literal["miles", "kilometers"] = "miles",
, default:'miles'
) –The units of distance to use for the result, unless radius is given.
-
radius
(float
, default:None
) –The radius of the Earth to use in calculating the results. If not given, we will use an appropriate value for the given
units
. Since the value of radius implies the distance units being used, if you specifyradius
the value ofunits
is ignored.
Returns:
-
NDArray[np.float64] :
–An NxN array of distances where N is the number of coordinates given, representing the distance between each pair of coordinates. The output maintains the same ordering of coordinates as the input.
Raises:
-
ValueError :
–if coordinates are not given in an expected format
top
Find the top size
elements in arr
and return their indices.
Assumes the array is flat and the kind of thing that can be order-compared.
bottom
Find the bottom size
elements in arr
and return their indices.
Assumes the array is flat and the kind of thing that can be order-compared.
is_numeric
Is this numpy array a numeric (non-complex) type?
shape_matches
Does the shape of the given array match this expression? Shape expressions are a tuple where each dimension is either an integer or a '?' character to signify any length is allowed.
dtype_name
Tries to return the most-human-readable name for a numpy dtype.
is_date_value_dtype
is_date_value_dtype(
dtype: dtype | type[generic],
*,
value_dtype: dtype | type[generic] | None = None,
) -> TypeGuard[DateValueType]
Check if the given dtype is structured with 'date' and 'value' fields.
Parameters:
-
dtype
(dtype | type[generic]
) –The dtype to check.
-
value_dtype
(dtype | type[generic] | None
, default:None
) –The optional expected dtype of the 'value' field. If None (default), the dtype of the 'value' field is not checked.
Returns:
-
TypeGuard[DateValueType]
–True if the dtype is date/value, false otherwise.
See Also
epymorph.util.is_date_value_array for examples.
is_date_value_array
is_date_value_array(
array: NDArray,
*,
value_dtype: type[DataT] | None = None,
) -> TypeGuard[NDArray[DateValueType]]
Check if the given array is a structured array with 'date' and 'value' fields.
Parameters:
-
array
(NDArray
) –The array to check.
-
value_dtype
(type[DataT]
, default:None
) –The optional expected dtype of the 'value' field. If None (default), the dtype of the 'value' field is not checked.
Returns:
-
TypeGuard[NDArray[DateValueType]]
–True if the array is a date/value array, false otherwise.
Examples:
to_date_value_array
to_date_value_array(
dates: NDArray[datetime64], values: NDArray[DataT]
) -> NDArray[DateValueType]
Combine separate arrays of dates and values to create a structured date/value array with named fields. The given dates will be broadcast against the given values, so they must have the same first-dimension.
Parameters:
-
dates
(NDArray[datetime64]
) –An 1D array of dates.
-
values
(NDArray[DataT]
) –An array of values corresponding to the dates, the first dimension of this array must be equal to the number of dates.
Returns:
-
NDArray[DateValueType]
–A structured array with fields 'date' and 'value'.
Raises:
-
ValueError
–If the dimensionality of the arrays is not compatible.
Examples:
extract_date_value
extract_date_value(
date_values: NDArray[DateValueType],
value_dtype: type[DataT] | None = None,
) -> tuple[NDArray[datetime64], NDArray[DataT]]
Extract separate arrays of dates and values from a structured date/value array.
Parameters:
-
date_values
(NDArray[DateValueType]
) –A structured array with fields 'date' and 'value'.
-
value_dtype
(type[DataT]
, default:None
) –The expected dtype of the 'value' field. If None (default), the dtype of the 'value' field is not checked.
Returns:
-
tuple[NDArray[datetime64], NDArray[DataT]]
–A tuple containing two arrays: dates and values.
Raises:
-
ValueError
–If
value_dtype
is provided and the dtype of the 'value' field does not match.
Examples:
check_ndarray
check_ndarray(
value: Any,
*,
dtype: Matcher[DTypeLike] = MatchAny(),
shape: Matcher[NDArray] = MatchAny(),
) -> None
Checks that a value is a numpy array matching the given dtype and shape Matchers. Raises a NumpyTypeError if a check doesn't pass.
subscriptions
subscriptions() -> Generator[Subscriber, None, None]
Manage a subscription context, where all subscriptions added through the returned Subscriber will be automatically unsubscribed when the context closes.
string_builder
string_builder(
indent: str = "",
*,
opener: str | None = None,
closer: str | None = None,
) -> Generator[StringBuilder, None, None]