Ignore:
Timestamp:
08/31/22 00:05:43 (5 weeks ago)
Author:
Maciej Komosinski
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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • framspy/evolalg/dissimilarity/frams_dissimilarity.py

    r1145 r1182  
    66from evolalg.dissimilarity.dissimilarity import Dissimilarity
    77
    8 #TODO eliminate overlap with dissimilarity.py
    9 
    108
    119class FramsDissimilarity(FramsStep):
    1210
    13     def __init__(self, frams_lib, reduction="mean", output_field="dissim", knn=None, *args, **kwargs):
     11    def __init__(self, frams_lib, reduction="mean", output_field="dissim",knn=None, *args, **kwargs):
    1412        super(FramsDissimilarity, self).__init__(frams_lib, *args, **kwargs)
    1513
    1614        self.output_field = output_field
    17         self.fn_reduce = None
     15        self.fn_reduce = Dissimilarity.get_reduction_by_name(reduction)
    1816        self.knn = knn
    19         if reduction == "mean":
    20             self.fn_reduce = np.mean
    21         elif reduction == "max":
    22             self.fn_reduce = np.max
    23         elif reduction == "min":
    24             self.fn_reduce = np.min
    25         elif reduction == "sum":
    26             self.fn_reduce = np.sum
    27         elif reduction == "knn_mean":
    28             self.fn_reduce = self.knn_mean
    29         elif reduction == "none" or reduction is None:
    30             self.fn_reduce = None
    31         else:
    32             raise ValueError("Unknown reduction type. Supported: mean, max, min, sum, knn_mean, none")
    3317
    34     def reduce(self, dissim_matrix):
    35         if self.fn_reduce is None:
    36             return dissim_matrix
    37         return self.fn_reduce(dissim_matrix, axis=1)
    3818
    3919    def call(self, population):
     
    4121        if len(population) == 0:
    4222            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)
    4525        for d,ind in zip(dissim, population):
    4626            setattr(ind, self.output_field, d)
    4727        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)
Note: See TracChangeset for help on using the changeset viewer.