1 | #!/usr/bin/env python |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | ########### SVN repository information ################### |
---|
4 | # $Date: 2021-04-04 08:00:26 +1000 (Sun, 04 Apr 2021) $ |
---|
5 | # $Author: toby $ |
---|
6 | # $Revision: 4874 $ |
---|
7 | # $URL: https://subversion.xray.aps.anl.gov/pyGSAS/trunk/exports/G2export_JSON.py $ |
---|
8 | # $Id: G2export_JSON.py 4874 2021-04-03 22:00:26Z toby $ |
---|
9 | ########### SVN repository information ################### |
---|
10 | |
---|
11 | # This module was written by Conrad Gillard. For any enquiries please contact conrad.gillard@gmail.com |
---|
12 | from __future__ import division, print_function |
---|
13 | import wx |
---|
14 | import GSASIIpath |
---|
15 | GSASIIpath.SetVersionNumber("$Revision: 4874 $") |
---|
16 | import GSASIIIO as G2IO |
---|
17 | from collections import OrderedDict |
---|
18 | from GSASIImath import ValEsd |
---|
19 | |
---|
20 | class Exportbracket(G2IO.ExportBaseclass): |
---|
21 | '''Enables export of parameters that are commonly needed for publications, in bracket notation |
---|
22 | ''' |
---|
23 | |
---|
24 | def __init__(self, G2frame): |
---|
25 | G2IO.ExportBaseclass.__init__(self,G2frame=G2frame,formatName='Bracket notation CSV', |
---|
26 | extension='.csv',longFormatName='Export commonly needed parameters') |
---|
27 | self.exporttype = ['project'] |
---|
28 | |
---|
29 | def Exporter(self, event=None): |
---|
30 | |
---|
31 | # Define function to extract parameter and sigma from covariances, for later use |
---|
32 | def GetParamSig(phase_num, hist_num, keyword, display_name): |
---|
33 | param_index = None |
---|
34 | try: |
---|
35 | param_index = self.OverallParms['Covariance']['varyList'].index(phase_num + ':' + hist_num + keyword) |
---|
36 | except: |
---|
37 | pass |
---|
38 | if param_index is not None: |
---|
39 | param = self.OverallParms['Covariance']['variables'][param_index] |
---|
40 | # Extract parameter uncertainty |
---|
41 | param_sig = self.OverallParms['Covariance']['sig'][param_index] |
---|
42 | # Create dictionary entry containing parameter and sigma in bracket notation |
---|
43 | model_parameters[display_name] = ValEsd(param, param_sig) |
---|
44 | |
---|
45 | # # set up for export |
---|
46 | self.InitExport(event) |
---|
47 | if self.ExportSelect(): return # set export parameters; get file name |
---|
48 | self.OpenFile() |
---|
49 | wx.BeginBusyCursor() |
---|
50 | |
---|
51 | # Export model parameters in bracket notation |
---|
52 | try: |
---|
53 | # Initialise ordered dictionary to hold model parameters and uncertainties |
---|
54 | model_parameters = OrderedDict() |
---|
55 | |
---|
56 | # load all of the tree into a set of dicts |
---|
57 | self.loadTree() |
---|
58 | # create a dict with refined values and their uncertainties |
---|
59 | self.loadParmDict() |
---|
60 | |
---|
61 | # Initialise phase counter, for later use |
---|
62 | phase_num = 0 |
---|
63 | # Extract lattice parameters and uncertainties, and convert to bracket notation |
---|
64 | for phasedict in self.Phases.items(): |
---|
65 | phasenam = phasedict[0] |
---|
66 | cellList, cellSig = self.GetCell(phasenam) |
---|
67 | # Initialise lattice parameter letter |
---|
68 | lp_letter = "a" |
---|
69 | for i in range(0, len(cellList)): |
---|
70 | # for cell in cellList: |
---|
71 | if cellSig[i] > 0: |
---|
72 | # Formulate lattice parameter in bracket notation |
---|
73 | current_lp_bracket = ValEsd(cellList[i], cellSig[i]) |
---|
74 | # Write to dictionary that will later be exported to CSV |
---|
75 | model_parameters[phasenam + " " + lp_letter + " (Ã
)"] = current_lp_bracket |
---|
76 | # Increment lattice parameter letter |
---|
77 | lp_letter = chr(ord(lp_letter[0]) + 1) |
---|
78 | else: |
---|
79 | break |
---|
80 | |
---|
81 | # Get phase and weight fractions uncertainties, if they have been refined |
---|
82 | for hist_num,hist_name in enumerate(self.Histograms): |
---|
83 | try: |
---|
84 | # Get phase fraction uncertainty, if phase fractions have been refined |
---|
85 | phasefrac_unc = self.sigDict[str(phase_num) + ':' + str(hist_num) + ':Scale'] |
---|
86 | # Get name of histogram associated with this phase, for later use |
---|
87 | #hist_name = list(self.Histograms.keys())[hist_num] |
---|
88 | # Extract phase fraction value |
---|
89 | phasefrac = phasedict[1]['Histograms'][hist_name]['Scale'][0] |
---|
90 | # Write phase if there is more than one histogram, specify which one |
---|
91 | if len(self.Histograms) > 1: |
---|
92 | model_parameters[phasenam + " Phase Fraction in: " + hist_name] = ( |
---|
93 | ValEsd(phasefrac, phasefrac_unc)) |
---|
94 | # If there is only one histogram, no need to specify which histogram the fraction is based on |
---|
95 | else: |
---|
96 | model_parameters[phasenam + " Phase Fraction"] = ValEsd(phasefrac, phasefrac_unc) |
---|
97 | except: |
---|
98 | pass |
---|
99 | |
---|
100 | try: |
---|
101 | var = str(phase_num) + ':' + str(hist_num) + ':WgtFrac' |
---|
102 | depSigDict = self.OverallParms['Covariance'].get('depSigDict',{}) |
---|
103 | weight_frac,weight_frac_unc = depSigDict.get(var,[0,None]) |
---|
104 | |
---|
105 | # Write phase + weight fractions in bracket notation to dictionary, to be exported as a CSV |
---|
106 | # If there is more than one histogram, specify which one the fraction is based on |
---|
107 | if len(self.Histograms) > 1: |
---|
108 | model_parameters[phasenam + " Weight Fraction in: " + hist_name] = ( |
---|
109 | ValEsd(weight_frac, weight_frac_unc)) |
---|
110 | # If there is only one histogram, no need to specify which histogram the fraction is based on |
---|
111 | else: |
---|
112 | model_parameters[phasenam + " Weight Fraction"] = ValEsd(weight_frac, weight_frac_unc) |
---|
113 | except: |
---|
114 | pass |
---|
115 | |
---|
116 | # Get preferred orientation details for phase, if refined |
---|
117 | try: |
---|
118 | pref_orr_props = phasedict[1]['Histograms'][hist_name]['Pref.Ori.'] |
---|
119 | # Check if March Dollase has been refined |
---|
120 | if pref_orr_props[2] and pref_orr_props[0] == "MD": |
---|
121 | # If so, first extract MD axis and write to dictionary to be exported |
---|
122 | MD_axis = "\'" + ''.join(map(str, pref_orr_props[3])) |
---|
123 | model_parameters[phasenam + " March Dollase Axis"] = MD_axis |
---|
124 | # Next, extract MD ratio |
---|
125 | MD_ratio = pref_orr_props[1] |
---|
126 | MD_sig = self.sigDict[str(phase_num)+":" + str(hist_num) + ":MD"] |
---|
127 | # Formulate MD ratio in bracket notation |
---|
128 | MD_bracket = ValEsd(MD_ratio, MD_sig) |
---|
129 | # Write MD ratio to dictionary to be exported |
---|
130 | model_parameters[phasenam + " March Dollase Ratio"] = MD_bracket |
---|
131 | except: |
---|
132 | pass |
---|
133 | # Increment phase number counter |
---|
134 | phase_num += 1 |
---|
135 | |
---|
136 | # Extract sample displacements, zero offset and D(ij)s (if refined) |
---|
137 | for i,hist_name in enumerate(self.Histograms): |
---|
138 | hist_num = str(i) |
---|
139 | # Extract zero offset, if refined |
---|
140 | GetParamSig("", hist_num, ':Zero', "Zero Offset") |
---|
141 | # Extract Bragg-Brentano sample displacement, if refined |
---|
142 | GetParamSig("", hist_num, ':Shift', "Sample Displacement (micron)") |
---|
143 | # Extract Debye-Scherrer sample X displacement, if refined |
---|
144 | GetParamSig("", hist_num, ':DisplaceX', "Sample X Displacement (micron)") |
---|
145 | # Extract Debye-Scherrer sample Y displacement, if refined |
---|
146 | GetParamSig("", hist_num, ':DisplaceY', "Sample Y Displacement (micron)") |
---|
147 | # Extract hydrostatic strains, if refined |
---|
148 | for phase_num,phase_name in enumerate(self.Phases): |
---|
149 | for d_i in range(1, 4): |
---|
150 | for d_j in range(1, 4): |
---|
151 | GetParamSig(str(phase_num), hist_num, ':D' + str(d_i) + str(d_j), |
---|
152 | phase_name + ' D' + str(d_i) + str(d_j)) |
---|
153 | |
---|
154 | # Extract atomic parameters, if refined |
---|
155 | for phase_num,phase_name in enumerate(self.Phases): |
---|
156 | # atom_list = list(self.Phases.values())[phase_num]["Atoms"] |
---|
157 | atom_list = self.Phases[phase_name]["Atoms"] # same as above? |
---|
158 | for atom_num,atom in enumerate(atom_list): |
---|
159 | # Extract isotropic thermal parameters, if refined |
---|
160 | GetParamSig(str(phase_num), ':', 'AUiso:' + str(atom_num), |
---|
161 | phase_name + ' ' + atom[0] + ' Uiso') |
---|
162 | # Extract anisotropic thermal parameters (Uijs), if refined |
---|
163 | for Ui in range(1, 4): |
---|
164 | for Uj in range(1, 4): |
---|
165 | GetParamSig(str(phase_num), ':', 'AU' + str(Ui) + str(Uj) + ':' + str(atom_num), |
---|
166 | phase_name + ' ' + atom[0] + ' U' + str(Ui) + str(Uj)) |
---|
167 | # Extract fractional occupancies, if refined |
---|
168 | GetParamSig(str(phase_num), ':', 'Afrac:' + str(atom_num), |
---|
169 | phase_name + ' ' + atom[0] + ' Occupancy') |
---|
170 | # Extract atom X Y Z, if refined |
---|
171 | for atom_axis in ('x', 'y', 'z'): |
---|
172 | variable_code = str(phase_num) + ':' + ':' + 'dA' + atom_axis + ':' + str(atom_num) |
---|
173 | # Get sigma, if refined |
---|
174 | try: |
---|
175 | param_index = self.OverallParms['Covariance']['varyList'].index(variable_code) |
---|
176 | # Extract uncertainty |
---|
177 | atom_axis_sig = self.OverallParms['Covariance']['sig'][param_index] |
---|
178 | # Extract value |
---|
179 | atom_axis_val = list(self.Phases.values())[phase_num]["Atoms"][atom_num][ord(atom_axis)-117] |
---|
180 | # Convert to bracket notation and add to dictionary, which will be exported as a CSV |
---|
181 | model_parameters[phase_name + ' ' + atom[0] + ' ' + atom_axis] = \ |
---|
182 | ValEsd(atom_axis_val, atom_axis_sig) |
---|
183 | except: pass |
---|
184 | |
---|
185 | # Extract rWp |
---|
186 | rWP = self.OverallParms['Covariance']['Rvals']['Rwp'] |
---|
187 | # Write to dictionary to be printed, rounding to 3 significant figures for readability |
---|
188 | model_parameters["wR"] = str(ValEsd(rWP, -0.1)) + "%" |
---|
189 | |
---|
190 | # Write to CSV |
---|
191 | # parameter_names = "" |
---|
192 | # for parameter_name in model_parameters.keys(): |
---|
193 | # parameter_names = parameter_names + str(parameter_name) + ", " |
---|
194 | # self.Write(parameter_names[0:-2]) |
---|
195 | |
---|
196 | # parameter_values = "" |
---|
197 | # for parameter_value in model_parameters.values(): |
---|
198 | # parameter_values = parameter_values + str(parameter_value) + ", " |
---|
199 | # self.Write(parameter_values[0:-2]) |
---|
200 | |
---|
201 | for name in model_parameters: |
---|
202 | self.Write('%s, %s,'%(name,model_parameters[name])) |
---|
203 | |
---|
204 | finally: |
---|
205 | wx.EndBusyCursor() |
---|
206 | self.CloseFile() |
---|