Skip to content

Commit a5735a2

Browse files
committed
Improved display of times etc.
1 parent 00a072e commit a5735a2

File tree

1 file changed

+74
-22
lines changed

1 file changed

+74
-22
lines changed

SiberneticReplay.py

Lines changed: 74 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,16 @@
2222
all_points = []
2323
all_point_types = []
2424

25-
2625
plotter = None
2726
offset3d_ = (0, 0, 0)
2827
slider = None
2928

3029
show_boundary = False
3130

31+
max_time = None
32+
33+
verbose = False
34+
3235

3336
def get_color_info_for_type(type_):
3437
"""
@@ -37,15 +40,19 @@ def get_color_info_for_type(type_):
3740
"""
3841

3942
if type_ == 1.1:
40-
return "#8BEBFC", "liquid 1", 5
43+
return "cyan", "liquid 1", 2
4144
elif type_ == 1.2:
42-
return "#3ACFF0", "liquid 2", 5
45+
return "red", "liquid 2", 10
46+
4347
elif type_ == 2.1:
44-
return "yellow", "elastic 1", 5
48+
return "pink", "elastic 1", 5
4549
elif type_ == 2.2:
4650
return "#FF0000", "elastic 2", 5
51+
elif type_ >= 2.3 and type_ < 2.4:
52+
return "lightyellow", "elastic variable", 5
4753
elif type_ > 2 and type_ < 3:
48-
return "#00cc00", "elastic variable", 5
54+
return "#00cc00", "elastic variable", 7
55+
4956
elif type_ == 3:
5057
return "grey", "boundary 0", 3
5158
elif type_ == 3.1:
@@ -69,7 +76,8 @@ def add_sibernetic_model(
6976
plotter, \
7077
offset3d_, \
7178
slider, \
72-
show_boundary
79+
show_boundary, \
80+
max_time
7381

7482
offset3d_ = offset3d
7583
plotter = pl
@@ -83,6 +91,8 @@ def add_sibernetic_model(
8391
time_count = 0
8492
logStep = None
8593

94+
dt = None
95+
8696
report_data = None
8797
count_point_types = {}
8898

@@ -91,6 +101,14 @@ def add_sibernetic_model(
91101
report_data = json.load(open(report_file, "r"))
92102
print(report_data)
93103
position_file = os.path.join(sim_dir, "position_buffer.txt")
104+
dt = float(report_data.get("dt").split(" ")[0])
105+
duration = float(report_data.get("duration").split(" ")[0])
106+
max_time = duration
107+
time_points = np.linspace(0, duration, int(duration / dt) + 1)
108+
print(
109+
"Simulation dt: %s ms, duration: %s ms, times: %s"
110+
% (dt, duration, time_points)
111+
)
94112

95113
if "worm" in report_data["configuration"]:
96114
muscle_activation_file = os.path.join(
@@ -104,8 +122,14 @@ def add_sibernetic_model(
104122

105123
f, ax = plt.subplots(tight_layout=True)
106124
ax.imshow(musc_dat, interpolation="none", aspect="auto", cmap="YlOrRd")
125+
126+
num_ticks = 5
127+
ax.set_xticks(np.linspace(0, musc_dat.shape[1], num_ticks))
128+
ax.set_xticklabels(np.linspace(0, duration, num_ticks))
129+
# quit()
130+
107131
# ax.set_ylim([-1, 1])
108-
ax.set_xlabel("Time (s)")
132+
ax.set_xlabel("Time (ms)")
109133
_ = ax.set_ylabel("Muscle")
110134

111135
h_chart = pv.ChartMPL(f, size=(0.35, 0.35), loc=(0.02, 0.06))
@@ -187,23 +211,38 @@ def add_sibernetic_model(
187211

188212
create_mesh(0)
189213

190-
max_time = len(all_points) - 1
214+
slider_text = "Time point"
215+
if max_time is None:
216+
max_time = len(all_points) - 1
217+
else:
218+
slider_text = "Time (ms)"
191219

192220
slider = pl.add_slider_widget(
193-
create_mesh, rng=[0, max_time], value=0, title="Time point", style="modern"
221+
create_mesh, rng=[0, max_time], value=0, title=slider_text, style="modern"
194222
)
195223

196224
pl.add_checkbox_button_widget(play_animation, value=False)
197225

198226

199-
def play_animation(play):
227+
def play_animation(play_button_active):
200228
global plotter, last_meshes, all_points, all_point_types, replaying, slider
201-
print("Playing animation: %s" % play)
229+
print(
230+
f"Animation button pressed. Button active {play_button_active}; replaying: {replaying}, slider value: {slider.GetSliderRepresentation().GetValue()}"
231+
)
202232

203-
if not play:
204-
replaying = False
205-
print("Animation stopped.")
206-
return
233+
if not play_button_active:
234+
if not replaying:
235+
print("Animation already stopped - restarting")
236+
slider.GetSliderRepresentation().SetValue(0)
237+
curr_time = slider.GetSliderRepresentation().GetValue()
238+
replaying = True
239+
plotter.update()
240+
plotter.render()
241+
else:
242+
curr_time = slider.GetSliderRepresentation().GetValue()
243+
replaying = False
244+
print("Animation stopped at %s." % curr_time)
245+
return
207246
else:
208247
replaying = True
209248
print("Animation started.")
@@ -218,8 +257,8 @@ def play_animation(play):
218257
curr_time = slider.GetSliderRepresentation().GetValue()
219258

220259
print(
221-
" --- Animating step %i (curr_time: %s) of %i, %s"
222-
% (i, curr_time, len(all_points), play)
260+
" --- Animating step %i/%i (curr_time: %s) of %i, %s"
261+
% (i, len(all_points), curr_time, len(all_points), play_button_active)
223262
)
224263
next_time = curr_time + 1
225264
slider.GetSliderRepresentation().SetValue(next_time)
@@ -229,6 +268,8 @@ def play_animation(play):
229268
plotter.render()
230269
time.sleep(replay_speed)
231270

271+
replaying = False
272+
232273

233274
def create_mesh(step):
234275
step_count = step
@@ -255,10 +296,11 @@ def create_mesh(step):
255296
if show_boundary is False and is_boundary:
256297
continue
257298

258-
print(
259-
" - Plotting %i points of type '%s' (%s), color: %s, size: %i"
260-
% (len(curr_points), type_, info, color, size)
261-
)
299+
if verbose:
300+
print(
301+
" - Plotting %i points of type '%s' (%s), color: %s, size: %i"
302+
% (len(curr_points), type_, info, color, size)
303+
)
262304

263305
if len(curr_points) == 0:
264306
continue
@@ -320,12 +362,22 @@ def create_mesh(step):
320362
swap_y_z=True,
321363
include_boundary=include_boundary,
322364
)
365+
plotter.window_size = [1600, 800]
366+
323367
plotter.set_background("white")
324368
plotter.add_axes()
325369
plotter.camera_position = "zx"
326370
plotter.camera.roll = 90
327371
plotter.camera.elevation = 45
328372
print(plotter.camera_position)
329373

374+
def on_close_callback(plotter):
375+
global replaying
376+
print(
377+
f"Plotter window is closing. Performing actions now (replaying: {replaying})."
378+
)
379+
replaying = False
380+
330381
if "-nogui" not in sys.argv:
331-
plotter.show()
382+
plotter.show(before_close_callback=on_close_callback, auto_close=True)
383+
print("Done showing")

0 commit comments

Comments
 (0)