def reference_evaluate_plot(ref_id, aliq):
    if fk.request.method == 'POST':
        _ref = ReferenceModel.objects.with_id(ref_id)
        if _ref is None:
            return core_response(404, 'Request suggested an empty response',
                                 'Unable to find this reference.')
        else:
            if fk.request.files:
                file_obj = fk.request.files['file']
                file_name = file_obj.filename
                _set, created = SetModel.objects.get_or_create(
                    created_at=str(datetime.datetime.utcnow()))
                if created:
                    _set.filename = '{0}-{1}'.format(str(_set.id), file_name)
                    file_path = '/tmp/{0}'.format(_set.filename)
                    try:
                        with open(file_path, 'wb') as set_file:
                            set_file.write(file_obj.read())
                        wb = load_workbook(file_path, read_only=True)
                        ws = wb.active
                        pressure = {
                            'aliq1': {
                                'run1': [],
                                'run2': []
                            },
                            'aliq2': {
                                'run1': [],
                                'run2': []
                            }
                        }
                        uptake = {
                            'aliq1': {
                                'run1': [],
                                'run2': []
                            },
                            'aliq2': {
                                'run1': [],
                                'run2': []
                            }
                        }
                        for odx, row in enumerate(ws.rows):
                            if odx >= 2:
                                # print "--- row ---"
                                if row[0].value is not None:
                                    pressure['aliq1']['run1'].append(
                                        row[0].value)
                                if row[1].value is not None:
                                    uptake['aliq1']['run1'].append(
                                        row[1].value)

                                if row[3].value is not None:
                                    pressure['aliq1']['run2'].append(
                                        row[3].value)
                                if row[4].value is not None:
                                    uptake['aliq1']['run2'].append(
                                        row[4].value)

                                if row[7].value is not None:
                                    pressure['aliq2']['run1'].append(
                                        row[7].value)
                                if row[8].value is not None:
                                    uptake['aliq2']['run1'].append(
                                        row[8].value)

                                if row[10].value is not None:
                                    pressure['aliq2']['run2'].append(
                                        row[10].value)
                                if row[11].value is not None:
                                    uptake['aliq2']['run2'].append(
                                        row[11].value)

                        evaluation = Evaluation(eval_id=_set.filename,
                                                reference=_ref,
                                                pressure=pressure,
                                                uptake=uptake)
                        evaluation.run()
                        # print str(evals)
                        os.remove(file_path)
                        _set.delete()
                        # reslt_path = evaluation.plot(aliq)
                        reslt_path = evaluation.error(aliq)
                        file_buffer = None
                        try:
                            with open(reslt_path, 'r') as _file:
                                file_buffer = StringIO(_file.read())
                            file_buffer.seek(0)
                        except:
                            print traceback.print_exc()
                        if file_buffer != None:
                            os.remove(reslt_path)
                            return fk.send_file(
                                file_buffer,
                                attachment_filename=reslt_path.split('/')[1],
                                mimetype='image/png')
                        else:
                            return core_response(
                                404, 'Request suggested an empty response',
                                'Unable to return plot image.')
                    except:
                        print traceback.print_exc()
                        _set.delete()
                        print "An error occured!!"
                        return core_response(204, 'Nothing created',
                                             'An error occured.')
                else:
                    return core_response(
                        204, 'Already exists',
                        'This should normaly never happened.')
            else:
                return core_response(204, 'Nothing created',
                                     'You must a set file.')
    # else:
    #     return core_response(405, 'Method not allowed', 'This endpoint supports only a POST method.')

    return """
def reference_evaluate_data(ref_id):
    if fk.request.method == 'POST':
        _ref = ReferenceModel.objects.with_id(ref_id)
        if _ref is None:
            return core_response(404, 'Request suggested an empty response',
                                 'Unable to find this reference.')
        else:
            if fk.request.files:
                file_obj = fk.request.files['file']
                file_name = file_obj.filename
                _set, created = SetModel.objects.get_or_create(
                    created_at=str(datetime.datetime.utcnow()))
                if created:
                    _set.filename = '{0}-{1}'.format(str(_set.id), file_name)
                    file_path = 'sets/tmp-{0}'.format(_set.filename)
                    try:
                        with open(file_path, 'wb') as set_file:
                            set_file.write(file_obj.read())
                        wb = load_workbook(file_path, read_only=True)
                        ws = wb.active
                        pressure = {
                            'aliq1': {
                                'run1': [],
                                'run2': []
                            },
                            'aliq2': {
                                'run1': [],
                                'run2': []
                            }
                        }
                        uptake = {
                            'aliq1': {
                                'run1': [],
                                'run2': []
                            },
                            'aliq2': {
                                'run1': [],
                                'run2': []
                            }
                        }
                        for odx, row in enumerate(ws.rows):
                            if odx >= 2:
                                # print "--- row ---"
                                if row[0].value is not None:
                                    pressure['aliq1']['run1'].append(
                                        row[0].value)
                                if row[1].value is not None:
                                    uptake['aliq1']['run1'].append(
                                        row[1].value)

                                if row[3].value is not None:
                                    pressure['aliq1']['run2'].append(
                                        row[3].value)
                                if row[4].value is not None:
                                    uptake['aliq1']['run2'].append(
                                        row[4].value)

                                if row[7].value is not None:
                                    pressure['aliq2']['run1'].append(
                                        row[7].value)
                                if row[8].value is not None:
                                    uptake['aliq2']['run1'].append(
                                        row[8].value)

                                if row[10].value is not None:
                                    pressure['aliq2']['run2'].append(
                                        row[10].value)
                                if row[11].value is not None:
                                    uptake['aliq2']['run2'].append(
                                        row[11].value)

                        evaluation = Evaluation(eval_id=_set.filename,
                                                reference=_ref,
                                                pressure=pressure,
                                                uptake=uptake)
                        evaluation.run()
                        os.remove(file_path)
                        _set.delete()
                        return core_response(
                            200,
                            'Results of new set {0} evaluated on reference[{1}].'
                            .format(file_name, ref_id), evaluation.results)
                    except:
                        print traceback.print_exc()
                        _set.delete()
                        print "An error occured!!"
                        return core_response(204, 'Nothing created',
                                             'An error occured.')
                else:
                    return core_response(
                        204, 'Already exists',
                        'This should normaly never happened.')
            else:
                return core_response(204, 'Nothing created',
                                     'You must a set file.')
    # else:
    #     return core_response(405, 'Method not allowed', 'This endpoint supports only a POST method.')

    return """
Exemplo n.º 3
0
def home_reference_evaluate_data():
    if fk.request.method == 'POST':
        refs = [r for r in ReferenceModel.objects()]
        _ref = refs[-1]
        if _ref is None:
            return core_response(404, 'Request suggested an empty response', 'Unable to find the newest reference.')
        else:
            if fk.request.files:
                file_obj = fk.request.files['file']
                file_name = file_obj.filename
                _set, created = SetModel.objects.get_or_create(created_at=str(datetime.datetime.utcnow()))
                if created:
                    _set.filename = '{0}-{1}'.format(str(_set.id), file_name)
                    file_path = '/tmp/{0}'.format(_set.filename)
                    try:
                        with open(file_path, 'wb') as set_file:
                            set_file.write(file_obj.read())
                        wb = load_workbook(file_path, read_only=True)
                        ws = wb.active
                        pressure = {'aliq1':{'run1':[], 'run2':[]}, 'aliq2':{'run1':[], 'run2':[]}}
                        uptake = {'aliq1':{'run1':[], 'run2':[]}, 'aliq2':{'run1':[], 'run2':[]}}
                        for odx, row in enumerate(ws.rows):
                            if odx >= 2:
                                # print "--- row ---"
                                if row[0].value is not None:
                                    pressure['aliq1']['run1'].append(row[0].value)
                                if row[1].value is not None:
                                    uptake['aliq1']['run1'].append(row[1].value)
                                
                                if row[3].value is not None:
                                    pressure['aliq1']['run2'].append(row[3].value)
                                if row[4].value is not None:
                                    uptake['aliq1']['run2'].append(row[4].value)
                                

                                if row[7].value is not None:
                                    pressure['aliq2']['run1'].append(row[7].value)
                                if row[8].value is not None:
                                    uptake['aliq2']['run1'].append(row[8].value)
                                
                                if row[10].value is not None:
                                    pressure['aliq2']['run2'].append(row[10].value)
                                if row[11].value is not None:
                                    uptake['aliq2']['run2'].append(row[11].value)

                        evaluation = Evaluation(eval_id=_set.filename, reference=_ref, pressure=pressure, uptake=uptake)
                        evaluation.run()
                        # print str(evals)
                        os.remove(file_path)
                        _set.delete()
                        reslt_path = evaluation.error()
                        file_buffer = None
                        try:
                            with open(reslt_path, 'r') as _file:
                                file_buffer = StringIO(_file.read())
                            file_buffer.seek(0)
                        except:
                            print traceback.print_exc()
                        if file_buffer != None:
                            # os.remove(reslt_path)
                            return fk.send_file(file_buffer, as_attachment=True, attachment_filename=reslt_path.split('/')[1], mimetype='image/png')
                        else:
                            return core_response(404, 'Request suggested an empty response', 'Unable to return plot image.')
                    except:
                        print traceback.print_exc()
                        _set.delete()
                        print "An error occured!!"
                        return core_response(204, 'Nothing created', 'An error occured.')
                else:
                    return core_response(204, 'Already exists', 'This should normaly never happened.')
            else:
                return core_response(204, 'Nothing created', 'You must a set file.')

    return """