Skip to content

Commit

Permalink
Merge pull request #201 from lcgraham/v2_master
Browse files Browse the repository at this point in the history
fixed type checking in postProcess and basicSampling, see issue #200
  • Loading branch information
eecsu committed May 25, 2016
2 parents a5a7a19 + 8a31ef6 commit 6d2813f
Show file tree
Hide file tree
Showing 18 changed files with 256 additions and 216 deletions.
15 changes: 8 additions & 7 deletions bet/calculateP/calculateP.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ def emulate_iid_lebesgue(domain, num_l_emulate, globalize=False):
:returns: a set of samples for emulation
"""
num_l_emulate = (num_l_emulate/comm.size) + \
(comm.rank < num_l_emulate%comm.size)
num_l_emulate = int((num_l_emulate/comm.size) + \
(comm.rank < num_l_emulate%comm.size))
lam_width = domain[:, 1] - domain[:, 0]
lambda_emulate = lam_width*np.random.random((num_l_emulate,
domain.shape[0]))+domain[:, 0]
Expand All @@ -50,10 +50,6 @@ def prob_emulated(discretization, globalize=True):
``num_l_emulate`` iid samples :math:`(\lambda_{emulate})`.
This is added to the emulated input sample set object.
.. todo::
@smattis the way this is written globalize does nothing
:param discretization: An object containing the discretization information.
:type class:`bet.sample.discretization`
:param bool globalize: Makes local variables global.
Expand Down Expand Up @@ -86,6 +82,8 @@ def prob_emulated(discretization, globalize=True):
_probabilities[i]/Itemp_sum

discretization._emulated_input_sample_set._probabilities_local = P
if globalize:
discretization._emulated_input_sample_set.local_to_global()
pass

def prob(discretization):
Expand Down Expand Up @@ -159,7 +157,10 @@ def prob_mc(discretization):
cvol = np.copy(vol)
comm.Allreduce([vol, MPI.DOUBLE], [cvol, MPI.DOUBLE], op=MPI.SUM)
vol = cvol
vol = vol/float(discretization._emulated_input_sample_set._values.shape[0])
num_l_emulate = discretization._emulated_input_sample_set.\
_values_local.shape[0]
num_l_emulate = comm.allreduce(num_l_emulate, op=MPI.SUM)
vol = vol/float(num_l_emulate)
discretization._input_sample_set._volumes = vol
discretization._input_sample_set.global_to_local()

Expand Down
26 changes: 13 additions & 13 deletions bet/postProcess/plotDomains.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def scatter_2D(sample_obj, sample_nos=None, color=None, p_ref=None, save=True,
the other markers.
:param sample_obj: contains samples to create scatter plot
:type sample_obj: :class:`~bet.sample.sample_set`
:type sample_obj: :class:`~bet.sample.sample_set_base`
:param list sample_nos: indicies of the samples to plot
:param color: values to color the samples by
:type color: :class:`numpy.ndarray`
Expand All @@ -65,7 +65,7 @@ def scatter_2D(sample_obj, sample_nos=None, color=None, p_ref=None, save=True,
:param string filename: filename to save the figure as
"""
if not isinstance(sample_obj, sample.sample_set):
if not isinstance(sample_obj, sample.sample_set_base):
raise bad_object("Improper sample object")
# check dimension of data to plot
if sample_obj.get_dim() != 2:
Expand Down Expand Up @@ -119,7 +119,7 @@ def scatter_3D(sample_obj, sample_nos=None, color=None, p_ref=None, save=True,
the other markers.
:param sample_obj: Object containing the samples to plot
:type sample_obj: :class:`~bet.sample.sample_set`
:type sample_obj: :class:`~bet.sample.sample_set_base`
:param list sample_nos: indicies of the samples to plot
:param color: values to color the samples by
:type color: :class:`numpy.ndarray`
Expand All @@ -133,7 +133,7 @@ def scatter_3D(sample_obj, sample_nos=None, color=None, p_ref=None, save=True,
:param string filename: filename to save the figure as
"""
if not isinstance(sample_obj, sample.sample_set):
if not isinstance(sample_obj, sample.sample_set_base):
raise bad_object("Improper sample object")
# check dimension of data to plot
if sample_obj.get_dim() != 3:
Expand Down Expand Up @@ -191,7 +191,7 @@ def show_param(sample_disc, rho_D=None, p_ref=None, sample_nos=None,
:param sample_disc: Object containing the samples to plot
:type sample_disc: :class:`~bet.sample.discretization`
or :class:`~bet.sample.sample_set`
or :class:`~bet.sample.sample_set_base`
:param list sample_nos: sample numbers to plot
:param rho_D: probability density function on D
:type rho_D: callable function that takes a :class:`np.array` and returns a
Expand All @@ -217,7 +217,7 @@ def show_param(sample_disc, rho_D=None, p_ref=None, sample_nos=None,
else:
if isinstance(sample_disc, sample.discretization):
sample_obj = sample_disc._input_sample_set
elif isinstance(sample_disc, sample.sample_set):
elif isinstance(sample_disc, sample.sample_set_base):
sample_obj = sample_disc
else:
raise bad_object("Improper sample object")
Expand Down Expand Up @@ -271,7 +271,7 @@ def show_data(sample_obj, rho_D=None, Q_ref=None, sample_nos=None,
the other markers.
:param sample_obj: Object containing the samples to plot
:type sample_obj: :class:`~bet.sample.sample_set`
:type sample_obj: :class:`~bet.sample.sample_set_base`
:param list sample_nos: sample numbers to plot
:param rho_D: probability density on D
:type rho_D: callable function that takes a :class:`np.array` and returns a
Expand Down Expand Up @@ -363,7 +363,7 @@ def show_data_domain_multi(sample_disc, Q_ref=None, Q_nums=None,
:param sample_disc: Object containing the samples to plot
:type sample_disc: :class:`~bet.sample.discretization` or
:class:`~bet.sample.sample_set`
:class:`~bet.sample.sample_set_base`
:param Q_ref: reference data value
:type Q_ref: :class:`numpy.ndarray` of shape (M, mdim)
:param list Q_nums: dimensions of the QoI to plot
Expand Down Expand Up @@ -472,7 +472,7 @@ def show_data_domain_2D(sample_disc, Q_ref=None, ref_markers=None,
:param sample_disc: Object containing the samples to plot
:type sample_disc: :class:`~bet.sample.discretization`
or :class:`~bet.sample.sample_set`
or :class:`~bet.sample.sample_set_base`
:param Q_ref: reference data value
:type Q_ref: :class:`numpy.ndarray` of shape (M, 2)
:param list ref_markers: list of marker types for :math:`Q_{ref}`
Expand Down Expand Up @@ -542,7 +542,7 @@ def scatter_param_multi(sample_obj, img_folder='figs/', showdim='all',
Creates two-dimensional projections of scatter plots of samples.
:param sample_obj: Object containing the samples to plot
:type sample_obj: :class:`~bet.sample.sample_set`
:type sample_obj: :class:`~bet.sample.sample_set_base`
:param bool save: flag whether or not to save the figure
:param bool interactive: flag whether or not to show the figure
:param string img_folder: folder to save the plots to
Expand All @@ -551,7 +551,7 @@ def scatter_param_multi(sample_obj, img_folder='figs/', showdim='all',
:type showdim: int or string
"""
if not isinstance(sample_obj, sample.sample_set):
if not isinstance(sample_obj, sample.sample_set_base):
raise bad_object("Improper sample object")

# If no specific coordinate number of choice is given set to be the first
Expand Down Expand Up @@ -622,7 +622,7 @@ def scatter2D_multi(sample_obj, color=None, p_ref=None, img_folder='figs/',
markers.
:param sample_obj: Object containing the samples to plot
:type sample_obj: :class:`~bet.sample.sample_set`
:type sample_obj: :class:`~bet.sample.sample_set_base`
:param color: values to color the ``samples`` by
:type color: :class:`numpy.ndarray`
:param string filename: filename to save the figure as
Expand All @@ -635,7 +635,7 @@ def scatter2D_multi(sample_obj, color=None, p_ref=None, img_folder='figs/',
:type showdim: int or string
"""
if not isinstance(sample_obj, sample.sample_set):
if not isinstance(sample_obj, sample.sample_set_base):
raise bad_object("Improper sample object")
# If no specific coordinate number of choice is given set to be the first
# coordinate direction.
Expand Down
66 changes: 50 additions & 16 deletions bet/postProcess/plotP.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,26 @@ class bad_object(Exception):
Exception for when the wrong type of object is used.
"""

class missing_attribute(Exception):
"""
Exception for missing attribute.
"""

def calculate_1D_marginal_probs(sample_set, nbins=20):

r"""
This calculates every single marginal of the probability measure
described by the probabilities within the sample_set object.
If the sample_set object is a discretization object, we assume
that the probabilities to be plotted are from the input space.
that the probabilities to be plotted are from the input space on the
emulated samples
(``discretization._emulated_input_sample_set._probabilties_local``).
This assumes that the user has already run
:meth:`~bet.calculateP.calculateP.prob_emulated`.
:param sample_set: Object containing samples and probabilities
:type sample_set: :class:`~bet.sample.sample_set` or
:type sample_set: :class:`~bet.sample.sample_set_base` or
:class:`~bet.sample.discretization`
:param nbins: Number of bins in each direction.
:type nbins: :int or :class:`~numpy.ndarray` of shape (ndim,)
Expand All @@ -41,12 +51,21 @@ def calculate_1D_marginal_probs(sample_set, nbins=20):
"""
if isinstance(sample_set, sample.discretization):
sample_obj = sample_set._input_sample_set
elif isinstance(sample_set, sample.sample_set):
sample_obj = sample_set._emulated_input_sample_set
if sample_obj is None:
raise missing_attribute("Missing emulated_input_sample_set")
elif isinstance(sample_set, sample.sample_set_base):
sample_obj = sample_set
else:
raise bad_object("Improper sample object")

# Check for local probabilities
if sample_obj._probabilities_local is None:
if sample_obj.probabilities is None:
raise missing_attribute("Missing probabilities")
else:
sample_obj.global_to_local()

# Make list of bins if only an integer is given
if isinstance(nbins, int):
nbins = nbins*np.ones(sample_obj.get_dim(), dtype=np.int)
Expand All @@ -61,8 +80,8 @@ def calculate_1D_marginal_probs(sample_set, nbins=20):
# Calculate marginals
marginals = {}
for i in range(sample_obj.get_dim()):
[marg, _] = np.histogram(sample_obj.get_values()[:, i], bins=bins[i],
weights=sample_obj.get_probabilities())
[marg, _] = np.histogram(sample_obj.get_values_local()[:, i], bins=bins[i],
weights=sample_obj.get_probabilities_local())
marg_temp = np.copy(marg)
comm.Allreduce([marg, MPI.DOUBLE], [marg_temp, MPI.DOUBLE], op=MPI.SUM)
marginals[i] = marg_temp
Expand All @@ -75,10 +94,16 @@ def calculate_2D_marginal_probs(sample_set, nbins=20):
This calculates every pair of marginals (or joint in 2d case) of
input probability measure defined on a rectangular grid.
If the sample_set object is a discretization object, we assume
that the probabilities to be plotted are from the input space.
that the probabilities to be plotted are from the input space on the
emulated samples
(``discretization._emulated_input_sample_set._probabilties_local``).
This assumes that the user has already run
:meth:`~bet.calculateP.calculateP.prob_emulated`.
:param sample_set: Object containing samples and probabilities
:type sample_set: :class:`~bet.sample.sample_set`
:type sample_set: :class:`~bet.sample.sample_set_base`
or :class:`~bet.sample.discretization`
:param nbins: Number of bins in each direction.
:type nbins: :int or :class:`~numpy.ndarray` of shape (ndim,)
Expand All @@ -87,12 +112,21 @@ def calculate_2D_marginal_probs(sample_set, nbins=20):
"""
if isinstance(sample_set, sample.discretization):
sample_obj = sample_set._input_sample_set
elif isinstance(sample_set, sample.sample_set):
sample_obj = sample_set._emulated_input_sample_set
if sample_obj is None:
raise missing_attribute("Missing emulated_input_sample_set")
elif isinstance(sample_set, sample.sample_set_base):
sample_obj = sample_set
else:
raise bad_object("Improper sample object")

# Check for local probabilities
if sample_obj._probabilities_local is None:
if sample_obj.probabilities is None:
raise missing_attribute("Missing probabilities")
else:
sample_obj.global_to_local()

if sample_obj.get_dim() < 2:
raise dim_not_matching("Incompatible dimensions of sample set"
" for plotting")
Expand All @@ -112,9 +146,9 @@ def calculate_2D_marginal_probs(sample_set, nbins=20):
marginals = {}
for i in range(sample_obj.get_dim()):
for j in range(i+1, sample_obj.get_dim()):
(marg, _) = np.histogramdd(sample_obj.get_values()[:, [i, j]],
(marg, _) = np.histogramdd(sample_obj.get_values_local()[:, [i, j]],
bins=[bins[i], bins[j]],
weights=sample_obj.get_probabilities())
weights=sample_obj.get_probabilities_local())
marg = np.ascontiguousarray(marg)
marg_temp = np.copy(marg)
comm.Allreduce([marg, MPI.DOUBLE], [marg_temp, MPI.DOUBLE],
Expand All @@ -139,7 +173,7 @@ def plot_1D_marginal_probs(marginals, bins, sample_set,
calculating marginals
:type bins: :class:`~numpy.ndarray` of shape (nbins+1,)
:param sample_set: Object containing samples and probabilities
:type sample_set: :class:`~bet.sample.sample_set`
:type sample_set: :class:`~bet.sample.sample_set_base`
or :class:`~bet.sample.discretization`
:param filename: Prefix for output files.
:type filename: str
Expand All @@ -153,7 +187,7 @@ def plot_1D_marginal_probs(marginals, bins, sample_set,
"""
if isinstance(sample_set, sample.discretization):
sample_obj = sample_set._input_sample_set
elif isinstance(sample_set, sample.sample_set):
elif isinstance(sample_set, sample.sample_set_base):
sample_obj = sample_set
else:
raise bad_object("Improper sample object")
Expand Down Expand Up @@ -202,7 +236,7 @@ def plot_2D_marginal_probs(marginals, bins, sample_set,
:param bins: Endpoints of bins used in calculating marginals
:type bins: :class:`~numpy.ndarray` of shape (nbins+1,2)
:param sample_set: Object containing samples and probabilities
:type sample_set: :class:`~bet.sample.sample_set`
:type sample_set: :class:`~bet.sample.sample_set_base`
or :class:`~bet.sample.discretization`
:param filename: Prefix for output files.
:type filename: str
Expand All @@ -216,7 +250,7 @@ def plot_2D_marginal_probs(marginals, bins, sample_set,
"""
if isinstance(sample_set, sample.discretization):
sample_obj = sample_set._input_sample_set
elif isinstance(sample_set, sample.sample_set):
elif isinstance(sample_set, sample.sample_set_base):
sample_obj = sample_set
else:
raise bad_object("Improper sample object")
Expand Down
14 changes: 7 additions & 7 deletions bet/postProcess/postTools.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def sort_by_rho(sample_set):
are also sorted.
:param sample_set: Object containing samples and probabilities
:type sample_set: :class:`~bet.sample.sample_set` or
:type sample_set: :class:`~bet.sample.sample_set_base` or
:class:`~bet.sample.discretization`
:param indices: sorting indices
:type indices: :class:`numpy.ndarray` of shape (num_samples,)
Expand All @@ -45,7 +45,7 @@ def sort_by_rho(sample_set):
P_samples = sample_set._input_sample_set.get_probabilities()
lam_vol = sample_set._input_sample_set.get_volumes()
data = sample_set._output_sample_set.get_values()
elif isinstance(sample_set, sample.sample_set):
elif isinstance(sample_set, sample.sample_set_base):
samples = sample_set.get_values()
P_samples = sample_set.get_probabilities()
lam_vol = sample_set.get_volumes()
Expand Down Expand Up @@ -95,7 +95,7 @@ def sample_prob(percentile, sample_set, sort=True, descending=False):
:param percentile: ratio of highest probability samples to select
:type percentile: float
:param sample_set: Object containing samples and probabilities
:type sample_set: :class:`~bet.sample.sample_set` or
:type sample_set: :class:`~bet.sample.sample_set_base` or
:class:`~bet.sample.discretization`
:type indices: :class:`numpy.ndarray` of shape (num_samples,)
:param indices: sorting indices
Expand All @@ -114,7 +114,7 @@ def sample_prob(percentile, sample_set, sort=True, descending=False):
P_samples = sample_set._input_sample_set.get_probabilities()
lam_vol = sample_set._input_sample_set.get_volumes()
data = sample_set._output_sample_set.get_values()
elif isinstance(sample_set, sample.sample_set):
elif isinstance(sample_set, sample.sample_set_base):
samples = sample_set.get_values()
P_samples = sample_set.get_probabilities()
lam_vol = sample_set.get_volumes()
Expand All @@ -129,7 +129,7 @@ def sample_prob(percentile, sample_set, sort=True, descending=False):
P_samples = sample_set._input_sample_set.get_probabilities()
lam_vol = sample_set._input_sample_set.get_volumes()
data = sample_set._output_sample_set.get_values()
elif isinstance(sample_set, sample.sample_set):
elif isinstance(sample_set, sample.sample_set_base):
samples = sample_set.get_values()
P_samples = sample_set.get_probabilities()
lam_vol = sample_set.get_volumes()
Expand Down Expand Up @@ -182,7 +182,7 @@ def sample_highest_prob(top_percentile, sample_set, sort=True):
:param top_percentile: ratio of highest probability samples to select
:type top_percentile: float
:param sample_set: Object containing samples and probabilities
:type sample_set: :class:`~bet.sample.sample_set`
:type sample_set: :class:`~bet.sample.sample_set_base`
or :class:`~bet.sample.discretization`
:type indices: :class:`numpy.ndarray` of shape (num_samples,)
:param indices: sorting indices
Expand All @@ -208,7 +208,7 @@ def sample_lowest_prob(bottom_percentile, sample_set, sort=True):
:param top_percentile: ratio of highest probability samples to select
:type top_percentile: float
:param sample_set: Object containing samples and probabilities
:type sample_set: :class:`~bet.sample.sample_set`
:type sample_set: :class:`~bet.sample.sample_set_base`
or :class:`~bet.sample.discretization`
:type indices: :class:`numpy.ndarray` of shape (num_samples,)
:param indices: sorting indices of unsorted ``P_samples``
Expand Down
7 changes: 7 additions & 0 deletions bet/sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,13 @@ def estimate_volume(self, n_mc_points=int(1E4)):
Calculate the volume faction of cells approximately using Monte
Carlo integration.
.. todo::
This currently presumes a uniform Lesbegue measure on the
``domain``. Currently the way this is written
``emulated_input_sample_set`` is NOT used to calculate the volume.
This should at least be an option.
:param int n_mc_points: If estimate is True, number of MC points to use
"""
num = self.check_num()
Expand Down
Loading

0 comments on commit 6d2813f

Please sign in to comment.