retuve.hip_xray.landmarks

This module contains the function to convert landmarks to metrics for hip x-ray.

 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"""
16This module contains the function to convert landmarks to metrics for hip x-ray.
17"""
18
19import time
20from typing import List
21
22from retuve.classes.metrics import Metric2D
23from retuve.hip_xray.classes import HipDataXray, LandmarksXRay
24from retuve.hip_xray.metrics.ace import find_ace
25from retuve.keyphrases.config import Config
26from retuve.logs import log_timings
27
28
29def landmarks_2_metrics_xray(
30    list_landmarks: List[LandmarksXRay],
31    config: Config,
32) -> List[HipDataXray]:
33    """
34    Convert the landmarks to metrics for hip x-ray
35
36    :param list_landmarks: The List of Landmarks
37    :param config: The Config
38
39    :return: The List of Hip Data
40    """
41    hips = []
42    timings = []
43
44    for frame_no, landmarks in enumerate(list_landmarks):
45        start = time.time()
46
47        hip = HipDataXray()
48
49        if landmarks.fem_l is None:
50            ace_l, ace_r = None, None
51            hip.recorded_error.append("No landmarks found.")
52        else:
53            ace_l, ace_r = find_ace(landmarks)
54
55        for name, value in [
56            (
57                "ace_index_left",
58                ace_l,
59            ),
60            (
61                "ace_index_right",
62                ace_r,
63            ),
64        ]:
65            hip.metrics.append(Metric2D(name, value))
66
67        hip.landmarks = landmarks
68        hip.frame_no = frame_no
69
70        hips.append(hip)
71        timings.append(time.time() - start)
72
73    log_timings(timings, title="Landmarks->Metrics Speed:")
74
75    return hips
def landmarks_2_metrics_xray( list_landmarks: List[retuve.hip_xray.classes.LandmarksXRay], config: retuve.keyphrases.config.Config) -> List[retuve.hip_xray.classes.HipDataXray]:
30def landmarks_2_metrics_xray(
31    list_landmarks: List[LandmarksXRay],
32    config: Config,
33) -> List[HipDataXray]:
34    """
35    Convert the landmarks to metrics for hip x-ray
36
37    :param list_landmarks: The List of Landmarks
38    :param config: The Config
39
40    :return: The List of Hip Data
41    """
42    hips = []
43    timings = []
44
45    for frame_no, landmarks in enumerate(list_landmarks):
46        start = time.time()
47
48        hip = HipDataXray()
49
50        if landmarks.fem_l is None:
51            ace_l, ace_r = None, None
52            hip.recorded_error.append("No landmarks found.")
53        else:
54            ace_l, ace_r = find_ace(landmarks)
55
56        for name, value in [
57            (
58                "ace_index_left",
59                ace_l,
60            ),
61            (
62                "ace_index_right",
63                ace_r,
64            ),
65        ]:
66            hip.metrics.append(Metric2D(name, value))
67
68        hip.landmarks = landmarks
69        hip.frame_no = frame_no
70
71        hips.append(hip)
72        timings.append(time.time() - start)
73
74    log_timings(timings, title="Landmarks->Metrics Speed:")
75
76    return hips

Convert the landmarks to metrics for hip x-ray

Parameters
  • list_landmarks: The List of Landmarks
  • config: The Config
Returns

The List of Hip Data