uhammer package

All listed classes and functions are available by importing from uhammer.

uhammer.lnprob_adapters module

uhammer.lnprob_adapters.distribution(prob_density_function, ndim, ranges)[source]

Helper to sample from a distribution.

Returns lnprob function and Parameters object to be used in sample.

Parameters
  • prob_density_function – Function taking vector of length ndim, must return logarithm of a probability density.

  • ndim – Dimension of input to prob_density_function.

  • ranges – Either tuple or list of tuples of numbers to restrict coordinates when sampling.

uhammer.lnprob_adapters.gaussian(cov)[source]

Returns logarithmic probability density function for gaussian with mean 0 and given covariance cov.

Parameters

cov – Covariance as a single number (real case) or square invertible matrix (multivariate case).

uhammer.lnprob_adapters.model(forward, probability, input_data, measured_data)[source]

Creates a function used as lnprob argument for uhammer.sample function..

Parameters
  • forward – models forward computation. Takes two arguments: p which must be an instance of Parameters and the given input_data.

  • probability – A function which computes to probability of difference of the computed forward model and the given measured_data.

  • input_data – Input data passed as second argument to forward.

  • measured_data – Measured data. Must be compatible with the return value of forward.

uhammer.parameters module

class uhammer.parameters.Parameters[source]

Bases: object

Collection of used parameters incl. names and ranges.

add(name, range_, start_value=None)[source]

Adds a parameter with given name and value range_ to the collection.

Parameters
  • name – Name of type str.

  • range – Allowed range as tuple or list of two float values. First value must be smaller than the second.

  • start_value – optional value to start sampling from, reduces burn-in phase if estimation for parameter exists.

start_value(name)[source]

uhammer.persisting module

uhammer.persisting.dont_persist()[source]
uhammer.persisting.load_samples(path)[source]

loads samples for persisted sampler file

uhammer.persisting.persist_every_n_iterations(n, path)[source]

Return value of this function can be passed as argument persist to sample function to store state during sampling. This allows later continuation of sampling, e.g. after crashes.

This persister saves state every n iterations and in case of failure.

Parameters
  • n – persist after every n iterations of the sampler

  • path – file path to store the relevant information.

uhammer.persisting.persist_final(path)[source]

Return value of this function can be passed as argument persist to sample function to store state during sampling. This allows later continuation of sampling, e.g. after crashes.

This persister only stores the information when sampling finishes and in case of failure.

Parameters

path – file path to store the relevant information.

uhammer.persisting.persist_on_error(path)[source]

Return value of this function can be passed as argument persist to sample function to store state during sampling. This allows later continuation of sampling, e.g. after crashes.

This persister only stores the information only in case of failure.

Parameters

path – file path to store the relevant information.

uhammer.sampler module

class uhammer.sampler.PickableEmceeSampler(nwalkers, ndim, log_prob_fn, pool=None, moves=None, args=None, kwargs=None, backend=None, vectorize=False, blobs_dtype=None, a=None, postargs=None, threads=None, live_dangerously=None, runtime_sortingfn=None)[source]

Bases: emcee.ensemble.EnsembleSampler

uhammer.sampler.capture_output_path(prefix, parallel)[source]
uhammer.sampler.continue_sampling(persistence_file_path, n_samples, parallel=False, show_progress=False, show_output=True, output_prefix=None, persist=<uhammer.persisting._SamplerPersister object>, verbose=True)[source]

Continues sampling from a persisted sampler.

Original arguments of sample() like lnprob, n_walkers_per_param`, seed, etc. are reused from the persisted sampler and can not be modified anymore.

Parameters
  • persistence_file_path – Path to a perviously persisted sampler. This includes lnprob, parameters and number of walkers.

  • n_samples – Number of samples to generate. So the return value is a numpy array of shape (n_samples, len(p)).

  • parallel – Run in sequention or parallel mode ? Automatic dection of number of cores, also auto detects if code is run using mpirun.

  • show_progress – Show progress of sampling.

  • show_output – set to True if you want to see the output from the workers on the terminal, else set it to False.

  • output_prefix – prefix for file path to record output from the workers. Default is None which means no recording of output.

  • persist – persisting strategy, on of the functions in persisting.

  • verbose – boolean value, if True continue_sampling() prints some information.

This function returns two values:

  • computed samples as a numpy array of shape (n_samples, len(p)

  • computed log probabilites as numpy array of shape (n_samples,).

Samples are arranged as follows, log probbilites in similar order:

samples

p-dimensional sample from walker 0

p-dimensional sample from walker 1

p-dimensional sample from walker n - 1

p-dimensional sample from walker 0

p-dimensional sample from walker 1

uhammer.sampler.sample(lnprob, p, args=None, n_walkers_per_param=10, seed=None, n_samples=1000, parallel=False, show_progress=False, show_output=True, output_prefix=None, persist=<uhammer.persisting._SamplerPersister object>, verbose=True)[source]

Runs emcee sampler to generate samples from the declared distribution.

Parameters
  • lnprob – Function for computing the natural logarithm of the distribution to sample from. Fist argument is a object with the current values of the declared parameters as attributes. The function may accept additioanal arguments for auxiliary data.

  • p – Instance of the Parameters class.

  • args – List with auxiliary values. Can also be None or [].

  • n_walkers_per_param – Number of walkers per parameter. Results in len(p) * n_walkers_per_param walkers in total.

  • seed – Used random seed in case you want reproducible samples. Skip this argument or use None else.

  • n_samples – Number of samples to generate. So the return value is a numpy array of shape (n_samples, len(p)).

  • parallel – Run in sequention or parallel mode? Automatically detects number of cores, also detects if code is run using mpirun.

  • show_progress – Show progress of sampling.

  • show_output – set to True if you want to see the output from the workers on the terminal, else set it to False.

  • output_prefix – prefix for file path to record output from the workers. Default is None which means no recording of output.

  • persist – persisting strategy, on of the functions in persisting.

  • verbose – boolean value. If set to True sample() prints extra information.

This function returns two values:

  • computed samples as a numpy array of shape (n_samples, len(p)

  • computed log probabilites as numpy array of shape (n_samples,).

Samples are arranged as follows, log probbilites in similar order:

samples

p-dimensional sample from walker 0

p-dimensional sample from walker 1

p-dimensional sample from walker n - 1

p-dimensional sample from walker 0

p-dimensional sample from walker 1

Module contents

class uhammer.Unbuffered(stream)[source]

Bases: object

write(data)[source]
writelines(datas)[source]