Skip to content

Commit 2c1c473

Browse files
committed
Include frame interaction as a script
1 parent 542342c commit 2c1c473

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed
+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import warnings
2+
from typing import Any, Callable, Iterable, Tuple
3+
4+
import numpy as np
5+
from ipywidgets import interact, widgets
6+
from skimage.transform import downscale_local_mean as downscale
7+
8+
from boiling_learning.preprocessing import Case
9+
10+
11+
def _make_show_frame_function(
12+
imshow: Callable[[np.ndarray], Any],
13+
downscale_factor: Iterable[int] = (8, 8, 1),
14+
) -> Callable[[np.ndarray], None]:
15+
def _imshow(image: np.ndarray) -> None:
16+
imshow(downscale(image, downscale_factor))
17+
18+
return _imshow
19+
20+
21+
def _interact_boiling_frames(
22+
cases: Tuple[Case, ...], imshow: Callable[[np.ndarray], Any]
23+
) -> None:
24+
cases_options = [(case.name, case) for case in cases]
25+
default_case = cases_options[0][1]
26+
cases_widget = widgets.Dropdown(
27+
options=cases_options, description='Case name:', value=default_case
28+
)
29+
30+
experiment_videos_options = [
31+
(experiment_video.name, experiment_video)
32+
for experiment_video in cases_widget.value.values()
33+
]
34+
experiment_videos_widget = widgets.Dropdown(
35+
options=experiment_videos_options,
36+
description='Experiment video:',
37+
value=experiment_videos_options[0][1],
38+
)
39+
40+
def update_videos_list(changes):
41+
experiment_videos_widget.options = [
42+
(experiment_video.name, experiment_video)
43+
for experiment_video in changes['new'].values()
44+
]
45+
46+
cases_widget.observe(update_videos_list, 'value')
47+
48+
with experiment_videos_widget.value.frames() as f:
49+
index_widget = widgets.IntSlider(
50+
value=0, min=0, max=len(f), description='Frame:'
51+
)
52+
53+
def update_max_index(changes):
54+
with changes['new'].frames() as f:
55+
index_widget.max = len(f)
56+
57+
experiment_videos_widget.observe(update_max_index, 'value')
58+
59+
def show_frames(case, ev, idx):
60+
imshow(ev.frame(idx))
61+
62+
interact(
63+
show_frames,
64+
case=cases_widget,
65+
ev=experiment_videos_widget,
66+
idx=index_widget,
67+
)
68+
69+
70+
def main(
71+
cases: Iterable[Case],
72+
colab_backend: bool = False,
73+
physics: str = 'boiling',
74+
) -> None:
75+
cases = tuple(cases)
76+
77+
if physics not in {'boiling', 'condensation'}:
78+
raise ValueError(
79+
'*physics* must be either "boiling" or "condensation"'
80+
)
81+
82+
imshow_imported: bool = False
83+
if colab_backend:
84+
try:
85+
from google.colab.patches import cv2_imshow
86+
87+
imshow_imported = True
88+
except (ImportError, ModuleNotFoundError):
89+
pass
90+
91+
if not imshow_imported:
92+
from cv2 import imshow as cv2_imshow
93+
94+
imshow = _make_show_frame_function(cv2_imshow)
95+
96+
with warnings.catch_warnings():
97+
if physics == 'boiling':
98+
_interact_boiling_frames(cases, imshow)
99+
else:
100+
raise ValueError(f'physics=="{physics}" is not supported yet.')
101+
102+
103+
if __name__ == '__main__':
104+
raise RuntimeError(
105+
'*interact_frames* cannot be executed as a standalone script yet.'
106+
)

0 commit comments

Comments
 (0)