1+ # -*- coding: utf-8 -*-
2+ """The actions module. This module includes
3+ various actions to be applied by the implemented RL agents
4+
5+ """
6+
17import abc
28import enum
39from typing import List , TypeVar , Any
4-
510import numpy as np
611
7- from src .utils .hierarchy_base import HierarchyBase
812from src .utils .mixins import WithHierarchyTable
913
1014Hierarchy = TypeVar ("Hierarchy" )
1115
1216
13- def move_next (iterators : List ) -> None :
14- """
15- Loop over the iterators and move them
16- to the next item
17- :param iterators: The list of iterators to propagate
18- :return: None
19- """
20- for item in iterators :
21- next (item )
22-
23-
2417class ActionType (enum .IntEnum ):
25- """
26- Defines the status of an Action
18+ """Defines the status of an Action
2719 """
2820
2921 INVALID_TYPE = - 1
@@ -53,39 +45,71 @@ def restore(self) -> bool:
5345
5446
5547class ActionBase (metaclass = abc .ABCMeta ):
56- """
57- Base class for actions
48+ """Base class for actions
5849 """
5950
6051 def __init__ (self , column_name : str , action_type : ActionType ) -> None :
52+ """Constructor
53+
54+ Parameters
55+ ----------
56+ column_name : The name of the column this is acting on
57+ action_type : The type of the action
58+
59+ """
6160 self .column_name = column_name
6261 self .action_type = action_type
6362 self .idx = None
6463 self .key = (self .column_name , self .action_type )
6564
6665 @abc .abstractmethod
6766 def act (self , ** ops ) -> Any :
68- """
69- Perform an action
70- :return:
67+ """Perform the action
68+
69+ Parameters
70+ ----------
71+ ops : The data to distort
72+
73+ Returns
74+ -------
75+ Typically the action returns the distorted subset of the data
76+
7177 """
7278
7379
7480class ActionIdentity (ActionBase ):
75- """
76- Implements the identity action
81+ """Implements the identity action. Use this action
82+ to signal that no distortion should be applied.
83+
7784 """
7885
7986 def __init__ (self , column_name : str ) -> None :
87+ """Constructor
88+
89+ Parameters
90+ ----------
91+ column_name : The name of the column this is acting on
92+
93+ """
8094 super (ActionIdentity , self ).__init__ (column_name = column_name , action_type = ActionType .IDENTITY )
8195 self .called = False
8296
8397 def act (self , ** ops ) -> Any :
98+ """Perform the action
99+
100+ Parameters
101+ ----------
102+ ops : The data to distort
103+
104+ Returns
105+ -------
106+
107+ The distorted column
108+
84109 """
85- Perform the action
86- :return:
87- """
110+
88111 self .called = True
112+ return ops ['data' ]
89113
90114
91115class ActionRestore (ActionBase , WithHierarchyTable ):
@@ -160,20 +184,35 @@ def act(self, **ops) -> None:
160184
161185
162186class ActionStringGeneralize (ActionBase , WithHierarchyTable ):
163- """
164- Implements the generalization action. The generalization_table
187+ """Implements the generalization action. The generalization_table
165188 must implement the __getitem__ function
189+
166190 """
167191
168- def __init__ (self , column_name : str , generalization_table : Hierarchy ):
169- super ( ActionStringGeneralize , self ). __init__ ( column_name = column_name , action_type = ActionType . GENERALIZE )
192+ def __init__ (self , column_name : str , generalization_table : Hierarchy ) -> None :
193+ """Constructor
170194
171- self .table = generalization_table
195+ Parameters
196+ ----------
197+ column_name : The column name this action is acting on
198+ generalization_table : The hierarchy for the generalization
172199
173- def act (self , ** ops ):
174200 """
175- Perform an action
176- :return:
201+ super (ActionStringGeneralize , self ).__init__ (column_name = column_name , action_type = ActionType .GENERALIZE )
202+ self .table = generalization_table
203+
204+ def act (self , ** ops ) -> Any :
205+ """Performs the action
206+
207+ Parameters
208+ ----------
209+ ops : The data to distort
210+
211+ Returns
212+ -------
213+
214+ The distorted data
215+
177216 """
178217
179218 # get the values of the column
@@ -191,12 +230,33 @@ def act(self, **ops):
191230 return ops ['data' ]
192231
193232 def add (self , key : Any , value : Any ) -> None :
233+ """Add a new item in the underlying hierarchy
234+
235+ Parameters
236+ ----------
237+ key : The key to use for the new item
238+ value : The value of the new item
239+
240+ Returns
241+ -------
242+
243+ None
244+ """
194245 self .table .add (key , value )
195246
196247
197248class ActionNumericBinGeneralize (ActionBase , WithHierarchyTable ):
249+ """Generalization Action for numeric columns using bins
250+ """
198251
199252 def __init__ (self , column_name : str , generalization_table : Hierarchy ):
253+ """
254+ Constructor
255+ Parameters
256+ ----------
257+ column_name : The name of the column this is acting on
258+ generalization_table : The bins to use
259+ """
200260 super (ActionNumericBinGeneralize , self ).__init__ (column_name = column_name , action_type = ActionType .GENERALIZE )
201261
202262 self .table = generalization_table
@@ -207,10 +267,16 @@ def __init__(self, column_name: str, generalization_table: Hierarchy):
207267 self .bins .append ((start , self .table [i ]))
208268 start = self .table [i ]
209269
210- def act (self , ** ops ):
270+ def act (self , ** ops ) -> Any :
211271 """
212- Perform an action
213- :return:
272+ Perform the action
273+ Parameters
274+ ----------
275+ ops : The data to distort
276+
277+ Returns
278+ -------
279+ Typically the action returns the distorted subset of the data
214280 """
215281
216282 # get the values of the column
0 commit comments