Skip to content

Commit

Permalink
- Big refactor to make documentation work correctly.
Browse files Browse the repository at this point in the history
  • Loading branch information
byrdie committed Nov 19, 2020
1 parent 9cff3ec commit 886b22e
Show file tree
Hide file tree
Showing 78 changed files with 2,653 additions and 3,156 deletions.
2 changes: 2 additions & 0 deletions docs/_templates/class_custom.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
:undoc-members:
:member-order: groupwise

.. automethod:: __init__

{% block attributes %}
{% if attributes %}
.. rubric:: {{ _('Attributes') }}
Expand Down
31 changes: 22 additions & 9 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
#
import os
import sys
import matplotlib.pyplot as plt
import matplotlib.axes
import astropy.time
import pandas


sys.path.insert(0, os.path.abspath('../'))

Expand All @@ -36,10 +41,16 @@
'sphinx.ext.inheritance_diagram',
]
autosummary_generate = True # Turn on sphinx.ext.autosummary
autosummary_imported_members = True

# autosummary_imported_members = True
# autoclass_content = 'both'
autodoc_typehints = "description"

# autosummary_filename_map = {
# 'kgpy.optics.Surface': 'kgpy.optics.Surface_cls',
# }

typehints_fully_qualified = True

graphviz_output_format = 'svg'
inheritance_graph_attrs = dict(rankdir='TB')

Expand All @@ -55,14 +66,17 @@

# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#

html_theme = 'bootstrap-astropy'
html_theme_options = {
'logotext1': 'kg', # white, semi-bold
'logotext2': 'py', # blue, light
'logotext3': ':docs', # white, light
'astropy_project_menubar': False
}
html_sidebars = {
'**': ['globaltoc.html'],
}

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
Expand All @@ -77,11 +91,10 @@
'numpy': ('https://numpy.org/doc/stable/', None),
'scipy': ('https://docs.scipy.org/doc/scipy/reference', None),
'matplotlib': ('https://matplotlib.org', None),
'astropy': ('https://docs.astropy.org/en/stable/', None)
'astropy': ('https://docs.astropy.org/en/stable/', None),
'pandas': ('https://pandas.pydata.org/pandas-docs/stable/', None),
}

import matplotlib.pyplot as plt
import matplotlib.axes
import astropy.time
plt.Axes.__module__ = matplotlib.axes.__name__
astropy.time.Time.__module__ = astropy.time.__name__
# plt.Axes.__module__ = matplotlib.axes.__name__
# astropy.time.Time.__module__ = astropy.time.__name__
# pandas.DataFrame.__module__ = pandas.__name__
106 changes: 97 additions & 9 deletions kgpy/__init__.py
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
9 changes: 9 additions & 0 deletions kgpy/__init__test.py
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))
37 changes: 0 additions & 37 deletions kgpy/_auto_axis.py

This file was deleted.

49 changes: 0 additions & 49 deletions kgpy/_linspace.py

This file was deleted.

42 changes: 0 additions & 42 deletions kgpy/_name.py

This file was deleted.

Loading

0 comments on commit 886b22e

Please sign in to comment.