def test_load_save(self): rng = RandomNumberGenerator() with self.assertRaises(IOError): rng.loadState("does/not/exist") with TestAreaContext("rng_state") as t: rng.saveState("rng.txt") val1 = rng.getInt() val2 = rng.getInt() rng.loadState("rng.txt") self.assertEqual(rng.getInt(), val1) self.assertEqual(rng.getInt(), val2)
def test_rng_state(self): rng = RandomNumberGenerator() with self.assertRaises(ValueError): rng.setState("12") rng.setState("0123456789ABCDEF") val1 = rng.getInt() val2 = rng.getInt() self.assertFalse(val1 == val2) rng.setState("0123456789ABCDEF") self.assertEqual(rng.getInt(), val1) self.assertEqual(rng.getInt(), val2)
def test_analysis_module(self): rng = RandomNumberGenerator() module = self.createAnalysisModule() ens_size = 12 obs_size = 1 state_size = 2 true_params = [1.25, 0.75] true_state = forward_model(true_params) obs = [(measure(true_state), 0.75)] A = Matrix(state_size, ens_size) ens = [] for iens in range(ens_size): param = [random.gauss(1.00, 1.00), random.gauss(1.00, 1.00)] ens.append(param) mask = BoolVector(default_value=True, initial_size=ens_size) mask[2] = False (A, S, E, D, R, dObs) = init_matrices(ens, mask, obs, rng) module.initUpdate(mask, S, R, dObs, E, D, rng) module.updateA(A, S, R, dObs, E, D, rng) mask[10] = False mask[5] = False (A, S, E, D, R, dObs) = init_matrices(ens, mask, obs, rng) self.assertEqual(S.dims(), (obs_size, mask.countEqual(True))) self.assertEqual(E.dims(), (obs_size, mask.countEqual(True))) self.assertEqual(D.dims(), (obs_size, mask.countEqual(True))) module.initUpdate(mask, S, R, dObs, E, D, rng) module.updateA(A, S, R, dObs, E, D, rng)
def setUp(self): if sys.platform.lower() == 'darwin': self.libname = os.path.join(res.res_lib_path, "rml_enkf.dylib") else: self.libname = os.path.join(res.res_lib_path, "rml_enkf.so") self.config_file = self.createTestPath("Equinor/config/obs_testing2/config") self.rng = RandomNumberGenerator(RngAlgTypeEnum.MZRAN, RngInitModeEnum.INIT_DEFAULT)
def test_load_save(self): rng = RandomNumberGenerator() with self.assertRaises(IOError): rng.loadState("does/not/exist") with TestAreaContext("rng_state") as t: rng.saveState( "rng.txt" ) t.sync() val1 = rng.getInt() val2 = rng.getInt() rng.loadState( "rng.txt" ) self.assertEqual( rng.getInt() , val1 ) self.assertEqual( rng.getInt() , val2 )
def test_rng_state(self): rng = RandomNumberGenerator() with self.assertRaises(ValueError): rng.setState("12") rng.setState("0123456789ABCDEF") val1 = rng.getInt() val2 = rng.getInt() self.assertFalse( val1 == val2 ) rng.setState("0123456789ABCDEF") self.assertEqual( rng.getInt() , val1) self.assertEqual( rng.getInt() , val2)
def test_matrix_copy_column(self): m = Matrix(10, 2) rng = RandomNumberGenerator(RngAlgTypeEnum.MZRAN, RngInitModeEnum.INIT_DEFAULT) m.randomInit(rng) with self.assertRaises(ValueError): m.copyColumn(0, 2) with self.assertRaises(ValueError): m.copyColumn(2, 0) with self.assertRaises(ValueError): m.copyColumn(-2, 0) m.copyColumn(1, 0) for i in range(m.rows()): self.assertEqual(m[i, 0], m[i, 1])
def test_sub_copy(self): m1 = Matrix(3, 3) rng = RandomNumberGenerator(RngAlgTypeEnum.MZRAN, RngInitModeEnum.INIT_DEFAULT) m1.randomInit(rng) with self.assertRaises(ValueError): m2 = m1.subCopy(0, 0, 4, 2) with self.assertRaises(ValueError): m2 = m1.subCopy(0, 0, 2, 4) with self.assertRaises(ValueError): m2 = m1.subCopy(4, 0, 1, 1) with self.assertRaises(ValueError): m2 = m1.subCopy(0, 2, 1, 2) m2 = m1.subCopy(0, 0, 2, 2) for i in range(2): for j in range(2): self.assertEqual(m1[i, j], m2[i, j])
def createAnalysisModule(self): rng = RandomNumberGenerator(RngAlgTypeEnum.MZRAN, RngInitModeEnum.INIT_DEFAULT) return AnalysisModule(lib_name=self.libname)
def test_matrix_random_init(self): m = Matrix(10, 10) rng = RandomNumberGenerator(RngAlgTypeEnum.MZRAN, RngInitModeEnum.INIT_DEFAULT) m.randomInit(rng)
def test_rng_default(self): rng = RandomNumberGenerator() self.assertIsInstance(rng.getDouble(), float)