def mutipleLearningMethod(inputVec, length1, length2, alpha, beta):
    threshold = 0.01

    iteration = 0
    # initialize A and X
    # print(inputVec)
    initDic = DictionaryLearning(n_components=length1, alpha=alpha)
    A = initDic.fit(inputVec).components_
    X = initDic.fit_transform(inputVec)

    # while error < threshold:
    while iteration < 20:
        # given A, X, update for B and Y
        dic = DictionaryLearning(n_components=length2, alpha=beta)
        B = dic.fit(inputVec - X.dot(A)).components_
        Y = dic.fit_transform(inputVec - X.dot(A))

        # given B, Y, update A, X
        dic2 = DictionaryLearning(n_components=length1, alpha=alpha)
        A = dic2.fit(inputVec - Y.dot(B)).components_
        X = dic2.fit_transform(inputVec - Y.dot(B))

        error = norm(inputVec-X.dot(A)-Y.dot(B)) + alpha*norm(X, 1) + beta*norm(Y, 1)
        iteration += 1

    return A, X, B, Y
Esempio n. 2
0
    def _get_stain_matrix(self, input_image: np.ndarray) -> np.ndarray:
        """Compute the 2x3 stain matrix with the method from the paper

        Args:
            input_image (np.array): Image to extract the stains from

        Returns:
            np.array: Extracted stains
        """
        mask = self._notwhite_mask(input_image, threshold=self.thres).reshape(
            (-1, ))
        optical_density = self._rgb_to_od(input_image).reshape((-1, 3))
        optical_density = optical_density[mask]
        n_features = optical_density.T.shape[1]

        dict_learner = DictionaryLearning(
            n_components=2,
            alpha=self.lambda_s,
            max_iter=10,
            fit_algorithm="lars",
            transform_algorithm="lasso_lars",
            transform_n_nonzero_coefs=n_features,
            random_state=0,
            positive_dict=True,
        )
        dictionary = dict_learner.fit_transform(optical_density.T).T

        if dictionary[0, 0] < dictionary[1, 0]:
            dictionary = dictionary[[1, 0], :]
        dictionary = self._normalize_rows(dictionary)
        return dictionary
Esempio n. 3
0
def dictionaryLearningMethod(inputVec, length):
    # length = len(vectors)
    length = length
    dic = DictionaryLearning(n_components=length, alpha=0.05)
    dictionary = dic.fit(inputVec).components_
    sparseCoder = dic.fit_transform(inputVec)
    # sparseCoder = dic.fit(vectors).components_
    # dictionary = dic.fit_transform(vectors)

    return dictionary, sparseCoder
Esempio n. 4
0
def dictionaryLearningMethod(vectors):
    # length = len(vectors)
    length = 5
    dic = DictionaryLearning(n_components=length, alpha=1)
    dictionary = dic.fit(vectors).components_
    sparseCoder = dic.fit_transform(vectors)
    # sparseCoder = dic.fit(vectors).components_
    # dictionary = dic.fit_transform(vectors)

    return dictionary.T , sparseCoder.T
Esempio n. 5
0
 def _estimate_linear_combination(self, imgs_vec, params):
     estimator = DictionaryLearning(n_components=params.get('nb_labels'),
                                    max_iter=params.get('max_iter'),
                                    fit_algorithm='lars',
                                    transform_algorithm='omp',
                                    split_sign=False,
                                    tol=params.get('tol'),
                                    n_jobs=1)
     fit_result = estimator.fit_transform(imgs_vec)
     components = estimator.components_
     return estimator, components, fit_result
Esempio n. 6
0
def init_atlas_dict_learn(imgs, nb_patterns, nb_iter=5, bg_threshold=0.1):
    """ estimating initial atlas using SoA method based on linear combinations

    :param list(ndarray) imgs: list of input images
    :param int nb_patterns: number of pattern in the atlas to be set
    :param int nb_iter: max number of iterations
    :param float bg_threshold:
    :return ndarray: estimated atlas

    >>> np.random.seed(0)
    >>> atlas = np.zeros((8, 12), dtype=int)
    >>> atlas[:3, 1:5] = 1
    >>> atlas[3:7, 6:12] = 2
    >>> luts = np.array([[0, 1, 0]] * 99 + [[0, 0, 1]] * 99 + [[0, 1, 1]] * 99)
    >>> imgs = [lut[atlas] for lut in luts]
    >>> init_atlas_dict_learn(imgs, 2)
    array([[0, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0],
           [0, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0],
           [0, 2, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0],
           [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1],
           [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1],
           [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1],
           [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1],
           [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
    """
    imgs_vec = np.array([np.ravel(im) for im in imgs])

    try:
        estimator = DictionaryLearning(n_components=nb_patterns + 1,
                                       fit_algorithm='lars',
                                       transform_algorithm='omp',
                                       split_sign=False,
                                       max_iter=nb_iter)
        fit_result = estimator.fit_transform(imgs_vec)
        components = estimator.components_

        ptn_used = np.sum(np.abs(fit_result), axis=0) > 0
        atlas_ptns = components.reshape((-1, ) + imgs[0].shape)

        atlas = convert_lin_comb_patterns_2_atlas(atlas_ptns, ptn_used,
                                                  bg_threshold)
    except Exception:
        logging.exception('CRASH: %s', init_atlas_dict_learn.__name__)
        atlas = np.zeros(imgs[0].shape, dtype=int)
    return atlas
Esempio n. 7
0
def cluster_sk_dictionary_learning(content):
    """ SK DL | | components: N, data:[[]], alpha: N, tol: N, fit: String, transform: String, split: bool """
    _config = DictionaryLearning(
        n_components=content['n_components'], 
        alpha=content['alpha'],
        max_iter=content['max_iter'],
        tol=content['tol'],
        fit_algorithm=content['fit_algorithm'], 
        transform_algorithm=content['transform_algorithm'], 
        split_sign=content['split_sign'],
        n_jobs=-1)
    _result = _config.fit_transform(content['data'])
    return httpWrapper(json.dumps({
        'result':  _result.tolist(),
        'components': _config.components_.tolist(),
        'error': _config.error_,
        'nIter': _config.n_iter_
    }, ignore_nan=True ))
Esempio n. 8
0
    def get_stain_matrix(img, luminosity_threshold=0.8, regulariser=0.1):
        """Stain matrix estimation.

        Args:
            img (:class:`numpy.ndarray`): input image used for stain matrix estimation
            luminosity_threshold (float): threshold used for tissue area selection
            regulariser (float): regulariser used in dictionary learning

        Returns:
            :class:`numpy.ndarray`: estimated stain matrix.

        """
        img = img.astype("uint8")  # ensure input image is uint8
        # convert to OD and ignore background
        tissue_mask = get_luminosity_tissue_mask(
            img, threshold=luminosity_threshold).reshape((-1, ))
        img_od = convert_RGB2OD(img).reshape((-1, 3))
        img_od = img_od[tissue_mask]

        # do the dictionary learning
        dl = DictionaryLearning(
            n_components=2,
            alpha=regulariser,
            transform_alpha=regulariser,
            fit_algorithm="lars",
            transform_algorithm="lasso_lars",
            positive_dict=True,
            verbose=False,
            max_iter=3,
            transform_max_iter=1000,
        )
        dictionary = dl.fit_transform(X=img_od.T).T

        # order H and E.
        # H on first row.
        dictionary = dl_output_for_h_and_e(dictionary)

        normalised_rows = dictionary / np.linalg.norm(dictionary, axis=1)[:,
                                                                          None]

        return normalised_rows
Esempio n. 9
0
    def get_stain_matrix(self, img):
        """Stain matrix estimation.

        Args:
            img (:class:`numpy.ndarray`):
                Input image used for stain matrix estimation

        Returns:
            :class:`numpy.ndarray`:
                Estimated stain matrix.

        """
        img = img.astype("uint8")  # ensure input image is uint8
        luminosity_threshold = self.__luminosity_threshold
        regularizer = self.__regularizer
        # convert to OD and ignore background
        tissue_mask = get_luminosity_tissue_mask(
            img, threshold=luminosity_threshold).reshape((-1, ))
        img_od = rgb2od(img).reshape((-1, 3))
        img_od = img_od[tissue_mask]

        # do the dictionary learning
        dl = DictionaryLearning(
            n_components=2,
            alpha=regularizer,
            transform_alpha=regularizer,
            fit_algorithm="lars",
            transform_algorithm="lasso_lars",
            positive_dict=True,
            verbose=False,
            max_iter=3,
            transform_max_iter=1000,
        )
        dictionary = dl.fit_transform(X=img_od.T).T

        # order H and E.
        # H on first row.
        dictionary = dl_output_for_h_and_e(dictionary)

        return dictionary / (np.linalg.norm(dictionary, axis=1)[:, None] +
                             sys.float_info.epsilon)
Esempio n. 10
0
S /= S.std(axis=0)  # Standardize data
# Mix data
A = np.array([[1, 1, 1], [0.5, 2, 1.0], [1.5, 1.0, 2.0]])  # Mixing matrix
X = np.dot(S, A.T)  # Generate observations

# Compute ICA
ica = FastICA(n_components=3)
S_ = ica.fit_transform(X)  # Reconstruct signals
A_ = ica.mixing_  # Get estimated mixing matrix
# For comparison, compute PCA
pca = PCA(n_components=3)
H = pca.fit_transform(X)  # Reconstruct signals based on orthogonal components
print("PCA:", np.sum(H[:, 0]*H[:, 1]))
# Compute DL
dl = DictionaryLearning(n_components=3)
D = dl.fit_transform(X)  # Reconstruct signals based on orthogonal components

# #############################################################################
# Plot results
print("Ploting...")
plt.figure()


models = [X, S, S_, H, D]
names = ['Observations (mixed signal)',
         'True Sources',
         'ICA recovered signals',
         'PCA recovered signals',
         'DL recovered signals']
colors = ['red', 'steelblue', 'orange', 'blue']
plt.figure(1)
Esempio n. 11
0
class SC(object):
    """
    Wrapper for sklearn package.  Performs sparse coding

    Sparse Coding, or Dictionary Learning has 5 methods:
       - fit(waveforms)
       update class instance with Sparse Coding fit

       - fit_transform()
       do what fit() does, but additionally return the projection onto new basis space

       - inverse_transform(A)
       inverses the decomposition, returns waveforms for an input A, using Z^\dagger

       - get_basis()
       returns the basis vectors Z^\dagger

       - get_params()
       returns metadata used for fits.
    """
    def __init__(self, num_components=10,
                 catalog_name='unknown',
                 alpha = 0.001,
                 transform_alpha = 0.01,
                 max_iter = 2000,
                 tol = 1e-9,
                 n_jobs = 1,
                 verbose = True,
                 random_state = None):

        self._decomposition   = 'Sparse Coding'
        self._num_components  = num_components
        self._catalog_name    = catalog_name
        self._alpha           = alpha
        self._transform_alpha = 0.001
        self._n_jobs          = n_jobs
        self._random_state    = random_state

        self._DL = DictionaryLearning(n_components=self._num_components,
                              alpha           = self._alpha,
                              transform_alpha = self._transform_alpha,
                              n_jobs          = self._n_jobs,
                              verbose         = verbose,
                              random_state    = self._random_state)

    def fit(self,waveforms):
        # TODO make sure there are more columns than rows (transpose if not)
        # normalize waveforms
        self._waveforms = waveforms
        self._DL.fit(self._waveforms)

    def fit_transform(self,waveforms):
        # TODO make sure there are more columns than rows (transpose if not)
        # normalize waveforms
        self._waveforms = waveforms
        self._A = self._DL.fit_transform(self._waveforms)
        return self._A

    def inverse_transform(self,A):
        # convert basis back to waveforms using fit
        new_waveforms = self._DL.inverse_transform(A)
        return new_waveforms

    def get_params(self):
        # TODO know what catalog was used! (include waveform metadata)
        params = self._DL.get_params()
        params['num_components'] = params.pop('n_components')
        params['Decompositon'] = self._decomposition
        return params

    def get_basis(self):
        """ Return the SPCA basis vectors (Z^\dagger)"""
        return self._DL.components_
Esempio n. 12
0
def get_representations(graph, rep_method, verbose = True, return_rep_method = False, nodes_to_embed = None):
	if rep_method.method == "degseqs" and rep_method.p is not None:
		graph.max_degree = rep_method.p #so that we can get degseqs of an arbitrary length (basically by padding with 0s)

	if verbose:
		print "Until layer: ", rep_method.max_layer
		print "sampling method: ", rep_method.sampling_method
		print "attribute class sizes: ", graph.attribute_class_sizes
	
	#Explicitly featurize the nodes
	feature_matrix = get_features(graph, rep_method, verbose, nodes_to_embed)
	if graph.directed == False:
		feature_matrix = np.hstack((feature_matrix, feature_matrix))
	if graph.directed:
		# print "got outdegree degseqs, now getting indegree..."
		indegree_graph = Graph(graph.G_adj.T, weighted = graph.weighted, signed = graph.signed, directed = graph.directed, node_features = graph.node_features)
		indegree_feature_matrix = get_features(indegree_graph, rep_method, verbose, nodes_to_embed)
# 		print feature_matrix.shape
# 		print indegree_feature_matrix.shape
		feature_matrix = np.hstack((np.dot(feature_matrix, 1), np.dot(indegree_feature_matrix, 17)))

	if verbose:
		print "Dimensionality in explicit feature space: ", feature_matrix.shape
	
	if rep_method.method == "degseqs": #want explicit featurization of nodes by deg sequences of neighbors
		if verbose:
			print "returning explicit features"
		return feature_matrix

	
	#Get landmark nodes
	if rep_method.p is None:
		rep_method.p = get_feature_dimensionality(graph, rep_method, verbose = verbose) #k*log(n), where k = 10
	elif rep_method.p > graph.N or (nodes_to_embed is not None and rep_method.p > len(nodes_to_embed)): 
# 		print "Warning: dimensionality greater than number of nodes to embed. Reducing to n"
		if (nodes_to_embed is not None and rep_method.p > len(nodes_to_embed)):
			rep_method.p = len(nodes_to_embed)
		else:
			rep_method.p = graph.N

	
	landmarks = get_sample_nodes(graph, rep_method, verbose = verbose, nodes_to_embed = nodes_to_embed)
	if verbose:
		print "landmark IDs: ", landmarks
		print feature_matrix.shape


	rep_method.landmarks = feature_matrix[landmarks]
	rep_method.landmark_indices = landmarks


	
	if nodes_to_embed is None:
		nodes_to_embed = range(graph.N)
		num_nodes = graph.N
	else:
		num_nodes = len(nodes_to_embed)

	C = np.zeros((num_nodes,rep_method.p))
	for graph_node in range(num_nodes):#nodes_to_embed:
		for landmark_node in range(rep_method.p):
			if rep_method.method == "struc2vec":
				#use struc2vec similarities
				C[graph_node,landmark_node] = struc2vec_sim_matrix[graph_node, landmarks[landmark_node]]
			else:
				if rep_method.landmarks is not None: #landmarks are hard coded as actual features of landmark nodes
					landmark_node_features = rep_method.landmarks[landmark_node]
				else:
					landmark_node_features = feature_matrix[landmarks[landmark_node]] #landmarks are indices of landmark nodes
				C[graph_node,landmark_node] = compute_similarity(graph, rep_method, feature_matrix[graph_node], 
					landmark_node_features, graph.node_attributes, (graph_node, landmark_node), attribute_class_sizes = graph.attribute_class_sizes)


	if rep_method.landmarks is None or not rep_method.use_landmarks:
		W_pinv = np.linalg.pinv(C[landmarks]) #make W have same entries for aligning nodes?
	else: #we have hard coded landmarks, compute their pairwise similarities
		W = np.zeros((rep_method.landmarks.shape[0], rep_method.landmarks.shape[0]))
		for i in range(W.shape[0]):
			for j in range(W.shape[1]): #NOTE based on structure only for now
				W[i,j] = compute_similarity(graph, rep_method, rep_method.landmarks[i], rep_method.landmarks[j])
		W_pinv = np.linalg.pinv(W)

    #Nystrom approximation: S'  = CWC^T /approx S, the full nxn similarity matrix
    #xnetmf: rank-p factorization of similarity matrix /approx skipgram:
    #YZ /approx S ==> Y as learned by struc2vec
    
    #Take rank-k factorization of S' to approximate skipgram embeddings
    #But Csqrt(W) * sqrt(W)C^T = S', and Csqrt(W) is rank k
    #So take Csqrt(W) as our representation
	if rep_method.implicit_factorization:
		# if np.linalg.matrix_rank(C[landmarks]) == C[landmarks].shape[0]:
		# 	# print ':)'
		# 	reprsn = np.dot(C, sp.linalg.sqrtm(W_pinv))
		# else:
		if verbose:
			print("W is singular: rank %d vs size %d" % (np.linalg.matrix_rank(C[landmarks]), C[landmarks].shape[0])), C[landmarks]
		U,X,V = np.linalg.svd(W_pinv)
		# dim = U_f.shape[1]
		# K = min(dim, 24)
		# print K
		# print X_f
		# U = U_f[:K]
		sqrtW_substitute = np.dot(U, np.diag(np.sqrt(X)))
		reprsn = np.dot(C, sqrtW_substitute) #make representation have same entries for aligning nodes?

	else:
		Sapprox = np.dot(np.dot(C, W_pinv), C.T)
		print "created similarity matrix"

		#doesn't work since low rank approx can have negative values
		#nmf = NMF(n_components = W_pinv.shape[0], init='random', random_state=0)
		nmf = DictionaryLearning(n_components = W_pinv.shape[0], random_state=0)
		reprsn = nmf.fit_transform(Sapprox)	
		print "...and factorized it"
		reprsn = reprsn.todense()

	if rep_method.normalize:
# 		print("normalizing...")
		norms = np.linalg.norm(reprsn, axis = 1).reshape((reprsn.shape[0],1))
		norms[norms == 0] = 1 #TODO figure out why getting zeros representation
		reprsn = reprsn / norms

	if return_rep_method:
		return reprsn, rep_method
	return reprsn
Esempio n. 13
0
#!/usr/bin/python
# -*- coding: utf-8 -*-
#[email protected]
"""
字典学习
"""
print(__doc__)

import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
from sklearn.decomposition import DictionaryLearning
mpl.style.use('fivethirtyeight')
from sklearn.datasets import make_circles

np.random.seed(0)

X, y = make_circles(n_samples=400, factor=.3, noise=.05)

pca = DictionaryLearning(n_components=2)
X_pca = pca.fit_transform(X)

fig = plt.figure()
ax = fig.add_subplot(211)
ax.scatter(X[:, 0], X[:, 1], c=y)
ax.axis("equal")
ax = fig.add_subplot(212)
ax.scatter(X_pca[:, 0], X_pca[:, 1], c=y)

ax.axis("equal")
plt.show()
data = data['ftdata_NLM']
temp = data[LR_flag, :]
m = np.mean(temp, 1)
temp = temp - m[:, None]
s = np.std(temp, 1) + 1e-16
temp = temp / s[:, None]
msk_small_region = (dfs_left.labels == 46) | (dfs_left.labels == 28) | (
    dfs_left.labels == 29)  # % motor
d = temp[msk_small_region, :]
d_corr = temp[~msk_small_region, :]
rho = np.corrcoef(d, d_corr)
rho = rho[range(d.shape[0]), d.shape[0] + 1:]
rho[~np.isfinite(rho)] = 0

DL = DictionaryLearning(n_components=8)
labs = DL.fit_transform(rho)
print(labs.shape)
labs = labs.argmax(1)

r = dfs_left_sm
r.labels = r.labels * 0
r.labels[msk_small_region] = labs
mesh = mlab.triangular_mesh(r.vertices[:, 0],
                            r.vertices[:, 1],
                            r.vertices[:, 2],
                            r.faces,
                            representation='surface',
                            opacity=1,
                            scalars=np.float64(r.labels))
#mlab.pipeline.surface(mesh)
mlab.gcf().scene.parallel_projection = True
Esempio n. 15
0
# Target Domain
dict_feat = [None] * 30
for subs in range(30):
    print(subs)

    feat = features[0, subs]

    #dict_sparse = DictionaryLearning(alpha=0.1, n_components=105, max_iter=10, transform_n_nonzero_coefs=105, verbose=3)
    #dict_sparse = SparsePCA(n_components=105, max_iter=3)
    #dict_sparse = MiniBatchDictionaryLearning(alpha=1, n_components=105, batch_size=10, n_iter=100)
    #dict_sparse.fit(feat)
    #Dt_0 = dict_sparse.components_
    #coder = SparseCoder(dictionary = Dt_0, transform_n_nonzero_coefs=105)
    #Rt_0 = coder.transform(feat)
    dict_sparse = SparsePCA(alpha=1, n_components=105, max_iter=20, verbose=3)
    Rt_0 = dict_sparse.fit_transform(feat)

    dict_feat[subs] = Rt_0

target_folder = 'C:\\Users\\Pouya\\Documents\\MATLAB\\DECAF\\Analysis\\Movie_Genre_adaptation\\feats_trans2.mat'
sio.savemat(target_folder, {'dict_feat': dict_feat, 'movie_feat': Rs_0})

## Music Genre classification
source_folder = 'C:\\Users\\Pouya\\Documents\\MATLAB\\DECAF\\Analysis\\MusicGenreClassification\\feats.mat'
dict = sio.loadmat(source_folder)
MCA_Ft = dict['MCA_Ft']
MEG_Ft = dict['MEG_Ft']
EEG_Ft = dict['EEG_Ft']

# Source Domain
cmp_num = 4
    def btnConvert_click(self):
        msgBox = QMessageBox()


        Fit = ui.cbFit.currentText()

        Transform = ui.cbTransform.currentText()

        # Tol
        try:
            Tol = np.float(ui.txtTole.text())
        except:
            msgBox.setText("Tolerance is wrong!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        # MaxIte
        try:
            MaxIter = np.int32(ui.txtMaxIter.text())
        except:
            msgBox.setText("Maximum number of iterations is wrong!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        if MaxIter < 1:
            msgBox.setText("Maximum number of iterations is wrong!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        # Alpha
        try:
            Alpha = np.float(ui.txtAlpha.text())
        except:
            msgBox.setText("Alpha is wrong!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        # Number of Job
        try:
            NJob = np.int32(ui.txtJobs.text())
        except:
            msgBox.setText("The number of parallel jobs is wrong!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        if NJob < 1:
            msgBox.setText("The number of parallel jobs must be greater than 1!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        # OutFile
        OutFile = ui.txtOutFile.text()
        if not len(OutFile):
            msgBox.setText("Please enter out file!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        # InFile
        InFile = ui.txtInFile.text()
        if not len(InFile):
            msgBox.setText("Please enter input file!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        if not os.path.isfile(InFile):
            msgBox.setText("Input file not found!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        if ui.rbScale.isChecked() == True and ui.rbALScale.isChecked() == False:
            msgBox.setText("Subject Level Normalization is just available for Subject Level Analysis!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        InData = io.loadmat(InFile)
        OutData = dict()
        OutData["imgShape"] = InData["imgShape"]

        if not len(ui.txtData.currentText()):
            msgBox.setText("Please enter Data variable name!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        try:
            X = InData[ui.txtData.currentText()]

            if ui.cbScale.isChecked() and (not ui.rbScale.isChecked()):
                X = preprocessing.scale(X)
                print("Whole of data is scaled X~N(0,1).")
        except:
            print("Cannot load data")
            return

        try:
            NumFea = np.int32(ui.txtNumFea.text())
        except:
            msgBox.setText("Number of features is wrong!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False
        if NumFea < 1:
            msgBox.setText("Number of features must be greater than zero!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        if NumFea > np.shape(X)[1]:
            msgBox.setText("Number of features is wrong!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        # Subject
        if not len(ui.txtSubject.currentText()):
            msgBox.setText("Please enter Subject variable name!")
            msgBox.setIcon(QMessageBox.Critical)
            msgBox.setStandardButtons(QMessageBox.Ok)
            msgBox.exec_()
            return False

        try:
            Subject = InData[ui.txtSubject.currentText()]
            OutData[ui.txtOSubject.text()] = Subject
        except:
            print("Cannot load Subject ID")
            return

        # Label
        if not len(ui.txtLabel.currentText()):
                msgBox.setText("Please enter Label variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
        OutData[ui.txtOLabel.text()] = InData[ui.txtLabel.currentText()]


        # Task
        if ui.cbTask.isChecked():
            if not len(ui.txtTask.currentText()):
                msgBox.setText("Please enter Task variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            OutData[ui.txtOTask.text()] = InData[ui.txtTask.currentText()]

        # Run
        if ui.cbRun.isChecked():
            if not len(ui.txtRun.currentText()):
                msgBox.setText("Please enter Run variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            OutData[ui.txtORun.text()] = InData[ui.txtRun.currentText()]


        # Counter
        if ui.cbCounter.isChecked():
            if not len(ui.txtCounter.currentText()):
                msgBox.setText("Please enter Counter variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            OutData[ui.txtOCounter.text()] = InData[ui.txtCounter.currentText()]




        # Matrix Label
        if ui.cbmLabel.isChecked():
            if not len(ui.txtmLabel.currentText()):
                msgBox.setText("Please enter Matrix Label variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            OutData[ui.txtOmLabel.text()] = InData[ui.txtmLabel.currentText()]


        # Design
        if ui.cbDM.isChecked():
            if not len(ui.txtDM.currentText()):
                msgBox.setText("Please enter Design Matrix variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            OutData[ui.txtODM.text()] = InData[ui.txtDM.currentText()]

        # Coordinate
        if ui.cbCol.isChecked():
            if not len(ui.txtCol.currentText()):
                msgBox.setText("Please enter Coordinator variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            OutData[ui.txtOCol.text()] = InData[ui.txtCol.currentText()]

        # Condition
        if ui.cbCond.isChecked():
            if not len(ui.txtCond.currentText()):
                msgBox.setText("Please enter Condition variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            OutData[ui.txtOCond.text()] = InData[ui.txtCond.currentText()]

        # Number of Scan
        if ui.cbNScan.isChecked():
            if not len(ui.txtScan.currentText()):
                msgBox.setText("Please enter Number of Scan variable name!")
                msgBox.setIcon(QMessageBox.Critical)
                msgBox.setStandardButtons(QMessageBox.Ok)
                msgBox.exec_()
                return False
            OutData[ui.txtOScan.text()] = InData[ui.txtScan.currentText()]

        Models = dict()
        Models["Name"] = "DictionaryLearning"

        if ui.rbALScale.isChecked():
            print("Partition data to subject level ...")
            SubjectUniq = np.unique(Subject)
            X_Sub = list()
            for subj in SubjectUniq:
                if ui.cbScale.isChecked() and ui.rbScale.isChecked():
                    X_Sub.append(preprocessing.scale(X[np.where(Subject == subj)[1], :]))
                    print("Data in subject level is scaled, X_" + str(subj) + "~N(0,1).")
                else:
                    X_Sub.append(X[np.where(Subject == subj)[1],:])
                print("Subject ", subj, " is extracted from data.")

            print("Running Dictionary Learning in subject level ...")
            X_Sub_PCA = list()
            lenPCA    = len(X_Sub)

            for xsubindx, xsub in enumerate(X_Sub):
                model = DictionaryLearning(n_components=NumFea,alpha=Alpha,max_iter=MaxIter,\
                                           tol=Tol,fit_algorithm=Fit,transform_alpha=Transform,n_jobs=NJob)
                X_Sub_PCA.append(model.fit_transform(xsub))
                Models["Model" + str(xsubindx + 1)] = str(model.get_params(deep=True))

                print("Dictionary Learning: ", xsubindx + 1, " of ", lenPCA, " is done.")

            print("Data integration ... ")
            X_new = None
            for xsubindx, xsub in enumerate(X_Sub_PCA):
                X_new = np.concatenate((X_new, xsub)) if X_new is not None else xsub
                print("Integration: ", xsubindx + 1, " of ", lenPCA, " is done.")
            OutData[ui.txtOData.text()] = X_new
        else:
            print("Running Dictionary Learning ...")
            model = DictionaryLearning(n_components=NumFea, alpha=Alpha, max_iter=MaxIter, \
                                   tol=Tol, fit_algorithm=Fit, transform_alpha=Transform, n_jobs=NJob)
            OutData[ui.txtOData.text()] = model.fit_transform(X)
            Models["Model"] = str(model.get_params(deep=True))

        OutData["ModelParameter"] = Models

        print("Saving ...")
        io.savemat(ui.txtOutFile.text(), mdict=OutData)
        print("DONE.")
        msgBox.setText("Dictionary Learning is done.")
        msgBox.setIcon(QMessageBox.Information)
        msgBox.setStandardButtons(QMessageBox.Ok)
        msgBox.exec_()
Esempio n. 17
0
class SC(object):
    """
    Wrapper for sklearn package.  Performs sparse coding

    Sparse Coding, or Dictionary Learning has 5 methods:
       - fit(waveforms)
       update class instance with Sparse Coding fit

       - fit_transform()
       do what fit() does, but additionally return the projection onto new basis space

       - inverse_transform(A)
       inverses the decomposition, returns waveforms for an input A, using Z^\dagger

       - get_basis()
       returns the basis vectors Z^\dagger

       - get_params()
       returns metadata used for fits.
    """
    def __init__(self,
                 num_components=10,
                 catalog_name='unknown',
                 alpha=0.001,
                 transform_alpha=0.01,
                 max_iter=2000,
                 tol=1e-9,
                 n_jobs=1,
                 verbose=True,
                 random_state=None):

        self._decomposition = 'Sparse Coding'
        self._num_components = num_components
        self._catalog_name = catalog_name
        self._alpha = alpha
        self._transform_alpha = 0.001
        self._n_jobs = n_jobs
        self._random_state = random_state

        self._DL = DictionaryLearning(n_components=self._num_components,
                                      alpha=self._alpha,
                                      transform_alpha=self._transform_alpha,
                                      n_jobs=self._n_jobs,
                                      verbose=verbose,
                                      random_state=self._random_state)

    def fit(self, waveforms):
        # TODO make sure there are more columns than rows (transpose if not)
        # normalize waveforms
        self._waveforms = waveforms
        self._DL.fit(self._waveforms)

    def fit_transform(self, waveforms):
        # TODO make sure there are more columns than rows (transpose if not)
        # normalize waveforms
        self._waveforms = waveforms
        self._A = self._DL.fit_transform(self._waveforms)
        return self._A

    def inverse_transform(self, A):
        # convert basis back to waveforms using fit
        new_waveforms = self._DL.inverse_transform(A)
        return new_waveforms

    def get_params(self):
        # TODO know what catalog was used! (include waveform metadata)
        params = self._DL.get_params()
        params['num_components'] = params.pop('n_components')
        params['Decompositon'] = self._decomposition
        return params

    def get_basis(self):
        """ Return the SPCA basis vectors (Z^\dagger)"""
        return self._DL.components_
Esempio n. 18
0
conn_vec = np.array(conn_vec).T

conn_vec_ASD = conn_vec[:, ASD_index]
conn_vec_TD = conn_vec[:, TD_index]
conn_vec_sorted = np.concatenate((conn_vec_ASD, conn_vec_TD), axis=1)
print('ASD : ', conn_vec_ASD.shape, '  ', 'TD : ', conn_vec_TD.shape, '  ',
      'conn_vec : ', conn_vec_sorted.shape)
np.save(savepath)

# # Sparse Dictrionary Learning
from sklearn.decomposition import DictionaryLearning

SDL = DictionaryLearning(n_components=100, alpha=100)

Dict_ASD = SDL.fit_transform(conn_vec_ASD)
repres_ASD = SDL.components_

Dict_TD = SDL.fit_transform(conn_vec_TD)
repres_TD = SDL.components_

from sklearn.decomposition import DictionaryLearning

SDL = DictionaryLearning(n_components=15, alpha=13)

Dict_total = SDL.fit_transform(conn_vec_sorted)
repres_total = SDL.components_
print('Dictionary ASD : ', Dict_ASD.shape, '   ', 'Dictionary TD : ',
      Dict_TD.shape)
print('Dictionary Total : ', Dict_total.shape)
pca.fit(mov)
#%%
import cv2
comps = np.reshape(pca.components_, [n_comps, 30, 30])
for count, comp in enumerate(comps):
    pl.subplot(4, 4, count + 1)
    blur = cv2.GaussianBlur(comp.astype(np.float32), (5, 5), 0)
    blur = np.array(blur / np.max(blur) * 255, dtype=np.uint8)
    ret3, th3 = cv2.threshold(
        blur, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
    pl.imshow((th3 * comp).T)

#%%
n_comps = 3
dl = DictionaryLearning(n_comps, alpha=1, verbose=True)
comps = dl.fit_transform(Yr.T)
comps = np.reshape(comps, [30, 30, n_comps]).transpose([2, 0, 1])
for count, comp in enumerate(comps):
    pl.subplot(4, 4, count + 1)
    pl.imshow(comp)
#%%
N_ICA_COMPS = 8
ica = FastICA(N_ICA_COMPS, max_iter=10000, tol=10e-8)
ica.fit(pca.components_)
#%
comps = np.reshape(ica.components_, [N_ICA_COMPS, 30, 30])
for count, comp in enumerate(comps):
    idx = np.argmax(np.abs(comp))
    comp = comp * np.sign(comp.flatten()[idx])
    pl.subplot(4, 4, count + 1)
    pl.imshow(comp.T)
import numpy as np
import matplotlib.pyplot as plt

from sklearn.datasets import load_digits
from sklearn.decomposition import DictionaryLearning

# For reproducibility
np.random.seed(1000)

if __name__ == '__main__':
    # Load MNIST digits
    digits = load_digits()

    # Perform a dictionary learning (and atom extraction) from the MNIST dataset
    dl = DictionaryLearning(n_components=36,
                            fit_algorithm='lars',
                            transform_algorithm='lasso_lars')
    X_dict = dl.fit_transform(digits.data)

    # Show the atoms that have been extracted
    fig, ax = plt.subplots(6, 6, figsize=(8, 8))

    samples = [dl.components_[x].reshape((8, 8)) for x in range(34)]

    for i in range(6):
        for j in range(6):
            ax[i, j].set_axis_off()
            ax[i, j].imshow(samples[(i * 5) + j], cmap='gray')

    plt.show()
Esempio n. 21
0
#array([-2.20719466, -3.16170819, -4.11622173])


tsvd = TruncatedSVD(2)
tsvd.fit(iris_data)
tsvd.transform(iris_data)

#One advantage of TruncatedSVD over PCA is that TruncatedSVD can operate on sparse
#matrices while PCA cannot


#Decomposition分解 to classify分类 with DictionaryLearning

from sklearn.decomposition import DictionaryLearning
dl = DictionaryLearning(3)
transformed = dl.fit_transform(iris_data[::2])
transformed[:5]
#array([[ 0. , 6.34476574, 0. ],
#[ 0. , 5.83576461, 0. ],
#[ 0. , 6.32038375, 0. ],
#[ 0. , 5.89318572, 0. ],
#[ 0. , 5.45222715, 0. ]])

#Next, let's fit (not fit_transform) the testing set:
transformed = dl.transform(iris_data[1::2])


#Putting it all together with Pipelines

#Let's briefly load the iris dataset and seed it with some missing values:
from sklearn.datasets import load_iris
y_test = y[1::2]

# 確認
X_train.shape
X_test.shape

# 1 辞書学習の実行 --------------------------------------------------------------------------------

# インスタンス生成
# --- 3種類のアヤメの花を表すため3を指定
dl = DictionaryLearning(n_components=3)
vars(dl)

# 学習
# --- データ全体でなく訓練データで学習
transformed = dl.fit_transform(X_train)
transformed[:5]

# テストデータの変換
test_transform = dl.fit_transform(X_test)
test_transform[:5]

# 2 学習結果の可視化 --------------------------------------------------------------------------------

# プロット設定
fig = plt.figure(figsize=(14, 7))

# 訓練データ
ax = fig.add_subplot(121, projection='3d')
ax.scatter(transformed[:, 0],
           transformed[:, 1],