def testCoefficientsForPointValues(self):
        coeffs = randn(self.model.GetNumberOfPrincipalComponents())
        s = self.model.DrawSample(coeffs)

        num_pts = s.GetNumberOfPoints()
        pt_ids = xrange(0, num_pts, num_pts / 500)

        pvList = statismo.PointValueList_vtkPD()
        pidVList = statismo.PointIdValueList_vtkPD()
        for pt_id in pt_ids:

            # we create a list of fixed points once using the point id ....
            val = statismo.vtkPoint(*getPDPointWithId(s, pt_id))
            pidVPair = statismo.PointIdValuePair_vtkPD(pt_id, val)
            pidVList.append(pidVPair)

            # ... and once more with the points
            representer = self.model.GetRepresenter()
            ref_pt = statismo.vtkPoint(
                *getPDPointWithId(representer.GetReference(), pt_id))
            pvPair = statismo.PointValuePair_vtkPD(ref_pt, val)
            pvList.append(pvPair)

        computed_coeffs_ptids = self.model.ComputeCoefficientsForPointIDValues(
            pidVList)
        computed_coeffs_pts = self.model.ComputeCoefficientsForPointValues(
            pvList)

        # does the list with the point and the one with the point ids yield the same result
        self.assertTrue((computed_coeffs_ptids == computed_coeffs_pts).all())

        # now compare it to the real coefficients
        diff = (coeffs -
                computed_coeffs_pts)[:-1]  # we ignore the last coefficient
        self.assertTrue((diff < 1e-3).all())
    def testPosteriorModelPointStaysPut(self):
        #Checks if a point that is fixed really stays where it was constrained to stay

        reference = self.representer.GetReference()
        sample = self.dataManager.GetData()[0].GetSample()
        pvList = statismo.PointValueList_vtkPD()

        ref_pt = getPDPointWithId(reference, 0)
        fixedpt = getPDPointWithId(sample, 0)
        pvList.append(
            statismo.PointValuePair_vtkPD(statismo.vtkPoint(*ref_pt),
                                          statismo.vtkPoint(*fixedpt)))
        pfmodelbuilder = statismo.PosteriorModelBuilder_vtkPD.Create()
        pf_model = pfmodelbuilder.BuildNewModel(self.dataManager.GetData(),
                                                pvList, 0.01, 0.01)

        # check for some samples if the points stay put
        coeffs1 = zeros(pf_model.GetNumberOfPrincipalComponents())
        coeffs1[1] = 3
        coeffs2 = zeros(pf_model.GetNumberOfPrincipalComponents())
        coeffs2[0] = -3

        for coeffs in [coeffs1, coeffs2]:
            partiallyFixedSample = pf_model.DrawSample(coeffs)
            self.assertAlmostEqual(
                partiallyFixedSample.GetPoints().GetPoint(0)[0], fixedpt[0], 1)
            self.assertAlmostEqual(
                partiallyFixedSample.GetPoints().GetPoint(0)[1], fixedpt[1], 1)
            self.assertAlmostEqual(
                partiallyFixedSample.GetPoints().GetPoint(0)[2], fixedpt[2], 1)
    def testCoefficientsForPointValues(self):
        coeffs = randn(self.model.GetNumberOfPrincipalComponents())
        s = self.model.DrawSample(coeffs)
                 
        num_pts = s.GetNumberOfPoints()
        pt_ids = xrange(0, num_pts, num_pts / 500) 
  
        pvList = statismo.PointValueList_vtkPD()
        pidVList = statismo.PointIdValueList_vtkPD()
        for pt_id in pt_ids:
             
            # we create a list of fixed points once using the point id ....
            val = statismo.vtkPoint(*getPDPointWithId(s, pt_id))            
            pidVPair = statismo.PointIdValuePair_vtkPD(pt_id, val)
            pidVList.append(pidVPair)
             
            # ... and once more with the points
            representer = self.model.GetRepresenter()
            ref_pt = statismo.vtkPoint(*getPDPointWithId(representer.GetReference(), pt_id)) 
            pvPair = statismo.PointValuePair_vtkPD(ref_pt, val)
            pvList.append(pvPair)
             
        computed_coeffs_ptids = self.model.ComputeCoefficientsForPointIDValues(pidVList)                        
        computed_coeffs_pts = self.model.ComputeCoefficientsForPointValues(pvList)
         
        # does the list with the point and the one with the point ids yield the same result
        self.assertTrue((computed_coeffs_ptids == computed_coeffs_pts).all())
 
        # now compare it to the real coefficients
        diff = (coeffs - computed_coeffs_pts)[:-1] # we ignore the last coefficient
        self.assertTrue((diff < 1e-3).all())                
    def testCheckPosteriorModelMean(self):
        # if we fix many points to correspond to one of the samples, and build a 
        # Posterior model, its mean should correspond to the sample
        nPointsFixed = 100
        nPointsTest = 1000        
        
        sample = self.dataManager.GetSampleDataStructure()[0].GetSample()        

        pvList = statismo.PointValueList_vtkPD()        

        reference = self.representer.GetReference()
        domainPoints = self.representer.GetDomain().GetDomainPoints()
        
        for pt_id in xrange(0, len(domainPoints), len(domainPoints) / nPointsFixed):
            fixed_pt = domainPoints[pt_id]
            value = statismo.vtkPoint(*getPDPointWithId(sample, pt_id))
            pointValue = statismo.PointValuePair_vtkPD(fixed_pt, value)
            pvList.append(pointValue)

        pfmodelbuilder = statismo.PosteriorModelBuilder_vtkPD.Create()
        pf_model = pfmodelbuilder.BuildNewModel(self.dataManager.GetSampleDataStructure(), pvList, 0.1, 0.1)

        
        partial_mean = pf_model.DrawMean()

        # now the sample that we used to fix the point should be similar to the mean. We test it by  
        for pt_id in xrange(0, sample.GetNumberOfPoints(), sample.GetNumberOfPoints() / nPointsTest):
            mean_pt = getPDPointWithId(partial_mean, pt_id)
            sample_pt = getPDPointWithId(sample, pt_id)
            self.assertAlmostEqual(mean_pt[0], sample_pt[0], 0) 
            self.assertAlmostEqual(mean_pt[1], sample_pt[1], 0)
            self.assertAlmostEqual(mean_pt[2], sample_pt[2], 0)
    def testCheckPosteriorModelMean(self):
        # if we fix many points to correspond to one of the samples, and build a
        # partiallyfixed model, its mean should correspond to the sample
        nPointsFixed = 100
        nPointsTest = 1000

        sample = self.dataManager.GetData()[0].GetSample()

        pvList = statismo.PointValueList_vtkPD()

        reference = self.representer.GetReference()
        domainPoints = self.representer.GetDomain().GetDomainPoints()

        for pt_id in xrange(0, len(domainPoints),
                            len(domainPoints) / nPointsFixed):
            fixed_pt = domainPoints[pt_id]
            value = statismo.vtkPoint(*getPDPointWithId(sample, pt_id))
            pointValue = statismo.PointValuePair_vtkPD(fixed_pt, value)
            pvList.append(pointValue)

        pfmodelbuilder = statismo.PosteriorModelBuilder_vtkPD.Create()
        pf_model = pfmodelbuilder.BuildNewModel(self.dataManager.GetData(),
                                                pvList, 0.1, 0.1)

        partial_mean = pf_model.DrawMean()

        # now the sample that we used to fix the point should be similar to the mean. We test it by
        for pt_id in xrange(0, sample.GetNumberOfPoints(),
                            sample.GetNumberOfPoints() / nPointsTest):
            mean_pt = getPDPointWithId(partial_mean, pt_id)
            sample_pt = getPDPointWithId(sample, pt_id)
            self.assertAlmostEqual(mean_pt[0], sample_pt[0], 0)
            self.assertAlmostEqual(mean_pt[1], sample_pt[1], 0)
            self.assertAlmostEqual(mean_pt[2], sample_pt[2], 0)
 def testCheckPosteriorModelVariancePlausibility(self):         
     # checks whether with every added point, the variance is decreasing
                    
     reference = self.representer.GetReference()
     sample  =  self.dataManager.GetSampleDataStructure()[0].GetSample()
     num_points = sample.GetNumberOfPoints()
     pvList = statismo.PointValueList_vtkPD()
     
     pfmodelbuilder = statismo.PosteriorModelBuilder_vtkPD.Create()
     pf_model = pfmodelbuilder.BuildNewModel(self.dataManager.GetSampleDataStructure(), pvList, 0.1, 0.1)
     total_var = pf_model.GetPCAVarianceVector().sum() 
     for pt_id in xrange(0, num_points, num_points / 10):
         ref_pt = statismo.vtkPoint(*getPDPointWithId(reference, pt_id))
         pt = statismo.vtkPoint(*getPDPointWithId(sample, pt_id))
         pvList.append(statismo.PointValuePair_vtkPD(ref_pt, pt))
         pfmodelbuilder = statismo.PosteriorModelBuilder_vtkPD.Create()
         pf_model = pfmodelbuilder.BuildNewModel(self.dataManager.GetSampleDataStructure(), pvList, 0.1, 0.1)
         total_sdev_prev = total_var
         total_var = pf_model.GetPCAVarianceVector().sum() 
         self.assertTrue(total_var < total_sdev_prev)
    def testCheckPosteriorModelVariancePlausibility(self):
        # checks whether with every added point, the variance is decreasing

        reference = self.representer.GetReference()
        sample = self.dataManager.GetData()[0].GetSample()
        num_points = sample.GetNumberOfPoints()
        pvList = statismo.PointValueList_vtkPD()

        pfmodelbuilder = statismo.PosteriorModelBuilder_vtkPD.Create()
        pf_model = pfmodelbuilder.BuildNewModel(self.dataManager.GetData(),
                                                pvList, 0.1, 0.1)
        total_var = pf_model.GetPCAVarianceVector().sum()
        for pt_id in xrange(0, num_points, num_points / 10):
            ref_pt = statismo.vtkPoint(*getPDPointWithId(reference, pt_id))
            pt = statismo.vtkPoint(*getPDPointWithId(sample, pt_id))
            pvList.append(statismo.PointValuePair_vtkPD(ref_pt, pt))
            pfmodelbuilder = statismo.PosteriorModelBuilder_vtkPD.Create()
            pf_model = pfmodelbuilder.BuildNewModel(self.dataManager.GetData(),
                                                    pvList, 0.1, 0.1)
            total_sdev_prev = total_var
            total_var = pf_model.GetPCAVarianceVector().sum()
            self.assertTrue(total_var < total_sdev_prev)
 def testPosteriorModelPointStaysPut(self):         
     #Checks if a point that is fixed really stays where it was constrained to stay
                    
     reference = self.representer.GetReference()
     sample  =  self.dataManager.GetSampleDataStructure()[0].GetSample()
     pvList = statismo.PointValueList_vtkPD()
             
     ref_pt = getPDPointWithId(reference, 0)
     fixedpt = getPDPointWithId(sample, 0)
     pvList.append(statismo.PointValuePair_vtkPD(statismo.vtkPoint(*ref_pt),  statismo.vtkPoint(*fixedpt)))
     pfmodelbuilder = statismo.PosteriorModelBuilder_vtkPD.Create()
     pf_model = pfmodelbuilder.BuildNewModel(self.dataManager.GetSampleDataStructure(), pvList, 0.01, 0.01)
     
     # check for some samples if the points stay put
     coeffs1 = zeros(pf_model.GetNumberOfPrincipalComponents())        
     coeffs1[1] = 3
     coeffs2 = zeros(pf_model.GetNumberOfPrincipalComponents())
     coeffs2[0] = -3        
     
     for coeffs in [coeffs1, coeffs2]:
         PosteriorSample = pf_model.DrawSample(coeffs)
         self.assertAlmostEqual(PosteriorSample.GetPoints().GetPoint(0)[0], fixedpt[0], 1)
         self.assertAlmostEqual(PosteriorSample.GetPoints().GetPoint(0)[1], fixedpt[1], 1)
         self.assertAlmostEqual(PosteriorSample.GetPoints().GetPoint(0)[2], fixedpt[2], 1)