Embedders

Embedding logic for anomaly detection.

class ecgan.anomaly_detection.embedder.Embedder(train_x, train_y, dataset)[source]

Bases: abc.ABC

Abstract embedding class.

Parameters
  • train_x (Union[Tensor, ndarray]) -- Data the initial embedding should be fit on.

  • train_y (Union[Tensor, ndarray]) -- Labels the initial embedding should be fit on.

  • dataset (str) -- Dataset identifier, has to be supported by ecgan.utils.datasets.DatasetFactory

abstract get_initial_embedding()[source]

Create an initial embedding and train a reducer.

Return type

Tuple[ndarray, BaseEstimator]

Returns

The embedding data as well as the reducer.

embed_test(test_x, test_y, include_initial_embedding)[source]

Embed test data into embedding pre-trained on the training data.

Parameters
  • test_x (Union[Tensor, ndarray]) -- Test data.

  • test_y (Union[Tensor, ndarray]) -- Test labels.

  • include_initial_embedding (bool) -- Flag to indicate if the embedding trained on train_x should be returned as well.

Return type

Tuple[ndarray, ndarray]

Returns

(embedding, labels) tuple. The labels have shifted to avoid conflicts with existing train labels.

embed(embeddable_data, labels=None)[source]

Embed novel data into given embedding. This data can be synthetic data.

Parameters
  • embeddable_data (Tensor) -- Any data which can be embedded into a trained embedding.

  • labels (Optional[Tensor]) -- Labels corresponding to the embeddable data.

Return type

Tuple[ndarray, ndarray]

Returns

(embedding, labels) tuple. The labels have shifted to avoid conflicts with existing train labels.

abstract get_plot(embedding, labels)[source]

Retrieve a plot of the embedding as an np.ndarray.

Return type

Figure

abstract draw_interpolation_path(trace, labels, embedding=None, fig_size=(15, 11.25))[source]

Draw an interpolation path of reconstructed data into an existing embedding.

Return type

Figure

class ecgan.anomaly_detection.embedder.UMAPEmbedder(train_x, train_y, dataset)[source]

Bases: ecgan.anomaly_detection.embedder.Embedder

Create UMAP embedding (on train/vali data) and allow embedding of novel (test) data.

get_initial_embedding()[source]

Create an initial embedding.

Return type

Tuple[ndarray, BaseEstimator]

get_plot(embedding, labels)[source]

Retrieve a plot of the embedding as a matplotlib Figure.

Return type

Figure

draw_interpolation_path(trace, labels, embedding=None, fig_size=(15, 11.25))[source]

Plot a trace/path in latent space between data in the trace tensor.

Parameters
  • trace (Union[ndarray, Tensor]) -- The trace to draw.

  • labels (Union[ndarray, Tensor]) -- The data labels.

  • embedding (Union[Tensor, ndarray, None]) -- The embedding the trace should be embedded into.

  • fig_size (Tuple[float, float]) -- Size of the matplotlib figure (width, height).

Return type

Figure

Returns

A matplotlib Figure of the visualized embedding.