topostats.utils#

Utilities

Attributes#

Functions#

convert_path(→ pathlib.Path)

Ensure path is Path object.

find_images(→ List)

Scan the specified directory for images with the given file extension.

get_out_path(→ pathlib.Path)

Adds the image path relative to the base directory to the output directory.

update_config(→ Dict)

Update the configuration with any arguments

_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.

folder_grainstats(→ None)

Creates saves a data frame of grain and tracing statictics at the folder level.

Module Contents#

topostats.utils.LOGGER#
topostats.utils.ALL_STATISTICS_COLUMNS = ('Molecule Number', 'centre_x', 'centre_y', 'radius_min', 'radius_max', 'radius_mean',...#
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.find_images(base_dir: str | pathlib.Path = None, file_ext: str = '.spm') List#

Scan the specified directory for images with the given file extension.

Parameters:
  • base_dir (Union[str, Path]) – Directory to recursively search for files, if not specified the current directory is scanned.

  • file_ext (str) – File extension to search for.

Returns:

List of files found with the extension in the given directory.

Return type:

List

topostats.utils.get_out_path(image_path: str | pathlib.Path = None, base_dir: str | pathlib.Path = None, output_dir: str | pathlib.Path = None) pathlib.Path#

Adds the image path relative to the base directory to the output directory.

Parameters:
  • image_path (Path) – The path of the current image.

  • base_dir (Path) – Directory to recursively search for files.

  • output_dir (Path) – The output directory specified in the configuration file.

Returns:

The output path that mirrors the input path structure.

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._get_mask(image: numpy.ndarray, threshold: 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 ‘lower’ with an associated value, second key is to have an ‘upper’ 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 upper and lower thresholds for the standard deviation method.

  • absolute (tuple) – Dict of lower and upper thresholds.

  • **kwargs

Returns:

Dictionary of thresholds, contains keys ‘lower’ and optionally ‘upper’.

Return type:

Dict

topostats.utils.create_empty_dataframe(columns: set = ALL_STATISTICS_COLUMNS) 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

topostats.utils.folder_grainstats(output_dir: str | pathlib.Path, base_dir: str | pathlib.Path, all_stats_df: pandas.DataFrame) None#

Creates saves a data frame of grain and tracing statictics at the folder level.

Parameters:
  • output_dir (Union[str, Path]) – Path of the output directory head.

  • base_dir (Union[str, Path]) – Path of the base directory where files were found.

  • all_stats_df (pd.DataFrame) – The dataframe containing all sample statistics run.

Returns:

This only saves the dataframes and does not retain them.

Return type:

None