Coverage for colour/io/tests/test_tabular.py: 100%
58 statements
« prev ^ index » next coverage.py v7.11.0, created at 2025-11-15 19:01 +1300
« prev ^ index » next coverage.py v7.11.0, created at 2025-11-15 19:01 +1300
1"""Define the unit tests for the :mod:`colour.io.tabular` module."""
3from __future__ import annotations
5import os
6import shutil
7import tempfile
9import pytest
11from colour.colorimetry import SpectralDistribution, SpectralShape
12from colour.io import (
13 read_sds_from_csv_file,
14 read_spectral_data_from_csv_file,
15 write_sds_to_csv_file,
16)
18__author__ = "Colour Developers"
19__copyright__ = "Copyright 2013 Colour Developers"
20__license__ = "BSD-3-Clause - https://opensource.org/licenses/BSD-3-Clause"
21__maintainer__ = "Colour Developers"
22__email__ = "colour-developers@colour-science.org"
23__status__ = "Production"
25__all__ = [
26 "ROOT_RESOURCES",
27 "COLOURCHECKER_N_OHTA_1",
28 "TestReadSpectralDataFromCsvFile",
29 "TestReadSdsFromCsvFile",
30 "TestWriteSdsToCsvFile",
31]
33ROOT_RESOURCES: str = os.path.join(os.path.dirname(__file__), "resources")
35COLOURCHECKER_N_OHTA_1: dict = {
36 380.0: 0.048,
37 385.0: 0.051,
38 390.0: 0.055,
39 395.0: 0.060,
40 400.0: 0.065,
41 405.0: 0.068,
42 410.0: 0.068,
43 415.0: 0.067,
44 420.0: 0.064,
45 425.0: 0.062,
46 430.0: 0.059,
47 435.0: 0.057,
48 440.0: 0.055,
49 445.0: 0.054,
50 450.0: 0.053,
51 455.0: 0.053,
52 460.0: 0.052,
53 465.0: 0.052,
54 470.0: 0.052,
55 475.0: 0.053,
56 480.0: 0.054,
57 485.0: 0.055,
58 490.0: 0.057,
59 495.0: 0.059,
60 500.0: 0.061,
61 505.0: 0.062,
62 510.0: 0.065,
63 515.0: 0.067,
64 520.0: 0.070,
65 525.0: 0.072,
66 530.0: 0.074,
67 535.0: 0.075,
68 540.0: 0.076,
69 545.0: 0.078,
70 550.0: 0.079,
71 555.0: 0.082,
72 560.0: 0.087,
73 565.0: 0.092,
74 570.0: 0.100,
75 575.0: 0.107,
76 580.0: 0.115,
77 585.0: 0.122,
78 590.0: 0.129,
79 595.0: 0.134,
80 600.0: 0.138,
81 605.0: 0.142,
82 610.0: 0.146,
83 615.0: 0.150,
84 620.0: 0.154,
85 625.0: 0.158,
86 630.0: 0.163,
87 635.0: 0.167,
88 640.0: 0.173,
89 645.0: 0.180,
90 650.0: 0.188,
91 655.0: 0.196,
92 660.0: 0.204,
93 665.0: 0.213,
94 670.0: 0.222,
95 675.0: 0.231,
96 680.0: 0.242,
97 685.0: 0.251,
98 690.0: 0.261,
99 695.0: 0.271,
100 700.0: 0.282,
101 705.0: 0.294,
102 710.0: 0.305,
103 715.0: 0.318,
104 720.0: 0.334,
105 725.0: 0.354,
106 730.0: 0.372,
107 735.0: 0.392,
108 740.0: 0.409,
109 745.0: 0.420,
110 750.0: 0.436,
111 755.0: 0.450,
112 760.0: 0.462,
113 765.0: 0.465,
114 770.0: 0.448,
115 775.0: 0.432,
116 780.0: 0.421,
117}
120class TestReadSpectralDataFromCsvFile:
121 """
122 Define :func:`colour.io.tabular.read_spectral_data_from_csv_file`
123 definition unit tests methods.
124 """
126 def test_read_spectral_data_from_csv_file(self) -> None:
127 """
128 Test :func:`colour.io.tabular.read_spectral_data_from_csv_file`
129 definition.
130 """
132 colour_checker_n_ohta = os.path.join(ROOT_RESOURCES, "colorchecker_n_ohta.csv")
133 data = read_spectral_data_from_csv_file(colour_checker_n_ohta)
134 assert list(data.keys()) == ["wavelength"] + [str(x) for x in range(1, 25)]
135 assert (
136 dict(zip(data["wavelength"], data["1"], strict=True))
137 == COLOURCHECKER_N_OHTA_1
138 )
140 colour_checker_n_ohta_transposed = os.path.join(
141 ROOT_RESOURCES, "colorchecker_n_ohta_transposed.csv"
142 )
143 data = read_spectral_data_from_csv_file(
144 colour_checker_n_ohta_transposed, transpose=True, delimiter="\t"
145 )
146 assert list(data.keys()) == ["wavelength"] + [str(x) for x in range(1, 25)]
147 assert (
148 dict(zip(data["wavelength"], data["1"], strict=True))
149 == COLOURCHECKER_N_OHTA_1
150 )
152 linss2_10e_5 = os.path.join(ROOT_RESOURCES, "linss2_10e_5.csv")
153 data = read_spectral_data_from_csv_file(
154 linss2_10e_5,
155 names=["wavelength", "l_bar", "m_bar", "s_bar"],
156 filling_values=0,
157 )
158 assert list(data.keys()) == ["wavelength", "l_bar", "m_bar", "s_bar"]
159 assert data["s_bar"][77] == 0
160 data = read_spectral_data_from_csv_file(
161 linss2_10e_5,
162 names=["wavelength", "l_bar", "m_bar", "s_bar"],
163 filling_values=-1,
164 )
165 assert data["s_bar"][77] == -1
168class TestReadSdsFromCsvFile:
169 """
170 Define :func:`colour.io.tabular.read_sds_from_csv_file` definition unit
171 tests methods.
172 """
174 def test_read_sds_from_csv_file(self) -> None:
175 """Test :func:`colour.io.tabular.read_sds_from_csv_file` definition."""
177 colour_checker_n_ohta = os.path.join(ROOT_RESOURCES, "colorchecker_n_ohta.csv")
178 sds = read_sds_from_csv_file(colour_checker_n_ohta)
179 for sd in sds.values():
180 assert isinstance(sd, SpectralDistribution)
182 assert sds["1"] == SpectralDistribution(COLOURCHECKER_N_OHTA_1, name="1")
185class TestWriteSdsToCsvFile:
186 """
187 Define :func:`colour.io.tabular.write_sds_to_csv_file` definition unit
188 tests methods.
189 """
191 def setup_method(self) -> None:
192 """Initialise the common tests attributes."""
194 self._temporary_directory = tempfile.mkdtemp()
196 def teardown_method(self) -> None:
197 """After tests actions."""
199 shutil.rmtree(self._temporary_directory)
201 def test_write_sds_to_csv_file(self) -> None:
202 """Test :func:`colour.io.tabular.write_sds_to_csv_file` definition."""
204 colour_checker_n_ohta = os.path.join(ROOT_RESOURCES, "colorchecker_n_ohta.csv")
205 sds = read_sds_from_csv_file(colour_checker_n_ohta)
206 colour_checker_n_ohta_test = os.path.join(
207 self._temporary_directory, "colorchecker_n_ohta.csv"
208 )
209 write_sds_to_csv_file(sds, colour_checker_n_ohta_test)
210 sds_test = read_sds_from_csv_file(colour_checker_n_ohta_test)
211 for key, value in sds.items():
212 assert value == sds_test[key]
214 def test_raise_exception_write_sds_to_csv_file(self) -> None:
215 """
216 Test :func:`colour.io.tabular.write_sds_to_csv_file` definition
217 raised exception.
218 """
220 colour_checker_n_ohta = os.path.join(ROOT_RESOURCES, "colorchecker_n_ohta.csv")
221 sds = read_sds_from_csv_file(colour_checker_n_ohta)
222 key = next(iter(sds.keys()))
223 sds[key] = sds[key].align(SpectralShape(400, 700, 10))
225 pytest.raises(ValueError, write_sds_to_csv_file, sds, "")