2
2
# coding: utf-8
3
3
4
4
import pytest
5
- import xarray as xr
6
5
from copy import copy
7
6
8
- from hypothesis import given , settings
9
- from hypothesis .strategies import floats
10
-
11
- from labelled_functions .labels import *
12
-
7
+ from labelled_functions .labels import LabelledFunction
13
8
from example_functions import *
14
9
15
- number = floats (allow_nan = False , allow_infinity = False )
16
-
17
10
18
11
def test_labelled_class ():
19
- ldt = LabelledFunction (compute_pi )
20
- assert ldt () == compute_pi (), "The LabelledFunction is not callable"
21
- assert ldt .__wrapped__ .__code__ == compute_pi .__code__ , "Attributes are not passed to encapsulated function"
22
- assert ldt .name == compute_pi .__name__ , "Name is wrong"
23
- assert str (ldt ) == "compute_pi() -> (pi)" , "String representation is wrong"
12
+ lpi = LabelledFunction (compute_pi )
13
+ assert lpi () == compute_pi ()
14
+ assert lpi .__wrapped__ .__code__ == compute_pi .__code__
15
+ assert lpi .name == compute_pi .__name__
16
+ assert list (lpi .input_names ) == []
17
+ assert list (lpi .output_names ) == ['pi' ]
18
+ assert str (lpi ) == "compute_pi() -> (pi)"
24
19
25
20
lc = LabelledFunction (cube )
26
21
assert lc (0 ) == cube (0 )
27
- assert lc .input_names == ['x' ]
28
- assert lc .output_names == ['length' , 'area' , 'volume' ]
22
+ assert list ( lc .input_names ) == ['x' ]
23
+ assert list ( lc .output_names ) == ['length' , 'area' , 'volume' ]
29
24
assert str (lc ) == "cube(x) -> (length, area, volume)"
30
25
31
26
@@ -35,15 +30,6 @@ def test_idempotence():
35
30
assert llc is lc
36
31
37
32
38
- def test_output_checking ():
39
- la = LabelledFunction (add )
40
- assert la ._has_never_been_run
41
-
42
- la .output_names = ['moose' , 'llama' ]
43
- with pytest .raises (TypeError ):
44
- la (1 , 2 )
45
-
46
-
47
33
def test_method ():
48
34
class A :
49
35
def __init__ (self ):
@@ -53,63 +39,77 @@ def f(self, x):
53
39
y = 2 * x + self .a
54
40
return y
55
41
56
- lab_Af = label (A ().f )
42
+ lab_Af = LabelledFunction (A ().f )
57
43
assert list (lab_Af .input_names ) == ['x' ]
58
44
assert list (lab_Af .output_names ) == ['y' ]
59
45
assert lab_Af (2 ) == 14
60
46
assert lab_Af (x = 2 ) == 14
61
47
62
- lab_f = label (A .f )
48
+ lab_f = LabelledFunction (A .f )
63
49
assert list (lab_f .input_names ) == ['self' , 'x' ]
64
50
assert list (lab_f .output_names ) == ['y' ]
65
51
assert lab_f (A (), 2 ) == 14
66
52
assert lab_f (A (), x = 2 ) == 14
67
53
68
54
55
+ def test_output_checking ():
56
+ la = LabelledFunction (add , output_names = ['shrubbery' ])
57
+ assert la ._has_never_been_run
58
+ assert la (1 , 2 ) == 3
59
+
60
+ # Wrong number of outputs
61
+ la = LabelledFunction (add , output_names = ['moose' , 'llama' ])
62
+ assert la ._has_never_been_run
63
+ with pytest .raises (TypeError ):
64
+ la (1 , 2 )
65
+
66
+
69
67
def test_recorder ():
70
68
a , b = 1 , 2
71
69
72
- assert label (compute_pi ).recorded_call () == {'pi' : 3.14159 }
70
+ assert LabelledFunction (compute_pi ).recorded_call () == {'pi' : 3.14159 }
73
71
74
- assert label (double ).recorded_call (a ) == {'x' : a , '2*x' : 2 * a }
72
+ assert LabelledFunction (double ).recorded_call (a ) == {'x' : a , '2*x' : 2 * a }
75
73
76
- assert label (optional_double ).recorded_call (a ) == {'x' : a , '2*x' : 2 * a }
77
- assert label (optional_double ).recorded_call (x = a ) == {'x' : a , '2*x' : 2 * a }
78
- assert label (optional_double ).recorded_call () == {'x' : 0 , '2*x' : 0 }
74
+ assert LabelledFunction (optional_double ).recorded_call (a ) == {'x' : a , '2*x' : 2 * a }
75
+ assert LabelledFunction (optional_double ).recorded_call (x = a ) == {'x' : a , '2*x' : 2 * a }
76
+ assert LabelledFunction (optional_double ).recorded_call () == {'x' : 0 , '2*x' : 0 }
79
77
80
- assert label (add ).recorded_call (a , b ) == {'x' : a , 'y' : b , 'x+y' : a + b }
78
+ assert LabelledFunction (add ).recorded_call (a , b ) == {'x' : a , 'y' : b , 'x+y' : a + b }
81
79
82
- assert label (optional_add ).recorded_call (a , b ) == {'x' : a , 'y' : b , 'x+y' : a + b }
83
- assert label (optional_add ).recorded_call (a ) == {'x' : a , 'y' : 0 , 'x+y' : a }
84
- assert label (optional_add ).recorded_call () == {'x' : 0 , 'y' : 0 , 'x+y' : 0 }
85
- assert label (optional_add ).recorded_call (x = a , y = b ) == {'x' : a , 'y' : b , 'x+y' : a + b }
86
- assert label (optional_add ).recorded_call (y = a , x = b ) == {'x' : b , 'y' : a , 'x+y' : a + b }
87
- assert label (optional_add ).recorded_call (y = a ) == {'x' : 0 , 'y' : a , 'x+y' : a }
80
+ assert LabelledFunction (optional_add ).recorded_call (a , b ) == {'x' : a , 'y' : b , 'x+y' : a + b }
81
+ assert LabelledFunction (optional_add ).recorded_call (a ) == {'x' : a , 'y' : 0 , 'x+y' : a }
82
+ assert LabelledFunction (optional_add ).recorded_call () == {'x' : 0 , 'y' : 0 , 'x+y' : 0 }
83
+ assert LabelledFunction (optional_add ).recorded_call (x = a , y = b ) == {'x' : a , 'y' : b , 'x+y' : a + b }
84
+ assert LabelledFunction (optional_add ).recorded_call (y = a , x = b ) == {'x' : b , 'y' : a , 'x+y' : a + b }
85
+ assert LabelledFunction (optional_add ).recorded_call (y = a ) == {'x' : 0 , 'y' : a , 'x+y' : a }
88
86
89
- assert label (cube ).recorded_call (a ) == {'x' : a , 'length' : 12 * a , 'area' : 6 * a ** 2 , 'volume' : a ** 3 }
87
+ assert LabelledFunction (cube ).recorded_call (a ) == {'x' : a , 'length' : 12 * a , 'area' : 6 * a ** 2 , 'volume' : a ** 3 }
90
88
91
89
with pytest .raises (TypeError ):
92
- label (all_kinds_of_args ).recorded_call (0 , 1 , 2 , 3 )
90
+ LabelledFunction (all_kinds_of_args ).recorded_call (0 , 1 , 2 , 3 )
93
91
94
- assert label (all_kinds_of_args ).recorded_call (0 , 1 , z = 2 , t = 3 ) == {'x' : 0 , 'y' : 1 , 'z' : 2 , 't' : 3 }
95
- assert label (all_kinds_of_args ).recorded_call (x = 0 , y = 1 , z = 2 , t = 3 ) == {'x' : 0 , 'y' : 1 , 'z' : 2 , 't' : 3 }
96
- assert label (all_kinds_of_args ).recorded_call (x = 0 , z = 2 , t = 3 ) == {'x' : 0 , 'y' : 1 , 'z' : 2 , 't' : 3 }
97
- assert label (all_kinds_of_args ).recorded_call (x = 0 , z = 2 ) == {'x' : 0 , 'y' : 1 , 'z' : 2 , 't' : 3 }
98
- assert label (all_kinds_of_args ).recorded_call (z = 2 , t = 3 , x = 0 ) == {'x' : 0 , 'y' : 1 , 'z' : 2 , 't' : 3 }
92
+ assert LabelledFunction (all_kinds_of_args ).recorded_call (0 , 1 , z = 2 , t = 3 ) == {'x' : 0 , 'y' : 1 , 'z' : 2 , 't' : 3 }
93
+ assert LabelledFunction (all_kinds_of_args ).recorded_call (x = 0 , y = 1 , z = 2 , t = 3 ) == {'x' : 0 , 'y' : 1 , 'z' : 2 , 't' : 3 }
94
+ assert LabelledFunction (all_kinds_of_args ).recorded_call (x = 0 , z = 2 , t = 3 ) == {'x' : 0 , 'y' : 1 , 'z' : 2 , 't' : 3 }
95
+ assert LabelledFunction (all_kinds_of_args ).recorded_call (x = 0 , z = 2 ) == {'x' : 0 , 'y' : 1 , 'z' : 2 , 't' : 3 }
96
+ assert LabelledFunction (all_kinds_of_args ).recorded_call (z = 2 , t = 3 , x = 0 ) == {'x' : 0 , 'y' : 1 , 'z' : 2 , 't' : 3 }
99
97
100
98
101
99
def test_namespace ():
102
100
namespace = {'x' : 0 , 'y' : 3 , 'z' : 2 }
103
101
new_namespace = LabelledFunction (add ).apply_in_namespace (namespace )
104
102
assert new_namespace == {'x+y' : 3 , 'x' : 0 , 'y' : 3 , 'z' : 2 }
105
103
104
+ import xarray as xr
106
105
in_ds = xr .Dataset (coords = {'radius' : np .linspace (0 , 1 , 10 ),
107
106
'length' : np .linspace (0 , 1 , 10 )})
108
107
out_ds = LabelledFunction (cylinder_volume ).apply_in_namespace (in_ds )
109
108
assert 'volume' in out_ds .data_vars
110
109
110
+
111
111
def test_set_default ():
112
- lc = label (cylinder_volume )
112
+ lc = LabelledFunction (cylinder_volume )
113
113
llc = lc .set_default (radius = 1.0 )
114
114
assert llc (length = 1.0 ) == np .pi
115
115
assert copy (llc )(length = 1.0 ) == np .pi
@@ -122,15 +122,17 @@ def test_set_default():
122
122
with pytest .raises (TypeError ):
123
123
rllc ()
124
124
125
+
125
126
def test_hide ():
126
127
a = np .random .rand (1 )[0 ]
127
- assert label (optional_add ).hide ('x' ).recorded_call (y = a ) == {'y' : a , 'x+y' : a }
128
- assert label (optional_add ).hide ('y' ).recorded_call (y = a ) == {'x' : 0 , 'x+y' : a }
129
- assert label (optional_add ).hide ('y' ).recorded_call () == {'x' : 0 , 'x+y' : 0 }
130
- assert label (optional_add ).hide_all_but ('y' ).recorded_call () == {'y' : 0 , 'x+y' : 0 }
128
+ assert LabelledFunction (optional_add ).hide ('x' ).recorded_call (y = a ) == {'y' : a , 'x+y' : a }
129
+ assert LabelledFunction (optional_add ).hide ('y' ).recorded_call (y = a ) == {'x' : 0 , 'x+y' : a }
130
+ assert LabelledFunction (optional_add ).hide ('y' ).recorded_call () == {'x' : 0 , 'x+y' : 0 }
131
+ assert LabelledFunction (optional_add ).hide_all_but ('y' ).recorded_call () == {'y' : 0 , 'x+y' : 0 }
132
+
131
133
132
134
def test_fix ():
133
- lc = label (cylinder_volume )
135
+ lc = LabelledFunction (cylinder_volume )
134
136
llc = lc .fix (radius = 1.0 )
135
137
assert llc .default_values == {'radius' : 1.0 }
136
138
assert llc .hidden_inputs == {'radius' }
0 commit comments