mirror of
https://github.com/K-Dense-AI/scientific-agent-skills.git
synced 2026-12-05 02:23:59 +08:00
* add OpenPIV skill under skills/openpiv
* Fix spec violations and unverified APIs in the OpenPIV skill
Verified everything against openpiv 0.25.4 in a clean venv; all 14 SKILL.md
snippets and both reference snippets now execute, and runner.py/run_example.py
run end to end on OpenPIV's bundled test1 pair.
Spec adherence:
- metadata was a JSON flow mapping, which strictyaml rejects outright -- the
skill failed `skills-ref validate` and would not have registered at all.
Converted to a block mapping.
- compatibility claimed Python 3.8+; openpiv 0.25.4 requires >=3.10.
- Added allowed-tools and a version-pinned install line.
APIs that do not exist and now do:
- `openpiv.masking` -> `openpiv.preprocess.dynamic_masking`, which returns
(image, mask). method="shirai" is not a method; it is "edges" or "intensity".
The masked image is returned already zeroed, so multiplying the frame by the
mask is wrong -- for "edges" the mask is uint8 0/255 and rescales by 255.
- `pyprocess.iterative_warping_piv` -> `openpiv.windef.simple_multipass`.
- `openpiv.smooth.smooth` -> `openpiv.smoothn.smoothn`.
- `global_val(u, v, u_threshold=, v_threshold=)` -> positional (min, max) tuples
as u_thresholds/v_thresholds.
- `local_median_val(u, v)` -> u_threshold and v_threshold are required.
- `replace_outliers(method="disc")` -> "disk"; an unknown method is not
rejected, it silently yields an all-zero kernel.
- Combining flags with np.maximum -> boolean OR; validators return bool arrays
where True marks a spurious vector.
references/advanced_algorithms.md described algorithms that do not exist in
OpenPIV ("epi-div_div", "pritzmd_piv", EDD, PrTrZMD) alongside invented
throughput and memory figures. Replaced with the real correlation, subpixel,
s2n, multi-pass, 3D, and phase-separation APIs plus the full PIVSettings table.
Two units traps found while testing and now documented:
- windef ignores settings.dt and settings.scaling_factor -- first_pass calls
extended_search_area_piv without dt, so the chain returns px/frame.
- global_val/local_median_val thresholds are in the units of u and v. After
extended_search_area_piv(dt=0.02) that is px/s, so the conventional (-30, 30)
px/frame limit rejects the entire field.
Scripts:
- runner.py: dropped --algorithm and --threads, which were accepted and never
used (--algorithm even offered a nonexistent "synthetic_aperture"), and
--mask static, which nothing implemented. --mask dynamic silently did nothing
because it caught the ImportError from the nonexistent module. Selects
correlation_method="linear" when search_area > window_size, since the default
"circular" aliases via FFT wrap-around. Validates argument ranges, pins the
Agg backend before pyplot, and no longer NaNs out the vectors it just
interpolated -- that is now opt-in via --drop_invalid.
- analyze.py: class was named PypivAnalyzer and documented as "using pivpy"
while importing neither. Renamed to PIVAnalyzer, added compute_strain and
compute_statistics to match what SKILL.md advertises, and derived the physical
grid spacing from the saved coordinates so the gradients are per unit length.
- run_example.py: was a subprocess wrapper around a hardcoded relative path.
Rewritten as a self-contained smoke test on OpenPIV's bundled image pair.
Security scan: SAFE, 0 findings.
---------
Co-authored-by: Timothy Kassis <timothy.kassis@k-dense.ai>
113 lines
4.0 KiB
Python
113 lines
4.0 KiB
Python
"""Post-processing helpers for a params.npz written by runner.py.
|
|
|
|
Verified against openpiv 0.25.4. Pure numpy -- no OpenPIV import needed here.
|
|
"""
|
|
|
|
from pathlib import Path
|
|
from typing import Dict, Optional, Tuple
|
|
|
|
import numpy as np
|
|
|
|
|
|
class PIVAnalyzer:
|
|
"""Derived quantities from a saved PIV velocity field.
|
|
|
|
The fields in params.npz are already scaled to physical units by runner.py, so
|
|
x and y are in the same unit as the scaling factor and u and v are that unit per
|
|
second. Pass the matching grid spacing to the gradient methods -- the default
|
|
dx=1.0 yields per-grid-cell derivatives, not per-unit-length ones.
|
|
"""
|
|
|
|
def __init__(self, params_file: str):
|
|
self.params_file = Path(params_file)
|
|
|
|
data = np.load(self.params_file)
|
|
self.x = data["x"]
|
|
self.y = data["y"]
|
|
self.u = data["u"]
|
|
self.v = data["v"]
|
|
# Boolean: True marks a vector flagged as spurious during processing.
|
|
self.flags = data["flags"].astype(bool)
|
|
|
|
@property
|
|
def grid_spacing(self) -> Tuple[float, float]:
|
|
"""(dx, dy) inferred from the coordinate arrays, in physical units."""
|
|
dx = float(np.abs(np.diff(self.x, axis=1)).mean()) if self.x.shape[1] > 1 else 1.0
|
|
dy = float(np.abs(np.diff(self.y, axis=0)).mean()) if self.y.shape[0] > 1 else 1.0
|
|
return dx, dy
|
|
|
|
def plot_vector_field(
|
|
self,
|
|
scale: int = 50,
|
|
width: float = 0.0035,
|
|
save_path: Optional[str] = None,
|
|
):
|
|
"""Quiver plot of the valid vectors. Saves to save_path, or shows interactively."""
|
|
import matplotlib.pyplot as plt
|
|
|
|
valid = ~self.flags
|
|
fig, ax = plt.subplots(figsize=(10, 8))
|
|
ax.quiver(
|
|
self.x[valid],
|
|
self.y[valid],
|
|
self.u[valid],
|
|
self.v[valid],
|
|
scale=scale,
|
|
width=width,
|
|
)
|
|
ax.set_xlabel("x")
|
|
ax.set_ylabel("y")
|
|
ax.set_title("Velocity Field")
|
|
ax.set_aspect("equal")
|
|
if save_path:
|
|
fig.savefig(save_path, dpi=150, bbox_inches="tight")
|
|
plt.close(fig)
|
|
else:
|
|
plt.show()
|
|
return fig
|
|
|
|
def get_velocity_magnitude(self) -> np.ndarray:
|
|
return np.sqrt(self.u**2 + self.v**2)
|
|
|
|
def compute_vorticity(
|
|
self, dx: Optional[float] = None, dy: Optional[float] = None
|
|
) -> np.ndarray:
|
|
"""Out-of-plane vorticity, dv/dx - du/dy. Defaults to the inferred grid spacing."""
|
|
gx, gy = self.grid_spacing
|
|
dx = gx if dx is None else dx
|
|
dy = gy if dy is None else dy
|
|
return np.gradient(self.v, dx, axis=1) - np.gradient(self.u, dy, axis=0)
|
|
|
|
def compute_strain(
|
|
self, dx: Optional[float] = None, dy: Optional[float] = None
|
|
) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
|
|
"""Return (exx, eyy, exy) of the 2D strain-rate tensor."""
|
|
gx, gy = self.grid_spacing
|
|
dx = gx if dx is None else dx
|
|
dy = gy if dy is None else dy
|
|
du_dx = np.gradient(self.u, dx, axis=1)
|
|
du_dy = np.gradient(self.u, dy, axis=0)
|
|
dv_dx = np.gradient(self.v, dx, axis=1)
|
|
dv_dy = np.gradient(self.v, dy, axis=0)
|
|
return du_dx, dv_dy, 0.5 * (du_dy + dv_dx)
|
|
|
|
def compute_statistics(self) -> Dict[str, float]:
|
|
"""Spatial mean and RMS over this single frame.
|
|
|
|
This is NOT Reynolds decomposition: subtracting one frame's spatial mean
|
|
measures spatial variance, which equals turbulent intensity only for a
|
|
homogeneous field. True turbulence statistics need an ensemble of pairs --
|
|
average over the time axis, then subtract that mean field from each frame.
|
|
"""
|
|
u_prime = self.u - np.nanmean(self.u)
|
|
v_prime = self.v - np.nanmean(self.v)
|
|
rms_u = float(np.nanstd(u_prime))
|
|
rms_v = float(np.nanstd(v_prime))
|
|
return {
|
|
"u_mean": float(np.nanmean(self.u)),
|
|
"v_mean": float(np.nanmean(self.v)),
|
|
"rms_u": rms_u,
|
|
"rms_v": rms_v,
|
|
"tke": 0.5 * (rms_u**2 + rms_v**2),
|
|
}
|