17
17
import numpy as np
18
18
19
19
import diffpy .srmise .srmiselog
20
+ from diffpy .srmise .baselines import Polynomial
20
21
from diffpy .srmise .baselines .base import BaselineFunction
21
22
from diffpy .srmise .srmiseerrors import SrMiseEstimationError
22
23
23
24
logger = logging .getLogger ("diffpy.srmise" )
24
25
25
- class Arbitrary (BaselineFunction ):
26
+
27
+ class Arbitrary (BaselineFunction ):
26
28
"""Methods for evaluating a baseline from an arbitrary function.
27
29
28
30
Supports baseline calculations with arbitrary functions. These functions,
@@ -64,10 +66,10 @@ def __init__(self, npars, valuef, jacobianf=None, estimatef=None, Cache=None):
64
66
# Define parameterdict
65
67
# e.g. {"a_0":0, "a_1":1, "a_2":2, "a_3":3} if npars is 4.
66
68
parameterdict = {}
67
- for d in range (self .testnpars + 1 ):
68
- parameterdict ["a_" + str (d )] = d
69
- formats = [' internal' ]
70
- default_formats = {' default_input' : ' internal' , ' default_output' : ' internal' }
69
+ for d in range (self .testnpars + 1 ):
70
+ parameterdict ["a_" + str (d )] = d
71
+ formats = [" internal" ]
72
+ default_formats = {" default_input" : " internal" , " default_output" : " internal" }
71
73
72
74
# Check that the provided functions are at least callable
73
75
if valuef is None or callable (valuef ):
@@ -93,7 +95,9 @@ def __init__(self, npars, valuef, jacobianf=None, estimatef=None, Cache=None):
93
95
metadict ["valuef" ] = (valuef , repr )
94
96
metadict ["jacobianf" ] = (jacobianf , repr )
95
97
metadict ["estimatef" ] = (estimatef , repr )
96
- BaselineFunction .__init__ (self , parameterdict , formats , default_formats , metadict , None , Cache )
98
+ BaselineFunction .__init__ (
99
+ self , parameterdict , formats , default_formats , metadict , None , Cache
100
+ )
97
101
98
102
#### Methods required by BaselineFunction ####
99
103
@@ -114,9 +118,8 @@ def estimate_parameters(self, r, y):
114
118
# TODO: check that estimatef returns something proper?
115
119
try :
116
120
return self .estimatef (r , y )
117
- except Exception , e :
118
- emsg = "Error within estimation routine provided to Arbitrary:\n " + \
119
- str (e )
121
+ except Exception as e :
122
+ emsg = "Error within estimation routine provided to Arbitrary:\n " + str (e )
120
123
raise SrMiseEstimationError (emsg )
121
124
122
125
def _jacobianraw (self , pars , r , free ):
@@ -137,10 +140,10 @@ def _jacobianraw(self, pars, r, free):
137
140
emsg = "No jacobian routine provided to Arbitrary."
138
141
raise NotImplementedError (emsg )
139
142
if len (pars ) != self .npars :
140
- emsg = "Argument pars must have " + str (self .npars )+ " elements."
143
+ emsg = "Argument pars must have " + str (self .npars ) + " elements."
141
144
raise ValueError (emsg )
142
145
if len (free ) != self .npars :
143
- emsg = "Argument free must have " + str (self .npars )+ " elements."
146
+ emsg = "Argument free must have " + str (self .npars ) + " elements."
144
147
raise ValueError (emsg )
145
148
146
149
# Allow an arbitrary function without a Jacobian provided act as
@@ -149,7 +152,7 @@ def _jacobianraw(self, pars, r, free):
149
152
# large performance implications if all other functions used while
150
153
# fitting a function define a Jacobian.
151
154
if nfree == 0 :
152
- return [None for p in range (len (par ))]
155
+ return [None for p in range (len (pars ))]
153
156
154
157
# TODO: check that jacobianf returns something proper?
155
158
return self .jacobianf (pars , r , free )
@@ -170,15 +173,17 @@ def _transform_parametersraw(self, pars, in_format, out_format):
170
173
if in_format == "internal" :
171
174
pass
172
175
else :
173
- raise ValueError ("Argument 'in_format' must be one of %s." \
174
- % self .parformats )
176
+ raise ValueError (
177
+ "Argument 'in_format' must be one of %s." % self .parformats
178
+ )
175
179
176
180
# Convert to specified output format from "internal" format.
177
181
if out_format == "internal" :
178
182
pass
179
183
else :
180
- raise ValueError ("Argument 'out_format' must be one of %s." \
181
- % self .parformats )
184
+ raise ValueError (
185
+ "Argument 'out_format' must be one of %s." % self .parformats
186
+ )
182
187
return temp
183
188
184
189
def _valueraw (self , pars , r ):
@@ -192,7 +197,7 @@ def _valueraw(self, pars, r):
192
197
...
193
198
r: sequence or scalar over which pars is evaluated"""
194
199
if len (pars ) != self .npars :
195
- emsg = "Argument pars must have " + str (self .npars )+ " elements."
200
+ emsg = "Argument pars must have " + str (self .npars ) + " elements."
196
201
raise ValueError (emsg )
197
202
198
203
# TODO: check that valuef returns something proper?
@@ -201,21 +206,22 @@ def _valueraw(self, pars, r):
201
206
def getmodule (self ):
202
207
return __name__
203
208
204
- #end of class Polynomial
209
+
210
+ # end of class Polynomial
205
211
206
212
# simple test code
207
- if __name__ == ' __main__' :
213
+ if __name__ == " __main__" :
208
214
209
- f = Polynomial (degree = 3 )
215
+ f = Polynomial (degree = 3 )
210
216
r = np .arange (5 )
211
217
pars = np .array ([3 , 0 , 1 , 2 ])
212
218
free = np .array ([True , False , True , True ])
213
- print f ._valueraw (pars , r )
214
- print f ._jacobianraw (pars , r , free )
219
+ print ( f ._valueraw (pars , r ) )
220
+ print ( f ._jacobianraw (pars , r , free ) )
215
221
216
- f = Polynomial (degree = - 1 )
222
+ f = Polynomial (degree = - 1 )
217
223
r = np .arange (5 )
218
224
pars = np .array ([])
219
225
free = np .array ([])
220
- print f ._valueraw (pars , r )
221
- print f ._jacobianraw (pars , r , free )
226
+ print ( f ._valueraw (pars , r ) )
227
+ print ( f ._jacobianraw (pars , r , free ) )
0 commit comments