def test_cat_valid(): with pytest.raises(ValueError): schema.Category( nodetype="category", input="x", content=[ schema.CategoryItem(key="30xyz", value=1.0), schema.CategoryItem(key=30, value=1.0), ], ) with pytest.raises(ValueError): schema.Category( nodetype="category", input="x", content=[ schema.CategoryItem(key="30xyz", value=1.0), schema.CategoryItem(key="30xyz", value=1.0), ], )
def make_cat(items, default): cset = wrap( schema.Correction( name="test", version=2, inputs=[ schema.Variable( name="cat", type="string" if isinstance(next(iter(items)), str) else "int", ) ], output=schema.Variable(name="a scale", type="real"), data=schema.Category( nodetype="category", input="cat", content=[ {"key": key, "value": value} for key, value in items.items() ], default=default, ), ) ) return cset["test"]
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