def test_find_connected_sites_with_logic_flattened(self): am = self.net.get_adjacency_matrix(fmt='coo') a = topotools.find_connected_sites(bonds=[0, 2], am=am, logic='or', flatten=True) assert np.all(a == [0, 1, 3]) a = topotools.find_connected_sites(bonds=[0, 2], am=am, logic='and', flatten=True) assert np.all(a == []) a = topotools.find_connected_sites(bonds=[0, 2], am=am, logic='xor', flatten=True) assert np.all(a == [0, 3]) a = topotools.find_connected_sites(bonds=[0, 2], am=am, logic='xnor', flatten=True) assert np.all(a == [1]) with pytest.raises(Exception): topotools.find_neighbor_bonds(bonds=[0], am=am, logic='nor') with pytest.raises(Exception): topotools.find_neighbor_bonds(bonds=[0], am=am, logic='nand')
def test_find_connected_sites_from_lil_format(self): pn = op.network.Cubic(shape=[5, 1, 1], spacing=1e-4) am = pn.create_adjacency_matrix(weights=pn.Ts, fmt='lil') am = am.tocoo() Ps1 = topotools.find_connected_sites(bonds=[3], am=am, flatten=True) am = pn.create_adjacency_matrix(weights=pn.Ts, fmt='coo') Ps2 = topotools.find_connected_sites(bonds=[3], am=am, flatten=True) assert np.all(Ps1 == Ps2)
def test_find_connected_sites_unflattened(self): am = self.net.create_adjacency_matrix(weights=self.net.Ts, fmt='coo') Ps = topotools.find_connected_sites(bonds=[0, 5], am=am, flatten=False, logic="xnor") x = np.array([[np.nan, 1], [1, np.nan]]) assert ((x == Ps) | (np.isnan(x) & np.isnan(Ps))).all() Ps = topotools.find_connected_sites(bonds=[0, 1, 5], am=am, logic="and", flatten=False) assert np.allclose(Ps, [np.array([], dtype="int64")] * 3)
def find_connected_pores(self, throats=[], flatten=False, mode='union'): r""" Return a list of pores connected to the given list of throats Parameters ---------- throats : array_like List of throats numbers flatten : boolean, optional If ``True`` (default) a 1D array of unique pore numbers is returned. If ``False`` each location in the the returned array contains a sub-arras of neighboring pores for each input throat, in the order they were sent. mode : string Specifies logic to filter the resulting list. Options are: **'or'** : (default) All neighbors of the input throats. This is also known as the 'union' in set theory or 'any' in boolean logic. Both keywords are accepted and treated as 'or'. **'xor'** : Only neighbors of one and only one input throat. This is useful for finding the sites that are not shared by any of the input throats. **'xnor'** : Neighbors that are shared by two or more input throats. This is equivalent to finding all neighbors with 'or', minus those found with 'xor', and is useful for finding neighbors that the inputs have in common. **'and'** : Only neighbors shared by all input throats. This is also known as 'intersection' in set theory and (somtimes) as 'all' in boolean logic. Both keywords are accepted and treated as 'and'. Returns ------- 1D array (if ``flatten`` is ``True``) or ndarray of arrays (if ``flatten`` is ``False``) Examples -------- >>> import openpnm as op >>> pn = op.network.Cubic(shape=[5, 5, 5]) >>> Ps = pn.find_connected_pores(throats=[0, 1]) >>> print(Ps) [[0 1] [1 2]] >>> Ps = pn.find_connected_pores(throats=[0, 1], flatten=True) >>> print(Ps) [0 1 2] """ Ts = self._parse_indices(throats) am = self.get_adjacency_matrix(fmt='coo') pores = topotools.find_connected_sites(bonds=Ts, am=am, flatten=flatten, logic=mode) return pores
def test_find_connected_sites(self): am = self.net.create_adjacency_matrix(weights=self.net.Ts, fmt='coo') Ps = topotools.find_connected_sites(bonds=[0], am=am, flatten=True) assert np.all(Ps == [0, 1]) Ps = topotools.find_connected_sites(bonds=[1], am=am, flatten=True) assert np.all(Ps == [0, 3]) Ps = topotools.find_connected_sites(bonds=[2], am=am, flatten=True) assert np.all(Ps == [1, 3]) Ps = topotools.find_connected_sites(bonds=[3], am=am, flatten=True) assert np.all(Ps == [1, 2]) Ps = topotools.find_connected_sites(bonds=[4], am=am, flatten=True) assert np.all(Ps == [3, 4]) Ps = topotools.find_connected_sites(bonds=[5], am=am, flatten=True) assert np.all(Ps == [1, 4]) Ps = topotools.find_connected_sites(bonds=[6], am=am, flatten=True) assert np.all(Ps == [4, 5])
def test_find_connected_sites_single(self): am = self.net.create_adjacency_matrix(fmt='coo') Ps = topotools.find_connected_sites(bonds=0, am=am, flatten=True) assert np.all(Ps == [0, 1])
def test_find_connected_sites_fmt_not_coo(self): am = self.net.create_adjacency_matrix(fmt='csr') with pytest.raises(Exception): topotools.find_connected_sites(bonds=[0], am=am, flatten=True)