Prior Data Generation¶
Overview¶
Before running rejection sampling, every prior realization
in a prior HDF5 file (/M<im> – see Data format) needs a matching
/D<id> dataset: the data that would be observed for that realization,
against which the actually observed data (in a data HDF5 file) is later
compared. Three functions cover the ways /D<id> gets created:
integrate.integrate.prior_data_em()– forward-model EM (TDEM) data from a resistivity model, e.g./M1(resistivity) ->/D<id>.integrate.integrate_borehole.prior_data_borehole()– turn a borehole log into class-probability prior data conditioned on a discrete model, e.g./M2(lithology) ->/D<id>.integrate.integrate.prior_data_identity()– copy a model parameter directly into/D<id>unchanged (identity mapping), used e.g. to compare/condition directly on model values rather than forward-modelled data.
All three add a new /D<id> dataset to the prior HDF5 file. What they return:
integrate.integrate.prior_data_em()returns the path of the prior-data HDF5 file (f_prior_data_h5). The id is theidyou passed.integrate.integrate.prior_data_identity()returns(f_prior_data_h5, id).integrate.integrate_borehole.prior_data_borehole()returns(P_obs, id_prior).
The ids then feed into id_use/id_prior of
integrate.integrate_rejection.integrate_rejection().
prior_data_em – EM forward modelling¶
import integrate as ig
f_prior_data_h5 = ig.prior_data_em(f_prior_h5, file_gex='system.gex')
prior_data_em is a thin dispatcher: it forward-models /M<im> through
one of the supported EM backends and writes the result as /D<id>.
integrate.integrate.forward_em() (forward response for a resistivity array, without
writing a file) uses exactly the same backend and device selection.
Backends¶
'ga-aem'– GA-AEM [GA-AEM] (gatdaem1d), seeintegrate.gaaem_forward. The only fully supported, production backend.'anemone'– PyTorch-based forward model with optional GPU support, seeintegrate.anemone_forward.'simpeg'– SimPEG [SimPEG]Simulation1DLayered, seeintegrate.simpeg_forward. Its validation against GA-AEM/AarhusInv is described inSIMPEG_VS_GAAEM.mdin the repository root.
Important
method='anemone' is integration in development: it requires a
local, unpublished checkout of the anemone package
(pip install -e /path/to/anemone) and has not yet been validated to
the same standard as GA-AEM. method='simpeg' is likewise newer and
less battle-tested than ga-aem. Do not rely on either for production
results yet.
Choosing the backend¶
The backend is resolved in this order:
the
method=keyword ('ga-aem'/'gaaem','anemone'or'simpeg'; case-insensitive);the
EM_FORWARD_METHODenvironment variable (same values);otherwise, auto-selection of the first installed backend, in the order
'anemone'->'ga-aem'->'simpeg'.
A backend counts as installed if its package imports: torch and
anemone for anemone; gatdaem1d including its compiled library for
ga-aem (a broken GA-AEM install counts as unavailable); simpeg >= 0.22 for
simpeg. Call with showInfo=1 to see which backend was used – an
auto-selected one is reported as e.g.
Using EM forward method: anemone (auto-selected).
Because auto-selection prefers anemone whenever it is installed, pass
method='ga-aem' (or set EM_FORWARD_METHOD=ga-aem) when you need the
production backend.
If the requested backend is not installed, the call does not stop. It
issues a RuntimeWarning and uses the first installed backend in the
auto-selection order (see Errors below). Check the warning, or call with
showInfo=1, to confirm which backend ran.
# Auto-selected backend (first installed of anemone, ga-aem, simpeg)
ig.prior_data_em(f_prior_h5, file_gex='system.gex')
# GA-AEM -- recommended for production use
ig.prior_data_em(f_prior_h5, file_gex='system.gex', method='ga-aem')
# Experimental / in development -- do not use for production results
ig.prior_data_em(f_prior_h5, file_gex='system.gex', method='anemone')
ig.prior_data_em(f_prior_h5, file_gex='system.gex', method='simpeg')
# or select the backend once for a whole session / script
export EM_FORWARD_METHOD=ga-aem
Choosing the device (anemone only)¶
For method='anemone', the Torch device is resolved in this order:
the
device=keyword (any stringtorch.deviceaccepts, e.g.'cpu','cuda','cuda:1','mps');the
EM_FORWARD_DEVICEenvironment variable;otherwise, auto-detection of the first usable device:
'cuda'iftorch.cuda.is_available()– i.e. a CUDA-enabled torch build, an NVIDIA driver and a GPU are all present (merely havingtorchinstalled is not enough);'mps'(Apple Silicon GPU) iftorch.backends.mps.is_available()and the device accepts float64 tensors. anemone computes in float64, which MPS has so far not supported, so in practice Macs currently fall through to'cpu'; this switches to'mps'automatically once float64 is supported;'cpu'.
device is ignored for 'ga-aem' and 'simpeg'. The chosen device is
printed with showInfo=1 and stored as the device attribute of the
written /D<id> dataset.
ig.prior_data_em(f_prior_h5, file_gex='system.gex', method='anemone') # auto: cuda -> mps -> cpu
ig.prior_data_em(f_prior_h5, file_gex='system.gex', method='anemone', device='cpu') # force CPU
ig.prior_data_em(f_prior_h5, file_gex='system.gex', method='anemone', device='cuda') # force GPU
An explicitly requested device is used as given, without any check: e.g.
device='cuda' on a machine without a usable GPU, or device='mps'
while MPS lacks float64, fails with the corresponding Torch error rather than
falling back to CPU.
Errors and fallback¶
An unknown
method(keyword orEM_FORWARD_METHOD) raisesValueError.If the requested backend is not installed, a
RuntimeWarningis issued and the first installed backend (anemone, then ga-aem, then simpeg) is used. The fallback is reported in theshowInfo=1output.If no backend is installed at all,
ImportErroris raised and lists the reason each backend is unavailable.
A fallback changes which forward model produces your data. For production
results, check that the warning does not appear, or set method explicitly.
prior_data_borehole – well-log conditioning¶
P_obs, id_prior = ig.prior_data_borehole(f_prior_h5, im_prior=2, BH=BH, parallel=True)
prior_data_borehole dispatches on BH['method'] (default
'mode_probability'):
'mode_probability'(recommended – fast and robust) ->integrate.integrate_borehole.prior_data_borehole_class_mode(): extracts the most frequent lithology class per observed depth interval, across all prior realizations.'layer_probability'->integrate.integrate_borehole.prior_data_borehole_class_layer(): a direct layer-probability approach usingintegrate.integrate.prior_data_identity()internally (no per-realization mode extraction).'class_exact'/'layer_probability_independent'– not yet implemented; raisesNotImplementedError.
The full BH borehole dictionary format (depth_top, depth_bottom,
class_obs, class_prob, X, Y, name, method, …), the
distance-weighted extrapolation to the survey grid
(integrate.integrate_borehole.Pobs_to_datagrid()), and the one-call
integrate.integrate_borehole.save_borehole_data() wrapper are documented in full in
Borehole Data Format – this section only covers the prior_data_borehole
entry point itself.
prior_data_identity – identity mapping¶
f_prior_data_h5, id = ig.prior_data_identity(f_prior_h5, im=1)
Copies /M<im> directly into a new /D<id> dataset, unchanged – no
forward modelling. Used internally by
integrate.integrate_borehole.prior_data_borehole_class_layer(), and directly whenever you need to
condition on/compare against a model parameter’s own values (e.g. a resistivity
or class-id log) rather than simulated data.
Key behaviour:
id=0(default) auto-assigns the next free/D<id>slot in the file.doMakePriorCopy=Truewrites to a new HDF5 file (name derived fromf_prior_h5,imandid) instead of modifyingf_prior_h5in place;Noptionally truncates to the firstNrealizations in that copy.If
/D<id>already exists, it is deleted and overwritten by default (forceDeleteExisting=True); passforceDeleteExisting=Falseto leave it untouched and return early instead.
Returns (f_prior_data_h5, id) – the (possibly copied) file path and the
data id actually used.
Examples¶
Runnable examples of these backends and functions:
See also¶
Data format – PRIOR.h5 layout,
/M<im>//D<id>conventions, and theTDEM/GA-AEM/SimPEGforward-model types.Borehole Data Format – full borehole (
BH) dictionary format and well-log integration workflow.Rejection Sampling – what happens next: running rejection sampling against the
/D<id>datasets created here.