-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added Results object and Results.solve() method with tests
- Loading branch information
edmundsj
committed
Jun 28, 2022
1 parent
d20b1c8
commit a9f9e59
Showing
7 changed files
with
121 additions
and
5 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
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,52 @@ | ||
from matplotlib import pyplot as plt | ||
|
||
|
||
class Results: | ||
def __init__(self, results_dict): | ||
self.inner_dict = results_dict | ||
|
||
def __getitem__(self, key): | ||
return self.inner_dict[key] | ||
|
||
def keys(self): | ||
return self.inner_dict.keys() | ||
|
||
def items(self): | ||
return self.inner_dict.items() | ||
|
||
def values(self): | ||
return self.inner_dict.values() | ||
|
||
def plot(self, x='wavelength', y='RTot', c=None, fig=None, ax=None, show=False): | ||
""" | ||
:param x: Variable to plot along the x-axis | ||
:param y: Variable to plot along the y-axis | ||
:param c: Variable to plot vs. x/y as distinct curves | ||
:param fig: Figure to use for plotting. If None, will create with pyplot interface | ||
:param ax: Axes to use for plotting. If None, will create with pyplot interface. | ||
:param show: Whether to show the plot using the pyplot interface. False by default. | ||
:returns fig, ax: Figure and Axes objects created with matplotlib pyplot interface | ||
""" | ||
if fig is None and ax is None: | ||
fig, ax = plt.subplots() | ||
elif fig is not None and ax is None: | ||
ax = fig.add_subplot() | ||
|
||
x_data = self[x] | ||
if hasattr(y, '__iter__') and not isinstance(y, str): | ||
y_data = [self[yy] for yy in y] | ||
else: | ||
y_data = [self[y]] | ||
|
||
for dat in y_data: | ||
ax.plot(x_data, dat) | ||
ax.legend(y) | ||
ax.set_xlabel(x) | ||
ax.set_ylabel(y) | ||
|
||
if show: | ||
plt.show() | ||
|
||
return fig, ax | ||
|
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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import pytest | ||
from rcwa import Results | ||
from matplotlib.figure import Axes, Figure | ||
from matplotlib import pyplot as plt | ||
|
||
|
||
@pytest.fixture() | ||
def results(): | ||
results_dict = {'rx': 1} | ||
res = Results(results_dict) | ||
yield res | ||
|
||
|
||
def test_getitem(results): | ||
assert results['rx'] == 1 | ||
|
||
|
||
def test_setitem(results): | ||
with pytest.raises(Exception) as ex: | ||
results['rx'] = 2 | ||
|
||
|
||
def test_keys(results): | ||
assert results.keys() == {'rx': 1}.keys() | ||
|
||
|
||
def test_values(results): | ||
assert list(results.values()) == list({'rx': 1}.values()) | ||
|
||
|
||
def test_items(results): | ||
assert results.items() == {'rx': 1}.items() | ||
|
||
|
||
def test_plot_fig(results): | ||
fig_desired = Figure() | ||
fig_actual, ax_actual = results.plot(x='rx', y='rx', fig=fig_desired, ax=None) | ||
assert fig_actual is fig_desired | ||
assert isinstance(ax_actual, Axes) | ||
assert fig_actual.axes[0] is ax_actual | ||
|
||
|
||
def test_plot_nofig(results): | ||
fig, ax = plt.subplots() | ||
fig_actual, ax_actual = results.plot(x='rx', y='rx', fig=None, ax=ax) | ||
assert isinstance(ax_actual, Axes) | ||
assert ax is ax_actual | ||
assert fig_actual is None | ||
assert fig.axes[0] is ax_actual | ||
|
||
|
||
def test_plot_nofig_noax(results): | ||
fig_actual, ax_actual = results.plot(x='rx', y='rx', fig=None, ax=None) | ||
assert isinstance(fig_actual, Figure) | ||
assert isinstance(ax_actual, Axes) | ||
assert fig_actual.axes[0] is ax_actual | ||
|
||
|
||
|