Skip to content

PdmContext

Notice for existing users: what changed in version 0.3.0

No public function or class was removed or renamed, and no parameter was removed or reordered. Existing code keeps working; the notes below list what you may notice.

Check when upgrading

  • Installation: gCastle is now an optional dependency. If you use the default causality function (calculate_with_pc), install it with pip install PdmContext[pc], which also installs torch (required by gCastle) ([movingpc] for the deprecated MovingPC).
  • ContextGenerator.contexts is now a read-only sequence (indexing, slicing, len, iteration) instead of a list, so append and item assignment are not available (assigning a whole list still works). A context changed in place after it was returned, and no longer referenced, is not kept with those changes.
  • NaN values are ignored by collect_data (as if the sample did not arrive). A NaN target sample returns None (no context). Previously a NaN in the window made characterization fail.
  • PdmContext.LightContextGeneration is deprecated (DeprecationWarning). Use ContextGenerator(..., characterize_it=False).
  • PdmContext.utils.MovingPC is deprecated (DeprecationWarning, experimental and not stable). Use calculate_with_pc, optionally with ContextGenerator(..., causality_every=k).

Results that change (bug fixes)

  • Returned contexts no longer change afterwards (they used to receive values of later samples).
  • Categorical sources: no future values in the series, a category sent before the context horizon is kept as active (values may be sent only when they change), no category is active before the first known value.
  • The context window contains exactly the samples of the horizon (it had one extra old sample).
  • Source names are matched exactly (e.g. data1 no longer includes data10; a source whose name contains the target name is no longer dropped).
  • Configuration series start from 0 in each window instead of wrapping around (bounded, without false drops).
  • Characterization: isolated events use the event rule, and the continuous rule uses the sum of deltas as documented.
  • Late data are placed in time order; the replace policy also works for the first buffered event.
  • simulate_stream no longer skips the first record.

Other fixes and additions

  • Memory is bounded for endless streams: stored contexts take about two windows of data per source (it was quadratic in the window), and the buffer is also trimmed when the target stops sending data.
  • Faster context generation with the same results: about 2.5x to 4x (larger windows gain more), with or without characterization, not counting the causality discovery itself.
  • New, opt-in: ContextGenerator(..., causality_every=k) runs the causality discovery every k contexts and reuses the last edges in between (default 1: unchanged).
  • New, opt-in: ContextGeneratorBatch.generate_contexts(timestamps, n_jobs=1) generates many contexts, in parallel processes when n_jobs > 1.
  • NumPy 2 and pandas 3 support, distances.PCA_pre caching, default DBscanContextStream distance.
  • The Pipelines classes accept the replace policy; empty_cause accepts timestamps.
  • SQLite field names are validated and InfluxDB query values escaped.
  • Test suite, including tests with the real PC (pip install PdmContext[test,pc], python -m pytest).

Context encapsulates all available multimodal information around a target signal (usually anomaly scores), towards better anomaly detection and predictive maintenance solutions.

A context represents the data observed in a time window (the context horizon), mapped from their multimodal form (sensor values, events, configuration changes, categorical states) to a multivariate time series (CD), and the relationships between them (CR), extracted with causal discovery.

Framework

Context generation is an online process: data are collected as they arrive, and each new value of the target series creates a new context.

Installation

pip install PdmContext

The causality discovery method is pluggable. Optional dependencies:

pip install PdmContext[pc]        # gCastle PC (and torch), used by the default causality function
pip install PdmContext[movingpc]  # deprecated MovingPC (gCastle + scipy)
pip install PdmContext[pca]       # PCA based context distance (scikit-learn)
pip install PdmContext[influxdb]  # InfluxDB storage

Quick start

import pandas as pd
from PdmContext.ContextGeneration import ContextGenerator
from PdmContext.utils.causal_discovery_functions import calculate_with_pc

con_gen = ContextGenerator(target="anomaly scores", context_horizon="8 hours", Causalityfunct=calculate_with_pc)

t = pd.Timestamp("2024-01-01 10:00")
con_gen.collect_data(timestamp=t, source="Machine_A", name="temperature", value=40.5)
con_gen.collect_data(timestamp=t, source="Machine_A", name="speed change", type="configuration")

# a sample of the target series creates a context
context = con_gen.collect_data(timestamp=t, source="Machine_A", name="anomaly scores", value=0.3)

context.CD   # {"anomaly scores": [...], "temperature@Machine_A": [...], "speed change@Machine_A": [...]}
context.CR   # {"edges": [...], "characterization": [...], "interpretation": [...]}

Continue with Concepts and Generating context.