diffpy.srfit 3.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. diffpy/__init__.py +15 -0
  2. diffpy/srfit/__init__.py +41 -0
  3. diffpy/srfit/equation/__init__.py +31 -0
  4. diffpy/srfit/equation/builder.py +702 -0
  5. diffpy/srfit/equation/equationmod.py +213 -0
  6. diffpy/srfit/equation/literals/__init__.py +71 -0
  7. diffpy/srfit/equation/literals/abcs.py +81 -0
  8. diffpy/srfit/equation/literals/argument.py +76 -0
  9. diffpy/srfit/equation/literals/literal.py +69 -0
  10. diffpy/srfit/equation/literals/operators.py +380 -0
  11. diffpy/srfit/equation/visitors/__init__.py +94 -0
  12. diffpy/srfit/equation/visitors/argfinder.py +62 -0
  13. diffpy/srfit/equation/visitors/printer.py +120 -0
  14. diffpy/srfit/equation/visitors/swapper.py +142 -0
  15. diffpy/srfit/equation/visitors/validator.py +113 -0
  16. diffpy/srfit/equation/visitors/visitor.py +62 -0
  17. diffpy/srfit/exceptions.py +29 -0
  18. diffpy/srfit/fitbase/__init__.py +53 -0
  19. diffpy/srfit/fitbase/calculator.py +110 -0
  20. diffpy/srfit/fitbase/configurable.py +57 -0
  21. diffpy/srfit/fitbase/constraint.py +117 -0
  22. diffpy/srfit/fitbase/fitcontribution.py +351 -0
  23. diffpy/srfit/fitbase/fithook.py +227 -0
  24. diffpy/srfit/fitbase/fitrecipe.py +826 -0
  25. diffpy/srfit/fitbase/fitresults.py +674 -0
  26. diffpy/srfit/fitbase/parameter.py +314 -0
  27. diffpy/srfit/fitbase/parameterset.py +110 -0
  28. diffpy/srfit/fitbase/profile.py +405 -0
  29. diffpy/srfit/fitbase/profilegenerator.py +168 -0
  30. diffpy/srfit/fitbase/profileparser.py +179 -0
  31. diffpy/srfit/fitbase/recipeorganizer.py +1074 -0
  32. diffpy/srfit/fitbase/restraint.py +117 -0
  33. diffpy/srfit/fitbase/simplerecipe.py +230 -0
  34. diffpy/srfit/fitbase/validatable.py +59 -0
  35. diffpy/srfit/interface/__init__.py +33 -0
  36. diffpy/srfit/interface/interface.py +180 -0
  37. diffpy/srfit/pdf/__init__.py +24 -0
  38. diffpy/srfit/pdf/basepdfgenerator.py +296 -0
  39. diffpy/srfit/pdf/characteristicfunctions.py +432 -0
  40. diffpy/srfit/pdf/debyepdfgenerator.py +115 -0
  41. diffpy/srfit/pdf/pdfcontribution.py +311 -0
  42. diffpy/srfit/pdf/pdfgenerator.py +77 -0
  43. diffpy/srfit/pdf/pdfparser.py +226 -0
  44. diffpy/srfit/sas/__init__.py +30 -0
  45. diffpy/srfit/sas/prcalculator.py +139 -0
  46. diffpy/srfit/sas/sasgenerator.py +70 -0
  47. diffpy/srfit/sas/sasimport.py +66 -0
  48. diffpy/srfit/sas/sasparameter.py +70 -0
  49. diffpy/srfit/sas/sasparser.py +133 -0
  50. diffpy/srfit/sas/sasprofile.py +94 -0
  51. diffpy/srfit/structure/__init__.py +60 -0
  52. diffpy/srfit/structure/basestructureparset.py +62 -0
  53. diffpy/srfit/structure/bvsrestraint.py +101 -0
  54. diffpy/srfit/structure/cctbxparset.py +292 -0
  55. diffpy/srfit/structure/diffpyparset.py +303 -0
  56. diffpy/srfit/structure/objcrystparset.py +1552 -0
  57. diffpy/srfit/structure/sgconstraints.py +761 -0
  58. diffpy/srfit/structure/srrealparset.py +92 -0
  59. diffpy/srfit/util/__init__.py +50 -0
  60. diffpy/srfit/util/argbinders.py +29 -0
  61. diffpy/srfit/util/inpututils.py +50 -0
  62. diffpy/srfit/util/nameutils.py +45 -0
  63. diffpy/srfit/util/observable.py +93 -0
  64. diffpy/srfit/util/tagmanager.py +150 -0
  65. diffpy/srfit/util/weakrefcallable.py +170 -0
  66. diffpy/srfit/version.py +26 -0
  67. diffpy_srfit-3.1.0.dist-info/METADATA +216 -0
  68. diffpy_srfit-3.1.0.dist-info/RECORD +73 -0
  69. diffpy_srfit-3.1.0.dist-info/WHEEL +5 -0
  70. diffpy_srfit-3.1.0.dist-info/licenses/AUTHORS.rst +10 -0
  71. diffpy_srfit-3.1.0.dist-info/licenses/LICENSE.rst +138 -0
  72. diffpy_srfit-3.1.0.dist-info/licenses/LICENSE_DANSE.rst +34 -0
  73. diffpy_srfit-3.1.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,314 @@
1
+ #!/usr/bin/env python
2
+ ##############################################################################
3
+ #
4
+ # diffpy.srfit by DANSE Diffraction group
5
+ # Simon J. L. Billinge
6
+ # (c) 2008 The Trustees of Columbia University
7
+ # in the City of New York. All rights reserved.
8
+ #
9
+ # File coded by: Chris Farrow
10
+ #
11
+ # See AUTHORS.txt for a list of people who contributed.
12
+ # See LICENSE_DANSE.txt for license information.
13
+ #
14
+ ##############################################################################
15
+ """Parameter classes.
16
+
17
+ Parameters encapsulate an adjustable parameter within SrFit.
18
+ """
19
+
20
+ # IDEA - Add onConstrain, onRestrain, onVary so that adaptors to Parameters
21
+ # can have more fine control over the construction of FitRecipes.
22
+ # IDEA - Add tags to parameters so they can be easily retrieved.
23
+ # IDEA - Consider scaling parameters to avoid precision issues in optimizers.
24
+
25
+ __all__ = ["Parameter", "ParameterProxy", "ParameterAdapter"]
26
+
27
+ from functools import wraps
28
+
29
+ import numpy
30
+
31
+ from diffpy.srfit.equation.literals import Argument
32
+ from diffpy.srfit.exceptions import SrFitError
33
+ from diffpy.srfit.fitbase.validatable import Validatable
34
+ from diffpy.srfit.interface import _parameter_interface
35
+ from diffpy.srfit.util.argbinders import bind2nd
36
+ from diffpy.srfit.util.nameutils import validateName
37
+
38
+
39
+ class Parameter(_parameter_interface, Argument, Validatable):
40
+ """Parameter class.
41
+
42
+ Attributes
43
+ name -- A name for this Parameter.
44
+ const -- A flag indicating whether this is considered a constant.
45
+ _value -- The value of the Parameter. Modified with 'setValue'.
46
+ value -- Property for 'getValue' and 'setValue'.
47
+ constrained -- A flag indicating if the Parameter is constrained
48
+ (default False).
49
+ bounds -- A 2-list defining the bounds on the Parameter. This can be
50
+ used by some optimizers when the Parameter is varied. See
51
+ FitRecipe.getBounds and FitRecipe.boundsToRestraints.
52
+ """
53
+
54
+ def __init__(self, name, value=None, const=False):
55
+ """Initialization.
56
+
57
+ name -- The name of this Parameter (must be a valid attribute
58
+ identifier)
59
+ value -- The initial value of this Parameter (default 0).
60
+ const -- A flag inticating whether the Parameter is a constant (like
61
+ pi).
62
+
63
+ Raises ValueError if the name is not a valid attribute identifier
64
+ """
65
+ self.constrained = False
66
+ self.bounds = [-numpy.inf, +numpy.inf]
67
+ validateName(name)
68
+ Argument.__init__(self, name, value, const)
69
+ return
70
+
71
+ def setValue(self, val):
72
+ """Set the value of the Parameter and the bounds.
73
+
74
+ val -- The value to assign.
75
+ lb -- The lower bounds for the bounds list. If this is None
76
+ (default), then the lower bound will not be alterered.
77
+ ub -- The upper bounds for the bounds list. If this is None
78
+ (default), then the upper bound will not be alterered.
79
+
80
+ Returns self so that mutators can be chained.
81
+ """
82
+ Argument.setValue(self, val)
83
+ return self
84
+
85
+ def setConst(self, const=True, value=None):
86
+ """Toggle the Parameter as constant.
87
+
88
+ const -- Flag indicating if the parameter is constant (default
89
+ True).
90
+ value -- An optional value for the parameter (default None). If this
91
+ is not None, then the parameter will get a new value,
92
+ constant or otherwise.
93
+
94
+ Returns self so that mutators can be chained.
95
+ """
96
+ self.const = bool(const)
97
+ if value is not None:
98
+ self.setValue(value)
99
+ return self
100
+
101
+ def boundRange(self, lb=None, ub=None):
102
+ """Set lower and upper bound of the Parameter.
103
+
104
+ lb -- The lower bound for the bounds list.
105
+ ub -- The upper bound for the bounds list.
106
+
107
+ Returns self so that mutators can be chained.
108
+ """
109
+ if lb is not None:
110
+ self.bounds[0] = lb
111
+ if ub is not None:
112
+ self.bounds[1] = ub
113
+ return self
114
+
115
+ def boundWindow(self, lr=0, ur=None):
116
+ """Create bounds centered on the current value of the Parameter.
117
+
118
+ lr -- The radius of the lower bound (default 0). The lower bound is
119
+ computed as value - lr.
120
+ ur -- The radius of the upper bound. The upper bound is computed as
121
+ value + ur. If this is None (default), then the value of the
122
+ lower radius is used.
123
+
124
+ Returns self so that mutators can be chained.
125
+ """
126
+ val = self.getValue()
127
+ lb = val - lr
128
+ if ur is None:
129
+ ur = lr
130
+ ub = val + ur
131
+ self.bounds = [lb, ub]
132
+ return self
133
+
134
+ def _validate(self):
135
+ """Validate my state.
136
+
137
+ This validates that value is not None.
138
+
139
+ Raises SrFitError if validation fails.
140
+ """
141
+ if self.value is None:
142
+ raise SrFitError("value of '%s' is None" % self.name)
143
+ return
144
+
145
+
146
+ # End class Parameter
147
+
148
+
149
+ class ParameterProxy(Parameter):
150
+ """A Parameter proxy for another parameter.
151
+
152
+ This allows for the same parameter to have multiple names.
153
+
154
+ Attributes
155
+ name -- A name for this ParameterProxy. Names should be unique within a
156
+ RecipeOrganizer and should be valid attribute names.
157
+ par -- The Parameter this is a proxy for.
158
+ """
159
+
160
+ def __init__(self, name, par):
161
+ """Initialization.
162
+
163
+ name -- The name of this ParameterProxy.
164
+ par -- The Parameter this is a proxy for.
165
+
166
+ Raises ValueError if the name is not a valid attribute identifier
167
+ """
168
+ validateName(name)
169
+
170
+ self.name = name
171
+ self.par = par
172
+ return
173
+
174
+ # define properties to use attributes of the proxied Parameter -----------
175
+
176
+ @property
177
+ def constrained(self):
178
+ """A flag indicating if the proxied Parameter is constrained."""
179
+ return self.par.constrained
180
+
181
+ @constrained.setter
182
+ def constrained(self, value):
183
+ self.par.constrained = bool(value)
184
+ return
185
+
186
+ @property
187
+ def bounds(self):
188
+ """List of lower and upper bounds of the proxied Parameter.
189
+
190
+ This can be used by some optimizers when the Parameter is
191
+ varied. See FitRecipe.getBounds and
192
+ FitRecipe.boundsToRestraints.
193
+ """
194
+ return self.par.bounds
195
+
196
+ @bounds.setter
197
+ def bounds(self, value):
198
+ self.par.bounds = value
199
+ return
200
+
201
+ @property
202
+ def _observers(self):
203
+ return self.par._observers
204
+
205
+ # wrap Parameter methods to use the target object ------------------------
206
+
207
+ @wraps(Parameter.setValue)
208
+ def setValue(self, val):
209
+ return self.par.setValue(val)
210
+
211
+ @wraps(Parameter.getValue)
212
+ def getValue(self):
213
+ return self.par.getValue()
214
+
215
+ @wraps(Parameter.setConst)
216
+ def setConst(self, const=True, value=None):
217
+ return self.par.setConst(const, value)
218
+
219
+ @wraps(Parameter.boundRange)
220
+ def boundRange(self, lb=None, ub=None):
221
+ return self.par.boundRange(lb, ub)
222
+
223
+ @wraps(Parameter.boundWindow)
224
+ def boundWindow(self, lr=0, ur=None):
225
+ return self.par.boundWindow(lr, ur)
226
+
227
+ def _validate(self):
228
+ """Validate my state.
229
+
230
+ This validates that value and par are not None.
231
+
232
+ Raises SrFitError if validation fails.
233
+ """
234
+ if self.par is None:
235
+ raise SrFitError("par is None")
236
+ self.par._validate()
237
+ return
238
+
239
+
240
+ # End class ParameterProxy
241
+
242
+
243
+ class ParameterAdapter(Parameter):
244
+ """An adapter for parameter-like objects.
245
+
246
+ This class wraps an object as a Parameter. The getValue and setValue
247
+ methods defer to the data of the wrapped object.
248
+ """
249
+
250
+ def __init__(self, name, obj, getter=None, setter=None, attr=None):
251
+ """Wrap an object as a Parameter.
252
+
253
+ name -- The name of this Parameter.
254
+ obj -- The object to be wrapped.
255
+ getter -- The unbound function that can be used to access the
256
+ attribute containing the parameter value. getter(obj)
257
+ should return the Parameter value. If getter is None
258
+ (default), it is assumed that an attribute is accessed
259
+ via attr. If attr is also specified, then the Parameter
260
+ value will be accessed via getter(obj, attr).
261
+ setter -- The unbound function that can be used to modify the
262
+ attribute containing the parameter value.
263
+ setter(obj, value) should set the attribute to the
264
+ passed value. If setter is None (default), it is assumed
265
+ that an attribute is accessed via attr. If attr is also
266
+ specified, then the Parameter value will be set via
267
+ setter(obj, attr, value).
268
+ attr -- The name of the attribute that contains the value of the
269
+ parameter. If attr is None (default), then both getter and
270
+ setter must be specified.
271
+
272
+ Raises ValueError if exactly one of getter or setter is not None, or if
273
+ getter, setter and attr are all None.
274
+ """
275
+ if getter is None and setter is None and attr is None:
276
+ raise ValueError("Specify attribute access")
277
+ if [getter, setter].count(None) == 1:
278
+ raise ValueError("Specify both getter and setter")
279
+
280
+ self.obj = obj
281
+ self.getter = getter
282
+ self.setter = setter
283
+ self.attr = attr
284
+
285
+ if attr is not None:
286
+ if getter is None:
287
+ self.getter = bind2nd(getattr, self.attr)
288
+ else:
289
+ self.getter = bind2nd(getter, self.attr)
290
+
291
+ if setter is None:
292
+ self.setter = bind2nd(setattr, self.attr)
293
+ else:
294
+ self.setter = bind2nd(setter, self.attr)
295
+
296
+ value = self.getValue()
297
+ Parameter.__init__(self, name, value)
298
+ return
299
+
300
+ def getValue(self):
301
+ """Get the value of the Parameter."""
302
+ return self.getter(self.obj)
303
+
304
+ def setValue(self, value):
305
+ """Set the value of the Parameter."""
306
+ if value != self.getValue():
307
+ self.setter(self.obj, value)
308
+ self.notify()
309
+ return self
310
+
311
+
312
+ # End class ParameterAdapter
313
+
314
+ # End of file
@@ -0,0 +1,110 @@
1
+ #!/usr/bin/env python
2
+ ##############################################################################
3
+ #
4
+ # diffpy.srfit by DANSE Diffraction group
5
+ # Simon J. L. Billinge
6
+ # (c) 2008 The Trustees of Columbia University
7
+ # in the City of New York. All rights reserved.
8
+ #
9
+ # File coded by: Chris Farrow
10
+ #
11
+ # See AUTHORS.txt for a list of people who contributed.
12
+ # See LICENSE_DANSE.txt for license information.
13
+ #
14
+ ##############################################################################
15
+ """ParameterSet class.
16
+
17
+ ParameterSets organize Parameters, Constraints, Restraints and other
18
+ ParameterSets. They provide attribute-access of other ParameterSets and
19
+ embedded Parameters.
20
+ """
21
+
22
+ __all__ = ["ParameterSet"]
23
+
24
+
25
+ from collections import OrderedDict
26
+
27
+ from diffpy.srfit.fitbase.recipeorganizer import RecipeOrganizer
28
+
29
+
30
+ class ParameterSet(RecipeOrganizer):
31
+ """Class for organizing Parameters and other ParameterSets.
32
+
33
+ ParameterSets are hierarchical organizations of Parameters, Constraints,
34
+ Restraints and other ParameterSets.
35
+
36
+ Contained Parameters and other ParameterSets can be accessed by name as
37
+ attributes in order to facilitate multi-level constraints and restraints.
38
+ These constraints and restraints can be placed at any level and a flattened
39
+ list of them can be retrieved with the getConstraints and getRestraints
40
+ methods.
41
+
42
+ Attributes
43
+ name -- A name for this organizer.
44
+ _calculators -- A managed dictionary of Calculators, indexed by name.
45
+ _constraints -- A set of constrained Parameters. Constraints can be
46
+ added using the 'constrain' methods.
47
+ _parameters -- A managed OrderedDict of parameters.
48
+ _restraints -- A set of Restraints. Restraints can be added using the
49
+ 'restrain' methods.
50
+ _parsets -- A managed dictionary of ParameterSets.
51
+ _eqfactory -- A diffpy.srfit.equation.builder.EquationFactory
52
+ instance that is used to create constraints and
53
+ restraints from string equations.
54
+
55
+ Properties
56
+ names -- Variable names (read only). See getNames.
57
+ values -- Variable values (read only). See getValues.
58
+ """
59
+
60
+ def __init__(self, name):
61
+ """Initialize.
62
+
63
+ name -- The name of this ParameterSet.
64
+ """
65
+ RecipeOrganizer.__init__(self, name)
66
+
67
+ self._parsets = OrderedDict()
68
+ self._manage(self._parsets)
69
+ return
70
+
71
+ # Alias Parameter accessors.
72
+ addParameter = RecipeOrganizer._addParameter
73
+ newParameter = RecipeOrganizer._newParameter
74
+ removeParameter = RecipeOrganizer._removeParameter
75
+
76
+ def addParameterSet(self, parset):
77
+ """Add a ParameterSet to the hierarchy.
78
+
79
+ parset -- The ParameterSet to be stored.
80
+
81
+ Raises ValueError if the ParameterSet has no name.
82
+ Raises ValueError if the ParameterSet has the same name as some other
83
+ managed object.
84
+ """
85
+ self._addObject(parset, self._parsets, True)
86
+ return
87
+
88
+ def removeParameterSet(self, parset):
89
+ """Remove a ParameterSet from the hierarchy.
90
+
91
+ Raises ValueError if parset is not managed by this object.
92
+ """
93
+ self._removeObject(parset, self._parsets)
94
+ return
95
+
96
+ def setConst(self, const=True):
97
+ """Set every parameter within the set to a constant.
98
+
99
+ const -- Flag indicating if the parameter is constant (default
100
+ True).
101
+ """
102
+ for par in self.iterPars():
103
+ par.setConst(const)
104
+
105
+ return
106
+
107
+
108
+ # End class ParameterSet
109
+
110
+ # End of file