def test_Normalize(self):
        """FIXME: THIS TEST BELONGS IN TEST_FEATURESET.PY"""

        from numpy.testing import assert_allclose
        result_fs = FeatureSet_Discrete.NewFromFitFile(self.test_fit_path)
        result_fs.Normalize()
        target_fs = FeatureSet_Discrete.NewFromFitFile(
            self.test_normalized_fit_path)

        assert_allclose(result_fs.data_matrix,
                        target_fs.data_matrix,
                        rtol=self.epsilon)
Beispiel #2
0
    def test_IfNotInterpolatable(self):
        """You can't graph predicted values if the classes aren't interpolatable."""

        testfilename = 'ShouldntBeGraphable.png'
        fitfilepath = wndchrm_test_dir + sep + 'test-l.fit'
        fs = FeatureSet_Discrete.NewFromFitFile(fitfilepath)
        train_set, test_set = fs.Split(randomize=False, quiet=True)
        train_set.Normalize()

        fw = FisherFeatureWeights.NewFromFeatureSet(train_set).Threshold()
        reduced_train_set = train_set.FeatureReduce(fw.names)
        reduced_test_set = test_set.FeatureReduce(fw.names)
        test_set.Normalize(train_set, quiet=True)

        batch_result = DiscreteBatchClassificationResult.New(reduced_train_set,
                                                             reduced_test_set,
                                                             fw,
                                                             quiet=True)
        graph = PredictedValuesGraph(batch_result)

        tempfile = self.tempdir + sep + testfilename

        with self.assertRaises(ValueError):
            graph.RankOrderedPredictedValuesGraph()
            graph.SaveToFile(tempfile)
    def test_PerSampleStatisticsWITHOUTPredictedValue(self):
        """DISCRETE ShuffleSplit/PerSampleStatistics w/ mini binucleate test set (no predicted value)"""

        fs = FeatureSet_Discrete.NewFromFitFile('../wndchrm_tests/test-l.fit')
        exp = DiscreteClassificationExperimentResult.NewShuffleSplit(
            fs, quiet=True)
        exp.PerSampleStatistics()
        self.assertTrue(True)
    def test_NewFromFeatureSet(self):
        """Fisher score calculation"""

        feature_set = FeatureSet_Discrete.NewFromFitFile(self.test_fit_path)
        feature_set.Normalize()
        result_weights = FisherFeatureWeights.NewFromFeatureSet(feature_set)

        # test weights generated from test-l.fit:
        # wndchrm classify -l -f1.0 -vtest_fit-l.weights test-l.fit test-l.fit
        target_weights = FisherFeatureWeights.NewFromFile(
            self.test_feat_weight_path)

        for target_val, res_val in zip(target_weights.values,
                                       result_weights.values):
            self.assertAlmostEqual(target_val, res_val, delta=self.epsilon)
Beispiel #5
0
class TestWND5Classification(unittest.TestCase):
    """WND5 Classification"""

    epsilon = 0.00001

    # Define paths to original files
    test_sig_path = join(test_dir, 't1_s01_c05_ij-l_precalculated.sig')
    test_fit_path = join(test_dir, 'test-l.fit')
    test_feat_wght_path = join(test_dir, 'test_fit-l.weights')
    test_tif_path = join(test_dir, 't1_s01_c05_ij.tif')

    # Here are the correct values that Python API needs to return:
    # wndchrm classify -l -f1.0 test-l.fit t1_s01_c05_ij.tif
    # t1_s01_c05_ij.tif	1.6e-27	0.083	0.917	*	4cell	3.835
    # wndchrm classify -l -f0.14765 test-l.fit t1_s01_c05_ij.tif
    # t1_s01_c05_ij.tif	3.23e-27	0.076	0.924	*	4cell	3.848
    # wndchrm classify -l -f0.0685 test-l.fit t1_s01_c05_ij.tif
    # t1_s01_c05_ij.tif	7.05e-27	0.069	0.931	*	4cell	3.862

    correct_marg_probs = {}
    correct_marg_probs[2919] = [0.083, 0.917]
    correct_marg_probs[431] = [0.076, 0.924]
    #correct_marg_probs[200] = [0.044, 0.956]
    # slight difference in marg probs due to my use of round() below
    correct_marg_probs[200] = [0.069, 0.931]

    # Load the original files once and only once for all this class's tests
    feature_set = FeatureSet_Discrete.NewFromFitFile(test_fit_path)
    feature_set.Normalize()

    test_sample = Signatures.NewFromSigFile(test_sig_path, test_tif_path)
    test_sample.Normalize(feature_set)

    all_weights = FisherFeatureWeights.NewFromFile(test_feat_wght_path)

    # --------------------------------------------------------------------------
    def Check(self, num_feats=None):
        weights = self.all_weights.Threshold(num_feats)
        feat_set = self.feature_set.FeatureReduce(weights.names)
        sample = self.test_sample.FeatureReduce(weights.names)
        result = DiscreteImageClassificationResult.NewWND5(
            feat_set, weights, sample)
        result_marg_probs = [ round( val, 3 ) \
          for val in result.marginal_probabilities ]
        self.assertSequenceEqual(self.correct_marg_probs[num_feats],
                                 result_marg_probs)

    # --------------------------------------------------------------------------
    def test_WND5_all_features(self):
        """WND5 classification with entire large feature set (2919 features)"""
        self.Check(2919)

    # --------------------------------------------------------------------------
    def test_WND5_15percent_threshold(self):
        """WND5 classification with large feature set 15% threshold (431 features)"""
        self.Check(431)

    # --------------------------------------------------------------------------
    def test_WND5_200_feat_threshold(self):
        """WND5 classification with large feature set & 200 feature threshold"""
        self.Check(200)