##평균 제곱 오차

import numpy as np
def mean_squared_error(y, t):
    return 0.5 * np.sum((y-t)**2)

t = [0, 0, 1, 0, 0, 0, 0, 0, 0, 0]
y = [0.1, 0.05, 0.6, 0.0, 0.05, 0.1, 0.0, 0.1, 0.0, 0.0]

print(mean_squared_error(np.array(y), np.array(t)))

y = [0.1, 0.05, 0.1, 0.0, 0.05, 0.1, 0.0, 0.6, 0.0, 0.0]

print(mean_squared_error(np.array(y), np.array(t)))

##교차 엔트로피 오차

import numpy as np

def mean_squared_error2(y, t):
    '''
    평균 제곱 오차를 반환합니다.

    입력:
       y: 출력 데이터
       x: 타겟 레이블, 원-핫 인코딩
    '''
    return 0.5 * np.sum((y - t) ** 2)

def cross_entropy_error(y, t):
    delta = 1e-7
    return -np.sum(t * np.log(y + delta))

cross_entropy_error(y, t)

y = np.array([0.1, 0.05, 0.6, 0.0, 0.05, 0.1, 0.0, 0.1, 0.0, 0.0])

cross_entropy_error(y, t)

t = np.array([0, 0, 1, 0, 0, 0, 0, 0, 0, 0])
y = np.array([0.1, 0.05, 0.1, 0.0, 0.05, 0.1, 0.0, 0.6, 0.0, 0.0])

##미니 배치 교차 엔트로피
def cross_entropy_error2(y, t):
    if y.ndim == 1:
        t = t.reshape(1, t.size)
        y = y.reshape(1, y.size)

    batch_size = y.shape[0]
    return -np.sum(t * np.log(y + 1e-7)) / batch_size

# 정답 레이블이 '2', '7'과 같은 숫자 레이블로 주어진 경우
def cross_entropy_error3(y, t):
    if y.ndim == 1:
        t = t.reshape(1, t.size)
        y = y.reshape(1, y.size)

    batch_size = y.shape[0]
    return -np.sum(np.log(y[np.arange(batch_size), t])) / batch_size

# 수치 미분
def numerical_diff(f, x):
    h = 1e-4
    return (f(x+h) - f(x-h)) / (2*h)

x = np.arange(0.0, 20.0, 0.1)
def function_1(x):
    return 0.01*x**2 + 0.1*x

y = function_1(x)

import matplotlib.pyplot as plt
plt.xlabel("x")
plt.ylabel("f(x)")
plt.plot(x, y)
plt.show()

#기울기
def numerical_gradient(f, x):
    h = 1e-4
    grad = np.zeros_like(x)

    for idx in range(x.size):
        tmp_val = x[idx]
        x[idx] = tmp_val + h
        fxh1 = f(x)

        x[idx] = tmp_val - h
        fxh2 = f(x)

        grad[idx] = (fxh1 - fxh2) / (2*h)
        x[idx] = tmp_val

    return grad

# 차원에 상관없이 반복자를 지정
def numerical_gradient2(f, x):
    h = 1e-4 # 0.0001
    grad = np.zeros_like(x)

    it = np.nditer(x, flags=['multi_index'], op_flags=['readwrite'])
    while not it.finished:
        idx = it.multi_index
        tmp_val = x[idx]
        x[idx] = float(tmp_val) + h
        fxh1 = f(x) # f(x+h)

        x[idx] = tmp_val - h
        fxh2 = f(x) # f(x-h)
        grad[idx] = (fxh1 - fxh2) / (2*h)

        x[idx] = tmp_val # 값 복원
        it.iternext()

    return grad

##경사하강법
def gradient_descent(f, init_x, lr=0.01, step_num=100):
    x = init_x

    for i in range(step_num):
        grad = numerical_gradient(f, x)
        x -= lr * grad
    return x

def function_2(x):
    return x[0]**2 + x[1]**2

init_x = np.array([-3.0, 4.0])
gradient_descent(function_2, init_x=init_x, lr=0.1, step_num=100)

def softmax(a):
    c = np.max(a)
    exp_a = np.exp(a - c)
    sum_exp_a = np.sum(exp_a)
    y = exp_a / sum_exp_a

    return y

class simpleNet:
    def __init__(self):
        self.W = np.random.randn(2, 3)

    def predict(self, x):
        return np.dot(x, self.W)

    def loss(self, x, t):
        z = self.predict(x)
        y = softmax(z)
        loss = cross_entropy_error(y, t)

        return loss

##신경망 학습
# coding: utf-8
import sys, os
sys.path.append(os.pardir)  # 부모 디렉터리의 파일을 가져올 수 있도록 설정
from common.functions import *
from common.gradient import numerical_gradient

class TwoLayerNet:

    def __init__(self, input_size, hidden_size, output_size, weight_init_std=0.01):
        # 가중치 초기화
        self.params = {}
        self.params['W1'] = weight_init_std * np.random.randn(input_size, hidden_size)
        self.params['b1'] = np.zeros(hidden_size)
        self.params['W2'] = weight_init_std * np.random.randn(hidden_size, output_size)
        self.params['b2'] = np.zeros(output_size)

    def predict(self, x):
        W1, W2 = self.params['W1'], self.params['W2']
        b1, b2 = self.params['b1'], self.params['b2']

        a1 = np.dot(x, W1) + b1
        z1 = sigmoid(a1)
        a2 = np.dot(z1, W2) + b2
        y = softmax(a2)

        return y

    # x : 입력 데이터, t : 정답 레이블
    def loss(self, x, t):
        y = self.predict(x)

        return cross_entropy_error(y, t)

    def accuracy(self, x, t):
        y = self.predict(x)
        y = np.argmax(y, axis=1)
        t = np.argmax(t, axis=1)

        accuracy = np.sum(y == t) / float(x.shape[0])
        return accuracy

    # x : 입력 데이터, t : 정답 레이블
    def numerical_gradient(self, x, t):
        loss_W = lambda W: self.loss(x, t)

        grads = {}
        grads['W1'] = numerical_gradient(loss_W, self.params['W1'])
        grads['b1'] = numerical_gradient(loss_W, self.params['b1'])
        grads['W2'] = numerical_gradient(loss_W, self.params['W2'])
        grads['b2'] = numerical_gradient(loss_W, self.params['b2'])

        return grads

    def gradient(self, x, t):
        W1, W2 = self.params['W1'], self.params['W2']
        b1, b2 = self.params['b1'], self.params['b2']
        grads = {}

        batch_num = x.shape[0]

        # forward
        a1 = np.dot(x, W1) + b1
        z1 = sigmoid(a1)
        a2 = np.dot(z1, W2) + b2
        y = softmax(a2)

        # backward
        dy = (y - t) / batch_num
        grads['W2'] = np.dot(z1.T, dy)
        grads['b2'] = np.sum(dy, axis=0)

        da1 = np.dot(dy, W2.T)
        dz1 = sigmoid_grad(a1) * da1
        grads['W1'] = np.dot(x.T, dz1)
        grads['b1'] = np.sum(dz1, axis=0)

        return grads

# coding: utf-8
import sys, os
sys.path.append(os.pardir)  # 부모 디렉터리의 파일을 가져올 수 있도록 설정
import numpy as np
import matplotlib.pyplot as plt
from dataset.mnist import load_mnist
from two_layer_net import TwoLayerNet

# 데이터 읽기
(x_train, t_train), (x_test, t_test) = load_mnist(normalize=True, one_hot_label=True)

network = TwoLayerNet(input_size=784, hidden_size=50, output_size=10)

# 하이퍼파라미터
iters_num = 10000  # 반복 횟수를 적절히 설정한다.
train_size = x_train.shape[0]
batch_size = 100   # 미니배치 크기
learning_rate = 0.1

train_loss_list = []
train_acc_list = []
test_acc_list = []

# 1에폭당 반복 수
iter_per_epoch = max(train_size / batch_size, 1)

for i in range(iters_num):
    # 미니배치 획득
    batch_mask = np.random.choice(train_size, batch_size)
    x_batch = x_train[batch_mask]
    t_batch = t_train[batch_mask]

    # 기울기 계산
    #grad = network.numerical_gradient(x_batch, t_batch)
    grad = network.gradient(x_batch, t_batch)

    # 매개변수 갱신
    for key in ('W1', 'b1', 'W2', 'b2'):
        network.params[key] -= learning_rate * grad[key]

    # 학습 경과 기록
    loss = network.loss(x_batch, t_batch)
    train_loss_list.append(loss)

    # 1에폭당 정확도 계산
    if i % iter_per_epoch == 0:
        train_acc = network.accuracy(x_train, t_train)
        test_acc = network.accuracy(x_test, t_test)
        train_acc_list.append(train_acc)
        test_acc_list.append(test_acc)
        print("train acc, test acc | " + str(train_acc) + ", " + str(test_acc))

# 그래프 그리기
markers = {'train': 'o', 'test': 's'}
x = np.arange(len(train_acc_list))
plt.plot(x, train_acc_list, label='train acc')
plt.plot(x, test_acc_list, label='test acc', linestyle='--')
plt.xlabel("epochs")
plt.ylabel("accuracy")
plt.ylim(0, 1.0)
plt.legend(loc='lower right')
plt.show()

python_class/deep_learning/deep learning from scratch/neural_network_04.py


# -*- coding: utf-8 -*-

from library.simulator_func_mysql import *

class simulator():
    def __init__(self):
        self.set_variables()
        self.call_library()

    def set_variables(self):
        # 시뮬레이터 번호 설정
        self.simul_num = int(input("시뮬레이팅 할 알고리즘 번호를 입력 하세요: "))

        # self.simul_reset 설정
        #       'y'  :  self.simul_num 에서 설정한 번호에 해당 하는 시뮬레이터 데이터베이스를 초기화 하고 처음 부터 실행
        #       'n' : self.simul_num 에서 설정한 번호에 해당 하는 시뮬레이터 데이터베이스를 초기화 하지 않고 이어서 실행
        #                    ex) 2020년 01월 01일까지 시뮬레이터를 마쳤는데, 그 이후로 연달아서 2020년 01월 02일 부터 시뮬레이터 테스트를 하고 싶은 경우

        input_reset = str(input("시뮬레이팅 데이터베이스 초기화 여부 : (y or n) "))

        if input_reset == 'y':
            self.simul_reset= True
        elif input_reset =='n':
            self.simul_reset= False
        else:
            print("y or n (소문자) 만 입력 가능 합니다.")
            exit(1)

    def call_library(self):
        # simulator_func_mysql 라이브러리 클래스 호출
        simulator_func_mysql(self.simul_num, self.simul_reset, True, 0)

if __name__ == "__main__":
    # simulator 클래스 호출
    simulator()


# ==================================
# Video Object Detection with YOLOv3
# ==================================

# RUN WITH EXAMPLE COMMAND BELOW:

# python YOLO_vid.py -i vid_IO/drive.mp4 -o vid_IO/drive_processed.mp4 -y yolov3

import numpy as np
import argparse
import imutils
import time
import cv2
import os

"""User inputs through command line"""
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--input", required=True, help="path to input video")
ap.add_argument("-o", "--output", required=True, help="path to output video")
ap.add_argument("-y", "--yolo", required=True, help="base path to YOLO directory")
ap.add_argument("-c", "--confidence", type=float, default=0.5, help="minimum probability to filter weak detections")
ap.add_argument("-t", "--threshold", type=float, default=0.3, help="threshold when applying non-maxima suppression")
args = vars(ap.parse_args())

def get_model():
	"""Load YOLOv3 using cv2 built in DNN module."""
	model = cv2.dnn.readNetFromDarknet(os.path.sep.join([args["yolo"], "yolov3.cfg"]),
												os.path.sep.join([args["yolo"], "yolov3.weights"]))

	# load COCO class labels (open coco.names file (concatenated) -> extract string -> removed lead and end
	# whitespace, split by \n)
	labels = open(os.path.sep.join([args["yolo"], "coco.names"])).read().strip().split("\n")

	# get output layer names (getLayerNames not subscriptable)
	getLayer = model.getLayerNames()
	out_layer_names = [getLayer[i[0] - 1] for i in model.getUnconnectedOutLayers()]
	# print("YOLOv3 output layer names:", *out_layer_names, sep=" ")

	return labels, model, out_layer_names

def get_color(labels):
	"""Initialize random colors."""
	np.random.seed(1)
	colors = np.random.randint(0, 255, size=(len(labels), 3), dtype="uint8")

	return colors

def init_video():
	"""Initialize video stream."""
	video_stream = cv2.VideoCapture(args["input"])
	writer = None # videoWriter object
	(frame_width, frame_height) = (None, None)

	return video_stream, writer, frame_width, frame_height

def get_frame_number(video_stream):
	"""Try to get total frame number for estimating process time."""
	try:
		prop = cv2.cv.CV_CAP_PROP_FRAME_COUNT if imutils.is_cv2() else cv2.CAP_PROP_FRAME_COUNT
		num_frame = int(video_stream.get(prop))
		print("{} total frames in video".format(num_frame))
	except:
		print("Cannot determine the approximate processing time needed.")
		num_frame = -1

	return num_frame

# -----------------------------------------------------------
# Below functions are all called in the video stream pipeline
# -----------------------------------------------------------

def preprocess_input(model, frame):
	"""Augment input and set up for forward pass."""
	blob = cv2.dnn.blobFromImage(frame, 1.0 / 255, (416, 416), swapRB=True, crop=False)
	model.setInput(blob)

def get_input(video_stream, frame_width, frame_height):
	"""Grab frames and return dimensions."""
	(grabbed, frame) = video_stream.read()
	# end of video, break out of loop
	if not grabbed:
		return None, None, None, grabbed
	if frame_width is None or frame_height is None:
		(frame_height, frame_width) = frame.shape[:2]

	return frame, frame_height, frame_width, grabbed

def forward_pass(writer, model, out_layer_names, num_frame):
	"""Forward pass, non-max suppression done by default. Estimate process time."""
	tick = time.time()
	layer_outputs = model.forward(out_layer_names)
	tock = time.time()

	# writer is uninitialized for the first frame only
	if writer == None and num_frame > 0:
		print("YOLOv3 took {:.3f} seconds for one frame".format(tock - tick))
		print("YOLOv3 takes estimated total time of {:.3f} seconds for the video".format((tock - tick) * num_frame))

	return layer_outputs

def filter_output(layer_outputs, frame_width, frame_height):
	"""Get lists for bounding box."""
	boxes = []
	confidences = []
	classIDs = []

	# process output
	for output in layer_outputs:
		for detection in output:
			scores = detection[5:] # detection starts with locational variables (0 to 1)
			classID = np.argmax(scores)
			confidence = scores[classID]

			if confidence > args["confidence"]: # filter out low confidence
				box_data = detection[:4] * np.array([frame_width, frame_height, frame_width, frame_height])
				(center_X, center_Y, box_width, box_height) = box_data.astype("int")

				x = int(center_X - (box_width / 2))
				y = int(center_Y - (box_height / 2))

				# record box data, confidence, and class ID for the detected (note boxes is 2d)
				boxes.append([x, y, int(box_width), int(box_height)])
				confidences.append(float(confidence))
				classIDs.append(classID)

	# with box dimension, we can now call non-maxima suppression (filtering out overlapping)
	indices = cv2.dnn.NMSBoxes(boxes, confidences, args["confidence"], args["threshold"])

	return boxes, confidences, classIDs, indices

def draw_box(frame, boxes, confidences, classIDs, indices, labels, colors):
	"""Draw all bounding boxes."""
	if len(indices) > 0:
		for i in indices.flatten():
			x,y,w,h = boxes[i][0],boxes[i][1],boxes[i][2],boxes[i][3]

			# draw, where OpenCV library certainly shines
			color = [int(c) for c in colors[classIDs[i]]]
			cv2.rectangle(frame, (x, y), (x + w, y + h), color, 1)
			object_name = "{}: {:.4f}".format(labels[classIDs[i]], confidences[i])
			cv2.putText(frame, object_name, (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 1)

	return frame

def write_to_video(writer, frame):
	"""Initialize writer for first frame."""
	if writer is None:
		fourcc = cv2.VideoWriter_fourcc(*"mp4v") # or change to *"MJPG")
		writer = cv2.VideoWriter(args["output"], fourcc, 30, (frame.shape[1], frame.shape[0]), True)

	writer.write(frame)

	return writer

def loop_frames(labels, colors, model, out_layer_names, video_stream, writer, frame_width, frame_height, num_frame):
	"""Loop through each frame until no frame is grabbed"""
	while True:
		frame, frame_height, frame_width, grabbed = get_input(video_stream, frame_width, frame_height)
		if not grabbed:
			# return both parameters for clean_up, breaks out of loop
			print("Finished!")
			return writer, video_stream

		preprocess_input(model, frame)
		yolo_output = forward_pass(writer, model, out_layer_names, num_frame)
		boxes, confidences, classIDs, indices = filter_output(yolo_output, frame_width, frame_height)
		output_frame = draw_box(frame, boxes, confidences, classIDs, indices, labels, colors)
		writer = write_to_video(writer, output_frame)

def show_output():
	"""Show output as a frame in video stream, press q to exit and update the fps."""
	output_stream = cv2.VideoCapture(args["output"])
	if (output_stream.isOpened() == False):
		print("Error opening video file, try opening it from the output path")

	while output_stream.isOpened():
		grabbed, frame = output_stream.read()
		if grabbed == True:
			cv2.imshow('Frame', frame)
			# press Q to exit
			if cv2.waitKey(25) & 0xFF == ord('q'):
				break
		else:
			break

	# clean up output
	output_stream.release()
	cv2.destroyAllWindows()

def clean_up(writer, video_stream):
	"""Stop recording fps and display performance data."""
	writer.release()
	video_stream.release()

def run():
	"""Organize and call the useful functions."""
	labels, model, out_layer_names = get_model()
	colors = get_color(labels)
	video_stream, writer, frame_width, frame_height = init_video()
	num_frame = get_frame_number(video_stream)
	writer, video_stream = loop_frames(labels, colors, model, out_layer_names, video_stream, writer, frame_width, frame_height, num_frame)
	clean_up(writer, video_stream)
	show_output()

run()


import matplotlib
import matplotlib.pyplot as plt
import pandas as pd
import sys
import matplotlib.dates as mdates

fin = sys.argv[1]
fin_raw = sys.argv[2]
font = {'size': 14}
matplotlib.rc('font', **font)
data = pd.read_csv(fin, parse_dates=True, index_col=0)
data_raw = pd.read_csv(fin_raw, parse_dates=True, index_col=0)
fig, ax = plt.subplots(figsize=(14, 8))
ax.plot(data.index, data['load_kg'], 'k')
ax.plot(data_raw.index, data_raw['load_kg'], 'g')
plt.fill_between(data.index, ax.get_ylim()[0], data['load_kg'], color=(1, 0.2, 0.2, 0.2))
plt.minorticks_on()
fig.autofmt_xdate()
# Use a more precise date string for the x axis locations in the toolbar.
ax.fmt_xdata = mdates.DateFormatter('%Y-%m-%d')
ax.grid(which='major', axis='both', linewidth=1.5, color='gray')
ax.xaxis.grid(True, 'minor', color='gray', alpha=0.3, ls='-', lw=1)
ax.yaxis.grid(True, 'minor', color='gray', alpha=0.3, ls='-', lw=1)
ax.set_xlabel('datetime')
ax.set_ylabel('load kg')
plt.grid('minor')
plt.tight_layout()
fout = fin.replace('.csv', '_withRaw.pdf')
plt.savefig(fout)
plt.show()


import textwrap
import time

from java.awt import Component
from java.awt.event import WindowAdapter
from javax.swing import (BoxLayout,  JLabel, JOptionPane, JPanel,
                         JPasswordField, JTextField, JList, JScrollPane)
from javax.swing.JOptionPane import (DEFAULT_OPTION, OK_CANCEL_OPTION,
                                     OK_OPTION, PLAIN_MESSAGE,
                                     UNINITIALIZED_VALUE, YES_NO_OPTION)

from robotide.lib.robot.utils import html_escape

MAX_CHARS_PER_LINE = 120

class _SwingDialog(object):

    def __init__(self, pane):
        self._pane = pane

    def _create_panel(self, message, widget):
        panel = JPanel()
        panel.setLayout(BoxLayout(panel, BoxLayout.Y_AXIS))
        label = self._create_label(message)
        label.setAlignmentX(Component.LEFT_ALIGNMENT)
        panel.add(label)
        widget.setAlignmentX(Component.LEFT_ALIGNMENT)
        panel.add(widget)
        return panel

    def _create_label(self, message):
        # JLabel doesn't support multiline text, setting size, or wrapping.
        # Need to handle all that ourselves. Feels like 2005...
        wrapper = textwrap.TextWrapper(MAX_CHARS_PER_LINE,
                                       drop_whitespace=False)
        lines = []
        for line in html_escape(message, linkify=False).splitlines():
            if line:
                lines.extend(wrapper.wrap(line))
            else:
                lines.append('')
        return JLabel('%s' % '
'.join(lines))

    def show(self):
        self._show_dialog(self._pane)
        return self._get_value(self._pane)

    def _show_dialog(self, pane):
        dialog = pane.createDialog(None, 'Robot Framework')
        dialog.setModal(False)
        dialog.setAlwaysOnTop(True)
        dialog.addWindowFocusListener(pane.focus_listener)
        dialog.show()
        while dialog.isShowing():
            time.sleep(0.2)
        dialog.dispose()

    def _get_value(self, pane):
        value = pane.getInputValue()
        return value if value != UNINITIALIZED_VALUE else None

class MessageDialog(_SwingDialog):

    def __init__(self, message):
        pane = WrappedOptionPane(message, PLAIN_MESSAGE, DEFAULT_OPTION)
        _SwingDialog.__init__(self, pane)

class InputDialog(_SwingDialog):

    def __init__(self, message, default, hidden=False):
        self._input_field = JPasswordField() if hidden else JTextField()
        self._input_field.setText(default)
        self._input_field.selectAll()
        panel = self._create_panel(message, self._input_field)
        pane = WrappedOptionPane(panel, PLAIN_MESSAGE, OK_CANCEL_OPTION)
        pane.set_focus_listener(self._input_field)
        _SwingDialog.__init__(self, pane)

    def _get_value(self, pane):
        if pane.getValue() != OK_OPTION:
            return None
        return self._input_field.getText()

class SelectionDialog(_SwingDialog):

    def __init__(self, message, options):
        pane = WrappedOptionPane(message, PLAIN_MESSAGE, OK_CANCEL_OPTION)
        pane.setWantsInput(True)
        pane.setSelectionValues(options)
        _SwingDialog.__init__(self, pane)

class MultipleSelectionDialog(_SwingDialog):

    def __init__(self, message, options):
        self._selection_list = JList(options)
        self._selection_list.setVisibleRowCount(8)
        panel = self._create_panel(message, JScrollPane(self._selection_list))
        pane = WrappedOptionPane(panel, PLAIN_MESSAGE, OK_CANCEL_OPTION)
        _SwingDialog.__init__(self, pane)

    def _get_value(self, pane):
        if pane.getValue() != OK_OPTION:
            return None
        return list(self._selection_list.getSelectedValuesList())

class PassFailDialog(_SwingDialog):

    def __init__(self, message):
        pane = WrappedOptionPane(message, PLAIN_MESSAGE, YES_NO_OPTION,
                                 None, ['PASS', 'FAIL'], 'PASS')
        _SwingDialog.__init__(self, pane)

    def _get_value(self, pane):
        value = pane.getValue()
        return value == 'PASS' if value in ['PASS', 'FAIL'] else None

class WrappedOptionPane(JOptionPane):
    focus_listener = None

    def getMaxCharactersPerLineCount(self):
        return MAX_CHARS_PER_LINE

    def set_focus_listener(self, component):
        self.focus_listener = WindowFocusListener(component)

class WindowFocusListener(WindowAdapter):

    def __init__(self, component):
        self.component = component

    def windowGainedFocus(self, event):
        self.component.requestFocusInWindow()


from django import forms
from .models import Museum, Group
from user.models import UserProfile

class CreateGroupForm(forms.Form):
    name = forms.CharField(max_length=50)
    number = forms.IntegerField(min_value=1, max_value=200)
    museum = forms.ModelChoiceField(Museum.objects.all())

    def create_users_pdf(self, user):
        museum = self.cleaned_data['museum']
        name = self.cleaned_data['name']

        group = Group(
            museum=museum,
            name=name,
            created_by=user
        )
        group.save()
        tokens = []
        for x in range(self.cleaned_data['number']):
            profile = UserProfile()
            profile.display_name = f'space cadet {x + 1}'
            profile.save()
            group.profiles.add(profile)
            museum.profiles.add(profile)
            tokens.append(profile.qr_token)

        group.save()
        museum.save()


from dataStructs.Stack import Stack

pairs = {'(':')','{':'}','[':']'}

def parChecker(arg):
	balanced = True
	s = Stack()

	for ch in arg:
		if ch in "({[" :
			s.push(ch)
		elif ch in ")}]":
			if s.isEmpty():
				balanced = False
				break
			else:
				top = s.pop()
				if pairs.get(top) != ch:
					balanced = False
					break
	return balanced

args = "[]"
print(parChecker(args))


'''
「検査陽性者の状況」画像から注記を抽出する処理 パターン２
'''

import re
import pytesseract
import cv2
import numpy as np

# 画像内の矩形を抽出
# https://stackoverflow.com/a/60068297
def cropTable(src):

    hei = src.shape[0]
    wid = src.shape[1]
    totalArea = wid * hei

    original = src.copy()
    gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
    thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

    # Find contours, obtain bounding box, extract and save ROI
    ROI_number = 0
    cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    cnts = cnts[0] if len(cnts) == 2 else cnts[1]

    lst = []
    for c in cnts:
        x,y,w,h = cv2.boundingRect(c)
        cv2.rectangle(src, (x, y), (x + w, y + h), (36,255,12), 2)
        ROI = original[y:y+h, x:x+w]
        # cv2.imwrite('ROI_{}.png'.format(ROI_number), ROI)

        # 面積が最大の矩形を記憶（ただし画像全体を覆う矩形は除外）
        area = w * h
        if (totalArea * 0.9) > area:
            lst.append((ROI_number, area, x, y, w, h))

        ROI_number += 1

    # 矩形面積の降順に並び替え
    lst.sort(key=lambda x:-x[1])

    # 1番目：「クラスターの感染」、2番目：「○月～」なハズなので
    # 2番目の y+h から 1番目の y までを切りだす
    first = lst[0]
    scond = lst[1]
    top = (scond[3]+scond[5])
    bottom = first[3]

    if top >= bottom:
        print("注記位置を特定できませんでした")
        return src

    img = original[top:bottom]
    return img

# 拡大と白黒化
def grayAndResize(src):
    neiborhood24 = np.array(
        [
            [1, 1, 1, 1, 1],
            [1, 1, 1, 1, 1],
            [1, 1, 1, 1, 1],
            [1, 1, 1, 1, 1],
            [1, 1, 1, 1, 1],
        ],
        np.uint8,
    )

    height = src.shape[0]
    width = src.shape[1]

    dilated = cv2.dilate(src, neiborhood24, iterations=1)
    diff = cv2.absdiff(dilated, src)
    contour = 255 - diff

    gray2 = cv2.resize(contour, (int(width * 2), int(height * 2)))
    th, img = cv2.threshold(gray2, 180, 255, cv2.THRESH_BINARY)
    return img

def recognize(jpg_path):
    src = cv2.imread(str(jpg_path))
    hei = src.shape[0]
    wid = src.shape[1]

    # 画像の上25%～60%でカット
    img = src[int(hei * 0.25):int(hei * 0.6)]
    # cv2.imwrite('remarks_ptn2_1_cropped.jpg', img)

    # 画像内の矩形を抽出
    img = cropTable(img)
    # cv2.imwrite('remarks_ptn2_2_rected.jpg', img)

    # 拡大と白黒化
    img = grayAndResize(img)
    # cv2.imwrite('remarks_ptn2_3_resized.jpg', img)

    # 範囲指定
    # ref http://blog.machine-powers.net/2018/08/02/learning-tesseract-command-utility/
    txt = pytesseract.image_to_string(img, lang="jpn", config="--psm 11").replace(".", "").replace(",", "")
    print(txt)

    # ※1 ※2 または (注) で始まる文を抽出
    remarks = re.findall("^(.*?\d{1} .*|\(.?.* .*)$", txt, re.M)

    def normalize(txt):
        # 行頭の ※1 ※2 や (注) を削除（空白以降を抽出）
        txt = txt[txt.find(' ') + 1:]
        # 空白を除去
        txt = txt.replace(' ', '')
        # 画像切れて認識できない「掲載。」を補完
        txt = re.sub('検査を行ったものについて掲.*$', '検査を行ったものについて掲載。', txt)
        return txt

    remarks = list(map(normalize, remarks))
    print(remarks)
    return remarks


import pickle
import DLfunctions as dl
from keras.preprocessing.sequence import pad_sequences

print("What is the name of the hyperparams file?(Local directory):")
params_file_name = input()

a_file = open(params_file_name, "rb")
params = pickle.load(a_file)

w = params["w"]
b = params["b"]

print("What is the questionable URL?")
X_URL = input()
X_URL_list = []

#convert to array
X_URL_list.append([ord(c) for c in X_URL][:-2])
print(X_URL_list)
padded = pad_sequences(X_URL_list,maxlen=3063,padding='post')
print(padded)
X_TEST = padded.T

if(dl.predict(w, b, X_TEST)):
    print("This is probably a bad URL!")
else:
    print("This is probably an okay URL!")


import FWCore.ParameterSet.Config as cms

pfZeroSuppressionThresholds_EB = [0.080]*170
pfZeroSuppressionThresholds_EEminus = [0.300]*39
pfZeroSuppressionThresholds_EEplus = pfZeroSuppressionThresholds_EEminus

#
# These are expected to be adjusted soon, while the thresholds for older setups will remain unchanged.
#
_pfZeroSuppressionThresholds_EB_2017 = pfZeroSuppressionThresholds_EB
_pfZeroSuppressionThresholds_EEminus_2017 = pfZeroSuppressionThresholds_EEminus
_pfZeroSuppressionThresholds_EEplus_2017 = _pfZeroSuppressionThresholds_EEminus_2017

particle_flow_zero_suppression_ECAL = cms.PSet(
    thresholds = cms.vdouble(pfZeroSuppressionThresholds_EB + pfZeroSuppressionThresholds_EEminus + pfZeroSuppressionThresholds_EEplus
        )
    )

_particle_flow_zero_suppression_ECAL_2017 = cms.PSet(
    thresholds = cms.vdouble(_pfZeroSuppressionThresholds_EB_2017 + _pfZeroSuppressionThresholds_EEminus_2017 + _pfZeroSuppressionThresholds_EEplus_2017
        )
    )

#
# The thresholds have been temporarily removed (lowered to 80 MeV in EB and 300 MeV in EE,
# then overseeded by the gathering and seeding PF cluster thresholds)
# Later, we may need to reintroduce eta dependent thresholds
# to mitigate the effect of the noise
#

from Configuration.Eras.Modifier_run2_ECAL_2017_cff import run2_ECAL_2017
run2_ECAL_2017.toReplaceWith(particle_flow_zero_suppression_ECAL, _particle_flow_zero_suppression_ECAL_2017)

from Configuration.Eras.Modifier_phase2_ecal_cff import phase2_ecal
phase2_ecal.toReplaceWith(particle_flow_zero_suppression_ECAL, _particle_flow_zero_suppression_ECAL_2017)


# -*- coding: utf-8 -*-
"""
Training deep convolutional neural networks.
Created on Thu Jul  5 11:00:00 2018
Author: Prasun Roy | CVPRU-ISICAL (http://www.isical.ac.in/~cvpr)
GitHub: https://github.com/prasunroy/cnn-on-degraded-images

"""

# imports
from __future__ import division
from __future__ import print_function

import glob
import json
import numpy
import os
import tensorflow

from keras import applications
from keras import optimizers
from keras.callbacks import CSVLogger
from keras.callbacks import EarlyStopping
from keras.callbacks import ModelCheckpoint
from keras.callbacks import TensorBoard
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import Flatten
from keras.models import Model
from keras.preprocessing.image import ImageDataGenerator
from matplotlib import pyplot

from libs.PipelineUtils import shutdown
from mlutils.callbacks import Telegram

# configurations
# -----------------------------------------------------------------------------
PROCESS_SEED = None

ARCHITECTURE = 'inceptionv3'
INCLUDE_TOPL = False
WEIGHTS_INIT = 'imagenet'
INPUT_TENSOR = None
INPUT_DSHAPE = (299, 299, 3)
POOLING_TYPE = None
NUM_TCLASSES = 10
FREEZE_LAYER = 0
NEURONS_FC_1 = 1024
NEURONS_FC_2 = 1024
DROPOUT_FC12 = 0.5
FN_OPTIMIZER = optimizers.sgd(lr=0.0001, momentum=0.5)

DATASET_ID = 'synthetic_digits'
DATA_TRAIN = 'data/{}/imgs_train/'.format(DATASET_ID)
DATA_VALID = 'data/{}/imgs_valid/'.format(DATASET_ID)
LABEL_MAPS = 'data/{}/labelmap.json'.format(DATASET_ID)
SAVE_AUGMT = False
BATCH_SIZE = 50
NUM_EPOCHS = 100
OUTPUT_DIR = 'output/{}/{}/'.format(DATASET_ID, ARCHITECTURE)

AUTH_TOKEN = None
TELCHAT_ID = None

F_SHUTDOWN = False
# -----------------------------------------------------------------------------

# setup seed for random number generators for reproducibility
numpy.random.seed(PROCESS_SEED)
tensorflow.set_random_seed(PROCESS_SEED)

# setup paths for augmented data
if SAVE_AUGMT:
    aug_dir_train = os.path.join(OUTPUT_DIR, 'augmented_data/imgs_train/')
    aug_dir_valid = os.path.join(OUTPUT_DIR, 'augmented_data/imgs_valid/')
else:
    aug_dir_train = None
    aug_dir_valid = None

# setup paths for model architecture
mdl_dir = os.path.join(OUTPUT_DIR, 'models')
mdl_file = os.path.join(mdl_dir, '{}.json'.format(ARCHITECTURE))

# setup paths for callbacks
log_dir = os.path.join(OUTPUT_DIR, 'logs')
cpt_dir = os.path.join(OUTPUT_DIR, 'checkpoints')
tbd_dir = os.path.join(OUTPUT_DIR, 'tensorboard')

log_file = os.path.join(log_dir, 'training.csv')
cpt_best = os.path.join(cpt_dir, '{}_best.h5'.format(ARCHITECTURE))
cpt_last = os.path.join(cpt_dir, '{}_last.h5'.format(ARCHITECTURE))

# validate paths
def validate_paths():
    flag = True
    data_dirs = [DATA_TRAIN, DATA_VALID]
    for directory in data_dirs:
        if not os.path.isdir(directory):
            print('[INFO] Data directory not found at {}'.format(directory))
            flag = False
    output_dirs = [OUTPUT_DIR, aug_dir_train, aug_dir_valid, mdl_dir, log_dir, cpt_dir, tbd_dir]
    output_dirs = [directory for directory in output_dirs if directory is not None]
    for directory in output_dirs:
        if not os.path.isdir(directory):
            os.makedirs(directory)
        elif len(glob.glob(os.path.join(directory, '*.*'))) > 0:
            print('[INFO] Output directory {} must be empty'.format(directory))
            flag = False

    return flag

# load data
def load_data():
    # image data generator configuration for training data augmentation
    data_gen_train = ImageDataGenerator(featurewise_center=False,
                                        samplewise_center=False,
                                        featurewise_std_normalization=False,
                                        samplewise_std_normalization=False,
                                        zca_whitening=False,
                                        zca_epsilon=1e-06,
                                        rotation_range=0.0,
                                        width_shift_range=0.0,
                                        height_shift_range=0.0,
                                        brightness_range=None,
                                        shear_range=0.0,
                                        zoom_range=0.0,
                                        channel_shift_range=0.0,
                                        fill_mode='nearest',
                                        cval=0.0,
                                        horizontal_flip=False,
                                        vertical_flip=False,
                                        rescale=1.0/255.0,
                                        preprocessing_function=None,
                                        data_format=None,
                                        validation_split=0.0)

    # image data generator configuration for validation data augmentation
    data_gen_valid = ImageDataGenerator(featurewise_center=False,
                                        samplewise_center=False,
                                        featurewise_std_normalization=False,
                                        samplewise_std_normalization=False,
                                        zca_whitening=False,
                                        zca_epsilon=1e-06,
                                        rotation_range=0.0,
                                        width_shift_range=0.0,
                                        height_shift_range=0.0,
                                        brightness_range=None,
                                        shear_range=0.0,
                                        zoom_range=0.0,
                                        channel_shift_range=0.0,
                                        fill_mode='nearest',
                                        cval=0.0,
                                        horizontal_flip=False,
                                        vertical_flip=False,
                                        rescale=1.0/255.0,
                                        preprocessing_function=None,
                                        data_format=None,
                                        validation_split=0.0)

    # training image data generator
    data_flow_train = data_gen_train.flow_from_directory(directory=DATA_TRAIN,
                                                         target_size=INPUT_DSHAPE[:-1],
                                                         color_mode='rgb',
                                                         classes=None,
                                                         class_mode='categorical',
                                                         batch_size=BATCH_SIZE,
                                                         shuffle=True,
                                                         seed=PROCESS_SEED,
                                                         save_to_dir=aug_dir_train,
                                                         save_prefix='',
                                                         save_format='png',
                                                         follow_links=False,
                                                         subset=None,
                                                         interpolation='nearest')

    # validation image data generator
    data_flow_valid = data_gen_valid.flow_from_directory(directory=DATA_VALID,
                                                         target_size=INPUT_DSHAPE[:-1],
                                                         color_mode='rgb',
                                                         classes=None,
                                                         class_mode='categorical',
                                                         batch_size=BATCH_SIZE,
                                                         shuffle=True,
                                                         seed=PROCESS_SEED,
                                                         save_to_dir=aug_dir_valid,
                                                         save_prefix='',
                                                         save_format='png',
                                                         follow_links=False,
                                                         subset=None,
                                                         interpolation='nearest')

    return (data_flow_train, data_flow_valid)

# build model
def build_model():
    model = None

    # create architecture
    if ARCHITECTURE.lower() == 'inceptionv3':
        model = applications.inception_v3.InceptionV3(include_top=INCLUDE_TOPL, weights=WEIGHTS_INIT, input_tensor=INPUT_TENSOR, input_shape=INPUT_DSHAPE, pooling=POOLING_TYPE, classes=NUM_TCLASSES)
    elif ARCHITECTURE.lower() == 'mobilenet':
        model = applications.mobilenet.MobileNet(include_top=INCLUDE_TOPL, weights=WEIGHTS_INIT, input_tensor=INPUT_TENSOR, input_shape=INPUT_DSHAPE, pooling=POOLING_TYPE, classes=NUM_TCLASSES)
    elif ARCHITECTURE.lower() == 'resnet50':
        model = applications.resnet50.ResNet50(include_top=INCLUDE_TOPL, weights=WEIGHTS_INIT, input_tensor=INPUT_TENSOR, input_shape=INPUT_DSHAPE, pooling=POOLING_TYPE, classes=NUM_TCLASSES)
    elif ARCHITECTURE.lower() == 'vgg16':
        model = applications.vgg16.VGG16(include_top=INCLUDE_TOPL, weights=WEIGHTS_INIT, input_tensor=INPUT_TENSOR, input_shape=INPUT_DSHAPE, pooling=POOLING_TYPE, classes=NUM_TCLASSES)
    elif ARCHITECTURE.lower() == 'vgg19':
        model = applications.vgg19.VGG19(include_top=INCLUDE_TOPL, weights=WEIGHTS_INIT, input_tensor=INPUT_TENSOR, input_shape=INPUT_DSHAPE, pooling=POOLING_TYPE, classes=NUM_TCLASSES)

    if not model is None:
        # freeze layers
        if FREEZE_LAYER < 0:
            for layer in model.layers:
                layer.trainable = False
        else:
            for layer in model.layers[:FREEZE_LAYER]:
                layer.trainable = False

        # add fully connected layers
        if not INCLUDE_TOPL:
            x = model.output
            x = Flatten()(x)
            x = Dense(NEURONS_FC_1, activation='relu')(x)
            x = Dropout(DROPOUT_FC12)(x)
            x = Dense(NEURONS_FC_2, activation='relu')(x)
            y = Dense(NUM_TCLASSES, activation='softmax')(x)

            # final architecture
            model_final = Model(inputs=model.input, outputs=y)
        else:
            model_final = model

        # compile the final model
        model_final.compile(optimizer=FN_OPTIMIZER, loss='categorical_crossentropy', metrics=['accuracy'])

    return model_final

# create callbacks
def callbacks():
    cb_log = CSVLogger(filename=log_file, append=True)
    cb_stp = EarlyStopping(monitor='val_loss', min_delta=0, patience=10, verbose=1)
    cb_cpt_best = ModelCheckpoint(filepath=cpt_best, monitor='val_acc', save_best_only=True, save_weights_only=True, verbose=1)
    cb_cpt_last = ModelCheckpoint(filepath=cpt_last, monitor='val_acc', save_best_only=False, save_weights_only=True, verbose=0)
    cb_tbd = TensorBoard(log_dir=tbd_dir, batch_size=BATCH_SIZE, write_grads=True, write_images=True)
    cb_tel = Telegram(auth_token=AUTH_TOKEN, chat_id=TELCHAT_ID, monitor='val_acc', out_dir=OUTPUT_DIR)

    return [cb_log, cb_stp, cb_cpt_best, cb_cpt_last, cb_tbd, cb_tel]

# plot learning curve
def plot(train_history):
    # plot training and validation loss
    pyplot.figure()
    pyplot.plot(train_history.history['loss'], label='loss')
    pyplot.plot(train_history.history['val_loss'], label='val_loss')
    pyplot.title('Training and Validation Loss')
    pyplot.xlabel('epoch')
    pyplot.ylabel('loss')
    pyplot.legend()
    pyplot.savefig(os.path.join(log_dir, 'plot_loss.png'))
    pyplot.show(block=False)

    # plot training and validation accuracy
    pyplot.figure()
    pyplot.plot(train_history.history['acc'], label='acc')
    pyplot.plot(train_history.history['val_acc'], label='val_acc')
    pyplot.title('Training and Validation Accuracy')
    pyplot.xlabel('epoch')
    pyplot.ylabel('accuracy')
    pyplot.legend()
    pyplot.savefig(os.path.join(log_dir, 'plot_accuracy.png'))
    pyplot.show(block=False)

    return

# train model
def train():
    # validate paths
    if not validate_paths():
        return

    # load data
    (data_flow_train, data_flow_valid) = load_data()

    # save labelmap
    with open(LABEL_MAPS, 'w') as file:
        json.dump(data_flow_train.class_indices, file)
    print('[INFO] Created labelmap')

    # build model
    print('[INFO] Building model... ', end='')
    model = build_model()
    if model is None:
        print('failed')
        return
    else:
        print('done')
        model.summary()

    # serialize model to json
    model_json = model.to_json()
    with open(mdl_file, 'w') as file:
        file.write(model_json)

    # create callbacks
    cb_list = callbacks()

    # acknowledgement
    data = {'chat_id': TELCHAT_ID,
            'text': '`Received a new training request.\nTASK ID: {}\nMODEL  : {}\nDATASET: {}`'\
            .format(cb_list[-1]._task_id, ARCHITECTURE.upper(), DATASET_ID.upper()),
            'parse_mode': 'Markdown'}
    cb_list[-1]._send_message(data)

    # train model
    train_history = model.fit_generator(generator=data_flow_train,
                                        steps_per_epoch=None,
                                        epochs=NUM_EPOCHS,
                                        verbose=1,
                                        callbacks=cb_list,
                                        validation_data=data_flow_valid,
                                        validation_steps=None,
                                        class_weight=None,
                                        max_queue_size=10,
                                        workers=1,
                                        use_multiprocessing=False,
                                        shuffle=True,
                                        initial_epoch=0)

    # plot learning curve
    plot(train_history)

    return

# main
if __name__ == '__main__':
    train()
    if F_SHUTDOWN:
        shutdown()


"""Utilities shared across the whole cog"""

# Built-in
import random
import re

# Third-party
from discord import Color

# Local
from .embed import create_embed, create_error_embed
from .settings import DEFAULT_USER_SETTINGS

# --------------------------------------------------------------------------------
# > Constants
# --------------------------------------------------------------------------------
DICE_REGEX = re.compile(r"(?P<qty>[1-9]\d{0,2})d(?P<sides>[1-9]\d{0,2})")
SIMPLE_ACTION_REGEX = re.compile(r"adv|dis|crit")
COMPLEX_ACTION_REGEX = re.compile(r"(?P<action>dl|dh|kl|kh)(?P<value>[1-9]\d{0,2})")
CHECK_REGEX = re.compile(r"(?P<comparator>=|!=|>|<|>=|<=)(?P<value>[1-9]\d{0,4})")
MODIFIER_REGEX = re.compile(r"[-+][1-9]\d{0,4}")

# --------------------------------------------------------------------------------
# > Base helpers
# --------------------------------------------------------------------------------
def generate_discord_markdown_string(lines):
    """
    Wraps a list of message into a discord markdown block
    :param [str] lines:
    :return: The wrapped string
    :rtype: str
    """
    output = ["```markdown"] + lines + ["```"]
    return "\n".join(output)

class Die:
    """A die you can roll"""

    def __init__(self, sides):
        """
        Creates a die of N sides that can be rolled
        :param int sides: Number of sides the die have
        """
        self.sides = sides
        self.value = None

    def roll(self):
        """
        :return: Rolls the die and returns the value
        :rtype: int
        """
        self.value = random.randint(1, self.sides)
        return self.value

    def copy(self):
        """
        :return: Creates and returns an identical Die from our instance
        :rtype: Die
        """
        die = Die(self.sides)
        die.value = self.value
        return die

class RollComponent:
    """Provides the skeleton for a DiceRoll component"""

    def __init__(self, dice_roll):
        """
        Initializes the component
        :param DiceRoll dice_roll: The DiceRoll instance the component is linked to
        """
        self.dice_roll = dice_roll
        self.errors = []

    def validate(self):
        """Should fill the `errors` property"""
        NotImplemented()

    def apply(self):
        """Should update the `dice_roll.total`"""
        NotImplemented()

    def update_embed(self, embed):
        """
        Should add a field and content to an existing embed
        :param Embed embed: An existing discord Embed message
        """
        NotImplemented()

# --------------------------------------------------------------------------------
# > Modifier Component
# --------------------------------------------------------------------------------
class RollModifier(RollComponent):
    """Applies a modifier at the end of your DiceRoll"""

    def __init__(self, dice_roll, value):
        """
        Initializes the instance
        :param DiceRoll dice_roll: The DiceRoll instance to link it to
        :param int value: The modifier amount (can be negative)
        """
        super().__init__(dice_roll)
        self.value = value

    def validate(self):
        """Adds an error if the value is at 0"""
        if self.value == 0:
            message = "[Modifier] Cannot have a modifier of 0"
            self.errors.append(message)

    def apply(self):
        """Updates the DiceRoll total by adding the modifier value"""
        self.dice_roll.total += self.value

    def update_embed(self, embed):
        """
        Adds a field which indicates the new total after the modifier was applied
        :param Embed embed: The embed massage to update
        """
        if not self.dice_roll.settings["verbose"]:
            return
        sign = "+" if self.value > 0 else "-"
        abs_value = abs(self.value)
        previous_total = self.dice_roll.total - self.value
        message = f"# {previous_total} {sign} {abs_value} = {self.dice_roll.total}"
        text = generate_discord_markdown_string([message])
        embed.add_field(
            name=f"Modifier {sign}{abs_value}",
            value=text,
            inline=False,
        )

# --------------------------------------------------------------------------------
# > Check Component
# --------------------------------------------------------------------------------
class RollCheck(RollComponent):
    """Performs a check at the very end of your DiceRoll"""

    def __init__(self, dice_roll, comparator, value):
        """
        Initializes the instance
        :param DiceRoll dice_roll: The DiceRoll instance to link it to
        :param str comparator: Comparaison that will be used, like > or <=
        :param int value: The value on the right side of the equation
        """
        super().__init__(dice_roll)
        self.comparator = comparator
        self.value = value
        self.success = None

    def validate(self):
        """Nothing to validate. Already done by the regex in DiceRoll"""
        pass

    def apply(self):
        """Compares the DiceRoll total to the provided value, using the comparator"""
        self.success = eval(f"{self.dice_roll.total} {self.comparator} {self.value}")

    def update_embed(self, embed):
        """
        Updates the title and color of the message based on the check results
        :param Embed embed: The embed message to update
        """
        if self.success:
            color = Color.green()
            title = f"Success with {self.dice_roll.total}!"
        else:
            color = Color.orange()
            title = f"Failure with {self.dice_roll.total}!"
        embed.title = title
        embed.color = color

# --------------------------------------------------------------------------------
# > Action Component
# --------------------------------------------------------------------------------
def create_roll_action(dice_roll, name, value=0):
    """
    Creates and returns the matching action using the right parameters
    :param DiceRoll dice_roll: The DiceRoll instance to link it to
    :param str name: Name or shortcut of the action
    :param int value: Value associated with the action
    :return: The created BaseAction that matches the provided name
    :rtype: BaseAction
    """
    action_map = {
        "dl": (KeepDropAction, [value, False, False]),
        "dh": (KeepDropAction, [value, False, True]),
        "kl": (KeepDropAction, [value, True, False]),
        "kh": (KeepDropAction, [value, True, True]),
        "adv": (Advantage, []),
        "dis": (Disadvantage, []),
        "crit": (CriticalHit, []),
    }
    action_class, args = action_map[name]
    return action_class(dice_roll, *args)

class BaseAction(RollComponent):
    """Base class to provide utilities to all actual Actions"""

    name = None

    def __init__(self, dice_roll):
        """
        Initializes the action
        :param DiceRoll dice_roll: The DiceRoll instance to link it to
        """
        super().__init__(dice_roll)
        self.dice_roll = dice_roll

    def _validate_one_die(self):
        """Adds an error if there is more than 1 dice"""
        if len(self.dice_roll.dice) != 1:
            message = f"[Action] `{self.name}` action can only be used with 1 die."
            self.errors.append(message)

    def _validate_one_die_type(self):
        """Adds an error if we have several dice types"""
        dice_sides = {die.sides for die in self.dice_roll.dice}
        if len(dice_sides) > 1:
            message = f"[Action] `{self.name}` action cannot be used with dice of different sizes."
            self.errors.append(message)

class Advantage(BaseAction):
    """Action to roll a second die and keep the best one"""

    name = "Advantage"

    def __init__(self, dice_roll):
        """
        Initializes the action and its state
        :param DiceRoll dice_roll: The DiceRoll instance to link it to
        """
        super().__init__(dice_roll)
        self.existing_die = None
        self.die = None

    def validate(self):
        """Checks if the DiceRoll only has one die"""
        self._validate_one_die()

    def apply(self):
        """Copies the existing die, re-rolls it, and keeps it if it's better"""
        self.existing_die = self.dice_roll.dice[0]
        self.die = self.existing_die.copy()
        self.die.roll()
        if self.die.value > self.existing_die.value:
            self.dice_roll.total = self.die.value

    def update_embed(self, embed):
        """
        Adds a field with the action result
        :param Embed embed: Embed message to update
        """
        if not self.dice_roll.settings["verbose"]:
            return
        if self.die.value > self.existing_die.value:
            result = f"Rolled {self.die.value} and kept it!"
        else:
            result = f"Rolled {self.die.value} and discarded it!"
        text = generate_discord_markdown_string([result])
        embed.add_field(
            name=self.name,
            value=text,
            inline=False,
        )

class Disadvantage(BaseAction):
    """Action to roll a second die and keep the worse one"""

    name = "Disadvantage"

    def __init__(self, dice_roll):
        """
        Initializes the action and its state
        :param DiceRoll dice_roll: The DiceRoll instance to link it to
        """
        super().__init__(dice_roll)
        self.existing_die = None
        self.die = None

    def validate(self):
        """Checks if the DiceRoll only has one die"""
        self._validate_one_die()

    def apply(self):
        """Copies the existing die, re-rolls it, and keeps it if it's worse"""
        self.existing_die = self.dice_roll.dice[0]
        self.die = self.existing_die.copy()
        self.die.roll()
        if self.die.value < self.existing_die.value:
            self.dice_roll.total = self.die.value

    def update_embed(self, embed):
        """
        Adds a field with the action result
        :param Embed embed: Embed message to update
        """
        if not self.dice_roll.settings["verbose"]:
            return
        if self.die.value < self.existing_die.value:
            result = f"Rolled {self.die.value} and kept it!"
        else:
            result = f"Rolled {self.die.value} and discarded it!"
        text = generate_discord_markdown_string([result])
        embed.add_field(
            name=self.name,
            value=text,
            inline=False,
        )

class CriticalHit(BaseAction):
    """Action to make a critical hit and double your dice/damage output"""

    name = "Critical hit"

    def validate(self):
        """Nothing to validate"""
        pass

    def apply(self):
        """Multiplies the dice score by 2"""
        self.before_total = self.dice_roll.total
        self.dice_roll.total *= 2
        self.after_total = self.dice_roll.total

    def update_embed(self, embed):
        """
        Adds a field indicating the new total
        :param Embed embed: Embed message to update
        """
        if not self.dice_roll.settings["verbose"]:
            return
        messages = [
            "All your dice scores are multiplied by 2",
            f"# {self.before_total} x 2 = {self.after_total}",
        ]
        text = generate_discord_markdown_string(messages)
        embed.add_field(
            name=self.name,
            value=text,
            inline=False,
        )

class KeepDropAction(BaseAction):
    """Action to keep or drop dice"""

    def __init__(self, dice_roll, amount, keep, high):
        """
        Initializes the action and its state
        :param DiceRoll dice_roll: The DiceRoll instance to link it to
        :param int amount: Amount of dice to keep or drop
        :param bool keep: Whether we keep (or drop)
        :param bool high: Whether the remaining dice are the highest (or lowest)
        """
        super().__init__(dice_roll)
        self.remaining_dice = []
        self.discarded_dice = []
        self.amount = amount
        self.keep = keep
        self.high = high
        self.name = self._compute_name()

    def validate(self):
        """Checks the dice count and types"""
        self._validate_one_die_type()
        self._validate_drop_keep_count(self.amount)

    def apply(self):
        """
        Keeps/drops the dice by splitting them into `remaining` and `discarded`
        Then updates the total by add the `remaining` only
        """
        dice = self.dice_roll.dice.copy()
        dice.sort(key=lambda x: x.value, reverse=self.high)
        if self.keep:
            # Keep High
            if self.high:
                self.remaining_dice = dice[: self.amount]
                self.discarded_dice = dice[self.amount :]
            # Keep Low
            else:
                self.remaining_dice = dice[self.amount :]
                self.discarded_dice = dice[: self.amount]
        else:
            # Drop High
            if self.high:
                self.remaining_dice = dice[self.amount :]
                self.discarded_dice = dice[: self.amount]
            # Drop Low
            else:
                self.remaining_dice = dice[: self.amount]
                self.discarded_dice = dice[self.amount :]
        self.before_total = self.dice_roll.total
        self.dice_roll.total = sum([die.value for die in self.remaining_dice])
        self.after_total = self.dice_roll.total

    def update_embed(self, embed):
        """
        Adds a field which list the discarded and remaining dice
        :param Embed embed: The embed message to update
        """
        if not self.dice_roll.settings["verbose"]:
            return
        remaining_values = [str(die.value) for die in self.remaining_dice]
        discarded_values = [str(die.value) for die in self.discarded_dice]
        messages = [
            f"[Discarded dice]({', '.join(discarded_values)})",
            f"[Remaining dice]({', '.join(remaining_values)})",
            f"Went down from {self.before_total} to {self.after_total}",
        ]
        text = generate_discord_markdown_string(messages)
        embed.add_field(
            name=self.name,
            value=text,
            inline=False,
        )

    def _compute_name(self):
        """
        :return: Computes and returns the action name based on its attributes
        :rtype: str
        """
        verb = "Keep" if self.keep else "Drop"
        direction = "high" if self.high else "low"
        return f"{verb} {direction} {self.amount}"

    def _validate_drop_keep_count(self, n):
        """
        Adds an error if we cannot drop/keep that many dice
        :param int n: Number of dice to drop/keep
        """
        if len(self.dice_roll.dice) <= n:
            message = f"[Action] You must roll more dice (`{n}`) than what you drop/keep (`{self.amount}`)."
            self.errors.append(message)

# --------------------------------------------------------------------------------
# > Dice Roll
# --------------------------------------------------------------------------------
class DiceRoll:
    """The state and action of rolling dice with various options"""

    def __init__(self, instructions, settings):
        """
        Initializes the state, then parses and validates the instructions
        :param [str] instructions: The user's instructions, like "1d6" or "adv"
        :param dict settings: The settings to use in ths roll
        """
        self.instructions = instructions
        self.settings = {**DEFAULT_USER_SETTINGS, **settings}
        # Roll parameters
        self.dice = []
        self.modifier = None
        self.action = None
        self.check = None
        # Results
        self.total = 0
        self.rolled = False
        # Error control
        self.action_counter = 0
        self.check_counter = 0
        self.modifier_counter = 0
        self._errors = []
        # Parsing
        self._parse_instructions()
        self._validate()

    # ----------------------------------------
    # API properties
    # ----------------------------------------
    @property
    def components(self):
        """
        :return: The ordered component instances linked to our DiceRoll
        :rtype: [RollComponent]
        """
        # Order matters
        potential_components = [self.action, self.modifier, self.check]
        return [c for c in potential_components if c is not None]

    @property
    def is_valid(self):
        """
        :return: Whether the instance is valid and can be played
        :rtype: bool
        """
        return len(self.errors) == 0

    @property
    def errors(self):
        """
        :return: The instance's and its components' errors
        :rtype: [str]
        """
        component_errors = []
        for component in self.components:
            component_errors.extend(component.errors)
        return self._errors + component_errors

    @property
    def errors_as_embed(self):
        """
        :return: Formats the errors into a Discord Embed
        :rtype: Embed
        """
        description = "\n".join(self.errors)
        return create_error_embed(description=description)

    @property
    def result_as_embed(self):
        """
        :return: Formats our instance's result into a Discord Embed
        :rtype: Embed
        """
        title = f"You rolled {self.total}"
        embed = create_embed(title=title)
        if self.settings["verbose"]:
            self._update_embed_with_dice_rolls(embed)
        for component in self.components:
            # "verbose" is handled individually in each component
            component.update_embed(embed)
        return embed

    # ----------------------------------------
    # API methods
    # ----------------------------------------
    def roll(self):
        """
        If valid: rolls the dice, applies all components, and returns the results
        Else: returns the errors
        :return: The embed results or errors
        :rtype: Embed
        """
        # Maybe skip
        if self.rolled:
            raise RuntimeError("This DiceRoll has already been rolled")
        if not self.is_valid:
            return self.errors_as_embed
        # Do roll
        for die in self.dice:
            self.total += die.roll()
        for component in self.components:
            component.apply()
        self.rolled = True
        return self.result_as_embed

    def copy(self):
        """
        :return: A new DiceRoll using our instance's instructions and settings
        :rtype: DiceRoll
        """
        return DiceRoll(self.instructions, self.settings)

    # ----------------------------------------
    # Helpers: parsing
    # ----------------------------------------
    def _parse_instructions(self):
        """Tries to parse all instruction using our regexes"""
        parsing_functions = [
            self._maybe_parse_dice,
            self._maybe_parse_action,
            self._maybe_parse_modifier,
            self._maybe_parse_check,
        ]
        for instruction in self.instructions:
            for parsing_func in parsing_functions:
                if parsing_func(instruction):
                    break
            else:
                message = (
                    f"[Instruction] Did not understand the instruction: `{instruction}`"
                )
                self._errors.append(message)

    def _maybe_parse_dice(self, instruction):
        """
        Checks if the instruction is a dice roll
        :param str instruction: String to parse
        :return: Whether it was a match
        :rtype: bool
        """
        match = re.fullmatch(DICE_REGEX, instruction)
        if match is not None:
            qty = int(match.group("qty"))
            sides = int(match.group("sides"))
            for i in range(qty):
                self.dice.append(Die(sides))
            return True
        return False

    def _maybe_parse_action(self, instruction):
        """
        Checks if the instruction is an action call
        :param str instruction: String to parse
        :return: Whether it was a match
        :rtype: bool
        """
        match = re.fullmatch(SIMPLE_ACTION_REGEX, instruction)
        if match is not None:
            self.action = create_roll_action(self, instruction)
            self.action_counter += 1
            return True
        match = re.fullmatch(COMPLEX_ACTION_REGEX, instruction)
        if match is not None:
            action_text = match.group("action")
            value = int(match.group("value"))
            self.action = create_roll_action(self, action_text, value)
            self.action_counter += 1
            return True
        return False

    def _maybe_parse_modifier(self, instruction):
        """
        Checks if the instruction is to apply a modifier
        :param str instruction: String to parse
        :return: Whether it was a match
        :rtype: bool
        """
        match = re.fullmatch(MODIFIER_REGEX, instruction)
        if match is not None:
            value = int(instruction)
            self.modifier = RollModifier(self, value)
            self.modifier_counter += 1
            return True
        return False

    def _maybe_parse_check(self, instruction):
        """
        Checks if the instruction is roll check/condition
        :param str instruction: String to parse
        :return: Whether it was a match
        :rtype: bool
        """
        match = re.fullmatch(CHECK_REGEX, instruction)
        if match is not None:
            comparator = match.group("comparator")
            value = int(match.group("value"))
            self.check = RollCheck(self, comparator, value)
            self.check_counter += 1
            return True
        return False

    # ----------------------------------------
    # Helpers: validation
    # ----------------------------------------
    def _validate(self):
        """Checks if our instance and its components are valid based on their states"""
        # Has dice
        if len(self.dice) == 0:
            message = "[Dice] You must provide at least one die (example: `1d6`)"
            self._errors.append(message)
        # Has 1 component of each max
        for text, counter in zip(
            ["Action", "Modifier", "Check"],
            [self.action_counter, self.modifier_counter, self.check_counter],
        ):
            if counter > 1:
                message = f"[{text}] You can only declare 1 {text.lower()} (provided: `{counter}`)"
                self._errors.append(message)
        # We check components only if no error so far
        if len(self._errors) > 0:
            return
        for component in self.components:
            component.validate()

    # ----------------------------------------
    # Helpers: output
    # ----------------------------------------
    def _update_embed_with_dice_rolls(self, embed):
        """
        Adds a `Dice` recap to the embed
        :param Embed embed: The embed to update
        """
        dice_per_sides = {}
        for die in self.dice:
            existing_list = dice_per_sides.get(die.sides, [])
            existing_list.append(die.value)
            dice_per_sides[die.sides] = existing_list
        lines = []
        total_score = 0
        for sides, values in dice_per_sides.items():
            line_score = sum(values)
            total_score += line_score
            string_values = [str(v) for v in values]
            line = f"[{len(values)}d{sides}]({', '.join(string_values)}) = {line_score}"
            lines.append(line)
        lines.append(f"# {total_score}")
        text = generate_discord_markdown_string(lines)
        embed.add_field(
            name="Dice rolls",
            value=text,
            inline=False,
        )


from cProfile import label
from tkinter import*
from tkinter import messagebox
from tkinter.filedialog import askopenfilename
import os
# import win32com.client
from pdf2docx import Converter

class ventana(Tk):
    # Constructor
    def __init__(self,*args,**kwargs):
        super().__init__(*args,**kwargs)
        self.title("Convertidor de pdf a word")
        self.geometry("300x200")
        self.resizable(0,0)
        self.widgest()

    def salir(self):
        valor = messagebox.askquestion(title="Convertidor de pdf a word",message="¿Esta seguro que deseas salir?")
        if valor == "yes":
            self.destroy()

    def archivo(self):
        valor = askopenfilename(defaultextension=".pdf",filetypes=[("Selecciona solo archivos .pdf","*.pdf")])
        if valor:
            self.generate_word2(valor)

    def generate_word2(self,valor):
        try:
            labelFile = Label(self,text="Cargando...")
            labelFile.place(x=30,y=39)
            ruta = os.path.abspath(valor)
            ruta_final = os.path.abspath(valor[0:-4] +".docx".format())
            cv = Converter(ruta)
            cv.convert(ruta_final,start=0,end=None)
            cv.close()
            labelFile.destroy()
            messagebox.showinfo(title="Convertidor de pdf a word",message='El archivo se genero correctamente en la siguiente ruta: '+ruta_final)
        except Exception as e :
            messagebox.showerror(title="Convertidor de pdf a word",message=str(e))

    # def generate_word(self,valor):
    #     try:
    #         word = win32com.client.Dispatch("word.Application")
    #         word.visible = 0
    #         ruta = os.path.abspath(valor)
    #         wb=word.Documents.Open(ruta)
    #         ruta_final = os.path.abspath(valor[0:-4] +"docx".format())
    #         wb.SaveAs2(ruta_final,FileFormat=16)
    #         wb.Close()
    #         word.Quit()
    #         # labelFile.destroy()
    #         messagebox.showinfo(title="Convertidor de pdf a word",message='El archivo se genero correctamente en la siguiente ruta: '+ruta_final)
    #     except Exception as e :
    #         messagebox.showerror(title="Convertidor de pdf a word",message=str(e))

    def widgest(self):
        labelFile=Label(self,text="Selecciona el pdf que deseas convertir.")
        labelFile.place(x=30,y=15)
        # Botones
        btnquitar=Button(self, text="Seleccionar Archivo", command=self.archivo, width=35)
        btnquitar.place(x=30, y=90)

        btnquitar=Button(self,text="Salir", command=self.salir, width=35)
        btnquitar.place(x=30, y=130)

if __name__ == "__main__":
    app=ventana()
    app.mainloop()


import asyncio
from datetime import datetime

from aiogram import types
from aiogram.utils.markdown import code

from init import bot, config, dp
from utils import try_sending_message
from verify import *
from verify import Status

# init

async def on_startup(app) -> None:
    """Simple hook for aiohttp application which manages webhook"""
    await bot.delete_webhook()
    await bot.set_webhook(config["webhook_url"])

async def start_worker() -> None:
    await bot.delete_webhook()
    await dp.start_polling()

# handlers

@dp.callback_query_handler()
async def pressed_verification_button(cb: types.CallbackQuery) -> None:
    user_id = cb.from_user.id
    chat_id = cb.message.chat.id
    key = cb.data

    if not Verify.can_verify(chat_id, user_id):
        return

    if key == config["key"]:
        await Verify.authorize(chat=cb.message.chat, user_id=user_id)
        text = f"Welcome {code(cb.from_user.full_name)}, have a lot of fun!"
        await try_sending_message(bot, chat_id=chat_id, text=text, parse_mode="Markdown")

    elif Verify.has_last_chance(chat_id, user_id):
        text = "Incorrect answer. Make sure to get it right next time or you will be banned *permanently*."
        response_msg = await try_sending_message(bot,
            chat_id=chat_id, text=text, parse_mode="Markdown"
        )
        Verify.chats[chat_id].users[user_id].pending_messages_ids.append(
            response_msg.message_id
        )
    else:
        await Verify.reject(bot, cb.message.chat, user_id)

@dp.message_handler(content_types=types.ContentTypes.NEW_CHAT_MEMBERS)
async def just_joined(message: types.Message) -> None:
    user_id: int = message.from_user.id
    user_name: str = message.from_user.username or message.from_user.full_name
    chat = message.chat

    if not Verify.can_request_verification(chat.id, user_id):
        return

    values = [getattr(u, "_values") for u in message.new_chat_members]
    uids = [u["id"] for u in values if not u["is_bot"]]

    if not uids:
        return

    response_msg = await try_sending_message(
        bot,
        chat_id=chat.id,
        text=f"Hi [@{user_name}](tg://user?id={str(user_id)})! Please answer the question below within the next (*{config['delay']} seconds*). Which emoji below represents an animal often associated with openSUSE?",
        parse_mode="Markdown",
        reply_markup=create_verification_keyboard(),
    )

    for task in asyncio.as_completed([Verify.restrict(chat, _id) for _id in uids]):
        uid = await task
        if not chat.id in Verify.chats:
            Verify.chats[chat.id] = Chat(chat.id)
        Verify.chats[chat.id].users[uid] = User(
            pending_messages_ids=[response_msg.message_id],
            joined_at=datetime.now(),
            attempts=0,
            status=Status.challenged_to_verify,
        )
        Verify.schedule_reject(bot, chat, uid)

@dp.message_handler(content_types=types.ContentTypes.ANY)
async def handle_otherwise(_any) -> None:
    print(f"Silently handled: {_any}")

if __name__ == "__main__":
    from sys import argv

    import uvloop

    loop = uvloop.new_event_loop()
    asyncio.set_event_loop(loop)

    if len(argv) >= 2 and "--webhook" in argv:
        print(
            f"Called with {argv}, running as aiohttp server after setting webhook."
        )
        from aiogram.utils import executor

        executor.start_webhook(
            dispatcher=dp,
            webhook_path=config['webhook_path'],
            skip_updates=True,
            on_startup=on_startup,
            host=config["app_host"],
            port=config["app_port"],
        )
    else:
        print(f"Called with {argv}, running as long-polling worker.")
        asyncio.run(start_worker())


import sys
import time
import numpy as np
import logging
import tensorflow as tf
from os.path import join as pjoin
from tqdm import tqdm

from evaluate import f1_score, exact_match_score

class QaSystemSolver(object):
    def __init__(self, model, dataset, answers, raw_answers, rev_vocab, **kwargs):

        self.model = model
        self.dataset = dataset
        self.answers = answers
        self.raw_answers = raw_answers
        self.rev_vocab = rev_vocab

        self.batch_size = kwargs.pop('batch_size', 64)
        self.start_lr = kwargs.pop('start_lr', 0.0001)
        self.epochs = kwargs.pop('epochs', 5)

        self.start_steps = kwargs.pop('start_steps', 0)
        self.print_every = kwargs.pop('print_every', 1000)
        self.save_every = kwargs.pop('save_every', 1000)
        self.save_every_epoch = kwargs.pop('save_every_epoch', True)

        self.sample = kwargs.pop('sample', 0)
        self.debug_num = kwargs.pop('debug_num', 0)
        self.max_grad_norm = kwargs.pop('max_grad_norm', 5.0)

        self.opt = kwargs.pop('opt', 'adam')
        self.train_dir = kwargs.pop('train_dir', 'output/train')
        self.summary_dir = kwargs.pop('summary_dir', 'output/tensorboard/summary')

        if self.opt == "adam":
            self.optimizer = tf.train.AdamOptimizer

        self.global_step = tf.Variable(self.start_steps, trainable=False)
        self.starter_learning_rate = tf.placeholder(tf.float32, name='start_lr')

    def train(self):
        tic = time.time()
        params = tf.trainable_variables()
        num_params = sum(map(lambda t: np.prod(tf.shape(t.value()).eval()), params))
        toc = time.time()
        logging.info("Number of params: %d (retreival took %f secs)" % (num_params, toc - tic))

        # train_context -> (num, 2, max_length)
        train_context = np.array(self.dataset['train_context'])
        train_question = np.array(self.dataset['train_question'])
        # train_answer -> (num, 2)
        train_answer = np.array(self.answers['train_answer'])

        if self.debug_num:
            assert isinstance(self.debug_num, int), 'the debug number should be a integer'
            assert self.debug_num < len(train_answer), 'check debug number!'
            train_answer = train_answer[0:self.debug_num]
            train_context = train_context[0:self.debug_num]
            train_question = train_question[0:self.debug_num]
            print_every = 5

        num_example = len(train_answer)
        logging.info('num example is {}'.format(num_example))
        shuffle_list = np.arange(num_example)

        learning_rate = tf.train.exponential_decay(self.starter_learning_rate, self.model.global_step,
                                                   1000, 0.9, staircase=True)
        tf.summary.scalar('learning_rate', learning_rate)

        with tf.name_scope('optimizer'):
            optimizer = self.optimizer(learning_rate)
            grad_var = optimizer.compute_gradients(self.model.final_loss)
            grad = [i[0] for i in grad_var]
            var = [i[1] for i in grad_var]
            grad_norm = tf.global_norm(grad)
            tf.summary.scalar('grad_norm', grad_norm)
            grad, use_norm = tf.clip_by_global_norm(grad, self.max_grad_norm)
            train_op = optimizer.apply_gradients(zip(grad, var), global_step=self.model.global_step)

        saver = tf.train.Saver()
        merged = tf.summary.merge_all()

        losses = []
        norms = []
        train_evals = []
        val_evals = []
        iters = self.start_steps
        save_path = pjoin(self.train_dir, 'weights')
        batch_size = self.batch_size
        batch_num = int(num_example * 1.0 / batch_size)
        total_iterations = self.epochs * batch_num + self.start_steps

        config = tf.ConfigProto(allow_soft_placement=True)
        config.gpu_options.allow_growth = True
        with tf.Session(config=config) as sess:
            train_writer = tf.summary.FileWriter(self.summary_dir + str(self.start_lr),
                                                              sess.graph)
            tic = time.time()

            for ep in range(self.epochs):
                np.random.shuffle(shuffle_list)
                train_context = train_context[shuffle_list]
                train_question = train_question[shuffle_list]
                train_answer = train_answer[shuffle_list]

                logging.info('training epoch ---- {}/{} -----'.format(ep + 1, self.epochs))
                ep_loss = 0.

                for it in xrange(batch_num):
                    sys.stdout.write('> %d / %d \r' % (iters % print_every, print_every))
                    sys.stdout.flush()

                    context = train_context[it * batch_size: (it + 1) * batch_size]
                    question = train_question[it * batch_size: (it + 1) * batch_size]
                    answer = train_answer[it * batch_size: (it + 1) * batch_size]

                    input_feed = self.load_input_feed(context, question, answer)
                    batch_final_loss = self.model.final_loss
                    summary, _,  loss, grad_norm= sess.run([merged, train_op, batch_final_loss, grad_norm], input_feed)

                    train_writer.add_summary(summary, iters)
                    ep_loss += loss
                    losses.append(loss)
                    norms.append(grad_norm)
                    iters += 1

                    if iters % print_every == 0:
                        toc = time.time()
                        logging.info('iters: {}/{} loss: {} norm: {}. time: {} secs'.format(
                            iters, total_iterations, loss, grad_norm, toc - tic))
                        tf1, tem, f1, em = self.evaluate_answer(sess, self.dataset, self.raw_answers, self.rev_vocab,
                                                                training=True, log=True, sample=self.sample)
                        train_evals.append((tf1, tem))
                        val_evals.append((f1, em))
                        tic = time.time()

                    if iters % self.save_every == 0:
                        saver.save(sess, save_path, global_step=iters)
                        self.evaluate_answer(sess, self.dataset, self.raw_answers, self.rev_vocab,
                                             training=True, log=True, sample=self.sample)

                if self.save_every_epoch:
                    saver.save(sess, save_path, global_step=iters)
                    self.evaluate_answer(sess, self.dataset, self.raw_answers, self.rev_vocab,
                                         training=True, log=True, sample=4000)
                logging.info('average loss of epoch {}/{} is {}'.format(ep + 1, self.epochs, ep_loss / batch_num))

                data_dict = {'losses': losses, 'norms': norms,
                             'train_eval': train_evals, 'val_eval': val_evals}
                c_time = time.strftime('%Y%m%d_%H%M', time.localtime())
                data_save_path = pjoin('cache', str(iters) + 'iters' + c_time + '.npz')
                np.savez(data_save_path, data_dict)

    def evaluate_answer(self, sess, dataset, raw_answers, rev_vocab,
                        sample=(100, 100), log=False, training=False):
        """
        Evaluate the model's performance using the harmonic mean of F1 and Exact Match (EM)
        with the set of true answer labels

        This step actually takes quite some time. So we can only sample 100 examples
        from either training or testing set.

        :param session: session should always be centrally managed in train.py
        :param dataset: a representation of our data, in some implementations, you can
                        pass in multiple components (arguments) of one dataset to this function
        :param sample: how many examples in dataset we look at
        :param log: whether we print to std out stream
        :return:
        """
        if not isinstance(rev_vocab, np.ndarray):
            rev_vocab = np.array(rev_vocab)

        if not isinstance(sample, tuple):
            sample = (sample, sample)

        tf1 = 0.
        tem = 0.

        input_batch_size = 100

        if training:
            train_len = sample[0]

            train_context = dataset['train_context'][:train_len]
            train_question = dataset['train_question'][:train_len]
            train_answer = raw_answers['raw_train_answer'][:train_len]

            train_a_s = np.array([], dtype=np.int32)
            train_a_e = np.array([], dtype=np.int32)

            for i in tqdm(range(train_len // input_batch_size), desc='trianing set'):
                train_as, train_ae = self.answer(self.model, sess,
                                                 np.array(train_context[i * input_batch_size:(i + 1) * input_batch_size]),
                                                 np.array(train_question[i * input_batch_size:(i + 1) * input_batch_size]))

                train_a_s = np.concatenate((train_a_s, train_as), axis=0)
                train_a_e = np.concatenate((train_a_e, train_ae), axis=0)

            # a_s and a_e -> (sample_num)
            for i in range(train_len):
                prediction_ids = train_context[i][0][train_a_s[i]:train_a_e[i]+1]
                prediction_answer = ' '.join(rev_vocab[prediction_ids])
                raw_answer = train_answer[i]
                tf1 += f1_score(prediction_answer, raw_answer)
                tem += exact_match_score(prediction_answer, raw_answer)
                # if i < 10:
                #     print("predict_answer: ", prediction_answer)
                #     print("ground truth: ", raw_answer)
                #     print ("f1: ", f1_score(prediction_answer, raw_answer))

            if log:
                logging.info("Training set ==> F1: {}, EM: {}, for {} samples".
                             format(tf1 / train_len, tem / train_len, train_len))

        f1 = 0.
        em = 0.
        val_len = sample[1]

        val_context = dataset['val_context'][:val_len]
        val_question = dataset['val_question'][:val_len]
        val_answer = raw_answers['raw_val_answer'][:val_len]

        val_a_s = np.array([], dtype=np.int32)
        val_a_e = np.array([], dtype=np.int32)

        for i in tqdm(range(val_len // input_batch_size), desc='val set'):
            val_as, val_ae = self.answer(self.model, sess,
                                         np.array(val_context[i * input_batch_size:(i + 1) * input_batch_size]),
                                         np.array(val_question[i * input_batch_size:(i + 1) * input_batch_size]))

            val_a_s = np.concatenate((val_a_s, val_as), axis=0)
            val_a_e = np.concatenate((val_a_e, val_ae), axis=0)

        # a_s and a_e -> (sample_num)
        for i in range(val_len):
            prediction_ids = val_context[i][0][val_a_s[i]:val_a_e[i]+1]
            prediction_answer = ' '.join(rev_vocab[prediction_ids])
            raw_answer = val_answer[i]
            f1 += f1_score(prediction_answer, raw_answer)
            em += exact_match_score(prediction_answer, raw_answer)
            # if i < 10:
            #     print("predict_answer: ", prediction_answer)
            #     print("ground truth: ", raw_answer)
            #     print ("f1: ", f1_score(prediction_answer, raw_answer))

        if log:
            logging.info("val set ==> F1: {}, EM: {}, for {} samples".
                         format(f1 / val_len, em / val_len, val_len))

        if training:
            return tf1/train_len, tem/train_len, f1/val_len, em/val_len
        else:
            return f1/val_len, em/val_len

    def answer(self, model, session, context, question):
        """
        Returns the probability distribution over different positions in the paragraph
        so that other methods like self.answer() will be able to work properly
        :return:
        """
        input_feed = {}
        input_feed[model.context] = context[:, 0, :]
        input_feed[model.context_m] = context[:, 1, :]
        input_feed[model.question] = question[:, 0, :]
        input_feed[model.question_m] = question[:, 1, :]

        output_feed = [model.s_score, model.e_score]

        s_score, e_score = session.run(output_feed, input_feed)

        a_s = np.argmax(s_score, axis=1)
        a_e = np.argmax(e_score, axis=1)

        return a_s, a_e

    def load_input_feed(self, context, question, answer):
        model = self.model
        input_feed = {}
        input_feed[model.context] = context[:, 0, :]
        input_feed[model.context_m] = context[:, 1, :]
        input_feed[model.question] = question[:, 0, :]
        input_feed[model.question_m] = question[:, 1, :]
        input_feed[model.answer_s] = answer[:, 0]
        input_feed[model.answer_e] = answer[:, 1]
        input_feed[model.starter_learning_rate] = self.start_lr
        return input_feed


    # ZeroDivisionError: Occurs when a number is divided by zero.
    # NameError: It occurs when a name is not found. It may be local or global.
    # IndentationError: If incorrect indentation is given.
    # IOError: It occurs when Input Output operation fails.
    # EOFError: It occurs when the end of the file is reached, and yet operations are being performed.

a=int(input("Enter A:"))
b=int(input("Enter B:"))

try:
    c=a/b
    print(c)
except Exception:
    print("Divide by zero error")


# 9-13 Practice
from collections import OrderedDict

word_dict = OrderedDict()
word_dict['if'] = "如果"
word_dict['else'] = "其他情况"
word_dict['for'] = "有限循环"
word_dict['while'] = "无限循环"
word_dict['raise'] = "抛出异常"
word_dict['try'] = "捕获异常"
word_dict['catch'] = "处理异常"
word_dict['finally'] = "捕获异常之后"
word_dict['def'] = "函数"
word_dict['break'] = "停止"
for k, v in word_dict.items():
    print(k + ": " + v)

# 9-14 Practice
from random import randint

class Die():

    def __init__(self, sides=6):
        self.sides = sides

    def roll_die(self):
        return randint(1, self.sides)

six_sides = Die(6)
ten_sides = Die(10)
twenty_sides = Die(20)

for side in range(0, 10):
    print("第" + str(side + 1) + "次： " + str(six_sides.roll_die()))

for side in range(0, 10):
    print("第" + str(side + 1) + "次： " + str(ten_sides.roll_die()))

for side in range(0, 10):
    print("第" + str(side + 1) + "次： " + str(twenty_sides.roll_die()))

# 9-15 Practice
# Python module of the week


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Jan  6 20:19:21 2022

@author: erikkoppes
"""

##Simple Tm Calculator Function
##input should be in the form of string with quotations e.g "CGACTCTTAGCGGTGGATCA"
def SimpleTmCalc(primer):
    #Define input DNA sequence
    DNA_input = primer

    #define nucleotide length
    DNAlen = len(DNA_input)

    #Define GC count
    DNAgc = DNA_input.count('G') + DNA_input.count('C')

    #Define GC percent
    DNAgcp = round((DNAgc / DNAlen) * 100, 2)

    #Define final 5 nucleotides
    DNAfin = DNA_input[(DNAlen-5):]

    #Define GC count final 5
    DNAgcf = (DNAfin.count('G')) + (DNAfin.count('C'))

    #Tm calculation
    DNAtm = (DNAgc * 4) + ((DNAlen - DNAgc) * 2)

    ##reverse seq
    DNAr = DNA_input[::-1]

    ##reverse complement seq
    DNArc = ""
    for letter in DNAr:
        if letter == "A":
            DNArc = DNArc + "T"
        elif letter == "C":
            DNArc = DNArc + "G"
        elif letter == "G":
            DNArc = DNArc + "C"
        elif letter == "T":
           DNArc = DNArc + "A"

    #Print results
    print("Primer DNA Sequence (5' to 3'): " + DNA_input)
    print('\tPrimer Length (nt): ' + str(DNAlen))
    print('\tPrimer GC Count (nt): ' + str(DNAgc))
    print('\tPrimer GC Percent (%): ' + str(DNAgcp))
    print('\tPrimer Simple Tm (C): '  + str(DNAtm))
    print('\tPrimer Last 5nt: ' + DNAfin)
    print('\tPrimer GC Count Last 5nt: ' +str(DNAgcf))
    print('\tPrimer Reverse Complement is: ' + DNArc)


# Define battle attributes for Role2: NInja. Ninja excels in stealth but lacks magic.
ninja_health = 3
ninja_magic = 1
ninja_stealth = 3
ninja_intelligence = 2

# Iniitalize ninja function
def ninja():
    """
    This function returns the string "ninja" & is used for character selection.
    """
    return "ninja"


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

import matplotlib.pyplot as plt
import numpy as np
from matplotlib import animation as animation
import math

#definindo a classe
class Oscilador:
#inicialização
	def __init__(self, lado, x, v):
		self.m= rob*(lado**3)
		self.l= lado
		self.x= x
		self.v= v
#aceleração
	def a(self, x, v):
		return (rob-rof)*g*(self.l**3)/self.m - (k/self.m)*(x-x0) - ro*v/self.m
#movimentação
	def move(self, t):
		at= self.a(self.x, self.v)
		self.x= self.x + self.v*dt + 0.5*at*(dt**2)
		atem= self.a(self.x, self.v)
		vtem= self.v + 0.5*(at+atem)*dt
		atem= self.a(self.x, vtem)
		self.v= self.v + 0.5*(at+atem)*dt
		self.at= self.a(self.x, self.v)
#declaração das variáveis
g= 9.8
lado= 0.1
rob= 8000	#densidade do bloco
rof= 1250	#densidade do fluido
ro= 2		#coeficiente de proporcionalidade
k= 40		#constante elástica
x0= 0.5
dt= 0.1
t= 0
#objeto
o1= Oscilador(lado, 0.51, 0)
#arrays
tmax= 60
t= np.arange(0, tmax, dt)
x= np.zeros(t.size)
v= np.zeros(t.size)
x[0]= o1.x
v[0]= o1.v

for i in range(t.size):
	o1.move(t[i])
	x[i]= o1.x
	v[i]= o1.v

#masterização dos gráficos
fig = plt.figure()
plt.title('Oscilador', fontsize=12)
#gráfico 1
XT=fig.add_subplot(331, xlim=(0, tmax), ylim=(min(x)*1.05, max(x)*1.05))
XT.xaxis.grid(True)
XT.yaxis.grid(True)
plt.setp(XT.get_xticklabels(), visible=False)
plt.xlabel('Tempo (s)')
plt.ylabel('Posicao (m)')
line1, = XT.plot([], [], 'g-', lw=1)
plt.legend(loc='upper right')
#gráfico 2
VT=fig.add_subplot(334, xlim=(0, tmax), ylim=(min(v)*1.05, max(v)*1.05))
VT.xaxis.grid(True)
VT.yaxis.grid(True)
plt.setp(VT.get_xticklabels(), visible=False)
plt.xlabel('Tempo(s)')
plt.ylabel('Velocidade(m/s)')
line2, = VT.plot([], [], 'r-', lw=1)
plt.legend(loc='upper right')
#gráfico 3
VX=fig.add_subplot(122, xlim=(min(x)*1.05, max(x)*1.05), ylim=(min(v)*1.05, max(v)*1.05))
VX.xaxis.grid(True)
VX.yaxis.grid(True)
plt.setp(VX.get_xticklabels(), visible=False)
plt.xlabel('Posicao (m)')
plt.ylabel('Velocidade (m/s)')
line3, = VX.plot([], [], 'b.', lw=0.5)
plt.legend(loc='upper right')

#animação
def init():
	line1.set_data([],[])
	line2.set_data([],[])
	line3.set_data([],[])
	return line1, line2, line3,
def animate(i):
	tt= t[:i]
	xx= x[:i]
	vv= v[:i]
	line1.set_data(tt, xx)
	line2.set_data(tt, vv)
	line3.set_data(xx, vv)
	return line1, line2, line3,
#execução da animação
anim= animation.FuncAnimation(fig, animate, init_func= init, frames= t.size,
interval= 20, blit= True, repeat= False)
#salvar animação
anim.save('oscilador.mp4', fps=30, extra_args=['-vcodec', 'libx264'])
#mostrar animação
plt.show()


#!/usr/bin/env python
"""Setup script for installing sofia."""

from setuptools import setup

config = {
    'name': 'sofia',
    'version': '0.0.1',
    'description': 'GUI',
    'author': 'Danna Xue',
    'author email': 'dannaxue@stanford.edu',
    'url': 'https://github.com/dannaxue/sofia.git',
    'download_url': 'https://github.com/dannaxue/sofia',
    'license': 'MIT',
    'packages': ['sofia'],
    'scripts': ['bin/sofia']
}

setup(**config)