From 8d96a467dc890b7403b00d93e52bce64c3fbbdbe Mon Sep 17 00:00:00 2001 From: David Mayerich Date: Wed, 3 Jan 2018 23:38:26 -0600 Subject: [PATCH] started converting NETWORK into a python class --- python/network.py | 525 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 1 file changed, 262 insertions(+), 263 deletions(-) diff --git a/python/network.py b/python/network.py index c5aa1d6..a02cd7b 100644 --- a/python/network.py +++ b/python/network.py @@ -39,11 +39,12 @@ class Fiber: self.points = [] self.radii = [] - def __init__ (self, p1, p2, pois, rads): - self.v0 = p1 - self.v1 = p2 - self.points = pois - self.radii = rads + #NOTE: there is no function overloading in Python +# 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. @@ -76,289 +77,287 @@ class Fiber: #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(' i.p[c]: - lower[c] = i.p[c] - if upper[c] < i.p[c]: - upper[c] = i.p[c] - return lower, upper + ''' + Imports a NWT file at location str. + Returns a list of Nodes objects and a list of Fiber objects. + ''' -#calculate the distance field at a given resolution -# R (triple) resolution along each dimension -def distancefield(nodeList, edgeList, R=(100, 100, 100)): +class Network: - #get a list of all node positions in the network - P = [] - for e in edgeList: - for p in e.points: - P.append(p) + def __init__(self, str): + with open(str, "rb") as file: + header = file.read(72) + bytes = file.read(4) + numVertex = int.from_bytes(bytes, byteorder='little') + bytes = file.read(4) + numEdges = int.from_bytes(bytes, byteorder='little') - #turn that list into a Numpy array so that we can create a KD tree - P = np.array(P) - - #generate a KD-Tree out of the network point array - tree = sp.spatial.cKDTree(P) + self.N = [] + self.F = [] + for i in range(numVertex): + node = NWT.readVertex(file) + self.N.append(node) - plt.scatter(P[:, 0], P[:, 1]) + for i in range(numEdges): + edge = NWT.readFiber(file) + self.F.append(edge) - #specify the resolution of the ouput grid - R = (200, 200, 200) + ''' + Creates a graph from a list of nodes and a list of edges. + Uses edge length as weight. + Returns a NetworkX Object. + ''' +# def createLengthGraph(self): +# G = nx.Graph() +# for i in range(len(self.nodeList)): +# G.add_node(i, p=V[i].p) +# for i in range(len(self.edgeList)): +# G.add_edge(self.edgeList[i].v0, self.edgeList[i].v1, weight = E[i].length()) +# +# 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)): +# G.add_node(i, p=V[i].p) +# for i in range(len(edgeList)): +# G.add_edge(edgeList[i].v0, edgeList[i].v1, weight = E[i].turtuosity()) +# +# return G - #generate a meshgrid of the appropriate size and resolution to surround the network - lower, upper = aabb(nodeList, edgeList) #get the space occupied by the network - x = np.linspace(lower[0], upper[0], R[0]) #get the grid points for uniform sampling of this space - y = np.linspace(lower[1], upper[1], R[1]) - z = np.linspace(lower[2], upper[2], R[2]) - X, Y, Z = np.meshgrid(x, y, z) - #Z = 150 * numpy.ones(X.shape) +# ''' +# 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)): +# G.add_node(i, p=V[i].p) +# for i in range(len(edgeList)): +# G.add_edge(edgeList[i].v0, edgeList[i].v1, weight = E[i].volume()) +# +# return G +#''' +#Returns the positions dictionary for the Circular layout. +#''' +#def getCircularLayout(graph, dim, radius): +# return nx.circular_layout(graph, dim, radius) +# +#''' +#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(self): + lower = self.N[0].p.copy() + upper = lower.copy() + for i in self.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 - Q = np.stack((X, Y, Z), 3) + #calculate the distance field at a given resolution + # R (triple) resolution along each dimension + def distancefield(self, R=(100, 100, 100)): + + #get a list of all node positions in the network + P = [] + for e in self.F: + for p in e.points: + P.append(p) + + #turn that list into a Numpy array so that we can create a KD tree + P = np.array(P) + + #generate a KD-Tree out of the network point array + tree = sp.spatial.cKDTree(P) + + plt.scatter(P[:, 0], P[:, 1]) + + #specify the resolution of the ouput grid + R = (200, 200, 200) + + #generate a meshgrid of the appropriate size and resolution to surround the network + lower, upper = self.aabb(self.N, self.F) #get the space occupied by the network + x = np.linspace(lower[0], upper[0], R[0]) #get the grid points for uniform sampling of this space + y = np.linspace(lower[1], upper[1], R[1]) + z = np.linspace(lower[2], upper[2], R[2]) + X, Y, Z = np.meshgrid(x, y, z) + #Z = 150 * numpy.ones(X.shape) + + + Q = np.stack((X, Y, Z), 3) + + + D, I = tree.query(Q) + + return D + #returns the number of points in the network + def npoints(self): + n = 0 + for f in self.F: + n = n + len(f.points) - 2 + n = n + len(self.N) + return n - D, I = tree.query(Q) + #returns the number of linear segments in the network + def nsegments(self): + n = 0 + for f in self.F: + n = n + len(f.points) - 1 + return n - return D - -#returns the number of points in the network -def npoints(N, F): - n = 0 - for f in F: - n = n + len(f.points) - 2 - n = n + len(N) - return n - -#returns the number of linear segments in the network -def nsegments(N, F): - n = 0 - for f in F: - n = n + len(f.points) - 1 - return n - -def vectorize(N, F): - #initialize three coordinate vectors () - n = nsegments(N, F) - X = np.zeros((n)) - Y = np.zeros((n)) - Z = np.zeros((n)) - idx = 0 - for i in range(0, len(F)): - for j in range(0, len(F[i].points)-1): - X[idx] = F[i].points[j][0]-F[i].points[j+1][0] - Y[idx] = F[i].points[j][1]-F[i].points[j+1][1] - Z[idx] = F[i].points[j][2]-F[i].points[j+1][2] - idx = idx + 1 - return X, Y, Z + def vectorize(self): + #initialize three coordinate vectors () + n = self.nsegments(self.N, self.F) + X = np.zeros((n)) + Y = np.zeros((n)) + Z = np.zeros((n)) + idx = 0 + for i in range(0, len(self.F)): + for j in range(0, len(self.F[i].points)-1): + X[idx] = self.F[i].points[j][0]-self.F[i].points[j+1][0] + Y[idx] = self.F[i].points[j][1]-self.F[i].points[j+1][1] + Z[idx] = self.F[i].points[j][2]-self.F[i].points[j+1][2] + idx = idx + 1 + return X, Y, Z -- libgit2 0.21.4