Last active
April 29, 2025 16:28
-
-
Save akaszynski/cb88e3e3a90a035da1359fb2f7b5c127 to your computer and use it in GitHub Desktop.
read-cas-movie.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Generate one video. | |
Loads all ducts, identifies a pairing between all that minimizes distance | |
between, morphs two, and then interpolates the solution between those two. | |
""" | |
from tqdm import tqdm | |
from pathlib import Path | |
from dataclasses import dataclass | |
import pyvista as pv | |
import re | |
import numpy as np | |
from scipy.spatial.distance import pdist, squareform | |
import matplotlib.pyplot as plt | |
from concurrent.futures import ProcessPoolExecutor | |
from multiprocessing import Manager | |
from tqdm.contrib.concurrent import process_map | |
@dataclass | |
class DuctModel: | |
mesh: pv.PolyData | |
parameters: dict | |
filename: Path | |
index: int | |
def extract_parameters(log_path): | |
params = {} | |
pattern = re.compile(r"Dimension: (\w+), Value: ([\d.]+)") | |
with open(log_path) as f: | |
for line in f: | |
match = pattern.search(line) | |
if match: | |
params[match.group(1)] = float(match.group(2)) | |
return params | |
def load_ducts(base_path): | |
print("Loading...", end="") | |
ducts = [] | |
exp_dirs = sorted( | |
Path(base_path).glob("experiment_*"), key=lambda p: int(p.name.split("_")[1]) | |
) | |
for exp_dir in exp_dirs: | |
stl_files = sorted(exp_dir.glob("*.stl"), key=lambda p: p.name) | |
log_file = exp_dir / "local_variations.log" | |
if stl_files and log_file.exists(): | |
mesh = pv.read(stl_files[0]) | |
params = extract_parameters(log_file) | |
exp_index = exp_dir.name.split("_")[1] | |
ducts.append(DuctModel(mesh, params, Path(stl_files[0]), exp_index)) | |
print(" done") | |
return ducts | |
base_dir = "/home/user/projects/datasets" | |
duct_models = load_ducts(base_dir) | |
keys = list(duct_models[0].parameters.keys()) | |
params_list = [[duct.parameters[k] for k in keys] for duct in duct_models] | |
params_array = np.array(params_list) | |
params_array_norm = params_array - params_array.min(0) | |
params_array_norm /= params_array_norm.max(0) | |
params_array_norm[:, 0] *= 1.5 | |
index_a = ordered_indices[40] | |
index_b = ordered_indices[80] | |
grid_a = pv.read(duct_models[index_a].filename.parent / "duct_baffle.cas.h5")[0] | |
grid_a.points = grid_a.points.astype(np.float64) | |
grid_a = grid_a.clean() | |
grid_b = pv.read(duct_models[index_b].filename.parent / "duct_baffle.cas.h5")[0] | |
grid_b.points = grid_b.points.astype(np.float64) | |
grid_b = grid_b.clean() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment