def test_cartesian_apply(self): from pyhrf.tools import cartesian_apply def foo(a, b, c=1, d=2): return a + b + c + d varying_args = {'a': range(2), 'b' : range(2), 'c' : range(2) } fixed_args = {'d' : 10} result = cartesian_apply(varying_args, foo, fixed_args=fixed_args) self.assertEqual(result, [10, 11, 11, 12, 11, 12, 12, 13])
def test_cartesian_apply_parallel(self): from pyhrf.tools import cartesian_apply from pyhrf.tools.backports import OrderedDict # OrderDict to keep track of parameter orders in the result # arg values must be hashable varying_args = OrderedDict([('a', range(2)), ('b', range(2)), ('c', range(2))]) fixed_args = {'d': 10} result_tree = cartesian_apply(varying_args, foo, fixed_args=fixed_args, nb_parallel_procs=4) self.assertEqual(result_tree, {0: {0: {0: 10, 1: 11}, 1: {0: 11, 1: 12}}, 1: {0: {0: 11, 1: 12}, 1: {0: 12, 1: 13}}})
def test_cartesian_eval(self): """ Test the multiple evaluations of a function that returns a xndarray, over the cartesian products of given arguments. """ def foo(a, b, size, aname): return xndarray(np.ones(size) * a + b, axes_names=[aname]) from pyhrf.tools import cartesian_apply from pyhrf.tools.backports import OrderedDict varying_args = OrderedDict([('a', [1, 2]), ('b', [.5, 1.5])]) fixed_args = {'size': 2, 'aname': 'my_axis'} res = tree_to_xndarray(cartesian_apply(varying_args, foo, fixed_args)) self.assertEqual(res.data.shape, (2, 2, 2)) npt.assert_array_equal( res.data, np.array([[[1.5, 1.5], [2.5, 2.5]], [[2.5, 2.5], [3.5, 3.5]]]))
def test_cartesian_eval(self): """ Test the multiple evaluations of a function that returns a xndarray, over the cartesian products of given arguments. """ def foo(a, b, size, aname): return xndarray(np.ones(size) * a + b, axes_names=[aname]) from pyhrf.tools import cartesian_apply from pyhrf.tools.backports import OrderedDict varying_args = OrderedDict([('a', [1, 2]), ('b', [.5, 1.5])]) fixed_args = {'size': 2, 'aname': 'my_axis'} res = tree_to_xndarray(cartesian_apply(varying_args, foo, fixed_args)) self.assertEqual(res.data.shape, (2, 2, 2)) npt.assert_array_equal(res.data, np.array([[[1.5, 1.5], [2.5, 2.5]], [[2.5, 2.5], [3.5, 3.5]]]))