Rev | Line | |
---|

[1113] | 1 | from abc import ABC |
---|

| 2 | |
---|

| 3 | from evolalg.base.step import Step |
---|

| 4 | import numpy as np |
---|

| 5 | |
---|

| 6 | |
---|

| 7 | class Dissimilarity(Step, ABC): |
---|

| 8 | |
---|

| 9 | def __init__(self, reduction="mean", output_field="dissim", *args, **kwargs): |
---|

| 10 | super(Dissimilarity, self).__init__(*args, **kwargs) |
---|

| 11 | |
---|

| 12 | self.output_field = output_field |
---|

| 13 | self.fn_reduce = None |
---|

| 14 | if reduction == "mean": |
---|

| 15 | self.fn_reduce = np.mean |
---|

| 16 | elif reduction == "max": |
---|

| 17 | self.fn_reduce = np.max |
---|

| 18 | elif reduction == "min": |
---|

| 19 | self.fn_reduce = np.min |
---|

| 20 | elif reduction == "sum": |
---|

| 21 | self.fn_reduce = np.sum |
---|

| 22 | elif reduction == "none" or reduction == None: |
---|

| 23 | self.fn_reduce = None |
---|

| 24 | else: |
---|

| 25 | raise ValueError("Unknown reduction type. Supported: mean, max, min, sum, none") |
---|

| 26 | |
---|

| 27 | def reduce(self, dissim_matrix): |
---|

| 28 | if self.fn_reduce is None: |
---|

| 29 | return dissim_matrix |
---|

| 30 | return self.fn_reduce(dissim_matrix, axis=1) |
---|

**Note:** See

TracBrowser
for help on using the repository browser.