Skip to content

Commit 7c831f9

Browse files
committed
Raise SrFitError on failed validation.
Correct few other instances of inappropriately raised AttributeError-s.
1 parent 9038999 commit 7c831f9

File tree

12 files changed

+68
-52
lines changed

12 files changed

+68
-52
lines changed

diffpy/srfit/equation/equationmod.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ def swap(self, oldlit, newlit):
187187

188188
def addLiteral(self, literal):
189189
"""Cannot add a literal to an Equation."""
190-
raise AttributeError("Cannot add literals to an Equation.")
190+
raise RuntimeError("Cannot add literals to an Equation.")
191191

192192
# Visitors can treat us differently than an Operator.
193193

diffpy/srfit/fitbase/constraint.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,21 @@
1212
# See LICENSE_DANSE.txt for license information.
1313
#
1414
########################################################################
15+
1516
"""Constraint class.
1617
1718
Constraints are used by a FitRecipe (and other RecipeOrganizers) to organize
1819
constraint equations. They store a Parameter object and an Equation object that
1920
is used to compute its value. The Constraint.constrain method is used to create
2021
this association.
21-
2222
"""
23+
2324
__all__ = ["Constraint"]
2425

26+
from diffpy.srfit.exceptions import SrFitError
2527
from diffpy.srfit.fitbase.validatable import Validatable
2628

29+
2730
class Constraint(Validatable):
2831
"""Constraint class.
2932
@@ -88,29 +91,29 @@ def _validate(self):
8891
This validates that par is not None.
8992
This validates eq.
9093
91-
Raises AttributeError if validation fails.
94+
Raises SrFitError if validation fails.
9295
9396
"""
9497
if self.par is None:
95-
raise AttributeError("par is None")
98+
raise SrFitError("par is None")
9699
if self.eq is None:
97-
raise AttributeError("eq is None")
100+
raise SrFitError("eq is None")
98101
self.par._validate()
99102
from diffpy.srfit.equation.visitors import validate
100103
try:
101104
validate(self.eq)
102105
except ValueError, e:
103-
raise AttributeError(e)
106+
raise SrFitError(e)
104107

105108
# Try to get the value of eq.
106109
try:
107110
val = self.eq()
108111
self.par.setValue(val)
109112
except TypeError, e:
110-
raise AttributeError("eq cannot be evaluated")
113+
raise SrFitError("eq cannot be evaluated")
111114
finally:
112115
if val is None:
113-
raise AttributeError("eq evaluates to None")
116+
raise SrFitError("eq evaluates to None")
114117

115118
return
116119

diffpy/srfit/fitbase/fitcontribution.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See LICENSE_DANSE.txt for license information.
1313
#
1414
########################################################################
15+
1516
"""FitContribution class.
1617
1718
FitContributions generate a residual function for a FitRecipe. A
@@ -20,14 +21,15 @@
2021
holds the observed and calculated signals.
2122
2223
See the examples in the documention for how to use a FitContribution.
23-
2424
"""
25+
2526
__all__ = ["FitContribution"]
2627

2728
from diffpy.srfit.interface import _fitcontribution_interface
2829
from diffpy.srfit.fitbase.parameterset import ParameterSet
2930
from diffpy.srfit.fitbase.recipeorganizer import equationFromString
3031
from diffpy.srfit.fitbase.parameter import ParameterProxy
32+
from diffpy.srfit.exceptions import SrFitError
3133

3234
class FitContribution(_fitcontribution_interface, ParameterSet):
3335
"""FitContribution class.
@@ -224,15 +226,15 @@ def setResidualEquation(self, eqstr = None):
224226
Keep that in mind when defining a new residual or using the built-in
225227
ones.
226228
227-
Raises AttributeError if the Profile is not yet defined.
229+
Raises SrFitError if the Profile is not yet defined.
228230
Raises ValueError if eqstr depends on a Parameter that is not part of
229231
the FitContribution.
230232
231233
"""
232234
if self.profile is None:
233-
raise AttributeError("Assign the Profile first")
235+
raise SrFitError("Assign the Profile first")
234236
if self._eq is None:
235-
raise AttributeError("Assign the Equation first")
237+
raise SrFitError("Assign the Equation first")
236238

237239
chivstr = "(eq - %s)/%s" % (self._yname, self._dyname)
238240
resvstr = "(eq - %s)/sum(%s**2)**0.5" % (self._yname, self._yname)
@@ -280,7 +282,7 @@ def _validate(self):
280282
This validates _eq.
281283
This validates _reseq and residual.
282284
283-
Raises AttributeError if validation fails.
285+
Raises SrFitError if validation fails.
284286
285287
"""
286288
self.profile._validate()
@@ -291,30 +293,30 @@ def _validate(self):
291293
try:
292294
validate(self._eq)
293295
except ValueError, e:
294-
raise AttributeError(e)
296+
raise SrFitError(e)
295297
if self._eq is None:
296-
raise AttributeError("_eq is None")
298+
raise SrFitError("_eq is None")
297299

298300
val = None
299301
try:
300302
val = self._eq()
301303
except TypeError, e:
302-
raise AttributeError("_eq cannot be evaluated: %s"%e)
304+
raise SrFitError("_eq cannot be evaluated: %s"%e)
303305

304306
if val is None:
305-
raise AttributeError("_eq evaluates to None")
307+
raise SrFitError("_eq evaluates to None")
306308

307309
# Try to get the value for residual
308310
try:
309311
validate(self._reseq)
310312
except ValueError, e:
311-
raise AttributeError(e)
313+
raise SrFitError(e)
312314
try:
313315
val = self.residual()
314316
except TypeError, e:
315-
raise AttributeError("residual cannot be evaluated")
317+
raise SrFitError("residual cannot be evaluated")
316318
if val is None:
317-
raise AttributeError("residual evaluates to None")
319+
raise SrFitError("residual evaluates to None")
318320
return
319321

320322
# End of file

diffpy/srfit/fitbase/parameter.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
from numpy import inf
2828

29+
from diffpy.srfit.exceptions import SrFitError
2930
from diffpy.srfit.equation.literals import Argument
3031
from diffpy.srfit.equation.literals.abcs import ArgumentABC
3132
from diffpy.srfit.util.nameutils import validateName
@@ -127,11 +128,11 @@ def _validate(self):
127128
128129
This validates that value is not None.
129130
130-
Raises AttributeError if validation fails.
131+
Raises SrFitError if validation fails.
131132
132133
"""
133134
if self.value is None:
134-
raise AttributeError("value of '%s' is None"%self.name)
135+
raise SrFitError("value of '%s' is None"%self.name)
135136
return
136137

137138
# End class Parameter
@@ -180,13 +181,13 @@ def _validate(self):
180181
181182
This validates that value and par are not None.
182183
183-
Raises AttributeError if validation fails.
184+
Raises SrFitError if validation fails.
184185
185186
"""
186187
if self.value is None:
187-
raise AttributeError("value is None")
188+
raise SrFitError("value is None")
188189
if self.par is None:
189-
raise AttributeError("par is None")
190+
raise SrFitError("par is None")
190191
return
191192

192193
# End class ParameterProxy

diffpy/srfit/fitbase/profile.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,23 @@
1212
# See LICENSE_DANSE.txt for license information.
1313
#
1414
########################################################################
15+
1516
"""The Profile class containing the physical and calculated data.
1617
1718
Profile holds the arrays representing an observed profile, a selected subset of
1819
the observed profile and a calculated profile. Profiles are used by Calculators
1920
to store a calculated signal, and by FitContributions to help calculate a
2021
residual equation.
21-
2222
"""
23+
2324
__all__ = ["Parameter", "Profile"]
2425

2526
import numpy
2627

2728
from diffpy.srfit.util.observable import Observable
2829
from diffpy.srfit.fitbase.parameter import Parameter
2930
from diffpy.srfit.fitbase.validatable import Validatable
31+
from diffpy.srfit.exceptions import SrFitError
3032

3133
# This is the roundoff tolerance for selecting bounds on arrays.
3234
epsilon = 1e-8
@@ -315,15 +317,15 @@ def _validate(self):
315317
This validates that x, y, dy, xobx, yobs and dyobs are not None.
316318
This validates that x, y, and dy are the same length.
317319
318-
Raises AttributeError if validation fails.
320+
Raises SrFitError if validation fails.
319321
320322
"""
321323
datanotset = any(v is None for v in
322324
[self.x, self.y, self.dy, self.xobs, self.yobs, self.dyobs])
323325
if datanotset:
324-
raise AttributeError("Missing data")
326+
raise SrFitError("Missing data")
325327
if len(self.x) != len(self.y) or len(self.x) != len(self.dy):
326-
raise AttributeError("Data are different lengths")
328+
raise SrFitError("Data are different lengths")
327329
return
328330

329331

diffpy/srfit/fitbase/profilegenerator.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See LICENSE_DANSE.txt for license information.
1313
#
1414
########################################################################
15+
1516
"""The ProfileGenerator class for generating a profile.
1617
1718
ProfileGenerators encapsulate the evaluation and required Parameters and
@@ -38,15 +39,16 @@
3839
> return a * exp(-0.5*((x-x0)/w)**2)
3940
4041
More examples can be found in the example directory of the documentation.
41-
4242
"""
43+
4344
__all__ = ["ProfileGenerator"]
4445

4546
from numpy import asarray
4647

4748
from diffpy.srfit.equation.literals.operators import Operator
48-
4949
from diffpy.srfit.fitbase.parameterset import ParameterSet
50+
from diffpy.srfit.exceptions import SrFitError
51+
5052

5153
class ProfileGenerator(Operator, ParameterSet):
5254
"""Base class for profile generators.
@@ -161,16 +163,14 @@ def _validate(self):
161163
This does not validate the operation, since this could be costly. The
162164
operation should be validated with a containing equation.
163165
164-
Raises AttributeError if validation fails.
166+
Raises SrFitError if validation fails.
165167
166168
"""
167169
if self.profile is None:
168-
raise AttributeError("profile is None")
170+
raise SrFitError("profile is None")
169171
self.profile._validate()
170172
ParameterSet._validate(self)
171-
172173
return
173174

174175

175-
176176
# End class ProfileGenerator

diffpy/srfit/fitbase/restraint.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,22 @@
1212
# See LICENSE_DANSE.txt for license information.
1313
#
1414
########################################################################
15+
1516
"""Restraints class.
1617
1718
Restraints are used by RecipeOrganizers to organize restraint equations.
1819
Restraints store an Equation, bounds on its value, and the form of the penalty
1920
function for breaking a restraint. This penalty is added to the residual
2021
equation calculated by a FitRecipe.
21-
2222
"""
23+
2324
__all__ = ["Restraint"]
2425

2526
from numpy import inf
2627

2728
from diffpy.srfit.fitbase.validatable import Validatable
29+
from diffpy.srfit.exceptions import SrFitError
30+
2831

2932
class Restraint(Validatable):
3033
"""Restraint class.
@@ -90,25 +93,25 @@ def _validate(self):
9093
9194
This validates eq.
9295
93-
Raises AttributeError if validation fails.
96+
Raises SrFitError if validation fails.
9497
9598
"""
9699
if self.eq is None:
97-
raise AttributeError("eq is None")
100+
raise SrFitError("eq is None")
98101
from diffpy.srfit.equation.visitors import validate
99102
try:
100103
validate(self.eq)
101104
except ValueError, e:
102-
raise AttributeError(e)
105+
raise SrFitError(e)
103106

104107
# Try to get the value of eq.
105108
try:
106109
val = self.eq()
107110
except TypeError, e:
108-
raise AttributeError("eq cannot be evaluated")
111+
raise SrFitError("eq cannot be evaluated")
109112
finally:
110113
if val is None:
111-
raise AttributeError("eq evaluates to None")
114+
raise SrFitError("eq evaluates to None")
112115

113116
return
114117

diffpy/srfit/pdf/basepdfgenerator.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,21 @@
1212
# See LICENSE_DANSE.txt for license information.
1313
#
1414
########################################################################
15+
1516
"""PDF profile generator base class.
1617
1718
The BasePDFGenerator class interfaces with SrReal PDF calculators and is used
1819
as a base for the PDFGenerator and DebyePDFGenerator classes.
19-
2020
"""
21+
2122
__all__ = ["BasePDFGenerator"]
2223

2324
import numpy
2425

2526
from diffpy.srfit.fitbase import ProfileGenerator
2627
from diffpy.srfit.fitbase.parameter import ParameterAdapter
2728
from diffpy.srfit.structure import struToParameterSet
29+
from diffpy.srfit.exceptions import SrFitError
2830

2931

3032
# FIXME - Parameter creation will have to be smarter once deeper calculator
@@ -266,13 +268,13 @@ def _validate(self):
266268
This validates that the phase is not None.
267269
This performs ProfileGenerator validations.
268270
269-
Raises AttributeError if validation fails.
271+
Raises SrFitError if validation fails.
270272
271273
"""
272274
if self._calc is None:
273-
raise AttributeError("_calc is None")
275+
raise SrFitError("_calc is None")
274276
if self._phase is None:
275-
raise AttributeError("_phase is None")
277+
raise SrFitError("_phase is None")
276278
ProfileGenerator._validate(self)
277279
return
278280

diffpy/srfit/pdf/pdfparser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ def parseString(self, patstring):
178178
inf_or_nan = re.compile('(?i)^[+-]?(NaN|Inf)\\b')
179179
has_drobs = True
180180
has_dGobs = True
181-
# raise PDFDataFormatError if something goes wrong
181+
# raise ParseError if something goes wrong
182182
robs = []
183183
Gobs = []
184184
drobs = []

0 commit comments

Comments
 (0)