qem.model

Functions

abstractmethod(funcobj)

A decorator indicating abstract methods.

load_dotenv([dotenv_path, stream, verbose, ...])

Parse a .env file and then load all the variables found as environment variables.

safe_convert_to_numpy(tensor)

Safely convert a Keras tensor to numpy array, handling different backends.

safe_convert_to_tensor(array[, dtype])

Safely convert a numpy array to Keras tensor.

Classes

GaussianKernel()

Gaussian kernel implementation.

GaussianModel(*args, **kwargs)

Gaussian peak model.

ImageModel(*args, **kwargs)

Base class for all image models.

LorentzianModel(*args, **kwargs)

Lorentzian peak model.

VoigtModel(*args, **kwargs)

Voigt peak model.

class qem.model.ImageModel(*args: Any, **kwargs: Any)[source]

Base class for all image models.

__init__(dx: float = 1.0)[source]

Initialize the model.

Parameters:

dx (float, optional) – Pixel size. Defaults to 1.0.

set_params(params)[source]
update_params(params)[source]

Update the model parameters (values only, not shapes).

build(input_shape=(1,))[source]
get_params()[source]
call(inputs)[source]

Forward pass of the model.

abstract model_fn(x, y, pos_x, pos_y, height, width, *args)[source]

Core model function that defines the peak shape.

abstract volume(params: dict) numpy.ndarray[source]

Calculate the volume of each peak.

sum(x_grid: numpy.ndarray, y_grid: numpy.ndarray, local: bool = True)[source]

Calculates the sum of all peaks on a grid, with an optional background.

This method supports both a global calculation and a memory-efficient local calculation suitable for JIT compilation.

Parameters:
  • x_grid (array) – A 2D array of X coordinates (from meshgrid).

  • y_grid (array) – A 2D array of Y coordinates (from meshgrid).

  • local (bool, optional) – If True, calculates peaks in local windows to conserve memory. This approach is JIT-compatible. Defaults to True.

Returns:

array – A 2D array representing the rendered image of peaks plus background.

class qem.model.GaussianModel(*args: Any, **kwargs: Any)[source]

Gaussian peak model.

volume(params: dict) numpy.ndarray[source]

Calculate the volume of each Gaussian peak.

For a 2D Gaussian, the volume is: height * 2π * width²

model_fn(x, y, pos_x, pos_y, height, width, *args)[source]

Core computation for Gaussian model using Keras.

class qem.model.LorentzianModel(*args: Any, **kwargs: Any)[source]

Lorentzian peak model.

volume(params: dict) numpy.ndarray[source]

Calculate the volume of each Lorentzian peak.

For a 2D Lorentzian, the volume is: height * π * width²

model_fn(x, y, pos_x, pos_y, height, width, *args)[source]

Core computation for Lorentzian model using Keras.

class qem.model.VoigtModel(*args: Any, **kwargs: Any)[source]

Voigt peak model.

__init__(dx: float = 1.0)[source]

Initialize the model.

Parameters:

dx (float, optional) – Pixel size. Defaults to 1.0.

set_params(params)[source]
build(input_shape=None)[source]
get_params()[source]
volume(params: dict) numpy.ndarray[source]

Calculate the volume of each Voigt peak.

For a 2D Voigt profile, the volume is a weighted sum of Gaussian and Lorentzian volumes: V = ratio * (height * 2π * width²) + (1-ratio) * (height * π * width²)

model_fn(x, y, pos_x, pos_y, height, width, ratio)[source]

Core computation for Voigt model using Keras.

class qem.model.GaussianKernel[source]

Gaussian kernel implementation.

__init__()[source]

Initialize the kernel.

Parameters:

backend (str, optional) – Backend to use (‘tensorflow’, ‘pytorch’, or ‘jax’). Defaults to ‘jax’.

gaussian_kernel(sigma)[source]

Creates a 2D Gaussian kernel with the given sigma.

gaussian_filter(image, sigma)[source]

Applies Gaussian filter to a 2D image.

qem.model.gaussian_2d_single(xy, pos_x, pos_y, height, width, background)

2D Gaussian function for single atom.