py4sci

Table Of Contents

Previous topic

compare.py

Next topic

block.py

This Page

ga_analyze.py

Module author: Geoff Hutchison <geoffh@pitt.edu>

ga_analyze.as_ndarray_3D(obj)[source]

Makes sure object is an ndarray with len(shape) = 3

Parameters:obj – arbitrary python object
Returns:numpy.ndarray
ga_analyze.as_ndarray_2D(obj)[source]

Makes sure object is an ndarray with len(shape) = 2

Parameters:obj – arbitrary python object
Returns:numpy.ndarray
ga_analyze.load_image(filename, make3D=False)[source]

Loads data from image file (png, jpg, etc).

Parameters:
  • filename (str) – name of file
  • make3D (bool) – forces object to be ndarray with len(shape)=3
Returns:

numpy.ndarray

ga_analyze.load_npy(filename, make3D=False)[source]

Loads data from numpy file (npy).

Parameters:
  • filename (str) – name of file
  • make3D (bool) – forces object to be ndarray with len(shape)=3
Returns:

numpy.ndarray

ga_analyze.disk_structure(n)[source]

Generate a mathematical morphology structure kernel (i.e., a circle/disk) of radius n

Parameters:n (int) – radius of disk
Returns:structuring kernel
Return type:numpy.ndarray
ga_analyze.square_structure(n)[source]

Generate a mathematical morphology structure kernel (i.e., a square) of size n

Parameters:n (int) – radius of disk
Returns:structuring kernel
Return type:numpy.ndarray

See also

disk_structure()

ga_analyze.granulometry(image, sizes=None, structure=<function disk_structure at 0x7fef975b3ed8>)[source]

Todo

comment function

Parameters:
  • image – data
  • sizes (list[int]) – list of particle sizes to test
Returns:

list of particle counts at each size

Return type:

list[int]

ga_analyze.average_domain_size(image, full=False)[source]

Calculate the average domain size for the image (for the “true” phase). Really, it returns the avg/std (over labeled regions) of the distance to a boundary. The distance average is over the “max of mins”. This means, first a minimum distance is calculated to the boundary for each region. Then the max of those distances is found for each region. Finally, the average of those max’s is taken. Full output returns the avg, the std, a labeled version of the image, the number of unique regions, the min-distance matrix, and the max-distance list.

Parameters:
Returns:

mean and standard deviation of domain sizes

Return type:

tuple(int)

ga_analyze.box_counting_dimension(image)[source]

Calculate the fractal dimension of the edges of the supplied image.

Parameters:image (numpy.ndarray) – data
Returns:slope of best-fit line of the log-log plot
Return type:int

See also

Wikipedia

ga_analyze.interface_size(image, periodic=False)[source]

Calculate the interfacial area (length for 2D images) between the phases. Faster version of interface_size_old() that works on any dimension. Tested for 1D, 2D, and 3D.

Parameters:
  • image (numpy.ndarray) – data
  • periodic (bool) – use periodic boundary conditions
Returns:

total length of interface (in pixels)

Return type:

int

..seealso: test_interface_size(), interface_size_old()

ga_analyze.transfer_distance(original, axis=0, rot90=1, full=False)[source]

Calculate the connectivity of the two phases to the side electrodes and the average “transfer distance” (i.e., the shortest distance a charge carrier must travel to be collected at an electrode).

The transfer distances and connectivity fractions are calculated using a breadth-first search from the electrodes left = white phase, right = black phase.

Parameters:
  • original (numpy.ndarray) – data
  • axis (int) – transfer axis (x=0, y=1, z=2)
  • rot90 (int) – rotate image
  • full (bool) – return more output
Returns:

average transfer distance and connectivity fraction (phase 1 and then phase 2)

Return type:

tuple(float)

ga_analyze.bottleneck_distribution(image)[source]

Count the distribution of bottlenecks

Parameters:image (numpy.ndarray) – data (binary)
Returns:count of bottlenecks of size 4 and size 2
Return type:tuple(int)

modify.py

Modify one or two images with various “mutation” options. - Blend and re-threshold two images - Gaussian Blur - Uniform Blur (preserves edges) - Shrink and tile - Enlarge and tile - Add pepper to a particular phase - Roughen edges - Grow one phase using diffusion-limited growth (fractal noise)

usage: modify.py [-h] [--type type] input1 [input2]

Positional arguments:
ifile1 input file #1
ifile2 input file #2
Options:
--type mutation type

Module author: Geoff Hutchison <geoffh@pitt.edu>

modify.threshold(image)[source]

Threshold an image to return a binary numpy.ndarray with a 50:50 mix of two phases.

Parameters:image (numpy.ndarray) – data
Returns:modified image data
Return type:numpy.ndarray
modify.blend_and_threshold(image1, image2, ratio=0.5, radius=2)[source]

Mix two images, blur and threshold

Parameters:
  • image1 (numpy.ndarray) – data1
  • image2 (numpy.ndarray) – data2
  • ratio (float) – mixing ratio (default is 50:50 mixture)
  • radius (float) – size of uniform blur kernel to use after mixing (default is no blurring)
Returns:

modified image data

Return type:

numpy.ndarray

modify.gblur_and_threshold(image, radius=2)[source]

Blur an image with a Gaussian kernel of supplied radius

Parameters:
Returns:

modified image data

Return type:

numpy.ndarray

modify.ublur_and_threshold(image, radius=2)[source]

Blur an image with a Uniform blur kernel of supplied radius (the uniform filter better preserves edges/boundaries

Parameters:
Returns:

modified image data

Return type:

numpy.ndarray

modify.shrink(image, scale_x=0.0, scale_y=0.0)[source]

Randomly shrink an image using different x and y scales. Re-tile and slice the image to ensure the dimensions remain the same.

Parameters:
  • image (numpy.ndarray) – data
  • scale_x (float) – x-dimension scale factor (or 0.0 for random choice)
  • scale_y (float) – y-dimension scale factor (or 0.0 for random choice)
Returns:

modified image data

Return type:

numpy.ndarray

modify.enlarge(image, zoom_x=0.0, zoom_y=0.0)[source]

Randomly enlarge an image using different x and y scales. Slice the image to ensure the dimensions remain the same.

Parameters:
  • image (numpy.ndarray) – data
  • zoom_x (float) – x-dimension scale factor (or 0.0 for random choice)
  • zoom_y (float) – y-dimension scale factor (or 0.0 for random choice)
Returns:

modified image data

Return type:

numpy.ndarray

modify.pepper(image, phase=0, count=0, percent=None, brush='point', overlap=False, maxTries=1000000000.0)[source]

Add pepper to image.

Parameters:
  • image – data
  • phase – phase ID (0, 1)
  • count – number of pixels to change
  • percent – percent of phase to change (overrides count!)
  • brush – which brush to use (point, square, square3, cross)
  • overlap – allow the brush to paint over existing pixels
Returns:

modified image data

Return type:

numpy.ndarray

modify.roughen(image, fraction=0.5, dilation=0, struct=None)[source]

Roughen the edges of the two phases. Use a binary-closing and dilation to find the edge between phases (ignoring pepper defects) and then flip the phase of a fraction of the resulting edge sites.

Parameters:
  • image (numpy.ndarray) – data
  • fraction (float) – percent of sites to flip
  • dilation (int) – number of times to dilate (expand) the border
  • struct (numpy.ndarray) – structure used for the mathematical morphology operations
Returns:

modified image data

Return type:

numpy.ndarray

modify.grow_ndimage(image, phase=-1, pixToAdd=-1)[source]

Grow points into a minority phase using diffusion-limited fractal growth. The result will ensure 50:50 mixtures and by default will determine the minority phase automatically, but both could be specified.

Parameters:
  • image (numpy.ndarray) – data
  • phase (int) – grey level to grow into (i.e., default 0 = add white into black regions)
  • pixToAdd (int) – number of points to grow, -1 for automatic detection of 50:50 mix
Returns:

modified image data

Return type:

numpy.ndarray

modify.invert(image)[source]

Invert the phases (i.e., black -> white, white->black)

Parameters:image (numpy.ndarray) – data
Returns:modified image data
Return type:numpy.ndarray
modify.add_noise(image)[source]
Parameters:image (numpy.ndarray) – data
Returns:modified image data
Return type:numpy.ndarray
modify.flip_random_sites(image, n=64)[source]

What random noise should be doing?

Parameters:
Returns:

modified image data

Return type:

numpy.ndarray

modify.dialate(image, struct=None)[source]

Grow edges of white phase.

Parameters:image (numpy.ndarray) – data
Returns:modified image data
Return type:numpy.ndarray
modify.erode(image, struct=None)[source]

Erode edges of white phase.

Parameters:image (numpy.ndarray) – data
Returns:modified image data
Return type:numpy.ndarray
modify.opening(image, struct=None)[source]

Open up white phase so that struct fits without overlapping black phase.

Parameters:image (numpy.ndarray) – data
Returns:modified image data
Return type:numpy.ndarray
modify.closing(image, struct=None)[source]

Open up black phase so that struct fits without overlapping white phase.

Parameters:image (numpy.ndarray) – data
Returns:modified image data
Return type:numpy.ndarray
modify.skeletonize_and_reconstruct(image, dfunc=None, sfunc=None)[source]

Deconstruct image into skeleton and distance mask. Alter the skeleton and/or distance mask and then reconstruct a new morphology. With no altering functions passed, this function will not modify the image.

Parameters:
  • image (numpy.ndarray) – data
  • dfunc (func) – function that alters distance mask
  • sfunc (func) – function that alters skeleton
Returns:

modified image data

Return type:

numpy.ndarray

modify.fourier_transform_and_reconstruct(image, detrend=False, window=False, ffunc=None)[source]

Take fourier transform, alter it, and reconstruct image. For some reason this is shifting the origin by 1 pixel after reconstruction, which should not happen.

Parameters:
  • image (numpy.ndarray) – data
  • ffunc (func) – function that alters FFT matrix
Returns:

modified image data

Return type:

numpy.ndarray

modify.randf(a, b, size=None)[source]

Random float in range.

Parameters:
  • a (float) – lower bound
  • b (float) – upper bound
Returns:

values

Return type:

float

modify.gauss1D(x, s=1.0, m=0.0)[source]

1D Gaussian.

Parameters:
  • x – x-value
  • s – sigma
  • m – mean
Returns:

values

Return type:

float

modify.gauss2D(x, y, sx=1.0, sy=1.0, mx=0.0, my=0.0)[source]

2D Gaussian.

Parameters:
  • x – x-value
  • y – y-value
  • sx – x-sigma
  • mx – x-mean
  • sy – y-sigma
  • my – y-mean
Returns:

values

Return type:

float

modify.create_random_distance_transform_on_skeleton(i0, s0, d0)[source]

Draw a bunch of Gaussians of random sigma along skeleton of image.

Parameters:

ga.py

Genetic algorithm design of 2D morphologies.

usage: ga.py [-h] [--rescore] [--silent]
             directory [population] [generations] [children] [mutability]
             [diversity]

Positional arguments:
dir initial directory
population size of population
generations # of generations
children # of children to create each generation
mutability # of items to mutate each generation
diversity percent of pixels that need to differ
Options:
--rescore=False
 rescore initial files
--silent=False set logging level to ERROR only

Module author: Geoff Hutchison <geoffh@pitt.edu>

ga.score(filename)[source]

Score individual image files for the genetic algorithm. The idea is to derive predictive factors for the langmuir performance (i.e., max power) based on the connectivity, phase fractions, domain sizes, etc. The scoring function should be based on multivariate fits from a database of existing simulations. To ensure good results, use robust regression techniques and cross-validate the best-fit.

Parameters:filename (str) – image file name

:return score (ideally as an estimated maximum power in W/(m^2)) :rtype float

ga.score_filenames(filenames, pool=None)[source]

Score a list of files in parallel using the supplied processing pool

Parameters:
  • filenames (list of str) – image file names
  • pool (multiprocessing.pool.Pool) – multiprocessing thread pool
Returns:

list of tuples (score, filename)

Return type:

list of tuple

ga.diversity_check(image1, image2)[source]

Compare two

Parameters:
  • file1 (string) – first filename
  • file2 (string) – second filename
Returns:

fraction of sites that differ