retuve.utils

Utility functions for the Retuve package.

 1# Copyright 2024 Adam McArthur
 2#
 3# Licensed under the Apache License, Version 2.0 (the "License");
 4# you may not use this file except in compliance with the License.
 5# You may obtain a copy of the License at
 6#
 7#     http://www.apache.org/licenses/LICENSE-2.0
 8#
 9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""
16Utility functions for the Retuve package.
17"""
18
19import logging
20import os
21import statistics
22import sys
23from typing import List, Tuple
24
25from retuve.logs import ulogger
26from retuve.typehints import MidLine
27
28if getattr(sys, "frozen", False):
29    # If the application is run as a bundle, the PyInstaller bootloader
30    # extends the sys module by a flag frozen=True and sets the app
31    # path into variable _MEIPASS'.
32    RETUVE_DIR = sys._MEIPASS
33else:
34    RETUVE_DIR = os.path.dirname(os.path.realpath(__file__))
35
36
37def register_config_dirs(config, other_dirs=[]):
38    hippa_log_file_dir = os.path.dirname(config.api.hippa_logging_file)
39    sql_path = os.path.dirname(config.api.db_path)
40    dirs = [
41        config.api.savedir,
42        config.api.upload_dir,
43        hippa_log_file_dir,
44        sql_path,
45    ]
46
47    dirs.extend(other_dirs)
48    dirs.extend(config.trak.datasets)
49    dirs.extend(config.batch.datasets)
50
51    for dir in dirs:
52        if dir and not os.path.exists(dir):
53            ulogger.info(f"Creating directory: {dir}")
54            os.makedirs(dir)
55
56
57def rmean(values: List[float]) -> float:
58    """
59    Calculate the rounded mean of a list of values.
60
61    :param values: The list of values to calculate the mean of.
62    :return: The rounded mean of the values.
63    """
64    return round(statistics.mean(values), 2)
65
66
67def find_midline_extremes(
68    midline: MidLine,
69) -> Tuple[Tuple[int, int], Tuple[int, int]]:
70    """
71    Finds the left-most and right-most white pixels in the midline.
72
73    :param midline: The midline to find the extremes of.
74    :return: The left-most and right-most white pixels.
75    """
76    # Identifying the left-most and right-most white pixels
77    if midline.size > 0:
78        left_most = midline[midline[:, 1].argmin()]
79        right_most = midline[midline[:, 1].argmax()]
80        return left_most, right_most
81    else:
82        logging.warning("No white pixels found in the image.")
83        return None, None
def register_config_dirs(config, other_dirs=[]):
38def register_config_dirs(config, other_dirs=[]):
39    hippa_log_file_dir = os.path.dirname(config.api.hippa_logging_file)
40    sql_path = os.path.dirname(config.api.db_path)
41    dirs = [
42        config.api.savedir,
43        config.api.upload_dir,
44        hippa_log_file_dir,
45        sql_path,
46    ]
47
48    dirs.extend(other_dirs)
49    dirs.extend(config.trak.datasets)
50    dirs.extend(config.batch.datasets)
51
52    for dir in dirs:
53        if dir and not os.path.exists(dir):
54            ulogger.info(f"Creating directory: {dir}")
55            os.makedirs(dir)
def rmean(values: List[float]) -> float:
58def rmean(values: List[float]) -> float:
59    """
60    Calculate the rounded mean of a list of values.
61
62    :param values: The list of values to calculate the mean of.
63    :return: The rounded mean of the values.
64    """
65    return round(statistics.mean(values), 2)

Calculate the rounded mean of a list of values.

Parameters
  • values: The list of values to calculate the mean of.
Returns

The rounded mean of the values.

def find_midline_extremes( midline: List[Tuple[int, int]]) -> Tuple[Tuple[int, int], Tuple[int, int]]:
68def find_midline_extremes(
69    midline: MidLine,
70) -> Tuple[Tuple[int, int], Tuple[int, int]]:
71    """
72    Finds the left-most and right-most white pixels in the midline.
73
74    :param midline: The midline to find the extremes of.
75    :return: The left-most and right-most white pixels.
76    """
77    # Identifying the left-most and right-most white pixels
78    if midline.size > 0:
79        left_most = midline[midline[:, 1].argmin()]
80        right_most = midline[midline[:, 1].argmax()]
81        return left_most, right_most
82    else:
83        logging.warning("No white pixels found in the image.")
84        return None, None

Finds the left-most and right-most white pixels in the midline.

Parameters
  • midline: The midline to find the extremes of.
Returns

The left-most and right-most white pixels.