Utilities

Plotting and TensorBoard logging helpers.

phenocoder.utils.plot_latent_space(model, generator, oh_enc, sample_frac=1, show=True, return_fig=False)[source]

Plot UMAP visualization of the latent space colored by dataset and z-position.

Generates a 2D UMAP projection of the latent space representations and creates two scatter plots: one colored by dataset and one colored by z-stack position.

Parameters:
  • model (CVAE | CondCVAE) – Trained CVAE or CondCVAE model with encoder.

  • generator (SequenceGenerator) – Data generator (SequenceGenerator) providing image patches.

  • oh_enc (OneHotEncoder) – One-hot encoder used for encoding conditions.

  • sample_frac (float, optional) – Fraction of generator batches to sample for plotting. Defaults to 1 (use all data).

  • show (bool, optional) – Whether to display the plot. Defaults to True.

  • return_fig (bool, optional) – Whether to return the figure object. Defaults to False.

Returns:

Figure object if return_fig=True, otherwise None.

Return type:

matplotlib.figure.Figure or None

phenocoder.utils.plot_reconstructions(model, generator, n_preview=200, batch_size=64, show=True, return_fig=False)[source]

Plot side-by-side comparison of input images and their VAE reconstructions.

Creates a montage visualization showing original input patches alongside their reconstructions from the VAE model. Displays the first channel for all selected patches.

Parameters:
  • model (CVAE | CondCVAE) – Trained CVAE or CondCVAE model with encoder and decoder.

  • generator (SequenceGenerator) – Data generator (SequenceGenerator) providing image patches.

  • n_preview (int, optional) – Number of image patches to visualize. Defaults to 200.

  • batch_size (int, optional) – Batch size for model predictions. Defaults to 64.

  • show (bool, optional) – Whether to display the plot. Defaults to True.

  • return_fig (bool, optional) – Whether to return the figure object. Defaults to False.

Returns:

Figure object if return_fig=True, otherwise None.

Return type:

matplotlib.figure.Figure or None

phenocoder.utils.plot_to_image(figure)[source]

Convert a matplotlib figure to a TensorFlow image tensor.

Converts the matplotlib plot to a PNG image in memory and returns it as a TensorFlow tensor suitable for TensorBoard logging. The supplied figure is closed and inaccessible after this call.

Parameters:

figure (matplotlib.figure.Figure) – Matplotlib figure to convert.

Returns:

TensorFlow image tensor with shape (1, height, width, 4) in RGBA format.

Return type:

tf.Tensor

phenocoder.utils.write_training_plots_to_tensorboard(model, data_generator_train, model_oh_enc, dir_tensorboard, n_preview=300, plot_frac=0.001)[source]

Write training visualization plots to TensorBoard.

Generates and writes reconstruction and latent space visualization plots to TensorBoard for monitoring model training progress.

Parameters:
  • model (CVAE | CondCVAE) – The trained model (CVAE or CondCVAE).

  • data_generator_train (SequenceGenerator) – Training data generator.

  • model_oh_enc (OneHotEncoder) – One-hot encoder for conditional models.

  • dir_tensorboard (Path or str) – Directory for TensorBoard logs.

  • n_preview (int, optional) – Number of samples for reconstruction plots. Defaults to 300.

  • plot_frac (float, optional) – Fraction of data for latent space visualization. Defaults to 0.001.

Returns:

None

Return type:

None