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)
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)