Skip to content

Commit

Permalink
Fix tissot ellipse plotting
Browse files Browse the repository at this point in the history
  • Loading branch information
rhiannonlynne committed Apr 1, 2024
1 parent 8f02edc commit af1e92a
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 14 deletions.
30 changes: 16 additions & 14 deletions rubin_sim/maf/plots/spatial_plotters.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,26 +574,29 @@ def _plot_tissot_ellipse(self, lon, lat, radius, **kwargs):
Parameters
----------
lon : float or array_like
longitude-like of ellipse centers (radians)
lat : float or array_like
latitude-like of ellipse centers (radians)
radius : float or array_like
radius of ellipses (radians)
matplotlib axes instance on which to draw ellipses.
Other Parameters
----------------
other keyword arguments will be passed to matplotlib.patches.Ellipse.
lon : `float` or array_like
longitude-like of ellipse centers (radians)
lat : `float` or array_like
latitude-like of ellipse centers (radians)
radius : `float` or array_like
radius of ellipses (radians)
**kwargs : `dict`
Keyword argument which will be passed to
`matplotlib.patches.Ellipse`.
Returns
-------
ellipses : `list` [ `matplotlib.patches.Ellipse` ]
List of ellipses to add to the plot.
# The code in this method adapted from astroML, which is BSD-licensed.
# See http: //github.com/astroML/astroML for details.
"""
# Code adapted from astroML, which is BSD-licensed.
# See http: //github.com/astroML/astroML for details.
ellipses = []
for lon, b, diam in np.broadcast(lon, lat, radius * 2.0):
el = Ellipse((lon, b), diam / np.cos(b), diam, **kwargs)
for ll, bb, diam in np.broadcast(lon, lat, radius * 2.0):
el = Ellipse((ll, bb), diam / np.cos(bb), diam, **kwargs)
ellipses.append(el)
return ellipses

Expand Down Expand Up @@ -674,7 +677,6 @@ def __call__(self, metric_value_in, slicer, user_plot_dict, fig=None):
slicer.slice_points["dec"][good],
plot_dict["radius"],
rasterized=True,
ax=ax,
)
if plot_dict["metricIsColor"]:
current = None
Expand Down
64 changes: 64 additions & 0 deletions tests/maf/test_plotters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# imports
import unittest

import numpy as np
from matplotlib.figure import Figure

import rubin_sim.maf as maf


class TestPlotters(unittest.TestCase):
def setUp(self):
self.rng = np.random.default_rng()

def test_healpix_plotters(self):
# Set up a metric bundle to send to plotters
bundle1 = maf.create_empty_metric_bundle()
nside = 64
bundle1.slicer = maf.HealpixSlicer(nside=nside)
bundle1._setup_metric_values()
bundle1.metric_values += self.rng.uniform(size=len(bundle1.slicer))
# First test healpix sky map - just that it runs.
bundle1.set_plot_funcs([maf.HealpixSkyMap()])
figs = bundle1.plot()
self.assertTrue(isinstance(figs["SkyMap"], Figure))
# Test healpix histogram - just that it runs
bundle1.set_plot_funcs([maf.HealpixHistogram()])
figs = bundle1.plot()
self.assertTrue(isinstance(figs["Histogram"], Figure))
# Test power spectrum
bundle1.set_plot_funcs([maf.HealpixPowerSpectrum()])
figs = bundle1.plot()
self.assertTrue(isinstance(figs["PowerSpectrum"], Figure))

def test_base_skymap(self):
bundle1 = maf.create_empty_metric_bundle()
npoints = 1000
ra = self.rng.uniform(low=0, high=360, size=npoints)
dec = self.rng.uniform(low=-90, high=90, size=npoints)
bundle1.slicer = maf.UserPointsSlicer(ra, dec)
bundle1._setup_metric_values()
bundle1.metric_values += self.rng.uniform(size=len(bundle1.slicer))
# Test skymap
bundle1.set_plot_funcs([maf.BaseSkyMap()])
figs = bundle1.plot()
self.assertTrue(isinstance(figs["SkyMap"], Figure))
# Test healpix histogram - just that it runs
bundle1.set_plot_funcs([maf.HealpixHistogram()])
figs = bundle1.plot()
self.assertTrue(isinstance(figs["Histogram"], Figure))

def test_oned_plotter(self):
bundle1 = maf.create_empty_metric_bundle()
npoints = 100
bins = np.arange(0, npoints, 1)
bundle1.slicer = maf.OneDSlicer(slice_col_name="test", bins=bins)
bundle1.slicer.slice_points = {"bins": bins}
bundle1.slicer.nslice = len(bins) - 1
bundle1.slicer.shape = len(bins) - 1
bundle1._setup_metric_values()
bundle1.metric_values += self.rng.uniform(size=len(bundle1.slicer))
# Test plotter
bundle1.set_plot_funcs([maf.OneDBinnedData()])
figs = bundle1.plot()
self.assertTrue(isinstance(figs["BinnedData"], Figure))

0 comments on commit af1e92a

Please sign in to comment.