toupy.restoration package

Submodules

toupy.restoration.GUI_tracker module

class toupy.restoration.GUI_tracker.AmpTracker(fig, ax1, ax2, X1, **params)[source]

Bases: PhaseTracker

Widgets for the phase ramp removal

Note

It inherits most of the functionality of PhaseTracker, except the ones related to amplitude projections rather than to phase projections. Please, refert to the docstring of PhaseTracker for further description.

apply_all_masks(event)[source]

Apply the linear air correction using current mask and log to all projections

apply_mask(event)[source]

Apply the air correction using current mask and apply the log

class toupy.restoration.GUI_tracker.PhaseTracker(fig, ax1, ax2, X1, **params)[source]

Bases: object

Widgets for the phase ramp removal

add_mask(event)[source]

Add the mask to the plot

apply_all_masks(event)[source]

Apply the linear phase correction using current mask to all projections

apply_mask(event)[source]

Apply the linear phase correction using current mask

cmvmax(val)[source]

Set the vmax equals to val on colormap

cmvmin(val)[source]

Set the vmin equals to val on colormap

down(event)[source]

Move projection number down using button Prev

draw_mask(event)[source]

Draw the mask using roipoly

key_event(event)[source]

Move projection number up/down using right/left arrows in the keyboard

load_masks(event)[source]

Load masks from file

mask_all(event)[source]

Use the same mask for all projections

onclose(event)[source]

Close the figure

onscroll(event)[source]

Move projection number up/down using the mouse scroll wheel

play(event)[source]

Plot one project after the other (play)

remove_all_mask(event)[source]

Remove all the masks

remove_mask(event)[source]

Remove the current selected area from the mask

remove_ramp(event)[source]

Remove linear phase ramp

remove_rampall(event)[source]

Remove linear phase ramp of all

save_masks(event)[source]

Save mask to file

submit(text)[source]

Textbox submit

unwrapping_all(event)[source]

Unwrap phase of all projections

unwrapping_phase(event)[source]

Unwrap phase

up(event)[source]

Move projection number up using button Next

update()[source]

Update the plot canvas

toupy.restoration.GUI_tracker.gui_plotamp(stack_objs, **params)[source]

GUI for the air removal from amplitude projections

Parameters
  • stack_objs (array_like) – Stack of amplitude projections

  • params (dict) – Dictionary of additonal parameters

  • params["autosave"] (bool) – Save the projections once load without asking

  • params["correct_bad"] (bool) – If true, it will interpolate bad projections. The numbers of projections to be corrected is given by params[“bad_projs”].

  • params["bad_projs"] (list of ints) – List of projections to be interpolated. It starts at 0.

  • params["vmin"] (float, None) – Minimum value of gray-level to display

  • params["vmax"] (float, None) – Maximum value of gray-level to display

Returns

stack_ampcorr – Stack of corrected amplitude projections

Return type

array_like

toupy.restoration.GUI_tracker.gui_plotphase(stack_objs, **params)[source]

GUI for the phase ramp removal from phase projections

Parameters
  • stack_objs (array_like) – Stack of phase projections

  • params (dict) – Dictionary of additonal parameters

  • params["autosave"] (bool) – Save the projections once load without asking

  • params["correct_bad"] (bool) – If true, it will interpolate bad projections. The numbers of projections to be corrected is given by params[“bad_projs”].

  • params["bad_projs"] (list of ints) – List of projections to be interpolated. It starts at 0.

  • params["vmin"] (float, None) – Minimum value of gray-level to display

  • params["vmax"] (float, None) – Maximum value of gray-level to display

Returns

stack_phasecorr – Stack of corrected phase projections

Return type

array_like

toupy.restoration.derivativetools module

toupy.restoration.derivativetools.calculate_derivatives(stack_array, roiy, roix, shift_method='fourier')[source]

Compute projection derivatives

Parameters
  • stack_array (array_like) – Input stack of arrays to calculate the derivatives

  • roix (tuple) – Limits of the area on which to calculate the derivatives

  • roiy (tuple) – Limits of the area on which to calculate the derivatives

  • shift_method (str) – Name of the shift method to use. For the available options, please see ShiftFunc() in toupy.registration

Returns

aligned_diff – Stack of derivatives of the arrays along the horizontal direction

Return type

array_like

toupy.restoration.derivativetools.calculate_derivatives_fft(stack_array, roiy, roix, n_cpus=- 1)[source]

Compute projection derivatives using FFTs

Parameters
  • stack_array (array_like) – Input stack of arrays to calculate the derivatives

  • roix (tuple) – Limits of the area on which to calculate the derivatives

  • roiy (tuple) – Limits of the area on which to calculate the derivatives

  • n_cpus (int) – The number of cpus for parallel computing. If n_cpus<0, the number of cpus will be determined by os.cpu_counts()

Returns

aligned_diff – Stack of derivatives of the arrays along the horizontal direction

Return type

array_like

toupy.restoration.derivativetools.chooseregiontoderivatives(stack_array, **params)[source]

Choose the region to be unwrapped

toupy.restoration.derivativetools.derivatives(input_array, shift_method='fourier')[source]

Calculate the derivative of an image

Parameters
  • input_array (array_like) – Input image to calculate the derivatives

  • shift_method (str) – Name of the shift method to use. For the available options, please see ShiftFunc() in toupy.registration

Returns

diffimg – Derivatives of the images along the row direction

Return type

array_like

toupy.restoration.derivativetools.derivatives_fft(input_img, symmetric=True, n_cpus=- 1)[source]

Calculate the derivative of an image using FFT along the horizontal direction

Parameters
  • input_array (array_like) – Input image to calculate the derivatives

  • symmetric (bool) – If True, symmetric difference is calculated

  • n_cpus (int) – The number of cpus for parallel computing. If n_cpus<0, the number of cpus will be determined by os.cpu_counts()

Returns

diffimg – Derivatives of the images along the row direction

Return type

array_like

toupy.restoration.derivativetools.derivatives_sino(input_sino, shift_method='fourier')[source]

Calculate the derivative of the sinogram

Parameters
  • input_array (array_like) – Input sinogram to calculate the derivatives

  • shift_method (str) – Name of the shift method to use. For the available options, please see ShiftFunc() in toupy.registration

Returns

diffsino – Derivatives of the sinogram along the radial direction

Return type

array_like

toupy.restoration.derivativetools.gradient_axis(x, axis=- 1)[source]

Compute the gradient (keeping dimensions) along one dimension only. By default, the axis is -1 (diff along columns).

toupy.restoration.ramptools module

toupy.restoration.ramptools.rmair(image, mask)[source]

Correcting amplitude factor using the mask from the phase ramp removal considering only pixels where mask is unity, arrays have center on center of array

Parameters
  • image (array_like) – Amplitude-contrast image

  • mask (bool) – Boolean array with indicating the locations from where the air value should be obtained

Returns

normalizedimage – Image normalized by the air values

Return type

array_like

toupy.restoration.ramptools.rmlinearphase(image, mask)[source]

Removes linear phase from object

Parameters
  • image (array_like) – Input image

  • mask (bool) – Boolean array with ones where the linear phase should be computed from

Returns

im_output – Linear ramp corrected image

Return type

array_like

toupy.restoration.ramptools.rmphaseramp(a, weight=None, return_phaseramp=False)[source]

Auxiliary functions to attempt to remove the phase ramp in a two-dimensional complex array a.

Parameters
  • a (array_like) – Input image as complex 2D-array.

  • weight (array_like, str, optional) – Pass weighting array or use 'abs' for a modulus-weighted phaseramp and Non for no weights.

  • return_phaseramp (bool, optional) – Use True to get also the phaseramp array p.

Returns

  • out (array_like) – Modified 2D-array, out=a*p

  • p (array_like, optional) – Phaseramp if return_phaseramp = True, otherwise omitted

Note

Function forked from Ptypy.plot_utils (https://github.com/ptycho/ptypy) and ported to Python 3.

Examples

>>> b = rmphaseramp(image)
>>> b, p = rmphaseramp(image , return_phaseramp=True)

toupy.restoration.roipoly module

Draw polygon regions of interest (ROIs) in matplotlib images, similar to Matlab’s roipoly function. See the file example.py for an application. Created by Joerg Doepfert 2014 based on code posted by Daniel Kornhauser.

class toupy.restoration.roipoly.roipoly(fig=[], ax=[], roicolor='b')[source]

Bases: object

displayMean(currentImage, **textkwargs)[source]
displayROI(**linekwargs)[source]
getMask(currentImage)[source]

toupy.restoration.unwraptools module

toupy.restoration.unwraptools.chooseregiontounwrap(stack_array, threshold=5000, parallel=False, ncores=1)[source]

Choose the region to be unwrapped

Parameters
  • stack_array (ndarray) – A 3-dimensional array containing the stack of projections to be unwrapped.

  • threshold (int, optional) – The threshold of the number of acceptable phase residues. (Default = 5000)

  • parallel (bool, optional) – If True, multiprocessing and threading will be used. (Default = False)

Returns

  • rx, ry (tuple) – Limits of the area to be unwrapped

  • airpix (tuple) – Position of the pixel which should contains only air/vacuum

toupy.restoration.unwraptools.distance(pixel1, pixel2)[source]

Return the Euclidean distance of two pixels.

Example

>>> distance(np.arange(1,10),np.arange(2,11))
3.0
toupy.restoration.unwraptools.get_charge(residues)[source]

Get the residues charges

Parameters

residues (ndarray) – A 2-dimensional array containing the with residues

Returns

  • posres (array_like) – Positions of the residues with positive charge

  • negres (array_like) – Positions of the residues with negative charge

toupy.restoration.unwraptools.phaseresidues(phimage)[source]

Calculates the phase residues 1 for a given wrapped phase image.

Parameters

phimage (ndarray) – A 2-dimensional array containing the phase-contrast images with gray-level in radians

Returns

residues – A 2-dimensional array containing the map of residues (valued +1 or -1)

Return type

ndarray

Note

Note that by convention the positions of the phase residues are marked on the top left corner of the 2 by 2 regions as shown below:

graph g {
    node [shape=plaintext];
    active -- right [label="  res4   "];
    right  -- belowright [label=" res3 "];
    below  -- belowright [label=" res2 "];
    below  -- active [label=" res1 "];
    { rank=same; active right }
    { rank=same; belowright below }
}

Inspired by PhaseResidues.m created by B.S. Spottiswoode on 07/10/2004 and by find_residues.m created by Manuel Guizar - Sept 27, 2011

References

1

R. M. Goldstein, H. A. Zebker and C. L. Werner, Radio Science 23, 713-720 (1988).

toupy.restoration.unwraptools.phaseresiduesStack(stack_array, threshold=5000)[source]

Calculate the map of residues on the stack

Parameters

stack_array (ndarray) – A 3-dimensional array containing the stack of projections from which to calculate the phase residues.

Returns

  • resmap (array_like) – Phase residue map

  • posres (tuple) – Positions of the residues in the format posres = (yres,xres)

toupy.restoration.unwraptools.phaseresiduesStack_parallel(stack_array, threshold=1000, ncores=2)[source]

Calculate the map of residues on the stack

Parameters
  • stack_array (ndarray) – A 3-dimensional array containing the stack of projections from which to calculate the phase residues.

  • threshold (int, optional) – The threshold of the number of acceptable phase residues. (Default = 5000)

Returns

  • resmap (array_like) – Phase residue map

  • posres (tuple) – Positions of the residues in the format posres = (yres,xres)

toupy.restoration.unwraptools.unwrapping_phase(stack_phasecorr, rx, ry, airpix, **params)[source]

Unwrap the phase of the projections in a stack.

Parameters
  • stack_phasecorr (ndarray) – A 3-dimensional array containing the stack of projections to be unwrapped

  • rx (tuple or list of ints) – Limits of the are to be unwrapped in x and y

  • ry (tuple or list of ints) – Limits of the are to be unwrapped in x and y

  • airpix (tuple or list of ints) – Position of pixel in the air/vacuum area

  • params (dict) – Dictionary of additional parameters

  • params["vmin"] (float, None) – Minimum value for the gray level at each display

  • params["vmin"] – Maximum value for the gray level at each display

Returns

stack_unwrap – A 3-dimensional array containing the stack of unwrapped projections

Return type

ndarray

Note

It uses the phase unwrapping algorithm by Herraez et al. 2 implemented in Scikit-Image (https://scikit-image.org).

References

2

Miguel Arevallilo Herraez, David R. Burton, Michael J. Lalor, and Munther A. Gdeisat, “Fast two-dimensional phase-unwrapping algorithm based on sorting by reliability following a noncontinuous path”, Journal Applied Optics, Vol. 41, No. 35, pp. 7437, 2002

toupy.restoration.unwraptools.wrap(phase)[source]

Wrap a scalar value or an entire array to [-0.5, 0.5).

Parameters

phase (float or array_like) – The value or signal to wrapped.

Returns

Wrapped value or array

Return type

float or array

Note

Created by Sebastian Theilenberg, PyMRR, which is available at Github repository: https://github.com/theilen/PyMRR.git

toupy.restoration.unwraptools.wraptopi(phase, endpoint=True)[source]

Wrap a scalar value or an entire array

Parameters
  • phase (float or array_like) – The value or signal to wrapped.

  • endpoint (bool, optional) – If endpoint=False, the scalar value or array is wrapped to [-pi, pi), whereas if endpoint=True, it is wrapped to (-pi, pi]. The default value is endpoint=True

Returns

Wrapped value or array

Return type

float or array

Example

>>> import numpy as np
>>> wraptopi(np.linspace(-np.pi,np.pi,7),endpoint=True)
array([ 3.14159265, -2.0943951 , -1.04719755, -0.        ,  1.04719755,
    2.0943951 ,  3.14159265])
>>> wraptopi(np.linspace(-np.pi,np.pi,7),endpoint=False)
array([-3.14159265, -2.0943951 , -1.04719755,  0.        ,  1.04719755,
    2.0943951 , -3.14159265])

toupy.restoration.vorticestools module

toupy.restoration.vorticestools.cart2pol(x, y)[source]

Change from cartesian to polar coordinates

Parameters
  • x (array_like) – Values in cartesian coordinates

  • y (array_like) – Values in cartesian coordinates

Returns

rho, phi – Values in polar coordinates

Return type

array_like

toupy.restoration.vorticestools.get_object_novort(img_phase, residues)[source]

Remove the vortices from the phase projections

Parameters
  • img_phase (array_like) – Phase image with vortices to be removed without linear phase ramp

  • residues (array_like) – Residues map

Returns

  • img_phase_novort (array_like) – Phase image without vortices

  • xres, yres (array_like) – Coordinates x and y of the vortices

toupy.restoration.vorticestools.get_probe_novort(img_phase, residues)[source]

Remove the vortices from the probe

Parameters
  • img_phase (array_like) – Probe image with vortices to be removed without linear phase ramp

  • residues (array_like) – Residues map

Returns

  • img_phase_novort (array_like) – Probe image without vortices

  • xres, yres (array_like) – Coordinates x and y of the vortices

toupy.restoration.vorticestools.pol2cart(rho, phi)[source]

Change from polar to cartesian coordinates

Parameters
  • rho (array_like) – Values in polar coordinates

  • phi (array_like) – Values in polar coordinates

Returns

x, y – Values in cartesian coordinates

Return type

array_like

toupy.restoration.vorticestools.rmvortices_object(img_in, to_ignore=100)[source]

Remove phase vortices on the object image ignoring an amount of pixels equals to to_ignore from the borders.

Parameters
  • img_phase (array_like) – Phase image with vortices to be removed.

  • to_ignore (int, optional) – amount of pixels to ignore from the borders.

Returns

  • img_phase_novort (array_like) – Phase image without vortices

  • xres, yres (array_like) – Coordinates x and y of the vortices

Note

An eventual linear phase ramp will be remove from the input image.

toupy.restoration.vorticestools.rmvortices_probe(img_in, to_ignore=100)[source]

Remove phase vortices on the probe image ignoring an amount of pixels equals to to_ignore from the borders.

Parameters
  • img_phase (array_like) – Probe image with vortices to be removed.

  • to_ignore (int, optional) – amount of pixels to ignore from the borders.

Returns

  • img_phase_novort (array_like) – Probe image without vortices

  • xres, yres (array_like) – Coordinates x and y of the vortices

Note

An eventual linear phase ramp will be remove from the input image.