-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalysis.py
133 lines (125 loc) · 6.04 KB
/
analysis.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
import numpy as np
import numpy.linalg as lag
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import matplotlib.animation as animation
import os
import shutil as sh
import glob
class Analysis:
def __init__(self,foldername,parameters,verbose=False,stride=1):
self.parameters = parameters
self.foldername = foldername
self.verbose = verbose
self.load_data(stride=stride)
if verbose:
print("Loaded Data")
def load_data(self,stride = 1):
npzfiles = glob.glob(f"{self.foldername}/*.npz")
self.times = [float(file.split("_")[-1][:-4]) for file in npzfiles]
# sort files based on times
npzfiles = [file for _,file in sorted(zip(self.times,npzfiles))]
n_files = len(npzfiles)//stride
self.location_data = np.zeros([n_files,self.parameters["N"],self.parameters["dimension"]])
self.state_data = np.zeros([n_files,self.parameters["N"],self.parameters["n_states"]])
self.velocity_data = np.zeros([n_files,self.parameters["N"],self.parameters["dimension"]])
self.signal_data = np.zeros([n_files,self.parameters["N"]])
only_locations = False
for i,file in enumerate(npzfiles[::stride]):
data = np.load(file)
self.location_data[i] = data["locations"]
if not only_locations:
try:
self.state_data[i] = data["states"]
self.velocity_data[i] = data["velocities"]
self.signal_data[i] = data["signal"]
except KeyError:
print("Only loaction data available")
only_locations = True
def plot(self,i):
fig, ax = plt.subplots()
speeds = lag.norm(self.velocity_data, axis=1)
cmap = cm.get_cmap("coolwarm")
ax.quiver(
self.location_data[i,:, 0],
self.location_data[i,:, 1],
self.velocity_data[i,:, 0],
self.velocity_data[i,:, 1],
# color=cmap(speeds),
scale=20,
)
# ax.quiver(self.location_data[:,0],self.location_data[:,1],self.velocity_data[:,0],self.velocity_data[:,1],cmap="viridis")
ax.set_xlim(0, self.parameters["Lx"])
ax.set_ylim(0, self.parameters["Ly"])
ax.axis("equal")
ax.axis("off")
def plot_density(self,i,ax=None,zmax=50,nedges=21):
locations = self.location_data[i,:,:]
if self.verbose:
print("Solving for histogram ... ")
xedges = np.linspace(0,self.parameters["Lx"],nedges)
yedges = np.linspace(0,self.parameters["Ly"],nedges)
H,xedges,yedges = np.histogram2d(locations[:,0],locations[:,1],bins=(xedges, yedges))
if self.verbose:
print("Plotting ... ")
if ax is None:
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set(xlim=(0, self.parameters["Lx"]), ylim=(0, self.parameters["Ly"]),zlim=(0, zmax))
X, Y = np.meshgrid(xedges[:-1], yedges[:-1])
ax.plot_surface(X, Y, H, cmap=cm.coolwarm,vmin=0,vmax=zmax)
return ax
def animate(self,stride=1):
fig, ax = plt.subplots()
fig.set_size_inches(6,6)
cmap = cm.get_cmap("jet")
self.speeds = lag.norm(self.velocity_data[0],axis=1)
quiver = ax.quiver(self.location_data[0,:,0],self.location_data[0,:,1],self.velocity_data[0,:,0],self.velocity_data[0,:,1],scale=20,cmap="coolwarm")
quiver.set_UVC(self.velocity_data[0,:,0],self.velocity_data[0,:,1],self.speeds)
colors = cmap((self.speeds-self.speeds.min())/(self.speeds.max()-self.speeds.min()))
# ax.set_prop_cycle('color',colors)
# plot the trajectories, coloring each line based on the speed of the cell
lines,= ax.plot(self.location_data[0,0,0],self.location_data[0,0,1])
ax.axis("off")
ax.axis("equal")
ax.set(xlim=(0,self.parameters["Lx"]),ylim=(0,self.parameters["Ly"]))
def update(j):
lines.set_data(self.location_data[:j*stride,0,0],self.location_data[:j*stride,0,1])
i = j*stride
quiver.set_offsets(self.location_data[i])
self.speeds = lag.norm(self.velocity_data[i],axis=1)
quiver.set_UVC(self.velocity_data[i,:,0],self.velocity_data[i,:,1],self.speeds)
anim = animation.FuncAnimation(fig,update,frames=self.location_data.shape[0]//stride,interval=10)
return anim
def animate_dots(self,stride=1):
fig, ax = plt.subplots()
fig.set_size_inches(6,6)
self.speeds = lag.norm(self.velocity_data[0],axis=1)
scatter = ax.scatter(self.location_data[0,:,0],self.location_data[0,:,1],c="k",s=0.5)
X,Y = np.meshgrid(np.linspace(0,self.parameters["Lx"],100),np.linspace(0,self.parameters["Ly"],100))
chemical_function = self.parameters["chemical"]
ax.imshow(chemical_function(X,Y),extent=(0,self.parameters["Lx"],0,self.parameters["Ly"]),origin="lower",cmap="coolwarm",alpha=0.5)
ax.axis("off")
ax.axis("equal")
ax.set(xlim=(0,self.parameters["Lx"]),ylim=(0,self.parameters["Ly"]))
def update(j):
i = j*stride
scatter.set_offsets(self.location_data[i])
anim = animation.FuncAnimation(fig,update,frames=self.location_data.shape[0]//stride,interval=10)
return anim
def animate_density(self,stride=1,nedges=21):
fig = plt.figure()
xedges = np.linspace(0,self.parameters["Lx"],nedges)
yedges = np.linspace(0,self.parameters["Ly"],nedges)
H,xedges,yedges = np.histogram2d(self.location_data[-1,:,0],self.location_data[-1,:,1],bins=(xedges, yedges))
zmax = H.max()
print(zmax)
ax = fig.add_subplot(111, projection='3d')
fig.set_size_inches(6,6)
ax1 = self.plot_density(0,ax,zmax=zmax,nedges=nedges)
def update(j):
i = j*stride
ax.clear()
ax1 = self.plot_density(i,ax,zmax,nedges=nedges)
anim = animation.FuncAnimation(fig,update,frames=self.location_data.shape[0]//stride,interval=10)
return anim