Skip to content

Commit

Permalink
Update print statements
Browse files Browse the repository at this point in the history
  • Loading branch information
AlainKadar committed Jan 29, 2024
1 parent 0f74495 commit 582aea1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 89 deletions.
81 changes: 6 additions & 75 deletions StructuralGT/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,6 @@ def gsd_to_G(gsd_name, sub=False, _2d=False, crop=None):
for i in list(range(min(positions.shape)))))
canvas[tuple(list(positions.T))] = 1
canvas = canvas.astype(int)
print('gsd_to_G canvas has shape ', canvas.shape)

G = sknwEdits.build_sknw(canvas)

Expand All @@ -279,31 +278,16 @@ def gsd_to_G(gsd_name, sub=False, _2d=False, crop=None):

#Function generates largest connected induced subgraph. Node and edge numbers are reset such that they are consecutive integers, starting from 0
def sub_G(G):
print('pre sub has ', G.vcount(), ' nodes')
print(f'Before removing smaller components, graph has {G.vcount()} nodes')
components = G.connected_components()
G = components.giant()
print('post sub has ', G.vcount(), ' nodes')
print(f'After removing smaller components, graph has {G.vcount()} nodes')

# G_sub = G.subgraph(max(nx.connected_components(G), key=len).copy())
# G = nx.relabel.convert_node_labels_to_integers(G_sub)

return G

#GT_Params_noGUI is a modified copy of the original SGT .py file, with the GUI modules removed
def write_averaged(gsd_name):
import GT_Params_noGUI

start = time.time()
G = gsd_to_G(gsd_name)
end = time.time()
G = sub_G(G)

start = time.time()
data = GT_Params_noGUI.run_GT_calcs(G,1,1,1,1,1,1,1,1,0,1,1,0)
end = time.time()
print('Ran GT_Params in', end-start, 'for a graph with ', G.vcount(), 'nodes')
datas = pd.DataFrame(data)
datas.to_csv(gsd_name + 'Averaged_indices.csv')

def debubble(g, elements):
if not isinstance(elements,list): raise error.StructuralElementError
Expand Down Expand Up @@ -333,7 +317,7 @@ def debubble(g, elements):
s.particles.typeid = ['0']*s.particles.N
f.append(s)
end = time.time()
print('Ran debubble in ', end-start, 'for an image with shape ', g._skeleton_3d.shape)
print(f'Ran debubble in {end-start} for an image with shape {g._skeleton_3d.shape}')

return g

Expand Down Expand Up @@ -361,7 +345,7 @@ def merge_nodes(g, disk_size):
s.particles.typeid = ['0']*s.particles.N
f.append(s)
end = time.time()
print('Ran merge in ', end-start, 'for an image with shape ', g._skeleton_3d.shape)
print(f'Ran merge in {end-start} for an image with shape {g._skeleton_3d.shape}')

return g

Expand All @@ -388,7 +372,7 @@ def prune(g, size):
s.particles.typeid = ['0']*s.particles.N
f.append(s)
end = time.time()
print('Ran prune in ', end-start, 'for an image with shape ', g._skeleton_3d.shape)
print(f'Ran prune in {end-start} for an image with shape {g._skeleton_3d.shape}')

return g

Expand All @@ -415,29 +399,10 @@ def remove_objects(g, size):
s.particles.typeid = ['0']*s.particles.N
f.append(s)
end = time.time()
print('Ran remove objects in ', end-start, 'for an image with shape ', g._skeleton_3d.shape)
print(f'Ran remove objects in {end-start} for an image with shape {g._skeleton_3d.shape})

return g

def igraph_ANC(directory, I):
start = time.time()
vclist = []

for node_i in I.vs:
for node_j in I.vs:
if node_i.index == node_j.index: continue
if I.are_connected(node_i, node_j): continue
cut = I.vertex_connectivity(source=node_i.index, target = node_j.index)
vclist.append(cut)

ANC = np.mean(np.asarray(vclist))
end = time.time()
np.savetxt(directory+'/ANC.csv',ANC)
print('ANC calculated as ', ANC, ' in ', end-start)

return ANC


def add_weights(g, weight_type=None, R_j=0, rho_dim=1):
if not isinstance(weight_type,list) and weight_type is not None: raise TypeError('weight_type must be list, even if single element')
for _type in weight_type:
Expand All @@ -451,40 +416,6 @@ def add_weights(g, weight_type=None, R_j=0, rho_dim=1):

return g.Gr


def gyration_moments_3(G, sampling=1, weighted=True):
Ax=0
Ay=0
Axy=0
node_count = np.asarray(list(range(G.vcount())))
mask = np.random.rand(G.vcount()) > (1-sampling)
trimmed_node_count = node_count[mask]
for i in trimmed_node_count:
for j in trimmed_node_count:
if i >= j: #Symetric matrix
continue

if weighted:
path = G.get_shortest_paths(i,to=j, weights='Resistance')
else:
path = G.get_shortest_paths(i,to=j)
Ax_term = 0
Ay_term = 0
Axy_term = 0
for hop_s,hop_t in zip(path[0][0:-1],path[0][1::]):
if weighted:
weight = G.es[G.get_eid(hop_s,hop_t)]['Conductance']
else:
weight = 1
Ax_term = Ax_term + weight*(((G.vs[hop_s]['o'][0]).astype(float) - (G.vs[hop_t]['o'][0]).astype(float))**2)
Ay_term = Ay_term + weight*(((G.vs[hop_s]['o'][1]).astype(float) - (G.vs[hop_t]['o'][1]).astype(float))**2)
Axy_term = Axy_term + weight*(((G.vs[hop_s]['o'][1]).astype(float) - (G.vs[hop_t]['o'][1].astype(float))) * ((G.vs[hop_s]['o'][0]).astype(float) - (G.vs[hop_t]['o'][0]).astype(float)))
Ax = Ax + (Ax_term)
Ay = Ay + (Ay_term)
Axy = Axy + (Axy_term)
A = np.array([[Ax,Axy,0],[Axy,Ay,0],[0,0,0]])/(len(trimmed_node_count)**2)
return A

def quadrupletise(i):
if len(str(i))==4: return str(i)
elif len(str(i))==3: return '0' + str(i)
Expand Down
14 changes: 3 additions & 11 deletions StructuralGT/electronic.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def compute(self, network, R_j, axis, boundary_conditions, source=-1,
sink_id = source_id + 1
network.graph_connected.add_vertices(2)

print("Graph has max ", network.shape)
print("Graph has shape ", network.shape)
axes = np.array([0, 1, 2])[0 : network.dim]
indices = axes[axes != axis]
axis_centre1 = np.zeros(network.dim, dtype=int)
Expand All @@ -49,15 +49,11 @@ def compute(self, network, R_j, axis, boundary_conditions, source=-1,
axis_centre2[axis] = network.shape[axis]
source_coord = axis_centre1 - delta
sink_coord = axis_centre2 + delta
print("source coord is ", source_coord)
print("sink coord is ", sink_coord)
print("Source coordinate is ", source_coord)
print("Sink coordinate is ", sink_coord)
network.graph_connected.vs[source_id]["o"] = source_coord
network.graph_connected.vs[sink_id]["o"] = sink_coord

print(
"Before connecting external nodes, G has vcount ",
network.graph_connected.vcount(),
)
for node in network.graph_connected.vs:
if node["o"][axis] >= boundary1[0] and node["o"][axis] <= boundary1[1]:
network.graph_connected.add_edges([(node.index, source_id)])
Expand All @@ -77,10 +73,6 @@ def compute(self, network, R_j, axis, boundary_conditions, source=-1,
] = base.connector(sink_coord, node["o"])

# Write skeleton connected to external node
print(network.graph_connected.is_connected(), " connected")
print(
"After connecting external nodes, G has vcount ", network.graph_connected.vcount()
)
connected_name = (
os.path.split(network.gsd_name)[0]
+ "/connected_"
Expand Down
12 changes: 9 additions & 3 deletions StructuralGT/networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,11 +351,11 @@ def img_to_skel(

end = time.time()
print(
"Ran stack_to_gsd() in ",
"Ran img_to_skel() in ",
end - start,
"for gsd with ",
"for skeleton with ",
len(positions),
"particles",
"voxels",
)

if debubble is not None:
Expand Down Expand Up @@ -593,6 +593,12 @@ def graph(self):
skeleton"""
return self.Gr

@property
def image(self):
""":class:`np.ndarray`: The original image used to obtain the graph."""
if self._2d: return self.image_stack[0][0]
else: return self.image_stack[:][0]

def from_gsd(_dir, filename, frame=0, depth=None):
"""
Function returns a Network object stored in a given .gsd file
Expand Down

0 comments on commit 582aea1

Please sign in to comment.