Skip to content

API Reference

This page is auto-generated from Python docstrings.


cintel.drift_detector

drift_detector.py - Project script (example).

Author: Your Name Here Date: 2026-mm-dd

Reference and Current System Metrics Data

  • Data is taken from a system measured during two different periods.
  • The reference data represents earlier, expected system behavior.
  • The current data represents more recent system behavior.
  • Each row represents one observation of system metrics.

  • Each CSV file includes these columns:

  • requests: number of requests handled
  • errors: number of failed requests
  • total_latency_ms: total response time in milliseconds

Purpose

  • Read reference and current system metrics from CSV files.
  • Compare the two datasets using simple summary statistics.
  • Detect meaningful changes in average system behavior.
  • Provide a simple baseline comparison approach that supports drift detection.
  • Save the comparison summary as a CSV artifact.
  • Log the pipeline process to assist with debugging and transparency.

Questions to Consider

  • What does "normal" system behavior look like?
  • How can we compare current measurements to a historical baseline?
  • When does a difference become large enough to indicate meaningful change?

Paths (relative to repo root)

INPUT FILE: data/reference_metrics.csv
INPUT FILE: data/current_metrics.csv
OUTPUT FILE: artifacts/drift_summary.csv

Terminal command to run this file from the root project folder

uv run python -m cintel.drift_detector

DESIGN:

Use this file to first learn and then apply your skills.

Reusable functions that do the work are kept in a utils file. We import functions and pass in what they need through their parentheses. Shared utility functions can be imported into a Python module or a reactive marimo notebook app.

Reusable visualization functions may come from sources such as eda-vizkit.

SKILLS:

  • Reading CSV files into Polars DataFrames.
  • Defining and using constants for thresholds.
  • Logging for debugging and transparency.
  • Comparing reference and current datasets to detect drift.

main

main() -> None

Run the pipeline.

log_header() logs a standard run header. log_path() logs repo-relative paths (privacy-safe).

Source code in src/cintel/drift_detector.py
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
def main() -> None:
    """Run the pipeline.

    log_header() logs a standard run header.
    log_path() logs repo-relative paths (privacy-safe).
    """
    log_header(LOG, "CINTEL")

    LOG.info("========================")
    LOG.info("START main()")
    LOG.info("========================")

    log_path(LOG, "ROOT_DIR", ROOT_DIR)
    log_path(LOG, "REFERENCE_FILE", REFERENCE_FILE)
    log_path(LOG, "CURRENT_FILE", CURRENT_FILE)
    log_path(LOG, "OUTPUT_FILE", OUTPUT_FILE)

    # Ensure the artifacts folder exists.
    ARTIFACTS_DIR.mkdir(parents=True, exist_ok=True)
    log_path(LOG, "ARTIFACTS_DIR", ARTIFACTS_DIR)

    # ----------------------------------------------------
    # STEP 1: READ REFERENCE AND CURRENT CSV INTO DATAFRAMES
    # ----------------------------------------------------
    # load_system_metrics() wraps polars' read_csv() — see utils_system.py.
    # Both files share the same schema (requests, errors, total_latency_ms),
    # so the same loader works for both periods.
    reference_df: pl.DataFrame = load_system_metrics(REFERENCE_FILE)
    current_df: pl.DataFrame = load_system_metrics(CURRENT_FILE)

    LOG.info(f"Loaded {reference_df.height} reference records")
    LOG.info(f"Loaded {current_df.height} current records")

    # ----------------------------------------------------
    # STEP 2: SUMMARIZE EACH PERIOD, COMPARE, AND FLAG DRIFT
    # ----------------------------------------------------
    # compute_drift_summary() does the full comparison in one call:
    # averages each period, combines them into one row, computes the
    # difference of means, and flags drift where |difference| exceeds
    # the given threshold — see utils_system.py for exactly how.
    LOG.info(f"REQUESTS_DRIFT_THRESHOLD: {REQUESTS_DRIFT_THRESHOLD}")
    LOG.info(f"ERRORS_DRIFT_THRESHOLD: {ERRORS_DRIFT_THRESHOLD}")
    LOG.info(f"LATENCY_DRIFT_THRESHOLD: {LATENCY_DRIFT_THRESHOLD}")

    drift_df: pl.DataFrame = compute_drift_summary(
        reference_df,
        current_df,
        requests_threshold=REQUESTS_DRIFT_THRESHOLD,
        errors_threshold=ERRORS_DRIFT_THRESHOLD,
        latency_threshold=LATENCY_DRIFT_THRESHOLD,
    )

    LOG.info("Calculated summary differences and drift flags")

    # ----------------------------------------------------
    # STEP 3: STATISTICAL COMPARISON OF ERROR RATES
    # ----------------------------------------------------
    stats_df: pl.DataFrame = compare_error_rates(
        reference_df,
        current_df,
    )

    stats_df.write_csv(OUTPUT_STATS_FILE)

    LOG.info("Statistical comparison of error rates:")
    LOG.info(stats_df)
    log_path(LOG, "STATS_FILE", OUTPUT_STATS_FILE)

    # ----------------------------------------------------
    # STEP 4: SAVE THE FLAT DRIFT SUMMARY AS AN ARTIFACT
    # ----------------------------------------------------
    drift_df.write_csv(OUTPUT_FILE)
    LOG.info(f"Wrote drift summary file: {OUTPUT_FILE}")

    # Take a look at the summary dataframe.
    # Lots of columns with one row of values.
    LOG.info("Drift summary dataframe:")
    LOG.info(drift_df)
    LOG.info("Let's make that a bit nicer to read...")
    LOG.info("All remaining steps are about creating a nicer display.")

    # ----------------------------------------------------
    # OPTIONAL STEP 4.1: LOG THE SUMMARY ONE FIELD PER LINE
    # ----------------------------------------------------
    # drift_df has one row with many columns.
    # Convert that one row to a dictionary so we can log:
    # column_name: value

    # The Polars to_dicts() function returns a list of dictionaries, one per row.
    # the [0] gets the first (and only) dictionary from the list.
    # We often count starting at zero
    # because the first row is 0 away from the start of the dataframe.
    drift_summary_dict = drift_df.to_dicts()[0]

    LOG.info("========================")
    LOG.info("Drift Detection Process: ")
    LOG.info("========================")
    LOG.info("1. Summarize each period with means.")
    LOG.info("2. Compute difference of means.")
    LOG.info("3. Flag drift if absolute difference of means exceeds a threshold.")
    LOG.info("========================")

    LOG.info("Drift summary (one field per line):")
    for field_name, field_value in drift_summary_dict.items():
        LOG.info(f"{field_name}: {field_value}")

    # ----------------------------------------------------
    # OPTIONAL STEP 4.2: CREATE A LONG-FORM ARTIFACT FOR DISPLAY
    # ----------------------------------------------------
    # Create a second artifact with one field per row.
    # This is easier to read than a single very wide row.
    # We create a new dataframe with two columns:
    # - field_name: the name of the summary field
    # - field_value: the value of the summary field (converted to string for display)

    drift_summary_long_df = pl.DataFrame(
        {
            "field_name": list(drift_summary_dict.keys()),
            "field_value": [str(value) for value in drift_summary_dict.values()],
        }
    )
    # ----------------------------------------------------
    # OPTIONAL STEP 4.3: SAVE THE LONG-FORM DRIFT SUMMARY AS AN ARTIFACT
    # ----------------------------------------------------
    drift_summary_long_df.write_csv(SUMMARY_LONG_FILE)
    LOG.info(f"Wrote long summary file: {SUMMARY_LONG_FILE}")

    LOG.info("========================")
    LOG.info("Pipeline executed successfully!")
    LOG.info("========================")
    LOG.info("END main()")

◄ Back to 🏠 Home