Ejemplo n.º 1
0
    def test_fit_parameters_two_clusters(self):
        # A sample with four contigs, each with three data points.
        
        x = np.log(np.array([[0.5,3.0,2.0],[3.0,1.0,1.0],
                             [2.5,1.5,1.0],[1.0,1.0,2.0]]))
        # two clusters, four contigs
        exp_clust = np.array([[0.7,0.3],[0.1,0.9],
                              [0.5,0.5],[0.2,0.8]])

        mu00 = (np.log(0.5)*0.7+np.log(3.0)*0.1+np.log(2.5)*0.5+np.log(1.0)*0.2)/1.5
        mu10 = (np.log(3.0)*0.7+np.log(1.0)*0.1+np.log(1.5)*0.5+np.log(1.0)*0.2)/1.5
        mu20 = (np.log(2.0)*0.7+np.log(1.0)*0.1+np.log(1.0)*0.5+np.log(2.0)*0.2)/1.5

        mu01 = (np.log(0.5)*0.3+np.log(3.0)*0.9+np.log(2.5)*0.5+np.log(1.0)*0.8)/2.5
        mu11 = (np.log(3.0)*0.3+np.log(1.0)*0.9+np.log(1.5)*0.5+np.log(1.0)*0.8)/2.5
        mu21 = (np.log(2.0)*0.3+np.log(1.0)*0.9+np.log(1.0)*0.5+np.log(2.0)*0.8)/2.5
        mu,sigma = model.fit_parameters(x,expected_clustering = exp_clust)
        assert_almost_equal(mu[0,0],mu00)
        assert_almost_equal(mu[0,1],mu10)
        assert_almost_equal(mu[0,2],mu20)

        assert_almost_equal(mu[1,0],mu01)
        assert_almost_equal(mu[1,1],mu11)
        assert_almost_equal(mu[1,2],mu21)

        sigma_test0 = np.array([((x[i,0] - mu[0,0])**2 + (x[i,1] -mu[0,1])**2 + (x[i,2]-mu[0,2])**2)*exp_clust[i,0] for i in range(4)]).sum()
        sigma_test0 /= 1.5

        sigma_test1 = np.array([((x[i,0] - mu[1,0])**2 + (x[i,1] -mu[1,1])**2 + (x[i,2]-mu[1,2])**2)*exp_clust[i,1] for i in range(4)]).sum()
        sigma_test1 /= 2.5
        
        assert_almost_equal(sigma[0],sigma_test0)
        assert_almost_equal(sigma[1],sigma_test1)
Ejemplo n.º 2
0
    def test_fit_parameters_single_cluster(self):
        # A sample with two contigs, each with three data points.
        
        x = np.log(np.array([[0.5,3.0,2.0],[3.0,1.0,1.0]]))
        # one cluster, two contigs
        exp_clust = np.array([[1.0],[1.0]])
        mu0 = np.log(np.array([0.5,3.0])).sum()/2.0
        mu1 = np.log([3.0,1.0]).sum()/2.0
        mu2 = np.log([2.0,1.0]).sum()/2.0
        mu,sigma = model.fit_parameters(x,exp_clust)
        assert_equal(mu[0,0],mu0)
        assert_equal(mu[0,1],mu1)
        assert_equal(mu[0,2],mu2)

        assert_equal(mu.shape,(1,3))

        diff_vec = [(x[i,0] - mu0)**2 + (x[i,1] -mu1)**2 + (x[i,2]-mu2)**2 for i in range(2)]
        sigma_test = np.array(diff_vec).sum()

        sigma_test /= 2.0
        
        assert_equal(sigma[0],sigma_test)