Skip to content

Commit

Permalink
added Results object and Results.solve() method with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
edmundsj committed Jun 28, 2022
1 parent d20b1c8 commit a9f9e59
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 5 deletions.
2 changes: 2 additions & 0 deletions rcwa/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
from rcwa.material import Material
from rcwa.crystal import Crystal
from rcwa.layer import LayerStack, Layer, freeSpaceLayer
from rcwa.results import Results
from rcwa.slicer import Slicer
from rcwa.grating import RectangularGrating, TriangularGrating, Grating
from rcwa.source import Source, zeroSource
from rcwa.solver import Solver
from rcwa.utils import Plotter
from rcwa.shorthand import complexArray

4 changes: 2 additions & 2 deletions rcwa/examples/bragg_mirror.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ def solve_system():
results = TMMSolver.solve(wavelength=wavelengths)
return results


if __name__ == '__main__':
results = solve_system()
#Plotter.plotEllipsometrySpectra(TMMSolver1.results)
Plotter.plotRTSpectra(results)
fig, ax = results.plot(x='wavelength', y=['RTot', 'TTot', 'conservation'])
plt.show()
1 change: 1 addition & 0 deletions rcwa/examples/diffraction_grating_triangular_1D.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from rcwa.shorthand import complexArray
import numpy as np


def solve_system():

reflection_layer = Layer(er=1.0, ur=1.0)
Expand Down
52 changes: 52 additions & 0 deletions rcwa/results.py
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

3 changes: 2 additions & 1 deletion rcwa/solver.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from rcwa.shorthand import *
from rcwa.matrices import *
from rcwa.harmonics import *
from rcwa import Layer, LayerStack
from rcwa import Layer, LayerStack, Results
from copy import deepcopy
from progressbar import ProgressBar, Bar, Counter, ETA
from itertools import product
Expand Down Expand Up @@ -188,6 +188,7 @@ def _package_results(self):
else:
new_results = self.results[0]

new_results = Results(new_results)
return new_results

def _append_results(self):
Expand Down
5 changes: 3 additions & 2 deletions rcwa/test/test_examples.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest
import numpy as np
from rcwa import Results
from numpy.testing import assert_allclose, assert_equal
from rcwa.examples.bragg_mirror import solve_system as solve_bragg
from rcwa.examples.diffraction_grating_1D import solve_system as solve_rectangular_1d
Expand All @@ -14,7 +15,7 @@

def test_bragg_mirror():
results = solve_bragg()
assert isinstance(results, dict)
assert isinstance(results, Results)
assert 'wavelength' in results.keys()
assert 'RTot' in results.keys()
assert 'TTot' in results.keys()
Expand Down Expand Up @@ -84,7 +85,7 @@ def test_solve_thin_film_dispersive():
def test_solve_triangular_pc():
results = solve_triangular_photonic_crystal()
for x in ['conservation', 'rx', 'RTot']:
assert x in results
assert x in results.keys()
rx_desired = np.array([ 0.0194899 +0.01175673j, -0.05570714+0.03010019j,
-0.03797483-0.03124727j, -0.03920141+0.01425774j,
0.20357306-0.00134404j, -0.01196725-0.02682893j,
Expand Down
59 changes: 59 additions & 0 deletions rcwa/test/test_results.py
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



0 comments on commit a9f9e59

Please sign in to comment.