1 | #!/usr/bin/env python |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | ########### SVN repository information ################### |
---|
4 | # $Date: 2022-11-06 18:25:48 +0000 (Sun, 06 Nov 2022) $ |
---|
5 | # $Author: toby $ |
---|
6 | # $Revision: 5365 $ |
---|
7 | # $URL: trunk/exports/G2export_Bracket.py $ |
---|
8 | # $Id: G2export_Bracket.py 5365 2022-11-06 18:25:48Z toby $ |
---|
9 | ########### SVN repository information ################### |
---|
10 | |
---|
11 | # This module initially written by Conrad Gillard. For any enquiries please contact conrad.gillard@gmail.com |
---|
12 | # Export3col exporter adapted from Exportbracket by BHT |
---|
13 | from __future__ import division, print_function |
---|
14 | import wx |
---|
15 | import GSASIIpath |
---|
16 | GSASIIpath.SetVersionNumber("$Revision: 5365 $") |
---|
17 | import GSASIIIO as G2IO |
---|
18 | from collections import OrderedDict |
---|
19 | from GSASIImath import ValEsd |
---|
20 | |
---|
21 | class Exportbracket(G2IO.ExportBaseclass): |
---|
22 | '''Enables export of parameters that are commonly needed for publications, in bracket notation |
---|
23 | ''' |
---|
24 | |
---|
25 | def __init__(self, G2frame): |
---|
26 | G2IO.ExportBaseclass.__init__(self,G2frame=G2frame,formatName='Bracket notation CSV', |
---|
27 | extension='.csv',longFormatName='Export commonly needed parameters') |
---|
28 | self.exporttype = ['project'] |
---|
29 | |
---|
30 | def Exporter(self, event=None): |
---|
31 | |
---|
32 | # Define function to extract parameter and sigma from covariances, for later use |
---|
33 | def GetParamSig(phase_num, hist_num, keyword, display_name): |
---|
34 | param_index = None |
---|
35 | try: |
---|
36 | param_index = self.OverallParms['Covariance']['varyList'].index(phase_num + ':' + hist_num + keyword) |
---|
37 | except: |
---|
38 | pass |
---|
39 | if param_index is not None: |
---|
40 | param = self.OverallParms['Covariance']['variables'][param_index] |
---|
41 | # Extract parameter uncertainty |
---|
42 | param_sig = self.OverallParms['Covariance']['sig'][param_index] |
---|
43 | # Create dictionary entry containing parameter and sigma in bracket notation |
---|
44 | model_parameters[display_name] = ValEsd(param, param_sig) |
---|
45 | |
---|
46 | # # set up for export |
---|
47 | self.InitExport(event) |
---|
48 | if self.ExportSelect(): return # set export parameters; get file name |
---|
49 | self.OpenFile() |
---|
50 | wx.BeginBusyCursor() |
---|
51 | |
---|
52 | # Export model parameters in bracket notation |
---|
53 | try: |
---|
54 | # Initialise ordered dictionary to hold model parameters and uncertainties |
---|
55 | model_parameters = OrderedDict() |
---|
56 | |
---|
57 | # load all of the tree into a set of dicts |
---|
58 | self.loadTree() |
---|
59 | # create a dict with refined values and their uncertainties |
---|
60 | self.loadParmDict() |
---|
61 | |
---|
62 | # Initialise phase counter, for later use |
---|
63 | phase_num = 0 |
---|
64 | # Extract lattice parameters and uncertainties, and convert to bracket notation |
---|
65 | for phasedict in self.Phases.items(): |
---|
66 | phasenam = phasedict[0] |
---|
67 | cellList, cellSig = self.GetCell(phasenam) |
---|
68 | # Initialise lattice parameter letter |
---|
69 | lp_letter = "a" |
---|
70 | for i in range(0, len(cellList)): |
---|
71 | # for cell in cellList: |
---|
72 | if cellSig[i] > 0: |
---|
73 | # Formulate lattice parameter in bracket notation |
---|
74 | current_lp_bracket = ValEsd(cellList[i], cellSig[i]) |
---|
75 | # Write to dictionary that will later be exported to CSV |
---|
76 | model_parameters[phasenam + " " + lp_letter + " (Ã
)"] = current_lp_bracket |
---|
77 | # Increment lattice parameter letter |
---|
78 | lp_letter = chr(ord(lp_letter[0]) + 1) |
---|
79 | else: |
---|
80 | break |
---|
81 | |
---|
82 | # Get phase and weight fractions uncertainties, if they have been refined |
---|
83 | for hist_num,hist_name in enumerate(self.Histograms): |
---|
84 | try: |
---|
85 | # Get phase fraction uncertainty, if phase fractions have been refined |
---|
86 | phasefrac_unc = self.sigDict[str(phase_num) + ':' + str(hist_num) + ':Scale'] |
---|
87 | # Get name of histogram associated with this phase, for later use |
---|
88 | #hist_name = list(self.Histograms.keys())[hist_num] |
---|
89 | # Extract phase fraction value |
---|
90 | phasefrac = phasedict[1]['Histograms'][hist_name]['Scale'][0] |
---|
91 | # Write phase if there is more than one histogram, specify which one |
---|
92 | if len(self.Histograms) > 1: |
---|
93 | model_parameters[phasenam + " Phase Fraction in: " + hist_name] = ( |
---|
94 | ValEsd(phasefrac, phasefrac_unc)) |
---|
95 | # If there is only one histogram, no need to specify which histogram the fraction is based on |
---|
96 | else: |
---|
97 | model_parameters[phasenam + " Phase Fraction"] = ValEsd(phasefrac, phasefrac_unc) |
---|
98 | except: |
---|
99 | pass |
---|
100 | |
---|
101 | try: |
---|
102 | var = str(phase_num) + ':' + str(hist_num) + ':WgtFrac' |
---|
103 | depSigDict = self.OverallParms['Covariance'].get('depSigDict',{}) |
---|
104 | weight_frac,weight_frac_unc = depSigDict.get(var,[0,None]) |
---|
105 | |
---|
106 | # Write phase + weight fractions in bracket notation to dictionary, to be exported as a CSV |
---|
107 | # If there is more than one histogram, specify which one the fraction is based on |
---|
108 | if len(self.Histograms) > 1: |
---|
109 | model_parameters[phasenam + " Weight Fraction in: " + hist_name] = ( |
---|
110 | ValEsd(weight_frac, weight_frac_unc)) |
---|
111 | # If there is only one histogram, no need to specify which histogram the fraction is based on |
---|
112 | else: |
---|
113 | model_parameters[phasenam + " Weight Fraction"] = ValEsd(weight_frac, weight_frac_unc) |
---|
114 | except: |
---|
115 | pass |
---|
116 | |
---|
117 | # Get preferred orientation details for phase, if refined |
---|
118 | try: |
---|
119 | pref_orr_props = phasedict[1]['Histograms'][hist_name]['Pref.Ori.'] |
---|
120 | # Check if March Dollase has been refined |
---|
121 | if pref_orr_props[2] and pref_orr_props[0] == "MD": |
---|
122 | # If so, first extract MD axis and write to dictionary to be exported |
---|
123 | MD_axis = "\'" + ''.join(map(str, pref_orr_props[3])) |
---|
124 | model_parameters[phasenam + " March Dollase Axis"] = MD_axis |
---|
125 | # Next, extract MD ratio |
---|
126 | MD_ratio = pref_orr_props[1] |
---|
127 | MD_sig = self.sigDict[str(phase_num)+":" + str(hist_num) + ":MD"] |
---|
128 | # Formulate MD ratio in bracket notation |
---|
129 | MD_bracket = ValEsd(MD_ratio, MD_sig) |
---|
130 | # Write MD ratio to dictionary to be exported |
---|
131 | model_parameters[phasenam + " March Dollase Ratio"] = MD_bracket |
---|
132 | except: |
---|
133 | pass |
---|
134 | # Increment phase number counter |
---|
135 | phase_num += 1 |
---|
136 | |
---|
137 | # Extract sample displacements, zero offset and D(ij)s (if refined) |
---|
138 | for i,hist_name in enumerate(self.Histograms): |
---|
139 | hist_num = str(i) |
---|
140 | # Extract zero offset, if refined |
---|
141 | GetParamSig("", hist_num, ':Zero', "Zero Offset") |
---|
142 | # Extract Bragg-Brentano sample displacement, if refined |
---|
143 | GetParamSig("", hist_num, ':Shift', "Sample Displacement (micron)") |
---|
144 | # Extract Debye-Scherrer sample X displacement, if refined |
---|
145 | GetParamSig("", hist_num, ':DisplaceX', "Sample X Displacement (micron)") |
---|
146 | # Extract Debye-Scherrer sample Y displacement, if refined |
---|
147 | GetParamSig("", hist_num, ':DisplaceY', "Sample Y Displacement (micron)") |
---|
148 | # Extract hydrostatic strains, if refined |
---|
149 | for phase_num,phase_name in enumerate(self.Phases): |
---|
150 | for d_i in range(1, 4): |
---|
151 | for d_j in range(1, 4): |
---|
152 | GetParamSig(str(phase_num), hist_num, ':D' + str(d_i) + str(d_j), |
---|
153 | phase_name + ' D' + str(d_i) + str(d_j)) |
---|
154 | |
---|
155 | # Extract atomic parameters, if refined |
---|
156 | for phase_num,phase_name in enumerate(self.Phases): |
---|
157 | # atom_list = list(self.Phases.values())[phase_num]["Atoms"] |
---|
158 | atom_list = self.Phases[phase_name]["Atoms"] # same as above? |
---|
159 | for atom_num,atom in enumerate(atom_list): |
---|
160 | # Extract isotropic thermal parameters, if refined |
---|
161 | GetParamSig(str(phase_num), ':', 'AUiso:' + str(atom_num), |
---|
162 | phase_name + ' ' + atom[0] + ' Uiso') |
---|
163 | # Extract anisotropic thermal parameters (Uijs), if refined |
---|
164 | for Ui in range(1, 4): |
---|
165 | for Uj in range(1, 4): |
---|
166 | GetParamSig(str(phase_num), ':', 'AU' + str(Ui) + str(Uj) + ':' + str(atom_num), |
---|
167 | phase_name + ' ' + atom[0] + ' U' + str(Ui) + str(Uj)) |
---|
168 | # Extract fractional occupancies, if refined |
---|
169 | GetParamSig(str(phase_num), ':', 'Afrac:' + str(atom_num), |
---|
170 | phase_name + ' ' + atom[0] + ' Occupancy') |
---|
171 | # Extract atom X Y Z, if refined |
---|
172 | for atom_axis in ('x', 'y', 'z'): |
---|
173 | variable_code = str(phase_num) + ':' + ':' + 'dA' + atom_axis + ':' + str(atom_num) |
---|
174 | # Get sigma, if refined |
---|
175 | try: |
---|
176 | param_index = self.OverallParms['Covariance']['varyList'].index(variable_code) |
---|
177 | # Extract uncertainty |
---|
178 | atom_axis_sig = self.OverallParms['Covariance']['sig'][param_index] |
---|
179 | # Extract value |
---|
180 | atom_axis_val = list(self.Phases.values())[phase_num]["Atoms"][atom_num][ord(atom_axis)-117] |
---|
181 | # Convert to bracket notation and add to dictionary, which will be exported as a CSV |
---|
182 | model_parameters[phase_name + ' ' + atom[0] + ' ' + atom_axis] = \ |
---|
183 | ValEsd(atom_axis_val, atom_axis_sig) |
---|
184 | except: pass |
---|
185 | |
---|
186 | # Extract rWp |
---|
187 | rWP = self.OverallParms['Covariance']['Rvals']['Rwp'] |
---|
188 | # Write to dictionary to be printed, rounding to 3 significant figures for readability |
---|
189 | model_parameters["wR"] = str(ValEsd(rWP, -0.1)) + "%" |
---|
190 | |
---|
191 | # Write to CSV |
---|
192 | # parameter_names = "" |
---|
193 | # for parameter_name in model_parameters.keys(): |
---|
194 | # parameter_names = parameter_names + str(parameter_name) + ", " |
---|
195 | # self.Write(parameter_names[0:-2]) |
---|
196 | |
---|
197 | # parameter_values = "" |
---|
198 | # for parameter_value in model_parameters.values(): |
---|
199 | # parameter_values = parameter_values + str(parameter_value) + ", " |
---|
200 | # self.Write(parameter_values[0:-2]) |
---|
201 | |
---|
202 | for name in model_parameters: |
---|
203 | self.Write('%s, %s,'%(name,model_parameters[name])) |
---|
204 | |
---|
205 | finally: |
---|
206 | wx.EndBusyCursor() |
---|
207 | self.CloseFile() |
---|
208 | |
---|
209 | |
---|
210 | class Export3col(G2IO.ExportBaseclass): |
---|
211 | '''Enables export of parameters that are commonly needed for publications, with esds |
---|
212 | in a separate column |
---|
213 | ''' |
---|
214 | |
---|
215 | def __init__(self, G2frame): |
---|
216 | G2IO.ExportBaseclass.__init__(self,G2frame=G2frame,formatName='common prm CSV', |
---|
217 | extension='.csv',longFormatName='Export commonly needed parameters with s.u. in a separate column') |
---|
218 | self.exporttype = ['project'] |
---|
219 | |
---|
220 | def ValEsd2col(self, param, param_sig): |
---|
221 | '''Return two values with the formated value as the first number and the |
---|
222 | standard uncertainty (if provided) as the second value. |
---|
223 | ''' |
---|
224 | col1 = ValEsd(param, -abs(param_sig)) |
---|
225 | col2 = '' |
---|
226 | if param_sig > 0: |
---|
227 | col2 = ValEsd(param_sig, -param_sig/100) |
---|
228 | return col1,col2 |
---|
229 | |
---|
230 | def Exporter(self, event=None): |
---|
231 | |
---|
232 | # Define function to extract parameter and sigma from covariances, for later use |
---|
233 | def GetParamSig(phase_num, hist_num, keyword, display_name): |
---|
234 | param_index = None |
---|
235 | try: |
---|
236 | param_index = self.OverallParms['Covariance']['varyList'].index(phase_num + ':' + hist_num + keyword) |
---|
237 | except: |
---|
238 | pass |
---|
239 | if param_index is not None: |
---|
240 | param = self.OverallParms['Covariance']['variables'][param_index] |
---|
241 | # Extract parameter uncertainty |
---|
242 | param_sig = self.OverallParms['Covariance']['sig'][param_index] |
---|
243 | # Create dictionary entry containing parameter and sigma in bracket notation |
---|
244 | model_parameters[display_name] = self.ValEsd2col(param, param_sig) |
---|
245 | |
---|
246 | # # set up for export |
---|
247 | self.InitExport(event) |
---|
248 | if self.ExportSelect(): return # set export parameters; get file name |
---|
249 | self.OpenFile() |
---|
250 | wx.BeginBusyCursor() |
---|
251 | |
---|
252 | # Export model parameters in bracket notation |
---|
253 | try: |
---|
254 | # Initialise ordered dictionary to hold model parameters and uncertainties |
---|
255 | model_parameters = OrderedDict() |
---|
256 | |
---|
257 | # load all of the tree into a set of dicts |
---|
258 | self.loadTree() |
---|
259 | # create a dict with refined values and their uncertainties |
---|
260 | self.loadParmDict() |
---|
261 | |
---|
262 | # Initialise phase counter, for later use |
---|
263 | phase_num = 0 |
---|
264 | # Extract lattice parameters and uncertainties, and convert to bracket notation |
---|
265 | for phasedict in self.Phases.items(): |
---|
266 | phasenam = phasedict[0] |
---|
267 | cellList, cellSig = self.GetCell(phasenam) |
---|
268 | # Initialise lattice parameter letter |
---|
269 | lp_letter = "a" |
---|
270 | for i in range(0, len(cellList)): |
---|
271 | # for cell in cellList: |
---|
272 | if cellSig[i] > 0: |
---|
273 | # Formulate lattice parameter |
---|
274 | model_parameters[phasenam + " " + lp_letter + " (Ã
)"] = self.ValEsd2col(cellList[i], cellSig[i]) |
---|
275 | # Increment lattice parameter letter |
---|
276 | lp_letter = chr(ord(lp_letter[0]) + 1) |
---|
277 | else: |
---|
278 | break |
---|
279 | |
---|
280 | # Get phase and weight fractions uncertainties, if they have been refined |
---|
281 | for hist_num,hist_name in enumerate(self.Histograms): |
---|
282 | try: |
---|
283 | # Get phase fraction uncertainty, if phase fractions have been refined |
---|
284 | phasefrac_unc = self.sigDict[str(phase_num) + ':' + str(hist_num) + ':Scale'] |
---|
285 | # Get name of histogram associated with this phase, for later use |
---|
286 | #hist_name = list(self.Histograms.keys())[hist_num] |
---|
287 | # Extract phase fraction value |
---|
288 | phasefrac = phasedict[1]['Histograms'][hist_name]['Scale'][0] |
---|
289 | # Write phase if there is more than one histogram, specify which one |
---|
290 | if len(self.Histograms) > 1: |
---|
291 | model_parameters[phasenam + " Phase Fraction in: " + hist_name] = ( |
---|
292 | self.ValEsd2col(phasefrac, phasefrac_unc)) |
---|
293 | # If there is only one histogram, no need to specify which histogram the fraction is based on |
---|
294 | else: |
---|
295 | model_parameters[phasenam + " Phase Fraction"] = self.ValEsd2col(phasefrac, phasefrac_unc) |
---|
296 | except: |
---|
297 | pass |
---|
298 | |
---|
299 | try: |
---|
300 | var = str(phase_num) + ':' + str(hist_num) + ':WgtFrac' |
---|
301 | depSigDict = self.OverallParms['Covariance'].get('depSigDict',{}) |
---|
302 | weight_frac,weight_frac_unc = depSigDict.get(var,[0,None]) |
---|
303 | |
---|
304 | # Write phase + weight fractions in bracket notation to dictionary, to be exported as a CSV |
---|
305 | # If there is more than one histogram, specify which one the fraction is based on |
---|
306 | if len(self.Histograms) > 1: |
---|
307 | model_parameters[phasenam + " Weight Fraction in: " + hist_name] = ( |
---|
308 | self.ValEsd2col(weight_frac, weight_frac_unc)) |
---|
309 | # If there is only one histogram, no need to specify which histogram the fraction is based on |
---|
310 | else: |
---|
311 | model_parameters[phasenam + " Weight Fraction"] = self.ValEsd2col(weight_frac, weight_frac_unc) |
---|
312 | except: |
---|
313 | pass |
---|
314 | |
---|
315 | # Get preferred orientation details for phase, if refined |
---|
316 | try: |
---|
317 | pref_orr_props = phasedict[1]['Histograms'][hist_name]['Pref.Ori.'] |
---|
318 | # Check if March Dollase has been refined |
---|
319 | if pref_orr_props[2] and pref_orr_props[0] == "MD": |
---|
320 | # If so, first extract MD axis and write to dictionary to be exported |
---|
321 | MD_axis = "\'" + ''.join(map(str, pref_orr_props[3])) |
---|
322 | model_parameters[phasenam + " March Dollase Axis"] = (MD_axis,'') |
---|
323 | # Next, extract MD ratio |
---|
324 | MD_ratio = pref_orr_props[1] |
---|
325 | MD_sig = self.sigDict[str(phase_num)+":" + str(hist_num) + ":MD"] |
---|
326 | # Formulate MD ratio in bracket notation |
---|
327 | MD_bracket = self.ValEsd2col(MD_ratio, MD_sig) |
---|
328 | # Write MD ratio to dictionary to be exported |
---|
329 | model_parameters[phasenam + " March Dollase Ratio"] = (MD_bracket,'') |
---|
330 | except: |
---|
331 | pass |
---|
332 | # Increment phase number counter |
---|
333 | phase_num += 1 |
---|
334 | |
---|
335 | # Extract sample displacements, zero offset and D(ij)s (if refined) |
---|
336 | for i,hist_name in enumerate(self.Histograms): |
---|
337 | hist_num = str(i) |
---|
338 | # Extract zero offset, if refined |
---|
339 | GetParamSig("", hist_num, ':Zero', "Zero Offset") |
---|
340 | # Extract Bragg-Brentano sample displacement, if refined |
---|
341 | GetParamSig("", hist_num, ':Shift', "Sample Displacement (micron)") |
---|
342 | # Extract Debye-Scherrer sample X displacement, if refined |
---|
343 | GetParamSig("", hist_num, ':DisplaceX', "Sample X Displacement (micron)") |
---|
344 | # Extract Debye-Scherrer sample Y displacement, if refined |
---|
345 | GetParamSig("", hist_num, ':DisplaceY', "Sample Y Displacement (micron)") |
---|
346 | # Extract hydrostatic strains, if refined |
---|
347 | for phase_num,phase_name in enumerate(self.Phases): |
---|
348 | for d_i in range(1, 4): |
---|
349 | for d_j in range(1, 4): |
---|
350 | GetParamSig(str(phase_num), hist_num, ':D' + str(d_i) + str(d_j), |
---|
351 | phase_name + ' D' + str(d_i) + str(d_j)) |
---|
352 | |
---|
353 | # Extract atomic parameters, if refined |
---|
354 | for phase_num,phase_name in enumerate(self.Phases): |
---|
355 | # atom_list = list(self.Phases.values())[phase_num]["Atoms"] |
---|
356 | atom_list = self.Phases[phase_name]["Atoms"] # same as above? |
---|
357 | for atom_num,atom in enumerate(atom_list): |
---|
358 | # Extract isotropic thermal parameters, if refined |
---|
359 | GetParamSig(str(phase_num), ':', 'AUiso:' + str(atom_num), |
---|
360 | phase_name + ' ' + atom[0] + ' Uiso') |
---|
361 | # Extract anisotropic thermal parameters (Uijs), if refined |
---|
362 | for Ui in range(1, 4): |
---|
363 | for Uj in range(1, 4): |
---|
364 | GetParamSig(str(phase_num), ':', 'AU' + str(Ui) + str(Uj) + ':' + str(atom_num), |
---|
365 | phase_name + ' ' + atom[0] + ' U' + str(Ui) + str(Uj)) |
---|
366 | # Extract fractional occupancies, if refined |
---|
367 | GetParamSig(str(phase_num), ':', 'Afrac:' + str(atom_num), |
---|
368 | phase_name + ' ' + atom[0] + ' Occupancy') |
---|
369 | # Extract atom X Y Z, if refined |
---|
370 | for atom_axis in ('x', 'y', 'z'): |
---|
371 | variable_code = str(phase_num) + ':' + ':' + 'dA' + atom_axis + ':' + str(atom_num) |
---|
372 | # Get sigma, if refined |
---|
373 | try: |
---|
374 | param_index = self.OverallParms['Covariance']['varyList'].index(variable_code) |
---|
375 | # Extract uncertainty |
---|
376 | atom_axis_sig = self.OverallParms['Covariance']['sig'][param_index] |
---|
377 | # Extract value |
---|
378 | atom_axis_val = list(self.Phases.values())[phase_num]["Atoms"][atom_num][ord(atom_axis)-117] |
---|
379 | # Convert to bracket notation and add to dictionary, which will be exported as a CSV |
---|
380 | model_parameters[phase_name + ' ' + atom[0] + ' ' + atom_axis] = \ |
---|
381 | self.ValEsd2col(atom_axis_val, atom_axis_sig) |
---|
382 | except: pass |
---|
383 | |
---|
384 | # Extract rWp |
---|
385 | rWP = self.OverallParms['Covariance']['Rvals']['Rwp'] |
---|
386 | # Write to dictionary to be printed, rounding to 3 significant figures for readability |
---|
387 | model_parameters["wR"] = (ValEsd(rWP, -0.1) + '%', '') |
---|
388 | |
---|
389 | # Write to CSV |
---|
390 | for name in model_parameters: |
---|
391 | self.Write('{:}, {:}, {:}'.format(name,*model_parameters[name])) |
---|
392 | |
---|
393 | finally: |
---|
394 | wx.EndBusyCursor() |
---|
395 | self.CloseFile() |
---|
396 | |
---|