def test_output_checking(): la = LabelledFunction(add, output_names=['shrubbery']) assert la._has_never_been_run assert la(1, 2) == 3 # Wrong number of outputs la = LabelledFunction(add, output_names=['moose', 'llama']) assert la._has_never_been_run with pytest.raises(TypeError): la(1, 2)
def test_namespace(): namespace = {'x': 0, 'y': 3, 'z': 2} new_namespace = LabelledFunction(add).apply_in_namespace(namespace) assert new_namespace == {'x+y': 3, 'x': 0, 'y': 3, 'z': 2} import xarray as xr in_ds = xr.Dataset(coords={ 'radius': np.linspace(0, 1, 10), 'length': np.linspace(0, 1, 10) }) out_ds = LabelledFunction(cylinder_volume).apply_in_namespace(in_ds) assert 'volume' in out_ds.data_vars
def test_set_default(): lc = LabelledFunction(cylinder_volume) llc = lc.set_default(radius=1.0) assert llc(length=1.0) == np.pi assert copy(llc)(length=1.0) == np.pi lllc = lc.set_default(radius=1.0, length=1.0) assert lllc() == np.pi rllc = lllc.reset_default('radius', 'length') assert rllc.default_values == {} with pytest.raises(TypeError): rllc()
def let(**kwargs): name = "let " + ", ".join( (f"{name}={value}" for name, value in kwargs.items())) return LabelledFunction(lambda: tuple(kwargs.values()), name=name, input_names=[], output_names=list(kwargs.keys()))
def test_labelling(): lpi = LabelledFunction(compute_pi) assert lpi() == compute_pi() assert lpi.__wrapped__.__code__ == compute_pi.__code__ assert lpi.name == compute_pi.__name__ assert list(lpi.input_names) == [] assert list(lpi.output_names) == ['pi'] assert str(lpi) == "compute_pi() -> (pi)" lc = LabelledFunction(cube) assert lc(0) == cube(0) assert list(lc.input_names) == ['x'] assert list(lc.output_names) == ['length', 'area', 'volume'] assert str(lc) == "cube(x) -> (length, area, volume)" assert lc.rename("square parallelepiped").name == "square parallelepiped" assert lc.name == "cube"
def relabel(old, new): def identity(**kwargs): return {new: kwargs[old]} lf = LabelledFunction(identity, name=f"relabel {old} as {new}", input_names=[old], output_names=[new]) return lf
def test_method(): class A: def __init__(self): self.a = 10.0 def f(self, x): y = 2 * x + self.a return y lab_Af = LabelledFunction(A().f) assert list(lab_Af.input_names) == ['x'] assert list(lab_Af.output_names) == ['y'] assert lab_Af(2) == 14 assert lab_Af(x=2) == 14 lab_f = LabelledFunction(A.f) assert list(lab_f.input_names) == ['self', 'x'] assert list(lab_f.output_names) == ['y'] assert lab_f(A(), 2) == 14 assert lab_f(A(), x=2) == 14
def show(*names): def showing(**kwargs): showed = {name: kwargs[name] for name in names} print(showed) return showed lf = LabelledFunction(showing, name="showing " + " ".join(names), input_names=names, output_names=names) return lf
def test_fix(): lc = LabelledFunction(cylinder_volume) llc = lc.fix(radius=1.0) assert llc(length=1.0) == np.pi assert keeping_inputs(llc)(length=1.0) == {'length': 1.0, 'volume': np.pi}