API Reference¶
Simulation package¶
Simulation package public API.
SimulationConfig
dataclass
¶
Validated simulation configuration.
Source code in src/vote_simulation/simulation/configuration.py
generate_data(config_path, show_progress=True)
¶
Generate (or retrieve cached) profiles for every combination defined in the config.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of file paths of generated/cached parquet files. |
Source code in src/vote_simulation/simulation/simulation.py
load_simulation_config(config_path=DEFAULT_CONFIG_PATH)
¶
Load and validate a simulation config file.
Source code in src/vote_simulation/simulation/configuration.py
obtain_data_instance(model, n_v, n_c, *, iteration=0, seed=161, base_path='data', extra_params=None)
¶
Load a cached profile or generate + persist it.
If the parquet file already exists the profile is loaded from disk; otherwise it is generated and saved for future reuse.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
str
|
Generative model code (e.g. "UNI", "IC"). |
required |
n_v
|
int
|
Number of voters. |
required |
n_c
|
int
|
Number of candidates. |
required |
iteration
|
int
|
Iteration index. |
0
|
seed
|
int
|
Random seed for generation (will be combined with iteration index for variability). |
161
|
base_path
|
str
|
Root folder for generated data (see config.output_base_path). |
'data'
|
extra_params
|
dict[str, object] | None
|
Optional dict of extra parameters to pass to the generator (per-model). |
None
|
Source code in src/vote_simulation/simulation/simulation.py
sim(file_path, rule_code)
¶
Execute a single rule on a single file
²
Source code in src/vote_simulation/simulation/simulation.py
simulation_from_config(config_path, show_progress=True, *, compute_metrics=True)
¶
Full pipeline: generate profiles, apply rules, save results.
For every (model, n_voters, n_candidates, iteration) combination:
1. Obtain (generate or load) the profile.
2. Run all requested rules.
3. Save the result in sim_result/<MODEL>_v<NV>_c<NC>/iter_XXXX.parquet.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_path
|
str
|
Path to the TOML configuration file (see docs for the template). |
required |
show_progress
|
bool
|
Whether to display a progress bar. |
True
|
compute_metrics
|
bool
|
Whether to compute :class: |
True
|
Source code in src/vote_simulation/simulation/simulation.py
Simulation result models¶
Rule registry¶
Rule index for mapping short codes to svvamp rule factories.
RuleResult
¶
Bases: Protocol
Protocol for rule results that have been post-processed to include co-winners.
Source code in src/vote_simulation/models/rules/registry.py
get_all_rules_codes()
¶
get_rule_builder(code)
¶
Return rule builder from code
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
code
|
str
|
rule encoding (detailed index in documentation) |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
if wrong code |
Returns:
| Name | Type | Description |
|---|---|---|
RuleBuilder |
RuleBuilder
|
rule applied |
Source code in src/vote_simulation/models/rules/registry.py
make_rule_builder(rule_factory)
¶
Create a public RuleBuilder from a Profile -> rule result factory.
This helper is intended for external users who want to register custom rules while reusing the registry's profile conversion and co-winner post-processing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rule_factory
|
Callable[[Profile], Any]
|
Callable that takes a |
required |
Returns:
| Type | Description |
|---|---|
RuleBuilder
|
A |
Source code in src/vote_simulation/models/rules/registry.py
register_rule(code, builder)
¶
Register a rule builder under a short code.
Generator registry¶
Generator registry mapping short codes to svvamp GeneratorProfile factories.
Usage examples:
> from vote_simulation.models.data_generation.generator_registry import get_generator_builder
> builder = get_generator_builder("UNI")
> profile = builder(n_v=100, n_c=5, seed=42)
GeneratorBuilder = Callable[..., Profile]
module-attribute
¶
Signature: (n_v, n_c, seed=0, **extra) -> svvamp.Profile
get_generator_builder(code)
¶
Return the generator builder for the given code.
Raises:
| Type | Description |
|---|---|
ValueError
|
If code is not registered. |
Source code in src/vote_simulation/models/data_generation/generator_registry.py
list_generator_codes()
¶
make_generator_builder(generator_factory, **default_kwargs)
¶
Create a public GeneratorBuilder from a generator factory.
This helper is intended for external users who want to register custom generators while reusing the registry's seeding and relabeling logic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
generator_factory
|
Callable[..., Any]
|
Callable |
required |
**default_kwargs
|
object
|
Default keyword arguments forwarded to the factory. |
{}
|
Returns:
| Type | Description |
|---|---|
GeneratorBuilder
|
A |
Example::
from svvamp import GeneratorProfileEuclideanBox
builder = make_generator_builder(
GeneratorProfileEuclideanBox,
box_dimensions=[1.0, 1.0, 1.0],
)
register_generator("MY_EUCLID_3D", builder)
Source code in src/vote_simulation/models/data_generation/generator_registry.py
normalize_between_0_and_1(profile)
¶
Return a new Profile with utilities normalized to [0, 1].
Source code in src/vote_simulation/models/data_generation/generator_registry.py
register_generator(code, builder)
¶
Register a generator builder under a short code.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
code
|
str
|
Short code - case-insensitive, will be normalized. |
required |
builder
|
GeneratorBuilder
|
Callable |
required |
Source code in src/vote_simulation/models/data_generation/generator_registry.py
Data instances¶
Load or generate election profiles and persist them.
DataInstance
¶
Encapsulates an election profile (utility matrix + candidate labels).
`DataInstance`` can be created in three ways:
From an existing file (CSV or Parquet)::
di = DataInstance("path/to/data.csv")
From a generator (wraps svvamp GeneratorProfile*)::
di = DataInstance.from_generator(
model_code="UNI", n_v=101, n_c=5, seed=42, iteration=0
)
From a raw Profile::
di = DataInstance.from_profile(profile)
Source code in src/vote_simulation/models/data_generation/data_instance.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 | |
n_candidates
property
¶
Number of candidates in this instance.
n_voters
property
¶
Number of voters in this instance.
build_profile(candidates, data)
¶
Build a svvamp.Profile from candidate labels and utility matrix.
Source code in src/vote_simulation/models/data_generation/data_instance.py
denormalize()
¶
Restore the original (pre-normalization) utility values.
Returns:
| Type | Description |
|---|---|
ndarray
|
2-D array with the same shape as |
ndarray
|
the utilities on their original scale. |
Source code in src/vote_simulation/models/data_generation/data_instance.py
from_generator(model_code, n_v, n_c, *, seed=0, iteration=0, **extra_params)
classmethod
¶
Generate an election profile using a registered generator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_code
|
str
|
Registered generator short code (e.g. |
required |
n_v
|
int
|
Number of voters. |
required |
n_c
|
int
|
Number of candidates. |
required |
seed
|
int
|
Base random seed for reproducibility. |
0
|
iteration
|
int
|
Iteration index (added to seed). |
0
|
**extra_params
|
object
|
Model-specific keyword arguments forwarded to the generator builder. |
{}
|
Returns:
| Type | Description |
|---|---|
DataInstance
|
A new |
Source code in src/vote_simulation/models/data_generation/data_instance.py
from_profile(profile, file_path='')
classmethod
¶
Wrap an existing svvamp.Profile into a DataInstance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
profile
|
Profile
|
An existing |
required |
file_path
|
str
|
Optional file path associated with the profile. |
''
|
Returns:
| Type | Description |
|---|---|
DataInstance
|
A new |
Source code in src/vote_simulation/models/data_generation/data_instance.py
get_csv(file_path)
¶
Load candidate labels and utility matrix from a CSV file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str
|
Path to the CSV file. |
required |
Returns:
| Type | Description |
|---|---|
tuple[ndarray, ndarray]
|
A tuple containing: - candidates: 1-D array of candidate names. - data: 2-D array of shape (n_voters, n_candidates) with utility values. |
Source code in src/vote_simulation/models/data_generation/data_instance.py
get_data(file_path)
¶
Load data from a CSV or Parquet file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str
|
Path to the data file. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
candidates |
ndarray
|
1-D array of candidate names. |
data |
ndarray
|
2-D array of shape |
Source code in src/vote_simulation/models/data_generation/data_instance.py
get_parquet(file_path)
¶
Load candidate labels and utility matrix from a Parquet file.
The Parquet file is expected to have one column per candidate (column name = candidate label) and one row per voter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str
|
Path to the Parquet file. |
required |
Returns:
| Type | Description |
|---|---|
tuple[ndarray, ndarray]
|
A tuple containing: - candidates: 1-D array of candidate names. - data: 2-D array of shape (n_voters, n_candidates) with utility values. |
Source code in src/vote_simulation/models/data_generation/data_instance.py
plot_heatmap(*, cluster_columns=False, cluster_rows=True, method='average', metric='euclidean', cmap='viridis', title=None, save_path=None, show=True)
¶
Visualize the utility matrix as a heatmap with optional hierarchical clustering.
Values are already in [0, 1]. Columns (candidates) are reordered by hierarchical clustering by default so that similar preference profiles appear next to each other.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cluster_columns
|
bool
|
Reorder candidates by hierarchical clustering (default True). |
False
|
cluster_rows
|
bool
|
Reorder voters by hierarchical clustering (default False). |
True
|
method
|
str
|
Linkage method ( |
'average'
|
metric
|
str
|
Distance metric ( |
'euclidean'
|
cmap
|
str
|
Matplotlib colormap name. |
'viridis'
|
title
|
str | None
|
Figure title. Defaults to model code if available. |
None
|
save_path
|
str | None
|
If provided, save the figure to this path. |
None
|
show
|
bool
|
Whether to call |
True
|
Returns:
| Type | Description |
|---|---|
dict
|
Dict with keys |
Raises:
| Type | Description |
|---|---|
ImportError
|
If matplotlib is not installed. |
Source code in src/vote_simulation/models/data_generation/data_instance.py
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 | |
save_csv(file_path)
¶
Persist the utility matrix to a CSV file (same layout as input).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str
|
Destination path (should end in |
required |
Returns:
| Type | Description |
|---|---|
str
|
The resolved absolute path of the written file. |
Source code in src/vote_simulation/models/data_generation/data_instance.py
save_parquet(file_path)
¶
Persist the utility matrix to a Parquet file.
Creates parent directories if needed. The file contains one column per candidate and one row per voter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str
|
Destination path (should end in |
required |
Returns:
| Type | Description |
|---|---|
str
|
The resolved absolute path of the written file. |