Skip to content

Commit 715d256

Browse files
committed
add alternate SpectralShape constructor
1 parent affff94 commit 715d256

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

colour/colorimetry/spectrum.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,29 @@ def __init__(self, start: Real, end: Real, interval: Real) -> None:
158158
self.end = end
159159
self.interval = interval
160160

161+
@classmethod
162+
def from_array(cls, data: ArrayLike) -> SpectralShape:
163+
"""Alternate constructor, create a SpectralShape from an ArrayLike list
164+
of values. Values must be evenly spaced.
165+
166+
Parameters
167+
----------
168+
data : ArrayLike
169+
The wavelength list
170+
171+
Returns
172+
-------
173+
SpectralShape
174+
"""
175+
data = np.asarray(data)
176+
177+
spacing = (diff_intermediate := np.diff(data))[0]
178+
if ~np.all(diff_intermediate == spacing):
179+
error = "data values must have equal spacing"
180+
raise RuntimeError(error)
181+
182+
return SpectralShape(data[0], data[-1], spacing)
183+
161184
@property
162185
def start(self) -> Real:
163186
"""

colour/colorimetry/tests/test_spectrum.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,6 +1417,21 @@ def test_range(self) -> None:
14171417
np.arange(0, 10 + 0.1, 0.1),
14181418
)
14191419

1420+
def test_from_data(self) -> None:
1421+
"""Test :func:`colour.colorimetry.spectrum.SpectralShape.from_array`"""
1422+
data = np.arange(400, 700 + 1, 10) # arange generates [start, stop)
1423+
shape = SpectralShape.from_array(data)
1424+
1425+
assert shape.start == 400
1426+
assert shape.end == 700
1427+
assert shape.interval == 10
1428+
1429+
assert shape == SpectralShape(400, 700, 10)
1430+
1431+
with pytest.raises(RuntimeError):
1432+
data = [400, 450, 500, 555, 600, 650, 700]
1433+
SpectralShape.from_array(data)
1434+
14201435

14211436
class TestSpectralDistribution:
14221437
"""

0 commit comments

Comments
 (0)