|
4 | 4 | import json |
5 | 5 | import tempfile |
6 | 6 | from collections import Counter |
| 7 | +from typing import TYPE_CHECKING |
7 | 8 |
|
8 | 9 | import pytest |
9 | 10 | import yaml |
|
24 | 25 | UniformDistributionParams, |
25 | 26 | load_model_configs, |
26 | 27 | ) |
| 28 | +from data_designer.lazy_heavy_imports import np |
27 | 29 |
|
| 30 | +if TYPE_CHECKING: |
| 31 | + import numpy as np |
28 | 32 |
|
29 | | -def test_image_context_get_context(): |
| 33 | + |
| 34 | +def test_image_context_get_contexts_single_string(): |
| 35 | + """Test get_contexts with a single string value.""" |
30 | 36 | image_context = ImageContext( |
31 | 37 | column_name="image_base64", data_type=ModalityDataType.BASE64, image_format=ImageFormat.PNG |
32 | 38 | ) |
33 | | - assert image_context.get_context({"image_base64": "somebase64encodedimagestring"}) == { |
34 | | - "type": "image_url", |
35 | | - "image_url": {"url": "data:image/png;base64,somebase64encodedimagestring", "format": "png"}, |
36 | | - } |
| 39 | + assert image_context.get_contexts({"image_base64": "somebase64encodedimagestring"}) == [ |
| 40 | + { |
| 41 | + "type": "image_url", |
| 42 | + "image_url": {"url": "data:image/png;base64,somebase64encodedimagestring", "format": "png"}, |
| 43 | + } |
| 44 | + ] |
37 | 45 |
|
38 | 46 | image_context = ImageContext(column_name="image_url", data_type=ModalityDataType.URL) |
39 | | - assert image_context.get_context({"image_url": "https://example.com/examle_image.png"}) == { |
40 | | - "type": "image_url", |
41 | | - "image_url": "https://example.com/examle_image.png", |
42 | | - } |
| 47 | + assert image_context.get_contexts({"image_url": "https://example.com/examle_image.png"}) == [ |
| 48 | + { |
| 49 | + "type": "image_url", |
| 50 | + "image_url": "https://example.com/examle_image.png", |
| 51 | + } |
| 52 | + ] |
| 53 | + |
| 54 | + |
| 55 | +def test_image_context_get_contexts_list_of_strings(): |
| 56 | + """Test get_contexts with a list of strings.""" |
| 57 | + image_context = ImageContext( |
| 58 | + column_name="image_base64", data_type=ModalityDataType.BASE64, image_format=ImageFormat.PNG |
| 59 | + ) |
| 60 | + assert image_context.get_contexts({"image_base64": ["image1base64", "image2base64", "image3base64"]}) == [ |
| 61 | + { |
| 62 | + "type": "image_url", |
| 63 | + "image_url": {"url": "data:image/png;base64,image1base64", "format": "png"}, |
| 64 | + }, |
| 65 | + { |
| 66 | + "type": "image_url", |
| 67 | + "image_url": {"url": "data:image/png;base64,image2base64", "format": "png"}, |
| 68 | + }, |
| 69 | + { |
| 70 | + "type": "image_url", |
| 71 | + "image_url": {"url": "data:image/png;base64,image3base64", "format": "png"}, |
| 72 | + }, |
| 73 | + ] |
| 74 | + |
| 75 | + image_context = ImageContext(column_name="image_url", data_type=ModalityDataType.URL) |
| 76 | + assert image_context.get_contexts( |
| 77 | + {"image_url": ["https://example.com/image1.png", "https://example.com/image2.png"]} |
| 78 | + ) == [ |
| 79 | + { |
| 80 | + "type": "image_url", |
| 81 | + "image_url": "https://example.com/image1.png", |
| 82 | + }, |
| 83 | + { |
| 84 | + "type": "image_url", |
| 85 | + "image_url": "https://example.com/image2.png", |
| 86 | + }, |
| 87 | + ] |
| 88 | + |
| 89 | + |
| 90 | +def test_image_context_get_contexts_numpy_array(): |
| 91 | + """Test get_contexts with numpy arrays (happens after parquet serialization).""" |
| 92 | + image_context = ImageContext( |
| 93 | + column_name="image_base64", data_type=ModalityDataType.BASE64, image_format=ImageFormat.PNG |
| 94 | + ) |
| 95 | + numpy_array = np.array(["image1base64", "image2base64"]) |
| 96 | + assert image_context.get_contexts({"image_base64": numpy_array}) == [ |
| 97 | + { |
| 98 | + "type": "image_url", |
| 99 | + "image_url": {"url": "data:image/png;base64,image1base64", "format": "png"}, |
| 100 | + }, |
| 101 | + { |
| 102 | + "type": "image_url", |
| 103 | + "image_url": {"url": "data:image/png;base64,image2base64", "format": "png"}, |
| 104 | + }, |
| 105 | + ] |
| 106 | + |
| 107 | + image_context = ImageContext(column_name="image_url", data_type=ModalityDataType.URL) |
| 108 | + numpy_array = np.array(["https://example.com/image1.png", "https://example.com/image2.png"]) |
| 109 | + assert image_context.get_contexts({"image_url": numpy_array}) == [ |
| 110 | + { |
| 111 | + "type": "image_url", |
| 112 | + "image_url": "https://example.com/image1.png", |
| 113 | + }, |
| 114 | + { |
| 115 | + "type": "image_url", |
| 116 | + "image_url": "https://example.com/image2.png", |
| 117 | + }, |
| 118 | + ] |
| 119 | + |
| 120 | + |
| 121 | +def test_image_context_get_contexts_json_serialized_list(): |
| 122 | + """Test get_contexts with a JSON serialized list of strings.""" |
| 123 | + image_context = ImageContext( |
| 124 | + column_name="image_base64", data_type=ModalityDataType.BASE64, image_format=ImageFormat.PNG |
| 125 | + ) |
| 126 | + json_str = json.dumps(["image1base64", "image2base64"]) |
| 127 | + assert image_context.get_contexts({"image_base64": json_str}) == [ |
| 128 | + { |
| 129 | + "type": "image_url", |
| 130 | + "image_url": {"url": "data:image/png;base64,image1base64", "format": "png"}, |
| 131 | + }, |
| 132 | + { |
| 133 | + "type": "image_url", |
| 134 | + "image_url": {"url": "data:image/png;base64,image2base64", "format": "png"}, |
| 135 | + }, |
| 136 | + ] |
| 137 | + |
| 138 | + image_context = ImageContext(column_name="image_url", data_type=ModalityDataType.URL) |
| 139 | + json_str = json.dumps(["https://example.com/image1.png", "https://example.com/image2.png"]) |
| 140 | + assert image_context.get_contexts({"image_url": json_str}) == [ |
| 141 | + { |
| 142 | + "type": "image_url", |
| 143 | + "image_url": "https://example.com/image1.png", |
| 144 | + }, |
| 145 | + { |
| 146 | + "type": "image_url", |
| 147 | + "image_url": "https://example.com/image2.png", |
| 148 | + }, |
| 149 | + ] |
| 150 | + |
| 151 | + |
| 152 | +def test_image_context_get_contexts_json_string_not_list(): |
| 153 | + """Test get_contexts with a JSON string that isn't a list (should treat as single string).""" |
| 154 | + image_context = ImageContext(column_name="image_url", data_type=ModalityDataType.URL) |
| 155 | + json_str = json.dumps({"nested": "object"}) |
| 156 | + # Should treat the entire JSON string as a single image URL |
| 157 | + assert image_context.get_contexts({"image_url": json_str}) == [ |
| 158 | + { |
| 159 | + "type": "image_url", |
| 160 | + "image_url": json_str, |
| 161 | + } |
| 162 | + ] |
| 163 | + |
| 164 | + |
| 165 | +def test_image_context_get_contexts_invalid_json(): |
| 166 | + """Test get_contexts with invalid JSON string (should treat as single string).""" |
| 167 | + image_context = ImageContext(column_name="image_url", data_type=ModalityDataType.URL) |
| 168 | + invalid_json = "not a valid json string" |
| 169 | + assert image_context.get_contexts({"image_url": invalid_json}) == [ |
| 170 | + { |
| 171 | + "type": "image_url", |
| 172 | + "image_url": invalid_json, |
| 173 | + } |
| 174 | + ] |
| 175 | + |
| 176 | + |
| 177 | +def test_image_context_get_contexts_empty_list(): |
| 178 | + """Test get_contexts with an empty list.""" |
| 179 | + image_context = ImageContext(column_name="image_url", data_type=ModalityDataType.URL) |
| 180 | + assert image_context.get_contexts({"image_url": []}) == [] |
43 | 181 |
|
44 | 182 |
|
45 | 183 | def test_image_context_validate_image_format(): |
|
0 commit comments