coniferest.session package

class coniferest.session.Session(data, metadata, decision_callback=<function prompt_decision_callback>, *, on_refit_callbacks=None, on_decision_callbacks=None, known_labels: ~typing.Dict[int, ~coniferest.label.Label] = None, model: ~coniferest.coniferest.Coniferest = None)[source]

Bases: object

Active anomaly detection session

Parameters:
  • data (array-like, shape (n_samples, n_features), dtype is number) – 2-D array of data points

  • metadata (array-like, shape (n_samples,), dtype is any) – 1-D array of metadata for each data point

  • decision_callback (callable, optional) – Function to be called when expert decision is required, it must return Label object with the decision and may terminate the session via Session.terminate(). Default is prompt_decision_callback Signature: ‘(metadata, data, session) -> Label’, where metadata is metadata of the object to be labeled, data is data of the object to be labeled, session is this session instance.

  • on_refit_callbacks (list of callable, or callable, or None, optional) – Functions to be called when model is refitted (before “decision_callback”), default is empty list. This function may call Session.terminate(). Signature: ‘(session) -> None’, where session is this session instance.

  • on_decision_callbacks (list of callable, or callable, or None, optional) – Functions to be called when expert decision is made (after “decision_callback”), default is empty list. This function may call Session.terminate(). Signature: ‘(metadata, data, session) -> None’, where metadata is metadata of the object has just been labeled, data is data of this object, session is this session instance.

  • known_labels (dict, optional) – Dictionary of known anomaly labels, keys are data/metadata indices, values are labels of type Label. Default is empty dictionary.

  • model (Coniferest or None, optional) – Anomaly detection model to use, default is PineForest().

current

Index of the last anomaly candidate

Type:

int

last_decision

Label of the last anomaly candidate or None if no decision was made

Type:

Label or None

scores

Current anomaly scores for all data points

Type:

array-like, shape (n_samples,)

terminated

True if session is terminated

Type:

bool

known_labels

Current dictionary of known anomaly labels

Type:

dict[int, Label]

known_anomalies

Array of indices of known anomalies

Type:

array-like

known_regulars

Array of indices of known regular objects

Type:

array-like

known_unknowns

Array of indices of known objects marked with Label::UNKNOWN

Type:

array-like

model

Anomaly detection model used

Type:

Coniferest

Examples

>>> from coniferest.datasets import ztf_m31
>>> from coniferest.session import Label, Session
>>> data, metadata = ztf_m31()
>>> s = Session(
...    data=data,
...    metadata=metadata,
...    decision_callback=lambda *_: Label.ANOMALY,
...    on_decision_callbacks=[lambda _metadata, _data, session: session.terminate()],
... )
>>> _ = s.run()
>>> assert len(s.known_labels) == len(s.known_anomalies) == 1
property current: int
property known_anomalies: ndarray
property known_labels: Dict[int, Label]
property known_regulars: ndarray
property known_unknowns: ndarray
property last_decision: Label | None
property model: Coniferest
run() Session[source]

Evaluate interactive anomaly detection session

property scores: ndarray
terminate() None[source]
property terminated: bool

Submodules

coniferest.session.callback module

class coniferest.session.callback.TerminateAfter(budget: int)[source]

Bases: object

Terminate session after given number of iterations.

This callback to be used as “on decision callback”: Session(…, on_decision_callbacks=[TerminateAfter(budget)])

Parameters:

budget (int) – Number of iterations after which session will be terminated.

class coniferest.session.callback.TerminateAfterNAnomalies(budget: int)[source]

Bases: object

Terminate session after given number of newly labeled anomalies.

This callback to be used as “on decision callback”: Session(…, on_decision_callbacks=[TerminateAfter(budget)])

Parameters:

budget (int) – Number of anomalies to stop after.

coniferest.session.callback.prompt_decision_callback(metadata, data, session) Label[source]

Prompt user to label the object as anomaly or regular.

If user sends keyboard interrupt, terminate the session.

coniferest.session.callback.viewer_decision_callback(metadata, data, session) Label[source]

Open SNAD Viewer for ZTF DR object. Metadata must be ZTF DR object ID.