Ejemplo n.º 1
0
 def test_find_neighbor_sites_with_bad_logic(self):
     with pytest.raises(Exception):
         am = self.net.create_adjacency_matrix(fmt='lil')
         topotools.find_neighbor_sites(sites=[0, 1],
                                       am=am,
                                       flatten=True,
                                       logic='foobar')
Ejemplo n.º 2
0
 def test_find_neighbor_sites_include_inputs(self):
     am = self.net.create_adjacency_matrix(fmt='lil')
     Ps = topotools.find_neighbor_sites(sites=[0, 1],
                                        am=am,
                                        flatten=True,
                                        logic='or',
                                        include_input=True)
     assert (Ps == [0, 1, 2, 3, 4]).all()
     Ps = topotools.find_neighbor_sites(sites=[0, 1],
                                        am=am,
                                        flatten=True,
                                        logic='or',
                                        include_input=False)
     assert (Ps == [2, 3, 4]).all()
Ejemplo n.º 3
0
 def test_find_neighbor_sites_flattened_with_logic(self):
     am = self.net.create_adjacency_matrix(fmt='lil')
     Ps = topotools.find_neighbor_sites(sites=[0, 1], am=am, flatten=True,
                                        logic='or')
     assert np.all(Ps == [2, 3, 4])
     Ps = topotools.find_neighbor_sites(sites=[0, 1], am=am, flatten=True,
                                        logic='and')
     assert np.all(Ps == [3])
     Ps = topotools.find_neighbor_sites(sites=[0, 1], am=am, flatten=True,
                                        logic='xor')
     assert np.all(Ps == [2, 4])
     Ps = topotools.find_neighbor_sites(sites=[0, 2], am=am, flatten=True,
                                        logic='xnor')
     assert np.all(Ps == [1])
Ejemplo n.º 4
0
 def test_find_neighbor_sites_unflattened_and(self):
     am = self.net.create_adjacency_matrix(fmt='lil')
     Ps = topotools.find_neighbor_sites(sites=[0, 2], am=am, logic='and',
                                        flatten=False, include_input=True)
     assert len(Ps) == 2
     assert np.all(Ps[0] == [1])
     assert np.all(Ps[1] == [1])
Ejemplo n.º 5
0
 def test_find_neighbor_sites_unflattened_xor_empty_set(self):
     am = self.net.create_adjacency_matrix(fmt='lil')
     Ps = topotools.find_neighbor_sites(sites=[0, 1, 2], am=am, logic='or',
                                        flatten=False, include_input=True)
     assert len(Ps) == 3
     assert np.all(Ps[0] == [1, 3])
     assert np.all(Ps[1] == [0, 2, 3, 4])
     assert np.all(Ps[2] == [1])
Ejemplo n.º 6
0
 def test_find_neighbor_sites(self):
     am = self.net.create_adjacency_matrix(fmt='lil')
     Ps = topotools.find_neighbor_sites(sites=[0], am=am)
     assert np.all(Ps == [1, 3])
     Ps = topotools.find_neighbor_sites(sites=[1], am=am)
     assert np.all(Ps == [0, 2, 3, 4])
     Ps = topotools.find_neighbor_sites(sites=[2], am=am)
     assert np.all(Ps == [1])
     Ps = topotools.find_neighbor_sites(sites=[3], am=am)
     assert np.all(Ps == [0, 1, 4])
     Ps = topotools.find_neighbor_sites(sites=[4], am=am)
     assert np.all(Ps == [1, 3, 5])
     Ps = topotools.find_neighbor_sites(sites=[5], am=am)
     assert np.all(Ps == [4])
     Ps = topotools.find_neighbor_sites(sites=[], am=am)
     assert Ps == []
Ejemplo n.º 7
0
 def test_find_neighbor_sites_fmt_not_lil(self):
     # Make sure results are correct even if converting to lil internally
     am = self.net.create_adjacency_matrix(fmt='csr')
     Ps = topotools.find_neighbor_sites(sites=[0], am=am)
     assert np.all(Ps == [1, 3])
     Ps = topotools.find_neighbor_sites(sites=[1], am=am)
     assert np.all(Ps == [0, 2, 3, 4])
     Ps = topotools.find_neighbor_sites(sites=[2], am=am)
     assert np.all(Ps == [1])
     Ps = topotools.find_neighbor_sites(sites=[3], am=am)
     assert np.all(Ps == [0, 1, 4])
     Ps = topotools.find_neighbor_sites(sites=[4], am=am)
     assert np.all(Ps == [1, 3, 5])
     Ps = topotools.find_neighbor_sites(sites=[5], am=am)
     assert np.all(Ps == [4])
Ejemplo n.º 8
0
    def find_neighbor_pores(self,
                            pores,
                            mode='union',
                            flatten=True,
                            include_input=False):
        r"""
        Returns a list of pores that are direct neighbors to the given pore(s)

        Parameters
        ----------
        pores : array_like
            Indices of the pores whose neighbors are sought

        flatten : boolean
            If ``True`` (default) the returned result is a compressed array of
            all neighbors.  If ``False``, a list of lists with each sub-list
            containing the neighbors for each input site.  Note that an
            *unflattened* list might be slow to generate since it is a Python
            ``list`` rather than a Numpy ``array``.

        include_input : bool
            If ``False`` (default) then the input pores are not included in
            the returned list(s).

        mode : string
            Specifies logic to filter the resulting list.  Options are:

            **'or'** : (default) All neighbors of the input pores.  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 pore.  This
            is useful for finding the pores that are not shared by any of the
            input pores.  This is known as 'exclusive_or' in set theory, and
            is an accepted input.

            **'xnor'** : Neighbors that are shared by two or more input pores.
            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 pores.  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
        -------
        If ``flatten`` is ``True``, returns a 1D array of pore indices filtered
        according to the specified mode.  If ``flatten`` is ``False``, returns
        a list of lists, where each list contains the neighbors of the
        corresponding input pores.

        Notes
        -----
        The ``logic`` options are applied to neighboring pores only, thus it
        is not possible to obtain pores that are part of the global set but
        not neighbors. This is because (a) the list of global pores might be
        very large, and (b) it is not possible to return a list of neighbors
        for each input pores if global pores are considered.

        Examples
        --------
        >>> import openpnm as op
        >>> pn = op.network.Cubic(shape=[5, 5, 5])
        >>> Ps = pn.find_neighbor_pores(pores=[0, 2])
        >>> print(Ps)
        [ 1  3  5  7 25 27]
        >>> Ps = pn.find_neighbor_pores(pores=[0, 1])
        >>> print(Ps)
        [ 2  5  6 25 26]
        >>> Ps = pn.find_neighbor_pores(pores=[0, 1], mode='union',
        ...                             include_input=True)
        >>> print(Ps)
        [ 0  1  2  5  6 25 26]
        >>> Ps = pn.find_neighbor_pores(pores=[0, 2], flatten=False)
        >>> print(Ps)
        [array([ 1,  5, 25]), array([ 1,  3,  7, 27])]
        >>> Ps = pn.find_neighbor_pores(pores=[0, 2], mode='xnor')
        >>> print(Ps)
        [1]
        >>> Ps = pn.find_neighbor_pores(pores=[0, 2], mode='xor')
        >>> print(Ps)
        [ 3  5  7 25 27]
        """
        pores = self._parse_indices(pores)
        if sp.size(pores) == 0:
            return sp.array([], ndmin=1, dtype=int)
        if 'lil' not in self._am.keys():
            self.get_adjacency_matrix(fmt='lil')
        neighbors = topotools.find_neighbor_sites(sites=pores,
                                                  logic=mode,
                                                  am=self._am['lil'],
                                                  flatten=flatten,
                                                  include_input=include_input)
        return neighbors
Ejemplo n.º 9
0
 def test_find_neighbor_sites_single(self):
     am = self.net.create_adjacency_matrix(fmt='lil')
     Ps = topotools.find_neighbor_sites(sites=0, am=am)
     assert np.all(Ps == [1, 3])
Ejemplo n.º 10
0
 def test_find_neighbor_sites_unsupported_logic(self):
     am = self.net.create_adjacency_matrix(fmt='lil')
     with pytest.raises(Exception):
         _ = topotools.find_neighbor_sites(sites=[0],
                                           am=am,
                                           logic="unsupported_logic")