forked from diffpy/diffpy.srfit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterface.py
More file actions
183 lines (132 loc) · 4.36 KB
/
interface.py
File metadata and controls
183 lines (132 loc) · 4.36 KB
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
#!/usr/bin/env python
##############################################################################
#
# diffpy.srfit by DANSE Diffraction group
# Simon J. L. Billinge
# (c) 2010 The Trustees of Columbia University
# in the City of New York. All rights reserved.
#
# File coded by: Chris Farrow
#
# See AUTHORS.txt for a list of people who contributed.
# See LICENSE_DANSE.txt for license information.
#
##############################################################################
"""Interface enhancements for Parameter-type classes.
Most interface additions can thought of by considering the classes in
SrFit to be sets of parameters. "+=" adds a new parameter, when that
makes sense. "|=" is the 'union' of these sets, and in general is used
to combine different objects. See individual interface classes for
specifics.
"""
__all__ = [
"ParameterInterface",
"FitRecipeInterface",
"RecipeOrganizerInterface",
]
import six
from diffpy.srfit.equation.literals.abcs import ArgumentABC
class ParameterInterface(object):
"""Mix-in class for enhancing the Parameter interface."""
def __lshift__(self, v):
"""set_value with <<
Think of '<<' as injecting a value
Attributes
----------
v
value or Argument derivative
"""
if isinstance(v, ArgumentABC):
self.value = v.value
else:
self.value = v
return self
# End class ParameterInterface
# ----------------------------------------------------------------------------
class RecipeOrganizerInterface(object):
"""Mix-in class for enhancing the RecipeOrganizer interface."""
def __imul__(self, args):
"""Constrain with *=
Think of '*' as a push-pin.
This accepts arguments for a single constraint.
"""
_applyargs(args, self.constrain)
return self
def __imod__(self, args):
"""Restrain with %=
This of '%' as a loose rope.
This accepts arguments for a single restraint.
"""
_applyargs(args, self.restrain)
return self
def __iadd__(self, args):
"""_new_parameter or _add_parameter with +=
Think of "+" as addition of a Parameter.
This accepts arguments for a single function call.
"""
# Want to detect _add_parameter or _new_parameter
def f(*args):
if isinstance(args[0], six.string_types):
self._new_parameter(*args)
else:
self._add_parameter(*args)
return
_applyargs(args, f)
return self
# End class RecipeOrganizerInterface
# ----------------------------------------------------------------------------
class FitRecipeInterface(object):
"""Mix-in class for enhancing the FitRecipe interface."""
def __ior__(self, args):
"""AddContribution with |=
Think of "|" as the union of components.
This accepts a single argument.
"""
self.add_contribution(args)
return self
def __iadd__(self, args):
"""add_variable or create_new_variable with +=
Think of "+" as addition of a variable.
This accepts a single argument or an iterable of single
arguments or argument tuples.
"""
# Want to detect add_variable or create_new_variable
def f(*args):
if isinstance(args[0], six.string_types):
self.create_new_variable(*args)
else:
self.add_variable(*args)
return
_applymanyargs(args, f)
return self
# End class FitRecipeInterface
# Local helper functions -----------------------------------------------------
def _applymanyargs(args, f):
"""Apply arguments to a function.
Args can be any of the following.
arg
(arg1, arg2, ...)
((arg1a, arg1b, ...), ...)
"""
if not hasattr(args, "__iter__"):
f(args)
return
for arg in args:
if hasattr(arg, "__iter__"):
f(*arg)
else:
f(arg)
return
def _applyargs(args, f):
"""Apply arguments to a function.
Args can be any of the following.
arg
(arg1, arg2, ...)
((arg1a, arg1b, ...), ...)
"""
if not hasattr(args, "__iter__"):
f(args)
else:
f(*args)
return
# End of file