-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphClass.py
159 lines (125 loc) · 3.77 KB
/
graphClass.py
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#!/usr/bin/env python
#-*- coding:utf-8 -*-
""" GraphClass: graph generation and management """
import sys
sys.path.append("tools/")
from graphTools import genGraphER, genGraphFS, genGraphEDR
import numpy as np
from graph_tool import *
from graph_tool.spectral import adjacency
from copy import deepcopy
#
#---
# GraphClass
#------------------------
class GraphClass:
#------------#
# Initialize #
#------------#
def __init__(self, dicProp={"Name": "Graph", "Type": "None", "Weighted": False}, graph=None):
# on récupère les propriétés
self.dicProperties = deepcopy(dicProp)
self.dicGenGraphType = {"Erdos-Renyi": genGraphER,
"Free-scale": genGraphFS,
"EDR": genGraphEDR}
# on génère un graphe
if graph is not None:
self.graph = gtGraph.copy()
self.updateProp()
elif dicProp["Type"] == "None":
self.graph = Graph()
else:
self.graph = self.dicGenGraphType[dicProp["Type"]](self.dicProperties)
self.setName()
self.updateProp()
#------------------#
# Graph management #
#------------------#
## copying
def copyGraph(self,gtGraphToCopy):
self.graph = gtGraphToCopy.copy()
def deepcopyGraph(self,gtGraphToCopy):
self.graph = Graph(gtGraphToCopy)
self.updateProp()
## set graph to existing graph-tool graph
def setGraph(self,gtGraphToCopy):
self.graph = gtGraphToCopy
self.updateProp()
## excitatory and exhibitory subgraphs
def genInhibSubgraph(self):
graph = self.graph.copy()
epropType = graph.new_edge_property("bool",-graph.edge_properties["type"].a+1)
graph.set_edge_filter(epropType)
inhibGraph = GraphClass()
inhibGraph.setGraph(Graph(graph,prune=True))
inhibGraph.setProp("Weighted", True)
return inhibGraph
def genExcSubgraph(self):
graph = self.graph.copy()
epropType = graph.new_edge_property("bool",graph.edge_properties["type"].a+1)
graph.set_edge_filter(epropType)
excGraph = GraphClass()
excGraph.setGraph(Graph(graph,prune=True))
excGraph.setProp("Weighted", True)
return excGraph
#---------------#
# Set functions #
#---------------#
def setName(self, strName = ""):
if strName:
self.dicProperties["Name"] = strName
else:
string = self.dicProperties["Type"]
for key,value in self.dicProperties.items():
if (key != "Type") and (key != "Weighted") and (value.__class__ != dict):
string += key[0] + str(value)
self.dicProperties["Name"] = string
def setDicProp(self,dicProp):
for strKey,value in dicProp.items():
self.dicProperties[strKey] = value
def setProp(self, strProp, valProp):
self.dicProperties[strProp] = valProp
#---------------#
# Get functions #
#---------------#
def getName(self):
return self.dicProperties["Name"]
def getGraph(self):
return self.graph
def getNodes(self):
return self.dicProperties["Nodes"]
def getNumEdges(self):
return self.dicProperties["Edges"]
def getProp(self):
return self.xmlProp
def isWeighted(self):
return self.dicProperties["Weighted"]
def getAdjacency(self):
if self.isWeighted():
return adjacency(self.graph, self.graph.edge_properties["weight"])
else:
return adjacency(self.graph)
#----------#
# Updating #
#----------#
def updateProp(self, dicProp=None, lstProp=None):
# basic properties
nNodes = self.graph.num_vertices()
self.dicProperties["Nodes"] = nNodes
nEdges = self.graph.num_edges()
self.dicProperties["Edges"] = nEdges
self.dicProperties["Density"] = nEdges / float(np.square(nNodes))
# special properties
if dicProp == None and lstProp == None:
None
elif lstProp == None:
for prop in dicProp.keys():
self.dicProperties[prop] = dicProp[prop]
else:
if "FracInhib" in lstProp:
self.dicProperties["FracInhib"] = getNumInhib(self.graph)/float(nEdges)
#-----------#
# Destroyer #
#-----------#
def __del__(self):
print('Graph died')