-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerate_attributes.py
More file actions
204 lines (171 loc) · 6.13 KB
/
generate_attributes.py
File metadata and controls
204 lines (171 loc) · 6.13 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import argparse
import concurrent.futures
import functools
import multiprocessing as mp
import subprocess
import tempfile
from pathlib import Path
from typing import List
import numpy as np
import tqdm
preamble = r"""\documentclass[tikz,crop]{standalone}
\usepackage{xcolor}
\usepackage{tikz}
\usetikzlibrary{calc,positioning,shadows,shadows.blur,matrix,backgrounds}
\tikzset{white background/.style={show background rectangle,tight background,
background rectangle/.style={fill=white}}}
\def\slidersize{3.1cm}
\def\spacingslider{-0.3em}
\def\IosSevenSliderFixed#1{
\tikz[baseline=-0.1cm]{
\coordinate (start) at (0,0);
\coordinate (end) at (\slidersize,0);
\coordinate (mark) at ($(start)!#1!(end)$);
\useasboundingbox (start|- 0,-.25) rectangle (end|- 0, .25);
\draw[line width=0.9mm, line cap=round, blue!50!cyan]
(start) -- (mark) edge[lightgray] (end);
\node[fill=white, draw=lightgray, very thin,
blur shadow={shadow xshift=0pt, shadow opacity=20, shadow yshift=-0.9mm,
shadow blur steps=6, shadow blur radius=0.3mm},
circle, minimum size=0.25cm, inner sep=0pt] at (mark) {};
}
}
\def\IosSevenSlider#1{
\tikz[baseline=-0.1cm]{
\coordinate (start) at (0,0);
\coordinate (end) at (\slidersize,0);
\coordinate (mark) at ($(start)!#1!(end)$);
\useasboundingbox (start|- 0,-.25) rectangle (end|- 0, .25);
\draw[line width=0.9mm, line cap=round, blue!50!cyan]
(start) -- (mark) edge[lightgray] (end);
\node[fill=white, draw=lightgray, very thin,
blur shadow={shadow xshift=0pt, shadow opacity=20, shadow yshift=-0.9mm,
shadow blur steps=6, shadow blur radius=0.3mm},
circle, minimum size=0.25cm,yshift=3.4pt, inner sep=0pt] at (mark) {};
}
}
"""
body = """
\\begin{{document}}
\\setlength{{\\fboxsep}}{{0pt}}
\\begin{{tikzpicture}}[white background]
{}
\\end{{tikzpicture}}
\\end{{document}}
"""
first_slider_template = """
\\node (slider0) {{\IosSevenSliderFixed{{{}}}}};
\\node[left=0em of slider0.west, align=center] {{$-1$}};
\\node[right=0em of slider0.east, align=center] {{$1$}};
\\node[left=1.5em of slider0.west, align=right] {{{}}};
"""
slider_template = """
\\node[
below=\spacingslider of slider{0}
] (slider{1}) {{\IosSevenSlider{{{2}}}}};
\\node[left=0em of slider{1}.west, align=center] {{$-1$}};
\\node[right=0em of slider{1}.east, align=center] {{$1$}};
\\node[left=1.5em of slider{1}.west, align=right] {{{3}}};
"""
MIN_VALUE = 0.0
MAX_VALUE = 1.0
NUM_FRAMES = 1200
NUM_RANDOM_POINTS = 6
BEZIER_VALUES = np.array([[0.0, 0.0], [0.6, 0.0], [0.4, 1.0], [1.0, 1.0]])
np.random.seed(0xCAFFE)
def interpolate_points(
point1: np.ndarray, point2: np.ndarray, steps: int
) -> np.ndarray:
t = np.linspace(0, 1, num=steps)[:, np.newaxis]
alphas = (
(1 - t) ** 3 * BEZIER_VALUES[0]
+ 3 * (1 - t) ** 2 * t * BEZIER_VALUES[1]
+ 3 * (1 - t) * t ** 2 * BEZIER_VALUES[2]
+ t ** 3 * BEZIER_VALUES[3]
)[..., 1]
alphas = alphas[:, np.newaxis]
new_points = (1 - alphas) * point1[np.newaxis] + alphas * point2[
np.newaxis
]
return new_points
def generate_points(num_attributes: int):
points = [[MIN_VALUE] * num_attributes, [MAX_VALUE] * num_attributes]
for i in range(num_attributes - 1, -1, -1):
new_point = points[-1][:]
new_point[i] = MIN_VALUE
points.append(new_point)
for i in range(num_attributes - 1, -1, -1):
new_point = points[-1][:]
new_point[i] = MAX_VALUE
points.append(new_point)
random_points = np.random.uniform(
MIN_VALUE, MAX_VALUE, size=(NUM_RANDOM_POINTS, num_attributes)
)
points.extend(random_points.tolist())
points.append([0.5] * num_attributes)
points_array = np.array(points)
return points_array
def generate_dynamics(points: np.ndarray) -> np.ndarray:
output = []
duration_per_combination = NUM_FRAMES // (len(points) - 1)
for i in range(0, points.shape[0] - 1):
start_point = points[i]
end_point = points[i + 1]
output.append(
interpolate_points(
start_point, end_point, duration_per_combination
)
)
return np.concatenate(output, axis=0)
def get_single_node(index: int, value: float, name: str) -> str:
if index == 0:
return first_slider_template.format(value, name)
return slider_template.format(index - 1, index, value, name)
def render_single(i: int, dynamic: np.ndarray, attribute_names: List[str]):
nodes = []
for j, name in enumerate(attribute_names):
nodes.append(get_single_node(j, dynamic[j], name))
tex_file = [preamble, body.format("\n".join(nodes))]
with tempfile.NamedTemporaryFile(suffix=".tex", mode="w") as temp_file:
content = "\n".join(tex_file)
temp_file.write(content)
temp_file.flush()
path = Path("frames")
compile_command = [
"pdflatex",
"-synctex=1",
"-interaction=nonstopmode",
"-file-line-error",
f"-output-dir=/tmp",
"-recorder",
temp_file.name,
]
convert_command = [
"convert",
"-density",
"300",
Path(temp_file.name).with_suffix(".pdf"),
path / f"frame{i:04d}.jpg",
]
subprocess.call(compile_command, stdout=subprocess.DEVNULL)
subprocess.call(convert_command, stdout=subprocess.DEVNULL)
def main():
parser = argparse.ArgumentParser()
parser.add_argument("attribute_names", nargs="+")
args = parser.parse_args()
points = generate_points(len(args.attribute_names))
dynamics = generate_dynamics(points)
prender = functools.partial(
render_single, attribute_names=args.attribute_names
)
with concurrent.futures.ProcessPoolExecutor(
max_workers=mp.cpu_count() - 2
) as pool:
list(
tqdm.tqdm(
pool.map(prender, list(range(len(dynamics))), dynamics),
total=len(dynamics),
)
)
if __name__ == "__main__":
main()