def test_init(self): """Test project.Project()""" if self.has_projects: # ZrO test project construction proj = project.Project(self.ZrO_dir, verbose=False) self.assertEqual(proj.path, self.ZrO_dir) self.assertEqual(proj.name, "ZrO")
def test_init(self): """Test project.Selection()""" if self.has_projects: # ZrO test project construction proj = project.Project(self.ZrO_dir, verbose=False) sel = project.Selection(proj) self.assertIs(sel.proj, proj) self.assertEqual(sel.path, "MASTER") self.assertEqual(sel.all, True)
def test_composition_axes(self): """Test CompositionAxes""" if self.has_projects: proj = project.Project(self.ZrO_dir, verbose=False) comp_axes = proj.composition_axes self.assertIs(proj.composition_axes, comp_axes) self.assertEqual(comp_axes.name, '0') self.assertEqual(comp_axes.components, ['Zr', 'Va', 'O']) self.assertEqual(comp_axes.n_independent_compositions, 1) self.assertEqual(comp_axes.mol_formula, 'Zr(2)Va(2-2a)O(2a)') self.assertEqual(comp_axes.param_formula, 'a(0.5-0.25Va+0.25O)') self.assertTrue( np.allclose(comp_axes.end_members['origin'], np.array([2., 2., 0.]))) self.assertTrue( np.allclose(comp_axes.end_members['a'], np.array([2., 0., 2.])))
def test_prim(self): """Test Prim""" if self.has_projects: proj = project.Project(self.ZrO_dir, verbose=False) prim = proj.prim self.assertIs(prim.proj, proj) self.assertIs(proj.prim, prim) self.assertTrue( np.allclose( prim.lattice_matrix, np.array([[3.23398686, -1.61699343, -0.], [0., 2.80071477, 0.], [0., 0., 5.16867834]]))) self.assertAlmostEqual(prim.lattice_parameters['a'], 3.23398686, places=6) self.assertAlmostEqual(prim.lattice_parameters['b'], 3.23398686, places=6) self.assertAlmostEqual(prim.lattice_parameters['c'], 5.16867834, places=6) self.assertAlmostEqual(prim.lattice_parameters['alpha'], 90., places=6) self.assertAlmostEqual(prim.lattice_parameters['beta'], 90., places=6) self.assertAlmostEqual(prim.lattice_parameters['gamma'], 120., places=6) #print prim.basis self.assertEqual(prim.coordinate_mode, 'Fractional') self.assertEqual(prim.lattice_symmetry_s, 'D6h') self.assertEqual(prim.lattice_symmetry_hm, '6/mmm') self.assertEqual(prim.lattice_system, 'hexagonal') self.assertEqual(prim.crystal_symmetry_s, 'D6h') self.assertEqual(prim.crystal_symmetry_hm, '6/mmm') self.assertEqual(prim.crystal_system, 'hexagonal') self.assertEqual(prim.crystal_family, 'hexagonal') self.assertEqual(prim.space_group_number, '191:194') self.assertEqual(prim.components, ['Zr', 'Va', 'O']) self.assertEqual(prim.elements, ['Zr', 'Va', 'O']) self.assertEqual(prim.n_independent_compositions, 1) self.assertEqual(prim.degrees_of_freedom, ['occupation'])
def test_save(self): """Test Selection.save, Selection.saveas""" if self.has_projects: # ZrO test project construction proj = project.Project(self.ZrO_dir, verbose=False) proj.capture("select --set-on") # we'll set 'selected' based on the scel_size Nconfig = 336 sel = project.Selection(proj) self.assertEqual(sel.data.shape, (Nconfig, 2)) self.assertEqual(sel.data['selected'].sum(), Nconfig) sel.query(['scel_size']) sel.data.loc[:, 'selected'] = sel.data['scel_size'] < 3 self.assertEqual(sel.data['selected'].sum(), 13) # saveas new selection file test_select = join(proj.path, 'test_select') if os.path.isfile(test_select): os.remove(test_select) new_sel = sel.saveas(test_select) self.assertEqual(new_sel.data.shape, (336, 3)) self.assertEqual(new_sel.data['selected'].sum(), 13) # saveas an existing file should raise self.assertRaises(Exception, lambda x: sel.saveas(test_select)) # then save the original selection back to the MASTER list sel.save() # then reload and check read_sel = project.Selection(proj, all=False) self.assertEqual(read_sel.data.shape, (13, 2)) # reload 'new_sel' and check read_new_sel = project.Selection(proj, test_select, all=False) self.assertEqual(read_new_sel.data.shape, (13, 3)) self.assertEqual(read_new_sel.data['selected'].sum(), 13) if os.path.isfile(test_select): os.remove(test_select)
def test_data(self): """Test Selection.data""" if self.has_projects: # ZrO test project construction proj = project.Project(self.ZrO_dir, verbose=False) proj.capture("select --set-on") sel = project.Selection(proj) Nconfig = 336 self.assertEqual(sel.all, True) self.assertEqual(sel.data.shape, (Nconfig, 2)) self.assertEqual(sel.data.columns[0], 'configname') self.assertEqual(sel.data.columns[1], 'selected') self.assertEqual(sel.data.dtypes[0], 'object') self.assertEqual(sel.data.dtypes[1], 'bool') self.assertEqual(list(sel.data['selected']), [True] * Nconfig) # selections are not automatically updated proj.capture("select --set-off") self.assertEqual(list(sel.data['selected']), [True] * Nconfig) # and query will not overwrite existing data sel.query(["selected"]) self.assertEqual(list(sel.data['selected']), [True] * Nconfig) # unless you query w/force sel.query(["selected"], force=True) self.assertEqual(list(sel.data['selected']), [False] * Nconfig) # this will add a column sel.query(['comp']) self.assertEqual(sel.data.shape, (Nconfig, 3)) self.assertEqual(sel.data.columns[2], 'comp(a)') self.assertEqual(sel.data.dtypes[2], 'float64') # Check 'all' constructor arg proj.capture("select --set 'lt(scel_size,3)'") sel = project.Selection(proj, all=True) self.assertEqual(sel.data.shape, (Nconfig, 2)) sel = project.Selection(proj, all=False) self.assertEqual(sel.data.shape, (13, 2))
def test_checkhull(self): """Test casm-learn --exRFE""" if self.has_projects: # ZrO test project construction proj = project.Project(self.ZrO_dir, verbose=False) # files and directories tmp_dir = join(proj.path, '.casm', 'tmp') fit_dir = join(proj.path, 'fit') fit_RFE = join(fit_dir, 'fit_RFE.json') default_bset = join(self.ZrO_dir, 'basis_sets', 'bset.default') default_bspecs = join(default_bset, 'bspecs.json') test_bset = join(self.ZrO_dir, 'basis_sets', 'bset.test') test_bspecs = join(test_bset, 'bspecs.json') test_clex = join(self.ZrO_dir, 'cluster_expansions', 'clex.formation_energy', 'calctype.default', 'ref.default', 'bset.test') test_eci = join(test_clex, 'eci.test', 'eci.json') # setup def _clean(): if os.path.exists(tmp_dir): shutil.rmtree(tmp_dir) if os.path.exists(fit_dir): shutil.rmtree(fit_dir) if os.path.exists(test_bset): shutil.rmtree(test_bset) if os.path.exists(test_clex): shutil.rmtree(test_clex) _clean() os.mkdir(fit_dir) # test 'casm-learn --exREF' testargs = ["casm-learn", "--exRFE"] with contexts.captured_output(wd=fit_dir) as (sout, serr): with contexts.patch.object(sys, 'argv', testargs): casm_learn.main() input = json.loads(sout.getvalue()) self.assertEqual(input['feature_selection']['method'], 'RFE') self.assertEqual(input['estimator']['method'], 'LinearRegression') # set random_state for CV score input['problem_specs']['cv']['kwargs']['random_state'] = 0 # have 'checkhull' only checking training configs input['checkhull'] = {'selection': 'train', 'write_results': True} # save 'fit_RFE.json' with open(fit_RFE, 'wb') as f: f.write(six.u(json.dumps(input, indent=2)).encode('utf-8')) # create 'test' bset and eci proj.capture('settings --new-bset test') proj.capture('settings --new-eci test') shutil.copyfile(default_bspecs, test_bspecs) stdout, stderr, returncode = proj.capture('bset -u') #print("OK 1") # create 'train' selection with contexts.captured_output(wd=fit_dir) as (sout, serr): proj.capture( "select --set 'and(is_calculated,lt(comp(a),0.695))' -o train" ) #print("OK 2") # fit testargs = 'casm-learn -s fit_RFE.json --quiet'.split() #print testargs with contexts.captured_output(wd=fit_dir) as (sout, serr): with contexts.patch.object(sys, 'argv', testargs): casm_learn.main() self.assertTrue( os.path.exists(join(fit_dir, 'fit_RFE_halloffame.pkl'))) self.assertTrue(os.path.exists(join(fit_dir, 'fit_RFE_specs.pkl'))) #print("OK 3") # view hall of fame testargs = 'casm-learn -s fit_RFE.json --hall --quiet'.split() #print testargs with contexts.captured_output(wd=fit_dir) as (sout, serr): with contexts.patch.object(sys, 'argv', testargs): casm_learn.main() lines = sout.getvalue().splitlines() res = dict(zip(lines[0].split(), lines[2].split())) self.assertEqual(len(lines), 3) self.assertEqual(res['Selected'], '0111001101000000001101001001000100010010...') self.assertEqual(int(res['#Selected']), 25) self.assertAlmostEqual(float(res['CV']), 0.016540883) # depends on 'random_state' self.assertAlmostEqual(float(res['RMS']), 0.014697276) self.assertAlmostEqual(float(res['wRMS']), 0.014697276) self.assertEqual(res['Estimator'], 'LinearRegression') self.assertEqual(res['FeatureSelection'], 'RFE') #print("OK 4") # check data in pickle file hall = learn.open_halloffame( join(fit_dir, 'fit_RFE_halloffame.pkl')) self.assertEqual(len(hall), 1) # open input file and set default values input = learn.open_input(fit_RFE) #testargs = 'casm-learn -s fit_RFE.json --checkhull --indiv 0 --quiet'.split() #print testargs with contexts.working_dir(wd=fit_dir): learn.checkhull(input, hall, indices=[0], verbose=False) self.assertTrue( os.path.exists(join(fit_dir, 'checkhull_fit_RFE_0_dft_gs'))) self.assertTrue( os.path.exists(join(fit_dir, 'checkhull_fit_RFE_0_clex_gs')))