# Changeset 1182

Ignore:
Timestamp:
08/31/22 00:05:43 (20 months ago)
Message:

More concise code and less redundancy in dissimilarity classes, added support for archive of genotypes, added hard limit on the number of genotype chars

Location:
framspy
Files:
5 edited

Unmodified
Removed
• ## framspy/evolalg/dissimilarity/dissimilarity.py

 r1145 self.output_field = output_field self.fn_reduce = None self.fn_reduce = Dissimilarity.get_reduction_by_name(reduction) self.knn = knn if reduction == "mean": # TODO change this 'elif' sequence to dictionary? self.fn_reduce = np.mean elif reduction == "max": self.fn_reduce = np.max elif reduction == "min": self.fn_reduce = np.min elif reduction == "sum": self.fn_reduce = np.sum elif reduction == "knn_mean": self.fn_reduce = self.knn_mean elif reduction == "none" or reduction is None: self.fn_reduce = None @staticmethod def reduce(dissim_matrix, fn_reduce, knn): if fn_reduce is None: return dissim_matrix elif fn_reduce is Dissimilarity.knn_mean: return fn_reduce(dissim_matrix, 1, knn) else: raise ValueError("Unknown reduction type. Supported: mean, max, min, sum, knn_mean, none") return fn_reduce(dissim_matrix, axis=1) def reduce(self, dissim_matrix): if self.fn_reduce is None: return dissim_matrix return self.fn_reduce(dissim_matrix, axis=1) def knn_mean(self, dissim_matrix,axis): return np.mean(np.partition(dissim_matrix, self.knn)[:,:self.knn],axis=axis) @staticmethod def knn_mean(dissim_matrix, axis, knn): return np.mean(np.partition(dissim_matrix, knn)[:, :knn], axis=axis) @staticmethod def get_reduction_by_name(reduction: str): if reduction not in REDUCTION_FUNCTION: raise ValueError(f"Unknown reduction type '{reduction}'. Supported: {','.join(REDUCTION_FUNCTION.keys())}") return REDUCTION_FUNCTION[reduction] REDUCTION_FUNCTION = { "mean": np.mean, "max": np.max, "min": np.min, "sum": np.sum, "knn_mean": Dissimilarity.knn_mean, "none": None }
• ## framspy/evolalg/dissimilarity/frams_dissimilarity.py

 r1145 from evolalg.dissimilarity.dissimilarity import Dissimilarity #TODO eliminate overlap with dissimilarity.py class FramsDissimilarity(FramsStep): def __init__(self, frams_lib, reduction="mean", output_field="dissim", knn=None, *args, **kwargs): def __init__(self, frams_lib, reduction="mean", output_field="dissim",knn=None, *args, **kwargs): super(FramsDissimilarity, self).__init__(frams_lib, *args, **kwargs) self.output_field = output_field self.fn_reduce = None self.fn_reduce = Dissimilarity.get_reduction_by_name(reduction) self.knn = knn if reduction == "mean": self.fn_reduce = np.mean elif reduction == "max": self.fn_reduce = np.max elif reduction == "min": self.fn_reduce = np.min elif reduction == "sum": self.fn_reduce = np.sum elif reduction == "knn_mean": self.fn_reduce = self.knn_mean elif reduction == "none" or reduction is None: self.fn_reduce = None else: raise ValueError("Unknown reduction type. Supported: mean, max, min, sum, knn_mean, none") def reduce(self, dissim_matrix): if self.fn_reduce is None: return dissim_matrix return self.fn_reduce(dissim_matrix, axis=1) def call(self, population): if len(population) == 0: return [] dissim_matrix = self.frams.dissimilarity([_.genotype for _ in population]) dissim = self.reduce(dissim_matrix) dissim_matrix = self.frams.dissimilarity([_.genotype for _ in population], 1) dissim = Dissimilarity.reduce(dissim_matrix, self.fn_reduce, self.knn) for d,ind in zip(dissim, population): setattr(ind, self.output_field, d) return population def knn_mean(self, dissim_matrix,axis): return np.mean(np.partition(dissim_matrix, self.knn)[:,:self.knn],axis=axis)
• ## framspy/evolalg/dissimilarity/levenshtein.py

 r1139 gen_dis.append(lev.distance(p.genotype, p2.genotype)) dissim.append(gen_dis) dissim = self.reduce(dissim) dissim = self.reduce(dissim, self.fn_reduce, self.knn) for d, ind in zip(dissim, population): setattr(ind, self.output_field, d)