How to use the rules.logger.info function in rules

To help you get started, we’ve selected a few rules examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github gerberlab / mitre / mitre / logit_rules.py View on Github external
logger.debug('Rule is [] (new)')
        else:
            logger.debug('Rule is %s' % str(map(index, current_rl[rule_index])))
        logger.debug('Candidate distribution (log scale):')
        for i,value in enumerate(candidate_distribution):
            logger.debug('%d\t%.3f' % (i,value))

        probability_this_option = 1./(2.*len(current_rl) + 1.)
        probability_reverse_option = probability_remove_proposal

        logger.debug('Add block: %s' % comment)
        logger.debug('Prior, log(marg. new/old): %.3g' % 
                     relative_prior)
        logger.debug('Move choice prob (rev/for): %.3g/%.3g' % 
                     (probability_reverse_option, probability_this_option))
        logger.info('Likelihood, log(marg. new/old): %.3g' % 
                    relative_likelihood)
        acceptance_ratio = (
            np.exp(relative_prior+relative_likelihood) * 
            (probability_reverse_option / probability_this_option)
        )
        logger.debug('Acceptance ratio: %.3g' % acceptance_ratio)
        self.attempts['add'] = self.attempts.get('add',0) + 1
        if (np.random.rand() < acceptance_ratio) or force_acceptance:
            if force_acceptance:
                logger.warning('Forcing acceptance for debugging purposes.')
            comment += ': accepted (ratio %.3g)' % acceptance_ratio
            self.successes['add'] = self.successes.get('add',0) + 1
            # Choose which of the previously marginalized-out 
            # primitives we actually want to add at this position.
            k = choose_from_relative_probabilities(
                np.exp(candidate_distribution)
github gerberlab / mitre / mitre / logit_rules.py View on Github external
rule_list = self.current_state.rl
        # Which combinations of rule i and primitive j are available?
        positions = []
        # How many copies of each present primitive are included in each rule?
        counts = []
        for i, rule in enumerate(rule_list):
            this_rule_counts = {}
            for j, primitive in enumerate(rule):
                positions.append((i,j))
                k = self.model.rule_population.get_primitive_index(primitive)
                this_rule_counts[k] = this_rule_counts.get(k,0) + 1
            counts.append(this_rule_counts)
        if N_updates is None:
            N_updates = 1 # len(positions)
        if len(positions) == 0:
            logger.info('No detectors to update')
            self.comments.append('Null update')
            return

        for _ in xrange(N_updates):
            # Can't use np.random.choice(positions),
            # positions looks like a 2-d array
            choice_index = np.random.choice(len(positions))
            i,j = positions[choice_index]
            primitive = rule_list[i][j]
            k0 = self.model.rule_population.get_primitive_index(primitive)
            # What are the relative multinomial pieces of the prior
            # for the rule lists we would obtain by putting primitives
            # 1...n_primitives at position i,j?
            multinomial_priors = np.zeros(len(primitives))
            # What are the relative rates of proposing an equivalent update step
            # for the rule lists we would obtain (etc).
github gerberlab / mitre / mitre / logit_rules.py View on Github external
# print 'ADD DEBUG: len nr %d' % len(new_rl)
            # print 'ADD DEBUG: len nr[ri] %d all lens %s' % (
            #     len(new_rl[rule_index]), 
            #     str(map(len,new_rl.rules)))
            # print 'cr all lens %s' % str(map(len,current_rl))
            new_rl[rule_index][primitive_index] = PrimitiveRule(*primitives[k])
            self.current_state.rl = new_rl
            self.ensure_current_rl_sorted()
            self.current_X = self.model.data.covariate_matrix(
                self.current_state.rl
            )
            # CAUTION: now beta is wrong
        else:
            comment += ': rejected (ratio %.3g)' % acceptance_ratio

        logger.info(comment)
        self.comments.append(comment)
github gerberlab / mitre / mitre / logit_rules.py View on Github external
return
        self.attempts['remove'] = self.attempts.get('remove',0) + 1
        if np.random.rand() < acceptance_ratio:
            comment += ': accepted (ratio %.3g)' % acceptance_ratio
            self.successes['remove'] = self.successes.get('add',0) + 1
            self.current_state.rl = shorter_rule
            shorter_X = self.model.data.covariate_matrix(shorter_rule)
            self.current_X = shorter_X
            # No need to ensure the current RL is sorted here;
            # possibly removing one entry from a sorted list preserves
            # its order.
            # CAUTION: beta is wrong
        else:
            comment += ': rejected (ratio %.3g)' % acceptance_ratio

        logger.info(comment)
        self.comments.append(comment)
github gerberlab / mitre / mitre / logit_rules.py View on Github external
def move_primitive(self):
        """ Choose a primitive, sample over its possible positions in the list.

        Includes moving it to new rules; for each position, both the primitive
        and its inverse are considered.

        """ 
        base_rl = self.current_state.rl.copy()
        if len(base_rl) == 0 or (len(base_rl) == 1 and len(base_rl[0]) == 1):
            self.comments.append('Null detector move')
            logger.info('Null detector move')
            return

        # Choose primitive to consider moving. Each primitive has equal 
        # probability of being chosen.
        primitives = []
        for i, rule in enumerate(base_rl.rules):
            for j, primitive in enumerate(rule):
                primitives.append((i,j,primitive))
        index = np.random.choice(len(primitives))
        i,j,primitive = primitives[index]
        
        # Document the choice, and prepare a reversed version of the 
        # primitive 
        comment_prefix = 'detector (%d,%d) moves to ' % (i,j)
        inverted_primitive = PrimitiveRule(*primitive.as_tuple())
        if primitive.direction == 'above':
github gerberlab / mitre / mitre / logit_rules.py View on Github external
def remove(self, dry_run=False):
        current_rl = self.current_state.rl
        # Deal with the case where the list is empty.
        if len(current_rl) == 0:
            logger.info('Remove block: Nothing to remove.')
            self.comments.append('Nothing to remove')
            return
        
        # Choose which primitive to remove
        positions = [(i,j) for i,subrule in 
                     enumerate(current_rl.rules) for 
                     j,_ in enumerate(subrule)]
        # Can't use np.random.choice(positions),
        # positions looks like a 2-d array
        choice_index = np.random.choice(len(positions))
        rule_index, primitive_index = positions[choice_index]
        comment = 'remove detector %d from rule %d' % (primitive_index,
                                                        rule_index)
        shorter_rule = current_rl.copy()
        if len(current_rl[rule_index]) == 1:
            debugcase = 'a'