Belle II Software development
calculation_list.py
1
8try:
9 from funcsigs import signature
10except ImportError:
11 from inspect import signature
12import itertools
13
14from hep_ipython_tools import calculation_queue
15
16
17def create_all_calculations(parameter_creator_function, **calculation_lists):
18 """
19 Create all calculations.
20 """
21
22 parameter_names_in_list = calculation_lists.keys()
23 parameter_values_in_list = calculation_lists.values()
24
25 every_parameter_combination = create_every_parameter_combination(parameter_names_in_list,
26 parameter_values_in_list)
27
28 all_queues = [calculation_queue.CalculationQueue() for _ in every_parameter_combination]
29
30 def add_queue_if_needed(c_queue, param_combination):
31 param_combination_copy = param_combination.copy()
32 args = signature(parameter_creator_function).parameters
33 if "queue" in args:
34 param_combination_copy.update({"queue": c_queue})
35
36 return param_combination_copy
37
38 every_parameter_combination_with_queue = [add_queue_if_needed(c_queue, parameters) for
39 c_queue, parameters in zip(all_queues, every_parameter_combination)]
40
41 def call_creator(param_combination):
42 calculation_kwargs = parameter_creator_function(**param_combination)
43 return calculation_kwargs
44
45 all_calculation_kwargs = map(call_creator, every_parameter_combination_with_queue)
46 return all_calculation_kwargs, all_queues, every_parameter_combination
47
48
49def create_every_parameter_combination(parameter_names, parameter_values):
50 """
51 Combine all parameter values with all other parameter values and form dictionaries with the correct
52 names.
53
54 Parameters:
55 parameter_names: A list of names of the parameters.
56 parameter_values: A list of lists with parameter values. Each list stands
57 for one parameter (so the len of parameter_names must be the len of
58 parameter_value).
59
60 Returns:
61 A list of dictionaries, each a single combination of parameters.
62 """
63
64 assert len(parameter_names) == len(parameter_values)
65
66 every_parameter_combination = itertools.product(*parameter_values)
67 every_parameter_combination_with_names = [
68 {parameter_name: value for parameter_name, value in zip(
69 parameter_names, values)} for values in every_parameter_combination]
70 return every_parameter_combination_with_names