LTEM image simulation

Simulation module

class PyLorentz.sim.sim.SimLTEM(mags, scale, zscale, verbose=1, ovf_file=None)[source]

Bases: MansuripurPhase, LinsupPhase, BaseSim

A class used to simulate Lorentz Transmission Electron Microscopy (LTEM) images.

__init__(mags, scale, zscale, verbose=1, ovf_file=None)[source]

Initialize the SimLTEM object.

Parameters:
  • mags (np.ndarray) – Magnetization array.

  • scale (float) – Scale factor for the simulation.

  • zscale (float) – Z-axis scale factor.

  • verbose (float | bool, optional) – Verbosity level. Default is 1.

  • ovf_file (os.PathLike | None, optional) – Path to OVF file. Default is None.

classmethod load_ovf(file, verbose=1)[source]

Load an OVF file and initialize a SimLTEM object.

Parameters:
  • file (str | os.PathLike) – Path to the OVF file.

  • verbose (int, optional) – Verbosity level. Default is 1.

Returns:

An instance of the SimLTEM class.

Return type:

SimLTEM

compute_phase(method='mansuripur', tilt_x=None, tilt_y=None, beam_energy=None, device='cpu', multiproc=True, **kwargs)[source]

Compute the phase shift for the simulation.

Parameters:
  • method (str | None, optional) – Phase computation method. Options are ‘mansuripur’ or ‘linsup’. Default is ‘mansuripur’.

  • tilt_x (float | None, optional) – Tilt angle in the x direction. Default is None.

  • tilt_y (float | None, optional) – Tilt angle in the y direction. Default is None.

  • beam_energy (float | None, optional) – Beam energy. Default is None.

  • device (str, optional) – Device to use for computation. Default is ‘cpu’.

  • multiproc (bool, optional) – Whether to use multiprocessing. Default is True.

  • **kwargs – Additional arguments for phase computation.

  • "mansuripur" (For phase_method ==) –

    • sym (bool): Symmetrize magnetizations

    • pad (bool or tuple): Shape to pad the magnetizations

    • pad_mode (str): passed to np.pad

Return type:

None

sim_images(defocus_values, scope, flip=False, thk_E_filter_sigma=1, amorphous_bkg=None, pad=None, pad_mode='edge', symmetrize=False, verbose=None)[source]

Simulate images at different defocus values.

Parameters:
  • defocus_values (float | list) – Single defocus value or list of defocus values.

  • scope (Microscope) – Microscope object.

  • flip (bool, optional) – Whether to flip the phase. Default is False.

  • thk_E_filter_sigma (float, optional) – Sigma value for Gaussian filter applied to the thickness map and phase_E. Default is 1.

  • amorphous_bkg (bool | float | None, optional) – Amorphous background level. Default is None.

  • padded_shape (tuple | None, optional) – Shape for padding. Default is None.

  • pad_mode (str, optional) – mode passed to np.pad. Default is “edge”.

  • sym (bool, optional) – Whether or not to symmetrize the phase

Returns:

A dataset containing simulated defocused images.

Return type:

DefocusedDataset

sim_TFS(defocus_values, scope, flip=False, thk_E_filter_sigma=1, amorphous_bkg=None, pad=None, pad_mode='edge', symmetrize=False, verbose=None)[source]

Simulate a Through Focal Series (TFS).

for single or set of defocus values, record a through focal series with/without flip one defocus val, flip=False -> [+-, +0, ++] one defocus val, flip=True, -> [[+-, +0, ++], [–, -0, -+]] multiple defocus vals, everything goes into a DefocusedDataset object

Parameters:
  • defocus_values (float | list) – Single defocus value or list of defocus values.

  • scope (Microscope) – Microscope object.

  • flip (bool, optional) – Whether to flip the phase. Default is False.

  • filter_sigma (float, optional) – Sigma value for Gaussian filter. Default is 1.

  • amorphous_bkg (bool | float | None, optional) – Amorphous background level. Default is None.

  • padded_shape (tuple | None, optional) – Shape for padding. Default is None.

Returns:

A series of simulated images at different focal depths.

Return type:

ThroughFocalSeries

copy()[source]

Returns a deep copy of itself.

Phase calculation

class PyLorentz.sim.comp_phase.LinsupPhase(mags, scale, zscale, verbose=1)[source]

Bases: BaseSim

A class for calculating phase shifts using the linear superposition method.

class PyLorentz.sim.comp_phase.MansuripurPhase(mags, scale, zscale, verbose=1)[source]

Bases: BaseSim

A class for calculating phase shifts using the Mansuripur method.

Base Sim module

class PyLorentz.sim.base_sim.BaseSim(mags, scale, zscale, verbose=1)[source]

Bases: object

A base class for simulations, providing common attributes and methods.

__init__(mags, scale, zscale, verbose=1)[source]

Initialize the BaseSim object.

Parameters:
  • mags (np.ndarray) – Magnetization array.

  • scale (float) – Scale factor for the simulation.

  • zscale (float) – Z-axis scale factor.

  • verbose (float | bool, optional) – Verbosity level. Default is 1.

vprint(*args, **kwargs)[source]

Print messages if verbose is enabled.

Return type:

None

property phase_B: numpy.ndarray

Get the B-phase.

property phase_E: numpy.ndarray

Get the E-phase.

property phase_T: numpy.ndarray

Get the total phase (B-phase + E-phase).

property phase_shape: tuple[int, ...]

Get the shape of the B-phase array.

set_sample_params(params)[source]

Set sample parameters.

Return type:

None

property sample_params: dict

Get the sample parameters.

property phase_method: str

Get the phase method.

property B0: float

Get the B0 parameter.

property sample_V0: float

Get the sample mean inner potential.

property sample_xip0: float

Get the sample extinction distance.

property mem_V0: float

Get the membrane mean inner potential.

property mem_xip0: float

Get the membrane extinction distance.

property mem_thickness: float

Get the membrane thickness.

property tilt_x: float

Get the tilt angle in the x direction.

property tilt_y: float

Get the tilt angle in the y direction.

property beam_energy: float

Get the beam energy.

property zscale: float

Get the z-axis scale factor.

property scale: float

Get the scale factor.

property Mz: numpy.ndarray

Get the z-component of the magnetization.

property My: numpy.ndarray

Get the y-component of the magnetization.

property Mx: numpy.ndarray

Get the x-component of the magnetization.

property shape: tuple[int, ...]

Get the shape of the magnetization array.

property mags: numpy.ndarray

Get the magnetization array.

property shape_func: numpy.ndarray

Get the shape function.

property flat_shape_func: numpy.ndarray

Get the flattened shape function.

get_flat_shape_func(perspective=True, sigma=0)[source]

Get the flattened shape function.

Parameters:
  • perspective (bool, optional) – Whether to account for perspective when tilting. Default

  • True. (is)

  • sigma (float, optional) – Sigma value for Gaussian filter. Default is 0.

Return type:

None

get_shape_func(mags=None)[source]

Return 3D shape function of the magnetization.

Parameters:

mags (np.ndarray | None, optional) – Magnetization array. Default is None.

Return type:

None

show_mags(xy_only=False, s3D=False, show_scale=False, **kwargs)[source]

Visualize the magnetization.

Parameters:
  • xy_only (bool, optional) – Whether to show only the XY components. Default is False.

  • s3D (bool, optional) – Whether to show in 3D. Default is False.

  • show_scale (bool, optional) – Whether to show the scale. Default is False.

  • **kwargs – Additional arguments for visualization.

Return type:

None

show_thickness_map(**kwargs)[source]

Visualize the thickness map.

Parameters:

**kwargs – Additional arguments for visualization.

Return type:

None

visualize(show_thickness_map=True, xy_only=False)[source]

Visualize the magnetization and thickness map.

Parameters:
  • show_thickness_map (bool, optional) – Whether to show the thickness map. Default is True.

  • xy_only (bool, optional) – Whether to show only the XY components. Default is False.

Return type:

None

show_phase()[source]

Visualize the B-phase and E-phase.

Return type:

None

show_phase_B(**kwargs)[source]

Visualize the B-phase.

Parameters:

**kwargs – Additional arguments for visualization.

Return type:

None

show_phase_E(**kwargs)[source]

Visualize the E-phase.

Parameters:

**kwargs – Additional arguments for visualization.

Return type:

None