Пример #1
0
def test_mcanode_v1():
    line_x = numx.zeros((1000, 2), "d")
    line_y = numx.zeros((1000, 2), "d")
    line_x[:, 0] = numx.linspace(-1, 1, num=1000, endpoint=1)
    line_y[:, 1] = numx.linspace(-0.2, 0.2, num=1000, endpoint=1)
    mat = numx.concatenate((line_x, line_y))
    utils.rotate(mat, uniform() * 2 * numx.pi)
    mat += uniform(2)
    mat -= mat.mean(axis=0)
    mca = MCANode()
    for i in xrange(5):
        mca.train(mat)

    bpca = PCANode()
    bpca.train(mat)
    bpca.stop_training()

    v = mca.get_projmatrix()
    bv = bpca.get_projmatrix()[:, ::-1]

    dcosines = numx.zeros(v.shape[1])
    for dim in xrange(v.shape[1]):
        dcosines[dim] = numx.fabs(numx.dot(v[:, dim], bv[:, dim].T)) / (
            numx.linalg.norm(v[:, dim]) * numx.linalg.norm(bv[:, dim]))
    assert_almost_equal(numx.ones(v.shape[1]), dcosines)
Пример #2
0
def doPCA(in_pts, D, tol=.8):
    pts=copy(in_pts)
    pts.shape = (len(in_pts),D)
    p=PCANode(output_dim=tol)
    p.train(pts)
    p.stop_training()
    return p
    def extract_master_keyword(self, title, article):
        assert article != None, 'article is invalid'
        assert len(article) > 0, 'article is invalid'

        title_morpheme_list = self._morpheme_analyzer.analyze_morpheme(title)
        title_keywords = [
            morpheme.morpheme for morpheme in title_morpheme_list
        ]

        article_morpheme_list = self._morpheme_analyzer.analyze_morpheme(
            article)
        #article_morpheme_list = filter(lambda x:x.frequency > 1, article_morpheme_list)

        article_matrix = self._create_article_matrix(article,
                                                     article_morpheme_list)

        if not isinstance(article_matrix, ndarray):
            return None

        pca = PCANode(svd=True)
        try:
            pca.execute(article_matrix)
        except Exception, e:
            print str(e), title
            logging.exception(
                'Exception raised in method extract_master_keyword with article title='
                + title)
Пример #4
0
def pre_processing(spks, ndet, tf, pca_dim=4):
    print 'starting to prewhiten w.r.t. noise..',
    spks_prw = sp.dot(spks, ndet.get_whitening_op(tf=tf))
    print 'done.'

    print 'pca projection: %s' % pca_dim,
    spks_pca = PCANode(output_dim=pca_dim)(spks_prw)
    print 'done.'

    return spks_pca
Пример #5
0
 def _train(self, data, label=None):
     """ Updates the estimated covariance matrix based on *data*. """
     # We simply ignore the class label since we
     # are doing unsupervised learning
     if self.wrapped_node is None:
         self.wrapped_node = PCANode(svd=self.svd, reduce=self.reduce)
     x = 1.0 * data.view(type=numpy.ndarray)
     self.wrapped_node.train(x)
     if self.channel_names is None:
         self.channel_names = data.channel_names
Пример #6
0
def pca(multidim_data, output_dim):
    """Principal Component Analysis"""
    pcanode = PCANode(input_dim=multidim_data.shape[1],
                      output_dim=output_dim,
                      dtype=np.float32,
                      svd=True,
                      reduce=True,
                      var_rel=1E-15,
                      var_abs=1E-15)
    pcanode.train(data)
    return np.dot(multidim_data, pcanode.get_projmatrix())
Пример #7
0
def bench_mdp(X, y, T, valid):
    #
    #       .. MDP ..
    #
    from mdp.nodes import PCANode
    start = datetime.now()
    clf = PCANode(output_dim=n_components)
    clf.train(X)
    clf.stop_training()
    delta = datetime.now() - start
    ev = explained_variance(X, clf.v.T).sum()
    return ev, delta
Пример #8
0
def PCA():
	col1 = np.array([[1.0, 2, 3, 4, 5, 6, 7, 8, 9]]).T
	col2 = np.array([[2.0, 4, 6, 8, 10, 12, 14, 16, 18]]).T
	matr12 = np.hstack((col1, col2))
	matr_arr = [matr12]
	pca_node = PCANode()
	d_arr = []

	for arr in matr_arr:
		result = pca_node.execute(arr)
		d_arr.append(pca_node.d)
		print pca_node.d
Пример #9
0
def test_mcanode_v2():
    iterval = 30
    t = numx.linspace(0, 4 * numx.pi, 500)
    x = numx.zeros([t.shape[0], 2])
    x[:, 0] = numx.real(numx.sin(t) + numx.power(numx.cos(11 * t), 2))
    x[:, 1] = numx.cos(11 * t)
    expnode = PolynomialExpansionNode(2)
    input_data = expnode(x)
    input_data = input_data - input_data.mean(axis=0)
    wtnnode = WhiteningNode()
    input_data = wtnnode(input_data)
    input_data = mdp.utils.timediff(input_data)

    ##Setup node/trainer
    output_dim = 4
    node = MCANode(output_dim=output_dim, eps=0.05)

    bpcanode = PCANode()
    bpcanode(input_data)
    # bv = bpcanode.v / numx.linalg.norm(bpcanode.v, axis=0)
    bv = bpcanode.v / mdp.numx.sum(bpcanode.v**2, axis=0)**0.5
    bv = bv[:, ::-1][:, :output_dim]

    _tcnt = time.time()

    v = []

    for i in xrange(iterval * input_data.shape[0]):
        node.train(input_data[i % input_data.shape[0]:i % input_data.shape[0] +
                              1])
        if (node.get_current_train_iteration() % 100 == 0):
            v.append(node.v)

    dcosines = numx.zeros([len(v), output_dim])
    for i in xrange(len(v)):
        for dim in xrange(output_dim):
            dcosines[i,
                     dim] = numx.fabs(numx.dot(v[i][:, dim], bv[:, dim].T)) / (
                         numx.linalg.norm(v[i][:, dim]) *
                         numx.linalg.norm(bv[:, dim]))

    print('\nTotal Time for {} iterations: {}'.format(iterval,
                                                      time.time() - _tcnt))
    assert_almost_equal(numx.ones(output_dim), dcosines[-1], decimal=2)
Пример #10
0
def test_ccipcanode_v2():
    iterval = 30
    t = numx.linspace(0, 4 * numx.pi, 500)
    x = numx.zeros([t.shape[0], 2])
    x[:, 0] = numx.real(numx.sin(t) + numx.power(numx.cos(11 * t), 2))
    x[:, 1] = numx.cos(11 * t)
    expnode = PolynomialExpansionNode(2)
    input_data = expnode(x)
    input_data = input_data - input_data.mean(axis=0)

    ##Setup node/trainer
    output_dim = 4
    node = CCIPCANode(output_dim=output_dim)

    bpcanode = PCANode(output_dim=output_dim)
    bpcanode(input_data)
    # bv = bpcanode.v / numx.linalg.norm(bpcanode.v, axis=0)
    bv = bpcanode.v / mdp.numx.sum(bpcanode.v**2, axis=0)**0.5

    v = []

    _tcnt = time.time()
    for i in xrange(iterval * input_data.shape[0]):
        node.train(input_data[i % input_data.shape[0]:i % input_data.shape[0] +
                              1])
        if (node.get_current_train_iteration() % 100 == 0):
            v.append(node.v)

    dcosines = numx.zeros([len(v), output_dim])
    for i in xrange(len(v)):
        for dim in xrange(output_dim):
            dcosines[i,
                     dim] = numx.fabs(numx.dot(v[i][:, dim], bv[:, dim].T)) / (
                         numx.linalg.norm(v[i][:, dim]) *
                         numx.linalg.norm(bv[:, dim]))

    print
    print 'Total Time for %d iterations: ' % (iterval), time.time() - _tcnt
    assert_almost_equal(numx.ones(output_dim), dcosines[-1], decimal=3)
Пример #11
0
    def ssort_key_on(self, ev):
        self._key = k = ev.key  # keep record of last keypress
        fig_struct, fig = self.plotter._resolve_fig(None)

        class_keys = ['1', '2', '3', '0']

        if k in class_keys:
            if isinstance(self.selected_object, box_GUI):
                for dname, dstruct in fig_struct['layers']['scores'][
                        'data'].items():
                    if self.selected_object.x1 < dstruct['data'][0] < self.selected_object.x2 and \
                    self.selected_object.y1 < dstruct['data'][1] < self.selected_object.y2:
                        if k == '1':
                            self.default_colors[dname] = 'r'
                            self.plotter.set_data_2(dname,
                                                    layer='detected',
                                                    style='r-')
                            self.plotter.set_data_2(dname,
                                                    layer='scores',
                                                    style='r*')
                        if k == '2':
                            self.default_colors[dname] = 'g'
                            self.plotter.set_data_2(dname,
                                                    layer='detected',
                                                    style='g-')
                            self.plotter.set_data_2(dname,
                                                    layer='scores',
                                                    style='g*')

                        if k == '3':
                            self.default_colors[dname] = 'b'
                            self.plotter.set_data_2(dname,
                                                    layer='detected',
                                                    style='b-')
                            self.plotter.set_data_2(dname,
                                                    layer='scores',
                                                    style='b*')

                        if k == '0':
                            self.default_colors[dname] = 'k'
                            self.plotter.set_data_2(dname,
                                                    layer='detected',
                                                    style='k-')
                            self.plotter.set_data_2(dname,
                                                    layer='scores',
                                                    style='k*')

            self.plotter.show()

        if k == 'd':
            try:
                self.crosses
            except AttributeError:
                print(
                    "Can't detect spikes until threshold crossings have been found."
                )
                return

            self.X = self.compute_bbox()

            self.default_colors = {}

            if len(self.X.shape) == 1:
                self.default_colors['spike0'] = 'k'
                self.add_data_points([list(range(0, len(self.X))), self.X],
                                     layer='detected',
                                     style=self.default_colors['spike0'] + '-',
                                     name='spike0',
                                     force=True)

            else:
                c = 0
                for spike in self.X:
                    name = 'spike' + str(c)
                    self.default_colors[name] = 'k'
                    self.add_data_points([list(range(0, len(spike))), spike],
                                         layer='detected',
                                         style=self.default_colors[name] + '-',
                                         name=name,
                                         force=True)
                    c += 1

            self.plotter.auto_scale_domain(xcushion=0, subplot='12')
            self.show()

            if self.tutorial == 'step3':
                print("STEP 3: ")
                print(
                    "You can now press 'p' to perform PCA on the detected spikes."
                )
                print(
                    "The bottom right subplot will display the first 3 principal components (in red, green, and yellow respectively.)"
                )
                print(
                    "The bottom left subplot will show the detected spikes projected onto the first two PCs"
                )
                self.tutorial = 'step4'

        if k == 'p':
            try:
                X = self.X
            except AttributeError:
                print('Must detect spikes before performing PCA.')
                return

            print('doing PCA...')

            self.p = PCANode(output_dim=0.99, reduce=True, svd=True)
            self.p.train(X)
            self.proj_vec1 = self.p.get_projmatrix()[:, 0]
            self.proj_vec2 = self.p.get_projmatrix()[:, 1]

            self.add_data_points(
                [list(range(0, len(self.proj_vec1))), self.proj_vec1],
                style='r-',
                layer='pcs',
                name='firstPC',
                force=True)
            self.add_data_points(
                [list(range(0, len(self.proj_vec2))), self.proj_vec2],
                style='g-',
                layer='pcs',
                name='secondPC',
                force=True)

            self.plotter.show()
            self.proj_PCs = ['firstPC', 'secondPC']

            try:
                self.add_data_points([
                    list(range(0, len(self.p.get_projmatrix()))),
                    self.p.get_projmatrix()[:, 2]
                ],
                                     style='y--',
                                     layer='pcs',
                                     name='thirdPC',
                                     force=True)
            except IndexError:
                pass

            self.add_legend(['r', 'g', 'y'], ['1st PC', '2nd PC', '3rd PC'],
                            '21')

            self.plotter.auto_scale_domain(xcushion=0, subplot='21')
            self.show()

            self.project_to_PC()

            if self.tutorial == 'step4':
                print("STEP 4: ")
                print("Use mouse clicks to explore the data.")
                print(
                    "Clicking on detected spikes in the top-right will highlight the corresponding projection in the bottom right (and vice versa)."
                )
                print(
                    "You can also change the set of PCs onto which the data are projected by clicking the desired projection PCs in the bottom left"
                )

                print("NOTE ALSO: ")
                print(
                    "Creating a bounding box in the upper-left plot and renaming it to 'ref_box', will change the search width of the detected spike."
                )
                print(
                    "e.g., if you want detected spikes to be 30 msec long, the box's .dx value must be 30."
                )
                print(
                    "After creating the box, it will be set to the current selected object. You can select the thresh line again by clicking on it."
                )