forked from EmbersArc/SCvx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
94 lines (72 loc) · 2.39 KB
/
utils.py
File metadata and controls
94 lines (72 loc) · 2.39 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
import numpy as np
import os
import matplotlib.pyplot as plt
import matplotlib.animation as animation
def euler_to_quat(a):
a = np.deg2rad(a)
cy = np.cos(a[1] * 0.5)
sy = np.sin(a[1] * 0.5)
cr = np.cos(a[0] * 0.5)
sr = np.sin(a[0] * 0.5)
cp = np.cos(a[2] * 0.5)
sp = np.sin(a[2] * 0.5)
q = np.zeros(4)
q[0] = cy * cr * cp + sy * sr * sp
q[1] = cy * sr * cp - sy * cr * sp
q[3] = cy * cr * sp + sy * sr * cp
q[2] = sy * cr * cp - cy * sr * sp
return q
def format_line(name, value, unit=''):
"""
Formats a line e.g.
{Name:} {value}{unit}
"""
name += ':'
if isinstance(value, (float, np.ndarray)):
value = f'{value:{0}.{4}}'
return f'{name.ljust(40)}{value}{unit}'
def save_arrays(path, a_dict):
"""
:param path: Output path
:param a_dict: A dict containing the name of the array as key.
"""
path = path.rstrip('/')
if not os.path.isdir(path):
os.mkdir(path)
if len(os.listdir(path)) == 0:
folder_number = '000'
else:
folder_number = str(int(max(os.listdir(path))) + 1).zfill(3)
os.mkdir(f'{path}/{folder_number}')
for key in a_dict:
np.save(f'{path}/{folder_number}/{key}.npy', a_dict[key])
def make_gif(plot_func, n_frames, output_path="output/result.gif", duration=0.33):
"""
Generates and saves a GIF of the convergence history.
:param plot_func: A callback function (fig, i) that plots frame i on fig.
:param n_frames: Total number of frames (iterations).
:param output_path: File path for the output GIF.
:param duration: Duration of each frame in seconds.
"""
print(f"Generating GIF ({n_frames} frames)...")
# Create a separate figure for the animation
fig_anim = plt.figure(figsize=(10, 8))
def update(frame):
fig_anim.clear()
plot_func(fig_anim, frame)
anim = animation.FuncAnimation(
fig_anim,
update,
frames=n_frames,
interval=duration * 1000,
blit=False
)
# Ensure output directory exists
os.makedirs(os.path.dirname(output_path), exist_ok=True)
try:
writer = animation.PillowWriter(fps=1/duration)
anim.save(output_path, writer=writer)
print(f"GIF saved to {output_path}")
except Exception as e:
print(f"Failed to save GIF: {e}")
plt.close(fig_anim)