Source code for spm1d.geom._clusters.lst


'''
ClusterList class.
'''

# Copyright (C) 2026  Todd Pataky


from . wrapped import WrappedCluster


from ... util._serial import DictSerial


[docs] class ClusterList(list, DictSerial): ''' List of ``ClusterWithInference`` objects. ''' def _asshortstr(self): n = len(self) if n==0: s = '[]' else: o = 'objects' if n>1 else 'object' s = f'[{n} Cluster {o}]' return s @staticmethod def _from_h5group(group): from . cluster import Cluster nclusters = group.attrs['nclusters'] clusters = [Cluster._from_h5group(group[f'cluster{i}']) for i in range(nclusters)] return ClusterList( clusters ) @property def _min_extent(self): return min( [c.extent for c in self] )
[docs] def sort(self): ''' Clusters in domain order (unlike ``list.sort`` the list is unchanged). :Returns: - New **ClusterList** sorted by position :Example: >>> import numpy as np >>> import spm1d >>> z = np.zeros( 101 ) >>> z[10:20], z[60:70] = 5, 5 >>> clusters = spm1d.geom.assemble_clusters( z, 3.5 ) >>> print( len( clusters.sort() ) ) 2 ''' return self.__class__( sorted( self ) )
def _wrap(self): # Most null fields in a permutation PDF have no clusters. if len(self) < 2: return c0,c1 = self[0], self[-1] if c0._isLboundary and c1._isRboundary and (c0.sign==c1.sign): self[0] = WrappedCluster( c0, c1 ) self.pop(-1) def _write_to_h5group(self, group): group.attrs['nclusters'] = len(self) for i,c in enumerate(self): c._write_to_h5group( group.create_group(f'cluster{i}') )