Skip to content

Commit b6ddf10

Browse files
committed
abandon profile go for possible sample by tomorrow
1 parent ab3387d commit b6ddf10

File tree

3 files changed

+206
-67
lines changed

3 files changed

+206
-67
lines changed

lib/config.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,11 @@
7878
}
7979
}
8080

81-
param_sample_size = 1
82-
stigma_sample_size = 10000
81+
param_sample_size = 500
82+
stigma_sample_size = 50000
8383
# scale_free_bounds = (10**(-.1),10**(.1))
84-
scale_free_bounds = (10**(-.25),10**(.25))
85-
# scale_free_bounds = (10**-1,10**1)
84+
# scale_free_bounds = (10**(-.25),10**(.25))
85+
scale_free_bounds = (10**-1,10**1)
8686
# scale_free_bounds = (10**-3,10**3)
8787
small_val = .05
8888

lib/graph_local_classes.py

+63-63
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ def __init__(self, nodes, edges):
1313
self.edges = sorted(edges)
1414
self.child_edges = {node: self.children_edges(node) for node in self.nodes}
1515

16+
def edge_mat_index(self,edge_mat):
17+
return np.array([self.edges.index(e) for e in edge_mat])
18+
1619
@classmethod
1720
def from_networkx(cls, graph, data=False):
1821
# cls == GraphStructure
@@ -261,25 +264,30 @@ def sample_edge_first_event_only(self, edge, time):
261264
return (event_time, edge[1])
262265

263266
# @profile
264-
# def sample_edge_first_event_only_tuple(self, edge, time):
265-
# index = self.structure.edges.index(edge)
266-
267-
# # does it occur?
268-
# occurs = np.random.rand() < self.params.p
269-
# if not occurs:
270-
# return []
267+
def sample_edge_mat_first_event_only(self, edge_mat, time_vec):
268+
# index_mat = self.structure.edge_mat_index(edge_mat)
269+
index_mat = np.array([self.structure.edges.index(e) for e in edge_mat])
270+
time_mat = time_vec[:,np.newaxis]
271+
event_out = np.zeros(shape=index_mat.shape)
272+
node_out = np.array([e[1] for e in edge_mat])
273+
# does it occur?
274+
occurs = np.random.rand(index_mat.shape) < self.params.p
275+
event_out[occurs],node_out[occurs] = None
276+
# if not occurs:
277+
# return None
271278

272-
# # how many events?
273-
# num_events = np.random.poisson(lam=self.params.mu[index])
274-
# if num_events == 0:
275-
# return []
279+
# how many events?
280+
num_events = np.random.poisson(lam=np.array([self.params.mu[index] for index in index_mat]))
281+
event_out[num_events==0],node_out[num_events==0] = None
282+
# if num_events == 0:
283+
# return None
276284

277-
# # when do those events occur?
278-
# event_time = time + np.random.exponential(scale=self.params.r[index]/num_events, size=1)
279-
# # event_times.sort()
280-
# # return [(t, edge[1]) for t in event_times]
281-
# return [(event_time, edge[1])]
282-
285+
# when do those events occur?
286+
event_time = time_mat + np.random.exponential(scale=np.array(self.params.r[index]/num_events, size=1))
287+
# event_times.sort()
288+
# return [(t, edge[1]) for t in event_times]
289+
# import ipdb; ipdb.set_trace()
290+
return (event_time, edge_mat[1])
283291

284292
def _sample(self, first_only=True, max_time=4.0):
285293
pending = [(self.init_time, self.init_node)]
@@ -368,58 +376,46 @@ def _sample_solely_first_events(self, max_time=4.0):
368376
return np.array([self._first_events[node] for node in self.structure.nodes])
369377

370378
# @profile
371-
def _sample_solely_first_events_better(self, max_time=4.0):
372-
pending = []
373-
heapq.heappush(pending,(self.init_time, self.init_node))
374-
# pending = [(self.init_time, self.init_node)]
375-
self._first_events = {node: np.inf for node in self.structure.nodes}
376-
processed_nodes = set()
377-
structure_nodes = set(self.structure.nodes)
378-
while len(pending) > 0:
379-
# time, node = pending.pop(0)
380-
# import ipdb; ipdb.set_trace()
381-
time, node = heapq.heappop(pending)
382-
383-
if processed_nodes==structure_nodes or time >= max_time:
384-
# this only works if it is first event only
385-
break
386-
387-
if node in processed_nodes:
388-
continue
389-
else:
390-
self._first_events[node] = time
391-
processed_nodes.add(node)
392-
393-
394-
#self._all_events.append((time, node))
395-
# if time < self._first_events[node]:
396-
# self._first_events[node] = time
379+
def _sample_multi_first_event_sets(self, k=1, max_time=4.0):
397380

381+
tmp_array = np.zeros(shape=(k,len(self.structure.nodes)))
382+
heaplist = [[]]*k
383+
for i in range(k):
384+
self._first_events = {node: np.inf for node in self.structure.nodes}
385+
pending = heaplist[i]
386+
heapq.heappush(pending,(self.init_time, self.init_node))
387+
# pending = [(self.init_time, self.init_node)]
388+
# self._all_events = []
389+
# short_circuit = False
390+
# import ipdb; ipdb.set_trace()
391+
processed_nodes = set()
392+
structure_nodes = set(self.structure.nodes)
393+
while len(pending) > 0:
394+
time, node = heapq.heappop(pending)
395+
if time >= max_time:
396+
break
398397

399-
# if processed_nodes==structure_nodes:
400-
# this only works if it is first event only
401-
# break
402398

403-
children_edges = self.structure.child_edges[node]
404-
# this goes to each of the children_edges of the node and initiates events along that edge
405-
# import ipdb; ipdb.set_trace()
406-
for edge in children_edges:
407-
if edge[1] in processed_nodes:
399+
if node in processed_nodes:
408400
continue
409401
else:
410-
child_events = self.sample_edge_first_event_only(edge, time)
411-
# import ipdb; ipdb.set_trace()
412-
if not child_events:
402+
processed_nodes.add(node)
403+
self._first_events[node] = time
404+
if processed_nodes==structure_nodes:
405+
# this only works if it is first event only
406+
break
407+
408+
children_edges = self.structure.child_edges[node]
409+
for edge in children_edges:
410+
if edge[1] in processed_nodes:
413411
continue
414-
# import ipdb; ipdb.set_trace()
415-
heapq.heappush(pending,child_events)
416-
# import ipdb; ipdb.set_trace()
417-
# pending.sort()
418-
419-
#self._all_events.sort()
420-
#self._compute_first_events()
421-
#return np.array(self._first_events)
422-
return np.array([self._first_events[node] for node in self.structure.nodes])
412+
else:
413+
child_events = self.sample_edge_first_event_only(edge, time)
414+
if not child_events:
415+
continue
416+
heapq.heappush(pending,child_events)
417+
tmp_array[i] = np.array([self._first_events[node] for node in self.structure.nodes])
418+
return tmp_array
423419

424420
def sample(self, k=1, first_only=True, max_time=4.0):
425421
first_events = np.zeros((k, len(self.structure.nodes)))
@@ -441,6 +437,10 @@ def sample_solely_first_events(self, k=1, first_only=True, max_time=4.0):
441437
# self._first_events = {node: np.inf for node in self.structure.nodes}
442438

443439
return first_events
440+
441+
# def sample_solely_first_events(self, k=1, first_only=True, max_time=4.0):
442+
# # first_events = np.zeros((k, len(self.structure.nodes)))
443+
# return self._samplee_multi_first_event_sets(k=k,max_time=max_time)
444444

445445
def sample_iter_solely_first_events(self, k=1, first_only=True, max_time=4.0):
446446
for i in range(k):

profile_code.ipynb

+139
Original file line numberDiff line numberDiff line change
@@ -3217,6 +3217,145 @@
32173217
"np.tile([0,1,2],[5])"
32183218
]
32193219
},
3220+
{
3221+
"cell_type": "code",
3222+
"execution_count": 64,
3223+
"metadata": {
3224+
"collapsed": false
3225+
},
3226+
"outputs": [
3227+
{
3228+
"data": {
3229+
"text/plain": [
3230+
"[[], [], [], [], [], [], [], [], [], []]"
3231+
]
3232+
},
3233+
"execution_count": 64,
3234+
"metadata": {},
3235+
"output_type": "execute_result"
3236+
}
3237+
],
3238+
"source": [
3239+
"[[]]*10"
3240+
]
3241+
},
3242+
{
3243+
"cell_type": "code",
3244+
"execution_count": 65,
3245+
"metadata": {
3246+
"collapsed": false
3247+
},
3248+
"outputs": [
3249+
{
3250+
"data": {
3251+
"text/plain": [
3252+
"array([7, 2])"
3253+
]
3254+
},
3255+
"execution_count": 65,
3256+
"metadata": {},
3257+
"output_type": "execute_result"
3258+
}
3259+
],
3260+
"source": []
3261+
},
3262+
{
3263+
"cell_type": "code",
3264+
"execution_count": 69,
3265+
"metadata": {
3266+
"collapsed": false
3267+
},
3268+
"outputs": [
3269+
{
3270+
"data": {
3271+
"text/plain": [
3272+
"array([[0, 1],\n",
3273+
" [2, 3]])"
3274+
]
3275+
},
3276+
"execution_count": 69,
3277+
"metadata": {},
3278+
"output_type": "execute_result"
3279+
}
3280+
],
3281+
"source": [
3282+
"\n",
3283+
"test = np.array([[1,2],[1,2]])\n",
3284+
"np.random.poisson(lam=test)"
3285+
]
3286+
},
3287+
{
3288+
"cell_type": "code",
3289+
"execution_count": 105,
3290+
"metadata": {
3291+
"collapsed": false
3292+
},
3293+
"outputs": [
3294+
{
3295+
"name": "stdout",
3296+
"output_type": "stream",
3297+
"text": [
3298+
"0\n",
3299+
"1\n"
3300+
]
3301+
}
3302+
],
3303+
"source": [
3304+
"test = np.array([x for x in range(16)])\n",
3305+
"test.resize((2,8))\n",
3306+
"test2 = np.array([print(i) for i,x in enumerate(test)])"
3307+
]
3308+
},
3309+
{
3310+
"cell_type": "code",
3311+
"execution_count": 101,
3312+
"metadata": {
3313+
"collapsed": false
3314+
},
3315+
"outputs": [
3316+
{
3317+
"name": "stdout",
3318+
"output_type": "stream",
3319+
"text": [
3320+
"[0 1]\n"
3321+
]
3322+
}
3323+
],
3324+
"source": [
3325+
"\n",
3326+
"print(test2)"
3327+
]
3328+
},
3329+
{
3330+
"cell_type": "code",
3331+
"execution_count": 94,
3332+
"metadata": {
3333+
"collapsed": false
3334+
},
3335+
"outputs": [
3336+
{
3337+
"name": "stdout",
3338+
"output_type": "stream",
3339+
"text": [
3340+
"[0 1 2 3 4 5 6 7]\n",
3341+
"[ 8 9 10 11 12 13 14 15]\n"
3342+
]
3343+
},
3344+
{
3345+
"data": {
3346+
"text/plain": [
3347+
"[None, None]"
3348+
]
3349+
},
3350+
"execution_count": 94,
3351+
"metadata": {},
3352+
"output_type": "execute_result"
3353+
}
3354+
],
3355+
"source": [
3356+
"[print(x) for x in test]"
3357+
]
3358+
},
32203359
{
32213360
"cell_type": "code",
32223361
"execution_count": null,

0 commit comments

Comments
 (0)