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 FFT-transformed via pyFFTW.
- 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 FFT-transformed via pyFFTW.
- Returns:
ifftw_array – Inverse Fourier transformed array
- Return type:
array_like
- toupy.utils.FFT_utils.nextpow2(number)[source]¶
Find the next power of two at or above
numberfor use with FFTs.
- toupy.utils.FFT_utils.nextpoweroftwo(number)[source]¶
Return the exponent of the next power of two at or above
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.padfor 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.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 circular mask with apodized edges.
- Return type:
ndarray
- 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.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.
- 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:
- 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]¶
Return a normalized 2-D Gaussian kernel array for convolutions.
- Parameters:
- Returns:
Normalized 2-D Gaussian kernel of shape
(2*sizey+1, 2*size+1).- Return type:
ndarray
Notes
Adapted from http://scipy.org/Cookbook/SignalSmooth
- toupy.utils.array_utils.hanning_apod1D(window_size, apod_width)[source]¶
Create 1D apodization window using Hanning window
- toupy.utils.array_utils.hanning_apodization(window_size, apod_width)[source]¶
Create apodization window using Hanning window
- 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 an array to the range
[0, 1].- Parameters:
input_array (array_like) – Input array to normalize.
- Returns:
Array with values scaled linearly to
[0, 1].- Return type:
ndarray
- toupy.utils.array_utils.padarray_bothsides(input_array, newshape, padmode='edge')[source]¶
Pad array in both sides
- toupy.utils.array_utils.polynomial1d(x, order=1, w=1)[source]¶
Generates a 1D orthonormal polynomial base.
- Parameters:
- Returns:
polyseries – Orthonormal polymonial up to order
- Return type:
array_like
Notes
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:
- 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]¶
Create a central cosine tapering mask for beam apodization.
The mask is zero inside the radius
zerorad, rises with a cosine taper overtappixpixels, and is unity beyondzerorad + tappix.- Parameters:
X (ndarray) – 2-D array of horizontal coordinate values (e.g. from
numpy.meshgrid()).Y (ndarray) – 2-D array of vertical coordinate values.
tappix (int or float) – Extent of the cosine taper in pixels.
zerorad (int or float) – Radius (in pixels) inside which the mask is zero.
- Returns:
taperfunc – 2-D apodization mask with the same shape as
XandY, values in[0, 1].- Return type:
ndarray
- toupy.utils.array_utils.replace_bad(input_stack, list_bad=[], temporary=False)[source]¶
correcting bad projections before unwrapping
- toupy.utils.array_utils.sharpening_image(input_image, filter_size=3, alpha=30)[source]¶
Sharpen image with a median filter
- 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:
- Returns:
y – The smoothed signal
- Return type:
array_like
Examples
>>> 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
- toupy.utils.array_utils.smooth2d(im, n, ny=None)[source]¶
Blur an image by convolving with a Gaussian kernel.
- Parameters:
- Returns:
improc – Smoothed image.
- Return type:
ndarray
Notes
Adapted 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]¶
Convert image gray-levels from amplitude (or log-amplitude) to absorption index beta.
- Parameters:
input_img (ndarray) – Image with gray-levels proportional to transmitted amplitude.
energy (float) – X-ray photon energy in keV.
voxelsize (float, list of float, or ndarray) – Voxel size in metres.
apply_log (bool, optional) – If
True, apply the natural logarithm toinput_imgbefore the conversion (for images that have not yet been log-transformed). DefaultFalse.
- Returns:
beta_img (ndarray) – Image with gray-levels in units of absorption index beta.
factor (float) – Conversion factor applied.
- toupy.utils.converter_utils.convert_to_delta(input_img, energy, voxelsize)[source]¶
Convert image gray-levels from phase-shifts to refractive index decrement delta.
- Parameters:
- Returns:
delta_img (ndarray) – Image with gray-levels in units of delta.
factor (float) – Conversion factor applied.
- toupy.utils.converter_utils.convert_to_mu(input_img, wavelen)[source]¶
Convert image gray-levels from absorption index Beta to linear attenuation coefficient mu.
- Parameters:
input_img (ndarray) – Image with gray-levels proportional to the absorption index Beta.
wavelen (float) – X-ray wavelength in metres.
- Returns:
Image with gray-levels in units of linear attenuation coefficient mu (m-1).
- Return type:
ndarray
- toupy.utils.converter_utils.convert_to_rhoe(input_img, wavelen)[source]¶
Convert image gray-levels from refractive index decrement delta to electron density.
- Parameters:
input_img (ndarray) – Image with gray-levels proportional to delta.
wavelen (float) – X-ray wavelength in metres.
- Returns:
Image with gray-levels in units of electron density (electrons m-3).
- Return type:
ndarray
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)))
- 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))
toupy.utils.funcutils module¶
- 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.progress_bar(count, block_size, total_size)[source]¶
Display a download progress bar in the terminal.
Intended as a
reporthookcallback forurllib.request.urlretrieve().
- class toupy.utils.funcutils.switch(value)[source]¶
Bases:
objectProvide switch/case functionality for Python.
Mimics the
switchstatement found in C, Java, and similar languages. Intended to be used in aforloop with a single iteration, where eachcaseis anifguarded by a call tomatch().- Parameters:
value (object) – The value to compare against in each case.
Examples
>>> for case in switch(x): ... if case(1): ... print("one") ... break ... if case(2, 3): ... print("two or three") ... break ... if case(): # default ... print("other")
- class toupy.utils.funcutils.tqdm(*_, **__)[source]¶
Bases:
ComparableDecorate an iterable object, returning an iterator which acts exactly like the original iterable, but prints a dynamically updating progressbar every time a value is requested.
- Parameters:
iterable (iterable, optional) – Iterable to decorate with a progressbar. Leave blank to manually manage the updates.
desc (str, optional) – Prefix for the progressbar.
total (int or float, optional) – The number of expected iterations. If unspecified, len(iterable) is used if possible. If float(“inf”) or as a last resort, only basic progress statistics are displayed (no ETA, no progressbar). If gui is True and this parameter needs subsequent updating, specify an initial arbitrary large positive number, e.g. 9e9.
leave (bool, optional) – If [default: True], keeps all traces of the progressbar upon termination of iteration. If None, will leave only if position is 0.
file (io.TextIOWrapper or io.StringIO, optional) – Specifies where to output the progress messages (default: sys.stderr). Uses file.write(str) and file.flush() methods. For encoding, see write_bytes.
ncols (int, optional) – The width of the entire output message. If specified, dynamically resizes the progressbar to stay within this bound. If unspecified, attempts to use environment width. The fallback is a meter width of 10 and no limit for the counter and statistics. If 0, will not print any meter (only stats).
mininterval (float, optional) – Minimum progress display update interval [default: 0.1] seconds.
maxinterval (float, optional) – Maximum progress display update interval [default: 10] seconds. Automatically adjusts miniters to correspond to mininterval after long display update lag. Only works if dynamic_miniters or monitor thread is enabled.
miniters (int or float, optional) – Minimum progress display update interval, in iterations. If 0 and dynamic_miniters, will automatically adjust to equal mininterval (more CPU efficient, good for tight loops). If > 0, will skip display of specified number of iterations. Tweak this and mininterval to get very efficient loops. If your progress is erratic with both fast and slow iterations (network, skipping items, etc) you should set miniters=1.
ascii (bool or str, optional) – If unspecified or False, use unicode (smooth blocks) to fill the meter. The fallback is to use ASCII characters “ 123456789#”.
disable (bool, optional) – Whether to disable the entire progressbar wrapper [default: False]. If set to None, disable on non-TTY.
unit (str, optional) – String that will be used to define the unit of each iteration [default: it].
unit_scale (bool or int or float, optional) – If 1 or True, the number of iterations will be reduced/scaled automatically and a metric prefix following the International System of Units standard will be added (kilo, mega, etc.) [default: False]. If any other non-zero number, will scale total and n.
dynamic_ncols (bool, optional) – If set, constantly alters ncols and nrows to the environment (allowing for window resizes) [default: False].
smoothing (float, optional) – Exponential moving average smoothing factor for speed estimates (ignored in GUI mode). Ranges from 0 (average speed) to 1 (current/instantaneous speed) [default: 0.3].
bar_format (str, optional) –
Specify a custom bar string formatting. May impact performance. [default: ‘{l_bar}{bar}{r_bar}’], where l_bar=’{desc}: {percentage:3.0f}%|’ and r_bar=’| {n_fmt}/{total_fmt} [{elapsed}<{remaining}, ‘
’{rate_fmt}{postfix}]’
- Possible vars: l_bar, bar, r_bar, n, n_fmt, total, total_fmt,
percentage, elapsed, elapsed_s, ncols, nrows, desc, unit, rate, rate_fmt, rate_noinv, rate_noinv_fmt, rate_inv, rate_inv_fmt, postfix, unit_divisor, remaining, remaining_s, eta.
Note that a trailing “: “ is automatically removed after {desc} if the latter is empty.
initial (int or float, optional) – The initial counter value. Useful when restarting a progress bar [default: 0]. If using float, consider specifying {n:.3f} or similar in bar_format, or specifying unit_scale.
position (int, optional) – Specify the line offset to print this bar (starting from 0) Automatic if unspecified. Useful to manage multiple bars at once (eg, from threads).
postfix (dict or *, optional) – Specify additional stats to display at the end of the bar. Calls set_postfix(**postfix) if possible (dict).
unit_divisor (float, optional) – [default: 1000], ignored unless unit_scale is True.
write_bytes (bool, optional) – Whether to write bytes. If (default: False) will write unicode.
lock_args (tuple, optional) – Passed to refresh for intermediate output (initialisation, iterating, and updating).
nrows (int, optional) – The screen height. If specified, hides nested bars outside this bound. If unspecified, attempts to use environment height. The fallback is 20.
colour (str, optional) – Bar colour (e.g. ‘green’, ‘#00ff00’).
delay (float, optional) – Don’t display until [default: 0] seconds have elapsed.
gui (bool, optional) – WARNING: internal parameter - do not use. Use tqdm.gui.tqdm(…) instead. If set, will attempt to use matplotlib animations for a graphical output [default: False].
- Returns:
out
- Return type:
decorated iterator.
- display(msg=None, pos=None)[source]¶
Use self.sp to display msg in the specified pos.
Consider overloading this function when inheriting to use e.g.: self.some_frontend(**self.format_dict) instead of self.sp.
- Parameters:
msg (str, optional. What to display (default: repr(self)).)
pos (int, optional. Position to moveto) – (default: abs(self.pos)).
- classmethod external_write_mode(file=None, nolock=False)[source]¶
Disable tqdm within context and refresh tqdm when exits. Useful when writing to standard output stream
- property format_dict¶
Public API for read-only member access.
- static format_meter(n, total, elapsed, ncols=None, prefix='', ascii=False, unit='it', unit_scale=False, rate=None, bar_format=None, postfix=None, unit_divisor=1000, initial=0, colour=None, **extra_kwargs)[source]¶
Return a string-based progress bar given some parameters
- Parameters:
total (int or float) – The expected total number of iterations. If meaningless (None), only basic progress statistics are displayed (no ETA).
elapsed (float) – Number of seconds passed since start.
ncols (int, optional) – The width of the entire output message. If specified, dynamically resizes {bar} to stay within this bound [default: None]. If 0, will not print any bar (only stats). The fallback is {bar:10}.
prefix (str, optional) – Prefix message (included in total width) [default: ‘’]. Use as {desc} in bar_format string.
ascii (bool, optional or str, optional) – If not set, use unicode (smooth blocks) to fill the meter [default: False]. The fallback is to use ASCII characters “ 123456789#”.
unit (str, optional) – The iteration unit [default: ‘it’].
unit_scale (bool or int or float, optional) – If 1 or True, the number of iterations will be printed with an appropriate SI metric prefix (k = 10^3, M = 10^6, etc.) [default: False]. If any other non-zero number, will scale total and n.
rate (float, optional) – Manual override for iteration rate. If [default: None], uses n/elapsed.
bar_format (str, optional) –
Specify a custom bar string formatting. May impact performance. [default: ‘{l_bar}{bar}{r_bar}’], where l_bar=’{desc}: {percentage:3.0f}%|’ and r_bar=’| {n_fmt}/{total_fmt} [{elapsed}<{remaining}, ‘
’{rate_fmt}{postfix}]’
- Possible vars: l_bar, bar, r_bar, n, n_fmt, total, total_fmt,
percentage, elapsed, elapsed_s, ncols, nrows, desc, unit, rate, rate_fmt, rate_noinv, rate_noinv_fmt, rate_inv, rate_inv_fmt, postfix, unit_divisor, remaining, remaining_s, eta.
Note that a trailing “: “ is automatically removed after {desc} if the latter is empty.
postfix (*, optional) – Similar to prefix, but placed at the end (e.g. for additional stats). Note: postfix is usually a string (not a dict) for this method, and will if possible be set to postfix = ‘, ‘ + postfix. However other types are supported (#382).
unit_divisor (float, optional) – [default: 1000], ignored unless unit_scale is True.
initial (int or float, optional) – The initial counter value [default: 0].
colour (str, optional) – Bar colour (e.g. ‘green’, ‘#00ff00’).
- Returns:
out
- Return type:
Formatted meter and stats, ready to display.
- static format_sizeof(num, suffix='', divisor=1000)[source]¶
Formats a number (greater than unity) with SI Order of Magnitude prefixes.
- monitor = None¶
- monitor_interval = 10¶
- classmethod pandas(**tqdm_kwargs)[source]¶
- Registers the current tqdm class with
pandas.core. ( frame.DataFrame | series.Series | groupby.(generic.)DataFrameGroupBy | groupby.(generic.)SeriesGroupBy ).progress_apply
A new instance will be created every time progress_apply is called, and each instance will automatically close() upon completion.
- Parameters:
tqdm_kwargs (arguments for the tqdm instance)
Examples
>>> import pandas as pd >>> import numpy as np >>> from tqdm import tqdm >>> from tqdm.gui import tqdm as tqdm_gui >>> >>> df = pd.DataFrame(np.random.randint(0, 100, (100000, 6))) >>> tqdm.pandas(ncols=50) # can use tqdm_gui, optional kwargs, etc >>> # Now you can use `progress_apply` instead of `apply` >>> df.groupby(0).progress_apply(lambda x: x**2)
References
<https://stackoverflow.com/questions/18603270/ progress-indicator-during-pandas-operations-python>
- reset(total=None)[source]¶
Resets to 0 iterations for repeated use.
Consider combining with leave=True.
- set_postfix(ordered_dict=None, refresh=True, **kwargs)[source]¶
Set/modify postfix (additional stats) with automatic formatting based on datatype.
- set_postfix_str(s='', refresh=True)[source]¶
Postfix without dictionary expansion, similar to prefix handling.
- static status_printer(file)[source]¶
Manage the printing and in-place updating of a line of characters. Note that if the string is longer than a line, then in-place updating may not work (it will print a new line at each refresh).
- update(n=1)[source]¶
Manually update the progress bar, useful for streams such as reading files. E.g.: >>> t = tqdm(total=filesize) # Initialise >>> for current_buffer in stream: … … … t.update(len(current_buffer)) >>> t.close() The last line is highly recommended, but possibly not necessary if t.update() will be called in such a way that filesize will be exactly reached and printed.
- classmethod wrapattr(stream, method, total=None, bytes=True, **tqdm_kwargs)[source]¶
stream : file-like object. method : str, “read” or “write”. The result of read() and
the first argument of write() should have a len().
>>> with tqdm.wrapattr(file_obj, "read", total=file_obj.size) as fobj: ... while True: ... chunk = fobj.read(chunk_size) ... if not chunk: ... break
toupy.utils.plot_utils module¶
- class toupy.utils.plot_utils.RegisterPlot(**params)[source]¶
Bases:
objectManage live plot updates during tomographic projection alignment.
Provides two high-level entry points:
plotsvertical()— vertical shift alignment diagnosticsplotshorizontal()— horizontal (sinogram) alignment diagnostics
Architecture¶
- Vertical alignment
fig_proj— small static figure (projection + ROI), shown once at initialisation and never updated.fig_main— unified 6-panel diagnostic figure, updated every iteration via its_out_mainOutputwidget.
- Horizontal alignment
fig_main— unified 6-panel diagnostic figure (single figure).
Display strategy¶
- Jupyter / ``%matplotlib widget``
Figures are rendered to PNG bytes via
savefig()and shown via IPythonDisplayHandleobjects obtained fromIPython.display.display(..., display_id=True). Subsequent updates callhandle.update(new_png)— noclear_outputis ever called, so the figure display items are never accidentally removed. The verbose text printed by the alignment loop is similarly updated in-place via a secondDisplayHandlethat holds anHTMLblock; the loop redirectsstdoutto aStringIObuffer and calls_verbose_update()after each iteration.- Terminal
Figures are redrawn via
canvas.draw_idle()+plt.pause().
- param **params:
Algorithm parameters forwarded to the canvas helpers. Must contain at least
'slicenum','sinohigh', and'sinolow'.
- plotshorizontal(recons, sinoorig, sinocurr, sinocomp, deltaslice, metric_error, count)[source]¶
Display or update the horizontal-alignment diagnostic figure.
First call — creates the unified 6-panel
fig_main.Subsequent calls — delegates to
updatehorizontal()which updates artists in-place and refreshesfig_main.- Parameters:
recons (ndarray) – Current reconstructed slice.
sinoorig (ndarray) – Original sinogram (fixed reference, never updated after iter 0).
sinocurr (ndarray) – Current aligned sinogram.
sinocomp (ndarray) – Synthetic sinogram computed from the reconstruction.
deltaslice (ndarray) – Current horizontal shift estimates.
count (int) – Current iteration number.
- plotsvertical(proj, lims, vertfluctinit, vertfluctcurr, deltastack, metric_error, count, max_correction=None)[source]¶
Display or update the vertical-alignment diagnostic figures.
First call — creates
fig_proj(static, shown once) andfig_main(unified 6-panel, updated each iteration).Subsequent calls — delegates to
updatevertical()which updates artists in-place and refreshes onlyfig_main.- Parameters:
proj (ndarray) – Current projection image (displayed once as ROI overlay).
lims (tuple) –
(limrow, limcol)ROI boundary indices.vertfluctinit (ndarray) – Initial vertical fluctuations (fixed reference).
vertfluctcurr (ndarray) – Current vertical fluctuations (updated each iteration).
deltastack (ndarray) – Current vertical shift estimates.
metric_error (list of float) – Error metric history (grows by one element per iteration).
count (int) – Current iteration number.
max_correction (float or None) – Maximum absolute vertical shift change this iteration (pixels). Displayed in the figure suptitle when provided.
- updatehorizontal()[source]¶
Update the horizontal-alignment diagnostic figure in-place.
Modifies existing artist objects — no new figures or axes are created.
Panels updated¶
recon — reconstructed slice (sharpens as alignment improves).
synthsino — synthetic sinogram.
currsino — current sinogram.
shifts — shift curves for all projections.
error — error-metric curve (grows one point per iteration).
fig_main.suptitle— iteration counter and latest error value.
- updatevertical()[source]¶
Update the vertical-alignment diagnostic figure in-place.
Modifies existing artist objects — no new figures or axes are created. Only
fig_mainis refreshed;fig_projis static.Panels updated¶
curr2d — current 2-D integral image.
curr1d — current 1-D integral line plots and mean overlay.
shifts — shift curves for all projections.
error — error-metric curve (grows one point per iteration).
fig_main.suptitle— iteration counter and latest error value.
- class toupy.utils.plot_utils.ShowProjections[source]¶
Bases:
objectShow projections and probe
- toupy.utils.plot_utils.animated_image(stack_array, *args)[source]¶
Iterative plot of the images using animation module of Matplotlib
- 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.
- toupy.utils.plot_utils.display_slice(recons, colormap='bone', vmin=None, vmax=None)[source]¶
Display tomographic slice
- 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
bonevmin (float, None, optional) – Minimum gray-level. The default value is
Nonevmax (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_figure(fig=None, close=True)[source]¶
Display a figure in the current environment and optionally close it.
In Jupyter: switches the figure’s canvas to a plain Agg renderer, renders to a PNG via savefig, and shows it as a static
display.Image. Switching to Agg before savefig is necessary because the default ipympl canvas (FigureCanvasWebAggCore) routes savefig throughcanvas.draw()→manager.refresh_all(), which raisesAttributeErrorwhenmanagerisNone(e.g. whenplt.ion()was never called). The Agg canvas has no manager dependency.In terminal: calls plt.show(block=False).
- Parameters:
fig (matplotlib Figure, optional) – Figure to display. Defaults to the current figure (plt.gcf()).
close (bool, optional) – If True (default), close the figure after displaying it in Jupyter. Set to False for figures that will be reused or updated.
- toupy.utils.plot_utils.show_fsc_curve(fn, FSC, T, snrt, ndim)[source]¶
Plot the FSC and threshold curves, save to disk, and display.
Draws the FSC curve, the threshold, a vertical dashed line at the estimated resolution crossing, and an informative title. The figure is saved to
FSC_2D.pngorFSC_3D.pngand then displayed.- Parameters:
fn (ndarray) – Spatial frequencies normalised by the Nyquist frequency.
FSC (ndarray) – Fourier Shell Correlation curve (real part).
T (ndarray) – Threshold curve.
snrt (float) – SNR threshold value used to select the threshold label:
0.2071→"1/2 bit threshold",0.5→"1 bit threshold", anything else →f"Threshold SNR = {snrt:g}".ndim (int) – Number of dimensions of the original data (2 or 3).
- Return type:
None
- toupy.utils.plot_utils.show_fsc_images(img1_apod, img2_apod)[source]¶
Display the two apodized images used in the FSC computation.
- Parameters:
img1_apod (ndarray) – First apodized image (or sagittal slice for 3D).
img2_apod (ndarray) – Second apodized image (or sagittal slice for 3D).
- toupy.utils.plot_utils.show_linearphase(image, mask, *args)[source]¶
Display a phase projection with an overlaid mask and a horizontal line cut.
- Parameters:
image (ndarray, shape (nr, nc)) – Phase image to display.
mask (ndarray, shape (nr, nc)) – Mask added to
imagefor the 2-D panel.*args –
- args[0]int or str, optional
Projection index used in the figure title. Defaults to an empty string if not provided.
- toupy.utils.plot_utils.show_random_fsc_curve(fn, fsc_obs, fsc_rand, fsc_corr, T, cutoff_fn, ndim)[source]¶
Plot the phase-randomization FSC test results and save to disk.
Left panel:
FSC_obs,FSC_rand,FSC_corr, the thresholdT, and a vertical dotted line at the randomisation cutoff frequency. Right panel:FSC_obs − FSC_rand(genuine signal above the cutoff), with a horizontal dashed line at zero.The figure is saved to
RandomFSC.pngand then displayed.- Parameters:
fn (ndarray) – Spatial frequencies normalised by the Nyquist frequency.
fsc_obs (ndarray) – Observed (standard) FSC curve.
fsc_rand (ndarray) – Phase-randomized FSC curve (noise floor).
fsc_corr (ndarray) – Corrected FSC, defined as
(FSC_obs - FSC_rand) / (1 - FSC_rand).T (ndarray) – Threshold curve.
cutoff_fn (float) – Normalised cutoff frequency (
cutoff_shell / fnyquist) at which phase randomisation begins.ndim (int) – Number of dimensions of the original data (2 or 3). Currently used only for potential future labelling.
- Return type:
None
- toupy.utils.plot_utils.show_resolution_map(rmap, ndim, title, filename, slice_idx=None, axis=0, cmap='viridis_r', vmin=None, vmax=None)[source]¶
Display a 2-D or 3-D local resolution map and save it to disk.
For a 3-D map a single slice is extracted along axis and shown as a 2-D image. The slice index and axis are appended to the title. For a 2-D map the full array is shown. A colorbar labelled
"Local resolution (pixels)"is added, and the figure is saved to filename before being displayed.- Parameters:
rmap (ndarray) – Local resolution map. May be 2-D or 3-D.
ndim (int) – Number of dimensions of the original data (2 or 3). Must match
rmap.ndim.title (str) – Base title string. For 3-D data the slice information is appended automatically.
filename (str) – Output filename (e.g.
"LocalFSC_resmap.png"). Saved withbbox_inches='tight'.slice_idx (int or None, optional) – Index of the slice to display along axis. Defaults to the central slice (
rmap.shape[axis] // 2) whenNone. Ignored for 2-D input.axis (int, optional) – Axis along which to extract the slice (3-D only). Default
0.cmap (str, optional) – Matplotlib colormap name. Default
'viridis_r'.vmin (float or None, optional) – Lower colour-scale limit.
Noneuses the data minimum.vmax (float or None, optional) – Upper colour-scale limit.
Noneuses the data maximum.
- Return type:
None
- toupy.utils.plot_utils.show_ssnr_curve(fn, FSC, SSNR, SSNR_T, snrt, ndim)[source]¶
Plot the SSNR curve with its threshold, resolution line, and asymptote.
The figure shows the Spectral Signal-to-Noise Ratio (SSNR) and its frequency-dependent threshold on a semi-logarithmic scale, together with a horizontal dotted line at the asymptotic threshold value and a vertical dashed line at the estimated resolution crossing. The figure is saved to
SSNR_2D.pngorSSNR_3D.pngand then displayed.- Parameters:
fn (ndarray) – Spatial frequencies normalised by the Nyquist frequency.
FSC (ndarray) – Fourier Shell/Ring Correlation curve (real part), included for potential future use but not plotted directly.
SSNR (ndarray) – Spectral Signal-to-Noise Ratio curve, derived from FSC via
SSNR = 2 * FSC / (1 - FSC).SSNR_T (ndarray) – Frequency-dependent SSNR threshold curve, derived from the FSC threshold
TviaSSNR_T = 2 * T / (1 - T).snrt (float) – SNR threshold value used to compute the asymptote and select the threshold name:
0.2071→"half-bit", anything else →"one-bit".ndim (int) – Number of dimensions of the original data (2 or 3).
- Return type:
None