from __future__ import annotations

import gc
import inspect
import os
import time
from pathlib import Path

import dill as pickle
import numpy as np
import pytest
import supervision as sv
from inference.core.workflows.core_steps.common.serializers import \
    serialise_sv_detections
from inference.core.workflows.execution_engine.constants import (
    AREA_CONVERTED_KEY_IN_INFERENCE_RESPONSE,
    AREA_CONVERTED_KEY_IN_SV_DETECTIONS, AREA_KEY_IN_INFERENCE_RESPONSE,
    AREA_KEY_IN_SV_DETECTIONS, BOUNDING_RECT_ANGLE_KEY_IN_INFERENCE_RESPONSE,
    BOUNDING_RECT_ANGLE_KEY_IN_SV_DETECTIONS,
    BOUNDING_RECT_HEIGHT_KEY_IN_INFERENCE_RESPONSE,
    BOUNDING_RECT_HEIGHT_KEY_IN_SV_DETECTIONS,
    BOUNDING_RECT_RECT_KEY_IN_INFERENCE_RESPONSE,
    BOUNDING_RECT_RECT_KEY_IN_SV_DETECTIONS,
    BOUNDING_RECT_WIDTH_KEY_IN_INFERENCE_RESPONSE,
    BOUNDING_RECT_WIDTH_KEY_IN_SV_DETECTIONS, CLASS_ID_KEY, CLASS_NAME_KEY,
    CONFIDENCE_KEY, DETECTED_CODE_KEY, DETECTION_ID_KEY, HEIGHT_KEY,
    IMAGE_DIMENSIONS_KEY, KEYPOINTS_CLASS_ID_KEY_IN_SV_DETECTIONS,
    KEYPOINTS_CLASS_NAME_KEY_IN_SV_DETECTIONS,
    KEYPOINTS_CONFIDENCE_KEY_IN_SV_DETECTIONS,
    KEYPOINTS_KEY_IN_INFERENCE_RESPONSE, KEYPOINTS_XY_KEY_IN_SV_DETECTIONS,
    PARENT_COORDINATES_KEY, PARENT_DIMENSIONS_KEY, PARENT_ID_KEY,
    PARENT_ORIGIN_KEY, PATH_DEVIATION_KEY_IN_INFERENCE_RESPONSE,
    PATH_DEVIATION_KEY_IN_SV_DETECTIONS, POLYGON_KEY,
    POLYGON_KEY_IN_INFERENCE_RESPONSE, POLYGON_KEY_IN_SV_DETECTIONS,
    ROOT_PARENT_COORDINATES_KEY, ROOT_PARENT_DIMENSIONS_KEY,
    ROOT_PARENT_ID_KEY, ROOT_PARENT_ORIGIN_KEY,
    SMOOTHED_SPEED_KEY_IN_INFERENCE_RESPONSE,
    SMOOTHED_SPEED_KEY_IN_SV_DETECTIONS,
    SMOOTHED_VELOCITY_KEY_IN_INFERENCE_RESPONSE,
    SMOOTHED_VELOCITY_KEY_IN_SV_DETECTIONS, SPEED_KEY_IN_INFERENCE_RESPONSE,
    SPEED_KEY_IN_SV_DETECTIONS, TIME_IN_ZONE_KEY_IN_INFERENCE_RESPONSE,
    TIME_IN_ZONE_KEY_IN_SV_DETECTIONS, TRACKER_ID_KEY,
    VELOCITY_KEY_IN_INFERENCE_RESPONSE, VELOCITY_KEY_IN_SV_DETECTIONS,
    WIDTH_KEY, X_KEY, Y_KEY)
from inference.core.workflows.execution_engine.entities.base import \
    ParentOrigin


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}######!')
    iteration = os.environ['CODEFLASH_TEST_ITERATION']
    with Path('{codeflash_run_tmp_dir_client_side}', f'test_return_values_{iteration}.bin').open('ab') as f:
        pickled_values = pickle.dumps((args, kwargs, exception)) if exception else pickle.dumps((args, kwargs, return_value))
        _test_name = f"{codeflash_test_module_name}:{(codeflash_test_class_name + '.' if codeflash_test_class_name else '')}{codeflash_test_name}:{codeflash_function_name}:{codeflash_line_id}".encode('ascii')
        f.write(len(_test_name).to_bytes(4, byteorder='big'))
        f.write(_test_name)
        f.write(codeflash_duration.to_bytes(8, byteorder='big'))
        f.write(len(pickled_values).to_bytes(4, byteorder='big'))
        f.write(pickled_values)
        f.write(codeflash_loop_index.to_bytes(8, byteorder='big'))
        f.write(len(invocation_id).to_bytes(4, byteorder='big'))
        f.write(invocation_id.encode('ascii'))
    if exception:
        raise exception
    return return_value

def test_empty_detections_returns_empty_predictions():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that an empty detections object returns empty predictions list.'
    detections = sv.Detections(xyxy=np.empty((0, 4), dtype=np.float32), confidence=np.empty(0, dtype=np.float32), class_id=np.empty(0, dtype=np.int32), tracker_id=None, mask=None)
    _call__bound__arguments = inspect.signature(serialise_sv_detections).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(serialise_sv_detections, 'tests.inference.unit_tests.test_serialise_sv_detections__unit_test_1', None, 'test_empty_detections_returns_empty_predictions', 'serialise_sv_detections', '2', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_single_detection_basic_fields():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test serialization of a single detection with basic required fields.'
    xyxy = np.array([[10.0, 20.0, 50.0, 60.0]], dtype=np.float32)
    confidence = np.array([0.95], dtype=np.float32)
    class_id = np.array([1], dtype=np.int32)
    data_dict = {'class_name': np.array(['person'], dtype=object), DETECTION_ID_KEY: np.array(['det_001'], dtype=object)}
    detections = sv.Detections(xyxy=xyxy, confidence=confidence, class_id=class_id, tracker_id=None, mask=None, data=data_dict)
    _call__bound__arguments = inspect.signature(serialise_sv_detections).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(serialise_sv_detections, 'tests.inference.unit_tests.test_serialise_sv_detections__unit_test_1', None, 'test_single_detection_basic_fields', 'serialise_sv_detections', '6', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output
    pred = result['predictions'][0]

def test_multiple_detections():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test serialization of multiple detections.'
    xyxy = np.array([[10.0, 20.0, 50.0, 60.0], [100.0, 150.0, 200.0, 250.0], [5.0, 5.0, 15.0, 15.0]], dtype=np.float32)
    confidence = np.array([0.95, 0.87, 0.72], dtype=np.float32)
    class_id = np.array([1, 2, 0], dtype=np.int32)
    data_dict = {'class_name': np.array(['person', 'car', 'dog'], dtype=object), DETECTION_ID_KEY: np.array(['det_001', 'det_002', 'det_003'], dtype=object)}
    detections = sv.Detections(xyxy=xyxy, confidence=confidence, class_id=class_id, tracker_id=None, mask=None, data=data_dict)
    _call__bound__arguments = inspect.signature(serialise_sv_detections).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(serialise_sv_detections, 'tests.inference.unit_tests.test_serialise_sv_detections__unit_test_1', None, 'test_multiple_detections', 'serialise_sv_detections', '6', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_detection_with_tracker_id():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test serialization includes tracker_id when present.'
    xyxy = np.array([[10.0, 20.0, 50.0, 60.0]], dtype=np.float32)
    confidence = np.array([0.95], dtype=np.float32)
    class_id = np.array([1], dtype=np.int32)
    tracker_id = np.array([42], dtype=np.int32)
    data_dict = {'class_name': np.array(['person'], dtype=object), DETECTION_ID_KEY: np.array(['det_001'], dtype=object)}
    detections = sv.Detections(xyxy=xyxy, confidence=confidence, class_id=class_id, tracker_id=tracker_id, mask=None, data=data_dict)
    _call__bound__arguments = inspect.signature(serialise_sv_detections).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(serialise_sv_detections, 'tests.inference.unit_tests.test_serialise_sv_detections__unit_test_1', None, 'test_detection_with_tracker_id', 'serialise_sv_detections', '7', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_detection_with_image_dimensions():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that image dimensions are correctly extracted and returned.'
    xyxy = np.array([[10.0, 20.0, 50.0, 60.0]], dtype=np.float32)
    confidence = np.array([0.95], dtype=np.float32)
    class_id = np.array([1], dtype=np.int32)
    data_dict = {'class_name': np.array(['person'], dtype=object), DETECTION_ID_KEY: np.array(['det_001'], dtype=object), IMAGE_DIMENSIONS_KEY: np.array([480, 640])}
    detections = sv.Detections(xyxy=xyxy, confidence=confidence, class_id=class_id, tracker_id=None, mask=None, data=data_dict)
    _call__bound__arguments = inspect.signature(serialise_sv_detections).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(serialise_sv_detections, 'tests.inference.unit_tests.test_serialise_sv_detections__unit_test_1', None, 'test_detection_with_image_dimensions', 'serialise_sv_detections', '6', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_detection_with_path_deviation():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test serialization includes path_deviation when present.'
    xyxy = np.array([[10.0, 20.0, 50.0, 60.0]], dtype=np.float32)
    confidence = np.array([0.95], dtype=np.float32)
    class_id = np.array([1], dtype=np.int32)
    data_dict = {'class_name': np.array(['person'], dtype=object), DETECTION_ID_KEY: np.array(['det_001'], dtype=object), PATH_DEVIATION_KEY_IN_SV_DETECTIONS: 5.5}
    detections = sv.Detections(xyxy=xyxy, confidence=confidence, class_id=class_id, tracker_id=None, mask=None, data=data_dict)
    _call__bound__arguments = inspect.signature(serialise_sv_detections).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(serialise_sv_detections, 'tests.inference.unit_tests.test_serialise_sv_detections__unit_test_1', None, 'test_detection_with_path_deviation', 'serialise_sv_detections', '6', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_detection_with_time_in_zone():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test serialization includes time_in_zone when present.'
    xyxy = np.array([[10.0, 20.0, 50.0, 60.0]], dtype=np.float32)
    confidence = np.array([0.95], dtype=np.float32)
    class_id = np.array([1], dtype=np.int32)
    data_dict = {'class_name': np.array(['person'], dtype=object), DETECTION_ID_KEY: np.array(['det_001'], dtype=object), TIME_IN_ZONE_KEY_IN_SV_DETECTIONS: 123.45}
    detections = sv.Detections(xyxy=xyxy, confidence=confidence, class_id=class_id, tracker_id=None, mask=None, data=data_dict)
    _call__bound__arguments = inspect.signature(serialise_sv_detections).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(serialise_sv_detections, 'tests.inference.unit_tests.test_serialise_sv_detections__unit_test_1', None, 'test_detection_with_time_in_zone', 'serialise_sv_detections', '6', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_detection_with_detected_code():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test serialization includes detected_code when present.'
    xyxy = np.array([[10.0, 20.0, 50.0, 60.0]], dtype=np.float32)
    confidence = np.array([0.95], dtype=np.float32)
    class_id = np.array([1], dtype=np.int32)
    data_dict = {'class_name': np.array(['barcode'], dtype=object), DETECTION_ID_KEY: np.array(['det_001'], dtype=object), DETECTED_CODE_KEY: 'ABC123XYZ'}
    detections = sv.Detections(xyxy=xyxy, confidence=confidence, class_id=class_id, tracker_id=None, mask=None, data=data_dict)
    _call__bound__arguments = inspect.signature(serialise_sv_detections).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(serialise_sv_detections, 'tests.inference.unit_tests.test_serialise_sv_detections__unit_test_1', None, 'test_detection_with_detected_code', 'serialise_sv_detections', '6', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_detection_with_area():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test serialization includes area when present.'
    xyxy = np.array([[10.0, 20.0, 50.0, 60.0]], dtype=np.float32)
    confidence = np.array([0.95], dtype=np.float32)
    class_id = np.array([1], dtype=np.int32)
    data_dict = {'class_name': np.array(['person'], dtype=object), DETECTION_ID_KEY: np.array(['det_001'], dtype=object), AREA_KEY_IN_SV_DETECTIONS: 1200}
    detections = sv.Detections(xyxy=xyxy, confidence=confidence, class_id=class_id, tracker_id=None, mask=None, data=data_dict)
    _call__bound__arguments = inspect.signature(serialise_sv_detections).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(serialise_sv_detections, 'tests.inference.unit_tests.test_serialise_sv_detections__unit_test_1', None, 'test_detection_with_area', 'serialise_sv_detections', '6', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_detection_with_area_converted():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test serialization includes converted area when present.'
    xyxy = np.array([[10.0, 20.0, 50.0, 60.0]], dtype=np.float32)
    confidence = np.array([0.95], dtype=np.float32)
    class_id = np.array([1], dtype=np.int32)
    data_dict = {'class_name': np.array(['person'], dtype=object), DETECTION_ID_KEY: np.array(['det_001'], dtype=object), AREA_CONVERTED_KEY_IN_SV_DETECTIONS: 85.5}
    detections = sv.Detections(xyxy=xyxy, confidence=confidence, class_id=class_id, tracker_id=None, mask=None, data=data_dict)
    _call__bound__arguments = inspect.signature(serialise_sv_detections).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(serialise_sv_detections, 'tests.inference.unit_tests.test_serialise_sv_detections__unit_test_1', None, 'test_detection_with_area_converted', 'serialise_sv_detections', '6', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_detection_with_zero_width_bbox():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test handling of detection with zero width (x1 == x2).'
    xyxy = np.array([[10.0, 20.0, 10.0, 60.0]], dtype=np.float32)
    confidence = np.array([0.95], dtype=np.float32)
    class_id = np.array([1], dtype=np.int32)
    data_dict = {'class_name': np.array(['person'], dtype=object), DETECTION_ID_KEY: np.array(['det_001'], dtype=object)}
    detections = sv.Detections(xyxy=xyxy, confidence=confidence, class_id=class_id, tracker_id=None, mask=None, data=data_dict)
    _call__bound__arguments = inspect.signature(serialise_sv_detections).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(serialise_sv_detections, 'tests.inference.unit_tests.test_serialise_sv_detections__unit_test_1', None, 'test_detection_with_zero_width_bbox', 'serialise_sv_detections', '6', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_detection_with_zero_height_bbox():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test handling of detection with zero height (y1 == y2).'
    xyxy = np.array([[10.0, 20.0, 50.0, 20.0]], dtype=np.float32)
    confidence = np.array([0.95], dtype=np.float32)
    class_id = np.array([1], dtype=np.int32)
    data_dict = {'class_name': np.array(['person'], dtype=object), DETECTION_ID_KEY: np.array(['det_001'], dtype=object)}
    detections = sv.Detections(xyxy=xyxy, confidence=confidence, class_id=class_id, tracker_id=None, mask=None, data=data_dict)
    _call__bound__arguments = inspect.signature(serialise_sv_detections).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(serialise_sv_detections, 'tests.inference.unit_tests.test_serialise_sv_detections__unit_test_1', None, 'test_detection_with_zero_height_bbox', 'serialise_sv_detections', '6', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_detection_with_inverted_bbox():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test handling of bbox with inverted coordinates (x2 < x1, y2 < y1).'
    xyxy = np.array([[50.0, 60.0, 10.0, 20.0]], dtype=np.float32)
    confidence = np.array([0.95], dtype=np.float32)
    class_id = np.array([1], dtype=np.int32)
    data_dict = {'class_name': np.array(['person'], dtype=object), DETECTION_ID_KEY: np.array(['det_001'], dtype=object)}
    detections = sv.Detections(xyxy=xyxy, confidence=confidence, class_id=class_id, tracker_id=None, mask=None, data=data_dict)
    _call__bound__arguments = inspect.signature(serialise_sv_detections).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(serialise_sv_detections, 'tests.inference.unit_tests.test_serialise_sv_detections__unit_test_1', None, 'test_detection_with_inverted_bbox', 'serialise_sv_detections', '6', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_detection_with_very_high_confidence():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test handling of confidence value at boundary (1.0).'
    xyxy = np.array([[10.0, 20.0, 50.0, 60.0]], dtype=np.float32)
    confidence = np.array([1.0], dtype=np.float32)
    class_id = np.array([1], dtype=np.int32)
    data_dict = {'class_name': np.array(['person'], dtype=object), DETECTION_ID_KEY: np.array(['det_001'], dtype=object)}
    detections = sv.Detections(xyxy=xyxy, confidence=confidence, class_id=class_id, tracker_id=None, mask=None, data=data_dict)
    _call__bound__arguments = inspect.signature(serialise_sv_detections).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(serialise_sv_detections, 'tests.inference.unit_tests.test_serialise_sv_detections__unit_test_1', None, 'test_detection_with_very_high_confidence', 'serialise_sv_detections', '6', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_detection_with_very_low_confidence():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test handling of very low confidence value.'
    xyxy = np.array([[10.0, 20.0, 50.0, 60.0]], dtype=np.float32)
    confidence = np.array([0.001], dtype=np.float32)
    class_id = np.array([1], dtype=np.int32)
    data_dict = {'class_name': np.array(['person'], dtype=object), DETECTION_ID_KEY: np.array(['det_001'], dtype=object)}
    detections = sv.Detections(xyxy=xyxy, confidence=confidence, class_id=class_id, tracker_id=None, mask=None, data=data_dict)
    _call__bound__arguments = inspect.signature(serialise_sv_detections).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(serialise_sv_detections, 'tests.inference.unit_tests.test_serialise_sv_detections__unit_test_1', None, 'test_detection_with_very_low_confidence', 'serialise_sv_detections', '6', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_detection_with_negative_class_id():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test handling of negative class_id.'
    xyxy = np.array([[10.0, 20.0, 50.0, 60.0]], dtype=np.float32)
    confidence = np.array([0.95], dtype=np.float32)
    class_id = np.array([-5], dtype=np.int32)
    data_dict = {'class_name': np.array(['unknown'], dtype=object), DETECTION_ID_KEY: np.array(['det_001'], dtype=object)}
    detections = sv.Detections(xyxy=xyxy, confidence=confidence, class_id=class_id, tracker_id=None, mask=None, data=data_dict)
    _call__bound__arguments = inspect.signature(serialise_sv_detections).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(serialise_sv_detections, 'tests.inference.unit_tests.test_serialise_sv_detections__unit_test_1', None, 'test_detection_with_negative_class_id', 'serialise_sv_detections', '6', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_detection_with_empty_class_name():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test handling of empty class name string.'
    xyxy = np.array([[10.0, 20.0, 50.0, 60.0]], dtype=np.float32)
    confidence = np.array([0.95], dtype=np.float32)
    class_id = np.array([1], dtype=np.int32)
    data_dict = {'class_name': np.array([''], dtype=object), DETECTION_ID_KEY: np.array(['det_001'], dtype=object)}
    detections = sv.Detections(xyxy=xyxy, confidence=confidence, class_id=class_id, tracker_id=None, mask=None, data=data_dict)
    _call__bound__arguments = inspect.signature(serialise_sv_detections).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(serialise_sv_detections, 'tests.inference.unit_tests.test_serialise_sv_detections__unit_test_1', None, 'test_detection_with_empty_class_name', 'serialise_sv_detections', '6', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_detection_with_special_characters_in_class_name():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test handling of special characters in class name.'
    xyxy = np.array([[10.0, 20.0, 50.0, 60.0]], dtype=np.float32)
    confidence = np.array([0.95], dtype=np.float32)
    class_id = np.array([1], dtype=np.int32)
    special_name = 'person@#$%^&*()'
    data_dict = {'class_name': np.array([special_name], dtype=object), DETECTION_ID_KEY: np.array(['det_001'], dtype=object)}
    detections = sv.Detections(xyxy=xyxy, confidence=confidence, class_id=class_id, tracker_id=None, mask=None, data=data_dict)
    _call__bound__arguments = inspect.signature(serialise_sv_detections).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(serialise_sv_detections, 'tests.inference.unit_tests.test_serialise_sv_detections__unit_test_1', None, 'test_detection_with_special_characters_in_class_name', 'serialise_sv_detections', '7', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_detection_with_large_coordinates():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test handling of very large coordinate values.'
    xyxy = np.array([[10000.0, 20000.0, 50000.0, 60000.0]], dtype=np.float32)
    confidence = np.array([0.95], dtype=np.float32)
    class_id = np.array([1], dtype=np.int32)
    data_dict = {'class_name': np.array(['person'], dtype=object), DETECTION_ID_KEY: np.array(['det_001'], dtype=object)}
    detections = sv.Detections(xyxy=xyxy, confidence=confidence, class_id=class_id, tracker_id=None, mask=None, data=data_dict)
    _call__bound__arguments = inspect.signature(serialise_sv_detections).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(serialise_sv_detections, 'tests.inference.unit_tests.test_serialise_sv_detections__unit_test_1', None, 'test_detection_with_large_coordinates', 'serialise_sv_detections', '6', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_detection_with_float_coordinates_precision():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that float coordinates are properly converted and precision is maintained.'
    xyxy = np.array([[10.5, 20.7, 50.3, 60.9]], dtype=np.float32)
    confidence = np.array([0.95], dtype=np.float32)
    class_id = np.array([1], dtype=np.int32)
    data_dict = {'class_name': np.array(['person'], dtype=object), DETECTION_ID_KEY: np.array(['det_001'], dtype=object)}
    detections = sv.Detections(xyxy=xyxy, confidence=confidence, class_id=class_id, tracker_id=None, mask=None, data=data_dict)
    _call__bound__arguments = inspect.signature(serialise_sv_detections).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(serialise_sv_detections, 'tests.inference.unit_tests.test_serialise_sv_detections__unit_test_1', None, 'test_detection_with_float_coordinates_precision', 'serialise_sv_detections', '6', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output
    pred = result['predictions'][0]

def test_detection_with_velocity_array():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test serialization includes velocity array when present.'
    xyxy = np.array([[10.0, 20.0, 50.0, 60.0]], dtype=np.float32)
    confidence = np.array([0.95], dtype=np.float32)
    class_id = np.array([1], dtype=np.int32)
    data_dict = {'class_name': np.array(['person'], dtype=object), DETECTION_ID_KEY: np.array(['det_001'], dtype=object), VELOCITY_KEY_IN_SV_DETECTIONS: np.array([1.5, 2.5])}
    detections = sv.Detections(xyxy=xyxy, confidence=confidence, class_id=class_id, tracker_id=None, mask=None, data=data_dict)
    _call__bound__arguments = inspect.signature(serialise_sv_detections).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(serialise_sv_detections, 'tests.inference.unit_tests.test_serialise_sv_detections__unit_test_1', None, 'test_detection_with_velocity_array', 'serialise_sv_detections', '6', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_detection_with_speed():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test serialization includes speed when present.'
    xyxy = np.array([[10.0, 20.0, 50.0, 60.0]], dtype=np.float32)
    confidence = np.array([0.95], dtype=np.float32)
    class_id = np.array([1], dtype=np.int32)
    data_dict = {'class_name': np.array(['person'], dtype=object), DETECTION_ID_KEY: np.array(['det_001'], dtype=object), SPEED_KEY_IN_SV_DETECTIONS: np.array([3.2])}
    detections = sv.Detections(xyxy=xyxy, confidence=confidence, class_id=class_id, tracker_id=None, mask=None, data=data_dict)
    _call__bound__arguments = inspect.signature(serialise_sv_detections).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(serialise_sv_detections, 'tests.inference.unit_tests.test_serialise_sv_detections__unit_test_1', None, 'test_detection_with_speed', 'serialise_sv_detections', '6', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_detection_with_smoothed_velocity_array():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test serialization includes smoothed_velocity when present.'
    xyxy = np.array([[10.0, 20.0, 50.0, 60.0]], dtype=np.float32)
    confidence = np.array([0.95], dtype=np.float32)
    class_id = np.array([1], dtype=np.int32)
    data_dict = {'class_name': np.array(['person'], dtype=object), DETECTION_ID_KEY: np.array(['det_001'], dtype=object), SMOOTHED_VELOCITY_KEY_IN_SV_DETECTIONS: np.array([0.8, 1.2])}
    detections = sv.Detections(xyxy=xyxy, confidence=confidence, class_id=class_id, tracker_id=None, mask=None, data=data_dict)
    _call__bound__arguments = inspect.signature(serialise_sv_detections).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(serialise_sv_detections, 'tests.inference.unit_tests.test_serialise_sv_detections__unit_test_1', None, 'test_detection_with_smoothed_velocity_array', 'serialise_sv_detections', '6', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_detection_with_smoothed_speed():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test serialization includes smoothed_speed when present.'
    xyxy = np.array([[10.0, 20.0, 50.0, 60.0]], dtype=np.float32)
    confidence = np.array([0.95], dtype=np.float32)
    class_id = np.array([1], dtype=np.int32)
    data_dict = {'class_name': np.array(['person'], dtype=object), DETECTION_ID_KEY: np.array(['det_001'], dtype=object), SMOOTHED_SPEED_KEY_IN_SV_DETECTIONS: np.array([2.8])}
    detections = sv.Detections(xyxy=xyxy, confidence=confidence, class_id=class_id, tracker_id=None, mask=None, data=data_dict)
    _call__bound__arguments = inspect.signature(serialise_sv_detections).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(serialise_sv_detections, 'tests.inference.unit_tests.test_serialise_sv_detections__unit_test_1', None, 'test_detection_with_smoothed_speed', 'serialise_sv_detections', '6', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_detection_with_parent_id_only():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test serialization includes parent_id when only parent_id is present.'
    xyxy = np.array([[10.0, 20.0, 50.0, 60.0]], dtype=np.float32)
    confidence = np.array([0.95], dtype=np.float32)
    class_id = np.array([1], dtype=np.int32)
    data_dict = {'class_name': np.array(['person'], dtype=object), DETECTION_ID_KEY: np.array(['det_001'], dtype=object), PARENT_ID_KEY: 'parent_123'}
    detections = sv.Detections(xyxy=xyxy, confidence=confidence, class_id=class_id, tracker_id=None, mask=None, data=data_dict)
    _call__bound__arguments = inspect.signature(serialise_sv_detections).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(serialise_sv_detections, 'tests.inference.unit_tests.test_serialise_sv_detections__unit_test_1', None, 'test_detection_with_parent_id_only', 'serialise_sv_detections', '6', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_detection_with_parent_id_same_as_root():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that parent metadata is NOT attached when parent_id equals root_parent_id.'
    xyxy = np.array([[10.0, 20.0, 50.0, 60.0]], dtype=np.float32)
    confidence = np.array([0.95], dtype=np.float32)
    class_id = np.array([1], dtype=np.int32)
    data_dict = {'class_name': np.array(['person'], dtype=object), DETECTION_ID_KEY: np.array(['det_001'], dtype=object), PARENT_ID_KEY: 'root_id', ROOT_PARENT_ID_KEY: 'root_id'}
    detections = sv.Detections(xyxy=xyxy, confidence=confidence, class_id=class_id, tracker_id=None, mask=None, data=data_dict)
    _call__bound__arguments = inspect.signature(serialise_sv_detections).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(serialise_sv_detections, 'tests.inference.unit_tests.test_serialise_sv_detections__unit_test_1', None, 'test_detection_with_parent_id_same_as_root', 'serialise_sv_detections', '6', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_detection_with_integer_parent_id():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test handling of integer parent_id (should be converted to string).'
    xyxy = np.array([[10.0, 20.0, 50.0, 60.0]], dtype=np.float32)
    confidence = np.array([0.95], dtype=np.float32)
    class_id = np.array([1], dtype=np.int32)
    data_dict = {'class_name': np.array(['person'], dtype=object), DETECTION_ID_KEY: np.array(['det_001'], dtype=object), PARENT_ID_KEY: 123}
    detections = sv.Detections(xyxy=xyxy, confidence=confidence, class_id=class_id, tracker_id=None, mask=None, data=data_dict)
    _call__bound__arguments = inspect.signature(serialise_sv_detections).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(serialise_sv_detections, 'tests.inference.unit_tests.test_serialise_sv_detections__unit_test_1', None, 'test_detection_with_integer_parent_id', 'serialise_sv_detections', '6', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_detection_with_bounding_rect_all_fields():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test serialization includes all bounding rect fields when all are present.'
    xyxy = np.array([[10.0, 20.0, 50.0, 60.0]], dtype=np.float32)
    confidence = np.array([0.95], dtype=np.float32)
    class_id = np.array([1], dtype=np.int32)
    data_dict = {'class_name': np.array(['person'], dtype=object), DETECTION_ID_KEY: np.array(['det_001'], dtype=object), BOUNDING_RECT_ANGLE_KEY_IN_SV_DETECTIONS: 45.0, BOUNDING_RECT_RECT_KEY_IN_SV_DETECTIONS: ((25, 40), (40, 40), 45), BOUNDING_RECT_HEIGHT_KEY_IN_SV_DETECTIONS: 40, BOUNDING_RECT_WIDTH_KEY_IN_SV_DETECTIONS: 40}
    detections = sv.Detections(xyxy=xyxy, confidence=confidence, class_id=class_id, tracker_id=None, mask=None, data=data_dict)
    _call__bound__arguments = inspect.signature(serialise_sv_detections).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(serialise_sv_detections, 'tests.inference.unit_tests.test_serialise_sv_detections__unit_test_1', None, 'test_detection_with_bounding_rect_all_fields', 'serialise_sv_detections', '6', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_detection_with_bounding_rect_partial_fields():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that bounding rect is only included if ALL required fields are present.'
    xyxy = np.array([[10.0, 20.0, 50.0, 60.0]], dtype=np.float32)
    confidence = np.array([0.95], dtype=np.float32)
    class_id = np.array([1], dtype=np.int32)
    data_dict = {'class_name': np.array(['person'], dtype=object), DETECTION_ID_KEY: np.array(['det_001'], dtype=object), BOUNDING_RECT_ANGLE_KEY_IN_SV_DETECTIONS: 45.0, BOUNDING_RECT_RECT_KEY_IN_SV_DETECTIONS: ((25, 40), (40, 40), 45)}
    detections = sv.Detections(xyxy=xyxy, confidence=confidence, class_id=class_id, tracker_id=None, mask=None, data=data_dict)
    _call__bound__arguments = inspect.signature(serialise_sv_detections).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(serialise_sv_detections, 'tests.inference.unit_tests.test_serialise_sv_detections__unit_test_1', None, 'test_detection_with_bounding_rect_partial_fields', 'serialise_sv_detections', '6', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_detection_with_keypoints_all_fields():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test serialization includes keypoints when all keypoint fields are present.'
    xyxy = np.array([[10.0, 20.0, 50.0, 60.0]], dtype=np.float32)
    confidence = np.array([0.95], dtype=np.float32)
    class_id = np.array([1], dtype=np.int32)
    data_dict = {'class_name': np.array(['person'], dtype=object), DETECTION_ID_KEY: np.array(['det_001'], dtype=object), KEYPOINTS_CLASS_ID_KEY_IN_SV_DETECTIONS: np.array([0, 1, 2]), KEYPOINTS_CLASS_NAME_KEY_IN_SV_DETECTIONS: np.array(['head', 'body', 'foot']), KEYPOINTS_CONFIDENCE_KEY_IN_SV_DETECTIONS: np.array([0.9, 0.95, 0.85]), KEYPOINTS_XY_KEY_IN_SV_DETECTIONS: np.array([[30, 25], [30, 40], [30, 55]])}
    detections = sv.Detections(xyxy=xyxy, confidence=confidence, class_id=class_id, tracker_id=None, mask=None, data=data_dict)
    _call__bound__arguments = inspect.signature(serialise_sv_detections).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(serialise_sv_detections, 'tests.inference.unit_tests.test_serialise_sv_detections__unit_test_1', None, 'test_detection_with_keypoints_all_fields', 'serialise_sv_detections', '6', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output
    keypoints = result['predictions'][0][KEYPOINTS_KEY_IN_INFERENCE_RESPONSE]

def test_detection_with_keypoints_partial_fields():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that keypoints are only included if ALL required fields are present.'
    xyxy = np.array([[10.0, 20.0, 50.0, 60.0]], dtype=np.float32)
    confidence = np.array([0.95], dtype=np.float32)
    class_id = np.array([1], dtype=np.int32)
    data_dict = {'class_name': np.array(['person'], dtype=object), DETECTION_ID_KEY: np.array(['det_001'], dtype=object), KEYPOINTS_CLASS_ID_KEY_IN_SV_DETECTIONS: np.array([0]), KEYPOINTS_CLASS_NAME_KEY_IN_SV_DETECTIONS: np.array(['head'])}
    detections = sv.Detections(xyxy=xyxy, confidence=confidence, class_id=class_id, tracker_id=None, mask=None, data=data_dict)
    _call__bound__arguments = inspect.signature(serialise_sv_detections).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(serialise_sv_detections, 'tests.inference.unit_tests.test_serialise_sv_detections__unit_test_1', None, 'test_detection_with_keypoints_partial_fields', 'serialise_sv_detections', '6', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_detection_with_high_precision_float_values():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test handling of high precision float values.'
    xyxy = np.array([[10.123456, 20.654321, 50.987654, 60.111111]], dtype=np.float32)
    confidence = np.array([0.9999999], dtype=np.float32)
    class_id = np.array([1], dtype=np.int32)
    data_dict = {'class_name': np.array(['person'], dtype=object), DETECTION_ID_KEY: np.array(['det_001'], dtype=object)}
    detections = sv.Detections(xyxy=xyxy, confidence=confidence, class_id=class_id, tracker_id=None, mask=None, data=data_dict)
    _call__bound__arguments = inspect.signature(serialise_sv_detections).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(serialise_sv_detections, 'tests.inference.unit_tests.test_serialise_sv_detections__unit_test_1', None, 'test_detection_with_high_precision_float_values', 'serialise_sv_detections', '6', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output
    pred = result['predictions'][0]

def test_large_number_of_detections():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test serialization of 500 detections.'
    n = 500
    xyxy = np.random.rand(n, 4).astype(np.float32) * 100
    confidence = np.random.rand(n).astype(np.float32)
    class_id = np.random.randint(0, 10, n).astype(np.int32)
    class_names = [f'class_{i % 10}' for i in range(n)]
    detection_ids = [f'det_{i}' for i in range(n)]
    data_dict = {'class_name': np.array(class_names, dtype=object), DETECTION_ID_KEY: np.array(detection_ids, dtype=object)}
    detections = sv.Detections(xyxy=xyxy, confidence=confidence, class_id=class_id, tracker_id=None, mask=None, data=data_dict)
    _call__bound__arguments = inspect.signature(serialise_sv_detections).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(serialise_sv_detections, 'tests.inference.unit_tests.test_serialise_sv_detections__unit_test_1', None, 'test_large_number_of_detections', 'serialise_sv_detections', '9', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output
    for pred in result['predictions']:
        pass

def test_detection_with_numeric_detection_id():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that numeric detection_id values are converted to strings.'
    xyxy = np.array([[10.0, 20.0, 50.0, 60.0]], dtype=np.float32)
    confidence = np.array([0.95], dtype=np.float32)
    class_id = np.array([1], dtype=np.int32)
    data_dict = {'class_name': np.array(['person'], dtype=object), DETECTION_ID_KEY: np.array([12345], dtype=object)}
    detections = sv.Detections(xyxy=xyxy, confidence=confidence, class_id=class_id, tracker_id=None, mask=None, data=data_dict)
    _call__bound__arguments = inspect.signature(serialise_sv_detections).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(serialise_sv_detections, 'tests.inference.unit_tests.test_serialise_sv_detections__unit_test_1', None, 'test_detection_with_numeric_detection_id', 'serialise_sv_detections', '6', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output

def test_large_detections_1000_objects():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test serialization of 1000 detections with various optional fields.'
    n = 1000
    xyxy = np.random.rand(n, 4).astype(np.float32) * 1000
    confidence = np.random.rand(n).astype(np.float32)
    class_id = np.random.randint(0, 50, n).astype(np.int32)
    tracker_id = np.random.randint(0, 100, n).astype(np.int32)
    class_names = [f'class_{i % 50}' for i in range(n)]
    detection_ids = [f'det_{i}' for i in range(n)]
    path_deviations = np.random.rand(n) * 10
    times_in_zone = np.random.rand(n) * 100
    data_dict = {'class_name': np.array(class_names, dtype=object), DETECTION_ID_KEY: np.array(detection_ids, dtype=object), IMAGE_DIMENSIONS_KEY: np.array([1080, 1920])}
    for i in range(0, n, 2):
        if i == 0:
            data_dict[PATH_DEVIATION_KEY_IN_SV_DETECTIONS] = path_deviations
            data_dict[TIME_IN_ZONE_KEY_IN_SV_DETECTIONS] = times_in_zone
    detections = sv.Detections(xyxy=xyxy, confidence=confidence, class_id=class_id, tracker_id=tracker_id, mask=None, data=data_dict)
    _call__bound__arguments = inspect.signature(serialise_sv_detections).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(serialise_sv_detections, 'tests.inference.unit_tests.test_serialise_sv_detections__unit_test_1', None, 'test_large_detections_1000_objects', 'serialise_sv_detections', '13', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output
    for pred in result['predictions']:
        pass

def test_large_detections_with_all_optional_fields():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test serialization of 100 detections with many optional fields.'
    n = 100
    xyxy = np.random.rand(n, 4).astype(np.float32) * 500
    confidence = np.random.rand(n).astype(np.float32)
    class_id = np.random.randint(0, 10, n).astype(np.int32)
    tracker_id = np.random.randint(0, 20, n).astype(np.int32)
    class_names = [f'class_{i % 10}' for i in range(n)]
    detection_ids = [f'det_{i}' for i in range(n)]
    data_dict = {'class_name': np.array(class_names, dtype=object), DETECTION_ID_KEY: np.array(detection_ids, dtype=object), IMAGE_DIMENSIONS_KEY: np.array([480, 640]), PATH_DEVIATION_KEY_IN_SV_DETECTIONS: np.random.rand(n) * 5, TIME_IN_ZONE_KEY_IN_SV_DETECTIONS: np.random.rand(n) * 50, AREA_KEY_IN_SV_DETECTIONS: np.random.rand(n) * 10000, AREA_CONVERTED_KEY_IN_SV_DETECTIONS: np.random.rand(n) * 100, VELOCITY_KEY_IN_SV_DETECTIONS: np.random.rand(n, 2), SPEED_KEY_IN_SV_DETECTIONS: np.random.rand(n), SMOOTHED_VELOCITY_KEY_IN_SV_DETECTIONS: np.random.rand(n, 2), SMOOTHED_SPEED_KEY_IN_SV_DETECTIONS: np.random.rand(n), PARENT_ID_KEY: np.array([f'parent_{i}' for i in range(n)], dtype=object)}
    detections = sv.Detections(xyxy=xyxy, confidence=confidence, class_id=class_id, tracker_id=tracker_id, mask=None, data=data_dict)
    _call__bound__arguments = inspect.signature(serialise_sv_detections).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(serialise_sv_detections, 'tests.inference.unit_tests.test_serialise_sv_detections__unit_test_1', None, 'test_large_detections_with_all_optional_fields', 'serialise_sv_detections', '10', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output
    for i, pred in enumerate(result['predictions']):
        pass

def test_many_keypoints_per_detection():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test serialization with many keypoints per detection.'
    n = 50
    xyxy = np.random.rand(n, 4).astype(np.float32) * 500
    confidence = np.random.rand(n).astype(np.float32)
    class_id = np.random.randint(0, 5, n).astype(np.int32)
    class_names = [f'class_{i % 5}' for i in range(n)]
    detection_ids = [f'det_{i}' for i in range(n)]
    num_keypoints = 17
    kp_class_ids = np.tile(np.arange(num_keypoints), (n, 1))
    kp_class_names = np.tile(np.array([f'kp_{i}' for i in range(num_keypoints)], dtype=object), (n, 1))
    kp_confidences = np.random.rand(n, num_keypoints)
    kp_xy = np.random.rand(n, num_keypoints, 2) * 500
    data_dict = {'class_name': np.array(class_names, dtype=object), DETECTION_ID_KEY: np.array(detection_ids, dtype=object), KEYPOINTS_CLASS_ID_KEY_IN_SV_DETECTIONS: kp_class_ids, KEYPOINTS_CLASS_NAME_KEY_IN_SV_DETECTIONS: kp_class_names, KEYPOINTS_CONFIDENCE_KEY_IN_SV_DETECTIONS: kp_confidences, KEYPOINTS_XY_KEY_IN_SV_DETECTIONS: kp_xy}
    detections = sv.Detections(xyxy=xyxy, confidence=confidence, class_id=class_id, tracker_id=None, mask=None, data=data_dict)
    _call__bound__arguments = inspect.signature(serialise_sv_detections).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(serialise_sv_detections, 'tests.inference.unit_tests.test_serialise_sv_detections__unit_test_1', None, 'test_many_keypoints_per_detection', 'serialise_sv_detections', '14', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output
    for pred in result['predictions']:
        for kp in pred[KEYPOINTS_KEY_IN_INFERENCE_RESPONSE]:
            pass

def test_mixed_optional_fields_subset():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that optional fields are only included when present in data.'
    n = 50
    xyxy = np.random.rand(n, 4).astype(np.float32) * 500
    confidence = np.random.rand(n).astype(np.float32)
    class_id = np.random.randint(0, 5, n).astype(np.int32)
    class_names = [f'class_{i % 5}' for i in range(n)]
    detection_ids = [f'det_{i}' for i in range(n)]
    data_dict = {'class_name': np.array(class_names, dtype=object), DETECTION_ID_KEY: np.array(detection_ids, dtype=object), AREA_KEY_IN_SV_DETECTIONS: np.random.rand(n) * 1000, DETECTED_CODE_KEY: 'TEST_CODE'}
    detections = sv.Detections(xyxy=xyxy, confidence=confidence, class_id=class_id, tracker_id=None, mask=None, data=data_dict)
    _call__bound__arguments = inspect.signature(serialise_sv_detections).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(serialise_sv_detections, 'tests.inference.unit_tests.test_serialise_sv_detections__unit_test_1', None, 'test_mixed_optional_fields_subset', 'serialise_sv_detections', '9', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output
    for pred in result['predictions']:
        pass

def test_detections_performance_100_iterations():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test performance by serializing detections 100 times.'
    n = 100
    xyxy = np.random.rand(n, 4).astype(np.float32) * 500
    confidence = np.random.rand(n).astype(np.float32)
    class_id = np.random.randint(0, 10, n).astype(np.int32)
    class_names = [f'class_{i % 10}' for i in range(n)]
    detection_ids = [f'det_{i}' for i in range(n)]
    data_dict = {'class_name': np.array(class_names, dtype=object), DETECTION_ID_KEY: np.array(detection_ids, dtype=object)}
    detections = sv.Detections(xyxy=xyxy, confidence=confidence, class_id=class_id, tracker_id=None, mask=None, data=data_dict)
    for _ in range(100):
        _call__bound__arguments = inspect.signature(serialise_sv_detections).bind(detections)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(serialise_sv_detections, 'tests.inference.unit_tests.test_serialise_sv_detections__unit_test_1', None, 'test_detections_performance_100_iterations', 'serialise_sv_detections', '9_0', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

def test_extreme_coordinate_ranges():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test serialization with extreme coordinate value ranges.'
    n = 100
    coords1 = np.random.rand(n // 2, 4).astype(np.float32) * 10
    coords2 = np.random.rand(n // 2, 4).astype(np.float32) * 1000000
    xyxy = np.vstack([coords1, coords2]).astype(np.float32)
    confidence = np.random.rand(n).astype(np.float32)
    class_id = np.random.randint(0, 5, n).astype(np.int32)
    class_names = [f'class_{i % 5}' for i in range(n)]
    detection_ids = [f'det_{i}' for i in range(n)]
    data_dict = {'class_name': np.array(class_names, dtype=object), DETECTION_ID_KEY: np.array(detection_ids, dtype=object)}
    detections = sv.Detections(xyxy=xyxy, confidence=confidence, class_id=class_id, tracker_id=None, mask=None, data=data_dict)
    _call__bound__arguments = inspect.signature(serialise_sv_detections).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(serialise_sv_detections, 'tests.inference.unit_tests.test_serialise_sv_detections__unit_test_1', None, 'test_extreme_coordinate_ranges', 'serialise_sv_detections', '11', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output
    small_coords_found = False
    large_coords_found = False
    for pred in result['predictions']:
        if pred[X_KEY] < 100:
            small_coords_found = True
        if pred[X_KEY] > 100000:
            large_coords_found = True

def test_integer_vs_float_precision_consistency():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that integer and float inputs maintain proper types in output.'
    n = 200
    xyxy = np.random.rand(n, 4).astype(np.float32) * 500
    confidence = np.random.rand(n).astype(np.float32)
    class_id = np.random.randint(0, 20, n).astype(np.int32)
    tracker_id = np.random.randint(0, 100, n).astype(np.int32)
    class_names = [f'class_{i % 20}' for i in range(n)]
    detection_ids = [f'det_{i}' for i in range(n)]
    data_dict = {'class_name': np.array(class_names, dtype=object), DETECTION_ID_KEY: np.array(detection_ids, dtype=object)}
    detections = sv.Detections(xyxy=xyxy, confidence=confidence, class_id=class_id, tracker_id=tracker_id, mask=None, data=data_dict)
    _call__bound__arguments = inspect.signature(serialise_sv_detections).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(serialise_sv_detections, 'tests.inference.unit_tests.test_serialise_sv_detections__unit_test_1', None, 'test_integer_vs_float_precision_consistency', 'serialise_sv_detections', '10', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    result = codeflash_output
    for pred in result['predictions']:
        pass