Example #1
0
    def setUp(self):
        """
        Set up
        """

        # create 1-1 map
        self.input_domain1 = np.column_stack((np.zeros((1,)), np.ones((1,))))

        def map_1t1(x):
            return np.sin(x)
        # create 3-1 map
        self.input_domain3 = np.column_stack((np.zeros((3,)), np.ones((3,))))

        def map_3t1(x):
            return np.sum(x, 1)
        # create 3-2 map

        def map_3t2(x):
            return np.vstack(([x[:, 0]+x[:, 1], x[:, 2]])).transpose()
        # create 10-4 map
        self.input_domain10 = np.column_stack(
            (np.zeros((10,)), np.ones((10,))))

        def map_10t4(x):
            x1 = x[:, 0] + x[:, 1]
            x2 = x[:, 2] + x[:, 3]
            x3 = x[:, 4] + x[:, 5]
            x4 = np.sum(x[:, [6, 7, 8, 9]], 1)
            return np.vstack([x1, x2, x3, x4]).transpose()

        self.savefiles = ["11t11", "1t1", "3to1", "3to2", "10to4"]
        self.models = [map_1t1, map_1t1, map_3t1, map_3t2, map_10t4]
        self.QoI_range = [np.array([2.0]), np.array([2.0]), np.array([3.0]),
                          np.array([2.0, 1.0]), np.array([2.0, 2.0, 2.0, 4.0])]

        # define parameters for the adaptive sampler

        num_samples = 100
        chain_length = 10
        num_chains_pproc = int(np.ceil(num_samples/float(chain_length *
                                                         comm.size)))
        num_chains = comm.size * num_chains_pproc
        num_samples = chain_length * np.array(num_chains)

        self.samplers = []
        for model in self.models:
            self.samplers.append(asam.sampler(num_samples, chain_length,
                                              model))

        self.input_domain_list = [self.input_domain1, self.input_domain1,
                                  self.input_domain3, self.input_domain3, self.input_domain10]

        self.test_list = list(zip(self.models, self.QoI_range, self.samplers,
                                  self.input_domain_list, self.savefiles))
Example #2
0
def verify_samples(QoI_range, sampler, input_domain,
                   t_set, savefile, initial_sample_type, hot_start=0):
    """
    Run :meth:`bet.sampling.adaptiveSampling.sampler.generalized_chains` and
    verify that the samples have the correct dimensions and are containted in
    the bounded parameter space.
    """

    # create indicator function
    Q_ref = QoI_range*0.5
    bin_size = 0.15*QoI_range
    maximum = 1/np.product(bin_size)

    def ifun(outputs):
        """
        Indicator function
        """
        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

    # create rhoD_kernel
    kernel_rD = asam.rhoD_kernel(maximum, ifun)
    if comm.rank == 0:
        print("dim", input_domain.shape)
    if not hot_start:
        # run generalized chains
        (my_discretization, all_step_ratios) = sampler.generalized_chains(
            input_domain, t_set, kernel_rD, savefile, initial_sample_type)
        print("COLD", comm.rank)
    else:
        # cold start
        sampler1 = asam.sampler(sampler.num_samples // 2, sampler.chain_length // 2,
                                sampler.lb_model)
        (my_discretization, all_step_ratios) = sampler1.generalized_chains(
            input_domain, t_set, kernel_rD, savefile, initial_sample_type)
        print("COLD then", comm.rank)
        comm.barrier()
        # hot start
        (my_discretization, all_step_ratios) = sampler.generalized_chains(
            input_domain, t_set, kernel_rD, savefile, initial_sample_type,
            hot_start=hot_start)
        print("HOT", comm.rank)
    comm.barrier()
    # check dimensions of input and output
    assert my_discretization.check_nums()

    # are the input in bounds?
    input_left = np.repeat([input_domain[:, 0]], sampler.num_samples, 0)
    input_right = np.repeat([input_domain[:, 1]], sampler.num_samples, 0)
    assert np.all(my_discretization._input_sample_set.get_values() <=
                  input_right)
    assert np.all(my_discretization._input_sample_set.get_values() >=
                  input_left)

    # check dimensions of output
    assert my_discretization._output_sample_set.get_dim() == len(QoI_range)

    # check dimensions of all_step_ratios
    assert all_step_ratios.shape == (sampler.num_chains, sampler.chain_length)

    # are all the step ratios of an appropriate size?
    assert np.all(all_step_ratios >= t_set.min_ratio)
    assert np.all(all_step_ratios <= t_set.max_ratio)

    # did the savefiles get created? (proper number, contain proper keys)
    comm.barrier()
    mdat = dict()
    # if comm.rank == 0:
    mdat = sio.loadmat(savefile)
    saved_disc = bet.sample.load_discretization(savefile)
    # compare the input
    nptest.assert_array_equal(my_discretization._input_sample_set.
                              get_values(), saved_disc._input_sample_set.get_values())
    # compare the output
    nptest.assert_array_equal(my_discretization._output_sample_set.
                              get_values(), saved_disc._output_sample_set.get_values())

    nptest.assert_array_equal(all_step_ratios, mdat['step_ratios'])
    assert sampler.chain_length == mdat['chain_length']
    assert sampler.num_samples == mdat['num_samples']
    assert sampler.num_chains == mdat['num_chains']
    nptest.assert_array_equal(sampler.sample_batch_no,
                              np.squeeze(mdat['sample_batch_no']))
Example #3
0
    rho_left = np.all(np.greater_equal(outputs, rho_left), axis=1)
    rho_right = np.all(np.less_equal(outputs, rho_right), axis=1)
    inside = np.logical_and(rho_left, rho_right)
    max_values = np.repeat(maximum, outputs.shape[0], 0)
    return inside.astype('float64')*max_values

kernel_mm = asam.maxima_mean_kernel(np.array([Q_ref]), rho_D)
kernel_rD = asam.rhoD_kernel(maximum, rho_D)
kernel_m = asam.maxima_kernel(np.array([Q_ref]), rho_D)
heur_list = [kernel_mm, kernel_rD, kernel_m]

# Create sampler
chain_length = 125
num_chains = 80
num_samples = num_chains*chain_length
sampler = asam.sampler(num_samples, chain_length, model)
inital_sample_type = "lhs"

# Get samples
# Run with varying kernels
gen_results = sampler.run_gen(heur_list, rho_D, maximum, param_domain,
        transition_set, sample_save_file) 
#run_reseed_results = sampler.run_gen(heur_list, rho_D, maximum, param_domain,
#        t_kernel, sample_save_file, reseed=3)

# Run with varying transition sets bounds
init_ratio = [0.1, 0.25, 0.5]
min_ratio = [2e-3, 2e-5, 2e-8]
max_ratio = [.5, .75, 1.0]
tk_results = sampler.run_tk(init_ratio, min_ratio, max_ratio, rho_D,
        maximum, param_domain, kernel_rD, sample_save_file)
    def setUp(self):
        """
        Set up for sampler.
        """

        # create 1-1 map
        self.input_domain1 = np.column_stack((np.zeros((1, )), np.ones((1, ))))

        def map_1t1(x):
            return np.sin(x)

        # create 3-1 map
        self.input_domain3 = np.column_stack((np.zeros((3, )), np.ones((3, ))))

        def map_3t1(x):
            return np.sum(x, 1)

        # create 3-2 map
        def map_3t2(x):
            return np.column_stack(([x[:, 0] + x[:, 1], x[:, 2]]))

        # create 10-4 map
        self.input_domain10 = np.column_stack((np.zeros((10, )), np.ones(
            (10, ))))

        def map_10t4(x):
            x1 = x[:, 0] + x[:, 1]
            x2 = x[:, 2] + x[:, 3]
            x3 = x[:, 4] + x[:, 5]
            x4 = np.sum(x[:, [6, 7, 8, 9]], 1)
            return np.column_stack([x1, x2, x3, x4])

        self.savefiles = ["11t11", "1t1", "3to1", "3to2", "10to4"]
        self.models = [map_1t1, map_1t1, map_3t1, map_3t2, map_10t4]
        self.QoI_range = [
            np.array([2.0]),
            np.array([2.0]),
            np.array([3.0]),
            np.array([2.0, 1.0]),
            np.array([2.0, 2.0, 2.0, 4.0])
        ]

        # define parameters for the adaptive sampler

        num_samples = 150
        chain_length = 10
        # num_chains_pproc = int(np.ceil(num_samples / float(chain_length *
        #                                                    comm.size)))
        # num_chains = comm.size * num_chains_pproc
        # num_samples = chain_length * np.array(num_chains)

        self.samplers = []
        for model in self.models:
            self.samplers.append(asam.sampler(num_samples, chain_length,
                                              model))

        self.input_domain_list = [
            self.input_domain1, self.input_domain1, self.input_domain3,
            self.input_domain3, self.input_domain10
        ]

        self.test_list = list(
            zip(self.models, self.QoI_range, self.samplers,
                self.input_domain_list, self.savefiles))
def verify_samples(QoI_range,
                   sampler,
                   input_domain,
                   t_set,
                   savefile,
                   initial_sample_type,
                   hot_start=0):
    """
    Run :meth:`bet.sampling.adaptiveSampling.sampler.generalized_chains` and
    verify that the samples have the correct dimensions and are containted in
    the bounded parameter space.
    """

    # create indicator function
    Q_ref = QoI_range * 0.5
    bin_size = 0.15 * QoI_range
    maximum = 1 / np.product(bin_size)

    def ifun(outputs):
        """
        Indicator function
        """
        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

    # create rhoD_kernel
    kernel_rD = asam.rhoD_kernel(maximum, ifun)
    if comm.rank == 0:
        print("dim", input_domain.shape)
    if not hot_start:
        # run generalized chains
        (my_discretization,
         all_step_ratios) = sampler.generalized_chains(input_domain, t_set,
                                                       kernel_rD, savefile,
                                                       initial_sample_type)
        print("COLD", comm.rank)
    else:
        # cold start
        sampler1 = asam.sampler(sampler.num_samples // 2,
                                sampler.chain_length // 2, sampler.lb_model)
        (my_discretization, all_step_ratios) = sampler1.generalized_chains(
            input_domain, t_set, kernel_rD, savefile, initial_sample_type)
        print("COLD then", comm.rank)
        comm.barrier()
        # hot start
        (my_discretization,
         all_step_ratios) = sampler.generalized_chains(input_domain,
                                                       t_set,
                                                       kernel_rD,
                                                       savefile,
                                                       initial_sample_type,
                                                       hot_start=hot_start)
        print("HOT", comm.rank)
    comm.barrier()
    # check dimensions of input and output
    assert my_discretization.check_nums()

    # are the input in bounds?
    input_left = np.repeat([input_domain[:, 0]], sampler.num_samples, 0)
    input_right = np.repeat([input_domain[:, 1]], sampler.num_samples, 0)
    assert np.all(
        my_discretization._input_sample_set.get_values() <= input_right)
    assert np.all(
        my_discretization._input_sample_set.get_values() >= input_left)

    # check dimensions of output
    assert my_discretization._output_sample_set.get_dim() == len(QoI_range)

    # check dimensions of all_step_ratios
    assert all_step_ratios.shape == (sampler.num_chains, sampler.chain_length)

    # are all the step ratios of an appropriate size?
    assert np.all(all_step_ratios >= t_set.min_ratio)
    assert np.all(all_step_ratios <= t_set.max_ratio)

    # did the savefiles get created? (proper number, contain proper keys)
    comm.barrier()
    mdat = dict()
    # if comm.rank == 0:
    mdat = sio.loadmat(savefile)
    saved_disc = bet.sample.load_discretization(savefile)
    saved_disc.local_to_global()

    # # compare the input
    nptest.assert_array_equal(my_discretization._input_sample_set.get_values(),
                              saved_disc._input_sample_set.get_values())
    # compare the output
    nptest.assert_array_equal(
        my_discretization._output_sample_set.get_values(),
        saved_disc._output_sample_set.get_values())

    nptest.assert_array_equal(all_step_ratios, mdat['step_ratios'])
    assert sampler.chain_length == mdat['chain_length']
    assert sampler.num_samples == mdat['num_samples']
    assert sampler.num_chains == mdat['num_chains']
    nptest.assert_array_equal(sampler.sample_batch_no,
                              np.squeeze(mdat['sample_batch_no']))
Example #6
0
    rho_right = np.all(np.less_equal(outputs, rho_right), axis=1)
    inside = np.logical_and(rho_left, rho_right)
    max_values = np.repeat(maximum, outputs.shape[0], 0)
    return inside.astype('float64') * max_values


kernel_mm = asam.maxima_mean_kernel(np.array([Q_ref]), rho_D)
kernel_rD = asam.rhoD_kernel(maximum, rho_D)
kernel_m = asam.maxima_kernel(np.array([Q_ref]), rho_D)
heur_list = [kernel_mm, kernel_rD, kernel_m]

# Create sampler
chain_length = 125
num_chains = 80
num_samples = num_chains * chain_length
sampler = asam.sampler(num_samples, chain_length, model)
inital_sample_type = "lhs"

# Get samples
# Run with varying kernels
gen_results = sampler.run_gen(heur_list, rho_D, maximum, param_domain,
                              transition_set, sample_save_file)
# run_reseed_results = sampler.run_gen(heur_list, rho_D, maximum, param_domain,
#        t_kernel, sample_save_file, reseed=3)

# Run with varying transition sets bounds
init_ratio = [0.1, 0.25, 0.5]
min_ratio = [2e-3, 2e-5, 2e-8]
max_ratio = [.5, .75, 1.0]
tk_results = sampler.run_tk(init_ratio, min_ratio, max_ratio, rho_D, maximum,
                            param_domain, kernel_rD, sample_save_file)
Example #7
0
    def setUp(self):
        """
        Set up
        """

        # create 1-1 map
        self.param_min1 = np.zeros((1, ))
        self.param_max1 = np.zeros((1, ))

        def map_1t1(x):
            """
            1 to 1 map
            """
            return x * 2.0

        # create 3-1 map
        self.param_min3 = np.zeros((3, ))
        self.param_max3 = np.ones((3, ))

        def map_3t1(x):
            """
            3 to 1 map
            """
            return np.expand_dims(np.sum(x, 1), axis=1)

        # create 3-2 map
        def map_3t2(x):
            """
            3 to 2 map
            """
            return np.vstack(([x[:, 0] + x[:, 1], x[:, 2]])).transpose()

        # create 10-4 map
        self.param_min10 = np.zeros((10, ))
        self.param_max10 = np.ones((10, ))

        def map_10t4(x):
            """
            10 to 4 map
            """
            x1 = x[:, 0] + x[:, 1]
            x2 = x[:, 2] + x[:, 3]
            x3 = x[:, 4] + x[:, 5]
            x4 = np.sum(x[:, [6, 7, 8, 9]], 1)
            return np.vstack([x1, x2, x3, x4]).transpose()

        self.savefiles = ["11t11", "1t1", "3to1", "3to2", "10to4"]
        self.models = [map_1t1, map_1t1, map_3t1, map_3t2, map_10t4]
        self.QoI_range = [
            np.array([2.0]),
            np.array([2.0]),
            np.array([3.0]),
            np.array([2.0, 1.0]),
            np.array([2.0, 2.0, 2.0, 4.0])
        ]

        # define parameters for the adaptive sampler

        num_samples = 1000
        chain_length = 100
        num_chains_pproc = int(
            np.ceil(num_samples / float(chain_length * comm.size)))
        num_chains = comm.size * num_chains_pproc
        num_samples = chain_length * np.array(num_chains)

        self.samplers = []
        for model in self.models:
            self.samplers.append(asam.sampler(num_samples, chain_length,
                                              model))

        self.param_min_list = [
            self.param_min1, self.param_min1, self.param_min3, self.param_min3,
            self.param_min10
        ]
        self.param_max_list = [
            self.param_max1, self.param_max1, self.param_max3, self.param_max3,
            self.param_max10
        ]

        self.test_list = zip(self.models, self.QoI_range, self.samplers,
                             self.param_min_list, self.param_max_list,
                             self.savefiles)