forked from diffpy/diffpy.srfit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator.py
More file actions
130 lines (107 loc) · 3.75 KB
/
calculator.py
File metadata and controls
130 lines (107 loc) · 3.75 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
#!/usr/bin/env python
##############################################################################
#
# diffpy.srfit by DANSE Diffraction group
# Simon J. L. Billinge
# (c) 2008 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.
#
##############################################################################
"""The Calculator for Parameter-aware functions.
Calculator is a functor class for producing a signal from embedded
Parameters. Calculators can store Parameters and ParameterSets,
Constraints and Restraints. Also, the __call__ function can be
overloaded to accept external arguments. Calculators are used to wrap
registered functions so that the function's Parameters are contained in
an object specific to the function. A custom Calculator can be added to
another RecipeOrganizer with the 'register_calculator' method.
"""
__all__ = ["Calculator"]
from diffpy.srfit.equation.literals.operators import Operator
from diffpy.srfit.fitbase.parameterset import ParameterSet
class Calculator(Operator, ParameterSet):
"""Base class for calculators.
A Calculator organizes Parameters and has a __call__ method that can
calculate a generic signal.
Attributes
----------
name
A name for this organizer.
meta
A dictionary of metadata needed by the calculator.
_calculators
A managed dictionary of Calculators, indexed by name.
_constraints
A set of constrained Parameters. Constraints can be
added using the 'constrain' methods.
_parameters
A managed OrderedDict of contained Parameters.
_parsets
A managed dictionary of ParameterSets.
_restraints
A set of Restraints. Restraints can be added using the
'restrain' or 'confine' methods.
_eqfactory
A diffpy.srfit.equation.builder.EquationFactory
instance that is used create Equations from string.
Operator Attributes
-------------------
args
List of Literal arguments
nin
Number of inputs (<1 means this is variable)
nout
Number of outputs (1)
operation
Function that performs the operation, self.__call__
symbol
Same as name
_value
The value of the Operator.
value
Property for 'getValue'.
Properties
----------
names
Variable names (read only). See get_names.
values
Variable values (read only). See get_values.
"""
# define abstract attributes from the Operator base.
nin = -1
nout = 1
def __init__(self, name):
ParameterSet.__init__(self, name)
self.meta = {}
# Initialize Operator attributes
Operator.__init__(self, name)
return
@property
def symbol(self):
return self.name
# Overload me!
def __call__(self, *args):
"""Calculate something.
This method must be overloaded. When overloading, you should
specify the arguments explicitly, otherwise the parameters must
be specified when adding the Calculator to a RecipeOrganizer.
"""
return 0
def operation(self, *args):
self._value = self.__call__(*args)
return self._value
def _validate(self):
"""Validate my state.
This performs ParameterSet validations. This does not validate
the operation, since this could be costly. The operation should
be validated with a containing equation.
Raises AttributeError if validation fails.
"""
ParameterSet._validate(self)
return
# End class Calculator