tmas.src.analysis

Module Contents

tmas.src.analysis.build_drug_info_dict(plate_info: Dict[str, List[List[str | float]]]) Dict[str, Dict[str, List[str | float]]][source]

Build a dictionary containing drug information based on plate data.

This function processes the plate_info dictionary. It returns a dictionary where each drug is a key, and the corresponding value is another dictionary containing sorted lists of dilutions and concentrations.

Parameters:

plate_info (dict) – A dictionary containing the following keys: - “drug_matrix”: - “dilution_matrix”: - “conc_matrix”:

Returns:

A dictionary where each key is a drug name, and the value is another dictionary containing: - “dilutions” - “concentrations”

Return type:

dict

Raises:

KeyError – If any of the expected keys (“drug_matrix”, “dilution_matrix”, “conc_matrix”) are missing from plate_info.

tmas.src.analysis.visualize_growth_matrix(image_name: str, img: Any, growth_matrix: List[List[str]], drug_info: Dict[str, Dict[str, List[str | float]]], drug_results: Dict[str, Dict[str, str | List[str]]], plate_info: Dict[str, List[List[str | float]]], output_directory: str, skip_well_results: List[str], show: bool = False) None[source]

Visualize the growth matrix on a drug susceptibility testing plate image.

This function overlays the growth detection information on an image of a microtitre plate, highlighting wells with detected growth, and annotating the wells with the corresponding drug names and concentrations. The visualized image is saved to the specified output directory, and optionally displayed based on the show parameter.

Parameters:
  • image_name (str) – The base name of the image, used for labeling and saving results.

  • img (numpy.ndarray) – The image of the plate. If the image is in grayscale or BGR format, it will be converted to RGB.

  • growth_matrix (list[list[str]]) – A matrix indicating the growth status of each well (‘growth’ or ‘-none-‘).

  • drug_info (dict) – A dictionary containing drug information, including dilutions and concentrations for each drug.

  • drug_results (dict) – A dictionary containing the results of the drug susceptibility test, including MIC values for each drug.

  • plate_info (dict) – A dictionary containing plate-specific information, including drug and concentration matrices.

  • output_directory (str) – The directory where the visualization image will be saved.

  • skip_well_results – List of drugs that have abnormal growth

  • show (bool) – Boolean flag indicating whether to display the image after saving. If True, the image is displayed.

Returns:

None

Return type:

None

tmas.src.analysis.save_mic_results(data: List[Dict[str, str | float]], format_type: str, filename: str, output_directory: str) None[source]

Save MIC results in the specified format to the specified directory, appending to existing files if they already exist.

Parameters:
  • data – List of dictionaries containing drug, results, MIC, and image name.

  • format_type – ‘csv’ or ‘json’ for the file format.

  • filename – Base filename for the output file.

  • output_directory (str) – The directory where the results should be saved.

tmas.src.analysis.analyze_growth_matrix(image_name: str, image: Any, growth_matrix: List[List[str]], plate_design: Dict[str, List[List[str | float]]], plate_design_type: str, output_directory: str, show: bool = False) Dict[str, Dict[str, str | List[str]]] | None[source]

Analyze the growth matrix and determine the Minimum Inhibitory Concentration (MIC) for each drug.

This function processes the growth matrix of a microtitre plate, compares it with the provided plate design, and calculates the MIC values for each drug tested. It also visualizes the results by overlaying growth detection information on the image of the plate, optionally displaying the visualization.

Parameters:
  • image_name (str) – The name of the image file used for labeling and saving results.

  • image (numpy.ndarray) – The image data of the plate, typically as a NumPy array.

  • growth_matrix (list[list[str]]) – A matrix indicating the growth status of each well (e.g., ‘growth’ or ‘-none-‘).

  • plate_design (dict) – A dictionary containing the design of the plate, including drug, dilution, and concentration matrices.

  • plate_design_type (str) – A string representing the type of plate design being used.

  • output_directory (str) – The directory where the visualization image and results should be saved.

  • show (bool) – A boolean flag indicating whether to display the visualization of the growth matrix.

Returns:

A dictionary containing the growth results and MIC values for each drug. The dictionary keys are drug names, and the values are dictionaries with ‘growth_array’ and ‘MIC’ keys.

Return type:

Optional[dict]

Raises:

ValueError – If the image is not in a supported format or if there are issues with the plate design data.

tmas.src.analysis.extract_image_name_from_path(image_path: str) str[source]

Extract the base name of an image file from a file path and remove specific suffixes.

This function takes a full file path to an image, extracts the base name of the file without its extension, and removes the suffixes ‘-filtered’ or ‘-raw’ from the base name if they are present. This is useful for standardizing image file names during processing.

Parameters:

image_path (str) – The full file path to the image file.

Returns:

The standardized base name of the image file without the extension and without ‘-filtered’ or ‘-raw’ suffixes.

Return type:

str

tmas.src.analysis.extract_plate_design_type_from_image_name(image_name: str) str[source]

Extract the plate design type from an image name.

Assumes that the plate design type is always the 6th element in a hyphen-separated image name string.

Parameters:

image_name (str) – The image name string, typically in a hyphen-separated format.

Returns:

The plate design type extracted from the image name.

Return type:

str

tmas.src.analysis.analyze_and_extract_mic(image_path: str, image: Any, detections: List[List[str]], plate_design: Dict[str, Dict[str, List[List[str | float]]]], format_type: str, output_directory: str, show: bool = False) Dict[str, Dict[str, str | List[str]]] | None[source]

Analyze the growth matrix and extract Minimum Inhibitory Concentration (MIC) results.

This function analyzes the growth detection results from an image of a microtitre plate to determine the Minimum Inhibitory Concentration (MIC) values for various drugs. It processes the image data and detection results based on the provided plate design and saves the results in the specified format.

Parameters:
  • image_path (str) – The full file path to the image to be processed.

  • image (Any) – The image data as a NumPy array, typically representing a microtitre plate.

  • detections (List[List[str]]) – A list of detection results indicating the growth status for each well on the plate.

  • plate_design (Dict[str, Dict[str, List[List[Union[str, float]]]]]) – A dictionary containing the plate design information, indexed by plate design type.

  • format_type (str) – The format in which the MIC results should be saved, either ‘csv’ or ‘json’.

  • output_directory (str) – The directory where the processed results and visualization should be saved.

  • show (bool) – A boolean flag indicating whether to display the visualization images. Defaults to False.

Returns:

A dictionary containing the MIC results and growth information for each drug, or None if the analysis fails.

Return type:

Optional[Dict[str, Dict[str, Union[str, List[str]]]]]