This module provides a function and a class to compute multidimensional histograms.
Given some 3D data:
>>> import numpy as np
>>> shape = (10**7, 3)
>>> sample = np.random.random(shape) * 500
>>> weights = np.random.random((shape[0],))
Computing the histogram with Histogramnd :
>>> from silx.math import Histogramnd
>>> n_bins = 35
>>> ranges = [[40., 150.], [-130., 250.], [0., 505]]
>>> histo, w_histo, edges = Histogramnd(sample, n_bins=n_bins, histo_range=ranges, weights=weights)
Histogramnd can accumulate sets of data that don’t have the same coordinates :
>>> from silx.math import Histogramnd
>>> histo_obj = Histogramnd(sample, n_bins=n_bins, histo_range=ranges, weights=weights)
>>> sample_2 = np.random.random(shape) * 200
>>> weights_2 = np.random.random((shape[0],))
>>> histo_obj.accumulate(sample_2, weights=weights_2)
And then access the results:
>>> histo = histo_obj.histo
>>> weighted_histo = histo_obj.weighted_histo
or even:
>>> histo, w_histo, edges = histo_obj
In some situations we need to compute the weighted histogram of several sets of data (weights) that have the same coordinates (sample).
Again, some data (2 sets of weights) :
>>> import numpy as np
>>> shape = (10**7, 3)
>>> sample = np.random.random(shape) * 500
>>> weights_1 = np.random.random((shape[0],))
>>> weights_2 = np.random.random((shape[0],))
And getting the result with HistogramLut :
>>> from silx.math import HistogramndLut
>>> n_bins = 35
>>> ranges = [[40., 150.], [-130., 250.], [0., 505]]
>>> histo_lut = HistogramndLut(sample, ranges, n_bins)
First call, with weight_1 :
>>> histo_lut.accumulate(weights_1)
Second call, with weight_2 :
>>> histo_lut.accumulate(weights_2)
Retrieving the results (this is a copy of what’s actually stored in this instance) :
>>> histo = histo_lut.histo
>>> w_histo = histo_lut.weighted_histo
Note that the following code gives the same result, but the HistogramndLut instance does not store the accumulated weighted histogram.
First call with weights_1
>>> histo, w_histo = histo_lut.apply_lut(weights_1)
Second call with weights_2
>>> histo, w_histo = histo_lut.apply_lut(weights_2, histo=histo, weighted_histo=w_histo)
When computing an histogram the caller is asked to provide the histogram range along each coordinates (parameter histo_range). This parameter must be given a [N, 2] array where N is the number of dimensions of the histogram.
In other words, the caller must provide, for each dimension, the left edge of the first (leftmost) bin, and the right edge of the last (rightmost) bin.
E.g. : for a 1D sample, for a histo_range equal to [0, 10] and n_bins=4, the bins ranges will be :
Computes the multidimensional histogram of some data.
Parameters: |
|
---|
If necessary, results can be unpacked from an instance of Histogramnd : histogram, weighted histogram, bins edge.
Example :
histo, w_histo, edges = Histogramnd(sample, histo_range, n_bins, weights)
Computes the multidimensional histogram of some data and accumulates it into the histogram held by this instance of Histogramnd.
Parameters: |
|
---|
Histogram array, or None if this instance was initialized without <sample> and accumulate has not been called yet.
Note
this is a reference to the array store in this Histogramnd instance, use with caution.
Weighted Histogram, or None if this instance was initialized without <sample>, or no weights have been passed to __init__ nor accumulate.
Note
this is a reference to the array store in this Histogramnd instance, use with caution.
Bins edges, or None if this instance was initialized without <sample> and accumulate has not been called yet.
The HistogramndLut class allows you to bin data onto a regular grid. The use of HistogramndLut is interesting when several sets of data that share the same coordinates (sample) have to be mapped onto the same grid.
Parameters: |
|
---|
Histogram (a copy of it), or None if ~accumulate has not been called yet (or clear was just called). If copy is set to False then the actual reference to the array is returned (use with caution).
Weighted histogram (a copy of it), or None if ~accumulate has not been called yet (or clear was just called). If copy is set to False then the actual reference to the array is returned (use with caution).
Returns True if the rightmost bin in each dimension is close (i.e : values equal to the rightmost bin edge is included in the bin).
Computes the multidimensional histogram of some data and adds it to the current histogram stored by this instance. The results can be retrieved with the histo and weighted_histo properties.
Parameters: |
|
---|
Computes the multidimensional histogram of some data and returns the result (it is NOT added to the current histogram stored by this instance).
Parameters: |
|
---|