Beispiel #1
0
def test_loadmat():
    """
    Tests :meth:`bet.sampling.basicSampling.loadmat`
    """
    np.random.seed(1)
    mdat1 = {'num_samples': 5}
    mdat2 = {'num_samples': 6}
    model = "this is not a model"

    my_input1 = sample_set(1)
    my_input1.set_values(np.random.random((5, 1)))
    my_output = sample_set(1)
    my_output.set_values(np.random.random((5, 1)))
    my_input2 = sample_set(1)
    my_input2.set_values(np.random.random((6, 1)))

    sio.savemat(os.path.join(local_path, 'testfile1'), mdat1)
    sio.savemat(os.path.join(local_path, 'testfile2'), mdat2)

    bet.sample.save_discretization(disc(my_input1, my_output),
                                   (os.path.join(local_path, 'testfile1')),
                                   globalize=True)
    bet.sample.save_discretization(disc(my_input2, None),
                                   os.path.join(local_path, 'testfile2'),
                                   "NAME",
                                   globalize=True)

    (loaded_sampler1,
     discretization1) = bsam.loadmat(os.path.join(local_path, 'testfile1'))
    nptest.assert_array_equal(discretization1._input_sample_set.get_values(),
                              my_input1.get_values())
    nptest.assert_array_equal(discretization1._output_sample_set.get_values(),
                              my_output.get_values())
    assert loaded_sampler1.num_samples == 5
    assert loaded_sampler1.lb_model is None

    (loaded_sampler2,
     discretization2) = bsam.loadmat(os.path.join(local_path, 'testfile2'),
                                     disc_name="NAME",
                                     model=model)
    nptest.assert_array_equal(discretization2._input_sample_set.get_values(),
                              my_input2.get_values())
    assert discretization2._output_sample_set is None
    assert loaded_sampler2.num_samples == 6
    assert loaded_sampler2.lb_model == model
    if os.path.exists(os.path.join(local_path, 'testfile1.mat')):
        os.remove(os.path.join(local_path, 'testfile1.mat'))
    if os.path.exists(os.path.join(local_path, 'testfile2.mat')):
        os.remove(os.path.join(local_path, 'testfile2.mat'))
Beispiel #2
0
def test_loadmat():
    """
    Tests :meth:`bet.sampling.basicSampling.loadmat`
    """
    np.random.seed(1)
    mdat1 = {'num_samples':5}
    mdat2 = {'num_samples':6}
    model = "this is not a model"

    my_input1 = sample_set(1)
    my_input1.set_values(np.random.random((5,1)))
    my_output = sample_set(1)
    my_output.set_values(np.random.random((5,1)))
    my_input2 = sample_set(1)
    my_input2.set_values(np.random.random((6,1)))


    sio.savemat(os.path.join(local_path, 'testfile1'), mdat1)
    sio.savemat(os.path.join(local_path, 'testfile2'), mdat2)

    
    bet.sample.save_discretization(disc(my_input1, my_output),
            (os.path.join(local_path, 'testfile1')), globalize=True)
    bet.sample.save_discretization(disc(my_input2, None),
            os.path.join(local_path, 'testfile2'), "NAME", globalize=True)

    (loaded_sampler1, discretization1) = bsam.loadmat(os.path.join(local_path,
        'testfile1'))
    nptest.assert_array_equal(discretization1._input_sample_set.get_values(),
            my_input1.get_values())
    nptest.assert_array_equal(discretization1._output_sample_set.get_values(),
            my_output.get_values())
    assert loaded_sampler1.num_samples == 5
    assert loaded_sampler1.lb_model is None

    (loaded_sampler2, discretization2) = bsam.loadmat(os.path.join(local_path,
        'testfile2'), disc_name="NAME", model=model)
    nptest.assert_array_equal(discretization2._input_sample_set.get_values(),
            my_input2.get_values())
    assert discretization2._output_sample_set is None
    assert loaded_sampler2.num_samples == 6
    assert loaded_sampler2.lb_model == model
    if os.path.exists(os.path.join(local_path, 'testfile1.mat')):
        os.remove(os.path.join(local_path, 'testfile1.mat'))
    if os.path.exists(os.path.join(local_path, 'testfile2.mat')):
        os.remove(os.path.join(local_path, 'testfile2.mat'))
Beispiel #3
0
def verify_compute_QoI_and_create_discretization(model, sampler,
                                                 input_sample_set, savefile):
    """
    Verify that the user samples are correct.
    """
    # evalulate the model at the samples directly
    output_values = (model(input_sample_set._values))
    if len(output_values.shape) == 1:
        output_sample_set = sample_set(1)
    else:
        output_sample_set = sample_set(output_values.shape[1])
    output_sample_set.set_values(output_values)
    discretization = disc(input_sample_set, output_sample_set)

    # evaluate the model at the sample
    print savefile, input_sample_set.get_dim()
    my_discretization = sampler.compute_QoI_and_create_discretization(\
        input_sample_set, savefile, globalize=True)
    #comm.barrier()

    my_num = my_discretization.check_nums()

    # compare the samples
    nptest.assert_array_equal(my_discretization._input_sample_set.get_values(),
                              discretization._input_sample_set.get_values())
    # compare the data
    nptest.assert_array_equal(
        my_discretization._output_sample_set.get_values(),
        discretization._output_sample_set.get_values())

    # did num_samples get updated?
    assert my_num == sampler.num_samples

    # did the file get correctly saved?
    saved_disc = bet.sample.load_discretization(savefile)
    mdat = sio.loadmat(savefile)
    print "HERE HERE", mdat, my_num
    #comm.barrier()
    # compare the samples
    nptest.assert_array_equal(my_discretization._input_sample_set.get_values(),
                              saved_disc._input_sample_set.get_values())
    # compare the data
    nptest.assert_array_equal(
        my_discretization._output_sample_set.get_values(),
        saved_disc._output_sample_set.get_values())
Beispiel #4
0
def verify_compute_QoI_and_create_discretization(model, sampler,
                                                 input_sample_set,
                                                 savefile):
    """
    Verify that the user samples are correct.
    """
    # evalulate the model at the samples directly
    output_values = (model(input_sample_set._values))
    if len(output_values.shape) == 1:
        output_sample_set = sample_set(1)
    else:
        output_sample_set = sample_set(output_values.shape[1])
    output_sample_set.set_values(output_values)
    discretization = disc(input_sample_set, output_sample_set)

    # evaluate the model at the sample
    print savefile, input_sample_set.get_dim()
    my_discretization = sampler.compute_QoI_and_create_discretization(\
        input_sample_set, savefile, globalize=True) 
    #comm.barrier()

    my_num = my_discretization.check_nums() 

    # compare the samples
    nptest.assert_array_equal(my_discretization._input_sample_set.get_values(),
            discretization._input_sample_set.get_values())
    # compare the data
    nptest.assert_array_equal(my_discretization._output_sample_set.get_values(),
            discretization._output_sample_set.get_values())

    # did num_samples get updated?
    assert my_num == sampler.num_samples
   
    # did the file get correctly saved?
    saved_disc = bet.sample.load_discretization(savefile)
    mdat = sio.loadmat(savefile)
    print "HERE HERE", mdat, my_num
    #comm.barrier()
    # compare the samples
    nptest.assert_array_equal(my_discretization._input_sample_set.get_values(),
        saved_disc._input_sample_set.get_values())
    # compare the data
    nptest.assert_array_equal(my_discretization._output_sample_set.get_values(),
       saved_disc._output_sample_set.get_values())
Beispiel #5
0
def test_loadmat_init():
    """
    Tests :meth:`bet.sampling.adaptiveSampling.loadmat` and
    :meth:`bet.sampling.adaptiveSampling.sampler.init`.
    """
    np.random.seed(1)
    chain_length = 5

    mdat1 = {'num_samples': 50, 'chain_length': chain_length}
    mdat2 = {'num_samples': 60, 'chain_length': chain_length}
    model = "this is not a model"

    my_input1 = sample_set(1)
    my_input1.set_values(np.random.random((50, 1)))
    my_output1 = sample_set(1)
    my_output1.set_values(np.random.random((50, 1)))
    my_input2 = sample_set(1)
    my_input2.set_values(np.random.random((60, 1)))
    my_output2 = sample_set(1)
    my_output2.set_values(np.random.random((60, 1)))

    num_samples = np.array([50, 60])
    num_chains_pproc1, num_chains_pproc2 = np.ceil(num_samples/float(
        chain_length*comm.size)).astype('int')
    num_chains1, num_chains2 = comm.size * np.array([num_chains_pproc1,
                                                     num_chains_pproc2])
    num_samples1, num_samples2 = chain_length * np.array([num_chains1,
                                                          num_chains2])

    mdat1['num_chains'] = num_chains1
    mdat1['kern_old'] = np.random.random((num_chains1,))
    mdat1['step_ratios'] = np.random.random((num_samples[0],))
    mdat2['num_chains'] = num_chains2
    mdat2['kern_old'] = np.random.random((num_chains2,))
    mdat2['step_ratios'] = np.random.random((num_samples[1],))

    sio.savemat(os.path.join(local_path, 'testfile1'), mdat1)
    sio.savemat(os.path.join(local_path, 'testfile2'), mdat2)

    bet.sample.save_discretization(disc(my_input1, my_output1),
                                   os.path.join(local_path, 'testfile1'), globalize=True)
    bet.sample.save_discretization(disc(my_input2, my_output2),
                                   os.path.join(local_path, 'testfile2'), globalize=True)
    loaded_sampler1, discretization1, _, _ = asam.loadmat(os.path.join(local_path,
                                                                       'testfile1'), hot_start=2)
    nptest.assert_array_equal(discretization1._input_sample_set.get_values(),
                              my_input1.get_values())
    nptest.assert_array_equal(discretization1._output_sample_set.get_values(),
                              my_output1.get_values())
    assert loaded_sampler1.num_samples == num_samples1
    assert loaded_sampler1.chain_length == chain_length
    assert loaded_sampler1.num_chains_pproc == num_chains_pproc1
    assert loaded_sampler1.num_chains == num_chains1
    nptest.assert_array_equal(np.repeat(np.arange(num_chains1), chain_length, 0),
                              loaded_sampler1.sample_batch_no)
    assert loaded_sampler1.lb_model == None

    loaded_sampler2, discretization2, _, _ = asam.loadmat(os.path.join(local_path,
                                                                       'testfile2'), lb_model=model, hot_start=2)
    nptest.assert_array_equal(discretization2._input_sample_set.get_values(),
                              my_input2.get_values())
    assert loaded_sampler2.num_samples == num_samples2
    assert loaded_sampler2.chain_length == chain_length
    assert loaded_sampler2.num_chains_pproc == num_chains_pproc2
    assert loaded_sampler2.num_chains == num_chains2
    nptest.assert_array_equal(np.repeat(np.arange(num_chains2), chain_length, 0),
                              loaded_sampler2.sample_batch_no)
    nptest.assert_array_equal(discretization2._output_sample_set.get_values(),
                              my_output2.get_values())
    comm.barrier()
    if comm.rank == 0:
        if os.path.exists(os.path.join(local_path, 'testfile1.mat')):
            os.remove(os.path.join(local_path, 'testfile1.mat'))
        if os.path.exists(os.path.join(local_path, 'testfile2.mat')):
            os.remove(os.path.join(local_path, 'testfile2.mat'))
Beispiel #6
0
def test_loadmat_parallel():
    """

    Tests :class:`bet.sampling.basicSampling.sampler.loadmat`.

    """
    np.random.seed(1)
    mdat1 = {'num_samples':10}
    mdat2 = {'num_samples':20}
    model = "this is not a model"

    my_input1 = sample_set(1)
    my_input1.set_values_local(np.array_split(np.random.random((10,1)),
        comm.size)[comm.rank])
    my_output1 = sample_set(1)
    my_output1.set_values_local(np.array_split(np.random.random((10,1)),
        comm.size)[comm.rank]) 
    my_input2 = sample_set(1)
    my_input2.set_values_local(np.array_split(np.random.random((20,1)),
        comm.size)[comm.rank])
    my_output2 = sample_set(1)
    my_output2.set_values_local(np.array_split(np.random.random((20,1)),
        comm.size)[comm.rank]) 

    file_name1 = 'testfile1.mat'
    file_name2 = 'testfile2.mat'

    if comm.size > 1:
        local_file_name1 = os.path.os.path.join(os.path.dirname(file_name1),
            "proc{}_{}".format(comm.rank, os.path.basename(file_name1)))
        local_file_name2 = os.path.os.path.join(os.path.dirname(file_name2),
            "proc{}_{}".format(comm.rank, os.path.basename(file_name2)))
    else:
        local_file_name1 = file_name1
        local_file_name2 = file_name2


    sio.savemat(local_file_name1, mdat1)
    sio.savemat(local_file_name2, mdat2)
    comm.barrier()
    
    bet.sample.save_discretization(disc(my_input1, my_output1),
            file_name1, globalize=False)
    bet.sample.save_discretization(disc(my_input2, my_output2),
            file_name2, "NAME", globalize=False)

    (loaded_sampler1, discretization1) = bsam.loadmat(file_name1)
    nptest.assert_array_equal(discretization1._input_sample_set.get_values(),
            my_input1.get_values())
    nptest.assert_array_equal(discretization1._output_sample_set.get_values(),
            my_output1.get_values())
    assert loaded_sampler1.num_samples == 10
    assert loaded_sampler1.lb_model is None

    (loaded_sampler2, discretization2) = bsam.loadmat(file_name2,
        disc_name="NAME", model=model)
    nptest.assert_array_equal(discretization2._input_sample_set.get_values(),
            my_input2.get_values())
    nptest.assert_array_equal(discretization2._output_sample_set.get_values(),
            my_output2.get_values())

    assert loaded_sampler2.num_samples == 20
    assert loaded_sampler2.lb_model == model
    if comm.size == 1:
        os.remove(file_name1)
        os.remove(file_name2)
    else:
        os.remove(local_file_name1)
        os.remove(local_file_name2)
def test_loadmat_init():
    """
    Tests :meth:`bet.sampling.adaptiveSampling.loadmat` and
    :meth:`bet.sampling.adaptiveSampling.sampler.init`.
    """
    np.random.seed(1)
    chain_length = 5

    mdat1 = {'num_samples': 50, 'chain_length': chain_length}
    mdat2 = {'num_samples': 60, 'chain_length': chain_length}
    model = "this is not a model"

    num_samples = np.array([50, 60])
    num_chains_pproc1, num_chains_pproc2 = np.ceil(
        num_samples / float(chain_length * comm.size)).astype('int')
    num_chains1, num_chains2 = comm.size * np.array(
        [num_chains_pproc1, num_chains_pproc2])
    num_samples1, num_samples2 = chain_length * np.array(
        [num_chains1, num_chains2])

    my_input1 = sample_set(1)
    my_input1.set_values(np.random.random((num_samples1, 1)))
    my_output1 = sample_set(1)
    my_output1.set_values(np.random.random((num_samples1, 1)))
    my_input2 = sample_set(1)
    my_input2.set_values(np.random.random((num_samples2, 1)))
    my_output2 = sample_set(1)
    my_output2.set_values(np.random.random((num_samples2, 1)))

    mdat1['num_chains'] = num_chains1
    mdat1['kern_old'] = np.random.random((num_chains1, ))
    mdat1['step_ratios'] = np.random.random((num_samples1, ))
    mdat2['num_chains'] = num_chains2
    mdat2['kern_old'] = np.random.random((num_chains2, ))
    mdat2['step_ratios'] = np.random.random((num_samples2, ))

    sio.savemat(os.path.join(local_path, 'testfile1'), mdat1)
    sio.savemat(os.path.join(local_path, 'testfile2'), mdat2)

    bet.sample.save_discretization(disc(my_input1, my_output1),
                                   os.path.join(local_path, 'testfile1'),
                                   globalize=True)
    bet.sample.save_discretization(disc(my_input2, my_output2),
                                   os.path.join(local_path, 'testfile2'),
                                   globalize=True)
    loaded_sampler1, discretization1, _, _ = asam.loadmat(os.path.join(
        local_path, 'testfile1'),
                                                          hot_start=2)
    nptest.assert_array_equal(discretization1._input_sample_set.get_values(),
                              my_input1.get_values())
    nptest.assert_array_equal(discretization1._output_sample_set.get_values(),
                              my_output1.get_values())
    assert loaded_sampler1.num_samples == num_samples1
    assert loaded_sampler1.chain_length == chain_length
    assert loaded_sampler1.num_chains_pproc == num_chains_pproc1
    assert loaded_sampler1.num_chains == num_chains1
    nptest.assert_array_equal(
        np.repeat(np.arange(num_chains1), chain_length, 0),
        loaded_sampler1.sample_batch_no)
    assert loaded_sampler1.lb_model is None

    loaded_sampler2, discretization2, _, _ = asam.loadmat(os.path.join(
        local_path, 'testfile2'),
                                                          lb_model=model,
                                                          hot_start=2)
    nptest.assert_array_equal(discretization2._input_sample_set.get_values(),
                              my_input2.get_values())
    assert loaded_sampler2.num_samples == num_samples2
    assert loaded_sampler2.chain_length == chain_length
    assert loaded_sampler2.num_chains_pproc == num_chains_pproc2
    assert loaded_sampler2.num_chains == num_chains2
    nptest.assert_array_equal(
        np.repeat(np.arange(num_chains2), chain_length, 0),
        loaded_sampler2.sample_batch_no)
    nptest.assert_array_equal(discretization2._output_sample_set.get_values(),
                              my_output2.get_values())
    comm.barrier()
    if comm.rank == 0:
        if os.path.exists(os.path.join(local_path, 'testfile1.mat')):
            os.remove(os.path.join(local_path, 'testfile1.mat'))
        if os.path.exists(os.path.join(local_path, 'testfile2.mat')):
            os.remove(os.path.join(local_path, 'testfile2.mat'))
Beispiel #8
0
def test_loadmat_parallel():
    """

    Tests :class:`bet.sampling.basicSampling.sampler.loadmat`.

    """
    np.random.seed(1)
    mdat1 = {'num_samples': 10}
    mdat2 = {'num_samples': 20}
    model = "this is not a model"

    my_input1 = sample_set(1)
    my_input1.set_values_local(
        np.array_split(np.random.random((10, 1)), comm.size)[comm.rank])
    my_output1 = sample_set(1)
    my_output1.set_values_local(
        np.array_split(np.random.random((10, 1)), comm.size)[comm.rank])
    my_input2 = sample_set(1)
    my_input2.set_values_local(
        np.array_split(np.random.random((20, 1)), comm.size)[comm.rank])
    my_output2 = sample_set(1)
    my_output2.set_values_local(
        np.array_split(np.random.random((20, 1)), comm.size)[comm.rank])

    file_name1 = 'testfile1.mat'
    file_name2 = 'testfile2.mat'

    if comm.size > 1:
        local_file_name1 = os.path.os.path.join(
            os.path.dirname(file_name1),
            "proc{}_{}".format(comm.rank, os.path.basename(file_name1)))
        local_file_name2 = os.path.os.path.join(
            os.path.dirname(file_name2),
            "proc{}_{}".format(comm.rank, os.path.basename(file_name2)))
    else:
        local_file_name1 = file_name1
        local_file_name2 = file_name2

    sio.savemat(local_file_name1, mdat1)
    sio.savemat(local_file_name2, mdat2)
    comm.barrier()

    bet.sample.save_discretization(disc(my_input1, my_output1),
                                   file_name1,
                                   globalize=False)
    bet.sample.save_discretization(disc(my_input2, my_output2),
                                   file_name2,
                                   "NAME",
                                   globalize=False)

    (loaded_sampler1, discretization1) = bsam.loadmat(file_name1)
    nptest.assert_array_equal(discretization1._input_sample_set.get_values(),
                              my_input1.get_values())
    nptest.assert_array_equal(discretization1._output_sample_set.get_values(),
                              my_output1.get_values())
    assert loaded_sampler1.num_samples == 10
    assert loaded_sampler1.lb_model is None

    (loaded_sampler2, discretization2) = bsam.loadmat(file_name2,
                                                      disc_name="NAME",
                                                      model=model)
    nptest.assert_array_equal(discretization2._input_sample_set.get_values(),
                              my_input2.get_values())
    nptest.assert_array_equal(discretization2._output_sample_set.get_values(),
                              my_output2.get_values())

    assert loaded_sampler2.num_samples == 20
    assert loaded_sampler2.lb_model == model
    if comm.size == 1:
        os.remove(file_name1)
        os.remove(file_name2)
    else:
        os.remove(local_file_name1)
        os.remove(local_file_name2)