Skip to content

Commit 99c6001

Browse files
pre-commit auto-fixes
1 parent fbc2e32 commit 99c6001

23 files changed

+48
-78
lines changed

pymatgen/analysis/fstar/fstar.py

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@
77
Diffraction Analyses Chem. Mater 2020 32 (3), 1002-1010. 10.1021/acs.chemmater.9b03646
88
99
"""
10+
from __future__ import annotations
1011

1112
import os
13+
1214
import numpy as np
1315
import pandas as pd
1416
import plotly.express as px
1517

16-
1718
# Load in the neutron form factors
18-
with open(os.path.join(os.path.dirname(__file__),
19-
"neutron_factors.csv")) as f:
19+
with open(os.path.join(os.path.dirname(__file__), "neutron_factors.csv")) as f:
2020
NEUTRON_SCATTER_DF = pd.read_csv(f)
2121
# from http://www.ccp14.ac.uk/ccp/web-mirrors/neutrons/n-scatter/n-lengths/LIST~1.HTM
2222

@@ -26,7 +26,7 @@ class FStarDiagram:
2626
Take a list of structure objects and/or cifs and use them to generate an f* phase diagram.
2727
"""
2828

29-
def __init__(self, structures, scattering_type='X-ray', custom_scatter=None):
29+
def __init__(self, structures, scattering_type="X-ray", custom_scatter=None):
3030
"""
3131
Initialize the f* diagram generator with the list of structures and scattering type.
3232
@@ -35,7 +35,7 @@ def __init__(self, structures, scattering_type='X-ray', custom_scatter=None):
3535
objects.
3636
scattering_type(str): Type of scattering to use in the f* calculation. Defaults to 'X-ray'
3737
which uses the atomic number as the scattering factor. 'Neutron' is a built in scattering
38-
type which uses neutron scattering factors. 'Custom' allows the user to supplement their
38+
type which uses neutron scattering factors. 'Custom' allows the user to supplement their
3939
own calculation with any set of scattering factors.
4040
custom_scatter(function): when using custom scattering set this equal to a global varialble that is equal
4141
to the custom scattering function.
@@ -47,10 +47,12 @@ def __init__(self, structures, scattering_type='X-ray', custom_scatter=None):
4747
self._equiv_inds = [struct.equivalent_indices for struct in self._structures]
4848
self.site_labels = self.get_site_labels()
4949
self.coords = self.get_fstar_coords()
50-
self.plot = px.scatter_ternary(data_frame=self.coords, a=self.site_labels[0], b=self.site_labels[1],
51-
c=self.site_labels[2])
50+
self.plot = px.scatter_ternary(
51+
data_frame=self.coords, a=self.site_labels[0], b=self.site_labels[1], c=self.site_labels[2]
52+
)
5253
print("The labels for this structure's unique sites are")
5354
print(self.site_labels)
55+
5456
def edit_fstar_diagram(self, combine_list=False, plot_list=False, **kwargs):
5557
"""
5658
Edit the plot of the f* diagram using plotly express.
@@ -68,11 +70,13 @@ def edit_fstar_diagram(self, combine_list=False, plot_list=False, **kwargs):
6870
if str(combo) not in self.site_labels:
6971
self.site_labels.append(str(combo))
7072
if plot_list:
71-
self.plot = px.scatter_ternary(data_frame=self.coords, a=plot_list[0], b=plot_list[1], c=plot_list[2],
72-
**kwargs)
73+
self.plot = px.scatter_ternary(
74+
data_frame=self.coords, a=plot_list[0], b=plot_list[1], c=plot_list[2], **kwargs
75+
)
7376
else:
74-
self.plot = px.scatter_ternary(data_frame=self.coords, a=self.site_labels[0], b=self.site_labels[1],
75-
c=self.site_labels[2], **kwargs)
77+
self.plot = px.scatter_ternary(
78+
data_frame=self.coords, a=self.site_labels[0], b=self.site_labels[1], c=self.site_labels[2], **kwargs
79+
)
7680

7781
def get_site_labels(self):
7882
"""
@@ -110,17 +114,17 @@ def get_site_labels(self):
110114
site_labels_fin = []
111115
for ind1, struct in enumerate(self._equiv_inds):
112116
site_labels = []
113-
for ind2, site in enumerate(struct):
114-
label = str(self._structures[ind1][site[0]].frac_coords) + \
115-
[str(sp) for sp, occ in self._structures[ind1][site[0]].species.items()][0]
117+
for _ind2, site in enumerate(struct):
118+
label = str(self._structures[ind1][site[0]].frac_coords) + next(
119+
str(sp) for sp, occ in self._structures[ind1][site[0]].species.items()
120+
)
116121
if label not in site_labels:
117122
site_labels.append(label)
118123
if len(site_labels) > len(site_labels_fin):
119124
site_labels_fin = site_labels
120125
return site_labels_fin
121126

122127
def get_fstar_coords(self):
123-
124128
"""
125129
Calculate the f* coordinates for the list of structures.
126130
"""
@@ -136,23 +140,23 @@ def get_fstar_coords(self):
136140
column = [label for label in self.site_labels if site_frac_coord in label]
137141
elements_and_occupancies = self._structures[ind1][site[0]].species.items()
138142
for sp, occ in elements_and_occupancies:
139-
if self._scatter == 'X-ray':
143+
if self._scatter == "X-ray":
140144
f_occ = sp.Z * occ
141-
if self._scatter == 'Neutron':
142-
for i, n in enumerate(NEUTRON_SCATTER_DF['Isotope'].values):
145+
if self._scatter == "Neutron":
146+
for i, n in enumerate(NEUTRON_SCATTER_DF["Isotope"].values):
143147
if hasattr(sp, "element"):
144148
if n == str(sp.element):
145-
f_occ = float(NEUTRON_SCATTER_DF.loc[i]['Coh b']) * occ
149+
f_occ = float(NEUTRON_SCATTER_DF.loc[i]["Coh b"]) * occ
146150
break
147151
else:
148152
continue
149153
else:
150154
if n == str(sp):
151-
f_occ = float(NEUTRON_SCATTER_DF.loc[i]['Coh b']) * occ
155+
f_occ = float(NEUTRON_SCATTER_DF.loc[i]["Coh b"]) * occ
152156
break
153157
else:
154158
continue
155-
if self._scatter == 'Custom':
159+
if self._scatter == "Custom":
156160
if hasattr(sp, "element"):
157161
f_occ = self._custscat(str(sp.element), occ, ind1, ind2)
158162
else:

tests/analysis/fstar/test_fstar.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,37 @@
1+
from __future__ import annotations
12

23
import os
34

45
from pymatgen.analysis.fstar.fstar import FStarDiagram
5-
from pymatgen.util.testing import TEST_FILES_DIR, PymatgenTest
66
from pymatgen.io.cif import CifParser
7+
from pymatgen.util.testing import TEST_FILES_DIR, PymatgenTest
78

89

910
class Test_FStarDiagram(PymatgenTest):
10-
1111
def setUp(self):
12-
1312
self.cif_list = [file for file in os.listdir(f"{TEST_FILES_DIR}/analysis/fstar") if file.endswith(".cif")]
14-
self.struct_list = [CifParser(f"{TEST_FILES_DIR}/analysis/fstar/"+file).get_structures(primitive=False, symmetrized=True, check_occu=False)[0]
15-
for file in self.cif_list]
13+
self.struct_list = [
14+
CifParser(f"{TEST_FILES_DIR}/analysis/fstar/" + file).get_structures(
15+
primitive=False, symmetrized=True, check_occu=False
16+
)[0]
17+
for file in self.cif_list
18+
]
1619
self.fstar = FStarDiagram(structures=self.struct_list)
1720

1821
def test_edit_fstar_diagram(self):
19-
20-
self.assertEqual(self.fstar.site_labels, ['[0. 0. 0.]Li', '[0. 0. 0.5]Co', '[0. 0. 0.25]O'])
22+
assert self.fstar.site_labels == ["[0. 0. 0.]Li", "[0. 0. 0.5]Co", "[0. 0. 0.25]O"]
2123
new = FStarDiagram(structures=self.struct_list)
22-
self.assertEqual(self.fstar.plot, new.plot)
23-
new.edit_fstar_diagram(combine_list=[['[0. 0. 0.5]Co', '[0. 0. 0.]Li']])
24-
self.assertEqual(new.site_labels, ['[0. 0. 0.]Li', '[0. 0. 0.5]Co', '[0. 0. 0.25]O',
25-
"['[0. 0. 0.5]Co', '[0. 0. 0.]Li']"])
26-
self.assertEqual(list(new.coords["['[0. 0. 0.5]Co', '[0. 0. 0.]Li']"].values),
27-
list(self.fstar.coords['[0. 0. 0.]Li'].values+self.fstar.coords['[0. 0. 0.5]Co'].values))
28-
self.assertEqual(self.fstar.plot, new.plot)
29-
new.edit_fstar_diagram(plot_list=['[0. 0. 0.5]Co', '[0. 0. 0.25]O', '[0. 0. 0.]Li'])
30-
self.assertTrue(self.fstar.plot != new.plot)
24+
assert self.fstar.plot == new.plot
25+
new.edit_fstar_diagram(combine_list=[["[0. 0. 0.5]Co", "[0. 0. 0.]Li"]])
26+
assert new.site_labels == [
27+
"[0. 0. 0.]Li",
28+
"[0. 0. 0.5]Co",
29+
"[0. 0. 0.25]O",
30+
"['[0. 0. 0.5]Co', '[0. 0. 0.]Li']",
31+
]
32+
assert list(new.coords["['[0. 0. 0.5]Co', '[0. 0. 0.]Li']"].values) == list(
33+
self.fstar.coords["[0. 0. 0.]Li"].values + self.fstar.coords["[0. 0. 0.5]Co"].values
34+
)
35+
assert self.fstar.plot == new.plot
36+
new.edit_fstar_diagram(plot_list=["[0. 0. 0.5]Co", "[0. 0. 0.25]O", "[0. 0. 0.]Li"])
37+
assert self.fstar.plot != new.plot

tests/analysis/fstar/test_nmc_Ni_excess11.cif

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,5 +175,3 @@ _refine_ls_wR_factor_all ?
175175
_computing_structure_refinement GSAS
176176

177177
# End of data set 1817019
178-
179-

tests/analysis/fstar/test_nmc_Ni_excess12.cif

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,5 +175,3 @@ _refine_ls_wR_factor_all ?
175175
_computing_structure_refinement GSAS
176176

177177
# End of data set 1817019
178-
179-

tests/analysis/fstar/test_nmc_Ni_excess13.cif

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,5 +175,3 @@ _refine_ls_wR_factor_all ?
175175
_computing_structure_refinement GSAS
176176

177177
# End of data set 1817019
178-
179-

tests/analysis/fstar/test_nmc_Ni_excess14.cif

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,5 +175,3 @@ _refine_ls_wR_factor_all ?
175175
_computing_structure_refinement GSAS
176176

177177
# End of data set 1817019
178-
179-

tests/analysis/fstar/test_nmc_Ni_excess15.cif

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,5 +175,3 @@ _refine_ls_wR_factor_all ?
175175
_computing_structure_refinement GSAS
176176

177177
# End of data set 1817019
178-
179-

tests/analysis/fstar/test_nmc_Ni_excess16.cif

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,5 +175,3 @@ _refine_ls_wR_factor_all ?
175175
_computing_structure_refinement GSAS
176176

177177
# End of data set 1817019
178-
179-

tests/analysis/fstar/test_nmc_Ni_excess17.cif

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,5 +175,3 @@ _refine_ls_wR_factor_all ?
175175
_computing_structure_refinement GSAS
176176

177177
# End of data set 1817019
178-
179-

tests/analysis/fstar/test_nmc_Ni_excess18.cif

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,5 +175,3 @@ _refine_ls_wR_factor_all ?
175175
_computing_structure_refinement GSAS
176176

177177
# End of data set 1817019
178-
179-

0 commit comments

Comments
 (0)