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

color_bins_layer breaks argument should take in iterables #1146

Closed
andy-esch opened this issue Nov 1, 2019 · 6 comments · Fixed by #1154
Closed

color_bins_layer breaks argument should take in iterables #1146

andy-esch opened this issue Nov 1, 2019 · 6 comments · Fixed by #1154
Assignees
Labels

Comments

@andy-esch
Copy link
Contributor

I tried passing a numpy array for the breaks argument of a color_bins_layer helper:

from cartoframes.viz.helpers import color_bins_layer

breaks = np.arange(
    0,
    np.log(counties_w_counts.properties_per_household.max()),
    np.log(counties_w_counts.properties_per_household.max()) / 7,
)

Map(
    [
        color_bins_layer(
            counties_w_counts[["geometry", "properties_per_household"]],
            "properties_per_household",
            stroke_color="transparent",
            palette="sunset",
            breaks=breaks,
        ),
        Layer("SELECT * FROM mamataakella.state_bounds", "color: opacity(black, 0.5)"),
    ],
    show_info=True,
    viewport={"zoom": 3.15, "lat": 39.051997, "lng": -99.333419},
)

And received the following error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-308-147ad3fa75fa> in <module>
     14             stroke_color="transparent",
     15             palette="sunset",
---> 16             breaks=breaks,
     17         ),
     18         Layer("SELECT * FROM mamataakella.state_bounds", "color: opacity(black, 0.5)"),

~/.local/share/virtualenvs/property_data-_sgn6fy1/lib/python3.7/site-packages/cartoframes/viz/helpers/color_bins_layer.py in color_bins_layer(source, value, title, method, bins, breaks, palette, size, opacity, stroke_color, stroke_width, description, footer, legend, popup, widget, animate)
     47         raise ValueError('Available methods are: "quantiles", "equal", "stdev".')
     48 
---> 49     func = 'buckets' if breaks else {
     50         'quantiles': 'globalQuantiles',
     51         'equal': 'globalEqIntervals',

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

My expectation is that breaks can be an ordered iterable (list, tuple, numpy array). It works fine with list and tuple, but the error above leads me to think that it's testing for whether it's a list or tuple (and not a string). Since numpy has such a common use in peoples workflows for calculating custom arrays, we should support it too. A the very least we need to improve the error message that it should be a list/tuple or one of the named strings.

@makella
Copy link
Contributor

makella commented Nov 4, 2019

@andy-esch I'm no expert in this area so forgive me if this is totally out of nowhere...

while reading this, remembered when I was working on the sdsc demo cartography that Giulia had used custom breaks as a numpy array and it worked:

Screen Shot 2019-11-04 at 10 55 31 AM

not sure this is totally related, but just wanted to post in case.

thanks!

@makella
Copy link
Contributor

makella commented Nov 4, 2019

I guess this goes to your point of converting to a list so CF accepts?

@andy-esch
Copy link
Contributor Author

Thanks for the response, @makella! Yeah, looks like she has the same issue of needing to convert it to a python list first.

@andy-esch
Copy link
Contributor Author

I had the same problem with range which is a common way to generate series:
Screen Shot 2019-11-04 at 1 13 07 PM

@makella
Copy link
Contributor

makella commented Nov 4, 2019

out of curiosity, did you try this without the method set because the logic with breaks is to overwrite a classification method if custom breaks are set. not sure this will help anything or if i understand how you are combining the two... but thought i'd ask!

@andy-esch
Copy link
Contributor Author

I used it because I wanted clear, evenly spaced values for the percentages. I could've tried method='equal' and bins=10 I guess but it felt easier to generate the bins I know I want ahead of time.

Just tried the way you suggest:
Screen Shot 2019-11-04 at 2 59 35 PM

More or less the same except for the < and > for the lower/upper values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants