measure.py 37.7 KB
 cc215 committed Feb 07, 2020 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 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 115 116 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 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 245 246 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 350 351 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 import numpy as np def dice_sim_coef(result, reference): """ Dice similarity coefficient """ result = np.atleast_1d(result.astype(np.bool)) reference = np.atleast_1d(reference.astype(np.bool)) tp = np.count_nonzero(result & reference) fp = np.count_nonzero(result & ~reference) fn = np.count_nonzero(~result & reference) try: dc = 2. * tp / float(2*tp + fp + fn) except ZeroDivisionError: dc = 0.0 return dc # Copyright (C) 2013 Oskar Maier # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # # author Oskar Maier # version r0.1.1 # since 2014-03-13 # status Release # build-in modules # third-party modules import numpy from scipy.ndimage import _ni_support from scipy.ndimage.morphology import distance_transform_edt, binary_erosion, \ generate_binary_structure from scipy.ndimage.measurements import label, find_objects from scipy.stats import pearsonr # own modules # code def dc(result, reference): r""" Dice coefficient Computes the Dice coefficient (also known as Sorensen index) between the binary objects in two images. The metric is defined as .. math:: DC=\frac{2|A\cap B|}{|A|+|B|} , where :math:A is the first and :math:B the second set of samples (here: binary objects). Parameters ---------- result : array_like Input data containing objects. Can be any type but will be converted into binary: background where 0, object everywhere else. reference : array_like Input data containing objects. Can be any type but will be converted into binary: background where 0, object everywhere else. Returns ------- dc : float The Dice coefficient between the object(s) in result and the object(s) in reference. It ranges from 0 (no overlap) to 1 (perfect overlap). Notes ----- This is a real metric. The binary images can therefore be supplied in any order. """ result = numpy.atleast_1d(result.astype(numpy.bool)) reference = numpy.atleast_1d(reference.astype(numpy.bool)) intersection = numpy.count_nonzero(result & reference) size_i1 = numpy.count_nonzero(result) size_i2 = numpy.count_nonzero(reference) try: dc = 2. * intersection / float(size_i1 + size_i2) except ZeroDivisionError: dc = 0.0 return dc def jc(result, reference): """ Jaccard coefficient Computes the Jaccard coefficient between the binary objects in two images. Parameters ---------- result: array_like Input data containing objects. Can be any type but will be converted into binary: background where 0, object everywhere else. reference: array_like Input data containing objects. Can be any type but will be converted into binary: background where 0, object everywhere else. Returns ------- jc: float The Jaccard coefficient between the object(s) in result and the object(s) in reference. It ranges from 0 (no overlap) to 1 (perfect overlap). Notes ----- This is a real metric. The binary images can therefore be supplied in any order. """ result = numpy.atleast_1d(result.astype(numpy.bool)) reference = numpy.atleast_1d(reference.astype(numpy.bool)) intersection = numpy.count_nonzero(result & reference) union = numpy.count_nonzero(result | reference) jc = float(intersection) / float(union) return jc def precision(result, reference): """ Precison. Parameters ---------- result : array_like Input data containing objects. Can be any type but will be converted into binary: background where 0, object everywhere else. reference : array_like Input data containing objects. Can be any type but will be converted into binary: background where 0, object everywhere else. Returns ------- precision : float The precision between two binary datasets, here mostly binary objects in images, which is defined as the fraction of retrieved instances that are relevant. The precision is not symmetric. See also -------- :func:recall Notes ----- Not symmetric. The inverse of the precision is :func:recall. High precision means that an algorithm returned substantially more relevant results than irrelevant. References ---------- .. [1] http://en.wikipedia.org/wiki/Precision_and_recall .. [2] http://en.wikipedia.org/wiki/Confusion_matrix#Table_of_confusion """ result = numpy.atleast_1d(result.astype(numpy.bool)) reference = numpy.atleast_1d(reference.astype(numpy.bool)) tp = numpy.count_nonzero(result & reference) fp = numpy.count_nonzero(result & ~reference) try: precision = tp / float(tp + fp) except ZeroDivisionError: precision = 0.0 return precision def recall(result, reference): """ Recall. Parameters ---------- result : array_like Input data containing objects. Can be any type but will be converted into binary: background where 0, object everywhere else. reference : array_like Input data containing objects. Can be any type but will be converted into binary: background where 0, object everywhere else. Returns ------- recall : float The recall between two binary datasets, here mostly binary objects in images, which is defined as the fraction of relevant instances that are retrieved. The recall is not symmetric. See also -------- :func:precision Notes ----- Not symmetric. The inverse of the recall is :func:precision. High recall means that an algorithm returned most of the relevant results. References ---------- .. [1] http://en.wikipedia.org/wiki/Precision_and_recall .. [2] http://en.wikipedia.org/wiki/Confusion_matrix#Table_of_confusion """ result = numpy.atleast_1d(result.astype(numpy.bool)) reference = numpy.atleast_1d(reference.astype(numpy.bool)) tp = numpy.count_nonzero(result & reference) fn = numpy.count_nonzero(~result & reference) try: recall = tp / float(tp + fn) except ZeroDivisionError: recall = 0.0 return recall def sensitivity(result, reference): """ Sensitivity. Same as :func:recall, see there for a detailed description. See also -------- :func:specificity """ return recall(result, reference) def specificity(result, reference): """ Specificity. Parameters ---------- result : array_like Input data containing objects. Can be any type but will be converted into binary: background where 0, object everywhere else. reference : array_like Input data containing objects. Can be any type but will be converted into binary: background where 0, object everywhere else. Returns ------- specificity : float The specificity between two binary datasets, here mostly binary objects in images, which denotes the fraction of correctly returned negatives. The specificity is not symmetric. See also -------- :func:sensitivity Notes ----- Not symmetric. The completment of the specificity is :func:sensitivity. High recall means that an algorithm returned most of the irrelevant results. References ---------- .. [1] https://en.wikipedia.org/wiki/Sensitivity_and_specificity .. [2] http://en.wikipedia.org/wiki/Confusion_matrix#Table_of_confusion """ result = numpy.atleast_1d(result.astype(numpy.bool)) reference = numpy.atleast_1d(reference.astype(numpy.bool)) tn = numpy.count_nonzero(~result & ~reference) fp = numpy.count_nonzero(result & ~reference) try: specificity = tn / float(tn + fp) except ZeroDivisionError: specificity = 0.0 return specificity def true_negative_rate(result, reference): """ True negative rate. Same as :func:sensitivity, see there for a detailed description. See also -------- :func:true_positive_rate :func:positive_predictive_value """ return sensitivity(result, reference) def true_positive_rate(result, reference): """ True positive rate. Same as :func:recall, see there for a detailed description. See also -------- :func:positive_predictive_value :func:true_negative_rate """ return recall(result, reference) def positive_predictive_value(result, reference): """ Positive predictive value. Same as :func:precision, see there for a detailed description. See also -------- :func:true_positive_rate :func:true_negative_rate """ return precision(result, reference) def hd(result, reference, voxelspacing=None, connectivity=1): """ Hausdorff Distance. Computes the (symmetric) Hausdorff Distance (HD) between the binary objects in two images. It is defined as the maximum surface distance between the objects. Parameters ---------- result : array_like Input data containing objects. Can be any type but will be converted into binary: background where 0, object everywhere else. reference : array_like Input data containing objects. Can be any type but will be converted into binary: background where 0, object everywhere else. voxelspacing : float or sequence of floats, optional The voxelspacing in a distance unit i.e. spacing of elements along each dimension. If a sequence, must be of length equal to the input rank; if a single number, this is used for all axes. If not specified, a grid spacing of unity is implied. connectivity : int The neighbourhood/connectivity considered when determining the surface of the binary objects. This value is passed to scipy.ndimage.morphology.generate_binary_structure and should usually be :math:> 1. Note that the connectivity influences the result in the case of the Hausdorff distance. Returns ------- hd : float The symmetric Hausdorff Distance between the object(s) in result and the object(s) in reference. The distance unit is the same as for the spacing of elements along each dimension, which is usually given in mm. See also -------- :func:assd :func:asd Notes ----- This is a real metric. The binary images can therefore be supplied in any order. """ hd1 = __surface_distances(result, reference, voxelspacing, connectivity).max() hd2 = __surface_distances(reference, result, voxelspacing, connectivity).max() hd = max(hd1, hd2) return hd def hd_2D_stack(result,reference,pixelspacing=None, connectivity=1): ''' binary object hd :param result: N*H*W :param reference: N*H*W :return: averaged hausdorff distance ''' mean = 0 c = 0 zdim = result.shape[0] for i in range(zdim): if (np.sum(result[i,:,:]) > 0 and np.sum(reference[i,:,:]) > 0): hausdorff_distance=hd(result[i],reference[i],voxelspacing=pixelspacing,connectivity=connectivity) c = c + 1 mean = mean + hausdorff_distance else: if (np.sum(result[i,:,:]) == 0 and np.sum(reference[i,:,:])==0): c=c else: mean =mean +1000000000 c=c+1  cc215 committed Mar 15, 2021 409  # print (i)  cc215 committed Feb 07, 2020 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 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 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 554 555 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 589 590 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 629 630 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 683 684 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 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 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 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 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 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000  if c==0: return -1 return mean / c def assd(result, reference, voxelspacing=None, connectivity=1): """ Average symmetric surface distance. Computes the average symmetric surface distance (ASD) between the binary objects in two images. Parameters ---------- result : array_like Input data containing objects. Can be any type but will be converted into binary: background where 0, object everywhere else. reference : array_like Input data containing objects. Can be any type but will be converted into binary: background where 0, object everywhere else. voxelspacing : float or sequence of floats, optional The voxelspacing in a distance unit i.e. spacing of elements along each dimension. If a sequence, must be of length equal to the input rank; if a single number, this is used for all axes. If not specified, a grid spacing of unity is implied. connectivity : int The neighbourhood/connectivity considered when determining the surface of the binary objects. This value is passed to scipy.ndimage.morphology.generate_binary_structure and should usually be :math:> 1. The decision on the connectivity is important, as it can influence the results strongly. If in doubt, leave it as it is. Returns ------- assd : float The average symmetric surface distance between the object(s) in result and the object(s) in reference. The distance unit is the same as for the spacing of elements along each dimension, which is usually given in mm. See also -------- :func:asd :func:hd Notes ----- This is a real metric, obtained by calling and averaging >>> asd(result, reference) and >>> asd(reference, result) The binary images can therefore be supplied in any order. """ assd = numpy.mean( (asd(result, reference, voxelspacing, connectivity), asd(reference, result, voxelspacing, connectivity))) return assd def asd(result, reference, voxelspacing=None, connectivity=1): """ Average surface distance metric. Computes the average surface distance (ASD) between the binary objects in two images. Parameters ---------- result : array_like Input data containing objects. Can be any type but will be converted into binary: background where 0, object everywhere else. reference : array_like Input data containing objects. Can be any type but will be converted into binary: background where 0, object everywhere else. voxelspacing : float or sequence of floats, optional The voxelspacing in a distance unit i.e. spacing of elements along each dimension. If a sequence, must be of length equal to the input rank; if a single number, this is used for all axes. If not specified, a grid spacing of unity is implied. connectivity : int The neighbourhood/connectivity considered when determining the surface of the binary objects. This value is passed to scipy.ndimage.morphology.generate_binary_structure and should usually be :math:> 1. The decision on the connectivity is important, as it can influence the results strongly. If in doubt, leave it as it is. Returns ------- asd : float The average surface distance between the object(s) in result and the object(s) in reference. The distance unit is the same as for the spacing of elements along each dimension, which is usually given in mm. See also -------- :func:assd :func:hd Notes ----- This is not a real metric, as it is directed. See assd for a real metric of this. The method is implemented making use of distance images and simple binary morphology to achieve high computational speed. Examples -------- The connectivity determines what pixels/voxels are considered the surface of a binary object. Take the following binary image showing a cross >>> from scipy.ndimage.morphology import generate_binary_structure >>> cross = generate_binary_structure(2, 1) array([[0, 1, 0], [1, 1, 1], [0, 1, 0]]) With connectivity set to 1 a 4-neighbourhood is considered when determining the object surface, resulting in the surface .. code-block:: python array([[0, 1, 0], [1, 0, 1], [0, 1, 0]]) Changing connectivity to 2, a 8-neighbourhood is considered and we get: .. code-block:: python array([[0, 1, 0], [1, 1, 1], [0, 1, 0]]) , as a diagonal connection does no longer qualifies as valid object surface. This influences the results asd returns. Imagine we want to compute the surface distance of our cross to a cube-like object: 0.20000000000000001 due to the center of the cross being considered surface as well. """ if (np.sum(result) > 0 and np.sum(reference) > 0): sds = __surface_distances(result, reference, voxelspacing, connectivity) asd = sds.mean() else: asd=1e100 return asd def ravd(result, reference): """ Relative absolute volume difference. Compute the relative absolute volume difference between the (joined) binary objects in the two images. Parameters ---------- result : array_like Input data containing objects. Can be any type but will be converted into binary: background where 0, object everywhere else. reference : array_like Input data containing objects. Can be any type but will be converted into binary: background where 0, object everywhere else. Returns ------- ravd : float The relative absolute volume difference between the object(s) in result and the object(s) in reference. This is a percentage value in the range :math:[-1.0, +inf] for which a :math:0 denotes an ideal score. Raises ------ RuntimeError If the reference object is empty. See also -------- :func:dc :func:precision :func:recall Notes ----- This is not a real metric, as it is directed. Negative values denote a smaller and positive values a larger volume than the reference. This implementation does not check, whether the two supplied arrays are of the same size. Examples -------- Considering the following inputs 0.0 """ result = numpy.atleast_1d(result.astype(numpy.bool)) reference = numpy.atleast_1d(reference.astype(numpy.bool)) vol1 = numpy.count_nonzero(result) vol2 = numpy.count_nonzero(reference) if 0 == vol2: raise RuntimeError('The second supplied array does not contain any binary object.') return (vol1 - vol2) / float(vol2) def volumesimilarity(result, reference): """ volume similarity= \frac{2*(v1-v2)}{v1+v2} Parameters ---------- result : array_like Input data containing objects. Can be any type but will be converted into binary: background where 0, object everywhere else. reference : array_like Input data containing objects. Can be any type but will be converted into binary: background where 0, object everywhere else. Returns ------- ravd : float The relative absolute volume difference between the object(s) in result and the object(s) in reference. This is a percentage value in the range :math:[-1.0, +inf] for which a :math:0 denotes an ideal score. Raises ------ RuntimeError If the reference object is empty. See also -------- :func:dc :func:precision :func:recall Notes ----- This is not a real metric, as it is directed. Negative values denote a smaller and positive values a larger volume than the reference. This implementation does not check, whether the two supplied arrays are of the same size. Examples -------- Considering the following inputs 0.0 """ result = numpy.atleast_1d(result.astype(numpy.bool)) reference = numpy.atleast_1d(reference.astype(numpy.bool)) vol1 = numpy.count_nonzero(result) vol2 = numpy.count_nonzero(reference) if 0 == vol2: raise RuntimeError('The second supplied array does not contain any binary object.') return 2*(vol1 - vol2) / float(vol2+vol1) def volume_correlation(results, references): r""" Volume correlation. Computes the linear correlation in binary object volume between the contents of the successive binary images supplied. Measured through the Pearson product-moment correlation coefficient. Parameters ---------- results : sequence of array_like Ordered list of input data containing objects. Each array_like will be converted into binary: background where 0, object everywhere else. references : sequence of array_like Ordered list of input data containing objects. Each array_like will be converted into binary: background where 0, object everywhere else. The order must be the same as for results. Returns ------- r : float The correlation coefficient between -1 and 1. p : float The two-side p value. """ results = numpy.atleast_2d(numpy.array(results).astype(numpy.bool)) references = numpy.atleast_2d(numpy.array(references).astype(numpy.bool)) results_volumes = [numpy.count_nonzero(r) for r in results] references_volumes = [numpy.count_nonzero(r) for r in references] return pearsonr(results_volumes, references_volumes) # returns (Pearson' def volume_change_correlation(results, references): r""" Volume change correlation. Computes the linear correlation of change in binary object volume between the contents of the successive binary images supplied. Measured through the Pearson product-moment correlation coefficient. Parameters ---------- results : sequence of array_like Ordered list of input data containing objects. Each array_like will be converted into binary: background where 0, object everywhere else. references : sequence of array_like Ordered list of input data containing objects. Each array_like will be converted into binary: background where 0, object everywhere else. The order must be the same as for results. Returns ------- r : float The correlation coefficient between -1 and 1. p : float The two-side p value. """ results = numpy.atleast_2d(numpy.array(results).astype(numpy.bool)) references = numpy.atleast_2d(numpy.array(references).astype(numpy.bool)) results_volumes = numpy.asarray([numpy.count_nonzero(r) for r in results]) references_volumes = numpy.asarray([numpy.count_nonzero(r) for r in references]) results_volumes_changes = results_volumes[1:] - results_volumes[:-1] references_volumes_changes = references_volumes[1:] - references_volumes[:-1] return pearsonr(results_volumes_changes, references_volumes_changes) # returns (Pearson's correlation coefficient, 2-tailed p-value) def obj_assd(result, reference, voxelspacing=None, connectivity=1): """ Average symmetric surface distance. Computes the average symmetric surface distance (ASSD) between the binary objects in two images. Parameters ---------- result : array_like Input data containing objects. Can be any type but will be converted into binary: background where 0, object everywhere else. reference : array_like Input data containing objects. Can be any type but will be converted into binary: background where 0, object everywhere else. voxelspacing : float or sequence of floats, optional The voxelspacing in a distance unit i.e. spacing of elements along each dimension. If a sequence, must be of length equal to the input rank; if a single number, this is used for all axes. If not specified, a grid spacing of unity is implied. connectivity : int The neighbourhood/connectivity considered when determining what accounts for a distinct binary object as well as when determining the surface of the binary objects. This value is passed to scipy.ndimage.morphology.generate_binary_structure and should usually be :math:> 1. The decision on the connectivity is important, as it can influence the results strongly. If in doubt, leave it as it is. Returns ------- assd : float The average symmetric surface distance between all mutually existing distinct binary object(s) in result and reference. The distance unit is the same as for the spacing of elements along each dimension, which is usually given in mm. See also -------- :func:obj_asd Notes ----- This is a real metric, obtained by calling and averaging The binary images can therefore be supplied in any order. """ assd = numpy.mean((obj_asd(result, reference, voxelspacing, connectivity), obj_asd(reference, result, voxelspacing, connectivity))) return assd def obj_asd(result, reference, voxelspacing=None, connectivity=1): """ Average surface distance between objects. First correspondences between distinct binary objects in reference and result are established. Then the average surface distance is only computed between corresponding objects. Correspondence is defined as unique and at least one voxel overlap. Parameters ---------- result : array_like Input data containing objects. Can be any type but will be converted into binary: background where 0, object everywhere else. reference : array_like Input data containing objects. Can be any type but will be converted into binary: background where 0, object everywhere else. voxelspacing : float or sequence of floats, optional The voxelspacing in a distance unit i.e. spacing of elements along each dimension. If a sequence, must be of length equal to the input rank; if a single number, this is used for all axes. If not specified, a grid spacing of unity is implied. connectivity : int The neighbourhood/connectivity considered when determining what accounts for a distinct binary object as well as when determining the surface of the binary objects. This value is passed to scipy.ndimage.morphology.generate_binary_structure and should usually be :math:> 1. The decision on the connectivity is important, as it can influence the results strongly. If in doubt, leave it as it is. Returns ------- asd : float The average surface distance between all mutually existing distinct binary object(s) in result and reference. The distance unit is the same as for the spacing of elements along each dimension, which is usually given in mm. See also -------- :func:obj_assd :func:obj_tpr :func:obj_fpr Notes ----- This is not a real metric, as it is directed. See obj_assd for a real metric of this. For the understanding of this metric, both the notions of connectedness and surface distance are essential. Please see :func:obj_tpr and :func:obj_fpr for more information on the first and :func:asd on the second. Examples -------- 1.742955328 Note that the connectivity also influence the notion of what is considered an object surface voxels. """ sds = list() labelmap1, labelmap2, _a, _b, mapping = __distinct_binary_object_correspondences(result, reference, connectivity) slicers1 = find_objects(labelmap1) slicers2 = find_objects(labelmap2) for lid2, lid1 in mapping.iteritems(): window = __combine_windows(slicers1[lid1 - 1], slicers2[lid2 - 1]) object1 = labelmap1[window] == lid1 object2 = labelmap2[window] == lid2 sds.extend(__surface_distances(object1, object2, voxelspacing, connectivity)) asd = numpy.mean(sds) return asd def obj_fpr(result, reference, connectivity=1): """ The false positive rate of distinct binary object detection. The false positive rates gives a percentage measure of how many distinct binary objects in the second array do not exists in the first array. A partial overlap (of minimum one voxel) is here considered sufficient. In cases where two distinct binary object in the second array overlap with a single distinct object in the first array, only one is considered to have been detected successfully and the other is added to the count of false positives. Parameters ---------- result : array_like Input data containing objects. Can be any type but will be converted into binary: background where 0, object everywhere else. reference : array_like Input data containing objects. Can be any type but will be converted into binary: background where 0, object everywhere else. connectivity : int The neighbourhood/connectivity considered when determining what accounts for a distinct binary object. This value is passed to scipy.ndimage.morphology.generate_binary_structure and should usually be :math:> 1. The decision on the connectivity is important, as it can influence the results strongly. If in doubt, leave it as it is. Returns ------- tpr : float A percentage measure of how many distinct binary objects in results have no corresponding binary object in reference. It has the range :math:[0, 1], where a :math:0 denotes an ideal score. Raises ------ RuntimeError If the second array is empty. See also -------- :func:obj_tpr Notes ----- This is not a real metric, as it is directed. Whatever array is considered as reference should be passed second. A perfect score of :math:0 tells that there are no distinct binary objects in the second array that do not exists also in the reference array, but does not reveal anything about objects in the reference array also existing in the second array (use :func:obj_tpr for this). Examples """ _, _, _, n_obj_reference, mapping = __distinct_binary_object_correspondences(reference, result, connectivity) return (n_obj_reference - len(mapping)) / float(n_obj_reference) def obj_tpr(result, reference, connectivity=1): """ The true positive rate of distinct binary object detection. The true positive rates gives a percentage measure of how many distinct binary objects in the first array also exists in the second array. A partial overlap (of minimum one voxel) is here considered sufficient. In cases where two distinct binary object in the first array overlaps with a single distinct object in the second array, only one is considered to have been detected successfully. Parameters ---------- result : array_like Input data containing objects. Can be any type but will be converted into binary: background where 0, object everywhere else. reference : array_like Input data containing objects. Can be any type but will be converted into binary: background where 0, object everywhere else. connectivity : int The neighbourhood/connectivity considered when determining what accounts for a distinct binary object. This value is passed to scipy.ndimage.morphology.generate_binary_structure and should usually be :math:> 1. The decision on the connectivity is important, as it can influence the results strongly. If in doubt, leave it as it is. Returns ------- tpr : float A percentage measure of how many distinct binary objects in result also exists in reference. It has the range :math:[0, 1], where a :math:1 denotes an ideal score. Raises ------ RuntimeError If the reference object is empty. See also -------- :func:obj_fpr Notes ----- This is not a real metric, as it is directed. Whatever array is considered as reference should be passed second. A perfect score of :math:1 tells that all distinct binary objects in the reference array also exist in the result array, but does not reveal anything about additional binary objects in the result array (use :func:obj_fpr for this). Examples 1.0 """ _, _, n_obj_result, _, mapping = __distinct_binary_object_correspondences(reference, result, connectivity) return len(mapping) / float(n_obj_result) def __distinct_binary_object_correspondences(reference, result, connectivity=1): """ Determines all distinct (where connectivity is defined by the connectivity parameter passed to scipy's generate_binary_structure) binary objects in both of the input parameters and returns a 1to1 mapping from the labelled objects in reference to the corresponding (whereas a one-voxel overlap suffices for correspondence) objects in result. All stems from the problem, that the relationship is non-surjective many-to-many.