def testIter(self): """Tests overloading operator ``__iter__``.""" weights = {'a': Weight(0.2), 'b': Weight(0.4), 'c': Weight(3.2)} meta_weight = MetaWeight(weights) self.assertSetEqual(set(iter(meta_weight)), set(iter(weights))) for key, value in meta_weight.iteritems(): self.assertEqual(value, weights[key])
def testLen(self): """Tests overloading operator ``__len__``.""" self.assertEqual( len(MetaWeight({ 'a': Weight(0.5), 'b': Weight(0.23) })), 2)
def RedefineClassifierIfLargeRegressionRange(self, analysis): """Disable features for revisions in regression range > 100 commits. For crash with big regression range, it's easy to get false positives if we enable TouchCrashedComponentFeature and TouchCrashedDirectoryFeature, Because it has a big chance to hit these features and cause false positives. So we should disable these 2 features. """ if (analysis.commit_count_in_regression_range < _BIG_REGRESSION_RANGE_COMMITS_THRESHOLD): return get_repository = self._predator.changelist_classifier._get_repository meta_weight = MetaWeight({ 'TouchCrashedFileMeta': MetaWeight({ 'MinDistance': Weight(1.), 'TopFrameIndex': Weight(1.), 'TouchCrashedFile': Weight(1.), }), }) meta_feature = WrapperMetaFeature([ TouchCrashedFileMetaFeature([ MinDistanceFeature(get_repository), TopFrameIndexFeature(), TouchCrashedFileFeature() ]) ]) self._predator.changelist_classifier = ChangelistClassifier( get_repository, meta_feature, meta_weight)
def setUp(self): """Set up some basic parts of our loglinear model. These parts describe a silly model for detecting whether an integer in [0..9] is the number 7. So ``X`` is the set of integers [0..9], and ``Y`` is the set of ``bool`` values. The independent variable is boolean-valued because we only have two categories: "yes, x == 7" and "no, x != 7". This doesn't take advantage of the fact that loglinear models can categorize larger sets of labels, but it's good enough for testing purposes. In addition to specifying ``X`` and ``Y``, we also specify a set of features and choose some random weights for them. """ super(LinearTestCase, self).setUp() self._meta_feature = WrapperMetaFeature([Feature0(), Feature1(), Feature2(), WrapperMetaFeature([Feature3(), Feature4()])]) self._meta_weight = MetaWeight( { 'Feature0': Weight(random.random()), 'Feature1': Weight(random.random()), 'Feature2': Weight(random.random()), 'WrapperFeature': MetaWeight( { 'Feature3': Weight(random.random()), 'Feature4': Weight(random.random()) }) }) self._X = range(10) self._Y = lambda _x: [False, True]
def __init__(self, get_repository, config): """Set the paramaters of the model - i.e. the weights and features. For some explanation of why the paramaters were set this way see these docs: https://docs.google.com/a/google.com/document/d/1TdDEDlUJX81-5yvB9IfdJFq5-kJBb_DwAgDH9cGfNao/edit?usp=sharing https://docs.google.com/a/google.com/document/d/1FHaghBX_FANjtiUP7D1pihZGxzEYdXA3Y7t0rSA4bWU/edit?usp=sharing As well as the following CLs: https://chromium-review.googlesource.com/c/599071 https://chromium-review.googlesource.com/c/585784 """ super(PredatorForUMASamplingProfiler, self).__init__(get_repository, config) meta_weight = MetaWeight({ 'TouchCrashedFileMeta': MetaWeight({ 'MinDistance': Weight(2.), 'TopFrameIndex': Weight(0.), 'TouchCrashedFile': Weight(1.), }) }) min_distance_feature = MinDistanceFeature(get_repository) top_frame_index_feature = TopFrameIndexFeature() touch_crashed_file_feature = TouchCrashedFileFeature() meta_feature = WrapperMetaFeature([ TouchCrashedFileMetaFeature([ min_distance_feature, top_frame_index_feature, touch_crashed_file_feature ], include_renamed_paths=True) ]) self._predator = Predator( ChangelistClassifier(get_repository, meta_feature, meta_weight), self._component_classifier, self._project_classifier)
def testEqual(self): """Tests ``__eq__`` and ``__ne__``.""" self.assertTrue( MetaWeight({'f1': Weight(0.008)}) == MetaWeight( {'f1': Weight(0.008)})) self.assertFalse( MetaWeight({'f1': MetaWeight({'f2': Weight(0.008)})}) == MetaWeight({'f1': MetaWeight({'f2': Weight(0.1)})})) self.assertFalse(MetaWeight({'f1': Weight(0.008)}) == MetaWeight({}))
def testMultiply(self): """Tests overloading operators ``__mul__`` and ``__rmul__``""" self.assertEqual( MetaWeight({ 'f1': Weight(0.8), 'f2': Weight(0.4) }) * MetaFeatureValue('f', { 'f1': 2., 'f2': 1. }), 2.) self.assertEqual( MetaFeatureValue('f', { 'f1': 0.8, 'f2': 0.4 }) * MetaWeight({ 'f1': Weight(2.), 'f2': Weight(1.) }), 2.) self.assertEqual( MetaWeight({ 'f1': Weight(0.8), 'f3': Weight(0.0) }) * MetaFeatureValue('f', { 'f1': Weight(2.), 'f2': Weight(9), 'f3': Weight(10) }), 1.6)
def testFilterReasonWithWeight(self): meta_weight = MetaWeight({ 'f1': Weight(2.), 'f2': Weight(0.), 'f3': Weight(1.) }) reason = MetaDict({'f1': ['reason1', 'reason3'], 'f2': ['reason2']}) model = UnnormalizedLogLinearModel(None, meta_weight) self.assertListEqual(model.FilterReasonWithWeight(reason), ['reason1', 'reason3'])
def testquadrance(self): """Tests ``quadrance`` property.""" self.assertEqual( MetaWeight({ 'a': Weight(0.3), 'b': Weight(0.2) }).quadrance, 0.13) self.assertEqual( MetaWeight({ 'a': Weight(0.3), 'b': Weight(-0.3) }).quadrance, 0.18)
def testIsZero(self): """Tests ``IsZero`` method.""" self.assertTrue( MetaWeight({ 'f1': Weight(0.008), 'f2': Weight(0.00004) }).IsZero(0.01)) self.assertFalse( MetaWeight({ 'f1': Weight(0.08), 'f2': Weight(0.00004) }).IsZero(0.001))
def testl1(self): """Tests ``l1`` property.""" self.assertEqual( MetaWeight({ 'a': Weight(0.3), 'b': Weight(0.2) }).l1, 0.5) self.assertEqual( MetaWeight({ 'a': Weight(0.3), 'b': Weight(-0.3) }).l1, 0.6)
def testDropZeroWeights(self): meta_weight = MetaWeight({ 'f1': Weight(0.02), 'f2': MetaWeight({ 'f3': Weight(0.00001), 'f4': Weight(0.0000003) }) }) meta_weight.DropZeroWeights(epsilon=0.0001) expected_meta_weight = MetaWeight({'f1': Weight(0.02)}) self.assertTrue(meta_weight == expected_meta_weight)
def setUp(self): super(ChangelistClassifierTest, self).setUp() meta_weight = MetaWeight({ 'TouchCrashedFileMeta': MetaWeight({ 'MinDistance': Weight(1.), 'TopFrameIndex': Weight(1.), 'TouchCrashedFile': Weight(1.), }) }) get_repository = GitilesRepository.Factory(self.GetMockHttpClient()) meta_feature = WrapperMetaFeature( [TouchCrashedFileMetaFeature(get_repository)]) self.changelist_classifier = ChangelistClassifier( get_repository, meta_feature, meta_weight)
def __init__(self, get_repository, config): super(FinditForClusterfuzz, self).__init__(get_repository, config) meta_weight = MetaWeight({ 'TouchCrashedFileMeta': MetaWeight({ 'MinDistance': Weight(1.), 'TopFrameIndex': Weight(1.), 'TouchCrashedFile': Weight(1.), }), 'TouchCrashedDirectory': Weight(1.), 'TouchCrashedComponent': Weight(1.) }) meta_feature = WrapperMetaFeature([ TouchCrashedFileMetaFeature(get_repository), TouchCrashedDirectoryFeature(), TouchCrashedComponentFeature(self._component_classifier) ]) self._predator = Predator( ChangelistClassifier(get_repository, meta_feature, meta_weight), self._component_classifier, self._project_classifier)
def testl0(self): """Tests ``l0`` property.""" self.assertEqual( MetaWeight({ 'a': Weight(0.2), 'b': Weight(0.), 'd': Weight(0.), 'e': Weight(2.) }).l0, 2) self.assertEqual(MetaWeight({'a': Weight(0.), 'b': Weight(0.)}).l0, 0)
def __init__(self, get_repository, config): super(PredatorForClusterfuzz, self).__init__(get_repository, config) meta_weight = MetaWeight({ 'TouchCrashedFileMeta': MetaWeight({ 'MinDistance': Weight(2.), 'TopFrameIndex': Weight(1.), 'TouchCrashedFile': Weight(1.), }), 'TouchCrashedDirectory': Weight(1.), 'TouchCrashedComponent': Weight(1.), 'NumberOfTouchedFiles': Weight(0.5), }) min_distance_feature = MinDistanceFeature(get_repository) top_frame_index_feature = TopFrameIndexFeature() touch_crashed_file_feature = TouchCrashedFileFeature() meta_feature = WrapperMetaFeature([ TouchCrashedFileMetaFeature([ min_distance_feature, top_frame_index_feature, touch_crashed_file_feature ], options=config.feature_options.get( 'TouchCrashedFileMetaFeature')), TouchCrashedDirectoryFeature( options=config.feature_options['TouchCrashedDirectory']), TouchCrashedComponentFeature( self._component_classifier, options=config.feature_options['TouchCrashedComponent']), NumberOfTouchedFilesFeature() ]) self._predator = Predator( ChangelistClassifier(get_repository, meta_feature, meta_weight), self._component_classifier, self._project_classifier)
def _MetaToNumPyArray(self, meta_weight): """Converts dict (mapping feature name to weight) to numpy array.""" return np.array(self._serializer.ToList(meta_weight, default=Weight(0)))
def testquadrance(self): """Tests ``quadrance`` property.""" self.assertEqual(Weight(0.3).quadrance, 0.09) self.assertEqual(Weight(-0.3).quadrance, 0.09)
def testl0(self): """Tests ``l0`` property.""" self.assertEqual(Weight(0.2).l0, 1) self.assertEqual(Weight(0.).l0, 0)
def testLen(self): """Tests overloading operator ``__len__``.""" self.assertEqual(len(Weight(0.5)), 1)
def testIsZero(self): """Tests ``IsZero`` method.""" self.assertTrue(Weight(0.00001).IsZero(0.001)) self.assertFalse(Weight(0.00001).IsZero(0.000001))
def testMetaWeightSetter(self): model = LogLinearModel(self._Y, self._meta_feature, self._meta_weight) new_meta_weight = copy.deepcopy(self._meta_weight) new_meta_weight['Feature0'] = Weight(2.1) model.meta_weight = new_meta_weight self.assertTrue(model.meta_weight == new_meta_weight)
def testMultiply(self): """Tests overloading operators ``__mul__`` and ``__rmul__``""" self.assertEqual((Weight(0.8) * 2.0), 0.8 * 2.0) self.assertEqual((2.0 * Weight(0.8)), 2.0 * 0.8)
def testl1(self): """Tests ``l1`` property.""" self.assertEqual(Weight(0.3).l1, 0.3) self.assertEqual(Weight(-0.3).l1, 0.3)
def testEqual(self): """Tests ``__eq__`` and ``__ne__``.""" self.assertTrue(Weight(0.2) == Weight(0.2)) self.assertTrue(Weight(0.2) != Weight(0.3))
def testFloat(self): """Tests convert ``Weight`` to float.""" self.assertEqual(float(Weight(0.8)), 0.8)