Giter Club home page Giter Club logo

learning-machines-drift's Introduction

Learning Machines

A Python package for monitoring dataset drift in production ML pipelines.

Built to run in any environment without uploading your data to external services.

Background

More background on learning machines.

Getting started

Requirements

  • Python 3.9

Install

To install the latest version, run the following:

pip install -U learning-machines-drift

Example usage

A simple example along with the below:

from learning_machines_drift import Dataset, Display, FileBackend, Monitor, Registry
from learning_machines_drift.datasets import example_dataset

# Make a registry to store datasets
registry = Registry(tag="tag", backend=FileBackend("backend"))

# Save example reference dataset of 100 samples
registry.save_reference_dataset(Dataset(*example_dataset(100, seed=0)))

# Log example dataset with 80 samples
with registry:
    registry.log_dataset(Dataset(*example_dataset(80, seed=1)))

# Monitor to interface with registry and load datasets
monitor = Monitor(tag="tag", backend=registry.backend).load_data()

# Measure drift and display results as a table
Display().table(monitor.metrics.scipy_kolmogorov_smirnov())

Development

Install

For a local copy:

git clone [email protected]:alan-turing-institute/learning-machines-drift
cd learning-machines-drift

To install:

poetry install

To install with dev and docs dependencies:

poetry install --with dev,docs

Tests

Run:

poetry run pytest

pre-commit checks

Run:

poetry run pre-commit run --all-files

To run checks before every commit, install as a pre-commit hook:

poetry run pre-commit install

Other tools

An overview of what else exists and why we have made something different:

What LM does differently

  • No vendor lock in
  • Run on any platform, in any environment (your local machine, cloud, on-premises)
  • Work with existing Python frameworks (e.g. scikit-learn)
  • Open source

learning-machines-drift's People

Contributors

sgreenbury avatar myyong avatar jsteyn avatar oscartgiles avatar

Stargazers

 avatar Felixity avatar  avatar

Watchers

Michael Burkhart avatar  avatar Tomas Lazauskas avatar Mahed Abroshan avatar  avatar

learning-machines-drift's Issues

Update SDMetrics

Current dependencies are here.

I am creating this issue because SDMetrics has been updated significantly from 0.6 to 0.8.

The plan is:

  • Create new branch my/update-sdv
  • Delete poetry.lock,
  • Update pyproject.toml sdmetrics = "^0.6.0" to sdmetrics = "0.8.0"
  • Run poetry update

I see that the poetry.lock file has been updated and sdmetrics is now at version 0.8.

  • Run pytests.

Tests run fine. Simple example works.

Add a class which filters logged data

We have the following classes:

  • Monitor: Logs data and saves to datastore
  • Registry: Loads all the logged data and then passes to HypothesisTest which applies drift metrics.

It would be good to have a new class that sits in the middle of Registry and HypothesisTest which allows you to query and filter data in the Registry:

Examples might include:

  • Filter by the date the data was logged
  • Filter by the feature value

You may also want to do split-apply-combine operations, for example:

  • Split the data into weeks and compare each week to the reference dataset.

Publish the package on PyPI

  • Identify requirements for publishing the package

Initial thoughts:

  • Complete missing docstrings ("TODO PEP257"). Do we have a docstring style we're using?
  • Formal documentation for API (e.g. sphinx)
  • New repo to remove data from previous commits
  • Distinct examples for demostrating the package

Measuring drift in temporal/longitudinal datasets

SD metrics implements a GMM for probabilistic modelling of continuous static data. Held-out data (registered/synthetic/etc) can then be scored with a likelihood given the fit on the reference dataset.

A similar approach could be considered for temporal data with probabilistic models such as HMM.

SD metrics also implements so time series metrics.

Tool for generating tabular/graphical drift outputs from passed data

Aim: to convert the prototyping work from cases into a flexible drift report generating script.

Make a script that parses an input config and csv and outputs html/pdf tables and graphs reporting drift trends from the data.

  • Input interface (csv, flags) and config file (e.g. toml)
  • Code for the iterating through the data with some trend schema (e.g. year by year) to generate output measures
  • Identify what the report outputs should exactly look like (e.g. tables of test statistic values, graphs of p-values)

Packages to consider using: unitreport

Fix python dependency to include `python=3.9`

Currently dependency specified as:

[tool.poetry.dependencies]
python = ">3.9,<3.10"

Installation is currently failing and should be updated to include python=3.9:

[tool.poetry.dependencies]
python = ">=3.9,<3.10"

Sketch out API for drift package

API design

This is a very rough first sketch of what the LM drift detection might look like.

The key feature of the drift detection library is it will compare datasets seen in production to a reference dataset (normally the dataset used to train the production model).

There are a few things to keep in mind:

  • We need to support logging a single row of data at a time. In the Alzheimer's dataset, only one patient will be logged at once.
  • We will need to persist data to storage. In the demo this will be the filesystem, but could support databases. As such we need some level of abstraction over backends.

An example of using the package

A simple API design based on whylogs might look like this. This first example assumes we have a trained classifier we are using in production.

with DriftDetector(tag="test_tag", expect_features = True, expect_labels = True, expect_latent = False) as detector:

    # Normally X and Y would come from a model fit. Here we use a sample dataset
    X, Y = datasets.logistic_model()

    detector.log_features(X)
    detector.log_labels(Y)

Let's assume this stores the features and labels in some persistent storage (e.g. disk, database). The tag argument to DriftDetector is a unique tag for the model being monitored. The arguments expect_{features | labels | latent} are optional, if set to True an exception will be raised if the features | labels | latent hasn't been logged by the time the context manager exits.

We must register a reference dataset to compare drift against. This is normally the dataset used to train the model. This can be registered before model inference, in which case we could extend our output above to provide information on drift at model inference time:

register_reference(tag = "prod_model", features = X_ref, labels = Y_ref)

with DriftDetector(tag="prod_model", expect_features = True, expect_labels = True, expect_latent = False) as detector:

    # Normally X and Y would come from a model fit. Here we use a sample dataset
    X, Y = datasets.logistic_model()

    detector.log_features(X)
    detector.log_labels(Y)
   
    # Write drift detection metrics to stdout
    detector.summary()

or we could register it afterwards

register_reference(tag = "prod_model", features = X_ref, labels = Y_ref)

with DriftDetector(tag="prod_model",) as detector:
     # Write drift detection metrics to stdout
    detector.summary()

Summary

We can load the data associated with a tag, ensuring the reference dataset is loaded.

with DriftDetector(tag="prod_model", expect_reference = True) as detector:
     # Write drift detection metrics to stdout
    detector.summary()

Rather than just writing to stdout we probably want to return in a data structure (e.g. Dictionary or data class).

Make initial visualisation class

Make a class to plot the scores from a returned hypothesis tests dict.

For example, function could be like:

def visualise(dict_of_variables: dict[str, dict[str, float]]) -> plt.axes:
    ...

Plot should have x-axis as keys of dict and y-axes as score values in the dict from key.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.