-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphClasses.py
More file actions
83 lines (64 loc) · 2.61 KB
/
graphClasses.py
File metadata and controls
83 lines (64 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import itertools as it
import numpy as np
import scipy as sp
import copy
class simpleGraph:
## custom class for graph manipulation
def __init__(self,maxdim = 15):
## max possible size of graph
# I think a max graph size is
# inevitable for a Markov-process
# graph generation scheme, which is
# where I think this will go
## number of verticies
self.nvert = maxdim
## number of edges
self.nedge = int(sp.special.comb(maxdim,2))
## attribued verticies list
self.ats = np.zeros((maxdim))
## attributed edge list
self.edges = np.zeros((self.nedge))
## unattributed edge list
self.uedges = np.zeros((self.nedge))
## holders for BO mat and AM
self.bomat = np.zeros((self.nvert,self.nvert))
self.am = np.zeros((self.nvert,self.nvert))
## holders for neighbors
self.neighbor1 = []
self.neighbor2 = []
## set internal counter
self.__intcount__ = 0
self.__indlist__ = range(maxdim)
def graphLoader(self,record):
## function to read in from csv
with open(record,'r') as f:
for ii, line in enumerate(f.readlines()):
if ii == 0:
self.ats = np.array([float(i) for i in line.split(",")])
self.nvert = len(self.ats)
self.bomat = np.zeros((self.nvert,self.nvert))
else:
self.bomat[ii-1,:] = np.array([float(i) for i in line.split(",")])
## now get the raw AM
self.am = copy.copy(self.bomat)
self.am[self.am > 1] = 1
## now do ordered attribute read for BO/AM matrices
for ii, c in enumerate(it.combinations(range(0,self.nvert),2)):
self.edges[ii] = self.bomat[c]
self.uedges[ii] = self.am[c]
## bulid out connectivtiy mape
self.__buildNeighborhoods()
def getVertexAts(self,ind):
return(self.ats[ind])
def getNeighbors1(self,ind):
return(list(it.compress(self.__indlist__,self.neighbor1[ind])))
def getNeighbors2(self,ind):
return(list(it.compress(self.__indlist__,self.neighbor2[ind])))
def __buildNeighborhoods(self):
## get inds of 1-neighbors
# from basic graph ops
amsq = np.matmul(self.am,self.am)
amsq -= np.diag(np.diag(amsq))
## populate neighborhoods
self.neighbor1 = self.am>0
self.neighbor2 = amsq>0