Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bda #294

Closed
wants to merge 299 commits into from
Closed

Bda #294

Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
299 commits
Select commit Hold shift + click to select a range
eda445a
added set plot functions
michael-gemmell May 31, 2023
2555605
added center mass sampling and Ti/Te y0_ref options
michael-gemmell May 31, 2023
20ae704
plot formatting changed
michael-gemmell May 31, 2023
e0b9c03
black formatting
michael-gemmell May 31, 2023
dfabdd0
deleted example bayes_opt
michael-gemmell May 31, 2023
b21b66d
moved sample with autocorr function to bayes_dev_workflow
michael-gemmell May 31, 2023
2f78d9d
renamed bayesworkflow
michael-gemmell May 31, 2023
9ed488e
stashing
michael-gemmell Jun 13, 2023
533a44a
multiplot formatting
michael-gemmell Jun 22, 2023
751ca81
generalised writing kinetic profiles to blobs
michael-gemmell Jun 27, 2023
59c8316
function autocorr plot added
michael-gemmell Jun 27, 2023
b2dee2d
ion density added to kinetic profiles saved
michael-gemmell Jun 27, 2023
8eee24e
inital commit of abstract workflow and example of usage
michael-gemmell Jun 27, 2023
408b619
doc strings
michael-gemmell Jun 27, 2023
de237dd
renaming workflow
michael-gemmell Jun 27, 2023
06c0746
Ti_ref as default
michael-gemmell Jun 30, 2023
5435769
abstract methods added
michael-gemmell Jun 30, 2023
ee3a62e
Changed to new MDSPlus structure
michael-gemmell Jul 4, 2023
a8ca5fa
Adding default methods
michael-gemmell Jul 4, 2023
6f1e330
moved sampling to it's own function in abstract class
michael-gemmell Jul 4, 2023
dd3abdf
moved sampling to it's own function in abstract class
michael-gemmell Jul 4, 2023
6f9d4a8
renamed abstract class to AbstractBayesWorkflow
michael-gemmell Jul 4, 2023
55e2f8f
comments
michael-gemmell Jul 4, 2023
d3bf315
added high density sampling as method
michael-gemmell Jul 4, 2023
430a44b
added attributes to __init__
michael-gemmell Jul 4, 2023
d633ae5
error handling __init__
michael-gemmell Jul 4, 2023
35b64cf
error handling __init__
michael-gemmell Jul 4, 2023
633b36c
moved read data to abstract class
michael-gemmell Jul 4, 2023
2ce1c58
initial commit
michael-gemmell Jul 7, 2023
08cf081
kwargs added
michael-gemmell Jul 7, 2023
82eb00b
xrcs wavelength units corrected
michael-gemmell Jul 7, 2023
666387b
background to int
michael-gemmell Jul 7, 2023
3690efb
stash
michael-gemmell Jul 11, 2023
3c23661
renamed impurities
michael-gemmell Jul 11, 2023
bf3bfb9
stashing
michael-gemmell Jul 12, 2023
fbfafe5
stashing
michael-gemmell Jul 13, 2023
9f62a78
stashing
michael-gemmell Jul 14, 2023
3828c49
fixing key names
michael-gemmell Jul 14, 2023
310a440
updating and testing example
michael-gemmell Jul 14, 2023
d8f9be5
added background as attribute of helike model
michael-gemmell Jul 14, 2023
1f0a0f9
fixed phantom methods
michael-gemmell Jul 14, 2023
7623a54
rearranged methods
michael-gemmell Jul 14, 2023
b3b7a26
moved background to call kwarg
michael-gemmell Aug 2, 2023
4a7e022
params and kwargs now are given as model_{var} and model prefix is re…
michael-gemmell Aug 2, 2023
e78f638
adding _phantom_data and _exp_data abstract methods
michael-gemmell Aug 2, 2023
82d6fee
renaming to BayesWorkflowExample
michael-gemmell Aug 2, 2023
2c1c48a
initial commit
michael-gemmell Aug 2, 2023
c7a2ca5
renamed phantom_params to profile_params
michael-gemmell Aug 2, 2023
db25e70
renamed bayesopt -> bayesmodel
michael-gemmell Aug 2, 2023
b1c5bd3
renamed bayesopt -> bayesmodel
michael-gemmell Aug 2, 2023
f78ce64
moved start point sampling to its own method _sample_start_points
michael-gemmell Aug 2, 2023
c3a418b
fixed variable name
michael-gemmell Aug 2, 2023
196eb60
fixed bug with start_points being overwritten
michael-gemmell Aug 2, 2023
31542b0
removed workflow_dev
michael-gemmell Aug 8, 2023
cc218b4
moving equilibrium to workflow object
michael-gemmell Aug 8, 2023
9cb0266
when reading raw data save transforms to their own attribute
michael-gemmell Aug 8, 2023
68c1872
transforms saved to workflow class
michael-gemmell Aug 8, 2023
f0f561f
fixed name
michael-gemmell Aug 8, 2023
3b2eaee
renamed example transform
michael-gemmell Aug 8, 2023
ae200d3
made example los function
michael-gemmell Aug 8, 2023
3d9d540
minor name fix
michael-gemmell Aug 8, 2023
cd04fb1
made read_test_data function
michael-gemmell Aug 8, 2023
dfdd791
made example los function
michael-gemmell Aug 8, 2023
4c6c1c2
minor type
michael-gemmell Aug 8, 2023
0ce85fa
added reader to read_test_data method
michael-gemmell Aug 8, 2023
fa5ac44
fixed copying workflow object states
michael-gemmell Aug 8, 2023
7d343a4
updated example to work with assign_profiles
michael-gemmell Aug 8, 2023
489d14a
can now run with pulse = None and synthetic transforms/equilibrium
michael-gemmell Aug 8, 2023
e04fe9b
formatting
michael-gemmell Aug 8, 2023
6fcff75
refactoring names
michael-gemmell Aug 8, 2023
eba8ca7
deleted old bayes_models tests
michael-gemmell Aug 8, 2023
b6ece95
reformatted _build_bckc for readability
michael-gemmell Aug 8, 2023
2bd93bf
moved percentage error to class attribute
michael-gemmell Aug 8, 2023
e46ac8a
fixing init percent error
michael-gemmell Aug 8, 2023
cc0aafd
black formatting
michael-gemmell Aug 8, 2023
acc50b5
renamed kin_prof -> plasma_profiles
michael-gemmell Aug 10, 2023
223a526
refactored window handling
michael-gemmell Aug 10, 2023
c9333ae
replaced doppler_broaden with physics.ev_doppler
michael-gemmell Aug 10, 2023
80bc391
removed methods from __init__
michael-gemmell Aug 10, 2023
3f5c089
stashing
michael-gemmell Aug 11, 2023
766825a
setup_plasma now takes kwargs
michael-gemmell Aug 15, 2023
cb93ae8
adding plasma to models now happens in setup_opt_data
michael-gemmell Aug 15, 2023
cf1c127
workflow broken up into methods
michael-gemmell Aug 15, 2023
3019e45
Merge branch 'st40' into michaelgemmell/bayes_workflow
michael-gemmell Aug 15, 2023
260a515
removed redundant kwargs
michael-gemmell Aug 15, 2023
0821b9d
fixed non_plasma call option
michael-gemmell Aug 15, 2023
78800c6
fixed plasma_initialisation
michael-gemmell Aug 15, 2023
62d6727
fixing violin plotting
michael-gemmell Aug 15, 2023
98190dd
units for xrcs.spectra.wavelength fixed
michael-gemmell Aug 15, 2023
f732da0
nchannels added to example_los
michael-gemmell Aug 17, 2023
ec1c9ca
adding print message for fake data reading
michael-gemmell Aug 17, 2023
b0fac37
adjusting example los
michael-gemmell Aug 17, 2023
03d68c7
bckc method not printing spectra/fit not available everytime
michael-gemmell Aug 17, 2023
72e6e7e
adjusting priors
michael-gemmell Aug 17, 2023
d5ebe08
moved kwargs to sample function
michael-gemmell Aug 17, 2023
056b7f5
nsamples kwarg added to sample_from_high_density_region
michael-gemmell Aug 17, 2023
e1b35c5
mocked bda_tree module
michael-gemmell Aug 17, 2023
3733bc6
fixed import
michael-gemmell Aug 17, 2023
b0f3fae
fixed import
michael-gemmell Aug 17, 2023
ad9400a
black formatting
michael-gemmell Aug 17, 2023
f0a1d65
black formatting
michael-gemmell Aug 17, 2023
3b1dcd8
fixed where mocked module is imported
michael-gemmell Aug 17, 2023
b8b6cee
black formatting
michael-gemmell Aug 17, 2023
d3e4e5c
mocking bda_tree import
michael-gemmell Aug 17, 2023
a7ec4cd
precommit
michael-gemmell Aug 17, 2023
b1babc4
precommit
michael-gemmell Aug 17, 2023
8e2f2d8
moving read_data tvector to kwargs
michael-gemmell Aug 17, 2023
519c4ed
precommit formatting
michael-gemmell Aug 17, 2023
f7623ce
precommit formatting
michael-gemmell Aug 17, 2023
67bd35c
precommit types
michael-gemmell Aug 17, 2023
d1afc87
precommit types
michael-gemmell Aug 17, 2023
1f38f27
precommit types
michael-gemmell Aug 17, 2023
c555f6b
precommit types
michael-gemmell Aug 17, 2023
b5d5840
precommit types
michael-gemmell Aug 17, 2023
9d12492
precommit types
michael-gemmell Aug 17, 2023
15c4aac
precommit types
michael-gemmell Aug 17, 2023
4362cd5
moving kwargs/args around
michael-gemmell Aug 17, 2023
37fff66
adding mode/run to main call
michael-gemmell Aug 22, 2023
d5b5eb5
pressure/stored energy are now saved/written
michael-gemmell Aug 22, 2023
07c6895
removing for loops from pressure/ion density/zeff calculations
michael-gemmell Aug 22, 2023
845954b
removing for loops from ion density calculations
michael-gemmell Aug 22, 2023
83ede9e
defaulting to using cached calculations -> factor 2 speed upgit add i…
michael-gemmell Aug 22, 2023
ca5900c
example transform moved to own function
michael-gemmell Aug 22, 2023
741808e
t = time_to_calculate and bckc don't print if chi2 not included
michael-gemmell Aug 22, 2023
cf3b01f
Thompson Scattering added
michael-gemmell Aug 23, 2023
e4fe2bc
error checking for missing data
michael-gemmell Aug 23, 2023
b316163
in _make_spectra moved sorting wavelengths to end of method and fixed…
michael-gemmell Aug 24, 2023
bc56b39
integrating spectra was removing nans so added them back post integra…
michael-gemmell Aug 24, 2023
5368990
pixel offset added as __call__ option
michael-gemmell Aug 24, 2023
82208ca
time vector in INPUTS added
michael-gemmell Aug 24, 2023
da94202
adjusting calibration factor
michael-gemmell Sep 4, 2023
0abbaaa
max-min ranges now 0.5-99.5
michael-gemmell Sep 4, 2023
6114dc4
high_density_sampling now uses 3 best points
michael-gemmell Sep 4, 2023
d210b28
Gelmin-Rubin diagnostic added to optimisation node
michael-gemmell Sep 4, 2023
7e7d02d
Gelman-Rubin diagnostic added to tree
michael-gemmell Sep 4, 2023
e3c2abf
stashing
michael-gemmell Sep 4, 2023
cb0df2a
now accepts variable number of input parameters and fills missing val…
michael-gemmell Sep 6, 2023
b48ea95
priors now more constrained for wped/wcenter
michael-gemmell Sep 6, 2023
90c38fb
priors now more constrained for wped/wcenter
michael-gemmell Sep 6, 2023
51ee0d9
fast particles from ASTRA included
michael-gemmell Sep 11, 2023
3979531
Merge branch 'st40' into michaelgemmell/bayes_workflow
marcosertoli Sep 11, 2023
b9d09e2
ASTRA options added to BDA
michael-gemmell Sep 12, 2023
4089d41
chers added
michael-gemmell Sep 12, 2023
e74ed9a
chers added
michael-gemmell Sep 12, 2023
f4bc980
Extra ASTRA options implemented
michael-gemmell Sep 13, 2023
875293c
prior for ne.wped increased to 30
michael-gemmell Sep 13, 2023
6545540
not automatically adding equil to transforms now
michael-gemmell Sep 13, 2023
fd859b5
Fixed plotting for multiple time points
michael-gemmell Sep 21, 2023
01ea6b4
save_pickle now takes dictionary as argument
michael-gemmell Sep 21, 2023
ada6ce0
stashing
michael-gemmell Sep 21, 2023
5609b93
stashing
michael-gemmell Sep 21, 2023
1e18dc3
merging
michael-gemmell Sep 21, 2023
ecdbb65
Rough batch script for running BDA
michael-gemmell Sep 21, 2023
9cd5d26
merging
michael-gemmell Sep 21, 2023
cf736c7
sampler stopping condition based on moments added
michael-gemmell Oct 2, 2023
4730dc8
cleaned up and added virtual observations
michael-gemmell Oct 12, 2023
ec77188
normalising TS profiles before fitting
michael-gemmell Oct 12, 2023
fd8e640
background can be class attribute or call argument
michael-gemmell Oct 12, 2023
3196e03
placeholder for filtering methods
michael-gemmell Oct 12, 2023
4979c2a
gutted workflow class / methods removed to context objects
michael-gemmell Oct 12, 2023
97bcfa8
gutted workflow class / methods removed to context objects
michael-gemmell Oct 12, 2023
1427561
moved ln_prior to stand alone func
michael-gemmell Oct 12, 2023
33ec07a
Initialisation fixed
michael-gemmell Oct 12, 2023
9b40044
fixed formatting/plotting of optimiser results and adjusted moments s…
michael-gemmell Oct 17, 2023
1bc37b1
TS dimension bug fix added
michael-gemmell Oct 17, 2023
bc5634e
formatting for results dict fixed
michael-gemmell Oct 17, 2023
365e4d8
Now plots for all time points
michael-gemmell Oct 17, 2023
2c6634a
renamed some nodes and removed TIME_BINS
michael-gemmell Oct 17, 2023
65060e9
renamed bayes_workflow_example -> bayes_workflow
michael-gemmell Oct 17, 2023
d81581f
missing self.
michael-gemmell Oct 17, 2023
bfd6027
missing self.
michael-gemmell Oct 17, 2023
1b05580
removed debug printing from sample_with_moments
michael-gemmell Oct 17, 2023
1955db2
renamed batch_bda -> bda_run
michael-gemmell Oct 17, 2023
89393c9
debug printing for stopping condition is now an optional input
michael-gemmell Oct 17, 2023
256e411
moved back to flat delta moment stopping condition
michael-gemmell Oct 17, 2023
4228a4e
default to plotting to test
michael-gemmell Oct 17, 2023
4899aef
comments
michael-gemmell Oct 17, 2023
f0590a6
moved sample_from_priors to external function
michael-gemmell Oct 17, 2023
bb2e8e9
moved checking of prior keys from sample_from_priors to BayesSettings…
michael-gemmell Oct 19, 2023
55e01f8
_build_bckc now creates nested dictionary instead of flat
michael-gemmell Oct 24, 2023
5219422
filtering now works on spectra with t dim
michael-gemmell Oct 24, 2023
153536b
renamed bayes_settings -> blackbox_settings
michael-gemmell Oct 24, 2023
23150eb
renamed bayes_settings -> blackbox_settings
michael-gemmell Oct 24, 2023
d1a7654
renamed bayes_settings -> blackbox_settings
michael-gemmell Oct 24, 2023
59071ac
renamed bayes_settings -> blackbox_settings
michael-gemmell Oct 24, 2023
8e3cfa1
added mock transforms to default_factory
michael-gemmell Oct 24, 2023
dc3d7a3
cxfi_tws_c example transform added
michael-gemmell Oct 24, 2023
1f4108f
ModelSettings dataclass created to handle model initialisation and ca…
michael-gemmell Oct 24, 2023
c9cb344
missing .self
michael-gemmell Oct 24, 2023
8fbc4ee
stopping criteria sampling rate added to OptimiserSettings
michael-gemmell Oct 24, 2023
b0f62e4
typo
michael-gemmell Oct 24, 2023
5f791b7
added moments tests
michael-gemmell Oct 25, 2023
5cc2c2d
adjusted nimp.y0 prior
michael-gemmell Oct 25, 2023
ffd70db
example run updated
michael-gemmell Oct 25, 2023
54fb968
more tests for walkers
michael-gemmell Oct 26, 2023
7e72f8d
typo
michael-gemmell Oct 26, 2023
d54840d
11089 pulse added
michael-gemmell Oct 30, 2023
78fd127
temporarily added channel filters for TS / CXFF_TWS_C
michael-gemmell Oct 30, 2023
0ec72e5
11089 run added
michael-gemmell Oct 31, 2023
e39440d
expanded wped prior for fitting the pedestal
michael-gemmell Oct 31, 2023
70578c8
Test for walker moves
michael-gemmell Nov 2, 2023
5398211
testing DE-MCMC
michael-gemmell Nov 2, 2023
4cffa96
Full 11089 run with DEMOVES
michael-gemmell Nov 2, 2023
f4726e4
testing without emcee move.DESnookerMove
michael-gemmell Nov 3, 2023
7039991
Testing without DESnookerMove
michael-gemmell Nov 3, 2023
de2c474
Merge branch 'st40' into bda
marcosertoli Nov 3, 2023
5ca60f4
Merge branch 'bda' of github.com:ukaea/Indica into bda
michael-gemmell Nov 17, 2023
aa88c26
fixed rho_poloidal -> rhop
michael-gemmell Dec 5, 2023
d3b1bd4
removed wrapper of kernels
michael-gemmell Dec 7, 2023
c3e4a36
dt added to read_ts
michael-gemmell Dec 7, 2023
5923ea6
using binned data for fits instead of raw
michael-gemmell Dec 7, 2023
9a1420b
hack to prevent dtype interpolation error
michael-gemmell Dec 7, 2023
c89eed8
closing plots after use
michael-gemmell Dec 7, 2023
b7fd1a7
set_ts_profiles added to plasma_context
michael-gemmell Dec 7, 2023
cb8d99c
update_profiles now only updates profiles which match parameters prov…
michael-gemmell Dec 7, 2023
f5aadf3
__main__ params adjusted
michael-gemmell Dec 8, 2023
fc577a2
reformatting __main__ to run mock example
michael-gemmell Dec 8, 2023
97cad54
fixed error in zeff calculatio
michael-gemmell Dec 13, 2023
5e16452
removed reading ts and added post_process_ts
michael-gemmell Dec 14, 2023
5abc891
reorganising post_processing args
michael-gemmell Jan 18, 2024
7f7ac08
R_midplane added to profiles
michael-gemmell Jan 18, 2024
883546b
gitcommit and user added
michael-gemmell Jan 18, 2024
67c1a44
R_midplane added
michael-gemmell Jan 18, 2024
fededb1
R_midplane co-ordinate transform added
michael-gemmell Jan 18, 2024
2b9555a
violin plots take error as input
michael-gemmell Jan 18, 2024
e5a404b
example workflow corrected
michael-gemmell Jan 18, 2024
6be843f
error handling for empty data dicts
michael-gemmell Jan 18, 2024
f9e9452
updated to include set_ts option
michael-gemmell Jan 18, 2024
a14650e
python 3.8 -> poetry 3.9
michael-gemmell Jan 18, 2024
b006ca5
python >= 3.9 required
michael-gemmell Jan 18, 2024
a2876ae
pint added for standard_utility
michael-gemmell Jan 18, 2024
5663c35
default to 43000000
michael-gemmell Jan 22, 2024
ed43c55
bda_nodes function is now a global var
michael-gemmell Jan 22, 2024
865c4a1
check for tree and use NEW or EDIT mode
michael-gemmell Jan 22, 2024
1637d1e
check for tree and use NEW or EDIT mode
michael-gemmell Jan 22, 2024
b85bde8
renaming Nimp -> niz1
michael-gemmell Jan 22, 2024
503fd5c
Plasma updating now works with Niz1/2_prof instead of Nimp_prof
michael-gemmell Jan 22, 2024
e0fed0e
Changing default use case
michael-gemmell Jan 24, 2024
3b12469
Nimp_prof -> Niz1_prof
michael-gemmell Jan 24, 2024
5a62608
Option for fitting rho profiles as if symmetric in -rho
michael-gemmell Jan 24, 2024
eb30028
Option for fitting rho profiles as if symmetric in -rho
michael-gemmell Jan 24, 2024
3eefe45
Alsu's 11XXX runs added
michael-gemmell Jan 26, 2024
0db793b
default values adjusted for Nimp
michael-gemmell Jan 26, 2024
d7dabeb
priors adjusted for Nimp
michael-gemmell Jan 26, 2024
50cea66
11032 added
michael-gemmell Feb 5, 2024
6d39408
fixed missing self.
michael-gemmell Feb 5, 2024
174b7a4
fixed missing self.
michael-gemmell Feb 5, 2024
9c7e76d
initial commit
michael-gemmell Feb 5, 2024
718e091
mocking opt_data keys
michael-gemmell Feb 5, 2024
ac148e9
speeding up run by limiting iterations
michael-gemmell Feb 5, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 143 additions & 0 deletions indica/bayesblackbox.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll leave the rest of the review to Jussi for now!

Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
from copy import deepcopy
import warnings
from flatdict import FlatDict
import numpy as np
from scipy.stats import uniform
np.seterr(all="ignore")
warnings.simplefilter("ignore", category=FutureWarning)

def gaussian(x, mean, sigma):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have too many of gaussian() functions across the project...
profiles_gauss has a quite general version of it, including background, amplitude, width and centroid, but it's missing the normalisation that you have in this version...
Can we generalise it and put in just 1 place, e.g. utilities or physics?

return 1 / (sigma * np.sqrt(2 * np.pi)) * np.exp(-1 / 2 * ((x - mean) / sigma) ** 2)


def get_uniform(lower, upper):
# Less confusing parameterisation of scipy.stats uniform
return uniform(loc=lower, scale=upper - lower)


def ln_prior(priors: dict, parameters: dict):
ln_prior = 0
for prior_name, prior_func in priors.items():
param_names_in_prior = [x for x in parameters.keys() if x in prior_name]
if param_names_in_prior.__len__() == 0:
# if prior assigned but no parameter then skip
continue
param_values = [parameters[x] for x in param_names_in_prior]
if hasattr(prior_func, "pdf"):
# for scipy.stats objects use pdf / for lambda functions just call
ln_prior += np.log(prior_func.pdf(*param_values))
else:
# if lambda prior with 2+ args is defined when only 1 of
# its parameters is given ignore it
if prior_func.__code__.co_argcount != param_values.__len__():
continue
else:
# Sorting to make sure args are given in the same order
# as the prior_name string
name_index = [
prior_name.find(param_name_in_prior)
for param_name_in_prior in param_names_in_prior
]
sorted_name_index, sorted_param_values = (
list(x) for x in zip(*sorted(zip(name_index, param_values)))
)
ln_prior += np.log(prior_func(*sorted_param_values))
return ln_prior

class BayesBlackBox:
"""
Bayesian black box model that creates _ln_posterior function from plasma and diagnostic model objects

Parameters
----------
data
processed diagnostic data of format [diagnostic].[quantity]
plasma_context
plasma context has methods for using plasma object
model_handler
model_handler object to be called by ln_posterior
quant_to_optimise
quantity from data which will be optimised with bckc from diagnostic_models
priors
prior functions to apply to parameters e.g. scipy.stats.rv_continuous objects

"""

def __init__(
self,
data: dict,
quant_to_optimise: list,
priors: dict,

plasma_context = None,
model_context = None,

percent_error: float = 0.10,
):
self.data = data
self.quant_to_optimise = quant_to_optimise
self.priors = priors

self.plasma_context = plasma_context
self.model_context = model_context

self.percent_error = percent_error

missing_data = list(set(quant_to_optimise).difference(data.keys()))
if missing_data: # gives list of keys in quant_to_optimise but not data
raise ValueError(f"{missing_data} not found in data given")

def ln_likelihood(self):
ln_likelihood = 0
for key in self.quant_to_optimise:
time_coord = self.plasma_context.plasma.time_to_calculate
model_data = self.bckc[key]
exp_data = self.data[key].sel(t=time_coord)
exp_error = exp_data * self.percent_error

if hasattr(self.data[key], "error"):
if (self.data[key].error != 0).any():
# TODO: Some models have an error of 0 given
exp_error = self.data[key].error.sel(t=time_coord)

_ln_likelihood = np.log(gaussian(model_data, exp_data, exp_error))
# treat channel as key dim which isn't averaged like other dims
if "channel" in _ln_likelihood.dims:
_ln_likelihood = _ln_likelihood.sum(dim="channel", skipna=True)
ln_likelihood += _ln_likelihood.mean(skipna=True).values
return ln_likelihood


def ln_posterior(self, parameters: dict, **kwargs):
"""
Posterior probability given to optimisers

Parameters
----------
parameters
inputs to optimise
kwargs
kwargs for models

Returns
-------
ln_posterior
log of posterior probability
blob
model outputs from bckc and kinetic profiles
"""

_ln_prior = ln_prior(self.priors, parameters)
if _ln_prior == -np.inf: # Don't call models if outside priors
return -np.inf, {}

self.plasma_context.update_profiles(parameters)
plasma_attributes = self.plasma_context.return_plasma_attrs()

self.bckc = FlatDict(self.model_context._build_bckc(parameters, **kwargs), ".") # model calls

_ln_likelihood = self.ln_likelihood() # compare results to data
ln_posterior = _ln_likelihood + _ln_prior

blob = deepcopy({**self.bckc, **plasma_attributes})
return ln_posterior, blob
Loading
Loading