Repository: PMEAL/OpenPNM (python)
Issue: find_connected_pores fails given empty list
It works unless flatten is True
Pull request description: Bugfix/374
I've made several little fixes and bug fixes while working on my myriad IP branches.  The PR collects all the changes to the framework separate from the IP work.  This will close #374 .

Patch:
diff --git a/OpenPNM/Base/__Core__.py b/OpenPNM/Base/__Core__.py
index 2c4e63439..75effaf00 100644
--- a/OpenPNM/Base/__Core__.py
+++ b/OpenPNM/Base/__Core__.py
@@ -108,7 +108,7 @@ class Core(dict):
 
     def _set_ctrl(self,controller):
         if self.name in controller.keys():
-            raise Exception('An object with that name is already present in simulation')
+            raise Exception('An object named '+self.name+' is already present in simulation')
         self._ctrl = controller
         controller.update({self.name: self})
 
@@ -130,7 +130,7 @@ class Core(dict):
             for item in objs:
                 keys = [key.split('.')[-1] for key in item.keys()]
                 if name in keys:
-                    raise Exception('That name is already in use as an array name')
+                    raise Exception(name+' is already in use as an array name')
             for item in objs:
                 if 'pore.'+self.name in item.keys():
                     item['pore.'+name] = item.pop('pore.'+self.name)
@@ -767,6 +767,7 @@ class Core(dict):
         convenience function and is a compliment to tomask().
 
         """
+        mask = sp.array(mask,ndmin=1)
         if sp.shape(mask)[0] == self.num_pores():
             indices = self.pores()[mask]
         elif sp.shape(mask)[0] == self.num_throats():
diff --git a/OpenPNM/Network/__GenericNetwork__.py b/OpenPNM/Network/__GenericNetwork__.py
index 7a2a846a1..7fb9bc710 100644
--- a/OpenPNM/Network/__GenericNetwork__.py
+++ b/OpenPNM/Network/__GenericNetwork__.py
@@ -254,7 +254,12 @@ class GenericNetwork(Core):
         >>> pn.find_connected_pores(throats=[0,1], flatten=True)
         array([0, 1, 5])
         """
-        Ps = self['throat.conns'][throats]
+        Ts = sp.array(throats, ndmin=1)
+        if Ts.dtype == bool:
+            Ts = self.toindices(Ts)
+        if sp.size(Ts) == 0:
+            return sp.ndarray([0,2], dtype=int)
+        Ps = self['throat.conns'][Ts]
         if flatten:
             Ps = sp.unique(sp.hstack(Ps))
         return Ps
@@ -349,8 +354,10 @@ class GenericNetwork(Core):
         array([ 3,  5,  7, 25, 27])
         """
         pores = sp.array(pores, ndmin=1)
+        if pores.dtype == bool:
+            pores = self.toindices(pores)
         if sp.size(pores) == 0:
-            return sp.array([], ndmin=1)
+            return sp.array([], ndmin=1, dtype=int)
         # Test for existence of incidence matrix
         try:
             neighborPs = self._adjacency_matrix['lil'].rows[[pores]]
@@ -419,8 +426,10 @@ class GenericNetwork(Core):
         array([array([0, 1, 2]), array([0, 3, 4, 5])], dtype=object)
         """
         pores = sp.array(pores, ndmin=1)
+        if pores.dtype == bool:
+            pores = self.toindices(pores)
         if sp.size(pores) == 0:
-            return sp.array([], ndmin=1)
+            return sp.array([], ndmin=1, dtype=int)
         # Test for existence of incidence matrix
         try:
             neighborTs = self._incidence_matrix['lil'].rows[[pores]]
@@ -477,11 +486,18 @@ class GenericNetwork(Core):
         >>> pn.num_neighbors(pores=[0, 2], flatten=True)
         6
         """
+        pores = sp.array(pores,ndmin=1)
+        if pores.dtype == bool:
+            pores = self.toindices(pores)
+        if sp.size(pores) == 0:
+            return sp.array([],ndmin=1,dtype=int)
 
         # Count number of neighbors
         if flatten:
-            neighborPs = self.find_neighbor_pores(pores, flatten=True,
-                                                  mode='union', excl_self=True)
+            neighborPs = self.find_neighbor_pores(pores,
+                                                  flatten=True,
+                                                  mode='union',
+                                                  excl_self=True)
             num = sp.shape(neighborPs)[0]
         else:
             neighborPs = self.find_neighbor_pores(pores, flatten=False)
@@ -519,6 +535,8 @@ class GenericNetwork(Core):
         >>> pn['pore.domain2'][[5, 6, 7]] = True
         >>> pn.find_interface_throats(labels=['domain1', 'domain2'])
         array([1, 4, 7])
+
+        TODO: It might be a good idea to allow overlapping regions
         """
         Tind = sp.array([], ndmin=1)
         if sp.shape(labels)[0] != 2:
@@ -555,11 +573,11 @@ class GenericNetwork(Core):
             An Np long list of clusters numbers
 
         """
-        if sp.shape(mask)[0] == self.num_throats():
+        if sp.size(mask) == self.num_throats():
             # Convert to boolean mask if not already
             temp = sp.zeros((self.num_throats(),), dtype=bool)
             temp[mask] = True
-        elif sp.shape(mask)[0] == self.num_pores():
+        elif sp.size(mask) == self.num_pores():
             conns = self.find_connected_pores(throats=self.throats())
             conns[:, 0] = mask[conns[:, 0]]
             conns[:, 1] = mask[conns[:, 1]]


Test patch:
diff --git a/test/integration/test_network_methods.py b/test/integration/test_network_methods.py
new file mode 100644
index 000000000..80bac9840
--- /dev/null
+++ b/test/integration/test_network_methods.py
@@ -0,0 +1,106 @@
+import scipy as sp
+import OpenPNM
+import pytest
+
+def test_find_connected_pores():
+    pn = OpenPNM.Network.Cubic(shape=(10,10,10))
+    a = pn.find_connected_pores(throats=[0,1])
+    assert sp.all(a.flatten() == [0, 1, 1, 2])
+    a = pn.find_connected_pores(throats=[0,1], flatten=True)
+    assert sp.all(a == [0, 1, 2])
+    Tind = sp.zeros((pn.Nt,), dtype=bool)
+    Tind[[0,1]] = True
+    a = pn.find_connected_pores(throats=Tind, flatten=True)
+    assert sp.all(a == [0, 1, 2])
+    a = pn.find_connected_pores(throats=[], flatten=True)
+    assert sp.shape(a) == (0,2)
+
+def test_find_neighbor_pores():
+    pn = OpenPNM.Network.Cubic(shape=(10,10,10))
+    a = pn.find_neighbor_pores(pores=[])
+    assert sp.size(a) == 0
+    Pind = sp.zeros((pn.Np,), dtype=bool)
+    Pind[[0,1]] = True
+    a = pn.find_neighbor_pores(pores=Pind)
+    assert sp.all(a == [2, 10, 11, 100, 101])
+    a = pn.find_neighbor_pores(pores=[0, 2], mode='union')
+    assert sp.all(a == [1, 3, 10,  12, 100, 102])
+    a = pn.find_neighbor_pores(pores=[0, 2], mode='intersection')
+    assert sp.all(a == [1])
+    a = pn.find_neighbor_pores(pores=[0, 2], mode='not_intersection')
+    assert sp.all(a == [3, 10, 12, 100, 102])
+    a = pn.find_neighbor_pores(pores=[0, 2],
+                               mode='union',
+                               excl_self=False)
+    assert sp.all(a == [ 0, 1, 2, 3, 10, 12, 100, 102])
+    a = pn.find_neighbor_pores(pores=[0, 2],
+                               mode='intersection',
+                               excl_self=False)
+    assert sp.all(a == [1])
+    a = pn.find_neighbor_pores(pores=[0, 2],
+                               mode='not_intersection',
+                               excl_self=False)
+    assert sp.all(a == [0, 2, 3, 10, 12, 100, 102])
+
+def test_find_neighbor_throats():
+    pn = OpenPNM.Network.Cubic(shape=(10,10,10))
+    a = pn.find_neighbor_throats(pores=[])
+    assert sp.size(a) == 0
+    Pind = sp.zeros((pn.Np,), dtype=bool)
+    Pind[[0,1]] = True
+    a = pn.find_neighbor_throats(pores=Pind)
+    assert sp.all(a == [ 0, 1, 900, 901, 1800, 1801])
+    a = pn.find_neighbor_throats(pores=[0, 2], mode='union')
+    assert sp.all(a == [ 0, 1, 2, 900, 902, 1800, 1802])
+    a = pn.find_neighbor_throats(pores=[0, 2], mode='intersection')
+    assert sp.size(a) == 0
+    a = pn.find_neighbor_throats(pores=[0, 2], mode='not_intersection')
+    assert sp.all(a == [ 0, 1, 2,  900,  902, 1800, 1802])
+
+def test_num_neighbors():
+    pn = OpenPNM.Network.Cubic(shape=(10,10,10))
+    a = pn.num_neighbors(pores=[])
+    assert sp.size(a) == 0
+    Pind = sp.zeros((pn.Np,), dtype=bool)
+    Pind[0] = True
+    a = pn.num_neighbors(pores=Pind)
+    assert a == 3
+    a = pn.num_neighbors(pores=[0,2], flatten=True)
+    assert a == 6
+    assert isinstance(a, int)
+    a = pn.num_neighbors(pores=[0,2], flatten=False)
+    assert sp.all(a == [3, 4])
+    a = pn.num_neighbors(pores=0, flatten=False)
+    assert sp.all(a = [3])
+    assert isinstance(a, sp.ndarray)
+
+def test_find_interface_throats():
+    pn = OpenPNM.Network.Cubic(shape=(3,3,3))
+    pn['pore.domain1'] = False
+    pn['pore.domain2'] = False
+    pn['pore.domain3'] = False
+    pn['pore.domain1'][[0, 1, 2]] = True
+    pn['pore.domain2'][[5, 6, 7]] = True
+    pn['pore.domain3'][18:26] = True
+    a = pn.find_interface_throats(labels=['domain1', 'domain2'])
+    assert a == [20]
+    a = pn.find_interface_throats(labels=['domain1', 'domain3'])
+    assert sp.size(a) == 0
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+