from __future__ import print_function
import matplotlib.pyplot as plt
import mpl_toolkits
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from numpy import linspace
import numpy as np
import cv2
import math
import os
from multiprocessing.dummy import Pool as ThreadPool
import glob
import pickle
import pandas as pd
from utils import *

np.set_printoptions(suppress=True)

class MonoChess_Calibrator:
    def __init__(self, pattern_type, pattern_rows, pattern_columns, distance_in_world_units=1.0,figsize=(12, 10), debug_dir=None,
                 term_criteria=(cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_COUNT, 1000, 0.0001)):

        pattern_types = ["chessboard", "symmetric_circles", "asymmetric_circles", "custom"]
        assert pattern_type in pattern_types, "pattern type must be one of {}".format(pattern_types)

        self.pattern_type = pattern_type
        self.pattern_rows = pattern_rows
        self.pattern_columns = pattern_columns
        self.distance_in_world_units = distance_in_world_units

        self.figsize = figsize
        self.debug_dir = debug_dir
        self.term_criteria = term_criteria
        self.subpixel_refinement = True  # turn on or off subpixel refinement
        if self.pattern_type in ["asymmetric_circles", "symmetric_circles"]:
            self.subpixel_refinement = False
            self.use_clustering = True
            self.blobParams = cv2.SimpleBlobDetector_Params()
            self.blobParams.minThreshold = 8
            self.blobParams.maxThreshold = 255
            self.blobParams.filterByArea = True
            self.blobParams.minArea = 50  # minArea may be adjusted to suit for your experiment
            self.blobParams.maxArea = 10e5  # maxArea may be adjusted to suit for your experiment
            self.blobParams.filterByCircularity = True
            self.blobParams.minCircularity = 0.8
            self.blobParams.filterByConvexity = True
            self.blobParams.minConvexity = 0.87
            self.blobParams.filterByInertia = True
            self.blobParams.minInertiaRatio = 0.01
        if self.pattern_type == "asymmetric_circles":
            self.double_count_in_column = True
        if self.debug_dir and not os.path.isdir(self.debug_dir):
            os.mkdir(self.debug_dir)

        self.name = None
        self.image_width = 1936
        self.image_height = 1216
        self.image_center = np.array([self.image_width / 2, self.image_height / 2])
        self.optical_area = (11.345, 7.126)  # mm
        self.see = True

    @staticmethod
    def _splitfn(fn):
        path, fn = os.path.split(fn)
        name, ext = os.path.splitext(fn)
        return path, name, ext

    def _symmetric_world_points(self):
        x, y = np.meshgrid(range(self.pattern_columns), range(self.pattern_rows))
        prod = self.pattern_rows * self.pattern_columns
        pattern_points = np.hstack((x.reshape(prod, 1), y.reshape(prod, 1), np.zeros((prod, 1)))).astype(np.float32)
        return (pattern_points)

    def _asymmetric_world_points(self):
        pattern_points = []
        if self.double_count_in_column:
            for i in range(self.pattern_rows):
                for j in range(self.pattern_columns):
                    x = j / 2
                    if j % 2 == 0:
                        y = i
                    else:
                        y = i + 0.5
                    pattern_points.append((x, y))
        else:
            for i in range(self.pattern_rows):
                for j in range(self.pattern_columns):
                    y = i / 2
                    if i % 2 == 0:
                        x = j
                    else:
                        x = j + 0.5

                    pattern_points.append((x, y))

        pattern_points = np.hstack((pattern_points, np.zeros((self.pattern_rows * self.pattern_columns, 1)))).astype(
            np.float32)
        return (pattern_points)

    def _chessboard_image_points(self, img):
        found, corners = cv2.findChessboardCorners(img, (self.pattern_columns, self.pattern_rows))
        return (found, corners)

    def _circulargrid_image_points(self, img, flags, blobDetector):
        found, corners = cv2.findCirclesGrid(img, (self.pattern_columns, self.pattern_rows),
                                             flags=flags, blobDetector=blobDetector)
        return (found, corners)

    def _calc_reprojection_error(self, figure_size=(12, 8), save_dir=None, limit=True):
        reprojection_error = []
        for i in range(len(self.calibration_df)):
            imgpoints2, _ = cv2.projectPoints(self.calibration_df.obj_points[i], self.calibration_df.rvecs[i],
                                              self.calibration_df.tvecs[i], self.K, self.D)
            temp_error = cv2.norm(self.calibration_df.img_points[i], imgpoints2, cv2.NORM_L2) / len(imgpoints2)
            reprojection_error.append(temp_error)

        self.calibration_df['reprojection_error'] = pd.Series(reprojection_error)
        avg_error = np.sum(np.array(reprojection_error)) / len(self.calibration_df.obj_points)
        x = [os.path.basename(p) for p in self.calibration_df.image_names]
        y_mean = [avg_error] * len(self.calibration_df.image_names)
        fig, ax = plt.subplots()
        fig.set_figwidth(figure_size[0])
        fig.set_figheight(figure_size[1])

        max_intensity = np.max(reprojection_error)
        cmap = cm.get_cmap('jet')
        colors = cmap(reprojection_error / max_intensity)  # * 255
        ax.scatter(x, reprojection_error, label='Reprojection error', c=colors, marker='o')  # plot before
        ax.bar(x, reprojection_error, color=colors, alpha=0.3)
        ax.plot(x, y_mean, label='Mean Reprojection error', linestyle='--')

        ax.legend(loc='upper right')
        for tick in ax.get_xticklabels():
            tick.set_rotation(90)
        ax.set_title("{} - Reprojection_error plot, Mean:{}".format(self.name, round(avg_error, 2)))
        ax.set_xlabel("Image_names")
        ax.set_ylabel("Reprojection error in pixels")
        if limit:
            ax.set_ylim(0, self.rms)

        if save_dir:
            plt.savefig(os.path.join(save_dir, "reprojection_error.png"))

        plt.show()
        print("The Mean Reprojection Error in pixels is:  {}".format(avg_error))

    def read_images(self, images_path_list, threads=4, custom_world_points_function=None,
                         custom_image_points_function=None, K=None,D=None):
        self.images = images_path_list
        if self.pattern_type == "custom":
            assert custom_world_points_function is not None, "Must implement a custom_world_points_function for 'custom' pattern "
            assert custom_image_points_function is not None, "Must implement a custom_image_points_function for 'custom' pattern"

        img_points = []
        obj_points = []
        working_images = []
        images_path_list.sort()
        print("There are {} {} images given for calibration".format(len(images_path_list), self.pattern_type))

        if self.pattern_type == "chessboard":
            pattern_points = self._symmetric_world_points() * self.distance_in_world_units

        elif self.pattern_type == "symmetric_circles":
            pattern_points = self._symmetric_world_points() * self.distance_in_world_units
            blobDetector = cv2.SimpleBlobDetector_create(self.blobParams)
            flags = cv2.CALIB_CB_SYMMETRIC_GRID
            if self.use_clustering:
                flags = cv2.CALIB_CB_SYMMETRIC_GRID + cv2.CALIB_CB_CLUSTERING

        elif self.pattern_type == "asymmetric_circles":
            pattern_points = self._asymmetric_world_points() * self.distance_in_world_units
            blobDetector = cv2.SimpleBlobDetector_create(self.blobParams)
            flags = cv2.CALIB_CB_ASYMMETRIC_GRID
            if self.use_clustering:
                flags = cv2.CALIB_CB_ASYMMETRIC_GRID + cv2.CALIB_CB_CLUSTERING

        elif self.pattern_type == "custom":
            pattern_points = custom_world_points_function(self.pattern_rows, self.pattern_columns)

        self.h, self.w = cv2.imread(images_path_list[0], 0).shape[:2]
        if K is not None and D is not None: #undistort images before calibration
            self.calibration_df = None
            newcameramtx, roi = cv2.getOptimalNewCameraMatrix(K, D, (self.w, self.h), 1, (self.w, self.h))

        def undistort_image(img):
            img2 = img
            # undistort
            mapx, mapy = cv2.initUndistortRectifyMap(K, D, None, newcameramtx, (self.w, self.h), 5)
            dst = cv2.remap(img2, mapx, mapy, cv2.INTER_LINEAR)
            x, y, w, h = roi
            dst = dst[y:y + h, x:x + w]
            #print('dst:{}'.format(np.shape(dst)))
            self.h, self.w = dst.shape[:2]
            return dst

        def process_single_image(img_path):
            img = cv2.imread(img_path, 0)  # gray scale
            if img is None:
                print("Failed to load {}".format(img_path))
                return None

            resized1 = img
            if K is not None and D is not None:  # undistort images before calibration
                img = undistort_image(img)

            if threads<=0:
                resized2 = img
                if True:
                    resized1 = cv2.flip(resized1, -1)
                    resized2 = cv2.flip(resized2, -1)

                cv2.imshow('original ', resized1)
                cv2.imshow('undistorted', resized2)
                cv2.waitKey(0)

            assert self.w == img.shape[1] and self.h == img.shape[0], "All the images must have same shape"

            if self.pattern_type == "chessboard":
                found, corners = self._chessboard_image_points(img)
            elif self.pattern_type == "asymmetric_circles" or self.pattern_type == "symmetric_circles":
                found, corners = self._circulargrid_image_points(img, flags, blobDetector)

            elif self.pattern_type == "custom":
                found, corners = custom_image_points_function(img, self.pattern_rows, self.pattern_columns)
                assert corners[0] == pattern_points[
                    0], "custom_image_points_function should return a numpy array of length matching the number of control points in the image"

            if found:
                if self.subpixel_refinement:
                    corners2 = cv2.cornerSubPix(img, corners, (11, 11), (-1, -1), self.term_criteria)
                else:
                    corners2 = corners.copy()

                if self.debug_dir or self.see:
                    vis = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
                    cv2.drawChessboardCorners(vis, (self.pattern_columns, self.pattern_rows), corners2, found)
                    #path, name, ext = self._splitfn(img_path)
                    #outfile = os.path.join(self.debug_dir, name + '_pts_vis.png')
                    #cv2.imwrite(outfile, vis)
            else:
                # print("Calibration board NOT FOUND")
                return (None)

            return (img_path, corners2, pattern_points)

        threads_num = int(threads)
        if threads_num <= 1:
            calibrationBoards = [process_single_image(img_path) for img_path in images_path_list]
        else:
            print("Running with %d threads..." % threads_num)
            pool = ThreadPool(threads_num)
            calibrationBoards = pool.map(process_single_image, images_path_list)

        calibrationBoards = [x for x in calibrationBoards if x is not None]
        for (img_path, corners, pattern_points) in calibrationBoards:
            working_images.append(img_path)
            img_points.append(corners)
            obj_points.append(pattern_points)

        self.calibration_df = pd.DataFrame({"image_names": working_images,
                                            "img_points": img_points,
                                            "obj_points": obj_points, })
        self.calibration_df.sort_values("image_names")
        self.calibration_df = self.calibration_df.reset_index(drop=True)
        print('start calibration obj_points:{},  img_points:{}'.format(np.shape(self.calibration_df.obj_points.ravel()),
                                                                       np.shape(self.calibration_df.img_points.ravel())))

    def calibrate(self, flags=0, project=True, K=None,D=None, save=False):
        self.K = K
        self.D = D
        if self.K is not None:
            print('Use fixed K - estimate only distortion')
            flags |= cv2.CALIB_FIX_INTRINSIC
            flags |= cv2.CALIB_FIX_PRINCIPAL_POINT
            flags |= cv2.CALIB_USE_INTRINSIC_GUESS
            flags |= cv2.CALIB_FIX_FOCAL_LENGTH
            #flags = cv2.CALIB_FIX_ASPECT_RATIO
        elif self.D is not None:
            print('Use fixed D - estimate only K matrix')
            flags |= cv2.CALIB_FIX_K1
            flags |= cv2.CALIB_FIX_K2
            flags |= cv2.CALIB_FIX_K3
            flags |= cv2.CALIB_FIX_TANGENT_DIST

        self.rms, self.K, self.D, \
        rvecs, tvecs = cv2.calibrateCamera(objectPoints=self.calibration_df.obj_points,
                                           imagePoints=self.calibration_df.img_points,
                                           imageSize=(self.w, self.h), cameraMatrix=K, distCoeffs=D,
                                           criteria=self.term_criteria,
                                           flags=flags)

        self.calibration_df['rvecs'] = pd.Series(rvecs)
        self.calibration_df['tvecs'] = pd.Series(tvecs)

        print("\nRMS:", self.rms)
        print("camera matrix:\n", self.K)
        print("distortion coefficients: ", self.D.ravel())
        if project:
            self._calc_reprojection_error(figure_size=self.figsize, save_dir=self.debug_dir)

        result_dictionary = {
            "rms": self.rms,
            "K": self.K,
            "D": self.D,
        }
        if save:
            save_obj(obj=result_dictionary, name=self.name)

        return result_dictionary

    def calibrationReport(self, K=None):
        if K is None:
            Distorsion_models = {'ST': ['Standard', 0, 'Standard'],
                             'RAT': ['Rational', cv2.CALIB_RATIONAL_MODEL, 'CALIB_RATIONAL_MODEL'],
                             'THP': ['Thin Prism', cv2.CALIB_THIN_PRISM_MODEL, 'CALIB_THIN_PRISM_MODEL'],
                             'TIL': ['Tilded', cv2.CALIB_TILTED_MODEL, 'CALIB_TILTED_MODEL'],  # }
                             'RAT+THP': ['Rational+Thin Prism', cv2.CALIB_RATIONAL_MODEL + cv2.CALIB_THIN_PRISM_MODEL,
                                         'CALIB_RATIONAL_MODEL + CALIB_THIN_PRISM_MODEL'],
                             'THP+TIL': ['Thin Prism+Tilded', cv2.CALIB_THIN_PRISM_MODEL + cv2.CALIB_TILTED_MODEL,
                                         'CALIB_THIN_PRISM_MODEL + CALIB_TILTED_MODEL'],
                             'RAT+TIL': ['Rational+Tilded', cv2.CALIB_RATIONAL_MODEL + cv2.CALIB_TILTED_MODEL,
                                         'CALIB_RATIONAL_MODEL + CALIB_TILTED_MODEL'],
                             'CMP': ['Complete',
                                     cv2.CALIB_RATIONAL_MODEL + cv2.CALIB_THIN_PRISM_MODEL + cv2.CALIB_TILTED_MODEL,
                                     'Complete']}
        else:
            Distorsion_models = {'ST': ['Standard', 0, 'Standard'],
                                 'RAT': ['Rational', cv2.CALIB_RATIONAL_MODEL, 'CALIB_RATIONAL_MODEL'],
                                 'THP': ['Thin Prism', cv2.CALIB_THIN_PRISM_MODEL, 'CALIB_THIN_PRISM_MODEL'],
                                 'TIL': ['Tilded', cv2.CALIB_TILTED_MODEL, 'CALIB_TILTED_MODEL'],  # }
                                 'RAT+THP': ['Rational+Thin Prism',
                                             cv2.CALIB_RATIONAL_MODEL + cv2.CALIB_THIN_PRISM_MODEL,
                                             'CALIB_RATIONAL_MODEL + CALIB_THIN_PRISM_MODEL']
                                 }

        calibration_results = pd.DataFrame(
            {"params": ['fx', 'fy', 'px', 'py', 'sk', 'k1', 'k2', 'p1', 'p2', 'k3', 'k4', 'k5', 'k6',
                        's1', 's2', 's3', 's4', 'tx', 'ty', 'Error']})
        rms_all = ['Error']

        min_error, flag = 10000000,0
        for key in Distorsion_models:
            print()
            print(key, '->', Distorsion_models[key][0], ' , ', Distorsion_models[key][1], ' , ',
                  Distorsion_models[key][2])
            flags = Distorsion_models[key][1]

            self.calibrate(flags=flags, project=False, K=K)
            s = np.array([self.K[0, 0], self.K[1, 1], self.K[0, 2], self.K[1, 2], self.K[0, 1]])
            s = np.append(s, self.D)
            calibration_results[str(key)] = pd.Series(s)
            calibration_results.fillna('---', inplace=True)
            rms_all.append(self.rms)
            calibration_results[str(key)] = calibration_results[str(key)].map(
                lambda x: round(x, 4) if isinstance(x, (int, float)) else x).astype(str)

            #if self.rms < min_error:
            #    min_error=self.rms
            #    flag = flags

        calibration_results.iloc[-1, :] = rms_all
        calibration_results.iloc[-1, :] = calibration_results.iloc[-1, :].map(
            lambda x: round(x, 4) if isinstance(x, (int, float)) else x)

        save_csv(obj=calibration_results, name=self.name+"_givenK" if K is not None else self.name)
        return flag

    def visualize_calibration_boards(self, cam_width=20.0, cam_height=10.0, scale_focal=40):
        '''visualize_views(camera_matrix=self.K,
                        rvecs=self.calibration_df.rvecs,
                        tvecs=self.calibration_df.tvecs,
                        board_width=self.pattern_columns,
                        board_height=self.pattern_rows,
                        square_size=self.distance_in_world_units,
                        cam_width=cam_width,
                        cam_height=cam_height,
                        scale_focal=scale_focal,
                        patternCentric=False,
                        figsize=self.figsize,
                        save_dir=self.debug_dir
                        )'''
        visualize_views(camera_matrix=self.K,
                        rvecs=self.calibration_df.rvecs,
                        tvecs=self.calibration_df.tvecs,
                        board_width=self.pattern_columns,
                        board_height=self.pattern_rows,
                        square_size=self.distance_in_world_units,
                        cam_width=cam_width,
                        cam_height=cam_height,
                        scale_focal=scale_focal,
                        patternCentric=True,
                        figsize=self.figsize,
                        save_dir=self.debug_dir
                        )
        plt.show()

    def doStuff(self, images, project = True, single_flag = True, K = None,D=None):
        self.read_images(images_path_list=images)
        if single_flag:
            flags=0
        else:
            flags = self.calibrationReport() #report with original data
        self.calibrate(flags=flags, project=project, K=K, D=D, save=True)

    def adjustCalibration(self,images,  K, D):
        print('adjustCalibration -> estimate Windshield distortion -> undistort images -> calibrate again')
        K_outside = K                         #assume the outside K matrix is ideal
        D_outside = D                         #take the outside camera distortion
        self.doStuff(images=images, single_flag=True, K=K_outside) #calibration with given K -> estimate only distortion params
        D_inside_full = self.D                #Estimated distortion = lens D + Windshield D

        Windshield_distortion = np.abs(D_inside_full - D_outside) * np.sign(D_outside)
        print('Windshield_distortion')
        print(Windshield_distortion)

        #read images and undistort them
        self.read_images(images_path_list=self.images, K=K_outside, D=Windshield_distortion)
        self.name = 'adjusted_'+self.name
        flags = self.calibrationReport()  # report with original data

        # for the final calibration fix aspect ration
        flags |= cv2.CALIB_FIX_ASPECT_RATIO
        self.calibrate(flags=flags, project=True)


import json
import numpy as np

class score_keeper():
	def __init__(self):
		self.score = 0

	def calc_score(img1, img2):
		centrePoint = [0, 0]
		frameCount = -1
		avgErr = []

		for keyPointInd in range(1, numKeypoints):
			overallInd = frameInd*numKeypoints + keyPointInd
			tgtKP = targetKeypointsDiv[overallInd]
			srcKP = sourceKeypointsDiv[overallInd]

			tgtKPJSON = json.loads(tgtKP)
			srcKPJSON = json.loads(srcKP)

			xTgtList[keyPointInd] = (tgtKPJSON["xCoord"] - tgtCentre[0])
			xSrcList[keyPointInd] = (srcKPJSON["xCoord"] - srcCentre[0])

			yTgtList[keyPointInd] = (tgtKPJSON["yCoord"] - tgtCentre[1])
			ySrcList[keyPointInd] = (srcKPJSON["yCoord"] - srcCentre[1])

		NormedXTgtList = xTgtList / np.max(np.abs(xTgtList))
		NormedYTgtList = yTgtList / np.max(np.abs(yTgtList))

		NormedXSrcList = xSrcList / np.max(np.abs(xSrcList))
		NormedYSrcList = ySrcList / np.max(np.abs(ySrcList))

		xDist = NormedXTgtList - NormedXSrcList
		yDist = NormedYTgtList - NormedYSrcList

		err = np.sqrt(xDist**2 + yDist**2)

		print(np.average(err))


import re
import sys
import pandas as pd
import urllib.request   # URL을 가져오기 위한 파이썬 모듈
import os
import cv2
import random
import numpy as np
import matplotlib.pyplot as plt
import glob
import math
from PyQt5 import uic
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtCore import QDate, QTime, Qt
from selenium import webdriver
from bs4 import BeautifulSoup as bs, BeautifulSoup
from urllib.request import urlopen
import urllib.parse

form_class = uic.loadUiType("drug.ui")[0]

data = pd.read_csv('./공공데이터개방_낱알식별목록.csv')
drug_name = pd.DataFrame(data['품목명'])

drug_simple_name = []
for i, row in drug_name.iterrows():
    drug_simple_name.append(re.split('[/,-,(,),|,<,>,\[,\]. :]',drug_name['품목명'][i])[0])

drug_simple_name = pd.DataFrame(drug_simple_name)
drug_simple_name.columns = ['simple_name']
new_data = pd.concat([data, drug_simple_name], axis=1)
# new_data[ new_data['simple_name'] == '갈미정']

class MyApp(QWidget, form_class):

    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.initUI()
        self.show()  # 창 보여주기

    def initUI(self):
        self.setWindowTitle('prescription')
        self.move(300, 300)

        self.name.returnPressed.connect(self.Write_name)
        self.Select_sex()
        self.Select_age()
        self.date_time()

        self.init_drug()

        self.drug.returnPressed.connect(self.Write_drug)
        self.waring0.setText("주의사항")
        self.waring0.setReadOnly(True)
        self.info0.setText("약 정보")
        self.info0.setReadOnly(True)

    def Write_name(self):
        self.name.setText()

    def Select_sex(self):
        self.sex.addItem('Woman')
        self.sex.addItem('Man')

    def Select_age(self):
        self.age.addItem('5-7')
        self.age.addItem('8-10')
        self.age.addItem('11-13')
        self.age.addItem('14-16')
        self.age.addItem('17-19')
        self.age.addItem('20-27')
        self.age.addItem('28-34')
        self.age.addItem('35-47')
        self.age.addItem('48-55')
        self.age.addItem('56-65')
        self.age.addItem('66-73')
        self.age.addItem('74-')

    def date_time(self):
        now = QDate.currentDate()  # 현재 날짜를 반환
        time = QTime.currentTime()  # 현재 시간을 반환
        date = now.toString('yyyy-MM-dd(dddd)')
        date1 = time.toString('hh:mm:ss')
        self.date.setText(date + ' / ' + date1)

    def init_drug(self):
        self.drug1.pixmap() == None
        self.drug2.pixmap() == None
        self.drug3.pixmap() == None
        self.drug4.pixmap() == None
        self.drug5.pixmap() == None

    def Write_drug(self):
        drug_name = self.drug.text() + '.jpg'
        print("약 이름 : {}".format(drug_name))
        drug_Img_dir = './drug_image/'  # 결과 이미지 저장할 파일, 경로
        self.drug_img_name = os.path.join(drug_Img_dir, drug_name)

        if os.path.isfile(self.drug_img_name):    # 파일이 있다면
            pixmap = QPixmap(self.drug_img_name)   # Qt 에서 이미지 가져오기

            # 이미지 크기를 label 사이즈 맞춰주기
            # QLabel 1 ~ 5 크기는 같음
            self.drug_W = self.drug1.width()  # 라벨의 가로길이
            self.drug_H = self.drug1.height()  # 라벨의 세로길이

            # 이미지를 라벨의 크기로 변경, 비율무시
            self.src_pixmap = pixmap.scaled(self.drug_W, self.drug_H, aspectRatioMode=Qt.IgnoreAspectRatio)

            # 이미지가 있다면 다음 칸에 이미지를 출력시키기
            drug_name_list = [self.drug1, self.drug2, self.drug3, self.drug4, self.drug5]
            drug_info_list = [self.info1, self.info2, self.info3, self.info4, self.info5]
            for num in drug_name_list:
                if num.pixmap() == None: # 해당 칸에 이미지가 없다면
                    num.setPixmap(self.src_pixmap)  # Qt 에서 원본 이미지 보여주기
                    break
                # elif num.pixmap() != None:   # 해당 칸에 이미지가 있다면
                #     continue
            # print(self.drug1.pixmap(), type(self.drug1.pixmap()))
            # print(self.drug2.pixmap(), type(self.drug2.pixmap()))

            # 약 내용 크롤링 하기
            # 약품 일련 번호 가져오기
            simple_name = self.drug.text()
            print("simple_name : {}".format(simple_name))
            drug_num = new_data[new_data['simple_name'] == simple_name]['품목일련번호']
            drug_num = list(drug_num)
            drug_num = str(drug_num[0])
            print("drug_num : {}".format(drug_num))

            driver = webdriver.Firefox(executable_path='/home/test/opencv/geckodriver')

            url = "https://nedrug.mfds.go.kr/pbp/CCBBB01/getItemDetail?itemSeq=" + str(drug_num)

            driver.get(url)
            req = driver.page_source
            soup = BeautifulSoup(req, 'html.parser')

            drug_info = soup.select_one('#_ee_doc > p')

            driver.quit()

            # print(drug_info.text, type(drug_info.text))
            # print(self.info1.text())

            for num in drug_info_list:
                if num.text() == '': # 해당 칸에 글자가 안 써있다면
                    num.setText(simple_name + '\n' + drug_info.text)  # Qt 에서 크롤링으로 가져온 정보를 입력하기
                    break

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = MyApp()
    sys.exit(app.exec_())


import nltk
import string
import os
import json

from bs4 import BeautifulSoup
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from nltk.stem import PorterStemmer

# Download nltk libraries if not available locally.
nltk.download('punkt')
nltk.download('stopwords')
#nltk.download('stem.porter')

def importCollection(collectionPath):
    '''
    This function imports all textual paragraphs from the collection of documents.

    :param string collectionPath: String path to the directory containing collection docs.
    :return: list of tokens for all documents.
    :returnType: Dictionary
    '''
    # Loop over all documents in the collection.
    vocabulary = dict()
    for filename in os.listdir(collectionPath):
        with open(os.path.join(collectionPath, filename), 'r') as f:
            soup=BeautifulSoup(f, features='lxml', from_encoding="utf-8-sig")

        res=soup.findAll("text")
        # Token list for each document.
        docVocabulary = []
        # Add tokens for each document.
        for text in res:
            docVocabulary.extend(tokenize(str(text).replace("<text>", "").replace("</text>", "").replace(",", " ").replace("-", " ")))

        # Removing duplicates from the list.
        docVocabulary = list(dict.fromkeys(docVocabulary))
        # Add document tokens to all doc dictionary.
        vocabulary[filename]=docVocabulary

    # Create json file.
    makeJson(vocabulary, "./assets/collection-tokens")

    return vocabulary

def tokenize(text):
    '''
    Tokenizes a string using nltk PorterStemmer. Performs necessary stopword and punctuation deductions.
    Uses nltk stopwords along with custom stopwords.

    :param string text: Text string to tokenize.
    :return: Tokens from the string
    :returnType: List
    '''
    # Initialize porter stemmer.
    ps = PorterStemmer()

    # Custom Stopwards to tackle edge cases.
    edgeStopWords = ['``', '\'s', 'n\'t', '\'d', "\'\'"]

    # Set of all stopwords.
    allStopWords = set(stopwords.words('english')).union((line.strip('\n') for line in open("./assets/stopwords.txt", "r"))).union(edgeStopWords)

    # List of Tokenized paragraph.
    tokens = [ps.stem(word.lower()) for word in word_tokenize(text)
        if word.lower() not in allStopWords and word not in string.punctuation
        and not word.isnumeric() and not isFloat(word)]

    # Return token list.
    return tokens

def isFloat(s):
    '''
    Checks whether a string is a float.

    :param string s: String to verify.
    :return: True if the string s is float number, False otherwise.
    :returnType: boolean
    '''
    try:
        return float(s)
    except ValueError:
        return False

def makeJson(dictionary, filename):
    '''
    Dumps dictionary into a json file of the given name and path.

    :param dictionary: Dictionary to be dumped in the json file.
    :param filename: Path and filename of json file to be created.
    :return: null
    :returnType: null
    '''
    with open(filename+".json", "w") as outfile:
        json.dump(dictionary, outfile)


#!/bin/python3
# %%
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import sys

file_name = sys.argv[1]
f = open(file_name)
f_rec = open(sys.argv[2])

line = f.readline()
i = 0
fig, ax = plt.subplots()
plt.rcParams["figure.figsize"] = [100, 100]

while len(line) > 0:
    array = np.array(line.split(), dtype=int)
    array = np.append(array, i)
    # print(array)
    ax.plot([i for _ in range(len(array))], array, 'r.', 1)
    line = f.readline()
    i = i + 1

line = f_rec.readline()
while len(line) > 0:
    vec = line.split()

    a = np.array(vec[:5], dtype=int)
    color = 'bgrcmyk'[a[4]]
    line_width = 10 if vec[5] == "true" else 5 if vec[6] == "true" else 1
    rec = patches.Rectangle((a[0], a[1]), a[2] - 1, a[3] - 1, edgecolor=color, facecolor='none', linewidth=line_width)
    ax.add_patch(rec)
    line = f_rec.readline()
plt.savefig("fig")


# Import libraries
import numpy as np
import pandas as pd
import os as os
from sklearn import preprocessing
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import LogisticRegression
from sklearn.naive_bayes import GaussianNB
from sklearn import svm
from sklearn.model_selection import train_test_split
import statsmodels.api as sm
from statsmodels.stats.outliers_influence import variance_inflation_factor
from statsmodels.stats.diagnostic import het_white
from sklearn.metrics import confusion_matrix
from sklearn.model_selection import KFold, cross_val_score
from sklearn.decomposition import PCA
from sklearn.ensemble import RandomForestRegressor

# Set directory
directory = "C:\\Users\\calmp\\OneDrive\\0 - Job Applications\\Job Apps\\2022 & 2023 Job Hunt\\0 - Interviews\\QuantSpark\\Case Study"
os.chdir(directory)

# Read excel
df = pd.read_csv("Retention_df.csv")

# Drop useless columns
df = df.drop(["Over18", "StandardHours", "Gender", "MonthlyIncome",
              "Department", "BusinessTravel"], axis=1)

# Subset high performers
df = df[df['PerformanceRating']>=4]

# Adjust distributions

# Target split
X = df.drop(['Left'], axis=1)
y = df['Left']

# STATISTICAL INFERENCE

# Drop one dummy per group to avoid multicollinearity
X = X.drop(['Non-Travel', 'HR_department', "Income_medium"], axis=1)

# Create interactions
X['Distance_x_WFH'] = X['DistanceFromHome']*X['workingfromhome']
X['LowIncome_x_Hike'] = X['Income_low']*X['PercentSalaryHike']

# Select main indepedent variables
vars_ = ['DistanceFromHome', 'workingfromhome',
         'Income_low', 'Travel_Frequently', 'Age',
         'Sales_department']

# Select controls
## Added in other variables, but no signifiance
# vars_.append('YearsSinceLastPromotion')

# Retain desired vars
X1 = X[vars_]

# Test for multicollinearity with VIF
## NumCompanies, Total working years = scale with age
vif_data = pd.DataFrame()
vif_data["feature"] = X1.columns
vif_data["VIF"] = [variance_inflation_factor(X1.values, i) for i in range(len(X1.columns))]

# Dropped for multicollineatiy:
    ## JobSat -> separate regression?
    ## SalaryHike
    ## = these factors are represented by underlying variables

# Add interactions
vars_.append('Distance_x_WFH')
#vars_.append('LowIncome_x_Hike')

# Run logit regression
logit_model=sm.Logit(y,X[vars_])
log_result=logit_model.fit(cov_type="HC0")
print(log_result.summary2())

# Just satisfaction
logit_model=sm.Logit(y,X['JobSatisfaction'])
log_result=logit_model.fit(cov_type="HC0")
print(log_result.summary2())

# Subset model for those who made complaints
to_drop = df['complaintfiled'] == 1
Xc = X[to_drop]
yc = y[to_drop]

# Add variable
vars_.remove('Distance_x_WFH')
vars_.append('complaintresolved')

# Run logit regression
logit_model=sm.Logit(yc,Xc[vars_])
log_result_c=logit_model.fit()
print(log_result_c.summary2())

# What determines job satisfaction?

# Re-split
X_sat = df.drop(['JobSatisfaction'], axis=1)
y_sat = df['JobSatisfaction']

# Drop nans
X_sat = X_sat.drop(['complaintresolved', 'complaintyears'], axis=1)

# Drop other unwatned vars
X_sat = X_sat.drop(['Left', "Travel_Rarely", "HR_department", "Income_medium"], axis=1)

# Drop due to multicol
X_sat = X_sat.drop(['PerformanceRating'], axis=1)

# Recursive feature elimination
from sklearn.feature_selection import RFE
ols = LinearRegression()
rfe = RFE(ols, 10)
rfe = rfe.fit(X_sat, y_sat)
vars_ = list(X_sat.columns[rfe.support_])

# Check multicol
vif_data = pd.DataFrame()
vif_data["feature"] = X_sat[vars_].columns
vif_data["VIF"] = [variance_inflation_factor(X_sat[vars_].values, i) for i in range(len(X_sat[vars_].columns))]

# Run logit regression
ols_sat=sm.OLS(y_sat,X_sat[vars_])
sat_result=ols_sat.fit()
print(sat_result.summary2())

# Run logit only with salary hike
ols_sat=sm.OLS(y_sat,X_sat['PercentSalaryHike'])
sat_result=ols_sat.fit()
print(sat_result.summary2())

# What determines salary hike?

# Re-split
X_sat = df.drop(['PercentSalaryHike'], axis=1)
y_sat = df['PercentSalaryHike']

# Drop nans
X_sat = X_sat.drop(['complaintresolved', 'complaintyears'], axis=1)

# Drop other unwatned vars
X_sat = X_sat.drop(['Left', "Non-Travel", "HR_department",
                    "Income_medium", 'JobSatisfaction'], axis=1)

# Drop due to multicol
#X_sat = X_sat.drop(['PerformanceRating'], axis=1)

# Recursive feature elimination
from sklearn.feature_selection import RFE
ols = LinearRegression()
rfe = RFE(ols, 11)
rfe = rfe.fit(X_sat, y_sat)
vars_ = list(X_sat.columns[rfe.support_])
#vars_.append('Age')

# Check multicol
vif_data = pd.DataFrame()
vif_data["feature"] = X_sat[vars_].columns
vif_data["VIF"] = [variance_inflation_factor(X_sat[vars_].values, i) for i in range(len(X_sat[vars_].columns))]

# Run logit regression
ols_sat=sm.OLS(y_sat,X_sat[vars_])
sat_result=ols_sat.fit()
print(sat_result.summary2())

# What determines performance rating?

# Re-split
X_sat = df.drop(['PerformanceRating'], axis=1)
y_sat = df['PerformanceRating']

# Drop nans
X_sat = X_sat.drop(['complaintresolved', 'complaintyears'], axis=1)

# Drop other unwatned vars
X_sat = X_sat.drop(['Left', "Non-Travel", "HR_department",
                    "Income_medium", 'JobSatisfaction', 'PercentSalaryHike',
                    "R&D_department", 'Sales_department'], axis=1)

# Drop due to multicol
#X_sat = X_sat.drop(['PerformanceRating'], axis=1)

# Recursive feature elimination
from sklearn.feature_selection import RFE
ols = LinearRegression()
rfe = RFE(ols, 15)
rfe = rfe.fit(X_sat, y_sat)
vars_ = list(X_sat.columns[rfe.support_])
#vars_.append('Age')

# Check multicol
vif_data = pd.DataFrame()
vif_data["feature"] = X_sat[vars_].columns
vif_data["VIF"] = [variance_inflation_factor(X_sat[vars_].values, i) for i in range(len(X_sat[vars_].columns))]

# Run logit regression
ols_sat=sm.OLS(y_sat,X_sat[vars_])
sat_result=ols_sat.fit()
print(sat_result.summary2())

# PREDICTION

# Fix predictors
X = X.drop(["complaintresolved", "complaintyears", 'Distance_x_WFH', 'LowIncome_x_Hike'], axis=1)

# Set cross validation
k_folds = KFold(n_splits = 8)

# Train test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)

# Feature scaling
scaler = preprocessing.StandardScaler().fit(X_train)
X_scaled = scaler.transform(X_train)

# PCA
pca = PCA(n_components=5)
pca.fit(X_train)
X_pca_train = pca.transform(X_train)
X_pca_test = pca.transform(X_test)

# Fit and run logistic regression
X_used = X_scaled.copy()
mod = LogisticRegression()
mod.fit(X_used, y_train)
y_pred = mod.predict(X_test)
scores = cross_val_score(mod, X_used, y_train, cv = k_folds)

print("Cross Validation Scores: ", scores)
print("Average CV Score: ", scores.mean())
print('Test Accuracy: {:.2f}'.format(mod.score(X_test, y_test)))

# Confusion matrix
confusion_matrix = confusion_matrix(list(y_test), list(y_pred))
print(confusion_matrix)

# Fit and run naive bayes
X_used = X_train.copy()
mod = GaussianNB()
mod.fit(X_used, y_train)
y_pred = mod.predict(X_test)
scores = cross_val_score(mod, X_used, y_train, cv = k_folds)

print("Cross Validation Scores: ", scores)
print("Average CV Score: ", scores.mean())
print('Test Accuracy: {:.2f}'.format(mod.score(X_test, y_test)))

# Confusion matrix
confusion_matrix = confusion_matrix(list(y_test), list(y_pred))
print(confusion_matrix)

# Fit and run SVM
X_used = X_scaled.copy()
mod = svm.SVC()
mod.fit(X_used, y_train)
y_pred = mod.predict(X_test)
scores = cross_val_score(mod, X_used, y_train, cv = k_folds)

print("Cross Validation Scores: ", scores)
print("Average CV Score: ", scores.mean())
print('Test Accuracy: {:.2f}'.format(mod.score(X_test, y_test)))

# Confusion matrix
confusion_matrix = confusion_matrix(list(y_test), list(y_pred))
print(confusion_matrix)

# Fit and run tree
from sklearn import tree
X_used = X_train.copy()
mod = tree.DecisionTreeClassifier()
mod.fit(X_used, y_train)
y_pred = mod.predict(X_test)
scores = cross_val_score(mod, X_used, y_train, cv = k_folds)

print("Cross Validation Scores: ", scores)
print("Average CV Score: ", scores.mean())
print('Test Accuracy: {:.2f}'.format(mod.score(X_test, y_test)))

# Confusion matrix
confusion_matrix = confusion_matrix(list(y_test), list(y_pred))
print(confusion_matrix)


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

#Import models
from django.core.management.base import BaseCommand
from ...models import Youtube_Video, Game_Tag

#import modules
import pprint
import datetime

class Command(BaseCommand):
    help = '--youtube_video_org_id [id] : update or create specific video info '
    MOBILE_TAG = ['mobile','スマホ','モバイル']
    GAME_DICT = {}
    GAME_CATEGORY_OTHERES = 1
    GAME_CATEGORY_PUBG_PC = 2
    GAME_CATEGORY_PUBG_MOBILE = 5
    GAME_CATEGORY_FORTNITE_PC = 4
    GAME_CATEGORY_FORTNITE_MOBILE = 18

    def add_arguments(self, parser):
            parser.add_argument('--youtube_video_org_id')
            parser.add_argument('--all', action='store_true')

    def handle(self, *args, **options):

            dt_now = datetime.datetime.now()
            print("=======start video category_update : {}==========".format(dt_now))

            youtube_video_org_id = options['youtube_video_org_id']
            is_all = options['all']

            #Get Game_Tag
            game_tags = Game_Tag.objects.all().order_by('priority')

            #Create Game Tag Dict
            for tag in game_tags:
                self.GAME_DICT[tag.game_tag.lower()] = tag.game_id

            #list of streamer objects
            videos = []

            #get all objects
            if youtube_video_org_id is not None:

                videos = Youtube_Video.objects.extra(where=[ "youtube_video_org_id = '{}'".format(youtube_video_org_id) ])

            elif is_all is True:

                where_state = " active = True AND ( is_ready = FALSE OR game_id = 1 ) "
                videos = Youtube_Video.objects.extra(where=[ where_state ]).order_by('id');

            else:

                where_state = " active = True AND is_ready = FALSE"
                videos = Youtube_Video.objects.extra(where=[ where_state ]).order_by('id');

            for v in videos:

                #deactivate
                if v.video_title is None:
                    v.active = False
                    v.save()
                    continue

                #set update_game_id to othres
                update_game_id = self.GAME_CATEGORY_OTHERES
                print("video_id:{},youtube_video_org_id:{}".format(v.id, v.youtube_video_org_id))

                #Get category id from game title
                update_game_id = self.get_category_from_title(v.video_title)

                #if game id is still GAME_CATEGORY_OTHERES, check tags
                if update_game_id == self.GAME_CATEGORY_OTHERES and v.youtube_tag is not None:
                    update_game_id = self.get_category_from_title(v.youtube_tag)

                print("GAME Category Update => {},{}".format(update_game_id,v.video_title))
                v.game_id = update_game_id;
                v.is_ready = True
                v.save()

    def get_category_from_title(self, str):

        #set up
        #print(str)
        title = str.lower()
        update_game_id = self.GAME_CATEGORY_OTHERES
        mobile_array = self.MOBILE_TAG
        game_dict = self.GAME_DICT

        #is mobile
        is_mobile = False
        for m_tag in mobile_array:
            if title.find(m_tag) >=0:
                is_mobile = True

        for g_tag, g_id in game_dict.items():

            #print('----id:{},tag:{}'.format(g_id,g_tag))
            if title.find(g_tag)>=0:

                update_game_id = g_id

                # IF game_id equal PUBG and is_mobile true, game_id is PUBG Mobile
                if update_game_id == self.GAME_CATEGORY_PUBG_PC and is_mobile == True:
                    update_game_id = self.GAME_CATEGORY_PUBG_MOBILE

                # IF game_id equal PUBG and is_mobile true, game_id is PUBG Mobile
                if update_game_id == self.GAME_CATEGORY_FORTNITE_PC and is_mobile == True:
                    update_game_id = self.GAME_CATEGORY_FORTNITE_MOBILE

                break

        return update_game_id


import requests
import parsel
from tqdm import tqdm

url = 'http://biqugse.com/8820/'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36'
}
response = requests.get(url, headers).text
selector = parsel.Selector(response)
novel_name = selector.css('#info h1::text').get()
detail_href = selector.css('#list dd a::attr(href)').getall()[9:625]
for href in tqdm(detail_href):
    list_url = 'http://biqugse.com' + href
    # print(list_url)
    data_details = requests.get(url=list_url, headers=headers).text
    selectors = parsel.Selector(data_details)
    title = selectors.css('.bookname h1::text').get()
    content_list = selectors.css('#content::text').getall()  # getall()获取返回的为列表
    # '\n'.join(content_list) 相当于把列表中的每一个元素都用\n合并
    content = '\n'.join(content_list)
    # print(content)
    with open(novel_name + '.txt', mode='a', encoding='utf-8') as f:
        f.write(title)
        f.write(content)
        f.write('\n')

小说下载器/笔趣阁.py


# encoding=utf-8
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

class WaitPageElement(object):
    def __init__(self, driver):
        self.driver = driver
        self.locTypeDict={'id': By.ID,
                          'name': By.NAME,
                          'link text': By.LINK_TEXT,
                          'partial link text': By.PARTIAL_LINK_TEXT,
                          'xpath': By.XPATH,
                          'css': By.CSS_SELECTOR}
        self.wait = WebDriverWait(self.driver, 10)

    def getPresentElement(self, locType, loc):
        """
        显式等待出现在DOM中，但不一定可见的元素。若存在，返回该页面元素对象。
        """
        print(locType.lower())
        print(self.locTypeDict[locType.lower()])
        try:
            if locType.lower() in self.locTypeDict:
                element = self.wait.until(EC.presence_of_element_located((self.locTypeDict[locType.lower()], loc)))
                return element
            else:
                raise  TypeError('未找到匹配的定位方式。')
        except Exception as e:
            raise e

    def getVisibleElement(self, locType, loc):
        """
        显式等待出现在DOM中并且可见的元素，返回该元素对象。
        """
        print(locType.lower())
        print(self.locTypeDict[locType.lower()])
        try:
            if locType.lower() in self.locTypeDict:
                element = self.wait.until(EC.visibility_of_element_located((self.locTypeDict[locType.lower()], loc)))
                return element
            else:
                raise  TypeError('未找到匹配的定位方式。')
        except Exception as e:
            raise e

if __name__ == '__main__':
    from selenium import webdriver
    driver = webdriver.Firefox()
    try:
        driver.get("http://localhost")
        wait = WaitPageElement(driver)
        wait.getPresentElement('xpath', '//input[@name="username"]').send_keys("gotIt")
        wait.getPresentElement('css', 'input[name="password"]').send_keys("gotIt123")
        wait.getVisibleElement('name', 'Submit').click()
        wait.getVisibleElement('partial link text', '请点击这里').click()
    finally:
        driver.quit()


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

# Cloning the github repo!

# In[1]:

# Clone the repo
# !git clone https://github.com/qubvel/segmentation_models.git

# In[2]:

get_ipython().run_line_magic('cd', 'segmentation_models')

# In[3]:

# Visualise dependencies
# !cat requirements.txt

# In[4]:

# Install dependencies
# !pip install -r requirements.txt

# Building the Model

# In[5]:

# Importing libraries!
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from PIL import Image
import random

# In[6]:

# Function to load the dataset!!

def load_data(folder_name, img_size):

    # Extract the names of images!!
    data = pd.read_csv(folder_name + '.csv', header = None)
    names = data[0].to_numpy()

    # Number of images (image + mask)
    n = int(len(names) / 2)
    # List for dataset (X,Y)
    data = []

    for i in range(n):
        # Read the i'th image
        im = Image.open(folder_name + '/' + names[2 * i])
        im = im.resize((img_size, img_size))
        tpx = np.array(im)
        # Read the i'th mask layer
        im = Image.open(folder_name + '/' + names[2 * i + 1])
        im = im.resize((img_size, img_size))
        tpy = np.array(im)
        # Binary Mask layer!!
        tpy[tpy > 1] = 1
        tpy = tpy.astype(float)

        data.append((tpx,tpy))
    # For loop ends here!!

    return(data)

# In[7]:

# Dataset directory!
path = '/home/ubuntu/Dataset/Segmentation/'

# Folder names for each dataset!
pv1 = ['PV01_Brick', 'PV01_Concrete', 'PV01_SteelTile']
pv3 = ['PV03_Cropland', 'PV03_Grassland', 'PV03_Rooftop', 'PV03_SalineAlkali', 'PV03_Shrubwood', 'PV03_WaterSurface']
pv8 = ['PV08_Ground', 'PV08_Rooftop']

# Data for rooftop training!!
# pv3 = ['PV03_Rooftop']
# pv8 = ['PV08_Rooftop']

# In[8]:

# Loading the dataset

img_size = 224 # size of input image

# First dataset
dataset = load_data(path + pv1[0], img_size)

# All other datasets
pv_others = pv1[1:] + pv3 + pv8

for pv in pv_others:
    temp = load_data(path + pv, img_size)
    dataset = dataset + temp

# Shuffling the samples
random.shuffle(dataset)

# In[9]:

print("Total number of samples: " + str(len(dataset)))

# In[10]:

# Plot some samples!
np.random.seed(1)
idx = np.random.randint(low = 0, high = len(dataset), size = 5)

for i in idx:
    img, mask = dataset[i]
    plt.figure(figsize=(8, 5))
    plt.subplot(121)
    plt.imshow(img)
    plt.xticks([])
    plt.yticks([])
    plt.title('Image ' + str(i))

    plt.subplot(122)
    plt.imshow(mask)
    plt.xticks([])
    plt.yticks([])
    plt.title('Mask ' + str(i))
    plt.show()

# In[11]:

# Importing segmentation models library
import tensorflow
from tensorflow import keras

import segmentation_models as sm

sm.set_framework('tf.keras')

keras.backend.set_image_data_format('channels_last')

# In[12]:

# DataLoader function to load each training batch lazily!
class Dataloder(keras.utils.Sequence):
    """Load data from dataset and form batches

    Args:
        dataset: instance of Dataset class for image loading and preprocessing.
        batch_size: Integet number of images in batch.
        shuffle: Boolean, if `True` shuffle image indexes each epoch.
    """

    def __init__(self, dataset, batch_size=1, shuffle=False):
        self.dataset = dataset
        self.batch_size = batch_size
        self.shuffle = shuffle
        self.indexes = np.arange(len(dataset))

        self.on_epoch_end()

    def __getitem__(self, i):

        # collect batch data
        start = i * self.batch_size
        stop = (i + 1) * self.batch_size
        data = []
        for j in range(start, stop):
            data.append(self.dataset[j])

        # transpose list of lists
        batch = [np.stack(samples, axis=0) for samples in zip(*data)]

        return batch

    def __len__(self):
        """Denotes the number of batches per epoch"""
        return len(self.indexes) // self.batch_size

    def on_epoch_end(self):
        """Callback function to shuffle indexes each epoch"""
        if self.shuffle:
            self.indexes = np.random.permutation(self.indexes)

# In[13]:

# Choosing the backbone
BACKBONE = 'resnet34'
preprocess_input = sm.get_preprocessing(BACKBONE)

BATCH_SIZE = 16
LR = 0.0001 # learning rate
EPOCHS = 10

# In[14]:

# Split the dataset 90%:5%:5% train:dev:test split
n = len(dataset)
m = int(n * 0.9)
v = m + int((n-m) / 2)

train = dataset[0:m]
val = dataset[m:v]
test = dataset[v:]

# Data loader for training
train_dataloader = Dataloder(train, batch_size=BATCH_SIZE, shuffle=True)
valid_dataloader = Dataloder(val, batch_size=1, shuffle=False)

# In[25]:

# Define model
model = sm.Unet(BACKBONE, encoder_weights='imagenet', classes=1, activation='sigmoid', encoder_freeze = True)

# Define optomizer
optim = keras.optimizers.Adam(LR)

# Define the loss (for our case it's binary!)
loss = sm.losses.bce_jaccard_loss

# Segmentation models losses can be combined together by '+' and scaled by integer or float factor
# jaccard_loss = sm.losses.JaccardLoss()
# bce_loss = sm.losses.BinaryCELoss()
# loss = jaccard_loss + (1 * bce_loss) # To try different combination of loss functions!!

# Define the metrics
metrics = [sm.metrics.IOUScore(threshold=0.5), sm.metrics.FScore(threshold=0.5)]

# Compile keras model with defined optimozer, loss and metrics
model.compile(optim, loss, metrics)

# define callbacks for learning rate scheduling and best checkpoints saving
callbacks = [
    keras.callbacks.ModelCheckpoint('./best_model.h5', save_weights_only=True, save_best_only=True, mode='min'),
    keras.callbacks.ReduceLROnPlateau(),
]

# In[26]:

# Fine tuning to keep the properly trained encoder weights (Note: set encoder_freeze = True)

# pretrain model decoder
model.fit(
    train_dataloader,
    steps_per_epoch=len(train_dataloader),
    epochs=2,
    validation_data=valid_dataloader,
    validation_steps=len(valid_dataloader),
)

# In[27]:

# Set all the layers trainable!
for layer in model.layers:
    layer.trainable = True

# Re-compile the model
optim = keras.optimizers.Adam(LR/10) # Reduce the learning rate if needed
model.compile(optim, loss, metrics)

# In[28]:

# Continue training
history = model.fit_generator(
    train_dataloader,
    steps_per_epoch=len(train_dataloader),
    epochs=EPOCHS,
    callbacks=callbacks,
    validation_data=valid_dataloader,
    validation_steps=len(valid_dataloader),
)

# In[29]:

# Plot training & validation iou_score values
plt.figure(figsize=(20, 5))
plt.subplot(121)
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Model loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Training Loss', 'Validation Loss'], loc='upper left')

# Plot training & validation loss values
plt.subplot(122)
plt.plot(history.history['iou_score'])
plt.plot(history.history['val_iou_score'])
plt.title('Model iou_score')
plt.ylabel('iou_score')
plt.xlabel('Epoch')
plt.legend(['Training IoU', 'Validation IoU'], loc='upper left')

plt.show()
# plt.savefig('loss.png')

# In[30]:

test_dataloader = Dataloder(test, batch_size=1, shuffle=False)

# load best weights
model.load_weights('best_model.h5')

scores = model.evaluate_generator(test_dataloader)

print("Loss: {:.4}".format(scores[0]))
for metric, value in zip(metrics, scores[1:]):
    print("mean {}: {:.4}".format(metric.__name__, value))

# In[48]:

# Predictions on Test dataset!
samples = np.random.randint(low = 0, high = len(test), size = 5)

for j in samples:
    x, y = test[j]
    plt.figure(figsize=(12, 5))
    plt.subplot(131)
    plt.imshow(x)
    plt.xticks([])
    plt.yticks([])
    plt.title('Image')

    plt.subplot(132)
    plt.imshow(y)
    plt.xticks([])
    plt.yticks([])
    plt.title('True Mask')

    mask = model.predict(x.reshape(1,img_size,img_size,3)).round()
    plt.subplot(133)
    plt.imshow(mask.reshape(img_size,img_size))
    plt.xticks([])
    plt.yticks([])
    plt.title('Predicted Mask')
    plt.show()

# In[ ]:

# In[ ]:

# In[ ]:


import pygame as pg
import sys, time

from gvars import *
from entities import Player, Magic, Student, Cat, Banner, Night

screen = None
redline = None
clock = None
player = None
magic_sprites = None
player_sprites = None
student_sprites = None
cat_sprites = None
banner_sprites = None
night_sprites = None
font_score = None
font_author = None
score = 0
now_hour = 0
date = 1
layer_1 = None
layer_2 = None
ommision = False
collision = False

def main():
    while True:
        mainPage()
        gameOn()
        gameOver()

def mainPage():
    # pg.mixer.music.unload()
    # pg.mixer.music.load(c_bgm_start)
    pg.mixer.Channel(0).play(pg.mixer.Sound(c_bgm_start))
    screen.fill(BABY_BLUE)
    bg = pg.transform.scale(pg.image.load('assets/header.png'),(c_header_width,c_header_height))
    screen.blit(bg,(270,152))
    bg = pg.transform.scale(pg.image.load('assets/start_instr.png'),(c_instr_width,c_instr_height))
    screen.blit(bg,(350,380))
    bg = pg.image.load('assets/student_front1.png')
    screen.blit(bg,(150,450))
    bg = pg.image.load('assets/player_front1.png')
    screen.blit(bg,(360,450))
    bg = pg.image.load('assets/player_front2.png')
    screen.blit(bg,(520,450))
    bg = pg.image.load('assets/player_front3.png')
    screen.blit(bg,(680,450))
    bg = pg.image.load('assets/student_front2.png')
    screen.blit(bg,(840,450))
    bg = pg.transform.scale(pg.image.load('assets/cat.png'),(c_cat_width*2,c_cat_height*2))
    screen.blit(bg,(850,370))

    authors = font_author.render('Game created by @lanran and @yuheng-wu', True, GRAY)
    screen.blit(authors, ((c_width - authors.get_width())//2, c_height - authors.get_height() - 5))

    pg.display.flip()
    while True:
        for event in pg.event.get():
            if event.type == pg.QUIT or (event.type == pg.KEYDOWN and event.key == pg.K_q):
                pg.display.quit()
                pg.quit()
                sys.exit()
            if event.type == pg.KEYDOWN and event.key == pg.K_SPACE:
                return

def gameOn():
    global score, ommision,collision, now_hour, date

    time_between = c_time_between
    student_speed = c_student_speed
    last_stu_time = 0
    score = 0
    clearGroup(student_sprites)
    clearGroup(cat_sprites)
    clearGroup(magic_sprites)
    clearGroup(banner_sprites)
    clearGroup(night_sprites)
    time_last_hour = time.time()
    now_hour = 9

    ommision = False
    collision = False

    animation_on = False
    animation_start = None
    animation_time = None
    animation_callback = None

    while True:
        clock.tick(c_fps)
        for event in pg.event.get():
            if event.type == pg.QUIT:
                sys.exit()
            elif event.type == pg.MOUSEBUTTONDOWN:
                handleMouseDown()

        if animation_on:
            if time.time() - animation_start > animation_time:
                animation_on = False
                if animation_callback != None:
                    animation_callback()
                animation_callback = None

        if not animation_on and (time.time() - last_stu_time > time_between):
            student_sprites.add(Student(student_speed))
            last_stu_time = time.time()

        if time.time() - time_last_hour > c_hour_length:
            now_hour += 1
            if now_hour > 12:
                now_hour -= 12
            time_last_hour = time.time()

            if now_hour == 10 or now_hour == 3:
                banner = Banner(now_hour)
                banner_sprites.add(banner)
                time_last_hour += c_banner_time
                time_between = c_time_between / 3

                animation_on = True
                animation_start = time.time()
                animation_time = c_banner_time

            elif now_hour == 6:
                night = Night()
                night_sprites.add(night)
                time_last_hour += c_student_time + c_night_time

                animation_on = True
                animation_start = time.time()
                animation_time = c_student_time + c_night_time

                def set_time():
                    global now_hour, date
                    nonlocal animation_on, animation_start, animation_time, time_last_hour

                    banner = Banner("newday")
                    banner_sprites.add(banner)

                    time_last_hour += c_banner_time
                    now_hour = 9
                    date+=1

                    animation_on = True
                    animation_start = time.time()
                    animation_time = c_banner_time

                animation_callback = set_time

            else:
                time_between = c_time_between

        player_sprites.update()
        magic_sprites.update()
        student_sprites.update()
        cat_sprites.update()
        banner_sprites.update()
        night_sprites.update()

        drawBackground()
        magic_sprites.draw(screen)
        student_sprites.draw(screen)
        drawBackground(True)
        cat_sprites.draw(screen)
        player_sprites.draw(screen)
        banner_sprites.draw(screen)
        night_sprites.draw(screen)

        score_board = font_score.render('Score: {}'.format(score), False, BLACK)
        screen.blit(score_board, (950, 0))

        time_board = font_score.render('Time: {}:00'.format(now_hour), False, BLACK)
        screen.blit(time_board, (950, font_score.get_linesize()))

        def order(n):
            if 4 <= n <= 20:
                return 'th'
            else:
                if n%10 == 1:
                    return 'st'
                elif n%10 == 2:
                    return 'nd'
                elif n%10 == 3:
                    return 'rd'
                else:
                    return 'th'

        date_board = font_score.render('The {}{} day'.format(date,order(date)), False, BLACK)
        screen.blit(date_board, (950, font_score.get_linesize()*2))

        pg.display.flip()

        collide_res = checkCollide()
        if collide_res < 0:
            collision = True
            return
        score += collide_res

        if checkOmission():
            ommision = True
            return

def gameOver():
    global score
    # pg.mixer.music.unload()
    # pg.mixer.music.load(c_bgm_end)
    # pg.mixer.music.play()
    pg.mixer.Channel(0).play(pg.mixer.Sound(c_bgm_end))
    while True:
        clock.tick(c_fps)
        for event in pg.event.get():
            if event.type == pg.QUIT or (event.type == pg.KEYDOWN and event.key == pg.K_q):
                pg.display.quit()
                pg.quit()
                sys.exit()
            elif event.type == pg.KEYDOWN and event.key == pg.K_SPACE:
                return
        if ommision == True:
            bg = pg.image.load('assets/endpg.png')
            screen.blit(bg,(0,0))
        elif collision == True:
            bg = pg.image.load('assets/endpg1.png')
            screen.blit(bg,(0,0))

        score_board = font_score.render('Score: {}'.format(score), False, BLACK)
        screen.blit(score_board, (500,290))

        pg.display.flip()

def drawBackground(layer2 = False):
    global layer_1, layer_2
    if layer2:
        screen.blit(layer_2,(0,0))
        return
    screen.blit(layer_1,(0,0))
    pg.draw.rect(screen, c_redline_color, redline)

def checkCollide():
    global student_sprites, magic_sprites, cat_sprites
    gonnaLose = False

    all_collide = pg.sprite.groupcollide(student_sprites, magic_sprites, False, False)

    for student in all_collide:
        magics = all_collide[student]
        for magic in magics:
            magic.kill()

        gonnaLose = gonnaLose or student.good
        cat = Cat(student.getCenter())
        cat_sprites.add(cat)
        student.kill()

    if gonnaLose:
        for student in student_sprites:
            student.speed = 0
        return -1

    return len(all_collide)

def checkOmission():
    global student_sprites

    student_escaped = None
    for student in student_sprites:
        if student.escaped():
            student_escaped = student
            break

    if student_escaped != None:
        for student in student_sprites:
            if student is not student_escaped:
                student.speed = 0
        return True
    return False

def handleMouseDown():
    global magic_sprites
    mouse_status = pg.mouse.get_pressed()
    if mouse_status[0] != 1: # not left button
        return

    magic = Magic()
    magic_sprites.add(magic)

def clearGroup(group):
    for item in group:
        item.kill()

def init():
    global screen, redline, clock, player, player_sprites, layer_1, layer_2,\
    student_sprites, magic_sprites, cat_sprites, font_score,\
    ommision, collision, banner_sprites, night_sprites, font_author

    pg.init()
    pg.mixer.init()
    pg.mouse.set_visible(False)
    screen = pg.display.set_mode((c_width, c_height))
    pg.display.set_caption(c_game_name)

    redline = pg.Rect((c_redline_left, c_redline_top), (c_redline_width, c_redline_height))

    magic_sprites = pg.sprite.Group()
    player_sprites = pg.sprite.Group()
    student_sprites = pg.sprite.Group()
    cat_sprites = pg.sprite.Group()
    banner_sprites = pg.sprite.Group()
    night_sprites = pg.sprite.Group()

    font_score = pg.font.SysFont('arial',20)
    font_score.set_bold(True)

    font_author = pg.font.SysFont('arial', 12)

    layer_1 = pg.image.load(c_bg_file).convert()
    layer_2 = pg.image.load(c_bg_file2).convert()

    player = Player()
    player_sprites.add(player)

    ommision = False
    collision = False

    clock = pg.time.Clock()

if __name__ == "__main__":
    init()
    main()


import argparse
from server import Server

if __name__ == "__main__":
        parser = argparse.ArgumentParser()
        parser.add_argument("--host", default="localhost", help="Host address to bind to")
        parser.add_argument("--port", type=int, default=1423, help="Port number to bind to")
        args = parser.parse_args()
        # start server & accept connections
        # start new thread for processing user request
        server = Server(args.host, args.port)
        server.start()


"""Tests the functions in day08"""
from typing import List
import pytest
from pytest import fixture

from ..day08 import (
    Operation,
    Instruction,
    parse_instruction,
    negate_instruction,
    determine_accumulator,
    swapped_instructions,
)

def test_parse_instruction():
    assert parse_instruction("jmp -1") == Instruction(Operation.JUMP, -1)
    assert parse_instruction("nop +1") == Instruction(Operation.NO_OPERATION, 1)
    assert parse_instruction("acc +11") == Instruction(Operation.ACCUMULATE, 11)

def test_negate_instruction():
    fn = negate_instruction
    assert fn(Instruction(Operation.JUMP, 1)) == Instruction(Operation.NO_OPERATION, 1)

    assert fn(Instruction(Operation.NO_OPERATION, 1)) == Instruction(Operation.JUMP, 1)

    assert fn(Instruction(Operation.ACCUMULATE, 1)) == Instruction(
        Operation.ACCUMULATE, 1
    )

@fixture
def instructions_loop() -> List[Instruction]:
    """Returns a list of instructions that lead to an loop"""
    return [Instruction(Operation.JUMP, 0)]

@fixture
def instructions_with_end() -> List[Instruction]:
    """Returns a list of instructions that lead to an accumulator 20"""
    return [
        Instruction(Operation.ACCUMULATE, 10),
        Instruction(Operation.JUMP, 2),
        Instruction(Operation.JUMP, 2),
        Instruction(Operation.JUMP, -1),
        Instruction(Operation.NO_OPERATION, 0),
        Instruction(Operation.ACCUMULATE, 10),
    ]

def test_determine_accumulator(instructions_loop, instructions_with_end):
    assert determine_accumulator(instructions_loop) is None
    assert determine_accumulator(instructions_with_end) == 20

def test_swapped_instructions(instructions_with_end):
    generator_instruction = swapped_instructions(instructions_with_end)

    # Testing 0th iteration. Nothing is being changed
    tmp_instructions = instructions_with_end.copy()
    assert next(generator_instruction) == tmp_instructions

    # Testing 1st iteration. First possible index is being changed
    tmp_instructions = instructions_with_end.copy()
    tmp_instructions[1] = Instruction(Operation.NO_OPERATION, 2)
    assert next(generator_instruction) == tmp_instructions

    # Testing 2nd iteration. Second possible index is being changed
    tmp_instructions = instructions_with_end.copy()
    tmp_instructions[2] = Instruction(Operation.NO_OPERATION, 2)
    assert next(generator_instruction) == tmp_instructions

    # Testing 3rd iteration. Third possible index is being changed
    tmp_instructions = instructions_with_end.copy()
    tmp_instructions[3] = Instruction(Operation.NO_OPERATION, -1)
    assert next(generator_instruction) == tmp_instructions

    # Testing 4th iteration. Fourth possible index is being changed
    tmp_instructions = instructions_with_end.copy()
    tmp_instructions[4] = Instruction(Operation.JUMP, 0)
    assert next(generator_instruction) == tmp_instructions

    with pytest.raises(StopIteration):  # The generator ends after that
        next(generator_instruction)


from django.db import models
from django.contrib.auth.models import User
from allauth.account.models import EmailAddress
from datetime import datetime
from django.utils import timezone
#from allauth.socialaccount.models import SocialAccount
#import hashlib

today = datetime.now()

class UserProfile(models.Model):
    user = models.OneToOneField(User, related_name='profile')
    img = models.ImageField(upload_to='web', default='sitio/static/img/defaultuser.jpg')
    fecha = models.DateTimeField(default=timezone.now)
    nombre = models.TextField()
    apellido = models.TextField()
    about_me = models.TextField(null=True, blank=True)

    def __unicode__(self):
        return "{}'s profile'".format(self.user.username)

    class Meta:
        db_table = 'user_profile'

    def __str__(self):
        return self.nombre

    def account_verified(self):
        if self.user.is_authenticated:
            result = EmailAddress.objects.filter(email=self.user.email)
            if len(result):
                return result[0].verified
        return False

User.profile = property(lambda u: UserProfile.objects.get_or_create(user=u)[0])


import re
import pandas as pd
import pretty_errors
import pycountry
from geonamescache import GeonamesCache
import json

    # Create an instance of the GeonamesCache class
gc = GeonamesCache()

    # Get a dictionary of all countries and their information, including capital cities
geo_countries = gc.get_countries()

df = pd.read_csv('/Users/juanreyesgarcia/Library/CloudStorage/OneDrive-FundacionUniversidaddelasAmericasPuebla/DEVELOPER/PROJECTS/CRAWLER_ALL/download/sql_distinct.csv')

    #df['location'] = df['location'].apply(clean_location)

def add_spaces(text):
    return re.sub(r'(?<=[a-z])(?=[A-Z])', ' ', text)

def clean_location_rows(rows):
    if not isinstance(rows, str):
        #print(f"{rows} is not a string! Returning unmodified")
        return rows
        # Keep countries, country codes, states, remote words, and timezones
    # Remove double quotes
    rows = rows.replace('"', '')

    # Add spaces before uppercase letters
    rows = add_spaces(rows)

    keep_words = ['remote', 'remoto', 'international', 'anywhere', 'worldwide', 'utc', 'apac', 'nafta', 'latam', 'asean', 'mena', 'brics', 'anz', 'gcc', 'cee', 'nordic', 'europe', 'americas', 'asia', 'africa', 'oceania', 'antarctica', 'southeast asia', 'emea', 'uk', 'usa', 'united', 'states', 'north', 'america', 'kingdom', 'korea', 'global', 'south', 'latin', 'america']

        # Get country codes and names
    country_codes = [country.alpha_2 for country in pycountry.countries]
    country_names = [country.name for country in pycountry.countries]
        # Extract the capital cities & countries from GeonamesCache
    capital_cities = [geo_countries[country]['capital'] for country in geo_countries]
    country_other_names = [geo_countries[country]['name'] for country in geo_countries]

        # US state codes
    state_codes = ['AL', 'AK', 'AZ', 'AR', 'CA', 'CO', 'CT', 'DE', 'FL', 'GA', 'HI', 'ID', 'IL', 'IN', 'IA', 'KS', 'KY', 'LA', 'ME', 'MD', 'MA', 'MI', 'MN', 'MS', 'MO', 'MT', 'NE', 'NV', 'NH', 'NJ', 'NM', 'NY', 'NC', 'ND', 'OH', 'OK', 'OR', 'PA', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VT', 'VA', 'WA', 'WV', 'WI', 'WY']

        # Remove unwanted characters and split the text into words
    text = re.sub(r'[^a-zA-Z0-9\s\+\-]', ' ', rows)
    words = text.split()

        # Keep only the desired words
    cleaned_words = [word for word in words if any([word.lower().startswith(kw) for kw in keep_words]) or word in country_codes or word in country_names or word in state_codes or word in capital_cities or word in country_other_names or re.match(r'UTC[+\-]\d{1,2}', word)]

        # Join the cleaned words back into a string
    cleaned_text = ' '.join(cleaned_words)
    return cleaned_text

df['location'] = df['location'].apply(clean_location_rows)

df.to_csv('/Users/juanreyesgarcia/Library/CloudStorage/OneDrive-FundacionUniversidaddelasAmericasPuebla/DEVELOPER/PROJECTS/CRAWLER_ALL/download/sql_distinct.csv', index=False)

SAVE_PATH = "/Users/juanreyesgarcia/Library/CloudStorage/OneDrive-FundacionUniversidaddelasAmericasPuebla/DEVELOPER/PROJECTS/CRAWLER_ALL/data"

def convert_names_to_codes(row):
    if isinstance(row, str):  # Check if row is a string

        # Country name transformed to their respective codes & clean
        with open(SAVE_PATH + '/continent_countries_with_capitals.json', 'r') as f:
            data = json.load(f)

        # Create dictionaries for country names and subdivision names
        country_dict = {}
        for continent in data.values():
            for country in continent['Countries']:
                country_dict[country['country_name']] = country['country_code']

        # Split the string into a list of names
        names = row.split()

        # Replace each name with its code
        codes = [country_dict.get(name, name) for name in names]

        # Remove duplicates and join the codes back into a string
        return ' '.join(pd.unique(codes))
    else:
        return row

# Apply the function to the 'location' column
#df['location'] = df['location'].apply(convert_names_to_codes)

#df.to_csv(SAVE_PATH + '/location_distinct.csv', index=False)

    #print(df['location'])

if __name__ == "__main__":
    clean_location_rows(df)


from flask import Flask
from flask import Blueprint, flash, g, redirect, render_template, request, url_for
import rr_main
from datetime import date
today = date.today()
current_year = date.today().strftime('%Y')

import numpy as np
import pandas as pd
import random

ratings = pd.read_csv('datasets/ratings-s.csv')
users = ratings.userid.unique()

recipeeze = Flask(__name__)
theme = 'cappucino'

@recipeeze.route('/')
def index():
    bg_image = 'static/images/emy-90710FOygrg-unsplash.jpg'
    bg_mobile = 'static/images/food-photographer-david-fedulov-h-R_D-zD8KQ-unsplash.jpg'
    #get the ingredients and search
    ingredients_list = ["What's in your fridge?","List a few things. example; cheddar, broccoli"]
    return render_template('index.html', bg_image = bg_image, bg_mobile = bg_mobile,
    ingredients = ingredients_list,theme = theme)

@recipeeze.route('/recipes', methods=['POST'])
#@login_required
def recipes():
    #posting the results
    bg_image = ''
    user_1 = random.choice(users)
    user_2 = random.choice(users)
    user_3 = random.choice(users)
    ingredient_list = request.form['ingredients']

    recipe_list1 = rr_main.set_up_ml(user_1,ingredient_list) # generate a list of recommendations for each user
    recipe_list2 = rr_main.set_up_ml(user_2,ingredient_list)
    recipe_list3 = rr_main.set_up_ml(user_3,ingredient_list)

    final_recommendation = rr_main.get_final_recommendation(recipe_list1,recipe_list2,recipe_list3)
    return render_template('recipe.html',
    recipes_table = final_recommendation, bg_image = bg_image,
    ingredients = ingredient_list,theme = theme)

if __name__ == "__main__":
    recipeeze.run()


from typing import Any, Optional

class Knot:

    def __init__(self, value: Optional[Any] = None, next_value: Optional['Knot'] = None) -> None:
        self.value = value
        self.next_value = next_value

    def __str__(self) -> str:
        return 'Knot [{value}]'.format(
            value=str(self.value)
        )

class LinkedList:

    def __init__(self) -> None:
        self.head = None
        self.length = 0

    def __str__(self) -> str:
        if self.head is not None:
            current = self.head
            values = [str(current.value)]
            while current.next_value is not None:
                current = current.next_value
                values.append(str(current.value))
            return '[{values}]'.format(values=' '.join(values))
        return '[]'

    def append(self, elem: Any) -> None:
        new_knot = Knot(elem)
        if self.head is None:
            self.head = new_knot
            return

        last_knot = self.head
        while last_knot.next_value:
            last_knot = last_knot.next_value
        last_knot.next_value = new_knot
        self.length += 1

    def remove(self, index: int) -> None:
        cur_knot = self.head
        cur_index = 0
        if self.length == 0 or self.length <= index:
            raise IndexError

        if cur_knot is not None:
            if index == 0:
                self.head = cur_knot.next_value
                self.length -= 1
                return

        while cur_knot.next_value is not None:
            prev = cur_knot
            cur_knot = cur_knot.next_value
            cur_index += 1
            if cur_index == index:
                prev.next_value = cur_knot.next_value
                self.length -= 1
                break

    def get(self, index: int) -> None:
        cur_knot = self.head
        cur_index = 0
        if self.length == 0 or self.length < index:
            raise IndexError

        while cur_knot.next_value is not None:
            if cur_index == index:
                return cur_knot.value
            cur_knot = cur_knot.next_value
            cur_index += 1
        return cur_knot.value

my_list = LinkedList()
my_list.append(10)
my_list.append(20)
my_list.append(30)
print('Текущий список:', my_list)
print('Получение третьего элемента:', my_list.get(2))
print('Удаление второго элемента.')
my_list.remove(1)
print('Новый список:', my_list)


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

from typing import List

class Solution:
    def maximalRectangle(self, matrix: List[List[str]]) -> int:
        if len(matrix) == 0: return 0
        m, n = len(matrix), len(matrix[0])
        dp = [[0 for j in range(n)] for i in range(m)]
        for i in range(m):
            for j in range(n):
                if matrix[i][j] == "0":
                    dp[i][j] = 0
                else:
                    dp[i][j] = dp[i - 1][j] + 1
        max_value = 0
        for line in dp:
            cur_value = self.largest_rectange_in_histogram(line)
            # print(line)
            # print(cur_value)
            # print('#' * 10)
            max_value = max(cur_value, max_value)
        return max_value

    def largest_rectange_in_histogram(self, heights):
        if len(heights) == 0: return 0
        heights.insert(0, 0)
        heights.append(0)
        ist, max_rectange = [], 0
        for i in range(0, len(heights)):
            while (len(ist) != 0 and heights[i] < heights[ist[-1]]):
                cur_index = ist[-1]
                ist.pop()
                left = ist[-1] + 1
                right = i - 1
                max_rectange = max(max_rectange, (right - left + 1) * heights[cur_index])

            ist.append(i)

        return max_rectange

if __name__ == '__main__':
    matrix1 = [
        ["1", "0", "1", "0", "0"],
        ["1", "0", "1", "1", "1"],
        ["1", "1", "1", "1", "1"],
        ["1", "0", "0", "1", "0"]
    ]
    matrix = [["0", "1"], ["0", "1"]]
    solution = Solution()
    ans = solution.maximalRectangle(matrix1)
    print('ans: ', ans)


import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 10, 100)
f = (np.exp((-x)/10))*np.sin(np.pi*x)
g = x*np.exp(-x/3)
plt.figure()
plt.plot(x, f,label='f')
plt.plot(x, g,label='g')
plt.xlabel('X')
plt.ylabel('Y')
plt.legend()
plt.show()


# pylint: disable=W0621,C0116,R0903,E0401,W0703,W1201,missing-function-docstring,E0401,C0114,W0511,W1203,C0200,C0103,W1203
import logging

from models.section import Section
from repository.persist_fs import PersistFS as persist_fs
from tests.moke.process_fs import ProcessFS as process_fs

def test_init(get_config_map, http_url):
    actual = Section(persist_fs, process_fs, get_config_map, http_url)
    assert actual.http_url == "http://cloud.google.com/docs"
    assert actual.dir_name == "http§§§cloud.google.com§docs"
    assert actual.dir_readme_md == "http§§§cloud.google.com§docs/readme.md"

def test_write(get_config_map, http_url):
    actual = Section(persist_fs, process_fs, get_config_map, http_url)
    logging.debug(actual)

def test_build_from_dir(get_config_map, simple_http, simple_dir):
    assert (
        Section.build_from_dir(
            persist_fs, process_fs, get_config_map, simple_http
        ).dir_name
        == simple_dir
    )


import os

import pickle
import rosbag
import numpy as np

from .timestamp_series import TimestampSeries
from .tools import *

DefaultTopic = '/device_time'

def guessTopics(bagFile):
  with rosbag.Bag(bagFile) as bag:
    found = []
    for topic, value in bag.get_type_and_topic_info().topics.items() :
      if value.msg_type == 'cuckoo_time_translator/DeviceTimestamp':
        found.append(topic)

  return found

class DeviceTimeStream():

  def __init__(self, bagFile, topic, invalidate=False):
    eventsFile = os.path.realpath(bagFile) + topic.replace("/", "_") + ".p"
    if os.path.exists(eventsFile):
        if invalidate:
            info("Removing " + eventsFile)
        else:
            verbose("Loading from " + eventsFile)
            self.__dict__ = pickle.load(open(eventsFile, "rb"))
            return

    bag = rosbag.Bag(bagFile)

    self.translated_hw_times = TimestampSeries()
    self.translated_hw_times_without_offset = TimestampSeries()
    self.receive_times = TimestampSeries()
    self.raw_hw_times = TimestampSeries()
    for topic, msg, t in bag.read_messages(topics=[topic]):
      self.translated_hw_times.append(msg.header.stamp.to_sec())
      self.translated_hw_times_without_offset.append(msg.header.stamp.to_sec() - msg.offset_secs)
      self.receive_times.append(msg.receive_time.to_sec())
      self.raw_hw_times.append(float(msg.event_stamp))

    self.zeroOffsetAllTheTime = np.linalg.norm(np.array(self.translated_hw_times_without_offset) - np.array(self.translated_hw_times)) < 1e-9

    bag.close()
    # Save data to file
    pickle.dump(self.__dict__, open(eventsFile, "wb"))


import click
import openai

from skainet import utils
from skainet.data import CONFIG

DEFAULT_CHAT_MODEL = CONFIG["moderation"]["model"]

@click.command()
@click.argument("input", type=utils.Prompt(), default="")
@click.option(
    "-m",
    "--model",
    type=str,
    default=DEFAULT_CHAT_MODEL,
    help=f"Model selection. Default is {DEFAULT_CHAT_MODEL}.",
)
def moderate(input: str, model: str):
    """Check if text violates OpenAI's Content Policy"""
    try:
        response = openai.Moderation.create(
            input=input,
            model=model,
        )
    except openai.OpenAIError as e:
        utils.handle_openai_error(e)
    else:
        click.echo(response)


#import art
import random
from art import logo, vs
from game_data import data
from replit import clear

def get_random_account():
    """Get data from random account"""
    return random.choice(data)

def format_data(account):
    """format the acct dat into printable formats."""
    account_name = account['name']
    account_descr = account['description']
    account_country = account['country']
    return (f"{account_name},a {account_descr}, from {account_country}")

def check_answer(player_guess, a_followers, b_followers):
    """Take the user guess and follower acounts and returns if they got it right."""
    if a_followers > b_followers:
        return player_guess == 'a'
    else:
        return player_guess == 'b'

def game():
    print(logo)
    score = 0
    game_should_continue = True
    compare_a = get_random_account()
    compare_b = get_random_account()
    #Make the game repeatable.
    while game_should_continue:
        #generate a random acct from the game data.
        #Making account at position B become the next position A

        compare_a = compare_b
        compare_b = get_random_account()
        while compare_a == compare_b:
            compare_b = get_random_account()

        print(f"Compare A: {format_data(compare_a)}.")
        print(vs)
        print(f"Against B: {format_data(compare_b)}.")

        #Ask user for a guess

        player_guess = input(
            "Who have more followers? Type 'A' or 'B': ").lower()

        #Check if the user is correct
        a_follower_account = compare_a['follower_count']
        b_follower_account = compare_b['follower_count']

        is_correct = check_answer(player_guess, a_follower_account,
                                  b_follower_account)

        #Clear the screen betwee rounds.
        clear()
        print(logo)
        #Give user feedback on their guess
        ##Score keeping.
        if is_correct:
            score += 1
            print(f"You're right! Current score: {score}.")
        else:
            game_should_continue = False
            print(f"Oops, that's wrong. Finallt score: {score}.")

game()


from container import Container
import subprocess
import threading
import requests
import json
from job import Job
import re

containers = [
    Container(1, "172.18.0.2"),
    Container(2, "172.18.0.3"),
    Container(3, "172.18.0.4")
]
jobs = []
stop = False

def docker_compose_up():
  # process = subprocess.Popen(["docker-compose", "--file", "image/docker-compose.yml", "up", "-d", "--scale", "baba=3"]).wait()
  process = subprocess.Popen(["docker-compose", "--file", "image/docker-compose.yml", "up", "-d", "--build", "--scale", "baba=3"]).wait()
def docker_compose_down():
  process = subprocess.Popen(["docker-compose", "--file", "image/docker-compose.yml", "down"]).wait()

def dispatch(container):
  global containers
  job = container.get_job()
  req = requests.get(f"http://{container.get_ip()}:50051/", data=json.dumps({
      "code": job.get_code(),
      "input": job.get_input(),
      "output": job.get_output()
  }))
  print(
      f"• job-{container.get_job()} completed with result {req.json()['status']}")
  container.clear()

def load_balance():
  global containers, jobs
  while not stop:
    for container in containers:
      if len(jobs) != 0 and not container.get_state():
        job = jobs.pop()
        print(f"○ Assigning job-{job} to container-{container.get_id()}")
        container.set_job(job)
        threading.Thread(target=dispatch, args=(container, )).start()

def cli():
  global stop, jobs
  print(">> Machine started...")
  print(">> enter your request or $status to check containers")
  threading.Thread(target=load_balance).start()

  last_job_id = 1

  while True:
    input_command = input()
    if input_command == ":wq":
      stop = True
      docker_compose_down()
      break
    elif input_command == ":status":
      print(">> container states:")
      for container in containers:
        state = "idle"
        if container.get_state():
          state = "active"
        print(f"\t • container {container.get_id()} is {state}")
    else:
      raw_jobs = re.findall("<(.*?)>", input_command)
      for raw_job in raw_jobs:
        last_job_id += 1
        [code, input_path, output_path] = list(
            map(lambda job: job.strip(), raw_job.split(",")))
        jobs.append(Job(last_job_id, code, input_path, output_path))

docker_compose_up()
cli()


from flask import Flask

app = Flask(__name__)

def get_fibonacci_numbers(n):
    numbers = [1, 1]
    for i in range(2, n):
        numbers.append(numbers[i - 1] + numbers[i - 2])
    return numbers

def get_fib(n):
    a, b = 1, 1
    for _ in range(n):
        yield a
        a, b = b, a + b

@app.route("/fibonacci")
def fibonacci():
    return ' '.join([str(s) for s in get_fibonacci_numbers(n)])

@app.route('/fib')
def fib():
    return '<br>'.join([str(s) for s in get_fib(100)])

if __name__ == '__main__':
    n = 100
    app.run(debug=True)


import pygame
import os
import math
import random
import time
from player import Player
from pipe import Pipe
from population import Population

population = 100 # set initial population

config = [
        # 3 input: bird y position, top pipe position and bottom pipe position
		{"nodeCount": 3, "type": "input"},
        # output 1: jump or not
		{"nodeCount": 1, "type": "output"}
	]

birds = [] # array contains bird object

def setup():
    global WIN_WIDTH
    global WIN_HEIGHT
    global FLOOR
    global STAT_FONT
    global END_FONT
    global DRAW_LINES
    global WIN
    global population
    global bg_img
    global bird_images
    global pipe_img
    global neat

    pygame.font.init()
    WIN_WIDTH = 600
    WIN_HEIGHT = 750
    FLOOR = 730

    # font style for displaying text
    STAT_FONT = pygame.font.SysFont("comicsans", 50)
    END_FONT = pygame.font.SysFont("comicsans", 70)

    # indicate whether to show line from birds to pipes or not
    # set to true if you want to draw lines
    DRAW_LINES = False

    WIN = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
    pygame.display.set_caption("Flappy Bird")

    # load image assets
    pipe_img = pygame.transform.scale2x(pygame.image.load(os.path.join("img","pipe.png")).convert_alpha())
    bg_img = pygame.transform.scale(pygame.image.load(os.path.join("img","bg.png")).convert_alpha(), (600, 900))
    bird_images = [pygame.transform.scale2x(pygame.image.load(os.path.join("img","bird" + str(x) + ".png"))) for x in range(1,4)]

    # init population
    for i in range(population):
        birds.append(Player(WIN, bird_images, 200, 200))

    # init NEAT algorithm
    neat = Population(config)

def draw(birds, win, pipes):
    """draw birds to the screen \n
    add line indication for detecting closest pipes
    """
    for pipe in pipes:
        pipe.show(WIN)
    for bird in birds:
        # draw lines from bird to pipe
        if DRAW_LINES:
            try:
                # draw lines from bird to closest pipe
                pygame.draw.line(win, (255,0,0), (bird.x+bird.img.get_width()/2, bird.y + bird.img.get_height()/2), (pipes[pipe_ind].x + pipes[pipe_ind].PIPE_TOP.get_width()/2, pipes[pipe_ind].height), 5)
                pygame.draw.line(win, (255,0,0), (bird.x+bird.img.get_width()/2, bird.y + bird.img.get_height()/2), (pipes[pipe_ind].x + pipes[pipe_ind].PIPE_BOTTOM.get_width()/2, pipes[pipe_ind].bottom), 5)
            except:
                pass
        # draw bird
        if bird.dead == False:
            bird.show()
    pygame.display.update()

setup()

#player = Player(WIN, bird_images ,200, 200)
pipes = [Pipe(pipe_img, 700)]
while True:
    add_pipe = False
    rem = []
    pipe_ind = 0

    for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()

    if len(birds) > 0:
            if len(pipes) > 1 and birds[0].x > pipes[0].x + pipes[0].PIPE_TOP.get_width():  # determine whether to use the first or second
                pipe_ind = 1

    WIN.blit(bg_img, (0, 0))
    for pipe in pipes:
        for bird in birds:
            # detect for bird collision
            if pipe.collide(bird):
                bird.dead = True

        if pipe.x + pipe.PIPE_TOP.get_width() < 0:
            rem.append(pipe)

        if not pipe.passed and pipe.x < bird.x:
            pipe.passed = True
            add_pipe = True
        pipe.update()

    # send bird location to the nn, top pipe location and bottom pipe location
    for i in range(population):
        birds[i].update()
        neat.observe([birds[i].y, abs(birds[i].y - pipes[pipe_ind].height), abs(birds[i].y - pipes[pipe_ind].bottom)], i)

    neat.feed_forward() # feed forward the network
    decisions = neat.think() # get neural network output in form of array

    for i in range(population):
        if decisions[i] > 0.5: # for tanh activation, we decide whether to jump or not if the value is larger than 0.5
            birds[i].jump()

    finish = True
    for i in range(len(birds)):
        if birds[i].dead == False:
            finish = False
            break

    if finish:
        pipes = [Pipe(pipe_img, 700)] # start over again
        for i in range(population):
            neat.set_fitness(birds[i].score, i) # set fitness score after all birds are killed
            birds[i] = Player(WIN, bird_images , 200, 200) # create new bird at given index postion
        print(neat.print_statistic())
        neat.perform_natural_selection() # do crossover and mutation method

    for bird in birds:
            # if bird goes out of map
            if bird.y + bird.img.get_height() - 10 >= FLOOR or bird.y < -50:
                bird.dead = True
    if add_pipe:
        pipes.append(Pipe(pipe_img, 700))

    for i in rem:
        pipes.remove(i)

    draw(birds, WIN, pipes) #draw everything to the screen


from datetime import datetime
import numpy as np
import json
from pandas import read_csv
from pandas import datetime
from matplotlib import pyplot
from sklearn.model_selection import train_test_split
from statsmodels.tsa.arima_model import ARIMA
from sklearn.metrics import mean_squared_error

with open("../Data/companies.json") as file:
    # Load data from the json file
    data = json.load(file)
    quarter_dates = []
    revenue = []
    for x in data['companies'][1]['income_statement']:
        # Fetch date information
        date_list = x['date'].split('-')
        date = datetime(int(date_list[0]), int(date_list[1]), int(date_list[2]))
        quarter_dates.append(date.toordinal())

        # Fetch revenue information
        revenue.append(float(x['Revenue']))

    # Reverse all the dates in the list
    quarter_dates.reverse()
    revenue.reverse()

    # Split data for training and testing
    quarter_dates_train, quarter_dates_test, revenue_train, revenue_test = \
        train_test_split(np.array(quarter_dates), np.array(revenue), test_size=0.30)

    history = [x for x in revenue_train]

    predictions = list()
    for t in range(len(revenue_test)):
        # this model will be different on each iteration because we are appending our predictions to history
        model = ARIMA(history, order=(5, 1, 0))
        model_fit = model.fit(disp=0)
        output = model_fit.forecast()
        yhat = output[0]
        predictions.append(yhat)
        obs = revenue_test[t]
        history.append(obs)
        print('predicted=%f, expected=%f' % (yhat, obs))
    error = mean_squared_error(revenue_test, predictions)
    print('Test MSE: %.3f' % error)

    # plot
    pyplot.plot(revenue_test, label="Actual values")
    pyplot.plot(predictions, color='red', label="Prediction")
    pyplot.xlabel("Date")
    pyplot.ylabel("Revenue ($ Billion)")
    pyplot.legend()
    pyplot.title("Revenue Prediction for the AEP ticker (ARIMA)")
    pyplot.show()