Commit 164e657b by Daniel Boeckenhoff

### tfields core added disjoint group and parts method for splitting and graph theory

parent 90b6d221
 ... ... @@ -719,7 +719,7 @@ class Tensors(AbstractNdarray): For circular (close into themself after ) coordinates at index assume segments and transform all values to segment number segment number Examples: >>> import tfields >>> import numpy as np ... ... @@ -743,8 +743,7 @@ class Tensors(AbstractNdarray): with self.tmp_transform(coordSys): # map all values to first segment self[:, coordinate] = \ (self[:, coordinate] - offset) % (periodicity / num_segments) + \ (self[:, coordinate] - offset) % (periodicity / num_segments) + \ offset + segment * periodicity / num_segments def equal(self, other, ... ... @@ -1298,7 +1297,7 @@ class TensorFields(Tensors): if weights is int: use field at index else: see Tensors._weights """ if isinstance(weights, int) : if isinstance(weights, int): weights = self.fields[weights] return super(TensorFields, self)._weights(weights, rigid=rigid) ... ... @@ -1680,15 +1679,60 @@ class TensorMaps(TensorFields): masks.append(~map_delete_mask) return masks def parts(self, *map_descriptions): """ Args: *map_descriptions (tuple): tuples of map_pos_idx (int): reference to map position used like: self.maps[map_pos_idx] map_indices_list (list of list of int): each int refers to index in a map. """ # raise ValueError(map_descriptions) parts = [] for map_description in map_descriptions: map_pos_idx, map_indices_list = map_description for map_indices in map_indices_list: obj = self.copy() map_indices = set(map_indices) # for speed up map_delete_mask = np.array( [True if i not in map_indices else False for i in range(len(self.maps[map_pos_idx]))]) obj.maps[map_pos_idx] = obj.maps[map_pos_idx][~map_delete_mask] obj = obj.cleaned(duplicates=False) parts.append(obj) return parts def disjoint_map(self, mp_idx): """ Find the disjoint sets of map = self.maps[mp_idx] Args: mp_idx (int): reference to map position used like: self.maps[mp_idx] Returns: map description(tuple): see self.parts Examples: >>> import tfields >>> a = tfields.TensorMaps([[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0]], ... maps=[[[0, 1, 2], [0, 2, 3]]]) >>> b = a.copy() >>> b[:, 0] += 2 >>> m = tfields.TensorMaps.merged(a, b) >>> mp_description = m.disjoint_map(0) >>> parts = m.parts(mp_description) >>> aa, ba = parts >>> assert aa.maps[0].equal(ba.maps[0]) >>> assert aa.equal(a) >>> assert ba.equal(b) """ maps_list = tfields.lib.sets.disjoint_group_indices(self.maps[mp_idx]) return (0, maps_list) if __name__ == '__main__': # pragma: no cover import doctest doctest.testmod() # doctest.run_docstring_examples(TensorFields.__getitem__, globals()) # doctest.run_docstring_examples(TensorMaps.__getitem__, globals()) # doctest.run_docstring_examples(TensorMaps.keep, globals()) # doctest.run_docstring_examples(TensorMaps.to_maps_masks, globals()) # doctest.run_docstring_examples(TensorMaps, globals()) # doctest.run_docstring_examples(TensorFields, globals()) # doctest.run_docstring_examples(AbstractNdarray.copy, globals()) # doctest.run_docstring_examples(TensorMaps.equal, globals())
 ... ... @@ -109,3 +109,5 @@ else: from . import stats from .stats import mode, median, mean from . import symbolics from . import sets from . import util