Exemple #1
0
    def induce(self,
               b,
               filestem='default',
               examples=None,
               pos=None,
               neg=None,
               cn2sd=True):
        """
        Generate features and find subgroups.
        
        @param filestem The base name of this experiment.
        @param examples Classified examples; can be used instead of separate pos / neg files below.
        @param pos String of positive examples.
        @param neg String of negative examples.
        @param b String with background knowledge.
        @param cn2sd Find subgroups after feature construction?

        Returns a tuple (features, weka, rules), where:
            - features is a set of prolog clauses of generated features,
            - weka is the propositional form of the input data,
            - rules is a set of generated cn2sd subgroup descriptions; 
              this will be an empty string if cn2sd is set to False.
        """
        # Write the inputs
        self.__prepare(filestem, b, examples=examples, pos=pos, neg=neg)

        # Write scripts
        self.__scripts(filestem)

        # Run the script
        logger.info("Running RSD...")
        try:
            for script in RSD.SCRIPTS:
                # Skip subgroup discovery part?
                if script == RSD.SUBGROUPS and not cn2sd:
                    continue
                p = SafePopen(['yap', '-s50000', '-h200000', '-L', script],
                              cwd=self.tmpdir,
                              stdout=PIPE).safe_run()
                stdout_str, stderr_str = p.communicate()
                logger.debug(stdout_str)
                logger.debug(stderr_str)
            logger.info("Done.")

            # Return the rules written in the output file.
            features = open('%s/%s' %
                            (self.tmpdir, filestem + '_frs.pl')).read()
            weka = open('%s/%s' % (self.tmpdir, filestem + '.arff')).read()
            rules = open(
                '%s/%s' %
                (self.tmpdir, filestem + '.rules')).read() if cn2sd else ''

            self.__cleanup()
            return (features, weka, rules)
        except OSError:
            raise RuntimeError(
                "Yap compiler could not be loaded! (see http://www.dcc.fc.up.pt/~vsc/Yap/)."
            )
Exemple #2
0
    def induce(self, b, filestem='default',
               examples=None,
               pos=None,
               neg=None,
               cn2sd=True,
               printOutput=False):
        """
        Generate features and find subgroups.
        
        @param filestem The base name of this experiment.
        @param examples Classified examples; can be used instead of separate pos / neg files below.
        @param pos String of positive examples.
        @param neg String of negative examples.
        @param b String with background knowledge.
        @param cn2sd Find subgroups after feature construction?

        Returns a tuple (features, weka, rules), where:
            - features is a set of prolog clauses of generated features,
            - weka is the propositional form of the input data,
            - rules is a set of generated cn2sd subgroup descriptions; 
              this will be an empty string if cn2sd is set to False.
        """
        # Write the inputs
        self.__prepare(filestem, b, examples=examples, pos=pos, neg=neg)

        # Write scripts
        self.__scripts(filestem)

        dumpFile = None
        if not printOutput:
            dumpFile = tempfile.TemporaryFile()

        # Run the script
        logger.info("Running RSD...")
        try:
            for script in RSD.SCRIPTS:
                # Skip subgroup discovery part?
                if script == RSD.SUBGROUPS and not cn2sd:
                    continue
                p = SafePopen(['yap', '-s50000', '-h200000', '-L', script],
                              cwd=self.tmpdir,
                              stdout=dumpFile,
                              stderr=dumpFile).safe_run()
                stdout_str, stderr_str = p.communicate()
                logger.debug(stdout_str)
                logger.debug(stderr_str)
            logger.info("Done.")

            # Return the rules written in the output file.
            features = open('%s/%s' % (self.tmpdir, filestem + '_frs.pl')).read()
            weka = open('%s/%s' % (self.tmpdir, filestem + '.arff')).read()
            rules = open('%s/%s' % (self.tmpdir, filestem + '.rules')).read() if cn2sd else ''

            self.__cleanup()
            return (features, weka, rules)
        except OSError:
            raise RuntimeError("Yap compiler could not be loaded! (see http://www.dcc.fc.up.pt/~vsc/Yap/).")
Exemple #3
0
    def induce(self, mode, pos, neg, b, filestem='default', printOutput=False):
        """
        Induce a theory or features in 'mode'.

            :param filestem: The base name of this experiment.
            :param mode: In which mode to induce rules/features.
            :param pos: String of positive examples.
            :param neg: String of negative examples.
            :param b: String of background knowledge.

            :return: The theory as a string or an arff dataset in induce_features mode.
            :rtype: str

        """
        # Write the inputs to appropriate files.
        self.__prepare(filestem, pos, neg, b)

        # Make a script to run aleph (with appropriate settings).
        self.__script(mode, filestem)

        logger.info("Running aleph...")

        dumpFile = None
        if not printOutput:
            dumpFile = tempfile.TemporaryFile()

        # Run the aleph script.
        p = SafePopen(['yap', '-s50000', '-h200000', '-L', Aleph.SCRIPT],
                      cwd=self.tmpdir,
                      stdout=dumpFile,
                      stderr=dumpFile).safe_run()
        stdout_str, stderr_str = p.communicate()

        logger.info("Done.")

        result = None
        if mode != 'induce_features':
            # Return the rules written in the output file.
            rules_fn = filestem + Aleph.RULES_SUFFIX
            result = open('%s/%s' % (self.tmpdir, rules_fn)).read()
            features = None
        else:
            features_fn = filestem + Aleph.FEATURES_SUFFIX
            features = open('%s/%s' % (self.tmpdir, features_fn)).read()
            dataset_fn = filestem + Aleph.PROP_DATASET_SUFFIX
            pl_dataset = open('%s/%s' % (self.tmpdir, dataset_fn)).read()
            result = self.__to_arff(features, pl_dataset, filestem)

        # Cleanup.
        self.__cleanup()
        return (result, features)
Exemple #4
0
    def induce(self, mode, pos, neg, b, filestem='default', printOutput=False):
        """
        Induce a theory or features in 'mode'.

        @param filestem The base name of this experiment.
        @param mode In which mode to induce rules/features.
        @param pos String of positive examples.
        @param neg String of negative examples.
        @param b String with background knowledge.

        @return The theory as a string or an arff dataset in induce_features mode.
        """
        # Write the inputs to appropriate files.
        self.__prepare(filestem, pos, neg, b)

        # Make a script to run aleph (with appropriate settings).
        self.__script(mode, filestem)

        logger.info("Running aleph...")

        dumpFile = None
        if not printOutput:
            dumpFile = tempfile.TemporaryFile()

        # Run the aleph script.
        p = SafePopen(['yap', '-s50000', '-h200000', '-L', Aleph.SCRIPT],
                      cwd=self.tmpdir,
                      stdout=dumpFile,
                      stderr=dumpFile
        ).safe_run()
        stdout_str, stderr_str = p.communicate()

        logger.info("Done.")

        result = None
        if mode != 'induce_features':
            # Return the rules written in the output file.
            rules_fn = filestem + Aleph.RULES_SUFFIX
            result = open('%s/%s' % (self.tmpdir, rules_fn)).read()
            features = None
        else:
            features_fn = filestem + Aleph.FEATURES_SUFFIX
            features = open('%s/%s' % (self.tmpdir, features_fn)).read()
            dataset_fn = filestem + Aleph.PROP_DATASET_SUFFIX
            pl_dataset = open('%s/%s' % (self.tmpdir, dataset_fn)).read()
            result = self.__to_arff(features, pl_dataset, filestem)


        # Cleanup.
        self.__cleanup()
        return (result, features)
Exemple #5
0
    def induce(self, mode, pos, neg, b, filestem='default'):
        """
        Induce a theory in 'mode'.
        
        @param filestem The base name of this experiment.
        @param mode In which mode to induce rules.
        @param pos String of positive examples.
        @param neg String of negative examples.
        @param b String with background knowledge.
        """
        # Write the inputs to appropriate files.
        self.__prepare(filestem, pos, neg, b)

        # Make a script to run aleph (with appropriate settings, stack/heap sizes, ...).
        self.__script(mode, filestem)

        logger.info("Running aleph...")

        # Run the aleph script.
        p = SafePopen(['yap', '-s50000', '-h200000', '-L', Aleph.SCRIPT], cwd=self.tmpdir, stdout=PIPE).safe_run()
        stdout_str, stderr_str = p.communicate()
        
        logger.debug(stdout_str)
        logger.debug(stderr_str)
        
        logger.info("Done.")
        
        # Return the rules written in the output file.
        rules = open('%s/%s' % (self.tmpdir, filestem + Aleph.RULES_SUFFIX)).read()

        #shutil.copy('%s/%s.py' % (self.tmpdir, filestem), '/home/anzev/programiranje/sdm/results/')
        
        # Cleanup.
        self.__cleanup()
        
        return rules