Utils API

cohere_core.config_verifier

Verification of configuration parameters.

cohere_core.config_verifier.verify(file_name, conf_map)

Verifies parameters.

Parameters:
  • file_name (str) – name of file the parameters are related to. Supported: config_prep, config_data, config_rec, config_disp

  • conf_map (dict) – parameters

Returns:

a message with description of error or empty string if no error

Return type:

str

cohere_core.utils

This module is a suite of utility functions.

cohere_core.utils.adjust_dimensions(arr, pads)

This function adds to or subtracts from each dimension of the array elements defined by pad. If the pad is positive, the array is padded in this dimension. If the pad is negative, the array is cropped. The dimensions of the new array is then adjusted to be supported by the opencl library.

Parameters:
  • arr (ndarray) – the array to pad/crop

  • pad (list) – list of three pad values, for each dimension

Returns:

the padded/cropped array

Return type:

ndarray

cohere_core.utils.arr_property(arr)

Used only in development. Prints max value of the array and max coordinates.

Parameters:

arr (ndarray) – array to find max

cohere_core.utils.binning(array, binsizes)

This function does the binning of the array. The array is binned in each dimension by the corresponding binsizes elements. If binsizes list is shorter than the array dimensions, the remaining dimensions are not binned.

Parameters:
  • array (ndarray) – the original array to be binned

  • binsizes (list) – a list defining binning factors for corresponding dimensions

Returns:

binned array

Return type:

ndarray

cohere_core.utils.gauss_conv_fft(arr, sigmas)

Calculates convolution of array with the Gaussian.

A Guassian distribution grid is calculated with the array dimensions. Fourier transform of the array is multiplied by the grid and inverse transformed. A correction is calculated and applied.

Parameters:
  • arr (ndarray) – subject array

  • sigmas (list) – sigmas in all dimensions

Returns:

convolution array

Return type:

ndarray

cohere_core.utils.gaussian(shape, sigmas, alpha=1)

Calculates Gaussian distribution grid in given dimensions.

Parameters:
  • shape (tuple) – shape of the grid

  • sigmas (list) – sigmas in all dimensions

  • alpha (float) – a multiplier

Returns:

Gaussian distribution grid

Return type:

ndarray

cohere_core.utils.get_centered(arr, center_shift)
This function finds maximum value in the array, and puts it in a center of a new array. If center_shift is not zeros,

the array will be shifted accordingly. The shifted elements are rolled into the other end of array.

Parameters:
  • arr (ndarray) – the original array to be centered

  • center_shift (list) – a list defining shift of the center

Returns:

centered array

Return type:

ndarray

cohere_core.utils.get_good_dim(dim)

This function calculates the dimension supported by opencl library (i.e. is multiplier of 2, 3, or 5) and is closest to the given starting dimension. If the dimension is not supported the function adds 1 and verifies the new dimension. It iterates until it finds supported value.

Parameters:

dim (int) – initial dimension

Returns:

a dimension that is supported by the opencl library, and closest to the original dimension

Return type:

int

cohere_core.utils.get_logger(name, ldir='')

Creates looger instance that will write to default.log file in a given directory. :param name: logger name :type name: str :param ldir: directory where to create log file :type ldir: str

Returns:

logger object from logging module

Return type:

logger

cohere_core.utils.get_zero_padded_centered(arr, new_shape)

This function pads the array with zeros to the new shape with the array in the center. :param arr: the original array to be padded :type arr: ndarray :param new_shape: new dimensions :type new_shape: tuple

Returns:

the zero padded centered array

Return type:

ndarray

cohere_core.utils.read_config(config)

This function gets configuration file. It checks if the file exists and parses it into a dictionary.

Parameters:

config (str) – configuration file name

Returns:

dictionary containing parsed configuration, None if the given file does not exist

Return type:

dict

cohere_core.utils.read_results(read_dir)

Reads results and returns array representation.

Parameters:

read_dir (str) – directory to read the results from

Returns:

image, support, and coherence arrays

Return type:

ndarray, ndarray, ndarray (or None)

cohere_core.utils.read_tif(filename)

This method reads tif type file and returns the data as ndarray.

Parameters:

filename (str) – tif format file name

Returns:

an array containing the data parsed from the file

Return type:

ndarray

cohere_core.utils.save_metrics(errs, dir, metrics=None)

Saves arrays metrics and errors by iterations in text file.

Parameters:
  • errs (list) – list of “chi” error by iteration

  • dir (str) – directory to write the file containing array metrics

  • metrics (dict) – dictionary with metric type keys, and metric values

cohere_core.utils.save_results(image, support, coh, errs, save_dir, metric=None)

Saves results of reconstruction. Saves the following files: image.np, support.npy, errors.npy, optionally coherence.npy, plot_errors.py, graph.npy, flow.npy, iter_array.npy

Parameters:
  • image (ndarray) – reconstructed image array

  • support (ndarray) – support array related to the image

  • coh (ndarray) – coherence array when pcdi feature is active, None otherwise

  • errs (ndarray) – errors “chi” by iterations

  • save_dir (str) – directory to write the files

  • metrics (dict) – dictionary with metric type keys, and metric values

cohere_core.utils.save_tif(arr, filename)

This method saves array in tif format file.

Parameters:
  • arr (ndarray) – array to save

  • filename (str) – tif format file name

cohere_core.utils.write_plot_errors(save_dir)

Creates python executable that draw plot of error by iteration. It assumes that the given directory contains “errors.npy” file

Parameters:

save_dir (str) – directory containing errors.npy file