Module Method Summary

get_filepaths(directory) This function will generate the file names in a directory tree by walking the tree either top-down or bottom-up.
readimages() Open a Qt dialog to get a directory and read all the .tif images inside of
get_fei_pixel_width(fname) Get the width of a pixel (in nm) from an FEI tif file.
get_tescan_pixel_width(fname) Get the width of a pixel (in nm) from a Tescan .hdr file.
trackfiducials(pix_width[, im_list, ...]) Find distance between fiducial marks in image list
plot_fid_distances(fid_distances[, ...]) Plot a list of values as the distance between two fiducial markers
fit_fid_distances(fid_distances[, ...]) Perform linear fit on widths between fiducials to get an average global slice thickness value.
image_browser([im_list, downsize, ...]) Open an OpenCV window that can browse through images using simple keyboard
export_video([im_list, filename, fourcc, ...]) Use OpenCV to write a video of all the images
slice_thickness(fid_distances[, ...]) Calculate the thickness of a series of slices from the fiducial widths
plot_thicknesses(thicknesses[, ...]) Plots a visualization of slice thickness measurements, as well as
save_output(data[, fname, overwrite, ...]) Write the output contained in a list to a csv file
import_csv_data(fname[, skip_rows, delimiter]) Read a csv file (saved by save_output) and get data back in a format that can be used by the other methods in this module.
_convert_units(to_convert, input_u, output_u) Convert values in a list from input_u to output_u

Module API Documentation

fibtracking.fibtracking.get_filepaths(directory)[source]

This function will generate the file names in a directory tree by walking the tree either top-down or bottom-up. For each directory in the tree rooted at directory top (including top itself), it yields a 3-tuple (dirpath, dirnames, filenames). (From http://stackoverflow.com/a/19308592/1435788)

Parameters:directory (str) – directory to walk and find files in
fibtracking.fibtracking.readimages()[source]

Open a Qt dialog to get a directory and read all the .tif images inside of it with OpenCV

Returns:
Return type:List of images read by opencv
fibtracking.fibtracking.get_fei_pixel_width(fname)[source]

Get the width of a pixel (in nm) from an FEI tif file. Similar to how we used to use egrep -a previously, but this is just Python so it’s simpler.

Parameters:fname (string) – Filename of image to read from
Returns:pixWidth – Width of the pixel (in nm)
Return type:float
fibtracking.fibtracking.get_tescan_pixel_width(fname)[source]

Get the width of a pixel (in nm) from a Tescan .hdr file. Similar to how we used to use egrep -a previously, but this is just Python so it’s simpler.

Parameters:fname (string) – Filename of image to read from
Returns:pixWidth – Width of the pixel (in nm)
Return type:float
fibtracking.fibtracking.trackfiducials(pix_width, im_list=None, im_number=0, plot_fiducials=False, fid_num_to_plot=25, per_row=5)[source]

Find distance between fiducial marks in image list

Parameters:
  • pix_width (float) – horizontal pixel width (in m) can be easily obtained by running egrep -a PixelWidth <img filename> | tail -n 1 or using
  • im_list (list of cv2 loaded images (for debugging)) –
  • im_number (int) – index of image to use for choosing the fiducial. Useful if the first few images do not show the fiducial fully
  • plot_fiducials (bool) – Switch to control whether an overview of fiducials will be plotted
  • fid_num_to_plot (int) – How many fiducials to plot (will be spread out over all images)
  • per_row (int) – How many subplots displayed per row
Returns:

  • A list containing the horizontal distance (in units of pix_width)
  • between the two selected fiducial marks on each image.

fibtracking.fibtracking.plot_fid_distances(fid_distances, input_units='m', output_units='um', slice_number_start=1, plot_diff=False, sns_style='white', sns_context='poster', sns_cmap=None, sns_main_color=0, sns_diff_color=0, sns_kw=None)[source]

Plot a list of values as the distance between two fiducial markers

Parameters:
  • fid_distances (list) – Values to plot
  • input_units (str) – units of values as provided
  • output_units (str) – desired output units for plot
  • slice_number_start (int) – starting slice number (for display purposes only)
  • plot_diff (bool) – Switch to control whether or not a plot of the difference between subsequent slices is shown as well (as subplot)
  • sns_style (str) – default style for seaborn (‘white’, ‘dark’, ‘darkgrid’, etc.)
  • sns_context (str) – context for plotting with seaborn
  • sns_cmap (list, str, tuple) – colormap for use with seaborn default is [“4C72B1”,”#004040”,”#023FA5”,”#8E063B”,”#098C09”,”#EF9708”] (light blue, dark teal, blue, red, green, orange) If str or tuple, then these parameters are passed to the seaborn.color_palette() function
  • sns_main_color (int) – color (# from cmap) to use for plotting the fiducial distance
  • sns_diff_color (int) – color (# from cmap) to use for plotting the differential distance
  • sns_kw (dict) – additional arguments to be passed to seaborn.set_style (see http://goo.gl/WvLdc6 for details)
Returns:

Handle to figure that is plotted (and axes array if subplots)

Return type:

matplotlib figure, <matplotlib axes>

fibtracking.fibtracking.fit_fid_distances(fid_distances, start_image=0, end_image=None, input_units='m', output_units='nm', fid_line_angle=40, plot_results=True, print_output=True, reject_m=None)[source]

Perform linear fit on widths between fiducials to get an average global slice thickness value.

Parameters:
  • fid_distances (list) – List of fiducial distances between slices in input_units
  • start_image (int) – Which image to start the fit at (can exclude first start_image images if desired)
  • end_image (int or None) – Which image to end the fit at. If None, all images will be used
  • input_units (str) – Units that fid_distances is provided in.
  • output_units (str) – Desired output units for plot
  • fid_line_angle (numeric) –

    Half-angle (in degrees) of fiducial line triangle (angle a, below)

    _images/half_angle.png
  • plot_results (boolean) – Whether or not to plot the results visually
  • print_output (boolean) – Whether or not to print the output to the terminal
  • reject_m (None or float) – “m” value to be used in the helper reject_outliers() function. If None, no modification of the data will be performed
Returns:

avg_t – Average thickness of slices, determined from fit

Return type:

float

fibtracking.fibtracking.image_browser(im_list=None, downsize=False, downsizefactor=0.5, labelimages=False)[source]

Open an OpenCV window that can browse through images using simple keyboard controls

Parameters:
  • im_list (list) – List of OpenCV images that will be navigated. If None, a Qt dialogue is opened to select a directory from which to read .tif files
  • downsize (bool) – Switch to control whether images are downsized when reading into memory (for easier visualization)
  • downsizefactor (float) – Factor by which to downsize the images (0.5 will make each dimension half it’s original size)
  • labelimages (bool) – Switch to control whether or not a text label will be written on the image to show it’s position in the stack. Note, this is written onto the image itself, and will persist for future operations on the image list, so this option should not be used if this image list is to be used otherwise.

Notes

The player is controlled with keyboard shortcuts:

Shortcut Action
f Animate stack forward
d Animate stack backward
s Stop animation
+ Double the speed of animation (faster)
- Half the speed of animation (slower)
n Step forwards by one image
b Step backwards by one image
Esc Exit the image browser
fibtracking.fibtracking.export_video(im_list=None, filename=None, fourcc=None, fps=20, downsize=False, downsizefactor=0.5, labelimages=False, labelsize=None)[source]

Use OpenCV to write a video of all the images

Parameters:
  • im_list (list) – List of OpenCV images that will be navigated. If None, a Qt dialogue is opened to select a directory from which to read images
  • filename (str) – Name of file to save as video
  • fourcc (None or str) – Codec to use for writing video (see http://www.fourcc.org/codecs.php) supplied as four character string (such as ‘DIVX’). If None, dialog will be presented to the user to choose.
  • fps (int) – frames per second to use in output
  • downsize (bool) – Switch to control whether images are downsized when reading into memory (for easier visualization)
  • downsizefactor (float) – Factor by which to downsize the images (0.5 will make each dimension half it’s original size)
  • labelimages (bool) – Switch to control whether or not a text label will be written on the image to show it’s position in the stack. Note, this is written onto the image itself, and will persist for future operations on the image list, so this option should not be used if this image list is to be used otherwise.
  • labelsize (int) – factor to change how large the labels will be written. If None, an appropriate default size will be attempted
Returns:

Return type:

None

fibtracking.fibtracking.slice_thickness(fid_distances, fid_line_angle=40, input_units='m', output_units='nm')[source]

Calculate the thickness of a series of slices from the fiducial widths

Parameters:
  • fid_distances (list) – list of distance between fiducials (calculated from trackfiducials() )
  • fid_line_angle (float) –

    half-angle (in degrees) of fiducial line triangle (angle a, below)

    _images/half_angle.png
  • input_units (str) – unit of input values (must be one of those supported by _convert_units
  • output_units (str) – desired unit of output (must be one of those supported by _convert_units
Returns:

  • thickness (list) – list of floats giving the difference of each slice
  • mean (float) – mean thickness value
  • std (float) – std dev of thickness values

fibtracking.fibtracking.plot_thicknesses(thicknesses, lowess_factor=0.05, axes_limits='auto', units='nm', xlabel='Slice #', ylabel='auto', plot_data=True, dataplotkw=None, plot_lowess=True, lowessplotkw=None, plot_mean=True, meanplotkw=None, plot_std=True, stdplotkw=None, plot_legend=True, legend_loc='best', sns_style='white', sns_context='poster', sns_cmap=None, sns_color_data=1, sns_color_lowess=2, sns_color_mean=3, sns_color_std=4, sns_style_kw=None)[source]

Plots a visualization of slice thickness measurements, as well as descriptive data

Parameters:
  • thicknesses (list) – thickness data to be plotted
  • lowess_factor (float) – factor to be used when smoothing thickness data for plotting (valid values are [0-1]) the higher the factor used, the more smoothed the data will be
  • axes_limits ('auto' or list of int) – axes limits to use when displaying the data. If ‘auto’, the limits will be determined from the data; otherwise, should be given in the order [xmin, xmax, ymin, ymax]
  • units (str) – units that values are given in (default of ‘nm’)
  • xlabel (str) – label to use for the x-axis
  • ylabel (str) – label to use for the y-axis if ‘auto’, label will be built including the units
  • plot_data (bool) – Switch to control whether or not raw data is plotted
  • dataplotkw (dict or None) – dictionary containing additional keyword arguments for the data scatterplot default included parameters are marker, linewidths, and label, which are set in the code if the value is None
  • plot_lowess (bool) – Switch to control whether or not smoothed data is plotted
  • lowessplotkw (dict or None) – dictionary containing additional keyword arguments for the LOWESS regression line plot default included parameters are linewidth. label is defined in the code (if not given here) defaults are set in the code if the value is None
  • plot_mean (bool) – Switch to control whether or not data mean is plotted
  • meanplotkw (dict or None) – dictionary containing additional keyword arguments for the mean line plot default included parameters are lw. label is defined in the code (if not given here) defaults are set in the code if the value is None
  • plot_std (bool) – Switch to control whether or not data std dev is plotted
  • meanplotkw – dictionary containing additional keyword arguments for the std dev line plots default included parameters are lw and ls. label is defined in the code (if not given here) defaults are set in the code if the value is None
  • plot_legend (bool) – Switch to control whether or not legend is shown on the plot
  • legend_loc (str or int) – see http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.legend for details Desired position for the legend
  • sns_style (str) – default style for seaborn (‘white’, ‘dark’, ‘darkgrid’, ‘whitegrid’)
  • sns_context (str) – context for plotting with seaborn
  • sns_cmap (list, str, tuple, or None) – colormap for use with seaborn default is [“4C72B1”,”#004040”,”#023FA5”,”#8E063B”,”#098C09”,”#EF9708”] (light blue, dark teal, blue, red, green, orange) If str or tuple, then these parameters are passed to the seaborn.color_palette() function default cmap is set in the code if the value is None
  • sns_color_data (int) – color (# from cmap) to use for plotting the thickness data
  • sns_color_lowess (int) – color (# from cmap) to use for plotting the lowess-smoothed data
  • sns_color_mean (int) – color (# from cmap) to use for plotting the data mean
  • sns_color_std (int) – color (# from cmap) to use for plotting the data std
  • sns_style_kw (dict or None) – additional arguments to be passed to seaborn.set_style defaults are set in the code if the value is None (see http://goo.gl/WvLdc6 for details)
Returns:

Return type:

fig = Handle to figure that is plotted

fibtracking.fibtracking.save_output(data, fname='slice_thicknesses_fibtracking.csv', overwrite=False, collabels='auto', units='m', delimiter=', ', print_time_stamp=True)[source]

Write the output contained in a list to a csv file

Parameters:
  • data (list) – list containing values to save (usually slice thicknesses)
  • fname (str) – string containing name of file to which to write
  • overwrite (bool) – if fname exists, switch to control whether to overwrite
  • collabels (list, 'auto', or None) – labels to write as column headers. If ‘auto’, default of “Slice, Thickness (units)” will be used. If a list, use str values separated by commas.
  • units (str) – units to write in output
  • delimiter (str) – delimiter to use in output
  • print_time_stamp (bool) – switch to control whether a time stamp is printed on the first line of the file
  • Returns – None
fibtracking.fibtracking.import_csv_data(fname, skip_rows=2, delimiter=', ')[source]

Read a csv file (saved by save_output) and get data back in a format that can be used by the other methods in this module.

Parameters:
  • fname (str) – Filename to read from
  • skip_rows (int) – Number of rows to skip at the beginning of the file (headers)
  • delimiter (str) – Delimiter for csv file (usually ‘,’)
Returns:

data – data from the requested file in the format expected by the other methods in this module

Return type:

NumPy array

fibtracking.fibtracking._convert_units(to_convert, input_u, output_u)[source]

Convert values in a list from input_u to output_u

Parameters:
  • to_convert (list or numeric) – values to convert from one unit to another
  • input_u (str) – [‘m’, ‘cm’, ‘mm’, ‘um’, or ‘nm’]
  • output_u (str) – [‘m’, ‘cm’, ‘mm’, ‘um’, or ‘nm’]
Returns:

values in new units

Return type:

list