def _make_pattern(self, base_sample): base_sample_weight = self._samples.pop(base_sample) base_weight = self.conf.INCUBATOR_NEW_PATTERN_IMPULSE_WEIGHT # find similar samples similar_samples = {} for sample, sample_weight in dict(self._samples).items(): activity = similarity(base_sample, sample) if activity >= self.conf.INCUBATOR_NEW_PATTERN_SIMILAR_SAMPLES_ACTIVITY: similar_samples[sample] = sample_weight del self._samples[sample] # create pattern from base sample pattern = Matrix.create(self.conf.UNIT_INPUT_HEIGHT, self.conf.UNIT_INPUT_WIDTH) for y, impulse in enumerate(base_sample): if impulse is not None: pattern.set(y, impulse, base_weight) # add similar samples to pattern for sample, sample_weight in similar_samples.items(): adding_weight = sample_weight / base_sample_weight * base_weight for y, impulse in enumerate(sample): if impulse is not None: new_weight = braking_add(pattern.get(y, impulse), adding_weight) pattern.set(y, impulse, new_weight) return pattern
def test_similarity(): sim = similarity( [1, 1], [1, 1], ) assert 0.9999 < sim <= 1.0, 'equal lists' sim = similarity( [1, 1], [0, 0], ) assert 0.0 <= sim < 0.00001, 'max difference' sim = similarity( [1, 1], [2, 1], ) assert 0.49 < sim < 0.51