Skip to content

Commit

Permalink
Adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bmorris3 committed Nov 8, 2017
1 parent 5d6b289 commit be211a6
Show file tree
Hide file tree
Showing 12 changed files with 1,322 additions and 24 deletions.
958 changes: 958 additions & 0 deletions ah_bootstrap.py

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,7 @@

# Example configuration for intersphinx: refer to the Python standard library.
intersphinx_mapping = {'https://docs.python.org/': None}
intersphinx_mapping['astropy'] = ('http://astropy.readthedocs.io/en/latest/', None)
intersphinx_mapping['astroquery'] = ('http://astroquery.readthedocs.io/en/latest/', None)
intersphinx_mapping['matplotlib'] = ('https://matplotlib.org/contents.html', None)
intersphinx_mapping['numpy'] = ('https://docs.scipy.org/doc/numpy/', None)
15 changes: 10 additions & 5 deletions mrspoc/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
# Licensed under the MIT License - see LICENSE.rst

from __future__ import (absolute_import, division, print_function,
unicode_literals)

from .star import *
from .tgas import *
from .gaia import *
# Packages may add whatever they like to this file, but
# should keep this content at the top.
# ----------------------------------------------------------------------------
from ._astropy_init import *
# ----------------------------------------------------------------------------

if not _ASTROPY_SETUP_:
from .star import *
from .tgas import *
from .gaia import *
46 changes: 46 additions & 0 deletions mrspoc/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# this contains imports plugins that configure py.test for astropy tests.
# by importing them here in conftest.py they are discoverable by py.test
# no matter how it is invoked within the source tree.

from astropy.tests.pytest_plugins import *

## Uncomment the following line to treat all DeprecationWarnings as
## exceptions. For Astropy v2.0 or later, there are 2 additional keywords,
## as follow (although default should work for most cases).
## To ignore some packages that produce deprecation warnings on import
## (in addition to 'compiler', 'scipy', 'pygments', 'ipykernel', and
## 'setuptools'), add:
## modules_to_ignore_on_import=['module_1', 'module_2']
## To ignore some specific deprecation warning messages for Python version
## MAJOR.MINOR or later, add:
## warnings_to_ignore_by_pyver={(MAJOR, MINOR): ['Message to ignore']}
# enable_deprecations_as_exceptions()

## Uncomment and customize the following lines to add/remove entries from
## the list of packages for which version numbers are displayed when running
## the tests. Making it pass for KeyError is essential in some cases when
## the package uses other astropy affiliated packages.
# try:
# PYTEST_HEADER_MODULES['Astropy'] = 'astropy'
# PYTEST_HEADER_MODULES['scikit-image'] = 'skimage'
# del PYTEST_HEADER_MODULES['h5py']
# except (NameError, KeyError): # NameError is needed to support Astropy < 1.0
# pass

## Uncomment the following lines to display the version number of the
## package rather than the version number of Astropy in the top line when
## running the tests.
# import os
#
## This is to figure out the package version, rather than
## using Astropy's
# try:
# from .version import version
# except ImportError:
# version = 'dev'
#
# try:
# packagename = os.path.basename(os.path.dirname(__file__))
# TESTED_VERSIONS[packagename] = version
# except NameError: # Needed to support Astropy <= 1.0.0
# pass
62 changes: 53 additions & 9 deletions mrspoc/star.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import matplotlib.pyplot as plt
import numpy as np
from scipy.integrate import quad
from matplotlib.patches import Circle
from matplotlib.patches import Circle, Ellipse
from matplotlib.collections import PatchCollection


Expand Down Expand Up @@ -90,7 +90,10 @@ def plot(self, ax=None, col=True, col_exaggerate=1):

patches = []
for spot in self.spots:
patches.append(Circle((spot.x, spot.y), spot.r))
longitude = np.arcsin(spot.x)
width = np.cos(longitude) * spot.r * 2
height = spot.r * 2
patches.append(Ellipse((spot.x, spot.y), width, height))

p1 = PatchCollection([Circle((0, 0), 1)], alpha=1, color='w')
p2 = PatchCollection(patches, alpha=(1-spot.contrast), color='k')
Expand Down Expand Up @@ -120,20 +123,61 @@ def center_of_light(self):
y_centroid : float
Photocenter in the y dimension, in units of stellar radii
"""
return self._centroid_analytic()

def _centroid_analytic(self):
"""
Compute the stellar centroid using an analytic approximation.
"""
x_centroid = 0
y_centroid = 0
total_flux = np.pi * self.r**2

for spot in self.spots:
def _x_weight(x):
return - spot.contrast * 2 * x * np.sqrt(spot.r**2 - (x - spot.x)**2)
# spot_longitude = np.arcsin(spot.x)
# foreshortened_width = np.cos(spot_longitude)
# the above is equivalent to:
foreshortened_width = np.sqrt(1 - spot.x**2)

def _x_weighted(x):
return - spot.contrast * x * np.sqrt(spot.r**2 - (x - spot.x)**2 /
foreshortened_width**2)

def _y_weight(y):
return - spot.contrast * 2 * y * np.sqrt(spot.r**2 - (y - spot.y)**2)
def _y_weighted(y):
return - spot.contrast * y * np.sqrt(spot.r**2 - (y - spot.y)**2)

x_i = quad(_x_weight, spot.x-spot.r, spot.x+spot.r)[0]
y_i = quad(_y_weight, spot.y-spot.r, spot.y+spot.r)[0]
b = spot.r * foreshortened_width
a = spot.r
total_flux -= (1 - spot.contrast) * np.pi * a * b

x_i = quad(_x_weighted, spot.x - spot.r*foreshortened_width,
spot.x + spot.r*foreshortened_width)[0]
y_i = quad(_y_weighted, spot.y - spot.r, spot.y + spot.r)[0]

x_centroid += x_i
y_centroid += y_i

return x_centroid, y_centroid
return x_centroid / total_flux, y_centroid / total_flux

def _centroid_numerical(self, n=1000):
"""
Compute the stellar centroid using a numerical approximation.
"""
image = np.zeros((n, n))
x = np.linspace(-1, 1, n)
y = np.linspace(-1, 1, n)
x, y = np.meshgrid(x, y)

on_star = x**2 + y**2 <= 1
image[on_star] = 1

for spot in self.spots:
foreshortened_width = np.sqrt(1 - spot.x**2)

on_spot = ((x - spot.x)**2/foreshortened_width**2 +
(y - spot.y)**2 <= spot.r**2)
image[on_spot & on_star] = spot.contrast

x_centroid = np.sum(image * x)/np.sum(image)
y_centroid = np.sum(image * y)/np.sum(image)
return x_centroid, y_centroid
Empty file added mrspoc/tests/__init__.py
Empty file.
31 changes: 31 additions & 0 deletions mrspoc/tests/coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[run]
source = {packagename}
omit =
{packagename}/_astropy_init*
{packagename}/conftest*
{packagename}/cython_version*
{packagename}/setup_package*
{packagename}/*/setup_package*
{packagename}/*/*/setup_package*
{packagename}/tests/*
{packagename}/*/tests/*
{packagename}/*/*/tests/*
{packagename}/version*

[report]
exclude_lines =
# Have to re-enable the standard pragma
pragma: no cover

# Don't complain about packages we have installed
except ImportError

# Don't complain if tests don't hit assertions
raise AssertionError
raise NotImplementedError

# Don't complain about script hooks
def main\(.*\):

# Ignore branches that don't pertain to this version of Python
pragma: py{ignore_python_version}
8 changes: 8 additions & 0 deletions mrspoc/tests/setup_package.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# import os

# If this package has tests data in the tests/data directory, add them to
# the paths here, see commented example
paths = [] #[os.path.join('data', '*fits')]

def get_package_data():
return {'mrspoc.tests': paths}
18 changes: 18 additions & 0 deletions mrspoc/tests/test_centroid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import numpy as np

from ..star import Star, Spot


def test_col_methods():
"""
python -c "from mrspoc.tests.test import test_col_methods as f; f()"
"""

star = Star()
star.spots = [Spot(x=0.8, y=0.00, r=0.2)]

x0, y0 = star._centroid_analytic()
x1, y1 = star._centroid_numerical()

diff = np.sqrt((x0 - x1)**2 + (y0 - y1)**2)
assert diff < star.r / 100
3 changes: 2 additions & 1 deletion mrspoc/tgas.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ def color_cut(b_minus_v):
color_cut(2) + 1, color_cut(2) - 1,
color_cut(0.6) - 1]

H, xedges, yedges = np.histogram2d(b_minus_v, M_V, bins=1000)
H, xedges, yedges = np.histogram2d(b_minus_v[abs(b_minus_v) > 1e-3],
M_V[abs(b_minus_v) > 1e-3], bins=1000)

extent = [xedges.min(), xedges.max(), yedges.max(), yedges.min()]
ax.imshow(np.log10(H.T), extent=extent, cmap=plt.cm.Greys, aspect=0.2)
Expand Down
57 changes: 57 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
[build_sphinx]
source-dir = docs
build-dir = docs/_build
all_files = 1

[build_docs]
source-dir = docs
build-dir = docs/_build
all_files = 1

[upload_docs]
upload-dir = docs/_build/html
show-response = 1

[tool:pytest]
minversion = 3.0
norecursedirs = build docs/_build
doctest_plus = enabled
addopts = -p no:warnings

[ah_bootstrap]
auto_use = True

[pycodestyle]
# E101 - mix of tabs and spaces
# W191 - use of tabs
# W291 - trailing whitespace
# W292 - no newline at end of file
# W293 - trailing whitespace
# W391 - blank line at end of file
# E111 - 4 spaces per indentation level
# E112 - 4 spaces per indentation level
# E113 - 4 spaces per indentation level
# E901 - SyntaxError or IndentationError
# E902 - IOError
select = E101,W191,W291,W292,W293,W391,E111,E112,E113,E901,E902
exclude = extern,sphinx,*parsetab.py

[metadata]
package_name = mrspoc
description = mrspoc
long_description = 'M-dwarf Rotational Stellar Photocenter Offset Calculator'
author = Brett Morris
author_email = bmmorris@uw.edu
license = MIT License
url = https://github.com/bmorris3/mrspoc
edit_on_github = True
github_project = bmorris3/mrspoc
# install_requires should be formatted as a comma-separated list, e.g.:
# install_requires = astropy, scipy, matplotlib
install_requires = numpy, astropy, matplotlib, scipy, sphinx-automodapi
# version should be PEP386 compatible (http://www.python.org/dev/peps/pep-0386)
version = 0.0.dev0

[entry_points]


0 comments on commit be211a6

Please sign in to comment.