Biogeme: Python Library  2.5
bio_iterator.py
Go to the documentation of this file.
1 
5 
6 
7 listOfIterator = []
8 
9 ## @brief Generic class for an iterator.
10 # @details Iterators are designed to define loops on the entries of
11 # the data file. It is assumed that the data file is composed of rows,
12 # each containing the same number of numeric entries. The first row of
13 # the file contains the headers, labeling each of these entries. There
14 # are three types of iterators:
15 # - row iterators are designed to iterate on rows of the data file. This is the most common type of iterators.
16 # - meta iterators are designed to iterate on group of rows of the data file. They are typically used for panel data, where several rows correspond to the same individual.
17 # - draw iterators are designed to iterate on the data file when user
18 # defined draws are generated. Note that this is valid only since
19 # biogeme 2.4. Before, this iterator was used for the calculation of
20 # the integral itself, together with an Operator Sum.
21 #
22 # Hierarchical nesting of iterators is possible, so that iterators may have a parent and a child.
23 class iterator:
24  # @param iteratorName Name of the iterator.
25  # @param name Name of the set of data that is being iterated. It is either the name of a metaiterator, or the string __dataFile__, when the iterator spans the entire database.
26  # @param child Name of the child
27  # @param variable Variable that contains the ID of the elements being iterated.
28  def __init__(self,iteratorName,name,child,variable):
29  self.iteratorName = iteratorName
30  self.name = name
31  self.child = child
32  self.variable = variable
33  listOfIterator.append(self) ;
34 
35  def __str__(self):
36  return "Iterator " + str(self.name) + " iterates on " + str(self.variable)
37 
38 ## @brief Iterates on the data file for generate the user defined draws (since biogeme 2.4)
39 # @details It is typically used with the DefineDraws expression to generate user defined draws.
40 #Typical usage:
41 # @code
42 # drawIterator('drawIter')
43 # TRIANG = DefineDraws('TRIANG', 0.5 * (bioDraws('BUNIF1')+bioDraws('BUNIF2')),'drawIter')
44 # @endcode
46  ## @param iteratorName Name of the iterator.
47  # @param name Name of the set of data that is being iterated. It is typically ignored for draw iterators. Use the default value.
48  # @param child Draw iterators have no children. Use the default value.
49  # @param variable Variable that contains the ID of the individuals in the data file. A different draw is generated for each different value of this variable.
50  def __init__(self,iteratorName,name="__dataFile__",child="",variable="__rowId__"):
51  msg = 'Deprecated syntax. No need to define a draw iterator anymore. Just remove the statement, and use the operator MonteCarlo instead of Sum.'
52  raise SyntaxError(msg)
53 
54 ## @brief meta iterators are designed to iterate on group of rows of the data file.
55 # @details They are typically used for panel data, where several rows correspond to the same individual.
56 # In the example represented in the table below, the meta iterator will identify 4 groups of data: rows 1 to 4, rows 5 to 7, rows 8 to 9 and rows 10 to 11. Note that group 1 and group 4 share the same Id. But the iterator does not take this into account, as only changes of the value of the identifier characterize a change of group. If rows 10 and 11 indeed belong to group 1, the data file must be edited so that they appear directly after row 4.
57 # @code
58 #__rowId__ Id ObsId Variables
59 # 1 1 1 ...
60 # 2 1 2 ...
61 # 3 1 3 ...
62 # 4 1 4 ...
63 # 5 2 1 ...
64 # 6 2 2 ...
65 # 7 2 3 ...
66 # 8 3 1 ...
67 # 9 3 2 ...
68 # 10 1 5 ...
69 # 11 1 6 ...
70 #@endcode
71 # An example of iterator on this data is
72 # @code
73 #metaIterator('personIter','__dataFile__','panelObsIter','Id')
74 #rowIterator('panelObsIter','personIter')
75 # @endcode
77  ## @param iteratorName Name of the iterator.
78  # @param name Name of the set of data that is being iterated. It is either the name of a metaiterator, or the string __dataFile__, when the iterator spans the entire database.
79  # @param child Name of the child
80  # @param variable Variable that contains the ID of the elements being iterated.
81  def __init__(self,iteratorName,name,child,variable):
82  iterator.__init__(self,iteratorName,name,child,variable)
83  self.type = 'META'
84 ## @brief row iterators are designed to iterate on rows of the data file.
85 # @details Examples:
86 # - Iterator on the data file: @code rowIterator('obsIter') @endcode
87 # - Iterator on another iterator:
88 #@code
89 #metaIterator('personIter','__dataFile__','panelObsIter','Id')
90 #rowIterator('panelObsIter','personIter')
91 #@endcode
93  ## @param iteratorName Name of the iterator.
94  # @param name Name of the set of data that is being iterated. It is either the name of a metaiterator, or the string __dataFile__, when the iterator spans the entire database.
95  # @param child Row iterators have no children. Use the default value.
96  # @param variable Ignored by row iterators. Use the default value.
97  def __init__(self,iteratorName,name="__dataFile__",child="",variable="__rowId__"):
98  iterator.__init__(self,iteratorName,name,child,variable)
99  self.type = 'ROW'
def __init__(self, iteratorName, name="__dataFile__", child="", variable="__rowId__")
Definition: bio_iterator.py:50
Iterates on the data file for generate the user defined draws (since biogeme 2.4) ...
Definition: bio_iterator.py:45
def __init__(self, iteratorName, name, child, variable)
Definition: bio_iterator.py:81
row iterators are designed to iterate on rows of the data file.
Definition: bio_iterator.py:92
meta iterators are designed to iterate on group of rows of the data file.
Definition: bio_iterator.py:76
Generic class for an iterator.
Definition: bio_iterator.py:23
def __init__(self, iteratorName, name="__dataFile__", child="", variable="__rowId__")
Definition: bio_iterator.py:97
Copyright 2016 Michel Bierlaire