Anchor Sampling AS
Pronunciation note: say AS like the French as 🂡, meaning the ace of a deck, or spell it A, S. Please do not pronounce it like “ass”.
Anchor Sampling (AS) is a location-anchored variant of QuickSampling. It preserves the same path-based simulation workflow, neighborhood template logic, continuous/categorical handling, multivariate support, kernels, and random or explicit simulation paths. The core conceptual change is the sampling constraint:
- In QS, once a neighborhood match is found, the sampled center can come from any admissible location in the training image(s).
- In AS, when simulating a cell at coordinate
(i,j,...), the sampled center value must come from that exact same coordinate across an aligned stack of training images.
This makes AS suitable when you have many realizations, scenarios, priors, or historical fields defined on the same grid geometry and you want neighborhood-based selection without losing spatial anchoring.
Parameters for AS
Usage: result = g2s(flag1, value1, flag2, value2, ...)
Schema outputs: result["simulation"] / result.simulation, result["indexmap"] / result.indexmap for selected TI ids, result["time"] / result.time, result["progress"] / result.progress, and result["job_id"] / result.job_id.
| Flag | Description | Mandatory |
|---|---|---|
-a |
Use as, AS, or AnchorSampling. |
✔ |
-ti |
Training images provided as an aligned stack. All TIs must have exactly the same spatial size as the simulation grid. In multivariate cases, the last dimension still represents the variables. NaN TI values are ignored. | ✔ |
-di |
Destination image (simulation grid). NaN values are simulated. Non-NaN values are conditioning data. Its spatial size must match every TI. | ✔ |
-dt |
Data type. 0 → continuous and 1 → categorical. Provide one value per variable. |
✔ |
-k |
Number of best anchored TI candidates retained before sampling. | ✔ |
-n |
Number of closest neighbors to consider. As in QS, this can be scalar or variable-specific. | ✔ |
-ki |
Optional kernel image defining the neighborhood footprint and weights. | |
-cnorm / -cn
|
Continuous mismatch norm power(s). Use one strictly positive value to apply the same p to all continuous variables, or one value per continuous variable. Continuous mismatch uses the rooted Lp form pow(sum(pow(abs(diff),p)),1/p) (kernel-weighted internally). Default is 2 (L2). |
|
-sp |
Optional simulation path. Default is a random path. As in QS, explicit path images are supported. | |
-j |
Parallel execution settings. Same meaning as in QS. | |
-s |
Random seed value. | |
-mi |
Optional prior or mask image with one value per TI at each spatial location. In Python and MATLAB this is typically a stack with trailing TI axis (spatial..., n_ti). Mismatch is computed first, then -mi is applied as an admissibility mask before top-k: non-finite (NaN/inf) or non-positive mask values are excluded. The retained top-k candidates are finally sampled using -mi as relative weights. |
Advanced parameters
| Flag | Description | Mandatory |
|---|---|---|
-kii |
Per-pixel kernel-selection image, same semantics as QS. | |
-ni |
Per-pixel number of neighbors. | |
-kvi |
Per-pixel number of retained best candidates. | |
-cti |
Treat each TI as periodic over each dimension when evaluating TI-side neighborhoods. | |
-csim |
Create a periodic simulation over each dimension. | |
-fs |
Full simulation with distinct paths per variable, same semantics as QS. |
Important differences from QS
- The index image exported by AS stores the selected TI id, not a flattened TI pixel address.
-
-iiis intentionally not supported in AS. Use-miinstead when you want to constrain or weight which TI can be selected at a given location. -
-adsimis not supported in AS. AS requires all TIs and the simulation grid to share the same geometry. - AS is CPU-first. GPU flags are accepted for interface compatibility, but the anchored matcher currently runs on CPU.
Examples
The following examples assume the G2S server is running.
Minimal Python example
#This code requires the G2S server to be running
import numpy as np
from g2s import g2s
rng = np.random.default_rng(4)
y, x = np.mgrid[0:96, 0:128].astype(np.float32)
ti1 = (np.sin(x / 8.0) + np.cos(y / 11.0)).astype(np.float32)
ti2 = (np.cos((x + y) / 13.0) + np.sin(y / 7.0)).astype(np.float32)
conditioning = np.full(ti1.shape, np.nan, dtype=np.float32)
known = rng.random(ti1.shape) < 0.05
conditioning[known] = ti1[known]
result = g2s(
"-a", "as",
"-ti", ti1, ti2,
"-di", conditioning,
"-dt", [0],
"-k", 2,
"-n", 8,
"-s", 100,
)
simulation = result["simulation"]
selected_ti = result["indexmap"]
print("job", result["job_id"], "simulation", simulation.shape, "selected TI", selected_ti.shape)
%This code requires the G2S server to be running
rng(4);
[x, y] = meshgrid(single(0:127), single(0:95));
ti1 = single(sin(x / 8) + cos(y / 11));
ti2 = single(cos((x + y) / 13) + sin(y / 7));
conditioning = single(nan(size(ti1)));
known = rand(size(ti1)) < 0.05;
conditioning(known) = ti1(known);
[result, ~] = g2s( ...
'-a', 'as', ...
'-ti', ti1, ti2, ...
'-di', conditioning, ...
'-dt', [0], ...
'-k', 2, ...
'-n', 8, ...
'-s', 100);
simulation = result.simulation;
selected_ti = result.indexmap;
disp(['job ', num2str(result.job_id)]);
disp(size(simulation));
disp(size(selected_ti));
Larger diagnostic example in Python
For richer schema-output scripts with rectangular synthetic cases, -mi edge cases, exported TI-id checks, and live figure windows, see example/python/as/AnchorSampling.py and example/python/as/AnchorSamplingSyntheticExperiment.py. Legacy positional-output versions remain under legacy_example/.
When to use AS instead of QS
Use AS when:
- your training data are available as a large stack of aligned realizations or scenarios
- the same coordinate across TIs has a stable physical meaning
- you want neighborhood matching to choose which TI to use at a location, rather than which location to sample from
Use QS when:
- stationarity in TI coordinates is acceptable
- the center value should be allowed to come from any TI location
- you need workflows such as TI-index maps through
-iior augmented-dimensional simulation
Notes
- AS keeps the same general workflow as QS, including categorical, continuous, multivariate, kernel-based, and path-image driven simulations.
- In masked runs, AS computes mismatch first, applies
-miadmissibility masking before top-kranking (dropping non-finite and non-positive entries), then uses-mias relative weights inside the retained set. - For
-cnorm/-cn, if you provide more than one value, the number of values must match the number of continuous variables in-dt.