Changeset 1182 for framspy/evolalg/dissimilarity
- Timestamp:
- 08/31/22 00:05:43 (2 years ago)
- Location:
- framspy/evolalg/dissimilarity
- Files:
-
- 1 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
framspy/evolalg/dissimilarity/dissimilarity.py
r1145 r1182 11 11 12 12 self.output_field = output_field 13 self.fn_reduce = None13 self.fn_reduce = Dissimilarity.get_reduction_by_name(reduction) 14 14 self.knn = knn 15 if reduction == "mean": # TODO change this 'elif' sequence to dictionary? 16 self.fn_reduce = np.mean 17 elif reduction == "max": 18 self.fn_reduce = np.max 19 elif reduction == "min": 20 self.fn_reduce = np.min 21 elif reduction == "sum": 22 self.fn_reduce = np.sum 23 elif reduction == "knn_mean": 24 self.fn_reduce = self.knn_mean 25 elif reduction == "none" or reduction is None: 26 self.fn_reduce = None 15 16 17 @staticmethod 18 def reduce(dissim_matrix, fn_reduce, knn): 19 if fn_reduce is None: 20 return dissim_matrix 21 elif fn_reduce is Dissimilarity.knn_mean: 22 return fn_reduce(dissim_matrix, 1, knn) 27 23 else: 28 r aise ValueError("Unknown reduction type. Supported: mean, max, min, sum, knn_mean, none")24 return fn_reduce(dissim_matrix, axis=1) 29 25 30 def reduce(self, dissim_matrix):31 if self.fn_reduce is None:32 return dissim_matrix33 return self.fn_reduce(dissim_matrix, axis=1)34 26 35 def knn_mean(self, dissim_matrix,axis): 36 return np.mean(np.partition(dissim_matrix, self.knn)[:,:self.knn],axis=axis) 27 @staticmethod 28 def knn_mean(dissim_matrix, axis, knn): 29 return np.mean(np.partition(dissim_matrix, knn)[:, :knn], axis=axis) 30 31 32 @staticmethod 33 def get_reduction_by_name(reduction: str): 34 35 if reduction not in REDUCTION_FUNCTION: 36 raise ValueError(f"Unknown reduction type '{reduction}'. Supported: {','.join(REDUCTION_FUNCTION.keys())}") 37 38 return REDUCTION_FUNCTION[reduction] 39 40 41 42 REDUCTION_FUNCTION = { 43 "mean": np.mean, 44 "max": np.max, 45 "min": np.min, 46 "sum": np.sum, 47 "knn_mean": Dissimilarity.knn_mean, 48 "none": None 49 } -
framspy/evolalg/dissimilarity/frams_dissimilarity.py
r1145 r1182 6 6 from evolalg.dissimilarity.dissimilarity import Dissimilarity 7 7 8 #TODO eliminate overlap with dissimilarity.py9 10 8 11 9 class FramsDissimilarity(FramsStep): 12 10 13 def __init__(self, frams_lib, reduction="mean", output_field="dissim", 11 def __init__(self, frams_lib, reduction="mean", output_field="dissim",knn=None, *args, **kwargs): 14 12 super(FramsDissimilarity, self).__init__(frams_lib, *args, **kwargs) 15 13 16 14 self.output_field = output_field 17 self.fn_reduce = None15 self.fn_reduce = Dissimilarity.get_reduction_by_name(reduction) 18 16 self.knn = knn 19 if reduction == "mean":20 self.fn_reduce = np.mean21 elif reduction == "max":22 self.fn_reduce = np.max23 elif reduction == "min":24 self.fn_reduce = np.min25 elif reduction == "sum":26 self.fn_reduce = np.sum27 elif reduction == "knn_mean":28 self.fn_reduce = self.knn_mean29 elif reduction == "none" or reduction is None:30 self.fn_reduce = None31 else:32 raise ValueError("Unknown reduction type. Supported: mean, max, min, sum, knn_mean, none")33 17 34 def reduce(self, dissim_matrix):35 if self.fn_reduce is None:36 return dissim_matrix37 return self.fn_reduce(dissim_matrix, axis=1)38 18 39 19 def call(self, population): … … 41 21 if len(population) == 0: 42 22 return [] 43 dissim_matrix = self.frams.dissimilarity([_.genotype for _ in population] )44 dissim = self.reduce(dissim_matrix)23 dissim_matrix = self.frams.dissimilarity([_.genotype for _ in population], 1) 24 dissim = Dissimilarity.reduce(dissim_matrix, self.fn_reduce, self.knn) 45 25 for d,ind in zip(dissim, population): 46 26 setattr(ind, self.output_field, d) 47 27 return population 48 49 def knn_mean(self, dissim_matrix,axis):50 return np.mean(np.partition(dissim_matrix, self.knn)[:,:self.knn],axis=axis) -
framspy/evolalg/dissimilarity/levenshtein.py
r1139 r1182 18 18 gen_dis.append(lev.distance(p.genotype, p2.genotype)) 19 19 dissim.append(gen_dis) 20 dissim = self.reduce(dissim )20 dissim = self.reduce(dissim, self.fn_reduce, self.knn) 21 21 for d, ind in zip(dissim, population): 22 22 setattr(ind, self.output_field, d)
Note: See TracChangeset
for help on using the changeset viewer.