django-river-ml package

Submodules

django_river_ml.announce module

class django_river_ml.announce.MessageAnnouncer[source]

Bases: object

announce(msg)[source]
listen()[source]

django_river_ml.apps module

class django_river_ml.apps.DjangoRiverMLConfig(app_name, app_module)[source]

Bases: AppConfig

name = 'django_river_ml'
ready()[source]

Override this method in subclasses to run code when Django starts.

verbose_name = 'River Online Machine Learning for Django'

django_river_ml.auth module

django_river_ml.auth.generate_jwt(username)[source]

Given a username generate a jwt token to return to the user with a default expiration of 10 minutes.

username (str) : the user’s username to add under “sub”

django_river_ml.auth.get_challenge(request)[source]

Given an unauthenticated request, return a challenge in the Www-Authenticate header

request (requests.Request): the Request object to inspect

django_river_ml.auth.get_token(request)[source]

The same as validate_token, but return the token object to check the associated user.

request (requests.Request) : the Request object to inspect

django_river_ml.auth.get_user(request)[source]

Given a request, read the Authorization header to get the base64 encoded username and token (password) which is a basic auth. If we return the user object, the user is successfully authenticated. Otherwise, return None. and the calling function should return Forbidden status.

request (requests.Request) : the Request object to inspect

django_river_ml.auth.is_authenticated(request)[source]

Function to check if a request is authenticated. Returns a boolean to indicate if the user is authenticated, and a response with the challenge if not.

request (requests.Request) : the Request object to inspect

django_river_ml.auth.validate_jwt(request)[source]

Given a jwt token, decode and validate

request (requests.Request) : the Request object to inspect

django_river_ml.client module

class django_river_ml.client.DjangoClient[source]

Bases: object

The Django client provides the main interaction between the storage and an internal user. It directly wraps the database, and is used by RiverClient to return responses to the API. If you need to interact with your models from inside of a Django application, use this class.

add_model(model, flavor, name=None)[source]

Add a model by name (e.g., via API post)

announce_event(event, data)[source]

Announce the event

announce_metrics(metrics)[source]

Announce the current metric values

delete_id(identifier)[source]

Delete a known identifier (cached prediction) from the database.

delete_model(name)[source]

Delete a model by name.

finish_learn(event, prediction, features, ground_truth, model_name, identifier=None, **kwargs)[source]

Finish a learning event. This can either be a prediction ‘predict’ event that was done on the server, or a ‘label’ event that retrieved a previous prediction and then updated metrics or the model.

get_model(name)[source]

Get a model by name

label(label, identifier, model_name)[source]

Given a previous prediction (we can get with an identifier) add a label.

learn(model_name, ground_truth=None, prediction=None, features=None, identifier=None, **kwargs)[source]

A learning event takes a learning schema

load_model(filename)[source]

Load a model (e.g., from pickle)

make_prediction(features, model_name)[source]

Shared function to make a prediction.

Returns True if successful with a prediction, otherwise False and an exception to return to the user.

metrics(model_name)[source]

Get metrics from the database for a specific model

models()[source]

Get models known to a database.

predict(features, model_name, identifier)[source]

Run a prediction

save_model(model, model_name)[source]

Save a model by name.

save_pickle(model_name, filename)[source]

Save a model by name to a pickle file

stats(model_name)[source]

Get stats for a model name. If they don’t exist, we return None.

update_metrics(prediction, ground_truth, model_name)[source]

Given a prediction, update metrics to reflect it.

class django_river_ml.client.RiverClient[source]

Bases: object

A river client includes shared functions for interacting with storage. This implementation was chosen so it can easily be plugged into another kind of server without needing to deal with the server-specific requests.

add_model(model, flavor, name=None)[source]

Add a model by name (e.g., via API post)

delete_id(identifier)[source]
delete_model(name)[source]

Delete a model by name.

get_model(name)[source]

Get a model by name

label(label, identifier, model_name)[source]

Given a previous prediction (we can get with an identifier) add a label.

learn(model_name, ground_truth=None, prediction=None, features=None, identifier=None, **kwargs)[source]

A learning event takes a learning schema

make_prediction(features, model_name)[source]

Shared function to make a prediction.

Returns True if successful with a prediction, otherwise False and an exception to return to the user.

metrics(model_name)[source]

Get metrics from the database for a specific model

models()[source]

Get models known to a database.

predict(features, model_name, identifier)[source]

Run a prediction

stats(model_name)[source]

A wrapper to return stats from the database

stream_events()[source]

Stream events from the events announcer.

stream_metrics()[source]

Stream events from the metrics announcer.

django_river_ml.exceptions module

exception django_river_ml.exceptions.FlavorNotSet(*args, **kwargs)[source]

Bases: InvalidUsage

exception django_river_ml.exceptions.InvalidUsage(message, status_code=None, payload=None)[source]

Bases: Exception

status_code = 400
to_dict()[source]
exception django_river_ml.exceptions.UnknownFlavor(*args, **kwargs)[source]

Bases: Exception

django_river_ml.flavors module

class django_river_ml.flavors.BinaryFlavor[source]

Bases: Flavor

check_model(model)[source]

Checks whether or not a model works for a flavor.

default_metrics()[source]

Default metrics to record globally as well as for each model.

property name
property pred_funcs

Listing of prediction functions to try (in that order)

class django_river_ml.flavors.ClusterFlavor[source]

Bases: Flavor

check_model(model)[source]

Checks whether or not a model works for a flavor.

default_metrics()[source]

Default metrics to record globally as well as for each model.

property name
property pred_funcs

Listing of prediction functions to try (in that order)

class django_river_ml.flavors.CremeFlavor[source]

Bases: Flavor

check_model(model)[source]

Checks whether or not a model works for a flavor.

default_metrics()[source]

Default metrics to record globally as well as for each model.

property learn_func

Learn function consistent for all models in river, creme is fit_one

property name
property pred_funcs

Listing of prediction functions to try (in that order)

class django_river_ml.flavors.CustomFlavor[source]

Bases: Flavor

A custom flavor aims to support a user custom model.

check_model(model)[source]

Checks whether or not a model works for a flavor.

default_metrics()[source]

Default metrics to record globally as well as for each model.

property name
property pred_funcs

Listing of prediction functions to try (in that order)

class django_river_ml.flavors.Flavor[source]

Bases: ABC

abstract check_model(model: Any) Tuple[bool, str][source]

Checks whether or not a model works for a flavor.

abstract default_metrics() List[Metric][source]

Default metrics to record globally as well as for each model.

property learn_func

Learn function consistent for all models in river, creme is fit_one

abstract property name
abstract property pred_funcs: str

Listing of prediction functions to try (in that order)

class django_river_ml.flavors.MultiClassFlavor[source]

Bases: Flavor

check_model(model)[source]

Checks whether or not a model works for a flavor.

default_metrics()[source]

Default metrics to record globally as well as for each model.

property name
property pred_funcs

Listing of prediction functions to try (in that order)

class django_river_ml.flavors.NeighborFlavor[source]

Bases: Flavor

check_model(model)[source]

Checks whether or not a model works for a flavor.

default_metrics()[source]

Default metrics to record globally as well as for each model.

property name
property pred_funcs

Listing of prediction functions to try (in that order)

class django_river_ml.flavors.RegressionFlavor[source]

Bases: Flavor

check_model(model)[source]

Checks whether or not a model works for a flavor.

default_metrics()[source]

Default metrics to record globally as well as for each model.

property name
property pred_funcs

Listing of prediction functions to try (in that order)

django_river_ml.flavors.all_models()[source]
django_river_ml.flavors.allowed_flavors()[source]
django_river_ml.flavors.check(model, flavor_name)[source]

Check a model against all flavors available

django_river_ml.middleware module

django_river_ml.middleware.timer_middleware(get_response)[source]

A custom class to start and end a counter. This should be used to decorate api.learn and api.predict

django_river_ml.model module

django_river_ml.model.model_to_dict(model)[source]

Dump a model to json

django_river_ml.namer module

class django_river_ml.namer.Namer[source]

Bases: object

generate(delim='-', length=4, chars='0123456789')[source]
Generate a robot name. Inspiration from Haikunator, but much more

poorly implemented ;)

Parameters

delim: Delimiter length: TokenLength chars: TokenChars

django_river_ml.settings module

django_river_ml.settings.generate_secret_keys(filename)[source]

A helper function to write a randomly generated secret key to file

django_river_ml.signals module

django_river_ml.signals.create_auth_token(sender, instance=None, created=False, **kwargs)[source]

Create a token for the user when the user is created

  1. Assign user a token

Create a token instance for all newly created User instances. We only run on user creation to avoid having to check for existence on each call to User.save. We also check if one is already created in case another app or the main user application is running the same function.

django_river_ml.signals.create_user_token(user)[source]

Function to create the token for the user, if it doesn’t exist.

django_river_ml.signals.get_user_token(user)[source]

Get a user token.

django_river_ml.storage module

class django_river_ml.storage.RedisBackend(host, port, db)[source]

Bases: StorageBackend

Redis is the suggest backend for a more production database.

close()[source]

Do something when the app shuts down.

class django_river_ml.storage.ShelveBackend(filename, flag='c', protocol=None, writeback=False)[source]

Bases: DbfilenameShelf, StorageBackend

Storage backend based on the shelve module from the standard library.

This should mainly be used for development and testing, but not production.

class django_river_ml.storage.StorageBackend[source]

Bases: ABC

Abstract storage backend.

This interface defines a set of methods to implement in order for a database to be used as a storage backend. This allows using different databases in a homogeneous manner by proving a single interface. Since online-ml models are largely defined by Python dictionaries, we use key value store databases like redis.

abstract close()[source]

Do something when the app shuts down.

get(key, default=None)[source]
django_river_ml.storage.add_model(model: Estimator, flavor: str, name: str | None = None) str[source]
django_river_ml.storage.close_db(e=None)[source]
django_river_ml.storage.delete_model(name: str)[source]
django_river_ml.storage.drop_db()[source]

This function’s responsability is to wipe out a database.

This could be implement within each StorageBackend, it’s just a bit more akward because at this point the database connection is not stored in the app anymore.

django_river_ml.storage.get_db() StorageBackend[source]

Get the database, an attribute of settings.

django_river_ml.storage.init_metrics(name: str)[source]
django_river_ml.storage.init_stats(name: str)[source]
django_river_ml.storage.set_flavor(flavor: str, name: str)[source]

django_river_ml.urls module

django_river_ml.utils module

django_river_ml.utils.format_sse(data: str, event=None) str[source]
>>> format_sse(data=json.dumps({'abc': 123}), event='Jackson 5')
'event: Jackson 5\ndata: {"abc": 123}\n\n'
django_river_ml.utils.get_server(request)[source]

Given a request, parse it to determine the server name and using http/https

django_river_ml.utils.humanize_ns(ns: int) str[source]

django_river_ml.version module

django_river_ml.views module

django_river_ml.views.auth module

class django_river_ml.views.auth.GetAuthToken(**kwargs)[source]

Bases: APIView

Given a GET request for a token, validate and return it.

allowed_methods = ('GET',)
authentication_classes = []
get(request, *args, **kwargs)[source]

GET /auth/token

permission_classes = []

django_river_ml.views.base module

django_river_ml.views.learn module

django_river_ml.views.metrics module

django_river_ml.views.model module

django_river_ml.views.predict module