From bda1bd09c5453b550b854962041b66e87068d10e Mon Sep 17 00:00:00 2001 From: Pavel Govyadinov Date: Sat, 16 Sep 2017 19:42:28 -0500 Subject: [PATCH] Added a network class implemented in python. Reads, writes NWT files, returns NetworkX graph objects, and makes layouts. --- python/network.py | 280 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 280 insertions(+), 0 deletions(-) create mode 100644 python/network.py diff --git a/python/network.py b/python/network.py new file mode 100644 index 0000000..7fc7f5b --- /dev/null +++ b/python/network.py @@ -0,0 +1,280 @@ +# -*- 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 + +''' + 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 = [] + self.radii = [] + + def __init__ (self, p1, p2, pois, rads): + self.v0 = p1 + self.v1 = p2 + self.points = pois + self.radii = rads + + ''' + 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): + volume = volume + 1.0/3.0 * math.pi * (math.pow(self.radii[i],2) + math.pow(self.radii[i+1],2) + self.radii[i]*self.radii[i+1]) * math.sqrt(math.pow(self.points[i][0]- self.points[i+1][0],2) + math.pow(self.points[i][1]- self.points[i+1][1],2) + math.pow(self.points[i][2]- self.points[i+1][2],2)) + + #print(volume) + return volume +''' + Writes the header given and open file descripion, number of verticies and number of edges. +''' +def writeHeader(open_file, numVerts, numEdges): + 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('