-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Add unified cfd-viz CLI with entry point and batch processing (… #36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2ebdf36
feat: Add unified cfd-viz CLI with entry point and batch processing (…
shaia ab174ea
fix: Remove scripts/ dependency from CLI for wheel compatibility
shaia 12d814c
fix: Only force Agg backend on headless systems
shaia 9e50e69
fix: Catch ValueError for invalid VTK files in monitor loop
shaia dd4205e
fix: Split animate --output (file) and --output-dir (frames directory)
shaia f390ec3
fix: Include field type in --all animation set
shaia 0d0ca72
fix: Handle ValueError from read_vtk_file in CLI commands
shaia 22c6e10
fix: Catch ValueError for malformed VTK files in batch processing
shaia ca0bf54
fix: Improve monitor and headless detection robustness
shaia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,177 @@ | ||
| """Batch processing for multiple VTK files from a TOML config. | ||
|
|
||
| Config format (``batch.toml``):: | ||
|
|
||
| [batch] | ||
| output_dir = "output/batch" | ||
|
|
||
| [[batch.jobs]] | ||
| vtk = "data/vtk_files/flow_0100.vtk" | ||
| analyses = ["vorticity", "profiles"] | ||
|
|
||
| [[batch.jobs]] | ||
| vtk = "data/vtk_files/flow_0200.vtk" | ||
| analyses = ["vorticity"] | ||
| output_dir = "output/batch/step200" # per-job override | ||
|
|
||
| [[batch.jobs]] | ||
| vtk_pattern = "data/vtk_files/flow_*.vtk" | ||
| analyses = ["animate"] | ||
| animate_type = "velocity" | ||
| fps = 10 | ||
|
|
||
| Supported analyses: ``vorticity``, ``profiles``, ``animate``. | ||
| """ | ||
|
|
||
| import os | ||
| import sys | ||
|
|
||
|
|
||
| def _load_toml(path): | ||
| """Load a TOML file, using tomllib (3.11+) or tomli.""" | ||
| try: | ||
| import tomllib | ||
| except ModuleNotFoundError: | ||
| import tomli as tomllib | ||
|
|
||
| with open(path, "rb") as f: | ||
| return tomllib.load(f) | ||
|
|
||
|
|
||
| def _progress(current, total, label=""): | ||
| """Write a simple progress line to stderr.""" | ||
| width = 30 | ||
| filled = int(width * current / total) if total > 0 else width | ||
| bar = "#" * filled + "-" * (width - filled) | ||
| pct = (current / total * 100) if total > 0 else 100 | ||
| sys.stderr.write(f"\r [{bar}] {pct:5.1f}% {label}") | ||
| if current >= total: | ||
| sys.stderr.write("\n") | ||
| sys.stderr.flush() | ||
|
|
||
|
|
||
| def _run_vorticity(vtk_file, output_dir): | ||
| from cfd_viz._cli_impl import create_vorticity_visualization | ||
| from cfd_viz.common import ensure_dirs, read_vtk_file | ||
|
|
||
| ensure_dirs() | ||
| try: | ||
| data = read_vtk_file(vtk_file) | ||
| except ValueError: | ||
| print(f" Warning: invalid VTK file {vtk_file}") | ||
| return | ||
| if data is None: | ||
| print(f" Warning: could not read {vtk_file}") | ||
| return | ||
|
|
||
| if data.u is None or data.v is None: | ||
| print(f" Warning: no velocity data in {vtk_file}") | ||
| return | ||
|
|
||
| create_vorticity_visualization(data.to_dict(), output_dir) | ||
|
|
||
|
|
||
| def _run_profiles(vtk_file, output_dir): | ||
| from cfd_viz._cli_impl import ( | ||
| _vtk_to_profiles_dict, | ||
| create_cross_section_analysis, | ||
| ) | ||
| from cfd_viz.common import ensure_dirs, read_vtk_file | ||
|
|
||
| ensure_dirs() | ||
| try: | ||
| data = read_vtk_file(vtk_file) | ||
| except ValueError: | ||
| print(f" Warning: invalid VTK file {vtk_file}") | ||
| return | ||
| if data is None: | ||
| print(f" Warning: could not read {vtk_file}") | ||
| return | ||
|
|
||
| data_dict = _vtk_to_profiles_dict(data) | ||
| if data_dict is None: | ||
| print(f" Warning: no velocity data in {vtk_file}") | ||
| return | ||
|
|
||
| create_cross_section_analysis(data_dict, output_dir) | ||
|
|
||
|
|
||
| def _run_animate(vtk_files, output_dir, animate_type="velocity", fps=5): | ||
| from cfd_viz._cli_impl import create_and_save_animation, load_vtk_files_to_frames | ||
|
|
||
| frames = load_vtk_files_to_frames(vtk_files) | ||
| output_path = os.path.join(output_dir, f"cfd_{animate_type}.gif") | ||
| create_and_save_animation(frames, animate_type, output_path, fps=fps) | ||
|
|
||
|
|
||
| def run_batch(config_path): | ||
| """Execute a batch config file.""" | ||
| import glob as globmod | ||
|
|
||
| try: | ||
| cfg = _load_toml(config_path) | ||
| except FileNotFoundError: | ||
| print(f"Error: config file not found: {config_path}", file=sys.stderr) | ||
| raise SystemExit(2) from None | ||
| except Exception as exc: | ||
| print(f"Error: failed to parse config: {exc}", file=sys.stderr) | ||
| raise SystemExit(2) from None | ||
|
|
||
| batch = cfg.get("batch", {}) | ||
| global_output = batch.get("output_dir", "output/batch") | ||
| jobs = batch.get("jobs", []) | ||
|
|
||
| if not jobs: | ||
| print("No jobs defined in config.") | ||
| return | ||
|
|
||
| total = len(jobs) | ||
| print(f"Batch: {total} job(s) from {config_path}") | ||
|
|
||
| for idx, job in enumerate(jobs, 1): | ||
| analyses = job.get("analyses", []) | ||
| job_output = job.get("output_dir", global_output) | ||
| os.makedirs(job_output, exist_ok=True) | ||
|
|
||
| # Resolve VTK files | ||
| vtk_files = [] | ||
| if "vtk" in job: | ||
| vtk_files = [job["vtk"]] | ||
| elif "vtk_pattern" in job: | ||
| vtk_files = sorted(globmod.glob(job["vtk_pattern"])) | ||
|
|
||
| if not vtk_files: | ||
| print(f" Job {idx}/{total}: no VTK files found, skipping") | ||
| _progress(idx, total, "skipped") | ||
| continue | ||
|
|
||
| label = ( | ||
| os.path.basename(vtk_files[0]) | ||
| if len(vtk_files) == 1 | ||
| else f"{len(vtk_files)} files" | ||
| ) | ||
| print(f" Job {idx}/{total}: {label} analyses={analyses}") | ||
|
|
||
| for analysis in analyses: | ||
| if analysis == "vorticity": | ||
| for vtk_file in vtk_files: | ||
| _run_vorticity(vtk_file, job_output) | ||
| elif analysis == "profiles": | ||
| for vtk_file in vtk_files: | ||
| _run_profiles(vtk_file, job_output) | ||
| elif analysis == "animate": | ||
| animate_type = job.get("animate_type", "velocity") | ||
| fps = job.get("fps", 5) | ||
| try: | ||
| _run_animate(vtk_files, job_output, animate_type, fps) | ||
| except Exception as exc: | ||
| print( | ||
| f" Warning: animate analysis failed for job" | ||
| f" {idx}/{total}: {exc}" | ||
| ) | ||
| else: | ||
| print(f" Warning: unknown analysis '{analysis}', skipping") | ||
|
|
||
| _progress(idx, total, label) | ||
|
|
||
| print("Batch processing complete.") | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.