import torch from torch.utils.data import Dataset import numpy as np from pathlib import Path from typing import List, Tuple, Optional # Import your existing loaders from load_volumes import process_fluent_export_sparse, VolumeSpec from load_planes import process_plane_export, PlaneSpec class WAKESETDataset(Dataset): def __init__(self, root_dir: str, subset: str = 'train', augment: bool = False): """ Args: root_dir: Path to WAKESET folder. subset: 'train', 'val', or 'test'. augment: If True, applies the rotation/flipping described in the paper. """ self.root = Path(root_dir) / "Volumes" self.files = sorted(list(self.root.glob("Forward_*_CUBE_128.csv"))) self.augment = augment # Simple split logic (matches paper Section 6.1) # In reality, you might load a specific split file here n = len(self.files) if subset == 'train': self.files = self.files[:int(0.8*n)] elif subset == 'val': self.files = self.files[int(0.8*n):int(0.9*n)] else: self.files = self.files[int(0.9*n):] def __len__(self): # If augmenting, we implicitly have 4x data (handled via index modulo) return len(self.files) * 4 if self.augment else len(self.files) def __getitem__(self, idx): # Handle Augmentation Indexing if self.augment: file_idx = idx // 4 aug_mode = idx % 4 # 0: None, 1: Flip, 2: Rot+, 3: Rot- else: file_idx = idx aug_mode = 0 # Load Raw Data (Cached .npz preferred) path = self.files[file_idx] npz_path = path.with_suffix('.npz') if npz_path.exists(): data = np.load(npz_path) vol = data['velocity_magnitude'] # Shape (128, 128, 128) else: # Fallback to robust loader raw = process_fluent_export_sparse(path, fill_value=0.0) vol = raw['velocity_magnitude'] # Convert to Tensor tensor = torch.from_numpy(vol).float().unsqueeze(0) # (C, D, H, W) # Apply Physics-Consistent Augmentation (Paper Section 5.3) if aug_mode == 1: # Flip across vertical mid-plane (assumes symmetry at 0-deg) tensor = torch.flip(tensor, dims=[2]) # Flip Y-axis # Note: True rotation requires rotating vector components (u,v,w) # not just the scalar magnitude grid. # Extract Kinematics from filename for Conditioning # "Forward_0100_ms..." spec = VolumeSpec.from_filename(path) speed = float(spec.velocity) / 1000.0 angle = float(spec.angle) kinematics = torch.tensor([speed, angle], dtype=torch.float32) return tensor, kinematics