-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathorganizer.py
286 lines (226 loc) · 9.13 KB
/
organizer.py
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
#!/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: Jiwu Liu
#
# See AUTHORS.txt for a list of people who contributed.
# See LICENSE.txt for license information.
#
##############################################################################
from diffpy.pdfgui.control.calculation import Calculation
from diffpy.pdfgui.control.controlerrors import ControlTypeError
from diffpy.pdfgui.control.fitdataset import FitDataSet
from diffpy.pdfgui.control.fitstructure import FitStructure
from diffpy.pdfgui.control.pdfcomponent import PDFComponent
class Organizer(PDFComponent):
"""Base class for Fitting. It holds separate lists of datasets,
strucs and calculations
datasets: dataset list
strucs: structure list
calcs: calculation list
"""
def __init__(self, name):
"""initialize
name -- component name
"""
from diffpy.pdfgui.control.pdflist import PDFList
PDFComponent.__init__(self, name)
self.datasets = PDFList()
self.strucs = PDFList()
self.calcs = PDFList()
# self.metadata is created but not pickled only for the purpose
# of plotting. It holds common metadata from all its datasets
self.metadata = {}
# controlCenter is the reference to global PDFGuiControl object
from diffpy.pdfgui.control.pdfguicontrol import pdfguicontrol
self.controlCenter = pdfguicontrol()
def __findList(self, id):
if isinstance(id, FitDataSet):
return self.datasets
elif isinstance(id, FitStructure):
return self.strucs
elif isinstance(id, Calculation):
return self.calcs
else:
emsg = "Unknown type object '%s'" % id.name
raise ControlTypeError(emsg)
def add(self, id, position=None):
"""add structure/dataset/calculation
id -- reference to structure/dataset/calculation
position -- position to insert, by default the last one
"""
objList = self.__findList(id)
if position is None:
position = len(objList)
objList.insert(position, id)
# successfully added, set the object owner
id.owner = self
def remove(self, id):
"""remove structure/dataset/calculation
id -- reference to structure/dataset/calculation
"""
objList = self.__findList(id)
objList.remove(id)
return id
def rename(self, id, newname):
"""rename structure/dataset/calculation
id -- reference to structure/dataset/calculation
newname -- new name to be given
"""
objList = self.__findList(id)
objList.rename(id.name, newname)
def index(self, id):
"""find the position of item in the list
id -- id of object
return : object position
"""
objList = self.__findList(id)
return objList.index(id.name)
def hasStructures(self):
"""Check to see if there are structures."""
return len(self.strucs) > 0
def getStructure(self, pos):
"""get structure by position
pos -- the position of structure in the list
"""
# The function can only be called by gui code. So don't catch IndexError
# Any IndexError is a program bug thus should be propagated as is.
return self.strucs[pos]
def hasDataSets(self):
"""Check to see if there are datasets."""
return len(self.datasets) > 0
def getDataSet(self, pos):
"""get dataset by position
pos -- the position of dataset in the list
"""
# The function can only be called by gui code. So don't catch IndexError
# Any IndexError is a program bug thus should be propagated as is.
return self.datasets[pos]
def hasCalculations(self):
"""Check to see if there are calculations."""
return len(self.calcs) > 0
def getCalculation(self, pos):
"""get calculation by position
pos -- the position of calculation in the list
"""
# The function can only be called by gui code. So don't catch IndexError
# Any IndexError is a program bug thus should be propagated as is.
return self.calcs[pos]
def load(self, z, subpath):
"""load data from a zipped project file
z -- zipped project file
subpath -- path to its own storage within project file
returns a tree of internal hierachy
"""
# subpath = projName/myName/
from diffpy.pdfgui.utils import unquote_plain
subs = subpath.split("/")
rootDict = z.fileTree[subs[0]][subs[1]]
if "structure" in rootDict:
for strucName in rootDict["structure"].keys():
struc = FitStructure(unquote_plain(strucName))
struc.load(z, subpath + "structure/" + strucName + "/")
self.add(struc)
if "dataset" in rootDict:
for datasetName in rootDict["dataset"].keys():
dataset = FitDataSet(unquote_plain(datasetName))
dataset.load(z, subpath + "dataset/" + datasetName + "/")
self.add(dataset)
if "calculation" in rootDict:
for calcName in rootDict["calculation"].keys():
calc = Calculation(unquote_plain(calcName))
calc.load(z, subpath + "calculation/" + calcName + "/")
self.add(calc)
self.__forward_spdiameter()
return self.organization()
def save(self, z, subpath):
"""save data from a zipped project file
z -- zipped project file
subpath -- path to its own storage within project file
"""
# strucs and datasets
from diffpy.pdfgui.utils import quote_plain
for struc in self.strucs:
struc.save(z, subpath + "structure/" + quote_plain(struc.name) + "/")
for dataset in self.datasets:
dataset.save(z, subpath + "dataset/" + quote_plain(dataset.name) + "/")
for calc in self.calcs:
calc.save(z, subpath + "calculation/" + quote_plain(calc.name) + "/")
return
def copy(self, other=None):
"""copy self to other. if other is None, create an instance
other -- ref to other object
returns reference to copied object
"""
if other is None:
other = Organizer(self.name)
for dataset in self.datasets:
other.add(dataset.copy())
for struc in self.strucs:
other.add(struc.copy())
for calc in self.calcs:
other.add(calc.copy())
return other
def organization(self):
"""get internal organization
returns a tree of internal hierachy
"""
org = [None] * 4
org[0] = self
org[1] = []
for dataset in self.datasets:
org[1].append((dataset.name, dataset))
org[2] = []
for struc in self.strucs:
org[2].append((struc.name, struc))
org[3] = []
for calc in self.calcs:
org[3].append((calc.name, calc))
return org
def __forward_spdiameter(strucs, datasets, calcs):
"""Copy spdiameter value loaded from fit or calculation to phase.
This function handles loading old PDFgui projects where
spdiameter belonged to FitDataSet or Calculation classes.
"""
# Jump out if any of structures has spdiameter set
for stru in strucs:
if stru.getvar("spdiameter"):
return
# Helper function to check if spdiameter is assigned
def spd_assigned(ds):
return bool(ds.spdiameter)
# Helper function to check if spdiameter is constrained
def spd_constrained(ds):
return "spdiameter" in ds.constraints
# Figure out the value and constraint for spdiameter.
spd_val = spd_cns = None
constrained_datas = list(filter(spd_constrained, datasets))
assigned_datas = list(filter(spd_assigned, datasets))
assigned_calcs = list(filter(spd_assigned, calcs))
if constrained_datas:
spd_val = constrained_datas[0].spdiameter
spd_cns = constrained_datas[0].constraints["spdiameter"]
elif assigned_datas:
spd_val = assigned_datas[0].spdiameter
elif assigned_calcs:
spd_val = assigned_calcs[0].spdiameter
# Assign spd_val to all structures that don't have it set
for stru in strucs:
if spd_val and not stru.getvar("spdiameter"):
stru.setvar("spdiameter", spd_val)
if spd_cns:
stru.constraints.setdefault("spdiameter", spd_cns)
# Finally remove any spdiameter constraints from all datasets
for ds in datasets:
ds.constraints.pop("spdiameter", None)
return
# End of class Organizer
# simple test code
if __name__ == "__main__":
Organizer("name")
# End of file