-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
185 lines (151 loc) · 5.4 KB
/
test.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
from pyspark.sql import SparkSession, SQLContext
from pyspark import SparkConf
import pyspark.sql.types as T
import pyspark.sql.functions as F
import numpy as np
import os
import sys
from pyspark.mllib.linalg.distributed import CoordinateMatrix, MatrixEntry
sys.path.insert(1, './utils')
sys.path.insert(1, './initializers')
sys.path.insert(1, './epi_models')
sys.path.insert(1, './scoring_models')
sys.path.insert(1, './edge_estimation_models')
from SparseDistributedMatrix import SparseDistributedMatrix
from SparseDistributedVector import SparseDistributedVector
from Initializer101 import Initializer101
import Simple_SIR as ssir
import ScoringWalker as sw
import StochasticEdgeEstimator as see
sys.path.insert(1, '.')
import SparkDependencyInjection as sdi
import PandiSimConfigInjection as pci
import PandiNetwork as pn
import PandiSim as ps
# os.environ['PYSPARK_SUBMIT_ARGS'] = '--packages graphframes:graphframes:0.8.0-spark3.0-s_2.12 pyspark-shell'
spark = SparkSession.builder.master('local')\
.config(key = "spark.default.parallelism", value = 4)\
.config(key = "spark.driver.memory", value = "4g")\
.config(key = "spark.executor.memory", value = "4g")\
.config(key = "spark.memory.fraction", value = "0.8")\
.getOrCreate()
# conf=SparkConf.set("spark.default.parallelism", 4)
sc = spark.sparkContext
sc.setCheckpointDir("hdfs://namenode:9000/rddch")
sqlc = SQLContext(sc)
logger = sc._jvm.org.apache.log4j
logger.LogManager.getLogger("org"). setLevel( logger.Level.ERROR )
logger.LogManager.getLogger("akka").setLevel( logger.Level.ERROR )
sdi.SparkDependencyInjection.set_spark(spark).set_spark_context(sc)
pci.PandiSimConfigInjection.set_write_to("d_pandisim").set_read_from("d_pandisim")
# a = SparseDistributedMatrix(sc, sc.parallelize([MatrixEntry(0, 0, 1),MatrixEntry(2, 0, 3),MatrixEntry(4, 0, 1)]), 4, 1).transpose()
# o = SparseDistributedMatrix.ones(sc, 4).transpose()
# r = o.dot(a).entries.collect()
# print(r)
# init = Initializer101(spark, 20,5)
# df = init.initialize_vertices()
# df.show()
# init.initialize_edges(df).show()
# sir = Simple_SIR()
# sir.run()
# print(sir.next_sotw())
# u = SparseDistributedVector(sc.parallelize([(0, 1), (1, 2), (2, 3)]), 3)
# v = SparseDistributedVector(sc.parallelize([(0, 1), (1, 2), (2, 3)]), 3)
# a = SparseDistributedMatrix(sc.parallelize([MatrixEntry(0, 1, 1.2),MatrixEntry(1, 0, 2.1),MatrixEntry(0, 2, 4)], 4), 3, 3)
# s = SparseDistributedVector(sc, sc.parallelize([(0, 1), (2, 3)]), 3)
# ones = SparseDistributedVector.repeat(sc, spark, 1, 4)
# twos = SparseDistributedVector.repeat(sc, spark, 2, 4)
# eye = SparseDistributedMatrix.diag(sc, ones)
# print(eye.dot(twos).rdd.collect())
# print(u.dot(a).rdd.collect())
# print(u.dot(a).rdd.collect())
# print(a.dot(u).rdd.collect())
# print(v.dot(u))
# print(v.outer(u).entries.collect())
# print(u.op(v).rdd.collect())
# init = Initializer101(6,3)
# init.initialize_vertices()
# init.initialize_edges(init.vertices)
# network = pn.PandiNetwork(init.vertices, init.edges, 6, 3)
# init.vertices.show()
# init.edges.show()
# (truncated, v) = network.verticesToSDV(cond = (F.col('health_status') != F.lit(-1)))
# print(v.rdd.collect())
# A = network.edgesToSDM(truncated)
# init.edges.show()
# print(A.entries.collect())
# print(e.dot(SparseDistributedMatrix.diag(sc, v)))
# se = v.apply(lambda x: np.exp(-2*x))
# D = SparseDistributedMatrix.diag(sc, se)
# M = A.dot(D)
# C = A.dot(se).apply(lambda x: 1/x).outer(SparseDistributedVector.repeat(sc, spark, 1, A.numRows()))
# P = M.multiply(C)
# print(M.entries.collect())
# print(C.entries.collect())
# r = P.transpose().dot(SparseDistributedVector.repeat(sc, spark, 1/P.numRows(), P.numRows()) )
# r = P.transpose().dot(r)
# r = P.transpose().dot(r)
# r = P.transpose().dot(r)
# r = P.transpose().dot(r)
# ns = v.apply(lambda x: 1 - x).dot(SparseDistributedMatrix.diag(sc, r))
# print(v.rdd.collect())
# print(v.op(ns, 'add').rdd.collect())
# sir = ssir.Simple_SIR(
# inits = {'S':0.9, 'I':0.1, 'R':0},
# params = {'beta':0.35, 'gamma':0.07, 'N':6, 't_end':20, 'step_size':1}
# )
# sir.run()
# dr = sir.current_sotw()[1]
# init = Initializer101(
# nbr_vertices = 6,
# nbr_edges = 2,
# nbr_infected = int(dr[0]),
# nbr_recovered = int(dr[1])
# )
# init = Initializer101(
# nbr_vertices = 20,
# nbr_edges = 4,
# nbr_infected = 3,
# nbr_recovered = 2
# )
# init.initialize_vertices()
# init.initialize_edges(init.vertices)
# network = pn.PandiNetwork(init.vertices, init.edges, init.nbr_vertices)
# network = init.toPandiNetwork()
# walker = sw.ScoringWalker(
# network,
# params = {'alpha-scaler':-2, 'walker-steps':3}
# )
# walker.run()
# walker.annotate((2,1))
# network.vertices.show()
# network.edges.show()
# edge_est = see.StochasticEdgeEstimator(
# network,
# params = {'SDF': 100, 'alpha': 80, 'beta': 100}
# )
# edge_est.run()
# network.vertices.show()
# network.edges.show(50, False)
# pandisim = ps.PandiSim(
# network = network,
# epi_model = sir,
# scoring_model = walker,
# edge_model = edge_est,
# params = {'take_screenshots':False}
# )
# pandisim.move()
# pandisim.take_screenshot()
# network.vertices.show()
# network.edges.show()
network = pn.PandiNetwork(None,None,6)
pandisim = ps.PandiSim(
network = network,
epi_model = None,
scoring_model = None,
edge_model = None,
params = {'take_screenshots':False}
)
pandisim.read_state(1)
network.vertices.show()
network.edges.show()