modified: __pycache__/evaluation_studio.cpython-314.pyc
modified: evaluation_studio.py modified: scripts/dataset.py new file: scripts/inference_utils.py modified: scripts/predict_single.py modified: scripts_2/__pycache__/task3_identify.cpython-314.pyc modified: scripts_2/task3_identify.py
This commit is contained in:
@@ -221,16 +221,14 @@ class LoadReport:
|
||||
|
||||
def list_harmonic_files(config: DataConfig) -> list[Path]:
|
||||
files = sorted(config.data_root.rglob(config.harmonic_pattern))
|
||||
return [file_path for file_path in files if extract_frequency_hz(file_path.name) is not None]
|
||||
return files
|
||||
|
||||
|
||||
def build_record_from_file(file_path: Path, config: DataConfig) -> SampleRecord:
|
||||
time_values, x_values, y_values, interpolation_count = load_aligned_signals(file_path, config)
|
||||
time_middle, x_middle, y_middle = get_middle_segment(time_values, x_values, y_values, config)
|
||||
sampling_rate = estimate_sampling_rate(time_middle)
|
||||
frequency_hz = extract_frequency_hz(file_path.name)
|
||||
if frequency_hz is None:
|
||||
frequency_hz = get_dominant_frequency(x_middle, sampling_rate)
|
||||
frequency_hz = get_dominant_frequency(x_middle, sampling_rate)
|
||||
|
||||
x_rms = calculate_rms(x_middle)
|
||||
y_rms = calculate_rms(y_middle)
|
||||
|
||||
72
scripts/inference_utils.py
Normal file
72
scripts/inference_utils.py
Normal file
@@ -0,0 +1,72 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from functools import lru_cache
|
||||
from pathlib import Path
|
||||
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
|
||||
|
||||
def _task1_curve_candidates(project_root: Path) -> tuple[Path, ...]:
|
||||
base_dir = project_root / "evaluation_outputs" / "task1_final"
|
||||
return (
|
||||
base_dir / "task1_final_dense_curve.csv",
|
||||
base_dir / "evaluation_dense_curve.csv",
|
||||
)
|
||||
|
||||
|
||||
@lru_cache(maxsize=8)
|
||||
def load_task1_dense_curve(project_root_str: str) -> tuple[np.ndarray, np.ndarray, np.ndarray] | None:
|
||||
project_root = Path(project_root_str)
|
||||
for path in _task1_curve_candidates(project_root):
|
||||
if not path.exists():
|
||||
continue
|
||||
try:
|
||||
curve_df = pd.read_csv(path)
|
||||
except Exception:
|
||||
continue
|
||||
required = {"frequency_hz", "pred_tr", "x_rms_interp"}
|
||||
if not required.issubset(set(curve_df.columns)):
|
||||
continue
|
||||
cleaned = curve_df[["frequency_hz", "pred_tr", "x_rms_interp"]].copy()
|
||||
cleaned["frequency_hz"] = pd.to_numeric(cleaned["frequency_hz"], errors="coerce")
|
||||
cleaned["pred_tr"] = pd.to_numeric(cleaned["pred_tr"], errors="coerce")
|
||||
cleaned["x_rms_interp"] = pd.to_numeric(cleaned["x_rms_interp"], errors="coerce")
|
||||
cleaned = cleaned.dropna()
|
||||
if cleaned.empty:
|
||||
continue
|
||||
cleaned = cleaned.sort_values("frequency_hz").drop_duplicates(subset="frequency_hz", keep="first")
|
||||
return (
|
||||
cleaned["frequency_hz"].to_numpy(dtype=np.float64),
|
||||
cleaned["pred_tr"].to_numpy(dtype=np.float64),
|
||||
cleaned["x_rms_interp"].to_numpy(dtype=np.float64),
|
||||
)
|
||||
return None
|
||||
|
||||
|
||||
def predict_task1_tr(
|
||||
model,
|
||||
*,
|
||||
feature_vector: np.ndarray,
|
||||
frequency_hz: float,
|
||||
normalization_eps: float,
|
||||
project_root: Path,
|
||||
prefer_curve: bool = True,
|
||||
) -> tuple[float, str]:
|
||||
features = np.asarray(feature_vector, dtype=np.float64).reshape(1, -1)
|
||||
tr_value = max(float(model.predict(features)[0]), normalization_eps)
|
||||
source = "model_direct"
|
||||
|
||||
if prefer_curve:
|
||||
curve = load_task1_dense_curve(str(project_root.resolve()))
|
||||
if curve is not None and features.shape[1] >= 3:
|
||||
freq_grid, _tr_grid, x_ref_grid = curve
|
||||
query_freq = float(np.clip(frequency_hz, freq_grid.min(), freq_grid.max()))
|
||||
x_ref = float(np.interp(query_freq, freq_grid, x_ref_grid))
|
||||
x_obs = float(features[0, 2])
|
||||
ratio = x_obs / max(x_ref, normalization_eps)
|
||||
if ratio > 1.15 or ratio < 0.85:
|
||||
tr_value = max(tr_value / ratio, normalization_eps)
|
||||
source = "model_amp_adapt"
|
||||
|
||||
return tr_value, source
|
||||
@@ -10,9 +10,11 @@ import numpy as np
|
||||
try:
|
||||
from .config import CORE_FEATURE_NAMES, evaluation_dir, make_experiment_config
|
||||
from .dataset import build_record_from_file
|
||||
from .inference_utils import predict_task1_tr
|
||||
except ImportError:
|
||||
from config import CORE_FEATURE_NAMES, evaluation_dir, make_experiment_config
|
||||
from dataset import build_record_from_file
|
||||
from inference_utils import predict_task1_tr
|
||||
|
||||
|
||||
def parse_args() -> argparse.Namespace:
|
||||
@@ -87,7 +89,14 @@ def main() -> None:
|
||||
|
||||
file_path = Path(args.file).resolve()
|
||||
record = build_record_from_file(file_path, config.data)
|
||||
pred_tr = max(float(model.predict([record.features.tolist()])[0]), config.data.normalization_eps)
|
||||
pred_tr, tr_source = predict_task1_tr(
|
||||
model,
|
||||
feature_vector=record.features,
|
||||
frequency_hz=float(record.frequency_hz),
|
||||
normalization_eps=float(config.data.normalization_eps),
|
||||
project_root=config.data.project_root,
|
||||
prefer_curve=True,
|
||||
)
|
||||
pred_rms = pred_tr * record.x_rms
|
||||
|
||||
out_dir = evaluation_dir(config)
|
||||
@@ -99,6 +108,7 @@ def main() -> None:
|
||||
for feature_name, feature_value in zip(CORE_FEATURE_NAMES, record.features.tolist()):
|
||||
print(f"{feature_name}: {feature_value:.6f}")
|
||||
print(f"Predicted TR: {pred_tr:.6f}")
|
||||
print(f"TR Source: {tr_source}")
|
||||
print(f"Predicted RMS: {pred_rms:.6f}")
|
||||
print(f"True RMS: {record.y_rms:.6f}")
|
||||
print(f"Relative RMS Error (%): {abs(pred_rms - record.y_rms) / max(abs(record.y_rms), 1e-12) * 100.0:.4f}")
|
||||
|
||||
Reference in New Issue
Block a user