def test_save_to_binary_file(self): tmpfile = gf.generate_temp_file(extension='h5') CP._save_to_bin_file(filename=tmpfile, datasetname='d1', mtx=np.array([0.1, 0.2])) self.assertTrue(os.path.isfile(tmpfile), msg='File exists') os.remove(tmpfile)
def test_add_to_log(self): tmpfile = gf.generate_temp_file(extension='txt') CP._add_to_log(filename=tmpfile, logstr='hello world') self.assertTrue(os.path.isfile(tmpfile), msg='File exists') with open(tmpfile, 'r') as file: loadstr = file.read() self.assertEqual(loadstr, 'hello world', msg='Message should match') os.remove(tmpfile)
def test_save_to_text_file(self): tmpfile = gf.generate_temp_file(extension='txt') CP._save_to_txt_file(filename=tmpfile, mtx=np.array([0.1, 0.2])) self.assertTrue(os.path.isfile(tmpfile), msg='File exists') loadmtx = np.loadtxt(tmpfile) self.assertTrue(np.array_equal(loadmtx, np.array([0.1, 0.2])), msg=str('Arrays should match: {}'.format(loadmtx))) os.remove(tmpfile)
def test_readingbinaryfile_array(self): tmpfile = gf.generate_temp_file(extension='h5') CP._save_to_bin_file(filename=tmpfile, datasetname='d1', mtx=np.array([[0.1, 0.2]])) self.assertTrue(os.path.isfile(tmpfile), msg='File exists') out = CP.read_in_bin_file(filename=tmpfile) self.assertTrue(np.array_equal(out, np.array([[0.1, 0.2]])), msg=str('Expected array match: {}'.format(out))) os.remove(tmpfile)
def test_dump_to_json_file(self): RS = ResultsStructure() tmpfile = gf.generate_temp_file(extension='json') results = {'model': 4, 'data': np.random.random_sample(size=(1000, 2))} RS.save_json_object(results=results, filename=tmpfile) retres = RS.load_json_object(filename=tmpfile) self.assertEqual(retres['model'], results['model'], msg='Model set to 4') self.assertTrue(np.array_equal(retres['data'], results['data']), msg='Arrays should be equal') os.remove(tmpfile)
def test_id_chain_no_with_bad_items(self): parallel_dir = gf.generate_temp_folder() setup_problem(parallel_dir, case='binary') tmpfile = gf.generate_temp_file() os.mkdir(parallel_dir + os.sep + 'chain') if not os.path.exists(parallel_dir + os.sep + tmpfile): with open(parallel_dir + os.sep + tmpfile, 'w'): pass chainfolders = os.listdir(parallel_dir) chainfolders2 = CP.check_parallel_directory_contents( parallel_dir, chainfolders) self.assertEqual( chainfolders2, ['chain_0', 'chain_1', 'chain_2'], msg='List of strings do not match ({}): {} neq {}'.format( parallel_dir, chainfolders2, ['chain_0', 'chain_1', 'chain_2'])) shutil.rmtree(parallel_dir)
def test_run_simulation_with_json(self): mcstat = gf.basic_mcmc() mcstat.simulation_options.nsimu = 100 mcstat.simulation_options.save_to_bin = False mcstat.simulation_options.save_to_txt = False tmpfile = gf.generate_temp_file(extension='json') tmpfolder = gf.generate_temp_folder() os.mkdir(tmpfolder) mcstat.simulation_options.savedir = tmpfolder mcstat.simulation_options.results_filename = tmpfile mcstat.simulation_options.save_to_json = True mcstat.run_simulation() self.assertTrue(mcstat._mcmc_status, msg='Expect True if successfully run') check_these = ['mcmcplot', 'PI', 'chainstats'] for ci in check_these: self.assertTrue(hasattr(mcstat, ci), msg=str('object has attribute: {}'.format(ci))) shutil.rmtree(tmpfolder)