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

Add map_values to Gradient #43

Merged
merged 6 commits into from
Jul 17, 2024
Merged

Add map_values to Gradient #43

merged 6 commits into from
Jul 17, 2024

Conversation

ekiefl
Copy link
Contributor

@ekiefl ekiefl commented Jul 16, 2024

Add map_values method to Gradient

Proposed solution to #41

Something that gave me pause: on one hand I want to "buy in" to the existing data structures and make the return type list[HexCode], but on the other hand I feel it's more practical to return the hex strings since I suspect any callers of this function are typically using it as a final step in their process with the library. I'm curious what you think @mezarque.

@ekiefl ekiefl requested a review from mezarque July 16, 2024 20:40
Copy link
Member

@keithchev keithchev left a comment

Choose a reason for hiding this comment

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

Just driving by and thought I'd drop some comments (I'll leave this for @mezarque to approve since it's related to the public-facing API).

arcadia_pycolor/gradient.py Outdated Show resolved Hide resolved
arcadia_pycolor/tests/test_gradients.py Show resolved Hide resolved
arcadia_pycolor/gradient.py Show resolved Hide resolved
Copy link
Member

@mezarque mezarque left a comment

Choose a reason for hiding this comment

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

Thanks for implementing this! Took a look and left some suggestions.

arcadia_pycolor/gradient.py Outdated Show resolved Hide resolved
return [mcolors.to_hex(cmap(0.5))] * len(values)

normalized_values = [(value - min_value) / (max_value - min_value) for value in values]
clamped_values = [max(0.0, min(1.0, value)) for value in normalized_values]
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if we should warn the user when values outside of the min/max range are provided, rather than clamping those values silently here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can only speak to my personal usage with colormaps, but if I'm touching the min and max values, it's usually because I'm interested in the middle of the color range, but far-reaching extrema values are decreasing contrast.

Like if your data is spread like this (each x a datapoint, each | the min/max value):

x                           x x    x x  x x x x x x x x                              x
|                                                                                    |
red                                  yellow                                      green

The bulk of the data is washed out. In comparison, by setting min/max inside the value range:

x                           x x    x x  x x x x x x x x                              x
                          |                             |
red                       red          yellow       green                        green

You get more contrast in the region you care about.

All this is to say that I personally would not want a warning that I'm doing this, because it would be very intentional.

Copy link
Member

Choose a reason for hiding this comment

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

I understand this use case a bit better, thanks! I do wonder if it would be good to use the matplotlib set_extremes functionality when windowing the data in this way, so that colors over and under the gradient are distinctly marked as out of range. Probably not relevant to your structure viewer use case, but in a heatmap or other context, it would be important to indicate so as to avoid misleading readers.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah good idea. By name, I thought that set_extremes would allow us to avoid normalizing the data altogether, by setting the extrema for the value range. But looking into the implementation, I now see it allows you to control the colors of those outside 0 to 1.

This could be useful if we wanted more fancy features and could be accomplished by propagating those args to the map_values call signature.

Either way, it's noteworthy that the default behavior for over and under is to clamp the color to the lowest/highest color. Here's a black to white cmap:

(Pdb) mcolors.to_hex(cmap(-1.0))
'#000000'
(Pdb) mcolors.to_hex(cmap(2.0))
'#ffffff'

That's what we we were manually doing. As a step towards implementing over/under/bad, the clamping op has been removed (0c1c8af).

arcadia_pycolor/gradient.py Outdated Show resolved Hide resolved
@ekiefl ekiefl requested a review from mezarque July 16, 2024 23:01
Copy link
Member

@mezarque mezarque left a comment

Choose a reason for hiding this comment

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

Looks good to me! Thanks for addressing those comments.

Before merging, I do wonder if we should get a sense of how often we want to cut new releases on GitHub and distributions to PyPI. @keithchev any thoughts? I imagine it would make Evan's life a little easier if this is included in the PyPI-distributed version, but we should be disciplined about what amount of change necessitates a minor or patch version bump.

@keithchev
Copy link
Member

Before merging, I do wonder if we should get a sense of how often we want to cut new releases on GitHub and distributions to PyPI. @keithchev any thoughts? I imagine it would make Evan's life a little easier if this is included in the PyPI-distributed version, but we should be disciplined about what amount of change necessitates a minor or patch version bump.

Yeah, if we end up with like version 0.5.200 we're probably doing something wrong. And of course we want to be disciplined about releasing breaking changes. But I think we can err on the side of leniency for minor versions for now, at least for new features like this one that may unblock or facilitate a user's work.

@mezarque
Copy link
Member

Sounds good! Feel free to merge @ekiefl. @keithchev would you mind distributing to PyPI?

@keithchev keithchev mentioned this pull request Jul 17, 2024
@ekiefl
Copy link
Contributor Author

ekiefl commented Jul 17, 2024

Merging now. Made a small change described here: #43 (comment)

@ekiefl ekiefl merged commit d4084e6 into main Jul 17, 2024
1 check passed
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