network.py 8.63 KB
``````# -*- coding: utf-8 -*-
"""
Created on Sat Sep 16 16:34:49 2017

@author: pavel
"""

import struct
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
import math

'''
Definition of the Node class
Duplicate of the node class in network
Stores the physical position, outgoing edges list and incoming edges list.
'''
class Node:
def __init__(self, point, outgoing, incoming):
self.p = point
self.o = outgoing
self.i = incoming

#    def p():
#        return self.p

'''
Definition of the Fiber class.
Duplicate of the Node class in network
Stores the starting vertex, the ending vertex, the points array and the radius array
'''
class Fiber:

def __init__ (self):
self.v0 = 0
self.v1 = 0
self.points = []

def __init__ (self, p1, p2, pois, rads):
self.v0 = p1
self.v1 = p2
self.points = pois

'''
return the length of the fiber.
'''
def length(self):
length = 0
for i in range(len(self.points)-1):
length = length + math.sqrt(pow(self.points[i][0]- self.points[i+1][0],2) + pow(self.points[i][1]- self.points[i+1][1],2) + pow(self.points[i][2]- self.points[i+1][2],2))

return length

'''
returns the turtuosity of the fiber.
'''
def turtuosity(self):
turtuosity = 0
distance = math.sqrt(math.pow(self.points[0][0]- self.points[len(self.points)-1][0],2) + math.pow(self.points[0][1]- self.points[len(self.points)-1][1],2) + math.pow(self.points[0][2]- self.points[len(self.points)-1][2],2))
turtuosity = self.length()/distance
#print(turtuosity)

return turtuosity

'''
returns the volume of the fiber.
'''
def volume(self):
volume = 0
for i in range(len(self.points)-1):

#print(volume)
return volume
'''
Writes the header given and open file descripion, number of verticies and number of edges.
'''
txt = "nwtFileFormat fileid(14B), desc(58B), #vertices(4B), #edges(4B): bindata"
b = bytearray()
b.extend(txt.encode())
open_file.write(b)
open_file.write(struct.pack('i', numVerts))
open_file.write(struct.pack('i', numEdges))

'''
Writes a single vertex to a file.
'''
def writeVertex(open_file, vertex):
open_file.write(struct.pack('<f',vertex.p[0]))
open_file.write(struct.pack('<f',vertex.p[1]))
open_file.write(struct.pack('<f',vertex.p[2]))
open_file.write(struct.pack('i', len(vertex.o)))
open_file.write(struct.pack('i', len(vertex.i)))
for j in range(len(vertex.o)):
open_file.write(struct.pack('i',vertex.o[j]))

for j in range(len(vertex.i)):
open_file.write(struct.pack('i', vertex.i[j]))

return

'''
Writes a single fiber to a file.
'''
def writeFiber(open_file, edge):
open_file.write(struct.pack('i',edge.v0))
open_file.write(struct.pack('i',edge.v1))
open_file.write(struct.pack('i',len(edge.points)))
for j in range(len(edge.points)):
open_file.write(struct.pack('<f', edge.points[j][0]))
open_file.write(struct.pack('<f', edge.points[j][1]))
open_file.write(struct.pack('<f', edge.points[j][2]))

return

'''
Writes the entire network to a file in str given the vertices array and the edges array.
'''
def exportNWT(str, vertices, edges):
with open(str, "wb") as file:
for i in range(len(vertices)):
writeVertex(file, vertices[i])

for i in range(len(edges)):
writeFiber(file, edges[i])

return

'''
Reads a single vertex from an open file and returns a node Object.
'''
points = np.tile(0., 3)
points[0] = struct.unpack('f', bytes)[0]
points[1] = struct.unpack('f', bytes)[0]
points[2] = struct.unpack('f', bytes)[0]

numO = int.from_bytes(bytes, byteorder='little')
outgoing = np.tile(0, numO)
numI = int.from_bytes(bts, byteorder='little')
incoming = np.tile(0, numI)
for j in range(numO):
outgoing[j] = int.from_bytes(bytes, byteorder='little')

for j in range(numI):
incoming[j] = int.from_bytes(bytes, byteorder='little')

node = Node(points, outgoing, incoming)
return node

'''
Reads a single fiber from an open file and returns a Fiber object .
'''
vtx0 = int.from_bytes(bytes, byteorder = 'little')
vtx1 = int.from_bytes(bytes, byteorder = 'little')
numVerts = int.from_bytes(bytes, byteorder = 'little')
pts = []

for j in range(numVerts):
point = np.tile(0., 3)
point[0] = struct.unpack('f', bytes)[0]
point[1] = struct.unpack('f', bytes)[0]
point[2] = struct.unpack('f', bytes)[0]
pts.append(point)

F = Fiber(vtx0, vtx1, pts, rads)

return F

'''
Imports a NWT file at location str.
Returns a list of Nodes objects and a list of Fiber objects.
'''
def importNWT(str):
with open(str, "rb") as file:
numVertex = int.from_bytes(bytes, byteorder='little')
numEdges = int.from_bytes(bytes, byteorder='little')

nodeList = []
fiberList = []
for i in range(numVertex):
nodeList.append(node)

for i in range(numEdges):
fiberList.append(edge)

#exportNWT("/home/pavel/Documents/Python/NetLayout/from_python_seg.nwt", nodeList, fiberList)
print(str)
return nodeList, fiberList;

'''
Creates a graph from a list of nodes and a list of edges.
Uses edge length as weight.
Returns a NetworkX Object.
'''

def createLengthGraph(nodeList, edgeList):
G = nx.Graph()
for i in range(len(nodeList)):
for i in range(len(edgeList)):

return G

'''
Creates a graph from a list of nodes and a list of edges.
Uses edge turtuosity as weight.
Returns a NetworkX Object.
'''
def createTortuosityGraph(nodeList, edgeList):
G = nx.Graph()
for i in range(len(nodeList)):
for i in range(len(edgeList)):

return G

'''
Creates a graph from a list of nodes and a list of edges.
Uses edge volume as weight.
Returns a NetworkX Object.
'''
def createVolumeGraph(nodeList, edgeList):
G = nx.Graph()
for i in range(len(nodeList)):
for i in range(len(edgeList)):

return G
'''
Returns the positions dictionary for the Circular layout.
'''

'''
Return the positions dictionary for the Spring layout.
'''
def getSpringLayout(graph, pos, iterations, scale):
return nx.spring_layout(graph, 2, None, pos, iterations, 'weight', scale, None)

'''
Draws the graph.
'''
def drawGraph(graph, pos):
nx.draw(graph, pos)
return

def aabb(nodeList, edgeList):

lower = nodeList[0].p.copy()
upper = lower.copy()
for i in nodeList:
for c in range(len(lower)):
if lower[c] > i.p[c]:
lower[c] = i.p[c]
if upper[c] < i.p[c]:
upper[c] = i.p[c]
return lower, upper``````