import matplotlib.pyplot as plt
import subprocess as sp
import pickle as pkl
import pandas as pd
import numpy as np
import math
import pdb

exec(open("/home/joelsmith/Projects/software/tracts/tracts.py").read())

#------------------Neutral Simulations----------------------

def run_dfuse(ratevec, tstart, popsize, markers):
    codedir = '../code/dfuse_files/'
    make_deme_file(popsize)
    for rate in ratevec:
        runstr = "dfuse -d "+codedir+"inputfiles/demefile_dmi -r 1 -g "+ \
                 str(tstart)+" -m "+str(rate*2)+" -G "+str(tstart)+" -o "+ \
                 codedir+"outputfiles/dmi"+" -l "+str(markers)
        sp.run(runstr,shell=True)
        haptable = pd.read_csv(codedir+"outputfiles/dmi.haplo",skiprows=[0])
        haptable.drop(haptable.columns[0:8], axis=1, inplace=True)
        haptable = haptable[haptable.isin(['a','b']).all(1)]
        mainfile = codedir+"outputfiles/dmi.main"
        pos = open(mainfile).readline().rstrip()
        pos = np.asarray(pos.split(' ')[2:])
        haptable.columns = pos
        haptable[haptable.isin(['a'])] = 1
        haptable[haptable.isin(['b'])] = 2
        hapfile = codedir+"tempfiles/haptable_mig_"+str(rate)+".csv"
        haptable.to_csv(hapfile,index=False)

def make_deme_file(popsize):
    inputdir = '../dfuse_files/inputfiles/'
    thefile = open(inputdir+"demefile_dmi", 'w+')
    input_list = [''] * 4
    input_list[0] = '1'
    input_list[1] = str(int(popsize))
    input_list[2] = str(int(popsize))
    for item in input_list:
        thefile.write("%s\n" % item)
    thefile.close()

def get_dfuse_tracts(ratevec,npts,maxlen,chrlen):
    codedir = "../dfuse_files/tempfiles/"
    bins = np.array([np.arange(0,maxlen*(1+.5/npts),float(maxlen)/npts)])[0]
    bintable = pd.DataFrame(index=bins[:-1],columns=ratevec)
    for rate in ratevec:
        chromdict = make_dfuse_chroms(rate,chrlen)
        tractdict = make_tracts(chromdict,chrlen)
        tractbins = make_tractbins(tractdict,chrlen,maxlen,npts)
        popahist = tractbins.get('popahist')
        popbhist = tractbins.get('popbhist')
        relpopa = popahist/np.sum(popahist)
        relpopb = popbhist/np.sum(popbhist)
        bintable[rate] = np.mean(np.vstack((relpopa,relpopb)),axis=0)
    binfile = codedir+"dfuse_bintable.pkl"
    pkl.dump(bintable,open(binfile, "wb" ))

def make_dfuse_chroms(rate, chrlen):
    codedir = "../dfuse_files/tempfiles/"
    haptable = pd.read_csv(codedir+"haptable_mig_"+str(rate)+".csv")
    chroms = np.asarray(haptable)
    pos = np.asarray(haptable.columns,dtype='float64')*chrlen
    chromdict = {"chroms":chroms,"pos":pos}
    return(chromdict)

def plot_figure_dfuse(ratevec,tstart,Ls,pop,maxlen,npts,chrlen):
    """Plots the predicted tract length distributions against the
    simulated tract length distributions from dfuse."""
    bindir = '/home/joelsmith/Projects/dmis/code/dfuse_files/tempfiles/'
    bintable = pkl.load(open(bindir+"dfuse_bintable.pkl", "rb"))
    bins = np.append(bintable.index.values,1)
    rows = bintable.shape[0]
    xAxis = np.array(list(range(1,rows+1)))[:rows-1] / np.array(rows)
    for rate in ratevec:
        mig = continuous_mig_hybridzone(rate,tstart)
        model = demographic_model(mig)
        tractsOut = model.expectperbin(Ls,pop,bins)
        tractDist = np.array(tractsOut)
        relTractDist = tractDist / np.sum(tractDist)
        obs = np.asarray(np.where(np.logical_not(bintable[rate]==0)))[0][:-1]
        obsvalues = bintable.iloc[obs][rate]
        plt.plot(bins[:npts],np.log(relTractDist[:npts:]),label='_nolegend_')
        plt.scatter(x=bintable.index[obs],y=np.log(obsvalues))
        plt.xlabel('Tract Length (Morgans)')
        plt.ylabel('log(Relative Frequency)')
        plt.legend(loc='upper right',title='Migration Rate')

def plot_image_dfuse(ratevec):
    f, (ax1, ax2, ax3) = plt.subplots(3, sharex=True, sharey=True)
    codedir = "../dfuse_files/tempfiles/"
    chroms1 = np.asarray(pd.read_csv(codedir+"haptable_mig_"+str(ratevec[0])+".csv"))
    chroms2 = np.asarray(pd.read_csv(codedir+"haptable_mig_"+str(ratevec[1])+".csv"))
    chroms3 = np.asarray(pd.read_csv(codedir+"haptable_mig_"+str(ratevec[2])+".csv"))
    mycmap = plt.cm.get_cmap('tab10')
    ax1.imshow(chroms1,extent=[0,1,0,chroms1.shape[0]],aspect='auto',cmap=mycmap,vmin=1,vmax=7)
    ax2.imshow(chroms2,extent=[0,1,0,chroms2.shape[0]],aspect='auto',cmap=mycmap,vmin=1,vmax=7)
    ax3.imshow(chroms3,extent=[0,1,0,chroms3.shape[0]],aspect='auto',cmap=mycmap,vmin=1,vmax=7)
    ax1.set_title(ratevec[0])
    ax2.set_title(ratevec[1])
    ax3.set_title(ratevec[2])
    ax3.set_xlabel('Position (Morgans)')

#------------------Simulations with DMIs----------------------

def print_fitness_matrix(seldmi,selanc,domdmi):
    if domdmi==0:
        h0=1
        h1=1
    if domdmi==1:
        h0=1/4
        h1=1/2
    if domdmi==2:
        h0=0
        h1=1/2
    if domdmi==3:
        h0=0
        h1=0
    fitness_matrix = np.zeros((3,3))
    fitness_matrix[[0,2],[0,2]] = 1
    fitness_matrix[[1,2],[0,1]] = 1-(selanc/2)
    fitness_matrix[[0,1],[1,2]] = 1-seldmi*h1
    fitness_matrix[1,1] = 1-seldmi*h0
    fitness_matrix[2,0] = 1-selanc
    fitness_matrix[0,2] = 1-seldmi
    print('\nFitness Matrix:\n')
    print(fitness_matrix)

def run_dfuse_dmis(rate, tstart, popsize, markers, dmipos, seldmi, selanc, domdmi):
    print_fitness_matrix(seldmi, selanc, domdmi)
    codedir = '../dfuse_files/'
    make_deme_file(popsize)
    make_epis_file(dmipos)
    runstr = "dfuse -d "+codedir+"inputfiles/demefile_dmi -r 1 -g "+ \
             str(tstart)+" -m "+str(rate*2)+" -G "+str(tstart)+" -o "+ \
             codedir+"outputfiles/dmi"+" -l "+str(markers)+" -e "+ \
             codedir+"inputfiles/episfile_dmi -c "+str(seldmi)+ \
             " -a "+str(selanc)+" -R "+str(domdmi)+" -S 'fertility'"
    sp.run(runstr,shell=True)
    haptable = pd.read_csv(codedir+"outputfiles/dmi.haplo",skiprows=[0])
    haptable.drop(haptable.columns[0:8], axis=1, inplace=True)
    haptable = haptable[haptable.isin(['a','b']).all(1)]
    mainfile = codedir+"outputfiles/dmi.main"
    pos = open(mainfile).readline().rstrip()
    pos = np.asarray(pos.split(' ')[2:])
    haptable.columns = pos
    haptable[haptable.isin(['a'])] = 1
    haptable[haptable.isin(['b'])] = 2
    hapfile = codedir+"tempfiles/haptable_mig_"+str(rate)+".csv"
    haptable.to_csv(hapfile,index=False)

def make_epis_file(dmipos):
    inputdir = '../dfuse_files/inputfiles/'
    thefile = open(inputdir+"episfile_dmi", 'w+')
    input_list = [''] * 8
    input_list[0] = '2 1 2'
    input_list[1] = '0 1'
    input_list[2] = '0 0'
    input_list[3] = '0 1'
    input_list[4] = '0 0'
    input_list[5] = '0 0'
    input_list[6] = str(dmipos[0])+' '+str(dmipos[1])
    input_list[7] = '0 1'
    for item in input_list:
        thefile.write("%s\n" % item)
    thefile.close()

def get_dfuse_tracts_dmis(rate,npts,maxlen,chrlen,dmipos):
    codedir = "../dfuse_files/tempfiles/"
    bins = np.array([np.arange(0,maxlen*(1+.5/npts),float(maxlen)/npts)])[0]
    chromdict = make_dfuse_chroms(rate,chrlen)
    tractdict = make_tracts_dmis(chromdict,chrlen,dmipos)
    tractbinsa = make_tractbins_dmis(tractdict['loca'],chrlen,maxlen,npts)
    tractbinsb = make_tractbins_dmis(tractdict['locb'],chrlen,maxlen,npts)
    apopahist = tractbinsa.get('popahist')
    apopbhist = tractbinsa.get('popbhist')
    bpopahist = tractbinsb.get('popahist')
    bpopbhist = tractbinsb.get('popbhist')
    bintablea = pd.DataFrame(data={'col1':apopahist,'col2':apopbhist})
    bintableb = pd.DataFrame(data={'col1':bpopahist,'col2':bpopbhist})
    binfilea = codedir+"dfuse_bintable_a.pkl"
    binfileb = codedir+"dfuse_bintable_b.pkl"
    pkl.dump(bintablea,open(binfilea, "wb" ))
    pkl.dump(bintableb,open(binfileb, "wb" ))

def make_tractbins_dmis(tractdict,chrlen,maxlen,npts):
    bins = np.array([np.arange(0,maxlen*(1+.5/npts),float(maxlen)/npts)])[0]
    popa = np.reshape(tractdict['popa'],(tractdict['popa'].shape[1],2))
    popb = np.reshape(tractdict['popb'],(tractdict['popb'].shape[1],2))
    popaMorgans = popa[:,0]/chrlen
    popbMorgans = popb[:,0]/chrlen
    countsa = np.histogram(popaMorgans,bins)[0]
    countsb = np.histogram(popbMorgans,bins)[0]
    popahist = countsa/(np.sum(countsa)+np.sum(countsb))
    popbhist = countsb/(np.sum(countsa)+np.sum(countsb))
    tractbins = {'popahist':popahist,'popbhist':popbhist}
    return(tractbins)

def plot_locus_tractlengths(ratevec,tstart,Ls,pop,maxlen,npts,chrlen):
    """Plots the predicted locus-specific tract lengths"""
    mig = continuous_mig_hybridzone(rate,tstart)
    model = demographic_model(mig)
    tractsOut = model.expectperbin(Ls,pop,bins)
    tractDist = np.array(tractsOut)
    relTractDist = tractDist / np.sum(tractDist)
    plt.plot(bins[:npts],np.log(relTractDist[:npts:]),label='_nolegend_')

def plot_figure_dfuse_dmis(ratevec,tstart,Ls,pop,maxlen,npts,chrlen):
    """Plots the predicted tract length distributions against the
    simulated tract length distributions from dfuse."""
    bindir = '/home/joelsmith/Projects/dmis/code/dfuse_files/tempfiles/'
    bintablea = pkl.load(open(bindir+"dfuse_bintable_a.pkl", "rb"))
    bintableb = pkl.load(open(bindir+"dfuse_bintable_b.pkl", "rb"))
    bins = np.append(bintablea.index.values,1)
    rows = bintablea.shape[0]
    xAxis = np.array(list(range(1,rows+1)))[:rows-1] / np.array(rows)
    f, (ax1, ax2) = plt.subplots(nrows=1, ncols=2,sharey=True)
    obsa = np.asarray(np.where(np.logical_not(bintablea['col1']==0)))[0][:-1]
    obsb = np.asarray(np.where(np.logical_not(bintablea['col2']==0)))[0][:-1]
    obsvaluesa = bintablea.iloc[obsa]['col1']
    obsvaluesb = bintablea.iloc[obsb]['col2']
    ax1.scatter(x=bintablea.index[obsa],y=np.log(obsvaluesa))
    ax1.scatter(x=bintablea.index[obsb],y=np.log(obsvaluesb))
    ax1.set_xlabel('Tract Length (Morgans)')
    ax1.set_ylabel('log(Relative Frequency)')
    obsa = np.asarray(np.where(np.logical_not(bintableb['col1']==0)))[0][:-1]
    obsb = np.asarray(np.where(np.logical_not(bintableb['col2']==0)))[0][:-1]
    obsvaluesa = bintableb.iloc[obsa]['col1']
    obsvaluesb = bintableb.iloc[obsb]['col2']
    ax2.scatter(x=bintableb.index[obsa],y=np.log(obsvaluesa))
    ax2.scatter(x=bintableb.index[obsb],y=np.log(obsvaluesb))
    ax2.set_xlabel('Tract Length (Morgans)')

def make_tracts_dmis(chromdict,chrlen,dmipos):
    chroms = chromdict['chroms']
    pos = np.int_(chromdict['pos'])
    loca = np.where(pos==dmipos[0]*chrlen)[0]
    locb = np.where(pos==dmipos[1]*chrlen)[0]
    switches = np.apply_along_axis(np.diff,1,chroms)
    switches[:,0] = 1
    index = 0
    lentablea = np.zeros((switches.shape[0],2),dtype='int')
    lentableb = np.zeros((switches.shape[0],2),dtype='int')
    for row in range(chroms.shape[0]):
        rowswitches = np.append(np.nonzero(switches[row,:])[0],len(pos)-1)
        lefta = rowswitches[np.max(np.where(rowswitches<loca)[0])]
        righta = rowswitches[np.min(np.where(rowswitches>loca)[0])]
        leftb = rowswitches[np.max(np.where(rowswitches<locb)[0])]
        rightb = rowswitches[np.min(np.where(rowswitches>locb)[0])]
        lena = pos[righta]-pos[lefta]
        lenb = pos[rightb]-pos[leftb]
        anca = chroms[row,loca]
        ancb = chroms[row,locb]
        lentablea[index:index+1,] = np.array([lena,anca])
        lentableb[index:index+1,] = np.array([lenb,ancb])
        index = index+1
    locapopa = lentablea[np.where(lentablea[:,1]==1),]
    locapopb = lentablea[np.where(lentablea[:,1]==2),]
    locbpopa = lentableb[np.where(lentableb[:,1]==1),]
    locbpopb = lentableb[np.where(lentableb[:,1]==2),]
    loca = {"popa":locapopa,"popb":locapopb}
    locb = {"popa":locbpopa,"popb":locbpopb}
    tractdict = {"loca":loca,"locb":locb}
    return(tractdict)

def plot_image_dfuse_dmis(rate):
    codedir = "../dfuse_files/tempfiles/"
    chroms = np.asarray(pd.read_csv(codedir+"haptable_mig_"+str(rate)+".csv"))
    mycmap = plt.cm.get_cmap('tab10')
    plt.imshow(chroms,extent=[0,1,0,chroms.shape[0]],aspect='auto',cmap=mycmap,vmin=1,vmax=7)
    plt.xlabel('Position (Morgans)')
    plt.ylabel('Chromosome')


from .cli import command, arg
from os.path import exists
from ..search import build_search_body
from ..connection import elasticsearch

import logging
log = logging.getLogger('elastico.cli.search')

@command('search',
    arg('--format', '-F',
        help="format string, which is applied to each match",
        default=None),
    arg('query', help="may be a query, a filename or '-' to read from stdin"),
    arg('index', help="index to query", nargs="?", default='*')
)
def search(config): #host, format, query):
    format = config.get('search.format')
    query  = config.get('search.query')

    log.debug("format=%r query=%r", format, query)
    content = None
    if query == '-':
        content = sys.stdin.read()
    elif exists(query):
        with open(query, 'r') as f:
            content = f.read()
    else:
        config = {}
        config['query'] = query
        config['format'] = format

    if content is not None:
        if content.startswith("---"):
            import frontmatter
            config, content = frontmatter.loads(content)
            config['search.template'] = content
        else:
            config = yaml.loads(io.StringIO(content))

    body = build_search_body(config, 'query')
    log.error("body=%r", body)
    es = elasticsearch(config)
    results = es.search(index=config.get('search.index', '*'), body=body)

    if config.get('search.format'):
        for hit in results['hits']['hits']:
            print(config['search.format'].format(hit))

    elif config.get('search.template'):
        import chevron
        args = {}
        for k in ('template', 'partials_path', 'partials_ext',
           'partials_dict', 'padding', 'def_ldel', 'def_rdel'):
            if k in config['search']:
                args[k] = config['search'][k]

        data = config.get('data', {}).copy()
        data.update(results)
        args['data'] = data
        print(chevron.render(**args))

    else:
        import pyaml
        pyaml.p(results)


from django.shortcuts import render, redirect,get_object_or_404
import requests
from .models import *
from django.db.models import Count
from django.contrib.auth.decorators import login_required
from firstApp.forms import EmpForm,AnlysForm
import tweepy
import pandas as pd
import csv
@login_required(login_url='/login/')
def IndexView(request):
    template_name='index.html'
    return render(request,template_name)

@login_required(login_url='/login/')
def HesapView(request):
    template_name='hesap.html'
    return render(request,template_name)

@login_required(login_url='/login/')
def ContactView(request):
    template_name='contact.html'
    contact = Contact.objects.filter(alıcı=request.user)
    cntfrm = EmpForm(request.POST or None)
    if cntfrm.is_valid():
        message = cntfrm.save(commit=False)
        message.user = request.user
        message.save()
        return redirect('contact')
    return render(request,template_name,{'contact':contact,'form':cntfrm})

def ClearContact(requst,id):
    secilen=get_object_or_404(Contact,id=id)
    secilen.delete()
    return redirect('contact')

def TweetGetir(request):
    consumer_key =''
    consumer_secret =''
    access_token =''
    access_token_secret =''
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    api=tweepy.API(auth)
    gelenVeri=request.GET.get('gelenVeri',default = 'None')
    if gelenVeri is not None and gelenVeri != '':
            tweets = api.search(q = gelenVeri,lang = "tr", count=10)
            data = pd.DataFrame(data=[tweet.text for tweet in tweets], columns=['text'])
            for i in range(len(data)):
                tweet=Tweets()
                tweet.tweet=data.loc[i]['text']
                tweet.user=request.user
                tweet.hastag=gelenVeri
                tweet.userName=""
                tweet.userAvatar=""
                tweet.save()
            return redirect('tweets')
    else:
        return redirect('tweets')

def FilterTweet(request,hastag):
    template_name='tweets.html'
    tweet_all= Tweets.objects.values('hastag').filter(user=request.user).annotate(total=Count('hastag'))
    tweet = Tweets.objects.filter(hastag=hastag)
    return render(request,template_name,{'tweet':tweet,'tweet_all':tweet_all})

@login_required(login_url='/login/')
def TweetView(request):
    template_name='tweets.html'
    tweet_all= Tweets.objects.values('hastag').filter(user=request.user).annotate(total=Count('hastag'))
    tweet = Tweets.objects.filter(user=request.user)
    contex={
        'tweet':tweet,'tweet_all':tweet_all
    }
    return render(request,template_name,contex)

@login_required(login_url='/login/')
def AnalizIstekView(request):
    template_name='analizistek.html'
    anlzfrm = AnlysForm(request.POST or None)
    if anlzfrm.is_valid():
        message = anlzfrm.save(commit=False)
        message.Auser = request.user
        message.Aalıci_id = 1
        message.save()
        return redirect('analizistek')
    return render(request,template_name,{'form':anlzfrm})

@login_required(login_url='/login/')
def AnalizView(request):
    if not request.user.is_authenticated:
       return redirect('index')
    post = Post.objects.filter(user=request.user )
    dosya=request.GET.get('durum',default = 'None')
    print(dosya)
    if dosya == 'None':
        dosya=str(329)
    kf= pd.read_csv('media/'+dosya+'.csv', encoding='utf_8')
    ld = pd.read_csv('media/'+dosya+'_LD.csv', encoding='utf_8')
    tk= pd.read_csv('media/'+dosya+'_TK.csv', encoding='utf_8')
    gd= pd.read_csv('media/'+dosya+'_GD.csv', encoding='utf_8')
    contex={
        'post':post,
        'kelimefrekans': kf.to_dict('records'),
        'gundagilimi': gd.to_dict('records'),
        'tweetkaynak': tk.to_dict('records'),
        'lokasyondagilimi': ld.to_dict('records'),
    }
    return render(request,'analiz.html',contex)


#!/usr/bin/env python
# coding: utf-8

# In[1]:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns

# In[2]:

data=pd.read_excel(r"C:\Users\user\Downloads\iris (4).xls")

# In[3]:

data

# In[5]:

data.isna().sum()

# In[6]:

data.describe()

# In[7]:

data['SL']=data['SL'].fillna(data['SL'].median())

# In[8]:

data

# In[9]:

data.isna().sum()

# In[10]:

data['SW']=data['SW'].fillna(data['SW'].median())

# In[11]:

data.isna().sum()

# In[12]:

data['PL']=data['PL'].fillna(data['PL'].median())

# In[13]:

data

# In[14]:

data.isna().sum()

# In[15]:

for i in['SL','SW','PL','PW']:
    plt.figure()
    plt.boxplot(data[i])
    plt.title(i)

# In[16]:

from sklearn.preprocessing import LabelEncoder

# In[17]:

label_encoder=LabelEncoder()

# In[18]:

data['Classification']=label_encoder.fit_transform(data['Classification'])

# In[19]:

data.head()

# In[20]:

data.tail()

# In[21]:

data

# In[22]:

data['Classification'].unique()

# # logistic regression

# In[56]:

x=data.drop(['Classification'],axis=1)
y=data['Classification']

# In[57]:

from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test=train_test_split(x,y,random_state=42,test_size=0.2)

# In[58]:

from sklearn.linear_model import LogisticRegression
logit_model=LogisticRegression()
logit_model.fit(x_train,y_train)
y_pred=logit_model.predict(x_test)

# In[59]:

y_pred

# In[60]:

from sklearn.metrics import confusion_matrix,accuracy_score
print('Accuracy is ',accuracy_score(y_test,y_pred))

# In[61]:

confusion_matrix(y_test,y_pred)

# # knn

# In[29]:

from sklearn.neighbors import KNeighborsClassifier
metric_k=[]
neighbors=np.arange(3,15)

# In[30]:

for k in neighbors:
    classifier=KNeighborsClassifier(n_neighbors=k,metric='minkowski',p=2)
    classifier.fit(x_train,y_train)
    y_pred=classifier.predict(x_test)
    acc=accuracy_score(y_test,y_pred)
    metric_k.append(acc)

# In[31]:

plt.plot(neighbors,metric_k,'o-')
plt.xlabel('k value')
plt.ylabel('accuracy')
plt.grid()

# In[33]:

classifier=KNeighborsClassifier(n_neighbors=4,metric='minkowski',p=2)
classifier.fit(x_train,y_train)
y_pred=classifier.predict(x_test)

# In[34]:

print('Accuracy is',accuracy_score(y_test,y_pred))

# In[35]:

confusion_matrix(y_test,y_pred)

# # svm

# In[62]:

from sklearn.svm import SVC
svmclf=SVC(kernel='linear')
svmclf.fit(x_train,y_train)

# In[63]:

y_pred=svmclf.predict(x_test)

# In[64]:

y_pred

# In[65]:

from sklearn.metrics import accuracy_score,confusion_matrix
print('Accuracy is',accuracy_score(y_test,y_pred))

# In[66]:

from sklearn.svm import SVC
svmclf=SVC(kernel='rbf')
svmclf.fit(x_train,y_train)

# In[67]:

y_pred=svmclf.predict(x_test)

# In[68]:

y_pred

# In[69]:

from sklearn.metrics import accuracy_score,confusion_matrix
print('Accuracy is',accuracy_score(y_test,y_pred))

# In[70]:

confusion_matrix(y_test,y_pred)

# # decision tree

# In[44]:

from sklearn.tree import DecisionTreeClassifier
dt_clf=DecisionTreeClassifier(random_state=42)
dt_clf.fit(x_train,y_train)
y_pred=dt_clf.predict(x_test)

# In[45]:

print('Accuracy is',accuracy_score(y_test,y_pred))

# In[46]:

print(confusion_matrix(y_test,y_pred))

# # random forest

# In[47]:

from sklearn.ensemble import RandomForestClassifier
rf_clf=RandomForestClassifier(random_state=42)
rf_clf.fit(x_train,y_train)

# In[48]:

y_pred=rf_clf.predict(x_test)

# In[49]:

y_pred

# In[50]:

print('Accuracy is',accuracy_score(y_test,y_pred))

# In[51]:

print(confusion_matrix(y_test,y_pred))

# In[52]:

#hyper parametric tuning
rf_c1f1=RandomForestClassifier(n_estimators=20,max_depth=3,criterion='entropy',min_samples_split=2,random_state=42)
rf_c1f1.fit(x_train,y_train)

# In[53]:

y_pred=rf_c1f1.predict(x_test)

# In[54]:

print('Accuracy is',accuracy_score(y_test,y_pred))

# In[55]:

print(confusion_matrix(y_test,y_pred))

# In[ ]:


import streamlit as st
from PIL import Image
import torch
import torchvision.transforms as transforms
from torchvision.models import resnet50
import requests

# Load the pre-trained ResNet50 model
#model = resnet50(pretrained=True)
#model.eval()

# Define the transformations to be applied to the uploaded image
#transform = transforms.Compose([
#    transforms.Resize((224, 224)),
#    transforms.ToTensor(),
#    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
#])

# Function to make predictions on the uploaded image
def classify_image(image):
    image = transform(image).unsqueeze(0)
    output = model(image)
    _, predicted_idx = torch.max(output, 1)
    return predicted_idx.item()

# Streamlit app
st.title("Image Classifier App")
st.write("Upload an image and the app will classify it.")

# File upload
uploaded_file = st.file_uploader("Choose an image file", type=["jpg", "jpeg", "png"])

# Display the uploaded image
if uploaded_file is not None:
    image = Image.open(uploaded_file)
    st.image(image, caption="Uploaded Image", use_column_width=True)

    # Make prediction on the uploaded image
    if st.button("Classify"):
        # Convert PIL image to RGB
        image = image.convert("RGB")

        # Classify the image
        predicted_idx = classify_image(image)

        # Get the label for the predicted class (using ImageNet labels)
        response = requests.get("https://raw.githubusercontent.com/anishathalye/imagenet-simple-labels/master/imagenet-simple-labels.json")
        labels = response.json()
        predicted_label = labels[predicted_idx]

        st.write(f"Prediction: {predicted_label}")


import os
import json
import requests
from utils.apipagespeed import lighthouses
from utils.check_status_code import check_status_code

with open('pagespeed.json',  encoding="utf8") as \
        pagespeedurls:
    urlsJson = json.load(pagespeedurls)

    for urls in urlsJson:
        statusOK = check_status_code(urls["url"])

        if statusOK == False:
            continue

        else:
            lighthouse = lighthouses(urls)

            if lighthouse is None:
                print('Erro no envio para a Elastic...')
                print('')

            else:
                urlelastic = os.environ["ELASTIC_URL_API"]
                login = os.environ["AUTH_USER"]
                pswd = os.environ["AUTH_PASS"]
                headers = {"Content-Type": "application/json"}
                r = requests.post(url=urlelastic, auth=(
                    login, pswd), headers=headers, data=json.dumps(lighthouse))
                print('Sucesso no Envio...')
                print('')


import numpy as np
from PIL import Image
from sklearn.naive_bayes import MultinomialNB
#importarea bibliotecilor

def valori_inspre_intervale(poze, intervale):
    poze_noi = np.zeros(poze.shape)
    for i in range(poze.shape[0]):
        poze_noi[i] = np.digitize(poze[i], intervale)
    return poze_noi - 1

# aceasta este o functie ce primeste un set de imagini si numarul de intervale si incadreaza aceste date
# intr-un numar de intervale dat
def citire_de_date(st, sf):
    i = st
    set = []
    while i < sf:
        if i < 10:
            cale = "/kaggle/input/unibuc-brain-ad/data/data/00000" + str(i) + ".png"
        elif i < 100:
            cale = "/kaggle/input/unibuc-brain-ad/data/data/0000" + str(i) + ".png"
        elif i < 1000:
            cale = "/kaggle/input/unibuc-brain-ad/data/data/000" + str(i) + ".png"
        elif i < 10000:
            cale = "/kaggle/input/unibuc-brain-ad/data/data/00" + str(i) + ".png"
        else:
            cale = "/kaggle/input/unibuc-brain-ad/data/data/0" + str(i) + ".png"
        poza = Image.open(cale).convert('L')
        linie = np.array(poza)
        linie_np = np.array(linie).flatten()
        set.append(linie)
        i = i + 1
    return set

# aceasta functie are ca rol citirea de date, fie de validare, test sau train.
imagini_ct_train = citire_de_date(1, 17001)

imagini_ct_valid = citire_de_date(15001, 17001)

imagini_ct_test = citire_de_date(17001, 22150)
# apelarea functiei pentru cele trei seturi de date. Am citit si datele de valid in train pentru o antrenare cat mai eficienta

indici_train = []
with open('/kaggle/input/unibuc-brain-ad/data/train_labels.txt', 'r') as f:
    f.readline()
    indici_train = [int(line.strip().split(",")[1]) for line in f][:15000]

indici_valid = []
with open('/kaggle/input/unibuc-brain-ad/data/validation_labels.txt', 'r') as f:
    f.readline()
    for line in f.readlines():
        indici_valid.append(int(line.strip().split(',')[1]))
        indici_train.append(int(line.strip().split(',')[1]))

# am citit labelurile pentru setul de train si valid, adaugand in indici_train si labelurile de la valid
acc = []
imagini_ct_valid = np.array((imagini_ct_valid))
imagini_ct_train = np.array((imagini_ct_train))
imagini_ct_test = np.array((imagini_ct_test))
intervale = np.linspace(0, 230, 4)

interval_train = valori_inspre_intervale(imagini_ct_train, intervale)
interval_train = interval_train.reshape(interval_train.shape[0], -1)
interval_valid = valori_inspre_intervale(imagini_ct_valid, intervale)
interval_valid = interval_valid.reshape(interval_valid.shape[0], -1)
interval_test = valori_inspre_intervale(imagini_ct_test, intervale)
interval_test = interval_test.reshape(interval_test.shape[0], -1)
#transformarea datelor in intervale si redimensionarea acestora
naive_bayes_model = MultinomialNB(alpha=3.2)
# apelarea functiei pentru Naive Bayes in care am introdus si un hiperparametru de 3.2
naive_bayes_model.fit(interval_train, indici_train)
# antrenarea modelului
print(naive_bayes_model.score(interval_valid, indici_valid))
indici_predictie = naive_bayes_model.predict(interval_test)

# apelarea functiei de predict pentru a obtine numarul de predictii
lista_afisare = [["id", "class"]]
for i in range(17001, 22150):
    lista_afisare.append([int(i), int(indici_predictie[i - 17001])])
# introducerea in lista_afisare datele pentru csv si ulterior salvarea fisierului
np.savetxt("Competitie.csv", lista_afisare, fmt="%s", delimiter=',')


import random
import shutil
from datetime import date
from itertools import compress

import numpy as np
import pytest

from thermal_events import (
    AnalysisStatus,
    Base,
    Category,
    Dataset,
    Device,
    LineOfSight,
    Method,
    ParentChildRelationship,
    Severity,
    ThermalEvent,
    ThermalEventCategoryLineOfSight,
    User,
    crud,
)
from thermal_events.database import get_db, temp_folder_path

from .test_thermal_event import (
    analysis_status,
    categories,
    devices,
    lines_of_sight,
    methods,
    severity_types,
    users,
)

from .test_thermal_event import random_event as _random_event

# Number of different datasets
NB_DATASETS = 3

# Random matrix encoding the compatibility between the lines of sights and the
# thermal event categories
rows = len(lines_of_sight)
cols = len(categories)
COMPATIBILITY = np.random.randint(0, 2, size=(rows, cols), dtype=bool)
row_sums = COMPATIBILITY.sum(axis=1)
row_indices = np.where(row_sums == 0)
COMPATIBILITY[row_indices, np.random.randint(0, cols, len(row_indices))] = True

def random_event(*args):
    return _random_event(*args, COMPATIBILITY)

def create_genealogy():
    """Generate random thermal events, send them to the database and create a
        random genealogy between the thermal events

    Returns:
        list: The ids of the randomly created thermal events, sorted
        numpy.ndarray: A matrix that encodes the parent / child relationship
            between the thermal events. The rows correspond to parents and the
            columns to children, so that mat[i, j] == True indicates that
            thermal event i is a parent of thermal event j
    """
    thermal_events = []
    for _ in range(10):
        thermal_events.append(random_event(10))
    crud.thermal_event.create(thermal_events)

    ids = sorted([x.id for x in thermal_events])

    mat = np.random.choice(a=[False, True], size=(len(ids), len(ids)))
    np.fill_diagonal(mat, 0)

    genealogy = []
    for i in range(len(ids)):
        for j in range(len(ids)):
            if not mat[i, j]:
                continue

            genealogy.append(
                ParentChildRelationship(
                    parent=ids[i], child=ids[j], timestamp_ns=random.randrange(100)
                )
            )

    with get_db() as session:
        session.add_all(genealogy)
        session.commit()

    return ids, mat

@pytest.fixture(scope="session", autouse=True)
def create_and_delete_temporary_database():
    """Create an temporary SQLITE database at the beginning of the test session,
    with all the necessary tables and values, and delete the database file at the
    end of the session
    """
    users.append([crud.user._user_name(), "current_user@domain.org"])

    with get_db() as session:
        # Ensure we work with a test database before creating tables
        assert "test_database.sqlite" in str(session.bind.url)

        Base.metadata.create_all(session.bind)

        session.add_all([LineOfSight(name=x) for x in lines_of_sight])
        session.add_all([Device(name=x) for x in devices])
        session.add_all([Category(name=x) for x in categories])
        categories_lines_of_sight = []
        for ind_category, category in enumerate(categories):
            for ind_los, line_of_sight in enumerate(lines_of_sight):
                if not COMPATIBILITY[ind_los, ind_category]:
                    continue
                categories_lines_of_sight.append(
                    ThermalEventCategoryLineOfSight(
                        thermal_event_category=category, line_of_sight=line_of_sight
                    )
                )
        session.add_all(categories_lines_of_sight)
        session.add_all([Method(name=x) for x in methods])
        session.add_all([User(name=x[0], email=x[1]) for x in users])
        session.add_all([Severity(name=x) for x in severity_types])
        session.add_all([AnalysisStatus(name=x) for x in analysis_status])
        session.add_all(
            [
                Dataset(creation_date=date.today(), annotation_type="bbox")
                for _ in range(NB_DATASETS)
            ]
        )

        session.commit()

    yield

    # Delete the temporary folder
    shutil.rmtree(temp_folder_path)

@pytest.fixture(autouse=True)
def reset_temporary_database():
    """Reset the temporary database after each test by emptying the tables linked
    to the thermal events (thermal_events, thermal_events_instances,
    thermal_events_genealogy)
    """
    yield

    # Clear the database
    with get_db() as session:
        # Ensure we work with the test database before emptying tables
        assert "test_database.sqlite" in str(session.bind.url)

        session.query(ParentChildRelationship).delete()
        session.query(ThermalEvent).delete()
        session.commit()

def test_thermal_event_create_read():
    # Generate a random thermal event
    expected = random_event(10)

    # Send the thermal event to the database
    crud.thermal_event.create(expected)

    # Read the thermal event from the database
    actual = crud.thermal_event.get(expected.id)

    assert expected.initial_timestamp_ns == actual.initial_timestamp_ns
    assert expected.final_timestamp_ns == actual.final_timestamp_ns
    assert expected.duration_ns == actual.duration_ns
    assert expected.max_temperature_C == actual.max_temperature_C
    assert len(expected.instances) == len(actual.instances)

def test_thermal_event_get_multi():
    # Generate random thermal events
    nb = 5
    thermal_events = [random_event(10) for _ in range(nb)]

    # Send the thermal events to the database
    crud.thermal_event.create(thermal_events)

    # Store their ids
    expected = [x.id for x in thermal_events]

    # Retrieve the thermal events and their ids
    thermal_events_read = crud.thermal_event.get_multi(limit=nb)
    actual = [x.id for x in thermal_events_read]

    assert actual == expected

def test_thermal_event_update():
    # Generate a random thermal event
    thermal_event = random_event(10)

    # Send the thermal event to the database
    crud.thermal_event.create(thermal_event)

    # Update its method with a different, randomly chosen one
    expected = random.choice([m for m in methods if m != thermal_event.method])
    thermal_event.method = expected
    crud.thermal_event.update(thermal_event)

    # Retriveve the event and check if its method has been updated
    thermal_event_db = crud.thermal_event.get(thermal_event.id)

    assert thermal_event_db.method == expected

    # Delete the event through an update by emptying its instances
    thermal_event_db.instances = []
    crud.thermal_event.update(thermal_event_db)

    assert crud.thermal_event.get(thermal_event_db.id) is None

def test_thermal_event_delete():
    # Generate a random thermal event
    thermal_event = random_event(10)

    # Send the thermal event to the database
    crud.thermal_event.create(thermal_event)

    # Delete the event and check it is deleted in the database
    crud.thermal_event.delete(thermal_event)

    assert crud.thermal_event.get(thermal_event.id) is None

def test_thermal_event_get_by_columns():
    # Generate a random thermal event with specific values for some attributes
    thermal_event = random_event(10)

    line_of_sight = random.choice(lines_of_sight)
    category = random.choice(
        list(
            compress(categories, COMPATIBILITY[lines_of_sight.index(line_of_sight), :])
        )
    )

    thermal_event.user = users[0][0]
    thermal_event.experiment_id = 123
    thermal_event.method = methods[0]
    thermal_event.line_of_sight = line_of_sight
    thermal_event.category = category

    # Send the thermal event to the database
    crud.thermal_event.create(thermal_event)

    events = crud.thermal_event.get_by_columns(user=users[0][0])
    assert len(events) == 1

    ids = crud.thermal_event.get_by_columns(experiment_id=123, return_columns=["id"])
    assert len(ids) == 1

    x, y = crud.thermal_event.get_by_columns(
        method=methods[0], return_columns=["experiment_id", "line_of_sight"]
    )
    assert len(x) == 1
    assert len(y) == 1

    ids = crud.thermal_event.get_by_columns(
        dataset=["xxx", "yyy"], return_columns=["id"]
    )
    assert len(ids) == 0

    events = crud.thermal_event.get_by_columns(
        dataset="1", line_of_sight=line_of_sight, category=category
    )
    assert len(events) == 1

def test_thermal_event_get_by_columns_exclude_time_intervals():
    # Generate random thermal events with different initial and final timestamps
    thermal_events = []
    thermal_events.append(random_event(10))
    thermal_events[-1].initial_timestamp_ns = 10
    thermal_events[-1].final_timestamp_ns = 20

    thermal_events.append(random_event(10))
    thermal_events[-1].initial_timestamp_ns = 15
    thermal_events[-1].final_timestamp_ns = 30

    thermal_events.append(random_event(10))
    thermal_events[-1].initial_timestamp_ns = 25
    thermal_events[-1].final_timestamp_ns = 50

    # Send the thermal events to the database
    crud.thermal_event.create(thermal_events)

    events = crud.thermal_event.get_by_columns_exclude_time_intervals(
        dataset=1, time_intervals=[15, None], return_columns=["id"]
    )
    assert len(events) == 1

    events = crud.thermal_event.get_by_columns_exclude_time_intervals(
        dataset=1,
        time_intervals=[None, 25],
    )
    assert len(events) == 2

    events = crud.thermal_event.get_by_columns_exclude_time_intervals(
        dataset=1,
        time_intervals=[[None, 20], [25, 60]],
    )
    assert len(events) == 1

def test_thermal_event_get_by_experiment_id():
    # Create random thermal events with different experiment ids
    thermal_events = []
    for experiment_id in range(100, 140):
        thermal_events.append(random_event(10))
        thermal_events[-1].experiment_id = experiment_id

    # Send the thermal events to the database
    crud.thermal_event.create(thermal_events)

    expected = [x.id for x in thermal_events if x.experiment_id == 100]
    actual = crud.thermal_event.get_by_experiment_id(100, return_columns=["id"])
    assert actual == expected

    expected = [x.id for x in thermal_events if 120 <= x.experiment_id <= 130]
    events = crud.thermal_event.get_by_experiment_id(120, 130)
    actual = [x.id for x in events]
    assert actual == expected

def test_thermal_event_get_by_experiment_id_line_of_sight():
    # Create random thermal events with different experiment ids and lines of sight
    ids = [100, 101]
    id_counts = [random.randrange(1, 10) for _ in ids]

    thermal_events = []
    for ind, experiment_id in enumerate(ids):
        for _ in range(id_counts[ind]):
            thermal_event = random_event(10)
            thermal_event.experiment_id = experiment_id
            thermal_event.line_of_sight = lines_of_sight[0]
            category = random.choice(
                list(
                    compress(
                        categories,
                        COMPATIBILITY[
                            lines_of_sight.index(thermal_event.line_of_sight), :
                        ],
                    )
                )
            )
            thermal_event.category = category
            thermal_events.append(thermal_event)

    # Send the thermal events to the database
    crud.thermal_event.create(thermal_events)

    expected = [
        x.id
        for x in thermal_events
        if x.experiment_id == ids[0]
        if x.line_of_sight == lines_of_sight[0]
    ]
    actual = crud.thermal_event.get_by_experiment_id_line_of_sight(
        ids[0], lines_of_sight[0], return_columns=["id"]
    )
    assert actual == expected

    events = crud.thermal_event.get_by_experiment_id_line_of_sight(
        ids[1], lines_of_sight[1]
    )
    assert len(events) == 0

def test_thermal_event_get_by_device():
    # Generate random thermal events with different devices
    device_counts = [random.randrange(10) for _ in devices]

    thermal_events = []
    for ind, device in enumerate(devices):
        for _ in range(device_counts[ind]):
            thermal_events.append(random_event(10))
            thermal_events[-1].device = device

    # Send the thermal events to the database
    crud.thermal_event.create(thermal_events)

    for ind, device in enumerate(devices):
        expected = [x.id for x in thermal_events if x.device == device]
        actual = crud.thermal_event.get_by_device(device, return_columns=["id"])
        assert actual == expected

def test_thermal_event_get_by_dataset():
    # Generate random thermal events with different datasets
    datasets = ["1", "2", "3", "1, 2", "2, 3"]
    dataset_counts = [random.randrange(1, 10) for _ in datasets]

    thermal_events = []
    for ind, dataset in enumerate(datasets):
        for _ in range(dataset_counts[ind]):
            thermal_events.append(random_event(10))
            thermal_events[-1].dataset = dataset

    # Send the thermal events to the database
    crud.thermal_event.create(thermal_events)

    for ind, dataset in enumerate(datasets):
        expected = [x.id for x in thermal_events if dataset in x.dataset]

        actual = crud.thermal_event.get_by_dataset(dataset, return_columns=["id"])
        assert sorted(actual) == sorted(expected)

def test_thermal_event_get_parents_of_thermal_event():
    # Create random thermal events with random parent/child relationships
    ids, mat = create_genealogy()

    for ind, id in enumerate(ids):
        parents = crud.thermal_event.get_parents_of_thermal_event(id)
        expected = list(compress(ids, mat[:, ind]))
        actual = [x.id for x in parents]

        assert actual == expected

def test_thermal_event_get_children_of_thermal_event():
    # Create random thermal events with random parent/child relationships
    ids, mat = create_genealogy()

    for ind, id in enumerate(ids):
        children = crud.thermal_event.get_children_of_thermal_event(id)
        actual = [x.id for x in children]
        expected = list(compress(ids, mat[ind, :]))

        assert actual == expected

def test_thermal_event_change_analysis_status():
    # Create a random thermal event
    thermal_event = random_event(10)

    # Send the thermal event to the database
    crud.thermal_event.create(thermal_event)

    # Change the status with a random one
    expected = random.choice(
        [m for m in analysis_status if m != thermal_event.analysis_status]
    )
    crud.thermal_event.change_analysis_status(thermal_event.id, expected)

    actual = crud.thermal_event.get_by_columns(
        id=thermal_event.id, return_columns=["analysis_status"]
    )[0]

    assert actual == expected

def test_user():
    # Check the users list
    assert crud.user.list() == sorted([x[0] for x in users], key=lambda s: s.lower())

    # Check that the current user has read and write rights
    assert crud.user.has_read_rights()
    assert crud.user.has_write_rights()

    # Check that the user's email address is correctly retrieved
    assert crud.user.email_address(users[0][0]) == users[0][1]
    assert crud.user.email_address(crud.user._user_name()) == users[-1][1]
    assert crud.user.email_address("fake user") is None

def test_thermal_event_category():
    # Check the categories list
    assert crud.thermal_event_category.list() == categories

    # Check the compatibilities between lines of sight and thermal event categories
    for ind, category in enumerate(categories):
        expected = list(compress(lines_of_sight, COMPATIBILITY[:, ind]))
        actual = crud.thermal_event_category.compatible_lines_of_sight(category)
        assert actual == expected

def test_dataset():
    # Check the number of datasets
    assert len(crud.dataset.list()) == NB_DATASETS

def test_analysis_status():
    # Check the analysis status list
    assert crud.analysis_status.list() == analysis_status

def test_line_of_sight():
    # Check the lines of sight list
    assert crud.line_of_sight.list() == lines_of_sight

def test_method():
    # Check the methods list
    assert crud.method.list() == methods

def test_device():
    # Check the devices list
    assert crud.device.list() == devices

def test_severity():
    # Check the severity types list
    assert crud.severity.list() == severity_types


import numbers
from typing import Optional, Iterable, Tuple, Union

import numpy as np

from ..algorithms.matrixaverager import MatrixAverager, ErrorPropagationMethod

class Curve:
    _data: np.ndarray

    def __init__(self):
        pass

    @property
    def q(self) -> np.ndarray:
        return self._data[:, 0]

    @q.setter
    def q(self, newvalue):
        self._data[:, 0] = newvalue

    @property
    def intensity(self) -> np.ndarray:
        return self._data[:, 1]

    @intensity.setter
    def intensity(self, newvalue):
        self._data[:, 1] = newvalue

    @property
    def uncertainty(self) -> np.ndarray:
        return self._data[:, 2]

    @uncertainty.setter
    def uncertainty(self, newvalue):
        self._data[:, 2] = newvalue

    @property
    def quncertainty(self) -> np.ndarray:
        return self._data[:, 3]

    @quncertainty.setter
    def quncertainty(self, newvalue):
        self._data[:, 3] = newvalue

    @property
    def binarea(self) -> np.ndarray:
        return self._data[:, 4]

    @binarea.setter
    def binarea(self, newvalue):
        self._data[:, 4] = newvalue

    @property
    def pixel(self) -> np.ndarray:
        return self._data[:, 5]

    @pixel.setter
    def pixel(self, newvalue):
        self._data[:, 5] = newvalue

    @classmethod
    def fromFile(cls, filename: str, *args, **kwargs) -> "Curve":
        data = np.loadtxt(filename, *args, **kwargs)
        self = cls()
        self._data = np.empty((data.shape[0], 6)) * np.nan
        self._data[:, :min(data.shape[1], 6)] = data[:, :min(data.shape[1], 6)]
        return self

    @classmethod
    def fromArray(cls, array: np.ndarray) -> "Curve":
        self = cls()
        self._data = np.empty((array.shape[0], 6), array.dtype) + np.nan
        self._data[:, :array.shape[1]] = array
        return self

    def __array__(self) -> np.ndarray:
        return self._data

    asArray = __array__

    @classmethod
    def fromVectors(cls, q: np.ndarray, intensity: np.ndarray, uncertainty: Optional[np.ndarray] = None,
                    quncertainty: Optional[np.ndarray] = None, binarea: Optional[np.ndarray] = None,
                    pixel: Optional[np.ndarray] = None) -> "Curve":
        assert (q is not None) and (intensity is not None)
        if not all([
            q.ndim == 1,
            intensity.ndim == 1,
            (uncertainty is None) or (uncertainty.ndim == 1),
            (quncertainty is None) or (quncertainty.ndim == 1),
            (binarea is None) or (binarea.ndim == 1),
            (pixel is None) or (pixel.ndim == 1)
        ]):
            raise ValueError('Supplied arrays must be one-dimensional.')
        if not all([
            len(intensity) == len(q),
            (uncertainty is None) or (len(uncertainty) == len(q)),
            (quncertainty is None) or (len(quncertainty) == len(q)),
            (binarea is None) or (len(binarea) == len(q)),
            (pixel is None) or (len(pixel) == len(q))
        ]):
            raise ValueError('All vectors must have the same length.')
        self = cls()
        self._data = np.empty((len(q), 6))
        self._data[:, 0] = q
        self._data[:, 1] = intensity
        self._data[:, 2] = uncertainty if uncertainty is not None else np.nan
        self._data[:, 3] = quncertainty if quncertainty is not None else np.nan
        self._data[:, 4] = binarea if binarea is not None else np.nan
        self._data[:, 5] = pixel if pixel is not None else np.nan
        return self

    def trim(self, left: float = -np.inf, right: float = np.inf, bottom=-np.inf, top=np.inf, bypixel: bool = False):
        if bypixel:
            xtrimidx = np.logical_and(self._data[:, 5] <= right, self._data[:, 5] >= left)
        else:
            xtrimidx = np.logical_and(self._data[:, 0] <= right, self._data[:, 0] >= left)
        ytrimidx = np.logical_and(self._data[:, 1] <= top, self._data[:, 1] >= bottom)
        curve = Curve()
        curve._data = self._data[np.logical_and(xtrimidx, ytrimidx)]
        return curve

    def __len__(self) -> int:
        return self._data.shape[0]

    def sanitize(self) -> "Curve":
        return Curve.fromArray(self._data[self.isvalid(), :])

    @classmethod
    def average(cls, curves: Iterable["Curve"], ierrorpropagation: ErrorPropagationMethod,
                qerrorpropagation: ErrorPropagationMethod) -> "Curve":
        qavg = MatrixAverager(errorpropagationmethod=qerrorpropagation)
        iavg = MatrixAverager(errorpropagationmethod=ierrorpropagation)
        aavg = MatrixAverager(errorpropagationmethod=ierrorpropagation)
        pavg = MatrixAverager(errorpropagationmethod=ierrorpropagation)
        for c in curves:
            qavg.add(c.q, c.quncertainty)
            iavg.add(c.intensity, c.uncertainty)
            aavg.add(c.binarea, c.binarea)
            pavg.add(c.pixel, c.pixel)
        q, dq = qavg.get()
        i, di = iavg.get()
        a = aavg.get()[0]
        p = pavg.get()[0]
        return Curve.fromVectors(q, i, di, dq, a, p)

    def isfinite(self) -> np.ndarray:
        idx = np.logical_and(np.isfinite(self.q, np.isfinite(self.intensity)))
        for vector in [self.uncertainty, self.quncertainty, self.pixel, self.binarea]:
            if np.isfinite(vector).sum() > 0:  # if not all elements are NaN
                idx = np.logical_and(idx, np.isfinite(vector))
        return idx

    def isvalid(self) -> np.ndarray:
        idx = np.logical_and(np.isfinite(self.q), np.isfinite(self.intensity))
        idx = np.logical_and(idx, self.q > 0)
        for vector in [self.uncertainty, self.quncertainty, self.pixel]:
            if np.isfinite(vector).sum() > 0:
                idx = np.logical_and(
                    idx,
                    np.logical_and(
                        np.isfinite(vector),
                        vector >= 0
                    ))
        if np.isfinite(self.binarea).sum() > 0:
            idx = np.logical_and(
                idx,
                np.logical_and(
                    np.isfinite(self.binarea),
                    self.binarea > 0
                ))
        return idx

    def __getitem__(self, item) -> "Curve":
        if isinstance(item, np.ndarray) and (item.dtype == np.bool):
            return Curve.fromArray(self._data[item, :])

    def _checkcompatibility(self, other: "Curve", maxdifferenceratio: float = 0.005):
        incompatibility = np.abs(self.q - other.q) / np.max(np.mean((self.q, other.q)), axis=0) * 2
        if np.any(incompatibility[np.isfinite(incompatibility)] > maxdifferenceratio):  # 0.01 means 1%
            raise ValueError(
                f'The two q-scales are incompatible. Max. incompatibility: {incompatibility[np.isfinite(incompatibility)].max()}')

    def __sub__(self, other: Union["Curve", numbers.Real, Tuple[numbers.Real, numbers.Real]]) -> "Curve":
        if isinstance(other, Curve):
            self._checkcompatibility(other)
            return self.fromVectors(
                q=0.5 * (self.q + other.q),
                intensity=self.intensity - other.intensity,
                uncertainty=(self.uncertainty ** 2 + other.uncertainty ** 2) ** 0.5,
                quncertainty=(self.quncertainty ** 2 + other.quncertainty ** 2) ** 0.5 / 2.0,
                binarea=None,
                pixel=None
            )
        elif isinstance(other, numbers.Real):
            return self.fromVectors(
                q=self.q,
                intensity=self.intensity - other,
                uncertainty=self.uncertainty,
                quncertainty=self.quncertainty,
                binarea=self.binarea,
                pixel=self.pixel
            )
        elif isinstance(other, tuple) and (len(other) == 2) and isinstance(other[0], numbers.Real) and isinstance(
                other[1], numbers.Real):
            return self.fromVectors(
                q=self.q,
                intensity=self.intensity - other[0],
                uncertainty=(self.uncertainty ** 2 + other[1] ** 2) ** 0.5,
                quncertainty=self.quncertainty,
                binarea=self.binarea,
                pixel=self.pixel
            )
        else:
            return NotImplemented

    def __add__(self, other: Union["Curve", numbers.Real, Tuple[numbers.Real, numbers.Real]]) -> "Curve":
        if isinstance(other, Curve):
            self._checkcompatibility(other)
            return self.fromVectors(
                q=0.5 * (self.q + other.q),
                intensity=self.intensity + other.intensity,
                uncertainty=(self.uncertainty ** 2 + other.uncertainty ** 2) ** 0.5,
                quncertainty=(self.quncertainty ** 2 + other.quncertainty ** 2) ** 0.5 / 2.0,
                binarea=None,
                pixel=None
            )
        elif isinstance(other, numbers.Real):
            return self.fromVectors(
                q=self.q,
                intensity=self.intensity + other,
                uncertainty=self.uncertainty,
                quncertainty=self.quncertainty,
                binarea=self.binarea,
                pixel=self.pixel
            )
        elif isinstance(other, tuple) and (len(other) == 2) and isinstance(other[0], numbers.Real) and isinstance(
                other[1], numbers.Real):
            return self.fromVectors(
                q=self.q,
                intensity=self.intensity + other[0],
                uncertainty=(self.uncertainty ** 2 + other[1] ** 2) ** 0.5,
                quncertainty=self.quncertainty,
                binarea=self.binarea,
                pixel=self.pixel
            )
        else:
            return NotImplemented

    def __rsub__(self, other: Union[numbers.Real, Tuple[numbers.Real, numbers.Real]]) -> "Curve":
        if isinstance(other, numbers.Real):
            return self.fromVectors(
                q=self.q,
                intensity=other - self.intensity,
                uncertainty=self.uncertainty,
                quncertainty=self.quncertainty,
                binarea=self.binarea,
                pixel=self.pixel
            )
        elif isinstance(other, tuple) and (len(other) == 2) and isinstance(other[0], numbers.Real) and isinstance(
                other[1], numbers.Real):
            return self.fromVectors(
                q=self.q,
                intensity=other[0] - self.intensity,
                uncertainty=(self.uncertainty ** 2 + other[1] ** 2) ** 0.5,
                quncertainty=self.quncertainty,
                binarea=self.binarea,
                pixel=self.pixel
            )
        else:
            return NotImplemented

    def __radd__(self, other: Union[numbers.Real, Tuple[numbers.Real, numbers.Real]]) -> "Curve":
        if isinstance(other, numbers.Real):
            return self.fromVectors(
                q=self.q,
                intensity=self.intensity + other,
                uncertainty=self.uncertainty,
                quncertainty=self.quncertainty,
                binarea=self.binarea,
                pixel=self.pixel
            )
        elif isinstance(other, tuple) and (len(other) == 2) and isinstance(other[0], numbers.Real) and isinstance(
                other[1], numbers.Real):
            return self.fromVectors(
                q=self.q,
                intensity=self.intensity + other[0],
                uncertainty=(self.uncertainty ** 2 + other[1] ** 2) ** 0.5,
                quncertainty=self.quncertainty,
                binarea=self.binarea,
                pixel=self.pixel
            )
        else:
            return NotImplemented

    def __truediv__(self, other: Union["Curve", numbers.Real, Tuple[numbers.Real, numbers.Real]]) -> "Curve":
        if isinstance(other, Curve):
            self._checkcompatibility(other)
            return self.fromVectors(
                q=0.5 * (self.q + other.q),
                intensity=self.intensity / other.intensity,
                uncertainty=(
                                    self.uncertainty ** 2 / other.intensity ** 2 + other.uncertainty ** 2 * self.intensity ** 2 / other.intensity ** 4) ** 0.5,
                quncertainty=(self.quncertainty ** 2 + other.quncertainty ** 2) ** 0.5 / 2.0,
                binarea=None,
                pixel=None
            )
        elif isinstance(other, numbers.Real):
            return self.fromVectors(
                q=self.q,
                intensity=self.intensity / other,
                uncertainty=np.abs(self.uncertainty / other),
                quncertainty=self.quncertainty,
                binarea=self.binarea,
                pixel=self.pixel
            )
        elif isinstance(other, tuple) and (len(other) == 2) and isinstance(other[0], numbers.Real) and isinstance(
                other[1], numbers.Real):
            return self.fromVectors(
                q=self.q,
                intensity=self.intensity / other[0],
                uncertainty=(self.uncertainty ** 2 / other[0] ** 2 + other[1] ** 2 * self.intensity ** 2 / other[
                    0] ** 4) ** 0.5,
                quncertainty=self.quncertainty,
                binarea=self.binarea,
                pixel=self.pixel
            )
        else:
            return NotImplemented

    def __mul__(self, other: Union["Curve", numbers.Real, Tuple[numbers.Real, numbers.Real]]) -> "Curve":
        if isinstance(other, Curve):
            self._checkcompatibility(other)
            return self.fromVectors(
                q=0.5 * (self.q + other.q),
                intensity=self.intensity * other.intensity,
                uncertainty=(
                                    self.uncertainty ** 2 * other.intensity ** 2 + other.uncertainty ** 2 * self.intensity ** 2) ** 0.5,
                quncertainty=(self.quncertainty ** 2 + other.quncertainty ** 2) ** 0.5 / 2.0,
                binarea=None,
                pixel=None
            )
        elif isinstance(other, numbers.Real):
            return self.fromVectors(
                q=self.q,
                intensity=self.intensity * other,
                uncertainty=np.abs(self.uncertainty * other),
                quncertainty=self.quncertainty,
                binarea=self.binarea,
                pixel=self.pixel
            )
        elif isinstance(other, tuple) and (len(other) == 2) and isinstance(other[0], numbers.Real) and isinstance(
                other[1], numbers.Real):
            return self.fromVectors(
                q=self.q,
                intensity=self.intensity * other[0],
                uncertainty=(self.uncertainty ** 2 * other[0] ** 2 + self.intensity ** 2 * other[1] ** 2) ** 0.5,
                quncertainty=self.quncertainty,
                binarea=self.binarea,
                pixel=self.pixel
            )
        else:
            return NotImplemented

    def __rtruediv__(self, other: Union[numbers.Real, Tuple[numbers.Real, numbers.Real]]) -> "Curve":
        if isinstance(other, numbers.Real):
            return self.fromVectors(
                q=self.q,
                intensity=other / self.intensity,
                uncertainty=np.abs(other * self.uncertainty / self.intensity ** 2),
                quncertainty=self.quncertainty,
                binarea=self.binarea,
                pixel=self.pixel
            )
        elif isinstance(other, tuple) and (len(other) == 2) and isinstance(other[0], numbers.Real) and isinstance(
                other[1], numbers.Real):
            return self.fromVectors(
                q=self.q,
                intensity=other[0] / self.intensity,
                uncertainty=(self.uncertainty ** 2 / self.intensity ** 4 * other[0] ** 2 + other[
                    1] ** 2 / self.intensity ** 2) ** 0.5,
                quncertainty=self.quncertainty,
                binarea=self.binarea,
                pixel=self.pixel
            )
        else:
            return NotImplemented

    def __rmul__(self, other: Union[numbers.Real, Tuple[numbers.Real, numbers.Real]]) -> "Curve":
        if isinstance(other, numbers.Real):
            return self.fromVectors(
                q=self.q,
                intensity=self.intensity * other,
                uncertainty=np.abs(self.uncertainty * other),
                quncertainty=self.quncertainty,
                binarea=self.binarea,
                pixel=self.pixel
            )
        elif isinstance(other, tuple) and (len(other) == 2) and isinstance(other[0], numbers.Real) and isinstance(
                other[1], numbers.Real):
            return self.fromVectors(
                q=self.q,
                intensity=self.intensity * other[0],
                uncertainty=(self.uncertainty ** 2 * other[0] ** 2 + self.intensity ** 2 * other[1] ** 2) ** 0.5,
                quncertainty=self.quncertainty,
                binarea=self.binarea,
                pixel=self.pixel
            )
        else:
            return NotImplemented


#!/usr/bin/env python3
# Plot the results of a benchmark or measurement file

import os
import argparse
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

sns.set_style("whitegrid")
sns.set_context("notebook")

FIXTURES = os.path.dirname(__file__)
FIGURES = os.path.join(FIXTURES, "figures")
OPS = os.path.join(FIXTURES, "data", "ops.csv")
BLAST = os.path.join(FIXTURES, "data", "blast.csv")

def plot_ops(path=None, outpath=None):
    path = path or OPS
    outpath = outpath or os.path.join(FIGURES, "benchmark_operations.png")
    _, ax = plt.subplots(figsize=(9,6))

    df = pd.read_csv(path)
    sns.barplot(x='op', y='benchmark', hue='store', ax=ax, data=df)

    ax.set_xlabel("operation")
    ax.set_ylabel("ns/op")
    ax.set_title("Sequential Access Go Benchmark")

    plt.savefig(outpath)

def plot_blast(path=None, outpath=None):
    path = path or BLAST
    outpath = outpath or os.path.join(FIGURES, "benchmark_blast_throughput.png")
    _, ax = plt.subplots(figsize=(9,6))

    df = pd.read_csv(path)
    sns.barplot(x='op', y='throughput', hue='store', ax=ax, data=df)

    ax.set_xlabel('operation')
    ax.set_ylabel('throughput (ops/sec)')
    ax.set_title("Blast of 5000 Concurrent Accesses: Throughput")

    plt.savefig(outpath)

if __name__ == '__main__':
    parser = argparse.ArgumentParser()

    parser.add_argument(
        '-v', '--viz', choices=('ops', 'blast'), default='ops',
        help='specify the plot to draw from test data',
    )
    parser.add_argument(
        'data', nargs="?", default=None, help="path to CSV data"
    )
    parser.add_argument(
        "-o", "--outpath", default=None, help="path to store the file"
    )

    args = parser.parse_args()
    if args.viz == 'ops':
        plot_ops(args.data, args.outpath)
    elif args.viz == 'blast':
        plot_blast(args.data, args.outpath)


import sys
from PyQt5.Qt import *
from PyQt5 import QtWidgets, QtCore, QtGui
import numpy as np
from PyQt5.QtWidgets import QComboBox

import cv2
import picture_chuli

class My_PS(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('图像处理')
        self.resize(1500, 900)

        wid = 1400
        hgt = 720

        #按钮背景
        self.back = QLabel(self)
        self.back.setFixedSize(1500, 0.1 * 900)
        self.back.setStyleSheet("QLabel{background:red;}")

        #图片区
        self.label_pic = QLabel(self)
        self.label_pic.setFixedSize(wid,hgt)
        self.label_pic.move(50, 100)
        self.label_pic.setFrameShape(QtWidgets.QFrame.Box)
        self.label_pic.setLineWidth(1)
        self.label_pic.setStyleSheet("QLabel{background:pink;}")

        #原图展示区
        self.label_daichuli = QtWidgets.QLabel(self.label_pic)
        self.label_daichuli.setGeometry(QtCore.QRect(wid/2-500,100,400,500))
        self.label_daichuli.setObjectName("label_daichuli")
        self.label_daichuli.setFrameShape(QtWidgets.QFrame.Box)
        self.label_daichuli.setLineWidth(1)
        self.label_daichuli.setStyleSheet("QLabel{background:white;}")

        #效果展示区
        self.label_result = QtWidgets.QLabel(self.label_pic)
        self.label_result.setGeometry(QtCore.QRect(wid/2+100, 100, 400, 500))
        self.label_result.setObjectName("label_daichuli")
        self.label_result.setFrameShape(QtWidgets.QFrame.Box)
        self.label_result.setLineWidth(1)
        self.label_result.setStyleSheet("QLabel{background:white;}")

        #按钮全局布局

        hlayout1 = QtWidgets.QVBoxLayout()
        hlayout1.setContentsMargins(50,10,1100,10)

        #打开图片按钮
        self.pushbutton_openimg = QtWidgets.QPushButton()
        self.pushbutton_openimg.setText("打开图片")
        self.pushbutton_openimg.clicked.connect(self.openimage)

        #保存图片按钮
        self.pushbtton_save = QtWidgets.QPushButton()
        self.pushbtton_save.setText("保存图片")
        self.pushbtton_save.clicked.connect(self.saveimage)

        #旋转按钮
        a = 500
        self.zuoxuan = QtWidgets.QPushButton(self.back)
        self.zuoxuan.setText("顺时针旋转")
        self.zuoxuan.clicked.connect(self.shunshizhen)
        self.zuoxuan.move(a,10)
        self.youxuan = QtWidgets.QPushButton(self.back)
        self.youxuan.setText("逆时针旋转")
        self.youxuan.clicked.connect(self.nishizhen)
        self.youxuan.move(a+100,10)
        self.jingxiang = QtWidgets.QPushButton(self.back)
        self.jingxiang.setText("镜像")
        self.jingxiang.clicked.connect(self.jingxiang1)
        self.jingxiang.move(a,50)
        self.shangxia = QtWidgets.QPushButton(self.back)
        self.shangxia.setText("上下颠倒")
        self.shangxia.clicked.connect(self.shangxia1)
        self.shangxia.move(a+100,50)

        # 加噪按钮
        self.lineEdit_n_value = QtWidgets.QLineEdit(self.back)
        self.lineEdit_n_value.setFixedSize(130,30)
        self.lineEdit_n_value.move(a+300,10)
        self.lineEdit_n_value.setText("1000")
        self.addnoise = QtWidgets.QPushButton(self.back)
        self.addnoise.move(a+450,10)
        self.addnoise.setText("添加椒盐噪音")
        self.addnoise.clicked.connect(self.addnoise1)
        self.lineEdit_mean_value = QtWidgets.QLineEdit(self.back)
        self.lineEdit_mean_value.move(a+300, 50)
        self.lineEdit_mean_value.setText("0")
        self.lineEdit_mean_value.setFixedSize(60,30)
        self.lineEdit_sigma_value = QtWidgets.QLineEdit(self.back)
        self.lineEdit_sigma_value.setText("25")
        self.lineEdit_sigma_value.move(a + 365, 50)
        self.lineEdit_sigma_value.setFixedSize(60, 30)
        self.addnoisy = QtWidgets.QPushButton(self.back)
        self.addnoisy.move(a+450,50)
        self.addnoisy.setText("添加高斯噪音")
        self.addnoisy.clicked.connect(self.addnoisy1)

        hlayout1.addWidget(self.pushbutton_openimg)
        hlayout1.addWidget(self.pushbtton_save)
        self.back.setLayout(hlayout1)

        # 实例化QComBox对象
        self.cb = QComboBox(self.back)

        self.cb.move(1150, 25)
        items = ["灰度", "二值化", "拉普拉斯","伽马变化","轮廓","sift检测","浮雕效果","水彩效果",
                 "中值滤波","高斯滤波","均值滤波","图像修复"]
        self.cb.addItems(items)

        # # 信号
        self.cb.currentIndexChanged[int].connect(self.print_value) # 条目发生改变，发射信号，传递条目内容
        # self.cb.highlighted[str].connect(self.print_value)  # 在下拉列表中，鼠标移动到某个条目时发出信号，传递条目内容

    def openimage(self):  # 选择本地图片上传
        # 弹出一个文件选择框，第一个返回值imgName记录选中的文件路径+文件名，第二个返回值imgType记录文件的类型
        imgName, imgType = QFileDialog.getOpenFileName(self, "打开图片", "","*.jpg;;*.png;;All Files(*)")
        jpg = QtGui.QPixmap(imgName).scaled(self.label_daichuli.width(),
                                            self.label_daichuli.height())  # 通过文件路径获取图片文件，并设置图片长宽为label控件大小j
        self.label_daichuli.setPixmap(jpg)  # 在label控件上显示选择的图片

    def saveimage(self):  # 保存图片到本地
        screen = QApplication.primaryScreen()
        pix = screen.grabWindow(self.label_result.winId())
        fd, type = QFileDialog.getSaveFileName(self, "保存图片", "", "*.jpg;;*.png;;All Files(*)")
        pix.save(fd)

    # a=["huidutu","erzhihua"]

    def print_value(self, i):
        self.caozuo(i)

    def caozuo(self,i):
        qimg = self.label_daichuli.pixmap()
        src = qtpixmap_to_opencv(qimg)
        if i == 0: ##亦可用switch方法
            newsrc = picture_chuli.gray_picture(src)
        elif i == 1:
            newsrc = picture_chuli.erzhihua(src)
        elif i == 2:
            newsrc = picture_chuli.laplas(src)
        elif i == 3:
            newsrc = picture_chuli.gama(src)
        elif i == 4:
            newsrc = picture_chuli.morphologyExfun(src)
        elif i == 5:
            newsrc = picture_chuli.sift_fun(src)
        elif i == 6:
            newsrc = picture_chuli.fudiao_fun(src)
        elif i == 7:
            newsrc = picture_chuli.shuicai_fun(src)
        elif i == 8:
            newsrc = picture_chuli.medianBlurfun(src)
        elif i == 9:
            newsrc = picture_chuli.GaussianBlurfun(src)
        elif i == 10:
            newsrc = picture_chuli.blurfun(src)
        elif i == 11:
            newsrc = picture_chuli.xiufu(src)

        pix = opencv_to_qtpixmap(newsrc)
        self.label_result.setPixmap(pix)

    def addnoise1(self):
        qimg = self.label_daichuli.pixmap()
        src = qtpixmap_to_opencv(qimg)
        num = self.lineEdit_n_value.text()
        num = int(num)
        newsrc = picture_chuli.jynoisy(src, num)
        pix = opencv_to_qtpixmap(newsrc)
        self.label_result.setPixmap(pix)

    def addnoisy1(self):
        qimg = self.label_daichuli.pixmap()
        src = qtpixmap_to_opencv(qimg)
        mean = self.lineEdit_mean_value.text()
        mean = int(mean)
        sigma = self.lineEdit_sigma_value.text()
        sigma = int(sigma)
        newsrc = picture_chuli.gsnoise(src, mean, sigma)
        pix = opencv_to_qtpixmap(newsrc)
        self.label_result.setPixmap(pix)

    def shangxia1(self):
        qimg = self.label_daichuli.pixmap()
        src = qtpixmap_to_opencv(qimg)
        newsrc = picture_chuli.sxfun(src)
        pix = opencv_to_qtpixmap(newsrc)
        self.label_result.setPixmap(pix)

    def jingxiang1(self):
        qimg = self.label_daichuli.pixmap()
        src = qtpixmap_to_opencv(qimg)
        newsrc = picture_chuli.flipfun(src)
        pix = opencv_to_qtpixmap(newsrc)
        self.label_result.setPixmap(pix)

    def shunshizhen(self):
        qimg = self.label_daichuli.pixmap()
        src = qtpixmap_to_opencv(qimg)
        newsrc = picture_chuli.RotateClockWise90(src)
        pix = opencv_to_qtpixmap(newsrc)
        self.label_result.setPixmap(pix)

    def nishizhen(self):
        qimg = self.label_daichuli.pixmap()
        src = qtpixmap_to_opencv(qimg)
        newsrc = picture_chuli.RotateAntiClockWise90(src)
        pix = opencv_to_qtpixmap(newsrc)
        self.label_result.setPixmap(pix)

def qtpixmap_to_opencv(qtpixmap):    #qtpixmap转opencv
    qimg = qtpixmap.toImage()
    temp_shape = (qimg.height(), qimg.bytesPerLine() * 8 // qimg.depth())
    temp_shape += (4,)
    ptr = qimg.bits()
    ptr.setsize(qimg.byteCount())
    result = np.array(ptr, dtype=np.uint8).reshape(temp_shape)
    result = result[..., :3]
    return result

def opencv_to_qtpixmap(cvimg):
    if cvimg.ndim==2:              #单通道
        height, width= cvimg.shape
        cvimg = cv2.cvtColor(cvimg, cv2.COLOR_BGR2RGB)
        cvimg = QImage(cvimg.data, width, height, QImage.Format_RGB888)
        pix = QPixmap.fromImage(cvimg)
        return pix
    else:                          #多个通道
        width = cvimg.shape[1]
        height = cvimg.shape[0]
        pixmap = QPixmap(width, height)  # 根据已知的高度和宽度新建一个空的QPixmap,
        qimg = pixmap.toImage()         # 将pximap转换为QImage类型的qimg
        for row in range(0, height):
            for col in range(0, width):
                b = cvimg[row, col, 0]
                g = cvimg[row, col, 1]
                r = cvimg[row, col, 2]
                pix = qRgb(r, g, b)
                qimg.setPixel(col, row, pix)
                pix = QPixmap.fromImage(qimg)
        return pix

if __name__ == '__main__':
    app = QApplication(sys.argv)
    Ps = My_PS()
    Ps.show()
    sys.exit(app.exec_())


from flask import current_app
import time
from flask import session
from flask_caching import Cache
import pandas as pd

from application.utils.logger import configure_logger

logger = configure_logger()

from application import create_app

from .cache import cache

def update_csv_file(df_name: str, df: pd.DataFrame, session=session) -> None:
    """Store the DataFrame in cache and raise an error if not stored."""

    dataframe_name = f"{df_name}_{session['session_id']}"
    cache.set(dataframe_name, df)
    # session['timestamp']= time.time()  # Update the timestamp
    cache.set('timestamp',(time.time()))

    # Check if the DataFrame is stored in the cache
    if cache.get(dataframe_name) is None:
        raise ValueError("DataFrame not stored in cache. Failed to update.")

    # Add dataframe_name to dataframes_saved_in_cache list in the session if not already present
    if 'dataframes_saved_in_cache' not in session:
        session['dataframes_saved_in_cache'] = []

    if dataframe_name not in session['dataframes_saved_in_cache']:
        session['dataframes_saved_in_cache'].append(dataframe_name)

def getting_csv(df_name: str,session=session) -> pd.DataFrame:
    """Retrieve the DataFrame from cache and return it."""

    dataframe_name = f"{df_name}_{session['session_id']}"
    retrieved_df = cache.get(dataframe_name)

    if retrieved_df is None:
        raise ValueError(f"DataFrame '{dataframe_name}' not found in cache.")

    return retrieved_df

# View cached keys and values
def view_cache():
    cached_keys = cache.cache._cache.keys()  # Get all cached keys
    return cached_keys

def clear_inactive_cache():
    while True:
        time.sleep(100000)  # Sleep for 5 minutes
        current_time = time.time()
        timestamp = cache.get('timestamp')
        print('-----------------------------------------------------',timestamp)
        if timestamp is not None and current_time - timestamp > 100000:
            logger.info('files stored in cache before clearing the cache data {}'.format(view_cache()))
            cache.clear()
            # session.clear()
            logger.info('files stoed in cache after clearing the cache data {}'.format(view_cache()))
            print('-----------------------------------------------------',timestamp)

def clear_inactive_cache_immediate():
    logger.info('Currently running the clear_inactive_cache_immediate and clearing the cache and session')
    cache.clear()
    session.clear()


"""
Call different aspects of tRNA-MAP program

Improvements:
- custom terminal output
- static summary
- different classes of mods, different colors
- Change heat map to one color because it is hard to see around 50%

Tests:
- run bacterial mod predictions on saccharomyces as a negative control/don't expect to do well.
- static summary
- different classes of mods, different colors

"""

def parseInput():
    """Parse command line input and output files"""
    import argparse

    #Argparse setup
    argParser = argparse.ArgumentParser(description = 'Predict tRNA Modifications')
    argParser.add_argument('-s', '--tRNAscan_ss', required = True, help = 'tRNAscan-SE secondary structure output')
    argParser.add_argument('-p', '--protein_sequences', required = True, help = 'protein fasta')
    argParser.add_argument('-o', '--output_directory', required = True, help = 'Path to output results')
    argParser.add_argument('-k', '--kingdom', required = True, choices = {'E', 'A', 'B'}, help = 'Domain of organism')
    argParser.add_argument('--cm_database', required = False, default = './CMlibrary.txt', help = 'File containing information about tRNA mod CMs to search')
    argParser.add_argument('--cp_database', required = False, default = './probLibrary.txt', help = 'File containing information about conditional probabilities')
    argParser.add_argument('--skip_sprinzl_align', required = False, action = 'store_true', help = 'Skips sprinzl alignment step to save time if this has already been done.')
    argParser.add_argument('--e_cutoff', required = False, default = 0.0001, help = 'Cutoff e value for protein searches, default is 0.0001')

    kingdoms = {'E': 'Eukaryota', 'A': 'Archaea', 'B': 'Bacteria'}
    #'-s ../SlicerV2/data/secStruct/strePneu_TIGR4-tRNAs.ss.sort -o ./strepneumo_test/ -k B -p ./strepneumo_test/GCF_000006885.1_ASM688v1_protein.faa --skip_sprinzl_align'.split()
    clArgs = argParser.parse_args()
    tRNAstruct = clArgs.tRNAscan_ss
    protSeqs = clArgs.protein_sequences
    orgKing = kingdoms[clArgs.kingdom]
    outDir = clArgs.output_directory
    cmFile = clArgs.cm_database
    probFile = clArgs.cp_database
    skipSprinzl = clArgs.skip_sprinzl_align
    eCutoff = float(clArgs.e_cutoff)

    return tRNAstruct, protSeqs, orgKing, outDir, cmFile, probFile, skipSprinzl, eCutoff

def main():
    """"""
    import mapSeqs
    import HMMprogramV1_2
    import predVis

    secStruct, protSeqs, orgKing, outDir, cmFile, probFile, skipSprinzl, eCutoff = parseInput()

    #format outDir convention
    if outDir[-1] == '/':
        outDir = outDir[:-1]

    #Predict tRNA sequences
    seqFile, scoreFile = mapSeqs.main(inCL = False, ssFile = secStruct, domain = orgKing,
                                      outputFile = outDir, cmsToSearch = cmFile,
                                      condProbs = probFile, skipSpr = skipSprinzl)

    #Search protein sequences
    protFile = '{0}/domainHits.txt'.format(outDir)
    domainHits = HMMprogramV1_2.main(inCL = False, refKingdomType = orgKing,
                                     refProteome = protSeqs, outFile = protFile, eValue = eCutoff)

    predVis.main(inCL = False, calls = seqFile,
                 scores = scoreFile, proteins = protFile,
                 outFile = outDir)

main()


import cv2
# import ffmpeg
import imageio
import os
import numpy as np

# (
#     ffmpeg
#         .input('./data/AICity_data/train/S03/c010/vdo.avi')
#         .filter('fps', fps=1)
#         .output('test/%d.png',
#                 start_number=0)
#         .run(capture_stdout=True, capture_stderr=True)
#  )

colors = {
    'r': (0, 0, 255),
    'g': (0, 255, 0),
    'b': (255, 0, 0),
    'w': (255,255,255)
}
color_ids = {}

def draw_boxes(image, boxes, tracker=None, color='g', linewidth=2, det=False, boxIds=False, old=False):
    rgb = colors[color]
    for box in boxes:
        # print(box.id)
        if boxIds:
            if box.id in list(color_ids.keys()):
                pass
            else:
                color_ids[box.id]=np.random.uniform(0,256,size=3)
            if old:
                cv2.putText(image, str(box.id), (int(box.xtl), int(box.ytl) + 120), cv2.FONT_ITALIC, 0.6, color_ids[box.id], linewidth)
            else:
                cv2.putText(image, str(box.id), (int(box.xtl), int(box.ytl) + 20), cv2.FONT_ITALIC, 0.6, color_ids[box.id], linewidth)

            if tracker is not None:
                if box.id in tracker:
                    if len(tracker[box.id])>2:
                        image =cv2.polylines(image,[np.array(tracker[box.id])],False,color_ids[box.id],linewidth)

            # if len(kalman_predictions[box.id])>2:
            #     image =cv2.polylines(image,[np.array(kalman_predictions[box.id])],False,color_ids[box.id],linewidth)

            image = cv2.rectangle(image, (int(box.xtl), int(box.ytl)), (int(box.xbr), int(box.ybr)), color_ids[box.id], linewidth)
        else:
            image = cv2.rectangle(image, (int(box.xtl), int(box.ytl)), (int(box.xbr), int(box.ybr)), rgb, linewidth)

        if det:
            cv2.putText(image, str(box.confidence), (int(box.xtl), int(box.ytl) - 5), cv2.FONT_ITALIC, 0.6, rgb, linewidth)

    return image

def draw_boxes_old(image, boxes, tracker,  color='g', linewidth=2, det=False, boxIds=False, old=False, shifted=False):
    rgb = colors[color]
    for box in boxes:
        if not shifted:
            image = cv2.rectangle(image, (int(box.xtl), int(box.ytl)), (int(box.xbr), int(box.ybr)), colors['r'], linewidth)
        else:
            image = cv2.rectangle(image, (int(box.box_flow[0]), int(box.box_flow[1])), (int(box.box_flow[2]), int(box.box_flow[3])), colors['b'], linewidth)
    return image

def save_gif(source_path, results_path, fps=10):
    # Build GIF

    with imageio.get_writer(results_path, mode='I', fps=fps) as writer:
        for filename in sorted(os.listdir(source_path)):
            if filename[0] != ".":
                # print(source_path + filename)
                image = imageio.imread(source_path + filename)
                writer.append_data(image)

def plot_3D(results_path):
    maps=[]
    for i in x:
        map_alfa = []
        for j in y:
            z = uniform(0.0,1)
            map_alfa.append(z)
        maps.append(map_alfa)
    X, Y = np.meshgrid(x, y)
    fig = plt.figure()
    ax = fig.gca(projection='3d')
    surf = ax.plot_surface(X, Y, np.array(maps),cmap='viridis', edgecolor='none')
    ax.set_xlabel('Alpha')
    ax.set_ylabel('Rho')
    ax.set_zlabel('mAP')
    # Add a color bar which maps values to colors.
    ax.view_init(45, 35)
    fig.colorbar(surf, shrink=0.5, aspect=5)
    plt.savefig(results_path+"grid_search.png")
    plt.show()


import logging
import pandas as pd
import numpy as np

import config

from sentence_transformers import SentenceTransformer
import gensim.downloader as api

logger = logging.getLogger(__name__)

class SentenceEmbeddings:
    def __init__(self, name: str) -> None:
        self.model_name = name

        logger.info(f'Initializing {self.model_name.upper()} for Sentence Embeddings')

        if 'mpnet' in self.model_name:
            logger.debug('Loading all-mpnet-base-v2 model')
            self.model = SentenceTransformer('all-mpnet-base-v2')
        elif 'distil-roberta' in self.model_name:
            logger.debug('Loading all-distilroberta-v1')
            self.model = SentenceTransformer('all-distilroberta-v1')

    def generate_embeddings(self, input_texts: pd.Series):
        logger.info(f'{self.model_name.upper()} - Generating sentence embeddings...')
        return self.model.encode(input_texts.to_list())

class WordEmbeddings:
    def __init__(self, name: str) -> None:
        self.model_name = name

        logger.info(f'Initializing {self.model_name.upper()} for Word Embeddings')

        if 'w2v-google' in self.model_name:
            logger.debug('Loading word2vec-google-news-300')
            self.model = api.load('word2vec-google-news-300')
        elif 'glove-twitter' in self.model_name:
            logger.debug('Loading glove-twitter-200')
            self.model = api.load('glove-twitter-200')
        elif 'glove-wiki' in self.model_name:
            logger.debug('Loading glove-wiki-gigaword-300')
            self.model = api.load('glove-wiki-gigaword-300')

    def generate_embeddings(self, input_texts: pd.Series) -> list:
        logger.info(f'{self.model_name.upper()} - Generating sentence embeddings...')
        # init embeddings list
        embeddings = []
        for tokens in input_texts:
            # init zero vector and vector list
            zero_vector = np.zeros(self.model.vector_size)
            vectors = []
            for token in tokens:
                if token in self.model:
                    try:
                        logger.debug(f'Checking {token} in vocabulary')
                        vectors.append(self.model[token])
                    except KeyError:
                        logger.warning(f'{token} not in vocabulary, skipping...')
                        continue
            if vectors:
                vectors = np.asarray(vectors)
                # compute average across 0 axis
                avg_vec = vectors.mean(axis=0)
                embeddings.append(avg_vec)
            else:
                embeddings.append(zero_vector)

        return embeddings


import streamlit as st
import seaborn as sns
import numpy as np
import math

#inserindo título do trabalho
st.markdown("# **Distribuições Probabilidade**")

#inserindo descrição trablaho
st.markdown("Escolha ao lado esquerdo e utilize os parâmetros das distribuições.")

#inserindo linha
st.markdown("---")

#incluindo título na barra lateral
st.sidebar.markdown("# **Menu Distribuição**")

#lista com aas distribuições disponíveis
distribuicao = ["1 - Gauss/Normal", "2 - Binomial", "3 - Binomial Negativa"
               ,"4 - Qui-quadrado", "5 - Gamma", "6 - Exponencial"
               ,"7 - Geométrica", "8 - Beta", "9 - T de Student"
               ,"10 - Poisson", "11 - Log-normal", "12 - Gumbel"
               , "13 - Weibull"]

#select box com as op
selecao_distribuicao = st.sidebar.selectbox("Selecione a distribuição",distribuicao)

#inserindo linha
st.sidebar.markdown("---")

#se distribuição gauss/normal
if selecao_distribuicao[0:2] == "1 ":

    #inserindo texto
    st.sidebar.markdown("Parâmetros Gauss/Normal")

    #tamanho amostra
    tamanho_amostra = st.sidebar.slider("Tamanho da amostra", min_value= 30, max_value= 10000, step= 10)

    #valor média
    media = st.sidebar.slider("Média", min_value= -10, max_value= 10, step= 1, value= 0)

    #valor desvio padrão
    desvio = st.sidebar.slider("Desvio Padrão", min_value= 1, max_value= 10, step= 1, value= 1)

    #distribuição
    data = np.random.normal(loc= media, scale= desvio, size= tamanho_amostra)

#se distribuição binomial
elif selecao_distribuicao[0:2] == "2 ":

    #inserindo texto
    st.sidebar.markdown("Parâmetros Binomial")

    #tamanho amostra
    tamanho_amostra = st.sidebar.slider("Tamanho da amostra", min_value= 30, max_value= 10000, step= 10)

    #probabilidade
    probabilidade = st.sidebar.slider("Probabilidade", min_value= 0.0, max_value= 1.0, step= 0.1, value= 0.5)

    #quantidade testes
    teste = st.sidebar.slider("Teste", min_value= 1000, max_value= 10000, step= 100, value= 5000)

    #distribuicao
    data = np.random.binomial(n= tamanho_amostra, p= probabilidade, size= teste)

#se distribuição binomial negativa
elif selecao_distribuicao[0:2] == "3 ":

    #inserindo texto
    st.sidebar.markdown("Parâmetros Binomial Negativa")

    #tamanho amostra
    tamanho_amostra = st.sidebar.slider("Tamanho da amostra", min_value= 30, max_value= 10000, step= 10)

    #probabilidade
    probabilidade = st.sidebar.slider("Probabilidade", min_value= 0.0, max_value= 1.0, step= 0.1, value= 0.5)

    #quantidade testes
    teste = st.sidebar.slider("Teste", min_value=1000, max_value=10000, step=100, value=5000)

    #distribuicao
    data = np.random.negative_binomial(n= tamanho_amostra, p= probabilidade, size= teste)

#se distribuição qui-quadrado
elif selecao_distribuicao[0:2] == "4 ":

    #inserindo texto
    st.sidebar.markdown("Parâmetros Qui-quadrado")

    #tamanho amostra
    tamanho_amostra = st.sidebar.slider("Tamanho da amostra", min_value= 30, max_value= 10000, step= 10)

    #graus liberdade
    graus = st.sidebar.slider("Graus Liberdade", min_value= 1, max_value= 100, step= 1, value= 2)

    #distribuicao
    data = np.random.chisquare(df= graus, size= tamanho_amostra)

#se distribuição Gamma
elif selecao_distribuicao[0:2] == "5 ":

    #inserindo texto
    st.sidebar.markdown("Parâmetros Gamma")

    #tamanho amostra
    tamanho_amostra = st.sidebar.slider("Tamanho da amostra", min_value= 30, max_value= 10000, step= 10)

    #forma
    forma = st.sidebar.slider("Forma", min_value= 1, max_value= 10, step= 1, value= 2)

    #escala
    escala = st.sidebar.slider("Escala", min_value= 1, max_value= 10, step= 1, value= 2)

    #distribuicao
    data = np.random.gamma(shape= forma, scale= escala, size= tamanho_amostra)

#se distribuição Exponencial
elif selecao_distribuicao[0:2] == "6 ":

    #inserindo texto
    st.sidebar.markdown("Parâmetros Exponencial")

    #tamanho amostra
    tamanho_amostra = st.sidebar.slider("Tamanho da amostra", min_value= 30, max_value= 10000, step= 10)

    #escala
    escala = st.sidebar.slider("Escala", min_value= 1, max_value= 10, step= 1, value= 2)

    #distribuicao
    data = np.random.exponential(scale= escala, size= tamanho_amostra)

#se distribuição Geometrica
elif selecao_distribuicao[0:2] == "7 ":

    #inserindo texto
    st.sidebar.markdown("Parâmetros Geométrica")

    #tamanho amostra
    tamanho_amostra = st.sidebar.slider("Tamanho da amostra", min_value= 30, max_value= 10000, step= 10)

    #probabilidade
    probabilidade = st.sidebar.slider("Probabilidade", min_value= 0.1, max_value= 1.0, step= 0.1, value= 0.1)

    #distribuicao
    data = np.random.geometric(p= probabilidade, size= tamanho_amostra)

#se distribuição Hipergeométrica
elif selecao_distribuicao[0:2] == "8 ":

    #inserindo texto
    st.sidebar.markdown("Parâmetros Beta")

    #tamanho amostra
    tamanho_amostra = st.sidebar.slider("Tamanho da amostra", min_value= 30, max_value= 10000, step= 10)

    #alfa
    alfa = st.sidebar.slider("Alfa", min_value= 1, max_value= 100, step= 1, value= 50)

    #beta
    beta = st.sidebar.slider("Beta", min_value= 1, max_value= 100, step= 1, value= 50)

    #distribuicao
    data = np.random.beta(a= alfa, b= beta, size= tamanho_amostra)

#se distribuição T de Student
elif selecao_distribuicao[0:2] == "9 ":

    #inserindo texto
    st.sidebar.markdown("Parâmetros T de Student")

    #tamanho amostra
    tamanho_amostra = st.sidebar.slider("Tamanho da amostra", min_value= 30, max_value= 10000, step= 10)

    #graus liberdade
    graus = st.sidebar.slider("Graus Liberdade", min_value= 1, max_value= 100, step= 1, value= 10)

    #distribuicao
    data = np.random.standard_t(df= graus, size= tamanho_amostra)

#se distribuição Poisson
elif selecao_distribuicao[0:2] == "10":

    #inserindo texto
    st.sidebar.markdown("Parâmetros Poisson")

    #tamanho amostra
    tamanho_amostra = st.sidebar.slider("Tamanho da amostra", min_value= 30, max_value= 10000, step= 10)

    #esperado
    esperado = st.sidebar.slider("Eventos esperados", min_value= 1, max_value= 1000, step= 10, value= 100)

    #distribuicao
    data = np.random.poisson(lam= esperado, size= tamanho_amostra)

#se distribuição log-normal
if selecao_distribuicao[0:2] == "11":

    #inserindo texto
    st.sidebar.markdown("Parâmetros Log-normal")

    #tamanho amostra
    tamanho_amostra = st.sidebar.slider("Tamanho da amostra", min_value= 30, max_value= 10000, step= 10)

    #valor média
    media = st.sidebar.slider("Média", min_value= -10, max_value= 10, step= 1, value= 0)

    #valor desvio padrão
    desvio = st.sidebar.slider("Desvio Padrão", min_value= 1, max_value= 10, step= 1, value= 1)

    #distribuição
    data = np.random.lognormal(mean= media, sigma= desvio, size= tamanho_amostra)

#se distribuição gumbel
if selecao_distribuicao[0:2] == "12":

    #inserindo texto
    st.sidebar.markdown("Parâmetros Gumbel")

    #tamanho amostra
    tamanho_amostra = st.sidebar.slider("Tamanho da amostra", min_value= 30, max_value= 10000, step= 10)

    #valor modo de distirbuição
    media = st.sidebar.slider("Média", min_value= -10, max_value= 10, step= 1, value= 0)

    #valor escala
    beta = st.sidebar.slider("Beta", min_value= 1, max_value= 10, step= 1, value= 1)

    #distribuição
    data = np.random.gumbel(loc= media, scale= beta, size= tamanho_amostra)

#se distribuição Weibull
elif selecao_distribuicao[0:2] == "13":

    #inserindo texto
    st.sidebar.markdown("Parâmetros Weibull")

    #tamanho amostra
    tamanho_amostra = st.sidebar.slider("Tamanho da amostra", min_value= 30, max_value= 10000, step= 10)

    #forma
    forma = st.sidebar.slider("Forma", min_value= 1, max_value= 10, step= 1, value= 4)

    #distribuicao
    data = np.random.weibull(a= forma, size= tamanho_amostra)

#calculando tamanho bin
bin_size = int(3 + math.log2(tamanho_amostra) * math.log(tamanho_amostra))

#gráfico com a distribuição
grafico = sns.displot(data, bins= bin_size, kde= True)

#plot gráfico
st.pyplot(grafico)


import os
from conans import ConanFile
from conans.tools import download, unzip

channel = os.getenv("CONAN_CHANNEL", "channel")
username = os.getenv("CONAN_USERNAME", "username")

class TestPackage(ConanFile):
    name = "test-package"
    version = "0.1"
    settings = "os"

    def build(self):
        archive = "premake.zip"
        download("https://github.com/premake/premake-core/releases/download/v5.0.0-alpha9/premake-5.0.0-alpha9-src.zip", archive)
        unzip(archive)
        os.unlink(archive)

        if self.settings.os == "Windows":
            os.chdir("premake-5.0.0-alpha9/build/gmake.windows")
            self.run("nmake")
            os.chdir("../..")

        elif self.settings.os == "Macos":
            os.chdir("premake-5.0.0-alpha9/build/gmake.macosx")
            self.run("make")

        elif self.settings.os == "Linux":
            os.chdir("premake-5.0.0-alpha9/build/gmake.unix")
            self.run("make")

    def package(self):
        self.copy("*", dst="bin", src="premake-5.0.0-alpha9/bin/release")