Authored by Pavel Govyadinov
1 parent e98c381d

### Added a network class implemented in python. Reads, writes NWT files, returns Ne…

`…tworkX graph objects, and makes layouts.`
Showing 1 changed file with 280 additions and 0 deletions
python/network.py 0 → 100644

 1 +# -*- coding: utf-8 -*- 2 +""" 3 +Created on Sat Sep 16 16:34:49 2017 4 + 5 +@author: pavel 6 +""" 7 + 8 +import struct 9 +import numpy as np 10 +import networkx as nx 11 +import matplotlib.pyplot as plt 12 +import math 13 + 14 +''' 15 + Definition of the Node class 16 + Duplicate of the node class in network 17 + Stores the physical position, outgoing edges list and incoming edges list. 18 +''' 19 +class Node: 20 + def __init__(self, point, outgoing, incoming): 21 + self.p = point 22 + self.o = outgoing 23 + self.i = incoming 24 + 25 +''' 26 + Definition of the Fiber class. 27 + Duplicate of the Node class in network 28 + Stores the starting vertex, the ending vertex, the points array and the radius array 29 +''' 30 +class Fiber: 31 + 32 + def __init__ (self): 33 + self.v0 = 0 34 + self.v1 = 0 35 + self.points = [] 36 + self.radii = [] 37 + 38 + def __init__ (self, p1, p2, pois, rads): 39 + self.v0 = p1 40 + self.v1 = p2 41 + self.points = pois 42 + self.radii = rads 43 + 44 + ''' 45 + return the length of the fiber. 46 + ''' 47 + def length(self): 48 + length = 0 49 + for i in range(len(self.points)-1): 50 + 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)) 51 + 52 + return length 53 + 54 + ''' 55 + returns the turtuosity of the fiber. 56 + ''' 57 + def turtuosity(self): 58 + turtuosity = 0 59 + 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)) 60 + turtuosity = self.length()/distance 61 + #print(turtuosity) 62 + 63 + return turtuosity 64 + 65 + ''' 66 + returns the volume of the fiber. 67 + ''' 68 + def volume(self): 69 + volume = 0 70 + for i in range(len(self.points)-1): 71 + 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)) 72 + 73 + #print(volume) 74 + return volume 75 +''' 76 + Writes the header given and open file descripion, number of verticies and number of edges. 77 +''' 78 +def writeHeader(open_file, numVerts, numEdges): 79 + txt = "nwtFileFormat fileid(14B), desc(58B), #vertices(4B), #edges(4B): bindata" 80 + b = bytearray() 81 + b.extend(txt.encode()) 82 + open_file.write(b) 83 + open_file.write(struct.pack('i', numVerts)) 84 + open_file.write(struct.pack('i', numEdges)) 85 + 86 + 87 +''' 88 + Writes a single vertex to a file. 89 +''' 90 +def writeVertex(open_file, vertex): 91 + open_file.write(struct.pack('