Belle II Software development
IPythonHandler Class Reference
Inheritance diagram for IPythonHandler:
Basf2IPythonHandler

Public Member Functions

def __init__ (self)
 
def process (self, result_queue=None, **kwargs)
 
def process_parameter_space (self, kwargs_creator_function, **parameter_lists)
 
def next_log_file_name (self)
 

Static Public Member Functions

def create_queue ()
 

Public Attributes

 log_files
 A list of open log files.
 
 information
 A shortcut for returning information on the environment.
 

Protected Attributes

 _calculation_type
 The calculation type to use.
 

Detailed Description

Handler class to start processes in an IPython notebook in a convenient way.
From this whole framework you should not need to create any instances by yourself but rather use the
given ipython handler for this.

Create a handler object in the beginning of your NB and use the two methods `process()`
and `process_parameter_space()` to turn parameters or a parameter creator function into a Calculation.
Do not create calculations on you own::

    from tracking.validation.ipython_handler import handler

    calculation = handler.process(parameters)

Definition at line 14 of file ipython_handler.py.

Constructor & Destructor Documentation

◆ __init__()

def __init__ (   self)
Each created log file gets registered and deleted if there are more than 20 log files present
or if the get_log function of the process is called (the log is saved elsewhere).
As the log files are saved to /tmp you have probably not to care about deleting them.

Reimplemented in Basf2IPythonHandler.

Definition at line 31 of file ipython_handler.py.

31 def __init__(self):
32 """
33 Each created log file gets registered and deleted if there are more than 20 log files present
34 or if the get_log function of the process is called (the log is saved elsewhere).
35 As the log files are saved to /tmp you have probably not to care about deleting them.
36 """
37
38
39 self.log_files = []
40
41
42 self.information = information.EnvironmentInformation()
43
44
45 self._calculation_type = calculation.Calculation
46

Member Function Documentation

◆ create_queue()

def create_queue ( )
static
Create a Calculation queue. You need to do this if you want to pass it to your modules
and write to it while processing the events.

Definition at line 138 of file ipython_handler.py.

138 def create_queue():
139 """
140 Create a Calculation queue. You need to do this if you want to pass it to your modules
141 and write to it while processing the events.
142 """
143 return calculation_queue.CalculationQueue()

◆ next_log_file_name()

def next_log_file_name (   self)
Return the name of the next log file.
If there are more than 20 log files present,
start deleting the oldest ones.

Definition at line 117 of file ipython_handler.py.

117 def next_log_file_name(self):
118 """
119 Return the name of the next log file.
120 If there are more than 20 log files present,
121 start deleting the oldest ones.
122 """
123 next_log_file = tempfile.mkstemp()
124 self.log_files.append(next_log_file)
125 while len(self.log_files) > 20:
126 first_log_file = self.log_files.pop(0)
127 f = first_log_file[0]
128 log_file_name = first_log_file[1]
129
130 os.close(f)
131 try:
132 os.unlink(log_file_name)
133 except OSError:
134 pass
135 return next_log_file[1]
136

◆ process()

def process (   self,
  result_queue = None,
**  kwargs 
)
Turn a parameter set into a Calculation that you can start, stop or whatever you want.

Arguments:
    result_queue: The CalculationQueue you want to use. Without giving
        this as a parameter the function creates one for you. Create
        one on your own with the function create_queue.

Reimplemented in Basf2IPythonHandler.

Definition at line 47 of file ipython_handler.py.

47 def process(self, result_queue=None, **kwargs):
48 """
49 Turn a parameter set into a Calculation that you can start, stop or whatever you want.
50
51 Arguments:
52 result_queue: The CalculationQueue you want to use. Without giving
53 this as a parameter the function creates one for you. Create
54 one on your own with the function create_queue.
55 """
56
57 if result_queue is None:
58 result_queue = calculation_queue.CalculationQueue()
59
60 calculation = self._calculation_type()
61 calculation.append(result_queue=result_queue, log_file_name=self.next_log_file_name(), parameters=None, **kwargs)
62
63 return calculation
64

◆ process_parameter_space()

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.

Definition at line 65 of file ipython_handler.py.

65 def process_parameter_space(self, kwargs_creator_function, **parameter_lists):
66 """
67 Create a list of calculations by combining all parameters with all parameters you provide and
68 feeding the tuple into the parameter_creator_function.
69 If the kwargs_creator_function has a parameter named queue, the function feeds the corresponding
70 created queue into the parameter_creator_function.
71 The parameter_creator_function must return a dictionary for every combination of parameters it gets,
72 which will be used to construct a process out of it.
73 See ipython_handler_basf2/ipython_handler for an example.
74
75 Please note that a list of calculations acts the same as a single calculation you would get from
76 the process function. You can handle 10 calculations the same way you would handle a single one.
77
78 The kwargs_creator_function can transform the incoming parameters into different ones. To make this
79 more clear, the resulting dictionary created by the kwargs_creator_function is called kwargs.
80 These are the ones, that will be used to create a calculation process, so they must be compatible to the
81 calculation you chose (namely compatible with the append function of the _calculation_type).
82
83 Arguments:
84 kwargs_creator_function: A function with as many input parameters
85 as parameters you provide. If the function has an additional
86 queue parameter it is fed with the corresponding queue for this
87 calculation.
88 parameter_lists: As many lists as you want. Every list is one
89 parameter. If you do not want a specific parameter
90 constellation to occur, you can return None in your
91 parameter_creator_function for this combination.
92
93 Usage::
94
95 def kwargs_creator_function(par_1, par_2, par_3, queue):
96 kwargs = {... f(par_1) ... g(par_2) ... h(par_3)}
97 queue.put(..., ...)
98 return kwargs
99
100 calculations = handler.process_parameter_space(kwargs_creator_function,
101 par_1=[1, 2, 3], par_2=["x", "y", "z"], par_3=[3, 4, 5])
102
103 The calculations will be created with the kwargs arguments.
104 """
105
106 all_kwargs, all_queues, all_parameters = calculation_list.create_all_calculations(kwargs_creator_function,
107 **parameter_lists)
108
109 calculations = self._calculation_type()
110
111 for kwargs, q, parameters in zip(all_kwargs, all_queues, all_parameters):
112 calculations.append(result_queue=q, log_file_name=self.next_log_file_name(),
113 parameters=parameters, **kwargs)
114
115 return calculations
116

Member Data Documentation

◆ _calculation_type

_calculation_type
protected

The calculation type to use.

Definition at line 45 of file ipython_handler.py.

◆ information

information

A shortcut for returning information on the environment.

Definition at line 42 of file ipython_handler.py.

◆ log_files

log_files

A list of open log files.

Definition at line 39 of file ipython_handler.py.


The documentation for this class was generated from the following file: