API Reference: Utils
General Utilities
general
╔══════════════════════════════════════════════════════════════════════════════╗ ║ MHRQI - Multi-scale Hierarchical Representation of Quantum Images ║ ║ Utility Functions: Encoding, Reconstruction, Sibling Smoothing ║ ║ ║ ║ Author: Keno S. Jose ║ ║ License: Apache 2.0 ║ ╚══════════════════════════════════════════════════════════════════════════════╝
angle_map(img, bit_depth=8)
Map pixel intensities to quantum rotation angles via arcsin encoding.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img
|
Grayscale image as integer array. |
required | |
bit_depth
|
Bit depth of the image (default 8). |
8
|
Returns:
| Type | Description |
|---|---|
|
Array of angles in [0, π]. |
Source code in mhrqi/utils/general.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | |
compose_rc(hierarchical_coord_vector, d=2)
Convert a hierarchical coordinate vector to (row, col) pixel coordinates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hierarchical_coord_vector
|
Sequence of qudit values (qy0, qx0, qy1, qx1, ...). Length must be even. |
required | |
d
|
Qudit dimension. |
2
|
Returns:
| Type | Description |
|---|---|
|
Tuple (r, c). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If hierarchical_coord_vector length is odd or any digit is out of range. |
Source code in mhrqi/utils/general.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | |
compute_register(r, c, d, sk_prev)
Compute the qudit register values (qy, qx) for pixel (r, c) at a given scale.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
r
|
Row index. |
required | |
c
|
Column index. |
required | |
d
|
Qudit dimension. |
required | |
sk_prev
|
Subdivision size at the previous level. |
required |
Returns:
| Type | Description |
|---|---|
|
Tuple (qy, qx). |
Source code in mhrqi/utils/general.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | |
generate_hierarchical_coord_matrix(N, d=2)
Generate the hierarchical coordinate matrix for an image of size N x N.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
N
|
Image side length. |
required | |
d
|
Qudit dimension. |
2
|
Returns:
| Type | Description |
|---|---|
|
List of hierarchical coordinate vectors. |
Source code in mhrqi/utils/general.py
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | |
get_max_depth(N, d)
Compute the maximum hierarchy depth for image size N and qudit dimension d.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
N
|
Image side length. |
required | |
d
|
Qudit dimension. |
required |
Returns:
| Type | Description |
|---|---|
|
max_depth = floor(log_d(N)). |
Source code in mhrqi/utils/general.py
33 34 35 36 37 38 39 40 41 42 43 44 45 | |
get_subdiv_size(k, N, d)
Compute the subdivision size at hierarchy level k.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
k
|
Hierarchy level. |
required | |
N
|
Image side length. |
required | |
d
|
Qudit dimension. |
required |
Returns:
| Type | Description |
|---|---|
|
Side length of subregions at level k. |
Source code in mhrqi/utils/general.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 | |
mhrqi_bins_to_image(bins, hierarchical_coord_matrix, d, image_shape, bias_stats=None, original_img=None)
Reconstruct an image from measurement bins with optional confidence-weighted smoothing.
When bias_stats is provided, each pixel is blended with its 8-neighborhood weighted by its denoiser confidence. Neighbors with confidence below CONFIDENCE_THRESHOLD are used as context; high-confidence pixels are trusted as-is.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bins
|
Measurement bins dict mapping position tuples to intensity stats. |
required | |
hierarchical_coord_matrix
|
List of hierarchical coordinate vectors. |
required | |
d
|
Qudit dimension. |
required | |
image_shape
|
Output image shape as (H, W). |
required | |
bias_stats
|
Optional dict mapping position tuples to hit/miss counts. |
None
|
|
original_img
|
Optional pre-computed baseline image to use as source. |
None
|
Returns:
| Type | Description |
|---|---|
|
Reconstructed image as a float array of shape image_shape. |
Source code in mhrqi/utils/general.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 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 | |
Visualization
visualization
╔══════════════════════════════════════════════════════════════════════════════╗ ║ MHRQI - Multi-scale Hierarchical Representation of Quantum Images ║ ║ Plotting and Metrics: Visualization, Quality Assessment, Benchmarking ║ ║ ║ ║ Author: Keno S. Jose ║ ║ License: Apache 2.0 ║ ╚══════════════════════════════════════════════════════════════════════════════╝
ImagePlotter
Utilities for displaying and comparing images.
Source code in mhrqi/utils/visualization.py
904 905 906 907 908 909 910 | |
MetricsPlotter
Visualization for image quality metrics and comparison reports.
Source code in mhrqi/utils/visualization.py
756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 | |
print_summary_text(competitors, keys, title)
staticmethod
Print a formatted metric table to stdout.
Source code in mhrqi/utils/visualization.py
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 | |
save_summary_report(ref_img, competitors, metric_keys, title, filename_suffix, save_dir, include_original_in_table=False)
staticmethod
Generate and save a unified figure with images and a ranked metrics table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ref_img
|
Reference image (uint8) or None. |
required | |
competitors
|
List of dicts with 'name', 'metrics', 'image' keys. |
required | |
metric_keys
|
List of metric names to display. |
required | |
title
|
Figure title. |
required | |
filename_suffix
|
Output filename without extension. |
required | |
save_dir
|
Directory to save the figure. |
required | |
include_original_in_table
|
Whether to include 'Original' in the table. |
False
|
Source code in mhrqi/utils/visualization.py
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 | |
TrendPlotter
Line graphs for trend analysis.
Source code in mhrqi/utils/visualization.py
913 914 915 916 | |
auto_detect_rois(img)
Auto-detect signal and background ROIs for CNR calculation.
Signal ROI: centroid of the top-10% intensity region. Background ROI: lowest-variance block with center bias.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img
|
Input image. |
required |
Returns:
| Type | Description |
|---|---|
|
Tuple (signal_roi, bg_roi), each as (y, x, h, w). |
Source code in mhrqi/utils/visualization.py
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 | |
bins_to_image(bins, d, N, kind='p', eps=0.0, vmin=0.0, vmax=1.0)
Convert bins directly to a uint8 image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bins
|
Measurement bins dict. |
required | |
d
|
Qudit dimension. |
required | |
N
|
Image size. |
required | |
kind
|
Value type ("p" for p-hat probability, "hit", "miss"). |
'p'
|
|
eps
|
Smoothing epsilon for p-hat. |
0.0
|
|
vmin
|
Minimum value for scaling to [0, 255]. |
0.0
|
|
vmax
|
Maximum value for scaling to [0, 255]. |
1.0
|
Returns:
| Type | Description |
|---|---|
|
uint8 image array of shape (N, N). |
Source code in mhrqi/utils/visualization.py
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 | |
compute_cnr(img, signal_roi=None, bg_roi=None)
Compute Contrast-to-Noise Ratio (CNR).
CNR = |mean_signal - mean_bg| / std_bg. ROIs are auto-detected if not provided.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img
|
Input image. |
required | |
signal_roi
|
Optional (y, x, h, w) signal region. |
None
|
|
bg_roi
|
Optional (y, x, h, w) background region. |
None
|
Returns:
| Type | Description |
|---|---|
|
Tuple (cnr_value, signal_roi, bg_roi). Higher CNR is better. |
Source code in mhrqi/utils/visualization.py
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 | |
compute_enl(img, roi=None)
Compute Equivalent Number of Looks (ENL).
ENL = mean² / variance. Evaluated on a homogeneous ROI if provided.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img
|
Input image. |
required | |
roi
|
Optional (y, x, h, w) region of interest. |
None
|
Returns:
| Type | Description |
|---|---|
|
ENL value. Higher is better. Capped at 10000. |
Reference
Ulaby et al., 1986.
Source code in mhrqi/utils/visualization.py
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 | |
compute_epi(img_original, img_denoised)
Compute Edge Preservation Index (EPI).
EPI is the Pearson correlation of Sobel gradient magnitudes between the original and denoised images. Higher values indicate better edge preservation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img_original
|
Original image. |
required | |
img_denoised
|
Denoised image. |
required |
Returns:
| Type | Description |
|---|---|
|
EPI in [-1, 1]. Higher is better. |
Reference
Sattar et al., 1997.
Source code in mhrqi/utils/visualization.py
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 | |
compute_fsim(img_ref, img_test)
Compute FSIM (Feature Similarity Index) using piq.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img_ref
|
Reference image. |
required | |
img_test
|
Test image. |
required |
Returns:
| Type | Description |
|---|---|
|
FSIM score in [0, 1]. Higher is better. |
Source code in mhrqi/utils/visualization.py
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 | |
compute_mse(img_gt, img_test)
Compute mean squared error between two images.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img_gt
|
Ground truth image. |
required | |
img_test
|
Test image. |
required |
Returns:
| Type | Description |
|---|---|
|
MSE value. |
Source code in mhrqi/utils/visualization.py
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 | |
compute_niqe(img_input)
Compute NIQE (Natural Image Quality Evaluator) using scikit-video.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img_input
|
Input image. |
required |
Returns:
| Type | Description |
|---|---|
|
NIQE score. Lower is better. |
Source code in mhrqi/utils/visualization.py
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 | |
compute_omqdi(img_noisy, img_denoised)
Compute OMQDI (Objective Measure of Quality of Denoised Images).
DOI: 10.1016/j.bspc.2021.102962
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img_noisy
|
Noisy input image (single channel). |
required | |
img_denoised
|
Denoised output image (single channel). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Tuple |
(OMQDI, EPF, NSF)
|
|
Source code in mhrqi/utils/visualization.py
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 | |
compute_psnr(img_gt, img_test, data_range=255.0)
Compute Peak Signal-to-Noise Ratio (PSNR) in dB.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img_gt
|
Ground truth image. |
required | |
img_test
|
Test image. |
required | |
data_range
|
Dynamic range of the images. |
255.0
|
Returns:
| Type | Description |
|---|---|
|
PSNR in dB. Higher is better. |
Source code in mhrqi/utils/visualization.py
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 | |
compute_smpi(img_original, img_filtered)
Compute Speckle Mean Preservation Index (SMPI).
Lower values indicate better speckle suppression with mean preservation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img_original
|
Original noisy image. |
required | |
img_filtered
|
Filtered image. |
required |
Returns:
| Type | Description |
|---|---|
|
SMPI value. Lower is better. |
Source code in mhrqi/utils/visualization.py
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 | |
compute_ssi(img_noisy, img_filtered, roi)
Compute Speckle Suppression Index (SSI).
SSI = (std_filtered / mean_filtered) / (std_noisy / mean_noisy), evaluated on a homogeneous ROI. Lower is better.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img_noisy
|
Noisy input image. |
required | |
img_filtered
|
Filtered output image. |
required | |
roi
|
Region of interest as (y, x, h, w) or array index. |
required |
Returns:
| Type | Description |
|---|---|
|
SSI value. Lower is better. |
Source code in mhrqi/utils/visualization.py
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 | |
compute_ssim(img_gt, img_test, data_range=255.0)
Compute Structural Similarity Index (SSIM).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img_gt
|
Ground truth image. |
required | |
img_test
|
Test image. |
required | |
data_range
|
Dynamic range of the images. |
255.0
|
Returns:
| Type | Description |
|---|---|
|
SSIM score. Higher is better. |
Source code in mhrqi/utils/visualization.py
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 | |
get_run_dir(run_dir=None)
Get or create the current run output directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
run_dir
|
If provided, use this path directly. |
None
|
Returns:
| Type | Description |
|---|---|
|
Path to the run output directory. |
Source code in mhrqi/utils/visualization.py
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | |
grid_to_image_uint8(grid, vmin=None, vmax=None, flip_vertical=False)
Convert an N x N grid into a uint8 image.
NaN values are replaced with 0 before scaling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grid
|
2D numpy array. |
required | |
vmin
|
Minimum value for scaling. Inferred from data if None. |
None
|
|
vmax
|
Maximum value for scaling. Inferred from data if None. |
None
|
|
flip_vertical
|
If True, flip the image vertically. |
False
|
Returns:
| Type | Description |
|---|---|
|
uint8 image array of shape (N, N). |
Source code in mhrqi/utils/visualization.py
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 | |
plot_bias_map(bias_stats, original_img, N, d, run_dir=None)
Visualize the denoiser confidence map derived from the outcome ancilla.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bias_stats
|
Dict mapping position vectors to hit/miss stats. |
required | |
original_img
|
Original grayscale image in [0, 1]. |
required | |
N
|
Image size. |
required | |
d
|
Qudit dimension. |
required | |
run_dir
|
Output directory. |
None
|
Returns:
| Type | Description |
|---|---|
|
Confidence ratio map as a 2D numpy array, or None if no stats. |
Source code in mhrqi/utils/visualization.py
924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 | |
plot_mse_map(img_gt, img_test, title='Per-pixel squared error', run_dir=None)
Save a per-pixel squared error heatmap.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img_gt
|
Ground truth image. |
required | |
img_test
|
Test image. |
required | |
title
|
Plot title. |
'Per-pixel squared error'
|
|
run_dir
|
Output directory. |
None
|
Source code in mhrqi/utils/visualization.py
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 | |
plot_shots_vs_mse(shots, mse_values, title='Shots vs MSE', run_dir=None)
Plot and save a shots vs MSE trend graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shots
|
List of shot counts. |
required | |
mse_values
|
List of MSE values. |
required | |
title
|
Plot title. |
'Shots vs MSE'
|
|
run_dir
|
Output directory. |
None
|
Source code in mhrqi/utils/visualization.py
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 | |
reset_run_dir()
Reset the cached run directory.
Source code in mhrqi/utils/visualization.py
89 90 91 92 | |
save_settings_plot(settings_dict, run_dir=None, filename='settings.png')
Create a visual table of run settings and save it as a PNG.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
settings_dict
|
Dict of setting names to values. |
required | |
run_dir
|
Output directory. Uses get_run_dir() if None. |
None
|
|
filename
|
Output filename. |
'settings.png'
|
Source code in mhrqi/utils/visualization.py
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | |
show_image_comparison(orig_img, recon_img, titles=('Original', 'Reconstructed'), run_dir=None, img_name=None)
Plot two images side by side and save the comparison.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
orig_img
|
Original image (2D uint8 preferred). |
required | |
recon_img
|
Reconstructed image (2D uint8 preferred). |
required | |
titles
|
Tuple of display titles for the two images. |
('Original', 'Reconstructed')
|
|
run_dir
|
Output directory. Uses get_run_dir() if None. |
None
|
|
img_name
|
Base name for saved files. |
None
|
Source code in mhrqi/utils/visualization.py
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 245 | |