Ejemplo n.º 1
0
    def apply2locations(self, locations, k=None):
        """Apply perturbation to locations.

        Parameters
        ----------
        locations: np.ndarray
            the spatial information to be perturbed.
        k: int (default=None)
            the perturbation indices.

        Returns
        -------
        locations: np.ndarray
            the spatial information perturbated.

        """
        ## 0. Prepare inputs
        # Check proper locations
        self._check_locations(locations)
        # Preparation of ks
        ks = range(self.k_perturb) if k is None else k
        ks = [k] if check_int(k) else ks
        ## 1. Main computation
        locations_p = self.basecoreperturbation.apply(locations, ks)
        ## 2. Format output
        if check_int(k):
            return locations_p[:, :, 0]
        return locations_p
Ejemplo n.º 2
0
    def apply2features(self, feature, k=None):
        """Apply perturbation to features.

        Parameters
        ----------
        features: np.ndarray or others
            the element features collection to be perturbed.
        k: int (default=None)
            the perturbation indices.

        Returns
        -------
        features: np.ndarray or others
            the element features collection perturbated.

        """
        ## Prepare inputs
        k = self._format_k_perturb(k)
        logi_int = check_int(k)
        if logi_int:
            k = [k]
        if len(feature.shape) == 1:
            feature = feature.reshape((len(feature), 1))
        ## Compute each change
        features_p = self.basecoreperturbation.apply(feature, k)
#        ## Compute perturbation
#        features_p = np.zeros((len(feature), 1, len(k)))
#        for i_k in k:
#            jitter_d = np.random.random(len(feature))
#            features_p[:, 0, i_k] = np.multiply(self._stds, jitter_d)
        ## Format output
        if logi_int:
            features_p = features_p[:, :, 0]
        return features_p
Ejemplo n.º 3
0
    def apply2indices(self, i, k=None):
        """Apply the transformation to the indices.

        Parameters
        ----------
        i: int, list or np.ndarray
            the indices of the elements `i`.
        k: int, list
            the perturbation indices.

        Returns
        -------
        i: int, list or np.ndarray
            the indices of the elements `i`.

        """
        k = self._format_k_perturb(k)
        if type(k) == list:
            if type(i) == np.ndarray:
                i = np.stack([i for ki in k], axis=1)
            elif type(i) == list:
                i = [i for ki in k]
            elif check_int(i):
                i = [i for ki in k]
        return i
Ejemplo n.º 4
0
    def apply2features(self, features, k=None):
        """Apply perturbation to features.

        Parameters
        ----------
        features: np.ndarray or others
            the element features collection to be perturbed.
        k: int (default=None)
            the perturbation indices.

        Returns
        -------
        features: np.ndarray or others
            the element features collection perturbated.

        """
        assert features.shape[1] == len(self.perturbations)
        logi_int = check_int(k)
        k = self._format_k_perturb(k)
        if logi_int:
            k = [k]
        ## Apply individual perturbation for each features
        features_p = []
        for i in range(len(self.perturbations)):
            features_p_k =\
                self.perturbations[i].apply2features(features[:, [i]], k)
            features_p.append(features_p_k)
        features_p = np.concatenate(features_p, axis=1)
        if logi_int:
            features_p = features_p[:, :, 0]
        return features_p
    def _format_reindices(self, reindices, auto=True):
        """Format reindices.

        Parameters
        ----------
        reindices: np.ndarray or tuple
            the reindices to apply permutation perturbations.

        """
        if type(reindices) == np.ndarray:
            self.k_perturb = reindices.shape[1]
            self.reindices = reindices
        elif type(reindices) == tuple:
            n, k_perturb = reindices
            if check_int(n) and check_int(k_perturb):
                auto_indices = [np.arange(n)] if auto else []
                self.k_perturb = k_perturb
                n_pert = k_perturb-1 if auto else k_perturb
                self.reindices = np.vstack(auto_indices +
                                           [np.random.permutation(n)
                                            for i in xrange(n_pert)]).T
Ejemplo n.º 6
0
    def _filter_indices(self, i, k):
        """Filter indices to get the transformed data.

        Parameters
        ----------
        i: int, list, np.ndarray
            the indices of elements.
        k: int, list, np.ndarray
            the indices of perturbations.

        Returns
        -------
        i: int, list, np.ndarray
            the indices of elements.
        k: int, list, np.ndarray
            the indices of perturbations.
        info_input: list
            the boolean information about the if the input is sequencial or
            only a unique index.

        """
        info_input = [True, True]
        ## Check i
        if type(i) == np.ndarray:
            i = list(i)
        elif check_int(i):
            info_input[0] = False
            i = [i]
        assert(type(i) == list)
        ## Check k
        k = self._format_k_perturb(k)
        if type(k) == np.ndarray:
            k = list(k)
        elif check_int(k):
            info_input[1] = False
            k = [k]
        assert(type(k) == list)
        return i, k, info_input
Ejemplo n.º 7
0
    def apply2relations_ind(self, relations, i, k):
        """For precomputed applications. Apply perturbation to relations.

        Parameters
        ----------
        relations: np.ndarray or others
            the relations between elements to be perturbated.

        Returns
        -------
        relations: np.ndarray or others
            the relations between elements perturbated.

        """
        if check_int(i):
            return self.relations_p[i][:, k]
        else:
            return self.relations_p[i][:, :, k]
Ejemplo n.º 8
0
    def apply2locs_ind_precomputed(self, i, k):
        """Apply perturbation to locations individually for precomputed
        applications.

        Parameters
        ----------
        i: int or list
            the element indices.
        k: int or list
            the perturbation indices.

        Returns
        -------
        locations: np.ndarray or others
            the spatial information perturbated.

        """
        if check_int(i):
            return self.locations_p[i][:, k]
        else:
            return self.locations_p[i][:, :, k]
Ejemplo n.º 9
0
    def apply2features(self, feature, k=None):
        """Apply perturbation to features.

        Parameters
        ----------
        features: np.ndarray or others
            the element features collection to be perturbed.
        k: int (default=None)
            the perturbation indices.

        Returns
        -------
        features: np.ndarray or others
            the element features collection perturbated.

        """
        ## Prepare inputs
#        categories = np.unique(feature)
#        if len(categories) > len(self.probs):
#            msg = "Not matching dimension between probs and features."
#            raise IndexError(msg)
        k = self._format_k_perturb(k)
        logi_int = check_int(k)
        if logi_int:
            k = [k]
        if len(feature.shape) == 1:
            feature = feature.reshape((len(feature), 1))
        ## Compute each change
        features_p = self.basecoreperturbation.apply(feature, k)
#        features_p = np.zeros((len(feature), 1, len(k)))
#        for i_k in k:
#            for i in xrange(len(feature)):
#                r = np.random.random()
#                idx = np.where(feature[i] == self.labels)[0]
#                idx2 = np.where(self.probs[idx] > r)[0][0]
#                features_p[i, 0, i_k] = self.labels[idx2]

        if logi_int:
            features_p = features_p[:, :, 0]
        return features_p
Ejemplo n.º 10
0
    def apply2indices(self, i, k=None):
        """Apply the transformation to the indices.

        Parameters
        ----------
        i: int, list or np.ndarray
            the indices of the elements `i`.
        k: int, list
            the perturbation indices.

        Returns
        -------
        i: int, list or np.ndarray
            the indices of the elements `i`.

        """
        k = self._format_k_perturb(k)
        if check_int(i):
            return self.basecoreperturbation.reindices[i, k]
        elif type(i) == list:
            return list(self.basecoreperturbation.reindices[i][:, k])
        else:
            return self.basecoreperturbation.reindices[list(i)][:, k]