linna.util
Module Contents
Classes
This takes a binary file for reading a pickle data stream. |
|
A reimplimentation of |
|
pool class if one wish to to multiprocess |
|
Transform parameters so that all the prior is gaussian with zero mean and unit variance |
|
Inverse the |
|
prepare data for torch |
|
Transform data vector from y-->y/sigma |
|
Transform data vector from y-->y sigma (Api is the same as |
|
Transform parameters from x --> (x-mean)/std or x --> (log10(x)-mean)/std |
|
Transform data vector: y-->y*std+mean or np.exp(y-->y*std+mean) |
|
Transform data vector: y-->(y-mean)/std or (np.log(y)-mean)/std (API tis the same as `` Y_transform_class`` |
|
Only for internal use |
|
A class to perform neural network sampling for each iteration |
|
Class do loglikelihood |
|
Class do derivative of loglikelihood (API the same as |
|
Class do 2nd derivative of loglikelihood (API the same as |
|
Class for internal use |
|
Class defined loss function |
|
Class for validation metric (API the same as loss) |
|
Priors handling |
Functions
|
|
|
|
|
|
|
transform a guaaisan distributed random variable to a uniformly distributed variable |
inverse transform a guaaisan distributed random variable to a uniformly distributed variable |
|
|
Retrieve the trained model |
|
Retrieve the trained model (more user friendly than retrieve_model) |
|
|
|
internal function |
|
Generate training point |
|
Internal function |
|
Internal function |
|
Internal function |
|
Internal function |
|
Run mcmc using the trained model |
|
Internal function |
Attributes
- class linna.util.CPU_Unpickler[source]
Bases:
linna.nn.pickle.UnpicklerThis takes a binary file for reading a pickle data stream.
The protocol version of the pickle is detected automatically, so no protocol argument is needed. Bytes past the pickled object’s representation are ignored.
The argument file must have two methods, a read() method that takes an integer argument, and a readline() method that requires no arguments. Both methods should return bytes. Thus file can be a binary file object opened for reading, an io.BytesIO object, or any other custom object that meets this interface.
Optional keyword arguments are fix_imports, encoding and errors, which are used to control compatibility support for pickle stream generated by Python 2. If fix_imports is True, pickle will try to map the old Python 2 names to the new names used in Python 3. The encoding and errors tell pickle how to decode 8-bit string instances pickled by Python 2; these default to ‘ASCII’ and ‘strict’, respectively. The encoding can be ‘bytes’ to read these 8-bit string instances as bytes objects.
- find_class(self, module, name)[source]
Return an object from a specified module.
If necessary, the module will be imported. Subclasses may override this method (e.g. to restrict unpickling of arbitrary classes and functions).
This method is called whenever a class or a function object is needed. Both arguments passed are str objects.
- linna.util.read_chain_and_cut(chainname, nk, ntimes=20, walkercut=False, method='emcee', flat=False)[source]
- class linna.util.chtoPool(comm=None)[source]
Bases:
schwimmbad.MPIPoolA reimplimentation of
schwimmbad.MPIPoolthat will not broacast redundant function- map(self, worker, tasks, callback=None)[source]
Evaluate a function or callable on each task in parallel using MPI.
The callable,
worker, is called on each element of thetasksiterable. The results are returned in the expected order (symmetric withtasks).- Parameters
worker (callable) – A function or callable object that is executed on each element of the specified
tasksiterable. This object must be picklable (i.e. it can’t be a function scoped within a function or alambdafunction). This should accept a single positional argument and return a single object.tasks (iterable) – A list or iterable of tasks. Each task can be itself an iterable (e.g., tuple) of values or data to pass in to the worker function.
callback (callable, optional) – An optional callback function (or callable) that is called with the result from each worker run and is executed on the master process. This is useful for, e.g., saving results to a file, since the callback is only called on the master thread.
- Returns
A list of results from the output of each
worker()call.- Return type
list
- class linna.util.chtoMultiprocessPool(nwalker)[source]
pool class if one wish to to multiprocess
- linna.util.gauss2unif(x)[source]
transform a guaaisan distributed random variable to a uniformly distributed variable
- Parameters
x (torch.tensor) – input
- Returns
output
- Return type
torch.tensor
- linna.util.invgauss2unif(x)[source]
inverse transform a guaaisan distributed random variable to a uniformly distributed variable
- Parameters
x (torch.tensor) – input
- Returns
output
- Return type
torch.tensor
- class linna.util.Transform(priors)[source]
Transform parameters so that all the prior is gaussian with zero mean and unit variance
- __call__(self, x, returnnumpy=True, inputnumpy=True)[source]
Transform perameters so that all the prior is gaussian with zero mean and unit variance
- Parameters
x (nd array or torch array) – array of parameters
returnnumpy (bool) – If true, then the return value will be in
numpyarray. Otherwise, the return value will be intorchtensorinputnumpy (bool) – If true, the return value should be in
numpyarray. Otherwise, the return value should be intorchtensor
- Returns
depends on the input parameters
returnnumpy- Return type
numpy array or torch tensor
- class linna.util.invTransform(priors)[source]
Inverse the
`Transform`function.- __call__(self, x, returnnumpy=True, inputnumpy=True)[source]
- Parameters
x (nd array or torch array) – array of parameters
returnnumpy (bool) – If true, then the return value will be in
numpyarray. Otherwise, the return value will be intorchtensorinputnumpy (bool) – If true, the return value should be in
numpyarray. Otherwise, the return value should be intorchtensor
- Returns
depends on the input parameters
returnnumpy- Return type
numpy array or torch tensor
- class linna.util.Y_transform_data(sigma, device)[source]
Transform data vector from y–>y/sigma
- class linna.util.Y_invtransform_data(sigma, device)[source]
Transform data vector from y–>y sigma (Api is the same as
Y_transform_data)
- class linna.util.X_transform_class(X_mean, X_std, device, dolog10index=None)[source]
Transform parameters from x –> (x-mean)/std or x –> (log10(x)-mean)/std
- class linna.util.Y_transform_class(y_mean, y_std, dev, ypositive=False)[source]
Transform data vector: y–>y*std+mean or np.exp(y–>y*std+mean)
- class linna.util.Y_invtransform_class(y_mean, y_std, data_tensor, dev, ypositive=False)[source]
Transform data vector: y–>(y-mean)/std or (np.log(y)-mean)/std (API tis the same as `` Y_transform_class``
- class linna.util._FunctionWrapper(f, args, kwargs)[source]
Bases:
objectOnly for internal use :meta private:
- linna.util.retrieve_model(outdir, inshape, outshape, nnmodel_in=ChtoModelv2)[source]
Retrieve the trained model
- Parameters
Outdir (string) – directory of the outdir
inshape (int) – input vector size of the model
outshape (int) – output vector size of the model
nnmodel_in (callable, optional) – neural network instance defined in nn.py
- Returns
model linna.util.Y_invtransform_data: callable that transform the model to the same space as data vector
- Return type
linna.predictor_gpu.Predictor
- linna.util.retrieve_model_wrapper_in(outdir, nnmodel_in=ChtoModelv2, no_grad=True)[source]
Retrieve the trained model (more user friendly than retrieve_model)
- Parameters
Outdir (string) – directory of the outdir
nnmodel_in (callable, optional) – neural network instance defined in nn.py
no_grad (bool) – True: not keep gradient information, Flase: keep gradient information
- Returns
a function takes in cosmological and nuisance parameters (torch tensor) and returns the prediction of the data vector using the neural network. Note that the output is in the format of torch.tensor, so that its differentiation can be evaluated.
- Return type
model (callable)
- class linna.util.NN_samplerv1(outdir, prior_range)[source]
A class to perform neural network sampling for each iteration
- generate_training_data(self, samples, model, pool=None, args=None, kwargs=None)[source]
Generate predicted data vector from a set of parameters
- Parameters
samples (ndarray) – 2d array containing data with float type. Set of parameters in each row
model (function) – a function that take a row of samples, args and kwargs and return the predicted data vector
pool (mpi pool, optional) – a mpi pool instance that can do pool.map(function, iterables).
args (lists, optional) – args or kwargs to be passed to model
kwargs (lists, optional) – args or kwargs to be passed to model
- Returns
each row corresponds to the output of model at each row of the samples.
- Return type
ndarray
- gensample_flat(self, Nsamples, omegab2cut=None)[source]
Generate parameters for training and validation using latin hypercube.
- Parameters
Nsamples (int) – number of samples to be generated.
omegab2cut (list of int) – 2 elements containing the lower and upper limits of omegab*h^2
- Returns
a 2d array containing data with float type. Parameters for training and validation
- Return type
ndarray
- gensample_chain(self, Nsamples, chain_in, nsigma, omegab2cut=None)[source]
Generate parameters for training and validation from a chain using latin hyper cube.
- Parameters
Nsamples (int) – number of samples to be generated.
chain_in (ndarray) – a mcmc chain.
nsigma (int) – up to this number an mcmc chain is generated
omegab2cut (list of int) – 2 elements containing the lower and upper limits of omegab*h^2
- Returns
a 2d array containing data with float type. Parameters for training and validation
- Return type
ndarray
- gensample_chain_randomsample(self, Nsamples, chain_in, nsigma, omegab2cut=None)[source]
Generate parameters for training and validation from a chain using latin hyper cube.
- Parameters
Nsamples (int) – number of samples to be generated.
chain_in (ndarray) – a mcmc chain.
nsigma (int) – up to this number an mcmc chain is generated
omegab2cut (list of int) – 2 elements containing the lower and upper limits of omegab*h^2
- Returns
a 2d array containing data with float type. Parameters for training and validation
- Return type
ndarray
- emcee_sample(self, log_prob, ndim, nwalkers, init, pool, transform, ntimes=50, tautol=0.01, dlnp=None, ddlnp=None, meanshift=0.1, stdshift=0.1, nk=1)[source]
Generate MCMC chains using emcee.
- Parameters
log_prob (function) – function of posterior.
ndim (int) – the dimension of posterior
nwalkers (int) – number of mcmc walkers
init (ndarray) – array of init points of the sampler
pool (mpi pool, optional) – a mpi pool instance that can do pool.map(function, iterables)
transform (function) – mapping mcmc samples to actually parameters
- Zeus_sample(self, log_prob, ndim, nwalkers, init, pool, transform, ntimes=50, tautol=0.01, dlnp=None, ddlnp=None, meanshift=0.1, stdshift=0.1, nk=1)[source]
Generate MCMC chains using zeus.
- Parameters
log_prob (function) – function of posterior.
ndim (int) – the dimension of posterior
nwalkers (int) – number of mcmc walkers
init (ndarray) – array of init points of the sampler
pool (mpi pool, optional) – a mpi pool instance that can do pool.map(function, iterables)
transform (function) – mapping mcmc samples to actually parameters
- class linna.util.Log_prob(data_new, invcov_new, model, y_invtransform_data, transform, temperature, loglikelihoodfunc, nograd=False)[source]
Class do loglikelihood
- class linna.util.Dlnp(data_new, invcov_new, model, y_invtransform_data, transform, temperature)[source]
Class do derivative of loglikelihood (API the same as
Log_prob)
- class linna.util.Ddlnp(data_new, invcov_new, model, y_invtransform_data, transform, temperature)[source]
Class do 2nd derivative of loglikelihood (API the same as
Log_prob)
- class linna.util.Auxilleryfunc(data_in, cov_tensor, inv_cov_tensor, y_transform_data, y_inv_transform, device)[source]
Class for internal use :meta private:
- class linna.util.Loss_fn(data_in, cov_tensor, inv_cov_tensor, y_transform_data, y_inv_transform, device)[source]
Class defined loss function
- class linna.util.Val_metric_fn(data_in, cov_tensor, inv_cov_tensor, y_transform_data, y_inv_transform, device)[source]
Class for validation metric (API the same as loss)
- linna.util.generate_training_point(theory, nnsampler, pool, outdir, ntrain, nval, data, invcov, chain=None, nsigma=1, omegab2cut=None, options=0, negloglike=None, nbest_in=None, chisqcut=None)[source]
Generate training point
- Parameters
theory (callable) – model
nnsampler (
NN_samplerv1instance) –pool (
chtoPoolinstance) – mpi pooloutdir (string) – output directory
ntrain (int) – number of training data
nval (int) – number of validation data
data (1d array) – float array, data vector
invcov (2d array) – float array, inverse of covariance matrix
chain (array or None) – if None: generate from prior, if and array: training sample will be generated using thie chain
nsigma (int) – if option ==0, this means we build a LH in nsignma region of the chain.
omegab2cut (list or None) – additional cut on omegabh2, if list, omegab2cut = [index of omegab, index of h, lower limit, upper limit]
options (int) – if 0, generate using Latin Hypercube. If 1, random sample the chain
negloglike (callable) – if not None, generate one sample usin maximum likelihood method (minimize negloglike)
nbest_in (int, optional) – number of samples to be included in the training set according to the optimizer
chisqcut (float, optional) – cut the training data if there chisq is greater than this value
- linna.util.train_nn(outdir, model, train_x, train_y, val_x, val_y, X_transform, y_transform, loss_fn, val_metric_fn, dev='cpu', verbose=False, retrain=True, pool=None, nocpu=False, size=0, rank=0, params=None)[source]
Internal function
- linna.util.train_NN(nnsampler, cov, inv_cov, sigma, outdir_in, outdir_list, data, dolog10index=None, ypositive=False, retrain=True, norder=2, temperature=None, docuda=False, pool=None, tsize=1, nnmodel_in=None, params=None, usebest=False)[source]
Internal function
- linna.util.run_mcmc(nnsampler, outdir, method, ndim, nwalkers, init, log_prob, dlnp=None, ddlnp=None, pool=None, transform=None, ntimes=50, tautol=0.01, meanshift=0.1, stdshift=0.1, nk=2)[source]
Run mcmc using the trained model
- Parameters
nnsampler (
NN_samplerv1instance) –outdir (string) – output directory
method (string) – mcmc method, only emcee or zeus is supported
ndim (int) – dimension of input parameters
nwalkers (int) – number of walkers
init (array) – numpy array with shape (nwalker, ndim)
log_probi (callable) – likelihood
pool (None or chtoPool) – mpi pool
transform (Transform or None) – function to transform input
ntimes (int) – number of autocorrelation time
tautol (float) – tolerance of autocorrelation time error
meanshift (float) – maximum shifts of parameter mean estimations between first half and second half of the chains in unit of sigma
stdshift (float) – maximum shift of parameter error estimation between first half and second half of the chains in unit of percent