from ..utils import transform_data
from ..metrics import pairwise_euclid_distances
from ..kernel import GaussKernel

import numpy as np

class Bandwidth(object):
    """bandwidth selection for rbf-kernel hyperparameter.

    Args:
        cov (ndarray): [description]
        inv_cov (ndarray): [description]
        bandwidth (ndarray): [description]

    Raises:
        ValueError: [description]
        ValueError: [description]
        NotImplementedError: [description]
        ValueError: [description]

    Returns:
        [type]: [description]

    Notes:
        Bandwidth selection strongly affects kernel density estimation.
        [1] D.W. Scott, “Multivariate Density Estimation: Theory, Practice, and Visualization”, John Wiley & Sons, New York, Chicester, 1992.
        [2] B.W. Silverman, “Density Estimation for Statistics and Data Analysis”, Vol. 26, Monographs on Statistics and Applied Probability, Chapman and Hall, London, 1986.
        [3] P. Hall, “Large Sample Optimality of Least Squares Cross-Validation in Density Estimation,” Ann. Stat., vol. 11, no. 4, pp. 1156–1174, 1983.
        [4] C. J. Stone, “An Asymptotically Optimal Window Selection Rule for Kernel Density Estimates,” Ann. Stat., vol. 12, no. 4, pp. 1285–1297, 1984.
        [5] W. Härdle, P. Hall, and J. S. Marron, “How far are automatically chosen regression smoothing parameters from their optimum?,” J. Am. Stat. Assoc., vol. 83, no. 401, pp. 86–95, 1988.
    """

    def __init__(self, data, method='scott', weights=None):
        """[summary]

        Args:
            data (array-like):
                Datapoints to estimate.
                1D-array [x_1,x_2,...,x_n]. This input is treated as 1-dimensional data.
                2D-array [[x_1^d1,x_1^d2], [x_2^d1,x_2^d2],...,[x_n^d1,x_n^d2]].

            method (str or float, optional):
                Defaults to 'scott'.
                Select the bandwidth selection method from　['cov', 'scott', 'silverman', 'median', 'LCV','LSCV'] .
                - Median heuristic
                - Scott heuristic [1]
                - Silverman heuristic [2]
                - LSCV [3-5]
                - LCV [3-5]
                - covariance of data

            weights (array-like, optional):
                Defaults to None. Weights of datapoints. This must be the same shape as dataset.

        Raises:
            ValueError: [description]
        """
        self.__data = transform_data(data)
        self.__n_data, self.__ndim = self.__data.shape

        if weights is not None:
            self.__weights = np.atleast_1d(weights).astype(float)
            self.__weights /= np.sum(self.__weights)
            if (self.__weights.ndim != 1) or (self.__weights.size != self.__n_data):
                raise ValueError("`weights` input should be one-dimensional or the length of the number of datapoints.")
        else:
            self.__weights = np.ones(self.__n_data) / self.__n_data  # 1/N

        self.__neff = 1. / np.sum(self.__weights**2)  # if weight is None -> neff = N
        self.__compute_covariance()
        self.set_bandwidth(method=method)

    def __compute_covariance(self):
        epsilon = 1e-12
        lambdaI = epsilon * np.eye(self.__ndim)
        if not hasattr(self, '__data_inv_cov'):
            self.__data_cov = np.atleast_2d(np.cov(self.__data, rowvar=False, bias=False, aweights=self.__weights))

            try:
                self.__data_inv_cov = np.linalg.inv(self.__data_cov)

            except:  # noqa: E722
                self.__data_cov += lambdaI
                self.__data_inv_cov = np.linalg.inv(self.__data_cov)

    def set_bandwidth(self, method=None):
        if (method is None) or (method == 'cov'):
            self.__method = 'cov'
            self.__covariance_factor = lambda: 1.0  # band width is std of data.

        elif method == 'median':
            self.__method = method
            self.__covariance_factor = self.median_method

        elif method == 'scott':
            self.__method = method
            self.__covariance_factor = self.scott_factor

        elif method == 'silverman':
            self.__method = method
            self.__covariance_factor = self.silverman_factor

        elif method == 'LSCV':
            """
            minimizing Least Square Cross Validation.
            """
            raise NotImplementedError

        elif method == 'LCV':
            """
            maximizing Likelihood Cross Validation.
            """
            self.__method = method
            self.__covariance_factor = self.LCV_method

        elif np.isscalar(method) and not isinstance(method, str):
            self.__method = 'constant'
            self.__covariance_factor = lambda: method

        else:
            msg = "'method' should be 'cov', 'scott', 'silverman', 'scalar', 'median', 'LSCV'"
            raise ValueError(msg)

        self.__compute_cov()

    def __compute_cov(self):
        self.__factor = self.__covariance_factor()
        if self.__method not in ['median', 'LSCV', 'LCV']:
            self._cov = self.__data_cov * self.__factor**2
            self._inv_cov = self.__data_inv_cov / self.__factor**2

    def scott_factor(self):
        """
        Compute Scott's factor.
        : n ^ (-1/(d+4))
        """
        return np.power(self.__neff, -1. / (self.__ndim + 4))

    def silverman_factor(self):
        """
        Compute the Silverman's factor.
        factor = (4/(d+2))**(1/(d+4)) * n**(-1/(d+4))
        so, j-th bandwidth
        h_j = factor*sig_j
        """
        return np.power(self.__neff * (self.__ndim + 2.0) / 4.0, -1. / (self.__ndim + 4))

    def median_method(self):
        '''
        The Euclidean distance between data is calculated, and the mode of distance is defined as the bandwidth.
        '''
        dists = pairwise_euclid_distances(self.__data, self.__data, True)
        ind = np.triu_indices(self.__n_data, k=1)
        h = np.median(dists[ind])
        if self.__method == 'median':
            """Prevents the calculated cov from changing when this function is executed."""
            self._cov = np.eye(self.__ndim) * h
            self._inv_cov = np.eye(self.__ndim) / h
        return h

    def LCV_method(self):
        if self.__method == 'LCV':
            # try:
            #     import optuna
            # except ImportError:
            #     print("Cannot find optuna library, please install it to use this option.")
            #     raise
            #
            # optuna.logging.disable_default_handler()
            # def LogLikelihood(trial):
            #     epsilon=1e-12
            #     cov = []
            #     for i in range(2):
            #         cov.append(trial.suggest_loguniform(f'h{i}', 1e-3, 1e+2))
            #     cov = np.diag(cov)
            #     M = gauss_kernel(self.__data,self.__data,cov=cov)
            #     return np.log((M-np.diag(np.diag(M))).mean(axis=1)+epsilon).mean().ravel()
            #
            # study = optuna.create_study(direction='maximize')
            # study.optimize(LogLikelihood,n_trials=100)
            # self._cov = np.diag(np.fromiter(study.best_params.values(),dtype=float))
            # self._inv_cov = np.linalg.inv(self._cov)
            try:
                import gpbayesopt
            except ImportError:
                print("Cannot find gpbayesopt library, please install it to use this option. \n \
                      Install: pip install git+https://github.com/JohnYKiyo/bayesian_optimization.git")
                raise

            kernel = GaussKernel(covariance=np.diag(np.ones(self.__ndim)))

            def LogLikelihood(cov):
                # print(cov,kernel.cov)
                epsilon = 1e-12
                kernel.cov = np.diag(cov.ravel())
                M = kernel(self.__data, self.__data, normalize=True)
                return np.log((M - np.diag(np.diag(M))).mean(axis=1) + epsilon).mean().ravel()

            bo = gpbayesopt.BayesOpt(LogLikelihood,
                                     initial_input=np.atleast_2d(np.ones(self.__ndim)),
                                     alpha=1e-6,
                                     kernel=gpbayesopt.kernel.MaternKernel(),
                                     acq=gpbayesopt.acquisition.UCB,
                                     acq_optim=gpbayesopt.acquisition_optimizer.Acquisition_L_BFGS_B_LogOptimizer(bounds=np.array([[-3, 2] for i in range(self.__ndim)]), n_trial=2),
                                     maximize=True,
                                     function_input_unpacking=False)
            bo.run_optim(50)
            self._cov = np.diag(bo.best_params)
            self._inv_cov = np.linalg.inv(self._cov)
            return 1.

    @property
    def cov(self):
        return self._cov

    @property
    def inv_cov(self):
        return self._inv_cov

    @property
    def bandwidth(self):
        return np.sqrt(np.diag(self._cov).sum())

    def __repr__(self):
        val = f'Bandwidth selection method: {self.__method}\n'
        val += f'Number of data: {self.__n_data}\n'
        val += f'Dimensions: {self.__ndim}\n'
        val += f'Covariance matrix:\n {self.cov}\n'
        val += f'Bandwidth: {self.bandwidth}\n'
        return val


import csv
import requests
from bs4 import BeautifulSoup

url ="https://kream.co.kr/social/tabs/style_discovery"
headers ={"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36"}
res=requests.get(url,headers=headers)
res.raise_for_status()

soup = BeautifulSoup(res.text, 'html.parser')

pictures = soup.find_all("div")

print(pictures)


from typing import List
import xlsxwriter

# 读取txt信息函数：
def ReadMatInfo(filepath):
    with open(filepath, 'r') as file:
        data: List[str] = [line.strip() for line in file.readlines()]
        return data

# 将从文件读取的物料X的已确定的MRP进行处理换行符
temp_confirmed_mrp_X = ReadMatInfo('./data_files/MRPConfirmed.txt')[0].split(' ', -1)

# 读取物料X的已确定MRP并加入数组
Confirmed_MRP_X = []
for i in range(len(temp_confirmed_mrp_X) - 1):
    Confirmed_MRP_X.insert(i, int(temp_confirmed_mrp_X[i + 1]))

# 将从文件读取的物料Y的已确定的MRP进行处理换行符
temp_confirmed_mrp_Y = ReadMatInfo('./data_files/MRPConfirmed.txt')[1].split(' ', -1)

# 读取物料Y的已确定MRP并加入数组
Confirmed_MRP_Y = []
for i in range(len(temp_confirmed_mrp_Y) - 1):
    Confirmed_MRP_Y.insert(i, int(temp_confirmed_mrp_Y[i + 1]))

# 将从文件读取的物料X的预测MRP进行处理换行符
temp_pre_mrp_X = ReadMatInfo('./data_files/MRPPredicted.txt')[0].split(' ', -1)

# 读取物料X的预测MRP
Predicted_MRP_X = []
for i in range(len(temp_pre_mrp_X) - 1):
    Predicted_MRP_X.insert(i, int(temp_pre_mrp_X[i + 1]))

# 将从文件读取的物料Y的预测MRP进行处理换行符
temp_pre_mrp_Y = ReadMatInfo('./data_files/MRPPredicted.txt')[1].split(' ', -1)

# 读取物料Y的预测MRP
Predicted_MRP_Y = []
for i in range(len(temp_pre_mrp_Y) - 1):
    Predicted_MRP_Y.insert(i, int(temp_pre_mrp_Y[i + 1]))

# 读取加工车间额定工作能力：

# 读取车间WC01的额定工作能力：
workshop01 = int((ReadMatInfo('./data_files/WCRatedCapacity.txt')[0].split(' ', -1))[1])

# 读取车间WC02的额定工作能力：
workshop02 = int((ReadMatInfo('./data_files/WCRatedCapacity.txt')[1].split(' ', -1))[1])

# 读取文件中工艺路线信息（一共有3条工艺路线）
# 读取第一条工艺路线X1
X1_process_num = int((ReadMatInfo('./data_files/Routing.txt')[0].split(' ', -1))[1])
X1_process_order = int((ReadMatInfo('./data_files/Routing.txt')[0].split(' ', -1))[2])
X1_workshop = (ReadMatInfo('./data_files/Routing.txt')[0].split(' ', -1))[3]
X1_precess_time = int((ReadMatInfo('./data_files/Routing.txt')[0].split(' ', -1))[4])
# 读取第二条工艺路线X2
X2_process_num = int((ReadMatInfo('./data_files/Routing.txt')[1].split(' ', -1))[1])
X2_process_order = int((ReadMatInfo('./data_files/Routing.txt')[1].split(' ', -1))[2])
X2_workshop = (ReadMatInfo('./data_files/Routing.txt')[1].split(' ', -1))[3]
X2_precess_time = int((ReadMatInfo('./data_files/Routing.txt')[1].split(' ', -1))[4])
# 读取第三条工艺路线Y
Y_process_num = int((ReadMatInfo('./data_files/Routing.txt')[2].split(' ', -1))[1])
Y_process_order = int((ReadMatInfo('./data_files/Routing.txt')[2].split(' ', -1))[2])
Y_workshop = (ReadMatInfo('./data_files/Routing.txt')[2].split(' ', -1))[3]
Y_precess_time = int((ReadMatInfo('./data_files/Routing.txt')[2].split(' ', -1))[4])

# 计算过去需求负荷
# 计算WC01的过去需求负载
past_demand_load1 = []
for i in range(len(Confirmed_MRP_X)):
    past_demand_load1.insert(i, Confirmed_MRP_X[i] * X1_precess_time + Confirmed_MRP_Y[i] * Y_precess_time)

# 计算WC02的过去需求负载
past_demand_load2 = []
for i in range(len(Confirmed_MRP_X)):
    past_demand_load2.insert(i, Confirmed_MRP_X[i] * X2_precess_time)

# 计算计划需求负荷
# 计算WC01的计划需求负荷
plan_demand_load1 = []
for i in range(len(Confirmed_MRP_X)):
    plan_demand_load1.insert(i, Predicted_MRP_X[i] * X1_precess_time + Predicted_MRP_Y[i] * Y_precess_time)

# 计算WC02的计划需求负荷
plan_demand_load2 = []
for i in range(len(Confirmed_MRP_X)):
    plan_demand_load2.insert(i, Predicted_MRP_X[i] * X2_precess_time)
# 计算总负荷
# 计算WC01的总负荷
total_load1 = []
for i in range(len(Confirmed_MRP_X)):
    total_load1.insert(i, past_demand_load1[i] + plan_demand_load1[i])

# 计算WC02的总负荷
total_load2 = []
for i in range(len(Confirmed_MRP_X)):
    total_load2.insert(i, past_demand_load2[i] + plan_demand_load2[i])

# 计算平均能力
# 计算WC01的平均能力
average_process_time1 = []
for i in range(len(Confirmed_MRP_X)):
    average_process_time1.insert(i, workshop01)
# 计算WC02的平均能力
average_process_time2 = []
for i in range(len(Confirmed_MRP_X)):
    average_process_time2.insert(i, workshop02)

# 计算余/欠能力
# 计算WC01的余/欠能力
surplus_capacity1 = []
for i in range(len(Confirmed_MRP_X)):
    surplus_capacity1.insert(i, total_load1[i] - average_process_time1[i])

# 计算WC02的余/欠能力
surplus_capacity2 = []
for i in range(len(Confirmed_MRP_X)):
    surplus_capacity2.insert(i, total_load2[i] - average_process_time2[i])

# 计算累计能力
# 计算WC01的累计能力
acc_capacity1 = []
acc_capacity1.insert(0, surplus_capacity1[0])
for i in range(1, len(Confirmed_MRP_X)):
    acc_capacity1.insert(i, surplus_capacity1[i] + surplus_capacity1[i - 1])
# 计算WC02的累计能力
acc_capacity2 = []
acc_capacity2.insert(0, surplus_capacity2[0])
for i in range(1, len(Confirmed_MRP_X)):
    acc_capacity2.insert(i, surplus_capacity2[i] + surplus_capacity2[i - 1])

# 将数据写入到Excel并创建表格
# 创建Excel文档
print("正在进行Excel录入中：")
wb = xlsxwriter.Workbook("./data_files/calculation_of_CRP.xlsx")
# 创建WC01计算结果的表单
ws1 = wb.add_worksheet(name="caculation_of_WC01")
calculation_item = ['过去需求量', '计划需求量', '总负荷量', '平均能力', '余/欠能力', '累计能力']
headline = ['周数', '1', '2', '3', '4', '5', '6']
# 将数据插入到表格中：
ws1.write_row('A1', headline)
ws1.write_column('A2', calculation_item)
ws1.write_row('B2', past_demand_load1)
ws1.write_row('B3', plan_demand_load1)
ws1.write_row("B4", total_load1)
ws1.write_row('B5', average_process_time1)
ws1.write_row('B6', surplus_capacity1)
ws1.write_row('B7', acc_capacity1)

# 创建WC02计算结果的表单
ws2 = wb.add_worksheet(name="caculation_of_WC02")
calculation_item = ['过去需求量', '计划需求量', '总负荷量', '平均能力', '余/欠能力', '累计能力']
headline = ['周数', '1', '2', '3', '4', '5', '6']
# 将数据插入到表格中：
ws2.write_row('A1', headline)
ws2.write_column('A2', calculation_item)
ws2.write_row('B2', past_demand_load2)
ws2.write_row('B3', plan_demand_load2)
ws2.write_row("B4", total_load2)
ws2.write_row('B5', average_process_time2)
ws2.write_row('B6', surplus_capacity2)
ws2.write_row('B7', acc_capacity2)

print("Excel录入完毕！！")
wb.close()

# 在控制台简单输出：
print("WC01结果：")
print("过去需求量是：")
print(past_demand_load1)
print("计划需求量是：")
print(plan_demand_load1)
print("总负荷是：")
print(total_load1)
print("平均能力是：")
print(average_process_time1)
print("余/欠能力是：")
print(surplus_capacity1)
print("累计能力是：")
print(acc_capacity1)

print("WC02结果：")
print("过去需求量是：")
print(past_demand_load2)
print("计划需求量是：")
print(plan_demand_load2)
print("总负荷是：")
print(total_load2)
print("平均能力是：")
print(average_process_time2)
print("余/欠能力是：")
print(surplus_capacity2)
print("累计能力是：")
print(acc_capacity2)


from django.conf.urls import patterns, include, url
from django.conf.urls.i18n import i18n_patterns
from django.conf import settings
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'zabbix_center.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),

    url(r'^admin/', include(admin.site.urls)),

    # bootstrap demo
    url(r'^demo/', include('demo.urls', namespace="demo")),
    url(r'^demo_test/', include('demo_test.urls', namespace="demo_test")),
)

if settings.DEBUG:
	urlpatterns += patterns('',
		url(r'^media/(?P<path>.*)$', 'django.views.static.serve',
			{'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
		url(r'', include('django.contrib.staticfiles.urls')),
	)


from laspy.file import File
import numpy as np
import matplotlib.pyplot as plt
import arcgis

# LasDatasetToRaster()
# arcgis.LasDatasetToRaster()

# arcgis.

inFile = File('17258975.las', mode='r')
rows = int(round(inFile.header.max[1]- inFile.header.min[1]))/8
cols = int(round(inFile.header.max[0]- inFile.header.min[0]))/8
rows = int(rows)
cols = int(cols)
a = np.zeros([rows, cols], dtype =float)
b = np.zeros([rows, cols])
lats = np.round(inFile.x%rows-1)
longs = np.round(inFile.y%cols-1)
for x,y,z in np.nditer([lats, longs, inFile.z]):
    a[int(x),int(y)] += 1
    b[int(x), int(y)] += z
print()
img = b/a
plt.figure()
plt.title("Raster Image")
plt.imshow(img, cmap=plt.cm.gray)
plt.show()
print()
# x = plt.scatter(inFile.X, inFile.Y, s=20, alpha=0.1)
# plt.title('Scatter plot pythonspot.com')
# plt.xlabel('x')
# plt.ylabel('y')
# plt.show()
# print()
#


import pandas as pd
from itertools import combinations
import numpy as np
import datetime
import os

dataFrame = ""
columnsWithSameValue = []
fullEmptyColumns = []

def startAnalyze(csv_path):
	global dataFrame, fullEmptyColumns
	# print("Importing CSV File\n")
	csv = pd.read_csv(csv_path, low_memory=False)
	csv_no_head = pd.read_csv(csv_path, header=None, low_memory=False)
	dataFrame = pd.DataFrame(csv)
	modifiedDataFrame = pd.DataFrame(csv)
	dataFrameNoHead = pd.DataFrame(csv_no_head)
	rows = len(dataFrame.axes[0])
	columns = len(dataFrame.axes[1])
	emptyCells = dataFrame.isnull()
	emptyCellsCount = emptyCells.sum().sum()
	missingRate = str(round((emptyCellsCount / (rows * columns)) * 100, 2))
	fullEmptyColumns = [col for col in dataFrame.columns if dataFrame[col].isnull().all()]
	# print(fullEmptyColumns)
	anyValueMissingColumn = dataFrame.columns[dataFrame.isnull().any()]
	singleValueColumnList = dataFrame.apply(pd.Series.nunique)
	singleValueColumn = singleValueColumnList[singleValueColumnList == 1]
	# anyValueMissingRowsByColumnName = dataFrame[anyValueMissingColumn[0]]
	columnList = pd.DataFrame(dataFrameNoHead.head(1))
	duplicateHeaderNames = [(i, j) for i, j in combinations(columnList, 2) if columnList[i].equals(columnList[j])]
	if len(duplicateHeaderNames) > 0:
		duplicateHeaderNames = duplicateHeaderNames[0]
	duplicateRows = dataFrame[dataFrame.duplicated()]
	anyValueMissingRow = dataFrame[dataFrame.isnull().any(axis=1)]
	anyValueMissingRow = pd.DataFrame(anyValueMissingRow)
	anyValueMissingRow = len(anyValueMissingRow.axes[0])
	for items in fullEmptyColumns:
		# print(items)
		modifiedDataFrame.drop(items, axis=1, inplace=True)
	# print(modifiedDataFrame)
	# print(dataFrame)
	mainDF = list(modifiedDataFrame.head(0))
	for colIt in range(len(modifiedDataFrame.axes[1])):
		for allIt in range(colIt, len(modifiedDataFrame.axes[1])):
			if (modifiedDataFrame.iloc[:, colIt]).equals(modifiedDataFrame.iloc[:, allIt]) and colIt != allIt:
				if mainDF[colIt] not in columnsWithSameValue:
					columnsWithSameValue.append(mainDF[colIt])
			# print(mainDF[colIt] + " matches with " + mainDF[allIt])
	# columnsHavingSameValue = dataFrame.loc[:, ~dataFrame.columns.duplicated()]
	# columnsHavingSameValue = list(columnsHavingSameValue.head(0))
	# print(columnsHavingSameValue)
	# columnsHavingSameValue = [(i, j) for i, j in combinations(dataFrame, 2) if dataFrame[i].equals(dataFrame[j])]
	# if len(columnsHavingSameValue) > 0:
	# 	columnsHavingSameValue = columnsHavingSameValue[0]
	print("Columns: " + str(columns) + "\nRows: " + str(rows) + "\nEmpty Cells: " + str(
		emptyCellsCount) + "\nMissing Rate: " + str(missingRate) + "%" + "\nFull Empty Columns: " + str(
		len(fullEmptyColumns)) + "\nDuplicated Header: " + str(
		len(duplicateHeaderNames)) + "\nSingle value column: " + str(
		len(singleValueColumn)) + "\nColumn with Missing Data: " + str(
		len(anyValueMissingColumn)) + "\nDuplicate Rows: " + str(len(duplicateRows)) + "\nIncomplete Rows: " + str(
		anyValueMissingRow) + "\nColumns with Same Value: " + str(len(columnsWithSameValue)))

	cellMapping = np.zeros((rows, columns))
	for i in range(len(emptyCells)):
		for j in range(columns):
			if not emptyCells.iloc[i, j]:
				cellMapping[i][j] = 1

	duplicateMapping = np.zeros(columns)
	# print(duplicateMapping)
	for j in range(columns):
		for col in duplicateHeaderNames:
			if columnList[col][0] == columnList[j].any():
				duplicateMapping[j] = 1
				break
	# print(duplicateMapping)

	duplicateHeaderArray = [""] * len(duplicateHeaderNames)
	# print(duplicateHeaderArray)
	for key, col in enumerate(duplicateHeaderNames):
		# print('Duplicate header name : ', [col][0])
		# print('Duplicate header name : ', mainDF[col])
		# print('Duplicate header name : ', columnList[col][0])
		# duplicateHeaderArray[key] = columnList[col][0]
		duplicateHeaderArray[key] = mainDF[col]
	# print(duplicateHeaderArray)
	# df.drop_duplicates(subset=['A', 'C'], keep=False) keep first / last

	singleValueColumnArray = [""] * len(pd.DataFrame(singleValueColumn).axes[0])
	# print(singleValueColumnArray)
	for key, col in enumerate(pd.DataFrame(singleValueColumn).axes[0]):
		# print('Single value column name : ', col)
		singleValueColumnArray[key] = col
	# print(singleValueColumnArray)

	anyValueMissingColumnArray = [""] * len(anyValueMissingColumn)
	for key, col in enumerate(anyValueMissingColumn):
		# print('Missing data : ' + col + " (" + str(round((dataFrame[col].isnull().sum()/len(dataFrame[col]))*100, 2)) + "%)")
		anyValueMissingColumnArray[key] = col + "," + str(
			round((dataFrame[col].isnull().sum() / len(dataFrame[col])) * 100, 2))
	# print(anyValueMissingColumnArray)

	columnsHavingSameValueArray = [""] * len(columnsWithSameValue)
	for key, col in enumerate(columnsWithSameValue):
		# print('Same value column name ' + str(key+1) + ' : ', col)
		columnsHavingSameValueArray[key] = col
	# print(columnsHavingSameValueArray)

	missingMapping = np.zeros(columns)
	# print(duplicateMapping)
	for j in range(columns):
		for col in anyValueMissingColumn:
			if col == columnList[j].any():
				missingMapping[j] = round((dataFrame[col].isnull().sum() / len(dataFrame[col])) * 100, 2)
				break
	# print(missingMapping)

	# print(date_finder(csv_path))
	# print(list(dataFrame.filter(regex='date_time')))
	# print(list(dataFrame.filter(regex='date')))
	# print(list(dataFrame.filter(regex='time')))
	# print(list(dataFrame.filter(regex='Date')))
	# print(list(dataFrame.filter(regex='Time')))
	# print(list(dataFrame.filter(regex='DATE')))
	# print(list(dataFrame.filter(regex='TIME')))
	# print(list(dataFrame.filter(regex='DATE_TIME')))
	# print(list(dataFrame.filter(regex='Date_Time')))

	# dataFrame = dataFrame[dataFrame.columns.drop(list(dataFrame.filter(regex='date_time')))]
	# dataFrame = dataFrame[dataFrame.columns.drop(list(dataFrame.filter(regex='date')))]
	# dataFrame = dataFrame[dataFrame.columns.drop(list(dataFrame.filter(regex='time')))]
	# dataFrame = dataFrame[dataFrame.columns.drop(list(dataFrame.filter(regex='Date')))]
	# dataFrame = dataFrame[dataFrame.columns.drop(list(dataFrame.filter(regex='Time')))]
	# dataFrame = dataFrame[dataFrame.columns.drop(list(dataFrame.filter(regex='DATE')))]
	# dataFrame = dataFrame[dataFrame.columns.drop(list(dataFrame.filter(regex='TIME')))]
	# dataFrame = dataFrame[dataFrame.columns.drop(list(dataFrame.filter(regex='DATE_TIME')))]
	# dataFrame = dataFrame[dataFrame.columns.drop(list(dataFrame.filter(regex='Date_Time')))]

	return "Data Received"

def startCleansing(actions):
	global dataFrame, fullEmptyColumns
	print(actions)

	for items in fullEmptyColumns:
		# print(items)
		dataFrame.drop(items, axis=1, inplace=True)

	allActions = actions.split("&")
	for item in allActions:
		if item[:4] == "SAME":
			if item[-4:] != "KEEP":
				print("Remove columns with same value")
				splitItem = item.split('=')[1]
				splitItem = splitItem[:-6]
				# print(splitItem)
				if splitItem in dataFrame.columns:
					dataFrame.drop(splitItem, axis=1, inplace=True)

	for item in allActions:
		if item[:7] == "MISSING":
			# print(item)
			if item[-4:] != "KEEP":
				print("Remove missing data column")
				splitItem = item.split('=')[1]
				splitItem = splitItem[:-6]
				print(splitItem)
				if splitItem in dataFrame.columns:
					dataFrame.drop(splitItem, axis=1, inplace=True)

	for item in allActions:
		if item[:6] == "SINGLE":
			if item[-4:] != "KEEP":
				print("Remove single value column")
				splitItem = item.split('=')[1]
				splitItem = splitItem[:-6]
				print(splitItem)
				if splitItem in dataFrame.columns:
					dataFrame.drop(splitItem, axis=1, inplace=True)

	for item in allActions:
		if item[:9] == "DUPLICATE":
			if item[-4:] != "KEEP":
				print("Remove duplicate column name")
				splitItem = item.split('=')[1]
				splitItem = splitItem[:-6]
				print(splitItem)
				if splitItem in dataFrame.columns:
					dataFrame.drop(splitItem, axis=1, inplace=True)

	for item in allActions:
		if item[:2] == "IR":
			if item[-4:] != "KEEP":
				print("Remove incomplete rows")
				dataFrame = dataFrame.dropna()

	for item in allActions:
		if item[:2] == "DR":
			if item[-4:] != "KEEP":
				print("Remove duplicate rows")
				dataFrame = dataFrame.drop_duplicates(keep='first')

	dataFrame = dataFrame[dataFrame.columns.drop(list(dataFrame.filter(regex='date_time')))]
	dataFrame = dataFrame[dataFrame.columns.drop(list(dataFrame.filter(regex='date')))]
	dataFrame = dataFrame[dataFrame.columns.drop(list(dataFrame.filter(regex='time')))]
	dataFrame = dataFrame[dataFrame.columns.drop(list(dataFrame.filter(regex='Date')))]
	dataFrame = dataFrame[dataFrame.columns.drop(list(dataFrame.filter(regex='Time')))]
	dataFrame = dataFrame[dataFrame.columns.drop(list(dataFrame.filter(regex='DATE')))]
	dataFrame = dataFrame[dataFrame.columns.drop(list(dataFrame.filter(regex='TIME')))]
	dataFrame = dataFrame[dataFrame.columns.drop(list(dataFrame.filter(regex='DATE_TIME')))]
	dataFrame = dataFrame[dataFrame.columns.drop(list(dataFrame.filter(regex='Date_Time')))]

	if not os.path.exists('Output'):
		os.mkdir('Output')

	dataFrame.to_csv(r'Output\\CSV_' + datetime.datetime.now().strftime("%d%m%Y%H%M%S") + '.csv', index=False,
									 header=True)

if __name__ == '__main__':
	startAnalyze(
		"E:\\\Tech Ninja\\CSV-File-Analyzer\\Sample Data\\100020538804102020083946.csv")
# startCleansing(
# 	"DUPLICATEsn_120=sn_12REMOVE&DUPLICATE1=sn_12.1KEEP&SINGLEsn_70=sn_7REMOVE&SINGLEsn_101=sn_10REMOVE&MISSINGsn_20=sn_2REMOVE&MISSINGsn_31=sn_3REMOVE&MISSINGsn_52=sn_5REMOVE&MISSINGsn_73=sn_7REMOVE&MISSINGsn_84=sn_8REMOVE&MISSINGsn_95=sn_9REMOVE&MISSINGsn_106=sn_10REMOVE&MISSINGsn_127=sn_12REMOVE&MISSINGsn_12.18=sn_12.1REMOVE&MISSINGsn_139=sn_13REMOVE&MISSINGsn_1610=sn_16REMOVE&SAMEsn_40=sn_4REMOVE&SAMEsn_141=sn_14REMOVE&DR=DR_REMOVE&IR=IR_REMOVE")


import torch
import torch.nn as nn
import torch.nn.functional as F
from data import DeviceDataLoader
from torch.utils.data import DataLoader

class FederatedNet(torch.nn.Module):
    def __init__(self, input_dim, classes):
        super().__init__()
        self.fc1 = nn.Linear(input_dim, 50)
        self.fc2 = nn.Linear(50, 50)
        self.fc3 = nn.Linear(50, 50)
        self.fc4 = nn.Linear(50, classes)
        self.track_layers = {'fc1': self.fc1, 'fc2': self.fc2, 'fc3': self.fc3, 'fc4': self.fc4}

    def forward(self, x):
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = F.relu(self.fc3(x))
        x = torch.sigmoid(self.fc4(x))
        return x

    def get_track_layers(self):
        return self.track_layers

    def apply_parameters(self, parameters_dict):
        with torch.no_grad():
            for layer_name in parameters_dict:
                self.track_layers[layer_name].weight.data *= 0
                self.track_layers[layer_name].bias.data *= 0
                self.track_layers[layer_name].weight.data += parameters_dict[layer_name]['weight']
                self.track_layers[layer_name].bias.data += parameters_dict[layer_name]['bias']

    def get_parameters(self):
        parameters_dict = dict()
        for layer_name in self.track_layers:
            parameters_dict[layer_name] = {
                'weight': self.track_layers[layer_name].weight.data,
                'bias': self.track_layers[layer_name].bias.data
            }
        return parameters_dict

    def batch_accuracy(self, outputs, labels):
        with torch.no_grad():
            _, predictions = torch.max(outputs, dim=1)
            return torch.tensor(torch.sum(predictions == labels).item() / len(predictions))

    def _process_batch(self, batch):
        images, labels = batch
        outputs = self(images)
        loss = torch.nn.functional.cross_entropy(outputs, labels)
        accuracy = self.batch_accuracy(outputs, labels)
        return (loss, accuracy)

    def fit(self, dataset, device, epochs, lr, batch_size=128, opt=torch.optim.SGD):
        dataloader = DeviceDataLoader(DataLoader(dataset, batch_size, shuffle=True), device)
        optimizer = opt(self.parameters(), lr)
        history = []
        for epoch in range(epochs):
            losses = []
            accs = []
            for batch in dataloader:
                loss, acc = self._process_batch(batch)
                loss.backward()
                optimizer.step()
                optimizer.zero_grad()
                loss.detach()

                losses.append(loss)
                accs.append(acc)
            avg_loss = torch.stack(losses).mean().item()
            avg_acc = torch.stack(accs).mean().item()
            history.append((avg_loss, avg_acc))
        return history

    def evaluate(self, dataset, device, batch_size=128):
        dataloader = DeviceDataLoader(DataLoader(dataset, batch_size), device)
        losses = []
        accs = []
        with torch.no_grad():
            for batch in dataloader:
                loss, acc = self._process_batch(batch)
                losses.append(loss)
                accs.append(acc)
        avg_loss = torch.stack(losses).mean().item()
        avg_acc = torch.stack(accs).mean().item()
        return (avg_loss, avg_acc)


from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
import hashlib
from ecc_functions import *

def is_pkcs7_padded(message):
    padding = message[-message[-1]:]
    return all(padding[i] == len(padding) for i in range(0, len(padding)))

def decrypt_flag(shared_secret: int, iv: str, ciphertext: str):
    # Derive AES key from shared secret
    sha1 = hashlib.sha1()
    sha1.update(str(shared_secret).encode('ascii'))
    key = sha1.digest()[:16]
    # Decrypt flag
    ciphertext = bytes.fromhex(ciphertext)
    iv = bytes.fromhex(iv)
    cipher = AES.new(key, AES.MODE_CBC, iv)
    plaintext = cipher.decrypt(ciphertext)

    if is_pkcs7_padded(plaintext):
        return unpad(plaintext, 16).decode('ascii')
    else:
        return plaintext.decode('ascii')

dict_params = {'iv': 'cd9da9f1c60925922377ea952afc212c',
               'encrypted_flag': 'febcbe3a3414a730b125931dccf912d2239f3e969c4334d95ed0ec86f6449ad8'}
G = (1804, 5368)
p = 9739
a = 497
b = 1768
Nb = 6534
x = G
q_x = 4726
while True:
    x = addition_P(x, G, a, b, p)
    if (x[0] == q_x):
        break
possibles_alice = [x, (x[0], -x[1] % p)]
for possible_alice_point in possibles_alice:
    x = k_P(Nb, possible_alice_point, a, b, p)
    shared_secret = x[0]  # x[1]
    iv = dict_params['iv']
    ciphertext = dict_params['encrypted_flag']
    try:
        print(decrypt_flag(shared_secret, iv, ciphertext))

    except BaseException as e:
        print(str(e))


import json
import sys

import requests

from .quizlet_set import QuizletSet

def get_lazy_dict(text):
    """
    Quizlet lazy loads page content by including a large piece of JSON in its
    response. This parses it into a dictionary.
    """
    begin_query = 'setPageData = '
    end_query = 'QLoad(\'Quizlet.setPageData\')'

    begin_index = text.find(begin_query) + len(begin_query)
    end_index = text.find(end_query) - 2
    # print(f'{begin_index}, {end_index}')
    return json.loads(text[begin_index:end_index])

def get_terms(text):
    data = get_lazy_dict(text)
    terms = []
    for term in data['termIdToTermsMap'].values():
        terms.append({
            'word': term['word'],
            'def': term['definition'],
            'imageUrl': term['_imageUrl']
        })
    return terms

def get_set(url):
    page = requests.get(url)

    terms = get_terms(page.text)
    description = 'Autogenerated by Quizhoot. Original Quizlet: {}'.format(url)
    return QuizletSet(terms, description=description)

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print('Expected path to JSON file')
    else:
        with open(sys.argv[1]) as f:
            data = json.load(f)
        url = data['url']
        out_path = data['outPath']

        qset = get_set(url)
        qset.write_terms_json(out_path)


# coding: utf-8
from __future__ import print_function
from __future__ import division

import torch
import torch.nn as nn
import torch.nn.functional as F

from libcity.model.abstract_model import AbstractModel
from math import sin, cos, sqrt, atan2, radians
import numpy as np

def identity_loss(y_true, y_pred):
    return torch.mean(y_pred - 0 * y_true)

class CARA1(nn.Module):

    def hard_sigmoid(self, x):
        x = x / 6 + 0.5
        x = F.threshold(-x, -1, -1)
        x = F.threshold(-x, 0, 0)
        return x

    def __init__(self, output_dim, input_dim,
                 init='glorot_uniform', inner_init='orthogonal', device=None,
                 **kwargs):
        super(CARA1, self).__init__()
        self.output_dim = output_dim
        self.init = init
        self.inner_init = inner_init
        self.activation = self.hard_sigmoid
        self.inner_activation = nn.Tanh()
        self.device = device
        self.build(input_dim)

    def add_weight(self, shape, initializer):
        ts = torch.zeros(shape)
        if initializer == 'glorot_uniform':
            ts = nn.init.xavier_normal_(ts)
        elif initializer == 'orthogonal':
            ts = nn.init.orthogonal_(ts)

        return nn.Parameter(ts)

    def build(self, input_shape):
        # self.input_spec = [InputSpec(shape=input_shape)]
        self.input_dim = input_shape

        self.W_z = self.add_weight((self.input_dim, self.output_dim),
                                   initializer=self.init)
        self.U_z = self.add_weight((self.output_dim, self.output_dim),
                                   initializer=self.init)
        self.b_z = self.add_weight((self.output_dim,),
                                   initializer='zero')
        self.W_r = self.add_weight((self.input_dim, self.output_dim),
                                   initializer=self.init)
        self.U_r = self.add_weight((self.output_dim, self.output_dim),
                                   initializer=self.init)
        self.b_r = self.add_weight((self.output_dim,),
                                   initializer='zero')
        self.W_h = self.add_weight((self.input_dim, self.output_dim),
                                   initializer=self.init)
        self.U_h = self.add_weight((self.output_dim, self.output_dim),
                                   initializer=self.init)
        self.b_h = self.add_weight((self.output_dim,),
                                   initializer='zero')

        self.A_h = self.add_weight((self.output_dim, self.output_dim),
                                   initializer=self.init)
        self.A_u = self.add_weight((self.output_dim, self.output_dim),
                                   initializer=self.init)

        self.b_a_h = self.add_weight((self.output_dim,),
                                     initializer='zero')
        self.b_a_u = self.add_weight((self.output_dim,),
                                     initializer='zero')

        self.W_t = self.add_weight((self.input_dim, self.output_dim),
                                   initializer=self.init)
        self.U_t = self.add_weight((1, self.output_dim),
                                   initializer=self.init)
        self.b_t = self.add_weight((self.output_dim,),
                                   initializer='zero')

        self.W_g = self.add_weight((self.input_dim, self.output_dim),
                                   initializer=self.init)
        self.U_g = self.add_weight((1, self.output_dim),
                                   initializer=self.init)
        self.b_g = self.add_weight((self.output_dim,),
                                   initializer='zero')

    def preprocess_input(self, x):
        return x

    def forward(self, x):
        """
        X : batch * timeLen * dims(有拓展)
        """
        tlen = x.shape[1]
        output = torch.zeros((x.shape[0], self.output_dim)).to(self.device)
        for i in range(tlen):
            output = self.step(x[:, i, :], output)
        return output

    def step(self, x, states):
        """
        用于多批次同一时间
        states为上一次多批次统一时间数据
        """
        h_tm1 = states

        # phi_t
        u = x[:, self.output_dim: 2 * self.output_dim]
        # delta_t
        t = x[:, 2 * self.output_dim: (2 * self.output_dim) + 1]
        # delta_g
        g = x[:, (2 * self.output_dim) + 1:]
        # phi_v
        x = x[:, :self.output_dim]

        t = self.inner_activation(torch.matmul(t, self.U_t))
        g = self.inner_activation(torch.matmul(g, self.U_g))
        #       Time-based gate
        t1 = self.inner_activation(torch.matmul(x, self.W_t) + t + self.b_t)
        #       Geo-based gate
        g1 = self.inner_activation(torch.matmul(x, self.W_g) + g + self.b_g)

        #       Contextual Attention Gate
        a = self.inner_activation(
            torch.matmul(h_tm1, self.A_h) + torch.matmul(u, self.A_u) + self.b_a_h + self.b_a_u)

        x_z = torch.matmul(x, self.W_z) + self.b_z
        x_r = torch.matmul(x, self.W_r) + self.b_r
        x_h = torch.matmul(x, self.W_h) + self.b_h

        u_z_ = torch.matmul((1 - a) * u, self.W_z) + self.b_z
        u_r_ = torch.matmul((1 - a) * u, self.W_r) + self.b_r
        u_h_ = torch.matmul((1 - a) * u, self.W_h) + self.b_h

        u_z = torch.matmul(a * u, self.W_z) + self.b_z
        u_r = torch.matmul(a * u, self.W_r) + self.b_r
        u_h = torch.matmul(a * u, self.W_h) + self.b_h

        #       update gate
        z = self.inner_activation(x_z + torch.matmul(h_tm1, self.U_z) + u_z)
        #       reset gate
        r = self.inner_activation(x_r + torch.matmul(h_tm1, self.U_r) + u_r)
        #       hidden state
        hh = self.activation(x_h + torch.matmul(r * t1 * g1 * h_tm1, self.U_h) + u_h)

        h = z * h_tm1 + (1 - z) * hh
        h = (1 + u_z_ + u_r_ + u_h_) * h
        return h
        # return h

def bpr_triplet_loss(x):
    positive_item_latent, negative_item_latent = x

    reg = 0

    loss = 1 - torch.log(torch.sigmoid(
        torch.sum(positive_item_latent, dim=-1, keepdim=True) -
        torch.sum(negative_item_latent, dim=-1, keepdim=True))) - reg

    return loss

class Recommender(nn.Module):
    def __init__(self, num_users, num_items, num_times, latent_dim, maxvenue=5, device=None):
        super(Recommender, self).__init__()
        self.maxVenue = maxvenue
        self.latent_dim = latent_dim
        self.device = device
        # num * maxVenue * dim
        self.U_Embedding = nn.Embedding(num_users, latent_dim)
        self.V_Embedding = nn.Embedding(num_items, latent_dim)
        self.T_Embedding = nn.Embedding(num_times, latent_dim)
        torch.nn.init.uniform_(self.U_Embedding.weight)
        torch.nn.init.uniform_(self.V_Embedding.weight)
        torch.nn.init.uniform_(self.T_Embedding.weight)
        self.rnn = nn.Sequential(
            CARA1(latent_dim, latent_dim, device=self.device, input_shape=(self.maxVenue, (self.latent_dim * 2) + 2,),
                  unroll=True,
                  ))
        #       latent_dim * 2 + 2 = v_embedding + t_embedding + time_gap + distance

    def forward(self, x):
        # INPUT = [self.user_input, self.time_input, self.gap_time_input, self.pos_distance_input,
        #          self.neg_distance_input, self.checkins_input,
        #          self.neg_checkins_input]
        # pass
        #       User latent factor
        user_input = x[0]
        time_input = x[1]
        gap_time_input = x[2]
        pos_distance_input = x[3]
        neg_distance_input = x[4]
        checkins_input = x[5]
        neg_checkins_input = x[6]

        self.u_latent = self.U_Embedding(user_input)
        self.t_latent = self.T_Embedding(time_input)

        h, w = gap_time_input.shape
        gap_time_input = gap_time_input.view(h, w, 1)
        rnn_input = torch.cat([self.V_Embedding(checkins_input), self.T_Embedding(time_input), gap_time_input], -1)
        neg_rnn_input = torch.cat([self.V_Embedding(neg_checkins_input), self.T_Embedding(time_input), gap_time_input],
                                  -1)
        h, w = pos_distance_input.shape
        pos_distance_input = pos_distance_input.view(h, w, 1)
        h, w = neg_distance_input.shape
        neg_distance_input = neg_distance_input.view(h, w, 1)
        rnn_input = torch.cat([rnn_input, pos_distance_input], -1)
        neg_rnn_input = torch.cat([neg_rnn_input, neg_distance_input], -1)
        self.checkins_emb = self.rnn(rnn_input)
        self.neg_checkins_emb = self.rnn(neg_rnn_input)

        pred = (self.checkins_emb * self.u_latent).sum(dim=1)
        neg_pred = (self.neg_checkins_emb * self.u_latent).sum(dim=1)
        return bpr_triplet_loss([pred, neg_pred])

    def rank(self, uid, hist_venues, hist_times, hist_time_gap, hist_distances):
        #         hist_venues = hist_venues + [candidate_venue]
        #         hist_times = hist_times + [time]
        #         hist_time_gap = hist_time_gap + [time_gap]
        #         hist_distances = hist_distances + [distance]

        # u_latent = self.U_Embedding(torch.tensor(uid))
        # v_latent = self.V_Embedding(torch.tensor(hist_venues))
        # t_latent = self.T_Embedding(torch.tensor(hist_times))
        u_latent = self.U_Embedding.weight[uid]
        v_latent = self.V_Embedding.weight[hist_venues.reshape(-1)].view(hist_venues.shape[0], hist_venues.shape[1], -1)
        t_latent = self.T_Embedding.weight[hist_times.reshape(-1)].view(hist_times.shape[0], hist_times.shape[1], -1)

        h, w = hist_time_gap.shape
        hist_time_gap = hist_time_gap.reshape(h, w, 1)
        h, w = hist_distances.shape
        hist_distances = hist_distances.reshape(h, w, 1)
        rnn_input = torch.cat([t_latent, torch.FloatTensor(hist_time_gap).to(self.device)], dim=-1)
        rnn_input = torch.cat([rnn_input, torch.FloatTensor(hist_distances).to(self.device)], dim=-1)

        rnn_input = torch.cat([v_latent, rnn_input], dim=-1)

        dynamic_latent = self.rnn(rnn_input)
        scores = torch.mul(dynamic_latent, u_latent).sum(1)
        # scores = np.dot(dynamic_latent, u_latent)
        return scores

class CARA(AbstractModel):
    """rnn model with long-term history attention"""

    def __init__(self, config, data_feature):
        super(CARA, self).__init__(config, data_feature)
        self.loc_size = data_feature['loc_size']
        self.tim_size = data_feature['tim_size']
        self.uid_size = data_feature['uid_size']
        self.id2locid = data_feature['id2locid']
        self.id2loc = []
        self.device = config['device']
        for i in range(self.loc_size - 1):
            self.id2loc.append(self.id2locid[str(i)])
        self.id2loc.append(self.loc_size)
        self.id2loc = np.array(self.id2loc)
        self.coor = data_feature['poi_coor']
        self.rec = Recommender(self.uid_size, self.loc_size, self.tim_size, 10, device=self.device)

    def get_time_interval(self, x):
        y = x[:, :-1]
        y = np.concatenate([x[:, 0, None], y], axis=1)
        return x - y

    def get_time_interval2(self, x):
        y = x[:-1]
        y = np.concatenate([x[0, None], y], axis=0)
        return x - y

    def get_pos_distance(self, x):
        y = np.concatenate([x[:, 0, None, :], x[:, :-1, :]], axis=1)
        r = 6373.0
        rx = np.radians(x)
        ry = np.radians(y)

        d = x - y
        a = np.sin(d[:, :, 0] / 2) ** 2 + np.cos(rx[:, :, 0]) * np.cos(ry[:, :, 0]) * np.sin(d[:, :, 1] / 2) ** 2
        c = 2 * np.arctan2(np.sqrt(a), np.sqrt(1 - a))
        return r * c

    def get_pos_distance2(self, x):
        x = np.array(x.tolist())
        y = np.concatenate([x[0, None, :], x[:-1, :]], axis=0)
        r = 6373.0
        rx = np.radians(x)
        ry = np.radians(y)

        d = x - y
        a = np.sin(d[:, 0] / 2) ** 2 + np.cos(rx[:, 0]) * np.cos(ry[:, 0]) * np.sin(d[:, 1] / 2) ** 2
        c = 2 * np.arctan2(np.sqrt(a), np.sqrt(1-a))
        return r * c

    def get_distance(self, lat1, lng1, lat2, lng2):
        r = 6373.0

        lat1 = radians(lat1)
        lon1 = radians(lng1)
        lat2 = radians(lat2)
        lon2 = radians(lng2)

        dlon = lon2 - lon1
        dlat = lat2 - lat1

        a = sin(dlat / 2) ** 2 + cos(lat1) * cos(lat2) * sin(dlon / 2) ** 2
        c = 2 * atan2(sqrt(a), sqrt(1 - a))

        distance = int(r * c)
        return distance

    def get_neg_checkins(self, vis, x, y):
        len1, len2 = x.shape
        x_res = []
        x_res_distance = y[:].copy()
        for i in range(len1):
            visits = x[i]
            j = np.random.randint(self.loc_size - 1)
            while j in vis[i]:
                j = np.random.randint(self.loc_size - 1)
            tmp = visits[:].copy()
            tmp[-1] = j
            x_res.append(tmp)
            j1 = self.coor[str(self.id2loc[visits[-1]])]
            j = self.coor[str(self.id2loc[j])]
            x_res_distance[i, -1] = self.get_distance(j1[0], j1[1], j[0], j[1])
        return x_res, x_res_distance

    def forward(self, batch):
        hloc = np.array(batch['current_loc'].cpu())[:, :5]
        target = np.array(batch['target'].cpu())
        h = target.shape
        target = target.reshape((*h, 1))
        hloc = np.concatenate([hloc, target], axis=1)
        hloc1 = self.id2loc[hloc]
        tloc = np.array(batch['current_tim'].cpu())[:, :5]
        target_tim = np.array(batch['target_tim'].cpu())
        h = target_tim.shape
        target_tim = target_tim.reshape((*h, 1))
        tloc = np.concatenate([tloc, target_tim], axis=1)
        x_users = batch['uid']
        t_interval = self.get_time_interval(tloc)

        titude = []
        for i in hloc1.reshape(-1):
            titude.append(self.coor[str(i)])
        titude = np.array(titude).reshape((hloc.shape[0], hloc.shape[1], 2))
        pos_distance = self.get_pos_distance(titude)
        x_neg_checkins, x_neg_distance = self.get_neg_checkins(np.array(batch['current_loc'].cpu()), hloc, pos_distance)
        x_neg_checkins = np.array(x_neg_checkins)
        x = [x_users, torch.tensor(tloc).to(self.device),
             torch.FloatTensor(t_interval).to(self.device), torch.FloatTensor(pos_distance).to(self.device),
             torch.FloatTensor(x_neg_distance).to(self.device), torch.tensor(hloc).to(self.device),
             torch.tensor(x_neg_checkins).to(self.device)]
        return self.rec(x)

    def predict(self, batch):
        hloc = np.array(batch['current_loc'].cpu())[:, :5]
        tloc = np.array(batch['current_tim'].cpu())[:, :5]
        x_users = batch['uid']
        my_true = batch['target']
        my_true_tim = batch['target_tim']
        output = []
        for id, mloc in enumerate(hloc):
            hlocs = []
            tlocs = []
            users = []
            t_intervals = []
            distances = []
            target = my_true[id].item()
            target_tim = my_true_tim[id].item()
            mu = x_users[id]
            mh, mt = hloc[id], tloc[id].copy()
            mt = np.append(mt, target_tim)
            mi = self.get_time_interval2(mt)
            for i in range(101):
                mh = hloc[id].copy()
                if i == 0:
                    mh = np.append(mh, target)
                    tmh = self.id2loc[mh]
                    mtt = []
                    for i in tmh.reshape(-1):
                        mtt.append(self.coor[str(i)])
                    mtt = np.array(mtt).reshape((tmh.shape[0], 2))
                    md = self.get_pos_distance2(mtt)
                else:
                    j = target
                    while j == target:
                        j = np.random.randint(0, self.loc_size - 1)
                    mh = np.append(mh, j)
                    tmh = self.id2loc[mh]
                    mtt = []
                    for i in tmh.reshape(-1):
                        mtt.append(self.coor[str(i)])
                    mtt = np.array(mtt).reshape((tmh.shape[0], 2))
                    md = self.get_pos_distance2(mtt)

                hlocs.append(mh)
                tlocs.append(mt)
                users.append(mu.item())
                t_intervals.append(mi)
                distances.append(md)
            output.append(self.rec.rank(np.array(users), np.array(hlocs), np.array(tlocs), np.array(t_intervals),
                                        np.array(distances)).cpu().detach().numpy())
        output = np.array(output)
        return torch.tensor(output).to(self.device)

    def calculate_loss(self, batch):
        return torch.mean(self.forward(batch))


# **************************************************************************** #
#                                                                              #
#                                                         :::      ::::::::    #
#    app.py                                             :+:      :+:    :+:    #
#                                                     +:+ +:+         +:+      #
#    By: msabwat <msabwat@student.42.fr>            +#+  +:+       +#+         #
#                                                 +#+#+#+#+#+   +#+            #
#    Created: 2018/03/29 11:36:55 by msabwat           #+#    #+#              #
#    Updated: 2018/04/26 17:04:39 by msabwat          ###   ########.fr        #
#                                                                              #
# **************************************************************************** #
import requests
import re
from flask import Flask, request
from flask_restful import Resource, Api

app = Flask(__name__)
api = Api(app)
fb_access_token="" ##Value to complete by creating a developper account on facebook or implementing OAUTH?

@app.route('/')

def hello():
    return 'User Status challenge'

def get_byid(response, host, uid):
    r = requests.get("https://graph.facebook.com/v2.12/" + uid + "?fields=link,name&access_token=" + fb_access_token)
    if r.status_code == 200:
        data = r.json()
        response['account_handle'] = data['name']
        response['account_url'] = data['link']
        response['status'] = "up"
    elif r.status_code == 190:
        response['status'] = "Invalid token" #implement a refresh ?
    else:
        response['account_handle'] = 'Not Found'
        response['account_url'] = 'Not Found'
        response['status'] = "down"
    return response

def get_byhandle(response, host, uid):
    r = requests.get("https://facebook.com/" + uid)
    data = str(r)
    r = re.search('fb:\/\/profile\/(.+?)', data)
    if 'profile' in str(r):
        response['account_id'] = str(r)
        response['account_url'] = "https://facebook.com/"+ uid
        response['status'] = "up"
    else:
        r = re.search('\/help\/\?ref\=404', data)
        if  '404' in str(r):
            response['account_id'] = 'Not found'
            response['account_url'] = 'Not found'
            response['status'] = "down"
        else:
            response['status'] = "Unknown error"
    return response

def get_byurl(response, host, uid):
    response['account_id'] = ''
    response['account_handle'] = ''
    response['status'] = ''
    return response

def handle_request(host, uid, uid_type):
    response = {}
    response['hostingparty'] = host
    if 'id' in uid_type:
        response['account_id'] = uid
        response = get_byid(response, host, uid)
    elif 'handle' in uid_type:
        response['account_handle'] = uid
        response = get_byhandle(response, host, uid)
    elif 'url' in uid_type:
        response['account_url'] = uid
        response = get_byurl(response, host, uid)
    return response

def handle_input(host, user_id):
    if 'id=' in user_id:
        user_id = user_id.split('=', 1)
        uid = user_id[1]
        id_type = user_id[0]
    elif 'handle=' in user_id:
        user_id = user_id.split('=', 1)
        uid = user_id[1]
        id_type = user_id[0]
    elif 'url=' in user_id:
        user_id = user_id.split('=', 1)
        uid = user_id[1]
        id_type = user_id[0]
    else:
        return 'Error, invalid request'
    return handle_request(host, uid, id_type)

class UserStatus(Resource):
    def get(self, host, user_id):
        return handle_input(host, user_id)

api.add_resource(UserStatus, '/user_status/<host>/<user_id>')

if __name__ == "__main__":
    app.run(debug=True, host='0.0.0.0', port=80)


import sys
sys.stdin = open('bj17471.txt','r')

from itertools import combinations
def DFS(l,x,ab):
    V[x]=ab
    for X in Map[x]:
        if X in l and V[X]==0:DFS(l,X,ab)
N=int(input())
L=[0]+[int(x)for x in input().split()]
R,Map=10**9,[0]
for i in range(N):
    n,*l=map(int,input().split())
    Map.append(l)
for c in range(1,N//2+1):
    for comb in combinations(range(1,N+1),c):
        V=[0]*(N+1)
        DFS(comb,comb[0],'a')
        b=[int(x)for x in range(1,N+1) if not x in comb]
        DFS(b,b[0],'b')
        if not all(V[1:]):continue
        A,B=0,0
        for i in range(1,N+1):
            if V[i]=='a':A+=L[i]
            else:B+=L[i]
        R=min(R,abs(A-B))
print(R if R!=10**9 else -1)

algoritm/20하반기 코딩테스트/.삼성 기출/게리맨더링/bj17471.py


import logging

log_format = "%(asctime)s:%(levelname)s:%(filename)s: %(message)s"
logging.basicConfig(level=logging.DEBUG,
                    #filename="app.log",
                    format=log_format,
                    handlers=[
                        logging.StreamHandler(),
                        logging.FileHandler("app.log")
                    ])

logging.debug("Debug message")
logging.info("Info message")
logging.warning("Warning message")
logging.error("Error message")

try:
    x = 0
    y = 1 / x
except Exception as exc:
    logging.critical(exc, exc_info=True)


import cv2
import glob
import numpy as np
import argparse
import os

########################## image process #########################
def obtain_single_image(img_path=None):
    cap = cv2.VideoCapture(-1)
    print(cap.isOpened())    # True

    # read it using mjpg streamer
    cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(*"MJPG"))
    cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
    cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
    cap.set(cv2.CAP_PROP_FPS, 180)

    ret, frame = cap.read()
    print("frame shape :", frame.shape)
    cv2.imshow("calibration :", frame)

    cv2.imwrite(img_path, frame)

    # # extract_green_circle(frame=frame, ball_color=ball_color, color_dist=color_dist)

    # if cv2.waitKey(1) & 0xFF == ord('q'):
    #     break
    cv2.waitKey(6000)

    # Release the camera capture object
    cap.release()

def capture_imgs(args=None):
    args.writing_img_path = args.word_writing_path + '/' + args.font_name + '/' + 'stroke_' + str(args.index) + '/' + args.user_name + '/'
    if not os.path.exists(args.writing_img_path):
        os.makedirs(args.writing_img_path)

    args.writing_img_path = args.writing_img_path + '/' + 'writing_' + str(args.img_index) + '.png'
    # args.writing_img_path = args.writing_img_path + '/' + 'evaluation_' + str(args.img_index) + '.png'

    obtain_single_image(img_path=args.writing_img_path)

if __name__ == "__main__":
    print('########## 一、开始内参标定 ##########')
    parser = argparse.ArgumentParser()
    parser.add_argument('--img_path', type=str, default="calibration_3", help='choose mode first !!!!')

    args = parser.parse_args()

    obtain_single_image(img_path="./test_main/data/" + args.img_path + ".png")


import pyvirtualcam
import numpy as np

import mediapipe as mp

from videoclass import *  # threads

# cap= cv2.VideoCapture(0)
DimX= 1280
DimY= 720
cap = VideoStream(1, x=DimX, y=DimY).start()

mp_face_mesh = mp.solutions.face_mesh
face_mesh = mp_face_mesh.FaceMesh()
mpDraw = mp.solutions.drawing_utils
drawing_spec = mpDraw.DrawingSpec(color=[255,0,0],thickness=2, circle_radius=2)

with pyvirtualcam.Camera(width=1280, height=720, fps=20) as cam:
    while True:
        img = cap.read()
        black_image = np.zeros((DimY, DimX, 3), np.uint8)
        #h, w, c = img.shape
        imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        results = face_mesh.process(imgRGB)

        if results.multi_face_landmarks:
            for  faceLms in results.multi_face_landmarks:
                mpDraw.draw_landmarks(imgRGB, faceLms, mp_face_mesh.FACE_CONNECTIONS,
                                      landmark_drawing_spec=drawing_spec,
                                      connection_drawing_spec=drawing_spec)
            cx_min = DimX
            cy_min = DimY
            cx_max = cy_max = 0
            for _, lm in enumerate(faceLms.landmark):
                cx, cy = int(lm.x * DimX), int(lm.y * DimY)
                if cx < cx_min:
                    cx_min = cx
                if cy < cy_min:
                    cy_min = cy
                if cx > cx_max:
                    cx_max = cx
                if cy > cy_max:
                    cy_max = cy
            cv2.rectangle(imgRGB, (cx_min, cy_min), (cx_max, cy_max), (255, 255, 0), 1)
            cv2.putText(imgRGB, "G. Suanno", (cx_min, cy_min-8), cv2.FONT_HERSHEY_SIMPLEX, .8, (255, 0, 255), 2)

        #cv2.imshow('img', imgRGB)
        cam.send(imgRGB)
        cam.sleep_until_next_frame()
        if cv2.waitKey(5) & 0xFF == ord('q'):
            break  # wait for ESC key to exit

    # cap.release()
    cap.stop()
    cv2.destroyAllWindows()


from bs4 import BeautifulSoup
import re,os,time

'''
STAGE I TEXT PROCESSING for both body text and dedications.
'''

def text(soup):
    '''
    Gets the body of the text file into string format.
    -----------------------------------
    Does not grab any text in the tag <front> which contains div tags such as ['title_page', 'dedication',
    'to_the_reader', 'list'...] that are not part of the main text.

    Does not grab any text in the tag <back> which contains div tags such as ['errata', 'index',
    'supplied_by_editor', ...] that are not part of the main text.

    Does not grab any text under the <table> or <foreign> tags

    Does not grab any text under div type 'coat_of_arms' or attribute 'lat'

    '''
    text_list = []
    for sibling in soup.find_all('w'):
        parent_name = [parent.name for parent in sibling.parents]
        parent_attrs = [parent.attrs for parent in sibling.parents]
        divType = [ats['type'] for ats in parent_attrs if 'type' in ats.keys() and ats['type'] == 'coat_of_arms']
        divLat = [ats['xml:lang'] for ats in parent_attrs if 'xml:lang' in ats.keys() and ats['xml:lang'] == 'lat']
        if not any(x in parent_name for x in ['front', 'table', 'back','foreign']) and 'coat_of_arms' not in divType and 'lat' not in divLat and re.search('lemma',str(sibling)) and str(sibling['lemma']) != 'n/a':
            text_list.append(sibling['lemma'])
    return ' '.join(text_list)

def dedication(soup):
    text_list = []
    for sibling in soup.find_all('w'):
        parent_attrs = [parent.attrs for parent in sibling.parents]
        parent_title = [ats['type'] for ats in parent_attrs if 'type' in ats.keys() and ats['type'] == 'dedication']
        if 'dedication' in parent_title and re.search('lemma',str(sibling)) and str(sibling['lemma']) != 'n/a':
            text_list.append(sibling['lemma'])
    return ' '.join(text_list)

def convert(folder,file, textfolder,typeText):
    '''
    Master function for converting each XML file into a dataframe
    '''
    fileName = os.path.join(folder,file)
    name = file.split('.')[0]
    data = open(fileName,'r')
    data = data.read()
    soup = BeautifulSoup(data,'html.parser')
    #call text function to output the body text as a plain text file
    with open(os.path.join(textfolder, name + '.txt'), 'w+') as file:
        if typeText == 'text': toFile = text(soup)
        elif typeText == 'dedication': toFile = dedication(soup)
        file.write(toFile)

if __name__ == '__main__':
    folder = input('Enter folder path: ')
    textfolder = input('Enter the folder path for your output text files: ')
    option = input('Enter A or B or with a number: ')
    typeText = input('Enter text or dedication: ')

    start = time.time()
    count = 0
    for file in os.listdir(folder):
        if option in file:
            count += 1
            if count % 10 == 0 and count != 0:
                print("Processed " + str(count) + " files so far")
            convert(folder,file,textfolder,typeText)
    print('The number of total files is ' + str(count))
    end = time.time()
    print("The time of execution is :", end-start, ' seconds')


# -*- coding: utf-8 -*-
###############################################
# EzTech Software & Consultancy Inc. (c) 2017
###############################################

from openerp import models, fields, api, _
from openerp.exceptions import Warning
import openerp.addons.decimal_precision as dp
from datetime import datetime
from dateutil.relativedelta import relativedelta
import logging
import time

_logger = logging.getLogger(__name__)
DF = "%Y-%m-%d"
EPS = 0.00001

class Loan(models.Model):
    _inherit = "wc.loan"

    @api.model
    def get_aging_data(self, date_as_of=False):
        self.ensure_one()

        if not date_as_of:
            date_as_of = fields.Date.context_today(self)

        total = 0.0
        current = 0.0
        pd_30 = 0.0
        pd_60 = 0.0
        pd_90 = 0.0
        pd_180 = 0.0
        pd_365 = 0.0    #181-365
        pd_over1y = 0.0
        pd_total = 0.0

        d0 = fields.Date.from_string(date_as_of)

        for det in self.details:
            pdue = max(det.principal_due - det.principal_paid, 0.0)
            if pdue>EPS:
                date_due = fields.Date.from_string(det.date_due)
                days = (d0-date_due).days
                total += pdue
                if days<1:
                    current += pdue
                elif days<=30:
                    pd_30 += pdue
                    pd_total += pdue
                elif days<=60:
                    pd_60 += pdue
                    pd_total += pdue
                elif days<=90:
                    pd_90 += pdue
                    pd_total += pdue
                elif days<=180:
                    pd_180 += pdue
                    pd_total += pdue
                elif days<=365:
                    pd_365 += pdue
                    pd_total += pdue
                else:
                    pd_over1y += pdue
                    pd_total += pdue

        return {
            'total': total,
            'current': current,
            'pd_30': pd_30,
            'pd_60': pd_60,
            'pd_90': pd_90,
            'pd_180': pd_180,
            'pd_365': pd_365,
            'pd_over1y': pd_over1y,
            'pd_total': pd_total,
        }

#


import contextlib
import functools
import logging
import os

import click
import torch
from tqdm import tqdm
from transformers import WEIGHTS_NAME, AdamW, get_constant_schedule_with_warmup, get_linear_schedule_with_warmup

logger = logging.getLogger(__name__)

def trainer_args(func):
    @click.option("--learning-rate", default=1e-5)
    @click.option("--lr-schedule", default="warmup_linear", type=click.Choice(["warmup_linear", "warmup_constant"]))
    @click.option("--weight-decay", default=0.01)
    @click.option("--max-grad-norm", default=0.0)
    @click.option("--adam-b1", default=0.9)
    @click.option("--adam-b2", default=0.98)
    @click.option("--adam-eps", default=1e-6)
    @click.option("--adam-correct-bias", is_flag=True)
    @click.option("--warmup-proportion", default=0.06)
    @click.option("--gradient-accumulation-steps", default=1)
    @click.option("--fp16", is_flag=True)
    @click.option("--fp16-opt-level", default="O2")
    @click.option("--fp16-min-loss-scale", default=1)
    @click.option("--fp16-max-loss-scale", default=4)
    @click.option("--save-steps", default=0)
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        return func(*args, **kwargs)

    return wrapper

class Trainer(object):
    def __init__(self, args, model, dataloader, num_train_steps, step_callback=None):
        self.args = args
        self.model = model
        self.dataloader = dataloader
        self.num_train_steps = num_train_steps
        self.step_callback = step_callback

        self.optimizer = self._create_optimizer(model)
        self.scheduler = self._create_scheduler(self.optimizer)

    def train(self):
        model = self.model
        optimizer = self.optimizer

        if self.args.fp16:
            from apex import amp

            model, optimizer = amp.initialize(
                model,
                optimizer,
                opt_level=self.args.fp16_opt_level,
                min_loss_scale=self.args.fp16_min_loss_scale,
                max_loss_scale=self.args.fp16_max_loss_scale,
            )

        if self.args.local_rank != -1:
            model = torch.nn.parallel.DistributedDataParallel(
                model,
                device_ids=[self.args.local_rank],
                output_device=self.args.local_rank,
                find_unused_parameters=True,
            )

        epoch = 0
        global_step = 0
        tr_loss = 0.0

        num_workers = torch.cuda.device_count()

        def maybe_no_sync(step):
            if (
                hasattr(model, "no_sync")
                and num_workers > 1
                and (step + 1) % self.args.gradient_accumulation_steps != 0
            ):
                return model.no_sync()
            else:
                return contextlib.ExitStack()

        model.train()

        with tqdm(total=self.num_train_steps, disable=self.args.local_rank not in (-1, 0)) as pbar:
            while True:
                for step, batch in enumerate(self.dataloader):
                    inputs = {k: v.to(self.args.device) for k, v in self._create_model_arguments(batch).items()}
                    outputs = model(**inputs)
                    loss = outputs[0]
                    if self.args.gradient_accumulation_steps > 1:
                        loss = loss / self.args.gradient_accumulation_steps

                    with maybe_no_sync(step):
                        if self.args.fp16:
                            with amp.scale_loss(loss, optimizer) as scaled_loss:
                                scaled_loss.backward()
                        else:
                            loss.backward()

                    tr_loss += loss.item()
                    if (step + 1) % self.args.gradient_accumulation_steps == 0:
                        if self.args.max_grad_norm != 0.0:
                            if self.args.fp16:
                                torch.nn.utils.clip_grad_norm_(amp.master_params(optimizer), self.args.max_grad_norm)
                            else:
                                torch.nn.utils.clip_grad_norm_(model.parameters(), self.args.max_grad_norm)

                        self.optimizer.step()
                        self.scheduler.step()
                        model.zero_grad()

                        pbar.set_description("epoch: %d loss: %.7f" % (epoch, loss.item()))
                        pbar.update()
                        global_step += 1

                        if self.step_callback is not None:
                            self.step_callback(model, global_step)

                        if (
                            self.args.local_rank in (-1, 0)
                            and self.args.output_dir
                            and self.args.save_steps > 0
                            and global_step % self.args.save_steps == 0
                        ):
                            output_dir = os.path.join(self.args.output_dir, "checkpoint-{}".format(global_step))

                            if hasattr(model, "module"):
                                torch.save(model.module.state_dict(), os.path.join(output_dir, WEIGHTS_NAME))
                            else:
                                torch.save(model.state_dict(), os.path.join(output_dir, WEIGHTS_NAME))

                        if global_step == self.num_train_steps:
                            break

                if global_step == self.num_train_steps:
                    break
                epoch += 1

        logger.info("global_step = %s, average loss = %s", global_step, tr_loss / global_step)

        return model, global_step, tr_loss / global_step

    def _create_optimizer(self, model):
        param_optimizer = list(model.named_parameters())
        no_decay = ["bias", "LayerNorm.weight"]
        optimizer_parameters = [
            {
                "params": [p for n, p in param_optimizer if p.requires_grad and not any(nd in n for nd in no_decay)],
                "weight_decay": self.args.weight_decay,
            },
            {
                "params": [p for n, p in param_optimizer if p.requires_grad and any(nd in n for nd in no_decay)],
                "weight_decay": 0.0,
            },
        ]
        return AdamW(
            optimizer_parameters,
            lr=self.args.learning_rate,
            eps=self.args.adam_eps,
            betas=(self.args.adam_b1, self.args.adam_b2),
            correct_bias=self.args.adam_correct_bias,
        )

    def _create_scheduler(self, optimizer):
        warmup_steps = int(self.num_train_steps * self.args.warmup_proportion)
        if self.args.lr_schedule == "warmup_linear":
            return get_linear_schedule_with_warmup(optimizer, warmup_steps, self.num_train_steps)
        if self.args.lr_schedule == "warmup_constant":
            return get_constant_schedule_with_warmup(optimizer, warmup_steps)

        raise RuntimeError("Unsupported scheduler: " + self.args.lr_schedule)

    def _create_model_arguments(self, batch):
        return batch


# -*- coding: utf-8 -*-
import matplotlib.pyplot as plt
import numpy as np

'''
散点图
'''

def scatter_chart():
    # 绘制表格
    fig = plt.figure()

    # 散点数 根
    n = 128
    # x轴 随机数
    X = np.random.normal(0, 1, n)
    # y轴 随机数
    Y = np.random.normal(0, 1, n)
    # 上色
    T = np.arctan2(Y, X)
    # 显示范围 二选一
    '''
     # 设置 三行三列 第一格子
    ax = fig.add_subplot(3, 3, 1)
    # 绘制散点 x,y s=点的大小 c=颜色上色 ,透明度
    ax.scatter(X, Y, s=75, c=T, alpha=.5)

      或者
    plt.axes([0.025, 0.025, 0.95, 0.95])
     # 绘制散点 x,y s=点的大小 c=颜色上色 ,透明度
    plt.scatter(X, Y, s=75, c=T, alpha=.5)
    '''
    # 设置 三行三列 第一格子
    ax = fig.add_subplot(3, 3, 1)
    # 绘制散点 x,y s=点的大小 c=颜色上色 ,透明度
    ax.scatter(X, Y, s=75, c=T, alpha=.5)

    # x的范围
    plt.xlim(-1.5, 1.5), plt.xticks([])
    # y的范围
    plt.ylim(-1.5, 1.5), plt.yticks([])
    # 可以查看图形的x轴的最小最大坐标和y轴的最小最大坐标
    plt.axis()
    # 设置横轴坐标
    plt.xlabel("x")
    # 设置纵轴坐标
    plt.ylabel("y")
    # 标题
    plt.title("scatter")
    # 生成图表
    plt.show()

'''
散点图1
'''
def scatter_chart_1():
    plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
    plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号

    N = 100
    x = np.random.randn(N)
    y = np.random.randn(N)
    plt.scatter(x, y)

    plt.title("散点图示例01")  # 显示图表名称
    plt.xlabel("x轴")  # x轴名称
    plt.ylabel("y轴")  # y轴名称
    plt.text(+1.2, -1, "By:cat", fontsize=16, color="purple")

    plt.show()

'''
柱状图
'''

def bar_chart():
    # 调节图形大小，宽，高
    fig = plt.figure()

    fig.add_subplot(332)

    n = 10

    X = np.arange(n)

    Y1 = (1 - X / float(n)) * np.random.uniform(0.5, 1.0, n)

    Y2 = (1 - X / float(n)) * np.random.uniform(0.5, 1.0, n)

    plt.bar(X, +Y1, facecolor='#9999ff', edgecolor='white')

    plt.bar(X, -Y2, facecolor='#ff9999', edgecolor='white')

    # 添加注释
    for x, y in zip(X, Y1):
        ## 位置 格式 水平位置 垂直位置
        plt.text(x + 0.4, y + 0.05, '%.2f' % y, ha='center', va='bottom')
    for x, y in zip(X, Y2):
        plt.text(x + 0.4, -y - 0.05, '%.2f' % y, ha='center', va='top')

    # 可以查看图形的x轴的最小最大坐标和y轴的最小最大坐标
    plt.axis()
    # 设置横轴坐标
    plt.xlabel("x")
    # 设置纵轴坐标
    plt.ylabel("y")
    plt.show()

'''
饼图
'''

# https://www.imooc.com/video/14983

def pie_chart():
    plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签

    # 调节图形大小，宽，高
    plt.figure(figsize=(6, 10))

    # 标签
    labels = ['娱乐', '育儿', '饮食', '房贷', '交通', '其它']
    # 数据
    sizes = [2, 5, 12, 70, 2, 9]
    # 每一块离开中心距离
    explode = (0, 0, 0, 0, 0, 0)
    # 自定义颜色列表
    colors = ['r', 'g', 'y', 'b']
    # 构造拼图   每一块离开中心距离  标签 控制饼图内百分比设置 控制饼图内百分比设置  显示阴影 起始绘制角度>90y轴正方向画起 自定义颜色
    patches, l_text, p_text = plt.pie(sizes, explode=explode, labels=labels, autopct='%1.1f%%', shadow=False, startangle=150, colors=colors)
    # 标题
    plt.title("饼图示例-8月份家庭支出")

    # 文本信息
    plt.text(1, -1.2, 'By:cat')

    # 改变文本的大小
    # 方法是把每一个text遍历。调用set_size方法设置它的属性
    for t in l_text:
        t.set_size(30)
    for t in p_text:
        t.set_size(20)
    # 添加图例
    plt.legend(loc="upper right", fontsize=10, bbox_to_anchor=(1.1, 1.05), borderaxespad=0.3)
    # loc =  'upper right' 位于右上角
    # bbox_to_anchor=[0.5, 0.5] # 外边距 上边 右边
    # ncol=2 分两列
    # borderaxespad = 0.3图例的内边距

    # 将饼图显示为正圆形
    # plt.gca().set_aspect('equal')
    # 或者
    plt.axis('equal')
    # 保存图表
    plt.savefig(r"E:\饼图02.png", dpi=200, bbox_inches='tight')
    # 显示图表
    plt.show()

if __name__ == '__main__':
    # 散点图
    # scatter_chart()
    # 散点图1
    # scatter_chart_1()
    # 柱状图
    # bar_chart()
    # 饼图
    pie_chart()

    pass


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('problempost', '0001_initial'),
    ]

    operations = [
        migrations.AddField(
            model_name='problempost',
            name='councilMem',
            field=models.CharField(max_length=30, default=''),
            preserve_default=True,
        ),
        migrations.AddField(
            model_name='problempost',
            name='status',
            field=models.CharField(max_length=10, default=''),
            preserve_default=True,
        ),
    ]