from __future__ import annotations

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

import cv2 as cv
import dill as pickle
import numpy as np
import pytest
import supervision as sv
from inference.core.workflows.core_steps.classical_cv.mask_area_measurement.v1 import \
    compute_detection_areas


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():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that empty detections return an empty list.'
    detections = sv.Detections.empty()
    _call__bound__arguments = inspect.signature(compute_detection_areas).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(compute_detection_areas, 'tests.inference.unit_tests.test_compute_detection_areas__unit_test_1', None, 'test_empty_detections', 'compute_detection_areas', '2', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    areas = codeflash_output

def test_single_bounding_box_no_mask():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test area computation for a single detection with bounding box only.'
    xyxy = np.array([[0.0, 0.0, 10.0, 20.0]])
    detections = sv.Detections(xyxy=xyxy, confidence=np.array([0.9]), class_id=np.array([0]))
    _call__bound__arguments = inspect.signature(compute_detection_areas).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(compute_detection_areas, 'tests.inference.unit_tests.test_compute_detection_areas__unit_test_1', None, 'test_single_bounding_box_no_mask', 'compute_detection_areas', '3', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    areas = codeflash_output

def test_multiple_bounding_boxes_no_mask():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test area computation for multiple detections with bounding boxes only.'
    xyxy = np.array([[0.0, 0.0, 5.0, 10.0], [10.0, 10.0, 20.0, 30.0], [0.0, 0.0, 1.0, 1.0]])
    detections = sv.Detections(xyxy=xyxy, confidence=np.array([0.9, 0.8, 0.7]), class_id=np.array([0, 1, 0]))
    _call__bound__arguments = inspect.signature(compute_detection_areas).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(compute_detection_areas, 'tests.inference.unit_tests.test_compute_detection_areas__unit_test_1', None, 'test_multiple_bounding_boxes_no_mask', 'compute_detection_areas', '3', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    areas = codeflash_output

def test_single_detection_with_full_mask():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test area computation for a detection with a full mask (all pixels non-zero).'
    mask = np.ones((1, 10, 20), dtype=np.uint8)
    xyxy = np.array([[0.0, 0.0, 20.0, 10.0]])
    detections = sv.Detections(xyxy=xyxy, confidence=np.array([0.9]), class_id=np.array([0]), mask=mask)
    _call__bound__arguments = inspect.signature(compute_detection_areas).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(compute_detection_areas, 'tests.inference.unit_tests.test_compute_detection_areas__unit_test_1', None, 'test_single_detection_with_full_mask', 'compute_detection_areas', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    areas = codeflash_output

def test_single_detection_with_partial_mask():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test area computation for a detection with a partial mask (some zero pixels).'
    mask = np.zeros((1, 10, 10), dtype=np.uint8)
    mask[0, :5, :] = 1
    xyxy = np.array([[0.0, 0.0, 10.0, 10.0]])
    detections = sv.Detections(xyxy=xyxy, confidence=np.array([0.9]), class_id=np.array([0]), mask=mask)
    _call__bound__arguments = inspect.signature(compute_detection_areas).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(compute_detection_areas, 'tests.inference.unit_tests.test_compute_detection_areas__unit_test_1', None, 'test_single_detection_with_partial_mask', 'compute_detection_areas', '5', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    areas = codeflash_output

def test_detection_with_mask_with_holes():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that mask pixels with holes (zero values) are not counted.'
    mask = np.ones((1, 10, 10), dtype=np.uint8)
    mask[0, 3:7, 3:7] = 0
    xyxy = np.array([[0.0, 0.0, 10.0, 10.0]])
    detections = sv.Detections(xyxy=xyxy, confidence=np.array([0.9]), class_id=np.array([0]), mask=mask)
    _call__bound__arguments = inspect.signature(compute_detection_areas).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(compute_detection_areas, 'tests.inference.unit_tests.test_compute_detection_areas__unit_test_1', None, 'test_detection_with_mask_with_holes', 'compute_detection_areas', '5', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    areas = codeflash_output

def test_detection_with_empty_mask_falls_back_to_bbox():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that zero-valued masks fall back to bounding box area.'
    mask = np.zeros((1, 10, 10), dtype=np.uint8)
    xyxy = np.array([[0.0, 0.0, 5.0, 10.0]])
    detections = sv.Detections(xyxy=xyxy, confidence=np.array([0.9]), class_id=np.array([0]), mask=mask)
    _call__bound__arguments = inspect.signature(compute_detection_areas).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(compute_detection_areas, 'tests.inference.unit_tests.test_compute_detection_areas__unit_test_1', None, 'test_detection_with_empty_mask_falls_back_to_bbox', 'compute_detection_areas', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    areas = codeflash_output

def test_multiple_detections_mixed_masks():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test multiple detections where some have masks and some rely on bounding boxes.'
    mask_full = np.ones((1, 5, 5), dtype=np.uint8)
    mask_empty = np.zeros((1, 10, 10), dtype=np.uint8)
    mask_partial = np.zeros((1, 8, 8), dtype=np.uint8)
    mask_partial[0, :4, :] = 1
    mask = np.vstack([mask_full, mask_empty, mask_partial])
    xyxy = np.array([[0.0, 0.0, 10.0, 10.0], [0.0, 0.0, 20.0, 20.0], [0.0, 0.0, 16.0, 16.0]])
    detections = sv.Detections(xyxy=xyxy, confidence=np.array([0.9, 0.8, 0.7]), class_id=np.array([0, 1, 0]), mask=mask)
    _call__bound__arguments = inspect.signature(compute_detection_areas).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(compute_detection_areas, 'tests.inference.unit_tests.test_compute_detection_areas__unit_test_1', None, 'test_multiple_detections_mixed_masks', 'compute_detection_areas', '8', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    areas = codeflash_output

def test_bounding_box_with_float_coordinates():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that floating-point bounding box coordinates are handled correctly.'
    xyxy = np.array([[0.5, 0.5, 10.5, 20.5]])
    detections = sv.Detections(xyxy=xyxy, confidence=np.array([0.9]), class_id=np.array([0]))
    _call__bound__arguments = inspect.signature(compute_detection_areas).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(compute_detection_areas, 'tests.inference.unit_tests.test_compute_detection_areas__unit_test_1', None, 'test_bounding_box_with_float_coordinates', 'compute_detection_areas', '3', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    areas = codeflash_output

def test_bounding_box_with_zero_width():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test detection with zero width (line).'
    xyxy = np.array([[5.0, 0.0, 5.0, 10.0]])
    detections = sv.Detections(xyxy=xyxy, confidence=np.array([0.9]), class_id=np.array([0]))
    _call__bound__arguments = inspect.signature(compute_detection_areas).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(compute_detection_areas, 'tests.inference.unit_tests.test_compute_detection_areas__unit_test_1', None, 'test_bounding_box_with_zero_width', 'compute_detection_areas', '3', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    areas = codeflash_output

def test_bounding_box_with_zero_height():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test detection with zero height (line).'
    xyxy = np.array([[0.0, 5.0, 10.0, 5.0]])
    detections = sv.Detections(xyxy=xyxy, confidence=np.array([0.9]), class_id=np.array([0]))
    _call__bound__arguments = inspect.signature(compute_detection_areas).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(compute_detection_areas, 'tests.inference.unit_tests.test_compute_detection_areas__unit_test_1', None, 'test_bounding_box_with_zero_height', 'compute_detection_areas', '3', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    areas = codeflash_output

def test_return_type_is_list():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that the return type is always a list.'
    xyxy = np.array([[0.0, 0.0, 10.0, 10.0]])
    detections = sv.Detections(xyxy=xyxy, confidence=np.array([0.9]), class_id=np.array([0]))
    _call__bound__arguments = inspect.signature(compute_detection_areas).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(compute_detection_areas, 'tests.inference.unit_tests.test_compute_detection_areas__unit_test_1', None, 'test_return_type_is_list', 'compute_detection_areas', '3', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    areas = codeflash_output

def test_return_values_are_floats():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that all returned area values are floats.'
    xyxy = np.array([[0.0, 0.0, 5.0, 10.0], [1.0, 1.0, 11.0, 21.0]])
    detections = sv.Detections(xyxy=xyxy, confidence=np.array([0.9, 0.8]), class_id=np.array([0, 1]))
    _call__bound__arguments = inspect.signature(compute_detection_areas).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(compute_detection_areas, 'tests.inference.unit_tests.test_compute_detection_areas__unit_test_1', None, 'test_return_values_are_floats', 'compute_detection_areas', '3', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    areas = codeflash_output
    for area in areas:
        pass

def test_large_scale_many_bounding_boxes():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test with 100 detections using bounding boxes only.'
    n_detections = 100
    xyxy = np.array([[float(i), float(i), float(i + 10), float(i + 20)] for i in range(n_detections)])
    detections = sv.Detections(xyxy=xyxy, confidence=np.ones(n_detections), class_id=np.zeros(n_detections, dtype=int))
    _call__bound__arguments = inspect.signature(compute_detection_areas).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(compute_detection_areas, 'tests.inference.unit_tests.test_compute_detection_areas__unit_test_1', None, 'test_large_scale_many_bounding_boxes', 'compute_detection_areas', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    areas = codeflash_output
    for area in areas:
        pass

def test_large_scale_many_detections_with_masks():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test with 50 detections that have masks.'
    n_detections = 50
    mask_height, mask_width = (10, 10)
    masks = np.zeros((n_detections, mask_height, mask_width), dtype=np.uint8)
    for i in range(n_detections):
        fill_height = max(1, i * mask_height // (n_detections * 2))
        masks[i, :fill_height, :] = 1
    xyxy = np.ones((n_detections, 4))
    detections = sv.Detections(xyxy=xyxy, confidence=np.ones(n_detections), class_id=np.zeros(n_detections, dtype=int), mask=masks)
    _call__bound__arguments = inspect.signature(compute_detection_areas).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(compute_detection_areas, 'tests.inference.unit_tests.test_compute_detection_areas__unit_test_1', None, 'test_large_scale_many_detections_with_masks', 'compute_detection_areas', '7', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    areas = codeflash_output
    for area in areas:
        pass

def test_large_scale_large_masks():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test with larger mask dimensions.'
    n_detections = 10
    mask_height, mask_width = (100, 100)
    masks = np.ones((n_detections, mask_height, mask_width), dtype=np.uint8)
    for i in range(n_detections):
        remove_count = i * 100
        masks[i].flat[:remove_count] = 0
    xyxy = np.ones((n_detections, 4))
    detections = sv.Detections(xyxy=xyxy, confidence=np.ones(n_detections), class_id=np.zeros(n_detections, dtype=int), mask=masks)
    _call__bound__arguments = inspect.signature(compute_detection_areas).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(compute_detection_areas, 'tests.inference.unit_tests.test_compute_detection_areas__unit_test_1', None, 'test_large_scale_large_masks', 'compute_detection_areas', '7', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    areas = codeflash_output
    for i in range(n_detections - 1):
        pass

def test_very_small_bounding_boxes():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test detections with very small bounding boxes.'
    xyxy = np.array([[0.0, 0.0, 0.1, 0.1], [0.0, 0.0, 0.01, 0.01], [0.0, 0.0, 0.001, 0.001]])
    detections = sv.Detections(xyxy=xyxy, confidence=np.array([0.9, 0.8, 0.7]), class_id=np.array([0, 1, 0]))
    _call__bound__arguments = inspect.signature(compute_detection_areas).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(compute_detection_areas, 'tests.inference.unit_tests.test_compute_detection_areas__unit_test_1', None, 'test_very_small_bounding_boxes', 'compute_detection_areas', '3', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    areas = codeflash_output

def test_very_large_bounding_boxes():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test detections with very large bounding boxes.'
    xyxy = np.array([[0.0, 0.0, 1000.0, 1000.0], [0.0, 0.0, 10000.0, 10000.0]])
    detections = sv.Detections(xyxy=xyxy, confidence=np.array([0.9, 0.8]), class_id=np.array([0, 1]))
    _call__bound__arguments = inspect.signature(compute_detection_areas).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(compute_detection_areas, 'tests.inference.unit_tests.test_compute_detection_areas__unit_test_1', None, 'test_very_large_bounding_boxes', 'compute_detection_areas', '3', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    areas = codeflash_output

def test_negative_coordinates():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that negative bounding box coordinates are handled correctly.'
    xyxy = np.array([[-10.0, -20.0, 10.0, 20.0], [-5.0, -5.0, 5.0, 5.0]])
    detections = sv.Detections(xyxy=xyxy, confidence=np.array([0.9, 0.8]), class_id=np.array([0, 1]))
    _call__bound__arguments = inspect.signature(compute_detection_areas).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(compute_detection_areas, 'tests.inference.unit_tests.test_compute_detection_areas__unit_test_1', None, 'test_negative_coordinates', 'compute_detection_areas', '3', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    areas = codeflash_output

def test_mask_with_different_dtypes():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that masks with different data types are handled correctly.'
    mask_bool = np.array([[[True, True], [False, True]]], dtype=bool)
    xyxy = np.array([[0.0, 0.0, 2.0, 2.0]])
    detections = sv.Detections(xyxy=xyxy, confidence=np.array([0.9]), class_id=np.array([0]), mask=mask_bool)
    _call__bound__arguments = inspect.signature(compute_detection_areas).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(compute_detection_areas, 'tests.inference.unit_tests.test_compute_detection_areas__unit_test_1', None, 'test_mask_with_different_dtypes', 'compute_detection_areas', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    areas = codeflash_output

def test_mask_with_high_values():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that masks with values > 1 (e.g., 255) are correctly handled.'
    mask = np.full((1, 5, 5), 255, dtype=np.uint8)
    xyxy = np.array([[0.0, 0.0, 5.0, 5.0]])
    detections = sv.Detections(xyxy=xyxy, confidence=np.array([0.9]), class_id=np.array([0]), mask=mask)
    _call__bound__arguments = inspect.signature(compute_detection_areas).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(compute_detection_areas, 'tests.inference.unit_tests.test_compute_detection_areas__unit_test_1', None, 'test_mask_with_high_values', 'compute_detection_areas', '4', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    areas = codeflash_output

def test_multiple_detections_consistent_ordering():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that detection areas are returned in the same order as input.'
    xyxy = np.array([[0.0, 0.0, 100.0, 1.0], [0.0, 0.0, 1.0, 100.0], [0.0, 0.0, 10.0, 10.0], [0.0, 0.0, 50.0, 2.0], [0.0, 0.0, 5.0, 5.0]])
    detections = sv.Detections(xyxy=xyxy, confidence=np.array([0.9, 0.8, 0.7, 0.6, 0.5]), class_id=np.array([0, 1, 0, 1, 0]))
    _call__bound__arguments = inspect.signature(compute_detection_areas).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(compute_detection_areas, 'tests.inference.unit_tests.test_compute_detection_areas__unit_test_1', None, 'test_multiple_detections_consistent_ordering', 'compute_detection_areas', '3', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    areas = codeflash_output

def test_single_pixel_mask():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test a mask with only a single non-zero pixel.'
    mask = np.zeros((1, 10, 10), dtype=np.uint8)
    mask[0, 5, 5] = 1
    xyxy = np.array([[0.0, 0.0, 10.0, 10.0]])
    detections = sv.Detections(xyxy=xyxy, confidence=np.array([0.9]), class_id=np.array([0]), mask=mask)
    _call__bound__arguments = inspect.signature(compute_detection_areas).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(compute_detection_areas, 'tests.inference.unit_tests.test_compute_detection_areas__unit_test_1', None, 'test_single_pixel_mask', 'compute_detection_areas', '5', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    areas = codeflash_output

def test_near_integer_float_coordinates():
    np.random.seed(42)
    codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
    'Test that near-integer float coordinates produce expected areas.'
    xyxy = np.array([[1e-07, 1e-07, 10.0000001, 20.0000001]])
    detections = sv.Detections(xyxy=xyxy, confidence=np.array([0.9]), class_id=np.array([0]))
    _call__bound__arguments = inspect.signature(compute_detection_areas).bind(detections)
    _call__bound__arguments.apply_defaults()
    codeflash_return_value = codeflash_wrap(compute_detection_areas, 'tests.inference.unit_tests.test_compute_detection_areas__unit_test_1', None, 'test_near_integer_float_coordinates', 'compute_detection_areas', '3', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
    codeflash_output = codeflash_return_value
    areas = codeflash_output