Predict embedding on new data X_test given the existing embedding on training data

        Uses the Nystrom Extension to estimate the eigenvectors.

        Currently only works with input_type data (i.e. not affinity or distance)
505166
def predict(self, X_test, y=None):
        
        if not hasattr(self, 'geom_'):
            raise RuntimeError('the .fit() function must be called before the .predict() function')
        if self.geom_.X is None:
            raise NotImplementedError('method only implemented when X passed as data')
        # Complete the adjacency matrix
        adjacency_kwds = self.geom_.adjacency_kwds
        if self.geom_.adjacency_method == 'cyflann':
            if 'cyflann_kwds' in adjacency_kwds.keys():
                cyflann_kwds = adjacency_kwds['cyflann_kwds']
            else:
                cyflann_kwds = {}
        total_adjacency_matrix = complete_adjacency_matrix(self.geom_.adjacency_matrix,
                                                           self.geom_.X,
                                                           X_test,adjacency_kwds)
        # Compute the affinity matrix, check method and kwds
        if self.geom_.affinity_kwds is not None:
            affinity_kwds = self.geom_.affinity_kwds
        else:
            affinity_kwds = {}
        if self.geom_.affinity_method is not None:
            affinity_method = self.geom_.affinity_method
        else:
            affinity_method = 'auto'
        total_affinity_matrix = compute_affinity_matrix(total_adjacency_matrix, affinity_method,
                                                       **affinity_kwds)
        # Compute the affinity matrix, check method and kwds
        if self.geom_.laplacian_kwds is not None:
            laplacian_kwds = self.geom_.laplacian_kwds
        else:
            laplacian_kwds = {}
        if self.geom_.laplacian_method is not None:
            laplacian_method = self.geom_.laplacian_method
        else:
            self.laplacian_method = 'auto'
        total_laplacian_matrix = compute_laplacian_matrix(total_affinity_matrix, laplacian_method,
                                                       **laplacian_kwds)
        # Take the columns of Laplacian and existing embedding and pass to Nystrom Extension
        (n_sample_train) = self.geom_.adjacency_matrix.shape[0]
        total_laplacian_matrix = total_laplacian_matrix.tocsr()
        C = total_laplacian_matrix[:, :n_sample_train]
        # warnings.warn(str(C.shape))
        eigenvalues, eigenvectors = nystrom_extension(C, self.eigenvectors_, self.eigenvalues_)
        # If diffusion maps compute diffusion time etc
        if self.diffusion_maps:
            embedding = compute_diffusion_maps(laplacian_method, eigenvectors, eigenvalues, self.diffusion_time)
        else:
            embedding = eigenvectors
        (n_sample_test) = X_test.shape[0]
        embedding_test=embedding[-n_sample_test:, :]
        return embedding_test, embedding
1031618
def predict_given_context(self, x, c, c_dims):
        
        assert len(c) == len(c_dims)
        _, index = self.dataset.nn_dims(x, c, range(self.dim_x), list(np.array(c_dims) + self.dim_x), k=1)
        return self.dataset.get_y(index[0])
7387
def predict_dims(self, q, dims_x, dims_y, dims_out, sigma=None, k=None):
        
        assert len(q) == len(dims_x) + len(dims_y)
        sigma_sq = self.sigma_sq if sigma is None else sigma*sigma
        k = k or self.k       
        
        dists, index = self.dataset.nn_dims(q[:len(dims_x)], q[len(dims_x):], dims_x, dims_y, k=k)

        w = self._weights(dists, index, sigma_sq)
        Xq  = np.array(np.append([1.0], q), ndmin = 2)
        X   = np.array([np.append([1.0], self.dataset.get_dims(i, dims_x=dims_x, dims_y=dims_y)) for i in index])
        Y = np.array([self.dataset.get_dims(i, dims=dims_out) for i in index])
        
        W   = np.diag(w)
        WX  = np.dot(W, X)
        WXT = WX.T

        B   = np.dot(np.linalg.pinv(np.dot(WXT, WX)),WXT)

        self.mat = np.dot(B, np.dot(W, Y))
        
        Yq  = np.dot(Xq, self.mat)

        return Yq.ravel()
843724
def predict(self, X):
        
        X = np.atleast_2d(X)
        m = np.empty(shape=(0,1))
        s = np.empty(shape=(0,1))

        for k in range(X.shape[0]):
            preds = []
            for pred in self.model.estimators_:
                preds.append(pred.predict(X[k,:])[0])
            m = np.vstack((m ,np.array(preds).mean()))
            s = np.vstack((s ,np.array(preds).std()))
        return m, s
514090
def predict_withGradients(self, X):
        
        if X.ndim==1: X = X[None,:]
        m, v = self.model.predict(X)
        v = np.clip(v, 1e-10, np.inf)
        dmdx, dvdx = self.model.predictive_gradients(X)
        dmdx = dmdx[:,:,0]
        dsdx = dvdx / (2*np.sqrt(v))

        return m, np.sqrt(v), dmdx, dsdx
432145
def _compute_prediction(self,X):
        
        A_pred = np.dot(X,self._Bhat)
        Y_pred = self._basis_object.inverse_transform(A_pred)
        return Y_pred
977820
def _check_vector_update(self, tokens, new_embedding):
        """"""
        assert self._idx_to_vec is not None, '`idx_to_vec` has not been initialized.'

        if not isinstance(tokens, (list, tuple)) or len(tokens) == 1:
            assert isinstance(new_embedding, nd.NDArray) and len(new_embedding.shape) in [1, 2], \
                '`new_embedding` must be a 1-D or 2-D NDArray if `tokens` is a single token.'
            if not isinstance(tokens, (list, tuple)):
                tokens = [tokens]
            if len(new_embedding.shape) == 1:
                new_embedding = new_embedding.expand_dims(0)

        else:
            assert isinstance(new_embedding, nd.NDArray) and len(new_embedding.shape) == 2, \
                '`new_embedding` must be a 2-D NDArray if `tokens` is a list of multiple strings.'
        if self._idx_to_vec is not None:
            assert new_embedding.shape == (len(tokens), self._idx_to_vec.shape[1]), \
                'The length of `new_embedding` must be equal to the number ' \
                'of tokens and the width of new_embedding must be equal ' \
                'to the dimension of embedding of the glossary.'
        else:
            assert new_embedding.shape[0] == len(tokens), \
                'The length of `new_embedding` must be equal to the number of tokens'
        return tokens
1185386
def _embedding_nonmetric_mds(matrix, dimensions=3, initial_coords=None):
    
    mds = sklearn.manifold.MDS(n_components=dimensions,
                               dissimilarity='precomputed',
                               metric=False)
    if initial_coords is not None:
        mds.fit(matrix, init=initial_coords)
    else:
        mds.fit(matrix)
    return mds.embedding_
468556
def infer_x(self, y_desired, **kwargs):
        
        
        sigma = kwargs.get('sigma', self.sigma)
        k     = kwargs.get('k', self.k)
         
        xq = self._guess_x(y_desired, k = k, sigma = sigma, **kwargs)[0]    
         
        dists, index = self.fmodel.dataset.nn_x(xq, k = k)

        w = self.fmodel._weights(dists, sigma*sigma)
        
        X   = np.array([self.fmodel.dataset.get_x_padded(i) for i in index])
        Y    = np.array([self.fmodel.dataset.get_y(i) for i in index])
         
        W   = np.diag(w)
        WX  = np.dot(W, X)
        WXT = WX.T
             
        B   = np.dot(np.linalg.pinv(np.dot(WXT, WX)),WXT)
        
        M = np.dot(B, np.dot(W, Y))
        
        _, idx = self.fmodel.dataset.nn_y(y_desired, k=1)        
        ynn = self.fmodel.dataset.get_y(idx[0])
        
        eps = 0.00001
        
        J = np.zeros((len(xq),len(y_desired)), dtype = np.float64)
        for i in range(len(xq)):
            xi = np.array(xq, dtype = np.float64)
            xi[i] = xi[i] + eps
            yi = np.dot(np.append(1.0, xi), M).ravel()
            J[i] = (yi - ynn) / eps
        J = np.transpose(J)
        Jinv = np.linalg.pinv(J)
        x = np.dot(Jinv, y_desired - ynn)
        
        return [x]
1249945
def predict(self, mllib_data):
        
        if isinstance(mllib_data, pyspark.mllib.linalg.Matrix):
            return to_matrix(self._master_network.predict(from_matrix(mllib_data)))
        elif isinstance(mllib_data, pyspark.mllib.linalg.Vector):
            return to_vector(self._master_network.predict(from_vector(mllib_data)))
        else:
            raise ValueError(
                'Provide either an MLLib matrix or vector, got {}'.format(mllib_data.__name__))
76099
def predict_raw(self, X):
        
        # b -- bias for the input and h layers
        b = np.ones((X.shape[0], 1))
        w2 = self.w[-(self.h + 1):].reshape(self.h + 1, 1)
        w1 = self.w[:-(self.h + 1)].reshape(self.i + 1, self.h)

        # Make X to have the same number of columns as self.i.
        # Because of the sparse matrix representation, X for prediction can
        # have a different number of columns.
        if X.shape[1] > self.i:
            # If X has more columns, cut extra columns.
            X = X[:, :self.i]
        elif X.shape[1] < self.i:
            # If X has less columns, cut the rows of the weight matrix between
            # the input and h layers instead of X itself because the SciPy
            # sparse matrix does not support .set_shape() yet.
            idx = range(X.shape[1])
            idx.append(self.i)        # Include the last row for the bias
            w1 = w1[idx, :]

        if sparse.issparse(X):
            return np.hstack((sigm(sparse.hstack((X, b)).dot(w1)), b)).dot(w2)
        else:
            return np.hstack((sigm(np.hstack((X, b)).dot(w1)), b)).dot(w2)
119804
def predict(self, x, *args, **kwargs):
        
        if not self.model:
            raise Exception('Not trained. Train first')

        points = self._dimensionality_check(x, ext='points')

        return self.execute(points, *args, **kwargs)[0]
690396
def gaussian_filter(data, sigma):
    
    if np.isscalar(sigma):
        sigma = (sigma,) * data.ndim

    baseline = data.mean()
    filtered = data - baseline
    for ax in range(data.ndim):
        s = float(sigma[ax])
        if s == 0:
            continue

        # generate 1D gaussian kernel
        ksize = int(s * 6)
        x = np.arange(-ksize, ksize)
        kernel = np.exp(-x**2 / (2*s**2))
        kshape = [1, ] * data.ndim
        kshape[ax] = len(kernel)
        kernel = kernel.reshape(kshape)

        # convolve as product of FFTs
        shape = data.shape[ax] + ksize
        scale = 1.0 / (abs(s) * (2*np.pi)**0.5)
        filtered = scale * np.fft.irfft(np.fft.rfft(filtered, shape, axis=ax) *
                                        np.fft.rfft(kernel, shape, axis=ax),
                                        axis=ax)

        # clip off extra data
        sl = [slice(None)] * data.ndim
        sl[ax] = slice(filtered.shape[ax]-data.shape[ax], None, None)
        filtered = filtered[sl]
    return filtered + baseline
1082413
def predict(self,param_dict):
        
        encoder_dict = self._designmatrix_object.encoder
        X, col_names = self._designmatrix_object.run_encoder(param_dict, encoder_dict)
        # compute predictions
        Y_pred = self._compute_prediction(X)
        return Y_pred
188936
def fitlin(imgarr,refarr):
    
    # Initialize variables
    _mat = np.zeros((3,3),dtype=np.float64)
    _xorg = imgarr[0][0]
    _yorg = imgarr[0][1]
    _xoorg = refarr[0][0]
    _yoorg = refarr[0][1]
    _sigxox = 0.
    _sigxoy = 0.
    _sigxo = 0.
    _sigyox = 0.
    _sigyoy = 0.
    _sigyo = 0.

    _npos = len(imgarr)
    # Populate matrices
    for i in range(_npos):
        _mat[0][0] += np.power((imgarr[i][0] - _xorg),2)
        _mat[0][1] += (imgarr[i][0] - _xorg) * (imgarr[i][1] - _yorg)
        _mat[0][2] += (imgarr[i][0] - _xorg)
        _mat[1][1] += np.power((imgarr[i][1] - _yorg),2)
        _mat[1][2] += imgarr[i][1] - _yorg

        _sigxox += (refarr[i][0] - _xoorg)*(imgarr[i][0] - _xorg)
        _sigxoy += (refarr[i][0] - _xoorg)*(imgarr[i][1] - _yorg)
        _sigxo += refarr[i][0] - _xoorg
        _sigyox += (refarr[i][1] - _yoorg)*(imgarr[i][0] -_xorg)
        _sigyoy += (refarr[i][1] - _yoorg)*(imgarr[i][1] - _yorg)
        _sigyo += refarr[i][1] - _yoorg

    _mat[2][2] = _npos
    _mat[1][0] = _mat[0][1]
    _mat[2][0] = _mat[0][2]
    _mat[2][1] = _mat[1][2]

    # Now invert this matrix
    _mat = linalg.inv(_mat)

    _a  = _sigxox*_mat[0][0]+_sigxoy*_mat[0][1]+_sigxo*_mat[0][2]
    _b  = -1*(_sigxox*_mat[1][0]+_sigxoy*_mat[1][1]+_sigxo*_mat[1][2])
    #_x0 = _sigxox*_mat[2][0]+_sigxoy*_mat[2][1]+_sigxo*_mat[2][2]

    _c  = _sigyox*_mat[1][0]+_sigyoy*_mat[1][1]+_sigyo*_mat[1][2]
    _d  = _sigyox*_mat[0][0]+_sigyoy*_mat[0][1]+_sigyo*_mat[0][2]
    #_y0 = _sigyox*_mat[2][0]+_sigyoy*_mat[2][1]+_sigyo*_mat[2][2]

    _xt = _xoorg - _a*_xorg+_b*_yorg
    _yt = _yoorg - _d*_xorg-_c*_yorg

    return [_a,_b,_xt],[_c,_d,_yt]
1273264
def predict(model, x):
    
    if not hasattr(x, "chunks") and hasattr(x, "to_dask_array"):
        x = x.to_dask_array()
    assert x.ndim == 2
    if len(x.chunks[1]) > 1:
        x = x.rechunk(chunks=(x.chunks[0], sum(x.chunks[1])))
    func = partial(_predict, model)
    xx = np.zeros((1, x.shape[1]), dtype=x.dtype)
    dt = model.predict(xx).dtype
    return x.map_blocks(func, chunks=(x.chunks[0], (1,)), dtype=dt).squeeze()
946185
def predict(self, x):
        
        if isinstance(x, RDD):
            return x.map(lambda v: self.predict(v))

        x = _convert_to_vector(x)
        if self.numClasses == 2:
            margin = self.weights.dot(x) + self._intercept
            if margin > 0:
                prob = 1 / (1 + exp(-margin))
            else:
                exp_margin = exp(margin)
                prob = exp_margin / (1 + exp_margin)
            if self._threshold is None:
                return prob
            else:
                return 1 if prob > self._threshold else 0
        else:
            best_class = 0
            max_margin = 0.0
            if x.size + 1 == self._dataWithBiasSize:
                for i in range(0, self._numClasses - 1):
                    margin = x.dot(self._weightsMatrix[i][0:x.size]) + \
                        self._weightsMatrix[i][x.size]
                    if margin > max_margin:
                        max_margin = margin
                        best_class = i + 1
            else:
                for i in range(0, self._numClasses - 1):
                    margin = x.dot(self._weightsMatrix[i])
                    if margin > max_margin:
                        max_margin = margin
                        best_class = i + 1
            return best_class