-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_colormap.py
More file actions
71 lines (58 loc) · 2.52 KB
/
Copy pathgenerate_colormap.py
File metadata and controls
71 lines (58 loc) · 2.52 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
import json
from pathlib import Path
import numpy as np
import matplotlib.pyplot as plt
import polanalyser as pa
def main():
# Keep polanalyser import for registration side-effects (e.g. CoP/ToP colormaps).
_ = pa
colormap_defs = [
{"name": "viridis", "label": "viridis", "file": "viridis.npy"},
{"name": "plasma", "label": "plasma", "file": "plasma.npy"},
{"name": "inferno", "label": "inferno", "file": "inferno.npy"},
{"name": "magma", "label": "magma", "file": "magma.npy"},
{"name": "cividis", "label": "cividis", "file": "cividis.npy"},
{"name": "twilight", "label": "twilight", "file": "twilight.npy"},
{"name": "RdBu", "label": "RdBu", "file": "RdBu.npy", "diverging": True},
{"name": "hsv", "label": "HSV", "file": "hsv.npy"},
{"name": "dop", "label": "Black-Red", "file": "black-red.npy"},
{"name": "cop", "label": "Yellow-Black-Blue", "file": "yellow-black-blue.npy"},
{"name": "top", "label": "Yellow-Cyan-Yellow", "file": "yellow-cyan-yellow.npy", "diverging": True},
{"name": "coolwarm", "label": "coolwarm", "file": "coolwarm.npy", "diverging": True},
]
colormap_dir = Path("public/colormaps")
manifest_path = colormap_dir / "manifest.json"
expected_files = {entry["file"] for entry in colormap_defs}
# Delete unmanaged colormap npy files.
removed_files = []
for npy_path in colormap_dir.glob("*.npy"):
if npy_path.name not in expected_files:
npy_path.unlink()
removed_files.append(npy_path.name)
# Regenerate (overwrite) every managed colormap npy file.
for entry in colormap_defs:
cmap = plt.get_cmap(entry["name"])
data = cmap(np.linspace(0, 1, 256))[:, :3].astype(np.float32) # (256, 3)
np.save(colormap_dir / entry["file"], data)
# Overwrite manifest with only the managed colormaps.
manifest = {
"colormaps": [
{
"label": entry["label"],
"file": entry["file"],
**({"diverging": True} if entry.get("diverging") else {}),
}
for entry in colormap_defs
]
}
with manifest_path.open("w") as f:
json.dump(manifest, f, indent=2)
print(f"Wrote {len(colormap_defs)} colormaps to manifest.")
if removed_files:
print("Deleted unmanaged npy files:")
for name in sorted(removed_files):
print(f"- {name}")
else:
print("No unmanaged npy files found.")
if __name__ == "__main__":
main()