ReFRACtor
factory_test.py
Go to the documentation of this file.
1 import numpy as np
2 from functools import partial
3 
4 import refractor.factory.creator as creator
5 import refractor.factory.param as param
6 from refractor.factory import process_config
7 
8 from refractor import framework as rf
9 
10 def ParamReturnCreator(param_type, **kwargs):
11  class ReturnCreatorHelper(creator.base.Creator):
12  val = param_type(**kwargs)
13  def create(self, **kwargs):
14  return self.param("val")
15 
16  return ReturnCreatorHelper
17 
18 class AddCreator(creator.base.Creator):
19 
20  x = param.Scalar()
21  y = param.Scalar()
22 
23  def create(self, **kwargs):
24  x = self.param("x")
25  y = self.param("y")
26  return x + y
27 
29 
30  # Check that scalar params are correctly checked
31  config_def = {
32  'creator': ParamReturnCreator(param.Scalar),
33  'val': 10,
34  }
35 
36  config_inst = process_config(config_def)
37 
38  assert config_inst == 10
39 
40  # Check that non scalars cause errors
41  config_def['val'] = np.arange(1,6)
42 
43  try:
44  config_inst = process_config(config_def)
45  except param.ParamError:
46  assert True
47  else:
48  assert False
49 
51 
52  # Check that scalar params are correctly checked
53  config_def = {
54  'creator': ParamReturnCreator(partial(param.Scalar, dtype=str)),
55  'val': "10",
56  }
57 
58  config_inst = process_config(config_def)
59 
60  assert config_inst == "10"
61 
62  # Check that non scalars cause errors
63  config_def['val'] = 10
64 
65  try:
66  config_inst = process_config(config_def)
67  except param.ParamError:
68  assert True
69  else:
70  assert False
71 
73 
74  # Check that arrays are correctly handled
75  config_def = {
76  'creator': ParamReturnCreator(param.Array),
77  'val': np.arange(1,6),
78  }
79 
80  config_inst = process_config(config_def)
81 
82  assert np.all(config_inst == np.array([1,2,3,4,5]))
83 
84  # Check that non arrays cause errors
85  config_def['val'] = 10
86 
87  try:
88  config_inst = process_config(config_def)
89  except param.ParamError:
90  assert True
91  else:
92  assert False
93 
94  config_def['val'] = [1,2,3,4,5]
95 
96  try:
97  config_inst = process_config(config_def)
98  except param.ParamError:
99  assert True
100  else:
101  assert False
102 
104 
105  # Check that arrays are correctly handled
106  config_def = {
107  'creator': ParamReturnCreator(param.ArrayWithUnit),
108  'val': rf.ArrayWithUnit_double_1(np.arange(1,6), "m"),
109  }
110 
111  config_inst = process_config(config_def)
112 
113 
115 
116  # Check that iterables are correctly handled
117  config_def = {
118  'item1': {
119  'creator': ParamReturnCreator(param.Iterable),
120  'val': [1,2,3,4,5]
121  },
122  'item2': {
123  'creator': ParamReturnCreator(param.Iterable, val_type=np.int),
124  'val': [1,2,3,4,5]
125  },
126  }
127 
128  config_inst = process_config(config_def)
129 
130  assert np.all(np.array(config_inst['item1']) == np.arange(1,6))
131  assert np.all(np.array(config_inst['item2']) == np.arange(1,6))
132 
133  # Check arrays are considered iterable
134  config_def['item1']['val'] = np.arange(1,6)
135 
136  config_inst = process_config(config_def)
137 
138  assert np.all(np.array(config_inst['item1']) == np.arange(1,6))
139 
141 
142  class TestClass(object):
143  pass
144 
145  # Check that iterables are correctly handled
146  config_def = {
147  'creator': ParamReturnCreator(param.InstanceOf, val_type=TestClass),
148  'val': TestClass(),
149  }
150 
151  config_inst = process_config(config_def)
152 
153  assert isinstance(config_inst, TestClass)
154 
155  # Force error on type
156  config_def['val'] = 5
157 
158  try:
159  config_inst = process_config(config_def)
160  except param.ParamError:
161  assert True
162  else:
163  assert False
164 
166 
167  config_def = {
168  'creator': ParamReturnCreator(param.Scalar),
169  'val': lambda **kwargs: 10,
170  }
171 
172  config_inst = process_config(config_def)
173 
174  assert config_inst == 10
175 
177 
178  config_def = {
179  'item1': {
180  'creator': ParamReturnCreator(param.Scalar),
181  'val': 5,
182  },
183  'item2': {
184  'creator': ParamReturnCreator(param.Scalar),
185  'val': 10,
186  },
187  'item3': {
188  'creator': ParamReturnCreator(param.Scalar),
189  'val': {
190  'creator': ParamReturnCreator(param.Scalar),
191  'val': 15,
192  },
193  },
194  }
195 
196  config_inst = process_config(config_def)
197 
198  assert config_inst['item1'] == 5
199  assert config_inst['item2'] == 10
200  assert config_inst['item3'] == 15
201 
203 
204  config_def = {
205  'order': ['common', 'use_common'],
206  'common': {
207  'creator': creator.base.SaveToCommon,
208  'x': 5,
209  'y': 6,
210  },
211  'use_common': {
212  'creator': AddCreator,
213  },
214  }
215 
216  config_inst = process_config(config_def)
217 
218  assert config_inst['use_common'] == 11
219 
221 
222  class ChoiceCreator(creator.base.Creator):
223  some_val = param.Choice(param.Scalar(int), param.Scalar(float))
224 
225  def create(self, **kwargs):
226  return self.param("some_val")
227 
228  config_def = {
229  'item1': {
230  'creator': ChoiceCreator,
231  'some_val': 10,
232  },
233  'item2': {
234  'creator': ChoiceCreator,
235  'some_val': 5.0,
236  },
237  }
238 
239  config_inst = process_config(config_def)
240 
241  assert config_inst['item1'] == 10
242  assert config_inst['item2'] == 5.0
243 
244  config_def['item1']['some_val'] = "ABC"
245 
246  try:
247  config_inst = process_config(config_def)
248  except param.ParamError:
249  assert True
250  else:
251  assert False
252 
254 
255  class BoundParamCreator(creator.base.Creator):
256  some_val = param.Choice(param.Scalar(int), param.Scalar(int))
257 
258  def create(self, **kwargs):
259  return self.some_val()
260 
261  config_def = {
262  'item1': {
263  'creator': BoundParamCreator,
264  'some_val': 10,
265  },
266  'item2': {
267  'creator': BoundParamCreator,
268  'some_val': 5,
269  },
270  }
271 
272  config_inst = process_config(config_def)
273 
274  assert config_inst['item1'] == 10
275  assert config_inst['item2'] == 5
276 
278 
279  config_def = {
280  'item1': {
281  'creator': ParamReturnCreator(param.ObjectVector),
282  'val': rf.vector_double(),
283  },
284  'item2': {
285  'creator': ParamReturnCreator(param.ObjectVector, vec_type="double"),
286  'val': rf.vector_double(),
287  }
288  }
289 
290  config_inst = process_config(config_def)
291 
292  assert isinstance(config_inst['item1'], rf.vector_double)
293  assert isinstance(config_inst['item2'], rf.vector_double)
def test_array_with_unit_param()
def test_iterable_param()
def test_common_store()
def test_scalar_with_type()
Definition: factory_test.py:50
def ParamReturnCreator(param_type, kwargs)
Definition: factory_test.py:10
def test_object_vector()
def test_param_choice()
def test_instanceof_param()
def create(self, kwargs)
Definition: factory_test.py:23
def test_scalar_param()
Definition: factory_test.py:28
def test_callable()
def test_bound_params()
def process_config(config_def)
Definition: __init__.py:4
def test_nested()
def test_array_param()
Definition: factory_test.py:72

Copyright © 2017, California Institute of Technology.
ALL RIGHTS RESERVED.
U.S. Government Sponsorship acknowledged.
Generated Fri Aug 24 2018 15:44:10