Encoders¶
Feature encodings for coordinates and time — useful as inputs to learned models. Cyclical time encoding maps a periodic coordinate onto the unit circle, \(\bigl(\sin(2\pi t/T),\cos(2\pi t/T)\bigr)\); Fourier features lift coordinates into a higher-dimensional sinusoidal basis.
Operators¶
EncodeTimeCyclical
¶
Bases: Operator
Wrap :func:xrops.transforms.encoders.encode_time_cyclical.
Source code in packages/xrtoolz-ops/src/xrops/transforms/operators.py
EncodeTimeOrdinal
¶
EncodeTimeOrdinal(reference_date: str | datetime64 | None = None, time: str = 'time', unit: str = 'D')
Bases: Operator
Wrap :func:xrops.transforms.encoders.encode_time_ordinal.
Source code in packages/xrtoolz-ops/src/xrops/transforms/operators.py
CyclicalEncode
¶
Bases: Operator
Sin/cos embedding of a periodic variable.
Reads ds[variable], calls :func:cyclical_encode, and attaches
two new data variables {variable}_sin and {variable}_cos to
the dataset (preserving all other variables).
Source code in packages/xrtoolz-ops/src/xrops/transforms/operators.py
FourierFeatures
¶
FourierFeatures(variable: str, num_freqs: int, scale: float = 1.0, *, output_name: str | None = None, feature_dim: str = 'feature')
Bases: Operator
Deterministic Fourier-feature encoding of ds[variable].
Output is a new variable with one extra trailing feature_dim
axis of length 2 * num_freqs.
Source code in packages/xrtoolz-ops/src/xrops/transforms/operators.py
RandomFourierFeatures
¶
RandomFourierFeatures(variable: str, num_features: int, sigma: float = 1.0, seed: int | None = None, *, output_name: str | None = None, feature_dim: str = 'feature')
Bases: Operator
Random Fourier features (Rahimi & Recht, 2007) of ds[variable].
Source code in packages/xrtoolz-ops/src/xrops/transforms/operators.py
PositionalEncoding
¶
PositionalEncoding(variable: str, num_freqs: int, include_input: bool = True, *, output_name: str | None = None, feature_dim: str = 'feature')
Bases: Operator
NeRF-style positional encoding of ds[variable].
Source code in packages/xrtoolz-ops/src/xrops/transforms/operators.py
TimeRescale
¶
TimeRescale(freq_dt: float = 1.0, freq_unit: str = 's', t0: str | datetime64 | None = None, time: str = 'time')
Bases: Operator
Wrap :func:xrops.transforms.encoders.time_rescale.
Source code in packages/xrtoolz-ops/src/xrops/transforms/operators.py
TimeUnrescale
¶
Functional primitives (Layer 0)¶
These pure functions back the operators above; each takes xr.DataArray/xr.Dataset and a dim: argument.
encode_time_cyclical
¶
encode_time_cyclical(time: DataArray, *, components: Sequence[str] = ('dayofyear', 'hour')) -> Dataset
Sin/cos encodings of datetime components as a Dataset of new variables.
For each component in components (any name accepted by
xarray's .dt accessor), two variables are produced:
{component}_sin and {component}_cos. The Layer-1
EncodeTimeCyclical operator merges these into the input Dataset
as new coordinates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
time
|
DataArray
|
Time coordinate / variable. |
required |
components
|
Sequence[str]
|
Iterable of datetime attributes to encode. |
('dayofyear', 'hour')
|
Returns:
| Type | Description |
|---|---|
Dataset
|
Dataset whose data variables are the requested sin/cos pairs, |
Dataset
|
each carrying the same dims as |
Source code in packages/xrtoolz-ops/src/xrops/transforms/_src/encoders/coord_time.py
encode_time_ordinal
¶
encode_time_ordinal(time: DataArray, *, reference_date: str | datetime64 | None = None, unit: str = 'D') -> DataArray
Ordinal float encoding of a time coordinate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
time
|
DataArray
|
Time coordinate / variable. |
required |
reference_date
|
str | datetime64 | None
|
Reference time. Defaults to |
None
|
unit
|
str
|
Pandas timedelta unit used to scale the offset. |
'D'
|
Returns:
| Type | Description |
|---|---|
DataArray
|
DataArray of ordinal floats with the same dims as |
DataArray
|
The Layer-1 |
DataArray
|
|
Source code in packages/xrtoolz-ops/src/xrops/transforms/_src/encoders/coord_time.py
cyclical_encode
¶
Sin/cos embedding of a periodic variable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
da
|
DataArray
|
Input values with period |
required |
period
|
float
|
Period length (e.g. 365.25 for day-of-year, 24 for hour
of day, |
required |
Returns:
| Type | Description |
|---|---|
Dataset
|
Dataset with |
Dataset
|
shape as |
Source code in packages/xrtoolz-ops/src/xrops/transforms/_src/encoders/basis.py
fourier_features
¶
fourier_features(da: DataArray, *, num_freqs: int, scale: float = 1.0, feature_dim: str = 'feature') -> DataArray
Deterministic Fourier-feature encoding.
Adds a trailing feature_dim of length 2 * num_freqs whose
entries alternate sin(2^k * scale * x) and cos(2^k * scale * x).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
da
|
DataArray
|
Input DataArray. |
required |
num_freqs
|
int
|
Number of frequency octaves to use. |
required |
scale
|
float
|
Base frequency. |
1.0
|
feature_dim
|
str
|
Name of the appended feature dimension. |
'feature'
|
Source code in packages/xrtoolz-ops/src/xrops/transforms/_src/encoders/basis.py
random_fourier_features
¶
random_fourier_features(da: DataArray, *, num_features: int, sigma: float = 1.0, seed: int | None = None, input_dim: Hashable | None = None, feature_dim: str = 'feature') -> DataArray
Random Fourier features in the style of Rahimi & Recht, 2007.
Approximates the RBF kernel feature map. Adds a trailing
feature_dim of length num_features.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
da
|
DataArray
|
Input DataArray. |
required |
num_features
|
int
|
Output feature dimension (must be even). |
required |
sigma
|
float
|
Bandwidth of the underlying RBF kernel. |
1.0
|
seed
|
int | None
|
Seed for the RNG used to draw the random frequencies. |
None
|
input_dim
|
Hashable | None
|
Optional existing dimension of |
None
|
feature_dim
|
str
|
Name of the appended feature dimension. |
'feature'
|
Source code in packages/xrtoolz-ops/src/xrops/transforms/_src/encoders/basis.py
positional_encoding
¶
positional_encoding(da: DataArray, *, num_freqs: int, include_input: bool = True, feature_dim: str = 'feature') -> DataArray
NeRF-style positional encoding.
Adds a trailing feature_dim of length
(2 * num_freqs) + include_input.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
da
|
DataArray
|
Input DataArray. |
required |
num_freqs
|
int
|
Number of octave frequencies. |
required |
include_input
|
bool
|
If |
True
|
feature_dim
|
str
|
Name of the appended feature dimension. |
'feature'
|
Source code in packages/xrtoolz-ops/src/xrops/transforms/_src/encoders/basis.py
time_rescale
¶
time_rescale(time: DataArray, *, freq_dt: float = 1.0, freq_unit: str = 's', t0: str | datetime64 | None = None) -> DataArray
Rescale a datetime64 time axis to a float offset.
t' = (t - t_0) / freq_dt, where freq_dt is expressed in
freq_unit. Stores t0, freq, and units as attrs so
that :func:time_unrescale can round-trip back to datetime64.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
time
|
DataArray
|
Time coordinate / variable to rescale. |
required |
freq_dt
|
float
|
Size of the time step in |
1.0
|
freq_unit
|
str
|
Pandas timedelta unit ( |
's'
|
t0
|
str | datetime64 | None
|
Reference time. Defaults to |
None
|
Returns:
| Type | Description |
|---|---|
DataArray
|
DataArray of |
DataArray
|
|
Source code in packages/xrtoolz-ops/src/xrops/transforms/_src/encoders/coord_time.py
time_unrescale
¶
Inverse of :func:time_rescale using its stored attrs.
Source code in packages/xrtoolz-ops/src/xrops/transforms/_src/encoders/coord_time.py
Coordinate-range conversions¶
Convert longitude between \([-180, 180)\) and \([0, 360)\), and latitude between the \([-90, 90]\) and \([0, 180]\) conventions.
lon_180_to_360
¶
Wrap longitudes from [-180, 180) into [0, 360).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coord
|
Float[ndarray, '*shape']
|
Array of longitude values in the |
required |
Returns:
| Type | Description |
|---|---|
Float[ndarray, '*shape']
|
Array of longitude values in the |
Float[ndarray, '*shape']
|
same shape as the input. |
Source code in packages/xrtoolz-ops/src/xrops/transforms/_src/encoders/coord_space.py
lon_360_to_180
¶
Wrap longitudes from [0, 360) into [-180, 180).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coord
|
Float[ndarray, '*shape']
|
Array of longitude values in the |
required |
Returns:
| Type | Description |
|---|---|
Float[ndarray, '*shape']
|
Array of longitude values in the |
Float[ndarray, '*shape']
|
the same shape as the input. |
Source code in packages/xrtoolz-ops/src/xrops/transforms/_src/encoders/coord_space.py
lat_90_to_180
¶
Wrap latitudes from [-90, 90) into [0, 180).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coord
|
Float[ndarray, '*shape']
|
Array of latitude values in the |
required |
Returns:
| Type | Description |
|---|---|
Float[ndarray, '*shape']
|
Array of latitude values in the |
Source code in packages/xrtoolz-ops/src/xrops/transforms/_src/encoders/coord_space.py
lat_180_to_90
¶
Wrap latitudes from [0, 180) into [-90, 90).
Useful when a dataset stores latitude as a 0-based index rather than the standard geographic convention.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coord
|
Float[ndarray, '*shape']
|
Array of latitude values in the |
required |
Returns:
| Type | Description |
|---|---|
Float[ndarray, '*shape']
|
Array of latitude values in the |