22
22
23
23
logger = logging .getLogger ("diffpy.srmise" )
24
24
25
- class NanoSpherical (BaselineFunction ):
25
+
26
+ class NanoSpherical (BaselineFunction ):
26
27
"""Methods for evaluation of baseline of spherical nanoparticle of uniform density.
27
28
28
29
Allowed formats are
@@ -47,29 +48,31 @@ def __init__(self, Cache=None):
47
48
evaluations.
48
49
"""
49
50
# Define parameterdict
50
- parameterdict = {' scale' : 0 , ' radius' : 1 }
51
- formats = [' internal' ]
52
- default_formats = {' default_input' : ' internal' , ' default_output' : ' internal' }
51
+ parameterdict = {" scale" : 0 , " radius" : 1 }
52
+ formats = [" internal" ]
53
+ default_formats = {" default_input" : " internal" , " default_output" : " internal" }
53
54
metadict = {}
54
- BaselineFunction .__init__ (self , parameterdict , formats , default_formats , metadict , None , Cache )
55
+ BaselineFunction .__init__ (
56
+ self , parameterdict , formats , default_formats , metadict , None , Cache
57
+ )
55
58
56
59
#### Methods required by BaselineFunction ####
57
60
58
- # def estimate_parameters(self, r, y):
59
- # """Estimate parameters for spherical baseline. (Not implemented!)
60
- #
61
- # Parameters
62
- # r - array along r from which to estimate
63
- # y - array along y from which to estimate
64
- #
65
- # Returns Numpy array of parameters in the default internal format.
66
- # Raises NotImplementedError if estimation is not implemented for this
67
- # degree, or SrMiseEstimationError if parameters cannot be estimated for
68
- # any other reason.
69
- # """
70
- # if len(r) != len(y):
71
- # emsg = "Arrays r, y must have equal length."
72
- # raise ValueError(emsg)
61
+ # def estimate_parameters(self, r, y):
62
+ # """Estimate parameters for spherical baseline. (Not implemented!)
63
+ #
64
+ # Parameters
65
+ # r - array along r from which to estimate
66
+ # y - array along y from which to estimate
67
+ #
68
+ # Returns Numpy array of parameters in the default internal format.
69
+ # Raises NotImplementedError if estimation is not implemented for this
70
+ # degree, or SrMiseEstimationError if parameters cannot be estimated for
71
+ # any other reason.
72
+ # """
73
+ # if len(r) != len(y):
74
+ # emsg = "Arrays r, y must have equal length."
75
+ # raise ValueError(emsg)
73
76
74
77
def _jacobianraw (self , pars , r , free ):
75
78
"""Return the Jacobian of the spherical baseline.
@@ -83,22 +86,26 @@ def _jacobianraw(self, pars, r, free):
83
86
needed. True for evaluation, False for no evaluation.
84
87
"""
85
88
if len (pars ) != self .npars :
86
- emsg = "Argument pars must have " + str (self .npars )+ " elements."
89
+ emsg = "Argument pars must have " + str (self .npars ) + " elements."
87
90
raise ValueError (emsg )
88
91
if len (free ) != self .npars :
89
- emsg = "Argument free must have " + str (self .npars )+ " elements."
92
+ emsg = "Argument free must have " + str (self .npars ) + " elements."
90
93
raise ValueError (emsg )
91
94
jacobian = [None for p in range (self .npars )]
92
95
if (free == False ).sum () == self .npars :
93
96
return jacobian
94
97
95
98
if np .isscalar (r ):
96
- if r <= 0. or r >= 2. * pars [1 ]:
97
- if free [0 ]: jacobian [0 ] = 0.
98
- if free [1 ]: jacobian [1 ] = 0.
99
+ if r <= 0.0 or r >= 2.0 * pars [1 ]:
100
+ if free [0 ]:
101
+ jacobian [0 ] = 0.0
102
+ if free [1 ]:
103
+ jacobian [1 ] = 0.0
99
104
else :
100
- if free [0 ]: jacobian [0 ] = self ._jacobianrawscale (pars , r )
101
- if free [1 ]: jacobian [1 ] = self ._jacobianrawradius (pars , r )
105
+ if free [0 ]:
106
+ jacobian [0 ] = self ._jacobianrawscale (pars , r )
107
+ if free [1 ]:
108
+ jacobian [1 ] = self ._jacobianrawradius (pars , r )
102
109
else :
103
110
s = self ._getdomain (pars , r )
104
111
if free [0 ]:
@@ -120,12 +127,12 @@ def _jacobianrawscale(self, pars, r):
120
127
"""
121
128
s = np .abs (pars [0 ])
122
129
R = np .abs (pars [1 ])
123
- rdivR = r / R
130
+ rdivR = r / R
124
131
# From abs'(s) in derivative, which is equivalent to sign(s) except at 0 where it
125
132
# is undefined. Since s=0 is equivalent to the absence of a nanoparticle, sign will
126
133
# be fine.
127
134
sign = np .sign (pars [1 ])
128
- return - sign * r * ( 1 - (3. / 4. ) * rdivR + (1. / 16. ) * rdivR ** 3 )
135
+ return - sign * r * ( 1 - (3.0 / 4.0 ) * rdivR + (1.0 / 16.0 ) * rdivR ** 3 )
129
136
130
137
def _jacobianrawradius (self , pars , r ):
131
138
"""Return partial Jacobian wrt radius without bounds checking.
@@ -141,7 +148,7 @@ def _jacobianrawradius(self, pars, r):
141
148
# From abs'(R) in derivative, which is equivalent to sign(R) except at 0 where it
142
149
# is undefined. Since R=0 is a singularity anyway, sign will be fine.
143
150
sign = np .sign (pars [1 ])
144
- return sign * s * ( 3 * r ** 2 * (r ** 2 - 4 * R ** 2 ))/ (16 * R ** 4 )
151
+ return sign * s * ( 3 * r ** 2 * (r ** 2 - 4 * R ** 2 )) / (16 * R ** 4 )
145
152
146
153
def _transform_parametersraw (self , pars , in_format , out_format ):
147
154
"""Convert parameter values from in_format to out_format.
@@ -162,15 +169,17 @@ def _transform_parametersraw(self, pars, in_format, out_format):
162
169
temp [0 ] = np .abs (temp [0 ])
163
170
temp [1 ] = np .abs (temp [1 ])
164
171
else :
165
- raise ValueError ("Argument 'in_format' must be one of %s." \
166
- % self .parformats )
172
+ raise ValueError (
173
+ "Argument 'in_format' must be one of %s." % self .parformats
174
+ )
167
175
168
176
# Convert to specified output format from "internal" format.
169
177
if out_format == "internal" :
170
178
pass
171
179
else :
172
- raise ValueError ("Argument 'out_format' must be one of %s." \
173
- % self .parformats )
180
+ raise ValueError (
181
+ "Argument 'out_format' must be one of %s." % self .parformats
182
+ )
174
183
return temp
175
184
176
185
def _valueraw (self , pars , r ):
@@ -185,11 +194,11 @@ def _valueraw(self, pars, r):
185
194
r - sequence or scalar over which pars is evaluated.
186
195
"""
187
196
if len (pars ) != self .npars :
188
- emsg = "Argument pars must have " + str (self .npars )+ " elements."
197
+ emsg = "Argument pars must have " + str (self .npars ) + " elements."
189
198
raise ValueError (emsg )
190
199
if np .isscalar (r ):
191
- if r <= 0. or r >= 2. * pars [1 ]:
192
- return 0.
200
+ if r <= 0.0 or r >= 2.0 * pars [1 ]:
201
+ return 0.0
193
202
else :
194
203
return self ._valueraw2 (pars , r )
195
204
else :
@@ -209,38 +218,44 @@ def _valueraw2(self, pars, r):
209
218
"""
210
219
s = np .abs (pars [0 ])
211
220
R = np .abs (pars [1 ])
212
- rdivR = r / R
213
- return - s * r * ( 1 - (3. / 4. ) * rdivR + (1. / 16. ) * rdivR ** 3 )
221
+ rdivR = r / R
222
+ return - s * r * ( 1 - (3.0 / 4.0 ) * rdivR + (1.0 / 16.0 ) * rdivR ** 3 )
214
223
215
224
def _getdomain (self , pars , r ):
216
225
"""Return slice object for which r > 0 and r < twice the radius"""
217
- low = r .searchsorted (0. , side = ' right' )
218
- high = r .searchsorted (2. * pars [1 ], side = ' left' )
226
+ low = r .searchsorted (0.0 , side = " right" )
227
+ high = r .searchsorted (2.0 * pars [1 ], side = " left" )
219
228
return slice (low , high )
220
229
221
230
def getmodule (self ):
222
231
return __name__
223
232
224
- #end of class NanoSpherical
233
+
234
+ # end of class NanoSpherical
225
235
226
236
# simple test code
227
- if __name__ == ' __main__' :
237
+ if __name__ == " __main__" :
228
238
229
239
f = NanoSpherical ()
230
240
r = np .arange (- 5 , 10 )
231
- pars = np .array ([- 1. , 7. ])
241
+ pars = np .array ([- 1.0 , 7.0 ])
232
242
free = np .array ([False , True ])
233
- print "Testing nanoparticle spherical baseline"
234
- print "Scale: %f, Radius: %f" % (pars [0 ], pars [1 ])
235
- print "-----------------------------------------"
243
+ print ( "Testing nanoparticle spherical baseline" )
244
+ print ( "Scale: %f, Radius: %f" % (pars [0 ], pars [1 ]) )
245
+ print ( "-----------------------------------------" )
236
246
val = f ._valueraw (pars , r )
237
- jac = f ._jacobianraw (pars , r , free )
238
- outjac = [j if j is not None else [None ]* len (r ) for j in jac ]
239
- print "r" .center (10 ), "value" .center (10 ), "jac(scale)" .center (10 ), "jac(radius)" .center (10 )
247
+ jac = f ._jacobianraw (pars , r , free )
248
+ outjac = [j if j is not None else [None ] * len (r ) for j in jac ]
249
+ print (
250
+ "r" .center (10 ),
251
+ "value" .center (10 ),
252
+ "jac(scale)" .center (10 ),
253
+ "jac(radius)" .center (10 ),
254
+ )
240
255
for tup in zip (r , val , * outjac ):
241
256
for t in tup :
242
257
if t is None :
243
- print ("%s" % None ).ljust (10 ),
258
+ print ("%s" % None ).ljust (10 ),
244
259
else :
245
- print ("% .3g" % t ).ljust (10 ),
260
+ print ("% .3g" % t ).ljust (10 ),
246
261
print
0 commit comments