Skip to content

Commit

Permalink
fix alchemy and anlysis
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielYang59 committed Dec 6, 2024
1 parent 3f7b180 commit 7ce8014
Show file tree
Hide file tree
Showing 18 changed files with 46 additions and 46 deletions.
4 changes: 2 additions & 2 deletions tests/alchemy/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def test_as_from_dict(self):

class TestRemoveDuplicatesFilter(TestCase):
def setUp(self):
with open(f"{TEST_FILES_DIR}/entries/TiO2_entries.json") as file:
with open(f"{TEST_FILES_DIR}/entries/TiO2_entries.json", encoding="utf-8") as file:
entries = json.load(file, cls=MontyDecoder)
self._struct_list = [entry.structure for entry in entries]
self._sm = StructureMatcher()
Expand All @@ -91,7 +91,7 @@ def test_as_from_dict(self):

class TestRemoveExistingFilter(TestCase):
def setUp(self):
with open(f"{TEST_FILES_DIR}/entries/TiO2_entries.json") as file:
with open(f"{TEST_FILES_DIR}/entries/TiO2_entries.json", encoding="utf-8") as file:
entries = json.load(file, cls=MontyDecoder)
self._struct_list = [entry.structure for entry in entries]
self._sm = StructureMatcher()
Expand Down
2 changes: 1 addition & 1 deletion tests/alchemy/test_materials.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def test_final_structure(self):
assert isinstance(deepcopy(self.trans), TransformedStructure)

def test_from_dict(self):
with open(f"{TEST_DIR}/transformations.json") as file:
with open(f"{TEST_DIR}/transformations.json", encoding="utf-8") as file:
dct = json.load(file)
dct["other_parameters"] = {"tags": ["test"]}
t_struct = TransformedStructure.from_dict(dct)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,7 @@ def test_real_systems(self):

def test_coordination_sequences(self):
BaTiO3_se_fpath = f"{TEST_FILES_DIR}/analysis/chemenv/structure_environments/se_mp-5020.json"
with open(BaTiO3_se_fpath) as file:
with open(BaTiO3_se_fpath, encoding="utf-8") as file:
dct = json.load(file)
struct_envs = StructureEnvironments.from_dict(dct)
lse = LightStructureEnvironments.from_structure_environments(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
class TestStructureConnectivity(PymatgenTest):
def test_serialization(self):
BaTiO3_se_fpath = f"{TEST_FILES_DIR}/analysis/chemenv/structure_environments/se_mp-5020.json"
with open(BaTiO3_se_fpath) as file:
with open(BaTiO3_se_fpath, encoding="utf-8") as file:
dd = json.load(file)
struct_envs = StructureEnvironments.from_dict(dd)
lse = LightStructureEnvironments.from_structure_environments(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def _strategy_test(self, strategy):

for json_file in files:
with self.subTest(json_file=json_file):
with open(f"{json_dir}/{json_file}") as file:
with open(f"{json_dir}/{json_file}", encoding="utf-8") as file:
dct = json.load(file)

atom_indices = dct["atom_indices"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def setUpClass(cls):
cls.lgf.setup_parameters(centering_type="standard")

def test_read_write_structure_environments(self):
with open(f"{json_dir}/test_T--4_FePO4_icsd_4266.json") as file:
with open(f"{json_dir}/test_T--4_FePO4_icsd_4266.json", encoding="utf-8") as file:
dd = json.load(file)

atom_indices = dd["atom_indices"]
Expand All @@ -51,7 +51,7 @@ def test_read_write_structure_environments(self):
with open(f"{self.tmp_path}/se.json", mode="w") as file:
json.dump(se.as_dict(), file)

with open(f"{self.tmp_path}/se.json") as file:
with open(f"{self.tmp_path}/se.json", encoding="utf-8") as file:
dd = json.load(file)

se2 = StructureEnvironments.from_dict(dd)
Expand All @@ -70,13 +70,13 @@ def test_read_write_structure_environments(self):
default=lambda obj: getattr(obj, "tolist", lambda: obj)(),
)

with open(f"{self.tmp_path}/lse.json") as file:
with open(f"{self.tmp_path}/lse.json", encoding="utf-8") as file:
LightStructureEnvironments.from_dict(json.load(file))

# assert lse == lse2

def test_structure_environments_neighbors_sets(self):
with open(f"{struct_env_dir}/se_mp-7000.json") as file:
with open(f"{struct_env_dir}/se_mp-7000.json", encoding="utf-8") as file:
dct = json.load(file)

struct_envs = StructureEnvironments.from_dict(dct)
Expand Down Expand Up @@ -221,7 +221,7 @@ def test_strategies(self):
assert multi_weights_strategy_2 != multi_weights_strategy_3

def test_read_write_voronoi(self):
with open(f"{json_dir}/test_T--4_FePO4_icsd_4266.json") as file:
with open(f"{json_dir}/test_T--4_FePO4_icsd_4266.json", encoding="utf-8") as file:
dd = json.load(file)

struct = Structure.from_dict(dd["structure"])
Expand All @@ -230,10 +230,10 @@ def test_read_write_voronoi(self):

detailed_voronoi_container = DetailedVoronoiContainer(structure=struct, valences=valences)

with open(f"{self.tmp_path}/se.json", mode="w") as file:
with open(f"{self.tmp_path}/se.json", mode="w", encoding="utf-8") as file:
json.dump(detailed_voronoi_container.as_dict(), file)

with open(f"{self.tmp_path}/se.json") as file:
with open(f"{self.tmp_path}/se.json", encoding="utf-8") as file:
dd = json.load(file)

detailed_voronoi_container2 = DetailedVoronoiContainer.from_dict(dd)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

class TestStructureEnvironments(PymatgenTest):
def test_structure_environments(self):
with open(f"{TEST_DIR}/se_mp-7000.json") as file:
with open(f"{TEST_DIR}/se_mp-7000.json", encoding="utf-8") as file:
dct = json.load(file)

struct_envs = StructureEnvironments.from_dict(dct)
Expand Down Expand Up @@ -143,7 +143,7 @@ def test_structure_environments(self):
assert ce != ce2

def test_light_structure_environments(self):
with open(f"{TEST_DIR}/se_mp-7000.json") as file:
with open(f"{TEST_DIR}/se_mp-7000.json", encoding="utf-8") as file:
dct = json.load(file)

struct_envs = StructureEnvironments.from_dict(dct)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ def test_CN_bias_weight(self):

def test_self_csms_weight(self):
# Get the StructureEnvironments for K2NaNb2Fe7Si8H4O31 (mp-743972)
with open(f"{struct_env_dir}/se_mp-743972.json") as file:
with open(f"{struct_env_dir}/se_mp-743972.json", encoding="utf-8") as file:
dct = json.load(file)
struct_envs = StructureEnvironments.from_dict(dct)

Expand Down Expand Up @@ -316,7 +316,7 @@ def test_self_csms_weight(self):
assert abs(self_w - 0.14204073172729198) < 1e-8

# Get the StructureEnvironments for SiO2 (mp-7000)
with open(f"{struct_env_dir}/se_mp-7000.json") as file:
with open(f"{struct_env_dir}/se_mp-7000.json", encoding="utf-8") as file:
dct = json.load(file)
struct_envs = StructureEnvironments.from_dict(dct)

Expand Down Expand Up @@ -360,7 +360,7 @@ def test_self_csms_weight(self):

def test_delta_csms_weight(self):
# Get the StructureEnvironments for K2NaNb2Fe7Si8H4O31 (mp-743972)
with open(f"{struct_env_dir}/se_mp-743972.json") as file:
with open(f"{struct_env_dir}/se_mp-743972.json", encoding="utf-8") as file:
dct = json.load(file)
struct_envs = StructureEnvironments.from_dict(dct)

Expand Down Expand Up @@ -492,7 +492,7 @@ def test_delta_csms_weight(self):
assert abs(delta_w - 0.103515625) < 1e-8

# Get the StructureEnvironments for SiO2 (mp-7000)
with open(f"{struct_env_dir}/se_mp-7000.json") as file:
with open(f"{struct_env_dir}/se_mp-7000.json", encoding="utf-8") as file:
dct = json.load(file)
struct_envs = StructureEnvironments.from_dict(dct)

Expand Down
10 changes: 5 additions & 5 deletions tests/analysis/elasticity/test_elastic.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ def setUp(self):

self.elastic_tensor_1 = ElasticTensor(self.ft)
filepath = f"{TEST_DIR}/Sn_def_stress.json"
with open(filepath) as file:
with open(filepath, encoding="utf-8") as file:
self.def_stress_dict = json.load(file)
with open(f"{TEST_DIR}/test_toec_data.json") as file:
with open(f"{TEST_DIR}/test_toec_data.json", encoding="utf-8") as file:
self.toec_dict = json.load(file)
self.structure = self.get_structure("Sn")

Expand Down Expand Up @@ -265,7 +265,7 @@ def test_energy_density(self):

class TestElasticTensorExpansion(PymatgenTest):
def setUp(self):
with open(f"{TEST_DIR}/test_toec_data.json") as file:
with open(f"{TEST_DIR}/test_toec_data.json", encoding="utf-8") as file:
self.data_dict = json.load(file)
self.strains = [Strain(sm) for sm in self.data_dict["strains"]]
self.pk_stresses = [Stress(d) for d in self.data_dict["pk_stresses"]]
Expand Down Expand Up @@ -366,7 +366,7 @@ def test_get_yield_stress(self):

class TestNthOrderElasticTensor(PymatgenTest):
def setUp(self):
with open(f"{TEST_DIR}/test_toec_data.json") as file:
with open(f"{TEST_DIR}/test_toec_data.json", encoding="utf-8") as file:
self.data_dict = json.load(file)
self.strains = [Strain(sm) for sm in self.data_dict["strains"]]
self.pk_stresses = [Stress(d) for d in self.data_dict["pk_stresses"]]
Expand Down Expand Up @@ -405,7 +405,7 @@ class TestDiffFit(PymatgenTest):
"""Test various functions related to diff fitting."""

def setUp(self):
with open(f"{TEST_DIR}/test_toec_data.json") as file:
with open(f"{TEST_DIR}/test_toec_data.json", encoding="utf-8") as file:
self.data_dict = json.load(file)
self.strains = [Strain(sm) for sm in self.data_dict["strains"]]
self.pk_stresses = [Stress(d) for d in self.data_dict["pk_stresses"]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def get_table():
default lambda table.
"""
json_path = f"{TEST_DIR}/test_lambda.json"
with open(json_path) as file:
with open(json_path, encoding="utf-8") as file:
return json.load(file)


Expand Down
2 changes: 1 addition & 1 deletion tests/analysis/structure_prediction/test_substitutor.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def get_table():
default lambda table.
"""
json_path = f"{TEST_DIR}/test_lambda.json"
with open(json_path) as file:
with open(json_path, encoding="utf-8") as file:
return json.load(file)


Expand Down
2 changes: 1 addition & 1 deletion tests/analysis/test_graphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def setUp(self):
# MoS2 example, structure graph obtained from critic2
# (not ground state, from mp-1023924, single layer)
stdout_file = f"{TEST_FILES_DIR}/command_line/critic2/MoS2_critic2_stdout.txt"
with open(stdout_file) as txt_file:
with open(stdout_file, encoding="utf-8") as txt_file:
reference_stdout = txt_file.read()
self.structure = Structure.from_file(f"{TEST_FILES_DIR}/command_line/critic2/MoS2.cif")
c2o = Critic2Analysis(self.structure, reference_stdout)
Expand Down
4 changes: 2 additions & 2 deletions tests/analysis/test_molecule_matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,12 @@ def test_get_rmsd(self):
@pytest.mark.skipif(platform.system() == "Windows", reason="Tests for openbabel failing on Win")
def test_group_molecules(self):
mol_matcher = MoleculeMatcher(tolerance=0.001)
with open(f"{TEST_DIR}/mol_list.txt") as file:
with open(f"{TEST_DIR}/mol_list.txt", encoding="utf-8") as file:
filename_list = [line.strip() for line in file]
mol_list = [Molecule.from_file(f"{TEST_DIR}/{file}") for file in filename_list]
mol_groups = mol_matcher.group_molecules(mol_list)
filename_groups = [[filename_list[mol_list.index(m)] for m in g] for g in mol_groups]
with open(f"{TEST_DIR}/grouped_mol_list.txt") as file:
with open(f"{TEST_DIR}/grouped_mol_list.txt", encoding="utf-8") as file:
grouped_text = file.read().strip()
assert str(filename_groups) == grouped_text

Expand Down
2 changes: 1 addition & 1 deletion tests/analysis/test_structure_matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

class TestStructureMatcher(PymatgenTest):
def setUp(self):
with open(f"{TEST_FILES_DIR}/entries/TiO2_entries.json") as file:
with open(f"{TEST_FILES_DIR}/entries/TiO2_entries.json", encoding="utf-8") as file:
entries = json.load(file, cls=MontyDecoder)
self.struct_list = [ent.structure for ent in entries]
self.oxi_structs = [
Expand Down
16 changes: 8 additions & 8 deletions tests/analysis/test_surface_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

class TestSlabEntry(PymatgenTest):
def setUp(self):
with open(f"{TEST_DIR}/ucell_entries.txt") as file:
with open(f"{TEST_DIR}/ucell_entries.txt", encoding="utf-8") as file:
ucell_entries = json.loads(file.read())
self.ucell_entries = ucell_entries

Expand Down Expand Up @@ -58,7 +58,7 @@ def test_properties(self):
assert ads.Nsurfs_ads_in_slab == 1

# Determine the correct binding energy
with open(f"{TEST_DIR}/isolated_O_entry.txt") as txt_file:
with open(f"{TEST_DIR}/isolated_O_entry.txt", encoding="utf-8") as txt_file:
isolated_O_entry = json.loads(txt_file.read())
O_cse = ComputedStructureEntry.from_dict(isolated_O_entry)
g_bind = (ads.energy - ml * clean.energy) / Nads - O_cse.energy_per_atom
Expand Down Expand Up @@ -122,7 +122,7 @@ class TestSurfaceEnergyPlotter(PymatgenTest):
def setUp(self):
entry_dict = get_entry_dict(f"{TEST_DIR}/Cu_entries.txt")
self.Cu_entry_dict = entry_dict
with open(f"{TEST_DIR}/ucell_entries.txt") as file:
with open(f"{TEST_DIR}/ucell_entries.txt", encoding="utf-8") as file:
ucell_entries = json.loads(file.read())

self.Cu_ucell_entry = ComputedStructureEntry.from_dict(ucell_entries["Cu"])
Expand Down Expand Up @@ -314,7 +314,7 @@ def setUp(self):
# Load all entries
La_hcp_entry_dict = get_entry_dict(f"{TEST_DIR}/La_hcp_entries.txt")
La_fcc_entry_dict = get_entry_dict(f"{TEST_DIR}/La_fcc_entries.txt")
with open(f"{TEST_DIR}/ucell_entries.txt") as txt_file:
with open(f"{TEST_DIR}/ucell_entries.txt", encoding="utf-8") as txt_file:
ucell_entries = json.loads(txt_file.read())
La_hcp_ucell_entry = ComputedStructureEntry.from_dict(ucell_entries["La_hcp"])
La_fcc_ucell_entry = ComputedStructureEntry.from_dict(ucell_entries["La_fcc"])
Expand Down Expand Up @@ -365,7 +365,7 @@ def get_entry_dict(filename):
# helper to generate an entry_dict

entry_dict = {}
with open(filename) as file:
with open(filename, encoding="utf-8") as file:
entries = json.loads(file.read())
for entry in entries:
sub_str = entry[25:]
Expand All @@ -392,7 +392,7 @@ def load_O_adsorption():
# Loads the dictionary for clean and O adsorbed Rh, Pt, and Ni entries

# Load the adsorbate as an entry
with open(f"{TEST_DIR}/isolated_O_entry.txt") as file:
with open(f"{TEST_DIR}/isolated_O_entry.txt", encoding="utf-8") as file:
isolated_O_entry = json.loads(file.read())
O_entry = ComputedStructureEntry.from_dict(isolated_O_entry)

Expand All @@ -403,7 +403,7 @@ def load_O_adsorption():
"Rh": {(1, 0, 0): {}},
}

with open(f"{TEST_DIR}/cs_entries_slabs.json") as file:
with open(f"{TEST_DIR}/cs_entries_slabs.json", encoding="utf-8") as file:
entries = json.loads(file.read())
for key in entries:
entry = ComputedStructureEntry.from_dict(entries[key])
Expand All @@ -419,7 +419,7 @@ def load_O_adsorption():
clean = SlabEntry(entry.structure, entry.energy, (1, 0, 0), label=f"{key}_clean")
metals_O_entry_dict[el][1, 0, 0][clean] = []

with open(f"{TEST_DIR}/cs_entries_o_ads.json") as file:
with open(f"{TEST_DIR}/cs_entries_o_ads.json", encoding="utf-8") as file:
entries = json.loads(file.read())
for key in entries:
entry = ComputedStructureEntry.from_dict(entries[key])
Expand Down
2 changes: 1 addition & 1 deletion tests/analysis/test_transition_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def test_run(self):

neb_analysis1.setup_spline(spline_options={"saddle_point": "zero_slope"})
assert_allclose(neb_analysis1.get_extrema()[1][0], (0.50023335723480078, 325.20003984140203))
with open(f"{TEST_DIR}/neb2/neb_analysis2.json") as file:
with open(f"{TEST_DIR}/neb2/neb_analysis2.json", encoding="utf-8") as file:
neb_analysis2_dict = json.load(file)
neb_analysis2 = NEBAnalysis.from_dict(neb_analysis2_dict)
assert_allclose(neb_analysis2.get_extrema()[1][0], (0.37255257367467326, 562.40825334519991))
Expand Down
2 changes: 1 addition & 1 deletion tests/analysis/test_wulff.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

class TestWulffShape(PymatgenTest):
def setUp(self):
with open(f"{TEST_DIR}/surface_samples.json") as data_file:
with open(f"{TEST_DIR}/surface_samples.json", encoding="utf-8") as data_file:
surface_properties = json.load(data_file)

surface_energies, miller_indices = {}, {}
Expand Down
12 changes: 6 additions & 6 deletions tests/analysis/xas/test_spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@

TEST_DIR = f"{TEST_FILES_DIR}/analysis/spectrum_test"

with open(f"{TEST_DIR}/LiCoO2_k_xanes.json") as file:
with open(f"{TEST_DIR}/LiCoO2_k_xanes.json", encoding="utf-8") as file:
k_xanes_dict = json.load(file, cls=MontyDecoder)
with open(f"{TEST_DIR}/LiCoO2_k_exafs.json") as file:
with open(f"{TEST_DIR}/LiCoO2_k_exafs.json", encoding="utf-8") as file:
k_exafs_dict = json.load(file, cls=MontyDecoder)
with open(f"{TEST_DIR}/ZnO_l2_xanes.json") as file:
with open(f"{TEST_DIR}/ZnO_l2_xanes.json", encoding="utf-8") as file:
l2_xanes_dict = json.load(file, cls=MontyDecoder)
with open(f"{TEST_DIR}/ZnO_l3_xanes.json") as file:
with open(f"{TEST_DIR}/ZnO_l3_xanes.json", encoding="utf-8") as file:
l3_xanes_dict = json.load(file, cls=MontyDecoder)
with open(f"{TEST_DIR}/site1_k_xanes.json") as file:
with open(f"{TEST_DIR}/site1_k_xanes.json", encoding="utf-8") as file:
site1_xanes_dict = json.load(file, cls=MontyDecoder)
with open(f"{TEST_DIR}/site2_k_xanes.json") as file:
with open(f"{TEST_DIR}/site2_k_xanes.json", encoding="utf-8") as file:
site2_xanes_dict = json.load(file, cls=MontyDecoder)


Expand Down

0 comments on commit 7ce8014

Please sign in to comment.