Ejemplo n.º 1
0
def action_info(model, version, output='text'):
    '''
    Returns a text or JSON with results info for a given model and version
    '''

    if model is None:
        return False, 'Empty model label'

    rdir = utils.model_path(model, version)
    if not os.path.isfile(os.path.join(rdir, 'results.pkl')):

        # compatibity method. use info.pkl
        if not os.path.isfile(os.path.join(rdir, 'info.pkl')):
            return False, 'Info file not found'

        with open(os.path.join(rdir, 'info.pkl'), 'rb') as handle:
            #retrieve a pickle file containing the keys 'model_build'
            #and 'model_validate' of results
            info = pickle.load(handle)
            info += pickle.load(handle)
        # end of compatibility method

    else:
        # new method, use results.pkl
        if not os.path.isfile(os.path.join(rdir, 'results.pkl')):
            return False, 'Info file not found'

        from flame.conveyor import Conveyor

        conveyor = Conveyor()
        with open(os.path.join(rdir, 'results.pkl'), 'rb') as handle:
            conveyor.load(handle)

        info = conveyor.getVal('model_build_info')
        info += conveyor.getVal('model_valid_info')

        if info == None:
            return False, 'Info not found'

    # when this function is called from the console, output is 'text'
    # write and exit
    if output == 'text':

        LOG.info(f'informing model {model} version {version}')

        for val in info:
            if len(val) < 3:
                LOG.info(val)
            else:
                LOG.info(f'{val[0]} ({val[1]}) : {val[2]}')
        return True, 'model informed OK'

    # this is only reached when this funcion is called from a web service
    # asking for a JSON

    # this code serializes the results in a list and then converts it
    # to a JSON
    json_results = []
    for i in info:
        json_results.append(conveyor.modelInfoJSON(i))

    #print (json.dumps(json_results))
    return True, json.dumps(json_results)
Ejemplo n.º 2
0
def action_info(model, version, output='text'):
    '''
    Returns a text or JSON with results info for a given model and version
    '''

    if model is None:
        return False, 'Empty model label'


    rdir = utils.model_path(model, version)
    if not os.path.isfile(os.path.join(rdir, 'results.pkl')):
        return False, 'Info file not found'

    from flame.conveyor import Conveyor

    conveyor = Conveyor()
    with open(os.path.join(rdir, 'results.pkl'), 'rb') as handle:
        conveyor.load(handle)

    # if there is an error, return the error Message        
    if conveyor.getError():
        error = conveyor.getErrorMessage()
        return False, error

    # collect warnings
    warning = conveyor.getWarningMessage()

    # collect build and validation info
    build_info =  conveyor.getVal('model_build_info')
    valid_info =  conveyor.getVal('model_valid_info')

    # merge everything 
    info = None
    for iinfo in (warning, build_info, valid_info):
        if info == None:
            info = iinfo
        else:
            if iinfo != None:
                info+=iinfo

    if info == None:
        return False, 'No relevant information found'

    # when this function is called from the console, output is 'text'
    # write and exit
    if output == 'text':

        LOG.info (f'informing model {model} version {version}')

        for val in info:
            if len(val) < 3:
                LOG.info(val)
            else:
                LOG.info(f'{val[0]} ({val[1]}) : {val[2]}')
        return True, 'model informed OK'

    # this is only reached when this funcion is called from a web service
    # asking for a JSON
    
    # this code serializes the results in a list and then converts it 
    # to a JSON  
    json_results = []
    for i in info:
        json_results.append(conveyor.modelInfoJSON(i))

    #print (json.dumps(json_results))
    return True, json.dumps(json_results)