Show code cell content
import matplotlib.pyplot as plt
import numpy as np
import SimpleITK as sitk
from armscan_env.clustering import (
TissueClusters,
TissueLabel,
find_DBSCAN_clusters,
)
from armscan_env.config import get_config
from armscan_env.envs.rewards import anatomy_based_rwd
from armscan_env.util.visualizations import show_clusters
config = get_config()
DBSCAN Clustering Search#
volume = sitk.ReadImage(config.get_single_labelmap_path(1))
volume_img = sitk.GetArrayFromImage(volume)
print(f"{volume_img.shape=}")
size = np.array(volume.GetSize()) * np.array(volume.GetSpacing())
print(f"{size=} mm")
transversal_extent = (0, size[0], 0, size[2])
longitudinal_extent = (0, size[1], 0, size[2])
frontal_extent = (0, size[0], size[1], 0)
volume_img.shape=(61, 864, 606)
size=array([150.0486095 , 213.93069077, 61. ]) mm
tissues = {
"bones": 1,
"tendons": 2,
"ulnar": 3,
}
DBSCAN might be a better clustering technique that offers more flexibility. It works by defining clusters as continuous regions of high density. It groups together points that are closely packed together (points with many nearby neighbors), marking as outliers points that lie alone in low-density regions (whose nearest neighbors are too far away). The DBSCAN algorithm has two parameters: min_samples
and eps
. The min_samples
parameter specifies the minimum number of points that a cluster must have in order to be considered a cluster. The eps
parameter specifies the maximum distance between two samples for one to be considered as in the neighborhood of the other.
clusters_680 = TissueClusters(
bones=find_DBSCAN_clusters(
TissueLabel.BONES,
volume_img[:, 680, :].T,
eps=4.1,
min_samples=46,
),
tendons=find_DBSCAN_clusters(
TissueLabel.TENDONS,
volume_img[:, 680, :].T,
eps=3.0,
min_samples=18,
),
ulnar=find_DBSCAN_clusters(
TissueLabel.ULNAR,
volume_img[:, 680, :].T,
eps=2.0,
min_samples=10,
),
)
fig = show_clusters(clusters_680, volume_img[:, 680, :].T, extent=transversal_extent)
fig.set_xlabel("X [mm]")
fig.set_ylabel("Z [mm]")
plt.show()
In general, this algorithm offers a better anatomical description, since it allows to reason about the average dimension of the clusters for each kind of tissue, removing possible outliers given by segmentation errors.
You can play around with the parameter of DBSCAN to find the best tuning, or you can use the constructor TissueClusters.from_labelmap_slice
which iterates through the tissues to find clusters with predetermined parameters eps
and min_samples
.
sweep_loss = []
clusters_list = []
for i in range(volume_img.shape[1]):
clusters = TissueClusters.from_labelmap_slice(volume_img[:, i, :].T)
loss = anatomy_based_rwd(clusters, n_landmarks=(7, 3, 1))
clusters_list.append(clusters)
print(f"Loss for slice {i}: {loss}")
sweep_loss.append(loss)
Show code cell output
Loss for slice 0: -1.0
Loss for slice 1: -1.0
Loss for slice 2: -1.0
Loss for slice 3: -1.0
Loss for slice 4: -1.0
Loss for slice 5: -1.0
Loss for slice 6: -1.0
Loss for slice 7: -1.0
Loss for slice 8: -1.0
Loss for slice 9: -1.0
Loss for slice 10: -1.0
Loss for slice 11: -1.0
Loss for slice 12: -1.0
Loss for slice 13: -1.0
Loss for slice 14: -1.0
Loss for slice 15: -1.0
Loss for slice 16: -1.0
Loss for slice 17: -1.0
Loss for slice 18: -1.0
Loss for slice 19: -1.0
Loss for slice 20: -1.0
Loss for slice 21: -1.0
Loss for slice 22: -1.0
Loss for slice 23: -1.0
Loss for slice 24: -1.0
Loss for slice 25: -1.0
Loss for slice 26: -1.0
Loss for slice 27: -1.0
Loss for slice 28: -1.0
Loss for slice 29: -1.0
Loss for slice 30: -1.0
Loss for slice 31: -1.0
Loss for slice 32: -1.0
Loss for slice 33: -1.0
Loss for slice 34: -1.0
Loss for slice 35: -1.0
Loss for slice 36: -1.0
Loss for slice 37: -1.0
Loss for slice 38: -1.0
Loss for slice 39: -1.0
Loss for slice 40: -1.0
Loss for slice 41: -1.0
Loss for slice 42: -1.0
Loss for slice 43: -1.0
Loss for slice 44: -1.0
Loss for slice 45: -1.0
Loss for slice 46: -1.0
Loss for slice 47: -1.0
Loss for slice 48: -1.0
Loss for slice 49: -1.0
Loss for slice 50: -1.0
Loss for slice 51: -1.0
Loss for slice 52: -1.0
Loss for slice 53: -1.0
Loss for slice 54: -1.0
Loss for slice 55: -1.0
Loss for slice 56: -1.0
Loss for slice 57: -1.0
Loss for slice 58: -1.0
Loss for slice 59: -1.0
Loss for slice 60: -1.0
Loss for slice 61: -1.0
Loss for slice 62: -1.0
Loss for slice 63: -1.0
Loss for slice 64: -1.0
Loss for slice 65: -1.0
Loss for slice 66: -1.0
Loss for slice 67: -1.0
Loss for slice 68: -1.0
Loss for slice 69: -1.0
Loss for slice 70: -1.0
Loss for slice 71: -1.0
Loss for slice 72: -1.0
Loss for slice 73: -1.0
Loss for slice 74: -1.0
Loss for slice 75: -1.0
Loss for slice 76: -1.0
Loss for slice 77: -1.0
Loss for slice 78: -1.0
Loss for slice 79: -1.0
Loss for slice 80: -1.0
Loss for slice 81: -1.0
Loss for slice 82: -1.0
Loss for slice 83: -1.0
Loss for slice 84: -1.0
Loss for slice 85: -1.0
Loss for slice 86: -1.0
Loss for slice 87: -1.0
Loss for slice 88: -1.0
Loss for slice 89: -1.0
Loss for slice 90: -1.0
Loss for slice 91: -1.0
Loss for slice 92: -1.0
Loss for slice 93: -1.0
Loss for slice 94: -1.0
Loss for slice 95: -1.0
Loss for slice 96: -1.0
Loss for slice 97: -1.0
Loss for slice 98: -1.0
Loss for slice 99: -1.0
Loss for slice 100: -1.0
Loss for slice 101: -1.0
Loss for slice 102: -1.0
Loss for slice 103: -1.0
Loss for slice 104: -1.0
Loss for slice 105: -1.0
Loss for slice 106: -1.0
Loss for slice 107: -1.0
Loss for slice 108: -1.0
Loss for slice 109: -1.0
Loss for slice 110: -1.0
Loss for slice 111: -1.0
Loss for slice 112: -1.0
Loss for slice 113: -1.0
Loss for slice 114: -1.0
Loss for slice 115: -1.0
Loss for slice 116: -1.0
Loss for slice 117: -1.0
Loss for slice 118: -1.0
Loss for slice 119: -1.0
Loss for slice 120: -1.0
Loss for slice 121: -1.0
Loss for slice 122: -1.0
Loss for slice 123: -1.0
Loss for slice 124: -1.0
Loss for slice 125: -1.0
Loss for slice 126: -1.0
Loss for slice 127: -1.0
Loss for slice 128: -1.0
Loss for slice 129: -1.0
Loss for slice 130: -1.0
Loss for slice 131: -1.0
Loss for slice 132: -1.0
Loss for slice 133: -1.0
Loss for slice 134: -1.0
Loss for slice 135: -1.0
Loss for slice 136: -1.0
Loss for slice 137: -1.0
Loss for slice 138: -1.0
Loss for slice 139: -1.0
Loss for slice 140: -1.0
Loss for slice 141: -1.0
Loss for slice 142: -1.0
Loss for slice 143: -1.0
Loss for slice 144: -1.0
Loss for slice 145: -1.0
Loss for slice 146: -1.0
Loss for slice 147: -1.0
Loss for slice 148: -1.0
Loss for slice 149: -1.0
Loss for slice 150: -1.0
Loss for slice 151: -1.0
Loss for slice 152: -1.0
Loss for slice 153: -1.0
Loss for slice 154: -1.0
Loss for slice 155: -1.0
Loss for slice 156: -1.0
Loss for slice 157: -1.0
Loss for slice 158: -1.0
Loss for slice 159: -1.0
Loss for slice 160: -1.0
Loss for slice 161: -1.0
Loss for slice 162: -1.0
Loss for slice 163: -1.0
Loss for slice 164: -1.0
Loss for slice 165: -1.0
Loss for slice 166: -1.0
Loss for slice 167: -1.0
Loss for slice 168: -1.0
Loss for slice 169: -1.0
Loss for slice 170: -1.0
Loss for slice 171: -1.0
Loss for slice 172: -1.0
Loss for slice 173: -1.0
Loss for slice 174: -1.0
Loss for slice 175: -1.0
Loss for slice 176: -1.0
Loss for slice 177: -1.0
Loss for slice 178: -1.0
Loss for slice 179: -1.0
Loss for slice 180: -1.0
Loss for slice 181: -1.0
Loss for slice 182: -1.0
Loss for slice 183: -1.0
Loss for slice 184: -1.0
Loss for slice 185: -1.0
Loss for slice 186: -1.0
Loss for slice 187: -1.0
Loss for slice 188: -1.0
Loss for slice 189: -1.0
Loss for slice 190: -1.0
Loss for slice 191: -1.0
Loss for slice 192: -1.0
Loss for slice 193: -1.0
Loss for slice 194: -1.0
Loss for slice 195: -1.0
Loss for slice 196: -1.0
Loss for slice 197: -1.0
Loss for slice 198: -1.0
Loss for slice 199: -1.0
Loss for slice 200: -1.0
Loss for slice 201: -1.0
Loss for slice 202: -1.0
Loss for slice 203: -1.0
Loss for slice 204: -1.0
Loss for slice 205: -1.0
Loss for slice 206: -1.0
Loss for slice 207: -1.0
Loss for slice 208: -1.0
Loss for slice 209: -1.0
Loss for slice 210: -1.0
Loss for slice 211: -1.0
Loss for slice 212: -1.0
Loss for slice 213: -1.0
Loss for slice 214: -1.0
Loss for slice 215: -1.0
Loss for slice 216: -1.0
Loss for slice 217: -1.0
Loss for slice 218: -1.0
Loss for slice 219: -1.0
Loss for slice 220: -1.0
Loss for slice 221: -1.0
Loss for slice 222: -1.0
Loss for slice 223: -0.8585858585858586
Loss for slice 224: -0.8585858585858586
Loss for slice 225: -0.8585858585858586
Loss for slice 226: -0.8585858585858586
Loss for slice 227: -0.8585858585858586
Loss for slice 228: -0.8585858585858586
Loss for slice 229: -0.8585858585858586
Loss for slice 230: -0.8585858585858586
Loss for slice 231: -0.8585858585858586
Loss for slice 232: -0.8585858585858586
Loss for slice 233: -0.8585858585858586
Loss for slice 234: -0.8585858585858586
Loss for slice 235: -0.8585858585858586
Loss for slice 236: -0.8585858585858586
Loss for slice 237: -0.8585858585858586
Loss for slice 238: -0.8585858585858586
Loss for slice 239: -0.8585858585858586
Loss for slice 240: -0.8585858585858586
Loss for slice 241: -0.8585858585858586
Loss for slice 242: -0.8585858585858586
Loss for slice 243: -1.0
Loss for slice 244: -1.0
Loss for slice 245: -0.8585858585858586
Loss for slice 246: -0.8585858585858586
Loss for slice 247: -0.8585858585858586
Loss for slice 248: -0.8585858585858586
Loss for slice 249: -0.8282828282828283
Loss for slice 250: -0.8282828282828283
Loss for slice 251: -0.8282828282828283
Loss for slice 252: -0.797979797979798
Loss for slice 253: -0.8282828282828283
Loss for slice 254: -0.8282828282828283
Loss for slice 255: -0.8282828282828283
Loss for slice 256: -0.8282828282828283
Loss for slice 257: -0.8282828282828283
Loss for slice 258: -0.8282828282828283
Loss for slice 259: -0.8282828282828283
Loss for slice 260: -0.8282828282828283
Loss for slice 261: -0.8282828282828283
Loss for slice 262: -0.8282828282828283
Loss for slice 263: -0.8282828282828283
Loss for slice 264: -0.8585858585858586
Loss for slice 265: -0.8585858585858586
Loss for slice 266: -0.8585858585858586
Loss for slice 267: -0.8585858585858586
Loss for slice 268: -0.8585858585858586
Loss for slice 269: -0.8585858585858586
Loss for slice 270: -0.8585858585858586
Loss for slice 271: -0.8585858585858586
Loss for slice 272: -0.8585858585858586
Loss for slice 273: -0.797979797979798
Loss for slice 274: -0.8282828282828283
Loss for slice 275: -0.8282828282828283
Loss for slice 276: -0.8282828282828283
Loss for slice 277: -0.8282828282828283
Loss for slice 278: -0.8282828282828283
Loss for slice 279: -0.8282828282828283
Loss for slice 280: -0.8282828282828283
Loss for slice 281: -0.8585858585858586
Loss for slice 282: -0.8585858585858586
Loss for slice 283: -0.8585858585858586
Loss for slice 284: -0.8585858585858586
Loss for slice 285: -0.8585858585858586
Loss for slice 286: -0.8585858585858586
Loss for slice 287: -0.8585858585858586
Loss for slice 288: -0.8585858585858586
Loss for slice 289: -0.8585858585858586
Loss for slice 290: -1.0
Loss for slice 291: -1.0
Loss for slice 292: -1.0
Loss for slice 293: -1.0
Loss for slice 294: -1.0
Loss for slice 295: -1.0
Loss for slice 296: -1.0
Loss for slice 297: -1.0
Loss for slice 298: -1.0
Loss for slice 299: -1.0
Loss for slice 300: -1.0
Loss for slice 301: -1.0
Loss for slice 302: -1.0
Loss for slice 303: -1.0
Loss for slice 304: -1.0
Loss for slice 305: -1.0
Loss for slice 306: -1.0
Loss for slice 307: -1.0
Loss for slice 308: -1.0
Loss for slice 309: -1.0
Loss for slice 310: -1.0
Loss for slice 311: -1.0
Loss for slice 312: -1.0
Loss for slice 313: -0.8585858585858586
Loss for slice 314: -0.8585858585858586
Loss for slice 315: -0.8585858585858586
Loss for slice 316: -0.8585858585858586
Loss for slice 317: -0.8585858585858586
Loss for slice 318: -0.8585858585858586
Loss for slice 319: -0.8585858585858586
Loss for slice 320: -0.8585858585858586
Loss for slice 321: -0.8585858585858586
Loss for slice 322: -0.8585858585858586
Loss for slice 323: -1.0
Loss for slice 324: -1.0
Loss for slice 325: -1.0
Loss for slice 326: -1.0
Loss for slice 327: -0.8585858585858586
Loss for slice 328: -0.8585858585858586
Loss for slice 329: -0.8585858585858586
Loss for slice 330: -1.0
Loss for slice 331: -1.0
Loss for slice 332: -1.0
Loss for slice 333: -0.8585858585858586
Loss for slice 334: -0.8585858585858586
Loss for slice 335: -0.8585858585858586
Loss for slice 336: -0.8585858585858586
Loss for slice 337: -0.8585858585858586
Loss for slice 338: -0.8585858585858586
Loss for slice 339: -0.8585858585858586
Loss for slice 340: -0.8585858585858586
Loss for slice 341: -0.8585858585858586
Loss for slice 342: -0.8585858585858586
Loss for slice 343: -0.8585858585858586
Loss for slice 344: -0.8585858585858586
Loss for slice 345: -0.8585858585858586
Loss for slice 346: -0.8585858585858586
Loss for slice 347: -0.8585858585858586
Loss for slice 348: -1.0
Loss for slice 349: -1.0
Loss for slice 350: -1.0
Loss for slice 351: -1.0
Loss for slice 352: -1.0
Loss for slice 353: -1.0
Loss for slice 354: -1.0
Loss for slice 355: -1.0
Loss for slice 356: -1.0
Loss for slice 357: -1.0
Loss for slice 358: -1.0
Loss for slice 359: -1.0
Loss for slice 360: -1.0
Loss for slice 361: -1.0
Loss for slice 362: -1.0
Loss for slice 363: -1.0
Loss for slice 364: -1.0
Loss for slice 365: -1.0
Loss for slice 366: -1.0
Loss for slice 367: -1.0
Loss for slice 368: -1.0
Loss for slice 369: -1.0
Loss for slice 370: -1.0
Loss for slice 371: -1.0
Loss for slice 372: -1.0
Loss for slice 373: -1.0
Loss for slice 374: -1.0
Loss for slice 375: -0.8585858585858586
Loss for slice 376: -0.8585858585858586
Loss for slice 377: -0.8585858585858586
Loss for slice 378: -0.8585858585858586
Loss for slice 379: -0.8585858585858586
Loss for slice 380: -0.8585858585858586
Loss for slice 381: -0.8585858585858586
Loss for slice 382: -0.8585858585858586
Loss for slice 383: -0.8585858585858586
Loss for slice 384: -0.8585858585858586
Loss for slice 385: -0.8585858585858586
Loss for slice 386: -0.8585858585858586
Loss for slice 387: -0.8585858585858586
Loss for slice 388: -0.8585858585858586
Loss for slice 389: -0.8585858585858586
Loss for slice 390: -0.8282828282828283
Loss for slice 391: -0.8282828282828283
Loss for slice 392: -0.8282828282828283
Loss for slice 393: -0.8282828282828283
Loss for slice 394: -0.8282828282828283
Loss for slice 395: -0.8282828282828283
Loss for slice 396: -0.797979797979798
Loss for slice 397: -0.797979797979798
Loss for slice 398: -0.797979797979798
Loss for slice 399: -0.797979797979798
Loss for slice 400: -0.797979797979798
Loss for slice 401: -0.797979797979798
Loss for slice 402: -0.797979797979798
Loss for slice 403: -0.797979797979798
Loss for slice 404: -0.797979797979798
Loss for slice 405: -0.797979797979798
Loss for slice 406: -0.797979797979798
Loss for slice 407: -0.797979797979798
Loss for slice 408: -0.797979797979798
Loss for slice 409: -0.797979797979798
Loss for slice 410: -0.797979797979798
Loss for slice 411: -0.797979797979798
Loss for slice 412: -0.797979797979798
Loss for slice 413: -0.7676767676767675
Loss for slice 414: -0.797979797979798
Loss for slice 415: -0.7676767676767675
Loss for slice 416: -0.7676767676767675
Loss for slice 417: -0.7676767676767675
Loss for slice 418: -0.7676767676767675
Loss for slice 419: -0.7373737373737372
Loss for slice 420: -0.7373737373737372
Loss for slice 421: -0.7676767676767675
Loss for slice 422: -0.797979797979798
Loss for slice 423: -0.797979797979798
Loss for slice 424: -0.797979797979798
Loss for slice 425: -0.797979797979798
Loss for slice 426: -0.797979797979798
Loss for slice 427: -0.7676767676767675
Loss for slice 428: -0.7676767676767675
Loss for slice 429: -0.7676767676767675
Loss for slice 430: -0.7373737373737372
Loss for slice 431: -0.7676767676767675
Loss for slice 432: -0.7676767676767675
Loss for slice 433: -0.7676767676767675
Loss for slice 434: -0.7676767676767675
Loss for slice 435: -0.7676767676767675
Loss for slice 436: -0.7676767676767675
Loss for slice 437: -0.7676767676767675
Loss for slice 438: -0.7676767676767675
Loss for slice 439: -0.7676767676767675
Loss for slice 440: -0.7676767676767675
Loss for slice 441: -0.7676767676767675
Loss for slice 442: -0.7676767676767675
Loss for slice 443: -0.7676767676767675
Loss for slice 444: -0.7676767676767675
Loss for slice 445: -0.7373737373737372
Loss for slice 446: -0.7676767676767675
Loss for slice 447: -0.7676767676767675
Loss for slice 448: -0.7373737373737372
Loss for slice 449: -0.7373737373737372
Loss for slice 450: -0.7373737373737372
Loss for slice 451: -0.7373737373737372
Loss for slice 452: -0.7373737373737372
Loss for slice 453: -0.7373737373737372
Loss for slice 454: -0.7373737373737372
Loss for slice 455: -0.7373737373737372
Loss for slice 456: -0.7373737373737372
Loss for slice 457: -0.7373737373737372
Loss for slice 458: -0.7373737373737372
Loss for slice 459: -0.7373737373737372
Loss for slice 460: -0.7373737373737372
Loss for slice 461: -0.7373737373737372
Loss for slice 462: -0.7373737373737372
Loss for slice 463: -0.7373737373737372
Loss for slice 464: -0.7676767676767675
Loss for slice 465: -0.7676767676767675
Loss for slice 466: -0.7373737373737372
Loss for slice 467: -0.7373737373737372
Loss for slice 468: -0.7373737373737372
Loss for slice 469: -0.7373737373737372
Loss for slice 470: -0.7373737373737372
Loss for slice 471: -0.7373737373737372
Loss for slice 472: -0.7373737373737372
Loss for slice 473: -0.7373737373737372
Loss for slice 474: -0.7373737373737372
Loss for slice 475: -0.7373737373737372
Loss for slice 476: -0.7373737373737372
Loss for slice 477: -0.7676767676767675
Loss for slice 478: -0.7676767676767675
Loss for slice 479: -0.7676767676767675
Loss for slice 480: -0.7676767676767675
Loss for slice 481: -0.7676767676767675
Loss for slice 482: -0.7676767676767675
Loss for slice 483: -0.7676767676767675
Loss for slice 484: -0.7676767676767675
Loss for slice 485: -0.7676767676767675
Loss for slice 486: -0.7676767676767675
Loss for slice 487: -0.7676767676767675
Loss for slice 488: -0.7676767676767675
Loss for slice 489: -0.7676767676767675
Loss for slice 490: -0.797979797979798
Loss for slice 491: -0.797979797979798
Loss for slice 492: -0.6868686868686869
Loss for slice 493: -0.6868686868686869
Loss for slice 494: -0.6868686868686869
Loss for slice 495: -0.6868686868686869
Loss for slice 496: -0.6868686868686869
Loss for slice 497: -0.6868686868686869
Loss for slice 498: -0.6868686868686869
Loss for slice 499: -0.8585858585858586
Loss for slice 500: -0.8585858585858586
Loss for slice 501: -0.8585858585858586
Loss for slice 502: -0.8585858585858586
Loss for slice 503: -0.8585858585858586
Loss for slice 504: -0.8585858585858586
Loss for slice 505: -0.8585858585858586
Loss for slice 506: -0.8585858585858586
Loss for slice 507: -0.8585858585858586
Loss for slice 508: -0.8282828282828283
Loss for slice 509: -0.8282828282828283
Loss for slice 510: -0.8282828282828283
Loss for slice 511: -0.8282828282828283
Loss for slice 512: -0.8282828282828283
Loss for slice 513: -0.8282828282828283
Loss for slice 514: -0.8282828282828283
Loss for slice 515: -0.8585858585858586
Loss for slice 516: -0.8282828282828283
Loss for slice 517: -0.8282828282828283
Loss for slice 518: -0.8282828282828283
Loss for slice 519: -0.8282828282828283
Loss for slice 520: -0.8282828282828283
Loss for slice 521: -0.8282828282828283
Loss for slice 522: -0.8282828282828283
Loss for slice 523: -0.8282828282828283
Loss for slice 524: -0.8282828282828283
Loss for slice 525: -0.8282828282828283
Loss for slice 526: -0.8282828282828283
Loss for slice 527: -0.8282828282828283
Loss for slice 528: -0.6868686868686869
Loss for slice 529: -0.6868686868686869
Loss for slice 530: -0.6868686868686869
Loss for slice 531: -0.6868686868686869
Loss for slice 532: -0.6868686868686869
Loss for slice 533: -0.6868686868686869
Loss for slice 534: -0.6868686868686869
Loss for slice 535: -0.6868686868686869
Loss for slice 536: -0.6868686868686869
Loss for slice 537: -0.6868686868686869
Loss for slice 538: -0.6868686868686869
Loss for slice 539: -0.6868686868686869
Loss for slice 540: -0.6868686868686869
Loss for slice 541: -0.6868686868686869
Loss for slice 542: -0.6868686868686869
Loss for slice 543: -0.6868686868686869
Loss for slice 544: -0.6868686868686869
Loss for slice 545: -0.6868686868686869
Loss for slice 546: -0.6868686868686869
Loss for slice 547: -0.6868686868686869
Loss for slice 548: -0.6868686868686869
Loss for slice 549: -0.6868686868686869
Loss for slice 550: -0.6868686868686869
Loss for slice 551: -0.6868686868686869
Loss for slice 552: -0.6868686868686869
Loss for slice 553: -0.6565656565656566
Loss for slice 554: -0.6565656565656566
Loss for slice 555: -0.1818181818181818
Loss for slice 556: -0.15151515151515152
Loss for slice 557: -0.15151515151515152
Loss for slice 558: -0.15151515151515152
Loss for slice 559: -0.15151515151515152
Loss for slice 560: -0.1818181818181818
Loss for slice 561: -0.2121212121212121
Loss for slice 562: -0.2121212121212121
Loss for slice 563: -0.2121212121212121
Loss for slice 564: -0.1818181818181818
Loss for slice 565: -0.2121212121212121
Loss for slice 566: -0.2121212121212121
Loss for slice 567: -0.2121212121212121
Loss for slice 568: -0.2121212121212121
Loss for slice 569: -0.2121212121212121
Loss for slice 570: -0.1818181818181818
Loss for slice 571: -0.1818181818181818
Loss for slice 572: -0.1818181818181818
Loss for slice 573: -0.1818181818181818
Loss for slice 574: -0.1818181818181818
Loss for slice 575: -0.6565656565656566
Loss for slice 576: -0.1818181818181818
Loss for slice 577: -0.1818181818181818
Loss for slice 578: -0.2121212121212121
Loss for slice 579: -0.2121212121212121
Loss for slice 580: -0.2121212121212121
Loss for slice 581: -0.1818181818181818
Loss for slice 582: -0.15151515151515152
Loss for slice 583: -0.15151515151515152
Loss for slice 584: -0.1818181818181818
Loss for slice 585: -0.1818181818181818
Loss for slice 586: -0.1818181818181818
Loss for slice 587: -0.1818181818181818
Loss for slice 588: -0.15151515151515152
Loss for slice 589: -0.1818181818181818
Loss for slice 590: -0.1818181818181818
Loss for slice 591: -0.5454545454545454
Loss for slice 592: -0.5151515151515151
Loss for slice 593: -0.5151515151515151
Loss for slice 594: -0.0909090909090909
Loss for slice 595: -0.0909090909090909
Loss for slice 596: -0.0909090909090909
Loss for slice 597: -0.0909090909090909
Loss for slice 598: -0.0909090909090909
Loss for slice 599: -0.06060606060606061
Loss for slice 600: -0.06060606060606061
Loss for slice 601: -0.12121212121212122
Loss for slice 602: -0.12121212121212122
Loss for slice 603: -0.12121212121212122
Loss for slice 604: -0.12121212121212122
Loss for slice 605: -0.15151515151515152
Loss for slice 606: -0.15151515151515152
Loss for slice 607: -0.15151515151515152
Loss for slice 608: -0.12121212121212122
Loss for slice 609: -0.0909090909090909
Loss for slice 610: -0.0909090909090909
Loss for slice 611: -0.0909090909090909
Loss for slice 612: -0.06060606060606061
Loss for slice 613: -0.0909090909090909
Loss for slice 614: -0.0909090909090909
Loss for slice 615: -0.0909090909090909
Loss for slice 616: -0.06060606060606061
Loss for slice 617: -0.0909090909090909
Loss for slice 618: -0.12121212121212122
Loss for slice 619: -0.0909090909090909
Loss for slice 620: -0.0909090909090909
Loss for slice 621: -0.12121212121212122
Loss for slice 622: -0.12121212121212122
Loss for slice 623: -0.12121212121212122
Loss for slice 624: -0.12121212121212122
Loss for slice 625: -0.12121212121212122
Loss for slice 626: -0.12121212121212122
Loss for slice 627: -0.1818181818181818
Loss for slice 628: -0.0909090909090909
Loss for slice 629: -0.12121212121212122
Loss for slice 630: -0.15151515151515152
Loss for slice 631: -0.15151515151515152
Loss for slice 632: -0.15151515151515152
Loss for slice 633: -0.15151515151515152
Loss for slice 634: -0.15151515151515152
Loss for slice 635: -0.15151515151515152
Loss for slice 636: -0.1818181818181818
Loss for slice 637: -0.1818181818181818
Loss for slice 638: -0.1818181818181818
Loss for slice 639: -0.1818181818181818
Loss for slice 640: -0.1818181818181818
Loss for slice 641: -0.1818181818181818
Loss for slice 642: -0.12121212121212122
Loss for slice 643: -0.12121212121212122
Loss for slice 644: -0.12121212121212122
Loss for slice 645: -0.12121212121212122
Loss for slice 646: -0.12121212121212122
Loss for slice 647: -0.12121212121212122
Loss for slice 648: -0.12121212121212122
Loss for slice 649: -0.12121212121212122
Loss for slice 650: -0.12121212121212122
Loss for slice 651: -0.5151515151515151
Loss for slice 652: -0.12121212121212122
Loss for slice 653: -0.12121212121212122
Loss for slice 654: -0.0909090909090909
Loss for slice 655: -0.12121212121212122
Loss for slice 656: -0.12121212121212122
Loss for slice 657: -0.0909090909090909
Loss for slice 658: -0.0909090909090909
Loss for slice 659: -0.0909090909090909
Loss for slice 660: -0.0909090909090909
Loss for slice 661: -0.06060606060606061
Loss for slice 662: -0.06060606060606061
Loss for slice 663: -0.06060606060606061
Loss for slice 664: -0.06060606060606061
Loss for slice 665: -0.06060606060606061
Loss for slice 666: -0.030303030303030304
Loss for slice 667: -0.030303030303030304
Loss for slice 668: -0.0
Loss for slice 669: -0.030303030303030304
Loss for slice 670: -0.030303030303030304
Loss for slice 671: -0.030303030303030304
Loss for slice 672: -0.0
Loss for slice 673: -0.0
Loss for slice 674: -0.0
Loss for slice 675: -0.0
Loss for slice 676: -0.030303030303030304
Loss for slice 677: -0.0
Loss for slice 678: -0.030303030303030304
Loss for slice 679: -0.0
Loss for slice 680: -0.0
Loss for slice 681: -0.0
Loss for slice 682: -0.0
Loss for slice 683: -0.0
Loss for slice 684: -0.0
Loss for slice 685: -0.030303030303030304
Loss for slice 686: -0.030303030303030304
Loss for slice 687: -0.030303030303030304
Loss for slice 688: -0.06060606060606061
Loss for slice 689: -0.06060606060606061
Loss for slice 690: -0.06060606060606061
Loss for slice 691: -0.06060606060606061
Loss for slice 692: -0.0909090909090909
Loss for slice 693: -0.0909090909090909
Loss for slice 694: -0.15151515151515152
Loss for slice 695: -0.12121212121212122
Loss for slice 696: -0.12121212121212122
Loss for slice 697: -0.12121212121212122
Loss for slice 698: -0.12121212121212122
Loss for slice 699: -0.12121212121212122
Loss for slice 700: -0.0909090909090909
Loss for slice 701: -0.12121212121212122
Loss for slice 702: -0.12121212121212122
Loss for slice 703: -0.12121212121212122
Loss for slice 704: -0.12121212121212122
Loss for slice 705: -0.12121212121212122
Loss for slice 706: -0.12121212121212122
Loss for slice 707: -0.0909090909090909
Loss for slice 708: -0.0909090909090909
Loss for slice 709: -0.0909090909090909
Loss for slice 710: -0.0909090909090909
Loss for slice 711: -0.12121212121212122
Loss for slice 712: -0.5151515151515151
Loss for slice 713: -0.15151515151515152
Loss for slice 714: -0.0909090909090909
Loss for slice 715: -0.12121212121212122
Loss for slice 716: -0.12121212121212122
Loss for slice 717: -0.0909090909090909
Loss for slice 718: -0.0909090909090909
Loss for slice 719: -0.5151515151515151
Loss for slice 720: -0.48484848484848486
Loss for slice 721: -0.12121212121212122
Loss for slice 722: -0.0909090909090909
Loss for slice 723: -0.12121212121212122
Loss for slice 724: -0.12121212121212122
Loss for slice 725: -0.12121212121212122
Loss for slice 726: -0.12121212121212122
Loss for slice 727: -0.12121212121212122
Loss for slice 728: -0.15151515151515152
Loss for slice 729: -0.15151515151515152
Loss for slice 730: -0.15151515151515152
Loss for slice 731: -0.12121212121212122
Loss for slice 732: -0.48484848484848486
Loss for slice 733: -0.48484848484848486
Loss for slice 734: -0.48484848484848486
Loss for slice 735: -0.48484848484848486
Loss for slice 736: -0.12121212121212122
Loss for slice 737: -0.15151515151515152
Loss for slice 738: -0.15151515151515152
Loss for slice 739: -0.12121212121212122
Loss for slice 740: -0.12121212121212122
Loss for slice 741: -0.12121212121212122
Loss for slice 742: -0.06060606060606061
Loss for slice 743: -0.12121212121212122
Loss for slice 744: -0.5454545454545454
Loss for slice 745: -0.48484848484848486
Loss for slice 746: -0.1818181818181818
Loss for slice 747: -0.5454545454545454
Loss for slice 748: -0.5454545454545454
Loss for slice 749: -0.5454545454545454
Loss for slice 750: -0.1818181818181818
Loss for slice 751: -0.1818181818181818
Loss for slice 752: -0.1818181818181818
Loss for slice 753: -0.24242424242424243
Loss for slice 754: -0.24242424242424243
Loss for slice 755: -0.2727272727272727
Loss for slice 756: -0.24242424242424243
Loss for slice 757: -0.2727272727272727
Loss for slice 758: -0.24242424242424243
Loss for slice 759: -0.2121212121212121
Loss for slice 760: -0.2121212121212121
Loss for slice 761: -0.2121212121212121
Loss for slice 762: -0.2121212121212121
Loss for slice 763: -0.24242424242424243
Loss for slice 764: -0.2121212121212121
Loss for slice 765: -0.1818181818181818
Loss for slice 766: -0.5757575757575757
Loss for slice 767: -0.2121212121212121
Loss for slice 768: -0.2121212121212121
Loss for slice 769: -0.15151515151515152
Loss for slice 770: -0.1818181818181818
Loss for slice 771: -0.1818181818181818
Loss for slice 772: -0.1818181818181818
Loss for slice 773: -0.15151515151515152
Loss for slice 774: -0.1818181818181818
Loss for slice 775: -0.1818181818181818
Loss for slice 776: -0.1818181818181818
Loss for slice 777: -0.1818181818181818
Loss for slice 778: -0.15151515151515152
Loss for slice 779: -0.1818181818181818
Loss for slice 780: -0.1818181818181818
Loss for slice 781: -0.1818181818181818
Loss for slice 782: -0.1818181818181818
Loss for slice 783: -0.15151515151515152
Loss for slice 784: -0.1818181818181818
Loss for slice 785: -0.1818181818181818
Loss for slice 786: -0.2121212121212121
Loss for slice 787: -0.5757575757575757
Loss for slice 788: -0.1818181818181818
Loss for slice 789: -0.5454545454545454
Loss for slice 790: -0.5454545454545454
Loss for slice 791: -0.5454545454545454
Loss for slice 792: -0.15151515151515152
Loss for slice 793: -0.5757575757575757
Loss for slice 794: -0.15151515151515152
Loss for slice 795: -0.2121212121212121
Loss for slice 796: -0.24242424242424243
Loss for slice 797: -0.24242424242424243
Loss for slice 798: -0.1818181818181818
Loss for slice 799: -0.2121212121212121
Loss for slice 800: -0.2121212121212121
Loss for slice 801: -0.24242424242424243
Loss for slice 802: -0.24242424242424243
Loss for slice 803: -0.30303030303030304
Loss for slice 804: -0.24242424242424243
Loss for slice 805: -0.24242424242424243
Loss for slice 806: -0.2727272727272727
Loss for slice 807: -0.2121212121212121
Loss for slice 808: -0.1818181818181818
Loss for slice 809: -0.24242424242424243
Loss for slice 810: -0.2727272727272727
Loss for slice 811: -0.30303030303030304
Loss for slice 812: -0.2727272727272727
Loss for slice 813: -0.2727272727272727
Loss for slice 814: -0.2727272727272727
Loss for slice 815: -0.30303030303030304
Loss for slice 816: -0.2727272727272727
Loss for slice 817: -0.24242424242424243
Loss for slice 818: -0.2121212121212121
Loss for slice 819: -0.2121212121212121
Loss for slice 820: -0.2121212121212121
Loss for slice 821: -0.1818181818181818
Loss for slice 822: -0.15151515151515152
Loss for slice 823: -0.1818181818181818
Loss for slice 824: -0.2121212121212121
Loss for slice 825: -0.1818181818181818
Loss for slice 826: -0.1818181818181818
Loss for slice 827: -0.2121212121212121
Loss for slice 828: -0.2121212121212121
Loss for slice 829: -0.1818181818181818
Loss for slice 830: -0.1818181818181818
Loss for slice 831: -0.2121212121212121
Loss for slice 832: -0.2121212121212121
Loss for slice 833: -0.2121212121212121
Loss for slice 834: -0.2121212121212121
Loss for slice 835: -0.2121212121212121
Loss for slice 836: -0.24242424242424243
Loss for slice 837: -0.24242424242424243
Loss for slice 838: -0.24242424242424243
Loss for slice 839: -0.24242424242424243
Loss for slice 840: -0.24242424242424243
Loss for slice 841: -0.24242424242424243
Loss for slice 842: -0.24242424242424243
Loss for slice 843: -0.24242424242424243
Loss for slice 844: -0.24242424242424243
Loss for slice 845: -0.24242424242424243
Loss for slice 846: -0.24242424242424243
Loss for slice 847: -0.24242424242424243
Loss for slice 848: -0.24242424242424243
Loss for slice 849: -0.24242424242424243
Loss for slice 850: -0.2121212121212121
Loss for slice 851: -0.2121212121212121
Loss for slice 852: -0.2121212121212121
Loss for slice 853: -0.24242424242424243
Loss for slice 854: -0.24242424242424243
Loss for slice 855: -0.1818181818181818
Loss for slice 856: -0.1818181818181818
Loss for slice 857: -0.1818181818181818
Loss for slice 858: -0.1818181818181818
Loss for slice 859: -0.1818181818181818
Loss for slice 860: -0.1818181818181818
Loss for slice 861: -0.6565656565656566
Loss for slice 862: -0.6565656565656566
Loss for slice 863: -0.6565656565656566
plt.plot(range(len(sweep_loss)), sweep_loss, marker="o")
plt.xlabel("Slice index")
plt.ylabel("Score")
plt.title("Score along axial slices")
plt.show()
The plot shows the score of the clustering along the axial slices. The optimal region corresponds to the anatomical description, as we can see in the following images.
# Find the max reward and show the slice for it
max_loss_idx = np.argmax(sweep_loss)
print(f"Minimum loss: {sweep_loss[max_loss_idx]} at index {max_loss_idx}")
fig, ax = plt.subplots(1, 2, figsize=(14, 7))
show_clusters(clusters_list[max_loss_idx], volume_img[:, max_loss_idx, :].T, ax=ax[0], aspect=6)
ax[1].imshow(volume_img[:, max_loss_idx, :], aspect=6, origin="lower")
for a in ax:
a.axis("off")
plt.show()
Minimum loss: -0.0 at index 668
zero_loss_indices = np.where(np.array(sweep_loss) == 0)[0]
print(f"{len(zero_loss_indices)} indices return a zero loss: ", zero_loss_indices)
nrows = 3
ncols = len(zero_loss_indices) // nrows
indices_to_display = nrows * ncols
if indices_to_display > 0:
fig, axes = plt.subplots(nrows=nrows, ncols=ncols, figsize=(20, 10))
axes = axes.flatten()
for i, idx in enumerate(zero_loss_indices[:indices_to_display]):
axes[i] = show_clusters(
tissue_clusters=clusters_list[idx],
slice=volume_img[:, idx, :].T,
aspect=6,
ax=axes[i],
)
axes[i].set_title(f"Index: {idx}, Loss: {sweep_loss[idx]:.2f}")
axes[i].axis("off")
plt.show()
12 indices return a zero loss: [668 672 673 674 675 677 679 680 681 682 683 684]
Segmenting the same image with the centro-symmetric clustering and DBSCAN demonstrates the difference in the clustering techniques. The centro-symmetric clustering is more sensitive to the noise in the image, while DBSCAN can be tuned to ignore the noise and focus on the main clusters.
from armscan_env.clustering import cluster_iter
fig, ax = plt.subplots(1, 2, figsize=(14, 7))
clusters_668 = cluster_iter(tissues, volume_img[:, 668, :].T)
show_clusters(clusters_668, volume_img[:, 668, :].T, aspect=6, ax=ax[0])
ax[0].axis("off")
ax[0].set_title("Centrosymmetric Clustering")
clusters_668 = TissueClusters.from_labelmap_slice(volume_img[:, 668, :].T)
show_clusters(clusters_668, volume_img[:, 668, :].T, aspect=6, ax=ax[1])
ax[1].axis("off")
ax[1].set_title("DBSCAN Clustering")
plt.show()