Skip to content

Commit 4a888af

Browse files
authored
Sort spectra before plotting (#75)
1 parent cc7f020 commit 4a888af

2 files changed

Lines changed: 38 additions & 4 deletions

File tree

‎cleo/light/light_dependence.py‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ def log_makima_interpolator(lambdas_nm, epsilons, lambda_new_nm):
5555
return _log_(makima_interpolator, lambdas_nm, epsilons, lambda_new_nm)
5656

5757

58+
def _sorted_spectrum(spectrum):
59+
spectrum_array = np.array(spectrum)
60+
return spectrum_array[spectrum_array[:, 0].argsort()].T
61+
62+
5863
# hacky MRO stuff...multiple inheritance only works because slots=False,
5964
# and must be placed *before* SynapseDevice to work right
6065
@define(eq=False, slots=False)
@@ -122,8 +127,7 @@ def _get_source_for_synapse(
122127
def epsilon(self, lambda_new: Quantity) -> float:
123128
"""Returns the :math:`\\varepsilon` value for a given lambda (including units)
124129
representing the relative sensitivity of the opsin to that wavelength."""
125-
lam_eps_array = np.array(self.spectrum)
126-
lambdas, epsilons = lam_eps_array[lam_eps_array[:, 0].argsort()].T
130+
lambdas, epsilons = _sorted_spectrum(self.spectrum)
127131
lambda_new /= nmeter
128132
eps_new = self.spectrum_interpolator(lambdas, epsilons, lambda_new)
129133

@@ -205,7 +209,7 @@ def plot_spectra(
205209

206210
fig, ax = plt.subplots()
207211
for ldd in ldds:
208-
lambdas, epsilons = np.array(ldd.spectrum).T
212+
lambdas, epsilons = _sorted_spectrum(ldd.spectrum)
209213
i_data_in_range = (lambdas > xlim[0]) & (lambdas < xlim[1])
210214
lambdas, epsilons = lambdas[i_data_in_range], epsilons[i_data_in_range]
211215
if not extrapolate:

‎tests/light/test_light.py‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1+
import matplotlib.pyplot as plt
12
import neo
23
import pytest
34
import quantities as pq
45
from brian2 import Network, NeuronGroup, asarray, mm, mm2, ms, mwatt, nmeter, np, um
56

67
import cleo
7-
from cleo.light import GaussianEllipsoid, KoehlerBeam, Light, LightModel, fiber473nm
8+
from cleo.light import (
9+
GaussianEllipsoid,
10+
KoehlerBeam,
11+
Light,
12+
LightModel,
13+
fiber473nm,
14+
plot_spectra,
15+
)
16+
from cleo.opto import chr2_4s
817
from cleo.utilities import normalize_coords, unit_safe_allclose
918

1019

@@ -106,6 +115,27 @@ def test_reset():
106115
assert light.value == 0
107116

108117

118+
def test_plot_spectra_sorts_wavelengths():
119+
sorted_opsin = chr2_4s()
120+
unsorted_opsin = chr2_4s()
121+
unsorted_opsin.spectrum = unsorted_opsin.spectrum[::-1]
122+
original_spectrum = unsorted_opsin.spectrum.copy()
123+
124+
sorted_fig, sorted_ax = plot_spectra(sorted_opsin)
125+
unsorted_fig, unsorted_ax = plot_spectra(unsorted_opsin)
126+
127+
np.testing.assert_allclose(
128+
unsorted_ax.lines[0].get_xdata(), sorted_ax.lines[0].get_xdata()
129+
)
130+
np.testing.assert_allclose(
131+
unsorted_ax.lines[0].get_ydata(), sorted_ax.lines[0].get_ydata()
132+
)
133+
assert unsorted_opsin.spectrum == original_spectrum
134+
135+
plt.close(sorted_fig)
136+
plt.close(unsorted_fig)
137+
138+
109139
def test_coords():
110140
light = Light(light_model=fiber473nm(), coords=[[0, 0, 0], [1, 1, 1]] * mm)
111141
assert light.n == 2

0 commit comments

Comments
 (0)