Example #1
0
 def setUp(self):
     """
     Set up the problem by calculating required ratios and widths.
     """
     self.boundary_ratio_radius = 0.1
     self.boundary_width_radius = self.radius*self.boundary_ratio_radius
     self.boundary_width = np.ones(self.center.shape) + \
         0.1*np.arange(len(self.center))
     self.right = self.center + .5*self.width
     self.left = self.center - .5*self.width
     self.boundary_ratio = self.boundary_width/self.width
     # create a list of coordinates that are outside the domain
     outcoords_rect = []
     outcoords_sphere = []
     # create a list of coordinates that are in on the boundary of the
     # domain
     oncoords_rect = []
     dim = len(self.width)
     for l, r, bw in zip(self.left, self.right, self.boundary_width):
         outcoords_rect.append(np.array([l-bw, r+bw]))
         outcoords_sphere.append(np.array([self.center-self.radius
                                           - self.boundary_width_radius,
                                           self.center+self.radius+self.boundary_width_radius]))
         oncoords_rect.append(np.array([l, r]))
     self.outcoords_rect = util.meshgrid_ndim(outcoords_rect)
     self.oncoords_rect = util.meshgrid_ndim(oncoords_rect)
     self.outcoords_sphere = util.meshgrid_ndim(outcoords_sphere)
     self.oncoords_sphere = np.row_stack((-np.eye(dim),
                                          np.eye(dim).transpose()))*self.radius+self.center
     print("SPHERE", self.center, self.radius, self.oncoords_sphere)
 def setUp(self):
     """
     Set up the problem by calculating required ratios and widths.
     """
     self.boundary_ratio_radius = 0.1
     self.boundary_width_radius = self.radius*self.boundary_ratio_radius
     self.boundary_width = np.ones(self.center.shape) + \
             0.1*np.arange(len(self.center))
     self.right = self.center + .5*self.width
     self.left = self.center - .5*self.width
     self.boundary_ratio = self.boundary_width/self.width
     # create a list of coordinates that are outside the domain
     outcoords_rect = []
     outcoords_sphere = []
     # create a list of coordinates that are in on the boundary of the
     # domain
     oncoords_rect = []
     dim = len(self.width)
     for l, r, bw in zip(self.left, self.right, self.boundary_width): 
         outcoords_rect.append(np.array([l-bw, r+bw]))
         outcoords_sphere.append(np.array([self.center-self.radius\
                 -self.boundary_width_radius,
             self.center+self.radius+self.boundary_width_radius]))
         oncoords_rect.append(np.array([l, r]))
     self.outcoords_rect = util.meshgrid_ndim(outcoords_rect)
     self.oncoords_rect = util.meshgrid_ndim(oncoords_rect)
     self.outcoords_sphere = util.meshgrid_ndim(outcoords_sphere)
     self.oncoords_sphere = np.row_stack((-np.eye(dim),
         np.eye(dim).transpose()))*self.radius+self.center 
     print "SPHERE", self.center, self.radius, self.oncoords_sphere
Example #3
0
    def setUp(self):
        """
        Set up problem.
        """
        emulated_input_samples = sample.sample_set(2)
        emulated_input_samples.set_domain(np.array([[0.0, 1.0], [0.0, 1.0]]))

        emulated_input_samples.set_values_local(
            util.meshgrid_ndim(
                (np.linspace(emulated_input_samples.get_domain()[0][0],
                             emulated_input_samples.get_domain()[0][1], 10),
                 np.linspace(emulated_input_samples.get_domain()[1][0],
                             emulated_input_samples.get_domain()[1][1], 10))))

        emulated_input_samples.set_probabilities_local(
            1.0 / float(comm.size) *
            (1.0 / float(emulated_input_samples.get_values_local().shape[0])) *
            np.ones((emulated_input_samples.get_values_local().shape[0], )))
        emulated_input_samples.set_volumes_local(
            1.0 / float(comm.size) *
            (1.0 / float(emulated_input_samples.get_values_local().shape[0])) *
            np.ones((emulated_input_samples.get_values_local().shape[0], )))
        emulated_input_samples.check_num()

        self.samples = emulated_input_samples
Example #4
0
    def setUp(self):
        """
        Test dimension, number of samples, and that all the samples are within
        lambda_domain.

        """
        lam_left = np.array([0.0, .25, .4])
        lam_right = np.array([1.0, 4.0, .5])
        lam_width = lam_right-lam_left

        self.lam_domain = np.zeros((3, 3))
        self.lam_domain[:, 0] = lam_left
        self.lam_domain[:, 1] = lam_right

        num_samples_dim = 2
        start = lam_left+lam_width/(2*num_samples_dim)
        stop = lam_right-lam_width/(2*num_samples_dim)
        d1_arrays = []
        
        for l, r in zip(start, stop):
            d1_arrays.append(np.linspace(l, r, num_samples_dim))

        self.num_l_emulate = 1000001

        self.lambda_emulate = calcP.emulate_iid_lebesgue(self.lam_domain,
                self.num_l_emulate)
        self.samples = util.meshgrid_ndim(d1_arrays)
        self.volume_exact = 1.0/self.samples.shape[0]
        self.lam_vol, self.lam_vol_local, self.local_index = calcP.\
                estimate_volume(self.samples, self.lambda_emulate)
Example #5
0
def center_and_layer1_points(center_pts_per_edge, center, r_ratio, sur_domain):
    r"""
    Generates a regular grid of center points that define the voronoi
    tesselation of exactly the interior of a hyperrectangle centered at
    ``center`` with sides of length ``r_ratio*sur_width`` and the layers
    of voronoi cells that bound these interior cells. The resulting voronoi
    tesselation exactly represents the hyperrectangle.

    This method can also be used to tile ``sur_domain`` with points to define
    voronoi regions if the user sets ``r_ratio = 1``.

    :param list() center_pts_per_edge: number of center points per edge and
        additional two points will be added to create the bounding layer
    :param center: location of the center of the hyperrectangle
    :type center: :class:`numpy.ndarray` of shape (mdim,)
    :param r_ratio: ratio of the length of the sides of the
        hyperrectangle to the surrounding domain
    :type r_ratio: double or list()
    :param sur_domain: minima and maxima of each dimension defining the
        surrounding domain
    :type sur_domain: :class:`numpy.ndarray` of shape (mdim, 2)

    :rtype: tuple
    :returns: (points, interior_and_layer1) where where points is an
        :class:`numpy.ndarray` of shape (num_points, dim), interior_and_layer1
        is a list() of dim :class:`numpy.ndarray`s of shape
        (center_pts_per_edge+2,).

    """
    if np.all(np.greater(r_ratio, 1)):
        msg = "The hyperrectangle defined by this ratio is larger than the"
        msg += "original domain."
        print msg

    # determine the width of the surrounding domain
    sur_width = sur_domain[:, 1]-sur_domain[:, 0]
    # determine the hyperrectangle defined by center and r_ratio
    rect_width = r_ratio*sur_width
    rect_domain = np.empty(sur_domain.shape)
    rect_domain[:, 0] = center - .5*rect_width
    rect_domain[:, 1] = center + .5*rect_width

    # determine the locations of the points for the 1st bounding layer
    layer1_left = rect_domain[:, 0]-rect_width/(2*center_pts_per_edge)
    layer1_right = rect_domain[:, 1]+rect_width/(2*center_pts_per_edge)

    interior_and_layer1 = list()
    for dim in xrange(sur_domain.shape[0]):
        # create interior points and 1st layer
        int_l1 = np.linspace(layer1_left[dim],
            layer1_right[dim], center_pts_per_edge[dim]+2)
        interior_and_layer1.append(int_l1)

    # use meshgrid to make the hyperrectangle shells
    if sur_domain.shape[0] == 1:
        points = interior_and_layer1[0]
    else:
        points = util.meshgrid_ndim(interior_and_layer1)
    return (points, interior_and_layer1, rect_domain)
Example #6
0
def test_meshgrid_ndim():
    """
    Tests :meth:`bet.util.meshgrid_ndim` for upto 10 vectors where each vector is
    equal to ``[0, 1]``.
    """
    for i in xrange(10):
        x = [[0, 1] for v in xrange(i + 1)]
        yield compare_to_bin_rep, util.meshgrid_ndim(x)
Example #7
0
def test_meshgrid_ndim():
    """
    Tests :meth:`bet.util.meshgrid_ndim` for upto 10 vectors where each vector is
    equal to ``[0, 1]``.
    """
    for i in xrange(10):
        x = [[0, 1] for v in xrange(i + 1)]
        yield compare_to_bin_rep, util.meshgrid_ndim(x)
Example #8
0
def center_and_layer1_points_binsize(center_pts_per_edge, center, r_size,
                                     sur_domain):
    """
    Generates a regular grid of center points that define the voronoi
    tesselation of exactly the interior of a hyperrectangle centered at
    ``center`` with sides of length ``r_size`` and the layers
    of voronoi cells that bound these interior cells. The resulting voronoi
    tesselation exactly represents the hyperrectangle.

    This method can also be used to tile ``sur_domain`` with points to define
    voronoi regions if the user sets ``r_ratio = 1``. (use binratio)

    :param list() center_pts_per_edge: number of center points per edge and
        additional two points will be added to create the bounding layer
    :param center: location of the center of the hyperrectangle
    :type center: :class:`numpy.ndarray` of shape (mdim,)
    :param r_size: size of the length of the sides of the
        hyperrectangle rect_domain to definie voronoi cells for
    :type r_size: double or list()
    :param sur_domain: minima and maxima of each dimension defining the
        surrounding domain. The surrounding domain is the bounded domain
        in the data space (i.e. the data domain).
    :type sur_domain: :class:`numpy.ndarray` of shape (mdim, 2)

    :rtype: tuple
    :returns: (points, interior_and_layer1, rect_domain) where where points is
        an :class:`numpy.ndarray` of shape (num_points, dim),
        interior_and_layer1 is a list() of dim :class:`numpy.ndarray`s of shape
        (center_pts_per_edge+2,), rect_domain is a :class:`numpy.ndarray` of
        shape (mdim, 2)

    """
    # determine the hyperrectangle (rect_domain) defined by center and r_size
    rect_width = r_size * np.ones(sur_domain[:, 0].shape)
    rect_domain = np.column_stack(
        [center - .5 * rect_width, center + .5 * rect_width])
    if np.all(np.greater(r_size, rect_width)):
        msg = "The hyperrectangle defined by this size is larger than the "
        msg += "original domain."
        print msg

    # determine the locations of the points for the 1st bounding layer
    layer1_left = rect_domain[:, 0] - rect_width / (2 * center_pts_per_edge)
    layer1_right = rect_domain[:, 1] + rect_width / (2 * center_pts_per_edge)

    interior_and_layer1 = list()
    for dim in xrange(sur_domain.shape[0]):
        # create interior points and 1st layer
        int_l1 = np.linspace(layer1_left[dim], layer1_right[dim],
                             center_pts_per_edge[dim] + 2)
        interior_and_layer1.append(int_l1)

    # use meshgrid to make the hyperrectangle shells
    points = util.meshgrid_ndim(interior_and_layer1)
    return (points, interior_and_layer1, rect_domain)
Example #9
0
 def setUp(self):
     """
     Set up problem.
     """
     self.lam_domain = np.array([[0.0, 1.0], [0.0, 1.0]])
     self.samples = util.meshgrid_ndim(
         (np.linspace(self.lam_domain[0][0], self.lam_domain[0][1], 10),
          np.linspace(self.lam_domain[1][0], self.lam_domain[1][1], 10)))
     self.P_samples = 1.0 / float(
         comm.size) * (1.0 / float(self.samples.shape[0])) * np.ones(
             (self.samples.shape[0], ))
Example #10
0
 def setUp(self):
     """
     Set up problem.
     """
     self.lam_domain = np.array([[0.0, 1.0], [0.0, 1.0]])
     self.samples = util.meshgrid_ndim(
         (
             np.linspace(self.lam_domain[0][0], self.lam_domain[0][1], 10),
             np.linspace(self.lam_domain[1][0], self.lam_domain[1][1], 10),
         )
     )
     self.P_samples = (
         1.0 / float(comm.size) * (1.0 / float(self.samples.shape[0])) * np.ones((self.samples.shape[0],))
     )
Example #11
0
 def setUp(self):
     points = list()
     self.edges = list()
     for dim in xrange(self.mdim):
         points_dim = np.linspace(self.sur_domain[dim, 0],
             self.sur_domain[dim, 1], 4)
         points.append(points_dim[1:-1])
         self.edges.append((points_dim[1:]+points_dim[:-1])/2.0)
     self.points = util.meshgrid_ndim(points)
     self.H, _ = np.histogramdd(self.points, self.edges, normed=True)
     volume = 1.0/(self.H*(2.0**self.mdim))
     self.volume = volume.ravel()
     output = vHist.histogramdd_volumes(self.edges, self.points)
     self.o_H, self.o_volume, self.o_edges = output
 def setUp(self):
     points = list()
     self.edges = list()
     for dim in xrange(self.mdim):
         points_dim = np.linspace(self.sur_domain[dim, 0],
                                  self.sur_domain[dim, 1], 4)
         points.append(points_dim[1:-1])
         self.edges.append((points_dim[1:] + points_dim[:-1]) / 2.0)
     self.points = util.meshgrid_ndim(points)
     self.H, _ = np.histogramdd(self.points, self.edges, normed=True)
     volume = 1.0 / (self.H * (2.0**self.mdim))
     self.volume = volume.ravel()
     output = vHist.histogramdd_volumes(self.edges, self.points)
     self.o_H, self.o_volume, self.o_edges = output
Example #13
0
    def setUp(self):
        """
        Set up problem.
        """
        self.lam_domain = np.array([[0.0, 1.0], [0.0, 1.0], [0.0, 1.0],
                                    [0.0, 1.0]])
        self.samples = util.meshgrid_ndim(
            (np.linspace(self.lam_domain[0][0], self.lam_domain[0][1], 10),
             np.linspace(self.lam_domain[1][0], self.lam_domain[1][1], 10),
             np.linspace(self.lam_domain[1][0], self.lam_domain[1][1], 10),
             np.linspace(self.lam_domain[1][0], self.lam_domain[1][1], 10)))
        self.data = self.samples * 3.0
        self.P_samples = (1.0 / float(self.samples.shape[0])) * np.ones(
            (self.samples.shape[0], ))
        self.filename = "testfigure"

        QoI_range = np.array([3.0, 3.0, 3.0, 3.0])
        Q_ref = QoI_range * 0.5
        bin_size = 0.15 * QoI_range
        maximum = 1 / np.product(bin_size)

        def ifun(outputs):
            """
            Indicator function.
            :param outputs: outputs
            :type outputs: :class:`numpy.ndarray` of shape (N, ndim)
            :rtype: :class:`numpy.ndarray` of shape (N,)
            :returns: 0 if outside of set or positive number if inside set
            """
            left = np.repeat([Q_ref - .5 * bin_size], outputs.shape[0], 0)
            right = np.repeat([Q_ref + .5 * bin_size], outputs.shape[0], 0)
            left = np.all(np.greater_equal(outputs, left), axis=1)
            right = np.all(np.less_equal(outputs, right), axis=1)
            inside = np.logical_and(left, right)
            max_values = np.repeat(maximum, outputs.shape[0], 0)
            return inside.astype('float64') * max_values

        self.rho_D = ifun
        self.lnums = [1, 2, 3]
        self.markers = []
        for m in Line2D.markers:
            try:
                if len(m) == 1 and m != ' ':
                    self.markers.append(m)
            except TypeError:
                pass
        self.colors = ('b', 'g', 'r', 'c', 'm', 'y', 'k')
Example #14
0
    def setUp(self):
        """
        Set up problem.
        """
        emulated_input_samples = sample.sample_set(2)
        emulated_input_samples.set_domain(np.array([[0.0,1.0],[0.0,1.0]]))

        emulated_input_samples.set_values_local(util.meshgrid_ndim((np.linspace(emulated_input_samples.get_domain()[0][0],
            emulated_input_samples.get_domain()[0][1], 10),
            np.linspace(emulated_input_samples.get_domain()[1][0],
                emulated_input_samples.get_domain()[1][1], 10))))

        emulated_input_samples.set_probabilities_local(1.0/float(comm.size)*\
                (1.0/float(emulated_input_samples.get_values_local().shape[0]))*\
                np.ones((emulated_input_samples.get_values_local().shape[0],)))
        emulated_input_samples.check_num()

        self.samples = emulated_input_samples
Example #15
0
    def setUp(self):
        """
        Set up problem.
        """
        self.lam_domain = np.array([[0.0, 1.0], [0.0, 1.0], [0.0, 1.0], [0.0, 1.0]])
        self.samples = util.meshgrid_ndim((np.linspace(self.lam_domain[0][0],
            self.lam_domain[0][1], 10), np.linspace(self.lam_domain[1][0],
            self.lam_domain[1][1], 10), np.linspace(self.lam_domain[1][0],
            self.lam_domain[1][1], 10), np.linspace(self.lam_domain[1][0],
            self.lam_domain[1][1], 10)))
        self.data = self.samples*3.0
        self.P_samples = (1.0/float(self.samples.shape[0]))*np.ones((self.samples.shape[0],))
        self.filename = "testfigure"

        QoI_range = np.array([3.0, 3.0, 3.0, 3.0])
        Q_ref = QoI_range*0.5
        bin_size = 0.15*QoI_range
        maximum = 1/np.product(bin_size)
        def ifun(outputs):
            """
            Indicator function.
            :param outputs: outputs
            :type outputs: :class:`numpy.ndarray` of shape (N, ndim)
            :rtype: :class:`numpy.ndarray` of shape (N,)
            :returns: 0 if outside of set or positive number if inside set
            """
            left = np.repeat([Q_ref-.5*bin_size], outputs.shape[0], 0)
            right = np.repeat([Q_ref+.5*bin_size], outputs.shape[0], 0)
            left = np.all(np.greater_equal(outputs, left), axis=1)
            right = np.all(np.less_equal(outputs, right), axis=1)
            inside = np.logical_and(left, right)
            max_values = np.repeat(maximum, outputs.shape[0], 0)
            return inside.astype('float64')*max_values
        self.rho_D = ifun
        self.lnums = [1, 2, 3]
        self.markers = []
        for m in Line2D.markers:
            try:
                if len(m) == 1 and m != ' ':
                    self.markers.append(m)
            except TypeError:
                pass
        self.colors = ('b', 'g', 'r', 'c', 'm', 'y', 'k')
Example #16
0
 def setUp(self):
     """
     Set up the problem
     """
     points = list()
     edges = list()
     self.rect_domain = np.empty((self.mdim, 2))
     for dim in xrange(self.mdim):
         points_dim = np.linspace(self.sur_domain[dim, 0],
             self.sur_domain[dim, 1], 4)
         points.append(points_dim[1:-1])
         edge = (points_dim[1:]+points_dim[:-1])/2.0
         edges.append(edge)
         self.rect_domain[dim, :] = edge[[0, -1]]
     points = util.meshgrid_ndim(points)
     H, _ = np.histogramdd(points, edges, normed=True)
     volume = 1.0/(H*(2.0**self.mdim))
     volumes = volume.ravel()
     output = vHist.simple_fun_uniform(points, volumes, self.rect_domain)
     self.rho_D_M, self.d_distr_samples, self.d_Tree = output
 def setUp(self):
     """
     Set up the problem
     """
     points = list()
     edges = list()
     self.rect_domain = np.empty((self.mdim, 2))
     for dim in xrange(self.mdim):
         points_dim = np.linspace(self.sur_domain[dim, 0],
                                  self.sur_domain[dim, 1], 4)
         points.append(points_dim[1:-1])
         edge = (points_dim[1:] + points_dim[:-1]) / 2.0
         edges.append(edge)
         self.rect_domain[dim, :] = edge[[0, -1]]
     points = util.meshgrid_ndim(points)
     H, _ = np.histogramdd(points, edges, normed=True)
     volume = 1.0 / (H * (2.0**self.mdim))
     volumes = volume.ravel()
     output = vHist.simple_fun_uniform(points, volumes, self.rect_domain)
     self.rho_D_M, self.d_distr_samples, self.d_Tree = output
 def test_points(self):
     """
     Test that the points are correct.
     """
     nptest.assert_array_almost_equal(
         self.points, util.meshgrid_ndim(self.interior_and_layer1))
Example #19
0
 def test_points(self):
     """
     Test that the points are correct.
     """
     nptest.assert_array_almost_equal(self.points,
         util.meshgrid_ndim(self.interior_and_layer1))
Example #20
0
    def setUp(self):
        """
        Set up problem.
        """
        # Create sample_set object for input_samples
        input_samples = sample.sample_set(4)

        input_samples.set_domain(np.array([[0.0, 1.0], [0.0, 1.0],
                                           [0.0, 1.0], [0.0, 1.0]]))
        input_samples.set_values(util.meshgrid_ndim(
            (np.linspace(input_samples.get_domain()[0, 0],
                         input_samples.get_domain()[0, 1], 3),
             np.linspace(input_samples.get_domain()[1, 0],
                         input_samples.get_domain()[1, 1], 3),
             np.linspace(input_samples.get_domain()[2, 0],
                         input_samples.get_domain()[2, 1], 3),
             np.linspace(input_samples.get_domain()[3, 0],
                         input_samples.get_domain()[3, 1], 3))))
        input_samples.set_probabilities(
            (1.0/float(input_samples.get_values().shape[0]))
            * np.ones((input_samples.get_values().shape[0],)))

        # Check that probabilities and values arrays have same number of entries
        input_samples.check_num()

        # Create sample_set object for output_samples
        output_samples = sample.sample_set(4)
        output_samples.set_values(input_samples.get_values()*3.0)
        output_samples.set_domain(3.0*input_samples.get_domain())

        self.disc = sample.discretization(input_samples, output_samples)

        self.filename = "testfigure"

        output_ref_datum = np.mean(output_samples.get_domain(), axis=1)

        bin_size = 0.15*(np.max(output_samples.get_domain(), axis=1) -
                         np.min(output_samples.get_domain(), axis=1))
        maximum = 1/np.product(bin_size)

        def ifun(outputs):
            """
            Indicator function.
            :param outputs: outputs
            :type outputs: :class:`numpy.ndarray` of shape (N, ndim)
            :rtype: :class:`numpy.ndarray` of shape (N,)
            :returns: 0 if outside of set or positive number if inside set
            """
            left = np.repeat([output_ref_datum-.5*bin_size],
                             outputs.shape[0], 0)
            right = np.repeat([output_ref_datum+.5*bin_size],
                              outputs.shape[0], 0)
            left = np.all(np.greater_equal(outputs, left), axis=1)
            right = np.all(np.less_equal(outputs, right), axis=1)
            inside = np.logical_and(left, right)
            max_values = np.repeat(maximum, outputs.shape[0], 0)
            return inside.astype('float64')*max_values

        self.rho_D = ifun
        self.lnums = [1, 2, 3]
        self.markers = []

        for m in Line2D.markers:
            try:
                if len(m) == 1 and m != ' ':
                    self.markers.append(m)
            except TypeError:
                pass

        self.colors = ('b', 'g', 'r', 'c', 'm', 'y', 'k')