|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +Created on Mon Aug 12 14:30:06 2019 |
| 4 | +
|
| 5 | +@author: Ruibzhan & Omid Bazgir |
| 6 | +""" |
| 7 | + |
| 8 | +from scipy.stats import pearsonr |
| 9 | +import numpy as np |
| 10 | +import random |
| 11 | +from scipy.spatial import distance |
| 12 | +import pickle |
| 13 | +import pandas as pd |
| 14 | +import time |
| 15 | +from itertools import product |
| 16 | + |
| 17 | +#%% |
| 18 | +def universial_corr(dist_matr, mapping_in_int): |
| 19 | + # dist_matr is a sqr matr |
| 20 | + Nn = dist_matr.shape[0] |
| 21 | + # find what is the int coordinates for each feature, get an array |
| 22 | + # Because np.where returns a tuple (x_position array, y_position array), a generation is used |
| 23 | + coord = np.array([[item[0] for item in np.where(mapping_in_int == ii)] for ii in range(Nn)]) |
| 24 | + # get a 1-d form distance the euclidean dist between pixles positions |
| 25 | + pixel_dist = distance.pdist(coord) |
| 26 | + pixel_dist = pixel_dist.reshape(len(pixel_dist),1) |
| 27 | + # convert the 2-d distance to 1d distance |
| 28 | + feature_dist = distance.squareform(dist_matr) |
| 29 | + feature_dist = feature_dist.reshape(len(feature_dist),1) |
| 30 | + ## pearsonr returns a tuple |
| 31 | + #corrs = pearsonr(feature_dist,pixel_dist)[0] |
| 32 | + L2_Norm = np.sqrt(sum((pixel_dist - feature_dist)**2)/sum(feature_dist**2)) |
| 33 | + return L2_Norm |
| 34 | +#%% |
| 35 | +def evaluate_swap(coord1,coord2,dist_matr,mapping_in_int,original_corr = -2): |
| 36 | + # Coord are in list[] |
| 37 | + # Avoid messing up with the origianl map |
| 38 | + # The correlation before swap can be passed to save some calculation |
| 39 | + the_map = mapping_in_int.copy() |
| 40 | + # If out of bound, return NaN. |
| 41 | + if coord1[0]<0 or coord1[1]<0 or coord2[0]<0 or coord2[1]<0: |
| 42 | + return np.nan |
| 43 | + if coord1[0]>=the_map.shape[0] or coord1[1]>=the_map.shape[0] or coord2[0]>=the_map.shape[0] or coord2[1]>=the_map.shape[0]: |
| 44 | + return np.nan |
| 45 | + # If not given, recompute. |
| 46 | + if original_corr<-1 or original_corr>1: |
| 47 | + original_corr = universial_corr(dist_matr,the_map) |
| 48 | + # Swap |
| 49 | + try: |
| 50 | + temp = the_map[coord1[0],coord1[1]] |
| 51 | + the_map[coord1[0],coord1[1]] = the_map[coord2[0],coord2[1]] |
| 52 | + the_map[coord2[0],coord2[1]] = temp |
| 53 | + changed_corr = universial_corr(dist_matr,the_map) |
| 54 | + return(changed_corr - original_corr) |
| 55 | + except IndexError: |
| 56 | + raise Warning ("Swap index:", coord1,coord2,"Index error. Check the coordnation.") |
| 57 | + return np.nan |
| 58 | + |
| 59 | +def evaluate_centroid(centroid,dist_matr,mapping_in_int): |
| 60 | + original_corr = universial_corr(dist_matr,mapping_in_int) |
| 61 | + results = [100000] # just to skip the 0 position |
| 62 | + for each_direc in product([-1,0,1],repeat = 2): |
| 63 | + #print(each_direc) |
| 64 | + # directions are returned as tuple (-1,1), (-1,0), (-1,1), (0,0), .... |
| 65 | + swap_coord = [centroid[0]+each_direc[0],centroid[1]+each_direc[1]] |
| 66 | + evaluation = evaluate_swap(centroid,swap_coord,dist_matr,mapping_in_int,original_corr) |
| 67 | + results.append(evaluation) |
| 68 | + results_array = np.array(results) |
| 69 | + #best_swap_direc = np.where(results_array == np.nanmax(results_array))[0][0] |
| 70 | + best_swap_direc = np.where(results_array == np.nanmin(results_array))[0][0] |
| 71 | + # Give the best direction as a int |
| 72 | + return best_swap_direc |
| 73 | + |
| 74 | +def evaluate_centroids_in_list(centroids_list,dist_matr,mapping_in_int): |
| 75 | + # and returns a dict |
| 76 | + results = dict() |
| 77 | + for each_centr in centroids_list: |
| 78 | + each_centr = tuple(each_centr) |
| 79 | + evaluation = evaluate_centroid(each_centr,dist_matr,mapping_in_int) |
| 80 | + results.update({each_centr:evaluation}) |
| 81 | + return results |
| 82 | + |
| 83 | +#%% |
| 84 | +def execute_coordination_swap(coord1,coord2,mapping_in_int): |
| 85 | + # try passing the ref. directly |
| 86 | + the_map = mapping_in_int#.copy() |
| 87 | + # If out of bound, return NaN. |
| 88 | + if coord1[0]<0 or coord1[1]<0 or coord2[0]<0 or coord2[1]<0: |
| 89 | + raise Warning("Swapping failed:",coord1,coord2,"-- Negative coordnation.") |
| 90 | + return the_map |
| 91 | + if coord1[0]>the_map.shape[0] or coord1[1]>the_map.shape[0] or coord2[0]>the_map.shape[0] or coord2[1]>the_map.shape[0]: |
| 92 | + raise Warning("Swapping failed:",coord1,coord2,"-- Coordnation out of bound.") |
| 93 | + return the_map |
| 94 | + |
| 95 | + temp = the_map[coord1[0],coord1[1]] |
| 96 | + the_map[coord1[0],coord1[1]] = the_map[coord2[0],coord2[1]] |
| 97 | + the_map[coord2[0],coord2[1]] = temp |
| 98 | + |
| 99 | + return(the_map) |
| 100 | + |
| 101 | +# Initial centriod id & Swapping direction: |
| 102 | +# 1 2 3 |
| 103 | +# 4 5 6 |
| 104 | +# 7 8 9 |
| 105 | +# 0 in swapping is preserved for the header. |
| 106 | + |
| 107 | +def execute_direction_swap(centroid,mapping_in_int,direction = 5): |
| 108 | + # Need to notice that [0] is the vertival coord, [1] is the horiz coord. similar to the matlab images. |
| 109 | + coord1 = list(centroid) |
| 110 | + coord2 = list(centroid) |
| 111 | + if direction not in range(1,10): |
| 112 | + raise ValueError("Invalid swapping direction.") |
| 113 | + if direction == 5: |
| 114 | + return mapping_in_int |
| 115 | + |
| 116 | + if direction in [1,4,7]: |
| 117 | + coord2[1] -=1 |
| 118 | + elif direction in [3,6,9]: |
| 119 | + coord2[1] +=1 |
| 120 | + |
| 121 | + if direction in [1,2,3]: |
| 122 | + coord2[0] -=1 |
| 123 | + elif direction in [7,8,9]: |
| 124 | + coord2[0] +=1 |
| 125 | + |
| 126 | + the_map = execute_coordination_swap(coord1,coord2,mapping_in_int) |
| 127 | + return the_map |
| 128 | + |
| 129 | +def execute_dict_swap(swapping_dict, mapping_in_int): |
| 130 | + for each_key in swapping_dict: |
| 131 | + execute_direction_swap(each_key,mapping_in_int,direction = swapping_dict[each_key]) |
| 132 | + return mapping_in_int |
0 commit comments