diff --git a/docs/source/extending.rst b/docs/source/extending.rst index 8dc4313f..022ef458 100644 --- a/docs/source/extending.rst +++ b/docs/source/extending.rst @@ -97,7 +97,7 @@ used as:: Note that the *unbound* methods are used. The names ``getter`` and ``setter`` describe how the accessor attributes are used to access the value of the -parameter. When ``xpar.getValue()`` is called, it redirects to +parameter. When ``xpar.get_value()`` is called, it redirects to ``SimpleAtom.getX(atom)``. If instead ``SimpleAtom`` had methods called ``get`` and ``set`` that take as @@ -107,7 +107,7 @@ can be adapted as:: xpar = ParameterAdapter("x", atom, getter = SimpleAtom.get, setter = SimpleAtom.set, attr = "x") -Thus, when ``xpar.getValue()`` is called, it in turn calls +Thus, when ``xpar.get_value()`` is called, it in turn calls ``SimpleAtom.get(atom, "x")``. ``xpar.set_value(value)`` calls ``SimpleAtom.set(atom, "x", value)``. diff --git a/news/getvalue-dep.rst b/news/getvalue-dep.rst new file mode 100644 index 00000000..7fef04eb --- /dev/null +++ b/news/getvalue-dep.rst @@ -0,0 +1,25 @@ +**Added:** + +* + +**Changed:** + +* + +**Deprecated:** + +* ``getValue`` is deprecated in favour of ``get_value`` on ``Parameter`` and on + the ``Literal`` hierarchy (``Argument``, ``Operator``, ``Equation``). The old + name still works and warns, and will be removed in version 4.0.0. + +**Removed:** + +* + +**Fixed:** + +* + +**Security:** + +* diff --git a/src/diffpy/srfit/equation/equationmod.py b/src/diffpy/srfit/equation/equationmod.py index 3c05dcbc..44bcaf01 100644 --- a/src/diffpy/srfit/equation/equationmod.py +++ b/src/diffpy/srfit/equation/equationmod.py @@ -81,7 +81,7 @@ class Equation(Operator): _value The value of the Operator. value - Property for 'getValue'. + Property for 'get_value'. """ # define abstract attributes from the Operator base. @@ -201,7 +201,7 @@ def __call__(self, *args, **kw): raise ValueError("No argument named '%s' here" % name) arg.set_value(val) - self._value = self.root.getValue() + self._value = self.root.get_value() return self._value def swap(self, oldlit, newlit): diff --git a/src/diffpy/srfit/equation/literals/abcs.py b/src/diffpy/srfit/equation/literals/abcs.py index 82c633cc..d930c1a5 100644 --- a/src/diffpy/srfit/equation/literals/abcs.py +++ b/src/diffpy/srfit/equation/literals/abcs.py @@ -31,7 +31,7 @@ def identify(self, visitor): pass @abstractmethod - def getValue(self): + def get_value(self): """Return the value of the literal.""" pass diff --git a/src/diffpy/srfit/equation/literals/argument.py b/src/diffpy/srfit/equation/literals/argument.py index c468d516..a8d387b6 100644 --- a/src/diffpy/srfit/equation/literals/argument.py +++ b/src/diffpy/srfit/equation/literals/argument.py @@ -39,7 +39,7 @@ class Argument(Literal, ArgumentABC): _value The value of the Argument. Modified with 'set_value'. value - Property for 'getValue' and 'set_value'. + Property for 'get_value' and 'set_value'. """ const = None @@ -55,7 +55,7 @@ def identify(self, visitor): """Identify self to a visitor.""" return visitor.onArgument(self) - def getValue(self): + def get_value(self): """Get the value of this Literal.""" return self._value @@ -77,7 +77,7 @@ def set_value(self, val): return value = property( - lambda self: self.getValue(), lambda self, val: self.set_value(val) + lambda self: self.get_value(), lambda self, val: self.set_value(val) ) diff --git a/src/diffpy/srfit/equation/literals/literal.py b/src/diffpy/srfit/equation/literals/literal.py index 48c48eaa..1f3b7b6d 100644 --- a/src/diffpy/srfit/equation/literals/literal.py +++ b/src/diffpy/srfit/equation/literals/literal.py @@ -23,6 +23,13 @@ from diffpy.srfit.equation.literals.abcs import LiteralABC from diffpy.srfit.util.observable import Observable +from diffpy.utils._deprecator import build_deprecation_message, deprecated + +literal_base = "diffpy.srfit.equation.literals.Literal" +removal_version = "4.0.0" +getValue_dep_msg = build_deprecation_message( + literal_base, "getValue", "get_value", removal_version +) class Literal(Observable, LiteralABC): @@ -49,10 +56,20 @@ def __init__(self, name=None): self.name = name return - def getValue(self): + def get_value(self): """Get the value of the Literal.""" raise NotImplementedError("Define in derived class") + @deprecated(getValue_dep_msg) + def getValue(self): + """This function has been deprecated and will be removed in + version 4.0.0. + + Please use diffpy.srfit.equation.literals.Literal.get_value + instead. + """ + return self.get_value() + def identify(self, visitor): """Identify self to a visitor.""" m = "'%s' must override 'identify'" % self.__class__.__name__ diff --git a/src/diffpy/srfit/equation/literals/operators.py b/src/diffpy/srfit/equation/literals/operators.py index 6715f309..917a17ae 100644 --- a/src/diffpy/srfit/equation/literals/operators.py +++ b/src/diffpy/srfit/equation/literals/operators.py @@ -119,14 +119,14 @@ def addLiteral(self, literal): self._flush(other=(self,)) return - def getValue(self): + def get_value(self): """Get or evaluate the value of the operator.""" if self._value is None: vals = [arg.value for arg in self.args] self._value = self.operation(*vals) return self._value - value = property(lambda self: self.getValue()) + value = property(lambda self: self.get_value()) def _loop_check(self, literal): """Check if a literal causes self-reference.""" diff --git a/src/diffpy/srfit/fitbase/calculator.py b/src/diffpy/srfit/fitbase/calculator.py index c0afc776..cc8d32a5 100644 --- a/src/diffpy/srfit/fitbase/calculator.py +++ b/src/diffpy/srfit/fitbase/calculator.py @@ -72,7 +72,7 @@ class Calculator(Operator, ParameterSet): _value The value of the Operator. value - Property for 'getValue'. + Property for 'get_value'. Properties ---------- diff --git a/src/diffpy/srfit/fitbase/fitrecipe.py b/src/diffpy/srfit/fitbase/fitrecipe.py index efa1aa64..cc6f6924 100644 --- a/src/diffpy/srfit/fitbase/fitrecipe.py +++ b/src/diffpy/srfit/fitbase/fitrecipe.py @@ -669,7 +669,7 @@ def __verify_parameters(self): badpars = [] for par in self.iterate_over_parameters(): try: - par.getValue() + par.get_value() except ValueError: badpars.append(par) @@ -1246,9 +1246,9 @@ def add_constraint(self, par, con, ns={}): # This will pass the value of a constrained parameter to the initial # value of a parameter constraint. if con in self._parameters.values(): - val = con.getValue() + val = con.get_value() if val is None: - val = par.getValue() + val = par.get_value() con.set_value(val) if par in self._parameters.values(): @@ -1483,7 +1483,7 @@ def initialize_recipe_with_results(self, results, verbose=True): print("Parameters set in FitRecipe:") print("=" * 30) set_parameters_dict = { - param.name: param.getValue() + param.name: param.get_value() for param in self._parameters.values() } self._pretty_print_results_dict(set_parameters_dict) diff --git a/src/diffpy/srfit/fitbase/fitresults.py b/src/diffpy/srfit/fitbase/fitresults.py index 4f7fa5d8..6e4b46fd 100644 --- a/src/diffpy/srfit/fitbase/fitresults.py +++ b/src/diffpy/srfit/fitbase/fitresults.py @@ -228,7 +228,7 @@ def update(self): # Store the constraint information self.connames = [con.par.name for con in recipe._oconstraints] - self.convals = [con.par.getValue() for con in recipe._oconstraints] + self.convals = [con.par.get_value() for con in recipe._oconstraints] if self.varnames: # Calculate the covariance @@ -314,7 +314,7 @@ def _calculate_jacobian(self): cond = [] for con in recipe._oconstraints: con.update() - cond.append(con.par.getValue()) + cond.append(con.par.get_value()) pvals[k] = v - h rk -= self.recipe.residual(pvals) @@ -322,9 +322,9 @@ def _calculate_jacobian(self): # FIXME - constraints are used for vectors as well! for i, con in enumerate(recipe._oconstraints): con.update() - val = con.par.getValue() + val = con.par.get_value() if numpy.isscalar(val): - cond[i] -= con.par.getValue() + cond[i] -= con.par.get_value() cond[i] /= 2 * h else: cond[i] = 0.0 diff --git a/src/diffpy/srfit/fitbase/parameter.py b/src/diffpy/srfit/fitbase/parameter.py index 74cab96c..2da0d2f4 100644 --- a/src/diffpy/srfit/fitbase/parameter.py +++ b/src/diffpy/srfit/fitbase/parameter.py @@ -42,6 +42,10 @@ parameter_base, "setValue", "set_value", removal_version ) +getValue_dep_msg = build_deprecation_message( + parameter_base, "getValue", "get_value", removal_version +) + setConst_dep_msg = build_deprecation_message( parameter_base, "setConst", "set_constant", removal_version ) @@ -67,7 +71,7 @@ class Parameter(_parameter_interface, Argument, Validatable): _value The value of the Parameter. Modified with ``set_value``. value - Property for ``getValue`` and ``set_value``. + Property for ``get_value`` and ``set_value``. constrained A flag indicating if the Parameter is constrained (default False). @@ -118,6 +122,15 @@ def set_value(self, val): Argument.set_value(self, val) return self + @deprecated(getValue_dep_msg) + def getValue(self): + """This function has been deprecated and will be removed in + version 4.0.0. + + Please use diffpy.srfit.fitbase.Parameter.get_value instead. + """ + return self.get_value() + @deprecated(setValue_dep_msg) def setValue(self, val): """This function has been deprecated and will be removed in @@ -209,7 +222,7 @@ def bound_window(self, lower_radius=0, upper_radius=None): Parameter Return self so that mutators can be chained. """ - val = self.getValue() + val = self.get_value() lower_bound = val - lower_radius if upper_radius is None: upper_radius = lower_radius @@ -317,9 +330,9 @@ def _observers(self): def set_value(self, val): return self.par.set_value(val) - @wraps(Parameter.getValue) - def getValue(self): - return self.par.getValue() + @wraps(Parameter.get_value) + def get_value(self): + return self.par.get_value() @wraps(Parameter.set_constant) def set_constant(self, const=True, value=None): @@ -355,7 +368,7 @@ def _validate(self): class ParameterAdapter(Parameter): """An adapter for parameter-like objects. - This class wraps an object as a Parameter. The getValue and + This class wraps an object as a Parameter. The get_value and set_value methods defer to the data of the wrapped object. """ @@ -415,11 +428,11 @@ def __init__(self, name, obj, getter=None, setter=None, attr=None): else: self.setter = bind2nd(setter, self.attr) - value = self.getValue() + value = self.get_value() Parameter.__init__(self, name, value) return - def getValue(self): + def get_value(self): """Get the value of the Parameter. Returns @@ -442,7 +455,7 @@ def set_value(self, value): ParameterAdapter Return self so that mutators can be chained. """ - if value != self.getValue(): + if value != self.get_value(): self.setter(self.obj, value) self.notify() return self diff --git a/src/diffpy/srfit/fitbase/profile.py b/src/diffpy/srfit/fitbase/profile.py index 45a295b7..d92d76f5 100644 --- a/src/diffpy/srfit/fitbase/profile.py +++ b/src/diffpy/srfit/fitbase/profile.py @@ -133,19 +133,19 @@ def __init__(self): # We want x, y, ycalc and dy to stay in-sync with xpar, ypar and dypar x = property( - lambda self: self.xpar.getValue(), + lambda self: self.xpar.get_value(), lambda self, val: self.xpar.set_value(val), ) y = property( - lambda self: self.ypar.getValue(), + lambda self: self.ypar.get_value(), lambda self, val: self.ypar.set_value(val), ) dy = property( - lambda self: self.dypar.getValue(), + lambda self: self.dypar.get_value(), lambda self, val: self.dypar.set_value(val), ) ycalc = property( - lambda self: self.ycpar.getValue(), + lambda self: self.ycpar.get_value(), lambda self, val: self.ycpar.set_value(val), ) diff --git a/src/diffpy/srfit/fitbase/profilegenerator.py b/src/diffpy/srfit/fitbase/profilegenerator.py index 2b642289..88bed95d 100644 --- a/src/diffpy/srfit/fitbase/profilegenerator.py +++ b/src/diffpy/srfit/fitbase/profilegenerator.py @@ -33,9 +33,9 @@ def __init__(self): self.newParameter("center", 0) self.newParameter("width", 0) def __call__(self, x): - a = self.amp.getValue() - x0 = self.center.getValue() - w = self.width.getValue() + a = self.amp.get_value() + x0 = self.center.get_value() + w = self.width.get_value() return a * exp(-0.5*((x-x0)/w)**2) More examples can be found in the example directory of the @@ -105,7 +105,7 @@ class ProfileGenerator(Operator, ParameterSet): _value The value of the Operator. value - Property for 'getValue'. + Property for 'get_value'. Properties ---------- diff --git a/src/diffpy/srfit/sas/sasparameter.py b/src/diffpy/srfit/sas/sasparameter.py index b74a58f1..631c0159 100644 --- a/src/diffpy/srfit/sas/sasparameter.py +++ b/src/diffpy/srfit/sas/sasparameter.py @@ -35,7 +35,7 @@ class SASParameter(Parameter): _value The value of the Parameter. Modified with 'set_value'. value - Property for 'getValue' and 'set_value'. + Property for 'get_value' and 'set_value'. constrained A flag indicating if the Parameter is constrained (default False). @@ -68,14 +68,14 @@ def __init__(self, name, model, parname=None): Parameter.__init__(self, name, val) return - def getValue(self): + def get_value(self): """Get the value of the Parameter.""" value = self._model.getParam(self._parname) return value def set_value(self, value): """Set the value of the Parameter.""" - if value != self.getValue(): + if value != self.get_value(): self._model.setParam(self._parname, value) self.notify() return self diff --git a/src/diffpy/srfit/structure/objcrystparset.py b/src/diffpy/srfit/structure/objcrystparset.py index 755f818e..55761162 100644 --- a/src/diffpy/srfit/structure/objcrystparset.py +++ b/src/diffpy/srfit/structure/objcrystparset.py @@ -1188,7 +1188,7 @@ def __init__(self, name, value=None, const=False): def set_value(self, val): """Change the value of the Parameter.""" - curval = self.getValue() + curval = self.get_value() val = float(val) if val == curval: @@ -1303,7 +1303,7 @@ class ObjCrystBondLengthParameter(StretchModeParameter): _value The value of the Parameter. Modified with 'set_value'. value - Property for 'getValue' and 'set_value'. + Property for 'get_value' and 'set_value'. constraint A callable that calculates the value of this Parameter. If this is None (None), the the Parameter is responsible for its @@ -1404,7 +1404,7 @@ def setConst(self, const=True, value=None): self.set_constant(const, value) return self - def getValue(self): + def get_value(self): """This calculates the value if it might have been changed. There is no guarantee that the ObjCrystMolAtomParSets underlying @@ -1458,7 +1458,7 @@ class ObjCrystBondAngleParameter(StretchModeParameter): _value The value of the Parameter. Modified with 'set_value'. value - Property for 'getValue' and 'set_value'. + Property for 'get_value' and 'set_value'. constraint A callable that calculates the value of this Parameter. If this is None (None), the the Parameter is responsible for its @@ -1565,7 +1565,7 @@ def setConst(self, const=True, value=None): self.set_constant(const, value) return self - def getValue(self): + def get_value(self): """This calculates the value if it might have been changed. There is no guarantee that the MolAtoms underlying the bond @@ -1626,7 +1626,7 @@ class ObjCrystDihedralAngleParameter(StretchModeParameter): _value The value of the Parameter. Modified with 'set_value'. value - Property for 'getValue' and 'set_value'. + Property for 'get_value' and 'set_value'. constraint A callable that calculates the value of this Parameter. If this is None (None), the the Parameter is responsible for its @@ -1745,7 +1745,7 @@ def setConst(self, const=True, value=None): self.set_constant(const, value) return self - def getValue(self): + def get_value(self): """This calculates the value if it might have been changed. There is no guarantee that the ObjCrystMolAtomParSets underlying diff --git a/src/diffpy/srfit/structure/sgconstraints.py b/src/diffpy/srfit/structure/sgconstraints.py index 2fdd48cb..22d57c8b 100644 --- a/src/diffpy/srfit/structure/sgconstraints.py +++ b/src/diffpy/srfit/structure/sgconstraints.py @@ -585,7 +585,7 @@ def _constrain_adps(self, positions): Uij = numpy.zeros((3, 3), dtype=float) for idx, par in enumerate(pars): i, j = _idxtoij[idx] - Uij[i, j] = Uij[j, i] = par.getValue() + Uij[i, j] = Uij[j, i] = par.get_value() Uijs.append(Uij) @@ -687,8 +687,8 @@ def _constrain_monoclinic(lattice): afactor = deg2rad ang90 = 90.0 * afactor lattice.alpha.set_constant(True, ang90) - beta = lattice.beta.getValue() - gamma = lattice.gamma.getValue() + beta = lattice.beta.get_value() + gamma = lattice.gamma.get_value() if ang90 != beta and ang90 == gamma: lattice.gamma.set_constant(True, ang90) @@ -741,7 +741,7 @@ def _constrain_trigonal(lattice): afactor = deg2rad ang90 = 90.0 * afactor ang120 = 120.0 * afactor - if lattice.gamma.getValue() == ang120: + if lattice.gamma.get_value() == ang120: lattice.add_constraint(lattice.b, lattice.a) lattice.alpha.set_constant(True, ang90) lattice.beta.set_constant(True, ang90) diff --git a/tests/test_constraint.py b/tests/test_constraint.py index 1a87cdc3..ac95bc85 100644 --- a/tests/test_constraint.py +++ b/tests/test_constraint.py @@ -51,12 +51,12 @@ def test_constrain_parameter(self): p2.set_value(2.5) c.update() - self.assertEqual(5.0, p1.getValue()) + self.assertEqual(5.0, p1.get_value()) p2.set_value(8.1) - self.assertEqual(5.0, p1.getValue()) + self.assertEqual(5.0, p1.get_value()) c.update() - self.assertEqual(16.2, p1.getValue()) + self.assertEqual(16.2, p1.get_value()) return @@ -96,12 +96,12 @@ def test_constrain_deprecated(self): p2.set_value(2.5) c.update() - self.assertEqual(5.0, p1.getValue()) + self.assertEqual(5.0, p1.get_value()) p2.set_value(8.1) - self.assertEqual(5.0, p1.getValue()) + self.assertEqual(5.0, p1.get_value()) c.update() - self.assertEqual(16.2, p1.getValue()) + self.assertEqual(16.2, p1.get_value()) return diff --git a/tests/test_diffpyparset.py b/tests/test_diffpyparset.py index a6e519b3..cecda1cf 100644 --- a/tests/test_diffpyparset.py +++ b/tests/test_diffpyparset.py @@ -43,37 +43,37 @@ def _testAtoms(): # Check the atoms thoroughly assert a1.element == s.Cu0.element assert a2.element == s.Ag0.element - assert a1.Uisoequiv == s.Cu0.Uiso.getValue() - assert a2.Uisoequiv == s.Ag0.Uiso.getValue() - assert a1.Bisoequiv == s.Cu0.Biso.getValue() - assert a2.Bisoequiv == s.Ag0.Biso.getValue() + assert a1.Uisoequiv == s.Cu0.Uiso.get_value() + assert a2.Uisoequiv == s.Ag0.Uiso.get_value() + assert a1.Bisoequiv == s.Cu0.Biso.get_value() + assert a2.Bisoequiv == s.Ag0.Biso.get_value() for i in range(1, 4): for j in range(i, 4): uijstru = getattr(a1, "U%i%i" % (i, j)) - uij = getattr(s.Cu0, "U%i%i" % (i, j)).getValue() - uji = getattr(s.Cu0, "U%i%i" % (j, i)).getValue() + uij = getattr(s.Cu0, "U%i%i" % (i, j)).get_value() + uji = getattr(s.Cu0, "U%i%i" % (j, i)).get_value() assert uijstru == uij assert uijstru == uji bijstru = getattr(a1, "B%i%i" % (i, j)) - bij = getattr(s.Cu0, "B%i%i" % (i, j)).getValue() - bji = getattr(s.Cu0, "B%i%i" % (j, i)).getValue() + bij = getattr(s.Cu0, "B%i%i" % (i, j)).get_value() + bji = getattr(s.Cu0, "B%i%i" % (j, i)).get_value() assert bijstru == bij assert bijstru == bji - assert a1.xyz[0] == s.Cu0.x.getValue() - assert a1.xyz[1] == s.Cu0.y.getValue() - assert a1.xyz[2] == s.Cu0.z.getValue() + assert a1.xyz[0] == s.Cu0.x.get_value() + assert a1.xyz[1] == s.Cu0.y.get_value() + assert a1.xyz[2] == s.Cu0.z.get_value() return def _testLattice(): # Test the lattice - assert dsstru.lattice.a == s.lattice.a.getValue() - assert dsstru.lattice.b == s.lattice.b.getValue() - assert dsstru.lattice.c == s.lattice.c.getValue() - assert dsstru.lattice.alpha == s.lattice.alpha.getValue() - assert dsstru.lattice.beta == s.lattice.beta.getValue() - assert dsstru.lattice.gamma == s.lattice.gamma.getValue() + assert dsstru.lattice.a == s.lattice.a.get_value() + assert dsstru.lattice.b == s.lattice.b.get_value() + assert dsstru.lattice.c == s.lattice.c.get_value() + assert dsstru.lattice.alpha == s.lattice.alpha.get_value() + assert dsstru.lattice.beta == s.lattice.beta.get_value() + assert dsstru.lattice.gamma == s.lattice.gamma.get_value() _testAtoms() _testLattice() diff --git a/tests/test_equation.py b/tests/test_equation.py index bd89881e..90b3dd75 100644 --- a/tests/test_equation.py +++ b/tests/test_equation.py @@ -69,7 +69,7 @@ def testSimpleFunction(make_args, noObserversInGlobalBuilders): assert v4 is eq.v4 assert 20 == eq() # 20 = 2.5*(1+3)*(4-2) - assert 20 == eq.getValue() # same as above + assert 20 == eq.get_value() # same as above assert 20 == eq.value # same as above assert 25 == eq(v1=2) # 25 = 2.5*(2+3)*(4-2) assert 50 == eq(v2=0) # 50 = 2.5*(2+3)*(4-0) @@ -164,7 +164,7 @@ def testEmbeddedEquation(make_args, noObserversInGlobalBuilders): v1.value = 1 assert 20 == eq() # 20 = 2.5*(1+3)*(4-2) - assert 20 == eq.getValue() # same as above + assert 20 == eq.get_value() # same as above assert 20 == eq.value # same as above assert 25 == eq(v1=2) # 25 = 2.5*(2+3)*(4-2) assert 50 == eq(v2=0) # 50 = 2.5*(2+3)*(4-0) diff --git a/tests/test_fitrecipe.py b/tests/test_fitrecipe.py index 022c27ca..0bd91169 100644 --- a/tests/test_fitrecipe.py +++ b/tests/test_fitrecipe.py @@ -219,8 +219,8 @@ def testResidual(self): ) self.assertEqual(2, self.fitcontribution.c.value) self.recipe.add_constraint(self.fitcontribution.A, var) - self.assertEqual(1, var.getValue()) - self.assertEqual(self.recipe.cont.A.getValue(), var.getValue()) + self.assertEqual(1, var.get_value()) + self.assertEqual(self.recipe.cont.A.get_value(), var.get_value()) # c is constrained to a constrained parameter. self.assertEqual(2, self.fitcontribution.c.value) # The equation should evaluate to sin(x+2) @@ -241,7 +241,7 @@ def testResidual(self): # give us chi2 = 0 again. self.recipe.remove_constraint(self.fitcontribution.c) self.fitcontribution.c.set_value(0) - res = self.recipe.residual([self.recipe.cont.A.getValue()]) + res = self.recipe.residual([self.recipe.cont.A.get_value()]) chi2 = 0 self.assertAlmostEqual(chi2, dot(res, res)) diff --git a/tests/test_literals.py b/tests/test_literals.py index 891bbde9..cfe3adb4 100644 --- a/tests/test_literals.py +++ b/tests/test_literals.py @@ -14,12 +14,15 @@ ############################################################################## """Tests for the diffpy.srfit.equation.literals module.""" +import re import unittest import numpy +import pytest import diffpy.srfit.equation.literals as literals import diffpy.srfit.equation.literals.abcs as abcs +from diffpy.srfit.equation.equationmod import Equation # ---------------------------------------------------------------------------- @@ -45,7 +48,7 @@ def testValue(self): """Test value setting.""" a = literals.Argument() - self.assertEqual(None, a.getValue()) + self.assertEqual(None, a.get_value()) # Test setting value a.set_value(3.14) @@ -53,7 +56,7 @@ def testValue(self): a.set_value(3.14) self.assertAlmostEqual(3.14, a.value) - self.assertAlmostEqual(3.14, a.getValue()) + self.assertAlmostEqual(3.14, a.get_value()) return @@ -102,7 +105,7 @@ def testValue(self): a.set_value(4) self.assertTrue(op._value is None) self.assertAlmostEqual(4, op.value) - self.assertAlmostEqual(4, op.getValue()) + self.assertAlmostEqual(4, op.get_value()) b.value = 2 self.assertTrue(op._value is None) @@ -114,16 +117,16 @@ def testAddLiteral(self): """Test adding a literal to an operator node.""" op = self.op - self.assertRaises(TypeError, op.getValue) + self.assertRaises(TypeError, op.get_value) op._value = 1 - self.assertEqual(op.getValue(), 1) + self.assertEqual(op.get_value(), 1) # Test addition and operations a = literals.Argument(name="a", value=0) b = literals.Argument(name="b", value=0) op.addLiteral(a) - self.assertRaises(TypeError, op.getValue) + self.assertRaises(TypeError, op.get_value) op.addLiteral(b) self.assertAlmostEqual(0, op.value) @@ -214,6 +217,69 @@ def test_value(self): return +# ---------------------------------------------------------------------------- +# Literal.getValue is deprecated in favor of Literal.get_value. Every Literal +# in the hierarchy must keep accepting the old name, warn with a message that +# names the replacement, and dispatch to the subclass implementation of +# get_value rather than to Literal's own NotImplementedError stub. + + +# C1: Argument holds the value directly. +# Expected: getValue warns and returns Argument.get_value. +def test_argument_get_value_deprecated(): + expected_msg = ( + "'diffpy.srfit.equation.literals.Literal.getValue' is deprecated " + "and will be removed in version 4.0.0. Please use " + "'diffpy.srfit.equation.literals.Literal.get_value' instead." + ) + expected_value = 3.5 + literal = literals.Argument(name="a", value=expected_value) + + with pytest.warns(DeprecationWarning, match=re.escape(expected_msg)): + actual_value = literal.getValue() + + assert actual_value == expected_value + + +# C2: Operator computes the value from its own literals. +# Expected: getValue warns and returns Operator.get_value. +def test_operator_get_value_deprecated(): + expected_msg = ( + "'diffpy.srfit.equation.literals.Literal.getValue' is deprecated " + "and will be removed in version 4.0.0. Please use " + "'diffpy.srfit.equation.literals.Literal.get_value' instead." + ) + expected_value = 3.5 + operator = literals.AdditionOperator() + operator.addLiteral(literals.Argument(name="a", value=1.5)) + operator.addLiteral(literals.Argument(name="b", value=2.0)) + + with pytest.warns(DeprecationWarning, match=re.escape(expected_msg)): + actual_value = operator.getValue() + + assert actual_value == expected_value + + +# C3: Equation evaluates the operator tree at its root. +# Expected: getValue warns and returns Equation.get_value. +def test_equation_get_value_deprecated(): + expected_msg = ( + "'diffpy.srfit.equation.literals.Literal.getValue' is deprecated " + "and will be removed in version 4.0.0. Please use " + "'diffpy.srfit.equation.literals.Literal.get_value' instead." + ) + expected_value = 3.5 + operator = literals.AdditionOperator() + operator.addLiteral(literals.Argument(name="a", value=1.5)) + operator.addLiteral(literals.Argument(name="b", value=2.0)) + equation = Equation(name="eq", root=operator) + + with pytest.warns(DeprecationWarning, match=re.escape(expected_msg)): + actual_value = equation.getValue() + + assert actual_value == expected_value + + # ---------------------------------------------------------------------------- if __name__ == "__main__": diff --git a/tests/test_objcrystparset.py b/tests/test_objcrystparset.py index 533092c2..1edc95b4 100644 --- a/tests/test_objcrystparset.py +++ b/tests/test_objcrystparset.py @@ -174,26 +174,26 @@ def testObjCrystParSet(self): def _testCrystal(): # Test the lattice assert occryst.a == pytest.approx(cryst.a.value) - assert occryst.b == pytest.approx(cryst.b.getValue()) - assert occryst.c == pytest.approx(cryst.c.getValue()) - assert occryst.alpha == pytest.approx(cryst.alpha.getValue()) - assert occryst.beta == pytest.approx(cryst.beta.getValue()) - assert occryst.gamma == pytest.approx(cryst.gamma.getValue()) + assert occryst.b == pytest.approx(cryst.b.get_value()) + assert occryst.c == pytest.approx(cryst.c.get_value()) + assert occryst.alpha == pytest.approx(cryst.alpha.get_value()) + assert occryst.beta == pytest.approx(cryst.beta.get_value()) + assert occryst.gamma == pytest.approx(cryst.gamma.get_value()) return def _testMolecule(): # Test position / occupancy - assert ocmol.X == pytest.approx(m.x.getValue()) - assert ocmol.Y == pytest.approx(m.y.getValue()) - assert ocmol.Z == pytest.approx(m.z.getValue()) - assert ocmol.Occupancy == pytest.approx(m.occ.getValue()) + assert ocmol.X == pytest.approx(m.x.get_value()) + assert ocmol.Y == pytest.approx(m.y.get_value()) + assert ocmol.Z == pytest.approx(m.z.get_value()) + assert ocmol.Occupancy == pytest.approx(m.occ.get_value()) # Test orientation - assert ocmol.Q0 == pytest.approx(m.q0.getValue()) - assert ocmol.Q1 == pytest.approx(m.q1.getValue()) - assert ocmol.Q2 == pytest.approx(m.q2.getValue()) - assert ocmol.Q3 == pytest.approx(m.q3.getValue()) + assert ocmol.Q0 == pytest.approx(m.q0.get_value()) + assert ocmol.Q1 == pytest.approx(m.q1.get_value()) + assert ocmol.Q2 == pytest.approx(m.q2.get_value()) + assert ocmol.Q3 == pytest.approx(m.q3.get_value()) # Check the atoms thoroughly for i in range(len(ocmol)): @@ -201,11 +201,11 @@ def _testMolecule(): ocsp = oca.GetScatteringPower() a = m.atoms[i] assert ocsp.GetSymbol() == a.element - assert oca.X == pytest.approx(a.x.getValue()) - assert oca.Y == pytest.approx(a.y.getValue()) - assert oca.Z == pytest.approx(a.z.getValue()) - assert oca.Occupancy == pytest.approx(a.occ.getValue()) - assert ocsp.Biso == pytest.approx(a.Biso.getValue()) + assert oca.X == pytest.approx(a.x.get_value()) + assert oca.Y == pytest.approx(a.y.get_value()) + assert oca.Z == pytest.approx(a.z.get_value()) + assert oca.Occupancy == pytest.approx(a.occ.get_value()) + assert ocsp.Biso == pytest.approx(a.Biso.get_value()) return _testCrystal() @@ -395,15 +395,19 @@ def testExplicitBondLengthParameter(self): # Have another atom tag along for the ride p1.addAtoms([a20]) - xyz0 = numpy.array([a0.x.getValue(), a0.y.getValue(), a0.z.getValue()]) - xyz7 = numpy.array([a7.x.getValue(), a7.y.getValue(), a7.z.getValue()]) + xyz0 = numpy.array( + [a0.x.get_value(), a0.y.get_value(), a0.z.get_value()] + ) + xyz7 = numpy.array( + [a7.x.get_value(), a7.y.get_value(), a7.z.get_value()] + ) xyz20 = numpy.array( - [a20.x.getValue(), a20.y.getValue(), a20.z.getValue()] + [a20.x.get_value(), a20.y.get_value(), a20.z.get_value()] ) dd = xyz0 - xyz7 d0 = numpy.dot(dd, dd) ** 0.5 - assert d0 == pytest.approx(p1.getValue(), abs=1e-6) + assert d0 == pytest.approx(p1.get_value(), abs=1e-6) # Record the unit direction of change for later u = dd / d0 @@ -413,16 +417,16 @@ def testExplicitBondLengthParameter(self): p1.set_value(scale * d0) # Verify that it has changed. - assert scale * d0 == pytest.approx(p1.getValue(), abs=1e-6) + assert scale * d0 == pytest.approx(p1.get_value(), abs=1e-6) xyz0a = numpy.array( - [a0.x.getValue(), a0.y.getValue(), a0.z.getValue()] + [a0.x.get_value(), a0.y.get_value(), a0.z.get_value()] ) xyz7a = numpy.array( - [a7.x.getValue(), a7.y.getValue(), a7.z.getValue()] + [a7.x.get_value(), a7.y.get_value(), a7.z.get_value()] ) xyz20a = numpy.array( - [a20.x.getValue(), a20.y.getValue(), a20.z.getValue()] + [a20.x.get_value(), a20.y.get_value(), a20.z.get_value()] ) dda = xyz0a - xyz7a @@ -457,13 +461,17 @@ def testExplicitBondAngleParameter(self): a20 = m.atoms[20] a25 = m.atoms[25] - xyz0 = numpy.array([a0.x.getValue(), a0.y.getValue(), a0.z.getValue()]) - xyz7 = numpy.array([a7.x.getValue(), a7.y.getValue(), a7.z.getValue()]) + xyz0 = numpy.array( + [a0.x.get_value(), a0.y.get_value(), a0.z.get_value()] + ) + xyz7 = numpy.array( + [a7.x.get_value(), a7.y.get_value(), a7.z.get_value()] + ) xyz20 = numpy.array( - [a20.x.getValue(), a20.y.getValue(), a20.z.getValue()] + [a20.x.get_value(), a20.y.get_value(), a20.z.get_value()] ) xyz25 = numpy.array( - [a25.x.getValue(), a25.y.getValue(), a25.z.getValue()] + [a25.x.get_value(), a25.y.get_value(), a25.z.get_value()] ) v1 = xyz7 - xyz0 @@ -478,26 +486,26 @@ def testExplicitBondAngleParameter(self): # Have another atom tag along for the ride p1.addAtoms([a25]) - assert angle0 == pytest.approx(p1.getValue(), abs=1e-6) + assert angle0 == pytest.approx(p1.get_value(), abs=1e-6) # Change the value scale = 1.05 p1.set_value(scale * angle0) # Verify that it has changed. - assert scale * angle0 == pytest.approx(p1.getValue(), abs=1e-6) + assert scale * angle0 == pytest.approx(p1.get_value(), abs=1e-6) xyz0a = numpy.array( - [a0.x.getValue(), a0.y.getValue(), a0.z.getValue()] + [a0.x.get_value(), a0.y.get_value(), a0.z.get_value()] ) xyz7a = numpy.array( - [a7.x.getValue(), a7.y.getValue(), a7.z.getValue()] + [a7.x.get_value(), a7.y.get_value(), a7.z.get_value()] ) xyz20a = numpy.array( - [a20.x.getValue(), a20.y.getValue(), a20.z.getValue()] + [a20.x.get_value(), a20.y.get_value(), a20.z.get_value()] ) xyz25a = numpy.array( - [a25.x.getValue(), a25.y.getValue(), a25.z.getValue()] + [a25.x.get_value(), a25.y.get_value(), a25.z.get_value()] ) v1a = xyz7a - xyz0a @@ -532,16 +540,20 @@ def testExplicitDihedralAngleParameter(self): a25 = m.atoms[25] a33 = m.atoms[33] - xyz0 = numpy.array([a0.x.getValue(), a0.y.getValue(), a0.z.getValue()]) - xyz7 = numpy.array([a7.x.getValue(), a7.y.getValue(), a7.z.getValue()]) + xyz0 = numpy.array( + [a0.x.get_value(), a0.y.get_value(), a0.z.get_value()] + ) + xyz7 = numpy.array( + [a7.x.get_value(), a7.y.get_value(), a7.z.get_value()] + ) xyz20 = numpy.array( - [a20.x.getValue(), a20.y.getValue(), a20.z.getValue()] + [a20.x.get_value(), a20.y.get_value(), a20.z.get_value()] ) xyz25 = numpy.array( - [a25.x.getValue(), a25.y.getValue(), a25.z.getValue()] + [a25.x.get_value(), a25.y.get_value(), a25.z.get_value()] ) xyz33 = numpy.array( - [a33.x.getValue(), a33.y.getValue(), a33.z.getValue()] + [a33.x.get_value(), a33.y.get_value(), a33.z.get_value()] ) v12 = xyz0 - xyz7 @@ -559,29 +571,29 @@ def testExplicitDihedralAngleParameter(self): # Have another atom tag along for the ride p1.addAtoms([a33]) - assert angle0 == pytest.approx(p1.getValue(), abs=1e-6) + assert angle0 == pytest.approx(p1.get_value(), abs=1e-6) # Change the value scale = 1.05 p1.set_value(scale * angle0) # Verify that it has changed. - assert scale * angle0 == pytest.approx(p1.getValue(), abs=1e-6) + assert scale * angle0 == pytest.approx(p1.get_value(), abs=1e-6) xyz0a = numpy.array( - [a0.x.getValue(), a0.y.getValue(), a0.z.getValue()] + [a0.x.get_value(), a0.y.get_value(), a0.z.get_value()] ) xyz7a = numpy.array( - [a7.x.getValue(), a7.y.getValue(), a7.z.getValue()] + [a7.x.get_value(), a7.y.get_value(), a7.z.get_value()] ) xyz20a = numpy.array( - [a20.x.getValue(), a20.y.getValue(), a20.z.getValue()] + [a20.x.get_value(), a20.y.get_value(), a20.z.get_value()] ) xyz25a = numpy.array( - [a25.x.getValue(), a25.y.getValue(), a25.z.getValue()] + [a25.x.get_value(), a25.y.get_value(), a25.z.get_value()] ) xyz33a = numpy.array( - [a33.x.getValue(), a33.y.getValue(), a33.z.getValue()] + [a33.x.get_value(), a33.y.get_value(), a33.z.get_value()] ) v12a = xyz0a - xyz7a diff --git a/tests/test_parameter.py b/tests/test_parameter.py index c954256b..732a085e 100644 --- a/tests/test_parameter.py +++ b/tests/test_parameter.py @@ -14,6 +14,7 @@ ############################################################################## """Tests for refinableobj module.""" +import re import unittest import numpy as np @@ -33,25 +34,25 @@ def testSetValue(self): par_l = Parameter("l") par_l.set_value(3.14) - self.assertAlmostEqual(3.14, par_l.getValue()) + self.assertAlmostEqual(3.14, par_l.get_value()) # Try array import numpy x = numpy.arange(0, 10, 0.1) par_l.setValue(x) - self.assertTrue(par_l.getValue() is x) + self.assertTrue(par_l.get_value() is x) self.assertTrue(par_l.value is x) # Change the array y = numpy.arange(0, 10, 0.5) par_l.value = y - self.assertTrue(par_l.getValue() is y) + self.assertTrue(par_l.get_value() is y) self.assertTrue(par_l.value is y) # Back to scalar par_l.set_value(1.01) - self.assertAlmostEqual(1.01, par_l.getValue()) + self.assertAlmostEqual(1.01, par_l.get_value()) self.assertAlmostEqual(1.01, par_l.value) return @@ -66,16 +67,16 @@ def testProxy(self): la = ParameterProxy("l2", par_l) self.assertEqual("l2", la.name) - self.assertEqual(par_l.getValue(), la.getValue()) + self.assertEqual(par_l.get_value(), la.get_value()) # Change the parameter par_l.value = 2.3 - self.assertEqual(par_l.getValue(), la.getValue()) + self.assertEqual(par_l.get_value(), la.get_value()) self.assertEqual(par_l.value, la.value) # Change the proxy la.value = 3.2 - self.assertEqual(par_l.getValue(), la.getValue()) + self.assertEqual(par_l.get_value(), la.get_value()) self.assertEqual(par_l.value, la.value) return @@ -92,34 +93,34 @@ def testWrapper(self): # Try Accessor adaptation la = ParameterAdapter( - "l", par_l, getter=Parameter.getValue, setter=Parameter.set_value + "l", par_l, getter=Parameter.get_value, setter=Parameter.set_value ) self.assertEqual(par_l.name, la.name) - self.assertEqual(par_l.getValue(), la.getValue()) + self.assertEqual(par_l.get_value(), la.get_value()) # Change the parameter par_l.set_value(2.3) - self.assertEqual(par_l.getValue(), la.getValue()) + self.assertEqual(par_l.get_value(), la.get_value()) # Change the adapter la.set_value(3.2) - self.assertEqual(par_l.getValue(), la.getValue()) + self.assertEqual(par_l.get_value(), la.get_value()) # Try Attribute adaptation la = ParameterAdapter("l", par_l, attr="value") self.assertEqual(par_l.name, la.name) self.assertEqual("value", la.attr) - self.assertEqual(par_l.getValue(), la.getValue()) + self.assertEqual(par_l.get_value(), la.get_value()) # Change the parameter par_l.set_value(2.3) - self.assertEqual(par_l.getValue(), la.getValue()) + self.assertEqual(par_l.get_value(), la.get_value()) # Change the adapter la.set_value(3.2) - self.assertEqual(par_l.getValue(), la.getValue()) + self.assertEqual(par_l.get_value(), la.get_value()) return @@ -214,5 +215,60 @@ def test_boundWindow(value, lower_radius, upper_radius, expected): assert actual == expected +# ---------------------------------------------------------------------------- +# getValue is deprecated in favor of get_value. The old name must still work, +# emit a DeprecationWarning naming its replacement, and return exactly what +# get_value returns, for Parameter and for both of its wrapping subclasses. + + +class _ValueHolder: + """A plain object for ParameterAdapter to wrap.""" + + def __init__(self, value): + self.value = value + + +def _make_parameter(value): + return Parameter("l", value) + + +def _make_parameter_proxy(value): + return ParameterProxy("l_proxy", Parameter("l", value)) + + +def _make_parameter_adapter(value): + return ParameterAdapter("l_adapted", _ValueHolder(value), attr="value") + + +@pytest.mark.parametrize( + "make_parameter, input_value", + [ + # C1: Parameter stores the value itself. + # Expected: getValue warns and returns the stored value. + (_make_parameter, 3.14), + # C2: ParameterProxy defers to the Parameter it proxies. + # Expected: getValue warns and returns the proxied value. + (_make_parameter_proxy, 3.14), + # C3: ParameterAdapter defers to the attribute it wraps. + # Expected: getValue warns and returns the wrapped value. + (_make_parameter_adapter, 3.14), + ], +) +def test_getValue_warns_and_forwards(make_parameter, input_value): + expected_msg = ( + "'diffpy.srfit.fitbase.Parameter.getValue' is deprecated and will " + "be removed in version 4.0.0. Please use " + "'diffpy.srfit.fitbase.Parameter.get_value' instead." + ) + parameter = make_parameter(input_value) + expected_value = parameter.get_value() + + with pytest.warns(DeprecationWarning, match=re.escape(expected_msg)): + actual_value = parameter.getValue() + + assert actual_value == expected_value + assert actual_value == input_value + + if __name__ == "__main__": unittest.main() diff --git a/tests/test_pdfgenerator.py b/tests/test_pdfgenerator.py index 0c56933a..4d915928 100644 --- a/tests/test_pdfgenerator.py +++ b/tests/test_pdfgenerator.py @@ -49,12 +49,12 @@ def testGenerator(diffpy_srreal_available, datafile): for par in gen.iterPars(recurse=False): pname = par.name defval = calc._getDoubleAttr(pname) - assert defval == par.getValue() + assert defval == par.get_value() # Test setting values par.set_value(1.0) - assert 1.0 == par.getValue() + assert 1.0 == par.get_value() par.set_value(defval) - assert defval == par.getValue() + assert defval == par.get_value() r = numpy.arange(0, 10, 0.1) y = gen(r) diff --git a/tests/test_recipeorganizer.py b/tests/test_recipeorganizer.py index 1b953c3b..96a82f88 100644 --- a/tests/test_recipeorganizer.py +++ b/tests/test_recipeorganizer.py @@ -294,7 +294,7 @@ def test_constrain_parameter(self): p2.set_value(10) self.m._constraints[p1].update() - self.assertEqual(20, p1.getValue()) + self.assertEqual(20, p1.get_value()) # Check errors on unregistered parameters self.assertRaises(ValueError, self.m.constrain, p1, "2*p3") @@ -310,7 +310,7 @@ def test_constrain_parameter(self): self.m.add_constraint(p1, p2) p2.set_value(7) self.m._constraints[p1].update() - self.assertEqual(7, p1.getValue()) + self.assertEqual(7, p1.get_value()) self.m.clear_all_constraints() actual_constrained_params = self.m.get_constrained_parmeters() @@ -425,9 +425,9 @@ def __init__(self, name): return def __call__(self, x): - A = self.A.getValue() - c = self.center.getValue() - w = self.width.getValue() + A = self.A.get_value() + c = self.center.get_value() + w = self.width.get_value() return A * numpy.exp(-0.5 * ((x - c) / w) ** 2) # End class GCalc @@ -470,9 +470,9 @@ def __init__(self, name): return def __call__(self, x): - A = self.A.getValue() - c = self.center.getValue() - w = self.width.getValue() + A = self.A.get_value() + c = self.center.get_value() + w = self.width.get_value() return A * numpy.exp(-0.5 * ((x - c) / w) ** 2) # End class GCalc @@ -566,8 +566,8 @@ def test_register_string_function(self): self.m._new_parameter("y", 3.0) # Make sure that x and y are in the organizer - self.assertEqual(0, self.m.x.getValue()) - self.assertEqual(3.0, self.m.y.getValue()) + self.assertEqual(0, self.m.x.get_value()) + self.assertEqual(3.0, self.m.y.get_value()) # Use eq1 in some equations diff --git a/tests/test_sas.py b/tests/test_sas.py index 516d387a..dc9ea3d9 100644 --- a/tests/test_sas.py +++ b/tests/test_sas.py @@ -121,13 +121,13 @@ def test_generator(sas_available): for pname in model.params: defval = model.getParam(pname) par = gen.get(pname) - assert defval == par.getValue() + assert defval == par.get_value() # Test setting values par.set_value(1.0) - assert 1.0 == par.getValue() + assert 1.0 == par.get_value() assert 1.0 == model.getParam(pname) par.set_value(defval) - assert defval == par.getValue() + assert defval == par.get_value() assert defval == model.getParam(pname) r = numpy.arange(1, 10, 0.1, dtype=float) diff --git a/tests/test_sgconstraints.py b/tests/test_sgconstraints.py index 40319d76..56498ff1 100644 --- a/tests/test_sgconstraints.py +++ b/tests/test_sgconstraints.py @@ -51,9 +51,9 @@ def test_ObjCryst_constrain_space_group(pyobjcryst_available): assert lattice.alpha.const assert lattice.beta.const assert lattice.gamma.const - assert pi / 2 == lattice.alpha.getValue() - assert pi / 2 == lattice.beta.getValue() - assert pi / 2 == lattice.gamma.getValue() + assert pi / 2 == lattice.alpha.get_value() + assert pi / 2 == lattice.beta.get_value() + assert pi / 2 == lattice.gamma.get_value() assert not lattice.a.const assert not lattice.b.const @@ -127,7 +127,7 @@ def test_DiffPy_constrain_as_space_group(datafile, pyobjcryst_available): # Make sure that the new parameters were created for par in sgpars: assert par is not None - assert par.getValue() is not None + assert par.get_value() is not None # Test the unconstrained atoms for scatterer in parset.getScatterers()[1::2]: diff --git a/tests/test_visitors.py b/tests/test_visitors.py index c7130be2..49381361 100644 --- a/tests/test_visitors.py +++ b/tests/test_visitors.py @@ -205,7 +205,7 @@ def testSimpleFunction(self): # plus2 has no arguments yet. Verify this. with pytest.raises(TypeError): - mult.getValue() + mult.get_value() # Add the arguments to plus2. plus2.addLiteral(v4) plus2.addLiteral(v5) diff --git a/tests/test_weakrefcallable.py b/tests/test_weakrefcallable.py index 688bc563..e45178ef 100644 --- a/tests/test_weakrefcallable.py +++ b/tests/test_weakrefcallable.py @@ -58,7 +58,7 @@ def test___call__(self): self.assertTrue(None is f._eq._value) # check WeakBoundMethod behavior with no fallback x = Parameter("x", value=3) - wgetx = weak_ref(x.getValue) + wgetx = weak_ref(x.get_value) self.assertEqual(3, wgetx()) del x self.assertRaises(ReferenceError, wgetx)