Ejemplo n.º 1
0
    def vocabulary_max(self, value):
        """
        Sets the maximum allowed vocabulary (bag) size. Samples with greater
        number of items will be truncated. Must be positive. Invalidates all
        the caches.

        :param value: the new maximum size which must be positive.
        :type value: int
        :raises ValueError: if the value is less than vocabulary_min or <= 0.
        """
        value = int(value)
        if value <= 0:
            raise ValueError("vocabulary_max must be > 0 (got %d)" % value)
        try:
            if value < self.vocabulary_min:
                raise ValueError(
                    "vocabulary_max may not be less than vocabulary_min")
        except AttributeError:
            pass
        self._vocabulary_max = value
        if self._relax_cache is not None:
            libwmdrelax.emd_relaxed_cache_fini(self._relax_cache)
        self._relax_cache = libwmdrelax.emd_relaxed_cache_init(value * 2)
        if self._exact_cache is not None:
            libwmdrelax.emd_cache_fini(self._exact_cache)
        self._exact_cache = libwmdrelax.emd_cache_init(value * 2)
        self._reset_caches()
Ejemplo n.º 2
0
 def test_with_cache(self):
     cache = libwmdrelax.emd_relaxed_cache_init(4)
     w1, w2, dist = self._get_w1_w2_dist()
     r = libwmdrelax.emd_relaxed(w1, w2, dist, cache)
     self.assertAlmostEqual(r, 0.6125112)
     r = libwmdrelax.emd_relaxed(w1, w2, dist, cache=cache)
     self.assertAlmostEqual(r, 0.6125112)
     libwmdrelax.emd_relaxed_cache_fini(cache)
Ejemplo n.º 3
0
 def vocabulary_max(self, value):
     value = int(value)
     if value <= 0:
         raise ValueError("vocabulary_max must be > 0 (got %d)" % value)
     self._vocabulary_max = value
     if self._relax_cache is not None:
         libwmdrelax.emd_relaxed_cache_fini(self._relax_cache)
     self._relax_cache = libwmdrelax.emd_relaxed_cache_init(value * 2)
     if self._exact_cache is not None:
         libwmdrelax.emd_cache_fini(self._exact_cache)
     self._exact_cache = libwmdrelax.emd_cache_init(value * 2)
     self._reset_caches()