ImageFolderMixin

class Stoner.Image.folders.ImageFolderMixin[source]

Bases: object

Mixin to provide a folder object for images.

ImageFolderMixin is designed to behave pretty much like DataFolder but with functions and loaders appropriate for image based files.

type

the type ob object to store in the folder (defaults to Stoner.Cire.Data)

Type:

Stoner.Image.core.ImageArray

extra_args

Extra arguments to use when instantiatoing the contents of the folder from a file on disk.

Type:

dict

pattern

A filename globbing pattern that matches the contents of the folder. If a regular expression is provided then any named groups are used to construct additional metadata entryies from the filename. Default is . to match all files with an extension.

Type:

str or regexp

read_means

If true, additional metadata keys are added that return the mean value of each column of the data. This can hep in grouping files where one column of data contains a constant value for the experimental state. Default is False

Type:

bool

recursive

Specifies whether to search recursively in a whole directory tree. Default is True.

Type:

bool

flatten

Specify where to present subdirectories as separate groups in the folder (False) or as a single group (True). Default is False. The DiskBasedFolderMixin.flatten() method has the equivalent effect and DiskBasedFolderMixin.unflatten() reverses it.

Type:

bool

directory

The root directory on disc for the folder - by default this is the current working directory.

Type:

str

multifile

Whether to select individual files manually that are not (necessarily) in a common directory structure.

Type:

boo

readlist

Whether to read the directory immediately on creation. Default is True

Type:

bool

Attributes Summary

images

Iterate over just the images in the Folder.

size

Return the size of an individual image or False if not all images are the same size.

Methods Summary

align(*args, **kargs)

Align each image in the folder to the reference image.

apply_all(func, *args, **kargs)

Apply function to all images in the stack.

as_stack()

Return a ImageStack of the images in the current group.

average([weights, _box, _metadata])

Get an array of average pixel values for the stack.

from_tiff(filename, **kargs)

Create a new ImageArray from a tiff file.

loadgroup()

Load all files from this group into memory.

mask_select()

Run the ImageFile.mask.select() on each image.

mean([_box, _metadata])

Calculate the mean value of all the images in the stack.

montage(*args, **kargs)

Call the plot method for each metadataObject, but switching to a subplot each time.

stddev([weights, _box, _metadata])

Calculate weighted standard deviation for stack.

stderr([weights, _box, _metadata])

Calculate standard error in the stack average.

to_tiff(filename)

Save the ImageArray as a tiff image with metadata.

Attributes Documentation

images

Iterate over just the images in the Folder.

size

Return the size of an individual image or False if not all images are the same size.

Methods Documentation

align(*args, **kargs)[source]

Align each image in the folder to the reference image.

Parameters:

ref (str, int, ImageFile, ImageArray or 2D array) – The reference image to align to. If a string or an int, then this is used to lookup the corresponding member of the ImageFolder which is then used. ImageFiles, ImageArrays and 2D arrays are used directly as reference images.

Keyword Arguments:
  • method (str) – The method is passed to the Stone.Image.ImageArray.align method to control how the image alignment is done. By default the ‘Scharr’ method is used.

  • box (int, float, tuple of ints or floats) – Specifies a subset of the images to be used to calculate the alignment with.

  • scale (int) – Magnification factor to scale the image by before doing the alignment for better sub=pixel alignments.

Returns:

The aligned ImageFolder.

apply_all(func, *args, **kargs)[source]

Apply function to all images in the stack.

Parameters:
  • func (string or callable) – if string it must be a function reachable by ImageArray

  • quiet (bool) – if False print ‘.’ for every iteration

Note

Further args, kargs are passed through to the function

as_stack()[source]

Return a ImageStack of the images in the current group.

average(weights=None, _box=False, _metadata='first')[source]

Get an array of average pixel values for the stack.

Pass through to numpy average

Keyword Arguments:
  • _box (crop box) – Specifies the region of the array to be averaged. Default - entire image

  • _metadata (str) – Specifies how to generate metadata for the averaged image. - “first”: Just ise the first image’s metadata - “common”: Find the common metadata across all images - “none’: no metadata from images.

Returns:

average(ImageArray) – average values

classmethod from_tiff(filename, **kargs)[source]

Create a new ImageArray from a tiff file.

loadgroup()[source]

Load all files from this group into memory.

mask_select()[source]

Run the ImageFile.mask.select() on each image.

mean(_box=False, _metadata='first')[source]

Calculate the mean value of all the images in the stack.

Keyword Arguments:
  • _box (crop box) – Specifies the region of the array to be averaged. Default - entire image

  • _metadata (str) – Specifies how to generate metadata for the averaged image. - “first”: Just ise the first image’s metadata - “common”: Find the common metadata across all images - “none’: no metadata from images.

Actually a synonym for self.average with not weights

montage(*args, **kargs)[source]

Call the plot method for each metadataObject, but switching to a subplot each time.

Parameters:
Keyword Arguments:
  • extra (callable(i,j,d)) – A callable that can carry out additional processing per plot after the plot is done

  • figsize (tuple(x,y)) – Size of the figure to create

  • dpi (float) – dots per inch on the figure

  • edgecolor,facecolor (matplotlib colour) – figure edge and frame colours.

  • frameon (bool) – Turns figure frames on or off

  • FigureClass (class) – Passed to matplotlib figure call.

  • plots_per_page (int) – maximum number of plots per figure.

Returns:

A list of matplotlib.pyplot.Axes instances.

Notes

If the underlying type of the Stoner.Core.metadataObject instances in the PlotFolder lacks a plot method, then the instances are converted to Stoner.Core.Data.

Each plot is generated as sub-plot on a page. The number of rows and columns of subplots is computed from the aspect ratio of the figure and the number of files in the PlotFolder.

stddev(weights=None, _box=False, _metadata='first')[source]

Calculate weighted standard deviation for stack.

Keyword Arguments:
  • _box (crop box) – Specifies the region of the array to be averaged. Default - entire image

  • _metadata (str) – Specifies how to generate metadata for the averaged image. - “first”: Just ise the first image’s metadata - “common”: Find the common metadata across all images - “none’: no metadata from images.

This is a biased standard deviation, may not be appropriate for small sample sizes

stderr(weights=None, _box=False, _metadata='first')[source]

Calculate standard error in the stack average.

Keyword Arguments:
  • _box (crop box) – Specifies the region of the array to be averaged. Default - entire image

  • _metadata (str) – Specifies how to generate metadata for the averaged image. - “first”: Just ise the first image’s metadata - “common”: Find the common metadata across all images - “none’: no metadata from images.

to_tiff(filename)[source]

Save the ImageArray as a tiff image with metadata.

Parameters:

filename (str) – Filename to save file as.

Note

PIL can save in modes “L” (8bit unsigned int), “I” (32bit signed int), or “F” (32bit signed float). In general max info is preserved for “F” type so if forcetype is not specified then this is the default. For boolean type data mode “L” will suffice and this is chosen in all cases. The type name is added as a string to the metadata before saving.