本文整理汇总了Python中pycbc.io.FieldArray.from_kwargs方法的典型用法代码示例。如果您正苦于以下问题:Python FieldArray.from_kwargs方法的具体用法?Python FieldArray.from_kwargs怎么用?Python FieldArray.from_kwargs使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pycbc.io.FieldArray
的用法示例。
在下文中一共展示了FieldArray.from_kwargs方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: likelihood_stats
# 需要导入模块: from pycbc.io import FieldArray [as 别名]
# 或者: from pycbc.io.FieldArray import from_kwargs [as 别名]
def likelihood_stats(self):
"""Returns the log likelihood ratio and log prior as a FieldArray.
The returned array has shape ntemps x nwalkers x niterations.
"""
# likelihood has shape ntemps x nwalkers x niterations
logl = self._sampler.lnlikelihood
# get prior from posterior
logp = self._sampler.lnprobability - logl
# compute the likelihood ratio
loglr = logl - self.likelihood_evaluator.lognl
return FieldArray.from_kwargs(loglr=loglr, prior=logp)
示例2: likelihood_stats
# 需要导入模块: from pycbc.io import FieldArray [as 别名]
# 或者: from pycbc.io.FieldArray import from_kwargs [as 别名]
def likelihood_stats(self):
"""Returns the likelihood stats as a FieldArray, with field names
corresponding to the type of data returned by the likelihood evaluator.
The returned array has shape nwalkers x niterations. If no additional
stats were returned to the sampler by the likelihood evaluator, returns
None.
"""
stats = numpy.array(self._sampler.blobs)
if stats.size == 0:
return None
arrays = dict([[field, stats[:, :, fi]]
for fi, field in
enumerate(self.likelihood_evaluator.metadata_fields)])
return FieldArray.from_kwargs(**arrays).transpose()
示例3: likelihood_stats
# 需要导入模块: from pycbc.io import FieldArray [as 别名]
# 或者: from pycbc.io.FieldArray import from_kwargs [as 别名]
def likelihood_stats(self):
"""Returns the likelihood stats as a FieldArray, with field names
corresponding to the type of data returned by the likelihood evaluator.
The returned array has shape nwalkers x niterations. If no additional
stats were returned to the sampler by the likelihood evaluator, returns
None.
"""
stats = numpy.array(self._sampler.blobs)
if stats.size == 0:
return None
# we'll force arrays to float; this way, if there are `None`s in the
# blobs, they will be changed to `nan`s
arrays = {field: stats[..., fi].astype(float)
for fi, field in
enumerate(self.likelihood_evaluator.metadata_fields)}
return FieldArray.from_kwargs(**arrays).transpose()
示例4: compute_acls
# 需要导入模块: from pycbc.io import FieldArray [as 别名]
# 或者: from pycbc.io.FieldArray import from_kwargs [as 别名]
def compute_acls(cls, fp, start_index=None, end_index=None):
"""Computes the autocorrleation length for all variable args and
temperatures in the given file.
Parameter values are averaged over all walkers at each iteration and
temperature. The ACL is then calculated over the averaged chain. If
the returned ACL is `inf`, will default to the number of current
iterations.
Parameters
-----------
fp : InferenceFile
An open file handler to read the samples from.
start_index : {None, int}
The start index to compute the acl from. If None, will try to use
the number of burn-in iterations in the file; otherwise, will start
at the first sample.
end_index : {None, int}
The end index to compute the acl to. If None, will go to the end
of the current iteration.
Returns
-------
FieldArray
An ntemps-long `FieldArray` containing the ACL for each temperature
and for each variable argument, with the variable arguments as
fields.
"""
acls = {}
if end_index is None:
end_index = fp.niterations
tidx = numpy.arange(fp.ntemps)
for param in fp.variable_args:
these_acls = numpy.zeros(fp.ntemps, dtype=int)
for tk in tidx:
samples = cls.read_samples(fp, param, thin_start=start_index,
thin_interval=1, thin_end=end_index,
temps=tk, flatten=False)[param]
# contract the walker dimension using the mean, and flatten
# the (length 1) temp dimension
samples = samples.mean(axis=1)[0,:]
acl = autocorrelation.calculate_acl(samples)
if numpy.isinf(acl):
acl = samples.size
these_acls[tk] = acl
acls[param] = these_acls
return FieldArray.from_kwargs(**acls)
示例5: likelihood_stats
# 需要导入模块: from pycbc.io import FieldArray [as 别名]
# 或者: from pycbc.io.FieldArray import from_kwargs [as 别名]
def likelihood_stats(self):
"""Returns the log likelihood ratio and log prior as a FieldArray.
The returned array has shape ntemps x nwalkers x niterations.
"""
# likelihood has shape ntemps x nwalkers x niterations
logl = self._sampler.lnlikelihood
# get prior from posterior
logp = self._sampler.lnprobability - logl
# compute the likelihood ratio
loglr = logl - self.likelihood_evaluator.lognl
kwargs = {'loglr': loglr, 'prior': logp}
# if different coordinates were used for sampling, get the jacobian
if self.likelihood_evaluator.sampling_transforms is not None:
samples = self.samples
# convert to dict
d = {param: samples[param] for param in samples.fieldnames}
logj = self.likelihood_evaluator.logjacobian(**d)
kwargs['logjacobian'] = logj
return FieldArray.from_kwargs(**kwargs)
示例6: compute_acls
# 需要导入模块: from pycbc.io import FieldArray [as 别名]
# 或者: from pycbc.io.FieldArray import from_kwargs [as 别名]
def compute_acls(cls, fp, start_index=None, end_index=None):
"""Computes the autocorrleation length for all variable args for all
walkers for all temps in the given file. If the returned acl is inf,
will default to the number of requested iterations.
Parameters
-----------
fp : InferenceFile
An open file handler to read the samples from.
start_index : {None, int}
The start index to compute the acl from. If None, will try to use
the number of burn-in iterations in the file; otherwise, will start
at the first sample.
end_index : {None, int}
The end index to compute the acl to. If None, will go to the end
of the current iteration.
Returns
-------
FieldArray
An ntemps x nwalkers `FieldArray` containing the acl for each
walker and temp for each variable argument, with the variable
arguments as fields.
"""
acls = {}
if end_index is None:
end_index = fp.niterations
tidx = numpy.arange(fp.ntemps)
widx = numpy.arange(fp.nwalkers)
for param in fp.variable_args:
these_acls = numpy.zeros((fp.ntemps, fp.nwalkers), dtype=int)
for tk in tidx:
for wi in widx:
samples = cls.read_samples(
fp, param,
thin_start=start_index, thin_interval=1,
thin_end=end_index,
walkers=wi, temps=tk)[param]
acl = autocorrelation.calculate_acl(samples)
these_acls[tk, wi] = int(min(acl, samples.size))
acls[param] = these_acls
return FieldArray.from_kwargs(**acls)
示例7: _oldstyle_read_acls
# 需要导入模块: from pycbc.io import FieldArray [as 别名]
# 或者: from pycbc.io.FieldArray import from_kwargs [as 别名]
def _oldstyle_read_acls(fp):
"""Deprecated: reads acls from older style files.
Parameters
----------
fp : InferenceFile
An open file handler to read the acls from.
Returns
-------
FieldArray
An ntemps-long ``FieldArray`` containing the acls for every
temperature, with the variable arguments as fields.
"""
group = fp.samples_group + '/{param}/temp{tk}'
tidx = numpy.arange(fp.ntemps)
arrays = {}
for param in fp.variable_args:
arrays[param] = numpy.array([
fp[group.format(param=param, tk=tk)].attrs['acl']
for tk in tidx])
return FieldArray.from_kwargs(**arrays)
示例8: read_acls
# 需要导入模块: from pycbc.io import FieldArray [as 别名]
# 或者: from pycbc.io.FieldArray import from_kwargs [as 别名]
def read_acls(fp):
"""Reads the acls of all the walker chains saved in the given file.
Parameters
----------
fp : InferenceFile
An open file handler to read the acls from.
Returns
-------
FieldArray
An nwalkers-long `FieldArray` containing the acl for each walker
and each variable argument, with the variable arguments as fields.
"""
group = fp.samples_group + '/{param}/walker{wi}'
widx = numpy.arange(fp.nwalkers)
arrays = {}
for param in fp.variable_args:
arrays[param] = numpy.array([
fp[group.format(param=param, wi=wi)].attrs['acl']
for wi in widx])
return FieldArray.from_kwargs(**arrays)
示例9: copy_samples
# 需要导入模块: from pycbc.io import FieldArray [as 别名]
# 或者: from pycbc.io.FieldArray import from_kwargs [as 别名]
def copy_samples(self, other, parameters=None, parameter_names=None,
read_args=None, write_args=None):
"""Should copy samples to the other files.
Parameters
----------
other : InferenceFile
An open inference file to write to.
parameters : list of str, optional
List of parameters to copy. If None, will copy all parameters.
parameter_names : dict, optional
Rename one or more parameters to the given name. The dictionary
should map parameter -> parameter name. If None, will just use the
original parameter names.
read_args : dict, optional
Arguments to pass to ``read_samples``.
write_args : dict, optional
Arguments to pass to ``write_samples``.
"""
# select the samples to copy
logging.info("Reading samples to copy")
if parameters is None:
parameters = self.variable_params
# if list of desired parameters is different, rename
if set(parameters) != set(self.variable_params):
other.attrs['variable_params'] = parameters
samples = self.read_samples(parameters, **read_args)
logging.info("Copying {} samples".format(samples.size))
# if different parameter names are desired, get them from the samples
if parameter_names:
arrs = {pname: samples[p] for p, pname in parameter_names.items()}
arrs.update({p: samples[p] for p in parameters if
p not in parameter_names})
samples = FieldArray.from_kwargs(**arrs)
other.attrs['variable_params'] = samples.fieldnames
logging.info("Writing samples")
other.write_samples(other, samples, **write_args)
示例10: copy
# 需要导入模块: from pycbc.io import FieldArray [as 别名]
# 或者: from pycbc.io.FieldArray import from_kwargs [as 别名]
def copy(self, other, parameters=None, parameter_names=None,
posterior_only=False, **kwargs):
"""Copies data in this file to another file.
The samples and stats to copy may be down selected using the given
kwargs. All other data (the "metadata") are copied exactly.
Parameters
----------
other : str or InferenceFile
The file to write to. May be either a string giving a filename,
or an open hdf file. If the former, the file will be opened with
the write attribute (note that if a file already exists with that
name, it will be deleted).
parameters : list of str, optional
List of parameters to copy. If None, will copy all parameters.
parameter_names : dict, optional
Rename one or more parameters to the given name. The dictionary
should map parameter -> parameter name. If None, will just use the
original parameter names.
posterior_only : bool, optional
Write the samples and likelihood stats as flattened arrays, and
set other's posterior_only attribute. For example, if this file
has a parameter's samples written to
`{samples_group}/{param}/walker{x}`, then other will have all of
the selected samples from all walkers written to
`{samples_group}/{param}/`.
\**kwargs :
All other keyword arguments are passed to `read_samples`.
Returns
-------
InferenceFile
The open file handler to other.
"""
if not isinstance(other, h5py.File):
# check that we're not trying to overwrite this file
if other == self.name:
raise IOError("destination is the same as this file")
other = InferenceFile(other, 'w')
# copy metadata over
self.copy_metadata(other)
# update other's posterior attribute
if posterior_only:
other.attrs['posterior_only'] = posterior_only
# select the samples to copy
logging.info("Reading samples to copy")
if parameters is None:
parameters = self.variable_args
# if list of desired parameters is different, rename variable args
if set(parameters) != set(self.variable_args):
other.attrs['variable_args'] = parameters
# if only the posterior is desired, we'll flatten the results
if not posterior_only and not self.posterior_only:
kwargs['flatten'] = False
samples = self.read_samples(parameters, **kwargs)
logging.info("Copying {} samples".format(samples.size))
# if different parameter names are desired, get them from the samples
if parameter_names:
arrs = {pname: samples[p] for p,pname in parameter_names.items()}
arrs.update({p: samples[p] for p in parameters
if p not in parameter_names})
samples = FieldArray.from_kwargs(**arrs)
other.attrs['variable_args'] = samples.fieldnames
logging.info("Writing samples")
other.samples_parser.write_samples_group(other, self.samples_group,
samples.fieldnames, samples)
# do the same for the likelihood stats
logging.info("Reading stats to copy")
stats = self.read_likelihood_stats(**kwargs)
logging.info("Writing stats")
other.samples_parser.write_samples_group(other, self.stats_group,
stats.fieldnames, stats)
# if any down selection was done, re-set the burn in iterations and
# the acl, and the niterations.
# The last dimension of the samples returned by the sampler should
# be the number of iterations.
if samples.shape[-1] != self.niterations:
other.attrs['acl'] = 1
other.attrs['burn_in_iterations'] = 0
other.attrs['niterations'] = samples.shape[-1]
return other
示例11: create_density_plot
# 需要导入模块: from pycbc.io import FieldArray [as 别名]
# 或者: from pycbc.io.FieldArray import from_kwargs [as 别名]
def create_density_plot(xparam, yparam, samples, plot_density=True,
plot_contours=True, percentiles=None, cmap='viridis',
contour_color=None, xmin=None, xmax=None, ymin=None, ymax=None,
exclude_region=None, fig=None, ax=None, use_kombine=False):
"""Computes and plots posterior density and confidence intervals using the
given samples.
Parameters
----------
xparam : string
The parameter to plot on the x-axis.
yparam : string
The parameter to plot on the y-axis.
samples : dict, numpy structured array, or FieldArray
The samples to plot.
plot_density : {True, bool}
Plot a color map of the density.
plot_contours : {True, bool}
Plot contours showing the n-th percentiles of the density.
percentiles : {None, float or array}
What percentile contours to draw. If None, will plot the 50th
and 90th percentiles.
cmap : {'viridis', string}
The name of the colormap to use for the density plot.
contour_color : {None, string}
What color to make the contours. Default is white for density
plots and black for other plots.
xmin : {None, float}
Minimum value to plot on x-axis.
xmax : {None, float}
Maximum value to plot on x-axis.
ymin : {None, float}
Minimum value to plot on y-axis.
ymax : {None, float}
Maximum value to plot on y-axis.
exclue_region : {None, str}
Exclude the specified region when plotting the density or contours.
Must be a string in terms of `xparam` and `yparam` that is
understandable by numpy's logical evaluation. For example, if
`xparam = m_1` and `yparam = m_2`, and you want to exclude the region
for which `m_2` is greater than `m_1`, then exclude region should be
`'m_2 > m_1'`.
fig : {None, pyplot.figure}
Add the plot to the given figure. If None and ax is None, will create
a new figure.
ax : {None, pyplot.axes}
Draw plot on the given axis. If None, will create a new axis from
`fig`.
use_kombine : {False, bool}
Use kombine's KDE to calculate density. Otherwise, will use
`scipy.stats.gaussian_kde.` Default is False.
Returns
-------
fig : pyplot.figure
The figure the plot was made on.
ax : pyplot.axes
The axes the plot was drawn on.
"""
if percentiles is None:
percentiles = numpy.array([50., 90.])
percentiles = 100. - percentiles
percentiles.sort()
if ax is None and fig is None:
fig = pyplot.figure()
if ax is None:
ax = fig.add_subplot(111)
# convert samples to array and construct kde
xsamples = samples[xparam]
ysamples = samples[yparam]
arr = numpy.vstack((xsamples, ysamples)).T
kde = construct_kde(arr, use_kombine=use_kombine)
# construct grid to evaluate on
if xmin is None:
xmin = xsamples.min()
if xmax is None:
xmax = xsamples.max()
if ymin is None:
ymin = ysamples.min()
if ymax is None:
ymax = ysamples.max()
npts = 100
X, Y = numpy.mgrid[xmin:xmax:complex(0,npts), ymin:ymax:complex(0,npts)]
pos = numpy.vstack([X.ravel(), Y.ravel()])
if use_kombine:
Z = numpy.exp(kde(pos.T).reshape(X.shape))
draw = kde.draw
else:
Z = kde(pos).T.reshape(X.shape)
draw = kde.resample
if exclude_region is not None:
# convert X,Y to a single FieldArray so we can use it's ability to
# evaluate strings
farr = FieldArray.from_kwargs(**{xparam: X, yparam: Y})
Z[farr[exclude_region]] = 0.
#.........这里部分代码省略.........
示例12: compute_acfs
# 需要导入模块: from pycbc.io import FieldArray [as 别名]
# 或者: from pycbc.io.FieldArray import from_kwargs [as 别名]
def compute_acfs(cls, fp, start_index=None, end_index=None,
per_walker=False, walkers=None, parameters=None):
"""Computes the autocorrleation function of the variable args in the
given file.
By default, parameter values are averaged over all walkers at each
iteration. The ACF is then calculated over the averaged chain. An
ACF per-walker will be returned instead if ``per_walker=True``.
Parameters
-----------
fp : InferenceFile
An open file handler to read the samples from.
start_index : {None, int}
The start index to compute the acl from. If None, will try to use
the number of burn-in iterations in the file; otherwise, will start
at the first sample.
end_index : {None, int}
The end index to compute the acl to. If None, will go to the end
of the current iteration.
per_walker : optional, bool
Return the ACF for each walker separately. Default is False.
walkers : optional, int or array
Calculate the ACF using only the given walkers. If None (the
default) all walkers will be used.
parameters : optional, str or array
Calculate the ACF for only the given parameters. If None (the
default) will calculate the ACF for all of the variable args.
Returns
-------
FieldArray
A ``FieldArray`` of the ACF vs iteration for each parameter. If
`per-walker` is True, the FieldArray will have shape
``nwalkers x niterations``.
"""
acfs = {}
if parameters is None:
parameters = fp.variable_args
if isinstance(parameters, str) or isinstance(parameters, unicode):
parameters = [parameters]
for param in parameters:
if per_walker:
# just call myself with a single walker
if walkers is None:
walkers = numpy.arange(fp.nwalkers)
arrays = [cls.compute_acfs(fp, start_index=start_index,
end_index=end_index,
per_walker=False, walkers=ii,
parameters=param)[param]
for ii in walkers]
acfs[param] = numpy.vstack(arrays)
else:
samples = cls.read_samples(fp, param,
thin_start=start_index,
thin_interval=1, thin_end=end_index,
walkers=walkers,
flatten=False)[param]
samples = samples.mean(axis=0)
acfs[param] = autocorrelation.calculate_acf(samples).numpy()
return FieldArray.from_kwargs(**acfs)
示例13: compute_acfs
# 需要导入模块: from pycbc.io import FieldArray [as 别名]
# 或者: from pycbc.io.FieldArray import from_kwargs [as 别名]
def compute_acfs(cls, fp, start_index=None, end_index=None,
per_walker=False, walkers=None, parameters=None,
temps=None):
"""Computes the autocorrleation function of the variable args in the
given file.
By default, parameter values are averaged over all walkers at each
iteration. The ACF is then calculated over the averaged chain for each
temperature. An ACF per-walker will be returned instead if
``per_walker=True``.
Parameters
-----------
fp : InferenceFile
An open file handler to read the samples from.
start_index : {None, int}
The start index to compute the acl from. If None, will try to use
the number of burn-in iterations in the file; otherwise, will start
at the first sample.
end_index : {None, int}
The end index to compute the acl to. If None, will go to the end
of the current iteration.
per_walker : optional, bool
Return the ACF for each walker separately. Default is False.
walkers : optional, int or array
Calculate the ACF using only the given walkers. If None (the
default) all walkers will be used.
parameters : optional, str or array
Calculate the ACF for only the given parameters. If None (the
default) will calculate the ACF for all of the variable args.
temps : optional, (list of) int or 'all'
The temperature index (or list of indices) to retrieve. If None
(the default), the ACF will only be computed for the coldest (= 0)
temperature chain. To compute an ACF for all temperates pass 'all',
or a list of all of the temperatures.
Returns
-------
FieldArray
A ``FieldArray`` of the ACF vs iteration for each parameter. If
`per-walker` is True, the FieldArray will have shape
``ntemps x nwalkers x niterations``. Otherwise, the returned
array will have shape ``ntemps x niterations``.
"""
acfs = {}
if parameters is None:
parameters = fp.variable_args
if isinstance(parameters, str) or isinstance(parameters, unicode):
parameters = [parameters]
if isinstance(temps, int):
temps = [temps]
elif temps == 'all':
temps = numpy.arange(fp.ntemps)
elif temps is None:
temps = [0]
for param in parameters:
subacfs = []
for tk in temps:
if per_walker:
# just call myself with a single walker
if walkers is None:
walkers = numpy.arange(fp.nwalkers)
arrays = [cls.compute_acfs(fp, start_index=start_index,
end_index=end_index,
per_walker=False, walkers=ii,
parameters=param,
temps=tk)[param][0,:]
for ii in walkers]
# we'll stack all of the walker arrays to make a single
# nwalkers x niterations array; when these are stacked
# below, we'll get a ntemps x nwalkers x niterations array
subacfs.append(numpy.vstack(arrays))
else:
samples = cls.read_samples(fp, param,
thin_start=start_index,
thin_interval=1,
thin_end=end_index,
walkers=walkers, temps=tk,
flatten=False)[param]
# contract the walker dimension using the mean, and flatten
# the (length 1) temp dimension
samples = samples.mean(axis=1)[0,:]
thisacf = autocorrelation.calculate_acf(samples).numpy()
subacfs.append(thisacf)
# stack the temperatures
# FIXME: the following if/else can be condensed to a single line
# using numpy.stack, once the version requirements are bumped to
# numpy >= 1.10
if per_walker:
nw, ni = subacfs[0].shape
acfs[param] = numpy.zeros((len(temps), nw, ni), dtype=float)
for tk in range(len(temps)):
acfs[param][tk,...] = subacfs[tk]
else:
acfs[param] = numpy.vstack(subacfs)
return FieldArray.from_kwargs(**acfs)