Example #1
0
    def __init__(self,
                 rec_class=None,
                 api_path=None,
                 extra_payload=dict(),
                 user='******',
                 rec_score_file='rec_state.obj',
                 verbose=True,
                 warm_start=False,
                 n_recs=1,
                 datasets=False,
                 use_knowledgebase=False,
                 term_condition='n_recs',
                 max_time=5):
        """Initializes AI managing agent."""

        # default supervised learning recommender settings
        self.DEFAULT_REC_CLASS = RandomRecommender
        self.DEFAULT_REC_ARGS = {'metric': 'accuracy'}

        # recommendation engines for different problem types
        # will be expanded as more types of probles are supported
        # (classification, regression, unsupervised, etc.)
        self.rec_engines = {"classification": None}

        # Request manager settings
        self.n_recs = n_recs if n_recs > 0 else 1
        self.continuous = n_recs < 1

        # api parameters, will be removed from self once the recommenders no longer
        # call the api directly.
        # See #98 <https://github.com/EpistasisLab/pennai/issues/98>
        if api_path == None:
            api_path = ('http://' + os.environ['LAB_HOST'] + ':' +
                        os.environ['LAB_PORT'])
        self.user = user
        self.api_path = api_path
        self.api_key = os.environ['APIKEY']

        self.verbose = verbose  #False: no printouts, True: printouts on updates

        # file name of stored scores for the recommender
        self.rec_score_file = rec_score_file

        # timestamp of the last time new experiments were processed
        self.last_update = 0

        # api
        self.labApi = api_utils.LabApi(api_path=self.api_path,
                                       user=self.user,
                                       api_key=self.api_key,
                                       extra_payload=extra_payload,
                                       verbose=self.verbose)

        self.load_options()  #loads algorithm parameters to self.ui_options

        self.initilize_recommenders(rec_class)  # set self.rec_engines

        # build dictionary of ml ids to names conversion
        self.ml_id_to_name = self.labApi.get_ml_id_dict()
        # print('ml_id_to_name:',self.ml_id_to_name)

        # dictionary of dataset threads, initilized and used by q_utils.
        # Keys are datasetIds, values are q_utils.DatasetThread instances.
        #WGL: this should get moved to the request manager
        self.dataset_threads = {}

        # local dataframe of datasets and their metafeatures
        self.dataset_mf_cache = pd.DataFrame()

        # store dataset_id to hash dictionary
        self.dataset_mf_cache_id_hash_lookup = {}

        if use_knowledgebase:
            self.load_knowledgebase()

        # set termination condition
        self.term_condition = term_condition
        if self.term_condition == 'n_recs':
            self.term_value = self.n_recs
        elif self.term_condition == 'time':
            self.term_value = max_time
        else:
            self.term_value = None

        # start the request manager
        self.requestManager = RequestManager(
            ai=self,
            defaultTermConditionStr=self.term_condition,
            defaultTermParam=self.term_value)

        # if there is a pickle file, load it as the recommender scores
        assert not (warm_start), "The `warm_start` option is not yet supported"

        # for comma-separated list of datasets in datasets, turn AI request on
        assert not (
            datasets
        ), "The `datasets` option is not yet supported: " + str(datasets)
Example #2
0
    def __init__(self,
                 rec=None,
                 api_path=None,
                 extra_payload=dict(),
                 user='******',
                 rec_score_file='rec_state.obj',
                 verbose=True,
                 warm_start=False,
                 n_recs=1,
                 datasets=False,
                 use_knowledgebase=False,
                 term_condition='n_recs',
                 max_time=5):
        """initializes AI managing agent."""
        # recommender settings
        if api_path == None:
            api_path = ('http://' + os.environ['LAB_HOST'] + ':' +
                        os.environ['LAB_PORT'])
        self.rec = rec
        self.n_recs = n_recs if n_recs > 0 else 1
        self.continous = n_recs < 1

        # api parameters, will be removed from self once the recommenders no longer
        # call the api directly.
        # See #98 <https://github.com/EpistasisLab/pennai/issues/98>
        self.user = user
        self.api_path = api_path
        self.api_key = os.environ['APIKEY']

        self.verbose = verbose  #False: no printouts, True: printouts on updates

        # file name of stored scores for the recommender
        self.rec_score_file = rec_score_file

        # timestamp of the last time new experiments were processed
        self.last_update = 0

        # api
        self.labApi = api_utils.LabApi(api_path=self.api_path,
                                       user=self.user,
                                       api_key=self.api_key,
                                       extra_payload=extra_payload,
                                       verbose=self.verbose)

        self.load_options()  #loads algorithm parameters to self.ui_options

        # create a default recommender if not set
        if (rec):
            self.rec = rec
        else:
            self.rec = RandomRecommender(ml_p=self.labApi.get_all_ml_p())

        # set the registered ml parameters in the recommender
        ml_p = self.labApi.get_all_ml_p()
        assert ml_p is not None
        assert len(ml_p) > 0
        self.rec.ml_p = ml_p
        # if hasattr(self.rec,'mlp_combos'):
        #     self.rec.mlp_combos = self.rec.ml_p['algorithm']+'|'+self.rec.ml_p['parameters']

        logger.debug('self.rec.ml_p:\n' + str(self.rec.ml_p.head()))

        # tmp = self.labApi.get_all_ml_p()
        # tmp.to_csv('ml_p_options.csv')
        # build dictionary of ml ids to names conversion
        self.ml_id_to_name = self.labApi.get_ml_id_dict()
        # print('ml_id_to_name:',self.ml_id_to_name)

        # dictionary of dataset threads, initilized and used by q_utils.
        # Keys are datasetIds, values are q_utils.DatasetThread instances.
        #WGL: this should get moved to the request manager
        self.dataset_threads = {}

        # local dataframe of datasets and their metafeatures
        self.dataset_mf = pd.DataFrame()

        if use_knowledgebase:
            self.load_knowledgebase()

        # set termination condition
        self.term_condition = term_condition
        if self.term_condition == 'n_recs':
            self.term_value = self.n_recs
        elif self.term_condition == 'time':
            self.term_value = max_time
        else:
            self.term_value = None

        # start the request manager
        self.requestManager = RequestManager(
            ai=self,
            defaultTermConditionStr=self.term_condition,
            defaultTermParam=self.term_value)

        # if there is a pickle file, load it as the recommender scores
        assert not (warm_start), "The `warm_start` option is not yet supported"

        # for comma-separated list of datasets in datasets, turn AI request on
        assert not (
            datasets
        ), "The `datasets` option is not yet supported: " + str(datasets)