-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into development
- Loading branch information
Showing
10 changed files
with
406 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
# Based on example at https://stackoverflow.com/questions/46325447/animated-interactive-plot-using-matplotlib | ||
|
||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
from matplotlib.animation import FuncAnimation | ||
import mpl_toolkits.axes_grid1 | ||
import matplotlib.widgets | ||
|
||
|
||
class Player(FuncAnimation): | ||
def __init__( | ||
self, | ||
fig, | ||
func, | ||
frames=None, | ||
init_func=None, | ||
fargs=None, | ||
save_count=None, | ||
mini=0, | ||
maxi=100, | ||
pos=(0.125, 0.92), | ||
**kwargs | ||
): | ||
self.i = 0 | ||
self.min = mini | ||
self.max = maxi | ||
self.runs = True | ||
self.forwards = True | ||
self.fig = fig | ||
self.func = func | ||
self.setup(pos) | ||
FuncAnimation.__init__( | ||
self, | ||
self.fig, | ||
self.update, | ||
frames=self.play(), | ||
init_func=init_func, | ||
fargs=fargs, | ||
save_count=save_count, | ||
**kwargs | ||
) | ||
|
||
def play(self): | ||
while self.runs: | ||
self.i = self.i + self.forwards - (not self.forwards) | ||
if self.i > self.min and self.i < self.max: | ||
yield self.i | ||
else: | ||
self.stop() | ||
yield self.i | ||
|
||
def start(self): | ||
self.runs = True | ||
self.event_source.start() | ||
|
||
def stop(self, event=None): | ||
self.runs = False | ||
self.event_source.stop() | ||
|
||
def forward(self, event=None): | ||
self.forwards = True | ||
self.start() | ||
|
||
def backward(self, event=None): | ||
self.forwards = False | ||
self.start() | ||
|
||
def oneforward(self, event=None): | ||
self.forwards = True | ||
self.onestep() | ||
|
||
def onebackward(self, event=None): | ||
self.forwards = False | ||
self.onestep() | ||
|
||
def onestep(self): | ||
if self.i > self.min and self.i < self.max: | ||
self.i = self.i + self.forwards - (not self.forwards) | ||
elif self.i == self.min and self.forwards: | ||
self.i += 1 | ||
elif self.i == self.max and not self.forwards: | ||
self.i -= 1 | ||
self.func(self.i) | ||
self.slider.set_val(self.i) | ||
self.fig.canvas.draw_idle() | ||
|
||
def setup(self, pos): | ||
playerax = self.fig.add_axes([pos[0], pos[1], 0.64, 0.04]) | ||
divider = mpl_toolkits.axes_grid1.make_axes_locatable(playerax) | ||
bax = divider.append_axes("right", size="80%", pad=0.05) | ||
sax = divider.append_axes("right", size="80%", pad=0.05) | ||
fax = divider.append_axes("right", size="80%", pad=0.05) | ||
ofax = divider.append_axes("right", size="100%", pad=0.05) | ||
sliderax = divider.append_axes("right", size="500%", pad=0.07) | ||
self.button_oneback = matplotlib.widgets.Button(playerax, label="$\u29CF$") | ||
self.button_back = matplotlib.widgets.Button(bax, label="$\u25C0$") | ||
self.button_stop = matplotlib.widgets.Button(sax, label="$\u25A0$") | ||
self.button_forward = matplotlib.widgets.Button(fax, label="$\u25B6$") | ||
self.button_oneforward = matplotlib.widgets.Button(ofax, label="$\u29D0$") | ||
self.button_oneback.on_clicked(self.onebackward) | ||
self.button_back.on_clicked(self.backward) | ||
self.button_stop.on_clicked(self.stop) | ||
self.button_forward.on_clicked(self.forward) | ||
self.button_oneforward.on_clicked(self.oneforward) | ||
self.slider = matplotlib.widgets.Slider( | ||
sliderax, "", self.min, self.max, valinit=self.i | ||
) | ||
self.slider.on_changed(self.set_pos) | ||
|
||
def set_pos(self, i): | ||
self.i = int(self.slider.val) | ||
self.func(self.i) | ||
|
||
def update(self, i): | ||
self.slider.set_val(i) | ||
|
||
|
||
if __name__ == "__main__": | ||
### using this class is as easy as using FuncAnimation: | ||
|
||
fig, ax = plt.subplots() | ||
x = np.linspace(0, 6 * np.pi, num=100) | ||
y = np.sin(x) | ||
|
||
ax.plot(x, y) | ||
(point,) = ax.plot([], [], marker="o", color="crimson", ms=15) | ||
|
||
def update(i): | ||
point.set_data(x[i], y[i]) | ||
|
||
ani = Player(fig, update, maxi=len(y) - 1) | ||
|
||
plt.show() |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
from matplotlib import pyplot as plt | ||
|
||
from numpy import genfromtxt | ||
import numpy as np | ||
import math | ||
import os | ||
|
||
data = genfromtxt("simdata.csv", delimiter=",").T | ||
|
||
print("Loaded data: %s" % (str(data.shape))) | ||
|
||
t = data[0] | ||
|
||
x_offset = 1 | ||
y_offset = 2 | ||
d_offset = 3 | ||
|
||
""" | ||
for i in [(j*3)+y_offset for j in range(49)]: | ||
plt.plot(t,my_data[i],label=i) | ||
plt.legend()""" | ||
|
||
fig, ax = plt.subplots() | ||
plt.get_current_fig_manager().set_window_title("2D WormSim replay") | ||
ax.set_aspect("equal") | ||
|
||
usingObjects = os.path.isfile("objects.csv") | ||
if usingObjects: | ||
Objects = genfromtxt("objects.csv", delimiter=",") | ||
for o in Objects: | ||
x = o[0] | ||
y = o[1] | ||
r = o[2] | ||
# print("Circle at (%s, %s), radius %s"%(x,y,r)) | ||
circle1 = plt.Circle((x, y), r, color="b") | ||
plt.gca().add_patch(circle1) | ||
else: | ||
print("No objects found") | ||
|
||
num_t = 30 | ||
timesteps = len(t) | ||
|
||
# Using same variable names as WormView.m | ||
Sz = len(data) | ||
Nt = len(data[0]) | ||
Nbar = int((Sz - 1) / 3) | ||
NSEG = int(Nbar - 1) | ||
D = 80e-6 | ||
|
||
R = [ | ||
D / 2.0 * abs(math.sin(math.acos(((i) - NSEG / 2.0) / (NSEG / 2.0 + 0.2)))) | ||
for i in range(Nbar) | ||
] | ||
|
||
CoM = np.zeros([Nt, Nbar, 3]) | ||
CoMplot = np.zeros([Nt, 2]) | ||
Dorsal = np.zeros([Nbar, 2]) | ||
Ventral = np.zeros([Nbar, 2]) | ||
|
||
print(f"Sz: {Sz}, Nt: {Nt}, Nbar: {Nbar}, NSEG: {NSEG}") | ||
# Dt = data(2,1) - data(1,1); | ||
|
||
ventral_plot = None | ||
midline_plot = None | ||
dorsal_plot = None | ||
|
||
from Player import Player | ||
|
||
|
||
def update(ti): | ||
global dorsal_plot, ventral_plot, midline_plot | ||
f = ti / timesteps | ||
|
||
color = "#%02x%02x00" % (int(0xFF * (f)), int(0xFF * (1 - f) * 0.8)) | ||
print("Time step: %s, fract: %f, color: %s" % (ti, f, color)) | ||
ds = [] | ||
xs = [] | ||
ys = [] | ||
|
||
for i in [(j * 3) + d_offset for j in range(Nbar)]: | ||
ds.append(data[i][ti]) | ||
|
||
for i in [(j * 3) + x_offset for j in range(Nbar)]: | ||
xs.append(data[i][ti]) | ||
|
||
for i in [(j * 3) + y_offset for j in range(Nbar)]: | ||
ys.append(data[i][ti]) | ||
|
||
for j in range(Nbar): | ||
dX = R[j] * math.cos(ds[j]) | ||
dY = R[j] * math.sin(ds[j]) | ||
|
||
Dorsal[j, 0] = xs[j] + dX | ||
Dorsal[j, 1] = ys[j] + dY | ||
Ventral[j, 0] = xs[j] - dX | ||
Ventral[j, 1] = ys[j] - dY | ||
|
||
if dorsal_plot == None: | ||
(dorsal_plot,) = ax.plot(Dorsal[:, 0], Dorsal[:, 1], color="grey", linewidth=1) | ||
else: | ||
dorsal_plot.set_data(Dorsal[:, 0], Dorsal[:, 1]) | ||
|
||
if ventral_plot == None: | ||
(ventral_plot,) = ax.plot( | ||
Ventral[:, 0], Ventral[:, 1], color="grey", linewidth=1 | ||
) | ||
else: | ||
ventral_plot.set_data(Ventral[:, 0], Ventral[:, 1]) | ||
|
||
if midline_plot == None: | ||
(midline_plot,) = ax.plot( | ||
xs, ys, color="g", label="t=%sms" % t[ti], linewidth=0.5 | ||
) | ||
else: | ||
midline_plot.set_data(xs, ys) | ||
|
||
|
||
ax.plot() # Causes an autoscale update. | ||
|
||
ani = Player(fig, update, maxi=timesteps - 1) | ||
|
||
plt.show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.