# from sklearn.naive_bayes import MultinomialNB
# from sklearn.naive_bayes import GaussianNB
# from sklearn.cluster import KMeans
import pandas as pd
# from random import shuffle
import numpy as np
import os
# from sklearn.feature_extraction.text import CountVectorizer
# from sklearn.feature_extraction.text import TfidfTransformer
from nltk.corpus import stopwords
# from nltk.corpus import stopwords
# from nltk.tokenize import word_tokenize
# import nltk
import re
import xml.etree.ElementTree as ET
from xml.etree.ElementTree import XMLParser
from lxml import etree

def obtener_text(path = str(), file = str()):
    """
    Función que regresa el texto del archivo que se le pasa, preferentemente pasar la ruta relativa de donde se encuentra el archivo.
    Funciona con los archivos #.review.post del corpus del mismo directorio.
    Retorna el texto unicamente.
    path = ruta relativa o completa del archivo seleccionado.
    """

    with open(path + file, encoding='latin1', mode = 'r') as f:
        text = f.read()

    listas = text.split('\n')
    test = []

    for linea in listas:
        aux = linea.split(' ')
        try:
            test.append(aux[1])
        except:
            pass

    cad = ' '.join(test)
    return cad

def normalizar(text = str()):
    # nltk.download('stopwords')
    '''
    Funcion para normalizar el texto y eliminar stopwords, así como signos de puntuación, guiones bajos y demás caracteres que no sean texto, retorna la cadena limpia.
    text : texto para normalizar
    '''
    stop_words = set(stopwords.words('spanish'))
    lower_string = text.lower()

    no_number_string = re.sub(r'\d+','',lower_string)
    no_sub_ = re.sub('[\_]',' ', no_number_string)
    no_punc_string = re.sub(r'[^\w\s]','', no_sub_)
    no_wspace_string = no_punc_string.strip()
    # no_wspace_string

    lst_string = [no_wspace_string][0].split()
    # print(lst_string)
    no_stpwords_string=""
    for i in lst_string:
        if not i in stop_words:
            no_stpwords_string += i+' '

    no_stpwords_string = no_stpwords_string[:-1]

    return no_stpwords_string

def get_rank (path = str(), file = str(), llave = 'rank'):
    """
    En la función solo se tiene que pasar el path, más el
    archivo del cual se quiera obtener el rank, o mejor dicho
    la valoración que se obtuvo en la pelicula, el archivo a pasar tiene que
    ser en formato .xml para que la función funcione de forma correcta,
    retorna el valor entero que se puso en la pelicula.

    path : ruta donde se encuentran los archivos xml
    file : nombre del archivo el cual se va a obtener el valor
    llave : atributo que se quiere, valor por defecto rank
    """

    with open(path + file, mode = 'r', encoding= 'latin1') as f:
        parser = etree.XMLParser(recover=True)
        tree = ET.parse(f, parser=parser)
        root = tree.getroot()

        att = root.attrib

    return int(att[llave])

def obtener_y (path = str(), file_pos = list(), file_xml = list()):
    """
    Funcion hecha para obtener el mismo número de archivos xml y de review.pos, regresa el valor del archivo xml.
    Retorna una lista.
    path : Dirección donde se encuentra el corpus
    file_pos : lista con los nombres del archivo review.pos
    xml_file : lista con los nombres del archivo xml contenidas en el corpus
    """
    file_of_x = list()
    value_of_y = list()

    for file in file_pos:
        aux = file.split('.')
        num = aux[0]
        comp = str(num) + '.xml'
        if comp in file_xml:
            file_of_x.append(obtener_text(path,file))
            value_of_y.append(get_rank(path, comp))

    return file_of_x, value_of_y


import requests
import json

match = {
    "Date": "21-01-2023",
    "Form": "decent",
    "Opposition": "tough",
    "season": "middle",
    "venue": "home",
    "Previous match": "0",
    "uEFa": "active"
}

#url = 'http://localhost:9696/predict'
url = 'https://klopp-s-liverp-prod-klopp-s-liverpool-hql7qt.mo4.mogenius.io/predict'
response = requests.post(url, json=match)
result = response.json()
print(result)


from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.encoders import jsonable_encoder
from pydantic import BaseModel
import boto3
import json

app = FastAPI()

origins = [
    "https://ai.galaxychain.zone",
    "https://galaxychain.zone",
    "http://localhost:3000",
    "https://galaxychain.zone",
    "https://ai-api.galaxychain.zone",
]

app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Amazon SageMaker Runtime Client
session = boto3.Session()
sagemaker_runtime = session.client('sagemaker-runtime')

# Sagemaker endpoint name.
endpoint_name = 'sm-endpoint-gpt-j-6b'

class DataIn(BaseModel):
    text: str
    datas: dict

@app.get("/elb")
async def server_status():
    response_body = {
        'status': 200,
        'data': "Healthy"
    }

    return jsonable_encoder(response_body)

@app.post('/gen-text')
async def text_generate(ai_params: DataIn):
    response_body = {
        'status': 100,
        'data': ""
    }

    payload = {
        'inputs': "",
        'parameters': {
            'max_length': 100,
            'do_sample': True,
            'no_repeat_ngram_size': 2,
            'temperature': 0.75,
            'top_k': 10,
            'top_p': 0.95,
            'early_stopping': True,
        }
    }

    user_text = ai_params.dict()['text']
    if user_text == "":
        response_body['status'] = 400
        response_body['data'] = "Please enter text."

        return jsonable_encoder(response_body)

    tempr_param = float(ai_params.dict()['datas']['randomness'])
    if tempr_param < 0.01:
        tempr_param = 0.01

    top_k_param = int(ai_params.dict()['datas']['fluency'])
    if top_k_param < 1:
        top_k_param = 1

    payload['inputs'] = user_text
    payload['parameters']['temperature'] = tempr_param
    payload['parameters']['top_k'] = top_k_param

    try:
        response = sagemaker_runtime.invoke_endpoint(
            EndpointName=endpoint_name,
            ContentType='application/json',
            Body=json.dumps(payload)
        )
        result = json.loads(response['Body'].read().decode())

        raw_text = result[0]['generated_text']
        res_text = str(raw_text).replace(user_text, "").replace("\n", " ").replace('"', "")

        response_body['status'] = 200
        response_body['data'] = res_text

        return jsonable_encoder(response_body)
    except:
        response_body['status'] = 503
        response_body['data'] = "AI server is overloaded"

        return jsonable_encoder(response_body)


import cv2
import  numpy as np
from matplotlib import pyplot as plt

# 模版匹配
img = cv2.imread("fb.png", 0)
img2 = img.copy()
template = cv2.imread("zdbz.png", 0)
w,h = template.shape[::-1]
method = eval("cv2.TM_CCOEFF")
res = cv2.matchTemplate(img2, template ,method)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
print(min_val,max_val,min_loc,max_loc)
topLeft = max_loc
bottomRight = (topLeft[0]+w,topLeft[1]+h)
print(bottomRight)

阴阳师副本自动化/副本自动化2.py


#!/usr/bin/python3

import numpy as np
import scipy.integrate
import matplotlib.pyplot as plt

def vdp(t,y):
    """calculate Van Der Pol Derivatives"""
    # y is a tuple (y0,y1)
    y0dot = y[1]
    y1dot = (1-y[0]**2)*y[1]-y[0]
    dydt = ( y0dot, y1dot )
    return dydt

solution = scipy.integrate.solve_ivp(vdp, t_span=(0,20), y0=(0,2), method='RK45', rtol=1e-6)

t = solution.t
y0 = solution.y[0]
y1 = solution.y[1]

fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot(t, y0, color='tab:blue', label='y1')
ax.plot(t, y1, color='tab:orange', label='y2')
ax.set_title('Solution of the van der Pol equation, mu=1')
ax.set_xlabel('time')
ax.set_ylabel('solution')
ax.legend()
plt.show()


import numpy as np
import matplotlib.pyplot as plt
import pyRaven as rav
import emcee
import corner
from scipy.stats import norm
import scipy
from statistics import mode

def fitdata(param,DataPacket,guess):
  '''
  This function fits a set of LSD profiles using scipy's curve fit function.

  Inputs:
    param - input parameter dictionary
    DataPacket - input DataPacket with real data
    guess - array of guess values for kappa, vsini, and vmac. Ex: np.array([1.3,250,30])

   Outputs:
    parameters - array of fit parameters
    covariance - covariance matrix of the fit
    modelout - the best fit model

  '''
  def model(v,kappa,vsini,vmac):
      '''
      This function creates the line profile model that will be fit to the observed profile

      Inputs:
        kappa - value of kappa that the walker is on in parameter space
        vsini - value of vsini that the walker is on in parameter space
        v - velocity array of the actual data

      Outputs:
        f - line profile model using the weak field method at the walker's position in parameter space
      '''
      param['general']['vsini']=vsini
      param['general']['vmac']=vmac
      param['general']['logkappa']=np.log(kappa)

      #pyRaven's weakfield disk integration function
      model=rav.diskint2.analytical(param,False)

      #interpolating the model to be size MCMC wants
      f=np.interp(v,model['vel'],model['flux'])
      return(f)

  x=DataPacket.scaled.lsds[0].vel#+DataPacket.vrad[0]
  y=DataPacket.scaled.lsds[0].specI
  if DataPacket.nobs!=1:
    for i in range(1,DataPacket.nobs):
      x=np.append(x,DataPacket.scaled.lsds[i].vel)#+DataPacket.vrad[i])
      y=np.append(y,DataPacket.scaled.lsds[i].specI)

  parameters,covariance = scipy.optimize.curve_fit(model,x,y,guess)

  modelout=model(x,parameters[0],parameters[1],parameters[2])
  modelout=modelout[:DataPacket.scaled.lsds[0].vel.size]
  return parameters,covariance,modelout

def fitdataMCMC(param,DataPacket,nsteps,guess):
  '''
  This function fits the LSD profile using MCMC

  Inputs:
    param - input parameter dictionary
    DataPacket - input DataPacket with real data
    nsteps - number of steps to run MCMC
    guess - array of guess values for kappa, vsini, and vmac. Ex: np.array([1.3,250,30])

  Outputs:
    kappa - The average fitted value of kappa
    vsini - The average fitted value of vsini
    vmac - The average fitted value of vmac
  '''
  #def model(v,c,a,b,Ic):
  #  return(-c*np.exp(-0.5*np.power(v-a,2)/b**2)+Ic)

  def model(v,kappa,vsini,vmac):
      '''
      This function creates the line profile model that will be fit to the observed profile

      Inputs:
        kappa - value of kappa that the walker is on in parameter space
        vsini - value of vsini that the walker is on in parameter space
        vmac - value of the vmac that the walker is on in parameter space
        v - velocity array of the actual data

      Outputs:
        f - line profile model using the weak field method at the walker's position in parameter space
      '''
      param['general']['vsini']=vsini
      param['general']['vmac']=vmac
      param['general']['logkappa']=np.log(kappa)

      #pyRaven's weakfield disk integration function
      model=rav.diskint2.analytical(param,False)

      #interpolating the model to be size MCMC wants
      f=np.interp(v,model['vel'],model['flux'])
      return(f)

  def lnprior(params):
    '''
    This function is used to set constraints on the parameter space the walkers are allowed in. I did this to try and save time, could probably use some tweaking.

    Inputs:
      params - list of walker parameters, in this code that is [kappa, vsini]

    Outputs:
      -infinity - if kappa and/or vsini are out of the specified ranges
      0 - otherwise
    '''
    kappa,vsini,vmac=params
    if kappa<=0.0 or kappa>=10.0 or vsini >= 500.0 or vsini<=0.0 or vmac<=0.0 or vmac>=100.0:
      return(-np.inf)
    else:
      return(0.0)

  def lnlike(params,v,I,Ierr):
    '''
    Inputs:
      params -list of walker parameters, in this code that is [kappa, vsini]
      v - velocity array of the data
      I - stokes I of the actual data
      Ierr - uncertainty in the stokes I of the actual data

    Outputs:
      The log likelihood using a gaussian function
    '''
    kappa,vsini,vmac= params
    m = model(v,kappa,vsini,vmac)
    sigma2 = Ierr**2 #+m**2*np.exp(2*log_f)
    return(-0.5 * np.sum((I - m) ** 2 / sigma2 + np.log(sigma2)))

  def lnprob(params,v,I,Ierr):
    '''
    Inputs:
      params - list of walker parameters, in this code that is [kappa, vsini]
      v - velocity array of the data
      I - stokes I of the actual data
      Ierr - uncertainty in the stokes I of the actual data

    Outputs:
      log probability. Used to determine how good a fit the model is
    '''
    prior=lnprior(params)
    if not np.isfinite(prior):
      return(-np.inf)
    else:
      return(prior+lnlike(params,v,I,Ierr))

  # Set up the convergence diagonstic plots. At the final step we want all the walkers to be very close together, i.e a straight line at the end.
  fig, ax = plt.subplots(3,1,figsize=(15,5))
  ax[0].set_title('Convergence Diagnostic Plots')

  fig1, ax1 = plt.subplots(1,1,figsize=(5,5)) #sets up the send set of plots

  #for i in range(1):
  kappa=np.array([])
  #log_f=np.array([])
  vsini=np.array([])
  vmac=np.array([])
  Ic=1.0 #defining the continuum value of the real data
  vsiniin=DataPacket.vsini #defining the vsini listed in the data packet

  v=DataPacket.scaled.lsds[0].vel#+DataPacket.vrad[0] #defining the velocity array of the data
  I=DataPacket.scaled.lsds[0].specI #defining the stokes I array of the data
  Ierr=DataPacket.scaled.lsds[0].specSigI #defining the stokes I error of the data
  for i in range(1,DataPacket.nobs):
    v=np.append(v,DataPacket.scaled.lsds[i].vel)#+DataPacket.vrad[i] #defining the velocity array of the data
    I=np.append(I,DataPacket.scaled.lsds[i].specI) #defining the stokes I array of the data
    Ierr=np.append(Ierr,DataPacket.scaled.lsds[i].specSigI) #defining the stokes I error of the data

  ndim = 3 #number of parameters to fit
  nwalkers= 10 * ndim #number of walkers (10/parameter)
  pguess = guess #initial guess for kappa and vsini
  positions = np.zeros((nwalkers,ndim)) #set up walker position array
  positions[:,0] = np.abs(np.random.randn(nwalkers)*pguess[0]*0.1+pguess[0]) #set the inital positions of the kappa walkers to be a random distribution around the guess
  positions[:,1] = np.random.randn(nwalkers)*pguess[1]*0.1+pguess[1] #set the initial positions of the vsini walkers
  positions[:,2] = np.random.randn(nwalkers)*pguess[2]*2

  sampler = emcee.EnsembleSampler(nwalkers,ndim,lnprob,args=(v,I,Ierr)) #set up MCMC. Note that the args keyword contains the real data arrays

  pos,prob,state = sampler.run_mcmc(positions, nsteps,progress=True) #runs MCMC for the specified number of steps

  #make the first set of plots
  res = [ax[j].plot(sampler.chain[:,:,j].T, '-', color='k', alpha=0.3) for j in range(3)]
  res = [ax[j].axhline(pguess[j]) for j in range(3)]

  #save the walker positions at each step (for diagnostics)
  #kappa=np.append(kappa,np.mean(sampler.flatchain[int(2*nsteps/3):], axis=0)[0])
  #vsini=np.append(vsini,np.mean(sampler.flatchain[int(2*nsteps/3):], axis=0)[1])
  #vmac=np.append(vmac,np.mean(sampler.flatchain[int(2*nsteps/3):], axis=0)[2])

  kappa=sampler.flatchain[int(2*100/3):][:,0]
  vsini=sampler.flatchain[int(2*100/3):][:,1]
  vmac=sampler.flatchain[int(2*100/3):][:,2]

  bins=20
  bin_means = (np.histogram(kappa, bins, weights=kappa)[0]/np.histogram(kappa, bins)[0])
  kappa=bin_means[np.histogram(kappa, bins)[0]==np.histogram(kappa, bins)[0].max()][0]

  bin_means = (np.histogram(vsini, bins, weights=vsini)[0]/np.histogram(vsini, bins)[0])
  vsini=bin_means[np.histogram(vsini, bins)[0]==np.histogram(vsini, bins)[0].max()][0]

  bin_means = (np.histogram(vmac, bins, weights=vmac)[0]/np.histogram(vmac, bins)[0])
  vmac=bin_means[np.histogram(vmac, bins)[0]==np.histogram(vmac, bins)[0].max()][0]

  #log_f=np.append(log_f,np.mean(sampler.flatchain, axis=0)[1])

  #make the second set of plots

  ax1.plot(DataPacket.scaled.lsds[0].vel,model(DataPacket.scaled.lsds[0].vel, kappa,vsini,vmac))
  ax1.plot(v,I)

  print('kappa: {} | vsini: {} | vmac: {}'.format(kappa,vsini,vmac))

  #make the corner plots
  flat_samples = sampler.get_chain(discard=0, thin=5, flat=True)
  labels = ["kappa","vsini",'vmac']
  corner.corner(
      flat_samples, labels=labels)

  return(kappa,vsini,vmac,sampler.flatchain)

def fitdata_novsini(param,DataPacket,guess):
  '''
  This function fits a set of LSD profiles using scipy's curve fit function.

  Inputs:
    param - input parameter dictionary
    DataPacket - input DataPacket with real data
    guess - array of guess values for kappa and vmac. Ex: np.array([1.3,30])

   Outputs:
    parameters - array of fit parameters
    covariance - covariance matrix of the fit
    modelout - the best fit model

  '''
  def model(v,kappa,vmac):
      '''
      This function creates the line profile model that will be fit to the observed profile

      Inputs:
        kappa - value of kappa that the walker is on in parameter space
        vmac - value of vmac that the walker is on in parameter space
        v - velocity array of the actual data

      Outputs:
        f - line profile model using the weak field method at the walker's position in parameter space
      '''
      param['general']['vmac']=vmac
      param['general']['logkappa']=np.log(kappa)

      #pyRaven's weakfield disk integration function
      model=rav.diskint2.analytical(param,False)

      #interpolating the model to be size MCMC wants
      f=np.interp(v,model['vel'],model['flux'])
      return(f)

  param['general']['vsini']=DataPacket.vsini

  x=DataPacket.scaled.lsds[0].vel#+DataPacket.vrad[0]
  y=DataPacket.scaled.lsds[0].specI
  if DataPacket.nobs!=1:
    for i in range(1,DataPacket.nobs):
      x=np.append(x,DataPacket.scaled.lsds[i].vel)#+DataPacket.vrad[i])
      y=np.append(y,DataPacket.scaled.lsds[i].specI)

  parameters,covariance = scipy.optimize.curve_fit(model,x,y,guess)

  modelout=model(x,parameters[0],parameters[1])
  modelout=modelout[:DataPacket.scaled.lsds[0].vel.size]
  return parameters,covariance,modelout


# # Categorize all issues
#
# To use: open with jupyter notebook/lab using jupytext and run all cells

# +
from getpass import getpass
from textwrap import dedent

from ipywidgets import Button, ToggleButtons, Output, VBox
from IPython.display import display, Markdown
import gitlab

# -

gl = gitlab.Gitlab(url="https://gitlab.kwant-project.org", private_token=getpass("Gitlab API token: "))
repo = gl.projects.get("zesje/zesje")

labels = repo.labels.list()

# +
label_dict = {tuple(label.name.split(": ")): label for label in labels if ": " in label.name}

categories = ["impact", "effort", "maintainability"]
degrees = ["low", "medium", "high"]

# +
description = Output()

selector = {
    category: ToggleButtons(
        options=[(degree, label_dict[category, degree]) for degree in degrees],
        description=category,
        label="medium",
        style={"button_color": label_dict},
    )
    for category in categories
}

submit = Button(description="submit & next", icon="check")

current_issue = None

def submit_labels(issue):
    other_labels = [i for i in issue.labels if ": " not in i]
    issue.labels = [i.value.name for i in selector.values()] + other_labels
    issue.save()

def render_issue(issue):
    return Markdown(
        dedent(
            f"""### [{issue.title}]({issue.web_url})

    {issue.description}
    """
        )
    )

def next_issue():
    issues = repo.issues.list(all=True, state="opened")
    for issue in issues:
        issue_categories = {label.split(": ")[0]: label.split(": ")[1] for label in issue.labels if ": " in label}
        already_labeled = len(issue_categories) == 3 and len(set(issue_categories)) == 3
        if not already_labeled:
            break
    else:
        submit.disabled = True
        submit.text = "Done!"
        for button in selector.values():
            button.disabled = True

        return None

    description.clear_output(wait=True)
    with description:
        display(render_issue(issue))

    for category, degree in issue_categories.items():
        selector[category].label = degree

    return issue

def submit_and_next(event):
    global current_issue

    if current_issue is not None:
        submit_labels(current_issue)

    current_issue = next_issue()

submit.on_click(submit_and_next)

VBox(children=[description] + list(selector.values()) + [submit])


from itertools import permutations
import cProfile

#
#make permutation of array
#

list = [1,2,3,4]

listPermutations =  permutations(list)
for permutation in listPermutations:
    print(permutation)

#
#count number of permutations
#

listPermutations =  permutations(list)
count = 0
for permutation in listPermutations:
    count += 1
print(len(list), count)

#
#check the performance and undestand
# how fast the space of permutations grows
#

def faculty(n):
    if n <= 1:
        return n
    else:
        return faculty(n-1)+n

def counter(n):
    count = 0
    for i in range(n):
        count += 1

    return count

cProfile.run("counter(faculty(10))")


#!/usr/bin/env python3
#

import sys, argparse
import ROOT
ROOT.PyConfig.IgnoreCommandLineOptions = True

def h2pgf(h):
    """ Convert TH1 into pgfplot data with error bars
    input: xmin xmax y ey
    output: x ex y ey
    ex = (xmax+xmin)/2
    """
    nbins = h.GetNbinsX()

#    print("# \\begin{axis}")
#    print("# \\addplot[const plot mark mid, black, solid, no markers, error bars/.cd, y dir=both, y explicit, error mark=none]")
#    print(" coordinates {")
    print("x ex y ey")
    for b in range(1, nbins+1):
        x = h.GetBinCenter(b)
        ex = h.GetBinWidth(b)/2.0
        y = h.GetBinContent(b)
        ey = h.GetBinError(b)
# print(x,ex,y,ey)
        if y>0.0:
            print(x, ex, y, ey)

def g2pgf(h):
    """ Convert TGraph into pgfplot data """

    N = h.GetN()

    print("\\begin{axis}")
    print("\\addplot[ultra thick]")
    print(" coordinates {")
    print("x ex y ey")
    for b in range(N):
        x = h.GetX()[b]
        y = h.GetY()[b]
        print(x, y)
    print("};")
    print("\\addlegendentry{TGraph};")

def main():
    """ A script to convert TH1 and TGraph into a pgfplot format """

    parser = argparse.ArgumentParser(description=main.__doc__, epilog='Homepage: https://github.com/kbat/mc-tools')
    parser.add_argument('root', type=str, help='ROOT file')
    parser.add_argument('hist', type=str, help='histogram name')
    args = parser.parse_args()

    f = ROOT.TFile(args.root)
    f.ls()
    h = f.Get(args.hist)

    if h.InheritsFrom("TH1"):
        h2pgf(h)
    elif h.InheritsFrom("TGraph"):
        g2pgf(h)

if __name__ == "__main__":
    sys.exit(main())