def test_formularef(): cset = wrap( schema.Correction( name="reftest", version=2, inputs=[ schema.Variable(name="x", type="real"), ], output=schema.Variable(name="a scale", type="real"), generic_formulas=[ schema.Formula( nodetype="formula", expression="[0] + [1]*x", parser="TFormula", variables=["x"], ), ], data=schema.Binning( nodetype="binning", input="x", edges=[0, 1, 2, 3], content=[ schema.FormulaRef( nodetype="formularef", index=0, parameters=[0.1, 0.2] ), schema.FormulaRef( nodetype="formularef", index=0, parameters=[1.1, -0.2] ), schema.FormulaRef( nodetype="formularef", index=0, parameters=[3.1, 0.5] ), ], flow="error", ), ) ) corr = cset["reftest"] assert corr.evaluate(0.5) == 0.1 + 0.2 * 0.5 assert corr.evaluate(1.5) == 1.1 + -0.2 * 1.5 assert corr.evaluate(2.5) == 3.1 + 0.5 * 2.5
def test_transform(): cset = wrap( schema.Correction( name="test", version=2, inputs=[ schema.Variable(name="torewrite", type="real"), ], output=schema.Variable(name="a scale", type="real"), data=schema.Transform( nodetype="transform", input="torewrite", rule=0.1, content=schema.Formula( nodetype="formula", expression="x", parser="TFormula", variables=["torewrite"], ), ), )) corr = cset["test"] assert corr.evaluate(0.5) == 0.1 assert corr.evaluate(1.5) == 0.1 cset = wrap( schema.Correction( name="test", version=2, inputs=[ schema.Variable(name="torewrite", type="int"), ], output=schema.Variable(name="a scale", type="real"), data=schema.Transform( nodetype="transform", input="torewrite", rule=schema.Category( nodetype="category", input="torewrite", content=[ { "key": 0, "value": 0 }, { "key": 1, "value": 4 }, { "key": 2, "value": 0 }, { "key": 9, "value": 3.000001 }, { "key": 10, "value": 2.999999 }, ], ), content=schema.Category( nodetype="category", input="torewrite", content=[ { "key": 0, "value": 0.0 }, { "key": 3, "value": 0.1 }, { "key": 4, "value": 0.2 }, ], ), ), )) corr = cset["test"] assert corr.evaluate(0) == 0.0 assert corr.evaluate(1) == 0.2 assert corr.evaluate(2) == 0.0 with pytest.raises(IndexError): corr.evaluate(3) assert corr.evaluate(9) == 0.1 assert corr.evaluate(10) == 0.1
def test_binning(): def binning(flow): cset = wrap( schema.Correction( name="test", version=2, inputs=[schema.Variable(name="x", type="real")], output=schema.Variable(name="a scale", type="real"), data=schema.Binning( nodetype="binning", input="x", edges=[0.0, 1.0, 3.0], content=[1.0, 2.0], flow=flow, ), )) return cset["test"] corr = binning(flow="error") with pytest.raises(RuntimeError): corr.evaluate(-1.0) assert corr.evaluate(0.0) == 1.0 assert corr.evaluate(0.2) == 1.0 assert corr.evaluate(1.0) == 2.0 with pytest.raises(RuntimeError): corr.evaluate(3.0) corr = binning(flow="clamp") assert corr.evaluate(-1.0) == 1.0 assert corr.evaluate(1.0) == 2.0 assert corr.evaluate(3.0) == 2.0 assert corr.evaluate(3000.0) == 2.0 corr = binning(flow=42.0) assert corr.evaluate(-1.0) == 42.0 assert corr.evaluate(0.0) == 1.0 assert corr.evaluate(1.0) == 2.0 assert corr.evaluate(2.9) == 2.0 assert corr.evaluate(3.0) == 42.0 def multibinning(flow): cset = wrap( schema.Correction( name="test", version=2, inputs=[ schema.Variable(name="x", type="real"), schema.Variable(name="y", type="real"), ], output=schema.Variable(name="a scale", type="real"), data=schema.MultiBinning( nodetype="multibinning", inputs=["x", "y"], edges=[ [0.0, 1.0, 3.0], [10.0, 20.0, 30.0, 40.0], ], content=[float(i) for i in range(2 * 3)], flow=flow, ), )) return cset["test"] corr = multibinning(flow="error") with pytest.raises(RuntimeError): corr.evaluate(0.0, 5.0) with pytest.raises(RuntimeError): corr.evaluate(-1.0, 5.0) assert corr.evaluate(0.0, 10.0) == 0.0 assert corr.evaluate(0.0, 20.0) == 1.0 assert corr.evaluate(0.0, 30.0) == 2.0 with pytest.raises(RuntimeError): corr.evaluate(0.0, 40.0) assert corr.evaluate(1.0, 10.0) == 3.0 assert corr.evaluate(1.0, 20.0) == 4.0 assert corr.evaluate(1.0, 30.0) == 5.0 with pytest.raises(RuntimeError): corr.evaluate(2.0, 5.0) corr = multibinning(flow="clamp") assert corr.evaluate(-1.0, 5.0) == 0.0 assert corr.evaluate(-1.0, 25.0) == 1.0 assert corr.evaluate(-1.0, 35.0) == 2.0 assert corr.evaluate(-1.0, 45.0) == 2.0 assert corr.evaluate(0.0, 45.0) == 2.0 assert corr.evaluate(2.0, 45.0) == 5.0 assert corr.evaluate(3.0, 45.0) == 5.0 assert corr.evaluate(3.0, 35.0) == 5.0 assert corr.evaluate(3.0, 25.0) == 4.0 assert corr.evaluate(3.0, 15.0) == 3.0 assert corr.evaluate(3.0, 5.0) == 3.0 assert corr.evaluate(0.0, 5.0) == 0.0 corr = multibinning(flow=42.0) assert corr.evaluate(-1.0, 5.0) == 42.0 assert corr.evaluate(2.0, 45.0) == 42.0 assert corr.evaluate(3.0, 5.0) == 42.0 corr = multibinning(flow=schema.Formula( nodetype="formula", expression="2.*x + 5.*y", parser="TFormula", variables=["x", "y"], )) assert corr.evaluate(-1.0, 5.0) == 2.0 * -1 + 5.0 * 5.0 assert corr.evaluate(0.0, 10.0) == 0.0