Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR: Implement support for slugs to "colour.utilities.CaseInsensitiveMapping" class. #993

Merged
merged 6 commits into from
Jun 18, 2022

Conversation

KelSolaar
Copy link
Member

@KelSolaar KelSolaar commented Jun 1, 2022

Summary

This PR implements support for slugs to colour.utilities.CaseInsensitiveMapping class as suggested in #991.

Two new methods have been added:

  • colour.utilities.CaseInsensitiveMapping.slugified_keys
  • colour.utilities.CaseInsensitiveMapping.slugified_items

They leverage the new colour.utilities.slugify definition copied from Django.

>>> import colour
>>> colour.RGB_COLOURSPACES['ALEXA Wide Gamut'].name
'ALEXA Wide Gamut'
>>> colour.RGB_COLOURSPACES['alexa-wide-gamut'].name
'ALEXA Wide Gamut'
>>> 'ALEXA Wide Gamut' in colour.RGB_COLOURSPACES
True
>>> 'alexa-wide-gamut' in colour.RGB_COLOURSPACES
True
>>> del colour.RGB_COLOURSPACES['alexa-wide-gamut']
>>> 'ALEXA Wide Gamut' in colour.RGB_COLOURSPACES
False
>>> list(colour.RGB_COLOURSPACES.slugified_keys())
['aces2065-1', 'acescc', 'acescct', 'acescg', 'acesproxy', 'adobe-rgb-1998', 'adobe-wide-gamut-rgb', 'apple-rgb', 'best-rgb', 'beta-rgb', 'blackmagic-wide-gamut', 'cie-rgb', 'cinema-gamut', 'colormatch-rgb', 'dcdm-xyz', 'dci-p3', 'dci-p3', 'dji-d-gamut', 'dragoncolor', 'dragoncolor2', 'davinci-wide-gamut', 'display-p3', 'don-rgb-4', 'eci-rgb-v2', 'erimm-rgb', 'ekta-space-ps-5', 'f-gamut', 'filmlight-e-gamut', 'itu-r-bt2020', 'itu-r-bt470-525', 'itu-r-bt470-625', 'itu-r-bt709', 'max-rgb', 'n-gamut', 'ntsc-1953', 'ntsc-1987', 'p3-d65', 'palsecam', 'prophoto-rgb', 'protune-native', 'redwidegamutrgb', 'redcolor', 'redcolor2', 'redcolor3', 'redcolor4', 'rimm-rgb', 'romm-rgb', 'russell-rgb', 's-gamut', 's-gamut3', 's-gamut3cine', 'smpte-240m', 'smpte-c', 'sharp-rgb', 'v-gamut', 'venice-s-gamut3', 'venice-s-gamut3cine', 'xtreme-rgb', 'srgb', 'aces', 'adobe1998', 'prophoto']
>>> list(colour.RGB_COLOURSPACES.slugified_items())[:5]
[('aces2065-1', RGB_Colourspace('ACES2065-1',
                [[  7.34700000e-01,   2.65300000e-01],
                 [  0.00000000e+00,   1.00000000e+00],
                 [  1.00000000e-04,  -7.70000000e-02]],
                [ 0.32168,  0.33767],
                'ACES',
                [[  9.52552396e-01,   0.00000000e+00,   9.36786000e-05],
                 [  3.43966450e-01,   7.28166097e-01,  -7.21325464e-02],
                 [  0.00000000e+00,   0.00000000e+00,   1.00882518e+00]],
                [[  1.04981102e+00,   0.00000000e+00,  -9.74845000e-05],
                 [ -4.95903023e-01,   1.37331305e+00,   9.82400361e-02],
                 [  0.00000000e+00,   0.00000000e+00,   9.91252018e-01]],
                linear_function,
                linear_function,
                False,
                False)), ('acescc', RGB_Colourspace('ACEScc',
                [[ 0.713,  0.293],
                 [ 0.165,  0.83 ],
                 [ 0.128,  0.044]],
                [ 0.32168,  0.33767],
                'ACES',
                [[ 0.66245418,  0.13400421,  0.15618769],
                 [ 0.27222872,  0.67408177,  0.05368952],
                 [-0.00557465,  0.00406073,  1.0103391 ]],
                [[ 1.64102338, -0.32480329, -0.2364247 ],
                 [-0.66366286,  1.61533159,  0.01675635],
                 [ 0.01172189, -0.00828444,  0.98839486]],
                log_encoding_ACEScc,
                log_decoding_ACEScc,
                False,
                False)), ('acescct', RGB_Colourspace('ACEScct',
                [[ 0.713,  0.293],
                 [ 0.165,  0.83 ],
                 [ 0.128,  0.044]],
                [ 0.32168,  0.33767],
                'ACES',
                [[ 0.66245418,  0.13400421,  0.15618769],
                 [ 0.27222872,  0.67408177,  0.05368952],
                 [-0.00557465,  0.00406073,  1.0103391 ]],
                [[ 1.64102338, -0.32480329, -0.2364247 ],
                 [-0.66366286,  1.61533159,  0.01675635],
                 [ 0.01172189, -0.00828444,  0.98839486]],
                log_encoding_ACEScct,
                log_decoding_ACEScct,
                False,
                False)), ('acescg', RGB_Colourspace('ACEScg',
                [[ 0.713,  0.293],
                 [ 0.165,  0.83 ],
                 [ 0.128,  0.044]],
                [ 0.32168,  0.33767],
                'ACES',
                [[ 0.66245418,  0.13400421,  0.15618769],
                 [ 0.27222872,  0.67408177,  0.05368952],
                 [-0.00557465,  0.00406073,  1.0103391 ]],
                [[ 1.64102338, -0.32480329, -0.2364247 ],
                 [-0.66366286,  1.61533159,  0.01675635],
                 [ 0.01172189, -0.00828444,  0.98839486]],
                linear_function,
                linear_function,
                False,
                False)), ('acesproxy', RGB_Colourspace('ACESproxy',
                [[ 0.713,  0.293],
                 [ 0.165,  0.83 ],
                 [ 0.128,  0.044]],
                [ 0.32168,  0.33767],
                'ACES',
                [[ 0.66245418,  0.13400421,  0.15618769],
                 [ 0.27222872,  0.67408177,  0.05368952],
                 [-0.00557465,  0.00406073,  1.0103391 ]],
                [[ 1.64102338, -0.32480329, -0.2364247 ],
                 [-0.66366286,  1.61533159,  0.01675635],
                 [ 0.01172189, -0.00828444,  0.98839486]],
                log_encoding_ACESproxy,
                log_decoding_ACESproxy,
                False,
                False))]

All the objects using the colour.utilities.CaseInsensitiveMapping class and its colour.utilities.LazyCaseInsensitiveMapping sub-class should now have the same capabilities:

>>> list(colour.MSDS_CMFS.slugified_keys())
['stockman-sharpe-2-degree-cone-fundamentals', 'stockman-sharpe-10-degree-cone-fundamentals', 'smith-pokorny-1975-normal-trichromats', 'wright-guild-1931-2-degree-rgb-cmfs', 'stiles-burch-1955-2-degree-rgb-cmfs', 'stiles-burch-1959-10-degree-rgb-cmfs', 'cie-1931-2-degree-standard-observer', 'cie-1964-10-degree-standard-observer', 'cie-2012-2-degree-standard-observer', 'cie-2012-10-degree-standard-observer', 'cie-2015-2-degree-standard-observer', 'cie-2015-10-degree-standard-observer', 'cie_2_1931', 'cie_10_1964']
>>> list(colour.SDS_ILLUMINANTS.slugified_keys())
['a', 'b', 'c', 'd50', 'd55', 'd60', 'd65', 'd75', 'e', 'fl1', 'fl2', 'fl3', 'fl4', 'fl5', 'fl6', 'fl7', 'fl8', 'fl9', 'fl10', 'fl11', 'fl12', 'fl31', 'fl32', 'fl33', 'fl34', 'fl35', 'fl36', 'fl37', 'fl38', 'fl39', 'fl310', 'fl311', 'fl312', 'fl313', 'fl314', 'fl315', 'hp1', 'hp2', 'hp3', 'hp4', 'hp5', 'led-b1', 'led-b2', 'led-b3', 'led-b4', 'led-b5', 'led-bh1', 'led-rgb1', 'led-v1', 'led-v2', 'id65', 'id50', 'iso-7589-photographic-daylight', 'iso-7589-sensitometric-daylight', 'iso-7589-studio-tungsten', 'iso-7589-sensitometric-studio-tungsten', 'iso-7589-photoflood', 'iso-7589-sensitometric-photoflood', 'iso-7589-sensitometric-printer']

Preflight

Code Style and Quality

  • Unit tests have been implemented and passed.
  • Mypy static checking has been run and passed.
  • Pre-commit hooks have been run and passed.
  • [N/A] New transformations have been added to the Automatic Colour Conversion Graph.
  • [N/A] New transformations have been exported to the relevant namespaces, e.g. colour, colour.models.

Documentation

  • New features are documented along with examples if relevant.
  • The documentation is Sphinx and numpydoc compliant.

@KelSolaar KelSolaar added this to the v0.4.2 milestone Jun 1, 2022
@coveralls
Copy link

coveralls commented Jun 1, 2022

Coverage Status

Coverage increased (+0.0004%) to 99.853% when pulling 4aa7de4 on feature/slugify into 09413da on develop.

@KelSolaar KelSolaar requested a review from zachlewis June 1, 2022 11:40
Copy link
Contributor

@zachlewis zachlewis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is perfect. Excellent choice of slugifiers, it seems to behave well with everything I throw at it. Thanks Thomas.

Now, maybe this is too much magic, but what would you think about making sluggified lookups delimiter-insensitive?

For example, slugify produces the following for variations of "Sony S-Log3 S-Gamut3.Cine":

{
 'Sony S-Log3 S-Gamut3.cine':  'sony-s-log3-s-gamut3cine',
 'SONY_SLOG3-SGAMUT3.CINE':    'sony_slog3-sgamut3cine',
 'sony_slog3_sgamut3cine':     'sony_slog3_sgamut3cine',
 'Sony: SLog3 / SGamut3.Cine': 'sony-slog3-sgamut3cine',
 'Sony: SLog3/SGamut3Cine':    'sony-slog3sgamut3cine',
 'Sony: S-Log3/S-Gamut3.Cine': 'sony-s-log3s-gamut3cine',
 'Sony/SLog3_SGamut3Cine':     'sonyslog3_sgamut3cine',
}

@KelSolaar
Copy link
Member Author

KelSolaar commented Jun 4, 2022

Not a bad idea, probably a bit dangerous but I guess we are reaching a point where CaseInsensitiveMapping is not appropriate name. There is also an increased risk of key collisions but it is probably not too bad.

Any idea for an alternative name?

@KelSolaar
Copy link
Member Author

KelSolaar commented Jun 4, 2022

Here are few ideas after wandering on thesaurus.com:

  • colour.utilities.BareMapping
  • colour.utilities.DecreasedMapping
  • colour.utilities.DestituteMapping
  • colour.utilities.FlatMapping
  • colour.utilities.InsensitiveMapping
  • colour.utilities.LiteralMapping
  • colour.utilities.PlainMapping
  • colour.utilities.ReducedMapping
  • colour.utilities.StrictMapping
  • colour.utilities.StrippedMapping

@zachlewis
Copy link
Contributor

With the exception of DCI-P3+, I think we’re probably okay in terms of name collisions, at least for colour’s purposes. I think the benefits of resolving very-similar-but-ever-so-slightly-different permutations of a string encoding very specific information outweigh the detriments of potential alias collisions. I can’t really imagine a case where “SGAMUT3CINE” and “S-Gamut3.Cine” intend to refer to different things.

As for the naming of the object… hmmm. Tricky. Off the top of my head… CaseDelimiterInsensitiveLookup, InsensitiveLookup, PermissiveLookup, FuzzyLookup, AliasedLookup?

Maybe CanonicalMapping?

@KelSolaar
Copy link
Member Author

Quite like CanonicalMapping: https://www.dictionary.com/browse/canonical

Mathematics. (of an equation, coordinate, etc.) in simplest or standard form.

@KelSolaar
Copy link
Member Author

image

:D

@KelSolaar KelSolaar force-pushed the feature/slugify branch 2 times, most recently from 620beb2 to 2bc02ac Compare June 11, 2022 23:51
@KelSolaar
Copy link
Member Author

@zachlewis : Changes are up!

Note that we have the expected issue with dcip3:

>>> list(colour.RGB_COLOURSPACES.canonical_keys())
['aces20651', 'acescc', 'acescct', 'acescg', 'acesproxy', 'adobergb1998', 'adobewidegamutrgb', 'applergb', 'bestrgb', 'betargb', 'blackmagicwidegamut', 'ciergb', 'cinemagamut', 'colormatchrgb', 'dcdmxyz', 'dcip3', 'dcip3', 'djidgamut', 'dragoncolor', 'dragoncolor2', 'davinciwidegamut', 'displayp3', 'donrgb4', 'ecirgbv2', 'erimmrgb', 'ektaspaceps5', 'fgamut', 'filmlightegamut', 'iturbt2020', 'iturbt470525', 'iturbt470625', 'iturbt709', 'maxrgb', 'ngamut', 'ntsc1953', 'ntsc1987', 'p3d65', 'palsecam', 'prophotorgb', 'protunenative', 'redwidegamutrgb', 'redcolor', 'redcolor2', 'redcolor3', 'redcolor4', 'rimmrgb', 'rommrgb', 'russellrgb', 'sgamut', 'sgamut3', 'sgamut3cine', 'smpte240m', 'smptec', 'sharprgb', 'vgamut', 'venicesgamut3', 'venicesgamut3cine', 'xtremergb', 'srgb', 'aces', 'adobe1998', 'prophoto']
>>> colour.RGB_COLOURSPACES['dcip3']
RGB_Colourspace('DCI-P3+',
                [[ 0.74,  0.27],
                 [ 0.22,  0.78],
                 [ 0.09, -0.09]],
                [ 0.314,  0.351],
                'DCI-P3',
                [[ 0.55907356,  0.24893595,  0.08657739],
                 [ 0.2039863 ,  0.88259109, -0.08657739],
                 [-0.00755505,  0.        ,  0.961971  ]],
                [[ 1.99040349, -0.56139586, -0.22966194],
                 [-0.45849279,  1.262346  ,  0.15487549],
                 [ 0.01563207, -0.00440904,  1.03772867]],
                functools.partial(<function gamma_function at 0x156105550>, exponent=0.3846153846153846),
                functools.partial(<function gamma_function at 0x156105550>, exponent=2.6),
                False,
                False)

I suppose we could rename it...

@KelSolaar KelSolaar force-pushed the feature/slugify branch 2 times, most recently from f9dbf64 to 7feae1b Compare June 12, 2022 02:36
@@ -127,7 +127,7 @@
"""

RGB_COLOURSPACE_DCI_P3_P: RGB_Colourspace = RGB_Colourspace(
"DCI-P3+",
"DCI-P3-P",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌

Copy link
Contributor

@zachlewis zachlewis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. This is lurvely.

@KelSolaar KelSolaar merged commit 7d501cf into develop Jun 18, 2022
@KelSolaar KelSolaar deleted the feature/slugify branch June 18, 2022 01:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants