-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Big refactor to make documentation work correctly.
- Loading branch information
Showing
78 changed files
with
2,653 additions
and
3,156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,109 @@ | ||
""" | ||
kgpy root package | ||
""" | ||
import dataclasses | ||
import typing as typ | ||
import numpy as np | ||
|
||
__all__ = [ | ||
'AutoAxis', | ||
'linspace', 'midspace', | ||
'Name', | ||
'fft', | ||
'rebin', | ||
'Obs', | ||
] | ||
|
||
from ._auto_axis import AutoAxis | ||
from ._linspace import linspace, midspace | ||
from ._name import Name | ||
from . import fft | ||
from ._rebin import rebin | ||
from ._obs import Obs | ||
|
||
Obs.__module__ = __name__ | ||
@dataclasses.dataclass | ||
class Name: | ||
""" | ||
Representation of a hierarchical namespace. | ||
Names are a composition of a parent, which is also a name, and a base which is a simple string. | ||
The string representation of a name is <parent>.base, where <parent> is the parent's string expansion. | ||
""" | ||
|
||
base: str = '' #: Base of the name, this string will appear last in the string representation | ||
parent: 'typ.Optional[Name]' = None #: Parent string of the name, this itself also a name | ||
|
||
def copy(self): | ||
if self.parent is not None: | ||
parent = self.parent.copy() | ||
else: | ||
parent = self.parent | ||
return type(self)( | ||
base=self.base, | ||
parent=parent, | ||
) | ||
|
||
def __add__(self, other: str) -> 'Name': | ||
""" | ||
Quickly create the name of a child's name by adding a string to the current instance. | ||
Adding a string to a name instance returns | ||
:param other: A string representing the basename of the new Name instance. | ||
:return: A new `kgpy.Name` instance with the `self` as the `parent` and `other` as the `base`. | ||
""" | ||
return type(self)(base=other, parent=self) | ||
|
||
def __repr__(self): | ||
if self.parent is not None: | ||
return self.parent.__repr__() + '.' + self.base | ||
|
||
else: | ||
return self.base | ||
|
||
|
||
def rebin(arr: np.ndarray, scale_dims: typ.Tuple[int, ...]) -> np.ndarray: | ||
""" | ||
Increases the size of an array by scale_dims in each i dimension by repeating each value scale_dims[i] times along | ||
that axis. | ||
:param arr: Array to modify | ||
:param scale_dims: Tuple with length ``arr.ndim`` specifying the size increase in each axis. | ||
:return: The resized array | ||
""" | ||
new_arr = np.broadcast_to(arr, scale_dims + arr.shape) | ||
start_axes = np.arange(arr.ndim) | ||
new_axes = 2 * start_axes + 1 | ||
new_arr = np.moveaxis(new_arr, start_axes, new_axes) | ||
|
||
new_shape = np.array(arr.shape) * np.array(scale_dims) | ||
new_arr = np.reshape(new_arr, new_shape) | ||
return new_arr | ||
|
||
|
||
def linspace(start: np.ndarray, stop: np.ndarray, num: int, axis: int = 0) -> np.ndarray: | ||
""" | ||
A modified version of :func:`numpy.linspace()` that returns a value in the center of the range between `start` | ||
and `stop` if `num == 1` unlike :func:`numpy.linspace` which would just return `start`. | ||
This function is often helfpul when creating a grid. | ||
Sometimes you want to test with only a single element, but you want that element to be in the center of the range | ||
and not off to one side. | ||
:param start: The starting value of the sequence. | ||
:param stop: The end value of the sequence, must be broadcastable with `start`. | ||
:param num: Number of samples to generate for this sequence. | ||
:param axis: The axis in the result used to store the samples. The default is the first axis. | ||
:return: An array the size of the broadcasted shape of `start` and `stop` with an additional dimension of length | ||
`num`. | ||
""" | ||
if num == 1: | ||
return np.expand_dims((start + stop) / 2, axis=axis) | ||
else: | ||
return np.linspace(start=start, stop=stop, num=num, axis=axis) | ||
|
||
|
||
def midspace(start: np.ndarray, stop: np.ndarray, num: int, axis: int = 0) -> np.ndarray: | ||
""" | ||
A modified version of :func:`numpy.linspace` that selects cell centers instead of cell edges. | ||
:param start: | ||
:param stop: | ||
:param num: | ||
:param axis: | ||
:return: | ||
""" | ||
a = np.linspace(start=start, stop=stop, num=num + 1, axis=axis) | ||
i0 = [slice(None)] * a.ndim | ||
i1 = i0.copy() | ||
i0[axis] = slice(None, ~0) | ||
i1[axis] = slice(1, None) | ||
return (a[i0] + a[i1]) / 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import numpy as np | ||
from . import rebin | ||
|
||
|
||
def test_rebin(): | ||
x = np.random.rand(2, 5, 7, 3) | ||
scales = (2, 2, 2, 2) | ||
new_x = rebin(x, scales) | ||
assert new_x.shape == tuple(x.shape * np.array(scales)) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.