(features, coords, dist_threshold=4, corr_threshold = 0.6)
| 494 | return [img, coords] |
| 495 | |
| 496 | def mergedpatch_gen(features, coords, dist_threshold=4, corr_threshold = 0.6): |
| 497 | |
| 498 | # Get patch distance in pixels with rendered segmentation level. Note that each patch is squared and therefore same distance. |
| 499 | patch_dist = abs(coords[0,2] - coords[0,0]) |
| 500 | print(patch_dist) |
| 501 | |
| 502 | # Compute feature similarity (cosine) and nearby pacthes (L2 norm - only need the top left x,y coordinates) |
| 503 | cosine_matrix = cosine_similarity(features, features) |
| 504 | coordinate_matrix = euclidean_distances(coords[:,:2], coords[:,:2]) |
| 505 | |
| 506 | # NOTE: random selection for the first patch for patch merging might be less biased towards tissue orientation and size. |
| 507 | indices_avail = np.arange(features.shape[0]) |
| 508 | np.random.seed(0) |
| 509 | np.random.shuffle(indices_avail) |
| 510 | |
| 511 | # Merging together nearby patches and similar within pre-defined threshold. |
| 512 | mergedfeatures = [] |
| 513 | indices_used = [] |
| 514 | for ref in indices_avail: |
| 515 | |
| 516 | # This has been merged already |
| 517 | if ref not in indices_used: |
| 518 | |
| 519 | # Making sure they won't be selected once more |
| 520 | if indices_used: |
| 521 | coordinate_matrix[ref,indices_used] = [np.Inf]*len(indices_used) |
| 522 | cosine_matrix[ref,indices_used] = [0.0]*len(indices_used) |
| 523 | |
| 524 | indices_dist = np.where(coordinate_matrix[ref] < patch_dist*dist_threshold, 1 , 0) |
| 525 | indices_corr = np.where(cosine_matrix[ref] > corr_threshold, 1 , 0) |
| 526 | final_indices = indices_dist * indices_corr |
| 527 | |
| 528 | # which includes already the ref patch |
| 529 | indices_used.extend(list(np.where(final_indices == 1)[0])) |
| 530 | mergedfeatures.append(tuple((features[final_indices==1,:], coords[final_indices==1,:]))) |
| 531 | else: |
| 532 | continue |
| 533 | |
| 534 | assert len(indices_used)==features.shape[0], f'Probably issue in contruscting merged features for graph {len(indices_used)}!={features.shape[0]}' |
| 535 | |
| 536 | return mergedfeatures |
| 537 | |
| 538 | class HNSW: |
| 539 | def __init__(self, space): |
no outgoing calls
no test coverage detected