def test_create_all_calculations_none(self):
        def creator_function(x, y):
            if x > 1:
                return None
            return {"a": 2 * x, "b": y + 1}

        all_calculation_kwargs, all_queues, every_parameter_combination = create_all_calculations(
            creator_function, x=[1, 2], y=[3, 4]
        )
        all_calculation_kwargs = list(all_calculation_kwargs)
        all_queues = list(all_queues)
        every_parameter_combination = list(every_parameter_combination)

        self.assertEqual(len(all_calculation_kwargs), 4)
        self.assertEqual(len(all_queues), 4)
        self.assertEqual(len(every_parameter_combination), 4)

        self.assertIn({"a": 2, "b": 4}, all_calculation_kwargs)
        self.assertIn({"a": 2, "b": 5}, all_calculation_kwargs)
        self.assertNotIn({"a": 4, "b": 4}, all_calculation_kwargs)
        self.assertNotIn({"a": 4, "b": 5}, all_calculation_kwargs)

        for combination in [{"x": 1, "y": 3}, {"x": 1, "y": 4}, {"x": 2, "y": 3}, {"x": 2, "y": 4}]:
            index = every_parameter_combination.index(combination)
            x = combination["x"]
            y = combination["y"]

            if x == 1:
                self.assertEqual(all_calculation_kwargs[index], {"a": 2 * x, "b": y + 1})
            else:
                self.assertEqual(all_calculation_kwargs[index], None)
    def test_create_all_calculations_queue(self):
        def creator_function(x, y, queue):
            return {"a": 2 * x, "b": y + 1, "queue": queue}

        all_calculation_kwargs, all_queues, every_parameter_combination = create_all_calculations(
            creator_function, x=[1, 2], y=[3, 4]
        )

        all_calculation_kwargs = list(all_calculation_kwargs)
        all_queues = list(all_queues)
        every_parameter_combination = list(every_parameter_combination)

        self.assertEqual(len(all_calculation_kwargs), 4)
        self.assertEqual(len(all_queues), 4)
        self.assertEqual(len(every_parameter_combination), 4)

        self.assertIn({"x": 1, "y": 3}, every_parameter_combination)
        self.assertIn({"x": 1, "y": 4}, every_parameter_combination)
        self.assertIn({"x": 2, "y": 3}, every_parameter_combination)
        self.assertIn({"x": 2, "y": 4}, every_parameter_combination)

        for combination in [{"x": 1, "y": 3}, {"x": 1, "y": 4}, {"x": 2, "y": 3}, {"x": 2, "y": 4}]:
            index = every_parameter_combination.index(combination)
            x = combination["x"]
            y = combination["y"]
            self.assertEqual(all_calculation_kwargs[index], {"a": 2 * x, "b": y + 1, "queue": all_queues[index]})
Ejemplo n.º 3
0
    def process_parameter_space(self, kwargs_creator_function, **parameter_lists):
        """
        Create a list of calculations by combining all parameters with all parameters you provide and
        feeding the tuple into the parameter_creator_function.
        If the kwargs_creator_function has a parameter named queue, the function feeds the corresponding
        created queue into the parameter_creator_function.
        The parameter_creator_function must return a dictionary for every combination of parameters it gets,
        which will be used to construct a process out of it.
        See ipython_handler_basf2/ipython_handler for an example.

        Please note that a list of calculations acts the same as a single calculation you would get from
        the process function. You can handle 10 calculations the same way you would handle a single one.

        The kwargs_creator_function can transform the incoming parameters into different ones. To make this
        more clear, the resulting dictionary created by the kwargs_creator_function is called kwargs.
        These are the ones, that will be used to create a calculation process, so they must be compatible to the
        calculation you chose (namely compatible with the append function of the _calculation_type).

        Arguments
        ---------
        kwargs_creator_function: A function with as many input parameters as parameters you provide.
           If the function has an additional queue parameter it is fed with the corresponding queue for this calculation.
        parameter_lists: As many lists as you want. Every list is one parameter. If you do not want a
           specific parameter constellation to occur, you can return None in your parameter_creator_function for
           this combination.

        Usage
        -----
            def kwargs_creator_function(par_1, par_2, par_3, queue):
                kwargs = {... f(par_1) ... g(par_2) ... h(par_3)}
                queue.put(..., ...)
                return kwargs

            calculations = handler.process_parameter_space(kwargs_creator_function,
                                                           par_1=[1, 2, 3], par_2=["x", "y", "z"], par_3=[3, 4, 5])

        The calculations will be created with the kwargs arguments.
        """

        all_kwargs, all_queues, all_parameters = calculation_list.create_all_calculations(kwargs_creator_function,
                                                                                          **parameter_lists)

        calculations = self._calculation_type()

        for kwargs, q, parameters in zip(all_kwargs, all_queues, all_parameters):
            calculations.append(result_queue=q, log_file_name=self.next_log_file_name(),
                                parameters=parameters, **kwargs)

        return calculations
Ejemplo n.º 4
0
    def test_create_all_calculations_none(self):
        def creator_function(x, y):
            if x > 1:
                return None
            return {"a": 2 * x, "b": y + 1}

        all_calculation_kwargs, all_queues, every_parameter_combination = create_all_calculations(
            creator_function, x=[1, 2], y=[3, 4])
        all_calculation_kwargs = list(all_calculation_kwargs)
        all_queues = list(all_queues)
        every_parameter_combination = list(every_parameter_combination)

        self.assertEqual(len(all_calculation_kwargs), 4)
        self.assertEqual(len(all_queues), 4)
        self.assertEqual(len(every_parameter_combination), 4)

        self.assertIn({"a": 2, "b": 4}, all_calculation_kwargs)
        self.assertIn({"a": 2, "b": 5}, all_calculation_kwargs)
        self.assertNotIn({"a": 4, "b": 4}, all_calculation_kwargs)
        self.assertNotIn({"a": 4, "b": 5}, all_calculation_kwargs)

        for combination in [{
                "x": 1,
                "y": 3
        }, {
                "x": 1,
                "y": 4
        }, {
                "x": 2,
                "y": 3
        }, {
                "x": 2,
                "y": 4
        }]:
            index = every_parameter_combination.index(combination)
            x = combination["x"]
            y = combination["y"]

            if x == 1:
                self.assertEqual(all_calculation_kwargs[index], {
                    "a": 2 * x,
                    "b": y + 1
                })
            else:
                self.assertEqual(all_calculation_kwargs[index], None)
Ejemplo n.º 5
0
    def test_create_all_calculations_queue(self):
        def creator_function(x, y, queue):
            return {"a": 2 * x, "b": y + 1, "queue": queue}

        all_calculation_kwargs, all_queues, every_parameter_combination = create_all_calculations(
            creator_function, x=[1, 2], y=[3, 4])

        all_calculation_kwargs = list(all_calculation_kwargs)
        all_queues = list(all_queues)
        every_parameter_combination = list(every_parameter_combination)

        self.assertEqual(len(all_calculation_kwargs), 4)
        self.assertEqual(len(all_queues), 4)
        self.assertEqual(len(every_parameter_combination), 4)

        self.assertIn({"x": 1, "y": 3}, every_parameter_combination)
        self.assertIn({"x": 1, "y": 4}, every_parameter_combination)
        self.assertIn({"x": 2, "y": 3}, every_parameter_combination)
        self.assertIn({"x": 2, "y": 4}, every_parameter_combination)

        for combination in [{
                "x": 1,
                "y": 3
        }, {
                "x": 1,
                "y": 4
        }, {
                "x": 2,
                "y": 3
        }, {
                "x": 2,
                "y": 4
        }]:
            index = every_parameter_combination.index(combination)
            x = combination["x"]
            y = combination["y"]
            self.assertEqual(all_calculation_kwargs[index], {
                "a": 2 * x,
                "b": y + 1,
                "queue": all_queues[index]
            })