analyze_image

The analyze_image module provides functions for analyzing single images using Nenya models.

Functions

nenya.analyze_image.get_latents(img, model_file, opt)

Get the Nenya latents for an input image.

Parameters:
  • img (numpy.ndarray) – Input image (64,64) or (1,64,64)

  • model_file (str) – Full path to the Nenya model file (must be on local filesystem)

  • opt (nenya.params.Params) – Parameters for the Nenya model

Returns:

Tuple of (latents, pre-processed image)

Return type:

tuple

nenya.analyze_image.calc_DT(images, random_jitter, verbose=False)

Calculate DT (temperature difference) for a given image or set of images.

Parameters:
  • images (numpy.ndarray) – Input images. Analyzed shape is (N, 64, 64) but various input shapes are allowed.

  • random_jitter (list) – Range to crop, amount to randomly jitter

  • verbose (bool, optional) – Whether to print progress. Defaults to False.

Returns:

DT value(s)

Return type:

numpy.ndarray or float

nenya.analyze_image.umap_image(nenya_model, img)

UMAP embed an input image using a specified model.

Parameters:
  • nenya_model (str) – Nenya model name, e.g. ‘v4’, ‘v5’

  • img (numpy.ndarray) – Input image

Returns:

Tuple of (UMAP embedding, pre-processed image, table file path, DT value, latents)

Return type:

tuple

Example Usage

from nenya import analyze_image
from nenya import io as nenya_io
import numpy as np

# Create or load an image
img = np.random.randn(64, 64)  # Example random image

# Load model options
opt, model_file = nenya_io.load_opt('v5')

# Get latents
latents, pp_img = analyze_image.get_latents(img, model_file, opt)

# Calculate DT
DT = analyze_image.calc_DT(img, opt.random_jitter)

# UMAP embed the image
embedding, pp_img, table_file, DT, latents = analyze_image.umap_image('v5', img)

print(f"UMAP coordinates: U0={embedding[0,0]:.3f}, U1={embedding[0,1]:.3f}")
print(f"DT value: {DT:.2f}")

Implementation Details

The umap_image function combines several steps:

  1. Loads the model options and model file

  2. Extracts latent representations from the image

  3. Calculates the DT value

  4. Loads the appropriate UMAP model

  5. Projects the latent vector into UMAP space

Image Preprocessing

Before extracting latents, images are preprocessed:

  1. Reshaped to the expected format if necessary

  2. Demeaned (mean subtracted)

  3. Converted to a PyTorch tensor

  4. Batched for model input

The calc_DT function:

  1. Reshapes the input to a standard format

  2. Calculates the 90th and 10th percentile temperatures in the center region

  3. Returns the difference as the DT value