Visualization Utilities¶
The visualize module contains functions for creating various types of visualizations, such as image grids and stacks, to help you inspect and analyze your data.
image_grid(grid_list, shape=(3, 3), figsize=10, bgr2rgb=False, normalize=True, cmap=None, alpha=1.0, title=None, save=None)
¶
Displays a list of images in a grid format.
This function is ideal for visualizing a collection of images, such as a dataset sample or the output of a generative model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grid_list
|
Union[List[Union[Image, NDArray[Any]]], NDArray[Any]]
|
A list of images or a 4D numpy array (batch of images). |
required |
shape
|
Tuple[int, int]
|
The grid dimensions (rows, cols). Defaults to (3, 3). |
(3, 3)
|
figsize
|
int
|
The size of the figure. The figure will be square. Defaults to 10. |
10
|
bgr2rgb
|
bool
|
If |
False
|
normalize
|
bool
|
If |
True
|
cmap
|
Optional[Union[str, List[Optional[str]]]]
|
The colormap(s) to apply.
Defaults to |
None
|
alpha
|
Union[float, List[Optional[float]]]
|
The alpha (transparency) value(s). Defaults to 1.0. |
1.0
|
title
|
Optional[str]
|
A title for the entire figure. Defaults to |
None
|
save
|
Optional[str]
|
The file path to save the figure. Defaults to |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
Figure |
Figure
|
The matplotlib Figure object. |
Example
Display a grid of random grayscale images:
images = [np.random.rand(50, 50) for _ in range(9)]
oc.visualize.image_grid(images, shape=(3, 3), title="Grayscale Images")
Display a batch of RGB images from a numpy array:
Source code in opencrate/core/utils/visualize.py
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 | |
image_stack(stack_lists, titles=None, figsize=(8, 8), bgr2rgb=False, normalize=True, cmap=None, alpha=1.0, save=None)
¶
Displays images in a stacked grid format, where each list of images forms a column.
This function is useful for comparing corresponding images side-by-side, such as original images, heatmaps, and masked versions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stack_lists
|
List[List[Union[Image, NDArray[Any]]]]
|
A list where each inner list contains images for one column. All inner lists must have the same length. |
required |
titles
|
Optional[List[str]]
|
A list of titles for each column.
Defaults to |
None
|
figsize
|
Tuple[float, float]
|
The size of the figure. Defaults to (8, 8). |
(8, 8)
|
bgr2rgb
|
bool
|
If |
False
|
normalize
|
bool
|
If |
True
|
cmap
|
Optional[Union[str, List[Optional[str]]]]
|
The colormap(s) to apply.
Can be a single string or a list of strings for each image.
Defaults to |
None
|
alpha
|
Union[float, List[Optional[float]]]
|
The alpha (transparency) value(s). Can be a single float or a list of floats for each image. Defaults to 1.0. |
1.0
|
save
|
Optional[str]
|
The file path to save the figure. If |
None
|
Example
Display two columns of images with titles:
# Create dummy images
originals = [np.random.rand(50, 50) for _ in range(3)]
processed = [img * 0.5 for img in originals]
oc.visualize.image_stack(
[originals, processed],
titles=["Original", "Processed"],
figsize=(6, 9)
)
Display with mixed colormaps and transparency:
Source code in opencrate/core/utils/visualize.py
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 | |
labeled_images(image_batch, label_batch, shape, label_names=None, figsize=10, title=None, bgr2rgb=False, normalize=True, save=None, cmap=None, alpha=1.0, show=True)
¶
Displays a grid of images with their corresponding labels.
This function is perfect for visualizing datasets for classification tasks, showing both the image and its ground truth or predicted label.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image_batch
|
Union[List[Union[Image, NDArray[Any]]], NDArray[Any]]
|
A list of images or a 4D numpy array (batch of images). |
required |
label_batch
|
Union[List[str], NDArray[Any]]
|
A list of labels corresponding to the images. |
required |
shape
|
Tuple[int, int]
|
The grid dimensions (rows, cols). |
required |
label_names
|
Optional[List[str]]
|
A list of strings to map numeric
labels to human-readable names. Defaults to |
None
|
figsize
|
int
|
The size of the figure. The figure will be square. Defaults to 10. |
10
|
title
|
Optional[str]
|
A title for the entire figure. Defaults to |
None
|
bgr2rgb
|
bool
|
If |
False
|
normalize
|
bool
|
If |
True
|
save
|
Optional[str]
|
The file path to save the figure. Defaults to |
None
|
cmap
|
Optional[Union[str, List[Optional[str]]]]
|
The colormap(s) to apply.
Defaults to |
None
|
alpha
|
Union[float, List[Optional[float]]]
|
The alpha (transparency) value(s). Defaults to 1.0. |
1.0
|
show
|
bool
|
If |
True
|
Returns:
| Name | Type | Description |
|---|---|---|
Figure |
Figure
|
The matplotlib Figure object. |
Example
Display images with numeric labels:
images = [np.random.rand(28, 28) for _ in range(4)]
labels = [0, 1, 1, 0]
oc.visualize.labeled_images(images, labels, shape=(2, 2))
Display images with mapped label names:
Source code in opencrate/core/utils/visualize.py
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 | |