1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
from unittest import mock
import numpy as np
import pytest
import warnings
from matplotlib.axes import Axes
from matplotlib import pyplot as plt
from einsteinpy.geodesic import Timelike
from einsteinpy.plotting import StaticGeodesicPlotter
@pytest.fixture()
def dummy_geod():
return Timelike(
metric="Kerr",
metric_params=(0.9,),
position=[2.15, np.pi / 2, 0.],
momentum=[0., 0., 1.5],
steps=50,
delta=0.5,
omega=0.01, # Close orbit
return_cartesian=True,
suppress_warnings=True,
)
def test_static_geod_plotter_has_axes(dummy_geod):
geod = dummy_geod
sgpl = StaticGeodesicPlotter()
sgpl.plot(geod)
assert isinstance(sgpl.ax, Axes)
@mock.patch("einsteinpy.plotting.geodesic.static.plt.show")
def test_static_geod_plotter_draws_plot2D(mock_show, dummy_geod):
geod = dummy_geod
sgpl = StaticGeodesicPlotter()
sgpl.plot2D(geod)
fig = sgpl.show()
mock_show.assert_called_with()
def test_static_geod_plotter_plot2D_raises_error(dummy_geod):
geod = dummy_geod
sgpl = StaticGeodesicPlotter()
try:
sgpl.plot2D(geod, coordinates=(-1, 2))
assert False
except IndexError:
assert True
@mock.patch("einsteinpy.plotting.geodesic.static.plt.show")
def test_static_geod_plotter_draws_parametric_plot(mock_show, dummy_geod):
geod = dummy_geod
sgpl = StaticGeodesicPlotter()
sgpl.parametric_plot(geod)
fig = sgpl.show()
mock_show.assert_called_with()
def test_static_geod_plotter_parameters(dummy_geod):
geod = dummy_geod
sgpl = StaticGeodesicPlotter(bh_colors=("#0F0", "#FAF"), draw_ergosphere=False)
assert sgpl.bh_colors == ("#0F0", "#FAF")
assert sgpl.draw_ergosphere is False
def test_static_geod_plotter_ax_warning(dummy_geod):
geod = dummy_geod
fig, ax = plt.subplots()
assert isinstance(ax, Axes)
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
sgpl = StaticGeodesicPlotter(ax=ax)
assert len(w) == 1 # 1 warning to be shown
assert issubclass(w[-1].category, PendingDeprecationWarning)
@mock.patch("einsteinpy.plotting.geodesic.static.plt.show")
def test_plot_calls_plt_geod_show(mock_show, dummy_geod):
geod = dummy_geod
sgpl = StaticGeodesicPlotter()
sgpl.plot(geod)
sgpl.show()
mock_show.assert_called_with()
@mock.patch("einsteinpy.plotting.geodesic.static.plt.show")
def test_plot_calls_plt_geod_show_view_init(mock_show, dummy_geod):
geod = dummy_geod
sgpl = StaticGeodesicPlotter()
sgpl.plot(geod)
sgpl.show(azim=-60, elev=30)
mock_show.assert_called_with()
@mock.patch("einsteinpy.plotting.geodesic.static.plt.savefig")
def test_plot_geod_save_saves_plot(mock_save, dummy_geod):
geod = dummy_geod
sgpl = StaticGeodesicPlotter()
sgpl.plot(geod)
name = "test_plot.png"
sgpl.save(name)
mock_save.assert_called_with(name)
assert sgpl.ax.name == "3d"
@mock.patch("einsteinpy.plotting.geodesic.static.plt.savefig")
def test_plot_geod_save_saves_parametric_plot(mock_save, dummy_geod):
geod = dummy_geod
sgpl = StaticGeodesicPlotter()
sgpl.parametric_plot(geod)
name = "Parametric.png"
sgpl.save()
mock_save.assert_called_with(name)
assert sgpl.ax.name != "3d"
@mock.patch("einsteinpy.plotting.geodesic.static.plt.show")
def test_static_geod_plotter_show_clear(mock_show, dummy_geod):
geod = dummy_geod
sgpl = StaticGeodesicPlotter(draw_ergosphere=False)
sgpl.plot(geod)
sgpl.show()
mock_show.assert_called_with()
sgpl.clear()
assert sgpl.fig.get_axes() == []
def test_animate_creates_ani(dummy_geod):
geod = dummy_geod
sgpl = StaticGeodesicPlotter()
sgpl.animate(geod, interval=10)
assert sgpl.ani
|