colour.colorimetry.spectrum Module

Spectrum

Defines the classes handling spectral data computation:

colour.colorimetry.spectrum.DEFAULT_WAVELENGTH_DECIMALS = 10

Default wavelength precision decimals.

DEFAULT_WAVELENGTH_DECIMALS : int

class colour.colorimetry.spectrum.SpectralMapping(data=None, wavelength_decimals=10, **kwargs)[source]

Bases: colour.utilities.data_structures.ArbitraryPrecisionMapping

Defines the base mapping for spectral data.

It enables usage of floating point wavelengths as keys by rounding them at a specific decimals count.

Parameters:
  • data (dict or SpectralMapping, optional) – Spectral data in a dict or SpectralMapping as follows: {wavelength \(\lambda_{i}\): value, wavelength \(\lambda_{i+1}\): value, ..., wavelength \(\lambda_{i+n}\): value}
  • wavelength_decimals (int, optional) – Decimals count the keys will be rounded at.
Other Parameters:
 

**kwargs (dict, optional) – Key / Value pairs to store into the mapping at initialisation.

wavelength_decimals

Examples

>>> data1 = {380.1999999998: 0.000039, 380.2000000000: 0.000039}
>>> mapping = SpectralMapping(data1, wavelength_decimals=10)
>>> # Doctests skip for Python 2.x compatibility.
>>> tuple(mapping.keys())  
(380.1999999..., 380.2)
>>> mapping = SpectralMapping(data1, wavelength_decimals=7)
>>> # Doctests skip for Python 2.x compatibility.
>>> tuple(mapping.keys())  
(380.2,)
wavelength_decimals

Property for self.key_decimals attribute.

Returns:self.key_decimals.
Return type:unicode
class colour.colorimetry.spectrum.SpectralShape(start=None, end=None, interval=None)[source]

Bases: object

Defines the base object for spectral power distribution shape.

Parameters:
  • start (numeric, optional) – Wavelength \(\lambda_{i}\) range start in nm.
  • end (numeric, optional) – Wavelength \(\lambda_{i}\) range end in nm.
  • interval (numeric, optional) – Wavelength \(\lambda_{i}\) range interval.
start
end
interval
boundaries
__str__()[source]
__repr__()[source]
__iter__()[source]
__contains__()[source]
__len__()[source]
__eq__()[source]
__ne__()[source]
range()[source]

Examples

>>> # Doctests skip for Python 2.x compatibility.
>>> SpectralShape(360, 830, 1)  
SpectralShape(360, 830, 1)
boundaries

Property for self._start and self._end private attributes.

Returns:self._start, self._end.
Return type:tuple
end

Property for self._end private attribute.

Returns:self._end.
Return type:numeric
interval

Property for self._interval private attribute.

Returns:self._interval.
Return type:numeric
range()[source]

Returns an iterable range for the spectral power distribution shape.

Returns:Iterable range for the spectral power distribution shape
Return type:ndarray
Raises:RuntimeError – If one of spectral shape start, end or interval attributes is not defined.

Examples

>>> SpectralShape(0, 10, 0.1).range()
array([  0. ,   0.1,   0.2,   0.3,   0.4,   0.5,   0.6,   0.7,   0.8,
         0.9,   1. ,   1.1,   1.2,   1.3,   1.4,   1.5,   1.6,   1.7,
         1.8,   1.9,   2. ,   2.1,   2.2,   2.3,   2.4,   2.5,   2.6,
         2.7,   2.8,   2.9,   3. ,   3.1,   3.2,   3.3,   3.4,   3.5,
         3.6,   3.7,   3.8,   3.9,   4. ,   4.1,   4.2,   4.3,   4.4,
         4.5,   4.6,   4.7,   4.8,   4.9,   5. ,   5.1,   5.2,   5.3,
         5.4,   5.5,   5.6,   5.7,   5.8,   5.9,   6. ,   6.1,   6.2,
         6.3,   6.4,   6.5,   6.6,   6.7,   6.8,   6.9,   7. ,   7.1,
         7.2,   7.3,   7.4,   7.5,   7.6,   7.7,   7.8,   7.9,   8. ,
         8.1,   8.2,   8.3,   8.4,   8.5,   8.6,   8.7,   8.8,   8.9,
         9. ,   9.1,   9.2,   9.3,   9.4,   9.5,   9.6,   9.7,   9.8,
         9.9,  10. ])
start

Property for self._start private attribute.

Returns:self._start.
Return type:numeric
class colour.colorimetry.spectrum.SpectralPowerDistribution(name, data, title=None)[source]

Bases: object

Defines the base object for spectral data computations.

Parameters:
  • name (unicode) – Spectral power distribution name.
  • data (dict or SpectralMapping) – Spectral power distribution data in a dict or SpectralMapping as follows: {wavelength \(\lambda_{i}\): value, wavelength \(\lambda_{i+1}\): value, ..., wavelength \(\lambda_{i+n}\): value}
  • title (unicode, optional) – Spectral power distribution title for figures.

Notes

  • Underlying spectral data is stored within a colour.SpectralMapping class mapping which implies that wavelengths keys will be rounded.
name
data
title
wavelengths
values
items
shape
__str__()[source]
__repr__()[source]
__hash__()[source]
__init__()[source]
__getitem__()[source]
__setitem__()[source]
__iter__()[source]
__contains__()[source]
__len__()[source]
__eq__()[source]
__ne__()[source]
__add__()[source]
__iadd__()[source]
__sub__()[source]
__isub__()[source]
__mul__()[source]
__imul__()[source]
__div__()[source]
__idiv__()[source]
__pow__()[source]
__ipow__()[source]
get()[source]
is_uniform()[source]
extrapolate()[source]
interpolate()[source]
align()[source]
trim_wavelengths()[source]
zeros()[source]
normalise()[source]
clone()[source]

Examples

>>> data = {510: 49.67, 520: 69.59, 530: 81.73, 540: 88.19}
>>> spd = SpectralPowerDistribution('Sample', data)
>>> # Doctests skip for Python 2.x compatibility.
>>> spd.wavelengths  
array([510, 520, 530, 540])
>>> spd.values
array([ 49.67,  69.59,  81.73,  88.19])
>>> spd.shape  
SpectralShape(510, 540, 10)
align(shape, interpolation_method=None, extrapolation_method=u'Constant', extrapolation_left=None, extrapolation_right=None)[source]

Aligns the spectral power distribution to given spectral shape: Interpolates first then extrapolates to fit the given range.

Parameters:
  • shape (SpectralShape) – Spectral shape used for alignment.
  • interpolation_method (unicode, optional) – {None, ‘Cubic Spline’, ‘Linear’, ‘Pchip’, ‘Sprague’}, Enforce given interpolation method.
  • extrapolation_method (unicode, optional) – {‘Constant’, ‘Linear’}, Extrapolation method.
  • extrapolation_left (numeric, optional) – Value to return for low extrapolation range.
  • extrapolation_right (numeric, optional) – Value to return for high extrapolation range.
Returns:

Aligned spectral power distribution.

Return type:

SpectralPowerDistribution

Examples

>>> data = {
...     510: 49.67,
...     520: 69.59,
...     530: 81.73,
...     540: 88.19,
...     550: 86.26,
...     560: 77.18}
>>> spd = SpectralPowerDistribution('Sample', data)
>>> print(spd.align(SpectralShape(505, 565, 1)))
SpectralPowerDistribution('Sample', (505.0, 565.0, 1.0))
>>> # Doctests skip for Python 2.x compatibility.
>>> spd.wavelengths  
array([505, 506, 507, 508, 509, 510, 511, 512, 513, 514, 515, 516, 517,
       518, 519, 520, 521, 522, 523, 524, 525, 526, 527, 528, 529, 530,
       531, 532, 533, 534, 535, 536, 537, 538, 539, 540, 541, 542, 543,
       544, 545, 546, 547, 548, 549, 550, 551, 552, 553, 554, 555, 556,
       557, 558, 559, 560, 561, 562, 563, 564, 565])
>>> spd.values  
array([ 49.67     ...,  49.67     ...,  49.67     ...,  49.67     ...,
        49.67     ...,  49.67     ...,  51.8341162...,  53.9856467...,
        56.1229464...,  58.2366197...,  60.3121800...,  62.3327095...,
        64.2815187...,  66.1448055...,  67.9143153...,  69.59     ...,
        71.1759958...,  72.6627938...,  74.0465756...,  75.3329710...,
        76.5339542...,  77.6647421...,  78.7406907...,  79.7741932...,
        80.7715767...,  81.73     ...,  82.6407518...,  83.507872 ...,
        84.3326333...,  85.109696 ...,  85.8292968...,  86.47944  ...,
        87.0480863...,  87.525344 ...,  87.9056578...,  88.19     ...,
        88.3858347...,  88.4975634...,  88.5258906...,  88.4696570...,
        88.3266460...,  88.0943906...,  87.7709802...,  87.3558672...,
        86.8506741...,  86.26     ...,  85.5911699...,  84.8503430...,
        84.0434801...,  83.1771110...,  82.2583874...,  81.2951360...,
        80.2959122...,  79.2700525...,  78.2277286...,  77.18     ...,
        77.18     ...,  77.18     ...,  77.18     ...,  77.18     ...])
clone()[source]

Clones the spectral power distribution.

Most of the SpectralPowerDistribution class operations are conducted in-place. The SpectralPowerDistribution.clone() method provides a convenient way to copy the spectral power distribution to a new object.

Returns:Cloned spectral power distribution.
Return type:SpectralPowerDistribution

Examples

>>> data = {510: 49.67, 520: 69.59, 530: 81.73, 540: 88.19}
>>> spd = SpectralPowerDistribution('Sample', data)
>>> print(spd)  
SpectralPowerDistribution('Sample', (510..., 540..., 10...))
>>> spd_clone = spd.clone()
>>> print(spd_clone)  
SpectralPowerDistribution('Sample (...)', (510..., 540..., 10...))
data

Property for self._data private attribute.

Returns:self._data.
Return type:SpectralMapping
extrapolate(shape, method=u'Constant', left=None, right=None)[source]

Extrapolates the spectral power distribution following CIE 15:2004 recommendation.

Parameters:
  • shape (SpectralShape) – Spectral shape used for extrapolation.
  • method (unicode, optional) – {‘Constant’, ‘Linear’},, Extrapolation method.
  • left (numeric, optional) – Value to return for low extrapolation range.
  • right (numeric, optional) – Value to return for high extrapolation range.
Returns:

Extrapolated spectral power distribution.

Return type:

SpectralPowerDistribution

References

[2]CIE TC 1-48. (2004). Extrapolation. In CIE 015:2004 Colorimetry, 3rd Edition (p. 24). ISBN:978-3-901-90633-6
[3]CIE TC 1-38. (2005). EXTRAPOLATION. In CIE 167:2005 Recommended Practice for Tabulating Spectral Data for Use in Colour Computations (pp. 19–20). ISBN:978-3-901-90641-1

Examples

>>> data = {510: 49.67, 520: 69.59, 530: 81.73, 540: 88.19}
>>> spd = SpectralPowerDistribution('Sample', data)
>>> spd.extrapolate(  
...     SpectralShape(400, 700)).shape
SpectralShape(400..., 700..., 10...)
>>> spd[400]  
array(49.67...)
>>> spd[700]  
array(88.1...)
get(wavelength, default=nan)[source]

Returns the value for given wavelength \(\lambda\).

Parameters:
  • wavelength (numeric or ndarray) – Wavelength \(\lambda\) to retrieve the value.
  • default (nan or numeric, optional) – Wavelength \(\lambda\) default value.
Returns:

Wavelength \(\lambda\) value.

Return type:

numeric or ndarray

Examples

>>> data = {510: 49.67, 520: 69.59, 530: 81.73, 540: 88.19}
>>> spd = SpectralPowerDistribution('Sample', data)
>>> # Doctests ellipsis for Python 2.x compatibility.
>>> spd.get(510)  
array(49.67...)
>>> spd.get(511)
array(nan)
>>> spd.get(np.array([510, 520]))
array([ 49.67,  69.59])
interpolate(shape=SpectralShape(None, None, None), method=None)[source]

Interpolates the spectral power distribution following CIE 167:2005 recommendations: the method developed by Sprague (1880) should be used for interpolating functions having a uniformly spaced independent variable and a Cubic Spline method for non-uniformly spaced independent variable.

Parameters:
  • shape (SpectralShape, optional) – Spectral shape used for interpolation.
  • method (unicode, optional) – {None, ‘Cubic Spline’, ‘Linear’, ‘Pchip’, ‘Sprague’}, Enforce given interpolation method.
Returns:

Interpolated spectral power distribution.

Return type:

SpectralPowerDistribution

Raises:
  • RuntimeError – If Sprague (1880) interpolation method is forced with a non-uniformly spaced independent variable.
  • ValueError – If the interpolation method is not defined.

Notes

Warning

  • If scipy is not unavailable the Cubic Spline method will fallback to legacy Linear interpolation.
  • Cubic Spline interpolator requires at least 3 wavelengths \(\lambda_n\) for interpolation.
  • Linear interpolator requires at least 2 wavelengths \(\lambda_n\) for interpolation.
  • Pchip interpolator requires at least 2 wavelengths \(\lambda_n\) for interpolation.
  • Sprague (1880) interpolator requires at least 6 wavelengths \(\lambda_n\) for interpolation.

References

[4]CIE TC 1-38. (2005). 9. INTERPOLATION. In CIE 167:2005 Recommended Practice for Tabulating Spectral Data for Use in Colour Computations (pp. 14–19). ISBN:978-3-901-90641-1

Examples

Uniform data is using Sprague (1880) interpolation by default:

>>> data = {
...     510: 49.67,
...     520: 69.59,
...     530: 81.73,
...     540: 88.19,
...     550: 86.26,
...     560: 77.18}
>>> spd = SpectralPowerDistribution('Sample', data)
>>> print(spd.interpolate(SpectralShape(interval=1)))
SpectralPowerDistribution('Sample', (510.0, 560.0, 1.0))
>>> spd[515]  
array(60.3121800...)

Non uniform data is using Cubic Spline interpolation by default:

>>> spd = SpectralPowerDistribution('Sample', data)
>>> spd[511] = 31.41
>>> print(spd.interpolate(SpectralShape(interval=1)))
SpectralPowerDistribution('Sample', (510.0, 560.0, 1.0))
>>> spd[515]  
array(21.4792222...)

Enforcing Linear interpolation:

>>> spd = SpectralPowerDistribution('Sample', data)
>>> print(spd.interpolate(
...     SpectralShape(interval=1), method='Linear'))
SpectralPowerDistribution('Sample', (510.0, 560.0, 1.0))
>>> spd[515]  
array(59.63...)

Enforcing Pchip interpolation:

>>> spd = SpectralPowerDistribution('Sample', data)
>>> print(spd.interpolate(
...     SpectralShape(interval=1), method='Pchip'))
SpectralPowerDistribution('Sample', (510.0, 560.0, 1.0))
>>> spd[515]  
array(60.7204982...)
is_uniform()[source]

Returns if the spectral power distribution has uniformly spaced data.

Returns:Is uniform.
Return type:bool

Examples

>>> data = {510: 49.67, 520: 69.59, 530: 81.73, 540: 88.19}
>>> spd = SpectralPowerDistribution('Sample', data)
>>> spd.is_uniform()
True

Breaking the interval by introducing a new wavelength \(\lambda\) value:

>>> spd[511] = 3.1415
>>> spd.is_uniform()
False
items

Property for self.items attribute. This is a convenient attribute used to iterate over the spectral power distribution.

Returns:Spectral power distribution data.
Return type:ndarray
name

Property for self._name private attribute.

Returns:self._name.
Return type:unicode
normalise(factor=1)[source]

Normalises the spectral power distribution with given normalization factor.

Parameters:factor (numeric, optional) – Normalization factor
Returns:Normalised spectral power distribution.
Return type:SpectralPowerDistribution

Examples

>>> data = {510: 49.67, 520: 69.59, 530: 81.73, 540: 88.19}
>>> spd = SpectralPowerDistribution('Sample', data)
>>> print(spd.normalise())  
SpectralPowerDistribution('Sample', (510..., 540..., 10...))
>>> spd.values  
array([ 0.5632157...,  0.7890917...,  0.9267490...,  1.        ])
shape

Property for self.shape attribute.

Returns the shape of the spectral power distribution in the form of a SpectralShape class instance.

Returns:Spectral power distribution shape.
Return type:SpectralShape

Notes

  • A non uniform spectral power distribution may will have multiple different interval, in that case SpectralPowerDistribution.shape returns the minimum interval size.

Warning

SpectralPowerDistribution.shape is read only.

Examples

Uniform spectral power distribution:

>>> data = {510: 49.67, 520: 69.59, 530: 81.73, 540: 88.19}
>>> SpectralPowerDistribution(  
...     'Sample', data).shape
SpectralShape(510..., 540..., 10...)

Non uniform spectral power distribution:

>>> data = {512.3: 49.67, 524.5: 69.59, 532.4: 81.73, 545.7: 88.19}
>>> # Doctests ellipsis for Python 2.x compatibility.
>>> SpectralPowerDistribution(  
...     'Sample', data).shape
SpectralShape(512.3, 545.7, 7...)
title

Property for self._title private attribute.

Returns:self._title.
Return type:unicode
trim_wavelengths(shape)[source]

Trims the spectral power distribution wavelengths to given spectral shape.

Parameters:shape (SpectralShape) – Spectral shape used for trimming.
Returns:Trimed spectral power distribution.
Return type:SpectralPowerDistribution

Examples

>>> data = {
...     510: 49.67,
...     520: 69.59,
...     530: 81.73,
...     540: 88.19,
...     550: 86.26,
...     560: 77.18}
>>> spd = SpectralPowerDistribution('Sample', data)
>>> print(spd.trim_wavelengths(  
...     SpectralShape(520, 550, 10)))
SpectralPowerDistribution('Sample', (520.0, 550.0, 10.0))
>>> # Doctests skip for Python 2.x compatibility.
>>> spd.wavelengths  
array([ 520.,  530.,  540.,  550.])
values

Property for self.values attribute.

Returns:Spectral power distribution wavelengths \(\lambda_n\) values.
Return type:ndarray

Warning

SpectralPowerDistribution.values is read only.

wavelengths

Property for self.wavelengths attribute.

Returns:Spectral power distribution wavelengths \(\lambda_n\).
Return type:ndarray
zeros(shape=SpectralShape(None, None, None))[source]

Zeros fills the spectral power distribution: Missing values will be replaced with zeros to fit the defined range.

Parameters:shape (SpectralShape, optional) – Spectral shape used for zeros fill.
Returns:Zeros filled spectral power distribution.
Return type:SpectralPowerDistribution
Raises:RuntimeError – If the spectral power distribution cannot be zeros filled.

Examples

>>> data = {
...     510: 49.67,
...     520: 69.59,
...     530: 81.73,
...     540: 88.19,
...     550: 86.26,
...     560: 77.18}
>>> spd = SpectralPowerDistribution('Sample', data)
>>> print(spd.zeros(SpectralShape(505, 565, 1)))
SpectralPowerDistribution('Sample', (505.0, 565.0, 1.0))
>>> spd.values
array([  0.  ,   0.  ,   0.  ,   0.  ,   0.  ,  49.67,   0.  ,   0.  ,
         0.  ,   0.  ,   0.  ,   0.  ,   0.  ,   0.  ,   0.  ,  69.59,
         0.  ,   0.  ,   0.  ,   0.  ,   0.  ,   0.  ,   0.  ,   0.  ,
         0.  ,  81.73,   0.  ,   0.  ,   0.  ,   0.  ,   0.  ,   0.  ,
         0.  ,   0.  ,   0.  ,  88.19,   0.  ,   0.  ,   0.  ,   0.  ,
         0.  ,   0.  ,   0.  ,   0.  ,   0.  ,  86.26,   0.  ,   0.  ,
         0.  ,   0.  ,   0.  ,   0.  ,   0.  ,   0.  ,   0.  ,  77.18,
         0.  ,   0.  ,   0.  ,   0.  ,   0.  ])
class colour.colorimetry.spectrum.TriSpectralPowerDistribution(name, data, mapping, title=None, labels=None)[source]

Bases: object

Defines the base object for colour matching functions.

A compound of three SpectralPowerDistribution is used to store the underlying axis data.

Parameters:
  • name (unicode) – Tri-spectral power distribution name.
  • data (dict) – Tri-spectral power distribution data.
  • mapping (dict) – Tri-spectral power distribution attributes mapping.
  • title (unicode, optional) – Tri-spectral power distribution title for figures.
  • labels (dict, optional) – Tri-spectral power distribution axis labels mapping for figures.
name
mapping
data
title
labels
x
y
z
wavelengths
values
items
shape
__str__()[source]
__repr__()[source]
__hash__()[source]
__init__()[source]
__getitem__()[source]
__setitem__()[source]
__iter__()[source]
__contains__()[source]
__len__()[source]
__eq__()[source]
__ne__()[source]
__add__()[source]
__iadd__()[source]
__sub__()[source]
__isub__()[source]
__mul__()[source]
__imul__()[source]
__div__()[source]
__idiv__()[source]
__pow__()[source]
__ipow__()[source]
get()[source]
is_uniform()[source]
extrapolate()[source]
interpolate()[source]
align()[source]
trim_wavelengths()[source]
zeros()[source]
normalise()[source]
clone()[source]

Examples

>>> x_bar = {510: 49.67, 520: 69.59, 530: 81.73, 540: 88.19}
>>> y_bar = {510: 90.56, 520: 87.34, 530: 45.76, 540: 23.45}
>>> z_bar = {510: 12.43, 520: 23.15, 530: 67.98, 540: 90.28}
>>> data = {'x_bar': x_bar, 'y_bar': y_bar, 'z_bar': z_bar}
>>> mapping = {'x': 'x_bar', 'y': 'y_bar', 'z': 'z_bar'}
>>> tri_spd = TriSpectralPowerDistribution('Observer', data, mapping)
>>> # Doctests skip for Python 2.x compatibility.
>>> tri_spd.wavelengths  
array([510, 520, 530, 540])
>>> tri_spd.values
array([[ 49.67,  90.56,  12.43],
       [ 69.59,  87.34,  23.15],
       [ 81.73,  45.76,  67.98],
       [ 88.19,  23.45,  90.28]])
>>> # Doctests skip for Python 2.x compatibility.
>>> tri_spd.shape  
SpectralShape(510, 540, 10)
align(shape, interpolation_method=None, extrapolation_method=u'Constant', extrapolation_left=None, extrapolation_right=None)[source]

Aligns the tri-spectral power distribution to given shape: Interpolates first then extrapolates to fit the given range.

Parameters:
  • shape (SpectralShape) – Spectral shape used for alignment.
  • interpolation_method (unicode, optional) – {None, ‘Cubic Spline’, ‘Linear’, ‘Pchip’, ‘Sprague’}, Enforce given interpolation method.
  • extrapolation_method (unicode, optional) – {‘Constant’, ‘Linear’}, Extrapolation method.
  • extrapolation_left (numeric, optional) – Value to return for low extrapolation range.
  • extrapolation_right (numeric, optional) – Value to return for high extrapolation range.
Returns:

Aligned tri-spectral power distribution.

Return type:

TriSpectralPowerDistribution

Examples

>>> x_bar = {
...     510: 49.67,
...     520: 69.59,
...     530: 81.73,
...     540: 88.19,
...     550: 89.76,
...     560: 90.28}
>>> y_bar = {
...     510: 90.56,
...     520: 87.34,
...     530: 45.76,
...     540: 23.45,
...     550: 15.34,
...     560: 10.11}
>>> z_bar = {
...     510: 12.43,
...     520: 23.15,
...     530: 67.98,
...     540: 90.28,
...     550: 91.61,
...     560: 98.24}
>>> data = {'x_bar': x_bar, 'y_bar': y_bar, 'z_bar': z_bar}
>>> mapping = {'x': 'x_bar', 'y': 'y_bar', 'z': 'z_bar'}
>>> tri_spd = TriSpectralPowerDistribution('Observer', data, mapping)
>>> print(tri_spd.align(SpectralShape(505, 565, 1)))
TriSpectralPowerDistribution('Observer', (505.0, 565.0, 1.0))
>>> # Doctests skip for Python 2.x compatibility.
>>> tri_spd.wavelengths  
array([505, 506, 507, 508, 509, 510, 511, 512, 513, 514, 515, 516, 517,
       518, 519, 520, 521, 522, 523, 524, 525, 526, 527, 528, 529, 530,
       531, 532, 533, 534, 535, 536, 537, 538, 539, 540, 541, 542, 543,
       544, 545, 546, 547, 548, 549, 550, 551, 552, 553, 554, 555, 556,
       557, 558, 559, 560, 561, 562, 563, 564, 565])
>>> tri_spd.values  
array([[ 49.67     ...,  90.56     ...,  12.43     ...],
       [ 49.67     ...,  90.56     ...,  12.43     ...],
       [ 49.67     ...,  90.56     ...,  12.43     ...],
       [ 49.67     ...,  90.56     ...,  12.43     ...],
       [ 49.67     ...,  90.56     ...,  12.43     ...],
       [ 49.67     ...,  90.56     ...,  12.43     ...],
       [ 51.8325938...,  91.2994928...,  12.5377184...],
       [ 53.9841952...,  91.9502387...,  12.7233193...],
       [ 56.1205452...,  92.5395463...,  12.9651679...],
       [ 58.2315395...,  93.0150037...,  13.3123777...],
       [ 60.3033208...,  93.2716331...,  13.8605136...],
       [ 62.3203719...,  93.1790455...,  14.7272944...],
       [ 64.2676077...,  92.6085951...,  16.0282961...],
       [ 66.1324679...,  91.4605335...,  17.8526544...],
       [ 67.9070097...,  89.6911649...,  20.2387677...],
       [ 69.59     ...,  87.34     ...,  23.15     ...],
       [ 71.1837378...,  84.4868033...,  26.5150469...],
       [ 72.6800056...,  81.0666018...,  30.3964852...],
       [ 74.0753483...,  77.0766254...,  34.7958422...],
       [ 75.3740343...,  72.6153870...,  39.6178858...],
       [ 76.5856008...,  67.8490714...,  44.7026805...],
       [ 77.7223995...,  62.9779261...,  49.8576432...],
       [ 78.7971418...,  58.2026503...,  54.8895997...],
       [ 79.8204447...,  53.6907852...,  59.6368406...],
       [ 80.798376 ...,  49.5431036...,  64.0011777...],
       [ 81.73     ...,  45.76     ...,  67.98     ...],
       [ 82.6093606...,  42.2678534...,  71.6460893...],
       [ 83.439232 ...,  39.10608  ...,  74.976976 ...],
       [ 84.2220071...,  36.3063728...,  77.9450589...],
       [ 84.956896 ...,  33.85464  ...,  80.552    ...],
       [ 85.6410156...,  31.7051171...,  82.8203515...],
       [ 86.27048  ...,  29.79448  ...,  84.785184 ...],
       [ 86.8414901...,  28.0559565...,  86.4857131...],
       [ 87.351424 ...,  26.43344  ...,  87.956928 ...],
       [ 87.7999266...,  24.8956009...,  89.2212178...],
       [ 88.19     ...,  23.45     ...,  90.28     ...],
       [ 88.5265036...,  22.1424091...,  91.1039133...],
       [ 88.8090803...,  20.9945234...,  91.6538035...],
       [ 89.0393279...,  20.0021787...,  91.9333499...],
       [ 89.2222817...,  19.1473370...,  91.9858818...],
       [ 89.3652954...,  18.4028179...,  91.8811002...],
       [ 89.4769231...,  17.7370306...,  91.7018000...],
       [ 89.5657996...,  17.1187058...,  91.5305910...],
       [ 89.6395227...,  16.5216272...,  91.4366204...],
       [ 89.7035339...,  15.9293635...,  91.4622944...],
       [ 89.76     ...,  15.34     ...,  91.61     ...],
       [ 89.8094041...,  14.7659177...,  91.8528616...],
       [ 89.8578890...,  14.2129190...,  92.2091737...],
       [ 89.9096307...,  13.6795969...,  92.6929664...],
       [ 89.9652970...,  13.1613510...,  93.2988377...],
       [ 90.0232498...,  12.6519811...,  94.0078786...],
       [ 90.0807467...,  12.1452800...,  94.7935995...],
       [ 90.1351435...,  11.6366269...,  95.6278555...],
       [ 90.1850956...,  11.1245805...,  96.4867724...],
       [ 90.2317606...,  10.6124724...,  97.3566724...],
       [ 90.28     ...,  10.11     ...,  98.24     ...],
       [ 90.28     ...,  10.11     ...,  98.24     ...],
       [ 90.28     ...,  10.11     ...,  98.24     ...],
       [ 90.28     ...,  10.11     ...,  98.24     ...],
       [ 90.28     ...,  10.11     ...,  98.24     ...],
       [ 90.28     ...,  10.11     ...,  98.24     ...]])
clone()[source]

Clones the tri-spectral power distribution.

Most of the TriSpectralPowerDistribution class operations are conducted in-place. The TriSpectralPowerDistribution.clone() method provides a convenient way to copy the tri-spectral power distribution to a new object.

Returns:Cloned tri-spectral power distribution.
Return type:TriSpectralPowerDistribution

Examples

>>> x_bar = {
...     510: 49.67,
...     520: 69.59,
...     530: 81.73,
...     540: 88.19,
...     550: 89.76,
...     560: 90.28}
>>> y_bar = {
...     510: 90.56,
...     520: 87.34,
...     530: 45.76,
...     540: 23.45,
...     550: 15.34,
...     560: 10.11}
>>> z_bar = {
...     510: 12.43,
...     520: 23.15,
...     530: 67.98,
...     540: 90.28,
...     550: 91.61,
...     560: 98.24}
>>> data = {'x_bar': x_bar, 'y_bar': y_bar, 'z_bar': z_bar}
>>> mapping = {'x': 'x_bar', 'y': 'y_bar', 'z': 'z_bar'}
>>> tri_spd = TriSpectralPowerDistribution('Observer', data, mapping)
>>> print(tri_spd)  
TriSpectralPowerDistribution('Observer', (510..., 560..., 10...))
>>> tri_spd_clone = tri_spd.clone()
>>> print(tri_spd_clone)  
TriSpectralPowerDistribution('Observer (...)', (510..., 560..., 10...))
data

Property for self._data private attribute.

Returns:self._data.
Return type:dict
extrapolate(shape, method=u'Constant', left=None, right=None)[source]

Extrapolates the tri-spectral power distribution following CIE 15:2004 recommendation. [2] [3]

Parameters:
  • shape (SpectralShape) – Spectral shape used for extrapolation.
  • method (unicode, optional) – {‘Constant’, ‘Linear’}, Extrapolation method.
  • left (numeric, optional) – Value to return for low extrapolation range.
  • right (numeric, optional) – Value to return for high extrapolation range.
Returns:

Extrapolated tri-spectral power distribution.

Return type:

TriSpectralPowerDistribution

Examples

>>> x_bar = {510: 49.67, 520: 69.59, 530: 81.73, 540: 88.19}
>>> y_bar = {510: 90.56, 520: 87.34, 530: 45.76, 540: 23.45}
>>> z_bar = {510: 12.43, 520: 23.15, 530: 67.98, 540: 90.28}
>>> data = {'x_bar': x_bar, 'y_bar': y_bar, 'z_bar': z_bar}
>>> mapping = {'x': 'x_bar', 'y': 'y_bar', 'z': 'z_bar'}
>>> tri_spd = TriSpectralPowerDistribution('Observer', data, mapping)
>>> tri_spd.extrapolate(  
...     SpectralShape(400, 700)).shape
SpectralShape(400..., 700..., 10...)
>>> tri_spd[400]
array([ 49.67,  90.56,  12.43])
>>> tri_spd[700]
array([ 88.19,  23.45,  90.28])
get(wavelength, default=nan)[source]

Returns the values for given wavelength \(\lambda\).

Parameters:
  • wavelength (numeric or array_like) – Wavelength \(\lambda\) to retrieve the values.
  • default (nan, numeric or array_like, optional) – Wavelength \(\lambda\) default values.
Returns:

Wavelength \(\lambda\) values.

Return type:

numeric or array_like

Examples

>>> x_bar = {510: 49.67, 520: 69.59, 530: 81.73, 540: 88.19}
>>> y_bar = {510: 90.56, 520: 87.34, 530: 45.76, 540: 23.45}
>>> z_bar = {510: 12.43, 520: 23.15, 530: 67.98, 540: 90.28}
>>> data = {'x_bar': x_bar, 'y_bar': y_bar, 'z_bar': z_bar}
>>> mapping = {'x': 'x_bar', 'y': 'y_bar', 'z': 'z_bar'}
>>> tri_spd = TriSpectralPowerDistribution('Observer', data, mapping)
>>> tri_spd.get(510)
array([ 49.67,  90.56,  12.43])
>>> tri_spd.get(np.array([510, 520]))
array([[ 49.67,  90.56,  12.43],
       [ 69.59,  87.34,  23.15]])
>>> tri_spd.get(511)
array([ nan,  nan,  nan])
>>> tri_spd.get(np.array([510, 520]))
array([[ 49.67,  90.56,  12.43],
       [ 69.59,  87.34,  23.15]])
interpolate(shape=SpectralShape(None, None, None), method=None)[source]

Interpolates the tri-spectral power distribution following CIE 167:2005 recommendations: the method developed by Sprague (1880) should be used for interpolating functions having a uniformly spaced independent variable and a Cubic Spline method for non-uniformly spaced independent variable. [4]

Parameters:
  • shape (SpectralShape, optional) – Spectral shape used for interpolation.
  • method (unicode, optional) – {None, ‘Cubic Spline’, ‘Linear’, ‘Pchip’, ‘Sprague’}, Enforce given interpolation method.
Returns:

Interpolated tri-spectral power distribution.

Return type:

TriSpectralPowerDistribution

Notes

Warning

See SpectralPowerDistribution.interpolate() method warning section.

Examples

Uniform data is using Sprague (1880) interpolation by default:

>>> x_bar = {
...     510: 49.67,
...     520: 69.59,
...     530: 81.73,
...     540: 88.19,
...     550: 89.76,
...     560: 90.28}
>>> y_bar = {
...     510: 90.56,
...     520: 87.34,
...     530: 45.76,
...     540: 23.45,
...     550: 15.34,
...     560: 10.11}
>>> z_bar = {
...     510: 12.43,
...     520: 23.15,
...     530: 67.98,
...     540: 90.28,
...     550: 91.61,
...     560: 98.24}
>>> data = {'x_bar': x_bar, 'y_bar': y_bar, 'z_bar': z_bar}
>>> mapping = {'x': 'x_bar', 'y': 'y_bar', 'z': 'z_bar'}
>>> tri_spd = TriSpectralPowerDistribution('Observer', data, mapping)
>>> print(tri_spd.interpolate(SpectralShape(interval=1)))
TriSpectralPowerDistribution('Observer', (510.0, 560.0, 1.0))
>>> tri_spd[515]  
array([ 60.3033208...,  93.2716331...,  13.8605136...])

Non uniform data is using Cubic Spline interpolation by default:

>>> data = {'x_bar': x_bar, 'y_bar': y_bar, 'z_bar': z_bar}
>>> mapping = {'x': 'x_bar', 'y': 'y_bar', 'z': 'z_bar'}
>>> tri_spd = TriSpectralPowerDistribution('Observer', data, mapping)
>>> tri_spd[511] = np.array([31.41, 95.27, 15.06])
>>> print(tri_spd.interpolate(SpectralShape(interval=1)))
TriSpectralPowerDistribution('Observer', (510.0, 560.0, 1.0))
>>> tri_spd[515]  
array([  21.4710405...,  100.6430015...,   18.8165196...])

Enforcing Linear interpolation:

>>> data = {'x_bar': x_bar, 'y_bar': y_bar, 'z_bar': z_bar}
>>> mapping = {'x': 'x_bar', 'y': 'y_bar', 'z': 'z_bar'}
>>> tri_spd = TriSpectralPowerDistribution('Observer', data, mapping)
>>> print(tri_spd.interpolate(  
...     SpectralShape(interval=1), method='Linear'))
TriSpectralPowerDistribution('Observer', (510.0, 560.0, 1.0))
>>> tri_spd[515]  
array([ 59.63...,  88.95...,  17.79...])

Enforcing Pchip interpolation:

>>> data = {'x_bar': x_bar, 'y_bar': y_bar, 'z_bar': z_bar}
>>> mapping = {'x': 'x_bar', 'y': 'y_bar', 'z': 'z_bar'}
>>> tri_spd = TriSpectralPowerDistribution('Observer', data, mapping)
>>> print(tri_spd.interpolate(  
...     SpectralShape(interval=1), method='Pchip'))
TriSpectralPowerDistribution('Observer', (510.0, 560.0, 1.0))
>>> tri_spd[515]  
array([ 60.7204982...,  89.6971406...,  15.6271845...])
is_uniform()[source]

Returns if the tri-spectral power distribution has uniformly spaced data.

Returns:Is uniform.
Return type:bool

Examples

>>> x_bar = {510: 49.67, 520: 69.59, 530: 81.73, 540: 88.19}
>>> y_bar = {510: 90.56, 520: 87.34, 530: 45.76, 540: 23.45}
>>> z_bar = {510: 12.43, 520: 23.15, 530: 67.98, 540: 90.28}
>>> data = {'x_bar': x_bar, 'y_bar': y_bar, 'z_bar': z_bar}
>>> mapping = {'x': 'x_bar', 'y': 'y_bar', 'z': 'z_bar'}
>>> tri_spd = TriSpectralPowerDistribution('Observer', data, mapping)
>>> tri_spd.is_uniform()
True

Breaking the interval by introducing new wavelength \(\lambda\) values:

>>> tri_spd[511] = np.array([49.6700, 49.6700, 49.6700])
>>> tri_spd.is_uniform()
False
items

Property for self.items attribute. This is a convenient attribute used to iterate over the tri-spectral power distribution.

Returns:Tri-spectral power distribution data.
Return type:ndarray
labels

Property for self._labels private attribute.

Returns:self._labels.
Return type:dict
mapping

Property for self._mapping private attribute.

Returns:self._mapping.
Return type:dict
name

Property for self._name private attribute.

Returns:self._name.
Return type:unicode
normalise(factor=1)[source]

Normalises the tri-spectral power distribution with given normalization factor.

Parameters:factor (numeric, optional) – Normalization factor
Returns:Normalised tri- spectral power distribution.
Return type:TriSpectralPowerDistribution

Notes

  • The implementation uses the maximum value for all axis.

Examples

>>> x_bar = {
...     510: 49.67,
...     520: 69.59,
...     530: 81.73,
...     540: 88.19,
...     550: 89.76,
...     560: 90.28}
>>> y_bar = {
...     510: 90.56,
...     520: 87.34,
...     530: 45.76,
...     540: 23.45,
...     550: 15.34,
...     560: 10.11}
>>> z_bar = {
...     510: 12.43,
...     520: 23.15,
...     530: 67.98,
...     540: 90.28,
...     550: 91.61,
...     560: 98.24}
>>> data = {'x_bar': x_bar, 'y_bar': y_bar, 'z_bar': z_bar}
>>> mapping = {'x': 'x_bar', 'y': 'y_bar', 'z': 'z_bar'}
>>> tri_spd = TriSpectralPowerDistribution('Observer', data, mapping)
>>> print(tri_spd.normalise())  
TriSpectralPowerDistribution('Observer', (510..., 560..., 10...))
>>> tri_spd.values  
array([[ 0.5055985...,  0.9218241...,  0.1265268...],
       [ 0.7083672...,  0.8890472...,  0.2356473...],
       [ 0.8319421...,  0.4657980...,  0.6919788...],
       [ 0.8976995...,  0.2387011...,  0.9189739...],
       [ 0.9136807...,  0.1561482...,  0.9325122...],
       [ 0.9189739...,  0.1029112...,  1.       ...]])
shape

Property for self.shape attribute.

Returns the shape of the tri-spectral power distribution in the form of a SpectralShape class instance.

Returns:Tri-spectral power distribution shape.
Return type:SpectralShape

Warning

TriSpectralPowerDistribution.shape is read only.

Examples

>>> x_bar = {510: 49.67, 520: 69.59, 530: 81.73, 540: 88.19}
>>> y_bar = {510: 90.56, 520: 87.34, 530: 45.76, 540: 23.45}
>>> z_bar = {510: 12.43, 520: 23.15, 530: 67.98, 540: 90.28}
>>> data = {'x_bar': x_bar, 'y_bar': y_bar, 'z_bar': z_bar}
>>> mapping = {'x': 'x_bar', 'y': 'y_bar', 'z': 'z_bar'}
>>> tri_spd = TriSpectralPowerDistribution('Observer', data, mapping)
>>> tri_spd.shape  
SpectralShape(510..., 540..., 10...)
title

Property for self._title private attribute.

Returns:self._title.
Return type:unicode
trim_wavelengths(shape)[source]

Trims the tri-spectral power distribution wavelengths to given shape.

Parameters:shape (SpectralShape) – Spectral shape used for trimming.
Returns:Trimmed tri-spectral power distribution.
Return type:TriSpectralPowerDistribution

Examples

>>> x_bar = {
...     510: 49.67,
...     520: 69.59,
...     530: 81.73,
...     540: 88.19,
...     550: 89.76,
...     560: 90.28}
>>> y_bar = {
...     510: 90.56,
...     520: 87.34,
...     530: 45.76,
...     540: 23.45,
...     550: 15.34,
...     560: 10.11}
>>> z_bar = {
...     510: 12.43,
...     520: 23.15,
...     530: 67.98,
...     540: 90.28,
...     550: 91.61,
...     560: 98.24}
>>> data = {'x_bar': x_bar, 'y_bar': y_bar, 'z_bar': z_bar}
>>> mapping = {'x': 'x_bar', 'y': 'y_bar', 'z': 'z_bar'}
>>> tri_spd = TriSpectralPowerDistribution('Observer', data, mapping)
>>> # Doctests skip for Python 2.x compatibility.
>>> print(tri_spd.trim_wavelengths(  
...     SpectralShape(520, 550, 10)))
TriSpectralPowerDistribution('Observer', (520.0, 550.0, 10.0))
>>> tri_spd.wavelengths  
array([ 520.,  530.,  540.,  550.])
values

Property for self.values attribute.

Returns:Tri-spectral power distribution wavelengths \(\lambda_n\) values.
Return type:ndarray

Warning

TriSpectralPowerDistribution.values is read only.

wavelengths

Property for self.wavelengths attribute.

Returns:Tri-spectral power distribution wavelengths \(\lambda_n\).
Return type:ndarray
x

Property for self.x attribute.

Returns:Spectral power distribution for x axis.
Return type:SpectralPowerDistribution

Warning

TriSpectralPowerDistribution.x is read only.

y

Property for self.y attribute.

Returns:Spectral power distribution for y axis.
Return type:SpectralPowerDistribution

Warning

TriSpectralPowerDistribution.y is read only.

z

Property for self.z attribute.

Returns:Spectral power distribution for z axis.
Return type:SpectralPowerDistribution

Warning

TriSpectralPowerDistribution.z is read only.

zeros(shape=SpectralShape(None, None, None))[source]

Zeros fills the tri-spectral power distribution: Missing values will be replaced with zeros to fit the defined range.

Parameters:shape (SpectralShape, optional) – Spectral shape used for zeros fill.
Returns:Zeros filled tri-spectral power distribution.
Return type:TriSpectralPowerDistribution

Examples

>>> x_bar = {
...     510: 49.67,
...     520: 69.59,
...     530: 81.73,
...     540: 88.19,
...     550: 89.76,
...     560: 90.28}
>>> y_bar = {
...     510: 90.56,
...     520: 87.34,
...     530: 45.76,
...     540: 23.45,
...     550: 15.34,
...     560: 10.11}
>>> z_bar = {
...     510: 12.43,
...     520: 23.15,
...     530: 67.98,
...     540: 90.28,
...     550: 91.61,
...     560: 98.24}
>>> data = {'x_bar': x_bar, 'y_bar': y_bar, 'z_bar': z_bar}
>>> mapping = {'x': 'x_bar', 'y': 'y_bar', 'z': 'z_bar'}
>>> tri_spd = TriSpectralPowerDistribution('Observer', data, mapping)
>>> print(tri_spd.zeros(SpectralShape(505, 565, 1)))
TriSpectralPowerDistribution('Observer', (505.0, 565.0, 1.0))
>>> tri_spd.values
array([[  0.  ,   0.  ,   0.  ],
       [  0.  ,   0.  ,   0.  ],
       [  0.  ,   0.  ,   0.  ],
       [  0.  ,   0.  ,   0.  ],
       [  0.  ,   0.  ,   0.  ],
       [ 49.67,  90.56,  12.43],
       [  0.  ,   0.  ,   0.  ],
       [  0.  ,   0.  ,   0.  ],
       [  0.  ,   0.  ,   0.  ],
       [  0.  ,   0.  ,   0.  ],
       [  0.  ,   0.  ,   0.  ],
       [  0.  ,   0.  ,   0.  ],
       [  0.  ,   0.  ,   0.  ],
       [  0.  ,   0.  ,   0.  ],
       [  0.  ,   0.  ,   0.  ],
       [ 69.59,  87.34,  23.15],
       [  0.  ,   0.  ,   0.  ],
       [  0.  ,   0.  ,   0.  ],
       [  0.  ,   0.  ,   0.  ],
       [  0.  ,   0.  ,   0.  ],
       [  0.  ,   0.  ,   0.  ],
       [  0.  ,   0.  ,   0.  ],
       [  0.  ,   0.  ,   0.  ],
       [  0.  ,   0.  ,   0.  ],
       [  0.  ,   0.  ,   0.  ],
       [ 81.73,  45.76,  67.98],
       [  0.  ,   0.  ,   0.  ],
       [  0.  ,   0.  ,   0.  ],
       [  0.  ,   0.  ,   0.  ],
       [  0.  ,   0.  ,   0.  ],
       [  0.  ,   0.  ,   0.  ],
       [  0.  ,   0.  ,   0.  ],
       [  0.  ,   0.  ,   0.  ],
       [  0.  ,   0.  ,   0.  ],
       [  0.  ,   0.  ,   0.  ],
       [ 88.19,  23.45,  90.28],
       [  0.  ,   0.  ,   0.  ],
       [  0.  ,   0.  ,   0.  ],
       [  0.  ,   0.  ,   0.  ],
       [  0.  ,   0.  ,   0.  ],
       [  0.  ,   0.  ,   0.  ],
       [  0.  ,   0.  ,   0.  ],
       [  0.  ,   0.  ,   0.  ],
       [  0.  ,   0.  ,   0.  ],
       [  0.  ,   0.  ,   0.  ],
       [ 89.76,  15.34,  91.61],
       [  0.  ,   0.  ,   0.  ],
       [  0.  ,   0.  ,   0.  ],
       [  0.  ,   0.  ,   0.  ],
       [  0.  ,   0.  ,   0.  ],
       [  0.  ,   0.  ,   0.  ],
       [  0.  ,   0.  ,   0.  ],
       [  0.  ,   0.  ,   0.  ],
       [  0.  ,   0.  ,   0.  ],
       [  0.  ,   0.  ,   0.  ],
       [ 90.28,  10.11,  98.24],
       [  0.  ,   0.  ,   0.  ],
       [  0.  ,   0.  ,   0.  ],
       [  0.  ,   0.  ,   0.  ],
       [  0.  ,   0.  ,   0.  ],
       [  0.  ,   0.  ,   0.  ]])
colour.colorimetry.spectrum.DEFAULT_SPECTRAL_SHAPE = SpectralShape(360.0, 780.0, 1.0)

Default spectral shape using ASTM E308–15 practise shape.

DEFAULT_SPECTRAL_SHAPE : SpectralShape

colour.colorimetry.spectrum.constant_spd(k, shape=SpectralShape(360.0, 780.0, 1.0))[source]

Returns a spectral power distribution of given spectral shape filled with constant \(k\) values.

Parameters:
  • k (numeric) – Constant \(k\) to fill the spectral power distribution with.
  • shape (SpectralShape, optional) – Spectral shape used to create the spectral power distribution.
Returns:

Constant \(k\) to filled spectral power distribution.

Return type:

SpectralPowerDistribution

Notes

Examples

>>> spd = constant_spd(100)
>>> spd.shape
SpectralShape(360.0, 780.0, 1.0)
>>> spd[400]
array(100.0)
colour.colorimetry.spectrum.zeros_spd(shape=SpectralShape(360.0, 780.0, 1.0))[source]

Returns a spectral power distribution of given spectral shape filled with zeros.

Parameters:shape (SpectralShape, optional) – Spectral shape used to create the spectral power distribution.
Returns:Zeros filled spectral power distribution.
Return type:SpectralPowerDistribution

See also

constant_spd()

Notes

Examples

>>> spd = zeros_spd()
>>> spd.shape
SpectralShape(360.0, 780.0, 1.0)
>>> spd[400]
array(0.0)
colour.colorimetry.spectrum.ones_spd(shape=SpectralShape(360.0, 780.0, 1.0))[source]

Returns a spectral power distribution of given spectral shape filled with ones.

Parameters:shape (SpectralShape, optional) – Spectral shape used to create the spectral power distribution.
Returns:Ones filled spectral power distribution.
Return type:SpectralPowerDistribution

See also

constant_spd()

Notes

Examples

>>> spd = ones_spd()
>>> spd.shape
SpectralShape(360.0, 780.0, 1.0)
>>> spd[400]
array(1.0)