from __future__ import annotations

import gc
import inspect
import logging
import os
import time
from pathlib import Path
from typing import Any, Dict, List
from unittest.mock import MagicMock, patch

import cv2
import dill as pickle
import numpy as np
import pytest
from inference.enterprise.workflows.enterprise_blocks.sinks.event_writer.v1 import \
    _detections_to_v2_instance_segmentations


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

class TestDetectionsToV2InstanceSegmentations:
    """Test suite for _detections_to_v2_instance_segmentations function."""

    def test_none_input_returns_empty_list(self):
        np.random.seed(42)
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test that None input returns an empty list.'
        _call__bound__arguments = inspect.signature(_detections_to_v2_instance_segmentations).bind(None)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(_detections_to_v2_instance_segmentations, 'tests.inference.unit_tests.test__detections_to_v2_instance_segmentations__unit_test_1', 'TestDetectionsToV2InstanceSegmentations', 'test_none_input_returns_empty_list', '_detections_to_v2_instance_segmentations', '1', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_empty_detections_returns_empty_list(self):
        np.random.seed(42)
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test that detections with no items returns empty list.'
        detections = MagicMock()
        detections.xyxy = np.array([])
        detections.confidence = np.array([])
        detections.data = {'class_name': []}
        detections.mask = None
        _call__bound__arguments = inspect.signature(_detections_to_v2_instance_segmentations).bind(detections)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(_detections_to_v2_instance_segmentations, 'tests.inference.unit_tests.test__detections_to_v2_instance_segmentations__unit_test_1', 'TestDetectionsToV2InstanceSegmentations', 'test_empty_detections_returns_empty_list', '_detections_to_v2_instance_segmentations', '6', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_single_detection_without_mask_not_included(self):
        np.random.seed(42)
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test that detection without sufficient points is not included in results.'
        detections = MagicMock()
        detections.xyxy = np.array([[10.0, 20.0, 30.0, 40.0]])
        detections.confidence = np.array([0.95])
        detections.data = {'class_name': ['cat']}
        detections.mask = None
        _call__bound__arguments = inspect.signature(_detections_to_v2_instance_segmentations).bind(detections)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(_detections_to_v2_instance_segmentations, 'tests.inference.unit_tests.test__detections_to_v2_instance_segmentations__unit_test_1', 'TestDetectionsToV2InstanceSegmentations', 'test_single_detection_without_mask_not_included', '_detections_to_v2_instance_segmentations', '6', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_single_detection_with_valid_mask(self):
        np.random.seed(42)
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test conversion of a single detection with a valid triangular mask.'
        mask = np.zeros((100, 100), dtype=np.float32)
        triangle = np.array([[10, 10], [90, 10], [50, 90]], dtype=np.int32)
        cv2.drawContours(mask, [triangle], 0, 1, -1)
        detections = MagicMock()
        detections.xyxy = np.array([[5.0, 5.0, 95.0, 95.0]])
        detections.confidence = np.array([0.87])
        detections.data = {'class_name': ['dog']}
        detections.mask = np.array([mask])
        _call__bound__arguments = inspect.signature(_detections_to_v2_instance_segmentations).bind(detections)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(_detections_to_v2_instance_segmentations, 'tests.inference.unit_tests.test__detections_to_v2_instance_segmentations__unit_test_1', 'TestDetectionsToV2InstanceSegmentations', 'test_single_detection_with_valid_mask', '_detections_to_v2_instance_segmentations', '9', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_multiple_detections_with_mixed_masks(self):
        np.random.seed(42)
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test conversion with multiple detections, some with valid masks.'
        mask1 = np.zeros((100, 100), dtype=np.float32)
        triangle1 = np.array([[10, 10], [90, 10], [50, 90]], dtype=np.int32)
        cv2.drawContours(mask1, [triangle1], 0, 1, -1)
        mask2 = np.zeros((100, 100), dtype=np.float32)
        triangle2 = np.array([[20, 20], [80, 20], [50, 80]], dtype=np.int32)
        cv2.drawContours(mask2, [triangle2], 0, 1, -1)
        detections = MagicMock()
        detections.xyxy = np.array([[5.0, 5.0, 95.0, 95.0], [0.0, 0.0, 100.0, 100.0]])
        detections.confidence = np.array([0.9, 0.75])
        detections.data = {'class_name': ['cat', 'dog']}
        detections.mask = np.array([mask1, mask2])
        _call__bound__arguments = inspect.signature(_detections_to_v2_instance_segmentations).bind(detections)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(_detections_to_v2_instance_segmentations, 'tests.inference.unit_tests.test__detections_to_v2_instance_segmentations__unit_test_1', 'TestDetectionsToV2InstanceSegmentations', 'test_multiple_detections_with_mixed_masks', '_detections_to_v2_instance_segmentations', '12', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_class_name_missing_uses_unknown(self):
        np.random.seed(42)
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        "Test that missing class names default to 'unknown'."
        mask = np.zeros((100, 100), dtype=np.float32)
        triangle = np.array([[10, 10], [90, 10], [50, 90]], dtype=np.int32)
        cv2.drawContours(mask, [triangle], 0, 1, -1)
        detections = MagicMock()
        detections.xyxy = np.array([[5.0, 5.0, 95.0, 95.0]])
        detections.confidence = np.array([0.5])
        detections.data = {'class_name': []}
        detections.mask = np.array([mask])
        _call__bound__arguments = inspect.signature(_detections_to_v2_instance_segmentations).bind(detections)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(_detections_to_v2_instance_segmentations, 'tests.inference.unit_tests.test__detections_to_v2_instance_segmentations__unit_test_1', 'TestDetectionsToV2InstanceSegmentations', 'test_class_name_missing_uses_unknown', '_detections_to_v2_instance_segmentations', '9', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_confidence_none_uses_default_zero(self):
        np.random.seed(42)
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test that None confidence defaults to 0.0.'
        mask = np.zeros((100, 100), dtype=np.float32)
        triangle = np.array([[10, 10], [90, 10], [50, 90]], dtype=np.int32)
        cv2.drawContours(mask, [triangle], 0, 1, -1)
        detections = MagicMock()
        detections.xyxy = np.array([[5.0, 5.0, 95.0, 95.0]])
        detections.confidence = None
        detections.data = {'class_name': ['cat']}
        detections.mask = np.array([mask])
        _call__bound__arguments = inspect.signature(_detections_to_v2_instance_segmentations).bind(detections)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(_detections_to_v2_instance_segmentations, 'tests.inference.unit_tests.test__detections_to_v2_instance_segmentations__unit_test_1', 'TestDetectionsToV2InstanceSegmentations', 'test_confidence_none_uses_default_zero', '_detections_to_v2_instance_segmentations', '9', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_bbox_center_calculation_correct(self):
        np.random.seed(42)
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test that bounding box center (x, y) is calculated correctly.'
        mask = np.zeros((100, 100), dtype=np.float32)
        triangle = np.array([[10, 10], [90, 10], [50, 90]], dtype=np.int32)
        cv2.drawContours(mask, [triangle], 0, 1, -1)
        detections = MagicMock()
        detections.xyxy = np.array([[0.0, 0.0, 100.0, 200.0]])
        detections.confidence = np.array([0.5])
        detections.data = {'class_name': ['cat']}
        detections.mask = np.array([mask])
        _call__bound__arguments = inspect.signature(_detections_to_v2_instance_segmentations).bind(detections)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(_detections_to_v2_instance_segmentations, 'tests.inference.unit_tests.test__detections_to_v2_instance_segmentations__unit_test_1', 'TestDetectionsToV2InstanceSegmentations', 'test_bbox_center_calculation_correct', '_detections_to_v2_instance_segmentations', '9', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_detection_index_out_of_class_names_range(self):
        np.random.seed(42)
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test handling when detection index exceeds class_names list length.'
        mask = np.zeros((100, 100), dtype=np.float32)
        triangle = np.array([[10, 10], [90, 10], [50, 90]], dtype=np.int32)
        cv2.drawContours(mask, [triangle], 0, 1, -1)
        detections = MagicMock()
        detections.xyxy = np.array([[5.0, 5.0, 95.0, 95.0], [0.0, 0.0, 50.0, 50.0]])
        detections.confidence = np.array([0.9, 0.8])
        detections.data = {'class_name': ['cat']}
        detections.mask = np.array([mask, mask])
        _call__bound__arguments = inspect.signature(_detections_to_v2_instance_segmentations).bind(detections)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(_detections_to_v2_instance_segmentations, 'tests.inference.unit_tests.test__detections_to_v2_instance_segmentations__unit_test_1', 'TestDetectionsToV2InstanceSegmentations', 'test_detection_index_out_of_class_names_range', '_detections_to_v2_instance_segmentations', '9', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_detection_index_out_of_mask_range(self):
        np.random.seed(42)
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test handling when detection index exceeds mask list length.'
        mask = np.zeros((100, 100), dtype=np.float32)
        triangle = np.array([[10, 10], [90, 10], [50, 90]], dtype=np.int32)
        cv2.drawContours(mask, [triangle], 0, 1, -1)
        detections = MagicMock()
        detections.xyxy = np.array([[5.0, 5.0, 95.0, 95.0], [0.0, 0.0, 50.0, 50.0]])
        detections.confidence = np.array([0.9, 0.8])
        detections.data = {'class_name': ['cat', 'dog']}
        detections.mask = np.array([mask])
        _call__bound__arguments = inspect.signature(_detections_to_v2_instance_segmentations).bind(detections)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(_detections_to_v2_instance_segmentations, 'tests.inference.unit_tests.test__detections_to_v2_instance_segmentations__unit_test_1', 'TestDetectionsToV2InstanceSegmentations', 'test_detection_index_out_of_mask_range', '_detections_to_v2_instance_segmentations', '9', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_mask_with_insufficient_contour_points(self):
        np.random.seed(42)
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test that masks with fewer than 3 contour points are not included.'
        mask = np.zeros((100, 100), dtype=np.float32)
        cv2.line(mask, (10, 10), (12, 12), 1, 1)
        detections = MagicMock()
        detections.xyxy = np.array([[5.0, 5.0, 95.0, 95.0]])
        detections.confidence = np.array([0.5])
        detections.data = {'class_name': ['cat']}
        detections.mask = np.array([mask])
        _call__bound__arguments = inspect.signature(_detections_to_v2_instance_segmentations).bind(detections)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(_detections_to_v2_instance_segmentations, 'tests.inference.unit_tests.test__detections_to_v2_instance_segmentations__unit_test_1', 'TestDetectionsToV2InstanceSegmentations', 'test_mask_with_insufficient_contour_points', '_detections_to_v2_instance_segmentations', '8', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_empty_mask_array(self):
        np.random.seed(42)
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test handling of empty mask array.'
        detections = MagicMock()
        detections.xyxy = np.array([[5.0, 5.0, 95.0, 95.0]])
        detections.confidence = np.array([0.5])
        detections.data = {'class_name': ['cat']}
        detections.mask = np.array([])
        _call__bound__arguments = inspect.signature(_detections_to_v2_instance_segmentations).bind(detections)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(_detections_to_v2_instance_segmentations, 'tests.inference.unit_tests.test__detections_to_v2_instance_segmentations__unit_test_1', 'TestDetectionsToV2InstanceSegmentations', 'test_empty_mask_array', '_detections_to_v2_instance_segmentations', '6', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_zero_width_bbox(self):
        np.random.seed(42)
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test bounding box with zero width.'
        mask = np.zeros((100, 100), dtype=np.float32)
        triangle = np.array([[10, 10], [90, 10], [50, 90]], dtype=np.int32)
        cv2.drawContours(mask, [triangle], 0, 1, -1)
        detections = MagicMock()
        detections.xyxy = np.array([[50.0, 5.0, 50.0, 95.0]])
        detections.confidence = np.array([0.5])
        detections.data = {'class_name': ['cat']}
        detections.mask = np.array([mask])
        _call__bound__arguments = inspect.signature(_detections_to_v2_instance_segmentations).bind(detections)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(_detections_to_v2_instance_segmentations, 'tests.inference.unit_tests.test__detections_to_v2_instance_segmentations__unit_test_1', 'TestDetectionsToV2InstanceSegmentations', 'test_zero_width_bbox', '_detections_to_v2_instance_segmentations', '9', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_zero_height_bbox(self):
        np.random.seed(42)
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test bounding box with zero height.'
        mask = np.zeros((100, 100), dtype=np.float32)
        triangle = np.array([[10, 10], [90, 10], [50, 90]], dtype=np.int32)
        cv2.drawContours(mask, [triangle], 0, 1, -1)
        detections = MagicMock()
        detections.xyxy = np.array([[5.0, 50.0, 95.0, 50.0]])
        detections.confidence = np.array([0.5])
        detections.data = {'class_name': ['cat']}
        detections.mask = np.array([mask])
        _call__bound__arguments = inspect.signature(_detections_to_v2_instance_segmentations).bind(detections)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(_detections_to_v2_instance_segmentations, 'tests.inference.unit_tests.test__detections_to_v2_instance_segmentations__unit_test_1', 'TestDetectionsToV2InstanceSegmentations', 'test_zero_height_bbox', '_detections_to_v2_instance_segmentations', '9', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_negative_bbox_coordinates(self):
        np.random.seed(42)
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test bounding box with negative coordinates.'
        mask = np.zeros((100, 100), dtype=np.float32)
        triangle = np.array([[10, 10], [90, 10], [50, 90]], dtype=np.int32)
        cv2.drawContours(mask, [triangle], 0, 1, -1)
        detections = MagicMock()
        detections.xyxy = np.array([[-50.0, -100.0, 50.0, 100.0]])
        detections.confidence = np.array([0.5])
        detections.data = {'class_name': ['cat']}
        detections.mask = np.array([mask])
        _call__bound__arguments = inspect.signature(_detections_to_v2_instance_segmentations).bind(detections)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(_detections_to_v2_instance_segmentations, 'tests.inference.unit_tests.test__detections_to_v2_instance_segmentations__unit_test_1', 'TestDetectionsToV2InstanceSegmentations', 'test_negative_bbox_coordinates', '_detections_to_v2_instance_segmentations', '9', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_very_large_bbox_coordinates(self):
        np.random.seed(42)
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test bounding box with very large coordinates.'
        mask = np.zeros((100, 100), dtype=np.float32)
        triangle = np.array([[10, 10], [90, 10], [50, 90]], dtype=np.int32)
        cv2.drawContours(mask, [triangle], 0, 1, -1)
        detections = MagicMock()
        detections.xyxy = np.array([[1000000.0, 1000000.0, 2000000.0, 2000000.0]])
        detections.confidence = np.array([0.5])
        detections.data = {'class_name': ['cat']}
        detections.mask = np.array([mask])
        _call__bound__arguments = inspect.signature(_detections_to_v2_instance_segmentations).bind(detections)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(_detections_to_v2_instance_segmentations, 'tests.inference.unit_tests.test__detections_to_v2_instance_segmentations__unit_test_1', 'TestDetectionsToV2InstanceSegmentations', 'test_very_large_bbox_coordinates', '_detections_to_v2_instance_segmentations', '9', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_confidence_value_one(self):
        np.random.seed(42)
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test confidence value of exactly 1.0.'
        mask = np.zeros((100, 100), dtype=np.float32)
        triangle = np.array([[10, 10], [90, 10], [50, 90]], dtype=np.int32)
        cv2.drawContours(mask, [triangle], 0, 1, -1)
        detections = MagicMock()
        detections.xyxy = np.array([[5.0, 5.0, 95.0, 95.0]])
        detections.confidence = np.array([1.0])
        detections.data = {'class_name': ['cat']}
        detections.mask = np.array([mask])
        _call__bound__arguments = inspect.signature(_detections_to_v2_instance_segmentations).bind(detections)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(_detections_to_v2_instance_segmentations, 'tests.inference.unit_tests.test__detections_to_v2_instance_segmentations__unit_test_1', 'TestDetectionsToV2InstanceSegmentations', 'test_confidence_value_one', '_detections_to_v2_instance_segmentations', '9', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_confidence_value_zero(self):
        np.random.seed(42)
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test confidence value of exactly 0.0.'
        mask = np.zeros((100, 100), dtype=np.float32)
        triangle = np.array([[10, 10], [90, 10], [50, 90]], dtype=np.int32)
        cv2.drawContours(mask, [triangle], 0, 1, -1)
        detections = MagicMock()
        detections.xyxy = np.array([[5.0, 5.0, 95.0, 95.0]])
        detections.confidence = np.array([0.0])
        detections.data = {'class_name': ['cat']}
        detections.mask = np.array([mask])
        _call__bound__arguments = inspect.signature(_detections_to_v2_instance_segmentations).bind(detections)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(_detections_to_v2_instance_segmentations, 'tests.inference.unit_tests.test__detections_to_v2_instance_segmentations__unit_test_1', 'TestDetectionsToV2InstanceSegmentations', 'test_confidence_value_zero', '_detections_to_v2_instance_segmentations', '9', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_exception_handling_invalid_xyxy(self):
        np.random.seed(42)
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test exception handling when xyxy is invalid.'
        detections = MagicMock()
        detections.xyxy = 'invalid'
        detections.confidence = np.array([0.5])
        detections.data = {'class_name': ['cat']}
        detections.mask = None
        _call__bound__arguments = inspect.signature(_detections_to_v2_instance_segmentations).bind(detections)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(_detections_to_v2_instance_segmentations, 'tests.inference.unit_tests.test__detections_to_v2_instance_segmentations__unit_test_1', 'TestDetectionsToV2InstanceSegmentations', 'test_exception_handling_invalid_xyxy', '_detections_to_v2_instance_segmentations', '6', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_exception_handling_missing_data_attribute(self):
        np.random.seed(42)
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test exception handling when data attribute is missing.'
        detections = MagicMock()
        detections.xyxy = np.array([[5.0, 5.0, 95.0, 95.0]])
        detections.confidence = np.array([0.5])
        del detections.data
        detections.mask = None
        _call__bound__arguments = inspect.signature(_detections_to_v2_instance_segmentations).bind(detections)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(_detections_to_v2_instance_segmentations, 'tests.inference.unit_tests.test__detections_to_v2_instance_segmentations__unit_test_1', 'TestDetectionsToV2InstanceSegmentations', 'test_exception_handling_missing_data_attribute', '_detections_to_v2_instance_segmentations', '6', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_points_field_initialized_empty(self):
        np.random.seed(42)
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test that points field is initialized as empty list.'
        detections = MagicMock()
        detections.xyxy = np.array([[5.0, 5.0, 95.0, 95.0]])
        detections.confidence = np.array([0.5])
        detections.data = {'class_name': ['cat']}
        detections.mask = None
        _call__bound__arguments = inspect.signature(_detections_to_v2_instance_segmentations).bind(detections)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(_detections_to_v2_instance_segmentations, 'tests.inference.unit_tests.test__detections_to_v2_instance_segmentations__unit_test_1', 'TestDetectionsToV2InstanceSegmentations', 'test_points_field_initialized_empty', '_detections_to_v2_instance_segmentations', '6', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_class_name_special_characters(self):
        np.random.seed(42)
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test class name with special characters.'
        mask = np.zeros((100, 100), dtype=np.float32)
        triangle = np.array([[10, 10], [90, 10], [50, 90]], dtype=np.int32)
        cv2.drawContours(mask, [triangle], 0, 1, -1)
        detections = MagicMock()
        detections.xyxy = np.array([[5.0, 5.0, 95.0, 95.0]])
        detections.confidence = np.array([0.5])
        detections.data = {'class_name': ['cat_123@#$%']}
        detections.mask = np.array([mask])
        _call__bound__arguments = inspect.signature(_detections_to_v2_instance_segmentations).bind(detections)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(_detections_to_v2_instance_segmentations, 'tests.inference.unit_tests.test__detections_to_v2_instance_segmentations__unit_test_1', 'TestDetectionsToV2InstanceSegmentations', 'test_class_name_special_characters', '_detections_to_v2_instance_segmentations', '9', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_points_conversion_to_float_list(self):
        np.random.seed(42)
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test that points are converted to float lists.'
        mask = np.zeros((100, 100), dtype=np.float32)
        triangle = np.array([[10, 10], [90, 10], [50, 90]], dtype=np.int32)
        cv2.drawContours(mask, [triangle], 0, 1, -1)
        detections = MagicMock()
        detections.xyxy = np.array([[5.0, 5.0, 95.0, 95.0]])
        detections.confidence = np.array([0.5])
        detections.data = {'class_name': ['cat']}
        detections.mask = np.array([mask])
        _call__bound__arguments = inspect.signature(_detections_to_v2_instance_segmentations).bind(detections)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(_detections_to_v2_instance_segmentations, 'tests.inference.unit_tests.test__detections_to_v2_instance_segmentations__unit_test_1', 'TestDetectionsToV2InstanceSegmentations', 'test_points_conversion_to_float_list', '_detections_to_v2_instance_segmentations', '9', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output
        for point in result[0]['points']:
            pass

    def test_mask_with_multiple_contours_selects_largest(self):
        np.random.seed(42)
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test that largest contour is selected when multiple exist.'
        mask = np.zeros((100, 100), dtype=np.float32)
        large_triangle = np.array([[5, 5], [95, 5], [50, 95]], dtype=np.int32)
        cv2.drawContours(mask, [large_triangle], 0, 1, -1)
        cv2.circle(mask, (20, 20), 2, 1, -1)
        detections = MagicMock()
        detections.xyxy = np.array([[0.0, 0.0, 100.0, 100.0]])
        detections.confidence = np.array([0.5])
        detections.data = {'class_name': ['cat']}
        detections.mask = np.array([mask])
        _call__bound__arguments = inspect.signature(_detections_to_v2_instance_segmentations).bind(detections)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(_detections_to_v2_instance_segmentations, 'tests.inference.unit_tests.test__detections_to_v2_instance_segmentations__unit_test_1', 'TestDetectionsToV2InstanceSegmentations', 'test_mask_with_multiple_contours_selects_largest', '_detections_to_v2_instance_segmentations', '10', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_hundred_detections_with_masks(self):
        np.random.seed(42)
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test conversion of 100 detections with valid masks.'
        num_detections = 100
        masks = []
        xyxy_list = []
        confidence_list = []
        class_names = []
        for i in range(num_detections):
            mask = np.zeros((100, 100), dtype=np.float32)
            triangle = np.array([[10, 10], [90, 10], [50, 90]], dtype=np.int32)
            cv2.drawContours(mask, [triangle], 0, 1, -1)
            masks.append(mask)
            xyxy_list.append([5.0 + i, 5.0 + i, 95.0 + i, 95.0 + i])
            confidence_list.append(0.5 + i % 50 / 100.0)
            class_names.append(f'class_{i % 10}')
        detections = MagicMock()
        detections.xyxy = np.array(xyxy_list)
        detections.confidence = np.array(confidence_list)
        detections.data = {'class_name': class_names}
        detections.mask = np.array(masks)
        _call__bound__arguments = inspect.signature(_detections_to_v2_instance_segmentations).bind(detections)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(_detections_to_v2_instance_segmentations, 'tests.inference.unit_tests.test__detections_to_v2_instance_segmentations__unit_test_1', 'TestDetectionsToV2InstanceSegmentations', 'test_hundred_detections_with_masks', '_detections_to_v2_instance_segmentations', '12', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_five_hundred_detections_mixed_validity(self):
        np.random.seed(42)
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test conversion of 500 detections with mix of valid and invalid masks.'
        num_detections = 500
        masks = []
        xyxy_list = []
        confidence_list = []
        class_names = []
        valid_count = 0
        for i in range(num_detections):
            if i % 2 == 0:
                mask = np.zeros((100, 100), dtype=np.float32)
                triangle = np.array([[10, 10], [90, 10], [50, 90]], dtype=np.int32)
                cv2.drawContours(mask, [triangle], 0, 1, -1)
                valid_count += 1
            else:
                mask = np.zeros((100, 100), dtype=np.float32)
            masks.append(mask)
            xyxy_list.append([5.0, 5.0, 95.0, 95.0])
            confidence_list.append(0.75)
            class_names.append('cat')
        detections = MagicMock()
        detections.xyxy = np.array(xyxy_list)
        detections.confidence = np.array(confidence_list)
        detections.data = {'class_name': class_names}
        detections.mask = np.array(masks)
        _call__bound__arguments = inspect.signature(_detections_to_v2_instance_segmentations).bind(detections)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(_detections_to_v2_instance_segmentations, 'tests.inference.unit_tests.test__detections_to_v2_instance_segmentations__unit_test_1', 'TestDetectionsToV2InstanceSegmentations', 'test_five_hundred_detections_mixed_validity', '_detections_to_v2_instance_segmentations', '13', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_thousand_detections_performance(self):
        np.random.seed(42)
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test conversion of 1000 detections for performance.'
        num_detections = 1000
        masks = []
        xyxy_list = []
        confidence_list = []
        class_names = []
        for i in range(num_detections):
            mask = np.zeros((50, 50), dtype=np.float32)
            square = np.array([[10, 10], [40, 10], [40, 40], [10, 40]], dtype=np.int32)
            cv2.drawContours(mask, [square], 0, 1, -1)
            masks.append(mask)
            xyxy_list.append([0.0, 0.0, 50.0, 50.0])
            confidence_list.append(0.9)
            class_names.append('object')
        detections = MagicMock()
        detections.xyxy = np.array(xyxy_list)
        detections.confidence = np.array(confidence_list)
        detections.data = {'class_name': class_names}
        detections.mask = np.array(masks)
        _call__bound__arguments = inspect.signature(_detections_to_v2_instance_segmentations).bind(detections)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(_detections_to_v2_instance_segmentations, 'tests.inference.unit_tests.test__detections_to_v2_instance_segmentations__unit_test_1', 'TestDetectionsToV2InstanceSegmentations', 'test_thousand_detections_performance', '_detections_to_v2_instance_segmentations', '12', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output

    def test_result_structure_consistency_large_scale(self):
        np.random.seed(42)
        codeflash_loop_index = int(os.environ['CODEFLASH_LOOP_INDEX'])
        'Test that result structure is consistent across all 100 entries.'
        num_detections = 100
        masks = []
        xyxy_list = []
        confidence_list = []
        class_names = []
        for i in range(num_detections):
            mask = np.zeros((100, 100), dtype=np.float32)
            triangle = np.array([[10, 10], [90, 10], [50, 90]], dtype=np.int32)
            cv2.drawContours(mask, [triangle], 0, 1, -1)
            masks.append(mask)
            xyxy_list.append([5.0, 5.0, 95.0, 95.0])
            confidence_list.append(0.8)
            class_names.append(f'cls_{i % 20}')
        detections = MagicMock()
        detections.xyxy = np.array(xyxy_list)
        detections.confidence = np.array(confidence_list)
        detections.data = {'class_name': class_names}
        detections.mask = np.array(masks)
        _call__bound__arguments = inspect.signature(_detections_to_v2_instance_segmentations).bind(detections)
        _call__bound__arguments.apply_defaults()
        codeflash_return_value = codeflash_wrap(_detections_to_v2_instance_segmentations, 'tests.inference.unit_tests.test__detections_to_v2_instance_segmentations__unit_test_1', 'TestDetectionsToV2InstanceSegmentations', 'test_result_structure_consistency_large_scale', '_detections_to_v2_instance_segmentations', '12', codeflash_loop_index, *_call__bound__arguments.args, **_call__bound__arguments.kwargs)
        codeflash_output = codeflash_return_value
        result = codeflash_output
        required_keys = {'class', 'x', 'y', 'width', 'height', 'confidence', 'points'}
        for entry in result:
            pass