Beispiel #1
0
def test_reconstruct_with_without_rowvar():
    """Tests the reconstruct works with rowvar correctly."""
    np.random.seed(1)
    data = np.random.normal(size=(10, 50))
    pc1 = PrincipalComponents(data)
    pc2 = PrincipalComponents(data.T, rowvar=True)
    pc1._direct()
    pc2._direct()
    check_cols_match_except_sign(pc1.reconstruct(pc1.project(3)), pc2.reconstruct(pc2.project(3)).T)
Beispiel #2
0
def test_project_with_without_rowvar():
    """Tests the project works with rowvar correctly."""
    np.random.seed(1)
    data = np.random.normal(size=(10, 50))
    pc1 = PrincipalComponents(data)
    pc2 = PrincipalComponents(data.T, rowvar=True)
    pc1._direct()
    pc2._direct()
    check_cols_match_except_sign(pc1.project(3), pc2.project(3).T)
    check_cols_match_except_sign(pc1.project(3), pc2.project(3).T)
Beispiel #3
0
 def __init__(self, npc=None, *args, **keywords):
     self._pc = PrincipalComponents(data)
     self._pc._direct()
     if npc is None:
         npc = len(self._pc._eigval)
     data = self._pc.project(npc)
     super(SplineModelPCAGaussianMixture, self).__init__(ncomponent,
                                                         data,
                                                         norm=False,
                                                         *args,
                                                         **keywords)
Beispiel #4
0
class SplineModelPCAGaussianMixture(GaussianMixture):
    """docstring for SplineModelGaussianMixture"""
    def __init__(self, npc=None, *args, **keywords):
        self._pc = PrincipalComponents(data)
        self._pc._direct()
        if npc is None:
            npc = len(self._pc._eigval)
        data = self._pc.project(npc)
        super(SplineModelPCAGaussianMixture, self).__init__(ncomponent,
                                                            data,
                                                            norm=False,
                                                            *args,
                                                            **keywords)

    def display(self,
                covariance=None,
                logalpha=None,
                name=None,
                extra=1,
                k=3,
                figures=(1, 2)):
        """Display covariances and alphas with matplotlib."""
        pyplot.close('all')
        super(SplineModelPCAGaussianMixture,
              self).display(covariance, logalpha, name, figures[0])
        figurenum = figures[1]
        if PLOTTING_AVAILABLE:
            print "Okay plotting"
            pyplot.ioff()
            pyplot.figure(figurenum)
            pyplot.clf()
            rows = cols = np.ceil(np.sqrt(self._ncomponent()))
            if rows * cols == self._ncomponent():
                rows = rows + 1
            ncomp, ndim = self._means.shape
            perspline = (ndim - extra) / 2
            for clust in xrange(self._ncomponent()):
                t = internal_knots(coef2knots(perspline))
                t = np.concatenate((np.zeros(4), t, np.ones(4)))
                pyplot.subplot(rows, cols, clust + 1)
                means = self._pc.reconstruct(self._means[clust, :]).squeeze()
                spline1, spline2 = unmix(means, k=k, extra=extra)
                plot_from_spline(spline1)
                plot_from_spline(spline2)
                pyplot.ylim(-0.2, 1.01)
            pyplot.show()
            pyplot.ion()
Beispiel #5
0
def test_direct_with_without_rowvar():
    """Tests the direct method, with and without rowvar, works correctly."""
    np.random.seed(1)
    data = np.random.normal(size=(10, 50))
    pc1 = PrincipalComponents(data)
    pc2 = PrincipalComponents(data.T, rowvar=True)
    pc1._direct()
    pc2._direct()
    check_cols_match_except_sign(pc1._eigvec, pc2._eigvec)
Beispiel #6
0
def test_em_with_without_rowvar():
    """Tests the EM method, with and without rowvar, works correctly."""
    np.random.seed(1)
    data = np.random.normal(size=(20, 50))
    pc1 = PrincipalComponents(data)
    pc2 = PrincipalComponents(data.T, rowvar=True)
    pc1._expectation_maximization(10)
    pc2._expectation_maximization(10)
    check_cols_match_except_sign(pc1._eigvec, pc2._eigvec)
Beispiel #7
0
def test_em_vs_snapshot_pc_rowvar_false():
    """Tests that EM and snapshot agree."""
    np.random.seed(1)
    data = np.random.normal(size=(10, 50))
    pc1 = PrincipalComponents(data)
    pc2 = PrincipalComponents(data)
    pc1._snapshot()
    pc2._expectation_maximization(5)
    check_cols_match_except_sign(pc1._eigvec[:, :5], pc2._eigvec)
Beispiel #8
0
def test_snapshot_vs_direct_pc_rowvar_false():
    """Tests that snapshot and direct agree."""
    np.random.seed(1)
    data = np.random.normal(size=(10, 50))
    pc1 = PrincipalComponents(data)
    pc2 = PrincipalComponents(data)
    pc1._direct()
    pc2._snapshot()
    check_cols_match_except_sign(pc1._eigvec[:, :4], pc2._eigvec[:, :4])
Beispiel #9
0
 def __init__(self, npc=None, *args, **keywords):
     self._pc = PrincipalComponents(data)
     self._pc._direct()
     if npc is None:
         npc = len(self._pc._eigval)
     data = self._pc.project(npc)
     super(SplineModelPCAGaussianMixture, self).__init__(ncomponent, data,
         norm=False, *args, **keywords)
Beispiel #10
0
class SplineModelPCAGaussianMixture(GaussianMixture):
    """docstring for SplineModelGaussianMixture"""
    def __init__(self, npc=None, *args, **keywords):
        self._pc = PrincipalComponents(data)
        self._pc._direct()
        if npc is None:
            npc = len(self._pc._eigval)
        data = self._pc.project(npc)
        super(SplineModelPCAGaussianMixture, self).__init__(ncomponent, data,
            norm=False, *args, **keywords)
    
    def display(self, covariance=None, logalpha=None, name=None, extra=1,
        k=3, figures=(1,2)):
        """Display covariances and alphas with matplotlib."""
        pyplot.close('all')
        super(SplineModelPCAGaussianMixture, self).display(covariance,
            logalpha, name, figures[0])
        figurenum = figures[1]
        if PLOTTING_AVAILABLE:
            print "Okay plotting"
            pyplot.ioff()
            pyplot.figure(figurenum)
            pyplot.clf()
            rows = cols = np.ceil(np.sqrt(self._ncomponent()))
            if rows * cols == self._ncomponent():
                rows = rows + 1
            ncomp, ndim = self._means.shape
            perspline = (ndim - extra) / 2
            for clust in xrange(self._ncomponent()):    
                t = internal_knots(coef2knots(perspline))
                t = np.concatenate((np.zeros(4),t,np.ones(4)))
                pyplot.subplot(rows, cols, clust+1)
                means = self._pc.reconstruct(self._means[clust, :]).squeeze()
                spline1, spline2 = unmix(means,k=k,extra=extra)
                plot_from_spline(spline1)
                plot_from_spline(spline2)
                pyplot.ylim(-0.2, 1.01)
            pyplot.show()
            pyplot.ion()
Beispiel #11
0
def test_direct_with_without_rowvar():
    """Tests the direct method, with and without rowvar, works correctly."""
    np.random.seed(1)
    data = np.random.normal(size=(10, 50))
    pc1 = PrincipalComponents(data)
    pc2 = PrincipalComponents(data.T, rowvar=True)
    pc1._direct()
    pc2._direct()
    check_cols_match_except_sign(pc1._eigvec, pc2._eigvec)
Beispiel #12
0
def test_em_with_without_rowvar():
    """Tests the EM method, with and without rowvar, works correctly."""
    np.random.seed(1)
    data = np.random.normal(size=(20, 50))
    pc1 = PrincipalComponents(data)
    pc2 = PrincipalComponents(data.T, rowvar=True)
    pc1._expectation_maximization(10)
    pc2._expectation_maximization(10)
    check_cols_match_except_sign(pc1._eigvec, pc2._eigvec)
Beispiel #13
0
def test_em_vs_snapshot_pc_rowvar_false():
    """Tests that EM and snapshot agree."""
    np.random.seed(1)
    data = np.random.normal(size=(10, 50))
    pc1 = PrincipalComponents(data)
    pc2 = PrincipalComponents(data)
    pc1._snapshot()
    pc2._expectation_maximization(5)
    check_cols_match_except_sign(pc1._eigvec[:, :5], pc2._eigvec)
Beispiel #14
0
def test_snapshot_vs_direct_pc_rowvar_false():
    """Tests that snapshot and direct agree."""
    np.random.seed(1)
    data = np.random.normal(size=(10, 50))
    pc1 = PrincipalComponents(data)
    pc2 = PrincipalComponents(data)
    pc1._direct()
    pc2._snapshot()
    check_cols_match_except_sign(pc1._eigvec[:, :4], pc2._eigvec[:, :4])
Beispiel #15
0
def test_ndim_property():
    """Tests the ndim property works with rowvar correctly."""
    data = np.zeros((10, 50))
    pc1 = PrincipalComponents(data)
    pc2 = PrincipalComponents(data.T, rowvar=True)
    assert pc1.ndim == pc2.ndim