Source code for upxo.surrModelOps.SModelJobs

"""
Batch 2D MCGS runs with LFI export for surrogate / ML training data.

Class ``mcgs2d_Surrogate`` repeatedly calls ``mcgs`` → ``simulate`` →
``detect_grains`` and saves each time-slice label field via ``ArrExp2d``.
Folder layouts: per-simulation or per-temporal-slice. Does **not** fit or
persist a surrogate model — export only.
"""

from upxo.ggrowth.mcgs import mcgs
from upxo.interfaces.io.expop import ArrExp2d as arex2
import os

[docs] class mcgs2d_Surrogate(): """ Batch 2D MCGS runs that export LFI arrays for surrogate / ML training. Repeatedly runs ``mcgs`` → ``simulate`` → ``detect_grains`` and writes each temporal-slice label field via :class:`~upxo.interfaces.io.expop.ArrExp2d`. Folder layouts: one directory per simulation or per temporal slice. Does **not** fit or persist a surrogate model — data export only. Workflow -------- 1. ``job = mcgs2d_Surrogate(input_dashboard='...xls')`` 2. ``job.set_NumberOfSimulations(N)`` 3. ``job.set_export_parameters(basePath, baseFilename, fileType, ...)`` 4. ``job.makeImages(TemporalOrSimFolders='temporal'|'sim')`` Attributes ---------- input_dashboard : str Path to the Excel MCGS dashboard. dim : int Fixed to 2 for this job class. Nsim : int Number of independent MC runs. basePath, baseFilename, fileType, fileNamePadLength Export location and naming. currentExportDirs : dict Directories created during the last export pass. """ __slots__ = ('input_dashboard', 'dim', 'Nsim', 'basePath', 'baseFilename', 'fileType', 'tslices', 'currentExportDirs', 'fileNamePadLength' ) def __init__(self, input_dashboard: str): """Initialise the instance.""" self.input_dashboard = input_dashboard self.dim = 2 self.currentExportDirs = dict()
[docs] def set_NumberOfSimulations(self, Nsim: int): """Set or update NumberOfSimulations.""" self.Nsim = Nsim
[docs] def set_export_parameters(self, basePath: str, baseFilename: str, fileType: str, fileNamePadLength: int = 4): """Set or update export parameters.""" self.basePath = basePath self.baseFilename = baseFilename self.fileType = fileType self.fileNamePadLength = fileNamePadLength
[docs] def makeImages(self, TemporalOrSimFolders='temporal', compression=False, fileLevelCompression=False): """Makeimages.""" for simCount in range(self.Nsim): UPXO_PXTAL = mcgs(input_dashboard=self.input_dashboard) UPXO_PXTAL.simulate() UPXO_PXTAL.detect_grains(library='connected_components_3d') if TemporalOrSimFolders.lower() == 'temporal': self.saveInTemporalFolders(simCount, UPXO_PXTAL, fileLevelCompression=fileLevelCompression) elif TemporalOrSimFolders.lower() == 'sim': self.saveInSimulationFolders(simCount, UPXO_PXTAL, fileLevelCompression=fileLevelCompression) else: raise ValueError("TemporalOrSimFolders must be either 'temporal' or 'sim'.")
[docs] def saveInSimulationFolders(self, simCount, UPXO_PXTAL, fileLevelCompression=False): """Saveinsimulationfolders.""" sim_dir_name = f"Path_{simCount+1:0{self.fileNamePadLength}d}" self.currentExportDirs[simCount] = os.path.join(self.basePath, sim_dir_name) os.makedirs(self.currentExportDirs[simCount], exist_ok=True) # ----------------------------------------------- for ts in UPXO_PXTAL.gs.keys(): # Apply dynamic padding to Time Slice file name filename = f"Ensemble_{ts:0{self.fileNamePadLength}d}.{self.fileType}" full_export_path = os.path.join(self.currentExportDirs[simCount], filename) arex2.save(UPXO_PXTAL.gs[ts].lfi, full_export_path, compression=fileLevelCompression)
[docs] def saveInTemporalFolders(self, simCount, UPXO_PXTAL, fileLevelCompression=False): """Saveintemporalfolders.""" for ts in UPXO_PXTAL.gs.keys(): # Apply dynamic padding to Time Slice Directory name ts_dir_name = f"EnsembleSet_{ts:0{self.fileNamePadLength}d}" self.currentExportDirs[ts] = os.path.join(self.basePath, ts_dir_name) os.makedirs(self.currentExportDirs[ts], exist_ok=True) # Apply dynamic padding to Simulation file name filename = f"Realization_{simCount+1:0{self.fileNamePadLength}d}.{self.fileType}" full_export_path = os.path.join(self.currentExportDirs[ts], filename) arex2.save(UPXO_PXTAL.gs[ts].lfi, full_export_path, compression=fileLevelCompression)