FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
diffpy.pdfgui/src/diffpy/pdfgui/control/pdfguimacros.py at cookie · diffpy/diffpy.pdfgui · GitHub
diffpy
diffpy.pdfgui
Repository navigation
Code
Issues
24
(24)
Pull requests
2
(2)
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
diffpy.pdfgui
/
src
/
diffpy
/
pdfgui
/
control
/
pdfguimacros.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
389 lines (324 loc) · 12.9 KB
Breadcrumbs
diffpy.pdfgui
/
src
/
diffpy
/
pdfgui
/
control
/
pdfguimacros.py
Copy path
File metadata and controls
389 lines (324 loc) · 12.9 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#!/usr/bin/env python
##############################################################################
#
# PDFgui by DANSE Diffraction group
# Simon J. L. Billinge
# (c) 2006 trustees of the Michigan State University.
# All rights reserved.
#
# File coded by: Chris Farrow
#
# See AUTHORS.txt for a list of people who contributed.
# See LICENSE.txt for license information.
#
##############################################################################
"""Methods for macros used in pdfgui."""
from
__future__
import
print_function
import
copy
import
os
from
diffpy
.
pdfgui
.
control
.
controlerrors
import
ControlValueError
from
diffpy
.
pdfgui
.
control
.
fitdataset
import
FitDataSet
def
makeRSeries
(
control
,
fit
,
maxfirst
=
None
,
maxlast
=
None
,
maxstep
=
None
,
minfirst
=
None
,
minlast
=
None
,
minstep
=
None
,
):
"""Make an series of fits with an increasing r-range.
The new fits are appended to the end of any current fits in the control.
control -- The control object that will contain the fits
fit -- The prototype fit
maxfirst -- The first value of the maximum of the fit range
maxlast -- The last value of the maximum of the fit range
maxstep -- The step size of the maximum of the fit range
minfirst -- The first value of the minimum of the fit range
minlast -- The last value of the minimum of the fit range
minstep -- The step size of the minimum of the fit range
returns a list of the new fit organization objects
"""
# Check to see if the input values are correct.
# MIN-MIN: FIRST < LAST
if
minfirst
is
not
None
and
minlast
is
not
None
and
not
minfirst
<
minlast
:
message
=
(
"The first value of the minimum (%.2f)
\
\n
must be less than the last value of the
\
\n
minimum (%.2f)"
%
(
minfirst
,
minlast
)
)
raise
ControlValueError
(
message
)
# MAX-MAX: FIRST < LAST
if
maxfirst
is
not
None
and
maxlast
is
not
None
and
not
maxfirst
<
maxlast
:
message
=
(
"The first value of the maximum (%.2f)
\
\n
must be less than the last value of the
\
\n
maximum (%.2f)"
%
(
maxfirst
,
maxlast
)
)
raise
ControlValueError
(
message
)
# MAX > MIN: FIRST-FIRST
if
maxfirst
is
not
None
and
minfirst
is
not
None
and
not
maxfirst
>
minfirst
:
message
=
(
"The first value of the fit maximum (%.2f)
\
\n
must be greater than first value of the fit
\
\n
minimum (%.2f)."
%
(
maxfirst
,
minfirst
)
)
raise
ControlValueError
(
message
)
# MAX > MIN: LAST-LAST
if
maxlast
is
not
None
and
minlast
is
not
None
and
not
maxlast
>
minlast
:
message
=
(
"The last value of the fit maximum (%.2f)
\
\n
must be greater than last value of the fit
\
\n
minimum (%.2f)."
%
(
maxlast
,
minlast
)
)
raise
ControlValueError
(
message
)
# STEP > 0
message
=
"Step size (%.2f) must be greater than 0."
if
maxstep
is
not
None
and
not
maxstep
>
0
:
raise
ControlValueError
(
message
%
maxstep
)
if
minstep
is
not
None
and
not
minstep
>
0
:
raise
ControlValueError
(
message
%
minstep
)
# Check to see that either max or min is fully specified
maxlist
=
[
maxfirst
,
maxlast
]
minlist
=
[
minfirst
,
minlast
]
if
maxlist
.
count
(
None
)
==
1
or
minlist
.
count
(
None
)
==
1
:
raise
ControlValueError
(
"First and last values are partially specified"
)
if
maxstep
is
None
and
minstep
is
None
:
raise
ControlValueError
(
"Either minstep or maxstep must be specified."
)
maxlist
=
[]
minlist
=
[]
if
maxfirst
is
not
None
:
if
maxstep
is
None
:
maxstep
=
minstep
maxrange
=
int
((
maxlast
-
maxfirst
)
/
(
1.0
*
maxstep
)
+
1
)
maxlist
=
[
maxfirst
+
i
*
maxstep
for
i
in
range
(
maxrange
)]
if
minfirst
is
not
None
:
if
minstep
is
None
:
minstep
=
maxstep
minrange
=
int
((
minlast
-
minfirst
)
/
(
1.0
*
minstep
)
+
1
)
minlist
=
[
minfirst
+
i
*
minstep
for
i
in
range
(
minrange
)]
# Resize the lists to the length of the shortest
serieslen
=
min
(
len
(
maxlist
),
len
(
minlist
))
if
serieslen
!=
0
:
maxlist
=
maxlist
[:
serieslen
]
minlist
=
minlist
[:
serieslen
]
else
:
serieslen
=
max
(
len
(
maxlist
),
len
(
minlist
))
basename
=
fit
.
name
fits
=
[]
newname
=
""
lastname
=
""
fitcopy
=
control
.
copy
(
fit
)
# Duplicate the original fit and change the appropriate parameters.
for
i
in
range
(
serieslen
):
lastname
=
newname
# Loop over datasets
for
ds
in
fitcopy
.
datasets
:
if
minlist
:
fitrmin
=
minlist
[
i
]
else
:
fitrmin
=
ds
.
fitrmin
if
maxlist
:
fitrmax
=
maxlist
[
i
]
else
:
fitrmax
=
ds
.
fitrmax
# Check to see that the values are in bounds and sensical
if
fitrmin
<
ds
.
rmin
or
fitrmin
>=
ds
.
rmax
:
message
=
(
"Fit minimum (%.2f) is outside the data range
\
\n
[%.2f, %.2f].
\
\n
Adjust the range of the series."
%
(
fitrmin
,
ds
.
rmin
,
ds
.
rmax
)
)
raise
ControlValueError
(
message
)
if
fitrmax
<=
ds
.
rmin
or
fitrmax
>
ds
.
rmax
:
message
=
(
"Fit maximum (%.2f) is outside the data range
\
\n
[%.2f, %.2f].
\
\n
Adjust the range of the series."
%
(
fitrmax
,
ds
.
rmin
,
ds
.
rmax
)
)
raise
ControlValueError
(
message
)
if
fitrmin
>=
fitrmax
:
message
=
(
"Fit minimum (%.2f) is greater than the
\
\n
maximum (%.2f).
\
\n
Increase maxstep or reduce minstep."
%
(
fitrmin
,
fitrmax
)
)
raise
ControlValueError
(
message
)
# Set the values if all is well
if
minlist
:
ds
.
fitrmin
=
fitrmin
if
maxlist
:
ds
.
fitrmax
=
fitrmax
# Set the parameters to the previous fit's name, if one exists.
if
lastname
:
parval
=
"=%s"
%
lastname
for
par
in
fitcopy
.
parameters
.
values
():
par
.
setInitial
(
parval
)
# Now paste the copy into the control.
newname
=
"%s-(%.2f,%.2f)"
%
(
basename
,
fitrmin
,
fitrmax
)
o
=
control
.
paste
(
fitcopy
,
new_name
=
newname
)
fits
.
append
(
o
)
return
[
f
.
organization
()
for
f
in
fits
]
# Temperature Series
def
makeTemperatureSeries
(
control
,
fit
,
paths
,
temperatures
):
"""Make a temperature series.
control -- pdguicontrol instance
fit -- The template fit
paths -- list of path names of new datasets
temperatures -- list of temperatures corresponding to the datasets
returns a list of the new fit organization objects
"""
if
len
(
fit
.
datasets
)
!=
1
:
message
=
"Can't apply macro to fits with multiple datasets."
raise
ControlValueError
(
message
)
fits
=
[]
# holds all of the other information about the dataset
fitbasename
=
fit
.
name
fitnewname
=
fit
.
name
fitlastname
=
fit
.
name
dataset
=
fit
.
datasets
[
0
]
for
i
in
range
(
len
(
paths
)):
filename
=
paths
[
i
]
fitlastname
=
fitnewname
fitcopy
=
control
.
copy
(
fit
)
# Get rid of the old dataset
temp
=
fitcopy
.
datasets
[
0
]
fitcopy
.
remove
(
temp
)
# Configure the new dataset
dsname
=
os
.
path
.
basename
(
filename
)
newdataset
=
FitDataSet
(
dsname
)
newdataset
.
readObs
(
filename
)
newdataset
.
qdamp
=
dataset
.
qdamp
newdataset
.
qbroad
=
dataset
.
qbroad
newdataset
.
dscale
=
dataset
.
dscale
newdataset
.
fitrmin
=
dataset
.
fitrmin
newdataset
.
fitrmax
=
dataset
.
fitrmax
rstep
=
dataset
.
fitrstep
st
=
dataset
.
getFitSamplingType
()
newdataset
.
setFitSamplingType
(
st
,
rstep
)
doping
=
dataset
.
metadata
.
get
(
"doping"
)
if
doping
is
None
:
doping
=
0.0
newdataset
.
metadata
[
"doping"
]
=
doping
newdataset
.
constraints
=
copy
.
deepcopy
(
dataset
.
constraints
)
# Set the chosen temperature
newdataset
.
metadata
[
"temperature"
]
=
temperatures
[
i
]
# Add the dataset to the fitcopy
fitcopy
.
add
(
newdataset
,
None
)
# Set the parameters to the previous fit's name, if one exists.
if
fitlastname
:
parval
=
"=%s"
%
fitlastname
for
par
in
fitcopy
.
parameters
.
values
():
par
.
setInitial
(
parval
)
# Now paste the copy into the control.
fitnewname
=
"%s-T%i=%g"
%
(
fitbasename
,
i
+
1
,
temperatures
[
i
])
o
=
control
.
paste
(
fitcopy
,
new_name
=
fitnewname
)
fits
.
append
(
o
)
return
[
f
.
organization
()
for
f
in
fits
]
# Doping Series
def
makeDopingSeries
(
control
,
fit
,
base
,
dopant
,
paths
,
doping
):
"""Make a temperature series.
control -- pdguicontrol instance
fit -- The template fit
base -- Name of the base element
dopant -- Name of the dopant element
paths -- list of path names of new datasets
doping -- list of doping values corresponding to the datasets
returns a list of the new fit organization objects
"""
from
diffpy
.
pdffit2
import
is_element
# Make sure that base and dopant are elements
base
=
base
.
title
()
dopant
=
dopant
.
title
()
if
not
is_element
(
base
):
raise
ControlValueError
(
"'%s' is not an element!"
%
base
)
if
not
is_element
(
dopant
):
raise
ControlValueError
(
"'%s' is not an element!"
%
dopant
)
# Make sure that base and dopant are in the structure file(s)
hasBase
=
False
hasDopant
=
False
for
S
in
fit
.
strucs
:
for
atom
in
S
:
if
atom
.
element
==
base
:
hasBase
=
True
if
atom
.
element
==
dopant
:
hasDopant
=
True
if
hasBase
and
hasDopant
:
break
if
not
hasBase
:
message
=
"The template structure does not contain the base atom."
raise
ControlValueError
(
message
)
if
not
hasDopant
:
message
=
"The template structure does not contain the dopant atom."
raise
ControlValueError
(
message
)
# Make sure we're only replacing a single dataset
if
len
(
fit
.
datasets
)
!=
1
:
message
=
"Can't apply macro to fits with multiple datasets."
raise
ControlValueError
(
message
)
fits
=
[]
# holds all of the other information about the dataset
fitbasename
=
fit
.
name
fitnewname
=
fit
.
name
fitlastname
=
fit
.
name
dataset
=
fit
.
datasets
[
0
]
for
i
in
range
(
len
(
paths
)):
filename
=
paths
[
i
]
fitlastname
=
fitnewname
fitcopy
=
control
.
copy
(
fit
)
# Get rid of the old dataset
temp
=
fitcopy
.
datasets
[
0
]
fitcopy
.
remove
(
temp
)
# Configure the new dataset
dsname
=
os
.
path
.
basename
(
filename
)
newdataset
=
FitDataSet
(
dsname
)
newdataset
.
readObs
(
filename
)
newdataset
.
qdamp
=
dataset
.
qdamp
newdataset
.
qbroad
=
dataset
.
qbroad
newdataset
.
dscale
=
dataset
.
dscale
newdataset
.
fitrmin
=
dataset
.
fitrmin
newdataset
.
fitrmax
=
dataset
.
fitrmax
rstep
=
dataset
.
fitrstep
st
=
dataset
.
getFitSamplingType
()
newdataset
.
setFitSamplingType
(
st
,
rstep
)
temperature
=
dataset
.
metadata
.
get
(
"temperature"
)
if
temperature
is
None
:
temperature
=
300.0
newdataset
.
metadata
[
"temperature"
]
=
temperature
newdataset
.
constraints
=
copy
.
deepcopy
(
dataset
.
constraints
)
# Set the chosen temperature
newdataset
.
metadata
[
"doping"
]
=
doping
[
i
]
# Add the dataset to the fitcopy
fitcopy
.
add
(
newdataset
,
None
)
# Update the doping information in the structures
for
S
in
fitcopy
.
strucs
:
for
A
in
S
:
if
A
.
element
==
dopant
:
A
.
occupancy
=
doping
[
i
]
if
A
.
element
==
base
:
A
.
occupancy
=
1
-
doping
[
i
]
# Set the parameters to the previous fit's name, if one exists.
if
fitlastname
:
parval
=
"=%s"
%
fitlastname
for
par
in
fitcopy
.
parameters
.
values
():
par
.
setInitial
(
parval
)
# Now paste the copy into the control.
fitnewname
=
"%s-%1.4f"
%
(
fitbasename
,
doping
[
i
])
o
=
control
.
paste
(
fitcopy
,
new_name
=
fitnewname
)
fits
.
append
(
o
)
return
[
f
.
organization
()
for
f
in
fits
]
if
__name__
==
"__main__"
:
from
diffpy
.
pdfgui
.
control
.
pdfguicontrol
import
PDFGuiControl
control
=
PDFGuiControl
()
control
.
load
(
"../../tests/testdata/ni.ddp"
)
fit
=
control
.
fits
[
0
]
olist
=
makeRSeries
(
control
,
fit
,
5
,
20
,
5
)
print
(
"
\n
"
.
join
(
f
[
0
].
name
for
f
in
olist
))
# End of file
Back
|
FazBrowse Home
|
New Git URL