import numpy as np
from osgeo import gdal, osr
import matplotlib.pylab as plt
import os
import time
import math
import glob
import datetime
import scipy as sp
from scipy.ndimage import label

# Import UMEP tools
import TreeGenerator.makevegdems as makevegdems
from SOLWEIG.shadowingfunction_wallheight_23 import shadowingfunction_wallheight_23
import SOLWEIG.Solweig_v2015_metdata_noload as metload
import SOLWEIG.Solweig1D_2019a_calc as so
from SOLWEIG.clearnessindex_2013b import clearnessindex_2013b
from SOLWEIG.wallalgorithms import findwalls
from SOLWEIG.misc import saveraster

# Import functions and classes for Tree planter
import TreePlanterCalc
import TreePlanterOptInit
from treeplanterclasses import Inputdata
from treeplanterclasses import Treedata
from treeplanterclasses import Regional_groups
from SOLWEIG_1D import tmrt_1d_fun

print(datetime.datetime.now().time())
start_time = time.time()

#### Paths
# Gustaf Adolfs Torg
infolder = 'C:/Users/xwanil/Desktop/Project_4/VegetationProject_Py3/inputdata/GustafAdolfSmall/'
#infolder = 'C:/Users/xwanil/Desktop/Project_4/VegetationProject_Py3/inputdata/GustafAdolfNSmall/'
#infolder = 'C:/Users/xwanil/Desktop/Project_4/VegetationProject_Py3/inputdata/GustafAdolfNorth/'
outfolder = 'C:/Users/xwanil/Desktop/Project_4/Output_ny_test/'
outfolder = 'C:/Users/xwanil/Desktop/Project_4/Output_tmrt_round/'
outfolder = 'C:/Users/xwanil/Desktop/Project_4/Output_dummy/'
metfilepath = infolder + 'gothenburg_1983_173.txt'

infolder2 = infolder + 'SOLWEIG_RUN/'

# Dummy streets
# East-West street canyon
#infolder = 'C:/Users/xwanil/Desktop/Project_4/Dummy/SOLWEIG_EW/'
# North-South street canyon
#infolder = 'C:/Users/xwanil/Desktop/Project_4/Dummy/SOLWEIG_NS/'
# SouthEast-NorthWest street canyon
#infolder = 'C:/Users/xwanil/Desktop/Project_4/Dummy/SOLWEIG_SENW/'
# SouthWest-NorthEast street canyon
#infolder = 'C:/Users/xwanil/Desktop/Project_4/Dummy/SOLWEIG_SWNE/'
# East-West street canyon 400x30 meters, H/W 0.5, 10 meter height, 20 meter width
#infolder = 'C:/Users/xwanil/Desktop/Project_4/Dummy/SOLWEIG_EW_400x30/HW05/'
#infolder = 'C:/Users/xwanil/Desktop/Project_4/Dummy/SOLWEIG_EW_300x30/HW05/'
# Study area
#infolder = 'C:/Users/xwanil/Desktop/Project_4/StudyArea/'
infolder = 'C:/Users/xwanil/Desktop/Project_4/StudyAreaJarntorget/'
# SOLWEIG output
infolder2 = infolder + 'SOLWEIG_OUT/'
# Tree planter output
outfolder = infolder + 'TreePlanterOutNew/'
# Met file
metfilepath = infolder + 'gothenburg_1983_173.txt'

## Searching for shadow and tmrt files in infolder
sh_fl = [f for f in glob.glob(infolder2 + 'Shadow_*')]
tmrt_fl = [f for f in glob.glob(infolder2 + 'Tmrt_*')]

# Creating vector with hours from file names
h_fl = np.zeros((sh_fl.__len__(),1))
for iz in range(sh_fl.__len__()):
    h_fl[iz,0] = np.float(sh_fl[iz][-9:-5])

# Which hours to look between
h_range = 1
#h_r1 = '0900'
#h_r2 = '1600'
h_r1 = '0900'
h_r2 = '1000'
#h_r1 = '1200'
#h_r2 = '1300'
#h_r1 = '1500'
#h_r2 = '1600'
if h_range == 1:
    for ix in range(sh_fl.__len__()):
        if h_r1 in sh_fl[ix]:
            r1 = ix
            break

    for iy in range(sh_fl.__len__()):
        if h_r2 in sh_fl[iy]:
            r2 = iy
            break

else:
    r1 = 0
    r2 = sh_fl.__len__()

r_range = range(r1,r2)

# Reading first file in folder to get extent (rows and cols)
dataSet = gdal.Open(sh_fl[1])
temp = dataSet.ReadAsArray().astype(np.float)
rows = temp.shape[0]
cols = temp.shape[1]

aoi = 1 # Area of interest

# Loading all shadow and tmrt rasters and summing up all tmrt into one
tree_input = Inputdata(r_range, sh_fl, tmrt_fl, infolder, infolder2, rows, cols, aoi)

# Loading rasters
dataSet = gdal.Open(infolder + 'DSM_12_00.tif')
tree_input.dsm = dataSet.ReadAsArray().astype(np.float)
dataSet = gdal.Open(infolder + 'DEM_12_00.tif')
tree_input.dem = dataSet.ReadAsArray().astype(np.float)

veg = 1     # CDSM
#veg = 0     # No CDSM
if veg == 1:
    dataSet = gdal.Open(infolder + 'CDSM_12_00.tif')
    tree_input.cdsm = dataSet.ReadAsArray().astype(np.float)
    tree_input.cdsm_b = tree_input.cdsm == 0
    tree_input.buildings = (tree_input.buildings == True) & (tree_input.cdsm_b == True)

#saveraster(tree_input.dataSet, 'C:/Users/xwanil/Desktop/Project_4/StudyAreaJarntorget/TreePlanterOut/5_trees/1000_1500/10/buildings_cdsm.tif', tree_input.buildings)

del dataSet

tau = 0.03      # ska in i widget bla bla

tmrt_1d, azimuth, altitude, amaxvalue = tmrt_1d_fun(metfilepath,tau,tree_input.lon,tree_input.lat,tree_input.dsm,r_range)

tmrt_1d = np.around(tmrt_1d, decimals=1)

# Tree specs
#ttype = 1       # Conifer
ttype = 2       # Deciduous
trunk = 2       # Trunk height
height = 5     # Tree height
dia = 3         # Canopy diameter
treey = math.ceil(rows / 2)  # Y-position of tree in empty setting. Y-position is in the middle of Y.
treex = math.ceil(cols / 2)  # X-position of tree in empty setting. X-position is in the middle of X.

treedata = Treedata(ttype, height, trunk, dia, treey, treex)

bld_orig = tree_input.buildings.copy()

tree_input.tmrt_s = tree_input.tmrt_s * tree_input.buildings  # Remove all Tmrt values that are in shade or on top of buildings

rows = tree_input.tmrt_s.shape[0]  # Y-extent of studied area
cols = tree_input.tmrt_s.shape[1]  # X-extent for studied area

cdsm_ = np.zeros((rows, cols))  # Empty cdsm
tdsm_ = np.zeros((rows, cols))  # Empty tdsm
dsm_empty = np.ones((rows, cols))  # Empty dsm raster
buildings_empty = np.ones((rows, cols))  # Empty building raster

rowcol = rows*cols

cdsm_, tdsm_ = makevegdems.vegunitsgeneration(buildings_empty, cdsm_, tdsm_, treedata.ttype, treedata.height, treedata.trunk, treedata.dia, treedata.treey, treedata.treex,
                                               cols, rows, tree_input.scale)

treebush = np.zeros((rows, cols))  # Empty tree bush matrix

treewalls = np.zeros((rows, cols))  # Empty tree walls matrix
treewallsdir = np.zeros((rows, cols))  # Empty tree walls direction matrix

treesh_ts1 = np.zeros((rows, cols, r_range.__len__()))      # Shade for each timestep, shade = 0
treesh_ts2 = np.zeros((rows, cols, r_range.__len__()))      # Shade for each timestep, shade = 1
treesh_sum_sh = np.zeros((rows,cols))                       # Sum of shade for all timesteps
treesh_sum_tmrt = np.zeros((rows, cols))                    # Sum of tmrt for all timesteps

dem_temp = np.ones((rows,cols))

# Create shadow for new tree
i_c = 0
for i in r_range:
    vegsh, sh, _, wallsh, wallsun, wallshve, _, facesun = shadowingfunction_wallheight_23(dem_temp, cdsm_, tdsm_,
                                                                                azimuth[0][i], altitude[0][i], tree_input.scale,
                                                                                amaxvalue, treebush, treewalls,
                                                                                treewallsdir * np.pi / 180.)

    treesh_ts1[:, :, i_c] = vegsh
    treesh_ts2[:, :, i_c] = (1 - vegsh)
    treesh_sum_sh = treesh_sum_sh + treesh_ts2[:,:,i_c] * i
    treesh_sum_tmrt[:, :] = treesh_sum_tmrt + treesh_ts2[:,:,i_c] * tmrt_1d[i_c,0]
    i_c += 1

## Regional groups for tree shadows
shadow_rg = Regional_groups(r_range, treesh_sum_sh, treesh_ts2, tmrt_1d)

from scipy.signal import argrelextrema
locm_y_ts, locm_x_ts = argrelextrema(tree_input.tmrt_ts[:,:,0], np.greater)
#locm_y_smx, locm_x_smx = argrelextrema(tree_input.tmrt_smooth_smx[:,:,0], np.greater)

#print(locm_y_ts.shape[0], locm_y_smx.shape[0])
print(locm_y_ts.shape[0])

#tree_input.tmrt_ts = tree_input.tmrt_smooth_smx

## Creating matrices with Tmrt for tree shadows at each possible position
treerasters, positions = TreePlanterCalc.treeplanter(tree_input, treedata, treesh_sum_tmrt, treesh_ts1, tmrt_1d , shadow_rg.shadow)

tree_input.padding(tree_input.shadow, tree_input.tmrt_ts, tree_input.buildings, treerasters.tpy[0], treerasters.tpx[0])

trees = 5

print(np.max(treerasters.d_tmrt))

print('Possible positions for a tree = ' + str(positions.pos.shape[0]))

d_tmrt_vector = treerasters.d_tmrt.flatten()
max_values = d_tmrt_vector[np.argsort(d_tmrt_vector)[-trees:]]
print(np.sum(max_values))

#n_trees = np.array([2,3,4])
#n_trees = np.array([2,3,4,5,6,7,8])
n_trees = np.array([trees])
#n_r = np.array([100,200,300,400,500,600,700,800,900,1000,1100,1200,1300,1400,1500,1600,1700,1800,1900,2000,3000,4000,5000,6000,7000,8000,9000,10000])
#n_r = np.array([20000])
random_iterations = 20000
n_r = np.ones((5), dtype=np.int)*random_iterations
#n_trees = np.array([3])
#n_r = np.array([100,200,300,400,500,600,700,800,900,1000])
#n_r = np.array([10,100,500,1000,2000,3000,4000,5000])
n_r = np.array([10,100,500,1000,2000,3000,4000,5000,10000,20000])
n_r = np.array([10000])
#n_r = np.array([10000])
#n_r = np.array([2000])

loop = n_r.shape[0]
#n_ir = np.array([1])
for idy in n_trees:
    trees = idy
    t_max = np.zeros((1, n_r.shape[0]))
    r_time = np.zeros((1, n_r.shape[0]))
    tree_table = np.zeros((n_r.shape[0], trees*2))

    for idx in range(loop):
        print(idx)
        s_time = time.time()
        r_iters = n_r[idx]

        # Running tree planter
        t_y, t_x, unique_tmrt, unique_tmrt_max, tree_paths = TreePlanterOptInit.treeoptinit(treerasters, tree_input, positions, treedata,
                                                                                shadow_rg, tmrt_1d, trees, r_iters)

        treeshadow_tmrt = np.zeros((rows, cols, trees))
        cdsm_tmrt = np.zeros((rows, cols, trees))
        tdsm_tmrt = np.zeros((rows, cols, trees))

        treeshadow_new = np.zeros((rows,cols))
        cdsm_new = np.zeros((rows,cols))
        tdsm_new = np.zeros((rows,cols))

        for i in range(0,trees):
            cdsm_ = np.zeros((rows,cols))       # Empty cdsm
            tdsm_ = np.zeros((rows,cols))       # Empty tdsm

            cdsm_tmrt[:,:,i], tdsm_tmrt[:,:,i] = makevegdems.vegunitsgeneration(bld_orig, cdsm_, tdsm_, ttype, height, trunk, dia, t_y[i], t_x[i], cols, rows, tree_input.scale)

            cdsm_new = cdsm_new + cdsm_tmrt[:, :, i]
            tdsm_new = tdsm_new + tdsm_tmrt[:, :, i]

            for j in r_range:
                vegsh, sh, _, wallsh, wallsun, wallshve, _, facesun = shadowingfunction_wallheight_23(dem_temp, cdsm_tmrt[:,:,i], tdsm_tmrt[:,:,i],
                                                                                azimuth[0][j], altitude[0][j], tree_input.scale,
                                                                                amaxvalue, treebush, treewalls, treewallsdir * np.pi / 180.)

                treeshadow_tmrt[:,:,i] = 1-vegsh
                treeshadow_new = treeshadow_new + treeshadow_tmrt[:,:,i]

        # Outputs
        if not os.path.exists(outfolder):   # Create folder if not existing
            os.makedirs(outfolder)

        rnr = str(idx)
        t_p = str(trees) + '_trees'
        fm_str = 'tmrt_' + str(trees) + '_trees_' + str(r_iters) + '_' + rnr + '.tif'

        t_dir = outfolder + t_p + '/' + h_r1 + '_' + h_r2 + '/' + str(r_iters)

        if not os.path.exists(t_dir):  # Create folder if not existing
            os.makedirs(t_dir)

        saveraster(tree_input.dataSet, t_dir + '/treeshadow_' + fm_str, treeshadow_new)
        saveraster(tree_input.dataSet, t_dir + '/cdsm_' + fm_str, cdsm_new)
        #saveraster(dataSet, t_dir + '/tdsm_' + fm_str, tdsm_new)
        #saveraster(dataSet, t_dir + '/sun_vs_tsh_' + fm_str, sun_vs_tsh)
        #saveraster(dataSet, t_dir + '/sum_tmrt.tif', sum_tmrt)
        #saveraster(dataSet, t_dir + '/sum_tmrt_sun_' + fm_str, sum_tmrt_s)
        #saveraster(dataSet, t_dir + '/sum_tmrt_sh_' + fm_str, sum_tmrt_tsh)

        tree_paths_all = np.zeros((rows,cols))
        for i in range(tree_paths.shape[2]):
            tree_paths_all += tree_paths[:,:,i]
        saveraster(tree_input.dataSet, t_dir + '/tree_paths_' + fm_str, tree_paths_all)

        if idx == 0:
            saveraster(tree_input.dataSet, t_dir + '/d_tmrt_' + fm_str, treerasters.d_tmrt)

        #if unique_tmrt_max[0].__len__() <= 1:
        #    print(unique_tmrt[unique_tmrt_max[0][0],:])
        #else:
        #    for ix in range(unique_tmrt_max[0].__len__()):
        #        print(unique_tmrt[unique_tmrt_max[0][ix],:])

        t_max[0,idx] = unique_tmrt[unique_tmrt_max[0][0],trees]
        r_time[0,idx] = time.time() - s_time

        print(t_max[0,idx])
        print(r_time[0,idx])

        for i in range(trees):
            tree_table[idx,i] = t_y[i]
            tree_table[idx,i+trees] = t_x[i]

        test = 0

        #print(t_y)
        #print(t_x)

        #print('--- ' + str(time.time() - s_time) + ' seconds to finish ' + str(
        #    r_iters) + ' randomization runs with ' + str(trees) + ' trees ---')

    out_fig = outfolder + 'Figures/'

    if not os.path.exists(out_fig):  # Create folder if not existing
        os.makedirs(out_fig)

    # Figure
    plt.scatter(n_r, t_max)
    plt.xlabel('Iterations')
    plt.ylabel(r'Decrease in T$_{mrt}$')
    f_name = out_fig + 'iters_vs_tmrt_test_' + str(random_iterations) + '_' + str(h_r1) + '_' + str(h_r2) + '_' + str(trees) + '_trees' + '.tif'
    plt.savefig(f_name, dpi=150)
    plt.clf()

    # Table
    t_out = np.zeros((n_r.shape[0], 4+trees*2))
    t_out[:, 0] = n_r
    t_out[:, 1] = t_max
    t_out[:, 2] = r_time
    t_out[:, 3] = 0#i_time
    t_out[:, 4:4+trees*2] = tree_table
    np.around(t_out, 1)
    t_head = 'Iterations Tmrt Time i_Time ' + 'y '*trees + 'x '*trees
    txt_dir = out_fig + 'table_' + str(random_iterations) + '_' + str(h_r1) + '_' + str(h_r2) + '_' + str(trees) + '_trees' + '.txt'
    np.savetxt(txt_dir, t_out, fmt='%1.2f', header=t_head)

print('--- ' + str(time.time() - start_time) + ' seconds to finish ---')

print(datetime.datetime.now().time())


# -*- coding: utf-8 -*-
"""
Created on Wed Feb 19 13:24:25 2020

@author: jubco
"""

def flatten(listBoi, memory=[]):
    for x in listBoi:           #for every element in the list
        if isinstance(x, list): #If it is another list, then:
            flatten(x,memory)   #We repeat the last few steps on that list
        else:
            memory.append(x)    #Otherwise, we add it to our memory

    return memory

print("Flatten on example: ", flatten([[1,2,3], [[4],[5]], 6,7,8]))
############################################################################

def powSet(setBoi):
    if len(setBoi) > 0:             #If not an empty set
        start = powSet(setBoi[:-1]) #split with recursion so you get everything minus the last element each recurse
        #print("First: ", first)
        return start + [elem + [setBoi[-1]] for elem in start] #Return every start + one of every element + last element
    else:
        return [[]]

print("\nPowerset of 1 2 3: ", powSet([1,2,3]))
############################################################################

from itertools import permutations #I knew about the import already so i just used that

def allPerms(listBoi):
    result = list(permutations(listBoi))
    return(result)

print("\nPermutations of 1 2 3: ", allPerms([1,2,3]), "\n")
############################################################################

import numpy as np

def spiral(n, end_corner):
    mat = np.full((n, n), -1)
    curnum = (n**2)-1
    if end_corner == 1:         #Upper Left
        curpos = (0,0)
        mat[curpos] = (n**2)-1
        cardinality = 'east'
    elif end_corner == 2:       #Upper Right
        curpos = (0,n-1)
        mat[curpos] = (n**2)-1
        cardinality = 'south'
    elif end_corner == 3:       #Lower Right
        curpos = (n-1,n-1)
        mat[curpos] = (n**2)-1
        cardinality = 'west'
    elif end_corner == 4:       #Lower left
        curpos = (n-1,0)
        mat[curpos] = (n**2)-1
        cardinality = 'north'
    else:
        print("Incorrect input, end_corner must be between 1 and 4")
        return 0

    for i in range(n):
        #If we are going right, keep decrementing to the right until you hit the last col or a non -1
        while(cardinality == 'east' and curpos[1]+1 != n and mat[(curpos[0]),(curpos[1])+1] == -1):
            curpos = ((curpos[0]),(curpos[1])+1)
            curnum -= 1
            mat[curpos] = curnum
        #Change direction
        if cardinality == 'east':
            if curpos[1]+1 == n or (mat[(curpos[0]),(curpos[1]+1)] != -1):
                cardinality = 'south'

        #If we are going south
        while(cardinality == 'south' and curpos[0]+1 != n and mat[(curpos[0]+1),(curpos[1])] == -1):
            curpos = ((curpos[0])+1,(curpos[1]))
            curnum -= 1
            mat[curpos] = curnum
        if cardinality == 'south':
            if curpos[0]+1 == n or (mat[(curpos[0]+1),(curpos[1])] != -1):
                cardinality = 'west'

        #If we are going west
        while(cardinality == 'west' and curpos[1]-1 != -1 and mat[(curpos[0]),(curpos[1]-1)] == -1):
            curpos = ((curpos[0]),(curpos[1])-1)
            curnum -= 1
            mat[curpos] = curnum
        if cardinality == 'west':
            if curpos[0]+1 == n or (mat[(curpos[0]),(curpos[1]-1)] != -1):
                cardinality = 'north'

        #If we are going north
        while(cardinality == 'north' and curpos[0]-1 != -1 and mat[(curpos[0]-1),(curpos[1])] == -1):
            curpos = ((curpos[0]-1),(curpos[1]))
            curnum -= 1
            mat[curpos] = curnum
        if cardinality == 'north':
            if curpos[0]-1 == n or (mat[(curpos[0]-1),(curpos[1])] != -1):
                cardinality = 'east'

    return mat

print("Spiral:\n",spiral(10,4))


from datetime import datetime
import hashlib

import grpc
from rest_framework import status
from rest_framework.decorators import api_view, parser_classes
from rest_framework.request import Request
from rest_framework.response import Response
from django.core.files.uploadedfile import InMemoryUploadedFile

import unidecode
from .serializers import *
from authentication.models import User
from grpc_client.client import Client
from drf_yasg.utils import swagger_auto_schema, no_body
from drf_yasg.openapi import Parameter, Schema
from backend import redis_connect
from drf_yasg import openapi
from grpc_client.file_service_pb2 import MetaData
from django.http import HttpResponse, JsonResponse
from django.core import exceptions
from wsgiref.util import FileWrapper
import unicodedata
from io import BytesIO
from rest_framework.generics import get_object_or_404
from rest_framework.parsers import FormParser, MultiPartParser

from base64 import b64encode

@swagger_auto_schema(methods=['get'], responses={200: FileSerializer(many=True)})
@api_view(['GET'])
def list_files(request: Request):
    user = User.objects.get(pk=request.user.pk)
    file_list: list[MetaData] = Client().get_file_list(user.get_bucket_name())
    files_in_db = user.files.all()
    for metadata in file_list:
        file, created = files_in_db.get_or_create(
            name=metadata.filename,
            defaults={
                'name': metadata.filename,
                'last_modified': metadata.last_modified,
                'hash': metadata.hash,
                'size': metadata.size,
                'content_type': metadata.content_type,
                'user_id': user.pk
            }
        )
        if not created:
            file.last_modified = metadata.last_modified
            file.hash = metadata.hash
            file.size = file.size
            file.content_type = file.content_type
        else:
            user.files.add(file)
        file.save()
    files_in_db.exclude(name__in=[f.filename for f in file_list]).delete()
    try:
        files_serializer = FileSerializer(user.files.all(), many=True)
        return Response(files_serializer.data)
    except Exception as e:
        print(e)
    return Response(status=status.HTTP_404_NOT_FOUND)

file_parameter = Parameter('file_name', openapi.IN_QUERY, type=openapi.TYPE_STRING, required=True)

@swagger_auto_schema(
    methods=['get'],
    responses={
        200: openapi.Schema(type=openapi.TYPE_FILE, format=openapi.FORMAT_BINARY),
        404: "Not Found",
    },
    manual_parameters=[file_parameter]
)
@api_view(['GET'])
def get_file(request: Request):
    file_name: str = request.query_params.get('file_name')
    user = User.objects.get(pk=request.user.pk)
    # file_serializer = FileSerializer(request.data)
    # file = File.objects.get(name=file_name)
    # user.folder.files.objects.get(name=)
    # info = Client().download_file(bucket_name=)
    # file_serializer = FileSerializer(name=info.name, last_modified=info.date, hash=info.hash)
    try:
        response = Client().download_file(bucket_name=user.get_bucket_name(), file_name=file_name)
    except grpc.RpcError as e:
        print(e)
        return Response("Not Found", status=status.HTTP_404_NOT_FOUND)

    data = b64encode(response.chunk_data)
    meta = response.meta

    db_file, created = user.files.get_or_create(name=meta.filename, defaults={
        'name': meta.filename,
        'size': meta.size,
        'hash': meta.hash,
        'last_modified': meta.last_modified,
        'content_type': meta.content_type
    })
    if not created:
        db_file.size = meta.size
        db_file.hash = meta.hash
        db_file.last_modified = meta.last_modified
        db_file.content_type = meta.content_type
    response = HttpResponse(data, content_type=meta.content_type)
    response['Content-Disposition'] = unidecode.unidecode('attachment; filename="' + file_name + '"')
    return response

@swagger_auto_schema(
    methods=['post'],
    manual_parameters=[
        Parameter(name='file', in_=openapi.IN_FORM, type=openapi.TYPE_FILE, required=True),
        Parameter(name='last_modified', in_=openapi.IN_FORM, type=openapi.TYPE_STRING, required=True)
    ],
    responses={
        200: FileSerializer(),
        409: "Old file",
        302: "Equal file",
        400: "Error"
    }
)
@api_view(['POST'])
@parser_classes([MultiPartParser])
def post_file(request: Request):
    user = User.objects.get(pk=request.user.pk)
    uploaded_file: InMemoryUploadedFile = request.FILES['file']
    file = File(
        name=uploaded_file.name,
        size=uploaded_file.size,
        last_modified=request.data['last_modified'],
        hash=md5(uploaded_file),
        content_type=uploaded_file.content_type
    )

    try:
        server_file_meta = Client().get_file_info(bucket_name=user.get_bucket_name(), file_name=uploaded_file.name)
    except grpc.RpcError as e:
        if e.code() == grpc.StatusCode.NOT_FOUND:
            file.user = user
            file.save()
            user.files.filter(name=file.name).exclude(pk=file.pk).delete()
            metadata: MetaData = Client().upload_file(file=file, data=uploaded_file)
            file.size = metadata.size
            file.hash = metadata.hash
            file.last_modified = metadata.last_modified
            file.content_type = metadata.content_type
            file.save()

            return Response(FileSerializer(file).data, status=status.HTTP_200_OK)
        else:
            return Response(status=status.HTTP_400_BAD_REQUEST)

    if server_file_meta.hash == file.hash:
        return Response("Equal file", status=status.HTTP_302_FOUND)

    if file.last_modified < server_file_meta.last_modified:
        return Response("Old file", status=status.HTTP_409_CONFLICT)
    else:
        file.user = user
        file.save()
        user.files.filter(name=file.name).exclude(pk=file.pk).delete()
        metadata: MetaData = Client().upload_file(file=file, data=uploaded_file)
        file.size = metadata.size
        file.hash = metadata.hash
        file.last_modified = metadata.last_modified
        file.content_type = metadata.content_type
        file.save()
    return Response(FileSerializer(file).data, status=status.HTTP_200_OK)

@api_view(['POST'])
def logout(request: Request):
    user = User.objects.get(pk=request.user.pk)
    response = Response(status=status.HTTP_200_OK)
    response.delete_cookie(key='uid')
    redis_connect.delete(user.pk)
    return response

@swagger_auto_schema(
    methods=['delete'],
    manual_parameters=[
        Parameter(name='file_name', in_=openapi.IN_QUERY, type=openapi.TYPE_STRING, required=True)
    ],
    responses={
        200: "OK",
        204: "No Content"
    }
)
@api_view(['DELETE'])
def remove_file(request: Request):
    user = User.objects.get(pk=request.user.pk)
    file_name = request.query_params.get('file_name')
    try:
        Client().remove_file(file_name=file_name, bucket_name=user.get_bucket_name())
    except:
        return Response("No Content", status=status.HTTP_204_NO_CONTENT)
    try:
        user.files.get(name=file_name).delete()
    except:
        pass
    return Response("OK", status=status.HTTP_200_OK)

def md5(file: InMemoryUploadedFile):
    hash_md5 = hashlib.md5()
    for chunk in file.chunks():
        hash_md5.update(chunk)
    return hash_md5.hexdigest()


#!/usr/bin/python3
import os,sys,time,importlib,simplejson
if sys.platform[0] == 'w': figpath=os.getenv('USERPROFILE') +os.sep+'.rt'+os.sep+ '.config.hash'
else: figpath=os.getenv('HOME') +os.sep+'.rt'+os.sep+ '.config.hash'
def getTable( file ):
		json_data={}
		if os.path.isfile(file):
				with open(file,'r', encoding="latin-1") as json_file: json_data = simplejson.load(json_file)
		return json_data
fig=getTable(figpath)
sys.path.append( fig['w']+'/widgets/python'.replace('/',os.sep) )


import urllib3
import requests
import sys
from bs4 import BeautifulSoup
import argparse
import webbrowser
from colored import fg

# include these variables in a print() function to output different clours to your terminal
txt_blue = fg('blue')
txt_green = fg('green')
txt_white = fg('white')
txt_red = fg('red')

# The Website address we will be targeting
url = "https://www.thephonebook.bt.com/Person/PersonSearch"

# Including generic User-Agent http header to make us look like a browser
HEADERS = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36'}

# A function for outputing the http response to make sure that bt.com is still talking back to us!
# Pass the http response as the argument 'r'
def http_status(r):
    # Generally a http status code higher than 200 is bad news
	if r.status_code == 200:
		traffic = txt_green
		print(txt_blue + "  (Info)\t" + txt_white + "HTTP status code: " + traffic + str(r.status_code) + txt_white)
	elif r.status_code <= 400:
		traffic = txt_blue
		print(txt_red + "  (warn)\t" + txt_white + "HTTP status code: " + traffic + str(r.status_code) + txt_white)
	else:
		traffic = txt_red
		print(txt_red + "  (warn)\t" + txt_white + "HTTP status code: " + traffic + str(r.status_code) + txt_white)

# Start scanning using a wordlist of names to bruteforce phone numbers
def scan_wordlist(street, area, wordlist, output):
    # Define variables
    numsSeen = set()
    num = set()

    # Open wordlist
    with open(wordlist) as f:
        list = f.read().splitlines()
    for name in list:

        # Here is where we package our parameters that we will send to 'bt.com', they are put into an array that will be passed to 'requests.get' as params
        payload = {"Surname": name, "Location": area, "Street": street}

        # We will wait 15 seconds for a response and try again, if we fail to get a response 3 times we skip that name and move on.
        try:
            r = requests.get(url, params=payload, headers=HEADERS, timeout=15)
        except:
            print(txt_blue + "  (info)\t" + txt_white + name + " Timed out, retrying")
            try:
                r = requests.get(url, params=payload, headers=HEADERS, timeout=15)
            except:
                print(txt_blue + "  (info)\t" + txt_white + name + " Timed out, twice, retrying")
                try:
                    r = requests.get(url, params=payload, headers=HEADERS, timeout=15)
                except:
                    print(txt_red + "  (warn)\t" + txt_white + name + " Timed out, tree times, skipping")
                    pass

        # Print http status to terminal
        http_status(r)
        print('trying:\t\t' + txt_blue + name + txt_white, end='\r')

        # Decode response from binary into UTF-8 charcters
        ret = r.content.decode('utf-8')

        # Use Beautiful Soup Module to parse html then find and extract the phone number
        soup = BeautifulSoup(ret, "html.parser")
        nums = soup.select("a[href^=\"tel\"]")

        for element in nums:

            # Extract inner content of 'html'  tags that are found
            num = element.decode_contents()

            # We want to remember numbers we have seen before so that we don't get repeats
            if num not in numsSeen:
                print(str(num) + " returned by: " + str(name))
                if output is not None:
                    o.write(str(element) + " | ")
                numsSeen.add(num)
                print(name, end='\r')
                if output is not None:
                    o.write("returned by: " + name + "
")
    # If outputing to a 'html' file, append closing tabs( </...> ) to the end of the file
    if output is not None:
        o.write("")
        o.close()
        webbrowser.open_new_tab(output)

    # Success
    return 0

# Start a scan only once using a single surname
def scan_surname(street, area, surname, output):

    # Here is where we package our parameters that we will send to 'bt.com', they are put into an array that will be passed to 'requests.get' as params
    payload = {"Surname": surname, "Location": area, "Street": street}

    # We will wait 15 seconds for a response and try again, if we fail to get a response 3 times we skip that name and move on.
    try:
        r = requests.get(url, params=payload, headers=HEADERS, timeout=15)
    except:
        print(txt_blue + "  (info)\t" + txt_white + name + " Timed out, retrying")
        try:
            r = requests.get(url, params=payload, headers=HEADERS, timeout=15)
        except:
            print(txt_blue + "  (info)\t" + txt_white + name + " Timed out, twice, retrying")
            try:
                r = requests.get(url, params=payload, headers=HEADERS, timeout=15)
            except:
                print(txt_red + "  (warn)\t" + txt_white + name + " Timed out, tree times, skipping")
                pass

    # Print http status to terminal
    http_status(r)

    # Decode response from binary into UTF-8 charcters
    ret = r.content.decode('utf-8')

    # Use Beautiful Soup Module to parse html then find and extract the phone number
    soup = BeautifulSoup(ret, "html.parser")
    nums = soup.select("a[href^=\"tel\"]")

    # If there we have found a phone number
    if (len(nums) > 1):
        for element in nums:

            # Extract inner content of 'html'  tags that are found
            num = element.decode_contents()
            print(num)

            # If outputing to a 'html' file, append closing tabs( </...> ) to the end of the file
            if output is not None:
                o.write(str(num))
                o.write("")
                o.close()
                # Finally open the default webbrowser and show the page
                webbrowser.open_new_tab(output)
    else:
    	print(txt_red + '(Notice)\t' + txt_white + 'No phone numbers returned')
    return 0

# Initialize -- Start Here! Here is where we parse command line argyments.
if __name__ == "__main__":
    parser=argparse.ArgumentParser(
        description='''Here is my lovelly little python script to bruteforce phonenumbers from the bt phonebook :-). ''',
        epilog="""Josjuar Lister 2021-2022""")
    parser.add_argument('-n', '--surname', help='specify surname')
    parser.add_argument('-a', '--area', help='Area Town or Postcode', required=True)
    parser.add_argument('-s', '--street', help='specify street', required=True)
    parser.add_argument('-w', '--wordlist', help='input wordlist file')
    parser.add_argument('-o', '--output', help='output html file and open')

    # To use arguments parsed here call 'args.<argument>'
    args=parser.parse_args()

    # If we want to output to a html file, ...
    if args.output is not None:
        print("output: " + args.output)
        o = open(args.output, "a")
        o.write("
" + args.output + "

area: " + args.area + "

street: " + args.street + "
")

    ##Wordlist scan
    if args.wordlist is not None:
        sys.exit(scan_wordlist(args.street, args.area, args.wordlist, args.output))

    ##One person scan
    elif args.surname is not None:
        sys.exit(scan_surname(args.street, args.area, args.surname, args.output))
else:
    exit(1)


# Importing modules

import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression, Ridge, LassoCV, ElasticNetCV
import logging
import statsmodels.api as sm
import warnings

warnings.filterwarnings('ignore')

# configuring logging operations
logging.basicConfig(filename='Regression_development_logs.log', level=logging.INFO,
                    format='%(levelname)s:%(asctime)s:%(message)s')

class LR_With_FeatureSelection:
    """This class is used to build Linear regression models with only the relevant features.
        reference_1: https://towardsdatascience.com/feature-selection-with-pandas-e3690ad8504b
        reference_2: https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LassoCV.html

        Parameters:
        X_train: Training data frame containing the independent features.
        y_train: Training dataframe containing the dependent or target feature.
        X_test: Testing dataframe containing the independent features.
        y_test: Testing dataframe containing the dependent or target feature.
    """

    def __init__(self, X_train, y_train, X_test, y_test):
        self.X_train = X_train
        self.y_train = y_train
        self.X_test = X_test
        self.y_test = y_test

    @property
    def forward_selection(self, significance_level=0.05):

        """ function accepts X_train, y_train and significance level as arguments and returns
            the linear regression model, its predictions of both the training and testing dataframes and the relevant features.
        """

        # Logging operation
        logging.info('Entered the Forward selection method of the LR_With_FeatureSelection class')

        try:
            initial_features = self.X_train.columns.tolist()
            best_features_FS = []  # All the relevant columns in a list

            while len(initial_features) > 0:
                remaining_features = list(set(initial_features) - set(best_features_FS))

                new_p_value = pd.Series(index=remaining_features)

                for new_column in remaining_features:
                    model = sm.OLS(self.y_train, sm.add_constant(self.X_train[best_features_FS + [new_column]])).fit()
                    new_p_value[new_column] = model.pvalues[new_column]

                min_p_value = new_p_value.min()

                if min_p_value < significance_level:
                    best_features_FS.append(new_p_value.idxmin())
                else:
                    break
            print()
            print('Features selected by Forward selection method in Linear regression are ', best_features_FS)
            print()

            X_train_FS = self.X_train[best_features_FS]
            X_test_FS = self.X_test[best_features_FS]

            lr = LinearRegression()

            lr.fit(X_train_FS, self.y_train)

            y_pred_train_FS = lr.predict(X_train_FS)
            y_pred_test_FS = lr.predict(X_test_FS)

            # logging operation
            logging.info('Linear regression model built successfully using Forward Selection method.')

            logging.info(
                'Exited the Forward Selection method method of the LR_With_FeatureSelection class')

            return lr, X_train_FS, y_pred_train_FS, X_test_FS, y_pred_test_FS, best_features_FS

        except Exception as e:

            # logging operation

            logging.error(
                'Exception occurred in Forward selection approach method of the LR_With_FeatureSelection class. Exception message:' + str(
                    e))
            logging.info(
                'Forward selection method unsuccessful. Exited the Forward selection method of the LR_With_FeatureSelection class')

    @property
    def backward_elimination(self, Significance_level=0.05):
        """Description: This method builds a linear regression model on all the features and eliminates
        each one w.r.t. its p-value if it is above 0.05. Else it will be retained Raises an exception if it fails.

        returns the linear regression model, its predictions of both the training and testing dataframes and the relevant features.
        """

        # Logging operation
        logging.info('Entered the Backward Elimination method of the LR_With_FeatureSelection class')
        try:
            best_features_BE = self.X_train.columns.tolist()
            while len(best_features_BE) > 0:
                features_with_constant = sm.add_constant((self.X_train[best_features_BE]))
                p_values = sm.OLS(self.y_train, features_with_constant).fit().pvalues[1:]
                max_p_value = p_values.max()

                if max_p_value >= Significance_level:
                    excluded_features = p_values.idxmax()
                    best_features_BE.remove(excluded_features)
                else:
                    break

            print()
            print("Features selected by the Backward elimination method in Linear regression are ", best_features_BE)
            print()

            x_train_BE = self.x_train[best_features_BE]  # considering only the relevant features
            x_test_BE = self.x_test[best_features_BE]  # considering only the relevant features

            lr = LinearRegression()  # instantiating linear regression model from LinearRegression of sci-kit learn

            lr.fit(x_train_BE, self.y_train)  # fitting

            y_pred_train_BE = lr.predict(x_train_BE)  # predictions on train data
            y_pred_test_BE = lr.predict(x_test_BE)  # predictions on test data

            # logging operation
            logging.info('Linear regression model built successfully using Backward Elimination approach.')

            logging.info(
                'Exited the backward_elimination method of the LR_With_FeatureSelection class')

            return lr, x_train_BE, y_pred_train_BE, x_test_BE, y_pred_test_BE, best_features_BE

        except Exception as e:
            # logging operation
            logging.error(
                'Exception occurred in backward_elimination method of the LR_With_FeatureSelection class. Exception '
                'message:' + str(e))
            logging.info(
                'Backward elimination method unsuccessful. Exited the backward_elimination method of the '
                'LR_With_FeatureSelection class')

class Embedded_method_for_feature_selection:
    """This class is used to train the models using Linear regression with Elastic Net model with iterative fitting along a regularization path.
        parameters
        x_train: Training data frame containing the independent features.
        y_train: Training dataframe containing the dependent or target feature.
        x_test: Testing dataframe containing the independent features.
        y_test: Testing dataframe containing the dependent or target feature.
    """

    def __init__(self, X_train, y_train, X_test, y_test):
        self.X_train = X_train
        self.y_train = y_train
        self.X_test = X_test
        self.y_test = y_test

    @property
    def Elastic_Net_CV(self):
        # logging operation
        logging.info('Entered the Elastic_Net_CV method of the Embedded_method_for_feature_selection class.')

        try:
            ECV = ElasticNetCV()  # Instantiating ElasticNetCV
            ECV.fit(self.X_train, self.y_train)  # fitting on the training data

            Coef = pd.Series(ECV.coef_, index=self.X_train.columns)  # feature importance by ElasticNetCV

            imp_Coef = Coef.sort_values(ascending=False)

            print()
            print(f'Feature importance by the ElasticNetCV are : {imp_Coef}')
            print()

            y_pred_train_ECV = ECV.predict(self.X_train)  # predictions on the train data
            y_pred_test_ECV = ECV.predict(self.X_test)  # predictions on the test data

            # logging operation

            logging.info('Linear regression model built successfully using Elastic_Net_CV approach. ')

            logging.info(
                'Exited the Elastic_Net_CV method of the Embedded_method_for_feature_selection class.')  # logging operation

            return ECV, self.X_train, y_pred_train_ECV, self.X_test, y_pred_test_ECV

        except Exception as e:
            # logging operation
            logging.error('Exception occurred in Elastic_Net_CV method of the Embedded_method_for_feature_selection class. Exception '
                          'message:' + str(e))
            logging.info('Elastic_Net_CV method unsuccessful. Exited the Elastic_Net_CV method of the '
                         'Embedded_method_for_feature_selection class ')


"""
Review Types
"""
from __future__ import annotations

import logging
import os
from dataclasses import dataclass, field
from typing import Iterator, List, Literal, Optional, Protocol, Tuple

import git
import unidiff

from sparrow.libs import config, scm

class BaseReview(Protocol):
    """
    Base Review Protocol
    """

    def current_file_contents(self, path: str) -> Optional[str]:
        """Read the current file contents which includes the changes made by this review"""
        ...

    def previous_file_contents(self, path: str) -> Optional[str]:
        """Read a file contents before the changes made by this review"""
        ...

    @property
    def diff_by_file(
        self,
    ) -> Iterator[Tuple[str, Optional[str], List[Tuple[int, int]]]]:
        """
        Returns a generator of (file_path, unified_diff, list of (hunk_start_line and hunk_end_line)) tuples
        """
        ...

    @property
    def root_dir(self) -> str:
        """Returns the root directory of the review"""
        ...

class FileReview(BaseReview):
    def __init__(self, path: str) -> None:
        self.path = path
        with open(path) as f:
            self.file_contents = f.read()

    def current_file_contents(self, path: str) -> Optional[str]:
        """Read a file at the head commit"""
        assert path == self.path
        return self.file_contents

    def previous_file_contents(self, path: str) -> Optional[str]:
        """Read a file contents before the changes made by this review"""
        return None

    @property
    def diff_by_file(
        self,
    ) -> Iterator[Tuple[str, Optional[str], List[Tuple[int, int]]]]:
        """
        Returns a generator of (file_path, unified_diff, list of (hunk_start_line and hunk_end_line)) tuples
        """
        yield self.path, self.file_contents, [(1, len(self.file_contents.splitlines()))]

    @property
    def root_dir(self) -> str:
        return ""

class SCMReview(BaseReview):
    """
    SCM based review
    """

    head_commit: str
    base_commit: Optional[str]
    repo: git.Repo
    patch_set: unidiff.PatchSet

    def __init__(
        self, head_commit: str, base_commit: Optional[str], repo_path: Optional[str]
    ) -> None:
        self.head_commit = head_commit
        self.base_commit = base_commit if base_commit else head_commit + "^"
        self.repo_root = repo_path or os.getcwd()
        repo = scm.get_repo(self.repo_root)
        if repo is None:
            raise ValueError(
                f"No git repository found with commit: {head_commit}, path: {self.repo_root}"
            )
        self.repo = repo
        self.patch_set = scm.patch_set(self.repo, self.head_commit, self.base_commit)

    def current_file_contents(self, path: str) -> Optional[str]:
        """Read a file at the head commit"""
        try:
            return (
                self.repo.commit(self.head_commit)
                .tree[path]
                .data_stream.read()
                .decode("utf-8")
            )
        except:  # pylint: disable=bare-except
            logging.exception("Error reading %s at commit: %s", path, self.head_commit)

    def previous_file_contents(self, path: str) -> Optional[str]:
        """Read a file contents before the changes made by this review"""
        try:
            return (
                self.repo.commit(self.base_commit)
                .tree[path]
                .data_stream.read()
                .decode("utf-8")
            )
        except:  # pylint: disable=bare-except
            # This could be the first commit
            pass

    @property
    def diff_by_file(
        self,
    ) -> Iterator[Tuple[str, Optional[str], List[Tuple[int, int]]]]:
        """
        Returns a generator of (file_path, unified_diff, list of (hunk_start_line and hunk_end_line)) tuples
        """
        for single_file_patch in self.patch_set:
            proceed, reason = can_review(single_file_patch)
            if not proceed:
                logging.info("Skipping %s: %s", single_file_patch.path, reason)
                yield single_file_patch.path, None, []
                continue
            unified_diff = "\n".join(str(patch) for patch in single_file_patch)
            hunk_lines = [
                (hunk.target_start, hunk.target_start + hunk.target_length)
                for hunk in single_file_patch
            ]
            yield single_file_patch.path, unified_diff, hunk_lines

    @property
    def root_dir(self) -> str:
        """Returns the root directory of the review"""
        return self.repo_root

@dataclass
class ReviewFile:
    """
    Wrapper for a single LLM call
    """

    file_path: str
    message: str
    status: Literal["needs_review", "skipped"]
    input_tokens: int
    skipped_reason: Optional[str] = None

@dataclass
class ReviewPlan:
    """
    Review broken down into individual review steps.

    Includes other metrics like estimated cost and input/output token counts computed during
    `plan` in case user confirmation is needed.
    """

    files: List[ReviewFile] = field(default_factory=list)
    estimated_cost: float = 0
    input_tokens: int = 0
    estimated_output_tokens: int = 0

    def add_file(self, file: ReviewFile, in_tokens: int, est_out_tokens: int) -> None:
        """
        Add a review step to the plan.
        """
        self.files.append(file)
        self.input_tokens += in_tokens
        self.estimated_output_tokens += est_out_tokens

def can_review(single_file: unidiff.PatchedFile) -> Tuple[bool, str]:
    """
    Returns True if the given file can be reviewed.
    """
    if single_file.is_binary_file:
        return False, "binary file"

    if single_file.is_removed_file:
        return False, "removed file"

    if config.is_excluded(single_file.path):
        return False, "excluded file extension"

    return True, ""


#! /usr/bin/python3
# __*__ coding=utf-8 __*__

import requests,json,datetime,time
from wxpy import *
from bs4 import BeautifulSoup
import xlwt

def Wechat_login():
  bot = Bot(cache_path=True)
  chats= bot.chats()
  friends = bot.friends()
  groups = bot.groups()
  return bot

def GetAQI():
  mainURL = 'http://www.pm25.in/nanchang'
  response = requests.get(mainURL)
  return str(response.content,encoding='utf-8')

def parser(content):
  soup = BeautifulSoup(content,features='html.parser')
  ## 明细
  # rows = soup.findAll('tr')
  # datas=[]
  # for row in rows:
  #   row=row.findChildren()
  #   data = []
  #   for column in row:
  #     data.append(column.next)
  #   datas.append(data)
  # return datas
  level = soup.find('div',attrs={'class':'level'}).findChildren()[0].next
  captions = soup.findAll('div',attrs={'class':'caption'})
  values = soup.findAll('div',attrs={'class':'value'})
  strmsg = "南昌空气质量情况:{},".format(level)
  for i in range(0,len(captions)):
    strmsg+="{}值为{}".format(captions[i].next,values[i].next)
  return strmsg.replace('\n','').replace(' ','')

def FillExcel(datas):
  wbk = xlwt.Workbook(encoding='utf-8')
  sheet = wbk.add_sheet('realtime_AQI',cell_overwrite_ok=True)
  for i in range(0,len(datas)):
    for j in range(0,len(datas[i])):
      sheet.write(i,j,datas[i][j])
  wbk.save('realtimeAQI_{}.xls'.format(datetime.datetime.now().strftime('%Y%m%d%H%M')))

bot = Wechat_login()

kd=ensure_one(bot.friends().search('kd'))
while(True):
  content = GetAQI()
  msg = parser(content)
  kd.send('当前时间：{}，{}'.format(datetime.datetime.now().strftime('%Y-%m-%d %H:%M'),msg))
  time.sleep(60*60)


import csv
import json
import boto3
from botocore.exceptions import ClientError

from django.conf import settings
from django.core.management.base import BaseCommand

from regluit.core.models import Edition, Identifier

s3 = boto3.resource('s3',
         aws_access_key_id=settings.AWS_ACCESS_KEY_ID,
         aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY)

class Command(BaseCommand):
    help = "translate doab ids to handles"
    def add_arguments(self, parser):
        parser.add_argument('filename', nargs='?', help="filename")
        parser.add_argument('--old_id', nargs='?', default=None, help="id to translate")

    def handle(self, filename, **options):
        self.stdout.write("doab ids to start: %s" % Identifier.objects.filter(type='doab').count())
        with open(filename, 'r') as jsonfile:
            newdoab = json.loads(jsonfile.read())
        done = 0
        if options['old_id']:
            to_do = Identifier.objects.filter(type='doab', value=options['old_id'])
        else:
            to_do = Identifier.objects.filter(type='doab')
        for doab in to_do:
            if doab.value.startswith("20.500.12854"):
                continue
            if doab.value in newdoab:
                # already done
                if Identifier.objects.filter(type='doab', value=newdoab[doab.value]).exists():
                    doab.delete()
                else:
                    old_cover_file_name = 'doab/%s/cover' % doab.value
                    new_cover_file_name = 'doab/%s' % newdoab[doab.value]
                    self.move_cover(old_cover_file_name, new_cover_file_name)
                    doab.value = newdoab[doab.value]
                    doab.save()
            else:
                doab.delete()
            done += 1
        self.stdout.write("doab ids at end: %s" % Identifier.objects.filter(type='doab').count())
        self.stdout.write("done:: %s" % done)

    def move_cover(self, old_name, new_name):
        if old_name == new_name:
            return
        old_url = "https://{}.s3.amazonaws.com/{}".format(
            settings.AWS_STORAGE_BUCKET_NAME, old_name)
        new_url = "https://{}.s3.amazonaws.com/{}".format(
            settings.AWS_STORAGE_BUCKET_NAME, new_name)
        copy_source = {
            'Bucket': settings.AWS_STORAGE_BUCKET_NAME,
            'Key': old_name
        }
        try:
            s3.meta.client.copy_object(
                CopySource=copy_source,
                Bucket=settings.AWS_STORAGE_BUCKET_NAME,
                Key=new_name, ACL='public-read')

            for ed in Edition.objects.filter(cover_image__contains=old_name):
                ed.cover_image = new_url
                ed.save()

            s3.meta.client.delete_object(Bucket=settings.AWS_STORAGE_BUCKET_NAME, Key=old_name,)
        except ClientError:
            self.stdout.write("problem moving %s to %s" % (old_name, new_name))


#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os, time, random, shutil, math
import mxnet as mx
import numpy as np
import matplotlib.pyplot as plt

from mxnet import gluon
from mxnet import autograd
from mxnet import image
from mxnet import init
from mxnet import nd
from mxnet.gluon.model_zoo import vision as models

task = "skirt_length_labels"
warmup_label_dir = 'data/web/Annotations/skirt_length_labels.csv'
base_label_dir = 'data/base/Annotations/label.csv'

image_path = []

with open(warmup_label_dir, 'r') as f:
    lines = f.readlines()
    tokens = [l.rstrip().split(',') for l in lines]
    for path, _, label in tokens:
        image_path.append(('data/web/'+path, label))

with open(base_label_dir, 'r') as f:
    lines = f.readlines()
    tokens = [l.rstrip().split(',') for l in lines]
    for path, tk, label in tokens:
        if tk == task:
            image_path.append(('data/base/'+path, label))

def plot_image(img_path):
    with open(img_path, 'rb') as f:
        img = image.imdecode(f.read())
    plt.imshow(img.asnumpy())
    return img

# 分配训练验证集
n = len(image_path)
random.seed(1024)
random.shuffle(image_path)
train_count = 0
for path, label in image_path:
    label_index = list(label).index('y')
    if train_count < n * 0.9:
        shutil.copy(path, os.path.join('data/train_valid', task, 'train', str(label_index)))
    else:
        shutil.copy(path, os.path.join('data/train_valid', task, 'val', str(label_index)))
    train_count += 1

# 迁移学习，定义预训练网络
pretrained_net = models.resnet50_v2(pretrained=True)

num_gpu = 1
ctx = [mx.gpu(i) for i in range(num_gpu)] if num_gpu > 0 else [mx.cpu()]

finetune_net = models.resnet50_v2(classes=6)
finetune_net.features = pretrained_net.features
finetune_net.output.initialize(init.Xavier(), ctx=ctx)
finetune_net.collect_params().reset_ctx(ctx)
finetune_net.hybridize()

# 训练集图像增广
def aug_train(data, label):
    im = data.astype('float32') / 255
    auglist = image.CreateAugmenter(data_shape=(3, 224, 224), resize=256,
                                    rand_crop=True, rand_mirror=True,
                                    mean=np.array([0.485, 0.456, 0.406]),
                                    std=np.array([0.229, 0.224, 0.225]))
    for aug in auglist:
        im = aug(im)
    im = nd.transpose(im, (2, 0, 1))
    return (im, nd.array([label]).asscalar())

# 验证集图像增广
def aug_val(data, label):
    im = data.astype('float32') / 255
    auglist = image.CreateAugmenter(data_shape=(3, 224, 224), resize=256,
                                    mean=np.array([0.485, 0.456, 0.406]),
                                    std=np.array([0.229, 0.224, 0.225]))
    for aug in auglist:
        im = aug(im)
    im = nd.transpose(im, (2, 0, 1))
    return (im, nd.array([label]).asscalar())

# 计算average precision
def calculate_ap(labels, outputs):
    cnt = 0
    ap = 0.
    for label, outptu in zip(labels, outputs):
        for lb, op in zip(label.asnumpy().astype(np.int), outputs.asnumpy()):
            op_argsort = np.argsort(op)[::-1]
            lb_int = int(lb)
            ap += 1.0 / (1+list(op_argsort).index(lb_int))
            cnt += 1
    return ((ap, cnt))

# 在训练集上的测试
def validate(net, val_data, ctx):
    metric = mx.metric.Accuracy()
    L = gluon.loss.SoftmaxCrossEntropyLoss()
    AP = 0.
    AP_cnt = 0
    val_loss = 0.
    for i, batch in enumerate(val_data):
        data = gluon.utils.split_and_load(batch[0], ctx_list=ctx, batch_axis=0, even_split=False)
        label = gluon.utils.split_and_load(batch[1], ctx_list=ctx, batch_axis=0, even_split=False)

        outputs = [net(X) for X in data]
        metric.update(label, outputs)
        loss = [L(yhat, y) for yhat, y in zip(outputs, label)]
        val_loss = sum([l.mean().asscalar() for l in loss]) / len(loss)
        ap, cnt = calculate_ap(label, outputs)
        AP += ap
        AP_cnt += cnt
    _, val_acc = metric.get()
    return ((val_acc, AP / AP_cnt, val_loss / len(val_data)))

lr = 1e-3
momentum = 0.9
wd = 1e-4
epochs = 5
batch_size = 64

train_path = os.path.join('data/train_valid', task, 'train')
val_path = os.path.join('data/train_valid', task, 'val')

# 训练集的DataLoader
train_data = gluon.data.DataLoader(gluon.data.vision.ImageFolderDataset(train_path, transform=aug_train),
                                   batch_size=batch_size, shuffle=True, num_workers=4)
# 验证集的DataLoader
val_data = gluon.data.DataLoader(gluon.data.vision.ImageFolderDataset(val_path, transform=aug_val),
                                 batch_size=batch_size, shuffle=False, num_workers=4)

trainer = gluon.Trainer(finetune_net.collect_params(), 'sgd', {'learning_rate': lr,
                                           'momentum': momentum, 'wd': wd})
L = gluon.loss.SoftmaxCrossEntropyLoss()
metric = mx.metric.Accuracy()

# train
for epoch in epochs:
    tic = time.time()

    train_loss = 0.
    metric.reset()
    AP = 0.
    AP_cnt = 0

    num_batch = len(train_data)

    for i, batch in enumerate(train_data):
        data = gluon.utils.split_and_load(batch[0], ctx_list=ctx, batch_axis=0, even_split=False)
        label = gluon.utils.split_and_load(batch[1], ctx_list=ctx, batch_axis=0, even_split=False)

        with autograd.record():
            outputs = [finetune_net(X) for X in data]
            loss = [L(yhat, y) for yhat, y in zip(outputs, label)]
        for l in loss:
            l.backward()
        trainer.step(batch_size)
        train_loss += sum([l.mean().asscalar() for l in loss]) / len(loss)

        metric.update(label, outputs)
        ap, cnt = calculate_ap(label, outputs)
        AP += ap
        AP_cnt += cnt

    train_map = AP / AP_cnt
    _, train_acc = metric.get()
    train_loss /= num_batch

    val_acc, val_map, val_loss = validate(finetune_net, val_data, ctx)
    print('[Epoch %d] train-acc: %f, mAP: %f, loss: %f, | val-acc: %f, mAP: %f, loss: %f | time: %f'
          % (epoch, train_acc, train_map, train_loss, val_acc, val_map, val_loss, time.time()()-tic))


from categories.views import addCategory, categoryListView

from django.urls import path
from . import views
app_name = 'categories'
urlpatterns = [
    path('',views.categoryListView,name ='list'),
    path('add/',views.addCategory, name ='add'),
    path('details/<int:pk>',views.categoryDetailView, name = 'details'),
    path('delete/<int:pk>',views.deleteCategoryView,name ='delete'),
]


# ---------------------------------------------------------------------------------
#                            Main Python file
# Desc: Run the models
from modules import outcome as outcome, survival as survival
import pandas

def generate_model_function():

    # get dataframes and store them into pkl files
    o_df = outcome.run()
    o_df.to_pickle('test_outcome.pkl')

    s_df = survival.run()
    o_df.to_pickle('test_survival.pkl')

generate_model_function()


from django.shortcuts import get_object_or_404
from rest_framework.views import APIView
from .models import Fruits
from .serializers import FruitsSerializer
from rest_framework.response import Response
from rest_framework import status
# Create your views here.

class FruitList(APIView):
	def get(self, request):
		queryset = Fruits.objects.all()
		serializer = FruitsSerializer(
			queryset, many=True, context={'request': request})
		return Response(serializer.data)

	def post(self, request):
		serializer = FruitsSerializer(data=request.data)
		serializer.is_valid(raise_exception=True)
		serializer.save()
		return Response(serializer.data, status=status.HTTP_201_CREATED)

class FruitDetail(APIView):
	def get(self, request, id):
		fruit = get_object_or_404(Fruits, pk=id)
		serializer = FruitsSerializer(fruit)
		return Response(serializer.data)

	def put(self, request, id):
		fruit = get_object_or_404(Fruits, pk=id)
		serializer = FruitsSerializer(fruit, data=request.data)
		serializer.is_valid(raise_exception=True)
		serializer.save()
		return Response(serializer.data)

	def delete(self, request, id):
		fruit = get_object_or_404(Fruits, pk=id)
		fruit.delete()
		return Response(status=status.HTTP_204_NO_CONTENT)


#!/usr/bin/python3
import string
import datetime

# Parameters
pathToFile = "resources/x.txt"
pathToNewFile = "resources/y.txt"
textToReplace = "xxxx"

# Open file and record file with timeSTAMP
text = open(pathToFile).read()
stamp = int(datetime.datetime.now().timestamp())
text = text.replace(textToReplace, str(stamp))
newTest = open(pathToNewFile, 'w')
newTest.write(text)
newTest.close()


# File:       example_PICMUS.py
# Author:     Dongwoon Hyun (dongwoon.hyun@stanford.edu)
# Created on: 2020-03-12
import os
import torch
import matplotlib.pyplot as plt
import numpy as np
from cubdl.das_torch import DAS_PW
from datasets.PWDataLoaders import load_data, get_filelist
from cubdl.PixelGrid import make_pixel_grid

device = torch.device("cuda:0")
# Find all the CUBDL Task 1 Data
# Optional other data subsets:
#   "all", "phantom", "invivo", "postcubdl", "simulation", "task1"
filelist = get_filelist(data_type="task1")

for data_source in filelist:
    for acq in filelist[data_source]:
        P, xlims, zlims = load_data(data_source, acq)

        # Define pixel grid limits (assume y == 0)
        wvln = P.c / P.fc
        dx = wvln / 2.5
        dz = dx  # Use square pixels
        grid = make_pixel_grid(xlims, zlims, dx, dz)
        fnum = 1

        # Make data torch tensors
        x = (P.idata, P.qdata)

        # Make 75-angle image
        dasN = DAS_PW(P, grid, rxfnum=fnum)
        idasN, qdasN = dasN(x)
        idasN, qdasN = idasN.detach().cpu().numpy(), qdasN.detach().cpu().numpy()
        iqN = idasN + 1j * qdasN
        bimgN = 20 * np.log10(np.abs(iqN))  # Log-compress
        bimgN -= np.amax(bimgN)  # Normalize by max value

        # Make 1-angle image
        idx = len(P.angles) // 2  # Choose center angle
        das1 = DAS_PW(P, grid, idx, rxfnum=fnum)
        idas1, qdas1 = das1(x)
        idas1, qdas1 = idas1.detach().cpu().numpy(), qdas1.detach().cpu().numpy()
        iq1 = idas1 + 1j * qdas1
        bimg1 = 20 * np.log10(np.abs(iq1))  # Log-compress
        bimg1 -= np.amax(bimg1)  # Normalize by max value

        # Display images via matplotlib
        extent = [xlims[0] * 1e3, xlims[1] * 1e3, zlims[1] * 1e3, zlims[0] * 1e3]
        plt.clf()
        plt.subplot(121)
        plt.imshow(bimg1, vmin=-60, cmap="gray", extent=extent, origin="upper")
        plt.title("1 angle")
        plt.subplot(122)
        plt.imshow(bimgN, vmin=-60, cmap="gray", extent=extent, origin="upper")
        plt.title("%d angles" % len(P.angles))
        plt.suptitle("%s%03d (c = %d m/s)" % (data_source, acq, np.round(P.c)))
        plt.pause(0.01)

        # Check to make sure save directory exists, if not, create it
        savedir = os.path.join("datasets", "test_pw_images", data_source)
        if not os.path.exists(savedir):
            os.makedirs(savedir)

        print(os.path.join("datasets", "test_pw_images", data_source, "%s%03d.jpg" % (data_source, acq)))
        plt.savefig(
            os.path.join(savedir, "%s%03d.jpg" % (data_source, acq))
        )


#!/usr/bin/env python3
from busquedas.models import ProyectosExcel
import csv
from datetime import datetime
# para ejecutar este script dentro de shell ejecutar el siguiente comando en
# la terminal, dentro del proyecto.
# python manage.py shell -i python < load_db.py
# otra forma de poblar datos es mediante dumpdata y fixtures
if not ProyectosExcel.objects.all().count() > 800:
    with open('load_start_info/proyectos_normalizados.csv', 'r') as f:
        reader = csv.DictReader(f)
        items = list(reader)

    for item in items:
        sigla_id = item.get('ID'),
        tesistas = int(item.get('TESISTAS')),
        autor = item.get('AUTOR'),
        titulo = item.get('TITULO'),
        tutor = item.get('TUTOR')
        docente = item.get('DOCENTE-1040')
        mencion = item.get('MENCION')
        fecha = item.get('FECHA')
        print(f"Proyecto: {titulo[0]}")
        # print(sigla_id[0], tesistas[0], autor[0], titulo[0], tutor, docente, mencion, fecha)
        ProyectosExcel.objects.create(
            tesistas = tesistas[0],
            sigla_id = sigla_id[0],
            autor = autor[0],
            titulo = titulo[0],
            tutor = tutor,
            docente = docente,
            mencion = mencion,
            fecha_concluida = datetime.strptime(fecha, "%Y-%m-%d"),
            )
else:
    print('*** Ya se cargo los proyectos')


# -*- coding: utf-8 -*-
import matplotlib
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
import seaborn as sns

func_invoc_frequency = pd.read_csv("../dataSet/funcInvoc_Freq_cov_per_app.csv")
func_invoc_frequency = func_invoc_frequency["cov_per_app"]

plt.figure(figsize=(13, 6))
# [0.7, 0.81, 0.91]
colors = sns.xkcd_rgb["denim blue"]
plt.hist(func_invoc_frequency, bins=int(len(func_invoc_frequency)/6), density=0, color=sns.xkcd_rgb["denim blue"])

plt.axvline(0.9, c=sns.xkcd_rgb["pale red"], ls='--', linewidth='2')
plt.axvline(0.96, c=sns.xkcd_rgb["pale red"], ls='--', linewidth='2')

plt.tick_params(labelsize=18)
#设置坐标轴名称
plt.xlabel('CoV of Function Invocations', fontsize=22)
plt.ylabel('Frequency', fontsize=22)

#设置坐标轴刻度
my_x_ticks = np.arange(0.8, 1.21, 0.1)
my_y_ticks = np.arange(0, 201, 25)
plt.xticks(my_x_ticks)
plt.yticks(my_y_ticks)

plt.grid()
plt.show()


import random
import pygame

ROCK = 1
PAPER = 2
SCISSORS = 3
screen_width, screen_height= 600, 400
background_color = (200, 173, 127)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

class Button:
    def __init__(self, x, y, width, height, text, callback):
        self.rect = pygame.Rect(x, y, width, height)
        self.color = WHITE
        self.text = text
        self.callback = callback

    def handle_event(self, event):
        if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
            if self.rect.collidepoint(event.pos):
                self.callback()

    def draw(self):
        pygame.draw.rect(screen, self.color, self.rect)
        text_surf = font.render(self.text, True, BLACK)
        text_rect = text_surf.get_rect(center=self.rect.center)
        screen.blit(text_surf, text_rect)

def choose_winner(turn1, turn2):
    win1, win2, tie = 1, 2, 0
    if turn1 == turn2:
        text = font.render("It's a tie.", True, BLACK)
        text_rect = text.get_rect(center=(screen_width // 2, screen_height // 2))
        screen.blit(text, text_rect)
        return tie
    if (turn1 == ROCK and turn2 == SCISSORS) or (turn1 == PAPER and turn2 == ROCK) or (turn1 == SCISSORS and turn2 == PAPER):
        text = font.render("You win!!!", True, BLACK)
        text_rect = text.get_rect(center=(screen_width // 2, screen_height // 2))
        screen.blit(text, text_rect)
        return win1
    text = font.render("You lose!", True, BLACK)
    text_rect = text.get_rect(center=(screen_width // 2, screen_height // 2))
    screen.blit(text, text_rect)
    return win2

def get_image(turn):
    if turn == ROCK:
        return rock_image
    if turn == PAPER:
        return paper_image
    if turn == SCISSORS:
        return scissors_image

def choose(turn):
    global turn1, turn2
    if turn == ROCK:
        turn1 = ROCK
    elif turn == PAPER:
        turn1 = PAPER
    else:
        turn1 = SCISSORS
    turn2 = random.randint(1, 3)

# Initialize pygame
pygame.init()
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Rock Paper Scissors")
font = pygame.font.Font(None, 36)
running = True

# import images
rock_image = pygame.image.load("rock.png")
paper_image = pygame.image.load("paper.png")
scissors_image = pygame.image.load("scissors.png")

# Choice buttons
rock_button = Button(60, screen_height - 40, 140, 30, "Rock", lambda: choose(ROCK))
paper_button = Button(230, screen_height - 40, 140, 30, "Paper", lambda: choose(PAPER))
scissors_button = Button(400, screen_height - 40, 140, 30, "Scissors", lambda: choose(SCISSORS))

turn1, turn2 = 0, 0

while running:
    # Changing surface color
    screen.fill(background_color)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                rock_button.handle_event(event)
                paper_button.handle_event(event)
                scissors_button.handle_event(event)
                if turn1 == 0:
                    continue
    if not (turn1 == 0 or turn2 == 0):
        cell_rect = pygame.Rect(50, 100, 150, 150)
        screen.blit(get_image(turn1), cell_rect)
        cell_rect = pygame.Rect(screen_width - 200, 100, 150, 150)
        screen.blit(pygame.transform.flip(get_image(turn2), True, False), cell_rect)
        choose_winner(turn1, turn2)

    rock_button.draw()
    paper_button.draw()
    scissors_button.draw()
    pygame.display.flip()

pygame.quit()


import easyocr
import cv2
from PIL import Image
import pytesseract

pytesseract.pytesseract.tesseract_cmd = 'D:\\Progarms\\tesseract\\tesseract'

#-------------------------

def draw_blocks(img, blocks):    #отрисовка блок распознаного текста от easyocr
    for i in blocks:
        ax = int(i[0][0][0])
        ay = int(i[0][0][1])
        bx = int(i[0][2][0])
        by = int(i[0][2][1])
        img = cv2.rectangle(img, (ax, ay), (bx, by), (0, 0, 255), 1)
    return img

def draw_paragraphs(img, paragr):    #отрисовка блок распознаного текста от easyocr
    for i in paragr:
        ax = int(i[0][0][0])
        ay = int(i[0][0][1])
        bx = int(i[0][1][0])
        by = int(i[0][1][1])
        img = cv2.rectangle(img, (ax, ay), (bx, by), (255, 0, 0), 1)
    return img

#-------------------------

def recognize_block_of_text(start_file):      #распознавание текста
    reader = easyocr.Reader(["en"], gpu = True)
    return reader.readtext(start_file, paragraph = False)

#-------------------------

def recognize_paragraphes(blocks):
    paragraphs = []                    #структура: [[[XверхЛ, YверхЛ], [XнизП,YнизП]], "текст"]
    for i in blocks:
        hasElement = False             #флаг наличия параграфа
        for j in paragraphs:
            if i[0][0][1] <= j[0][1][1] and (
                ((i[0][0][0] <= j[0][0][0]) and (i[0][2][0] >= j[0][0][0])) or
                (i[0][0][0]<= j[0][1][0] and i[0][2][0] >= j[0][1][0]) or
                (i[0][0][0] >= j[0][0][0] and i[0][2][0] <= j[0][1][0])):

                j[0][1][1] = i[0][2][1]
                if i[0][0][0] < j[0][0][0]:
                    j[0][0][0] = i[0][0][0]
                if i[0][2][0] > j[0][1][0]:
                    j[0][1][0] = i[0][2][0]
                j[1] += i[1] + " "
                hasElement = True
                break
        if not hasElement:          #создаем новый параграф
            paragraphs.append([[ [i[0][0][0], i[0][0][1]] , [i[0][2][0], i[0][2][1]] ], i[1]])
    return paragraphs

#-------------------------

def recognize_text(img_file, drBlocks = False, drParagraphs=False):
    blc = recognize_block_of_text(img_file)
    prg = recognize_paragraphes(blc)
    if drBlocks:
        img = cv2.imread(img_file)
        img = draw_blocks(img, blc)
        cv2.imwrite(img_file, img)
    if drParagraphs:
        img = cv2.imread(img_file)
        img = draw_paragraphs(img, prg)
        cv2.imwrite(img_file, img)
    return prg

#-------------------------

def recognize_text_tesseract(img_file, paragrapth):
    im = Image.open(img_file)
    rs = im.crop((paragrapth[0][0][0], paragrapth[0][0][1], paragrapth[0][1][0], paragrapth[0][1][1]))
    rs.save("textRec.jpg")
    return pytesseract.image_to_string('textRec.jpg')

def just_tesseract(img_file):
    return pytesseract.image_to_string(img_file)


"""

A script to batch test ORFs by int random fetching method

Example script:
python -i example_by_trans_shuffling.py -s human -olf list/golub/xaa -sn 1 -o1 output/golub/xaa.example.fa -o2 output/golub/xaa.example.pep_len

Parameter description:
s  = species
olf = orf_list_file
sn = simulation_num
"""

__version__ = "1.0.1"
__email__ = "haiwang.yang@northwestern.edu"

from timeit import default_timer as timer
from common_functions import Orf, Transcript2, get_elements, get_A2B, object_to_pickle, pickle_to_object
from simulate_by_trans_shuffling import example_shu
import subprocess
import argparse
import random
import os
import re
import pickle
from Bio.Seq import Seq
from pyfaidx import Fasta

if __name__ == "__main__":
    # fetching parameters
    parser = argparse.ArgumentParser(description='please provide information')
    parser.add_argument('-s', '--species', type=str) # human
    parser.add_argument('-olf', '--orf_list_file', type=str) # orf list file path
    parser.add_argument('-sn', '--simulation_num', type=int) # simulation num
    parser.add_argument('-o1', '--output1', type=str) # example orf cds seq
    parser.add_argument('-o2', '--output2', type=str) # example orf pep len

    args = parser.parse_args()
    species = args.species
    orf_list_file = args.orf_list_file
    simulation_num = args.simulation_num
    output1 = args.output1
    output2 = args.output2

    transcript_fasta = Fasta("annotation/" + species + ".transcript.fa")
    cds_range_file = "annotation/" + species + ".transcript.cds_range"
    transcript_id_to_transcript_len = get_A2B(cds_range_file, 1, 3)
    transcript_id_to_canonical_start = get_A2B(cds_range_file, 1, 4)
    transcript_id_to_UTR3_len = get_A2B(cds_range_file, 1, 6)

    with open(output1, "w") as w1, open(output2, "w") as w2:
        for orf_id in get_elements(orf_list_file):
            print(orf_id)
            orf = Orf(species, orf_id)
            transcript_id = orf.transcript_id
            transcript_seq = str(transcript_fasta[transcript_id])

            orf_type = orf.orf_type
            if orf_type != "noncoding":
                transcript_len = int(transcript_id_to_transcript_len[transcript_id])
                canonical_start = int(transcript_id_to_canonical_start[transcript_id])
                UTR3_len = int(transcript_id_to_UTR3_len[transcript_id])
                canonical_end = transcript_len - UTR3_len
                lst_sim_all_orf_cds, lst_sim_main_orf_cds, lst_sim_uorf_cds, lst_sim_overlapping_uorf_cds, lst_sim_all_orf_pep_len, lst_sim_main_orf_pep_len, lst_sim_uorf_pep_len, lst_sim_overlapping_uorf_pep_len = example_shu(species, orf_id, transcript_seq, canonical_start, canonical_end, simulation_num)
            else:
                canonical_start, canonical_end = 0, 0
                lst_sim_all_orf_cds, lst_sim_main_orf_cds, lst_sim_uorf_cds, lst_sim_overlapping_uorf_cds, lst_sim_all_orf_pep_len, lst_sim_main_orf_pep_len, lst_sim_uorf_pep_len, lst_sim_overlapping_uorf_pep_len = example_shu(species, orf_id, transcript_seq, canonical_start, canonical_end, simulation_num)

            i = 0
            for _ in lst_sim_all_orf_cds:
                w1.write(">" + orf_id + "__all" + str(i)  + "\n" + _ + "\n")
                i += 1

            i = 0
            for _ in lst_sim_main_orf_cds:
                w1.write(">" + orf_id + "__main" + str(i)  + "\n" + _ + "\n")
                i += 1

            i = 0
            for _ in lst_sim_uorf_cds:
                w1.write(">" + orf_id + "__uORF" + str(i)  + "\n" + _ + "\n")
                i += 1

            i = 0
            for _ in lst_sim_overlapping_uorf_cds:
                w1.write(">" + orf_id + "__ouORF" + str(i)  + "\n" + _ + "\n")
                i += 1

            w2.write(orf_id + "\tall"  + "\t" + ",".join([str(_) for _ in lst_sim_all_orf_pep_len]) + "\n")
            w2.write(orf_id + "\tmain"  + "\t" + ",".join([str(_) for _ in lst_sim_main_orf_pep_len]) + "\n")
            w2.write(orf_id + "\tuORF"  + "\t" + ",".join([str(_) for _ in lst_sim_uorf_pep_len]) + "\n")
            w2.write(orf_id + "\touORF"  + "\t" + ",".join([str(_) for _ in lst_sim_overlapping_uorf_pep_len]) + "\n")


import os
import math
import numpy as np
from keras.models import load_model

class MultilayerClassifier:

    def __init__(self):
        self.intern_extern_model = load_model('..\\models\\Intern-Extern-Model\\150px\\binary.hdf5', compile=True)
        self.intern_model = load_model('..\\models\\Intern-Extern-Model\\150px\\intern.hdf5', compile=True)
        self.extern_model = load_model('..\\models\\Intern-Extern-Model\\150px\\extern.hdf5', compile=True)

    def __prediction_to_class(self, arg):
        options = {0: "02_ER",
                   1: "04_LN_sy",
                   2: "01_AR",
                   3: "03_LN_md",
                   4: "05_LB",
                   5: "06_AG"
                   }
        return options.get(arg, "nothing")

    def __calc_extern_prediction(self, prediction):
        return math.fabs((prediction - 0.5) / 0.5) * 100

    def __clssify_intern_extern(self, document_img):

        prediction = self.intern_extern_model.predict(document_img)
        class_and_prediction = {"prediction": prediction, 'class': int(round(prediction.item(0)))}
        return class_and_prediction

    def __classify_intern_document(self, document_img):
        prediction = self.intern_model.predict(document_img)
        class_and_prediction = {"prediction": np.max(prediction), 'class': np.argmax(prediction) + 2,
                                'class_name': self.__prediction_to_class(np.argmax(prediction) + 2)}
        return class_and_prediction

    def get_label_dir(self, folders):

        directories = [d for d in os.listdir(folders)
                       if os.path.isdir(os.path.join(folders, d))]
        print(directories)
        return directories

    def __classify_extern_document(self, document_img):

        prediction = self.extern_model.predict(document_img)
        class_and_prediction = {"prediction": self.__calc_extern_prediction(prediction),
                                'class': int(round(prediction.item(0))),
                                'class_name': self.__prediction_to_class(int(round(prediction.item(0))))}
        return class_and_prediction

    def classify_single_document(self, document_img):

        img = self.__convert_image(document_img)
        intern_extern_prediction = self.__clssify_intern_extern(img)

        if intern_extern_prediction['class'] == 1:
            return self.__classify_intern_document(img)

        elif intern_extern_prediction['class'] == 0:
            return self.__classify_extern_document(img)

    def __convert_image(self, image):
        x = image.shape[0]
        y = image.shape[0]
        img = np.array(image).astype('float32')
        reshaped_image = np.reshape(img, [1, x, y, 3])
        return reshaped_image

if __name__ == "__main__":

    classificator = MultilayerClassifier()
    data_dir = "C:\\tmp\\test2"
    directories = classificator.get_label_dir(data_dir)
    from utils.evaluate_model import Evaluator

    evaluator = Evaluator()
    classNames = evaluator.get_class_names(data_dir)
    test_X, test_Y = evaluator.get_test_data(data_dir, 150, 150)

    true_classes = evaluator.labels_to_classes(test_Y)
    predicted_classes = []

    for index, data in enumerate(test_X):
        predictions = classificator.classify_single_document(data)
        predicted_classes.append(directories.index(predictions['class_name']))

    print("macro recal=", evaluator.recall(true_classes, predicted_classes, 'weighted'))
    print("micro recal=", evaluator.recall(true_classes, predicted_classes, 'micro'))
    print("macro precision=", evaluator.presision_score(true_classes, predicted_classes, 'weighted'))
    print("micro precision=", evaluator.presision_score(true_classes, predicted_classes, 'micro'))
    print("macro f1=", evaluator.f1_score(true_classes, predicted_classes, 'weighted'))
    print("micro f1=", evaluator.f1_score(true_classes, predicted_classes, 'micro'))
    print('report: ', evaluator.classification_report(true_classes, predicted_classes, classNames))
    print('accurary:', evaluator.accuracy_score(true_classes, predicted_classes))

    from utils.confusionmatrix import Confusionmatrix

    cnf = Confusionmatrix(directories)
    cnf_matriy = cnf.compute_confusion_matrix(true_classes, predicted_classes)
    cnf.plot_confusion_matrix(cnf_matriy, False, title='150px')


import numpy as np
import matplotlib.pyplot as plt
import math
import cmath
import cv2

N = 101

j = complex(0, 1)
base = np.arange(0, N)

def empty(a):
    pass

cv2.namedWindow('Trackbar')
cv2.resizeWindow('Trackbar', 640, 280)
cv2.createTrackbar('offset', 'Trackbar', (N-1)//2, N, empty)
cv2.createTrackbar('freq1', 'Trackbar', 2, 20, empty)
cv2.createTrackbar('freq2', 'Trackbar', 5, 20, empty)

offset = (N-1)//2
freq1 = 2
freq2 = 5

#broadcast a function f to all elements of array
def broadcast(f, arr):
    ans = []
    for ele in arr:
        ans.append(f(ele))
    return np.array(ans)

#Xk is the fourier transform value of the wave of frequency = k-offset

def DFT(x):
    N = len(x)
    X = np.zeros(N, dtype = complex)
    for k in range(N):
        amp = 0
        for n in range(N):
            amp += x[n] * cmath.exp(-j*2*math.pi*(k-offset)*n/N)
        X[k] = amp
    return X

def iDFT(X):
    N = len(X)
    x = np.zeros(N, dtype = complex)
    for n in range(N):
        val = 0
        for k in range(N):
            val += X[k] * cmath.exp(j*2*math.pi*(k-offset)*n/N)
        x[n] = val/N
    return x

fig = plt.figure(0, figsize = (7,7))
f1=freq1
f2=freq2
ofs=offset

flag = False
while True:

    f1 = cv2.getTrackbarPos('freq1','Trackbar')
    f2 = cv2.getTrackbarPos('freq2','Trackbar')
    ofs = cv2.getTrackbarPos('offset','Trackbar')

    if not (flag==1 and f1==freq1 and f2==freq2 and ofs==offset):

        fig.clear(True)
        flag = True
        freq1 = f1
        freq2 = f2
        offset = ofs

        x = np.sin(2*cmath.pi*freq1*base/N) + np.sin(2*cmath.pi*freq2*base/N)
        X = DFT(x)
        Xamp = broadcast(abs, X)
        Xphase = broadcast(cmath.phase, X)
        x_idft = iDFT(X).real

        #fig1
        plt.subplot(2, 2, 1)
        plt.plot(x, color = 'b')
        plt.title('x(n)')

        #fig2
        plt.subplot(2, 2, 2)
        plt.xlim(base[0]-offset,base[N-1]-offset)
        plt.plot(base-offset, Xamp, color = 'b')
        plt.title('Xamp(k-offset)')

        #fig3
        plt.subplot(2, 2, 3)
        plt.xlim(base[0]-offset,base[N-1]-offset)
        plt.plot(base-offset, Xphase, color = 'b')
        plt.title('Xphase(k-offset)')

        #fig4
        plt.subplot(2,2,4)
        plt.plot(x_idft, color = 'g')
        plt.title("iDFT(X)")

    plt.draw()
    plt.pause(0.1)

    key = cv2.waitKey(1)

    if key==ord('q'):
        print('ggez')
        break

plt.close(fig)
cv2.destroyAllWindows()
exit()


import os
# import jinja2 lib
import jinja2
# import regex lib
import webapp2

__author__ = "Harry Staley <staleyh@gmail.com>"
__version__ = "1.0"

# sets the locaiton of the templates folder contained in the home of this file.
TEMPLATE_DIR = os.path.join(os.path.dirname(__file__), 'templates')
# envokes the jinja2 envronment pointing it to the location of the templates.
# with user input markup automatically escaped
JINJA_ENV = jinja2.Environment(loader=jinja2.FileSystemLoader(TEMPLATE_DIR))

class Handler(webapp2.RequestHandler):
    """
    Handler class for all functions in this app for rendering
    any templates.
    """

    def write(self, *a, **kw):
        """ displays the respective function, parameters, ect. """
        self.response.out.write(*a, **kw)

    def render_str(self, template, **params):
        """ Gets the template and passes it with paramanters. """
        tmp = JINJA_ENV.get_template(template)
        return tmp.render(params)

    def render(self, template, **kw):
        """ Calls render_str and displays the template. """
        self.write(self.render_str(template, **kw))

class ShoppintListHandler(Handler):
    """
    Takes input from Handler and renders the markup
    defined.
    """

    def get(self):
        """
        Passes markup from defined file and passes it to the renderer.
        """
        items = self.request.get_all("food")
        # pases the value in item variable into the jinja2 template
        self.render("shopping_list.html", items=items)

WSGI_APP = webapp2.WSGIApplication([
    ('/shoppinglist', ShoppintListHandler)
], debug=True)


import cv2
import numpy as np
import pyttsx3
import googletrans
from googletrans import Translator
import streamlit as st
import time

# This Function does transformation over the bounding boxes detected by the text detection model
def fourPointsTransform(frame, vertices):

    # Print vertices of each bounding box
    vertices = np.asarray(vertices).astype(np.float32)
    outputSize = (100, 32)
    targetVertices = np.array([
        [0, outputSize[1] - 1],
        [0, 0],
        [outputSize[0] - 1, 0],
        [outputSize[0] - 1, outputSize[1] - 1]], dtype="float32")

    # Apply perspective transform
    rotationMatrix = cv2.getPerspectiveTransform(vertices, targetVertices)
    result = cv2.warpPerspective(frame, rotationMatrix, outputSize)
    return result

# Defining function that returns the scale factor to resize the output image according to
# the size/shape matrix of the input image
def setScaleFactor(frame):

    #Get image height and width
    frame_h, frame_w, ch = frame.shape

    # Define scaling factor according to preset values of the input image size so as
    # the output image is scaled down to a generic size,"k" is the scaling factor value
    if (frame_h > 2000) and (frame_w > 2000):
        k = 0.2
    elif (2000 > frame_h > 1000) and 2000 > frame_w > 1000:
        k = 0.4
    elif (frame_h < 1000) or frame_w < 1000:
        k = 0.6
    elif (frame_h < 500) and frame_w < 500:
        k = 1
    else:
        k = 0.5
    return k

# Set title.
st.title('OpenCV Text-to-speach')

# Upload image.
uploaded_file = st.sidebar.file_uploader('Choose a text image', type='jpg')

if uploaded_file is not None:
    # Convert the file to an opencv image.
    file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
    img = cv2.imdecode(file_bytes, 1)
    inp_col, op_col = st.columns(2)
    with inp_col:
        st.header('Original')
        # Display uploaded image.
        orig = img.copy()
        st.image(orig, channels='BGR', use_column_width=True)

    # Define list to store the vocabulary in
    vocabulary =[]

    # Open file to import the vocabulary
    with open("/Users/aneerbanchakraborty/Documents/git/C0/c0-module-text-detection-ocr/resources/alphabet_94.txt") as f:

        # Read the file line by line
        for l in f:

            # Append each line into the vocabulary list.
            vocabulary.append(l.strip())

        #Close the file
        f.close()

    # DB model for text-detection based on resnet50
    text_detector = cv2.dnn_TextDetectionModel_DB("/Users/aneerbanchakraborty/Documents/git/C0/c0-module-text-detection-ocr/resources/DB_TD500_resnet50.onnx")
    binThresh = 0.3
    polyThresh = 0.5
    maxCandidates = 200
    unclipRatio = 2.0

    text_detector.setBinaryThreshold(binThresh).setPolygonThreshold(polyThresh).setMaxCandidates(maxCandidates).setUnclipRatio(unclipRatio)
    text_detector.setInputParams(1.0/255, (736, 736), (122.67891434, 116.66876762, 104.00698793) , True)

    # CRNN model for text-recognition
    text_recogniser = cv2.dnn_TextRecognitionModel("/Users/aneerbanchakraborty/Documents/git/C0/c0-module-text-detection-ocr/resources/crnn_cs.onnx")
    text_recogniser.setDecodeType("CTC-greedy")
    text_recogniser.setVocabulary(vocabulary)
    text_recogniser.setInputParams(1/127.5, (100,32), (127.5, 127.5, 127.5))

    # Get the scaling factor
    scaling_factor = setScaleFactor(img)
    # Resize the image depending on the original image shape and the scaling factor
    image = cv2.resize(img, None, fx= scaling_factor, fy= scaling_factor, interpolation= cv2.INTER_LINEAR)

    # Use the DB text detector initialised previously to detect the presence of text in the image
    boxes, confs = text_detector.detect(image)

    text_data=[]

    #Iterate throught the bounding boxes detected by the text detector model
    for box in boxes:

        # Apply transformation on the bounding box detected by the text detection algorithm
        cropped_roi  = fourPointsTransform(image,box)

        # Recognise the text using the crnn model
        recResult = text_recogniser.recognize(cropped_roi)

        # Append recognised text to the data storage variable
        text_data.append(recResult)

    # Joining the text data together to form a output sentence
    text_data=' '.join(text_data)

    # Draw the bounding boxes of text detected.
    cv2.polylines(image, boxes, True, (255, 0, 255), 4)
    with op_col:
        st.header('Detected')
        st.image(image[:,:,::-1])

    translator = Translator()
    speechEngine =  pyttsx3.init()
    speechEngine. setProperty("rate", 120)
    st.sidebar.header('Detected Language: '+ googletrans.LANGUAGES[translator.detect(text_data).lang])
    list_of_lang = ['None']
    list_of_lang.extend(googletrans.LANGUAGES.values())
    lang = st.sidebar.selectbox('Translate into :', list_of_lang)

    if lang != 'None':
        text = translator.translate(text_data, lang).text

        st.title(text)

        # Speak out the translated text using the text to speech engine
        press = st.button('Speak')
        if press:
            speechEngine.say(translator.translate(text_data, lang).text)
            speechEngine.runAndWait()
            time.sleep(7)
            speechEngine.endLoop()


import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.impute import SimpleImputer

import sys # Import capability to grab a function from a file in a different directory
sys.path.insert(0, '../Tools/')
from modelComparison import compareModels

import warnings
warnings.simplefilter(action = "ignore")
# Suppress error output: python3.6/site-packages/sklearn/ensemble/forest.py:245: FutureWarning: The default value of n_estimators will change from 10 in version 0.20 to 100 in 0.22. "10 in version 0.20 to 100 in 0.22.", FutureWarning) and a SettingWithCopyWarning

# Import data
drugUse = pd.read_csv("drug-use-by-age.csv")

# Taking a Look At Original Data
print("\n\nHere's a look at the original dataset!\n", drugUse.head(), sep = "\n0")

# Replace all dashes (represents missing value)  in data with NaN. inplace = True because we don't need to retain the original dataset! Note: inplace = False by default which means it doesn't alter the original data (it assigns your result to a new variable).
# We want to predict alcohol-frequency values so set all other columns as features
drugUse.replace("-", np.NaN, inplace = True)

y = drugUse["alcohol-frequency"]
X = drugUse.drop(['alcohol-frequency'], axis = 1) # axis = 1 signifies using columns

'''
X = drugUse.select_dtypes(exclude = ["object"])

Note: THIS DELETES COLUMNS WITH NaN VAULES SINCE NaN IS OF OBJECT TYPE!!! (I was stuck for half an hour trying to find out why all columns with missing values were being dropped haha)
'''

# Find columns with missing data entries
colsWithMissingData = [col for col in X.columns if X[col].isnull().any()]
print("\n\n--------------------\nColumns with missing data:", colsWithMissingData)

# METHOD 1 (Dropping Columns With Missing Data Values)
# Split our data
trimmedX = X.drop(colsWithMissingData, axis = 1, inplace = False)
trainX, validateX, trainY, validateY = train_test_split(trimmedX, y, random_state = 1)
print("--------------------\n\nHere's a peek at our training data when we drop all columns with missing values!", trainX.head(), sep = "\n\n")

print("--------------------")

methodOneMAE = compareModels(trainX, validateX, trainY, validateY)

# METHOD 2 (Imputation)
imputer = SimpleImputer(strategy = "most_frequent")
trainX, validateX, trainY, validateY = train_test_split(X, y, random_state = 1)
imputedTrainX = pd.DataFrame(imputer.fit_transform(trainX))
imputedValidateX = pd.DataFrame(imputer.transform(validateX))

imputedTrainX.columns = trainX.columns
imputedValidateX.columns = validateX.columns

print("\n\nHere's a peek at our training data when we impute missing values!", imputedTrainX.head(), sep = "\n\n")

methodTwoMAE = compareModels(imputedTrainX, imputedValidateX, trainY, validateY)

print("--------------------")

# METHOD 3 (Imputation and Column Labels)
for col in colsWithMissingData:
    trainX[col + '_missing'] = trainX[col].isnull()
    validateX[col + '_missing'] = validateX[col].isnull()
    # Create columns that tell where missing values occurred

newImputer = SimpleImputer(strategy = "median")
newlyImputedTrainX = pd.DataFrame(newImputer.fit_transform(trainX))
newlyImputedValidateX = pd.DataFrame(newImputer.transform(validateX))

newlyImputedTrainX.columns = trainX.columns
newlyImputedValidateX.columns = validateX.columns

methodThreeMAE = compareModels(newlyImputedTrainX, newlyImputedValidateX, trainY, validateY)

print("\n\nHere's a peek at our training data when we impute missing values and keep track of which rows have imputed values", trainX.head(), sep = "\n\n")

print("--------------------")

# Evaluation Results
print("\n-->Dropping all columns with missing data produces a random forest model with MAE:", methodOneMAE)

print("\n-->Imputing missing values with the most frequent value of each column produces a random forest model with an MAE of", methodTwoMAE)

print("\n-->Imputing missing values with the median value of each column and tracking which rows have imputed valuesproduces a random forest model with an MAE of", methodThreeMAE)


from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn import metrics
import matplotlib.pyplot as plt

import base64
import pandas as pd
import numpy as np
from io import BytesIO

from ..models import Report
from ..shared import PREDICTION_CHOICES

actual_problem = ''

def get_report_name(prediction_selected):
    for prediction in PREDICTION_CHOICES:
        if prediction[0] == prediction_selected:
            return prediction[1]
    return ''

def get_graph():
    buffer = BytesIO()
    plt.savefig(buffer, format='png')
    buffer.seek(0)
    image_png = buffer.getvalue()
    graph = base64.b64encode(image_png)
    graph = graph.decode('utf-8')
    buffer.close()
    return graph

def save_report(description):
    report_name = get_report_name(actual_problem)

    chart = get_graph()

    if Report.objects.filter(name=actual_problem).exists():
        report = Report.objects.get(name=actual_problem)
        report.description = description
        report.image_str = chart
        report.title=report_name
        report.save()
        print('MODIFIED report: {}'.format(report))
    else:
        new_report = Report(name=actual_problem, title=report_name, image_str=chart, description=description)
        new_report.save()
        print('NEW report: {}'.format(new_report))
    return chart

# 24
def cases_tests_comparison_country(df, variables, problem):
    plt.switch_backend('AGG')
    global actual_problem
    actual_problem = problem

    days_count=[]
    days_count2=[]
    target = variables[0] # Vaccinated
    country = variables[1]
    country_param = variables[2]
    tests_param = variables[3]
    days_to_predict = int(variables[4])
    degree_number = int(variables[5])

    # Fitlering by country
    country_selected_df = df.loc[df[country_param] == country]
    infected_df = len(country_selected_df[target])
    tests_df = len(country_selected_df[tests_param])

    # Storing days count
    for i in range (0, infected_df):
        days_count.append(i)

    for i in range (0, tests_df):
        days_count2.append(i)

    X = np.asarray(days_count)[0:days_to_predict + 1]
    Y = country_selected_df[target][0:days_to_predict + 1]
    X2 = np.asarray(days_count2)[0:days_to_predict + 1]
    Y2 = country_selected_df[tests_param][0:days_to_predict + 1]
    plt.scatter(X, Y)
    plt.scatter(X2, Y2, c='coral')

    X = X[:, np.newaxis]
    Y = Y[:, np.newaxis]
    X2 = X2[:, np.newaxis]
    Y2 = Y2[:, np.newaxis]

    # Setting polynomial degree
    nb_degree=int(degree_number)
    polynomial_features=PolynomialFeatures(degree=nb_degree)
    polynomial_features2=PolynomialFeatures(degree=nb_degree)
    X_TRANSF=polynomial_features.fit_transform(X)
    X_TRANSF2=polynomial_features2.fit_transform(X2)

    model= LinearRegression()
    model2= LinearRegression()
    model.fit(X_TRANSF,Y)

    Y_NEW = model.predict(X_TRANSF)
    rmse=np.sqrt(metrics.mean_squared_error(Y,Y_NEW))

    r2=metrics.r2_score(Y,Y_NEW)
    x_new_main=0.0
    x__new_max=float(days_to_predict)

    X_NEW=np.linspace(x_new_main,x__new_max,50)

    X_NEW=X_NEW[:,np.newaxis]
    X_NEW_TRANSF =polynomial_features.fit_transform(X_NEW)
    Y_NEW=model.predict(X_NEW_TRANSF)
    prediction_value = Y_NEW[int(Y_NEW.size - 1)][0]

    # ==========================
    # Country 2 calculations
    model2.fit(X_TRANSF2,Y2)

    Y_NEW2 = model2.predict(X_TRANSF2)
    rmse=np.sqrt(metrics.mean_squared_error(Y2,Y_NEW2))

    r2=metrics.r2_score(Y2,Y_NEW2)
    x_new_main=0.0
    x__new_max=float(days_to_predict)

    X_NEW2=np.linspace(x_new_main,x__new_max,50)
    X_NEW2=X_NEW2[:,np.newaxis]
    X_NEW_TRANSF2 =polynomial_features2.fit_transform(X_NEW2)
    Y_NEW2=model2.predict(X_NEW_TRANSF2)
    prediction_value2 = Y_NEW2[int(Y_NEW2.size - 1)][0]

    # ==================================

    plt.plot(X_NEW2,Y_NEW2,color='red')
    plt.plot(X_NEW,Y_NEW,color='black')
    plt.grid()
    title='Grado={}; RMSE ={}; R2 ={}'.format(degree_number,round(rmse,2),round(r2,2))
    plt.title('Comparación tendencia en Casos/Pruebas en ' + country.upper() + '\n' + title)
    plt.xlabel('Días')
    plt.ylabel('Data')
    plt.legend(('Casos', 'Pruebas'), loc='upper right')
    if prediction_value > prediction_value2:
        description = ''' \n
        La tendencia de casos en el paìs de {} para los siguientes {} días indica que el numero de
        vacunados tiende a decrecer conforme pasan los días en comparación a las pruebas de vacunación, que crecen de una forma más lenta.
        '''.format(country, days_to_predict)
    else:
        description = ''' \n
        La tendencia de personas vacunadas en el paìs de {} para los siguientes {} días indica que el numero de
        pruebas tiende a decrecer conforme pasan los días en comparación a la confirmación de nuevos casos, que crecen de una forma más lenta.
        '''.format(country, days_to_predict)
    save_report(description)