Skip to content

Commit

Permalink
Fix issue with "colour.SpectralShape.range" method returning an extra…
Browse files Browse the repository at this point in the history
… number in some conditions.

Closes #140.
  • Loading branch information
KelSolaar committed Sep 18, 2014
1 parent ba51f65 commit de8dbea
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 16 deletions.
40 changes: 24 additions & 16 deletions colour/colorimetry/spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,17 +256,17 @@ def __iter__(self):
--------
>>> shape = SpectralShape(0, 10, 1)
>>> for wavelength in shape: print(wavelength)
0
1
2
3
4
5
6
7
8
9
10
0.0
1.0
2.0
3.0
4.0
5.0
6.0
7.0
8.0
9.0
10.0
"""

return iter(self.range())
Expand Down Expand Up @@ -416,10 +416,18 @@ def range(self):
'"steps" attributes is not defined!'))

if self.__range is None:
# We want to include the *end* of interval value, in order to
# do that we add *steps* to *end*, however because of floating
# precision issues, we may in some instances get an extra number as
# mentioned in Numpy documentation:
# http://docs.scipy.org/doc/numpy/reference/generated/numpy.arange.html # noqa
# So that the interval is not exceeded we actually add 99.9% of
# *steps*.
# TODO: Check if the above approach is bullet proof.

self.__range = np.arange(self.__start,
self.__end + self.__steps,
self.__end + self.__steps * 0.999,
self.__steps)

return self.__range


Expand Down Expand Up @@ -3413,7 +3421,7 @@ def constant_spd(k,
--------
>>> spd = constant_spd(100)
>>> spd.shape
SpectralShape(360, 830, 1)
SpectralShape(360.0, 830.0, 1.0)
>>> spd[400]
100.0
"""
Expand Down Expand Up @@ -3453,7 +3461,7 @@ def zeros_spd(shape=DEFAULT_SPECTRAL_SHAPE):
--------
>>> spd = zeros_spd()
>>> spd.shape
SpectralShape(360, 830, 1)
SpectralShape(360.0, 830.0, 1.0)
>>> spd[400]
0.0
"""
Expand Down Expand Up @@ -3489,7 +3497,7 @@ def ones_spd(shape=DEFAULT_SPECTRAL_SHAPE):
--------
>>> spd = ones_spd()
>>> spd.shape
SpectralShape(360, 830, 1)
SpectralShape(360.0, 830.0, 1.0)
>>> spd[400]
1.0
"""
Expand Down
17 changes: 17 additions & 0 deletions colour/colorimetry/tests/tests_spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -1990,6 +1990,23 @@ def test_range(self):
[wavelength for wavelength in SpectralShape(0, 10, 0.1)],
np.arange(0, 10 + 0.1, 0.1))

# Dedicated test for potential extra number with non uniformly spaced
# variables.
self.assertEqual(
len(SpectralPowerDistribution(
'White',
{380.0000: 1.0000,
417.7778: 1.0000,
455.5556: 0.9999,
493.3333: 0.9993,
531.1111: 0.9992,
568.8889: 0.9998,
606.6667: 1.0000,
644.4444: 1.0000,
682.2222: 1.0000,
720.0000: 1.0000})),
10)


class TestSpectralPowerDistribution(unittest.TestCase):
"""
Expand Down

0 comments on commit de8dbea

Please sign in to comment.