Skip to content

Commit

Permalink
Update "colour.SpectralShape.range" method with stronger implementation.
Browse files Browse the repository at this point in the history
Closes #140.
  • Loading branch information
KelSolaar committed Sep 18, 2014
1 parent de8dbea commit 82c5aca
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 30 deletions.
28 changes: 15 additions & 13 deletions colour/colorimetry/spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
LinearInterpolator1d,
SplineInterpolator,
SpragueInterpolator)
from colour.utilities import is_string
from colour.utilities import is_string, warning

__author__ = 'Colour Developers'
__copyright__ = 'Copyright (C) 2013 - 2014 - Colour Developers'
Expand Down Expand Up @@ -416,18 +416,20 @@ 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 * 0.999,
self.__steps)
samples = round(
(self.__steps + self.__end - self.__start) / self.__steps)
self.__range, current_steps = np.linspace(self.__start,
self.__end,
samples,
retstep=True)

if current_steps != self.__steps:
self.__steps = current_steps
warning(('"{0}" shape could not be honored, using '
'"{1}"!').format((self.__start,
self.__end,
self.__steps),
self))
return self.__range


Expand Down
17 changes: 0 additions & 17 deletions colour/colorimetry/tests/tests_spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -1990,23 +1990,6 @@ 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 82c5aca

Please sign in to comment.