def test_bs_data_query(): op = BSDataQuery() q = op.query( path_type=BSPathType.setyawan_curtarolo, band_gap_min=0, band_gap_max=5, efermi_min=0, efermi_max=5, magnetic_ordering=Ordering.FM, is_gap_direct=True, is_metal=False, ) fields = [ "bandstructure.setyawan_curtarolo.band_gap", "bandstructure.setyawan_curtarolo.efermi", ] c = {field: {"$gte": 0, "$lte": 5} for field in fields} assert q == { "criteria": { "bandstructure.setyawan_curtarolo.magnetic_ordering": "FM", "bandstructure.setyawan_curtarolo.is_gap_direct": True, "bandstructure.setyawan_curtarolo.is_metal": False, **c, } } with ScratchDir("."): dumpfn(op, "temp.json") new_op = loadfn("temp.json") q = new_op.query( path_type=BSPathType.setyawan_curtarolo, band_gap_min=0, band_gap_max=5, efermi_min=0, efermi_max=5, magnetic_ordering=Ordering.FM, is_gap_direct=True, is_metal=False, ) c = {field: {"$gte": 0, "$lte": 5} for field in fields} assert q == { "criteria": { "bandstructure.setyawan_curtarolo.magnetic_ordering": "FM", "bandstructure.setyawan_curtarolo.is_gap_direct": True, "bandstructure.setyawan_curtarolo.is_metal": False, **c, } }
def test_feature_serialization(self): processed_cycler_run_path = os.path.join(TEST_FILE_DIR, PROCESSED_CYCLER_FILE) with ScratchDir('.'): os.environ['BEEP_ROOT'] = os.getcwd() pcycler_run = loadfn(processed_cycler_run_path) featurizer = DeltaQFastCharge.from_run(processed_cycler_run_path, os.getcwd(), pcycler_run) dumpfn(featurizer, featurizer.name) features_reloaded = loadfn(featurizer.name) self.assertIsInstance(features_reloaded, DeltaQFastCharge) # test nominal capacity is being generated self.assertEqual(features_reloaded.X.loc[0, 'nominal_capacity_by_median'], 1.0628421000000001)
def get_interpolated_dos(self, mat): nelect = mat["calc_settings"]["nelect"] bs_dict = mat["bandstructure"]["uniform_bs"] bs_dict["structure"] = mat['structure'] bs = BandStructure.from_dict(bs_dict) if bs.is_spin_polarized: with ScratchDir("."): BoltztrapRunner(bs=bs, nelec=nelect, run_type="DOS", dos_type="TETRA", spin=1, timeout=60).run(path_dir=os.getcwd()) an_up = BoltztrapAnalyzer.from_files("boltztrap/", dos_spin=1) with ScratchDir("."): BoltztrapRunner(bs=bs, nelec=nelect, run_type="DOS", dos_type="TETRA", spin=-1, timeout=60).run(path_dir=os.getcwd()) an_dw = BoltztrapAnalyzer.from_files("boltztrap/", dos_spin=-1) cdos = an_up.get_complete_dos(bs.structure, an_dw) else: with ScratchDir("."): BoltztrapRunner(bs=bs, nelec=nelect, run_type="DOS", dos_type="TETRA", timeout=60).run(path_dir=os.getcwd()) an = BoltztrapAnalyzer.from_files("boltztrap/") cdos = an.get_complete_dos(bs.structure) return cdos
def get_free_sphere_params(structure, rad_dict=None, probe_rad=0.1): """ Analyze the void space in the input structure using voronoi decomposition Calls Zeo++ for Voronoi decomposition. Args: structure: pymatgen.core.structure.Structure rad_dict (optional): Dictionary of radii of elements in structure. If not given, Zeo++ default values are used. Note: Zeo++ uses atomic radii of elements. For ionic structures, pass rad_dict with ionic radii probe_rad (optional): Sampling probe radius in Angstroms. Default is 0.1 A Returns: voronoi nodes as pymatgen.core.structure.Strucutre within the unit cell defined by the lattice of input structure voronoi face centers as pymatgen.core.structure.Strucutre within the unit cell defined by the lattice of input structure """ with ScratchDir("."): name = "temp_zeo1" zeo_inp_filename = name + ".cssr" ZeoCssr(structure).write_file(zeo_inp_filename) rad_file = None rad_flag = False if rad_dict: rad_file = name + ".rad" rad_flag = True with open(rad_file, "w+") as fp: for el in rad_dict.keys(): fp.write(f"{el} {rad_dict[el].real}\n") atmnet = AtomNetwork.read_from_CSSR(zeo_inp_filename, rad_flag=rad_flag, rad_file=rad_file) out_file = "temp.res" atmnet.calculate_free_sphere_parameters(out_file) if os.path.isfile(out_file) and os.path.getsize(out_file) > 0: with open(out_file) as fp: output = fp.readline() else: output = "" fields = [val.strip() for val in output.split()][1:4] if len(fields) == 3: fields = [float(field) for field in fields] free_sphere_params = { "inc_sph_max_dia": fields[0], "free_sph_max_dia": fields[1], "inc_sph_along_free_sph_path_max_dia": fields[2], } return free_sphere_params
def test_feature_generation_list_to_json(self): processed_cycler_run_path = os.path.join( TEST_FILE_DIR, "PreDiag_000240_000227_truncated_structure.json") with ScratchDir("."): os.environ["BEEP_PROCESSING_DIR"] = TEST_FILE_DIR # os.environ['BEEP_PROCESSING_DIR'] = os.getcwd() # Create dummy json obj json_obj = { "file_list": [processed_cycler_run_path, processed_cycler_run_path], "run_list": [0, 1], } json_string = json.dumps(json_obj) newjsonpaths = process_file_list_from_json( json_string, processed_dir=os.getcwd()) reloaded = json.loads(newjsonpaths) # Check that at least strings are output self.assertIsInstance(reloaded["file_list"][-1], str) # Ensure first is correct features_reloaded = loadfn(reloaded["file_list"][4]) self.assertIsInstance(features_reloaded, DeltaQFastCharge) self.assertEqual( features_reloaded.X.loc[0, "nominal_capacity_by_median"], 0.07114775279999999, ) features_reloaded = loadfn(reloaded["file_list"][-1]) self.assertIsInstance(features_reloaded, DiagnosticProperties) self.assertListEqual( list(features_reloaded.X.iloc[2, :]), [ 141, 0.9859837086597274, 7.885284043, 4.323121513988055, 21.12108276469096, 30, 100, 'reset', 'discharge_energy' ], ) # Workflow output output_file_path = Path(tempfile.gettempdir()) / "results.json" self.assertTrue(output_file_path.exists()) output_data = json.loads(output_file_path.read_text()) output_json = output_data[0] self.assertEqual(reloaded["file_list"][0], output_json["filename"]) self.assertEqual(os.path.getsize(output_json["filename"]), output_json["size"]) self.assertEqual(0, output_json["run_id"]) self.assertEqual("featurizing", output_json["action"]) self.assertEqual("success", output_json["status"])
def test_insertion_electrode_query(): op = WorkingIonQuery() q = op.query(working_ion="Li", ) assert q == {"criteria": {"working_ion": "Li"}} with ScratchDir("."): dumpfn(op, "temp.json") new_op = loadfn("temp.json") q = new_op.query(working_ion="Li", ) assert q == {"criteria": {"working_ion": "Li"}}
def test_generate_final_report(self): with ScratchDir('.'): df = pd.read_csv(os.path.join(CAMD_TEST_FILES, 'test_df.csv')) experiment_params = {"dataframe": df} # Construct and start loop new_loop = Loop(df, AgentStabilityML5, ATFSampler, AnalyzeStability, agent_params={}, create_seed=True, analyzer_params={}, experiment_params=experiment_params, ) new_loop.generate_report_plot( "report.png", os.path.join(CAMD_TEST_FILES, "report.log")) self.assertTrue(os.path.isfile("report.png"))
def test_write(self): s = Structure( Lattice( np.array([[3.16, 0.1, 0.2], [0.1, 3.17, 0.3], [0.1, 0.2, 3]])), ["Mo", "Mo"], [[0, 0, 0], [0.13, 0.4, 0.2]], ) with ScratchDir("."): write_data_from_structure(s, "test.data") lmp = LammpsData.from_file("test.data", atom_style="charge") lmp2 = LammpsData.from_structure(s) lmp2.write_file("test2.data") self.assertTrue(str(lmp) == str(lmp2))
def test_from_csv(self): csv_file = os.path.join(TEST_FILE_DIR, "parameter_test.csv") # Test basic functionality with ScratchDir('.') as scratch_dir: makedirs_p(os.path.join(scratch_dir, "procedures")) makedirs_p(os.path.join(scratch_dir, "names")) generate_protocol_files_from_csv(csv_file, scratch_dir) self.assertEqual( len(os.listdir(os.path.join(scratch_dir, "procedures"))), 3) # Test avoid overwriting file functionality with ScratchDir('.') as scratch_dir: makedirs_p(os.path.join(scratch_dir, "procedures")) makedirs_p(os.path.join(scratch_dir, "names")) dumpfn({"hello": "world"}, os.path.join("procedures", "name_000007.000")) generate_protocol_files_from_csv(csv_file, scratch_dir) post_file = loadfn(os.path.join("procedures", "name_000007.000")) self.assertEqual(post_file, {"hello": "world"}) self.assertEqual( len(os.listdir(os.path.join(scratch_dir, "procedures"))), 3)
def test_magnetic_query(): op = SearchMagneticQuery() assert op.query(ordering=Ordering.FiM) == {"criteria": {"ordering": "FiM"}} with ScratchDir("."): dumpfn(op, "temp.json") new_op = loadfn("temp.json") assert new_op.query(ordering=Ordering.FiM) == { "criteria": { "ordering": "FiM" } }
def test_is_stable_query(): op = SearchIsStableQuery() assert op.query(is_stable=True) == {"criteria": {"is_stable": True}} with ScratchDir("."): dumpfn(op, "temp.json") new_op = loadfn("temp.json") assert new_op.query(is_stable=True) == { "criteria": { "is_stable": True } }
def test_is_theoretical_query(): op = SearchIsTheoreticalQuery() assert op.query(theoretical=False) == {"criteria": {"theoretical": False}} with ScratchDir("."): dumpfn(op, "temp.json") new_op = loadfn("temp.json") assert new_op.query(theoretical=False) == { "criteria": { "theoretical": False } }
def get_tm_version(): """Get the turbomole version currently in use. This basically runs a define and exits at the very beginning and extracts the version from the header of the output. """ with ScratchDir('.'): define_quit() with open('define.log', 'r') as f: data = f.read() r = r'TURBOMOLE.*V([\d\.]+\d)\s+.*' match_version = re.search(r, data) return match_version.group(1)
def test_get_diffusion_coeff(self): with ScratchDir("."): structured_datapath_loc = os.path.join( TEST_FILE_DIR, "PreDiag_000240_000227_truncated_structure.json") structured_datapath = auto_load_processed(structured_datapath_loc) diffusion_df = featurizer_helpers.get_diffusion_coeff( structured_datapath, 1) print(np.round(diffusion_df.iloc[0].to_list(), 3)) self.assertEqual( np.round(diffusion_df.iloc[0].to_list(), 3)[0], -0.016) self.assertEqual( np.round(diffusion_df.iloc[0].to_list(), 3)[5], -0.011)
def test_postprocess(self): # test gzipped and zipping of additional files with cd(os.path.join(test_files_lobster3)): with ScratchDir(".", copy_from_current_on_enter=True) as d: shutil.copy("lobsterin", "lobsterin.orig") v = LobsterJob("hello", gzipped=True, add_files_to_gzip=VASP_OUTPUT_FILES) v.postprocess() self.assertTrue(os.path.exists("WAVECAR.gz")) self.assertTrue(os.path.exists("lobsterin.gz")) self.assertTrue(os.path.exists("lobsterout.gz")) self.assertTrue(os.path.exists("INCAR.gz")) self.assertTrue(os.path.exists("lobsterin.orig.gz")) with ScratchDir(".", copy_from_current_on_enter=True) as d: shutil.copy("lobsterin", "lobsterin.orig") v = LobsterJob("hello", gzipped=False, add_files_to_gzip=VASP_OUTPUT_FILES) v.postprocess() self.assertTrue(os.path.exists("WAVECAR")) self.assertTrue(os.path.exists("lobsterin")) self.assertTrue(os.path.exists("lobsterout")) self.assertTrue(os.path.exists("INCAR")) self.assertTrue(os.path.exists("lobsterin.orig"))
def test_run(self): with ScratchDir(".") as d: for f in ["INCAR", "POSCAR", "POTCAR", "KPOINTS"]: shutil.copy(os.path.join(test_dir, f), f) oldincar = Incar.from_file("INCAR") v = GenerateVaspInputJob("pymatgen.io.vasp.sets.MPNonSCFSet", contcar_only=False) v.run() incar = Incar.from_file("INCAR") self.assertEqual(incar["ICHARG"], 11) self.assertEqual(oldincar["ICHARG"], 1) kpoints = Kpoints.from_file("KPOINTS") self.assertEqual(str(kpoints.style), "Reciprocal")
def test_feature_generation_full_model(self): processed_cycler_run_path = os.path.join(TEST_FILE_DIR, PROCESSED_CYCLER_FILE) with ScratchDir("."): os.environ["BEEP_PROCESSING_DIR"] = os.getcwd() pcycler_run = loadfn(processed_cycler_run_path) featurizer = DeltaQFastCharge.from_run(processed_cycler_run_path, os.getcwd(), pcycler_run) self.assertEqual(len(featurizer.X), 1) # just test if works for now # Ensure no NaN values self.assertFalse(np.any(featurizer.X.isnull()))
def test_chemsys_query(): op = ChemsysQuery() assert op.query(chemsys="Fe-Bi-O") == {"criteria": {"chemsys": "Bi-Fe-O"}} with ScratchDir("."): dumpfn(op, "temp.json") new_op = loadfn("temp.json") assert new_op.query(chemsys="Fe-Bi-O") == { "criteria": { "chemsys": "Bi-Fe-O" } }
def test_to_file(self): filename = 'BCS - 171.64.160.115_Ta19_ourprotocol_gdocSEP2019_CC7.mps' bcs = Settings.from_file(os.path.join(BIOLOGIC_TEMPLATE_DIR, filename)) test_name = 'test.mps' with ScratchDir('.'): bcs.to_file(test_name) original = open(os.path.join(BIOLOGIC_TEMPLATE_DIR, filename), encoding='ISO-8859-1').readlines() parsed = open(test_name, encoding='ISO-8859-1').readlines() udiff = list(difflib.unified_diff(original, parsed)) for line in udiff: print(line) self.assertFalse(udiff)
def test_serialization(self): smaller_run = ArbinDatapath.from_file(self.bad_file) with ScratchDir("."): dumpfn(smaller_run, "smaller_cycler_run.json") resurrected = loadfn("smaller_cycler_run.json") self.assertIsInstance(resurrected, BEEPDatapath) self.assertIsInstance(resurrected.raw_data, pd.DataFrame) self.assertEqual( smaller_run.raw_data.voltage.to_list(), resurrected.raw_data.voltage.to_list() ) self.assertEqual( smaller_run.raw_data.current.to_list(), resurrected.raw_data.current.to_list() )
def test_setup(self): with cd(test_dir): with ScratchDir('.', copy_from_current_on_enter=True): f = FeffJob("hello", backup=True) f.setup() parameter = Tags.from_file('feff.inp') parameter_orig = Tags.from_file('feff.inp.orig') self.assertEqual(parameter, parameter_orig) atom = Atoms.cluster_from_file('feff.inp') atom_origin = Atoms.cluster_from_file('feff.inp.orig') self.assertEqual(atom, atom_origin)
def test_piezo_query(): op = PiezoelectricQuery() assert op.query(piezo_modulus_min=0, piezo_modulus_max=5) == { "criteria": {"piezo.e_ij_max": {"$gte": 0, "$lte": 5}} } with ScratchDir("."): dumpfn(op, "temp.json") new_op = loadfn("temp.json") assert new_op.query(piezo_modulus_min=0, piezo_modulus_max=5) == { "criteria": {"piezo.e_ij_max": {"$gte": 0, "$lte": 5}} }
def test_chgcar_test_id_query(): op = ChgcarTaskIDQuery() assert op.query(task_ids="mp-149, mp-13") == { "criteria": {"task_id": {"$in": ["mp-149", "mp-13"]}} } with ScratchDir("."): dumpfn(op, "temp.json") new_op = loadfn("temp.json") assert new_op.query(task_ids="mp-149, mp-13") == { "criteria": {"task_id": {"$in": ["mp-149", "mp-13"]}} }
def test_mocomplete_get_query(): op = MPCompleteGetQuery() assert op.query(public_name="Test Test", public_email="*****@*****.**",) == { "criteria": {"public_name": "Test Test", "public_email": "*****@*****.**"} } with ScratchDir("."): dumpfn(op, "temp.json") new_op = loadfn("temp.json") assert new_op.query(public_name="Test Test", public_email="*****@*****.**",) == { "criteria": {"public_name": "Test Test", "public_email": "*****@*****.**"} }
def test_molecule_base_query(): op = MoleculeBaseQuery() q = op.query( nelements_min=0, nelements_max=5, EA_min=0, EA_max=5, IE_min=0, IE_max=5, charge_min=0, charge_max=5, pointgroup="C3v", smiles="CN=C=O", ) fields = [ "nelements", "EA", "IE", "charge", ] c = {field: {"$gte": 0, "$lte": 5} for field in fields} assert q == {"criteria": {"pointgroup": "C3v", "smiles": "CN=C=O", **c}} with ScratchDir("."): dumpfn(op, "temp.json") new_op = loadfn("temp.json") q = new_op.query( nelements_min=0, nelements_max=5, EA_min=0, EA_max=5, IE_min=0, IE_max=5, charge_min=0, charge_max=5, pointgroup="C3v", smiles="CN=C=O", ) c = {field: {"$gte": 0, "$lte": 5} for field in fields} assert q == { "criteria": { "pointgroup": "C3v", "smiles": "CN=C=O", **c } }
def test_generate_maccor_waveform_file_custom(self): velocity_waveform_file = os.path.join(TEST_FILE_DIR, "US06_velocity_waveform.txt") mwf_config = { 'control_mode': 'I', 'value_scale': 1, 'charge_limit_mode': 'R', 'charge_limit_value': 2, 'discharge_limit_mode': 'P', 'discharge_limit_value': 3, 'charge_end_mode': 'V', 'charge_end_operation': '>=', 'charge_end_mode_value': 4.2, 'discharge_end_mode': 'V', 'discharge_end_operation': '<=', 'discharge_end_mode_value': 3, 'report_mode': 'T', 'report_value': 10, 'range': 'A', } with ScratchDir('.') as scratch_dir: df_power = convert_velocity_to_power_waveform( velocity_waveform_file, 'mph') df_MWF = pd.read_csv(generate_maccor_waveform_file( df_power, "test_US06_waveform", scratch_dir, mwf_config=mwf_config), sep='\t', header=None) df_MWF_ref = pd.read_csv(os.path.join( TEST_FILE_DIR, "US06_reference_custom_settings.mwf"), sep="\t", header=None) # Check dimensions with the reference mwf file self.assertEqual(df_MWF.shape, df_MWF_ref.shape) # Check that control_mode, charge/discharge state, limit and limit_value columns are identical. self.assertTrue( (df_MWF.iloc[:, [0, 1, 3, 4]] == df_MWF_ref.iloc[:, [0, 1, 3, 4]] ).all().all()) # Check that power values are close to each other (col 2) relative_differences = np.abs( (df_MWF.iloc[:, 2] - df_MWF_ref.iloc[:, 2]) / df_MWF_ref.iloc[:, 2]) self.assertLessEqual(np.mean(relative_differences) * 100, 0.01) # mean percentage error < 0.01%
def test_get_vbm_bandgap(self): with ScratchDir("."): os.mkdir("bulk") #first check the direct vasprun load method copyfile(os.path.join(pmgtestfiles_loc, "vasprun.xml"), "bulk/vasprun.xml") pp = PostProcess(".") (testvbm, testgap) = pp.get_vbm_bandgap() self.assertAlmostEqual(testvbm, 1.5516, places=3) self.assertAlmostEqual(testgap, 2.5390, places=3) #secondly check a band gap pull pp = PostProcess(".", mpid="mp-2534") #GaAs mpid (testvbm_mpid, testgap_mpid) = pp.get_vbm_bandgap() self.assertAlmostEqual(testvbm_mpid, 2.6682, places=3) self.assertAlmostEqual(testgap_mpid, 0.1887, places=3)
def test_serialization(self): # Test invocation from string method builder = PropertyWorkflowBuilder( self.elasticity, self.materials, "emmet.workflows.property_workflows.generate_elastic_workflow", lpad=self.lpad) serialized = builder.as_dict() new = PropertyWorkflowBuilder.from_dict(serialized) self.assertEqual(new._wf_function_string, "emmet.workflows.property_workflows.generate_elastic_workflow") with ScratchDir('.'): dumpfn(builder, "builder.json") new_builder = loadfn("builder.json") self.assertTrue(isinstance(new_builder, Builder))
def test_all_filenames(self): """Test to see if renaming works on all filenames""" files = loadfn(os.path.join(TEST_FILE_DIR, "test_filenames.json")) with ScratchDir("."): os.environ["BEEP_PROCESSING_DIR"] = os.getcwd() os.mkdir("data-share") os.mkdir(os.path.join("data-share", "raw_cycler_files")) os.mkdir(os.path.join("data-share", "renamed_cycler_files")) # filter for only files with protocol in name files = [file for file in files if "batch8" not in file] for filename in files: Path(os.path.join("data-share", "raw_cycler_files", filename)).touch() process_files_json() pass # to exit scratch dir context
def test_generate_dQdV_peak_fits(self): processed_cycler_run_path = os.path.join( TEST_FILE_DIR, "PreDiag_000304_000153_truncated_structure.json") with ScratchDir("."): os.environ["BEEP_PROCESSING_DIR"] = TEST_FILE_DIR pcycler_run = loadfn(processed_cycler_run_path) peaks_df = featurizer_helpers.generate_dQdV_peak_fits( pcycler_run, 'rpt_0.2C', 0, 1, plotting_y_n=1) print(len(peaks_df.columns)) self.assertEqual(peaks_df.columns.tolist(), [ 'm0_Amp_rpt_0.2C_1', 'm0_Mu_rpt_0.2C_1', 'm1_Amp_rpt_0.2C_1', 'm1_Mu_rpt_0.2C_1', 'm2_Amp_rpt_0.2C_1', 'm2_Mu_rpt_0.2C_1', 'trough_height_0_rpt_0.2C_1', 'trough_height_1_rpt_0.2C_1' ])