AD phase reconstruction

Automatic differentiation (AD) based methods of phase reconstruction.

AD phase reconstruction

class PyLorentz.phase.AD_phase.ADPhase(dd, device, save_dir=None, name=None, verbose=True, scope=None, sample_params={}, rng_seed=None, LRs={}, scheduler_type=None, noise_frac=0.01, gaussian_sigma=1)[source]

Bases: BasePhaseReconstruction

ADPhase class for phase reconstruction using defocused datasets and DIPs.

__init__(dd, device, save_dir=None, name=None, verbose=True, scope=None, sample_params={}, rng_seed=None, LRs={}, scheduler_type=None, noise_frac=0.01, gaussian_sigma=1)[source]

Initialize the ADPhase object.

Parameters:
  • dd (DefocusedDataset) – The defocused dataset.

  • device (Union[str, int]) – The device to use (CPU or GPU).

  • save_dir (Optional[os.PathLike], optional) – Directory to save results.

  • name (Optional[str], optional) – Name for the results.

  • verbose (bool, optional) – Verbosity level.

  • scope (Optional[Microscope], optional) – Microscope object.

  • sample_params (dict, optional) – Sample parameters.

  • rng_seed (Optional[int], optional) – Random seed.

  • LRs (dict, optional) – Learning rates for optimization.

  • scheduler_type (Optional[str], optional) – Type of learning rate scheduler.

  • noise_frac (float, optional) – Fraction of noise to add.

  • gaussian_sigma (float, optional) – Sigma value for Gaussian filter.

property shape
property shape_full

shape with padding

property pad

Padding around all items in pixels, equal padding before/after each axis. (pad_y, pad_x)

property recon_phase: numpy.ndarray

Returns the reconstructed phase after applying Gaussian filter. This is the cropped recon phase without padding.

Returns:

Reconstructed phase image.

Return type:

Optional[np.ndarray]

property recon_phase_full: numpy.ndarray

Returns the reconstructed phase after applying Gaussian filter. This includes any padding.

Returns:

Reconstructed phase image.

Return type:

Optional[np.ndarray]

property best_phase: numpy.ndarray

Returns the best phase after applying Gaussian filter. This is the cropped best phase without padding.

Returns:

Best phase image.

Return type:

np.ndarray

property best_phase_full: numpy.ndarray

Returns the best phase after applying Gaussian filter. This is the full phase with any padding.

Returns:

Best phase image.

Return type:

np.ndarray

property phase_B_full: numpy.ndarray | None
set_best_iter(iter_ind=-1)[source]

Sets the best phase from the specified iteration index.

Parameters:

iter_ind (int, optional) – Index of the iteration to use for the best phase.

Return type:

None

property recon_amp: numpy.ndarray

Returns the reconstructed amplitude after applying Gaussian filter. This is the cropped recon amplitude.

Returns:

Reconstructed amplitude image.

Return type:

Optional[np.ndarray]

property recon_amp_full: numpy.ndarray

Returns the reconstructed amplitude after applying Gaussian filter. This is the full recon amplitude with any padding.

Returns:

Reconstructed amplitude image.

Return type:

Optional[np.ndarray]

property best_amp: numpy.ndarray

Returns the best amplitude after applying Gaussian filter. This is the cropped best amplitude without padding.

Returns:

Best amplitude image.

Return type:

np.ndarray

property best_amp_full: numpy.ndarray

Returns the best amplitude after applying Gaussian filter. This is the full amplitude with padding.

Returns:

Best amplitude image.

Return type:

np.ndarray

property gaussian_sigma: float

Returns the Gaussian sigma value.

Returns:

Gaussian sigma value.

Return type:

float

property scope: Microscope

Returns the microscope object.

Returns:

Microscope object.

Return type:

Optional[Microscope]

property device: str | torch.device

Returns the device used for computation.

Returns:

Device for computation.

Return type:

str

property guess_phase: torch.Tensor

Returns the guess phase used to pre-train the DIP.

Returns:

Guess phase tensor.

Return type:

Optional[Tensor]

property guess_amp: torch.Tensor

Returns the guess amplitude used to pre-train the DIP or if solve_amp is False.

Returns:

Guess amplitude tensor.

Return type:

Tensor

property input_DIP: torch.Tensor | None

Returns the noise used as input for one or both DIPs.

Returns:

Input noise tensor.

Return type:

Optional[Tensor]

reconstruct(num_iter, model=None, num_pretrain_iter=0, solve_amp=False, solve_amp_scale=True, guess_amp=None, LRs={}, scheduler_type=None, save=False, name=None, save_dir=None, noise_frac=None, guess_phase='SITIE', input_DIP='SITIE', reset=True, print_every=-1, verbose=1, store_iters_every=-1, qc=None, pad=None, **kwargs)[source]

Performs the reconstruction process.

Parameters:
  • num_iter (int) – Number of iterations for reconstruction.

  • model (Optional[Union[nn.Module, list[nn.Module]]], optional) – Model or list of models for DIP.

  • num_pretrain_iter (int, optional) – Number of pretraining iterations.

  • solve_amp (bool, optional) – Whether to solve for amplitude.

  • solve_amp_scale (bool, optional) – Whether to solve amplitude scale.

  • guess_amp (Optional[Union[float, np.ndarray]], optional) – Guess amplitude.

  • LRs (dict, optional) – Learning rates for optimization.

  • scheduler_type (Optional[str], optional) – Type of learning rate scheduler.

  • save (bool, optional) – Whether to save results.

  • name (Optional[str], optional) – Name for the saved results.

  • save_dir (Optional[os.PathLike], optional) – Directory to save results.

  • noise_frac (Optional[float], optional) – Fraction of noise to add.

  • guess_phase (Union[str, np.ndarray, None], optional) – Guess phase or method to obtain it.

  • reset (bool, optional) – Whether to reset the model.

  • print_every (int, optional) – Frequency of printing progress.

  • verbose (int, optional) – Verbosity level.

  • store_iters_every (int, optional) – Frequency of storing iterations.

  • qc (Optional[any], optional) – Quality control object.

  • **kwargs – Additional keyword arguments for scheduler parameters.

Return type:

Self

get_TFs()[source]

Returns a tensor containing the transfer functions according to self.scope and self.defavls

show_best(crop=5)[source]
show_prediction()[source]

Show the predicted image that is compared to the input image.

Return type:

None

show_final(crop=5)[source]

Show the phase and induction of the final iteration.

Parameters:
  • crop (int, optional) – Amount to crop off of induction maps before displaying; often

  • 5. (necessary in order to avoid edge artifacts. Defaults to)

Return type:

None

visualize(crop=5)[source]

Plot the best reconstructed phase and induction maps.

Parameters:
  • crop (int, optional) – Amount to crop off of induction maps before displaying; often

  • 5. (necessary in order to avoid edge artifacts. Defaults to)

save_results(iter_ind=None, save_dir=None, name=None, overwrite=False)[source]

Save the recontructed phase, Bx, By, and color images. # TODO add saving amplitude and phase_E

Parameters:
  • iter_ind (int, optional) – Index to save. Defaults to None which saves best phase.

  • save_dir (os.PathLik], optional) – Directory to save in. Defaults to None

  • self.save_dir. (which saves in)

  • name (str, optional) – Name to prepend saved files. Defaults to None.

  • overwrite (bool, optional) – Whether or not to overwrite files. Defaults to False.

Deep image prior model

class PyLorentz.phase.DIP_NN.DIP_NN(num_images=1, nb_filters=16, use_dropout=False, batch_norm=False, upsampling_mode='nearest')[source]

Bases: Module

Autoencoder for reconstructing object wave and amplitude of LTEM images.

Parameters:
  • num_images (int) – int, number of input channels, equal to number of images in TFS.

  • nb_filters (int) – int, number of filters in the first convolutional block.

  • use_dropout (bool) – bool, whether to use dropout in the inner layers.

  • batch_norm (bool) – bool, whether to use batch normalization after each convolutional layer.

  • upsampling_mode (str) – str, “bilinear” or “nearest” upsampling method.

__init__(num_images=1, nb_filters=16, use_dropout=False, batch_norm=False, upsampling_mode='nearest')[source]
forward(x)[source]

Forward pass for the DIP network.

Parameters:

x (Tensor) – Input tensor.

Return type:

Tensor

Returns:

Output tensor after passing through the network.

class PyLorentz.phase.DIP_NN.conv2dblock(nb_layers, input_channels, output_channels, kernel_size=3, stride=1, padding=1, use_batchnorm=False, lrelu_a=0.01, dropout_=0, last_sigmoid=False, last_tanh=False, last_skipReLU=False)[source]

Bases: Module

A block consisting of convolutional layers with optional batch normalization and dropout.

Parameters:
  • nb_layers (int) – int, number of convolutional layers.

  • input_channels (int) – int, number of input channels.

  • output_channels (int) – int, number of output channels.

  • kernel_size (int) – int, size of the convolutional kernel.

  • stride (int) – int, stride of the convolution.

  • padding (int) – int, padding for the convolution.

  • use_batchnorm (bool) – bool, whether to use batch normalization.

  • lrelu_a (float) – float, negative slope for the Leaky ReLU activation.

  • dropout – float, dropout rate.

  • last_sigmoid (bool) – bool, whether to use a sigmoid activation on the last layer.

  • last_tanh (bool) – bool, whether to use a tanh activation on the last layer.

  • last_skipReLU (bool) – bool, whether to skip ReLU activation on the last layer.

__init__(nb_layers, input_channels, output_channels, kernel_size=3, stride=1, padding=1, use_batchnorm=False, lrelu_a=0.01, dropout_=0, last_sigmoid=False, last_tanh=False, last_skipReLU=False)[source]
forward(x)[source]

Forward pass for the conv2dblock.

Parameters:

x (Tensor) – Input tensor.

Return type:

Tensor

Returns:

Output tensor after passing through the block.

class PyLorentz.phase.DIP_NN.upsample_block(input_channels, output_channels, scale_factor=2, mode='bilinear')[source]

Bases: Module

Upsampling block using interpolation followed by a convolution.

Parameters:
  • input_channels (int) – int, number of input channels.

  • output_channels (int) – int, number of output channels.

  • scale_factor (int) – int, factor by which to scale the input.

  • mode (str) – str, interpolation mode, either “bilinear” or “nearest”.

__init__(input_channels, output_channels, scale_factor=2, mode='bilinear')[source]
forward(x)[source]

Forward pass for the upsample_block.

Parameters:

x (Tensor) – Input tensor.

Return type:

Tensor

Returns:

Output tensor after upsampling and convolution.

PyLorentz.phase.DIP_NN.rng_seed(seed)[source]

Set the random seed for reproducibility.

Parameters:

seed (int) – The seed value to use.

Return type:

None

Returns:

None

PyLorentz.phase.DIP_NN.weight_reset(m)[source]

Reset the weights of a given module.

Parameters:

m (Module) – The neural network module to reset.

Return type:

None

Returns:

None