ContextGeneration¶
PdmContext.ContextGeneration.ContextGenerator ¶
ContextGenerator(target, context_horizon='8 hours', Causalityfunct=calculate_with_pc, mapping_functions=None, debug=False, characterize_it=True, causality_every=1)
This Class handle the Context Generation. It keeps an internal buffer to build the context for a target series based on the provided context_horizon. All data are passed though the collect_data method, which return a corresponding Context when target data are passed.
Parameters:
target: The name of the target source, which will be used as the baseline in order to map different samples rate to that of the target sample rate.
context_horizon: The time period to look back for context data, the form of that parameter is "8 hours"
Causalityfunct: the causality discovery method to use to produce causal relationships between context data, This must be a function f(names, data, timestamps), where names is the list with the names of the series, data a 2D numpy array (one column per series) and timestamps the timestamps of the rows, which returns a list of (cause, effect) edges.
mapping_functions: Dictionary used to associate each type with a mapping function.
User can use this dictionary to define his own mapping function and types of sources
The default mappers keep state per source name, so each ContextGenerator needs its own mapper objects (do not share the same mapping_functions dictionary between generators).
Default value None: use default mappers for types: isolated,configuration, categorical and univariate
Default Sources Types Supported (in case of mapping_functions is None):
1) Continuous type (those that have some kind of arithmetic value)
2) Discrete events (without value) , where one of the type isolated or configuration or categorical must be assigned
A guide on how to specify the type is, that events which assumed to have impact only on their occurrence, are called
isolated, while others that are related to some kind of configuration with more permanent impact, are called configuration.
Categorical values can be defined as categorical type
Essentially the type of the events define the way that will be transformed to real values time-series.
debug: If it runs on debug mode
characterize_it: when False, no characterization and interpretation of the edges are calculated (faster).
causality_every: run the causality discovery every causality_every contexts (default 1: for every context). The contexts in between reuse the edges of the last causality discovery (only edges between series of the context), which is faster when the causality discovery is expensive.
contexts
property
writable
¶
contexts
The contexts of the last horizon (oldest first), as a read only sequence of Context objects.
The contexts are kept in a memory bounded form and rebuilt when accessed (a returned context which is still referenced is returned itself).
collect_data ¶
collect_data(timestamp, source, name, value=None, type='Univariate', replace=None)
This method is used when data are passed iteratively, and stored in buffer When data of target source arrive, a corresponding context is produced. Sources can be of different sample rate (all sources are mapped to the targets sample rate when context is produced)
Default Sources Supported (in case of mapping_functions is None) :
1) Continuous type (those that have some kind of arithmetic value)
2) Discrete events (without value) , where one of the type isolated or configuration or categorical must be assigned A guide on how to specify the type is, that events which assumed to have impact only on their occurrence, are called isolated, while others that are related to some kind of configuration with more permanent impact, are called configuration. Categorical values can be defined as categorical type Essentially the type of the events define the way that will be transformed to real values time-series.
Parameters:
timestamp: The timestamp of the arrived value
source: The source of the arrived value
name: The name (or identifier) of the arrived value
value: The value (float), in case this is None the arrived data is considered as event
type: the type of the data associated with the mapping function
replace: a list of tuples, which define a replacement policy. If a tuple (e,b) exist in replace list, then when a b event is inserted, it will replace an e event with the same timestamp (if exists).
NaN values are ignored (as if the sample did not arrive), also for the target (no context is created).
return: structure.Context object when the data name match to the target name or None.
add_to_buffer ¶
add_to_buffer(e, replace=None)
Adds an Event point to the buffer (keeping the buffer time ordered)
trim_buffer ¶
trim_buffer()
Keep only the events of the last horizon in the buffer, plus the last event before the horizon of sources whose mapper needs the value active at the start of the window (e.g. categorical values sent only when they change), one event per source.
generate_context ¶
generate_context(e, buffer)
Generate context (and interpretation only when mapping function is the default).
Parameters:
e: Eventpoint related to the last target's data.
buffer: A list with all Eventpoints in the time horizon
create_interpretation ¶
create_interpretation(contextcurrent, target_series_name)
This method collect all the contexts in the horizon and calls the interpretation method.
Parameters:
contextcurrent: The Context for which the interpretation is calculated
target_series_name: Then name of the target variable
interpret ¶
interpret(context, causeswindow, target, type_of_series)
This method enhance the CR part of the Context with interpretation relating to the target variable. The interpretation are based on the edges extracted from the Casualty discovery. For each edge of type (seriesA -> target) in the edges we test if the seriesA interprets the target using the following rules:
if seriesA is isolated: We check if its last occurrence is the current timestamp or the previous one.
if seriesA is configuration: We check if the edges (seriesA -> target) appears in at least 80% contexts in the horizon (i.e. in causeswindow list).
if seriesA is continuous: Then that means the target is related with seriesA and we add it to the interpretation.
All the interpretations are tagged with a timestamp which refers to the first time of appearense of consecutive interpretations. Then the interpretation are sorted using this timestamp. This is done to provide a hierarchy to the interpretation since it may be the case, that when seriesA cause target , and SeriesB cause target, the oldest one is stronger since the SeriesB may be effect of seriesA.
Parameters:
context: Context Object to be interpreted
causeswindow: list with the last horizon contexts
target: the name of the target variable to interpret
type_of_series: dictionary which define the type of each series (isolated,configuration or continuous)
create_context ¶
create_context(current, buffer)
Transform the data collected to the buffer in to suitable form and generates the CD part of the context along with the edges and characterizations:
Steps:
Create CD: Parallel continuous representations of all different sources in the buffer. This step involves, matching the different sample rates of different sources to that of the target. Transform the Event sources to continuous representation.
Calculate Causality edges: Perform Causal Discovery using the causality function, to create edges (part of CR of the Context)
Tag each edge with characterization: for each (a,b) in the edges, a characterization of (unknown, decrease, increase), based on the type of the a.
Parameters:
current: The current Event of target's data which triger the context creation
buffer: ordered list with Eventpoint of all sources.
return: Context object.
get_characterization ¶
get_characterization(context, target_series_name=None)
This method calculate the characterizations of (a,b) edges to characterize the influence of a in b by one of the three characterizations: unknown, decrease, increase.
The characterization is calculated differently for event and continuous data. Parameters:
context: dictionary with a kye 'edges' for which a parallel list of characterizations will be created.
target_series_name: name of the target series in the context (default: the target of the generator).
characterize_event_edge ¶
characterize_event_edge(context, edge)
This method characterizes the edge (a,b) when a is isolated or configuration event. To do this, it detects the last occurrence of a and split the data of series a and series b based on that. Then checks if the median of b after the occurrence is at larger than the median b before plus two times the standard deviation of b series before the occurrence. If that is true then is characterized as increase. Else it is checked for the opposite, (if it is smaller for at least 2 times the standard deviation of b) and characterized as decrease. Otherwise, is characterized as unknown.
Parameters:
context: that the edge to be characterized belongs.
edge: A tuple (a,b) where a is isolated or configuration event.
return: A characterization for the edge (unknown, decrease or increase)
characterize_event_continuous ¶
characterize_event_continuous(context, edge)
This method characterizes the edge (a,b) when a is continuous. To do this, the delta between the timestamps of the series b is calculated (i.e. the difference between current and next timestamps). If the summ of the deltas between b series data, is greater than 2 times the standard deviation of the b, then the increase characterization is returned. If the sum is lower than 2 times the standard deviation the decreased characterization is returned. Otherwise, the unknown characterization is returned.
Parameters:
context: that the edge to be characterized belongs.
edge: A tuple (a,b) where a is isolated or configuration event.
return: A characterization for the edge (unknown, decrease or increase)
calculate_edges ¶
calculate_edges(alldata, timestamp, timestamps)
Formulate the data in appropriate form to call self.calculate_causality which return the edges for the context.
Parameters:
alldata: a 2D numpy array with all series data (equivalent to the CD of the Context)
timestamp: timestamp of the context from which the edges are calculated.
return: a dictionary with 'edges' key (containing the calculated edges after Causality discovery).
create_continuous_representation ¶
create_continuous_representation(target_series_name, target_series, df, type_of_series, allcodes, mapping_functions, target_key=None, occurrences_of=None)
This method handles the creation of continuous representation for all type of sources observed in context, and is the first part for creating the CD of the context.
Based on the type of each source, call the appropriate method to create the continuous representaiton.
Parameters:
target_series_name: The name of the target series.
target_series: Used to align sample rate.
type_of_series: A dictionary to define the type of the different sources (the type can be Isolated, Configuration, Categorical and Univariate).
allcodes: Contain all the names for the sources we want to build the context.
mapping_functions: Dictionary used to associate each type with a mapping function.
target_key: The name@source of the target series (when None, any source with target_series_name is the target).
occurrences_of: dictionary name -> list of (value, time) of each source (when None, collected from df).
return: The CD part of the context.
PdmContext.ContextGeneration.ContextGeneratorBatch ¶
ContextGeneratorBatch(df_data, target, type_of_series, context_horizon='8 hours', Causalityfunct=calculate_with_pc, debug=False, file_path=None)
This version doesn't support interpretation and is created for faster experiments using Context.
df_data The data to consider in context in form of Data Frame.
** target** The name of the target source, which will be used as the baseline in order to map different samples
type_of_series: dictionary which define the type of each series (isolated,configuration or continuous) rate to that of the target sample rate.
context_horizon The time period to look back for context data, the form of that parameter is "8 hours"
Causalityfunct the causality discovery method to use to produce causal relationships between context data, This must be a function with parameters two equal size lists, one with names and the other with data (a list of list or 2D numpy array).
debug If it runs on debug mode
file_path Store the results of context in pickle file, considered only when it is not None.
generate_contexts ¶
generate_contexts(datetime_indexes, n_jobs=1)
Generate the contexts of several timestamps (same result as calling generate_context for each one).
datetime_indexes: the timestamps (of df_data index) for which contexts are generated.
n_jobs: number of processes (default 1: sequential). With n_jobs > 1 the contexts are generated in
parallel processes, so the causality function must be picklable (e.g. a module level function, not a lambda),
and on Windows/macOS the calling script needs an if __name__ == "__main__": guard.
return: list with the contexts, in the order of datetime_indexes.