question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add .bin_centers to BinnedStatisticResult (and possible API change)

See original GitHub issue

Is your feature request related to a problem? Please describe.

  1. I used binned_statistic and binned_statistic_2d quite a bit, but often need to get the bin centers out to plot (statistic) vs. (bin centers). So, in 1D, I always end up typing this:

    stat = binned_statistic(…) bin_ctr = 0.5 * (stat.bin_edges[1:] + stat.bin_edges[:-1])

  2. As a separate but related point, in 1D and DD, the bin edges can be retrieved with stat.bin_edges, but in 2D, the bin edges are provided separately as .x_edges and .y_edges. I think this makes the API less consistent, and would propose that BinnedStatistic2dResult should instead have .bin_edges.

Describe the solution you’d like

  1. I propose either adding a new attribute to the BinnedStatisticResult NamedTuple’s to store the bin centers, or promoting these into small classes and adding a property that computes the bin centers on the fly from the bin edges.

  2. I propose deprecating .x_edges and .y_edges in BinnedStatistic2dResult and moving to .bin_edges.

Thanks for reading!

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
mdhabercommented, May 18, 2022

Note that we already have _make_tuple_bunch, which allows us to convert the output to an object with as many fields as we want but still works like a tuple (with the original elements) if it is unpacked.

1reaction
rkerncommented, May 18, 2022

The result class could implement __len__() and __getitem__() to emulate the legacy tuple items while still adding more additional attributes that are only accessible as attributes.

class BinnedStatistic2dResult(object):
    def __init__(self, statistic, x_edge, y_edge, binnumber):
        self.statistic = statistic
        self.x_edge = x_edge
        self.y_edge = y_edge
        self.binnumber = binnumber
        self.bin_edges = [x_edge, y_edge]
    def __len__(self):
        return 4
    def __getitem__(self, i):
        return (self.statistic, self.x_edge, self.y_edge, self.binnumber)[i]

One other option for bin centers might be to add a utility function that converts bin edges to the (centers, widths) representation. After the discussion about bin centers on numpy/numpy#13194, it seems that the manual conversion is more of a burden for people than I would have expected.

Read more comments on GitHub >

github_iconTop Results From Across the Web

scipy.stats.binned_statistic — SciPy v1.9.3 Manual
This function allows the computation of the sum, mean, median, or other statistic of the values (or set of values) within each bin....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found