Source code for spm1d.geom._clusters.wrapped


'''
WrappedCluster class.
'''

# Copyright (C) 2026  Todd Pataky


import numpy as np
from . _base import _Cluster, _WithInference


class WrappedCluster( _Cluster ):
    '''
    Cluster wrapping around the ends of a circular domain.
    '''

    iswrapped       = True

    def __init__(self, c0, c1):
        self._InferenceClass = WrappedClusterWithInference
        self._c0             = c0
        self._c1             = c1
        self._Q              = c0._Q            # domain size (defined during right-boundary interpolation)
        self._x              = c0._x + c1._x    # post-interpolated
        self.z               = c0.z + c1.z      # post-interpolated
        self._u              = c0._u            # threshold
        self.sign            = c0.sign
        self._endpoints      = c1._endpoints[0], c0._endpoints[-1]  # pre-interpolation


    def _asarray(self, modular=False):
        x0,z0 = self._c0._asarray().T
        x1,z1 = self._c1._asarray().T
        x     = np.hstack( [x1, self._Q + x0] )
        z     = np.hstack( [z1, z0] )
        if modular:
            x = x % self._Q
        return np.vstack( [x,z] ).T

    @property
    def centroid(self):
        '''
        Cluster centroid ``(x, z)``.
        '''
        x,z   = self._asarray( modular=False ).T
        xc    = x.mean() % self._Q
        zc    = z.mean()
        return xc, zc

    @property
    def endpoints(self):
        '''
        Interpolated cluster start and end positions ``(x0, x1)`` in domain
        nodes.
        '''
        return self._c1._x[0], self._c0._x[-1]

    @property
    def _endpoints_preinterp(self):
        return self._c1._endpoints[0], self._c0._endpoints[1]

    @property
    def extent(self):
        '''
        Cluster width ``x1 - x0`` in domain nodes (1 for single-node clusters).
        '''
        return self._c0.extent + self._c1.extent

    @property
    def height(self):
        '''
        Least extreme cluster value (threshold when interpolated). Same as
        ``min``.
        '''
        return self.min

    @property
    def integral(self):
        '''
        Area between the statistic and threshold.
        '''
        return self._c0.integral + self._c1.integral

    @property
    def max(self):
        '''
        Most extreme cluster statistic value (minimum for negative clusters).
        '''
        return max(self.z) if (self.sign==1) else min(self.z)

    @property
    def min(self):
        '''
        Least extreme cluster statistic value (threshold when interpolated).
        '''
        return min(self.z) if (self.sign==1) else max(self.z)

    @property
    def nnodes(self):
        '''
        Number of cluster nodes (excluding interpolated end points).
        '''
        return self._c0.nnodes + self._c1.nnodes

    @property
    def _bbar(self):
        #  The longer of the two parts carries the cluster's label.
        b0, b1 = self._c0._bbar, self._c1._bbar
        return b1 if (b1[1] >= b0[1]) else b0

    @property
    def _bbars(self):
        return [ self._c1._bbar, self._c0._bbar ]

    def _write_to_h5group(self, group):
        group.attrs['iswrapped']   = True
        group.attrs['isinference'] = self._isinference
        self._c0._write_to_h5group( group.create_group('c0') )
        self._c1._write_to_h5group( group.create_group('c1') )

    @staticmethod
    def _from_h5group(group):
        from . cluster import Cluster, _replace_none_str
        c0, c1   = Cluster._from_h5group( group['c0'] ), Cluster._from_h5group( group['c1'] )
        cluster  = WrappedCluster( c0, c1 )
        if bool( group.attrs['isinference'] ):
            mtype    = _replace_none_str( group.attrs['metric_type'], str )
            metric   = _replace_none_str( group.attrs['metric'], float )
            p        = _replace_none_str( group.attrs['p'], float )
            cluster  = cluster._as_inference_cluster( metric, p, metric_type=mtype )
        return cluster


[docs] class WrappedClusterWithInference(_WithInference, WrappedCluster): ''' Wrapped cluster with inference results (same members as ``ClusterWithInference``). ''' def _write_to_h5group(self, group): from . cluster import _nonestr super()._write_to_h5group( group ) group.attrs['metric_type'] = _nonestr(self.metric_type) group.attrs['metric'] = _nonestr(self.metric) group.attrs['p'] = _nonestr(self.p)