Skip to content

Base classes

BaseHypergrid

hypergrid.base.base_hypergrid.BaseHypergrid

Bases: ABC

Abstract interface for all Hypergrid implementations.

Subclasses MUST implement: fit, update, get_mass, get_edges. Optional capabilities (rebin_to, to_dense, to_sparse, to_vector, compare, sample, plot_*) are provided by mixing in the classes from hypergrid.mixin. BaseTensorHypergrid includes all mixins and is the recommended base.

Source code in hypergrid\base\base_hypergrid.py
class BaseHypergrid(ABC):
    """
    Abstract interface for all Hypergrid implementations.

    Subclasses MUST implement: fit, update, get_mass, get_edges.
    Optional capabilities (rebin_to, to_dense, to_sparse, to_vector, compare,
    sample, plot_*) are provided by mixing in the classes from hypergrid.mixin.
    BaseTensorHypergrid includes all mixins and is the recommended base.
    """

    def __init__(self, dim):
        self.dim = dim

    @abstractmethod
    def fit(self, data, weights=None):
        """Build histogram from scratch."""

    @abstractmethod
    def update(self, data, weights=None):
        """Accumulate new data into the existing histogram."""

    @abstractmethod
    def get_mass(self):
        """Return the histogram as {tuple_index: float}."""

    @abstractmethod
    def get_edges(self):
        """Return list of edge arrays, one per dimension."""
fit(data, weights=None) abstractmethod

Build histogram from scratch.

Source code in hypergrid\base\base_hypergrid.py
@abstractmethod
def fit(self, data, weights=None):
    """Build histogram from scratch."""
update(data, weights=None) abstractmethod

Accumulate new data into the existing histogram.

Source code in hypergrid\base\base_hypergrid.py
@abstractmethod
def update(self, data, weights=None):
    """Accumulate new data into the existing histogram."""
get_mass() abstractmethod

Return the histogram as {tuple_index: float}.

Source code in hypergrid\base\base_hypergrid.py
@abstractmethod
def get_mass(self):
    """Return the histogram as {tuple_index: float}."""
get_edges() abstractmethod

Return list of edge arrays, one per dimension.

Source code in hypergrid\base\base_hypergrid.py
@abstractmethod
def get_edges(self):
    """Return list of edge arrays, one per dimension."""

BaseTensorHypergrid

hypergrid.base._base_tensor.BaseTensorHypergrid

Bases: BaseHypergrid, RebinMixin, ComparisonMixin, EmbeddingMixin, VisualizationMixin, StatsMixin

Shared base for all fixed-edge hypergrid variants.

Provides: _get_bin_index, fit, update, get_edges. Subclasses must set self.storage in their init (after super().init) and implement get_mass().

Parameters:

Name Type Description Default
edges list of array-like or None

Bin edges per dimension. When None, subclasses (e.g. AdaptiveHypergrid) are responsible for setting self.edges, self.dim, self.shape later.

None
Source code in hypergrid\base\_base_tensor.py
class BaseTensorHypergrid(
    BaseHypergrid,
    RebinMixin,
    ComparisonMixin,
    EmbeddingMixin,
    VisualizationMixin,
    StatsMixin,
):
    """
    Shared base for all fixed-edge hypergrid variants.

    Provides: _get_bin_index, fit, update, get_edges.
    Subclasses must set self.storage in their __init__ (after super().__init__)
    and implement get_mass().

    Parameters
    ----------
    edges : list of array-like or None
        Bin edges per dimension. When None, subclasses (e.g. AdaptiveHypergrid)
        are responsible for setting self.edges, self.dim, self.shape later.
    """

    def __init__(self, edges=None):
        if edges is not None:
            edges = [np.asarray(e, dtype=float) for e in edges]
            super().__init__(dim=len(edges))
            self.edges = edges
            self.shape = [len(e) - 1 for e in edges]
        else:
            super().__init__(dim=None)
            self.edges = None
            self.shape = None

    def fit(self, data, weights=None):
        self.storage.clear()
        self.update(data, weights)

    def update(self, data, weights=None):
        data = np.asarray(data, dtype=float)
        if data.ndim == 1:
            data = data[np.newaxis, :]
        if weights is None:
            weights = np.ones(len(data))
        else:
            weights = np.asarray(weights, dtype=float)

        for point, w in zip(data, weights):
            idx = self._get_bin_index(point)
            if idx is not None:
                self.storage.add(idx, w)

    def _get_bin_index(self, point):
        idx = []
        for d in range(self.dim):
            e = self.edges[d]
            val = point[d]
            if val < e[0] or val >= e[-1]:
                return None
            idx.append(int(np.searchsorted(e, val, side="right")) - 1)
        return tuple(idx)

    def get_edges(self):
        return self.edges