Skip to content

Commit

Permalink
Additional docstrings fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
Kalle Westerling committed Oct 16, 2023
1 parent 22aa8d2 commit 7390847
Show file tree
Hide file tree
Showing 11 changed files with 1,116 additions and 1,051 deletions.
85 changes: 39 additions & 46 deletions deepsensor/data/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,18 +317,17 @@ def _cast_to_dtype(
"""
Cast context and target data to the default dtype.
Parameters
----------
var : ...
...
TODO unit test this by passing in a variety of data types and checking that they are
cast correctly.
Returns
-------
context : tuple. Tuple of context data with specified dtype.
target : tuple. Tuple of target data with specified dtype.
..
TODO unit test this by passing in a variety of data types and
checking that they are cast correctly.
Args:
var : ...
...
Returns:
tuple: Tuple of context data with specified dtype.
tuple: Tuple of target data with specified dtype.
"""

def cast_to_dtype(var):
Expand Down Expand Up @@ -526,24 +525,21 @@ def _check_links(
"""
Check that the context-target links are valid.
Parameters
----------
links : Tuple[int, int] | List[Tuple[int, int]]
Specifies links between context and target data. Each link is a
tuple of two integers, where the first integer is the index of the
context data and the second integer is the index of the target
data. Can be a single tuple in the case of a single link. If None,
no links are specified. Default: None.
Returns
-------
links : Tuple[int, int] | List[Tuple[int, int]]
The input links, if valid.
Raises
------
ValueError
If the links are not valid.
Args:
links (Tuple[int, int] | List[Tuple[int, int]]):
Specifies links between context and target data. Each link is a
tuple of two integers, where the first integer is the index of
the context data and the second integer is the index of the
target data. Can be a single tuple in the case of a single
link. If None, no links are specified. Default: None.
Returns:
Tuple[int, int] | List[Tuple[int, int]]
The input links, if valid.
Raises:
ValueError
If the links are not valid.
"""
if links is None:
return None
Expand Down Expand Up @@ -827,22 +823,19 @@ def time_slice_variable(self, var, date, delta_t=0):
"""
Slice a variable by a given time delta.
Parameters
----------
var : ...
Variable to slice.
delta_t : ...
Time delta to slice by.
Returns
-------
var : ...
Sliced variable.
Raises
------
ValueError
If the variable is of an unknown type.
Args:
var (...):
Variable to slice.
delta_t (...):
Time delta to slice by.
Returns:
var (...)
Sliced variable.
Raises:
ValueError
If the variable is of an unknown type.
"""
# TODO: Does this work with instantaneous time?
delta_t = pd.Timedelta(delta_t, unit=self.time_freq)
Expand Down
125 changes: 64 additions & 61 deletions deepsensor/data/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -647,19 +647,17 @@ def unnormalise(
"""
Unnormalise data.
Parameters
----------
data : :class:`xarray.DataArray` | :class:`xarray.Dataset` | :class:`pandas.DataFrame` | List[:class:`xarray.DataArray` | :class:`xarray.Dataset` | :class:`pandas.DataFrame`]
Data to unnormalise.
add_offset : bool, optional
Whether to add the offset to the data when unnormalising. Set to
False to unnormalise uncertainty values (e.g. std dev). Defaults to
True.
Returns
-------
:class:`xarray.DataArray` | :class:`xarray.Dataset` | :class:`pandas.DataFrame` | List[:class:`xarray.DataArray` | :class:`xarray.Dataset` | :class:`pandas.DataFrame`]
Unnormalised data.
Args:
data (:class:`xarray.DataArray` | :class:`xarray.Dataset` | :class:`pandas.DataFrame` | List[:class:`xarray.DataArray` | :class:`xarray.Dataset` | :class:`pandas.DataFrame`]):
Data to unnormalise.
add_offset (bool, optional):
Whether to add the offset to the data when unnormalising. Set
to False to unnormalise uncertainty values (e.g. std dev).
Defaults to True.
Returns:
:class:`xarray.DataArray` | :class:`xarray.Dataset` | :class:`pandas.DataFrame` | List[:class:`xarray.DataArray` | :class:`xarray.Dataset` | :class:`pandas.DataFrame`]:
Unnormalised data.
"""
if isinstance(data, list):
return [
Expand All @@ -675,25 +673,35 @@ def xarray_to_coord_array_normalised(
"""
Convert xarray to normalised coordinate array.
Parameters
----------
da : :class:`xarray.Dataset` | :class:`xarray.DataArray`
...
Args:
da (:class:`xarray.Dataset` | :class:`xarray.DataArray`)
...
Returns
-------
:class:`numpy:numpy.ndarray`
A normalised coordinate array of shape ``(2, N)``.
Returns:
:class:`numpy:numpy.ndarray`
A normalised coordinate array of shape ``(2, N)``.
"""
x1, x2 = da["x1"].values, da["x2"].values
X1, X2 = np.meshgrid(x1, x2, indexing="ij")
return np.stack([X1.ravel(), X2.ravel()], axis=0)


def process_X_mask_for_X(X_mask: xr.DataArray, X: xr.DataArray):
def process_X_mask_for_X(
X_mask: xr.DataArray, X: xr.DataArray
) -> xr.DataArray:
"""Process X_mask by interpolating to X and converting to boolean.
Both X_mask and X are xarray DataArrays with the same spatial coords.
Args:
X_mask (:class:`xarray.DataArray`):
...
X (:class:`xarray.DataArray`):
...
Returns:
:class:`xarray.DataArray`
...
"""
X_mask = X_mask.astype(float).interp_like(
X, method="nearest", kwargs={"fill_value": 0}
Expand All @@ -705,24 +713,23 @@ def process_X_mask_for_X(X_mask: xr.DataArray, X: xr.DataArray):

def mask_coord_array_normalised(
coord_arr: np.ndarray, mask_da: Union[xr.DataArray, xr.Dataset, None]
):
) -> np.ndarray:
"""
Remove points from (2, N) numpy array that are outside gridded xarray boolean mask.
If `coord_arr` is shape `(2, N)`, then `mask_da` is a shape `(N,)` boolean array
(True if point is inside mask, False if outside).
Parameters
----------
coord_arr : ...
...
mask_da : ...
...
Returns
-------
...
...
Remove points from (2, N) numpy array that are outside gridded xarray
boolean mask.
If `coord_arr` is shape `(2, N)`, then `mask_da` is a shape `(N,)` boolean
array (True if point is inside mask, False if outside).
Args:
coord_arr (:class:`numpy:numpy.ndarray`):
...
mask_da (:class:`xarray.Dataset` | :class:`xarray.DataArray`):
...
Returns:
:class:`numpy:numpy.ndarray`
...
"""
if mask_da is None:
return coord_arr
Expand All @@ -739,17 +746,15 @@ def da1_da2_same_grid(da1: xr.DataArray, da2: xr.DataArray) -> bool:
.. note::
``da1`` and ``da2`` are assumed normalised by ``DataProcessor``.
Parameters
----------
da1 : :class:`xarray.DataArray`
...
da2 : :class:`xarray.DataArray`
...
Returns
-------
bool
Whether ``da1`` and ``da2`` are on the same grid.
Args:
da1 (:class:`xarray.DataArray`):
...
da2 (:class:`xarray.DataArray`):
...
Returns:
bool
Whether ``da1`` and ``da2`` are on the same grid.
"""
x1equal = np.array_equal(da1["x1"].values, da2["x1"].values)
x2equal = np.array_equal(da1["x2"].values, da2["x2"].values)
Expand All @@ -763,16 +768,14 @@ def interp_da1_to_da2(da1: xr.DataArray, da2: xr.DataArray) -> xr.DataArray:
.. note::
``da1`` and ``da2`` are assumed normalised by ``DataProcessor``.
Parameters
----------
da1 : :class:`xarray.DataArray`
...
da2 : :class:`xarray.DataArray`
...
Returns
-------
:class:`xarray.DataArray`
Interpolated xarray.
Args:
da1 (:class:`xarray.DataArray`):
...
da2 (:class:`xarray.DataArray`):
...
Returns:
:class:`xarray.DataArray`
Interpolated xarray.
"""
return da1.interp(x1=da2["x1"], x2=da2["x2"], method="nearest")
Loading

0 comments on commit 7390847

Please sign in to comment.