retuve.hip_us.metrics.dev

A function for getting all the other metrics that are just for development purposes.

  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"""
 16A function for getting all the other metrics that are just for development purposes.
 17"""
 18
 19from typing import List
 20
 21from retuve.classes.seg import SegFrameObjects
 22from retuve.hip_us.classes.dev import DevMetricsUS
 23from retuve.hip_us.classes.enums import HipLabelsUS
 24from retuve.hip_us.classes.general import HipDatasUS
 25from retuve.keyphrases.config import Config
 26
 27
 28def get_dev_metrics(
 29    hip_datas: HipDatasUS,
 30    results: List[SegFrameObjects],
 31    config: Config,
 32) -> HipDatasUS:
 33    """
 34    Get all the development metrics.
 35
 36    :param hip_datas: HipDatasUS: The HipDatasUS object.
 37    :param results: List[SegFrameObjects]: The results of the segmentation.
 38    :param config: Config: The Config object.
 39
 40    :return: HipDatasUS: The updated HipDatasUS object with the development metrics.
 41    """
 42    dev_metrics = DevMetricsUS()
 43
 44    dev_metrics.graf_frame = hip_datas.graf_frame
 45
 46    ace_marked_hips = []
 47    fem_marked_hips = []
 48
 49    for hip_data, seg_frame_objs in zip(hip_datas, results):
 50        detected = [seg_obj.cls for seg_obj in seg_frame_objs]
 51        detected_bool = any(detected)
 52
 53        if detected_bool:
 54            dev_metrics.no_frames_segmented += 1
 55
 56        if hip_data.marked():
 57            dev_metrics.no_frames_marked += 1
 58
 59        if HipLabelsUS.OsIchium in detected:
 60            dev_metrics.os_ichium_detected = True
 61
 62        if HipLabelsUS.IlliumAndAcetabulum in detected:
 63            ace_marked_hips.append(hip_data)
 64
 65        if HipLabelsUS.FemoralHead in detected:
 66            fem_marked_hips.append(hip_data)
 67
 68    if len(ace_marked_hips) > 0:
 69        dev_metrics.acetabular_mid_frame = ace_marked_hips[
 70            len(ace_marked_hips) // 2
 71        ].frame_no
 72
 73    if len(fem_marked_hips) > 0:
 74        dev_metrics.fem_mid_frame = fem_marked_hips[
 75            len(fem_marked_hips) // 2
 76        ].frame_no
 77
 78    dev_metrics.critial_error = hip_datas.recorded_error.critical
 79
 80    dev_metrics.cr_points = (
 81        [
 82            round(point[2] / (config.hip.z_gap * (200 / len(hip_datas))), 0)
 83            for point in hip_datas.cr_points
 84        ]
 85        if hip_datas.cr_points
 86        else []
 87    )
 88
 89    if dev_metrics.cr_points:
 90        dev_metrics.cr_points = [
 91            dev_metrics.cr_points[2],
 92            dev_metrics.cr_points[1],
 93            dev_metrics.cr_points[0],
 94        ]
 95
 96    dev_metrics.total_frames = len(hip_datas)
 97
 98    hip_datas.dev_metrics = dev_metrics
 99
100    return hip_datas
 29def get_dev_metrics(
 30    hip_datas: HipDatasUS,
 31    results: List[SegFrameObjects],
 32    config: Config,
 33) -> HipDatasUS:
 34    """
 35    Get all the development metrics.
 36
 37    :param hip_datas: HipDatasUS: The HipDatasUS object.
 38    :param results: List[SegFrameObjects]: The results of the segmentation.
 39    :param config: Config: The Config object.
 40
 41    :return: HipDatasUS: The updated HipDatasUS object with the development metrics.
 42    """
 43    dev_metrics = DevMetricsUS()
 44
 45    dev_metrics.graf_frame = hip_datas.graf_frame
 46
 47    ace_marked_hips = []
 48    fem_marked_hips = []
 49
 50    for hip_data, seg_frame_objs in zip(hip_datas, results):
 51        detected = [seg_obj.cls for seg_obj in seg_frame_objs]
 52        detected_bool = any(detected)
 53
 54        if detected_bool:
 55            dev_metrics.no_frames_segmented += 1
 56
 57        if hip_data.marked():
 58            dev_metrics.no_frames_marked += 1
 59
 60        if HipLabelsUS.OsIchium in detected:
 61            dev_metrics.os_ichium_detected = True
 62
 63        if HipLabelsUS.IlliumAndAcetabulum in detected:
 64            ace_marked_hips.append(hip_data)
 65
 66        if HipLabelsUS.FemoralHead in detected:
 67            fem_marked_hips.append(hip_data)
 68
 69    if len(ace_marked_hips) > 0:
 70        dev_metrics.acetabular_mid_frame = ace_marked_hips[
 71            len(ace_marked_hips) // 2
 72        ].frame_no
 73
 74    if len(fem_marked_hips) > 0:
 75        dev_metrics.fem_mid_frame = fem_marked_hips[
 76            len(fem_marked_hips) // 2
 77        ].frame_no
 78
 79    dev_metrics.critial_error = hip_datas.recorded_error.critical
 80
 81    dev_metrics.cr_points = (
 82        [
 83            round(point[2] / (config.hip.z_gap * (200 / len(hip_datas))), 0)
 84            for point in hip_datas.cr_points
 85        ]
 86        if hip_datas.cr_points
 87        else []
 88    )
 89
 90    if dev_metrics.cr_points:
 91        dev_metrics.cr_points = [
 92            dev_metrics.cr_points[2],
 93            dev_metrics.cr_points[1],
 94            dev_metrics.cr_points[0],
 95        ]
 96
 97    dev_metrics.total_frames = len(hip_datas)
 98
 99    hip_datas.dev_metrics = dev_metrics
100
101    return hip_datas

Get all the development metrics.

Parameters
  • hip_datas: HipDatasUS: The HipDatasUS object.
  • results: List[SegFrameObjects]: The results of the segmentation.
  • config: Config: The Config object.
Returns

HipDatasUS: The updated HipDatasUS object with the development metrics.