Skip to content

Commit

Permalink
Merge pull request #170 from smattis/v2_master2
Browse files Browse the repository at this point in the history
Stable sample.py and unit tests
  • Loading branch information
lcgraham committed Mar 23, 2016
2 parents b791d5d + b2e566d commit d2d89a9
Show file tree
Hide file tree
Showing 2 changed files with 270 additions and 29 deletions.
70 changes: 41 additions & 29 deletions bet/sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ def load_sample_set(file_name, sample_set_name=None):
loaded_set = sample_set(np.squeeze(mdat[sample_set_name+"_dim"]))

for attrname in dir(loaded_set):
if attrname is not '_dim' and is not '_kdtree':
if attrname is not '_dim' and attrname is not '_kdtree':
if attrname in mdat.keys():
if attrname in sample_set.vector_names:
setattr(loaded_set, attrname,
np.squeeze(mdat[sample_set_name+attrname))
np.squeeze(mdat[sample_set_name+attrname]))
else:
setattr(loaded_set, attrname, mdat[sample_set_name+attrname)
setattr(loaded_set, attrname, mdat[sample_set_name+attrname])
return loaded_set

class sample_set(object):
Expand Down Expand Up @@ -168,14 +168,14 @@ def check_num(self):
num = None
for array_name in self._array_names:
current_array = getattr(self, array_name)
if current_array:
if current_array is not None:
if num is None:
num = current_array.shape[0]
first_array = array_name
else:
if num != current_array.shape[0]:
raise length_not_matching("length of " + array_name +"\
inconsistent with " + first_array)
raise length_not_matching("length of " + array_name +
" inconsistent with " + first_array)
return num

def get_dim(self):
Expand All @@ -198,7 +198,7 @@ def set_values(self, values):
"""
self._values = util.fix_dimensions_data(values)
if self._values.shape[0] != self._dim:
if self._values.shape[1] != self._dim:
raise dim_not_matching("dimension of values incorrect")

def get_values(self):
Expand Down Expand Up @@ -415,19 +415,25 @@ def get_error_estimates_local(self):
return self._error_estimates_local

def local_to_global(self):
"""
Makes global arrays from available local ones.
"""
for array_name in self._array_names:
current_array_local = getattr(self, array_name + "_local")
if current_array_local:
setattr(self, array_name, util.get_global(current_array_local))
if current_array_local is not None:
setattr(self, array_name, util.get_global_values(current_array_local))
pass

def global_to_local(self):
"""
Makes local arrays from available global ones.
"""
num = self.check_num()
global_index = np.arange(num, dytpe=np.int)
self._local_index = np.array_split(global_index, comm.size)
global_index = np.arange(num, dtype=np.int)
self._local_index = np.array_split(global_index, comm.size)[comm.rank]
for array_name in self._array_names:
current_array = getattr(self, array_name)
if current_array:
if current_array is not None:
setattr(self, array_name + "_local",
current_array[self._local_index])

Expand All @@ -453,7 +459,7 @@ def save_discretization(save_disc, file_name, discretization_name=None):
for attrname in dir(save_disc):
curr_attr = getattr(save_disc, attrname)
if curr_attr is not None:
if attrname is in discretization.sample_set_names:
if attrname in discretization.sample_set_names:
save_sample_set(curr_attr, file_name,
distrcretization_name+attrname)
else:
Expand Down Expand Up @@ -499,15 +505,15 @@ def load_discretization(file_name, discretization_name=None):
return None

for attrname in dir(loaded_disc):
if attrname is not '_input_sample_set' and is not '_output_sample_set':
if attrname is not '_input_sample_set' and attrname is not '_output_sample_set':
if attrname in discretization.vector_names:
setattr(loaded_disc, attrname,
np.squeeze(mdat[discretization_name+attrname))
np.squeeze(mdat[discretization_name+attrname]))
elif attrname in discreitzation.sample_set_sames:
setattr(loaded_disc, attrname, load_sample_set(file_name,
distrcretization_name+attrname))
discretization_name+attrname))
elif attrname in mdat.keys():
setattr(loaded_disc, attrname, mdat[discretization_name+attrname)
setattr(loaded_disc, attrname, mdat[discretization_name+attrname])
return loaded_disc


Expand All @@ -528,8 +534,8 @@ class discretization(object):


def __init__(self, input_sample_set, output_sample_set,
emulated_input_sample_set=None, emulated_output_sample_set=None,
output_probability_set=None):
output_probability_set=None,
emulated_input_sample_set=None, emulated_output_sample_set=None):
#: Input sample set :class:`~bet.sample.sample_set`
self._input_sample_set = input_sample_set
#: Output sample set :class:`~bet.sample.sample_set`
Expand Down Expand Up @@ -584,10 +590,12 @@ def set_io_ptr(self, globalize=True):
:meth:`scipy.spatial.KDTree.query``
"""
if not self._output_sample_set._values_local:
self._output_sample_set.get_local_values()
(_, self._io_ptr_local) = self._output_probability_set.get_kdtree.query\
(self._output_sample_set.values_local)
if self._output_sample_set._values_local is None:
self._output_sample_set.global_to_local()
if self._output_probability_set._kdtree is None:
self._output_probability_set.set_kdtree()
(_, self._io_ptr_local) = self._output_probability_set.get_kdtree().query\
(self._output_sample_set._values_local)
if globalize:
self._io_ptr = util.get_global_values(self._io_ptr_local)

Expand Down Expand Up @@ -619,9 +627,11 @@ def set_emulated_ii_ptr(self, globalize=True):
:meth:`scipy.spatial.KDTree.query``
"""
if not self._emulated_input_sample_set.values._local:
self._output_sample_set.get_local_values()
(_, self._emulated_ii_ptr_local) = self._input_sample_set.get_kdtree.\
if self._emulated_input_sample_set._values_local is None:
self._emulated_input_sample_set.global_to_local()
if self._input_sample_set._kdtree is None:
self._input_sample_set.set_kdtree()
(_, self._emulated_ii_ptr_local) = self._input_sample_set.get_kdtree().\
query(self._emulated_input_sample_set._values_local)
if globalize:
self._emulated_ii_ptr = util.get_global_values\
Expand Down Expand Up @@ -655,10 +665,12 @@ def set_emulated_oo_ptr(self, globalize=True):
:meth:`scipy.spatial.KDTree.query``
"""
if not self._emulated_output_sample_set.values._local:
self._emulated_output_sampe_set.get_local_values()
if self._emulated_output_sample_set._values_local is None:
self._emulated_output_sample_set.global_to_local()
if self._output_probability_set._kdtree is None:
self._output_probability_set.set_kdtree()
(_, self._emulated_oo_ptr_local) = self._output_probability_set.\
get_kdtree.query(self._emulated_output_sample_set._values_local)
get_kdtree().query(self._emulated_output_sample_set._values_local)
if globalize:
self._emulated_oo_ptr = util.get_global_values\
(self._emulated_oo_ptr_local)
Expand Down
229 changes: 229 additions & 0 deletions test/test_sample.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
# Copyright (C) 2016 The BET Development TEam

# Steve Mattis 03/23/2016

import unittest
import numpy as np
import numpy.testing as nptest
import bet.sample as sample
import bet.util as util
from bet.Comm import comm, MPI



class Test_sample_set(unittest.TestCase):
def setUp(self):
self.dim = 2
self.num = 100
self.values = np.ones((self.num, self.dim))
self.sam_set = sample.sample_set(dim=self.dim)
self.sam_set.set_values(self.values)
def test_check_dim(self):
"""
Check set_dim
"""
self.assertEqual(self.dim, self.sam_set.get_dim())
def test_set_values(self):
"""
Check set_values.
"""
values = np.ones((150, self.dim))
self.sam_set.set_values(values)
nptest.assert_array_equal(util.fix_dimensions_data(values), self.sam_set.get_values())
def test_get_values(self):
"""
Check get_samples.
"""
nptest.assert_array_equal(util.fix_dimensions_data(self.values), self.sam_set.get_values())
def test_append_values(self):
"""
Check appending of values.
"""
new_values = np.zeros((10, self.dim))
self.sam_set.append_values(new_values)
nptest.assert_array_equal(util.fix_dimensions_data(new_values), self.sam_set.get_values()[self.num::,:])
def test_get_dim(self):
"""
Check to see if dimensions are correct.
"""
self.assertEqual(self.dim, self.sam_set.get_dim())
def test_probabilities(self):
"""
Check probability methods
"""
prob = 1.0/float(self.num)*np.ones((self.num,))
self.sam_set.set_probabilities(prob)
self.sam_set.check_num()
nptest.assert_array_equal(prob, self.sam_set.get_probabilities())
def test_volumes(self):
"""
Check volume methods
"""
vol = 1.0/float(self.num)*np.ones((self.num,))
self.sam_set.set_volumes(vol)
self.sam_set.check_num()
nptest.assert_array_equal(vol, self.sam_set.get_volumes())

def test_error_estimates(self):
"""
Check error estimate methods
"""
ee = np.ones((self.num, self.dim))
self.sam_set.set_error_estimates(ee)
self.sam_set.check_num()
nptest.assert_array_equal(ee, self.sam_set.get_error_estimates())

def test_jacobian_methods(self):
"""
Check jacobian methods.
"""
jac = np.ones((self.num, 3, self.dim))
self.sam_set.set_jacobians(jac)
self.sam_set.check_num()
nptest.assert_array_equal(jac, self.sam_set.get_jacobians())

def test_check_num(self):
"""
Check check_num.
"""
prob = 1.0/float(self.num)*np.ones((self.num,))
self.sam_set.set_probabilities(prob)
vol = 1.0/float(self.num)*np.ones((self.num,))
self.sam_set.set_volumes(vol)
ee = np.ones((self.num, self.dim))
self.sam_set.set_error_estimates(ee)
jac = np.ones((self.num, 3, self.dim))
self.sam_set.set_jacobians(jac)
num = self.sam_set.check_num()
self.assertEqual(self.num, num)
new_values = np.zeros((10, self.dim))
self.sam_set.append_values(new_values)
self.assertRaises(sample.length_not_matching,self.sam_set.check_num)

def test_kd_tree(self):
"""
Check features of the KD Tree
"""
self.sam_set.set_kdtree()
self.sam_set.get_kdtree()

def test_parallel_features(self):
"""
Check parallel features.
"""
prob = 1.0/float(self.num)*np.ones((self.num,))
self.sam_set.set_probabilities(prob)
vol = 1.0/float(self.num)*np.ones((self.num,))
self.sam_set.set_volumes(vol)
ee = np.ones((self.num, self.dim))
self.sam_set.set_error_estimates(ee)
jac = np.ones((self.num, 3, self.dim))
self.sam_set.set_jacobians(jac)
self.sam_set.global_to_local()
self.assertNotEqual(self.sam_set._values_local, None)
if comm.size > 1 :
for array_name in self.sam_set._array_names:
current_array = getattr(self.sam_set, array_name+"_local")
if current_array is not None:
self.assertGreater(getattr(self.sam_set, array_name).shape[0], current_array.shape[0])
local_size = current_array.shape[0]
num = comm.allreduce(local_size, op=MPI.SUM)
self.assertEqual(num, self.num)
current_array_global = util.get_global_values(current_array)
nptest.assert_array_equal(getattr(self.sam_set, array_name), current_array_global)
else:
for array_name in self.sam_set._array_names:
current_array = getattr(self.sam_set, array_name+"_local")
if current_array is not None:
nptest.assert_array_equal(getattr(self.sam_set, array_name), current_array)

for array_name in self.sam_set._array_names:
current_array = getattr(self.sam_set, array_name)
if current_array is not None:
setattr(self.sam_set, array_name + "_old", current_array)
current_array = None
self.sam_set.local_to_global()
for array_name in self.sam_set._array_names:
current_array = getattr(self.sam_set, array_name + "_local")
if current_array is not None:
nptest.assert_array_equal(getattr(self.sam_set, array_name),
getattr(self.sam_set, array_name + "_old"))
def test_domain(self):
"""
Test domain information.
"""
domain = np.ones((self.dim,2))
self.sam_set.set_domain(domain)
nptest.assert_array_equal(domain, self.sam_set.get_domain())


class Test_sample_set_1d(Test_sample_set):
def setUp(self):
self.dim = 1
self.num = 100
self.values = np.ones((self.num, self.dim))
self.sam_set = sample.sample_set(dim=self.dim)
self.sam_set.set_values(self.values)

class Test_discretization_simple(unittest.TestCase):
def setUp(self):
self.dim1 = 3
self.num = 100
self.dim2 = 1
values1 = np.ones((self.num, self.dim1))
values2 = np.ones((self.num, self.dim2))
values3 = np.ones((self.num, self.dim2))
self.input = sample.sample_set(dim=self.dim1)
self.output = sample.sample_set(dim=self.dim2)
self.output_probability_set = sample.sample_set(dim=self.dim2)
self.input.set_values(values1)
self.output.set_values(values2)
self.output_probability_set.set_values(values3)
self.disc = sample.discretization(input_sample_set = self.input,
output_sample_set = self.output,
output_probability_set = self.output_probability_set)

def Test_check_nums(self):
"""
Test number checking.
"""
num = self.disc.check_nums()
self.assertEqual(num, self.num)

def Test_set_io_ptr(self):
"""
Test setting io ptr
"""
#TODO be careful if we change Kdtree
self.disc.set_io_ptr(globalize=True)
self.disc.get_io_ptr()
self.disc.set_io_ptr(globalize=False)
self.disc.get_io_ptr()

def Test_set_emulated_ii_ptr(self):
"""
Test setting emulated ii ptr
"""
#TODO be careful if we change Kdtree
values = np.ones((10, self.dim1))
self.emulated = sample.sample_set(dim=self.dim1)
self.emulated.set_values(values)
self.disc._emulated_input_sample_set = self.emulated
self.disc.set_emulated_ii_ptr(globalize=True)
self.disc.get_emulated_ii_ptr()
self.disc.set_emulated_ii_ptr(globalize=False)
self.disc.get_emulated_ii_ptr()

def Test_set_emulated_oo_ptr(self):
"""
Test setting emulated oo ptr
"""
#TODO be careful if we change Kdtree
values = np.ones((10, self.dim2))
self.emulated = sample.sample_set(dim=self.dim2)
self.emulated.set_values(values)
self.disc._emulated_output_sample_set = self.emulated
self.disc.set_emulated_oo_ptr(globalize=True)
self.disc.get_emulated_oo_ptr()
self.disc.set_emulated_oo_ptr(globalize=False)
self.disc.get_emulated_oo_ptr()

0 comments on commit d2d89a9

Please sign in to comment.