예제 #1
0
파일: manage.py 프로젝트: phi-grib/flame
def action_predictions_list():
    '''
    shows a table with the list of predictions 
    '''
    # get de model repo path
    predictions_path = pathlib.Path(utils.predictions_repository_path())
    if predictions_path.is_dir() is False:
        return False, 'the predictions repository path does not exist. Please run "flame -c config".'

    # get directories in model repo path
    dirs = [x for x in predictions_path.iterdir() if x.is_dir()]

    result = []
    iresult = []
    # iterate models
    for d in dirs:

        #label is retrieved from the directory name
        label = d.parts[-1]

        #metainfo is extracted from prediction-meta picke
        with open(d.joinpath('prediction-meta.pkl'), 'rb') as handle:
            endpoint = pickle.load(handle)
            version = pickle.load(handle)
            ifile = pickle.load(handle)
            time = pickle.load(handle)
            timestamp = pickle.load(handle)
            try:
                modelID = pickle.load(handle)
            except:
                modelID = '*legacy*'

        # ifile is simplified to avoid discossing the repository
        ifile = os.path.basename(ifile)

        # ensemble models are hidden
        if label[0:8] == 'ensemble':
            continue

        # add as a tupla
        iresult.append((label, endpoint, version, time, ifile, modelID))

        # format as a text line for reverse date sorting and printing
        line = f'{label:10} {endpoint:15}   {version}   {time}   {ifile} {modelID}'
        result.append((timestamp, line))

    result.sort(reverse=True, key=getdate)

    [print(i[1]) for i in result]

    return True, iresult
예제 #2
0
def action_predictions_result (label, output='text'):
    '''
    try to retrieve the prediction result with the label used as argument
    returns 
        - (False, Null) if it there is no directory or the predictions 
          pickle files cannot be found 
        
        - (True, object) with the results otherwyse
    '''
    # get de model repo path
    predictions_path = pathlib.Path(utils.predictions_repository_path())

    label_path = predictions_path.joinpath(label)

    if not os.path.isdir(label_path):
        if output != 'text':
            return False, {'code':0, 'message': f'directory {label_path} not found'}
        print (f'directory {label_path} not found')
        return False, None

    result_path = label_path.joinpath('prediction-results.pkl')
    if not result_path.is_file():
        if output != 'text':
            return False, {'code':0, 'message': f'predictions not found for {label} directory'}
        print (f'predictions not found for {label} directory')
        return False, None

    iconveyor = Conveyor()

    with open(result_path, 'rb') as handle:
        success, message = iconveyor.load(handle)

    if not success:
        if output != 'text':
            return False, {'code':1, 'message': f'error reading prediction results with message {message}'}
        print (f'error reading prediction results with message {message}')
        return False, None

    # console output    
    print_prediction_result(('obj_num','number of objects',iconveyor.getVal('obj_num')))

    if iconveyor.isKey('external-validation'):
        for val in iconveyor.getVal('external-validation'):
            print_prediction_result (val)   

    if iconveyor.isKey('values'):
        for i in range (iconveyor.getVal('obj_num')):
            print (iconveyor.getVal('obj_nam')[i], '\t', float("{0:.4f}".format(iconveyor.getVal('values')[i])))

    # return iconveyor
    return True, iconveyor
예제 #3
0
파일: manage.py 프로젝트: phi-grib/flame
def action_predictions_remove(label):
    '''
    try to remove the prediction result with the label used as argument
    returns 
        - (False, message) if it there is no directory or the removal failed 
        - (True, OK) removal succeeded
    '''
    # get de model repo path
    predictions_path = pathlib.Path(utils.predictions_repository_path())

    label_path = predictions_path.joinpath(label)

    if not os.path.isdir(label_path):
        return (False, f'directory {label_path} not found')

    try:
        shutil.rmtree(label_path)
    except Exception as e:
        return (False, f'failed to remove {label_path} with error: {e}')

    return (True, 'OK')
예제 #4
0
파일: odata.py 프로젝트: EMVGaron/flame
    def run_apply(self):
        ''' Process the results of apply.
            The ouput generated by the prediction are:       
            1. prediction-results.pkl and prediction-meta.pkl 
            2. console output
            2. molecular descriptors file in TSV format [optional]
            3. results file in TSV format [optional]
        '''

        # if len(self.conveyor.getMain()) == 0:
        #     self.conveyor.setError('Unable to find main prediction')

        if 'ghost' not in self.format:
            opath = utils.predictions_repository_path()
            if os.path.isdir(opath):
                opath = os.path.join(opath, self.label)
                if not os.path.isdir(opath):
                    os.mkdir(opath)

        ###
        # 1. prediction-results.pkl and prediction-meta.pkl
        ###

        # Save conveyor from prediction only if confidential is False
        if not self.param.getVal(
                'confidential') and 'ghost' not in self.format:

            results_pkl_path = os.path.join(opath, 'prediction-results.pkl')
            meta_pkl_path = os.path.join(opath, 'prediction-meta.pkl')
            LOG.info(f'saving model results to: {opath}')

            # dump conveyor
            with open(results_pkl_path, 'wb') as handle:
                self.conveyor.save(handle)

            # dump metainfo
            with open(meta_pkl_path, 'wb') as handle:
                pickle.dump(self.conveyor.getMeta('endpoint'), handle)
                pickle.dump(self.conveyor.getMeta('version'), handle)
                pickle.dump(self.conveyor.getMeta('input_file'), handle)
                now = datetime.now()
                pickle.dump(now.strftime("%d/%m/%Y %H:%M:%S"), handle)
                pickle.dump(datetime.timestamp(now), handle)
                pickle.dump(self.conveyor.getMeta('modelID'), handle)
                pickle.dump(self.conveyor.getWarningMessage(), handle)
                pickle.dump(self.conveyor.getErrorMessage(), handle)

        ####
        # !  in case of error, print and return error
        ####
        if self.conveyor.getError():
            error_msg = self.conveyor.getErrorMessage()
            return False, error_msg

        ####
        # 2. console output
        ####
        #print (self.results)

        if 'ghost' not in self.format:
            self.print_result(('obj_num', 'number of objects',
                               self.conveyor.getVal('obj_num')))

            if self.conveyor.isKey('external-validation'):
                for val in self.conveyor.getVal('external-validation'):
                    self.print_result(val)

            if self.conveyor.isKey('values'):
                nams = self.conveyor.getVal('obj_nam')
                vals = self.conveyor.getVal('values')
                for i in range(self.conveyor.getVal('obj_num')):
                    # print (self.conveyor.getVal('obj_nam')[i], '\t' , float("{0:.4f}".format(self.conveyor.getVal('values')[i])))
                    print(f'{nams[i]}\t{vals[i]:.4f}')

        ###
        # 3. molecular descriptors file in TSV format [optional]
        ###
        if 'ghost' not in self.format:
            if self.param.getVal('output_md'):
                self._output_md()

        ###
        # 4. results file in TSV format [optional]
        ###
        if 'ghost' not in self.format and 'TSV' in self.format:
            LOG.info('writting results to TSV file "output.tsv"')
            # label and smiles
            key_list = ['obj_nam']
            if self.conveyor.isKey('SMILES'):
                key_list.append('SMILES')

            # main result
            key_list += self.conveyor.getMain()

            # add all object type results
            # manifest = self.results['manifest']
            # for item in manifest:
            #     if item['dimension'] == 'objs' and item['key'] not in key_list:
            #         key_list.append(item['key'])
            key_obj = self.conveyor.objectKeys()

            for i in key_obj:
                if i not in key_list:
                    key_list.append(i)

            with open('output.tsv', 'w') as fo:
                header = ''
                for label in key_list:
                    header += label + '\t'
                fo.write(header + '\n')

                obj_num = int(self.conveyor.getVal('obj_num'))

                for i in range(obj_num):
                    line = ''
                    for key in key_list:

                        ikey = self.conveyor.getVal(key)

                        if ikey is None:
                            line += '-\t'
                            continue

                        if i >= len(ikey):
                            val = None
                        else:
                            val = ikey[i]

                        if val is None:
                            line += '-'
                        else:
                            if isinstance(val, float):
                                line += "%.4f" % val
                            else:
                                line += str(val)
                        line += '\t'
                    fo.write(line + '\n')

        return True, self.conveyor
예제 #5
0
파일: config.py 프로젝트: ismaelresp/flame
def config(path: str=None) -> bool:
    """Configures model repository.

    Loads config.yaml and writes a correct model repository path
    with the path provided by the user or a default from appdirs
    if the path is not provided.
    """

    # ---- CLI interface -----

    if path is None:  # set default
        default_models_path = Path(appdirs.user_data_dir('models', 'flame'))
        default_spaces_path = Path(appdirs.user_data_dir('spaces', 'flame'))
        default_predictions_path = Path(appdirs.user_data_dir('predictions', 'flame'))

        print(f'Setting model, space and predictions repositories (default) to {default_models_path}, {default_spaces_path} and {default_predictions_path}'
              '\nWould you like to continue?(y/n)')

        if ask_user():
            if default_models_path.exists() or default_spaces_path.exists() or default_predictions_path.exists():
                print(f'These paths already exists. '
                      'Would you like to set them anyway?(y/n)')
                if ask_user():
                    utils.set_repositories(default_models_path, default_spaces_path, default_predictions_path)
                else:
                    print('aborting...')
                    return False

            else:  # models_path doesn't exists
                default_models_path.mkdir(parents=True)
                default_spaces_path.mkdir(parents=True)
                default_predictions_path.mkdir(parents=True)
                utils.set_repositories(default_models_path, default_spaces_path, default_predictions_path)

            print(f'model repository set to {default_models_path}')
            print(f'space repository set to {default_spaces_path}')
            print(f'predictions repository set to {default_predictions_path}')
            return True

        else:
            print('aborting...')
            return False

    else:  # path input by user
        in_path = Path(path).expanduser()
        in_path_models = Path.joinpath(in_path,'models')
        in_path_spaces = Path.joinpath(in_path,'spaces')
        in_path_predictions = Path.joinpath(in_path,'predictions')
        current_models_path = Path(utils.model_repository_path())
        current_spaces_path = Path(utils.space_repository_path())
        current_predictions_path = Path(utils.predictions_repository_path())

        if in_path_models == current_models_path and in_path_spaces == current_spaces_path and in_path_predictions == current_predictions_path:
            print(f'{in_path_models} already is model repository path')
            print(f'{in_path_spaces} already is space repository path')
            print(f'{in_path_predictions} already is predictions repository path')
            return False

        elif not (in_path_models.exists() and in_path_spaces.exists() and in_path_predictions.exists()):
            print("paths doesn't exists. Would you like to create it?(y/n)")

            if ask_user():
                if not in_path_models.exists():
                    in_path_models.mkdir(parents=True)
                if not in_path_spaces.exists():                
                    in_path_spaces.mkdir(parents=True)
                if not in_path_predictions.exists():                
                    in_path_predictions.mkdir(parents=True)
                utils.set_repositories(in_path_models, in_path_spaces, in_path_predictions)
            else:
                print('aborting...')
                return False

        else:  # in_path exists
            utils.set_repositories(in_path_models, in_path_spaces, in_path_predictions)

        print(f'space repository set to {in_path_spaces}')
        print(f'model repository set to {in_path_models}')
        print(f'predictions repository set to {in_path_predictions}')
        return True
예제 #6
0
    def run_apply(self):
        ''' Process the results of apply.
            The ouput generated by the prediction are:       
            1. console output
            2. molecular descriptors file in TSV format [optional]
            3. results file in TSV format [optional]
            4. this function return results in JSON format [optional]
        '''

        if len(self.conveyor.getMain()) == 0:
            self.conveyor.setError('Unable to find main prediction')
            return

        opath = utils.predictions_repository_path()
        if os.path.isdir(opath):
            opath = os.path.join(opath, self.label)
            if not os.path.isdir(opath):
                os.mkdir(opath)

        # the ouput generated by the prediction are:
        #
        # 1. console output
        # 2. molecular descriptors file in TSV format [optional]
        # 3. results file in TSV format [optional]
        # 4. this function return results in JSON format [optional]

        ####
        # 1. console output
        ####
        #print (self.results)

        self.print_result(
            ('obj_num', 'number of objects', self.conveyor.getVal('obj_num')))

        if self.conveyor.isKey('external-validation'):
            for val in self.conveyor.getVal('external-validation'):
                self.print_result(val)

        if self.conveyor.isKey('values'):
            for i in range(self.conveyor.getVal('obj_num')):
                print(
                    self.conveyor.getVal('obj_nam')[i], '\t',
                    float("{0:.4f}".format(self.conveyor.getVal('values')[i])))

        ###
        # 2. molecular descriptors file in TSV format [optional]
        ###
        if self.param.getVal('output_md'):
            self._output_md()

        ###
        # 3. results file in TSV format [optional]
        ###
        if 'TSV' in self.format:
            LOG.info('writting results to TSV file "output.tsv"')
            # label and smiles
            key_list = ['obj_nam']
            if self.conveyor.isKey('SMILES'):
                key_list.append('SMILES')

            # main result
            key_list += self.conveyor.getMain()

            # add all object type results
            # manifest = self.results['manifest']
            # for item in manifest:
            #     if item['dimension'] == 'objs' and item['key'] not in key_list:
            #         key_list.append(item['key'])
            key_obj = self.conveyor.objectKeys()

            for i in key_obj:
                if i not in key_list:
                    key_list.append(i)

            with open('output.tsv', 'w') as fo:
                header = ''
                for label in key_list:
                    header += label + '\t'
                fo.write(header + '\n')

                obj_num = int(self.conveyor.getVal('obj_num'))

                for i in range(obj_num):
                    line = ''
                    for key in key_list:

                        ikey = self.conveyor.getVal(key)

                        if ikey is None:
                            line += '-\t'
                            continue

                        if i >= len(ikey):
                            val = None
                        else:
                            val = ikey[i]

                        if val is None:
                            line += '-'
                        else:
                            if isinstance(val, float):
                                line += "%.4f" % val
                            else:
                                line += str(val)
                        line += '\t'
                    fo.write(line + '\n')

        # the function returns "True, output". output can be empty or a JSON
        output = ''

        ###
        # 4. this function return results in JSON format [optional]
        ###
        # returns a JSON with the prediction results
        if 'JSON' in self.format:
            if self.param.getVal('input_type') == 'model_ensemble':
                output = self.conveyor.getJSON(xdata=True)
            else:
                output = self.conveyor.getJSON()

        #print (self.conveyor.getJSON())

        # Save conveyor from prediction only if confidential is False
        if not self.param.getVal('confidential'):

            if not os.path.isdir(opath):
                return True, output

            results_pkl_path = os.path.join(opath, 'prediction-results.pkl')
            meta_pkl_path = os.path.join(opath, 'prediction-meta.pkl')
            LOG.info('saving model results to: {}'.format(opath))

            # dump conveyor
            with open(results_pkl_path, 'wb') as handle:
                self.conveyor.save(handle)

            # dump metainfo
            with open(meta_pkl_path, 'wb') as handle:
                pickle.dump(self.conveyor.getMeta('endpoint'), handle)
                pickle.dump(self.conveyor.getMeta('version'), handle)
                pickle.dump(self.conveyor.getMeta('input_file'), handle)
                now = datetime.now()
                pickle.dump(now.strftime("%d/%m/%Y %H:%M:%S"), handle)
                pickle.dump(datetime.timestamp(now), handle)

                # print (self.conveyor.getMeta('endpoint'))
                # print ('saving version',self.conveyor.getMeta('version'))
                # print (self.conveyor.getMeta('input_file'))

                # now = datetime.now()
                # print (now.strftime("%d/%m/%Y %H:%M:%S"))

        return True, output