from django.shortcuts import render
from django.views import View
from django.http import HttpResponseRedirect
from django.http import HttpResponseForbidden
import requests
from NCNUSALineNotify import settings
from .models import Message, AccessToken, BroadcastGroup
import logging

logger = logging.getLogger(__name__)

def lineNotifyMessage(token, msg):
    headers = {
        "Authorization": "Bearer " + token,
        "Content-Type": "application/x-www-form-urlencoded"
    }
    payload = {'message': msg}
    r = requests.post("https://notify-api.line.me/api/notify",
                      headers=headers, params=payload
                      )
    return r

def index(request):
    if request.session.get('registered', False):
        return render(request, "register.html")
    return render(request, "register.html", {
        "id": settings.LINE_CLIENT,
        "url": settings.HOST+"/register",
    })

def register(request):
    if not request.GET.get("code", False):
        return HttpResponseForbidden("Not for you")
    payload, url = {
        "grant_type": "authorization_code",
        "code": request.GET.get("code"),
        "redirect_uri": settings.HOST+"/register",
        "client_id": settings.LINE_CLIENT,
        "client_secret": settings.LINE_CLIENT_KEY
    }, "https://notify-bot.line.me/oauth/token"
    r = requests.post(url, params=payload)
    if r.status_code == 200:
        AccessToken(token=r.json()["access_token"]).save()
        request.session["registered"] = True
    else:
        return HttpResponseForbidden("Fail")
    return HttpResponseRedirect("/")

class Send(View):
    def send_pro(self, content, tokens):
        for token in tokens:
            status = lineNotifyMessage(token.token, content)
            if status.status_code == 401:
                token.delete()
            elif status.status_code != 200:
                return status
        return None

    def get(self, request):
        groupsOptions = [{'name': '全部', 'id': -1}]
        for x in BroadcastGroup.objects.all():
            groupsOptions.append({'name': x.name, 'id': x.id})
        if not request.user.is_authenticated:  # 未登入
            return HttpResponseRedirect('/admin/login/?next=/pushMessage')
        return render(request, "index.html", {
            "groups": groupsOptions
        })

    def post(self, request):
        groupsOptions = [{'name': '全部', 'id': -1}]
        for x in BroadcastGroup.objects.all():
            groupsOptions.append({'name': x.name, 'id': x.id})
        if not request.user.is_authenticated:  # 未登入
            return HttpResponseForbidden('想幹嘛')
        title = request.POST.get("title")
        content = request.POST.get("content")
        groups = request.POST.getlist("groups")
        if title is None or content is None or len(groups) == 0:
            return render(request, "index.html", {
                "title": title,
                "content": content,
                "message": "未填寫完成",
                "groups": groupsOptions
            })
        if len(title) > 20:
            return render(request, "index.html", {
                "title": title,
                "content": content,
                "message": "標題長度不要超過20",
                "groups": groupsOptions
            })
        for group in groups:
            if int(group) == -1:
                tokens = AccessToken.objects.all()
                status = self.send_pro(content, tokens)
                if status is not None:
                    logger.error(status)
                    return render(request, "index.html", {
                        "title": title,
                        "content": content,
                        "message": "推播發送失敗",
                        "groups": groupsOptions
                    })
                Message(
                    user=request.user,
                    message=content,
                    toGroup="全部",
                    title=title
                ).save()
            else:
                tokens = AccessToken.objects.filter(groups=int(group))
                status = self.send_pro(content, tokens)
                if status is not None:
                    logger.error(status)
                    return render(request, "index.html", {
                        "title": title,
                        "content": content,
                        "message": "推播發送失敗",
                        "groups": groupsOptions
                    })
                Message(
                    user=request.user,
                    message=content,
                    toGroup=BroadcastGroup.objects.get(id=group).name,
                    title=title
                ).save()
        return render(request, "index.html", {
                "title": title,
                "content": content,
                "message": "推播發送成功",
                "groups": groupsOptions
        })


from django.db import models
from mighty.models.base import Base
from mighty import translates as _
from datetime import datetime

class TimelineModel(Base):
    object_id = models.ForeignKey('', on_delete=models.CASCADE)
    field = models.CharField(_.field, max_length=255, db_index=True)
    value = models.TextField(_.value)
    fmodel = models.CharField(_.fmodel, max_length=255)
    date_begin = models.DateField(_.date_begin)
    date_end = models.DateField(_.date_end, null=True, blank=True)
    user = models.CharField(max_length=255)
    to_init = False
    model_activate_timeline = True

    class Meta:
        abstract = True
        verbose_name = _.v_timeline
        verbose_name_plural = _.vp_timeline
        ordering = ['-date_begin']
        unique_together = ['date_begin', 'value']

    @property
    def is_init(self):
        return type(self).objects.filter(object_id=self.object_id, field=self.field).first()

    def replace_value(self):
        setattr(self.object_id, self.field, self.value)
        self.object_id.save()

    def save(self, *args, **kwargs):
        first = self.is_init
        if not first and not self.to_init:
            init = type(self)(
                object_id=self.object_id,
                field=self.field,
                value=str(getattr(self.object_id, self.field)),
                date_begin=self.object_id.date_create,
                date_end=self.date_begin
            )
            init.to_init = True
            init.save()
        last = type(self).objects.filter(object_id=self.object_id, field=self.field, date_end__isnull=True)
        last.update(date_end=self.date_begin)
        self.replace_value()
        super().save(*args, **kwargs)


import RPi.GPIO as gpio
from time import sleep
import random
import sys
import os
from multiprocessing import Process
import subprocess

gpio.setMode(gpio.board)
gpio.setup(11, gpio.OUT)
servo = gpio.pwm(11, 50)
servo.start(0)
gpio.setup(13, gpio.OUT)
enable = gpio.pwm(13, 50)
enable.start(0)
gpio.output(13, gpio.LOW)
stop = 0
gpio.setup(16, gpio.IN)

def start():
    global playfinished
    global started
    if started != 1:
        playfinished = 0
        os.system("mpv start.wav")
        playfinished = 1
    started = 1

def rotorspin():
    gpio.out(13, gpio.HIGH)

def servospin():
    servo.ChangeDutyCycle(6.25)
    servo.ChangeDutyCycle(7.5)
    servo.ChangeDutyCycle(8.75)
    servo.ChangeDutyCycle(7.5)

def stop():
    global stop
    if gpio.input(16) == 1:
        stop = 1

def ambient():
    global playfinished
    if start == 1 and playfinished == 1:
        playfinished = 0
        os.system("mpv loop.wav")
        playfinished = 1

while True:
    if gpio.input(16) == 1:
        stop = 1
        sleep(2)
    if stop != 1:
        Process(target=start()).start()
        Process(target=ambient()).start()
        sleep(3)
        Process(target=servospin()).start()
        Process.start(target=rotorspin()).start()
    else:
        if gpio.input(16) == 1:
            stop = 0
            sleep(2)


import sys
from cx_Freeze import setup, Executable
import os

print(os.getcwd())
path = os.getcwd()
base = None
if sys.platform == "win32":
    base = "Win32GUI"
    opts = {"includes": ["re", "atexit"],'include_files': [
                                'blue-folder-open-document.png',
                                'arrow-curve-180-left.png',
                              'images/arrow-curve.png',
                              'images/blue-folder-open-document.png',
                              'images/blue-folder-open-document.png',
                              'images/clipboard-paste-document-text.png',
                              'images/disk--pencil.png',
                              'images/disk.png',
                              'images/document-copy.png',
                              'images/scissors.png',
                              'images/selection-input.png']}

setup(name="Notepad2020",
      version='0.1',
      description = "Notepad",
      options={
          'bdist_mac': {
              'bundle_name': "XXX",
          }

      },
      author = "Paul",

      executables = [Executable('notepad.py', base=base)])

Практическое использование ООП/code/notepad2/setup.py


#!/usr/bin/python

'''
Basic Display Widget
'''

import sys
import cv2
from PyQt5.QtWidgets import QWidget
from PyQt5.QtGui import QPainter, QFont
from PyQt5.QtCore import Qt, QRect
from PyQt5.QtWidgets import QApplication
import gui.app_widgets.common as apw_comm

class BasicDisplayWidget(QWidget):
    '''
    用于显示图像的 Widget 控件
    '''
    def __init__(self, parent=None):
        super().__init__(parent)
        self.display_image = None
        self.display_width = 0
        self.display_height = 0
        return

    def paintEvent(self, e):
        '''
        重写绘图事件
        '''
        painter = QPainter(self)
        if self.display_image is None:
            painter.setPen(Qt.black)
            painter.setFont(QFont("Arial", 30))
            painter.drawText(e.rect(), Qt.AlignCenter, "Identity Finder")
        else:
            window_rect = self.rect()
            image_rect = QRect(0, 0, self.display_width, self.display_height)
            ratio_of_image = image_rect.width() / image_rect.height()
            ratio_of_window = window_rect.width() / window_rect.height()
            if ratio_of_image > ratio_of_window:
                window_rect.setHeight(window_rect.width()/ratio_of_image)
                delta = (self.rect().height()-window_rect.height())*0.5
                window_rect.setY(delta)
                window_rect.setHeight(window_rect.height()+delta)
            else:
                window_rect.setWidth(window_rect.height()*ratio_of_image)
                delta = (self.rect().width()-window_rect.width())*0.5
                window_rect.setX(delta)
                window_rect.setWidth(window_rect.width()+delta)
            painter.drawImage(window_rect, self.display_image, image_rect)
        return

    def setDisplayCvImage(self, img_cv):
        '''
        设置要显示的图像
        '''
        if img_cv is None:
            self.display_image = None
            return
        self.setDisplayQImage(apw_comm.img_cv2qt(img_cv))
        return

    def setDisplayQImage(self, img_qt):
        '''
        设置要显示的图像
        '''
        if img_qt is None:
            self.display_image = None
            return
        self.display_image = img_qt
        self.display_width = img_qt.width()
        self.display_height = img_qt.height()
        self.update()
        return

if __name__ == '__main__':
    img = cv2.imread("/root/Documents/GitReps/text-detection/data/CarPlates/Common/冀FA3215.jpg")
    app = QApplication(sys.argv)
    widget = BasicDisplayWidget()
    # widget.setDisplayImage(img)
    widget.show()
    sys.exit(app.exec_())


import dlib
import imutils

import numpy as np
import cv2 as cv

from imutils import face_utils
from scipy.spatial import distance as dist

class WrapperDrowsinessModel:
    def __init__(self):
        super(WrapperDrowsinessModel, self).__init__()
        self.face_detector = dlib.get_frontal_face_detector()
        self.landmark_predict = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')
        self.thresh = 0.35

    @staticmethod
    def _preprocess(frame: np.ndarray):
        frame = imutils.resize(frame, width=640)
        img_gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
        return img_gray

    def predict(self, frame):
        preprocessed_frame = self._preprocess(frame)
        faces = self.face_detector(preprocessed_frame)
        if len(faces) == 0:
            return 1
        else:
            face = faces[0]

        landmarks = self.landmark_predict(preprocessed_frame, face)
        drowsy = self._postprocess(landmarks)

        # # -------------------
        # cv.rectangle(frame, (face.left(), face.top()), (face.right(), face.bottom()), (0, 255, 0), 5)
        # cv.rectangle(frame, left_p1, left_p2, (255, 0, 0), 5)
        # cv.rectangle(frame, right_p1, right_p2, (255, 0, 0), 5)
        # if drowsy:
        #     cv.putText(frame, 'Drowsy', (30, 30), cv.FONT_HERSHEY_DUPLEX, 1, (0, 0, 255), 2)
        # cv.imshow("Video", frame)

        return drowsy

    def _postprocess(self, landmarks):
        (L_start, L_end) = face_utils.FACIAL_LANDMARKS_IDXS["left_eye"]
        (R_start, R_end) = face_utils.FACIAL_LANDMARKS_IDXS['right_eye']
        shape = face_utils.shape_to_np(landmarks)

        # parsing the landmarks list to extract left eye and right eye landmarks
        left_eye = shape[L_start: L_end]
        right_eye = shape[R_start:R_end]

        # # ------------------------------
        # left_p1, left_p2 = self._get_bounding_box(left_eye)
        # right_p1, right_p2 = self._get_bounding_box(right_eye)

        left_EAR = self._calculate_ear(left_eye)
        right_EAR = self._calculate_ear(right_eye)
        avg = (left_EAR + right_EAR) / 2

        return avg < self.thresh

    @staticmethod
    def _calculate_ear(eye):
        # calculate the vertical distances
        y1 = dist.euclidean(eye[1], eye[5])
        y2 = dist.euclidean(eye[2], eye[4])

        # calculate the horizontal distance
        x1 = dist.euclidean(eye[0], eye[3])

        # calculate the EAR
        EAR = (y1 + y2) / x1
        return EAR

    @staticmethod
    def _get_bounding_box(eye):
        p1 = np.min(eye, axis=0)
        p2 = np.max(eye, axis=0)

        return p1 - 20, p2 + 20


from django.shortcuts import render
from .models import Balance, Member, Ministry, Tithe, User, Income

def index(request):
    # Fetch the required data from the database or other sources
    current_balance = Balance.objects.get(pk=1).amount
    total_active_members = Member.objects.filter(is_active=True).count()
    total_ministries = Ministry.objects.count()
    total_users = User.objects.count()

    # Retrieve recent income and tithes
    recent_incomes_tithes = Income.objects.order_by('-date')[:5]
    recent_incomes_tithes |= Tithe.objects.order_by('-date')[:5]

    context = {
        'current_balance': current_balance,
        'total_active_members': total_active_members,
        'total_ministries': total_ministries,
        'total_users': total_users,
        'recent_incomes_tithes': recent_incomes_tithes,
    }

    return render(request, 'admin/index.html', context)


from flask import render_template
from app import app, db
from app.models.forms import NewBookForm
from app.models.tables import Book

@app.route("/novo_livro", methods=["GET", "POST"])
def new_book():
    form = NewBookForm()
    if form.validate_on_submit():
        b = Book(title=form.title.data,
                 author=form.author.data,
                 publisher=form.publisher.data,
                 gender=form.gender.data)
        db.session.add(b)
        db.session.commit()
    return render_template("newbook.html",
                           form=form)


import numpy as np
import tensorflow as tf
from tensorflow import keras
# import libraries
from keras.preprocessing import image
import numpy as np
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense,GlobalAveragePooling2D
from keras.callbacks import *
from keras.models import Model
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.models import Sequential, save_model, load_model

# define the training directory with the images
train_data_dir  = "/content/test/test"

# set the image size
img_height = 224
img_width = 224

# define some of the model parameters
batch_size = 32

# create an image data generator and apply data augmentation
train_datagen = ImageDataGenerator(rescale=1./255,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True,
    validation_split=0.2) # set validation split

# training generator
train_generator = train_datagen.flow_from_directory(
    train_data_dir,
    target_size=(img_height, img_width),
    batch_size=batch_size,
    shuffle = True,
    class_mode='categorical',
    subset='training') # set as training data

# validation generator
validation_generator = train_datagen.flow_from_directory(
    train_data_dir, # same directory as training data
    target_size=(img_height, img_width),
    batch_size=batch_size,
    shuffle = True,
    class_mode='categorical',
    subset='validation') # set as validation data

# define the model architecture, we perform transfer learning using the Xception model trained in colab
base_model = keras.applications.Xception(
    weights="imagenet",  # Load weights pre-trained on ImageNet.
    input_shape=(224, 224, 3),
    include_top=False,
)  # Do not include the ImageNet classifier at the top.

x = base_model.output
x = GlobalAveragePooling2D()(x)
# let's add a fully-connected layer
x = Dense(1024, activation='relu')(x)
x = Dropout(0.25)(x)

# and a logistic layer -- let's say we have 200 classes
predictions = Dense(48, activation='softmax')(x)

for layer in base_model.layers:
    layer.trainable = False

# this is the model we will train
model = Model(inputs=base_model.input, outputs=predictions)
print(model.summary())

# Train the top layer of the model
model.compile(
    optimizer=keras.optimizers.Adam(),
    loss="categorical_crossentropy",
    metrics=["accuracy"],
)

epochs = 50

hist=model.fit(
    train_generator,
    steps_per_epoch = 14320 // batch_size,
    epochs=epochs,
    verbose=1,
    validation_data=validation_generator,
    validation_steps= (3566 // batch_size))

# Unfreeze the base_model. Note that it keeps running in inference mode
# since we passed `training=False` when calling it. This means that
# the batchnorm layers will not update their batch statistics.
# This prevents the batchnorm layers from undoing all the training
# we've done so far.
base_model.trainable = True
model.summary()

model.compile(
    optimizer=keras.optimizers.Adam(1e-5),  # Low learning rate
    loss=keras.losses.BinaryCrossentropy(from_logits=True),
    metrics=[keras.metrics.BinaryAccuracy()],
)

epochs = 10
model.fit(train_generator, epochs=epochs, validation_data=validation_generator)

# save model
filepath = './ML_model/saved_model'
save_model(model, filepath)


# -*- coding: utf-8 -*-
import pandas as pd
import random
from math import floor
master = pd.read_csv('dbs\master.csv')

#////////////////////////////////////////////////""
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mtconnectdatabase"]
mycol = mydb["orders"]
orders = []
orderType=['A','B','C','D','E','E2','F','F4','GZ','G']
#////////////////////////////////////////////////""

orgs = list(master['names'])
machs = list(master['machines'])
cu = list(master['CapacityUtil'])
ports =list(master['agentPorts'])
batchNumbers=random.sample(range(20000,100000),(sum(machs)))
indexBatch=0
for i in range(len(orgs)):
    path='temp_folder/'+str(orgs[i])+'/adapters/'
    machines=machs[i]
    capacity=cu[i]
    Aport=ports[i]
    ON=floor((capacity*machines)/100)
    count=1
    pathOrg='temp_folder/'+str(orgs[i])+'/'
    batch = open(pathOrg+'batchStart.bat','w+')
    kill = open(pathOrg+'batchKill.bat','w+')

    for m in range(machines):
        #//////

        t=random.sample(orderType,1)
        name='Mach'+str(m+1)
        MID=str(i+1)+name
        order= { "_id": MID, "QTY": random.randint(100,4000), "PartID": t[0],"Order":batchNumbers[indexBatch]}
        orders.append(order)

        #/////
        mId=str(m+1)
        fpath=path+'Mach'+mId+'.py'

        f = open(fpath,'w')
        port= Aport+m+1
        if count<=ON:
            mState = 'ON'
            count+=1
        else:
            mState='OFF'
            count+=1
        indexBatch+=1

        weed='{ "_id": M }'
        f.write(f'''import socket
import time
import random
HOST = '127.0.0.1'
PORT = {port}

import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mtconnectdatabase"]
mycol = mydb["orders"]
M="{MID}"
myquery = {weed}

while True:
    mydoc = mycol.find(myquery)
    for x in mydoc:
      order= ((x['Order']))
      QTY=((x['QTY']))
      PartID=((x['PartID']))
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.connect((HOST, PORT))
        data=random.randint(50,95)
        breakdown=random.paretovariate(1)
        bd=0
        c=0
        if isinstance(order,str):
            ran = "{name}|OEE|"+"UNAVAILABLE"+"|power|UNAVAILABLE"+"|Idle|Empty|UNAVAILABLE"
        if isinstance(order,int):
            if('{mState}'=='OFF'):
                data=0
            if('{mState}'!='OFF') and (breakdown>=2) and (breakdown<=500):
                ran = "{name}|OEE|"+str(data)+"|power|Cutting|"+str(order)+"|"+str(QTY)+"|"+PartID
                c=1
            if(c==0):
                ran = "{name}|OEE|"+str(data)+"|power|{mState}|"+str(order)+"|"+str(QTY)+"|"+PartID

        if breakdown>500:
            ran = "{name}|OEE|"+"UNAVAILABLE"+"|power|BREAKDOWN|"+str(order)+"|"+str(QTY)+"|"+PartID
            bd=1

        time.sleep(1)
        strin=bytes(ran,encoding='utf-8')
        s.sendall(strin)
        if(bd==1):
           repairTime=random.randint(50,100)
           while repairTime>=1:
               time.sleep(1)
               repairTime-=1
           bd=0

''')
        id="O"+str(i)+"M"+str(m)
        batch.write(f'start /min "[{id}]" python adapters/Mach{mId}.py\n')
        kill.write(f'taskkill /f /FI "WINDOWTITLE eq [{id}]\n')

batch.close()
kill.close()
f.close()
x = mycol.insert_many(orders)


import matplotlib as mpl
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
import warnings
from mpl_toolkits.mplot3d import Axes3D
from cde import ConditionalDensity

class BaseConditionalDensitySimulation(ConditionalDensity):

  def pdf(self, X, Y):
    """ Conditional probability density function p(y|x) of the underlying probability model

    Args:
      X: x to be conditioned on - numpy array of shape (n_points, ndim_x)
      Y: y target values for witch the pdf shall be evaluated - numpy array of shape (n_points, ndim_y)

    Returns:
      p(X|Y) conditional density values for the provided X and Y - numpy array of shape (n_points, )
    """

    raise NotImplementedError

  def log_pdf(self, X, Y):
    """ Conditional log-probability log p(y|x). Requires the model to be fitted.

       Args:
         X: numpy array to be conditioned on - shape: (n_samples, n_dim_x)
         Y: numpy array of y targets - shape: (n_samples, n_dim_y)

       Returns:
          conditional log-probability log p(y|x) - numpy array of shape (n_query_samples, )

     """
    # This method is numerically unfavorable and should be overwritten with a numerically stable method
    with warnings.catch_warnings():
      warnings.simplefilter("ignore")
      log_prob = np.log(self.pdf(X, Y))
    return log_prob

  def cdf(self, X, Y):
    """ Conditional cumulated probability density function P(Y < y | x) of the underlying probability model

    Args:
      X: x to be conditioned on - numpy array of shape (n_points, ndim_x)
      Y: y target values for witch the cdf shall be evaluated - numpy array of shape (n_points, ndim_y)

    Returns:
     P(Y < y | x) cumulated density values for the provided X and Y - numpy array of shape (n_points, )
    """

    raise NotImplementedError

  def simulate_conditional(self, X):
    """ Draws random samples from the conditional distribution

    Args:
      X: x to be conditioned on when drawing a sample from y ~ p(y|x) - numpy array of shape (n_samples, ndim_x)

    Returns:
      Conditional random samples y drawn from p(y|x) - numpy array of shape (n_samples, ndim_y)
    """
    raise NotImplementedError

  def simulate(self, n_samples):
    """ Draws random samples from the unconditional distribution p(x,y)

    Args:
      n_samples: (int) number of samples to be drawn from the conditional distribution

    Returns:
      (X,Y) - random samples drawn from p(x,y) - numpy arrays of shape (n_samples, ndim_x) and (n_samples, ndim_y)
    """
    raise NotImplementedError

  def plot(self, xlim=(-5, 5), ylim=(-5, 5), resolution=100, mode="pdf", show=False, numpyfig=False):
    """ Plots the distribution specified in mode if x and y are 1-dimensional each

    Args:
      xlim: 2-tuple specifying the x axis limits
      ylim: 2-tuple specifying the y axis limits
      resolution: integer specifying the resolution of plot
      mode: spefify which dist to plot ["pdf", "cdf", "joint_pdf"]

    """
    modes = ["pdf", "cdf", "joint_pdf"]
    assert mode in modes, "mode must be on of the following: " + modes
    assert self.ndim == 2, "Can only plot two dimensional distributions"

    if show == False and mpl.is_interactive():
      plt.ioff()

    # prepare mesh
    linspace_x = np.linspace(xlim[0], xlim[1], num=resolution)
    linspace_y = np.linspace(ylim[0], ylim[1], num=resolution)
    X, Y = np.meshgrid(linspace_x, linspace_y)
    X, Y = X.flatten(), Y.flatten()

    # calculate values of distribution
    if mode == "pdf":
      Z = self.pdf(X, Y)
    elif mode == "cdf":
      Z = self.cdf(X, Y)
    elif mode == "joint_pdf":
      Z = self.joint_pdf(X, Y)

    X, Y, Z = X.reshape([resolution, resolution]), Y.reshape([resolution, resolution]), Z.reshape(
      [resolution, resolution])
    fig = plt.figure(dpi=300)
    ax = fig.gca(projection='3d')
    surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm, rcount=resolution, ccount=resolution,
                           linewidth=100, antialiased=True)
    plt.xlabel("x")
    plt.ylabel("y")
    if show:
      plt.show()

    if numpyfig:
      fig.tight_layout(pad=0)
      fig.canvas.draw()
      numpy_img = np.fromstring(fig.canvas.tostring_rgb(), dtype=np.uint8, sep='')
      numpy_img = numpy_img.reshape(fig.canvas.get_width_height()[::-1] + (3,))
      return numpy_img

    return fig

  def mean_(self, x_cond, n_samples=10**6):
    """ Mean of the fitted distribution conditioned on x_cond
    Args:
      x_cond: different x values to condition on - numpy array of shape (n_values, ndim_x)

    Returns:
      Means E[y|x] corresponding to x_cond - numpy array of shape (n_values, ndim_y)
    """
    assert x_cond.ndim == 2

    if self.can_sample:
      return self._mean_mc(x_cond, n_samples=n_samples)
    else:
      return self._mean_pdf(x_cond)

  def std_(self, x_cond, n_samples=10 ** 6):
    """ Standard deviation of the fitted distribution conditioned on x_cond

    Args:
      x_cond: different x values to condition on - numpy array of shape (n_values, ndim_x)

    Returns:
      Standard deviations  sqrt(Var[y|x]) corresponding to x_cond - numpy array of shape (n_values, ndim_y)
    """
    x_cond = self._handle_input_dimensionality(x_cond)
    assert x_cond.ndim == 2
    return self._std_pdf(x_cond, n_samples=n_samples)

  def covariance(self, x_cond, n_samples=10**6):
    """ Covariance of the fitted distribution conditioned on x_cond

    Args:
      x_cond: different x values to condition on - numpy array of shape (n_values, ndim_x)
      n_samples: number of samples for monte carlo model_fitting

    Returns:
      Covariances Cov[y|x] corresponding to x_cond - numpy array of shape (n_values, ndim_y, ndim_y)
    """
    if self.has_pdf:
      return self._covariance_pdf(x_cond)
    elif self.can_sample:
      return self._covariance_mc(x_cond, n_samples=n_samples)
    else:
      raise NotImplementedError()

  def skewness(self, x_cond, n_samples=10 ** 6):
    """ Skewness of the fitted distribution conditioned on x_cond

       Args:
         x_cond: different x values to condition on - numpy array of shape (n_values, ndim_x)

       Returns:
         Skewness Skew[y|x] corresponding to x_cond - numpy array of shape (n_values, ndim_y, ndim_y)
       """
    x_cond = self._handle_input_dimensionality(x_cond)
    assert x_cond.ndim == 2
    if self.has_pdf:
      return self._skewness_pdf(x_cond, n_samples=n_samples)
    elif self.can_sample:
      return self._skewness_pdf(x_cond, n_samples=n_samples)
    else:
      raise NotImplementedError()

  def kurtosis(self, x_cond, n_samples=10 ** 6):
    """ Kurtosis of the fitted distribution conditioned on x_cond

       Args:
         x_cond: different x values to condition on - numpy array of shape (n_values, ndim_x)

       Returns:
         Kurtosis Kurt[y|x] corresponding to x_cond - numpy array of shape (n_values, ndim_y, ndim_y)
       """
    x_cond = self._handle_input_dimensionality(x_cond)
    assert x_cond.ndim == 2
    if self.has_pdf:
      return self._kurtosis_pdf(x_cond, n_samples=n_samples)
    elif self.can_sample:
      return self._kurtosis_mc(x_cond, n_samples=n_samples)
    else:
      raise NotImplementedError()

  def value_at_risk(self, x_cond, alpha=0.01, n_samples=10**6):
    """ Computes the Value-at-Risk (VaR) of the fitted distribution. Only if ndim_y = 1

    Args:
      x_cond: different x values to condition on - numpy array of shape (n_values, ndim_x)
      alpha: quantile percentage of the distribution
      n_samples: number of samples for monte carlo model_fitting

    Returns:
       VaR values for each x to condition on - numpy array of shape (n_values)
    """
    assert self.ndim_y == 1, "Value at Risk can only be computed when ndim_y = 1"
    assert x_cond.ndim == 2

    if self.has_cdf:
      return self._quantile_cdf(x_cond, alpha=alpha)
    elif self.can_sample:
      return self._quantile_mc(x_cond, alpha=alpha, n_samples=n_samples)
    else:
      raise NotImplementedError()

  def conditional_value_at_risk(self, x_cond, alpha=0.01, n_samples=10**6):
    """ Computes the Conditional Value-at-Risk (CVaR) / Expected Shortfall of the fitted distribution. Only if ndim_y = 1

       Args:
         x_cond: different x values to condition on - numpy array of shape (n_values, ndim_x)
         alpha: quantile percentage of the distribution
         n_samples: number of samples for monte carlo model_fitting

       Returns:
         CVaR values for each x to condition on - numpy array of shape (n_values)
       """
    assert self.ndim_y == 1, "Value at Risk can only be computed when ndim_y = 1"
    x_cond = self._handle_input_dimensionality(x_cond)
    assert x_cond.ndim == 2

    VaRs = self.value_at_risk(x_cond, alpha=alpha, n_samples=n_samples)

    if self.has_pdf:
      return self._conditional_value_at_risk_mc_pdf(VaRs, x_cond, alpha=alpha, n_samples=n_samples)
    elif self.can_sample:
      return self._conditional_value_at_risk_sampling(VaRs, x_cond, n_samples=n_samples)
    else:
      raise NotImplementedError("Distribution object must either support pdf or sampling in order to compute CVaR")

  def tail_risk_measures(self, x_cond, alpha=0.01, n_samples=10**6):
    """ Computes the Value-at-Risk (VaR) and Conditional Value-at-Risk (CVaR)

        Args:
          x_cond: different x values to condition on - numpy array of shape (n_values, ndim_x)
          alpha: quantile percentage of the distribution
          n_samples: number of samples for monte carlo model_fitting

        Returns:
          - VaR values for each x to condition on - numpy array of shape (n_values)
          - CVaR values for each x to condition on - numpy array of shape (n_values)
        """
    assert self.ndim_y == 1, "Value at Risk can only be computed when ndim_y = 1"
    assert x_cond.ndim == 2

    VaRs = self.value_at_risk(x_cond, alpha=alpha, n_samples=n_samples)

    if self.has_pdf:
      CVaRs = self._conditional_value_at_risk_mc_pdf(VaRs, x_cond, alpha=alpha, n_samples=n_samples)
    elif self.can_sample:
      CVaRs = self._conditional_value_at_risk_sampling(VaRs, x_cond, n_samples=n_samples)
    else:
      raise NotImplementedError("Distribution object must either support pdf or sampling in order to compute CVaR")

    assert VaRs.shape == CVaRs.shape == (len(x_cond),)
    return VaRs, CVaRs

  def get_configuration(self, deep=True):
    param_dict = super(BaseConditionalDensitySimulation, self).get_params(deep=deep)
    param_dict['simulator'] = self.__class__.__name__
    return param_dict

  def _handle_input_dimensionality(self, X, Y=None):
    # assert that both X an Y are 2D arrays with shape (n_samples, n_dim)

    if X.ndim == 1:
      X = np.expand_dims(X, axis=1)

    if Y is not None:
      if Y.ndim == 1:
        Y = np.expand_dims(Y, axis=1)

      assert X.shape[0] == Y.shape[0], "X and Y must have the same length along axis 0"
      assert X.ndim == Y.ndim == 2, "X and Y must be matrices"

    if Y is None:
      return X
    else:
      return X, Y

  def _compute_data_statistics(self):
    _, Y = self.simulate(n_samples=10**4)
    return np.mean(Y, axis=0), np.std(Y, axis=0)


import sys
import csv
import os

import utilities.common as utils

from spytest.st_time import get_timestamp
from spytest.datamap import DataMap

from .mail import send as email

slave_cols0 = ['#', 'Module', 'TestFunction', 'Result', 'TimeTaken',
              'ExecutedOn', 'Syslogs', 'FCLI', "TSSH", "DCNT", 'Description', "Devices"]
slave_cols1 = ['#', 'Feature', 'TestCase', 'Result',
              'ResultType', 'ExecutedOn', 'Description', 'Function', 'Module', "Devices"]
slave_cols2 = ['#', "Device", 'Module', 'TestFunction', 'LogMessage']
slave_cols3 = ["#", "Module", "Result", "Test Time", "INFRA Time (ms)", "CMD Time (ms)",
               "TG Time (ms)", "Wait(sec)", "TGWait(sec)", "PROMPT NFOUND", "Description"]

slave_cols = [slave_cols0, slave_cols1, slave_cols2, slave_cols3]
merge_cols0 = ['#', "Node", 'Module', 'TestFunction', 'Result',
              'TimeTaken', 'ExecutedOn', 'Syslogs', 'FCLI', "TSSH", "DCNT", 'Description', "Devices"]
merge_cols1 = ['#', "Node", 'Feature', 'TestCase', 'Result',
               'ResultType', 'ExecutedOn', 'Description', 'Function', 'Module', "Devices"]
merge_cols2 = ['#', "Node", "Device", 'Module', 'TestFunction', 'LogMessage']
merge_cols3 = ["#", "Node", "Module", "Result", "Test Time", "INFRA Time (ms)", "CMD Time (ms)",
                "TG Time (ms)", "Wait(sec)", "TGWait(sec)", "PROMPT NFOUND", "Description"]
merge_cols = [merge_cols0, merge_cols1, merge_cols2, merge_cols3]

class Result(object):

    def __init__(self, prefix, is_slave=True):
        self.csv_fd = [None, None, None]
        self.writer = [None, None, None]
        self.count  = [0, 0, 0]
        self.result = None
        self.desc = None
        self.default_result = None
        self.default_desc = None
        dmap = DataMap("messages")
        self.msgs = dmap.get()
        self.prefix = prefix
        self.report_csv = [None, None, None]
        self.report_csv[0] = "{}_result.csv".format(prefix)
        self.report_csv[1] = "{}_tcresult.csv".format(prefix)
        self.report_csv[2] = "{}_syslog.csv".format(prefix)
        if is_slave:
            self.open_csv(prefix, 0)
            self.open_csv(prefix, 1)
            self.open_csv(prefix, 2)

    def __del__(self):
        self.close_csv(0)

    def clear(self):
        self.result = None
        self.desc = None
        self.default_result = None
        self.default_desc = None

    def get(self):
        if not self.result:
            if not self.default_result:
                return ["ScriptError", "test case exited without setting any result"]
            return [self.default_result, self.default_desc]
        return [self.result, self.desc]

    def build_msg(self, name, *args):
        if name not in self.msgs:
            raise ValueError("unknown message identifier '{}'".format(name))

        s = self.msgs[name]
        try:
            rv = s.format(*args)
        except:
            raise ValueError("expected arguments not provided")

        return rv

    def set_default_error(self, res, code, *args):
        self.default_result = res
        try:
            if code:
                self.default_desc = self.build_msg(code, *args)
            else:
                self.default_desc = None
        except Exception as e:
            print(e)
            self.default_desc = "Invalid error code {} : {}".format(code, e)
            self.default_result = "Fail"
        return self.default_desc

    def set(self, res, code, *args):
        if self.result and self.result != "Pass":
            msg = "result already set to {} -- ignoring".format(self.result)
            print(msg)
            return msg
        self.result = res
        try:
            self.desc = self.build_msg(code, *args)
        except Exception as e:
            print(e)
            self.desc = "Invalid error code {} : {}".format(code, e)
            self.result = "Fail"
        return self.desc

    def _build_record(self, nodeid, func, tcid, time_taken, comp, result=None,
                      desc=None, rtype="Executed", index=0, syslog_count=0,
                      fcli=0, tryssh=0, dut_list=[]):
        self.count[index] = self.count[index] + 1
        result_def, desc_def = self.get()
        result = result_def if result is None else result
        desc = desc_def if desc is None else desc

        executedOn = get_timestamp(False)

        if comp:
            rcdict = {
                "#": self.count[index],
                "Feature": comp,
                "TestCase": tcid,
                "Result": result,
                "ExecutedOn": executedOn,
                "ResultType": rtype,
                "Description": desc,
                "Function": func,
                "Module": nodeid.split(':')[0],
                "Devices": " ".join(dut_list),
            }
        else:
            rcdict = {
                "#": self.count[index],
                "Module": nodeid.split(':')[0],
                "TestFunction": func,
                "Result": result,
                "ExecutedOn": executedOn,
                "TimeTaken": time_taken,
                "Syslogs": syslog_count,
                "FCLI": fcli,
                "TSSH": tryssh,
                "DCNT": len(dut_list),
                "Description": desc,
                "Devices": " ".join(dut_list),
            }
        return rcdict

    def publish(self, nodeid, func, tcid, time_taken, comp, result=None,
                desc=None, rtype="Executed", syslogs=None,
                fcli=0, tryssh=0, dut_list=[]):
        syslog_count = 0 if not syslogs else len(syslogs)
        index = 0 if rtype == "Executed" else 1
        rcdict = self._build_record(nodeid, func, tcid, time_taken, comp,
                                    result, desc, rtype, index, syslog_count,
                                    fcli, tryssh, dut_list)
        self.write_csv(rcdict, index)

        if not comp:
            index = 2
            for devname, msgtype, syslog in syslogs:
                func = func if func else msgtype
                self.count[index] = self.count[index] + 1
                d = {
                    "#": self.count[index],
                    "Device": devname,
                    "Module": nodeid.split(':')[0],
                    "TestFunction": func,
                    "LogMessage": syslog,
                    }
                self.write_csv(d, index)

        return rcdict

    def email(self, mailcfg, attachments=None, is_html=False):
        body = """
    Note: This is an automated mail sent by the SpyTest application. Please do not reply.
    """
        if is_html:
            body = mailcfg.body
        else:
            body = mailcfg.body + body
        subject = mailcfg.subject
        recipients = utils.split_byall(mailcfg.recipients)
        server = mailcfg.server
        if attachments is None:
            attachments = []
            attachments.append(self.report_csv[0])
            attachments.append(self.report_csv[1])
            attachments.append(self.report_csv[2])
        email(server, recipients, subject, body, attachments, is_html)

    def open_csv(self, prefix, index):
        report_csv = self.report_csv[index]
        if os.getenv("SPYTEST_RESULTS_PREFIX"):
            rows = Result.read_report_csv(report_csv)
        else:
            rows = []
        if sys.version_info.major < 3:
            csv_fd = open(report_csv, "wb")
        else:
            csv_fd = open(report_csv, "w", newline='')
        writer = csv.DictWriter(csv_fd, fieldnames=slave_cols[index],
                                     dialect="excel")
        self.csv_fd[index] = csv_fd
        if not rows:
            writer.writeheader()
        else:
            l_rows = Result.prepend_row_index(rows)
            utils.write_csv_writer(slave_cols[index], l_rows, writer)
            self.count[index] = len(rows)
        self.writer[index] = writer

    def close_csv(self, index):
        if self.csv_fd[index]:
            self.csv_fd[index].close()
            self.csv_fd[index] = None
        self.writer[index] = None

    def write_csv(self, rcdict, index):
        if self.writer[index]:
            self.writer[index].writerow(rcdict)
            self.csv_fd[index].flush()

    @staticmethod
    def read_report_csv(filepath, rmindex=True):
        rows = []
        try:
            with utils.open_file(filepath) as fd:
                for row in csv.reader(fd):
                    if row[0] != '#':
                        if rmindex:
                            row.pop(0)
                        rows.append(row)
        except:
            #print("failed to open {} to read".format(filepath))
            pass

        return rows

    @staticmethod
    def write_report_csv(filepath, rows, rtype, is_batch=True, append=False):
        if append:
            l_rows = rows
        else:
            l_rows = Result.prepend_row_index(rows)
        if is_batch:
            utils.write_csv_file(merge_cols[rtype], l_rows, filepath, append)
        else:
            utils.write_csv_file(slave_cols[rtype], l_rows, filepath, append)

    @staticmethod
    def prepend_row_index(rows):
        l_rows=[]
        for i,row in enumerate(rows):
            l_row = [i+1]
            l_row.extend(row)
            l_rows.append(l_row)
        return l_rows

    @staticmethod
    def write_report_png(filepath, rows, index):
        try:
            import matplotlib
            matplotlib.use('Agg')
            import matplotlib.pyplot as plt
            buckets = dict()
            all_colors = {
                "green":0, "red":0, "orange":0, "blue":0,
                "cyan":0, "olive":0, "sienna":0, "peru":0,
                "indigo":0, "magenta":0, "lightblue":0,
                "yellow":0, "salmon":0, "palegreen":0,
                "pink":0, "crimson":0, "lightpink":0
            }
            res_colors = {
                "Pass":"green", "DUTFail":"red", "Fail":"orange",
                "ScriptError":"blue", "EnvFail":"cyan", "DepFail":"olive",
                "ConfigFail":"sienna", "TopoFail":"peru", "TGenFail":"indigo",
                "Timeout":"magenta", "Skipped":"lightblue", "Unsupported": "yellow"
            }
            for row in rows:
                res = row[index]
                if not res:
                    continue
                if res not in buckets:
                    buckets[res] = 0
                buckets[res] = buckets[res] + 1
            labels = []
            colors = []
            for label in buckets:
                if label in res_colors:
                    color = res_colors[label]
                    all_colors[color]=1
                else:
                    for c,used in all_colors.items():
                        if not used:
                            color=c
                            all_colors[color]=1
                            break
                colors.append(color)
                labels.append("{} [{}]".format(label, buckets[label]))
            sizes = buckets.values()
            plt.pie(sizes, colors=colors, labels=labels, autopct='%1.1f%%', startangle=140)
            plt.axis('equal')
            plt.savefig(filepath)
            plt.clf()
        except Exception as e:
            print(e)

    @staticmethod
    def write_report_html(filepath, rows, rtype=0, is_batch=True, index=3):
        l_rows = Result.prepend_row_index(rows)
        if is_batch:
            utils.write_html_table(merge_cols[rtype], l_rows, filepath)
        else:
            utils.write_html_table(slave_cols[rtype], l_rows, filepath)
        if rtype in [0, 1]:
            png_file = os.path.splitext(filepath)[0]+'.png'
            Result.write_report_png(png_file, l_rows, index)

{'matplotlib': 'matplotlib', 'plt': 'matplotlib.pyplot'}.prepend_row_index

{'matplotlib': 'matplotlib', 'plt': 'matplotlib.pyplot'}.write_report_png


from game.player import EnemyPlayer, Player
from mahjong.constants import EAST, NORTH, SOUTH, WEST
from mahjong.tile import Tile, TilesConverter
from mahjong.utils import plus_dora
from utils.decisions_logger import MeldPrint
from utils.general import is_sangenpai

class Table:
    # our bot
    player = None
    # main bot + all other players
    players = None

    dora_indicators = None

    dealer_seat = 0
    round_number = -1
    round_wind_number = 0
    count_of_riichi_sticks = 0
    count_of_honba_sticks = 0

    count_of_remaining_tiles = 0
    count_of_players = 4

    meld_was_called = False

    # array of tiles in 34 format
    revealed_tiles = None
    revealed_tiles_136 = None

    # bot is playing mainly with ari-ari rules, so we can have them as default
    has_open_tanyao = True
    has_aka_dora = True

    def __init__(self, bot_config=None):
        self._init_players(bot_config)
        self.dora_indicators = []
        self.revealed_tiles = [0] * 34
        self.revealed_tiles_136 = []

    def __str__(self):
        dora_string = TilesConverter.to_one_line_string(
            self.dora_indicators, print_aka_dora=self.player.table.has_aka_dora
        )

        return "Wind: {}, Honba: {}, Dora Indicators: {}".format(
            self.round_wind_number, self.count_of_honba_sticks, dora_string
        )

    def init_round(
        self, round_wind_number, count_of_honba_sticks, count_of_riichi_sticks, dora_indicator, dealer_seat, scores
    ):

        # we need it to properly display log for each round
        self.round_number += 1

        self.meld_was_called = False
        self.dealer_seat = dealer_seat
        self.round_wind_number = round_wind_number
        self.count_of_honba_sticks = count_of_honba_sticks
        self.count_of_riichi_sticks = count_of_riichi_sticks

        self.revealed_tiles = [0] * 34
        self.revealed_tiles_136 = []

        self.dora_indicators = []
        self.add_dora_indicator(dora_indicator)

        # erase players state
        for player in self.players:
            player.erase_state()
            player.dealer_seat = dealer_seat
        self.set_players_scores(scores)

        # 136 - total count of tiles
        # 14 - tiles in dead wall
        # 13 - tiles in each player hand
        self.count_of_remaining_tiles = 136 - 14 - self.count_of_players * 13

        if round_wind_number == 0 and count_of_honba_sticks == 0:
            i = 0
            seats = [0, 1, 2, 3]
            for player in self.players:
                player.first_seat = seats[i - dealer_seat]
                i += 1

    def erase_state(self):
        self.dora_indicators = []
        self.revealed_tiles = [0] * 34
        self.revealed_tiles_136 = []

    def add_called_meld(self, player_seat, meld):
        self.meld_was_called = True

        # if meld was called from the other player, then we skip one draw from the wall
        if meld.opened:
            # but if it's an opened kan, player will get a tile from
            # a dead wall, so total number of tiles in the wall is the same
            # as if he just draws a tile
            if meld.type != MeldPrint.KAN and meld.type != meld.SHOUMINKAN:
                self.count_of_remaining_tiles += 1
        else:
            # can't have a pon or chi from the hand
            assert meld.type == MeldPrint.KAN or meld.type == meld.SHOUMINKAN
            # player draws additional tile from the wall in case of closed kan or shouminkan
            self.count_of_remaining_tiles -= 1

        self.get_player(player_seat).add_called_meld(meld)

        tiles = meld.tiles[:]
        # called tile was already added to revealed array
        # because it was called on the discard
        if meld.called_tile is not None:
            tiles.remove(meld.called_tile)

        # for shouminkan we already added 3 tiles
        if meld.type == meld.SHOUMINKAN:
            tiles = [meld.tiles[0]]

        for tile in tiles:
            self._add_revealed_tile(tile)

        for player in self.players:
            player.is_ippatsu = False

    def add_called_riichi_step_one(self, player_seat):
        """
        We need to mark player in riichi to properly defence against his riichi tile discard
        """
        player = self.get_player(player_seat)
        player.in_riichi = True

        # we had to check will we go for defence or not
        if player_seat != 0:
            self.player.enemy_called_riichi(player_seat)

    def add_called_riichi_step_two(self, player_seat):
        player = self.get_player(player_seat)

        if player.scores is not None:
            player.scores -= 1000

        self.count_of_riichi_sticks += 1

        player.is_ippatsu = True
        assert len(player.discards) >= 1, "Player had to have at least one discarded tile after riichi"
        latest_discard = player.discards[-1]
        latest_discard.riichi_discard = True
        player.riichi_tile_136 = latest_discard.value

        player.is_oikake_riichi = len([x for x in self.players if x.in_riichi]) > 1
        if not player.is_oikake_riichi:
            other_riichi_players = [x for x in self.players if x.in_riichi and x != player]
            player.is_oikake_riichi_against_dealer_riichi_threat = any([x.is_dealer for x in other_riichi_players])

        open_hand_threat = False
        for other_player in self.players:
            if other_player == player:
                continue

            for meld in other_player.melds:
                dora_number = 0
                if meld.type == MeldPrint.CHI:
                    continue

                for tile in meld.tiles:
                    dora_number += plus_dora(tile, self.dora_indicators, add_aka_dora=self.has_aka_dora)

                if dora_number >= 3:
                    open_hand_threat = True
        player.is_riichi_against_open_hand_threat = open_hand_threat

    def add_discarded_tile(self, player_seat, tile_136, is_tsumogiri):
        """
        :param player_seat:
        :param tile_136: 136 format tile
        :param is_tsumogiri: was tile discarded from hand or not
        """
        if player_seat != 0:
            self.count_of_remaining_tiles -= 1

        tile = Tile(tile_136, is_tsumogiri)
        tile.riichi_discard = False
        player = self.get_player(player_seat)
        player.add_discarded_tile(tile)

        self._add_revealed_tile(tile_136)

        player.is_ippatsu = False

    def add_dora_indicator(self, tile):
        self.dora_indicators.append(tile)
        self._add_revealed_tile(tile)

    def is_dora(self, tile):
        return plus_dora(tile, self.dora_indicators, add_aka_dora=self.has_aka_dora)

    def set_players_scores(self, scores, uma=None):
        for i in range(0, len(scores)):
            self.get_player(i).scores = scores[i] * 100

            if uma:
                self.get_player(i).uma = uma[i]

        self.recalculate_players_position()

    def recalculate_players_position(self):
        temp_players = self.get_players_sorted_by_scores()
        for i in range(0, len(temp_players)):
            temp_player = temp_players[i]
            self.get_player(temp_player.seat).position = i + 1

    def set_players_names_and_ranks(self, values):
        for x in range(0, len(values)):
            self.get_player(x).name = values[x]["name"]
            self.get_player(x).rank = values[x]["rank"]

    def get_player(self, player_seat):
        return self.players[player_seat]

    def get_players_sorted_by_scores(self):
        return sorted(self.players, key=lambda x: (x.scores or 0, -x.first_seat), reverse=True)

    @property
    def round_wind_tile(self):
        if self.round_wind_number < 4:
            return EAST
        elif 4 <= self.round_wind_number < 8:
            return SOUTH
        elif 8 <= self.round_wind_number < 12:
            return WEST
        else:
            return NORTH

    def is_common_yakuhai(self, tile_34):
        return is_sangenpai(tile_34) or tile_34 == self.round_wind_tile

    def _add_revealed_tile(self, tile):
        self.revealed_tiles_136.append(tile)
        tile_34 = tile // 4
        self.revealed_tiles[tile_34] += 1

        assert (
            self.revealed_tiles[tile_34] <= 4
        ), f"we have only 4 tiles in the game: {TilesConverter.to_one_line_string([tile])}"

    def _init_players(self, bot_config):
        self.player = Player(self, 0, self.dealer_seat, bot_config)

        self.players = [self.player]
        for seat in range(1, self.count_of_players):
            player = EnemyPlayer(self, seat, self.dealer_seat)
            self.players.append(player)


#This is my Pokemon Store!
import datetime
from time import sleep
from random import randint
print ('Hello! Welcome to The Pokemon Shop!')
sleep(1)

user_money = 100
def coupons():
    pokedoll_coupons = 0
    return pokedoll_coupons

prices_pokeballs = {
    'Pokeball': 5,
    'Ultra Ball': 12,
    'Master Ball': 10000,
    'Premier Ball': 10,
    'Nest Ball': 15,
    'Timer Ball': 15,
    'Heal Ball': 50,
    'Great Ball': 10,
    'Luxary Ball': 20
    }
stock_pokeballs = {
    'Pokeball': 342,
    'Ultra Ball': 0,
    'Master Ball': 6,
    'Premier Ball': 58,
    'Nest Ball': 0,
    'Timer Ball': 211,
    'Heal Ball': 109,
    'Great Ball': 2,
    'Luxary Ball': 35,
    }

j = 'Darkrai'
j_fact = 'Darkrai\'s type is Dark!'
j_evolution = 'Darkrai doesn\'t have any evolutions.'
f = 'Pikachu'
f_fact = 'Pikachu\'s type is electric!'
f_evolution = 'It\'s the second evolution of Pichu, and it\'s third is Raichu!'
m = 'Celebi'
m_fact = 'Celebi\'s type is psychic and grass!'
m_evolution = 'Celebi doesn\'t have any evoutions.'
a = 'Deoxys'
a_fact = 'Deoxys\' type is psychic!'
a_evolution = 'Deoxys does not evolve, but can take 5 different forms!'
ma = 'Giratina'
ma_fact = 'Giratina\'s type is dragon and ghost!'
ma_evolution = 'Giratina does not evolve, but it can take 2 different forms!'
ju = 'Mimikyu'
ju_fact = 'Mimikyu is a ghost and fairy Pokemon, though other information is being discovered still!'
ju_evolution = 'Mimikyu has no known evolutions or other forms.'
jul = 'Snorlax'
jul_fact = 'Snorlax\'s type is normal.'
jul_evolution = 'Snorlax is the second evolution of Munchlax!'
au = 'Jirachi'
au_fact = 'Jirachi\'s type is psychic and steel!'
au_evolution = 'It does not take any other forms or evolutions.'
s = 'Phione'
s_fact = 'Phione is a water type Pokemon!'
s_evolution = 'Though Phione doesn\'t have any other evolutions, it can be bred through a Manaphy or another Phione.'
o = 'Lickytung'
o_fact = 'It\'s type is normal!'
o_evolution = 'It is the first evolution of Lickylicky!'
n = 'Meowth'
n_fact = 'Meowth\'s type is normal!'
n_evolution = 'It is the first evolution or Persian!'
d = 'Mew'
d_fact = 'Mew\'s type is psychic!'
d_evolution = 'Mew doesn\'t have any evolutions, but it is said to be related to Mewtwo!'

pokedolls = {
    'Mew',
    'Jigglypuff',
    'Snorlax',
    'Mimikyu',
    'Charmander'
    }

def pokeballs1():
    print('. . .')
    print ('Welcome to the Pokeball Section!')
    message = input('Would you like to see our prices? ')
    if message.lower() == 'yes':
        for item in prices_pokeballs:
            print (((item)+': ')+('$'+str(prices_pokeballs[item])))
    elif message.lower() == 'no':
        print ('Ah ok I see.')
    else:
        print ('It was a yes or no question!')
        return pokeballs1()
def pokeballs2():
    message2 = input('Would you like to see what we have in stock today? ')
    if message2.lower() == 'yes':
        for item in stock_pokeballs:
            print (((item)+': ')+(str(stock_pokeballs[item])))
        cart = []
        q1 = input('Would you like to add anything to your cart? ')
        if q1.lower() == 'yes':
            q2 = input('What would you like to add? ')
            cart.append(q2)
            print ('Your cart has: ')
            for item in cart:
                print (item)
        else:
            print ('Ok! Come back to Pokeballs soon :)')
    elif message2.lower() == 'no':
        print ('Ah ok I see.')
    else:
        print ('It was a yes or no question!')
        return pokeballs2()

def pokemon_monthly(month):
    print ('. . .')
    print ('Welcome to Pokemon Monthly!')
    sleep(1)
    print ('. . .')
    message_month2 = ('This month our famous pokemon is: ')
    if month == 1:
        print ('The month is January!')
        print ((message_month2)+(j)+'!')
        print (j_fact)
        print (j_evolution)
    if month == 2:
        print('The month is February!')
        print ((message_month2)+(f)+'!')
        print (f_fact)
        print (f_evolution)
    if month == 3:
        print ('The month is March!')
        print ((message_month2)+(m)+'!')
        print (m_fact)
        print (m_evolution)
    if month == 4:
        print ('The month is April!')
        print ((message_month2)+(a)+'!')
        print (a_fact)
        print (a_evolution)
    if month == 5:
        print ('The month is May!')
        print ((message_month2)+(ma)+'!')
        print (ma_fact)
        print (ma_evolution)
    if month == 6:
        print ('The month is June!')
        print ((message_month2)+(ju)+'!')
        print (ju_fact)
        print (ju_evolution)
    if month == 7:
        print ('The month is July!')
        print ((message_month2)+(jul)+'!')
        print (jul_fact)
        print (jul_evolution)
    if month == 8:
        print ('The month is August!')
        print ((message_month2)+(au)+'!')
        print (au_fact)
        print (au_evolution)
    if month == 9:
        print ('The month is September!')
        print ((message_month2)+(s)+'!')
        print (s_fact)
        print (s_evolution)
    if month == 10:
        print ('The month is October!')
        print ((message_month2)+(o)+'!')
        print (o_fact)
        print (o_evolution)
    if month == 11:
        print ('The month is November!')
        print ((message_month2)+(n)+'!')
        print (n_fact)
        print (n_evolution)
    if month == 12:
        print ('The month is December!')
        print ((message_month2)+(d)+'!')
        print (d_fact)
        print (d_evolution)

def pokequiz():
    correct = 0
    print ('. . .')
    print ('This is the Pokemon Quiz!')
    print ('You will answer 3 questions!')
    print ('. . .')
    sleep(1)
    print ('QUESTION 1')
    sleep(1)
    print ('What is not a region in the Pokemon world?')
    print ('a. Unova')
    print ('b. Johto')
    print ('c. Sinnoh')
    print ('d. Plento')
    a1 = input('Put your answer here: ')
    if a1 == 'd':
        print ('Correct!')
        correct += +1
    else:
        print ('Incorrect.')
    print ('. . .')
    sleep(1)
    print ('QUESTION 2')
    sleep(1)
    print ('A pokemon only goes up to level 99.')
    print ('a. True')
    print ('b. False')
    print ('c. Neither')
    a2 = input('Put your answer here: ')
    if a2 == 'b':
        print ('Correct!')
        correct += +1
    else:
        print ('Incorrect.')
    print ('. . .')
    sleep(1)
    print ('QUESTION 3')
    print ('Which Pokemon is a legendary?')
    print ('a. Rotom')
    print ('b. Snorlax')
    print ('c. Mew')
    print ('d. Phione')
    a3 = input('Put your answer here: ')
    if a3 == 'c':
        print ('Correct!')
        correct += +1
    else:
        print ('Incorrect.')
    print ('. . .')
    sleep(1)
    print ('You got '+str(correct)+' question(s) correct out of 3.')

def pokemon_guess():
    def user_guess():
        guess1 = int(input('Guess a number 1 through 10: '))
        return guess1
    print ('. . .')
    print ('Welcome to the Pokemon Guess game!')
    guess = user_guess()
    roll = randint(1,10)
    if guess > 10:
        print ('You\'re guess was too high!')
        return pokemon_guess()
    else:
        print ('Loading Number. . .')
        sleep(1)
        print (roll)
        sleep(1)
        if roll == guess:
            print ('You\'ve won!')
            print ('You get a free Pokemon Doll!')
            print ('So you may go to the Pokedoll shop and claim your prize!')
        elif roll != guess:
            print ('You\'ve lost.')
            def guess_again():
                message = input('Would you like to play again? ')
                if message == 'yes':
                    return pokemon_guess()
                elif message == 'no':
                    print ('Ok! Come back again soon!')
                else:
                    print ('It\'s a Yes or No question!')
                    return guess_again()
            guess_again()

def pokedoll():
    print ('. . .')
    sleep(1)
    print ('Hello! Welcome to the Pokedoll shop!')
    question = input('Would you like to see what we have? ')
    if question.lower() == 'yes':
        print ('Ok! We have:')
        sleep(1)
        for item in pokedolls:
            print (item)
        cart = []
        q1 = input('Would you like to add anything to your cart? ')
        if q1.lower() == 'yes':
            q2 = input('What would you like to add? ')
            cart.append(q2)
            print ('. . .')
            print ('Ok! Your cart has: ')
            for item in cart:
                print (item+' doll')
        elif q1.lower() == 'no':
            print ('Ok! Come back to Pokedolls soon :)')
        else:
            print ('That wasn\'t a yes or no!')
            return pokedoll()
    elif question.lower() == 'no':
        print ('Ah ok I see.')
    else:
        print ('That was not a yes or a no!')
        return pokedoll()

def end():
    print ('. . .')
    message2 = input('Is there anything else you would like to see? ')
    if message2 == 'yes':
        return step_1()
    elif message2 == 'no':
        print ('Ok! Have a good day, come back soon :)')
    else:
        print ('Please answer with Yes or No!')
        return end()
today = datetime.date.today()
def step_1():
    print ('. . .')
    print ('Our store has Pokeballs, Pokemon Monthly, Pokedolls, The Pokemon Quiz, and Pokemon Guess!')
    message = input('Which of our sections would you like to see? ')
    if message.lower() == 'pokeballs':
        pokeballs1()
        sleep(.5)
        pokeballs2()
        end()
    elif message.lower() == 'the pokemon quiz':
        pokequiz()
        end()
    elif message.lower() == 'pokedolls':
        pokedoll()
        end()
    elif message.lower() == 'pokemon monthly':
        pokemon_monthly(today.month)
        end()
    elif message.lower() == 'pokemon guess':
        pokemon_guess()
        end()
    else:
        print ('That is not on our list!')
        return step_1()

step_1()
outro = input('PRESS ENTER TO CLOSE')


import MetaTrader5 as  mt5
import pandas as pd
import pytz
from datetime import datetime
import json
from backtesting import Backtest, Strategy
from backtesting.lib import resample_apply

# from TestingStrategy.backtestingStrategy import StrategyOne
from TestingStrategy.backtestingStrategy import MyStrategy

with open('botSettings.json') as config_file:
    config = json.load(config_file)

dateFrom = config['utc_from']
dateTo = config['utc_to']

def GetDataForBackTesting():
    #Set Timezone
    timezone = pytz.timezone("US/Eastern")
    utc_from = timezone.localize(datetime(*dateFrom)).astimezone(pytz.utc)
    utc_to = timezone.localize(datetime(*dateTo)).astimezone(pytz.utc)

    timestamp_from = int(utc_from.timestamp())
    timestamp_to = int(utc_to.timestamp())

    quarter_mins_data = pd.DataFrame(GetRatesQuarterMins(timestamp_from,timestamp_to))
    quarter_mins_data['open'] = pd.to_numeric(quarter_mins_data['open'], errors='coerce')

    # quarter_mins_data['open'] = pd.to_numeric()
    quarter_mins_data['time']=pd.to_datetime(quarter_mins_data['time'], unit='s')

    return quarter_mins_data

def StartBackTesting(quarterMinsData):
    # Print the provided 15-minute data
    print(quarterMinsData)
    print(type(quarterMinsData))
    # strategy = MyStrategy(broker= any,data= quarterMinsData, params=any)
    # strategy_params = {
    #     'broker': 'MetaQuotes-Demo',
    #     'data': quarterMinsData,
    #     'params': {}
    # }
    # strategy = MyStrategy(**strategy_params)

    # Now you can use the 'strategy' instance to run your backtest or perform other actions
    # For example, you can create a Backtest instance and run it using the 'strategy'

    # Define column names
    columns = ['time','open', 'high', 'low', 'close', 'tick_volume']
    # Convert quarterMinsData to pandas DataFrame
    df = pd.DataFrame(quarterMinsData, columns=columns)
    #mapping the dataframe
    # to do add time to the mapping as well
    df = df.rename(columns={
                                'open': 'Open',
                                'high': 'High',
                                'low': 'Low',
                                'close': 'Close',
                                'tick_volume': 'Volume'
                            })
    bt = Backtest(data = df, strategy= MyStrategy)
    result = bt.run()

    # Print the results
    print(result)

def GetRatesHourly(dateFrom,utcTo):
    hourly_rates = mt5.copy_rates_range("GBPJPY", mt5.TIMEFRAME_H1, dateFrom, utcTo)
    hourly_rates_frame = pd.DataFrame(hourly_rates)
    hourly_rates_frame['time']=pd.to_datetime(hourly_rates_frame['time'], unit='s')
    #data preparation
    selected_columns_hourly_rates_frame = hourly_rates_frame[['time', 'open', 'high', 'low', 'tick_volume']]
    return selected_columns_hourly_rates_frame

def GetRatesQuarterMins(dateFrom,utcTo):
    #data rates of 15 minutes candle
    quarter_mins_rates = mt5.copy_rates_range("GBPJPY", mt5.TIMEFRAME_M15, dateFrom, utcTo)
    quarter_mins_rates = pd.DataFrame(quarter_mins_rates)
    quarter_mins_rates['time']=pd.to_datetime(quarter_mins_rates['time'], unit='s')
    print("\nDisplay dataframe with data for 15 minutes candle")
    selected_columns_quarter_mins_rates = quarter_mins_rates[['time', 'open', 'high', 'low','close','tick_volume']]
    return selected_columns_quarter_mins_rates


import os, sys
import shutil
from functools import partial

def makefolders(root_dir, folders):
    for folder in folders:print('CREATE '+folder)
    concat_path = partial(os.path.join, root_dir)
    makedirs = partial(os.makedirs, exist_ok=True)  # Python 3.2+
    for path in map(concat_path, folders):
        makedirs(path)

def movefolders(folders_to_move):
    print('MOVE ' + folders_to_move[0], folders_to_move[1])
    shutil.move(folders_to_move[0], folders_to_move[1])

def deletefolder(folder_to_delete):
    print('DELETE '+folder_to_delete)
    try:
        shutil.rmtree(folder_to_delete)
    except OSError as err:
        # print("OS error: {0}".format(err))
        print('Cannot delete', folder_to_delete, ' - ', folder_to_delete.split("/")[0], 'does not exist')

def list_files(startpath):
    print('LIST')
    for root, dirs, files in sorted(os.walk(startpath)):
        level = root.replace(startpath, '').count(os.sep)
        indent = ' ' * (level)
        if level>0:
            print('{}{}'.format(indent, os.path.basename(root)))

        # WON'T WORK IN ORDER
        # subindent = ' ' * 2 * (level + 0)
        # for d in sorted(dirs):
            # print('')
            # print('{}{}'.format(subindent, d))

if __name__=='__main__':
    root_dir = sys.argv[1]
    # root_dir = os.getcwd()
    folder_list = [['fruits','vegetables','grains','fruits/apples','fruits/apples/fuji'],
        ['grains/squash'],['foods']]
    moves = [['grains/squash', 'vegetables'],['grains', 'foods'],
        ['fruits', 'foods'],['vegetables', 'foods']]
    deletes = ['fruits/apples','foods/fruits/apples']
    makefolders(root_dir, folder_list[0])
    list_files(root_dir)
    makefolders(root_dir, folder_list[1])
    movefolders(moves[0])
    makefolders(root_dir, folder_list[2])
    movefolders(moves[1])
    movefolders(moves[2])
    movefolders(moves[3])
    list_files(root_dir)
    # deletefolder(deletes)
    for i in range(len(deletes)):
        deletefolder(deletes[i])
    list_files(root_dir)


from transformers import pipeline
from transformers import AutoModelForSequenceClassification
from transformers import BertJapaneseTokenizer
import random

# from huggingface_hub import snapshot_download
# download_path = snapshot_download(repo_id="daigo/bert-base-japanese-sentiment")

# print(download_path)
# dir_name = '/workspaces/fastapi/model'

# # パイプラインの準備
# model = AutoModelForSequenceClassification.from_pretrained(dir_name)
# tokenizer = BertJapaneseTokenizer.from_pretrained(dir_name)

# #モデルのダウンロード
# model.save_pretrained(dir_name)
# tokenizer.save_pretrained(dir_name)

# nlp = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer)

# マンガの台詞
# manga_list = ["“海賊王”に！！！おれはなるっ！！！！",
#               "あきらめたらそこで試合終了ですよ……？",
#               "計画通り",
#               "お前はもう 死んでいる",
#               "生殺与奪の権を他人に握らせるな！",
#               "さすがディオ！ おれたちにできない事を平然とやってのける そこにシビれる！ あこがれるゥ！",
#               "我がドイツの医学薬学は世界一ィィィ！",
#               "『てめーは、おれを怒らせた』",
#               "だが断る",
#               "吐き気をもよおす『邪悪』とはッ！ なにも知らぬ無知なる者を利用する事だ……!! 自分の利益だけのために利用する事だ… 父親がなにも知らぬ『娘』を!! てめーだけの都合でッ！ 許さねえッ！ あんたは今 再びッ！ オレの心を『裏切った』ッ！"]

# 感情分析の実行
# for manga in manga_list:
#     print(nlp(manga))

# # 感情分析の実行
# for manga in manga_list:
#     print(nlp(manga))

class NegaPosi:
    def __init__(self):
        dir_name = '/workspaces/fastapi/model'
        self.model = AutoModelForSequenceClassification.from_pretrained(dir_name)
        self.tokenizer = BertJapaneseTokenizer.from_pretrained(dir_name)

    def reply(self, input: str):
        nlp = pipeline("sentiment-analysis", model=self.model, tokenizer=self.tokenizer)
        negaposi = nlp(input)[0]["label"]
        if negaposi == "ネガティブ":
            rep_list = ["わかる、つらいよね", "うんうん、"]
        else:
            text = "いいね！"
        text = random.choice()
        return text


import time
import numpy as np
import streamlit as st

st.write("streamlit version = {}".format(st.__version__))
st.latex(r''' e^{i\pi} + 1 = 0 ''')

#barra de progeso
# se coloca la barra de progreso siempre del lado izquierdo
progress_bar = st.sidebar.progress(0)
# se establece la barra de progeso vacía
status_text = st.sidebar.empty()

last_rows = np.random.randn(1, 1)

#aquí se crea el gráfico de lineas
chart = st.line_chart(last_rows)

for i in range(1,51):
    new_rows = last_rows[-1 , :] + np.random.randn(5,1).cumsum(axis=0)
    #barra de estatus de la barra de progreso
    status_text.text("%i%% Complete" %i)
    #actualización de la barra de progreso
    progress_bar.progress(i)
    #añadiendo los nuevos registros  al gráfico
    chart.add_rows(new_rows)
    #la siguiente linea es para acumular los valores actualizados
    last_rows = new_rows

    time.sleep(0.1)

#reseteando la barra d progreso
progress_bar.empty()
# boton para re ejecutar
st.button('correr')

# para ejecutar esto, desde la consola hacer
# streamlit run 'nombre.py'


DEFAULT_CONTEXT = 'ctx'
class Canvas(object):
    def __init__(self,script,cid='mytag', context=None):
        self.cid=cid
        self.context = context if context else DEFAULT_CONTEXT
        self.wrap =  context is None
        self._html =[]
        self._add_canvas(cid)
        self._get_canvas()
        self._add_script_tag(script)

    def _add_canvas(self, cid):
        html='''<canvas id="{}" width="700" height="410">
Some default content can appear here.

</canvas>'''.format(cid)
        self._html.append(html)

    def _get_canvas(self):
        script = ''' '''.format(cid = self.cid, ctx=self.context)
        self._html.append(script)

    def _add_script_tag(self, script):
        if self.wrap:
            self._html.append(''.format(self.context,script))
        else:
            self._html.append(''.format(script))

    def html(self):
        return '\n'.join(self._html)

    def _repr_html_(self):
        return self.html()

import random
import string
import shlex
from argparse import ArgumentParser
from IPython.core.magic import (
    magics_class, line_cell_magic, Magics)
from IPython.core.display import HTML

@magics_class
class CanvasMagic(Magics):
    def __init__(self, shell, cache_display_data=False):
        super(CanvasMagic, self).__init__(shell)
        self.cache_display_data = cache_display_data

    @line_cell_magic
    def canvas(self,line, cell=''):
        '''Run JS canvas commands.'''
        parser = ArgumentParser()
        parser.add_argument('-c', '--context', default=None)
        parser.add_argument('-I', '--id', default=None)
        parser.add_argument('--wrap', dest='wrap_env', action='store_true')
        parser.add_argument('-v', '--variable', default=None)
        parser.set_defaults(wrap_env=False)
        args = parser.parse_args(shlex.split(line))

        if args.variable:
            cell = self.shell.user_ns[args.variable]

        #Create a random id for the canvas tag to try to prevent clashes
        argsid = args.id if args.id else ''.join(random.choices(string.ascii_uppercase + string.digits, k=5))

        if not args.context:
            if not args.wrap_env: context = None
            else: context=DEFAULT_CONTEXT
        else: context = args.context
        #We need to add the extra \n in case the last line in the cell is a comment that would comment out </script>
        return Canvas(cell+'\n', context=context, cid=argsid)

#ip = get_ipython()
#ip.register_magics(CanvasMagic)


#!/usr/bin/env python3

import pygal

def print_fa_data(fa,name):
	x_time = []
	out_octets = []
	out_packets = []
	in_octets = []
	in_packets = []
	with open('static/'+name+fa+'.txt', 'r') as f:
	    for line in f.readlines():
	        # eval(line) lee cada linea como diccionario no como cadena
	        line = eval(line)
	        t = line['time']
	        x_time.append(t[-15:-5])
	        out_packets.append(float(line['fa_out_uPackets']))
	        out_octets.append(float(line['fa_out_oct']))
	        in_packets.append(float(line['fa_in_uPackets']))
	        in_octets.append(float(line['fa_in_oct']))

	line_chart = pygal.Line()
	line_chart.title = name +' '+fa
	line_chart.x_labels = x_time
	line_chart.add('Oct. salida', out_octets)
	line_chart.add('Paq. salida', out_packets)
	line_chart.add('Oct. entrada', in_octets)
	line_chart.add('Paq. entrada', in_packets)
	line_chart.render_to_png('static/'+name+fa+'img.png')


import asyncio
import sys
import time

import traceback
import aiohttp
import aioredis
from waifustream import danbooru, index
from waifustream.index import IndexEntry

task_sem = asyncio.Semaphore(value=10)
n_complete = 0

async def index_one(redis, sess, post):
    global task_sem, n_complete

    if post.url is None:
        return 0

    async with task_sem:
        t1 = time.perf_counter()

        img = await post.fetch(sess)
        t2 = time.perf_counter()

        imhash = index.combined_hash(img)
        t3 = time.perf_counter()

        img.close()
        entry = IndexEntry.from_danbooru_post(imhash, post)
        inserted = await entry.add_to_index(redis)
        t4 = time.perf_counter()

        total_time = t4 - t1
        fetch_time = t2 - t1
        hash_time = t3 - t2
        index_time = t4 - t3

        n_complete += 1

        if inserted:
            print("Indexed post {} (#{}) - times: {:.4f} / {:.4f} / {:.4f} (total {:.4f} seconds)".format(post.id, n_complete, fetch_time, hash_time, index_time, total_time))
            return 1
        else:
            print("Post {} already in index (#{}) - times: {:.4f} / {:.4f} / {:.4f} (total {:.4f} seconds)".format(post.id, n_complete, fetch_time, hash_time, index_time, total_time))
            return 0

    return 0

async def main():
    character = sys.argv[1]

    futs = []

    t_start = time.perf_counter()

    redis = await aioredis.create_redis('redis://localhost')
    async with aiohttp.ClientSession() as sess:
        async for post in danbooru.search(sess, [character], index.exclude_tags):
            futs.append(asyncio.ensure_future(index_one(redis, sess, post)))

        print("=== waiting for {} tasks to return ===".format(len(futs)))
        res = await asyncio.gather(*futs)

    t_end = time.perf_counter()

    n_success = sum(res)
    n_total = len(res)

    print("Indexed {} / {} images in {:.4f} seconds".format(n_success, n_total, t_end - t_start))

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())


import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt

import pandas as pd
import numpy as np
from scipy.stats import hypergeom
import random
import sys

from scipy.integrate import simpson

plt.rcParams.update({'font.size': 24})

class StepwiseHypergeom:

    def __init__(self, full_ranking_files, external_successes_file, bucket_size, bucket='', mol_type='', full_ranking_top_perc=-1, successes_top_perc=-1, max_x_lim=-1, max_y_lim=-1):

        """
            This class calculates the stepwise hypergeometric enrichment between a full ranked list of genes and a set of external files containing ranked 'successes'.

            - full_ranking_file:
                full reference gene ranking; file should be provided as a single-column ranked list of genes (withouth column header)

            - external_successes_files:
                list of external files containing the 'successes'; each file should be provided as a single-column ranked list of genes (withouth column header)

            - full_ranking_top_perc:
                top percentage of full_ranking_file's hits to restrict the enrichment analysis into (e.g 0.05); if -1, the full list is used

            - successes_top_perc:
                top percentage of each of external_successes_files' hits to restrict the enrichment analysis into (e.g 0.05); if -1, the full list is used

            - max_x_lim:
                x-axis limit for the hypergeometric enrichment plots

            - max_y_lim:
                y-axis limit for the hypergeometric enrichment plots
        """

        #self.full_ranking_df = self.read_full_ranking(full_ranking_file)
        self.full_ranking_files = full_ranking_files

        self.external_successes = pd.read_csv(external_successes_file, header=None)

        self.full_ranking_top_perc = full_ranking_top_perc
        self.successes_top_perc = successes_top_perc

        self.bucket_size = bucket_size
        self.bucket = bucket
        self.mol_type = mol_type

        # plotting parameters
        self.figure_name = 'both'
        self.max_x_lim = max_x_lim
        self.max_y_lim = max_y_lim

    def calc_phred_score(self, pval):
        phred = None

        # Set an upper cap of 500 for all Phred scores
        if pval == 0:
            phred = 500
        else:
            phred = -10 * np.log10(pval)
            if phred > 500:
                phred = 500

        return phred

    def read_full_ranking(self, full_ranking_file):

        full_ranking_df = pd.read_csv(full_ranking_file, header=None)
        full_ranking_df.columns = ['Gene_Name']
        full_ranking_df['full_ranking'] = range(1, full_ranking_df.shape[0]+1)

        return full_ranking_df

    def calc_hypergeom_pvals(self, full_ranking_df):

        external_successes = self.external_successes.copy()
        # external top predictions
        external_successes.columns = ['Gene_Name']
        #print("\n external ranking:", external_successes.head())
        #print(external_successes.shape)

        # Subset top external hits to then overlap with the full rankings df
        if self.successes_top_perc != -1:
            #external_successes = external_successes.iloc[:int(external_successes.shape[0] * self.successes_top_perc), :]
            external_successes = external_successes.iloc[:self.bucket_size, :]
        external_top_genes = external_successes['Gene_Name'].tolist()
        print('- Total number of Successes:', len(external_top_genes))

        # Restrict full ranking to top perc results if 'full_ranking_top_perc' is specified
        if self.full_ranking_top_perc != -1:
            #print(full_ranking_df.shape)
            #full_ranking_df = full_ranking_df.loc[:int(full_ranking_df.shape[0] * self.full_ranking_top_perc)]
            full_ranking_df = full_ranking_df.iloc[:992]
            #print(full_ranking_df.shape)
            print('- Restrict full ranking to top % results: ' + str(int(self.full_ranking_top_perc*100)) + '% (' + str(full_ranking_df.shape[0]) + ')' )

        M = full_ranking_df.shape[0]
        print('- Population Size:', M)
        #print('\n Full ranking:\n', full_ranking_df.head())

        full_ranking_df = full_ranking_df.loc[full_ranking_df['Gene_Name'].isin(external_top_genes)]
        full_ranking_df.reset_index(drop=True, inplace=True)
        #print('\n', full_ranking_df.head())
        #print('\n', full_ranking_df.tail())

        n = full_ranking_df.shape[0]
        print('\nTotal number of Successes in Full ranking (overlap):', n)

        # ************* Hypergeometric Test *************
        hypergeom_pvals = []
        hypergeom_ordered_genes = []

        for x in range(full_ranking_df.shape[0]):

            #print(full_ranking_df.iloc[x, 0])

            N = full_ranking_df.iloc[x, full_ranking_df.shape[1]-1]
            #print(x, N)

            cur_pval = hypergeom.sf(x - 1, M, n, N)
            #print(cur_pval)

            hypergeom_pvals = hypergeom_pvals + [cur_pval]

            cur_gene = full_ranking_df.loc[x, 'Gene_Name']
            hypergeom_ordered_genes = hypergeom_ordered_genes + [cur_gene]
        # ***********************************************

        #min_pval = min(hypergeom_pvals)
        hypergeom_pvals = [self.calc_phred_score(pval) for pval in hypergeom_pvals]
        print('\nMax phred:', max(hypergeom_pvals))
        print('Avg phred:', np.mean(hypergeom_pvals))

        return hypergeom_pvals

    def plot_hypergeom_stepwise(self, hypergeom_pvals, ax, label, color='#33a02c', signif_thres=0.05, plot_pval_thres=True):

        if plot_pval_thres:
            signif_thres = self.calc_phred_score(signif_thres)
            ax.axhline(y=signif_thres, linestyle='--', color='red', label='p-val: 0.05')

        ax.plot(hypergeom_pvals, color=color,
                label=label,
                linewidth=1)

        ax.set_xlim(left=-0.5)

        y_label = 'Phred score from hypergeometric test\n(significance increasing in positive direction)'
        ax.set_ylabel(y_label, fontsize=14)

        ax.legend(bbox_to_anchor=(1.32, 1), fontsize=12, loc='upper right', framealpha =0.6)
        for line in ax.legend().get_lines():
            line.set_linewidth(2.0)

    def get_area_under_curve(self,yvals):
        # - Calc. AUC based on Simpson's area / trapezoid area
        # - Restrict area above the line: y=phred(0.05)

        phred = self.calc_phred_score(0.05)
        yvals = np.array(yvals)

        diff = yvals - phred
        curve_values = np.maximum(diff, 0) #negative values are replaced with 0
        curve_auc = simpson(curve_values, dx=1)

        return curve_auc

    def main(self):
        print("hello")
        hypergeom_results = {}

        # Calculate hypergeometric enrichments for each external successes file
        for f in self.full_ranking_files:
            #print(f)
            full_ranking_df = self.read_full_ranking(f)
            ranking_file = f.split('/')[-1]
            print('\n\n\n> ', ranking_file + ':', full_ranking_df.shape)

            hypergeom_results[ranking_file] = self.calc_hypergeom_pvals(full_ranking_df)

        # # Calculate hypergeometric enrichments against a random gene ranking
        random_full_ranking = full_ranking_df.sample(frac=1, random_state=0)
        random_full_ranking.reset_index(drop=True, inplace=True)
        random_full_ranking['full_ranking'] = range(1, full_ranking_df.shape[0]+1)
        #print(random_full_ranking.head())
        print('\n\n\n> Random:', random_full_ranking.shape)
        random_hypergeom_pvals = self.calc_hypergeom_pvals(random_full_ranking)

        print(sorted(hypergeom_results, key=lambda k: len(hypergeom_results[k]), reverse=True))

        # ------------------------- Start plotting ----------------------------
        print('\n> Plotting hypergeom. enrichment plots...')
        fig, ax = plt.subplots(figsize=(12, 8))

        colors = ['#2171b5', '#238b45', '#fe9929', '#fff7bc']
        cnt = 0
        for external_dataset in sorted(hypergeom_results, key=lambda k: len(hypergeom_results[k]), reverse=True):  #hypergeom_results.items():

            hypergeom_pvals = hypergeom_results[external_dataset]

            #calculate area under curve
            auc = self.get_area_under_curve(hypergeom_pvals)
            print(external_dataset+" auc= ",auc)

            self.plot_hypergeom_stepwise(hypergeom_pvals, ax, label=external_dataset.split('.')[0], color=colors[cnt], plot_pval_thres=False)
            cnt += 1

        self.plot_hypergeom_stepwise(random_hypergeom_pvals, ax, label='random', color='black')

        # Set axis limits
        if self.max_x_lim != -1:
            ax.set_xlim(0, self.max_x_lim)
        else:
            ax.set_xlim(0, max(len(l) for k, l in hypergeom_results.items()))
        if self.max_y_lim != -1:
            ax.set_ylim(0, self.max_y_lim)

        # Define filename top perc. identifiers
        if self.successes_top_perc == -1:
            success_top_str = 'all'
        else:
            success_top_str = 'top' + str(int(self.successes_top_perc * 100)) + 'perc'

        if self.full_ranking_top_perc == -1:
            full_top_str = 'all'
        else:
            full_top_str = 'top' + str(int(self.full_ranking_top_perc * 100)) + 'perc'

        if self.mol_type == 'Kings':
            plt.title('Kings et al')
        else:
            plt.title(self.mol_type + ' ' + self.bucket)

        # Save fig to pdf
        fig.savefig(self.mol_type+'_External_' + self.bucket + '-vs-Full_ranking_' + self.figure_name + '_' + full_top_str + '.pdf', bbox_inches='tight')
        fig.savefig(self.mol_type+'_External_' + self.bucket + '-vs-Full_ranking_' + self.figure_name + '_' + full_top_str + '.png', bbox_inches='tight')
        plt.close()

        print('\n> Done.')


import speech_recognition as sr
import os
import serial
from time import sleep

rec = sr.Recognizer()  # starts voice recognition

def listen():
    with sr.Microphone() as source:  # simplifies to source
        audio = rec.listen(source)  # listens to microphone
        voice = ""  # nothing was said
        try:
            voice = rec.recognize_google(audio)  # uses google for speech-to-text
        except:
            print("WHAT?")  # prints this if nothing is heard
    print("You said: ", voice)  # prints what the microphone heard
    return voice

if __name__ == '__main__':
    ser = serial.Serial('COM4', 9600)   # starts serial com
    sleep(2)    # waits 2 seconds

    while True:
        comm = listen()     # calls function to get what was said
        if comm == 'on':
            ser.write(b'H')     # sends encoded 'H' to serial
        elif comm == 'off':
            ser.write(b'L')     # sends encoded 'H' to serial
    ser.close()     # cleanup


from asyncio import futures
import torch

import numpy as np

import math
import utils.encoding as encoding
import utils.utils as ut

def randomRayt(near,far,numSam=64,ifrand=True):
    tLayer=torch.linspace(near,far,numSam,device=torch.device('cpu'))
    if ifrand:
        rayT=tLayer+torch.rand(numSam,device=torch.device('cpu'))*((far-near)/numSam)
    else:
        rayT=tLayer
    return rayT

## compute coast points in cpu
## rayO [H,W,3] rayD [H,W,3]
## return flatPoints [H*W*numSam,3] flatrayd [H*W*numSam,3] rayT [numSam] H W
def getcoastpoints(rayT,rayO,rayD):

    rundevice=rayO.device
    if rayT.device!=rundevice:
        rayT=rayT.to(rundevice)

    points = rayO[...,None,:]+rayD[...,None,:]*rayT[:,None]
    flatPoints=torch.reshape(points,(-1,points.shape[-1]))
    flatrayd=torch.reshape(rayD[...,None,:].broadcast_to(points.shape),(-1,rayD.shape[-1]))

    return flatPoints,flatrayd

## camrayT [numsampc] lirayt [numsaml] rayO[H,W,3] rayD [H,W,3] dir [3]
def getcoastpointsPos(camrayT,lirayT,rayO,rayD,pos):

    rundevice=rayO.device
    if camrayT.device!=rundevice:
        camrayT=camrayT.to(rundevice)
    if lirayT.device!=rundevice:
        lirayT=lirayT.to(rundevice)

    pointssam = rayO[...,None,:]+rayD[...,None,:]*camrayT[:,None]
    lightdir=(pointssam-pos[0,...])/(pointssam-pos[0,...]).norm(2,-1)[...,np.newaxis]
    lightpoints=lirayT[:,None]*lightdir[...,None,:]
    pointsall=(pointssam[...,None,:]+lightpoints).view(-1,3)
    # print(pointsall.shape)
    flatrayd=(rayD[...,None,None,:].broadcast_to(lightpoints.shape)).reshape(-1,3)
    return pointsall,flatrayd

# compute fine points in cpu
## rayO [H,W,3] rayD [H,W,3] sigma [H,W,numcoast] rayT [numcoast]
## return flatPoints [H*W*(numSam+numFine),3] flatrayd [H*W*(numSam+numFine),3] newRayT [H,W,(numSam+numFine)] H W
def getfinepoints(rayT,sigma,rayO,rayD,numFine=64):
    rundevice=rayO.device
    if rayT.device!=rundevice:
        rayT=rayT.to(rundevice)

    dt=torch.concat([torch.diff(rayT),torch.tensor(1e10,device=rundevice).broadcast_to(1)])
    if sigma.device!=rundevice:
        sigma=sigma.to(rundevice)
    else:
        sigma=sigma
    alfa=torch.exp(-sigma*dt)

    T=torch.cumprod(alfa + 1e-10,dim=-1,dtype=torch.float32)
    weight=T*(1.-alfa)  #权重计算，防止除0
    weight=weight[...,1:-1]+1e-5
    weight=weight/(torch.sum(weight,dim=-1,dtype=torch.float32)[...,np.newaxis])  #权重归一化

    cumWeight=torch.concat((torch.zeros_like(weight,dtype=torch.float32,device=rundevice)[...,:1],torch.cumsum(weight,dim=-1,dtype=torch.float32)),dim=-1)  #权重累积，权重大对应选取采样点个数多

    indis=torch.sort(torch.rand((weight.shape[0],weight.shape[1],(rayT.shape[0]+numFine)),dtype=torch.float32,device=rundevice),dim=-1).values #获取随机权重

    rayT_mid=(rayT[1:]+rayT[:-1])/2

    sortedIndis=torch.searchsorted(cumWeight,indis,right=True)#为每个权重值分配点，权重大对应选取采样点个数多，且权重间距正比于实际距离

    downIndis=torch.max(torch.tensor(0,device=rundevice),sortedIndis-1)

    upIndis=torch.min(sortedIndis,torch.tensor(cumWeight.shape[-1]-1,device=rundevice))

    scaleIndis=torch.stack([downIndis,upIndis],-1)

    w_g=torch.gather(cumWeight[...,np.newaxis,:].broadcast_to(cumWeight.shape[0],cumWeight.shape[1],scaleIndis.shape[-2],cumWeight.shape[-1]),-1,scaleIndis)

    rt_g=torch.gather(rayT_mid[np.newaxis,np.newaxis,np.newaxis,:].broadcast_to(scaleIndis.shape[0],scaleIndis.shape[1],scaleIndis.shape[2],rayT_mid.shape[-1]),-1,scaleIndis)

    scale=w_g[...,1]-w_g[...,0]
    scale[scale<1e-5]=1
    t=(indis-w_g[...,0])/scale
    newRayT=rt_g[...,0]+t*(rt_g[...,1]-rt_g[...,0]) #对于每个新的采样点，精采样点的位置正比于该点权重所在位置
    # print(newRayT.shape)
    newRayT=torch.sort(torch.concat((newRayT,rayT[np.newaxis,np.newaxis,:].broadcast_to((newRayT.shape[0],newRayT.shape[1],rayT.shape[-1]))),dim=-1),dim=-1).values

    points = rayO[...,None,:]+rayD[...,None,:]*newRayT[...,:,None]
    flatPoints=torch.reshape(points,(-1,points.shape[-1]))
    flatrayd=torch.reshape(rayD[...,None,:].broadcast_to(points.shape),(-1,rayD.shape[-1]))

    return flatPoints,flatrayd,newRayT

def batch_enc(enc_hash,enc_freq,flatpoints,flatrayd,fn,if_hash=False,chunkSize=1024*32):
    # device=next(fn.parameters()).device
    if if_hash:
        return torch.concat([fn(enc_hash(flatpoints[i:i+chunkSize,...]),enc_freq(flatrayd[i:i+chunkSize,...])) for i in range(0,flatpoints.shape[0],chunkSize)],0)
    else:
        return torch.concat([fn(enc_freq(flatpoints[i:i+chunkSize,...]),enc_freq(flatrayd[i:i+chunkSize,...])) for i in range(0,flatpoints.shape[0],chunkSize)],0)

# comput raw to rgb in GPU
def raw2rgb(raw,H,W,numsam):

        modelOut=torch.reshape(raw,(H,W,numsam,-1))
        sigma=torch.relu(modelOut[...,3])
        rgb=torch.sigmoid(modelOut[...,:3])

        return sigma,rgb

# comput RGB to colormap depthmap accmap
# rayT [H,W,numsam] sigma [H,W,numsam] rgb [H,W,numsam,3]
def rgb2output(rayT,sigma,rgb):
    rundevice=sigma.device
    rayT=rayT.to(rundevice)
    dt=torch.concat([torch.diff(rayT,dim=-1),(torch.tensor(1e10,device=rundevice).broadcast_to(rayT.shape))[...,:1]],dim=-1)
    alfa=torch.exp(-sigma*dt)

    T=torch.cumprod(alfa + 1e-10,dim=-1,dtype=torch.float32)

    weight=T*(1.-alfa)

    # normweight=weight/(torch.max(weight,dim=-1).values)[...,np.newaxis]

    rgb=rgb.permute(3,0,1,2)
    cr=torch.sum(weight*rgb,dim=-1)
    depthMap=torch.sum(weight*rayT,dim=-1)
    # depthMap=torch.sum(normweight*rayT,dim=-1)
    accMap=torch.sum(weight,dim=-1)
    return cr.permute(1,2,0),depthMap,accMap


"""Add subject ordinal numbers to preserve order

Revision ID: ea0a1b8dd57f
Revises: 9c00de5646cd
Create Date: 2017-08-16 13:20:41.635660

"""

# revision identifiers, used by Alembic.
revision = 'ea0a1b8dd57f'
down_revision = '9c00de5646cd'

from alembic import op
import sqlalchemy as sa

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.execute('CREATE SEQUENCE subject_seq')
    op.add_column('session_subject', sa.Column('ordinal', sa.Integer(), server_default=sa.text("nextval('subject_seq')"), nullable=True))
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.execute('DROP SEQUENCE subject_seq')
    op.drop_column('session_subject', 'ordinal')
    # ### end Alembic commands ###


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, mean_absolute_error

house_data = load_boston()

# print(house_data['DESCR'])

X = pd.DataFrame(house_data['data'], columns=house_data['feature_names'])
y = house_data['target']

print(X.describe())

# hist = X.hist(X.columns, figsize=(10, 10))
# plt.figure(figsize=(10, 7))
# sns.heatmap(X.corr())
# sns.heatmap(X)
# plt.show()

# split dataset into train and test
X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.7)

scaler = StandardScaler()

# normalize dataset: from 0 to 1
X_train = scaler.fit_transform(X_train)
X_test = scaler.fit_transform(X_test)

# check dispersion
print(np.std(X_train, axis=0))

# fir linear regression

model = LinearRegression()

model.fit(X_train, y_train)

y_train_prediction = model.predict(X_train)
y_test_prediction = model.predict(X_test)

# какие признаки внесли отрицательный и положительный вклад
plt.figure(figsize=(20, 8))
plt.bar(X.columns, model.coef_)
plt.show()

# оценка качества работы алгоритма по двум метрикам - МАЕ и MAS
# считаем метрику по истине и предсказанным результатам
print('MSE train', mean_squared_error(y_train, y_train_prediction))
print('MSE test', mean_squared_error(y_test, y_test_prediction))

# средняя величина, насколько мы ошибаемся при предсказании. Измеряется в том же, в чем измеряется истина.
print('MAE train', mean_absolute_error(y_train, y_train_prediction))
print('MAE test', mean_absolute_error(y_test, y_test_prediction))

# очень грубая оценка:
# средняя истинная цена:
print(y.mean())
# средняя ошибка:
print(mean_absolute_error(y_test, y_test_prediction))
# смотрим, на сколько процентов мы в среднем ошибаемся при предсказании.

# кросс-валидация:

result = cross_val_score(estimator=LinearRegression(), X=X, y=y, scoring='neg_mean_absolute_error', cv=5)

print(f'Average MAE {-result.mean()}, stadart deviation {result.std()}')

print()