zabgrab.services.binning

Counting values in bins (show --bin and plot --bin).

Bins are given by their edges, in increasing order. N edges make N + 1 bins: the first holds everything below the first edge and the last everything from the last edge up, and each bin includes its lower edge but not its upper one, as numpy.histogram’s bins do: with edges 10 and 100, 10 is in 10 to 100 and 100 in ≥ 100.

Functions

histograms(series, bins[, options])

Count each numeric series' values in bins.

Classes

Bins(edges)

Bins, by their edges.

Histogram(series, counts)

How many of a series' values fall in each bin.

class zabgrab.services.binning.Bins(edges)[source]

Bases: object

Bins, by their edges.

Parameters:

edges (tuple[float, ...])

edges

The edges, strictly increasing.

Type:

tuple of float

Examples

>>> bins = Bins.parse(["10,100", "1000"])
>>> bins.labels
['< 10', '10 to 100', '100 to 1000', '≥ 1000']
>>> [bins.index(value) for value in (5, 10, 99.9, 100, 5000)]
[0, 1, 1, 2, 3]
classmethod parse(texts)[source]

Read bin edges given on the command line.

Parameters:

texts (sequence of str) – Comma-separated numbers; each --bin given adds its edges.

Returns:

Bins – The bins.

Raises:

ValueError – If an edge isn’t a finite number, or the edges aren’t increasing.

Return type:

Bins

property labels: list[str]

Name the bins, e.g. < 10, 10 to 100 and ≥ 100.

index(value)[source]

Find the bin a value falls in (its lower edge counts as in it).

Parameters:

value (float)

Return type:

int

position(value)[source]

Place a value along the bins, drawn side by side one unit apart.

Bin i is centred on i, so the edge between bins i and i + 1 is at i + 0.5. A value inside a bin between two edges is placed in proportion between them. The first and last bins are open-ended, with no width to measure against, so values inside them go in their middle.

Parameters:

value (float) – The value.

Returns:

  • position (float) – Where the value goes.

  • exact (bool) – Whether the position is exact (False inside an open-ended bin).

Return type:

tuple[float, bool]

Examples

>>> bins = Bins((10, 100, 1000))
>>> [bins.position(value) for value in (10, 55, 1000, 5, 5000)]
[(0.5, True), (1.0, True), (2.5, True), (0, False), (3, False)]
class zabgrab.services.binning.Histogram(series, counts)[source]

Bases: object

How many of a series’ values fall in each bin.

Parameters:
series

The series.

Type:

Series

counts

The number of values in each bin.

Type:

list of int

property total: int

The number of values counted.

zabgrab.services.binning.histograms(series, bins, options=None)[source]

Count each numeric series’ values in bins.

Raw data’s values are counted, and hourly trends’ averages. Text items have no size, so they’re left out.

Parameters:
  • series (sequence of Series) – The data.

  • bins (Bins) – The bins.

  • options (ListingOptions, optional) – Filters to apply first (--grep, --above and --below).

Returns:

list of Histogram – One per numeric series, in the order given.

Return type:

list[Histogram]