示例#1
0
def predictThread(command, output, temp_dir=''):

    print("Thread Start")
    success, results = context.predict_cmd(command, output_format=output)
    shutil.rmtree(temp_dir)
    # print (f"Folder {temp_dir} removed")
    print("Thread End")
示例#2
0
    def put(self, request, modelname, version, format=None):

        # get the upladed file with name "file"
        try:
            file_obj = request.FILES["SDF"]
        except MultiValueDictKeyError as e:
            return JsonResponse({'error': 'Datatest not provided'},
                                status=status.HTTP_400_BAD_REQUEST)

        # Set the temp filesystem storage
        temp_dir = tempfile.mkdtemp(prefix="predict_data_", dir=None)
        fs = FileSystemStorage(location=temp_dir)
        # save the file to the new filesystem
        path = fs.save(file_obj.name, ContentFile(file_obj.read()))

        predict_data = os.path.join(temp_dir, path)

        arguments = {
            'endpoint': modelname,
            'version': int(version),
            'infile': predict_data
        }

        try:
            success, results = context.predict_cmd(arguments,
                                                   output_format='JSON')
        except Exception as e:
            return Response(str(e), status=status.HTTP_400_BAD_REQUEST)

        if success:
            return JsonResponse(json.loads(results), status=status.HTTP_200_OK)
        else:
            return Response(results, status=status.HTTP_404_NOT_FOUND)
示例#3
0
    def POST(self, ifile, model, version, temp_dir):
        
        ifile = os.path.join(tempfile.gettempdir(),temp_dir,ifile)

        # TODO: for now, only working for plain models (no external input sources)          
        model = {'endpoint' : model,
                 'version' : numeric_version(version),
                 'label': 'temp',
                 'infile' : ifile}

        success, results = context.predict_cmd(model, 'JSON')
        
        return results
示例#4
0
def main():

    LOG.debug('-------------NEW RUN-------------\n')
    parser = argparse.ArgumentParser(
        description=
        'Use Flame to either build a model from or apply a model to the input file.'
    )

    parser.add_argument('-f', '--infile', help='Input file.', required=False)

    parser.add_argument('-e',
                        '--endpoint',
                        help='Endpoint model name.',
                        required=False)

    parser.add_argument('-v',
                        '--version',
                        help='Endpoint model version.',
                        required=False)

    parser.add_argument('-a',
                        '--action',
                        help='Manage action.',
                        required=False)

    parser.add_argument(
        '-c',
        '--command',
        action='store',
        choices=['predict', 'build', 'manage', 'config'],
        help='Action type: \'predict\' or \'build\' or \'manage\'',
        required=True)

    # parser.add_argument('-log', '--loglevel',
    #                     help='Logger level of verbosity',)

    parser.add_argument('-p',
                        '--path',
                        help='Defines de new path for models repository.',
                        required=False)

    args = parser.parse_args()

    # init logger Level and set general config
    # another way around would be create a handler with the level
    # and append it to the global instance of logger

    # if args.loglevel:
    #     numeric_level = getattr(logging, args.loglevel.upper(), None)
    #     if not isinstance(numeric_level, int):
    #         raise ValueError('Invalid log level: {}'.format(args.loglevel))
    #     logging.basicConfig(level=numeric_level)

    # make sure flame has been configured before running any command, unless this command if used to
    # configure flame
    if args.command != 'config':
        configuration_warning()

    if args.command == 'predict':

        if (args.endpoint is None) or (args.infile is None):
            print(
                'flame predict : endpoint and input file arguments are compulsory'
            )
            return

        version = utils.intver(args.version)

        command_predict = {
            'endpoint': args.endpoint,
            'version': version,
            'infile': args.infile
        }

        LOG.info(f'Starting prediction with model {args.endpoint}'
                 f' version {version} for file {args.infile}')

        success, results = context.predict_cmd(command_predict)
        # print('flame predict : ', success, results)

    elif args.command == 'build':

        if (args.endpoint is None) or (args.infile is None):
            print(
                'flame build : endpoint and input file arguments are compulsory'
            )
            return

        command_build = {'endpoint': args.endpoint, 'infile': args.infile}

        LOG.info(f'Starting building model {args.endpoint}'
                 f' with file {args.infile}')

        success, results = context.build_cmd(command_build)
        # print('flame build : ', success, results)

    elif args.command == 'manage':
        manage_cmd(args)

    elif args.command == 'config':
        config(args.path)
        change_config_status()
示例#5
0
def main():

    LOG.debug('-------------NEW RUN-------------\n')
    parser = argparse.ArgumentParser(
        description=
        f'Flame version {__version__}. Use Flame to build and manage predictive models or to predict using them.'
    )

    parser.add_argument('-f', '--infile', help='Input file.', required=False)

    parser.add_argument('-e',
                        '--endpoint',
                        help='Endpoint model name.',
                        required=False)

    parser.add_argument('-s',
                        '--space',
                        help='Chemical space name.',
                        required=False)

    parser.add_argument('-v',
                        '--version',
                        help='Endpoint model version.',
                        required=False)

    parser.add_argument('-a',
                        '--action',
                        help='Manage action.',
                        required=False)

    parser.add_argument('-p',
                        '--parameters',
                        help='File with parameters for the current action.',
                        required=False)

    parser.add_argument(
        '-c',
        '--command',
        action='store',
        choices=['predict', 'search', 'build', 'sbuild', 'manage', 'config'],
        help=
        'Action type: \'predict\' or \'search\' or \'build\' \'sbuild\' or \'manage\' or \'config\'',
        required=True)

    # parser.add_argument('-log', '--loglevel',
    #                     help='Logger level of verbosity',)

    parser.add_argument(
        '-d',
        '--directory',
        help=
        'Defines the root directory for the models and spaces repositories.',
        required=False)
    parser.add_argument('-t',
                        '--documentation_file',
                        help='File with manually filled documentation fields.',
                        required=False)

    parser.add_argument(
        '-l',
        '--label',
        help='Label for facilitating the identification of the prediction.',
        required=False)

    parser.add_argument(
        '-inc',
        '--incremental',
        help=
        'The input file must be added to the existing training series. Only for "build" command.',
        action='store_true',
        required=False)

    parser.add_argument('--smarts',
                        help='SMARTS string used as input for similarity',
                        required=False)

    args = parser.parse_args()

    # init logger Level and set general config
    # another way around would be create a handler with the level
    # and append it to the global instance of logger

    # if args.loglevel:
    #     numeric_level = getattr(logging, args.loglevel.upper(), None)
    #     if not isinstance(numeric_level, int):
    #         raise ValueError('Invalid log level: {}'.format(args.loglevel))
    #     logging.basicConfig(level=numeric_level)

    if args.infile is not None:
        if not os.path.isfile(args.infile):
            LOG.error(f'Input file {args.infile} not found')
            return

    # make sure flame has been configured before running any command, unless this command if used to
    # configure flame
    if args.command != 'config':
        utils.config_test()

    if args.command == 'predict':

        if (args.endpoint is None) or (args.infile is None):
            LOG.error(
                'flame predict : endpoint and input file arguments are compulsory'
            )
            return

        version = utils.intver(args.version)

        if args.label is None:
            label = 'temp'
        else:
            label = args.label

        command_predict = {
            'endpoint': args.endpoint,
            'version': version,
            'label': label,
            'infile': args.infile
        }

        LOG.info(
            f'Starting prediction with model {args.endpoint}'
            f' version {version} for file {args.infile}, labelled as {label}')

        success, results = context.predict_cmd(command_predict)
        if not success:
            LOG.error(results)

    elif args.command == 'search':

        if (args.space is None) or (args.infile is None
                                    and args.smarts is None):
            LOG.error(
                'flame search : space and input file arguments are compulsory')
            return

        version = utils.intver(args.version)
        if args.label is None:
            label = 'temp'
        else:
            label = args.label

        command_search = {
            'space': args.space,
            'version': version,
            'infile': args.infile,
            'smarts': args.smarts,
            'runtime_param': args.parameters,
            'label': label
        }

        LOG.info(
            f'Starting search on space {args.space}'
            f' version {version} for file {args.infile}, labelled as {label}')

        success, results = context.search_cmd(command_search)

        if not success:
            LOG.error(results)

    elif args.command == 'build':

        if (args.endpoint is None):
            LOG.error('flame build : endpoint argument is compulsory')
            return

        command_build = {
            'endpoint': args.endpoint,
            'infile': args.infile,
            'param_file': args.parameters,
            'incremental': args.incremental
        }

        LOG.info(f'Starting building model {args.endpoint}'
                 f' with file {args.infile} and parameters {args.parameters}')

        success, results = context.build_cmd(command_build)

        if not success:
            LOG.error(results)

    elif args.command == 'sbuild':

        if (args.space is None):
            LOG.error('flame sbuild : space argument is compulsory')
            return

        command_build = {
            'space': args.space,
            'infile': args.infile,
            'param_file': args.parameters
        }

        LOG.info(f'Starting building model {args.space}'
                 f' with file {args.infile} and parameters {args.parameters}')

        success, results = context.sbuild_cmd(command_build)

        if not success:
            LOG.error(results)

    elif args.command == 'manage':
        success, results = context.manage_cmd(args)
        if not success:
            LOG.error(results)

    elif args.command == 'config':
        success, results = config.configure(args.directory,
                                            (args.action == 'silent'))
        if not success:
            LOG.error(f'{results}, configuration unchanged')