-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpython_kinetics_aux.py
More file actions
156 lines (125 loc) · 4.58 KB
/
python_kinetics_aux.py
File metadata and controls
156 lines (125 loc) · 4.58 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
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
from IPython.display import display, clear_output
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import platform
import numpy as np
def animate_trajectory(s,loop=False,display_step=False,interval=10):
def update_frame(i, frame,text=None):
if equal_size:
x = [p.position.x for p in s.trajectory[i]]
y = [p.position.y for p in s.trajectory[i]]
frame.set_data([x,y])
else:
xy = [(p.position.x, p.position.y) for p in s.trajectory[i]]
frame.set_offsets(xy)
if display_step:
text.set_text(str(i))
if text:
return frame,text
else:
return frame,
def init():
return []
if platform.system() == 'Darwin':
blit=False
else:
blit=True
particle_size = s.particles[0].radius * 12
particle_sizes = [(p.radius*12)**2 for p in s.particles]
equal_size = len(set(particle_sizes))==1
no_steps = len(s.trajectory)
try:
length = s.box_length /10
except AttributeError:
length = 10
if not equal_size:
length = length/1.5
particle_sizes = [((p.radius*12)**2)/1.5**2 for p in s.particles]
fig = plt.figure(figsize=(length,length))
for t in s.tracks:
track_x=[]
track_y=[]
for vec in t.geometry:
track_x.append(vec.x)
track_y.append(vec.y)
plt.plot(track_x,track_y,color='black')
#plt.plot is faster than scatter but can only plot points of equal size
if equal_size:
frame, = plt.plot([],[], c='b',linestyle='', marker='o',markersize=particle_size ) # initialise plot
else:
frame = plt.scatter([],[], s=particle_sizes ) # initialise plot
if display_step:
ax = fig.gca()
text = ax.text(length/2,length/2,'')
else:
text = None
try:
plt.xlim(-2, s.box_length+2) # set x and y limits with a bit of extra padding
plt.ylim(-2, s.box_length+2)
except AttributeError:
plt.xlim(-2, 102) # set x and y limits with a bit of extra padding
plt.ylim(-2, 102)
frame_ani = animation.FuncAnimation(fig, update_frame, no_steps, fargs=(frame,text),
init_func=init,interval=interval, blit=blit, repeat=loop)
plt.show()
return frame_ani
def clunky_display_frame(s):
clear_output(wait=True)
try:
fig = s.fig
ax = s.ax
frame = s.frame
except AttributeError:
plt.ion()
s.fig = plt.figure(figsize=(10,10))
s.ax = s.fig.add_subplot(111)
s.ax.set_xlim(-2, s.box_length+2) # set x and y limits with a bit of extra padding
s.ax.set_ylim(-2, s.box_length+2)
s.frame, = s.ax.plot([],[],c='b',linestyle='', marker='o',markersize=16.5)
fig=s.fig
ax=s.ax
frame=s.frame
plt.show(False)
particle_x = [p.position.x for p in s.particles]
particle_y = [p.position.y for p in s.particles]
frame.set_data([particle_x,particle_y])
#fig.canvas.draw()
plt.show()
display(fig)
def display_particle(p):
plt.figure(figsize=(10,10))
plt.plot([p.position.x],[p.position.y],marker='o',linestyle='', markersize=12)
plt.show()
def display_trajectory(particles):
def update_frame(i, frame):
x=[particles[i].position.x]
y=[particles[i].position.y]
frame.set_data([x,y])
return frame,
def init():
return []
if platform.system() == 'Darwin':
blit=False
else:
blit=True
fig = plt.figure(figsize=(10,10))
no_steps = len(particles)
frame, = plt.plot([],[],marker='o',linestyle='', markersize=12)
min_x,max_x = min([p.position.x for p in particles]), max([p.position.x for p in particles])
min_y,max_y = min([p.position.y for p in particles]), max([p.position.y for p in particles])
if min_x != max_x:
plt.xlim(min_x,max_x)
else:
plt.xlim(min_x-5, max_x+5)
if min_y != max_y:
plt.ylim(min_y,max_y)
else:
plt.ylim(min_y-5,max_y+5)
frame_ani = animation.FuncAnimation(fig, update_frame, no_steps, fargs=(frame,), interval=5,
init_func=init,blit=blit, repeat=False)
plt.show()
return frame_ani
try:
from ipywidgets import interactive
except ImportError:
from IPython.html.widgets import interactive