• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

colour-science / colour-datasets / 7853010122

10 Feb 2024 05:53AM UTC coverage: 96.225%. Remained the same
7853010122

push

github

KelSolaar
Use `np.reshape` function instead of `.reshape` method.

2243 of 2331 relevant lines covered (96.22%)

0.96 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/colour_datasets/loaders/hung1995.py
1
"""
2
Constant Hue Loci Data - Hung and Berns (1995)
3
==============================================
4

5
Defines the objects implementing support for *Hung and Berns (1995)* *Constant
6
Hue Loci Data* dataset loading:
7

8
-   :class:`colour_datasets.loaders.DatasetLoader_Hung1995`
9
-   :func:`colour_datasets.loaders.build_Hung1995`
10

11
References
12
----------
13
-   :cite:`Hung1995` : Hung, P.-C., & Berns, R. S. (1995). Determination of
14
    constant Hue Loci for a CRT gamut and their predictions using color
15
    appearance spaces. Color Research & Application, 20(5), 285-295.
16
    doi:10.1002/col.5080200506
17
"""
18

19
from __future__ import annotations
1✔
20

21
import os
1✔
22
from collections import namedtuple
1✔
23

24
import numpy as np
1✔
25
from colour import CCS_ILLUMINANTS, xy_to_XYZ, xyY_to_XYZ
1✔
26
from colour.hints import Dict
1✔
27

28
from colour_datasets.loaders import AbstractDatasetLoader
1✔
29
from colour_datasets.records import datasets
1✔
30

31
__author__ = "Colour Developers"
1✔
32
__copyright__ = "Copyright 2019 Colour Developers"
1✔
33
__license__ = "BSD-3-Clause - https://opensource.org/licenses/BSD-3-Clause"
1✔
34
__maintainer__ = "Colour Developers"
1✔
35
__email__ = "colour-developers@colour-science.org"
1✔
36
__status__ = "Production"
1✔
37

38
__all__ = [
1✔
39
    "ConstantPerceivedHueColourMatches_Hung1995",
40
    "DatasetLoader_Hung1995",
41
    "build_Hung1995",
42
]
43

44

45
class ConstantPerceivedHueColourMatches_Hung1995(
1✔
46
    namedtuple(
47
        "ConstantPerceivedHueColourMatches_Hung1995",
48
        ("name", "XYZ_r", "XYZ_cr", "XYZ_ct", "metadata"),
49
    )
50
):
51
    """
52
    Define *Hung and Berns (1995)* *Constant Hue Loci Data*
53
    colour matches data for a given hue angle.
54

55
    Parameters
56
    ----------
57
    name
58
        *Hung and Berns (1995)* *Constant Hue Loci Data* hue angle or
59
        name.
60
    XYZ_r
61
        *CIE XYZ* tristimulus values of the reference illuminant.
62
    XYZ_cr
63
        *CIE XYZ* tristimulus values of the reference colour under the
64
        reference illuminant.
65
    XYZ_ct
66
        *CIE XYZ* tristimulus values of the colour matches under the reference
67
        illuminant.
68
    metadata
69
        Dataset metadata.
70
    """
71

72

73
class DatasetLoader_Hung1995(AbstractDatasetLoader):
1✔
74
    """
75
    Define the *Hung and Berns (1995)* *Constant Hue Loci Data*
76
    dataset loader.
77

78
    Attributes
79
    ----------
80
    -   :attr:`colour_datasets.loaders.DatasetLoader_Hung1995.ID`
81

82
    Methods
83
    -------
84
    -   :meth:`colour_datasets.loaders.DatasetLoader_Hung1995.__init__`
85
    -   :meth:`colour_datasets.loaders.DatasetLoader_Hung1995.load`
86

87
    References
88
    ----------
89
    :cite:`Hung1995`
90
    """
91

92
    ID: str = "3367463"
1✔
93
    """Dataset record id, i.e. the *Zenodo* record number."""
1✔
94

95
    def __init__(self) -> None:
1✔
96
        super().__init__(datasets()[DatasetLoader_Hung1995.ID])
1✔
97

98
    def load(
1✔
99
        self,
100
    ) -> Dict[str, Dict[str, ConstantPerceivedHueColourMatches_Hung1995]]:
101
        """
102
        Sync, parse, convert and return the *Hung and Berns (1995)*
103
        *Constant Hue Loci Data* dataset content.
104

105
        Returns
106
        -------
107
        :class:`dict`
108
            *Hung and Berns (1995)* *Constant Hue Loci Data* dataset content.
109

110
        Examples
111
        --------
112
        >>> from colour_datasets.utilities import suppress_stdout
113
        >>> dataset = DatasetLoader_Hung1995()
114
        >>> with suppress_stdout():
115
        ...     dataset.load()
116
        >>> len(dataset.content.keys())
117
        6
118
        """
119

120
        super().sync()
1✔
121

122
        self._content = {}
1✔
123

124
        filenames = {
1✔
125
            "Table I.csv": "Reference colors.",
126
            "Table II.csv": "Intra- and interobserver variances for each "
127
            "reference hue expressed in circumferential "
128
            "hue-angle difference.",
129
            "Table III.csv": "Weight-averaged constant hue loci for the CL "
130
            "experiment.",
131
            "Table IV.csv": "Weight-averaged constant hue loci for the VL "
132
            "experiment.",
133
        }
134

135
        for filename in filenames:
1✔
136
            datafile_path = os.path.join(self.record.repository, "dataset", filename)
1✔
137

138
            self._content[filename.split(".")[0]] = np.genfromtxt(
1✔
139
                datafile_path,
140
                delimiter=",",
141
                names=True,
142
                dtype=None,
143
                encoding="utf-8",
144
            )
145

146
        hues = [
1✔
147
            "Red",
148
            "Red-yellow",
149
            "Yellow",
150
            "Yellow-green",
151
            "Green",
152
            "Green-cyan",
153
            "Cyan",
154
            "Cyan-blue",
155
            "Blue",
156
            "Blue-magenta",
157
            "Magenta",
158
            "Magenta-red",
159
        ]
160

161
        XYZ_r = xy_to_XYZ(CCS_ILLUMINANTS["CIE 1931 2 Degree Standard Observer"]["C"])
1✔
162

163
        for table, experiment in [("Table III", "CL"), ("Table IV", "VL")]:
1✔
164
            key = f"Constant Hue Loci Data - {experiment}"
1✔
165
            self._content[key] = {}
1✔
166
            for hue in hues:
1✔
167
                for sample_r in self._content["Table I"]:
1✔
168
                    sample_r = sample_r.tolist()  # noqa: PLW2901
1✔
169
                    if sample_r[0] == hue:
1✔
170
                        XYZ_cr = xyY_to_XYZ(sample_r[1:4]) / 100
1✔
171
                        break
1✔
172

173
                XYZ_ct = []
1✔
174
                metadata: Dict = {
1✔
175
                    "Color name": [],
176
                    "C*uv": [],
177
                }
178
                for sample_t in self._content[table]:
1✔
179
                    sample_t = sample_t.tolist()  # noqa: PLW2901
1✔
180
                    if sample_t[0] != hue:
1✔
181
                        continue
1✔
182

183
                    XYZ_ct.append(sample_t[2:])
1✔
184
                    metadata["Color name"].append(sample_t[0])
1✔
185
                    metadata["C*uv"].append(sample_t[1])
1✔
186

187
                self._content[key][hue] = ConstantPerceivedHueColourMatches_Hung1995(
1✔
188
                    hue, XYZ_r, XYZ_cr, np.vstack(XYZ_ct) / 100, metadata
189
                )
190

191
        return self._content
1✔
192

193

194
_DATASET_LOADER_HUNG1995: DatasetLoader_Hung1995 | None = None
1✔
195
"""
1✔
196
Singleton instance of the *Hung and Berns (1995)*
197
*Constant Hue Loci Data* dataset loader.
198
"""
199

200

201
def build_Hung1995(load: bool = True) -> DatasetLoader_Hung1995:
1✔
202
    """
203
    Singleton factory that builds the *Hung and Berns (1995)*
204
    *Constant Hue Loci Data* dataset loader.
205

206
    Parameters
207
    ----------
208
    load
209
        Whether to load the dataset upon instantiation.
210

211
    Returns
212
    -------
213
    :class:`colour_datasets.loaders.DatasetLoader_Hung1995`
214
        Singleton instance of the *Hung and Berns (1995)*
215
        *Constant Hue Loci Data* dataset loader.
216

217
    References
218
    ----------
219
    :cite:`Hung1995`
220
    """
221

222
    global _DATASET_LOADER_HUNG1995  # noqa: PLW0603
223

224
    if _DATASET_LOADER_HUNG1995 is None:
1✔
225
        _DATASET_LOADER_HUNG1995 = DatasetLoader_Hung1995()
1✔
226
        if load:
1✔
227
            _DATASET_LOADER_HUNG1995.load()
1✔
228

229
    return _DATASET_LOADER_HUNG1995
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc