topostats.utils#

Utilities.

Attributes#

Functions#

convert_path(→ pathlib.Path)

Ensure path is Path object.

update_config(→ dict)

Update the configuration with any arguments.

update_plotting_config(→ dict)

Update the plotting config for each of the plots in plot_dict.

_get_mask(→ numpy.ndarray)

Calculate a mask for pixels that exceed the threshold.

get_mask(→ numpy.ndarray)

Mask data that should not be included in flattening.

get_thresholds(→ dict)

Obtain thresholds for masking data points.

create_empty_dataframe(→ pandas.DataFrame)

Create an empty data frame for returning when no results are found.

Module Contents#

topostats.utils.LOGGER#
topostats.utils.ALL_STATISTICS_COLUMNS = ('image', 'basename', 'molecule_number', 'area', 'area_cartesian_bbox', 'aspect_ratio',...#
topostats.utils.convert_path(path: str | pathlib.Path) pathlib.Path[source]#

Ensure path is Path object.

Parameters:

path (Union[str, Path]) – Path to be converted.

Returns:

pathlib Path

Return type:

Path

topostats.utils.update_config(config: dict, args: dict | argparse.Namespace) dict[source]#

Update the configuration with any arguments.

Parameters:
  • config (dict) – Dictionary of configuration (typically read from YAML file specified with ‘-c/–config <filename>’)

  • args (Namespace) – Command line arguments

Returns:

Dictionary updated with command arguments.

Return type:

Dict

topostats.utils.update_plotting_config(plotting_config: dict) dict[source]#

Update the plotting config for each of the plots in plot_dict.

Ensures that each entry has all the plotting configuration values that are needed.

topostats.utils._get_mask(image: numpy.ndarray, thresh: float, threshold_direction: str, img_name: str = None) numpy.ndarray[source]#

Calculate a mask for pixels that exceed the threshold.

Parameters:
  • image (np.array) – Numpy array representing image.

  • threshold (float) – A float representing the threshold

  • threshold_direction (str) – A string representing the direction that should be thresholded. (“above”, “below”)

  • img_name (str) – Name of image being processed

Returns:

Numpy array of image with objects coloured.

Return type:

np.array

topostats.utils.get_mask(image: numpy.ndarray, thresholds: dict, img_name: str = None) numpy.ndarray[source]#

Mask data that should not be included in flattening.

Parameters:
  • image (np.ndarray) – 2D Numpy array of the image to have a mask derived for.

  • thresholds (dict) – Dictionary of thresholds, at a bare minimum must have key ‘below’ with an associated value, second key is to have an ‘above’ threshold.

  • img_name (str) – Image name that is being masked.

Returns:

2D Numpy boolean array of points to mask.

Return type:

np.ndarray

topostats.utils.get_thresholds(image: numpy.ndarray, threshold_method: str, otsu_threshold_multiplier: float = None, threshold_std_dev: dict = None, absolute: dict = None, **kwargs) dict[source]#

Obtain thresholds for masking data points.

Parameters:
  • image (np.ndarray) – 2D Numpy array of image to be masked

  • threshold_method (str) – Method for thresholding, ‘otsu’, ‘std_dev’ or ‘absolute’ are valid options.

  • threshold_std_dev (dict) – Dict of above and below thresholds for the standard deviation method.

  • absolute (tuple) – Dict of below and above thresholds.

  • **kwargs

Returns:

Dictionary of thresholds, contains keys ‘below’ and optionally ‘above’.

Return type:

Dict

topostats.utils.create_empty_dataframe(columns: set = ALL_STATISTICS_COLUMNS, index: tuple = 'molecule_number') pandas.DataFrame[source]#

Create an empty data frame for returning when no results are found.

Parameters:

columns (list) – Columns of the empty dataframe.

Returns:

Empty Pandas DataFrame.

Return type:

pd.DataFrame