encodermap.callbacks package#

Submodules#

encodermap.callbacks.callbacks module#

Callbacks to strew into the Autoencoder classes.

class CheckpointSaver(parameters=None)[source]#

Bases: EncoderMapBaseCallback

Callback that saves an encodermap.models model.

Parameters:

parameters (Optional['AnyParameters'])

on_checkpoint_step(epoch, logs=None)[source]#

Overwrites parent class’ on_checkpoint_step method.

Uses encodermap.misc.saving_loading_models.save_model to save the model. Luckily, the keras callbacks contain the model as an attribute (self.model).

Parameters:
Return type:

None

class EarlyStop(patience=0)[source]#

Bases: Callback

Stop training when the loss is at its min, i.e. the loss stops decreasing.

Parameters:

patience (int) – Number of epochs to wait after min has been hit. After this number of no improvement, training stops.

on_train_batch_end(batch, logs=None)[source]#

Gets the current loss at the end of the batch compares it to previous batches.

Parameters:
Return type:

None

on_train_begin(logs=None)[source]#

Sets some attributes at the beginning of training.

Parameters:

logs (dict | None)

Return type:

None

on_train_end(logs=None)[source]#

Prints a message after training, if an early stop occured.

Parameters:

logs (dict | None)

Return type:

None

class EncoderMapBaseCallback(parameters=None)[source]#

Bases: Callback

Base class for callbacks in EncoderMap.

The Parameters class in EncoderMap has a summary_step variable that dictates when variables and other tensors are logged to TensorBoard. No matter what property is logged there will always be a code section executing a if train_step % summary_step == 0 code snippet. This is handled centrally in this class. This class is instantiated inside the user-facing AutoEncoderClass classes and is provided with the appropriate parameters (Parameters for EncoderMap and ADCParameters for AngleDihedralCartesianEncoderMap). Thus, subclassing this class does not need to implement a new __init__ method. Only the on_summary_step and the on_checkpoint_step methods need to be implemented for sub-classes if this class with code that should happen when these events happen.

Examples:

In this example, the on_summary_step method causes an exception.

>>> from typing import Optional
>>> import encodermap as em
...
>>> class MyCallback(em.callbacks.EncoderMapBaseCallback):
...     def on_summary_step(self, step: int, logs: Optional[dict] = None) -> None:
...         raise Exception(f"Summary step {self.steps_counter} has been reached.")
...
>>> emap = em.EncoderMap()  
Output...
>>> emap.add_callback(MyCallback)
>>> emap.train()  
Traceback (most recent call last):
    ...
Exception: Summary step 10 has been reached.
Parameters:

parameters (Optional['AnyParameters'])

steps_counter#

The current step counter. Increases every on_train_batch_end.

Type:

int

p (Union[encodermap.parameters.Parameters, encodermap.parameters.ADCParameters]

The parameters for this callback. Based on the summary_step and checkpoint_step of the encodermap.parameters.Parameters class different class-methods are called.

on_checkpoint_step(step, logs=None)[source]#

Executed, when the currently finished batch matches encodermap.Parameters.checkpoint_step

Parameters:
  • step (int) – The number of the current step.

  • logs (Optional[dict]) – logs is a dict containing the metrics results.

Return type:

None

on_summary_step(step, logs=None)[source]#

Executed, when the currently finished batch matches encodermap.Parameters.summary_step

Parameters:
  • step (int) – The number of the current step.

  • logs (Optional[dict]) – logs is a dict containing the metrics results.

Return type:

None

on_train_batch_end(batch, logs=None)[source]#

Called after a batch ends. The number of batch is provided by keras.

This method is the backbone of all of EncoderMap’s callbacks. After every batch is method is called by keras. When the number of that batch matches either encodermap.Parameters.summary_step or encodermap.Parameters.checkpoint_step the code on self.on_summary_step, or self.on_checkpoint_step is executed. These methods should be overwritten by child classes.

Parameters:
  • batch (int) – The number of the current batch. Provided by keras.

  • logs (Optional[dict]) – logs is a dict containing the metrics results.

Return type:

None

class ImageCallback(parameters, highd_data, image_step, backend='matplotlib', mpl_scatter_kws=None, mpl_hist_kws=None, plotly_scatter_kws=None, plotly_hist_kws=None, additional_fns=None, when='batch', save_dir=None)[source]#

Bases: Callback

Writes images to tensorboard.

Parameters:
  • parameters (AnyParameters)

  • highd_data (np.ndarray)

  • image_step (int)

  • backend (Literal['matplotlib', 'plotly'])

  • mpl_scatter_kws (Optional[dict])

  • mpl_hist_kws (Optional[dict])

  • plotly_scatter_kws (Optional[dict])

  • plotly_hist_kws (Optional[dict])

  • additional_fns (Optional[list[Callable]])

  • when (Literal['batch', 'epoch'])

  • save_dir (Optional[Union[str, Path]])

get_lowd()[source]#
on_epoch_end(epoch, logs=None)[source]#

Calls encodermap.misc.summaries.image_summary on epoch end.

Parameters:
Return type:

None

on_train_batch_end(batch, logs=None)[source]#

Calls encodermap.misc.summaries.image_summary on batch end.

Parameters:
Return type:

None

save_image_to_dir(lowd, step)[source]#

Saves the lowd representation to disk, so it can be looked at later.

Parameters:
Return type:

None

class IncreaseCartesianCost(parameters=None)[source]#

Bases: Callback

Callback for the enocdermap.autoencoder.AngleDihedralCarteisanEncoderMap.

This callback implements the soft-start of the cartesian cost.

Parameters:

parameters (Optional[ADCParameters])

calc_current_cartesian_cost_scale(epoch)[source]#

Calculates the current cartesian distance scale, based on the parameters self.a, self.b self.p.cartesian_cost_scale.

on_train_batch_end(batch, logs=None)[source]#

Sets the value of the keras backend variable self.current_cartesian_cost_scale

Parameters:
class NoneInterruptCallback[source]#

Bases: Callback

A callback that interrupts training, when NaN is encountered in weights.

on_train_batch_end(batch, logs=None)[source]#

Gets the current loss at the end of the batch compares it to previous batches.

Parameters:
Return type:

None

class ProgressBar(parameters=None)[source]#

Bases: EncoderMapBaseCallback

Progressbar Callback. Mix in with model.fit() and make sure to set verbosity to zero.

Parameters:

parameters (Optional['AnyParameters'])

on_summary_step(epoch, logs=None)[source]#

Update the progress bar after an epoch with the current loss.

Parameters:
  • epoch (int) – Current epoch. Will be automatically passed by tensorflow.

  • logs (Optional[dict]) – Also automatically passed by tensorflow. Contains metrics and losses. logs[‘loss’] will be written to the progress bar.

Return type:

None

on_train_batch_end(batch, logs=None)[source]#

Overwrites the parent class’ on_train_batch_end and adds a progress-bar update.

Parameters:
Return type:

None

on_train_begin(logs=None)[source]#

Simply creates the progressbar once training starts.

Parameters:

logs (dict | None)

Return type:

None

on_train_end(logs=None)[source]#

Close the Progress Bar

Parameters:

logs (dict | None)

Return type:

None

class TensorboardWriteBool(parameters=None)[source]#

Bases: Callback

This class saves the value of the keras variable log_bool.

Based on this variable, stuff will be written to tensorboard, or not.

Parameters:

parameters (Optional['AnyParameters'])

on_train_batch_end(batch, logs=None)[source]#

Sets the value of the keras backend variable log_bool.

This method does not use the batch argument because the variable self.current_training_step is used.

Parameters:
Return type:

None

encodermap.callbacks.metrics module#

Metrics are meta-variables that can be computed to observe the training but are not directly linked to loss/cost and gradients.

class ADCClashMetric(*args, **kwargs)[source]#

Bases: AngleDihedralCartesianEncoderMapBaseMetric

Parameters:
  • distance_unit (Literal['nm', 'ang'])

  • name (str)

  • parameters (Optional[ADCParameters])

custom_update_state = True#

Metric that computes clashes between atoms in the reconstructed backbone.

Please choose the correct distance unit.

classmethod from_config(config, custom_objects=None)[source]#

Reconstructs this keras serializable from a dict.

Parameters:
get_config()[source]#

Serializes this keras serializable.

Returns:

A dict with the serializable objects.

Return type:

dict[str, Any]

result()[source]#

Computes and returns the scalar metric value tensor or a dict of scalars.

Result computation is an idempotent operation that simply calculates the metric value using the state variables.

Returns:

A scalar tensor, or a dictionary of scalar tensors.

update_state(y_true, y_pred, sample_weight=None)[source]#

Updates this metric.

y_true (tf.Tensor): The train goal. y_pred (tf.Tensor): Current model output.

Parameters:
  • y_true (Tensor)

  • y_pred (Tensor)

Return type:

None

class ADCRMSDMetric(*args, **kwargs)[source]#

Bases: AngleDihedralCartesianEncoderMapBaseMetric

Parameters:
custom_update_state = True#
result()[source]#

Computes and returns the scalar metric value tensor or a dict of scalars.

Result computation is an idempotent operation that simply calculates the metric value using the state variables.

Returns:

A scalar tensor, or a dictionary of scalar tensors.

update_state(y_true, y_pred, sample_weight=None)[source]#

Accumulates statistics for the metric.

Note: This function is executed as a graph function in graph mode. This means:

  1. Operations on the same resource are executed in textual order. This should make it easier to do things like add the updated value of a variable to another, for example.

  2. You don’t need to worry about collecting the update ops to execute. All update ops added to the graph by this function will be executed.

As a result, code should generally work the same way with graph or eager execution.

Parameters:
  • *args

  • **kwargs – A mini-batch of inputs to the Metric.

class AngleDihedralCartesianEncoderMapBaseMetric(*args, **kwargs)[source]#

Bases: Metric

Parameters:
custom_update_state = False#
classmethod from_config(config, custom_objects=None)[source]#

Reconstructs this keras serializable from a dict.

Parameters:
get_config()[source]#

Serializes this keras serializable.

Returns:

A dict with the serializable objects.

Return type:

dict[str, Any]

class EncoderMapBaseMetric(*args, **kwargs)[source]#

Bases: Metric

Base class for metrics in EncoderMap.

Metrics are used to judge the performance of ML models. They are similar to loss functions as they can (but don’t have to) be computed at every iteration of the training. Oftentimes, metrics implement more expensive calculations. Metrics are also automatically added to a model’s training history, accessible via history = emap.train().

Examples:

In this example, the update method always returns zero.

>>> import encodermap as em
>>> import numpy as np
...
>>> class MyMetric(em.callbacks.EncoderMapBaseMetric):
...    def update(self, y_true, y_pred, sample_weight=None):
...        return 0.0
...
>>> emap = em.EncoderMap()  
Output...
>>> emap.add_metric(MyMetric)
>>> history = emap.train()  
Saving...
>>> np.mean(history["MyMetric Metric"])
0.0
>>> len(history["MyMetric Metric"]) == emap.p.n_steps
True
Parameters:
custom_update_state = True#
classmethod from_config(config, custom_objects=None)[source]#

Reconstructs this keras serializable from a dict.

Parameters:
get_config()[source]#

Serializes this keras serializable.

Returns:

A dict with the serializable objects.

Return type:

dict[str, Any]

result()[source]#

Computes and returns the scalar metric value tensor or a dict of scalars.

Result computation is an idempotent operation that simply calculates the metric value using the state variables.

Returns:

A scalar tensor, or a dictionary of scalar tensors.

update_state(y_true, y_pred, sample_weight=None)[source]#

Updates this metric.

Parameters:
  • y_true (Tensor)

  • y_pred (Tensor)

Return type:

None

class OmegaAngleBaseMetric(*args, **kwargs)[source]#

Bases: AngleDihedralCartesianEncoderMapBaseMetric

custom_update_state = True#
class SidechainVsBackboneFrequencyBaseMetric(*args, **kwargs)[source]#

Bases: AngleDihedralCartesianEncoderMapBaseMetric

custom_update_state = True#
kabsch_tf(a, b)[source]#
Parameters:
  • a (Tensor)

  • b (Tensor)

Return type:

Tensor

kabsch_weighted(P, Q, W=None)[source]#

Taken from Jimmy C. Kromann’s RMSD (charnley/rmsd) Using the Kabsch algorithm with two sets of paired point P and Q. Each vector set is represented as an NxD matrix, where D is the dimension of the space. An optional vector of weights W may be provided.

Note that this algorithm does not require that P and Q have already been overlayed by a centroid translation.

The function returns the rotation matrix U, translation vector V, and RMS deviation between Q and P’, where P’ is:

P’ = P * U + V

For more info see http://en.wikipedia.org/wiki/Kabsch_algorithm

Parameters:
  • P (np.ndarray) – Points A with shape (n_points, 3).

  • Q (np.ndarray) – Points B with shape (n_points, 3).

  • W (np.ndarray) – Weights with shape (n_points, ).

Returns:

The RMSD value in the same units, as the input points.

Return type:

float

rmsd(a, b, translate)[source]#
rmsd_numpy(a, b, translate=True)[source]#

Implements Kabsch-Umeyama algorithm.

References

@article{kabsch1976solution,

title={A solution for the best rotation to relate two sets of vectors}, author={Kabsch, Wolfgang}, journal={Acta Crystallographica Section A: Crystal Physics, Diffraction, Theoretical and General Crystallography}, volume={32}, number={5}, pages={922–923}, year={1976}, publisher={International Union of Crystallography}

}

Parameters:
Return type:

ndarray

rmsd_tf(a, b, p)[source]#
Parameters:
Return type:

tf.Tensor

Module contents#