gfa.simulation#
Simulate data from the group factor analysis (GFA) generative model.
Classes
Complete output of a GFA simulation run. |
Functions
Generate samples from the full group factor analysis model. |
|
Generate observed data via the GFA observation model. |
|
Save complete simulation result to safetensors file. |
|
Save simulation recipe (config + hyperprior only). |
|
Load complete simulation from file. |
|
Load simulation recipe from file. |
- class GFASimulationResult(
- config: GFASimConfig,
- hyperprior: ObsParamsHyperPrior | ObsParamsHyperPriorStructured,
- obs_params: ObsParamsRealization,
- latents: LatentsRealization,
- observations: ObsStatic,
Complete output of a GFA simulation run.
Bundles the specification (config + hyperprior) with the sampled outputs (obs_params, latents, observations). This is a run artifact analogous to a fitted
GFAModel.- Parameters:
- config
GFASimConfig Experimental design parameters used for simulation.
- hyperprior
ObsParamsHyperPriororObsParamsHyperPriorStructured Probabilistic model specification.
- obs_params
ObsParamsRealization Sampled observation model parameters (C, d, phi, alpha).
- latents
LatentsRealization Sampled latent variables.
- observations
ObsStatic Generated observed data.
- config
- simulate(
- config: GFASimConfig,
- hyperprior: ObsParamsHyperPrior | ObsParamsHyperPriorStructured,
Generate samples from the full group factor analysis model.
- Parameters:
- config
GFASimConfig Experimental design parameters (n_samples, y_dims, x_dim, snr, seed).
- hyperprior
ObsParamsHyperPriororObsParamsHyperPriorStructured Probabilistic model specification. For structured sparsity patterns, use
ObsParamsHyperPriorStructuredwherea_alphaandb_alphaarrays specify group- and column-specific patterns. Usenp.infina_alphato force zero loadings.
- config
- Returns:
GFASimulationResultComplete simulation output including config, hyperprior, sampled parameters, latents, and observations.
Examples
>>> config = GFASimConfig( ... n_samples=100, ... y_dims=np.array([10, 10]), ... x_dim=5, ... random_seed=42, ... ) >>> hyperprior = ObsParamsHyperPrior( ... a_alpha=1.0, b_alpha=1.0, a_phi=1.0, b_phi=1.0, beta_d=1.0 ... ) >>> result = simulate(config, hyperprior) >>> result.observations.data.shape (20, 100)
- sample_observations(
- latents: LatentsRealization,
- obs_params: ObsParamsRealization | ObsParamsPoint,
- rng: Generator,
Generate observed data via the GFA observation model.
- Parameters:
- latents
LatentsRealization Sampled latent data.
- obs_params
ObsParamsRealizationorObsParamsPoint GFA observation model parameters, either as a full realization or as point estimates.
- rng
numpy.random.Generator NumPy random number generator.
- latents
- Returns:
ObsStaticGenerated observed data.
- save_simulation(
- path: str | PathLike[str],
- result: GFASimulationResult,
Save complete simulation result to safetensors file.
Saves the full snapshot: config, hyperprior, obs_params, latents, and observations. Use
load_simulation()to restore.- Parameters:
- path
strorPathLike Output file path (conventionally ends in .safetensors).
- result
GFASimulationResult Complete simulation result to save.
- path
See also
save_simulation_recipeSave only config and hyperprior (smaller file).
- save_simulation_recipe(
- path: str | PathLike[str],
- config: GFASimConfig,
- hyperprior: ObsParamsHyperPrior | ObsParamsHyperPriorStructured,
Save simulation recipe (config + hyperprior only).
Saves a minimal file that can regenerate the full simulation when passed to
simulate(). Requiresconfig.random_seedto be set.- Parameters:
- path
strorPathLike Output file path (conventionally ends in .safetensors).
- config
GFASimConfig Simulation configuration. Must have random_seed set.
- hyperprior
ObsParamsHyperPriororObsParamsHyperPriorStructured Probabilistic model specification.
- path
- Raises:
ValueErrorIf config.random_seed is None.
See also
save_simulationSave complete results (larger file).
- load_simulation( ) GFASimulationResult[source]#
Load complete simulation from file.
- Parameters:
- path
strorPathLike Path to .safetensors file saved with
save_simulation().
- path
- Returns:
GFASimulationResultComplete simulation result.
- Raises:
ValueErrorIf file contains only a recipe. Use
load_simulation_recipe()andsimulate()to regenerate.
See also
load_simulation_recipeLoad recipe and regenerate via simulate().
- load_simulation_recipe( ) tuple[GFASimConfig, ObsParamsHyperPrior | ObsParamsHyperPriorStructured][source]#
Load simulation recipe from file.
Works for both recipe-only files and full snapshots (extracts just the specification).
- Parameters:
- path
strorPathLike Path to .safetensors file.
- path
- Returns:
- config
GFASimConfig Simulation configuration.
- hyperprior
ObsParamsHyperPriororObsParamsHyperPriorStructured Probabilistic model specification.
- config
Examples
>>> config, hyperprior = load_simulation_recipe("simulation.safetensors") >>> result = simulate(config, hyperprior) # Regenerate from recipe