Skip to content

Commit 46626e9

Browse files
committed
Better display of time
1 parent 26712f7 commit 46626e9

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

Player.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ def __init__(
1919
mini=0,
2020
maxi=100,
2121
pos=(0.125, 0.92),
22+
times=None,
23+
t_units="",
2224
**kwargs,
2325
):
2426
self.i = 0
@@ -39,6 +41,8 @@ def __init__(
3941
save_count=save_count,
4042
**kwargs,
4143
)
44+
self.times = times
45+
self.t_units = t_units
4246

4347
def play(self):
4448
while self.runs:
@@ -82,6 +86,7 @@ def onestep(self):
8286
self.i -= 1
8387
self.func(self.i)
8488
self.slider.set_val(self.i)
89+
8590
self.fig.canvas.draw_idle()
8691

8792
def setup(self, pos):
@@ -105,14 +110,32 @@ def setup(self, pos):
105110
self.slider = matplotlib.widgets.Slider(
106111
sliderax, "", self.min, self.max, valinit=self.i
107112
)
113+
self.slider.valtext.set_visible(False)
108114
self.slider.on_changed(self.set_pos)
115+
self.slider.label.set_horizontalalignment("left")
116+
label_position = self.slider.label.get_position() # Get the current position
117+
self.slider.label.set_position(
118+
(label_position[0] + 1.1, label_position[1])
119+
) # Adjust x-coordinate
120+
121+
# self.text_box = matplotlib.widgets.TextBox(textax, label="", initial="0 ms")
109122

110123
def set_pos(self, i):
111124
self.i = int(self.slider.val)
112125
self.func(self.i)
126+
self.set_time_text(self.i)
127+
128+
def set_time_text(self, i):
129+
time_text = str(i)
130+
if self.times is not None:
131+
time_text = "%s%s" % (self.times[i], self.t_units)
132+
133+
# self.text_box.set_val(time_text)
134+
self.slider.label.set_text(time_text)
113135

114136
def update(self, i):
115137
self.slider.set_val(i)
138+
self.set_time_text(i)
116139

117140

118141
if __name__ == "__main__":

WormView.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,22 @@ def main():
127127

128128
ax.set_ylim([-1.5, 1.5])
129129

130-
t = np.array(wcon["data"][0]["t"])
130+
t_units = ""
131+
x_units = ""
132+
y_units = ""
133+
134+
if "units" in wcon:
135+
t_units = wcon["units"].get("t")
136+
x_units = wcon["units"].get("x")
137+
y_units = wcon["units"].get("y")
138+
print(f"Time units: {t_units}, x units: {x_units}, y units: {y_units}")
139+
140+
times = np.array(wcon["data"][0]["t"])
131141
x = np.array(wcon["data"][0]["x"]).T
132142
y = np.array(wcon["data"][0]["y"]).T
133143

134144
print(
135-
f"Range of time: {t[0]}->{t[0]}; x range: {x.max()}->{x.min()}; y range: {y.max()}->{y.min()}"
145+
f"Range of time: {times[0]}{t_units}->{times[-1]}{t_units}; x range: {x.max()}{x_units}->{x.min()}{x_units}; y range: {y.max()}{y_units}->{y.min()}{y_units}"
136146
)
137147
factor = 0.05
138148
if abs(x.max() - x.min()) > abs(y.max() - y.min()):
@@ -146,7 +156,7 @@ def main():
146156
mid = (x.max() + x.min()) / 2
147157
ax.set_xlim([mid - side * (0.5 + factor), mid + side * (0.5 + factor)])
148158

149-
num_steps = t.size
159+
num_steps = times.size
150160

151161
if "px" in wcon["data"][0] and "py" in wcon["data"][0]:
152162
if args.ignore_wcon_perimeter:
@@ -170,13 +180,14 @@ def main():
170180
def update(ti):
171181
global midline_plot, perimeter_plot
172182
f = ti / num_steps
183+
t = times[ti]
173184

174185
color = "#%02x%02x00" % (int(0xFF * (f)), int(0xFF * (1 - f) * 0.8))
175-
print("Time step: %s, fract: %f, color: %s" % (ti, f, color))
186+
print("Time %s%s, step: %s, fract: %f, color: %s" % (t, t_units, ti, f, color))
176187

177188
if midline_plot is None:
178189
(midline_plot,) = ax.plot(
179-
x[:, ti], y[:, ti], color="g", label="t=%sms" % t[ti], linewidth=0.5
190+
x[:, ti], y[:, ti], color="g", label="t=%sms" % times[ti], linewidth=0.5
180191
)
181192
else:
182193
midline_plot.set_data(x[:, ti], y[:, ti])
@@ -189,7 +200,9 @@ def update(ti):
189200
else:
190201
perimeter_plot.set_data(px[:, ti], py[:, ti])
191202

192-
anim = Player(fig, update, maxi=num_steps - 1)
203+
anim = Player(
204+
fig, update, maxi=num_steps - 1, times=[t for t in times], t_units=t_units
205+
)
193206

194207
# TODO WormViewCSV and WormViewWCON - should WormViewCSV just be the original WormView? That's what it initially did.
195208
# TODO Could take out Player and WormViewWCON into separate repo - Taking out Player could be ugly. It is quite coupled with WormView due to the update function.

0 commit comments

Comments
 (0)