7
7
Diffraction Analyses Chem. Mater 2020 32 (3), 1002-1010. 10.1021/acs.chemmater.9b03646
8
8
9
9
"""
10
+ from __future__ import annotations
10
11
11
12
import os
13
+
12
14
import numpy as np
13
15
import pandas as pd
14
16
import plotly .express as px
15
17
16
-
17
18
# 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 :
20
20
NEUTRON_SCATTER_DF = pd .read_csv (f )
21
21
# from http://www.ccp14.ac.uk/ccp/web-mirrors/neutrons/n-scatter/n-lengths/LIST~1.HTM
22
22
@@ -26,7 +26,7 @@ class FStarDiagram:
26
26
Take a list of structure objects and/or cifs and use them to generate an f* phase diagram.
27
27
"""
28
28
29
- def __init__ (self , structures , scattering_type = ' X-ray' , custom_scatter = None ):
29
+ def __init__ (self , structures , scattering_type = " X-ray" , custom_scatter = None ):
30
30
"""
31
31
Initialize the f* diagram generator with the list of structures and scattering type.
32
32
@@ -35,7 +35,7 @@ def __init__(self, structures, scattering_type='X-ray', custom_scatter=None):
35
35
objects.
36
36
scattering_type(str): Type of scattering to use in the f* calculation. Defaults to 'X-ray'
37
37
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
39
39
own calculation with any set of scattering factors.
40
40
custom_scatter(function): when using custom scattering set this equal to a global varialble that is equal
41
41
to the custom scattering function.
@@ -47,10 +47,12 @@ def __init__(self, structures, scattering_type='X-ray', custom_scatter=None):
47
47
self ._equiv_inds = [struct .equivalent_indices for struct in self ._structures ]
48
48
self .site_labels = self .get_site_labels ()
49
49
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
+ )
52
53
print ("The labels for this structure's unique sites are" )
53
54
print (self .site_labels )
55
+
54
56
def edit_fstar_diagram (self , combine_list = False , plot_list = False , ** kwargs ):
55
57
"""
56
58
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):
68
70
if str (combo ) not in self .site_labels :
69
71
self .site_labels .append (str (combo ))
70
72
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
+ )
73
76
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
+ )
76
80
77
81
def get_site_labels (self ):
78
82
"""
@@ -110,17 +114,17 @@ def get_site_labels(self):
110
114
site_labels_fin = []
111
115
for ind1 , struct in enumerate (self ._equiv_inds ):
112
116
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
+ )
116
121
if label not in site_labels :
117
122
site_labels .append (label )
118
123
if len (site_labels ) > len (site_labels_fin ):
119
124
site_labels_fin = site_labels
120
125
return site_labels_fin
121
126
122
127
def get_fstar_coords (self ):
123
-
124
128
"""
125
129
Calculate the f* coordinates for the list of structures.
126
130
"""
@@ -136,23 +140,23 @@ def get_fstar_coords(self):
136
140
column = [label for label in self .site_labels if site_frac_coord in label ]
137
141
elements_and_occupancies = self ._structures [ind1 ][site [0 ]].species .items ()
138
142
for sp , occ in elements_and_occupancies :
139
- if self ._scatter == ' X-ray' :
143
+ if self ._scatter == " X-ray" :
140
144
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 ):
143
147
if hasattr (sp , "element" ):
144
148
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
146
150
break
147
151
else :
148
152
continue
149
153
else :
150
154
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
152
156
break
153
157
else :
154
158
continue
155
- if self ._scatter == ' Custom' :
159
+ if self ._scatter == " Custom" :
156
160
if hasattr (sp , "element" ):
157
161
f_occ = self ._custscat (str (sp .element ), occ , ind1 , ind2 )
158
162
else :
0 commit comments