toupy.utils package

Submodules

toupy.utils.FFT_utils module

toupy.utils.FFT_utils.fastfftn(input_array, **kwargs)[source]

Auxiliary function to use pyFFTW. It does the align, planning and apply FFTW transform

Parameters

input_array (array_like) – Array to be FFTWed

Returns

fftw_array – Fourier transformed array

Return type

array_like

toupy.utils.FFT_utils.fastifftn(input_array, **kwargs)[source]

Auxiliary function to use pyFFTW. It does the align, planning and apply inverse FFTW transform

Parameters

input_array (array_like) – Array to be FFTWed

Returns

ifftw_array – Inverse Fourier transformed array

Return type

array_like

toupy.utils.FFT_utils.is_power2(num)[source]

States if a number num is a power of two

toupy.utils.FFT_utils.nextpow2(number)[source]

Find the next power 2 of number for FFT

toupy.utils.FFT_utils.nextpoweroftwo(number)[source]

Returns next power of two following number

toupy.utils.FFT_utils.padfft(input_array, pad_mode='reflect')[source]

Auxiliary function to pad arrays for Fourier transforms. It accepts 1D and 2D arrays.

Parameters
  • input_array (array_like) – Array to be padded

  • mode (str) – Padding mode to treat the array borders. See numpy.pad for modes. The default value is reflect.

Returns

  • array_pad (array_like) – Padded array

  • N_pad (array_like) – padded frequency coordinates

  • padw (int, list of ints) – pad width

toupy.utils.FFT_utils.padrightside(nbins)[source]

Returns pad_width for padding at the right side given a value of nbins The pad_width is calculated with next_fast_len function from PyFFTW package

toupy.utils.FFT_utils.padwidthbothsides(nbins)[source]

Returns pad_width for padding both sides given a value of nbins

toupy.utils.array_utils module

toupy.utils.array_utils.create_circle(inputimg)[source]

Create circle with apodized edges

Parameters

inputimg (array_like) – Input image from which to calculate the circle

Returns

t – Array containing the circle

Return type

array_like

toupy.utils.array_utils.create_mask_borders(tomogram, mask_array, threshold=4e-07)[source]

Create mask for border of tomographic volume

Parameters
  • tomogram (array_like) – Input volume

  • mask (bool array_like) – Input mask

  • threshold (float, optional) – Threshold value. The default value is 4e-7.

Returns

mask_array – Masked array

Return type

array_like

toupy.utils.array_utils.crop(input_array, delcropx, delcropy)[source]

Crop images

Parameters
  • input_array (array_like) – Input image to be cropped

  • delcropx (int) – amount of pixel to be cropped in x

  • delcropy (int) – amount of pixel to be cropped in y

Returns

Cropped image

Return type

array_like

toupy.utils.array_utils.cropROI(input_array, roi=[])[source]

Crop ROI

Parameters
  • input_array (array_like) – Input image to be cropped

  • roi (list of int) – ROI of interest. roi should be [top, bottom, left, right]

Returns

Cropped image

Return type

array_like

toupy.utils.array_utils.fract_hanning(outputdim, unmodsize)[source]

Creates a square hanning window if unmodsize = 0 (or ommited), otherwise the output array will contain an array of ones in the center and cosine modulation on the edges, the array of ones will have DC in upper left corner.

Parameters
  • outputdim (int) – Size of the output array

  • unmodsize (int) – Size of the central array containing no modulation.

Returns

Square array containing a fractional separable Hanning window with DC in upper left corner.

Return type

array_like

toupy.utils.array_utils.fract_hanning_pad(outputdim, filterdim, unmodsize)[source]

Creates a square hanning window if unmodsize = 0 (or ommited), otherwise the output array will contain an array of ones in the center and cosine modulation on the edges, the array of ones will have DC in upper left corner.

Parameters
  • outputdim (int) – Size of the output array

  • filterdim (int) – Size of filter (it will zero pad if filterdim < outputdim)

  • unmodsize (int) – Size of the central array containing no modulation.

Returns

Square array containing a fractional separable Hanning window with DC in upper left corner.

Return type

array_like

toupy.utils.array_utils.gauss_kern(size, sizey=None)[source]

Returns a normalized 2D gauss kernel array for convolutions

Parameters
  • size (int) – Size of the kernel

  • sizey (int, optional) – Vertical size of the kernel if not squared

Returns

Normalized kernel

Return type

array_like

Notes

from: http://scipy.org/Cookbook/SignalSmooth

toupy.utils.array_utils.hanning_apod1D(window_size, apod_width)[source]

Create 1D apodization window using Hanning window

Parameters
  • window_size (int) – Window size

  • apod_width (int) – Apodization width

Returns

hannwindow1D – 1D Hanning window for the apodization

Return type

array_like

toupy.utils.array_utils.hanning_apodization(window_size, apod_width)[source]

Create apodization window using Hanning window

Parameters
  • window_size (tuple) – Window size

  • apod_width (int) – Apodization width

Returns

hannwindow2D – 2D Hanning window for the apodization

Return type

array_like

toupy.utils.array_utils.mask_borders(imgarray, mask_array, threshold=4e-07)[source]

Mask borders using the gradient

Parameters
  • imgarray (array_like) – Input image

  • mask_array (bool array_like) – Input mask

  • threshold (float, optional) – Threshold value. The default value is 4e-7.

Returns

mask_array – Masked array

Return type

array_like

toupy.utils.array_utils.normalize_array(input_array)[source]

Normalize the input array

toupy.utils.array_utils.padarray_bothsides(input_array, newshape, padmode='edge')[source]

Pad array in both sides

Parameters
  • input_array (array_like) – Input array

  • newshape (tuple) – New shape of the array to be padded

  • padmode (str) – Padding mode. The default is edge

Returns

Padded array

Return type

array_like

toupy.utils.array_utils.polynomial1d(x, order=1, w=1)[source]

Generates a 1D orthonormal polynomial base.

Parameters
  • x (array_like) – Array containing the values of x for the polynomial

  • order (int, optional) – Order of the polynomial. The defaul value is 1.

  • w (int, optional) – Weights of the coefficients. The defaul value is 1.

Returns

polyseries – Orthonormal polymonial up to order

Return type

array_like

Note

Inspired by legendrepoly1D_2.m created by Manuel Guizar in March 10,2009

toupy.utils.array_utils.projectpoly1d(func1d, order=1, w=1)[source]

Projects a 1D function onto orthonormalized base

Parameters
  • func1d (array_like) – Array containing the values of the 1D function

  • order (int, optional) – Order of the polynomial. The defaul value is 1.

  • w (int, optional) – Weights of the coefficients. The defaul value is 1.

Returns

projfunc1d – Projected 1D funtion on orthonormal base

Return type

array_like

Note

Inspired by projectleg1D_2.m created by Manuel Guizar in March 10,2009

toupy.utils.array_utils.radtap(X, Y, tappix, zerorad)[source]

Creates a central cosine tapering for beam. It receives the X and Y coordinates, tappix is the extent of tapering, zerorad is the radius with no data (zeros).

toupy.utils.array_utils.replace_bad(input_stack, list_bad=[], temporary=False)[source]

correcting bad projections before unwrapping

Parameters
  • input_stack (array_like) – Stack of projections

  • list_bad (list) – List of bad projections

  • temporary (bool) – If False, the projection will be interpolated with the previous and after projections. If True, the projection will be replaced by the previous projection.

toupy.utils.array_utils.round_to_even(x)[source]

Round number x to next even number

toupy.utils.array_utils.sharpening_image(input_image, filter_size=3, alpha=30)[source]

Sharpen image with a median filter

Parameters
  • input_image (array_like) – Image to be sharpened

  • filter_size (int) – Size of the filter

  • alpha (float) – Strength of the sharpening

Returns

Sharpened image

Return type

array_like

toupy.utils.array_utils.smooth1d(x, window_len=11, window='hanning')[source]

Smooth the data using a window with requested size.

This method is based on the convolution of a scaled window with the signal. The signal is prepared by introducing reflected copies of the signal (with the window size) in both ends so that transient parts are minimized in the begining and end part of the output signal.

Parameters
  • x (array_like) – The input signal

  • window_len (int, optional,) – The dimension of the smoothing window; should be an odd integer. The default value is 11.

  • window (str, optional) – The type of window from flat, hanning, hamming, bartlett, blackman flat window will produce a moving average smoothing.

Returns

y – The smoothed signal

Return type

array_like

Example

>>> import numpy as np
>>> t=np.linspace(-2,2,0.1)
>>> x=np.sin(t)+np.random.randn(len(t))*0.1
>>> y=smooth(x)

Notes

see also: numpy.hanning, numpy.hamming, numpy.bartlett, numpy.blackman, numpy.convolve

scipy.signal.lfilter

Adapted from : https://scipy-cookbook.readthedocs.io/items/SignalSmooth.html from: http://scipy.org/Cookbook/SignalSmooth

toupy.utils.array_utils.smooth2d(im, n, ny=None)[source]

Blurs the image by convolving with a gaussian kernel of typical

size n. The optional keyword argument ny allows for a different size in the y direction.

Parameters
  • im (array_like) – Input image

  • n (int, optional) – Typical size of the gaussian kernel

  • n – Size in the y direction if not squared

Returns

improc – Smoothed image

Return type

array_like

Notes

from: http://scipy.org/Cookbook/SignalSmooth

toupy.utils.array_utils.smooth_image(input_image, filter_size=3)[source]

Smooth image with a median filter

Parameters
  • input_image (array_like) – Image to be smoothed

  • filter_size (int) – Size of the filter

Returns

Smoothed image

Return type

array_like

toupy.utils.array_utils.sort_array(input_array, ref_array)[source]

Sort array based on another array

Parameters
  • input_array (array_like) – Array to be sorted

  • ref_array (array_like) – Array on which the sorting will be based

Returns

  • sorted_input_array (array_like) – Sorted input array

  • sorted_ref_array (array_like) – Sorted reference array

toupy.utils.converter_utils module

toupy.utils.converter_utils.convert_to_beta(input_img, energy, voxelsize, apply_log=False)[source]

Converts the image gray-levels from amplitude to beta

toupy.utils.converter_utils.convert_to_delta(input_img, energy, voxelsize)[source]

Converts the image gray-levels from phase-shifts to delta

toupy.utils.converter_utils.convert_to_mu(input_img, wavelen)[source]

Converts the image gray-levels from absoption index Beta to linear attenuation coefficient mu

toupy.utils.converter_utils.convert_to_rhoe(input_img, wavelen)[source]

Converts the image gray-levels from delta to electron density

toupy.utils.converter_utils.convert_to_rhom(input_img, wavelen, A, Z)[source]

Converts the image gray-levels from electron density to mass density

toupy.utils.fit_utils module

toupy.utils.fit_utils.model_erf(t, *coeffs)[source]

Model for the erf fitting

P0 + P1*t + (P2/2)*(1-erf(sqrt(2)*(x-P3)/(P4)))

Parameters
  • t (ndarray) – Input coordinates

  • coeffs[0] (float) – P0 (noise)

  • coeffs[1] (float) – P1 (linear term)

  • coeffs[2] (float) – P2 (Maximum amplitude)

  • coeffs[3] (float) – P3 (center)

  • coeffs[4] (float) – P4 (width)

Returns

Array containing the model

Return type

ndarray

toupy.utils.fit_utils.model_tanh(t, *coeffs)[source]

Model for the erf fitting

P0 + P1*t + (P2/2)*(1-tanh(sqrt(2)*(x-P3)/P4))

Parameters
  • t (ndarray) – Input coordinates

  • coeffs[0] (float) – P0 (noise)

  • coeffs[1] (float) – P1 (linear term)

  • coeffs[2] (float) – P2 (Maximum amplitude)

  • coeffs[3] (float) – P3 (center)

  • coeffs[4] (float) – P4 (width)

Returns

Array containing the model

Return type

ndarray

toupy.utils.fit_utils.residuals_erf(coeffs, y, t)[source]

Residuals for the least-squares optimization coeffs as the ones of the model erf function

Parameters
  • y (ndarray) – The data

  • t (ndarray) – Input coordinates

Returns

Residuals

Return type

ndarray

toupy.utils.fit_utils.residuals_tanh(coeffs, y, t)[source]

Residuals for the least-squares optimization coeffs as the ones of the model tanh function

Parameters
  • y (ndarray) – The data

  • t (ndarray) – Input coordinates

Returns

Residuals

Return type

ndarray

toupy.utils.funcutils module

toupy.utils.funcutils.checkhostname(func)[source]

Check if running in OAR, if not, exit.

toupy.utils.funcutils.deprecated(func)[source]

This is a decorator which can be used to mark functions as deprecated. It will result in a warning being emitted when the function is used.

toupy.utils.funcutils.downloadURL(url, fname)[source]

Download file from a URL.

Parameters
  • url (str) – URL address

  • fname (str) – Filename as to be stored

toupy.utils.funcutils.downloadURLfile(url, filename)[source]

Download and save file from a URL.

Parameters
  • url (str) – URL address

  • fname (str) – Filename as to be stored

toupy.utils.funcutils.progbar(curr, total, textstr='')[source]

Create a progress bar for for-loops.

Parameters
  • curr (int) – Current value to shown in the progress bar

  • total (int) – Maximum size of the progress bar.

  • textstr (str) – String to be shown at the right side of the progress bar

class toupy.utils.funcutils.switch(value)[source]

Bases: object

This class provides the functionality of switch or case in other languages than python. This mimics the functionality of switch in Python

__iter__()[source]

Return the match method once, then stop

match(*args)[source]

Indicate whether or not to enter a case suite

toupy.utils.plot_utils module

class toupy.utils.plot_utils.RegisterPlot(**params)[source]

Bases: object

Display plots during registration

plotshorizontal(recons, sinoorig, sinocurr, sinocomp, deltaslice, metric_error, count)[source]

Display plots during the horizontal registration

plotsvertical(proj, lims, vertfluctinit, vertfluctcurr, deltastack, metric_error, count)[source]

Display plots during the vertical registration

updatehorizontal()[source]

Update the plot canvas during horizontal registration

updatevertical()[source]

Update the plot canvas during vertical registration

class toupy.utils.plot_utils.ShowProjections[source]

Bases: object

Show projections and probe

static probe2HSV(probe)[source]

Special tricks for the probe display in HSV

show_projections(obj, probe, idxp)[source]

Show the object and the probe :param obj: Object to show :type obj: ndarray :param probe: Probe to show :type probe: ndarray :param idxp: Projection number :type idxp: int

update_show()[source]

Update the canvas

toupy.utils.plot_utils.animated_image(stack_array, *args)[source]

Iterative plot of the images using animation module of Matplotlib

Parameters
  • stack_array (ndarray) – Array containing the stack of images to animate. The first index corresponds to the image number in the sequence of images.

  • args[0] (list of ints) – Row limits to display

  • args[1] (list of ints) – Column limits to display

toupy.utils.plot_utils.autoscale_y(ax, margin=0.1)[source]

This function rescales the y-axis based on the data that is visible given the current xlim of the axis.

Parameters
  • ax (object) – A matplotlib axes object

  • margin (float) – The fraction of the total height of the y-data to pad the upper and lower ylims

toupy.utils.plot_utils.display_slice(recons, colormap='bone', vmin=None, vmax=None)[source]

Display tomographic slice

Parameters
  • recons (array_like) – Tomographic slice

  • colormap (str, optional) – Colormap name. The default value is bone

  • vmin (float, None) – Minimum gray-level. The default value is None

  • vmax (float, None) – Maximum gray-level. The default value is None

toupy.utils.plot_utils.isnotebook()[source]

Check if code is executed in the IPython notebook. This is important because jupyter notebook does not support iterative plots

toupy.utils.plot_utils.iterative_show(stack_array, limrow=[], limcol=[], airpixel=[], onlyroi=False, colormap='bone', vmin=None, vmax=None)[source]

Iterative plot of the images

Parameters
  • stack_array (ndarray) – Array containing the stack of images to animate. The first index corresponds to the image number in the sequence of images.

  • limrow (list of ints) – Limits of rows in the format [begining, end]

  • limcol (list of ints) – Limits of cols in the format [begining, end]

  • airpixel (list of ints) – Position of pixel in the air/vacuum

  • onlyroi (bool) – If True, it displays only the ROI. If False, it displays the entire image.

  • colormap (str, optional) – Colormap name. The default value is bone

  • vmin (float, None, optional) – Minimum gray-level. The default value is None

  • vmax (float, None, optional) – Maximum gray-level. The default value is None

toupy.utils.plot_utils.plot_checkangles(angles)[source]

Plot the angles for each projections and the derivatives to check for anomalies

Parameters

angles (array_like) – Array of angles

toupy.utils.plot_utils.show_linearphase(image, mask, *args)[source]

Show projections and probe