Skip to content

Commit

Permalink
Replace tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AlainKadar committed Nov 12, 2023
1 parent d52ce87 commit 0b777e2
Show file tree
Hide file tree
Showing 13 changed files with 258 additions and 0 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
16 changes: 16 additions & 0 deletions StructuralGT/pytest/_test_InteractionNetworks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from StructuralGT import interaction_networks, base
import StructuralGT
import pytest

_path = StructuralGT.__path__[0]

def test_find_node():
pass
"""
N = interaction_networks.InteractionNetwork(_path + '/pytest/data/ANF')
N.binarize()
N.set_graph([0,1000,0,1000,0,4])
N.to_gsd()
N.node_calc()
N.Node_labelling(N.Degree[0],'Degree','test.gsd')
"""
51 changes: 51 additions & 0 deletions StructuralGT/pytest/_test_ResistiveNetworks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from StructuralGT import physical_networks, base
import StructuralGT
import pytest
import os
import shutil

_path = StructuralGT.__path__[0]

@pytest.fixture(params=[pytest.param(_path+'/pytest/data/AgNWN'),
pytest.param(_path+'/pytest/data/ANF')])
def binarize(request):
_dir = request.param
if os.path.isdir(_dir+'/Binarized'): shutil.rmtree(_dir+'/Binarized')

N = StructuralGT.physical_networks.ResistiveNetwork(_dir)
N.binarize()

#assert N.img_bin.shape == N.img.shape

return N

@pytest.fixture
def gsd(binarize):
N = binarize
if N._2d:
N.stack_to_gsd(crop=[149, 868, 408, 1127], rotate=45)
else:
N.stack_to_gsd(crop=[0,100,0,90,4,9])

return N

@pytest.fixture
def graph(gsd):
N = gsd
N.set_graph(weight_type=['FixedWidthConductance'], sub=True)

return N

@pytest.fixture
def potential(graph):
N = graph
if N._2d:
N.potential_distribution(0,[0,20],[180,200],R_j=10)
else:
N.potential_distribution(0,[0,20],[70,90],R_j=10)

return N

def test_node_labelling(potential):
N = potential
N.Node_labelling(N.P,'P','test.gsd')
38 changes: 38 additions & 0 deletions StructuralGT/pytest/_test_StructuralNetworks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from StructuralGT import physical_networks, base
import StructuralGT
import pytest

_path = StructuralGT.__path__[0]

def test_3D():
N = physical_networks.StructuralNetwork(_path + '/pytest/data/Rectangle1')
N.binarize()
N.stack_to_gsd()
N.set_graph()
N.node_calc()
N.Node_labelling(N.Degree[0],'Degree','test.gsd')

def test_3D_crop():
N = physical_networks.StructuralNetwork(_path + '/pytest/data/Rectangle1')
N.binarize()
N.stack_to_gsd(crop=[0,550,0,550,0,2])
N.set_graph()
N.node_calc()
N.Node_labelling(N.Degree[0],'Degree','test.gsd')

def test_2D():
N = physical_networks.StructuralNetwork(_path + '/pytest/data/AgNWN')
N.binarize()
N.stack_to_gsd()
N.set_graph()
N.node_calc()
N.Node_labelling(N.Degree[0],'Degree','test.gsd')

def test_2D_crop():
N = physical_networks.StructuralNetwork(_path + '/pytest/data/AgNWN')
N.binarize()
N.stack_to_gsd(crop=[0,550,0,550])
N.set_graph()
N.node_calc()
N.Node_labelling(N.Degree[0],'Degree','test.gsd')

30 changes: 30 additions & 0 deletions StructuralGT/pytest/network_factory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from StructuralGT import networks
import numpy as np

import StructuralGT
_path = StructuralGT.__path__[0]

anf_options = {"Thresh_method":0, "gamma": 3, "md_filter": 0, "g_blur": 1,
"autolvl": 0,"fg_color":0,"laplacian": 0, "scharr": 0, "sobel":0 ,
"lowpass": 1, "asize":7, "bsize":3, "wsize":5, "thresh": 103}

agnwn_options = {"Thresh_method":0, "gamma": 3, "md_filter": 0, "g_blur": 1,
"autolvl": 0,"fg_color":0,"laplacian": 0, "scharr": 0, "sobel":0 ,
"lowpass": 1, "asize":7, "bsize":3, "wsize":5, "thresh": 103}

def fibrous(weight_type=None):
ANF = networks.Network(_path + '/pytest/data/ANF')
ANF.binarize(options_dict=anf_options)
ANF.img_to_skel(crop=[200,300,200,300, 1, 3])
ANF.set_graph(sub=True, weight_type=weight_type)

return ANF

def conductive():
AgNWN = networks.Network(_path + '/pytest/data/AgNWN')
AgNWN.binarize(options_dict=agnwn_options)
AgNWN.img_to_skel(crop=[149, 868, 408, 1127], rotate=45)
AgNWN.set_graph(weight_type=['FixedWidthConductance'], sub=True, R_j=10, rho_dim=2)

return AgNWN

25 changes: 25 additions & 0 deletions StructuralGT/pytest/test_AverageNodalConnectivity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from StructuralGT.average_nodal_connectivity import AverageNodalConnectivity
from network_factory import fibrous

import igraph as ig
import pytest
from networkx import average_node_connectivity
import numpy.testing as npt


class TestAverageNodalConnectivity:
def test(self):
#Obtain a connected graph
testNetwork = fibrous()
testGraph = testNetwork.graph.to_networkx()

#Instantiate a compute module and run calculation
ComputeModule = AverageNodalConnectivity()
ComputeModule.compute(testNetwork)

#Check that the StructuralGT and networkx values are equal
npt.assert_allclose(ComputeModule.average_nodal_connectivity,
average_node_connectivity(testGraph),
rtol=1e-2
)

30 changes: 30 additions & 0 deletions StructuralGT/pytest/test_Electronic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from StructuralGT.electronic import Electronic
from network_factory import conductive

import igraph as ig
import pytest
import numpy.testing as npt

import StructuralGT
_path = StructuralGT.__path__[0]

class TestElectronic:
@pytest.fixture
def test_compute(self):
#Obtain a conductive graph
testNetwork = conductive()

#Instantiate a compute module and run calculation
ComputeModule = Electronic()
ComputeModule.compute(testNetwork, 10, 0, [[0,50],[testNetwork.shape[0]-50, testNetwork.shape[0]]])

return ComputeModule

def test_Kirchhoff(self, test_compute):
#Ensure Kirchhoff's Law satisfied
ComputeModule = test_compute

npt.assert_allclose((ComputeModule.P[-2] - ComputeModule.P[-1])/ComputeModule.effective_resistance,
1,
atol=1e-2
)
68 changes: 68 additions & 0 deletions StructuralGT/pytest/test_Structural.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
from StructuralGT.structural import Structural
from network_factory import fibrous
import igraph as ig
import pytest
from networkx import diameter, density, clustering, average_clustering
import numpy.testing as npt


class TestUnweightedStructural:
@pytest.fixture
def test_compute(self):
#Obtain an unweighted connected graph
testNetwork = fibrous()
testGraph = testNetwork.graph.to_networkx()

#Instantiate a compute module and run calculation
ComputeModule = Structural()
ComputeModule.compute(testNetwork)

return ComputeModule, testGraph

def test_diameter(self, test_compute):
ComputeModule, testGraph = test_compute
npt.assert_allclose(
ComputeModule.diameter,
diameter(testGraph),
atol=1e-2,
)

def test_density(self, test_compute):
ComputeModule, testGraph = test_compute
npt.assert_allclose(
ComputeModule.density,
density(testGraph),
atol=1e-2,
)

def test_average_clustering(self, test_compute):
"""
ComputeModule, testGraph = test_compute
npt.assert_allclose(
ComputeModule.average_clustering,
average_clustering(testGraph),
atol=1e-2,
)
"""
pass

class TestWeightedStructural:
@pytest.fixture
def test_compute(self):
#Obtain an unweighted connected graph
testNetwork = fibrous(weight_type=['Length'])
testGraph = testNetwork.graph.to_networkx()

#Instantiate a compute module and run calculation
ComputeModule = Structural(weight_type=['Length'])
ComputeModule.compute(testNetwork)

return ComputeModule, testGraph

def test_diameter(self, test_compute):
ComputeModule, testGraph = test_compute
npt.assert_allclose(
ComputeModule.diameter,
diameter(testGraph),
atol=1e-2,
)

0 comments on commit 0b777e2

Please sign in to comment.