from __future__ import annotations

import gc
import os
import time

import numpy as np
import pytest
import supervision as sv
from inference.core.workflows.core_steps.common.utils import \
    add_inference_keypoints_to_sv_detections
from inference.core.workflows.execution_engine.constants import (
    KEYPOINTS_CLASS_ID_KEY_IN_INFERENCE_RESPONSE,
    KEYPOINTS_CLASS_ID_KEY_IN_SV_DETECTIONS,
    KEYPOINTS_CLASS_NAME_KEY_IN_INFERENCE_RESPONSE,
    KEYPOINTS_CLASS_NAME_KEY_IN_SV_DETECTIONS,
    KEYPOINTS_CONFIDENCE_KEY_IN_INFERENCE_RESPONSE,
    KEYPOINTS_CONFIDENCE_KEY_IN_SV_DETECTIONS,
    KEYPOINTS_KEY_IN_INFERENCE_RESPONSE, KEYPOINTS_XY_KEY_IN_SV_DETECTIONS,
    X_KEY, Y_KEY)


def codeflash_wrap(codeflash_wrapped, codeflash_test_module_name, codeflash_test_class_name, codeflash_test_name, codeflash_function_name, codeflash_line_id, codeflash_loop_index, *args, **kwargs):
    test_id = f'{codeflash_test_module_name}:{codeflash_test_class_name}:{codeflash_test_name}:{codeflash_line_id}:{codeflash_loop_index}'
    if not hasattr(codeflash_wrap, 'index'):
        codeflash_wrap.index = {}
    if test_id in codeflash_wrap.index:
        codeflash_wrap.index[test_id] += 1
    else:
        codeflash_wrap.index[test_id] = 0
    codeflash_test_index = codeflash_wrap.index[test_id]
    invocation_id = f'{codeflash_line_id}_{codeflash_test_index}'
    test_stdout_tag = f"{codeflash_test_module_name}:{(codeflash_test_class_name + '.' if codeflash_test_class_name else '')}{codeflash_test_name}:{codeflash_function_name}:{codeflash_loop_index}:{invocation_id}"
    print(f'!$######{test_stdout_tag}######$!')
    exception = None
    gc.disable()
    try:
        counter = time.perf_counter_ns()
        return_value = codeflash_wrapped(*args, **kwargs)
        codeflash_duration = time.perf_counter_ns() - counter
    except Exception as e:
        codeflash_duration = time.perf_counter_ns() - counter
        exception = e
    gc.enable()
    print(f'!######{test_stdout_tag}:{codeflash_duration}######!')
    if exception:
        raise exception
    return return_value

def test_single_detection_with_single_keypoint():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test adding a single keypoint to a single detection.'
    detections = sv.Detections(xyxy=np.array([[10.0, 20.0, 30.0, 40.0]]), confidence=np.array([0.9]), class_id=np.array([0]))
    inference_prediction = [{KEYPOINTS_KEY_IN_INFERENCE_RESPONSE: [{KEYPOINTS_CLASS_NAME_KEY_IN_INFERENCE_RESPONSE: 'head', KEYPOINTS_CLASS_ID_KEY_IN_INFERENCE_RESPONSE: 1, KEYPOINTS_CONFIDENCE_KEY_IN_INFERENCE_RESPONSE: 0.95, X_KEY: 15.0, Y_KEY: 25.0}]}]
    codeflash_return_value = codeflash_wrap(add_inference_keypoints_to_sv_detections, 'tests.inference.unit_tests.test_add_inference_keypoints_to_sv_detections__unit_test_1', None, 'test_single_detection_with_single_keypoint', 'add_inference_keypoints_to_sv_detections', '3', codeflash_loop_index, inference_prediction, detections)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_single_detection_with_multiple_keypoints():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test adding multiple keypoints to a single detection.'
    detections = sv.Detections(xyxy=np.array([[0.0, 0.0, 100.0, 100.0]]), confidence=np.array([0.9]), class_id=np.array([0]))
    inference_prediction = [{KEYPOINTS_KEY_IN_INFERENCE_RESPONSE: [{KEYPOINTS_CLASS_NAME_KEY_IN_INFERENCE_RESPONSE: 'head', KEYPOINTS_CLASS_ID_KEY_IN_INFERENCE_RESPONSE: 1, KEYPOINTS_CONFIDENCE_KEY_IN_INFERENCE_RESPONSE: 0.95, X_KEY: 50.0, Y_KEY: 50.0}, {KEYPOINTS_CLASS_NAME_KEY_IN_INFERENCE_RESPONSE: 'left_eye', KEYPOINTS_CLASS_ID_KEY_IN_INFERENCE_RESPONSE: 2, KEYPOINTS_CONFIDENCE_KEY_IN_INFERENCE_RESPONSE: 0.92, X_KEY: 40.0, Y_KEY: 45.0}, {KEYPOINTS_CLASS_NAME_KEY_IN_INFERENCE_RESPONSE: 'right_eye', KEYPOINTS_CLASS_ID_KEY_IN_INFERENCE_RESPONSE: 3, KEYPOINTS_CONFIDENCE_KEY_IN_INFERENCE_RESPONSE: 0.93, X_KEY: 60.0, Y_KEY: 45.0}]}]
    codeflash_return_value = codeflash_wrap(add_inference_keypoints_to_sv_detections, 'tests.inference.unit_tests.test_add_inference_keypoints_to_sv_detections__unit_test_1', None, 'test_single_detection_with_multiple_keypoints', 'add_inference_keypoints_to_sv_detections', '3', codeflash_loop_index, inference_prediction, detections)
    codeflash_output = codeflash_return_value
    result = codeflash_output
    for i in range(3):
        pass

def test_multiple_detections_with_varying_keypoint_counts():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test adding different numbers of keypoints to different detections.'
    detections = sv.Detections(xyxy=np.array([[0.0, 0.0, 10.0, 10.0], [20.0, 20.0, 30.0, 30.0], [40.0, 40.0, 50.0, 50.0]]), confidence=np.array([0.9, 0.8, 0.85]), class_id=np.array([0, 1, 0]))
    inference_prediction = [{KEYPOINTS_KEY_IN_INFERENCE_RESPONSE: [{KEYPOINTS_CLASS_NAME_KEY_IN_INFERENCE_RESPONSE: 'kp1', KEYPOINTS_CLASS_ID_KEY_IN_INFERENCE_RESPONSE: 1, KEYPOINTS_CONFIDENCE_KEY_IN_INFERENCE_RESPONSE: 0.9, X_KEY: 5.0, Y_KEY: 5.0}, {KEYPOINTS_CLASS_NAME_KEY_IN_INFERENCE_RESPONSE: 'kp2', KEYPOINTS_CLASS_ID_KEY_IN_INFERENCE_RESPONSE: 2, KEYPOINTS_CONFIDENCE_KEY_IN_INFERENCE_RESPONSE: 0.85, X_KEY: 6.0, Y_KEY: 6.0}]}, {KEYPOINTS_KEY_IN_INFERENCE_RESPONSE: [{KEYPOINTS_CLASS_NAME_KEY_IN_INFERENCE_RESPONSE: 'kp1', KEYPOINTS_CLASS_ID_KEY_IN_INFERENCE_RESPONSE: 1, KEYPOINTS_CONFIDENCE_KEY_IN_INFERENCE_RESPONSE: 0.91, X_KEY: 25.0, Y_KEY: 25.0}, {KEYPOINTS_CLASS_NAME_KEY_IN_INFERENCE_RESPONSE: 'kp2', KEYPOINTS_CLASS_ID_KEY_IN_INFERENCE_RESPONSE: 2, KEYPOINTS_CONFIDENCE_KEY_IN_INFERENCE_RESPONSE: 0.88, X_KEY: 26.0, Y_KEY: 26.0}, {KEYPOINTS_CLASS_NAME_KEY_IN_INFERENCE_RESPONSE: 'kp3', KEYPOINTS_CLASS_ID_KEY_IN_INFERENCE_RESPONSE: 3, KEYPOINTS_CONFIDENCE_KEY_IN_INFERENCE_RESPONSE: 0.87, X_KEY: 27.0, Y_KEY: 27.0}]}, {KEYPOINTS_KEY_IN_INFERENCE_RESPONSE: [{KEYPOINTS_CLASS_NAME_KEY_IN_INFERENCE_RESPONSE: 'kp1', KEYPOINTS_CLASS_ID_KEY_IN_INFERENCE_RESPONSE: 1, KEYPOINTS_CONFIDENCE_KEY_IN_INFERENCE_RESPONSE: 0.92, X_KEY: 45.0, Y_KEY: 45.0}]}]
    codeflash_return_value = codeflash_wrap(add_inference_keypoints_to_sv_detections, 'tests.inference.unit_tests.test_add_inference_keypoints_to_sv_detections__unit_test_1', None, 'test_multiple_detections_with_varying_keypoint_counts', 'add_inference_keypoints_to_sv_detections', '3', codeflash_loop_index, inference_prediction, detections)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_detection_returns_original_detection_object():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that the function returns the same detections object (modified in-place).'
    detections = sv.Detections(xyxy=np.array([[0.0, 0.0, 10.0, 10.0]]), confidence=np.array([0.9]), class_id=np.array([0]))
    original_id = id(detections)
    inference_prediction = [{KEYPOINTS_KEY_IN_INFERENCE_RESPONSE: [{KEYPOINTS_CLASS_NAME_KEY_IN_INFERENCE_RESPONSE: 'kp', KEYPOINTS_CLASS_ID_KEY_IN_INFERENCE_RESPONSE: 1, KEYPOINTS_CONFIDENCE_KEY_IN_INFERENCE_RESPONSE: 0.9, X_KEY: 5.0, Y_KEY: 5.0}]}]
    codeflash_return_value = codeflash_wrap(add_inference_keypoints_to_sv_detections, 'tests.inference.unit_tests.test_add_inference_keypoints_to_sv_detections__unit_test_1', None, 'test_detection_returns_original_detection_object', 'add_inference_keypoints_to_sv_detections', '4', codeflash_loop_index, inference_prediction, detections)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_empty_inference_prediction_with_empty_detections():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test with empty inference predictions and empty detections.'
    detections = sv.Detections.empty()
    inference_prediction = []
    codeflash_return_value = codeflash_wrap(add_inference_keypoints_to_sv_detections, 'tests.inference.unit_tests.test_add_inference_keypoints_to_sv_detections__unit_test_1', None, 'test_empty_inference_prediction_with_empty_detections', 'add_inference_keypoints_to_sv_detections', '3', codeflash_loop_index, inference_prediction, detections)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_single_detection_with_no_keypoints():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test detection with empty keypoints list.'
    detections = sv.Detections(xyxy=np.array([[0.0, 0.0, 10.0, 10.0]]), confidence=np.array([0.9]), class_id=np.array([0]))
    inference_prediction = [{KEYPOINTS_KEY_IN_INFERENCE_RESPONSE: []}]
    codeflash_return_value = codeflash_wrap(add_inference_keypoints_to_sv_detections, 'tests.inference.unit_tests.test_add_inference_keypoints_to_sv_detections__unit_test_1', None, 'test_single_detection_with_no_keypoints', 'add_inference_keypoints_to_sv_detections', '3', codeflash_loop_index, inference_prediction, detections)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_multiple_detections_all_without_keypoints():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test multiple detections with no keypoints.'
    detections = sv.Detections(xyxy=np.array([[0.0, 0.0, 10.0, 10.0], [20.0, 20.0, 30.0, 30.0], [40.0, 40.0, 50.0, 50.0]]), confidence=np.array([0.9, 0.8, 0.85]), class_id=np.array([0, 1, 0]))
    inference_prediction = [{KEYPOINTS_KEY_IN_INFERENCE_RESPONSE: []}, {KEYPOINTS_KEY_IN_INFERENCE_RESPONSE: []}, {KEYPOINTS_KEY_IN_INFERENCE_RESPONSE: []}]
    codeflash_return_value = codeflash_wrap(add_inference_keypoints_to_sv_detections, 'tests.inference.unit_tests.test_add_inference_keypoints_to_sv_detections__unit_test_1', None, 'test_multiple_detections_all_without_keypoints', 'add_inference_keypoints_to_sv_detections', '3', codeflash_loop_index, inference_prediction, detections)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_inference_prediction_missing_keypoints_key():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    "Test when inference prediction doesn't contain KEYPOINTS_KEY_IN_INFERENCE_RESPONSE."
    detections = sv.Detections(xyxy=np.array([[0.0, 0.0, 10.0, 10.0]]), confidence=np.array([0.9]), class_id=np.array([0]))
    inference_prediction = [{}]
    codeflash_return_value = codeflash_wrap(add_inference_keypoints_to_sv_detections, 'tests.inference.unit_tests.test_add_inference_keypoints_to_sv_detections__unit_test_1', None, 'test_inference_prediction_missing_keypoints_key', 'add_inference_keypoints_to_sv_detections', '3', codeflash_loop_index, inference_prediction, detections)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_mismatched_detection_count_raises_error():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    "Test that ValueError is raised when prediction and detection counts don't match."
    detections = sv.Detections(xyxy=np.array([[0.0, 0.0, 10.0, 10.0], [20.0, 20.0, 30.0, 30.0]]), confidence=np.array([0.9, 0.8]), class_id=np.array([0, 1]))
    inference_prediction = [{KEYPOINTS_KEY_IN_INFERENCE_RESPONSE: []}, {KEYPOINTS_KEY_IN_INFERENCE_RESPONSE: []}, {KEYPOINTS_KEY_IN_INFERENCE_RESPONSE: []}]
    with pytest.raises(ValueError) as exc_info:
        codeflash_return_value = codeflash_wrap(add_inference_keypoints_to_sv_detections, 'tests.inference.unit_tests.test_add_inference_keypoints_to_sv_detections__unit_test_1', None, 'test_mismatched_detection_count_raises_error', 'add_inference_keypoints_to_sv_detections', '3_0', codeflash_loop_index, inference_prediction, detections)

def test_keypoint_with_zero_confidence():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test keypoint with zero confidence.'
    detections = sv.Detections(xyxy=np.array([[0.0, 0.0, 10.0, 10.0]]), confidence=np.array([0.9]), class_id=np.array([0]))
    inference_prediction = [{KEYPOINTS_KEY_IN_INFERENCE_RESPONSE: [{KEYPOINTS_CLASS_NAME_KEY_IN_INFERENCE_RESPONSE: 'kp', KEYPOINTS_CLASS_ID_KEY_IN_INFERENCE_RESPONSE: 1, KEYPOINTS_CONFIDENCE_KEY_IN_INFERENCE_RESPONSE: 0.0, X_KEY: 5.0, Y_KEY: 5.0}]}]
    codeflash_return_value = codeflash_wrap(add_inference_keypoints_to_sv_detections, 'tests.inference.unit_tests.test_add_inference_keypoints_to_sv_detections__unit_test_1', None, 'test_keypoint_with_zero_confidence', 'add_inference_keypoints_to_sv_detections', '3', codeflash_loop_index, inference_prediction, detections)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_keypoint_with_negative_coordinates():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test keypoint with negative coordinates.'
    detections = sv.Detections(xyxy=np.array([[0.0, 0.0, 10.0, 10.0]]), confidence=np.array([0.9]), class_id=np.array([0]))
    inference_prediction = [{KEYPOINTS_KEY_IN_INFERENCE_RESPONSE: [{KEYPOINTS_CLASS_NAME_KEY_IN_INFERENCE_RESPONSE: 'kp', KEYPOINTS_CLASS_ID_KEY_IN_INFERENCE_RESPONSE: 1, KEYPOINTS_CONFIDENCE_KEY_IN_INFERENCE_RESPONSE: 0.9, X_KEY: -5.0, Y_KEY: -10.0}]}]
    codeflash_return_value = codeflash_wrap(add_inference_keypoints_to_sv_detections, 'tests.inference.unit_tests.test_add_inference_keypoints_to_sv_detections__unit_test_1', None, 'test_keypoint_with_negative_coordinates', 'add_inference_keypoints_to_sv_detections', '3', codeflash_loop_index, inference_prediction, detections)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_keypoint_with_large_coordinates():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test keypoint with very large coordinates.'
    detections = sv.Detections(xyxy=np.array([[0.0, 0.0, 10.0, 10.0]]), confidence=np.array([0.9]), class_id=np.array([0]))
    inference_prediction = [{KEYPOINTS_KEY_IN_INFERENCE_RESPONSE: [{KEYPOINTS_CLASS_NAME_KEY_IN_INFERENCE_RESPONSE: 'kp', KEYPOINTS_CLASS_ID_KEY_IN_INFERENCE_RESPONSE: 1, KEYPOINTS_CONFIDENCE_KEY_IN_INFERENCE_RESPONSE: 0.9, X_KEY: 10000.0, Y_KEY: 20000.0}]}]
    codeflash_return_value = codeflash_wrap(add_inference_keypoints_to_sv_detections, 'tests.inference.unit_tests.test_add_inference_keypoints_to_sv_detections__unit_test_1', None, 'test_keypoint_with_large_coordinates', 'add_inference_keypoints_to_sv_detections', '3', codeflash_loop_index, inference_prediction, detections)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_keypoint_with_float_coordinates():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test keypoint with floating point coordinates.'
    detections = sv.Detections(xyxy=np.array([[0.0, 0.0, 10.0, 10.0]]), confidence=np.array([0.9]), class_id=np.array([0]))
    inference_prediction = [{KEYPOINTS_KEY_IN_INFERENCE_RESPONSE: [{KEYPOINTS_CLASS_NAME_KEY_IN_INFERENCE_RESPONSE: 'kp', KEYPOINTS_CLASS_ID_KEY_IN_INFERENCE_RESPONSE: 1, KEYPOINTS_CONFIDENCE_KEY_IN_INFERENCE_RESPONSE: 0.9, X_KEY: 5.123456789, Y_KEY: 5.987654321}]}]
    codeflash_return_value = codeflash_wrap(add_inference_keypoints_to_sv_detections, 'tests.inference.unit_tests.test_add_inference_keypoints_to_sv_detections__unit_test_1', None, 'test_keypoint_with_float_coordinates', 'add_inference_keypoints_to_sv_detections', '3', codeflash_loop_index, inference_prediction, detections)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_keypoint_class_name_empty_string():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test keypoint with empty string class name.'
    detections = sv.Detections(xyxy=np.array([[0.0, 0.0, 10.0, 10.0]]), confidence=np.array([0.9]), class_id=np.array([0]))
    inference_prediction = [{KEYPOINTS_KEY_IN_INFERENCE_RESPONSE: [{KEYPOINTS_CLASS_NAME_KEY_IN_INFERENCE_RESPONSE: '', KEYPOINTS_CLASS_ID_KEY_IN_INFERENCE_RESPONSE: 1, KEYPOINTS_CONFIDENCE_KEY_IN_INFERENCE_RESPONSE: 0.9, X_KEY: 5.0, Y_KEY: 5.0}]}]
    codeflash_return_value = codeflash_wrap(add_inference_keypoints_to_sv_detections, 'tests.inference.unit_tests.test_add_inference_keypoints_to_sv_detections__unit_test_1', None, 'test_keypoint_class_name_empty_string', 'add_inference_keypoints_to_sv_detections', '3', codeflash_loop_index, inference_prediction, detections)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_keypoint_class_id_zero():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test keypoint with class_id of zero.'
    detections = sv.Detections(xyxy=np.array([[0.0, 0.0, 10.0, 10.0]]), confidence=np.array([0.9]), class_id=np.array([0]))
    inference_prediction = [{KEYPOINTS_KEY_IN_INFERENCE_RESPONSE: [{KEYPOINTS_CLASS_NAME_KEY_IN_INFERENCE_RESPONSE: 'kp', KEYPOINTS_CLASS_ID_KEY_IN_INFERENCE_RESPONSE: 0, KEYPOINTS_CONFIDENCE_KEY_IN_INFERENCE_RESPONSE: 0.9, X_KEY: 5.0, Y_KEY: 5.0}]}]
    codeflash_return_value = codeflash_wrap(add_inference_keypoints_to_sv_detections, 'tests.inference.unit_tests.test_add_inference_keypoints_to_sv_detections__unit_test_1', None, 'test_keypoint_class_id_zero', 'add_inference_keypoints_to_sv_detections', '3', codeflash_loop_index, inference_prediction, detections)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_array_dtypes_correct():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that returned arrays have correct dtypes.'
    detections = sv.Detections(xyxy=np.array([[0.0, 0.0, 10.0, 10.0]]), confidence=np.array([0.9]), class_id=np.array([0]))
    inference_prediction = [{KEYPOINTS_KEY_IN_INFERENCE_RESPONSE: [{KEYPOINTS_CLASS_NAME_KEY_IN_INFERENCE_RESPONSE: 'kp', KEYPOINTS_CLASS_ID_KEY_IN_INFERENCE_RESPONSE: 1, KEYPOINTS_CONFIDENCE_KEY_IN_INFERENCE_RESPONSE: 0.9, X_KEY: 5.0, Y_KEY: 5.0}]}]
    codeflash_return_value = codeflash_wrap(add_inference_keypoints_to_sv_detections, 'tests.inference.unit_tests.test_add_inference_keypoints_to_sv_detections__unit_test_1', None, 'test_array_dtypes_correct', 'add_inference_keypoints_to_sv_detections', '3', codeflash_loop_index, inference_prediction, detections)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_large_number_of_detections():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test with a large number of detections (100).'
    n = 100
    xyxy = np.random.rand(n, 4) * 100
    xyxy[:, 2:] += xyxy[:, :2]
    detections = sv.Detections(xyxy=xyxy, confidence=np.random.rand(n), class_id=np.arange(n) % 5)
    inference_prediction = []
    for i in range(n):
        num_kps = i % 10
        keypoints = []
        for j in range(num_kps):
            keypoints.append({KEYPOINTS_CLASS_NAME_KEY_IN_INFERENCE_RESPONSE: f'kp_{j}', KEYPOINTS_CLASS_ID_KEY_IN_INFERENCE_RESPONSE: j, KEYPOINTS_CONFIDENCE_KEY_IN_INFERENCE_RESPONSE: np.random.rand(), X_KEY: np.random.rand() * 100, Y_KEY: np.random.rand() * 100})
        inference_prediction.append({KEYPOINTS_KEY_IN_INFERENCE_RESPONSE: keypoints})
    codeflash_return_value = codeflash_wrap(add_inference_keypoints_to_sv_detections, 'tests.inference.unit_tests.test_add_inference_keypoints_to_sv_detections__unit_test_1', None, 'test_large_number_of_detections', 'add_inference_keypoints_to_sv_detections', '7', codeflash_loop_index, inference_prediction, detections)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_large_number_of_keypoints_per_detection():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test with a large number of keypoints per detection (100 keypoints).'
    detections = sv.Detections(xyxy=np.array([[0.0, 0.0, 10.0, 10.0]]), confidence=np.array([0.9]), class_id=np.array([0]))
    num_kps = 100
    keypoints = []
    for j in range(num_kps):
        keypoints.append({KEYPOINTS_CLASS_NAME_KEY_IN_INFERENCE_RESPONSE: f'kp_{j}', KEYPOINTS_CLASS_ID_KEY_IN_INFERENCE_RESPONSE: j % 10, KEYPOINTS_CONFIDENCE_KEY_IN_INFERENCE_RESPONSE: 0.5 + j % 50 / 100, X_KEY: j % 10 * 1.0, Y_KEY: j // 10 * 1.0})
    inference_prediction = [{KEYPOINTS_KEY_IN_INFERENCE_RESPONSE: keypoints}]
    codeflash_return_value = codeflash_wrap(add_inference_keypoints_to_sv_detections, 'tests.inference.unit_tests.test_add_inference_keypoints_to_sv_detections__unit_test_1', None, 'test_large_number_of_keypoints_per_detection', 'add_inference_keypoints_to_sv_detections', '6', codeflash_loop_index, inference_prediction, detections)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_large_number_of_detections_with_many_keypoints():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test with 50 detections, each with up to 50 keypoints.'
    n = 50
    xyxy = np.random.rand(n, 4) * 100
    xyxy[:, 2:] += xyxy[:, :2]
    detections = sv.Detections(xyxy=xyxy, confidence=np.random.rand(n), class_id=np.arange(n) % 5)
    inference_prediction = []
    for i in range(n):
        num_kps = i % 50 + 1
        keypoints = []
        for j in range(num_kps):
            keypoints.append({KEYPOINTS_CLASS_NAME_KEY_IN_INFERENCE_RESPONSE: f'kp_{j}', KEYPOINTS_CLASS_ID_KEY_IN_INFERENCE_RESPONSE: j % 10, KEYPOINTS_CONFIDENCE_KEY_IN_INFERENCE_RESPONSE: 0.5 + np.random.rand() * 0.4, X_KEY: np.random.rand() * 100, Y_KEY: np.random.rand() * 100})
        inference_prediction.append({KEYPOINTS_KEY_IN_INFERENCE_RESPONSE: keypoints})
    codeflash_return_value = codeflash_wrap(add_inference_keypoints_to_sv_detections, 'tests.inference.unit_tests.test_add_inference_keypoints_to_sv_detections__unit_test_1', None, 'test_large_number_of_detections_with_many_keypoints', 'add_inference_keypoints_to_sv_detections', '7', codeflash_loop_index, inference_prediction, detections)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_varying_string_lengths_in_class_names():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test keypoints with varying class name string lengths.'
    detections = sv.Detections(xyxy=np.array([[0.0, 0.0, 10.0, 10.0]]), confidence=np.array([0.9]), class_id=np.array([0]))
    keypoints = []
    class_names = ['a', 'ab', 'abcdefghij', 'a' * 100, 'a' * 1000]
    for i, name in enumerate(class_names):
        keypoints.append({KEYPOINTS_CLASS_NAME_KEY_IN_INFERENCE_RESPONSE: name, KEYPOINTS_CLASS_ID_KEY_IN_INFERENCE_RESPONSE: i, KEYPOINTS_CONFIDENCE_KEY_IN_INFERENCE_RESPONSE: 0.9, X_KEY: float(i), Y_KEY: float(i)})
    inference_prediction = [{KEYPOINTS_KEY_IN_INFERENCE_RESPONSE: keypoints}]
    codeflash_return_value = codeflash_wrap(add_inference_keypoints_to_sv_detections, 'tests.inference.unit_tests.test_add_inference_keypoints_to_sv_detections__unit_test_1', None, 'test_varying_string_lengths_in_class_names', 'add_inference_keypoints_to_sv_detections', '6', codeflash_loop_index, inference_prediction, detections)
    codeflash_output = codeflash_return_value
    result = codeflash_output
    for i, name in enumerate(class_names):
        pass

def test_consistency_with_multiple_calls():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that multiple calls with same inputs produce same outputs.'
    detections1 = sv.Detections(xyxy=np.array([[0.0, 0.0, 10.0, 10.0]]), confidence=np.array([0.9]), class_id=np.array([0]))
    detections2 = sv.Detections(xyxy=np.array([[0.0, 0.0, 10.0, 10.0]]), confidence=np.array([0.9]), class_id=np.array([0]))
    inference_prediction = [{KEYPOINTS_KEY_IN_INFERENCE_RESPONSE: [{KEYPOINTS_CLASS_NAME_KEY_IN_INFERENCE_RESPONSE: 'kp', KEYPOINTS_CLASS_ID_KEY_IN_INFERENCE_RESPONSE: 1, KEYPOINTS_CONFIDENCE_KEY_IN_INFERENCE_RESPONSE: 0.95, X_KEY: 5.0, Y_KEY: 5.0}]}]
    codeflash_return_value = codeflash_wrap(add_inference_keypoints_to_sv_detections, 'tests.inference.unit_tests.test_add_inference_keypoints_to_sv_detections__unit_test_1', None, 'test_consistency_with_multiple_calls', 'add_inference_keypoints_to_sv_detections', '4', codeflash_loop_index, inference_prediction, detections1)
    codeflash_output = codeflash_return_value
    result1 = codeflash_output
    codeflash_return_value = codeflash_wrap(add_inference_keypoints_to_sv_detections, 'tests.inference.unit_tests.test_add_inference_keypoints_to_sv_detections__unit_test_1', None, 'test_consistency_with_multiple_calls', 'add_inference_keypoints_to_sv_detections', '6', codeflash_loop_index, inference_prediction, detections2)
    codeflash_output = codeflash_return_value
    result2 = codeflash_output

def test_mixed_confidence_values():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test with confidence values near boundaries (0.0, 1.0).'
    detections = sv.Detections(xyxy=np.array([[0.0, 0.0, 10.0, 10.0]]), confidence=np.array([0.9]), class_id=np.array([0]))
    confidence_values = [0.0, 0.001, 0.5, 0.999, 1.0]
    keypoints = []
    for i, conf in enumerate(confidence_values):
        keypoints.append({KEYPOINTS_CLASS_NAME_KEY_IN_INFERENCE_RESPONSE: f'kp_{i}', KEYPOINTS_CLASS_ID_KEY_IN_INFERENCE_RESPONSE: i, KEYPOINTS_CONFIDENCE_KEY_IN_INFERENCE_RESPONSE: conf, X_KEY: float(i), Y_KEY: float(i)})
    inference_prediction = [{KEYPOINTS_KEY_IN_INFERENCE_RESPONSE: keypoints}]
    codeflash_return_value = codeflash_wrap(add_inference_keypoints_to_sv_detections, 'tests.inference.unit_tests.test_add_inference_keypoints_to_sv_detections__unit_test_1', None, 'test_mixed_confidence_values', 'add_inference_keypoints_to_sv_detections', '6', codeflash_loop_index, inference_prediction, detections)
    codeflash_output = codeflash_return_value
    result = codeflash_output
    for i, conf in enumerate(confidence_values):
        pass

def test_negative_class_ids():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test with negative class IDs.'
    detections = sv.Detections(xyxy=np.array([[0.0, 0.0, 10.0, 10.0]]), confidence=np.array([0.9]), class_id=np.array([0]))
    inference_prediction = [{KEYPOINTS_KEY_IN_INFERENCE_RESPONSE: [{KEYPOINTS_CLASS_NAME_KEY_IN_INFERENCE_RESPONSE: 'kp1', KEYPOINTS_CLASS_ID_KEY_IN_INFERENCE_RESPONSE: -1, KEYPOINTS_CONFIDENCE_KEY_IN_INFERENCE_RESPONSE: 0.9, X_KEY: 5.0, Y_KEY: 5.0}, {KEYPOINTS_CLASS_NAME_KEY_IN_INFERENCE_RESPONSE: 'kp2', KEYPOINTS_CLASS_ID_KEY_IN_INFERENCE_RESPONSE: -100, KEYPOINTS_CONFIDENCE_KEY_IN_INFERENCE_RESPONSE: 0.8, X_KEY: 6.0, Y_KEY: 6.0}]}]
    codeflash_return_value = codeflash_wrap(add_inference_keypoints_to_sv_detections, 'tests.inference.unit_tests.test_add_inference_keypoints_to_sv_detections__unit_test_1', None, 'test_negative_class_ids', 'add_inference_keypoints_to_sv_detections', '3', codeflash_loop_index, inference_prediction, detections)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_special_characters_in_class_names():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test with special characters in class names.'
    detections = sv.Detections(xyxy=np.array([[0.0, 0.0, 10.0, 10.0]]), confidence=np.array([0.9]), class_id=np.array([0]))
    special_names = ['kp_with_underscore', 'kp-with-dash', 'kp.with.dot', 'kp/with/slash', 'kp with space', 'kp\twith\ttab', 'kp\nwith\nnewline', '🔑', '中文']
    keypoints = []
    for i, name in enumerate(special_names):
        keypoints.append({KEYPOINTS_CLASS_NAME_KEY_IN_INFERENCE_RESPONSE: name, KEYPOINTS_CLASS_ID_KEY_IN_INFERENCE_RESPONSE: i, KEYPOINTS_CONFIDENCE_KEY_IN_INFERENCE_RESPONSE: 0.9, X_KEY: float(i), Y_KEY: float(i)})
    inference_prediction = [{KEYPOINTS_KEY_IN_INFERENCE_RESPONSE: keypoints}]
    codeflash_return_value = codeflash_wrap(add_inference_keypoints_to_sv_detections, 'tests.inference.unit_tests.test_add_inference_keypoints_to_sv_detections__unit_test_1', None, 'test_special_characters_in_class_names', 'add_inference_keypoints_to_sv_detections', '6', codeflash_loop_index, inference_prediction, detections)
    codeflash_output = codeflash_return_value
    result = codeflash_output
    for i, name in enumerate(special_names):
        pass