import pandas as pd
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import accuracy_score, roc_curve, auc
from sklearn.decomposition import PCA
from sklearn.naive_bayes import GaussianNB
import matplotlib.pyplot as plt
import time

# 读取数据文件
file_path = "../data/abalone.data"  # 替换为实际文件路径
column_names = ["Sex", "Length", "Diameter", "Height", "Whole weight",
                "Shucked weight", "Viscera weight", "Shell weight", "Rings"]
data = pd.read_csv(file_path, header=None, names=column_names)

# 使用LabelEncoder将标签编码为数字
label_encoder = LabelEncoder()
data['Sex'] = label_encoder.fit_transform(data['Sex'])

# 提取特征列和标签列
X = data.drop("Sex", axis=1)
y_true = data["Sex"]

# 使用PCA进行降维
pca = PCA(n_components=None)  # 设置为None，表示保留所有主成分
X_pca = pca.fit_transform(X)

# 查看PCA保留的维度
num_components = pca.n_components_
print("Number of PCA Components:", num_components)
explained_variance_ratios = pca.explained_variance_ratio_
print("Explained Variance Ratios:", explained_variance_ratios)
# 打印各主成分的标签
for i in range(num_components):
    print("PCA Component {} Label: {}".format(i + 1, column_names[i + 1]))

# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X_pca, y_true, test_size=0.2, random_state=42)
print("X_test", X_test)
print("y_test", y_test)
# 使用朴素贝叶斯分类器（这里使用高斯朴素贝叶斯）

# 设置朴素贝叶斯分类器的参数范围
param_grid = {'var_smoothing': [1e-9, 1e-8, 1e-7, 1e-6, 1e-5, 1e-4, 1e-3, 1e-2, 0.1]}

# 使用GridSearchCV进行交叉验证和参数选择
nb_classifier = GaussianNB()
grid_search = GridSearchCV(nb_classifier, param_grid, cv=5, scoring='accuracy')

# 计算训练时间
start_time = time.time()
grid_search.fit(X_train, y_train)
training_time = time.time() - start_time

# 输出最佳参数和交叉验证准确率
print("Best Parameters:", grid_search.best_params_)
print("Cross-Validation Accuracy:{:.2f}%".format(round((grid_search.best_score_ * 100), 2)))
print("Training Time with Cross-Validation: {:.4f} seconds".format(training_time))

# 在测试集上进行预测
start_time = time.time()
y_pred = grid_search.best_estimator_.predict(X_test)
testing_time = time.time() - start_time
print("Testing Time: {:.4f} seconds".format(testing_time))

# 计算准确率
accuracy = accuracy_score(y_test, y_pred)
print("Classifier Accuracy:{:.2f}%".format(accuracy * 100))

# 计算混淆矩阵
conf_matrix = confusion_matrix(y_test, y_pred)
print("confusion_matrix", conf_matrix)

# 显示混淆矩阵
disp = ConfusionMatrixDisplay(confusion_matrix=conf_matrix, display_labels=label_encoder.classes_)
disp.plot(cmap='viridis', values_format='d')
plt.title('Naive Bayes Confusion Matrix')
# plt.savefig('NaiveBayesConfusionMatrix.png')
plt.show()

# 计算训练和测试的计算复杂度（近似值）
training_complexity = X_train.shape[1] * X_train.shape[0]
testing_complexity = X_test.shape[1] * X_test.shape[0]
print("Approximate Training Complexity: O({})".format(training_complexity))
print("Approximate Testing Complexity: O({})".format(testing_complexity))

# 绘制ROC曲线
y_probs = grid_search.best_estimator_.predict_proba(X_test)

# 为每个类别计算ROC曲线和AUC值
fpr = dict()
tpr = dict()
roc_auc = dict()
for i in range(len(label_encoder.classes_)):
    fpr[i], tpr[i], _ = roc_curve(y_test == i, y_probs[:, i])
    roc_auc[i] = auc(fpr[i], tpr[i])
print("roc_auc", roc_auc)
print("fpr", fpr)
print("tpr", tpr)

# 绘制所有类别的ROC曲线
plt.figure(figsize=(10, 6))
for i in range(len(label_encoder.classes_)):
    plt.plot(fpr[i], tpr[i], label=f'Class {label_encoder.classes_[i]} (AUC = {roc_auc[i]:.2f})')

# 绘制对角线
plt.plot([0, 1], [0, 1], 'k--', lw=2)
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])

# 设置图例
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Naive Bayes (ROC) Curve')
plt.legend(loc="lower right")
# plt.savefig('NaiveBayesROC.png')
plt.show()


from enum import Enum
from typing import Optional

from pygame import (
	Color,
	Rect,
	SRCALPHA,
	Surface,
	draw,
	font as fonts,
)

class Center(str, Enum):
	HORIZONTAL = 'top'
	VERTICAL = 'left'

def center(source: Surface, dest: Surface, axes: Optional[Center] = None) -> Rect:
	rect = source.get_rect()
	rect.center = dest.get_rect().center
	if axes is not None:
		exec(f'rect.{axes} = source.get_rect().{axes}')
	return rect

def blit_center(source: Surface, dest: Surface, axes: Optional[Center] = None):
	dest.blit(source, center(source, dest, axes))

def new_surface(size: Optional[int] = 40) -> Surface:
	return Surface((size, size), SRCALPHA)

def render_text(font: str, text: str, size: int, color: Color) -> Surface:
	return fonts.SysFont(font, size).render(text, True, color)

class ModeButton:
	corner_formulae = [
		'(m, m)',
		'(r.w - tr[1].w - m, m)',
		'(m, r.h - tr[2].h - m)',
	]

	@staticmethod
	def __common(
			color: Color,
			texting: Optional[tuple[str, str, int]] = None
	) -> Surface:
		surf = new_surface()
		if texting is not None:
			blit_center(render_text(*texting, color), surf)
		draw.rect(surf, color, (2, 2, 36, 36), 3)
		return surf

	@staticmethod
	def center(font: str, color: Color) -> Surface:
		return ModeButton.__common(color, (font, '12', 25))

	@staticmethod
	def color(font: str, color: Color) -> Surface:
		return ModeButton.__common(color, (font, 'color', 15))

	@staticmethod
	def corner(font: str, color: Color) -> Surface:
		surf = ModeButton.__common(color)
		text = [render_text(font, str(i + 1), 21, color) for i in range(3)]
		tr = [t.get_rect() for t in text]
		m = 6  # m for margin
		r = surf.get_rect()
		coord = [eval(f, loc) for loc in (locals(),) for f in ModeButton.corner_formulae]
		surf.blits(zip(text, coord), False)
		return surf

	@staticmethod
	def digit(font: str, color: Color) -> Surface:
		return ModeButton.__common(color, (font, '9', 55))


from django import forms

from documents.models import Document
from wresume.utils import CustomClearableFileInput

class DocumentForm(forms.ModelForm):
    class Meta:
        model = Document
        fields = (
            'title',
            'description',
            'doc',
            'user'
        )
        widgets = {
            'user': forms.HiddenInput,
            'doc': CustomClearableFileInput
        }


import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import scipy.stats as stats

# 可视化的中文处理
plt.rcParams['font.sans-serif'] = 'SimHei'
plt.rcParams['axes.unicode_minus'] = False
plt.style.use('ggplot')
cd = pd.read_csv(r'C:\Users\Lenovo\Desktop\chengduxinfang.csv',encoding='gbk')
cd = cd[cd['states'].isin(['住宅在售','住宅售罄','住宅未开盘','住宅下期待开']) ]
cd.price = cd.price.str.extract('(.*?)元/平')
cd.dropna(subset=['price'],inplace=True)
cd['price'] = cd['price'].astype(int)
# print(cd['price'].unique())
# print(cd.nunique())# 计算DataFrame每一列不重复元素的个数
# print(cd.isnull().sum())# 计算DataFrame每一列缺失值的个数
# print(cd.isnull().sum()/cd.shape[0])
# 计算DataFrame每一列不重复元素的个数
# def func(x):
#     y=len(x.unique())
#     return y
# address_unique=cd.apply(func)
# app_info.comments.describe(percentiles=np.arange(0, 1.2, 0.2))
# print(cd.info())
# print(cd.describe())
# print(cd.describe(include=['object']))
# 新增年龄和工龄两列
# df['age'] = pd.datetime.today().year - df.birthday.dt.year
# df['workage'] = pd.datetime.today().year - df.start_work.dt.year

# loandata['term']=loandata['term'].map(str.strip)
# time.strftime('%Y-%m-%d %H:%M:%S,time.localtime())

# sales['销售时间'], sales['销售星期'] = sales['销售时间'].str.split(' ', 1).str
# sales['销售时间'] = pd.to_datetime(sales['销售时间'],format = '%Y-%m-%d',errors = 'coerce')
# sales = sales[(sales['销售数量'] > 0) & (sales['应收金额'] > 0)]
# month = (sales['销售时间'].max() - sales['销售时间'].min()).days//30

##对去重后的数据按照天进行重新采样
#首先要把索引变成时间
# sales.index = pd.DatetimeIndex(sales['销售时间'])
#然后对其按照每天从新采样
# salesd = sales.resample('D').count()

#画图
# salesd.plot(x = salesd.index, y = '实收金额')
# plt.xlabel('Time')
# plt.ylabel('Money')
# plt.title('xiao shou shu ju')
# plt.show()
#按月采样
# salesm = sales.resample('M').count()

# 数据分列
# grade_split = pd.DataFrame((x.split('-') for x in loandata.grade), index=loandata.index,
#                            columns=['grade', 'sub_grade'])  # 将一列数据拆分开显示为两列
# loandata = pd.merge(loandata, grade_split, how='inner', on="categories", right_index=True,
#                     left_index=True)  # 合并两个pd对象，根据列categories合并
# loandata = pd.merge(loandata, grade_split, right_index=True, left_index=True)  # 直接合并pd对象，两个pd对象顺序一样

sns.set_style("whitegrid")
fig,axes=plt.subplots(1,3) #创建一个一行三列的画布
sns.distplot(cd.price,fit = stats.norm,norm_hist = True,ax=axes[0]) #左图
sns.distplot(cd.price,hist=False,ax=axes[1]) #中图
sns.distplot(cd.price,kde=False,ax=axes[2]) #右图
plt.show()

sns.distplot(cd.price,color='steelblue')
plt.show()

# fig,axes=plt.subplots(1,2)
# sns.distplot(cd.price,norm_hist=True,kde=False,ax=axes[0]) #左图 密度
# sns.distplot(cd.price,kde=False,ax=axes[1]) #右图 计数
# plt.show()

baobao = pd.read_csv(r'C:\Users\Lenovo\Desktop\tianmaotianmaobaobao.csv',encoding='gbk')
baobao=baobao.loc[baobao.sale!='None',:]
baobao.index=range(0,baobao.shape[0])
baobao['new_sale']=baobao.sale.str[:-1].astype('int')
y=lambda x:float(x[:-1]) * 10000 if x.find('万') else float(x)
baobao['new_estimate']=baobao.estimate.apply(y)
baobao=baobao.drop(['sale','estimate'],axis=1)

# data = baobao.corr()
# sns.heatmap(data)
# plt.show()

# print(baobao['new_estimate'].sort_values())

# sns.lmplot(x='price', y='new_estimate',data=baobao,fit_reg=True, scatter=True ) #散点图
# plt.show()
# sns.lmplot(x='price', y='new_estimate',hue='shop',data=baobao,fit_reg=False, scatter=True)
# plt.show()

# train = train[-((train.SalePrice < 200000) &  (train.GrLivArea > 4000))]

# sns.jointplot(x='Area',y='Tprice',data=sh) #散点图
# sns.jointplot(x='Area',y='Tprice',data=sh，kind='hex')
# plt.show()

# lm = sns.lmplot(x = 'Age', y = 'Fare', data = titanic, hue = 'Sex', fit_reg=True)
# lm.set(title = 'Fare x Age')
# axes = lm.axes
# axes[0,0].set_ylim(-5,)
# axes[0,0].set_xlim(-5,85)

# from os import path

# d=path.dirname(__file__)

# df.drop(df[df['职位名称'].str.contains('实习')].index, inplace=True)
# train.drop(train[(train['GrLivArea']>4000) & (train['SalePrice']<300000)].index)

# pattern = '\d+'
# df['work_year'] = df['工作经验'].str.findall(pattern)
# # 数据处理后的工作年限
# avg_work_year = []
# # 工作年限
# for i in df['work_year']:
#    # 如果工作经验为'不限'或'应届毕业生',那么匹配值为空,工作年限为0
#    if len(i) == 0:
#        avg_work_year.append(0)
#    # 如果匹配值为一个数值,那么返回该数值
#    elif len(i) == 1:
#        avg_work_year.append(int(''.join(i)))
#    # 如果匹配值为一个区间,那么取平均值
#    else:
#        num_list = [int(j) for j in i]
#        avg_year = sum(num_list)/2
#        avg_work_year.append(avg_year)
# df['工作经验'] = avg_work_year

# prices_avg = {'0到50/月': 0, '50到100/月': 0, '100到150/月': 0, '150到200/月': 0, '大于200/月': 0}
# 	for d in data:
# 		price = int(float(d.get('price')) / float(d.get('area')))
# 		if price in range(0, 50):
# 			prices_avg['0到50/月'] += 1
# 		elif price in range(50, 100):
# 			prices_avg['50到100/月'] += 1
# 		elif price in range(100, 150):
# 			prices_avg['100到150/月'] += 1
# 		elif price in range(150, 200):
# 			prices_avg['150到200/月'] += 1
# 		else:
# 			prices_avg['大于200/月'] += 1
# 	DrawPie(title='上海租房(单位面积)月租金分布饼图', data=prices_avg, savepath='./results')

# gender_map = {0: 'unknown', 1: 'male', 2: 'female'}
# data['gender'] = data['gender'].apply(lambda x: gender_map[x]) #映射
# data.sample(5) #随机选5行

# newdataset = dataset[-dataset['城区'].isin(['燕郊'])]

# data['year']=data['date'].apply(lambda x:x.year)
# data['month']=data['date'].apply(lambda x:x.month)
# data['day']=data['date'].apply(lambda x:x.day)

# plt.subplot2grid((2,3),(1,0), colspan=2)  # colspan = 2 表示横向跨度是 2
# plt.figure(1, figsize=(40, 60))
plt.subplot(2,2,1)
cd['area'].value_counts().plot(kind='bar')
plt.subplot(2,2,2)
cd['area'].value_counts().plot(kind='hist')
plt.subplot(2,2,3)
cd['area'].value_counts().plot(kind='pie')
plt.subplot(2,2,4)
cd['area'].value_counts().plot(kind='kde')
plt.show()
# plt.subplot(3,2,5)
cd['area'].value_counts().plot(kind='barh')
plt.show()
cd['area'].value_counts().plot(kind='box')
plt.show()
cd['area'].value_counts().plot(kind='area')
plt.show()
cd['area'].value_counts().plot(color='r',marker='D')
plt.show()
baobao.plot(kind='scatter', x='price', y='new_estimate', color='g')
plt.show()
'''Series.plot（kind ='line'，ax = None，figsize = None，use_index = True，title = None，grid = None，legend = False，
  style = None，logx = False，logy = False，loglog = False，xticks = None，yticks = None，xlim = None，ylim = None，rot = None，
  fontsize = None，colormap = None，table = False，yerr = None，xerr = None，label = None，secondary_y = False，** kwds ）'''

import scipy.stats as stats
# 绘制直方图
sns.distplot(a = cd['area'].value_counts(), bins = 10, fit = stats.norm, norm_hist = True,
             hist_kws = {'color':'steelblue', 'edgecolor':'black'},
             kde_kws = {'color':'black', 'linestyle':'--', 'label':'核密度曲线'},
             fit_kws = {'color':'red', 'linestyle':':', 'label':'正态密度曲线'})
# 显示图例
plt.legend()
# 显示图形
plt.show()

# 导入绘图模块
import matplotlib.pyplot as plt
# 设置绘图风格
plt.style.use('ggplot')
# 绘制直方图
sunspots.counts.plot(kind = 'hist', bins = 30, normed = True)
# 绘制核密度图
sunspots.counts.plot(kind = 'kde')
# 图形展现
plt.show()


#!/usr/bin/env python3
from typing import Dict, Iterable, List, Optional, Union

import numpy as np
import pandas as pd
from sklearn import preprocessing
from sklearn.preprocessing import _data as sklearn_preprocessing_data

from snowflake import snowpark
from snowflake.ml._internal import telemetry
from snowflake.ml.modeling.framework import _utils, base

class StandardScaler(base.BaseTransformer):
    r"""
    Standardizes features by removing the mean and scaling to unit variance. Values must be of float type.

    For more details on what this transformer does, see [sklearn.preprocessing.StandardScaler]
    (https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.StandardScaler.html).

    Args:
        with_mean: bool, default=True
            If True, center the data before scaling.

        with_std: bool, default=True
            If True, scale the data unit variance (i.e. unit standard deviation).

        input_cols: Optional[Union[str, List[str]]], default=None
            The name(s) of one or more columns in a DataFrame containing a feature to be scaled.

        output_cols: Optional[Union[str, List[str]]], default=None
            The name(s) of one or more columns in a DataFrame in which results will be stored. The number of
            columns specified must match the number of input columns.

        passthrough_cols: Optional[Union[str, List[str]]], default=None
            A string or a list of strings indicating column names to be excluded from any
            operations (such as train, transform, or inference). These specified column(s)
            will remain untouched throughout the process. This option is helpful in scenarios
            requiring automatic input_cols inference, but need to avoid using specific
            columns, like index columns, during training or inference.

        drop_input_cols: Optional[bool], default=False
            Remove input columns from output if set True. False by default.

    Attributes:
        scale_: Optional[Dict[str, float]] = {}
            Dictionary mapping input column names to relative scaling factor to achieve zero mean and unit variance.
            If a variance is zero, unit variance could not be achieved, and the data is left as-is, giving a scaling
            factor of 1. None if with_std is False.

        mean_: Optional[Dict[str, float]] = {}
            Dictionary mapping input column name to the mean value for that feature. None if with_mean is False.

        var_: Optional[Dict[str, float]] = {}
            Dictionary mapping input column name to the variance for that feature. Used to compute scale_. None if
            with_std is False
    """

    def __init__(
        self,
        *,
        with_mean: bool = True,
        with_std: bool = True,
        input_cols: Optional[Union[str, Iterable[str]]] = None,
        output_cols: Optional[Union[str, Iterable[str]]] = None,
        passthrough_cols: Optional[Union[str, Iterable[str]]] = None,
        drop_input_cols: Optional[bool] = False,
    ) -> None:
        """
        Standardize features by removing the mean and scaling to unit variance.

        Args:
            with_mean: If True, center the data before scaling.
                This does not work (and will raise an exception) when attempted on
                sparse matrices, because centering them entails building a dense
                matrix which in common use cases is likely to be too large to fit in
                memory.
            with_std: If True, scale the data to unit variance (or equivalently,
                unit standard deviation).
            input_cols: Single or multiple input columns.
            output_cols: Single or multiple output columns.
            passthrough_cols: A string or a list of strings indicating column names to be excluded from any
                operations (such as train, transform, or inference). These specified column(s)
                will remain untouched throughout the process. This option is helful in scenarios
                requiring automatic input_cols inference, but need to avoid using specific
                columns, like index columns, during in training or inference.
            drop_input_cols: Remove input columns from output if set True. False by default.

        Attributes:
            scale_: dict {column_name: value} or None
                Per feature relative scaling of the data to achieve zero mean and unit
                variance. If a variance is zero, we can't achieve unit variance, and
                the data is left as-is, giving a scaling factor of 1. `scale_` is equal
                to `None` when `with_std=False`.
            mean_: dict {column_name: value} or None
                The mean value for each feature in the training set.
                Equal to ``None`` when ``with_mean=False``.
            var_: dict {column_name: value} or None
                The variance for each feature in the training set. Used to compute
                `scale_`. Equal to ``None`` when ``with_std=False``.
        """
        self.with_mean = with_mean
        self.with_std = with_std

        self.scale_: Optional[Dict[str, float]] = {} if with_std else None
        self.mean_: Optional[Dict[str, float]] = {} if with_mean else None
        self.var_: Optional[Dict[str, float]] = {} if with_std else None

        self.custom_states: List[str] = []
        if with_mean:
            self.custom_states.append(_utils.NumericStatistics.MEAN)
        if with_std:
            self.custom_states.append(_utils.NumericStatistics.VAR_POP)
            self.custom_states.append(_utils.NumericStatistics.STDDEV_POP)

        super().__init__(drop_input_cols=drop_input_cols, custom_states=self.custom_states)

        self.set_input_cols(input_cols)
        self.set_output_cols(output_cols)
        self.set_passthrough_cols(passthrough_cols)

    def _reset(self) -> None:
        """
        Reset internal data-dependent state of the scaler, if necessary.
        __init__ parameters are not touched.
        """
        super()._reset()
        if hasattr(self, "scale_"):
            self.scale_ = {} if self.with_std else None
        if hasattr(self, "mean_"):
            self.mean_ = {} if self.with_mean else None
        if hasattr(self, "var_"):
            self.var_ = {} if self.with_std else None

    @telemetry.send_api_usage_telemetry(
        project=base.PROJECT,
        subproject=base.SUBPROJECT,
    )
    def fit(self, dataset: Union[snowpark.DataFrame, pd.DataFrame]) -> "StandardScaler":
        """
        Compute mean and std values of the dataset.

        Args:
            dataset: Input dataset.

        Returns:
            Fitted scaler.
        """
        super()._check_input_cols()
        super()._check_dataset_type(dataset)
        self._reset()

        if isinstance(dataset, pd.DataFrame):
            self._fit_sklearn(dataset)
        else:
            self._fit_snowpark(dataset)

        self._is_fitted = True
        return self

    def _fit_sklearn(self, dataset: pd.DataFrame) -> None:
        dataset = self._use_input_cols_only(dataset)
        sklearn_scaler = self._create_unfitted_sklearn_object()
        sklearn_scaler.fit(dataset[self.input_cols])

        for i, input_col in enumerate(self.input_cols):
            if self.mean_ is not None:
                self.mean_[input_col] = float(sklearn_scaler.mean_[i])
            if self.scale_ is not None:
                self.scale_[input_col] = float(sklearn_scaler.scale_[i])
            if self.var_ is not None:
                self.var_[input_col] = float(sklearn_scaler.var_[i])

    def _fit_snowpark(self, dataset: snowpark.DataFrame) -> None:
        computed_states = self._compute(dataset, self.input_cols, self.custom_states)

        # assign states to the object
        for input_col in self.input_cols:
            numeric_stats = computed_states[input_col]

            if self.mean_ is not None:
                self.mean_[input_col] = float(numeric_stats[_utils.NumericStatistics.MEAN])

            if self.var_ is not None:
                self.var_[input_col] = float(numeric_stats[_utils.NumericStatistics.VAR_POP])

            if self.scale_ is not None:
                self.scale_[input_col] = sklearn_preprocessing_data._handle_zeros_in_scale(
                    float(numeric_stats[_utils.NumericStatistics.STDDEV_POP])
                )

    @telemetry.send_api_usage_telemetry(
        project=base.PROJECT,
        subproject=base.SUBPROJECT,
    )
    def transform(self, dataset: Union[snowpark.DataFrame, pd.DataFrame]) -> Union[snowpark.DataFrame, pd.DataFrame]:
        """
        Perform standardization by centering and scaling.

        Args:
            dataset: Input dataset.

        Returns:
            transformed_dataset: Output dataset.
        """
        self._enforce_fit()
        super()._check_input_cols()
        super()._check_output_cols()
        super()._check_dataset_type(dataset)

        if isinstance(dataset, snowpark.DataFrame):
            output_df = self._transform_snowpark(dataset)
        else:
            output_df = self._transform_sklearn(dataset)

        return self._drop_input_columns(output_df) if self._drop_input_cols is True else output_df

    def _transform_snowpark(self, dataset: snowpark.DataFrame) -> snowpark.DataFrame:
        """
        Perform standardization by centering and scaling on
        Snowpark dataframe.

        Args:
            dataset: Input dataset.

        Returns:
            Output dataset.
        """
        passthrough_columns = [c for c in dataset.columns if c not in self.output_cols]
        output_columns = []
        for _, input_col in enumerate(self.input_cols):
            output_column = dataset[input_col]

            if self.mean_ is not None:
                output_column = output_column - self.mean_[input_col]
            if self.scale_ is not None:
                output_column = output_column / self.scale_[input_col]

            output_columns.append(output_column)

        transformed_dataset: snowpark.DataFrame = dataset.with_columns(self.output_cols, output_columns)
        # Reorder columns. Passthrough columns are added at the right to the output of the transformers.
        transformed_dataset = transformed_dataset[self.output_cols + passthrough_columns]
        return transformed_dataset

    def _create_unfitted_sklearn_object(self) -> preprocessing.StandardScaler:
        return preprocessing.StandardScaler(with_mean=self.with_mean, with_std=self.with_std)

    def _create_sklearn_object(self) -> preprocessing.StandardScaler:
        """
        Get an equivalent sklearn StandardScaler.

        Returns:
            The Sklearn StandardScaler.
        """
        scaler = self._create_unfitted_sklearn_object()
        if self._is_fitted:
            scaler.scale_ = self._convert_attribute_dict_to_ndarray(self.scale_, np.float64)
            scaler.mean_ = self._convert_attribute_dict_to_ndarray(self.mean_, np.float64)
            scaler.var_ = self._convert_attribute_dict_to_ndarray(self.var_, np.float64)
        return scaler


import datetime as dt

from django.core.validators import MaxValueValidator, MinValueValidator
from django.shortcuts import get_object_or_404
from rest_framework import serializers
from rest_framework.serializers import ValidationError
from django.contrib.auth import get_user_model
from django.contrib.auth.validators import UnicodeUsernameValidator

from reviews.models import Category, Comment, Genre, Review, Title
from users.validators import username_validator

User = get_user_model()

class CategorySerializer(serializers.ModelSerializer):
    """Класс сериализатор категории."""

    class Meta:
        fields = ('name', 'slug',)
        model = Category

class GenreSerializer(serializers.ModelSerializer):
    """Класс сериализатор жанра."""

    class Meta:
        fields = ('name', 'slug')
        model = Genre

class TitleListSerializer(serializers.ModelSerializer):
    """Класс сериализатор получения списка произведений."""

    genre = GenreSerializer(many=True, read_only=True)
    category = CategorySerializer(read_only=True)
    rating = serializers.IntegerField(read_only=True)

    class Meta:
        model = Title
        fields = '__all__'
        read_only_fields = ('id', 'name',
                            'year', 'description')

class CommentSerializer(serializers.ModelSerializer):
    """Сериалайзер комментариев."""

    author = serializers.SlugRelatedField(
        read_only=True,
        slug_field='username'
    )

    class Meta:
        model = Comment
        fields = ('id', 'author', 'pub_date', 'text')
        read_only_fields = ('id', 'pub_date')

class TitleCreateSerializer(serializers.ModelSerializer):
    """Класс сериализатор создания произведений."""

    category = serializers.SlugRelatedField(
        slug_field='slug',
        many=False,
        queryset=Category.objects.all()
    )
    genre = serializers.SlugRelatedField(
        slug_field='slug',
        many=True,
        required=False,
        queryset=Genre.objects.all()
    )

    class Meta:
        model = Title
        fields = '__all__'

    def year_validate(self, value):
        current_year = dt.date.today().year
        if value > current_year:
            raise serializers.ValidationError(
                'Год выпуска не может быть больше текущего'
            )
        return value

    def to_representation(self, instance):
        return TitleListSerializer(instance, context=self.context).data

class ReviewSerializer(serializers.ModelSerializer):
    """Сериализатор модели User."""

    title = serializers.PrimaryKeyRelatedField(
        read_only=True,
    )
    author = serializers.SlugRelatedField(
        default=serializers.CurrentUserDefault(),
        slug_field='username',
        read_only=True
    )
    score = serializers.IntegerField(
        validators=[
            MinValueValidator(1, 'Оценка должна быть не меньше 1.'),
            MaxValueValidator(10, 'Оценка должна быть не больше 10.')
        ],
    )

    class Meta:
        model = Review
        fields = '__all__'

    def validate(self, data):
        if self.context['request'].method == 'POST':
            title_id = self.context.get('view').kwargs.get('title_id')
            title = get_object_or_404(Title, id=title_id)
            request = self.context['request']
            author = request.user
            if Review.objects.filter(title=title, author=author).exists():
                raise serializers.ValidationError('Вы уже оставили свой отзыв'
                                                  'к этому произведению!')
        return data

class SignUpSerializer(serializers.Serializer):
    username = serializers.CharField(
        validators=(
            UnicodeUsernameValidator(),
        ),
        max_length=150
    )
    email = serializers.EmailField(
        max_length=254
    )

    def validate_username(self, username):
        if username.lower() == 'me':
            raise serializers.ValidationError('Недопустимое имя пользователя.')
        return username

    def validate(self, data):
        suspect_email = User.objects.filter(email=data['email']).exists()
        suspect_user = User.objects.filter(username=data['username']).exists()
        if User.objects.filter(username=data['username'],
                               email=data['email']).exists():
            return data
        if suspect_user:
            raise ValidationError('Это имя уже занято')
        if suspect_email:
            raise ValidationError('Эта почта уже зарегистрирована')
        return data

    class Meta:
        model = User
        fields = ('email',
                  'username',
                  )

class TokenSerializer(serializers.Serializer):
    confirmation_code = serializers.CharField(
        max_length=32,
    )
    username = serializers.CharField(max_length=150)

class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('username', 'email',
                  'first_name', 'last_name', 'bio', 'role',)

    def validate_username(self, value):
        return username_validator(value)

class UserRestrictedSerializer(UserSerializer):
    username = serializers.CharField(
        max_length=150,
        required=True,
        validators=[
            username_validator,
        ]
    )
    email = serializers.EmailField(
        required=True,
        max_length=254
    )

    class Meta(UserSerializer.Meta):
        read_only_fields = ('username', 'email', 'role')


import tinder.tinder_resource as tinder_resource
import json

facebook_token = ''
facebook_id = ''

# WARNING: THIS TAKES A WHILE!
def get_detailed_matches():
    data = tinder_resource.get_matches()['matches']
    matches = {}
    for match in data:
        user_id = match['person']['_id']
        user = tinder_resource.get_user(user_id)
        matches[user_id] = user['results']

    return matches

def save_detailed_matches():
    save('detailed_matches.json', jsonify(get_detailed_matches()))

def save_data():
    save('matches_data.json', jsonify(tinder_resource.get_matches()['matches']))
    save('profile.json', jsonify(tinder_resource.get_own_profile()))
    save('raw.json', jsonify(tinder_resource.get_matches()))

def save(file_name, data):
    f = open(file_name, 'w')
    f.write(data)
    f.close()

def jsonify(data):
    return json.dumps(data, indent=4)

tinder_resource.auth(facebook_token=facebook_token, facebook_id=facebook_id)

#save_data()
#save_detailed_matches()


from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from absl.testing import parameterized

import numpy as np
from tensor2tensor.data_generators import problem_hparams
from tensor2tensor.models.research import transformer_aux
import tensorflow.compat.v1 as tf
from tensorflow.compat.v1 import estimator as tf_estimator

class TransformerAuxTest(parameterized.TestCase, tf.test.TestCase):

  @parameterized.parameters(
      dict(
          tensor=np.array(
              [1, 2, 3, 4]
          ),
          shift=0,
          axis=0,
          target=np.array(
              [1, 2, 3, 4]
          ),
      ),
      dict(
          tensor=np.array(
              [1, 2, 3, 4]
          ),
          shift=2,
          axis=0,
          target=np.array(
              [0, 0, 1, 2]
          ),
      ),
      dict(
          tensor=np.array(
              [1, 2, 3, 4]
          ),
          shift=-2,
          axis=0,
          target=np.array(
              [3, 4, 0, 0]
          ),
      ),
      dict(
          tensor=np.array(
              [[1, 2, 3, 4],
               [5, 6, 7, 8]]
          ),
          shift=2,
          axis=1,
          target=np.array(
              [[0, 0, 1, 2],
               [0, 0, 5, 6]]
          ),
      ),
  )
  def test_shift_and_pad(self, tensor, shift, axis, target):
    with self.test_session() as session:
      output = transformer_aux.shift_and_pad(tensor, shift, axis)
      output_val = session.run(output)
      self.assertAllEqual(output_val, target)

  def test_transformer_aux_body(self):
    batch_size = 3
    input_length = 5
    target_length = 16
    vocab_size = 9
    hparams = transformer_aux.transformer_aux_tiny()
    hparams.shift_values = "-5,1,2,3"
    p_hparams = problem_hparams.test_problem_hparams(vocab_size,
                                                     vocab_size,
                                                     hparams)
    hparams.problem_hparams = p_hparams
    inputs = np.random.randint(
        vocab_size, size=(batch_size, input_length, 1, 1))
    targets = np.random.randint(
        vocab_size, size=(batch_size, target_length, 1, 1))
    features = {
        "inputs": tf.constant(inputs, dtype=tf.int32),
        "targets": tf.constant(targets, dtype=tf.int32),
        "target_space_id": tf.constant(1, dtype=tf.int32),
    }
    tf.train.create_global_step()
    model = transformer_aux.TransformerAux(hparams, tf_estimator.ModeKeys.TRAIN,
                                           p_hparams)
    logits, losses = model(features)

    self.assertIn("training", losses)
    self.assertIn("auxiliary", losses)

    with self.test_session() as session:
      session.run(tf.global_variables_initializer())
      logits_val = session.run(logits)
      self.assertEqual(logits_val.shape,
                       (batch_size, target_length, 1, 1, vocab_size))

if __name__ == "__main__":
  tf.test.main()


import os
import io
import plugemin
import unittest

try:
    from unittest import mock
except ImportError:
    import mock

class Loader(object):
    def __init__(self, name, obj):
        self.obj = obj
        self.name = name

    def load(self):
        return self.obj

csv = [
    "name,age,gender\n",
    "alice,23,female\n",
    "bob,32,male\n",
]

json = [
"""{
    "name": "alice",
    "age": 23,
    "gender": "female"
}""",
"""{
    "name": "bob",
    "age": 32,
    "gender": "male"
}"""]

xml = [
"""<root>
    <name>alice</name>
    <age>23</age>
    <gender>female</gender>
</root>""",
"""<root>
    <name>bob</name>
    <age>32</age>
    <gender>male</gender>
</root>""",
]

template = """Hello {{name}}, \nI understand that you are a {{gender}} of {{age}} years of age."""

inmem_file = io.StringIO()

def join_with_expanduser(path):
    return os.path.abspath(os.path.expanduser(os.path.join(*path)))

class TestPlugemin(unittest.TestCase):
    @mock.patch("pkg_resources.iter_entry_points", return_value=[Loader("CsvInput", plugemin.CsvInput), Loader("JsonInput", plugemin.JsonInput)])
    def test_plugemin_template(self, mock_entrypoints):
        """plugemin should take a series of structured data
        and render templates based on each item in the input.

        By default, the template is rendered as a jinja2 template
        and we accept csv
        """
        results = list(plugemin.render(template, csv))
        expected = [
            """Hello alice, \nI understand that you are a female of 23 years of age.""",
            """Hello bob, \nI understand that you are a male of 32 years of age.""",
        ]
        self.assertEqual(expected, results)

    @mock.patch("os.path.isdir", return_value=False)
    @mock.patch("pkg_resources.iter_entry_points", return_value=[Loader("CsvInput", plugemin.CsvInput), Loader("JsonInput", plugemin.JsonInput)])
    def test_plugemin_template_search_path(self, mock_entrypoints, mock_isdir):
        results = list(plugemin.render(template, csv))
        calls = [
            mock.call(join_with_expanduser(["/", "plugemin", "templates"])),
            mock.call(join_with_expanduser(["~", "plugemin", "templates"])),
            mock.call(join_with_expanduser([".", "plugemin", "templates"])),
        ]
        mock_isdir.assert_has_calls(calls)

class TestInputPlugins(unittest.TestCase):

    @mock.patch("pkg_resources.iter_entry_points", return_value=[Loader("CsvInput", plugemin.CsvInput), Loader("JsonInput", plugemin.JsonInput)])
    def test_CsvInput_plugin(self, mock_entrypoints):
        """an input plugin is designed to take an iterator and
        yield dicts based on each item yielded.
        """
        results = list(plugemin.render(template, csv, input_plugin="CsvInput"))
        expected = [
            """Hello alice, \nI understand that you are a female of 23 years of age.""",
            """Hello bob, \nI understand that you are a male of 32 years of age.""",
        ]
        self.assertEqual(expected, results)

    @mock.patch("pkg_resources.iter_entry_points", return_value=[Loader("CsvInput", plugemin.CsvInput), Loader("JsonInput", plugemin.JsonInput)])
    def test_JsonInput_plugin(self, mock_entrypoints):
        results = list(plugemin.render(template, json, input_plugin="JsonInput"))
        expected = [
            """Hello alice, \nI understand that you are a female of 23 years of age.""",
            """Hello bob, \nI understand that you are a male of 32 years of age.""",
        ]
        self.assertEqual(expected, results)

    @mock.patch("pkg_resources.iter_entry_points", return_value=[Loader("CsvInput", plugemin.CsvInput), Loader("JsonInput", plugemin.JsonInput), Loader("XmlInput", plugemin.XmlInput)])
    def test_XmlInput_plugin(self, mock_entrypoints):
        results = list(plugemin.render(template, xml, input_plugin="XmlInput"))
        expected = [
            """Hello alice, \nI understand that you are a female of 23 years of age.""",
            """Hello bob, \nI understand that you are a male of 32 years of age.""",
        ]
        self.assertEqual(expected, results)

    @mock.patch("pkg_resources.iter_entry_points", return_value=[Loader("CsvInput", plugemin.CsvInput)])
    def test_find_plugins_calls_pkgutils_iter_entry_points(self, mock_entrypoints):
        with self.assertRaises(KeyError):
            results = list(plugemin.render(template, json, input_plugin="JsonInput"))


'''FNN-based CBF
   Haiqiang Niu 08/08/2018
   ( modified 03/13/2019 E. Ozanich)
'''
from numpy.random import seed, randn
seed(1)
import tensorflow as tf
tf.set_random_seed(1)
import time
import numpy as np
import math
import os

from keras.layers import Dense, GaussianNoise, Dropout, BatchNormalization, Activation
from keras.optimizers import Adam,SGD,RMSprop
import keras.backend as K
from config import params
from sklearn.preprocessing import MinMaxScaler
from keras.models import Sequential
from keras.callbacks import EarlyStopping, LearningRateScheduler
from keras.regularizers import l1, l2
# instantiate regularizer
reg = l2(0.001)

# # set GPU # #
#os.environ["CUDA_DEVICE_ORDER"] = "00000000:03:00.0"
#os.environ["CUDA_VISIBLE_DEVICES"] = "0,1"
gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=params.GPU_frac) # fix memory usage
tf.GPUOptions.allow_growth = True

# don't use early stopping
#callback = EarlyStopping(monitor='val_loss',
#                  min_delta=0.01,
#                  patience=20, # default: 2, number of increasing steps before stopping
#                  verbose=0, mode='min')

# learning rate scheduler
def step_decay(epoch):
    init=1e-4
    drop = 0.5
    epoch_drop = 30
    lrate = init*math.pow(drop, math.floor((1+epoch)/epoch_drop))
    return lrate

lrate = LearningRateScheduler(step_decay)

# # Set Training file ---------------------------------
fname = 'train'

nlayers = 5
params.nhidden = 512

if params.Coh:
    coh = 'coh'
else:
    coh = 'incoh'

modelname = 'FNN_models/%02ddeep_%04dhid_%s_%s_%03dbatch_%s'% (nlayers, params.nhidden, params.output,                                                          params.loss, params.batch_size, coh)
print(modelname)

#time.sleep(4600) # to delay, if multiple tasks

#-----------------------------------------------------------------
# # load data # #
#  training data
# two sources
x_train = np.load(params.fpath + 'train/x_' + fname + '.npy')
y_train = np.load(params.fpath + 'train/y_' + fname + '.npy')

print(x_train.shape)

#  validation data
params.fname = str(0) + 'dB/'
x_val = x_train
y_val = y_train

#-----------------------------------------------------------------
# # start TF session # #
sess=tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) # start a Tensorflow session using GPU
tf.set_random_seed(1)
input_size = x_train.shape[1]

# # create model # #
model = Sequential()
#model.add(GaussianNoise(0,input_shape=(input_size,)))
model.add(Dense(params.nhidden, activation='relu', input_shape=(input_size,)))
#model.add(GaussianNoise(0.1))
#model.add(Activation(activation='relu'))#, kernel_regularizer=l2(0.02)))

model.add(Dense(params.nhidden, activation='relu'))
model.add(Dense(params.nhidden, activation='relu'))
model.add(Dense(params.nhidden, activation='relu'))
layer7 = model.add(Dense(params.nhidden, activation='relu')) # visualize this layer

#model.add(Dropout(0.5))

model.add(Dense(params.nclasses, activation=params.output))#, activity_regularizer=l2(0.001)))
model.compile(loss=params.loss,
              optimizer=Adam(0.001))#0.1))
model.summary()

#-----------------------------------------------------------------
# # train model # #
t0 = time.time()
print(modelname)
history = model.fit(x_train, y_train,
                    batch_size=params.batch_size,
                    epochs=params.nepochs,
                   # validation_data=(x_val,y_val),
                    verbose=1, shuffle=False)#, callbacks = [callback])
print('%1d epochs trained in %.2f seconds.' % (params.nepochs, (time.time() - t0)) )

#-----------------------------------------------------------------
# # save model and training loss # #
history = history.history
model.save(modelname + '.h5')
np.savez(modelname + '_history', loss=history['loss'])#, val_loss = history['val_loss'])


"""Utilities related to data clustering catalogs."""

import logging

import numpy as np
from scipy import interpolate

from cosmopipe.lib.utils import ScatteredBaseClass
from cosmopipe.lib import mpi

def vector_projection(vector, direction):
    r"""
    Vector components of given vectors in a given direction.

    .. math::
       \mathbf{v}_\mathbf{d} &= (\mathbf{v} \cdot \hat{\mathbf{d}}) \hat{\mathbf{d}} \\
       \hat{\mathbf{d}} &= \frac{\mathbf{d}}{\|\mathbf{d}\|}

    Adapted from https://github.com/bccp/nbodykit/blob/master/nbodykit/transform.py

    Parameters
    ----------
    vector : array
        Array of vectors to be projected (along last dimension).

    direction : array
        Projection direction, 1D or 2D (if different direction for each input ``vector``) array.
        It will be normalized.

    Returns
    -------
    projection : array
        Vector components of the given vectors in the given direction.
        Same shape as input ``vector``.
    """
    direction = np.asarray(direction, dtype='f8')
    direction = direction / (direction ** 2).sum(axis=-1)[:, None] ** 0.5
    projection = (vector * direction).sum(axis=-1)
    projection = projection[:, None] * direction

    return projection

class DistanceToRedshift(object):

    """Class that holds a conversion distance -> redshift."""

    def __init__(self, distance, zmax=100., nz=2048, interp_order=3):
        """
        Initialize :class:`DistanceToRedshift`.
        Creates an array of redshift -> distance in log(redshift) and instantiates
        a spline interpolator distance -> redshift.

        Parameters
        ----------
        distance : callable
            Callable that provides distance as a function of redshift (array).

        zmax : float, default=100.
            Maximum redshift for redshift <-> distance mapping.

        nz : int, default=2048
            Number of points for redshift <-> distance mapping.

        interp_order : int, default=3
            Interpolation order, e.g. ``1`` for linear interpolation, ``3`` for cubic splines.
        """
        self.distance = distance
        self.zmax = zmax
        self.nz = nz
        zgrid = np.logspace(-8,np.log10(self.zmax),self.nz)
        self.zgrid = np.concatenate([[0.], zgrid])
        self.rgrid = self.distance(self.zgrid)
        self.interp = interpolate.UnivariateSpline(self.rgrid,self.zgrid,k=interp_order,s=0)

    def __call__(self, distance):
        """Return (interpolated) redshift at distance ``distance`` (scalar or array)."""
        return self.interp(distance)

class RedshiftDensityInterpolator(ScatteredBaseClass):
    """
    Class that computes and interpolates a redshift density histogram :math:`n(z)` from an array of redshift and optionally weights.
    Adapted from: https://github.com/bccp/nbodykit/blob/master/nbodykit/algorithms/zhist.py
    """

    @mpi.MPIInit
    def __init__(self, redshifts, weights=None, bins=None, fsky=1., radial_distance=None, interp_order=1):
        r"""
        Initialize :class:`RedshiftDensityInterpolator`.

        Parameters
        ----------
        redshifts : array
            Array of redshifts.

        weights : array, default=None
            Array of weights, same shape as ``redshifts``. Defaults to 1.

        bins : int, array, string, default=None
            If `bins` is an integer, it defines the number of equal-width
            bins in the given range. If `bins` is a sequence, it defines the bin
            edges, including the rightmost edge, allowing for non-uniform bin widths.
            If 'scott', Scott's rule is used to estimate the optimal bin width
            from the input data. Defaults to 'scott'.

        fsky : float, default=1
            The sky area fraction, which is used in the volume calculation when normalizing :math:`n(z)`.
            ``1`` corresponds to full-sky: :math:`4 \pi` or :math:`\simeq 41253\; \mathrm{deg}^{2}`.

        radial_distance : callable, default=None
            Radial distance to use when converting redshifts into comoving distance.
            If ``None``, ``redshifts`` and optionally ``bins`` are assumed to be in distance units.

        interp_order : int, default=1
            Interpolation order, e.g. ``1`` for linear interpolation, ``3`` for cubic splines.
        """
        def zrange(redshifts):
            if self.is_mpi_scattered():
                return mpi.min_array(redshifts,mpicomm=self.mpicomm),mpi.max_array(redshifts,mpicomm=self.mpicomm)
            else:
                zrange = None
                if self.is_mpi_root():
                    zrange = np.min(redshifts),np.max(redshifts)
                return self.mpicomm.bcast(zrange,root=self.mpiroot)

        if bins is None or bins == 'scott':
            # scott's rule
            if self.is_mpi_scatter():
                var = mpi.var_array(redshifts,aweights=weights,ddof=1,mpicomm=self.mpicomm)
                gsize = mpi.size_array(redshifts)
            else:
                var,gsize = None,None
                if self.is_mpi_root():
                    var = np.cov(redshifts,aweights=weights,ddof=1)
                    gsize = redshifts.size
                var,gsize = self.mpicomm.bcast((var,gsize),root=self.mpiroot)
            sigma = np.sqrt(var)
            dz = sigma * (24. * np.sqrt(np.pi) / gsize) ** (1. / 3)
            zrange = zrange(redshifts)
            nbins = np.ceil((maxval - minval) * 1. / dx)
            nbins = max(1, nbins)
            edges = minval + dx * np.arange(nbins + 1)

        if np.ndim(bins) == 0:
            bins = np.linspace(*zrange(redshifts),num=bins+1,endpoint=True)

        counts = np.histogram(redshifts,weights=weights,bins=bins)
        if self.is_mpi_scattered():
            counts = mpicomm.allreduce(counts,op=mpi.MPI.SUM)

        if radial_distance is not None:
            dbins = radial_distance(bins)
        else:
            dbins = bins
        dvol = fsky*4./3.*np.pi*(dbins[1:]**3 - dbins[:-1]**3)
        self.z = (bins[:-1] + bins[1:])/2.
        self.density = counts/dvol
        self.spline = interpolate.UnivariateSpline(self.z,self.density,k=interp_order,s=0)

    def __call__(self, z):
        """Return density at redshift ``z`` (scalar or array)."""
        return self.spline(z)

def distance(position):
    """Return cartesian distance, taking coordinates along ``position`` last axis."""
    return np.sqrt((position**2).sum(axis=-1))

def cartesian_to_sky(position, wrap=True, degree=True):
    r"""
    Transform cartesian coordinates into distance, RA, Dec.

    Parameters
    ----------
    position : array of shape (N,3)
        Position in cartesian coordinates.

    wrap : bool, default=True
        Whether to wrap RA in :math:`[0, 2 \pi]`.

    degree : bool, default=True
        Whether RA, Dec are in degree (``True``) or radian (``False``).

    Returns
    -------
    dist : array
        Distance.

    ra : array
        Right Ascension.

    dec : array
        Declination.
    """
    dist = distance(position)
    ra = np.arctan2(position[:,1],position[:,0])
    if wrap: ra %= 2.*np.pi
    dec = np.arcsin(position[:,2]/dist)
    conversion = np.pi/180. if degree else 1.
    return dist, ra/conversion, dec/conversion

def sky_to_cartesian(dist, ra, dec, degree=True, dtype=None):
    """
    Transform distance, RA, Dec into cartesian coordinates.

    Parameters
    ----------
    dist : array
        Distance.

    ra : array
        Right Ascension.

    dec : array
        Declination.

    degree : default=True
        Whether RA, Dec are in degree (``True``) or radian (``False``).

    dtype : numpy.dtype, default=None
        :class:`numpy.dtype` for returned array.

    Returns
    -------
    position : array
        position in cartesian coordinates; of shape (len(dist),3).
    """
    conversion = 1.
    if degree: conversion = np.pi/180.
    position = [None]*3
    cos_dec = np.cos(dec*conversion)
    position[0] = cos_dec*np.cos(ra*conversion)
    position[1] = cos_dec*np.sin(ra*conversion)
    position[2] = np.sin(dec*conversion)
    return (dist*np.asarray(position,dtype=dtype)).T


# python3.5 buildtagger.py <train_file_absolute_path> <model_file_absolute_path>

import os
import math
import sys

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

import random
import datetime
from collections import defaultdict

## HYPERPARAMETERS ##
DEBUG = False

WORD_VEC_DIM = 128
CHAR_VEC_DIM = 32
CNN_WINDOW_K = 3
CNN_FILTERS_L = 32
LSTM_FEATURES = 64
LSTM_LAYERS = 1
LSTM_DROPOUT = 0

TIME_LIMIT_MIN = 9
TIME_LIMIT_SEC = 0
EPOCH = 2
LEARNING_RATE = 0.001

init_time = datetime.datetime.now()
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
if DEBUG: print(device)
torch.set_printoptions(threshold=5000)
torch.manual_seed(3940242394)

TAG_TO_IX = {
  '``' : 0,
  '#' : 1,
  '$' : 2,
  "''" : 3,
  ',' : 4,
  '-LRB-' : 5,
  '-RRB-' : 6,
  '.' : 7,
  ':' : 8,
  'CC' : 9,
  'CD' : 10,
  'DT' : 11,
  'EX' : 12,
  'FW' : 13,
  'IN' : 14,
  'JJ' : 15,
  'JJR' : 16,
  'JJS' : 17,
  'LS' : 18,
  'MD' : 19,
  'NN' : 20,
  'NNP' : 21,
  'NNPS' : 22,
  'NNS' : 23,
  'PDT' : 24,
  'POS' : 25,
  'PRP' : 26,
  'PRP$' : 27,
  'RB' : 28,
  'RBR' : 29,
  'RBS' : 30,
  'RP' : 31,
  'SYM' : 32,
  'TO' : 33,
  'UH' : 34,
  'VB' : 35,
  'VBD' : 36,
  'VBG' : 37,
  'VBN' : 38,
  'VBP' : 39,
  'VBZ' : 40,
  'WDT' : 41,
  'WP' : 42,
  'WP$' : 43,
  'WRB' : 44,
}

IX_TO_TAG = {ix:tag for tag, ix in TAG_TO_IX.items()}

## POTENTIAL STRATS ##
# CNN:
#   padding,
#   layers,
#   stride length,
#   combine instead of max pool,
#   activation function
# DROPOUT
# RANDOM INITIALIZATION OF EMBEDDINGS
# UNKNOWN WORDS
# ADAM
# INIT HIDDEN
# FLATTEN PARAMETERS

## MODEL
class POSModel(nn.Module):

  def __init__(self, word_vocab, char_vocab):
    super(POSModel, self).__init__()

    self.word_vocab = word_vocab
    self.char_vocab = char_vocab
    self.word_vocab_length = len(word_vocab)
    self.char_vocab_length = len(char_vocab)
    self.word_embeddings = nn.Embedding(self.word_vocab_length + 1, WORD_VEC_DIM, padding_idx=len(self.word_vocab)).to(device)
    self.char_embeddings = nn.Embedding(self.char_vocab_length + 1, CHAR_VEC_DIM, padding_idx=len(self.char_vocab)).to(device)

    self.conv1d = nn.Conv1d(in_channels=CHAR_VEC_DIM, out_channels=CNN_FILTERS_L, kernel_size=CNN_WINDOW_K, stride=1, padding=(CNN_WINDOW_K-1)//2, bias=True).to(device)
    self.pool = nn.AdaptiveMaxPool1d(1).to(device)

    self.lstm = nn.LSTM(
      input_size=WORD_VEC_DIM+CNN_FILTERS_L,
      hidden_size=LSTM_FEATURES,
      num_layers=LSTM_LAYERS,
      dropout=LSTM_DROPOUT,
      bidirectional=True
    ).to(device)

    self.hidden2tag = nn.Linear(LSTM_FEATURES * 2, len(TAG_TO_IX)).to(device)

  def forward(self, word_sentence):

    word_input = torch.LongTensor([self.word_vocab_length if word not in self.word_vocab else self.word_vocab[word] for word in word_sentence]).to(device)
    word_embeds = self.word_embeddings(word_input).unsqueeze(1).to(device)

    char_input = [torch.LongTensor([len(self.char_vocab) if char not in self.char_vocab else self.char_vocab[char] for char in word]).to(device) for word in word_sentence]

    char_input = nn.utils.rnn.pad_sequence(char_input, batch_first=True, padding_value=self.char_vocab_length)

    char_embeds = self.char_embeddings(char_input).transpose(1,2).to(device)

    char_rep = self.conv1d(char_embeds).to(device)

    char_rep = self.pool(char_rep).transpose(1,2).to(device)

    word_rep = torch.cat((word_embeds, char_rep), dim=2).to(device)

    self.lstm.flatten_parameters()
    output, _ = self.lstm(word_rep)
    out = self.hidden2tag(output.view(len(word_input), -1))
    return out

def train_model(train_file, model_file):
  # use torch library to save model parameters, hyperparameters, etc. to model_file
  reader = open(train_file)
  out_lines = reader.readlines()
  pairs_sentence_lines = []
  reader.close()

  word_vocab = {}
  char_vocab = {}

  for i in range(0, len(out_lines)):
    cur_out_line = out_lines[i].strip()
    cur_out_tokens = cur_out_line.split(' ')
    pairs_sentence = []

    for j in range(0, len(cur_out_tokens)):
      word, _, tag = cur_out_tokens[j].rpartition('/')
      if word not in word_vocab:
        word_vocab[word] = len(word_vocab)
      for char in word:
        if char not in char_vocab:
          char_vocab[char] = len(char_vocab)

      # convert tags to index at this point
      pairs_sentence.append((word, TAG_TO_IX[tag]))

    pairs_sentence_lines.append(pairs_sentence)

  losses = []
  loss_function = nn.CrossEntropyLoss()
  model = POSModel(word_vocab, char_vocab)

  optimizer = optim.Adam(params=model.parameters(), lr=LEARNING_RATE)

  for epoch in range(EPOCH):
    total_loss = 0
    for i, pair_sentence in enumerate(pairs_sentence_lines):

      word_sentence, tag_sentence = zip(*pair_sentence)
      model.zero_grad()

      output = model(word_sentence).to(device)
      loss = loss_function(output, torch.LongTensor(tag_sentence).to(device)).to(device)
      loss.backward()
      optimizer.step()

      if (i+1) % 100 == 0:

        time_diff = datetime.datetime.now() - init_time

        if time_diff > datetime.timedelta(minutes=TIME_LIMIT_MIN, seconds=TIME_LIMIT_SEC):
          torch.save((word_vocab, char_vocab, model.state_dict()), model_file)
          return

        if DEBUG:
          print ('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}, Time Elapsed: {}'
                .format(epoch+1, EPOCH, i+1, len(pairs_sentence_lines), loss.item(), time_diff))

      total_loss += loss.item()

    losses.append(total_loss)
    torch.save((word_vocab, char_vocab, model.state_dict()), model_file)

  print('Finished...')

if __name__ == "__main__":
  # make no changes here
  train_file = sys.argv[1]
  model_file = sys.argv[2]
  train_model(train_file, model_file)


from PyQt5 import QtCore, QtGui, QtWidgets
import sqlite3
import sys

from PyQt5.QtWidgets import QApplication, QMessageBox
from PyQt5.QtWidgets import QMainWindow, QTableWidgetItem

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
        self.verticalLayout.setObjectName("verticalLayout")
        self.horizontalLayout = QtWidgets.QHBoxLayout()
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
        self.textEdit.setObjectName("textEdit")
        self.horizontalLayout.addWidget(self.textEdit)
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setObjectName("pushButton")
        self.horizontalLayout.addWidget(self.pushButton)
        self.editButton = QtWidgets.QPushButton(self.centralwidget)
        self.editButton.setObjectName("editButton")
        self.horizontalLayout.addWidget(self.editButton)
        spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.horizontalLayout.addItem(spacerItem)
        self.verticalLayout.addLayout(self.horizontalLayout)
        self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
        self.tableWidget.setObjectName("tableWidget")
        self.tableWidget.setColumnCount(0)
        self.tableWidget.setRowCount(0)
        self.verticalLayout.addWidget(self.tableWidget)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "Запуск"))
        self.editButton.setText(_translate("MainWindow", "Изменить"))

class MyWidget(QMainWindow, Ui_MainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.con = sqlite3.connect("films_db.sqlite")
        self.pushButton.clicked.connect(self.update_result)
        self.editButton.clicked.connect(self.update_elems)

    def update_result(self):

        cur = self.con.cursor()
        que = "SELECT * FROM films WHERE " + self.textEdit.toPlainText()
        result = cur.execute(que).fetchall()

        self.tableWidget.setRowCount(len(result))
        self.tableWidget.setColumnCount(len(result[0]))

        for i, elem in enumerate(result):
            for j, val in enumerate(elem):
                self.tableWidget.setItem(i, j, QTableWidgetItem(str(val)))

    def update_elems(self):
        rows = list(set(map(lambda x: x.row(), self.tableWidget.selectedItems())))
        ids = [self.tableWidget.item(i, 0).text() for i in rows]
        valid = QMessageBox.question(self, '',
                                     f"Действительно заменить элементы с id {', '.join(ids)}",
                                     QMessageBox.Yes, QMessageBox.No)
        if valid == QMessageBox.Yes:
            cur = self.con.cursor()
            for i in ids:
                data = cur.execute(f"SELECT * FROM films WHERE id = {i}").fetchone()
                new_data = (data[0], data[1][::-1], data[2] + 1000, data[3], data[4] * 2)
                cur.execute(f"DELETE FROM films WHERE id = {i}")
                cur.execute("INSERT INTO films VALUES (?, ?, ?, ?, ?)", new_data)
            self.con.commit()

sys._excepthook = sys.excepthook

def exception_hook(exctype, value, traceback):
    sys._excepthook(exctype, value, traceback)
    sys.exit(1)

sys.excepthook = exception_hook

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = MyWidget()
    ex.show()
    sys.exit(app.exec())


import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

data = pd.read_csv("./NZX.NZ.csv")

# print(data["Adj Close"])

date = data.Date[0:15]
openVal = data.Open[0:15]
high = data.High[0:15]
low = data.Low[0:15]
close = data.Close[0:15]
adjClose = data["Adj Close"][0:15]
volume = data.Volume[0:15]

for x in range(0, 1):
    print(" Date: " + date[x] + "\n",
          "Open: " + str(openVal[x]) + "\n",
          "High: " + str(high[x]) + "\n",
          "Low: " + str(low[x]) + "\n",
          "Close: " + str(close[x]) + "\n",
          "Vol: " + str(volume[x]) + "\n")

# labels = ['G1', 'G2', 'G3', 'G4', 'G5']
# men_means = [20, 34, 30, 35, 27]
# women_means = [25, 32, 34, 20, 25]

y = []
for i in np.arange(0, openVal.max() + 1):
    y.append(i)
    print(y)
x = np.arange(len(date[0:8]))  # the label locations
width = 0.20  # the width of the bars

fig, ax = plt.subplots()
rects1 = ax.bar(x - width / 1, openVal[0:8], width, label='Open', color="r")
rects2 = ax.bar(x + width / 3, high[0:8], width, label='High', color="g")

# Add some text for labels, title and custom x-axis tick labels, etc.
ax.set_ylabel('Values')
ax.set_title('Open Close Values')
#ax.set_yticks(y)
#ax.set_yticklabels((y))
ax.set_xticks(x)
ax.set_xticklabels((date[0:8]))
ax.legend()

ax.bar_label(rects1, padding=20, color="r")
ax.bar_label(rects2, padding=10, color="g")

plt.ylim(0,2)

fig.autofmt_xdate()
fig.tight_layout()

plt.show()


import os
import re
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

class QuestionsParser():
    def __init__(self, url_list):
        self.url_list = url_list

    def parse_questions(self, number_of_questions_per_difficulty_level):
        url_list = self.url_list
        all_questions_lists = []
        all_questions_urls_list = []
        os.environ['PATH'] = "/Users/ashutoshkumar/seleniumdrivers"
        options = Options()
        options.headless = True
        driver = webdriver.Chrome(options=options)
        for url in url_list:
            print("Getting questions from", url)
            driver.get(url)
            d = {}
            self.__parse_questions_for_url(driver, d)
            questions_list = self.__get_questions_list(d, number_of_questions_per_difficulty_level, all_questions_urls_list)
            all_questions_lists.extend(questions_list)
            time.sleep(3)
        driver.close()
        return all_questions_lists

    def __parse_questions_for_url(self, driver, d):
        problems_table = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((webdriver.common.by.By.CSS_SELECTOR, "table.problems")))
        for row in problems_table.find_elements(webdriver.common.by.By.TAG_NAME, "tr"):
            try: # parse questions details
                problem_details = row.find_element(webdriver.common.by.By.TAG_NAME, "td")
                problem_url = problem_details.find_element(webdriver.common.by.By.TAG_NAME, "a").get_attribute("href")
                problem_name_and_tag = row.find_element(webdriver.common.by.By.CSS_SELECTOR, "td:nth-of-type(2)").text
                posn = problem_name_and_tag.index("\n")
                problem_name = problem_name_and_tag[:posn]
                problem_tag = problem_name_and_tag[posn+1:]
                tmp_d = {}
                tmp_d["problem_code"] = problem_details.text
                tmp_d["problem_url"] = problem_url
                tmp_d["problem_name"] = problem_name
                tmp_d["problem_tag"] = problem_tag
                match_obj = re.search("[A-Z]+",problem_details.text)
                code = match_obj.group()
                if d.get(code, -1) == -1:
                    d[code] = [tmp_d]
                else:
                    d[code].append(tmp_d)
            except Exception as e:
                pass
        try:
            pagination = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((webdriver.common.by.By.CSS_SELECTOR, "div.pagination")))
            last_anchor_tag = pagination.find_elements(webdriver.common.by.By.TAG_NAME, "a")[-1]
            if last_anchor_tag.text == "→":
                last_anchor_tag.click()
                time.sleep(3)
                print("moving to next page...")
                self.__parse_questions_for_url(driver, d)
        except Exception as ex:
            pass

    def __get_questions_list(self, question_dict, num_of_questions_per_difficulty, all_questions_urls_list):
        all_questions_arr = []
        for difficulty in question_dict:
            questions_arr = []
            for question in question_dict[difficulty]:
                question_tags = question['problem_tag']
                question_url = question['problem_url']
                match_obj = re.search('\*+', question_tags)
                if not match_obj and question_url not in all_questions_urls_list:
                    questions_arr.append(question)
                    all_questions_urls_list.append(question_url) # to remove duplicates
                    if len(questions_arr) >= 10:
                        break
            all_questions_arr.extend(questions_arr)
        return all_questions_arr


from django.urls import path
from django.contrib import admin
from basicapp import views
from django.conf import settings
from django.conf.urls.static import static

app_name = 'basicapp'
urlpatterns = [
    path('', views.user_login, name='login'),
    path('instituteAdmin/adduser/', views.adduser, name='adduser'),
    path('accountInfo/', views.userAccountInfo, name='accountInfo'),
    path('addnewsfeed/', views.addNewsFeed, name='addnewsfeed'),
    path('index/', views.index, name='index'),
    path('InstituteProfilePage/', views.instituteprofilepage, name='InstituteProfilePage'),
    path('instituteAdmin/InstituteExtra/', views.InstituteExtra, name='InstituteInfo'),
    path('index/addNewsFeed/', views.instituteAdmin, name='newsfeed'),
    path('InstituteExtra/InstituteSocities/', views.institutesocities, name='InstituteSocities'),
    path('index/<int:pk>/', views.NewsFeedDetail.as_view(), name ='detail'),
    path('index/<int:pk>/comment/', views.add_comment_to_post, name = 'add_comment_to_post'),
    path('index/internships/', views.internships, name = 'internships'),
    path('index/scholarships/', views.scholarships, name = 'scholarships'),
    path('index/programmes/', views.programmes, name = 'programmes'),
    path('index/addUserInterest/', views.addUserInterest, name='addUserInterest'),

]

urlpatterns+=static(settings.STATIC_URL,document_root=settings.STATIC_ROOT)
urlpatterns+=static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)


from collections import defaultdict
import numpy as np

data = [[int(point) for point in row] for row in open('input.txt').read().split('\n')]

graph_input = np.array(data)

class Graph():
  def __init__(self):
    self.caves = defaultdict(list)
    self.risks = {}

  def add_edge(self, from_cave, to_cave, risk):
    self.caves[from_cave].append(to_cave)
    self.caves[to_cave].append(from_cave)
    self.risks[(from_cave, to_cave)] = risk
    # self.risks[(to_cave, from_cave)] = risk

def dijsktra(graph, initial, end):
  # shortest paths is a dict of nodes
  # whose value is a tuple of (previous node, weight)
  shortest_paths = {initial: (None, 0)}
  current_node = initial
  visited = set()

  while current_node != end:
    visited.add(current_node)
    destinations = graph.caves[current_node]
    weight_to_current_node = shortest_paths[current_node][1]

    for next_node in destinations:
      weight = graph.risks[(current_node, next_node)] + weight_to_current_node
      if next_node not in shortest_paths:
        shortest_paths[next_node] = (current_node, weight)
      else:
        current_shortest_weight = shortest_paths[next_node][1]
        if current_shortest_weight > weight:
          shortest_paths[next_node] = (current_node, weight)

    next_destinations = {node: shortest_paths[node] for node in shortest_paths if node not in visited}
    if not next_destinations:
      return "Route Not Possible"
    # next node is the destination with the lowest weight
    current_node = min(next_destinations, key=lambda k: next_destinations[k][1])

  # Work back through destinations in shortest path
  path = []
  while current_node is not None:
    path.append(current_node)
    next_node = shortest_paths[current_node][0]
    current_node = next_node
  # Reverse path
  path = path[::-1]
  return path

# get indices for caves within the cave system
# and get a list of neighbors for each cave
indices = list(np.ndindex(graph_input.shape))
neighbors = defaultdict(list)

for i in indices:
  # for j in [(i[0]-1, i[1]-1), (i[0]-1, i[1]), (i[0]-1, i[1]+1), (i[0], i[1]-1), (i[0], i[1]+1), (i[0]+1, i[1]-1), (i[0]+1, i[1]), (i[0]+1, i[1]+1)]:
  for j in [(i[0]-1, i[1]), (i[0], i[1]-1), (i[0], i[1]+1), (i[0]+1, i[1])]:
    if j in indices:
      neighbors[i].append(j)

# build the graph
graph = Graph()

for i in indices:
  for n in neighbors[i]:
    graph.add_edge(i, n, graph_input[n])

start = (0,0)
x, y = graph_input.shape
end = (x-1, y-1)
path = dijsktra(graph, start, end)

print(sum([graph_input[p] for p in path][1:]))

# 70 wrong


#!/usr/bin/env python
import os

from setuptools import find_packages, setup

README = open('README.rst').read()

setup(
    name='cmsplugin-',
    version=__import__('cmsplugin_').__version__,
    author='...',
    author_email='...@gmail.com',
    description = ('django-cms plugin template to quickly create your own custom plugins.'),
    long_description = README,
    license = "BSD",
    url = "https://github.com/.../cmsplugin-",
    packages=find_packages(),
    provides=['cmsplugin_', ],
    include_package_data=True,
    install_requires = [
        'django-sekizai',
    ],
)


# -*- coding: utf-8 -*-
"""Bowtie pager."""

import flask
from flask_socketio import emit
import eventlet

class Pager(object):
    """Tell the client to send a message to the server."""

    _NEXT_UUID = 0

    @classmethod
    def _next_uuid(cls):
        cls._NEXT_UUID += 1
        return cls._NEXT_UUID

    def __init__(self):
        """Create a pager."""
        self._uuid = Pager._next_uuid()

    def notify(self):
        """Notify the client.

        The function passed to ``App.respond`` will get called.
        """
        if flask.has_request_context():
            emit('page#' + str(self._uuid))
        else:
            sio = flask.current_app.extensions['socketio']
            sio.emit('page#' + str(self._uuid))
        eventlet.sleep()


from typing import TYPE_CHECKING, Any, Type, TypeVar, Union, cast

import numpy as np

from docarray.typing.tensor.audio.abstract_audio_tensor import AbstractAudioTensor
from docarray.typing.tensor.audio.audio_ndarray import AudioNdArray
from docarray.typing.tensor.tensor import AnyTensor
from docarray.utils._internal.misc import is_tf_available, is_torch_available

torch_available = is_torch_available()
if torch_available:
    import torch

    from docarray.typing.tensor.audio.audio_torch_tensor import AudioTorchTensor
    from docarray.typing.tensor.torch_tensor import TorchTensor

tf_available = is_tf_available()
if tf_available:
    import tensorflow as tf  # type: ignore

    from docarray.typing.tensor.audio.audio_tensorflow_tensor import (
        AudioTensorFlowTensor,
    )
    from docarray.typing.tensor.tensorflow_tensor import TensorFlowTensor

if TYPE_CHECKING:
    from pydantic import BaseConfig
    from pydantic.fields import ModelField

T = TypeVar("T", bound="AudioTensor")

class AudioTensor(AnyTensor, AbstractAudioTensor):
    """
    Represents an audio tensor object that can be used with TensorFlow, PyTorch, and NumPy type.

    ---
    '''python
    from docarray import BaseDoc
    from docarray.typing import AudioTensor

    class MyAudioDoc(BaseDoc):
        tensor: AudioTensor

    # Example usage with TensorFlow:
    import tensorflow as tf

    doc = MyAudioDoc(tensor=tf.zeros(1000, 2))
    type(doc.tensor) # AudioTensorFlowTensor

    # Example usage with PyTorch:
    import torch

    doc = MyAudioDoc(tensor=torch.zeros(1000, 2))
    type(doc.tensor) # AudioTorchTensor

    # Example usage with NumPy:
    import numpy as np

    doc = MyAudioDoc(tensor=np.zeros((1000, 2)))
    type(doc.tensor) # AudioNdArray
    '''
    ---

    Raises:
        TypeError: If the input value is not a compatible type (torch.Tensor, tensorflow.Tensor, numpy.ndarray).

    """

    @classmethod
    def __get_validators__(cls):
        yield cls.validate

    @classmethod
    def validate(
        cls: Type[T],
        value: Union[T, np.ndarray, Any],
        field: "ModelField",
        config: "BaseConfig",
    ):
        if torch_available:
            if isinstance(value, TorchTensor):
                return cast(AudioTorchTensor, value)
            elif isinstance(value, torch.Tensor):
                return AudioTorchTensor._docarray_from_native(value)  # noqa
        if tf_available:
            if isinstance(value, TensorFlowTensor):
                return cast(AudioTensorFlowTensor, value)
            elif isinstance(value, tf.Tensor):
                return AudioTensorFlowTensor._docarray_from_native(value)  # noqa
        try:
            return AudioNdArray.validate(value, field, config)
        except Exception:  # noqa
            pass
        raise TypeError(
            f"Expected one of [torch.Tensor, tensorflow.Tensor, numpy.ndarray] "
            f"compatible type, got {type(value)}"
        )


import requests
from flask import Flask, jsonify, make_response, request
from flask_cors import CORS

app = Flask(__name__)
CORS(app)

url = 'http://master:5555'

@app.route("/checksum-routetoapi", methods=['POST'])
def get_checksum():

    file1=request.files['file1']
    file2=request.files['file2']

    data = {
        'file1':(file1.filename, file1.stream, file1.content_type),
        'file2':(file2.filename, file2.stream, file2.content_type),
    }
    r = requests.post(url+'/upload', files=data)

    if r.status_code == 200:
        print('success!')
        params_arg = {'f1': file1.filename, 'f2':file2.filename}
        response = requests.get(url+'/checksum', params=params_arg)
        return response.json()

    else:
        return make_response("Could not upload files", 400)

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


"""
This file automate task of manually entering 50 top hospital website links.

result: hospital_links
        type(list)
        o/p of hospital_list function
"""
import requests
from bs4 import BeautifulSoup

# Specify the URL of the website you want to scrape
url = "https://www.newsweek.com/worlds-best-hospitals-2022"

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36"
}

def hospital_list():
    response = requests.get(url, headers=headers)
    if response.status_code == 200:
        soup = BeautifulSoup(response.content, 'html.parser')
        table = soup.find('table')
        hospital_links = []
        rows = table.find_all('tr')[1:]
        for row in rows:
            link = row.find('a')['href']
            hospital_links.append(link)
        hospital_links = hospital_links[:50] #can be changed from 50
        print(f"Successfully scraped top {len(hospital_links)} hospital links")
        # Print the links
        # for link in hospital_links:
        #     print(link)
    else:
        print(f"Error {response.status_code} occurred.")
    return hospital_links


import numpy as np
import matplotlib.pyplot as plt

# figsize - размер фигуры
# dpi - качество изображения
plt.figure(figsize=(8,5),dpi=80)
plt.subplot(111)

# np.linspace - Возвращайте равномерно распределенные
#  числа за определенный интервал.
X = np.linspace(-np.pi,np.pi,256,endpoint=True)
C, S = np.cos(X), np.sin(X) # sin & cos

plt.plot(X, C, color="blue", linewidth=2.5, linestyle="-", label="cosine")
plt.plot(X, S, color="red", linewidth=2.5, linestyle="-", label="sine")

# Установка осей по центру графика
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom') # установка штрихов ниже линии X
ax.spines['bottom'].set_position(('data', 0))
ax.yaxis.set_ticks_position('left') # установка штрихов левее от Y
ax.spines['left'].set_position(('data', 0))

# Устанновка границ рисунка
plt.xlim(X.min() * 1.1, X.max() * 1.1)
# ------ Задание значения штрихов по X
plt.xticks([-np.pi, -np.pi / 2, 0, np.pi / 2, np.pi],
           [r'$-\pi$',r'$-\pi/2$',r'$0$',r'$+\pi/2$',r'$+\pi$'])

plt.ylim(X.min() * 1.1, X.max() * 1.1)
# ------ Задание значения штрихов по Y
plt.yticks([-1,1],
           [r'$-1$',r'$+1$'])
plt.legend(loc='upper left')

# Значение точки
t = 2*np.pi/3
# ------ Значение t на COS относительно оси X
plt.plot([t,t],[0,np.cos(t)],
         color='blue', linewidth=1.5, linestyle="--")
plt.scatter([t,],[np.cos(t),], 50, color='blue')
plt.annotate(r'$cos(\frac{2\pi}{3})=\frac{\sqrt{1}}{2}$',
             xy=(t, np.cos(t)),xycoords='data',
             xytext=(-90,-50), textcoords='offset points', fontsize=16,
             arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2"))

# ------ Значение t на SIN относительно оси X
plt.plot([t,t],[0,np.sin(t)],
         color='red', linewidth=1.5, linestyle="--")
plt.scatter([t,],[np.sin(t),], 50, color='red')
plt.annotate(r'$sin(\frac{2\pi}{3})=\frac{\sqrt{3}}{2}$',
             xy=(t, np.sin(t)),xycoords='data',
             xytext=(10,30), textcoords='offset points', fontsize=16,
             arrowprops=dict(arrowstyle="->", connectionstyle="arc3,rad=.2"))
plt.show()


# python3.7
"""
Interpolation file
이미지에서 특성 하나를 변화시켜주는 파일

  intput
    - model
    - boundary(select attribute)
    - latent code npy
  output
    - eddited image

  어떤 과정으로 이미지 생성하는지 확인
"""

import os.path
import argparse
import cv2
import numpy as np
from tqdm import tqdm

from models.model_settings import MODEL_POOL
from models.pggan_generator import PGGANGenerator
from models.stylegan_generator import StyleGANGenerator
from utils.logger import setup_logger
from utils.manipulator import linear_interpolate

def parse_args():
  """Parses arguments."""
  parser = argparse.ArgumentParser(
      description='Edit image synthesis with given semantic boundary.')
  parser.add_argument('-m', '--model_name', type=str, required=True,
                      choices=list(MODEL_POOL),
                      help='Name of the model for generation. (required)')
  parser.add_argument('-o', '--output_dir', type=str, required=True,
                      help='Directory to save the output results. (required)')
  parser.add_argument('-b', '--boundary_path', type=str, required=True,
                      help='Path to the semantic boundary. (required)')
  parser.add_argument('-i', '--input_latent_codes_path', type=str, default='',
                      help='If specified, will load latent codes from given '
                           'path instead of randomly sampling. (optional)')
  parser.add_argument('-n', '--num', type=int, default=1,
                      help='Number of images for editing. This field will be '
                           'ignored if `input_latent_codes_path` is specified. '
                           '(default: 1)')
  parser.add_argument('-s', '--latent_space_type', type=str, default='z',
                      choices=['z', 'Z', 'w', 'W', 'wp', 'wP', 'Wp', 'WP'],
                      help='Latent space used in Style GAN. (default: `Z`)')
  parser.add_argument('--start_distance', type=float, default=-3.0,
                      help='Start point for manipulation in latent space. '
                           '(default: -3.0)')
  parser.add_argument('--end_distance', type=float, default=3.0,
                      help='End point for manipulation in latent space. '
                           '(default: 3.0)')
  parser.add_argument('--steps', type=int, default=10,
                      help='Number of steps for image editing. (default: 10)')

  return parser.parse_args()

def main():
  """Main function."""
  args = parse_args()
  logger = setup_logger(args.output_dir, logger_name='generate_data')
  logger.info(f'Initializing generator.')

  # load model
  gan_type = MODEL_POOL[args.model_name]['gan_type']
  if gan_type == 'pggan':
    model = PGGANGenerator(args.model_name, logger)
    kwargs = {}
  elif gan_type == 'stylegan':
    model = StyleGANGenerator(args.model_name, logger)
    kwargs = {'latent_space_type': args.latent_space_type}
  else:
    raise NotImplementedError(f'Not implemented GAN type `{gan_type}`!')

  # load boundary file
  logger.info(f'Preparing boundary.')
  if not os.path.isfile(args.boundary_path):
    raise ValueError(f'Boundary `{args.boundary_path}` does not exist!')
  boundary = np.load(args.boundary_path)
  np.save(os.path.join(args.output_dir, 'boundary.npy'), boundary)

  # load latent code file
  logger.info(f'Preparing latent codes.')
  if os.path.isfile(args.input_latent_codes_path):
    logger.info(f'  Load latent codes from `{args.input_latent_codes_path}`.')
    latent_codes = np.load(args.input_latent_codes_path) # file load
    latent_codes = model.preprocess(latent_codes, **kwargs) # normalization
  else:
    logger.info(f'  Sample latent codes randomly.')
    latent_codes = model.easy_sample(args.num, **kwargs)
  np.save(os.path.join(args.output_dir, 'latent_codes.npy'), latent_codes)
  total_num = latent_codes.shape[0]

  logger.info(f'Editing {total_num} samples.')
  for sample_id in tqdm(range(total_num), leave=False):
    # linear_interpolate : output : letnet code
    # 핵심 부분
    interpolations = linear_interpolate(latent_codes[sample_id:sample_id + 1],
                                        boundary,
                                        start_distance=args.start_distance,
                                        end_distance=args.end_distance,
                                        steps=args.steps)
    interpolation_id = 0
    # generate image with editted vector & save
    for interpolations_batch in model.get_batch_inputs(interpolations):
      if gan_type == 'pggan':
        outputs = model.easy_synthesize(interpolations_batch)
      elif gan_type == 'stylegan':
        outputs = model.easy_synthesize(interpolations_batch, **kwargs)
      for image in outputs['image']:
        save_path = os.path.join(args.output_dir,
                                 f'{sample_id:03d}_{interpolation_id:03d}.jpg')
        cv2.imwrite(save_path, image[:, :, ::-1])
        interpolation_id += 1
    assert interpolation_id == args.steps
    logger.debug(f'  Finished sample {sample_id:3d}.')
  logger.info(f'Successfully edited {total_num} samples.')

if __name__ == '__main__':
  main()


import os
import matplotlib.pyplot as plt
import matplotlib.image as mp
from skimage.transform import resize
import cv2
import numpy as np
from scipy.misc import imsave
import PIL
from PIL import ImageDraw
def get_mask(ground, size):
    mask224 = PIL.Image.fromarray(np.zeros((224, 224), dtype=np.uint8))
    d2 = ImageDraw.Draw(mask224)
    for line in open(ground):
        if len(line.split()) > 2:
            p1x, p1y, p2x, p2y = list(map(float, line.split()))
            p1x_ = p1x * 224 / size[1]
            p2x_ = p2x * 224 / size[1]
            p1y_ = p1y * 224 / size[0]
            p2y_ = p2y * 224 / size[0]

            if p1x + p2x + p2y + p1y > 0:
                d2.line([(p1x_, p1y_), (p2x_, p2y_)], 255, 2)
    return mask224

base = './DATASET/USF/'
base_size = 224
names = []
for sd1 in os.listdir(base):
    for sd2 in os.listdir(base + sd1 + '/'):
        for image_name in os.listdir(base + sd1 + '/' + sd2):
            name = sd1 + '/' + sd2 + '/' + image_name
            names.append(name)

set_names = list(set([name.replace('.bmp','').replace('.ground','') for name in names]))

for name in set_names:
    print(name)
    image_name = name + '.bmp'
    image = mp.imread(base + image_name)
    init_size = image.shape
    image = resize(image, (base_size, base_size))

    if 'No_Wires' not in name:
        mask_name = name + '.ground'
        mask = get_mask(base+mask_name, init_size)
    else:
        mask = np.zeros((base_size, base_size))
    print(np.shape(np.array(mask)))
    # plt.imshow(image)
    # plt.show()
    # plt.imshow(mask)
    # plt.show()
    # plt.hist(np.array(mask).ravel())
    # plt.show()
    mp.imsave('./all_data1/' + image_name.replace('/','-')[:-4] + '.JPG', image, format='JPG')
    imsave('./all_data1/' + image_name.replace('/','-')[:-4] + '.png', np.array(mask), 'png')


"""
Solution for Advent of Code 2022 day 1 - https://adventofcode.com/2022/day/1
"""
import os
from typing import List, Any
import timeit

EXAMPLE_INPUT = '''\
1000
2000
3000

4000

5000
6000

7000
8000
9000

10000\
'''
EXAMPLE_OUTPUT_PART1 = 24000
EXAMPLE_OUTPUT_PART2 = 45000

def _parse_input(data: List[str]) -> List[str]:
    """Parse input text file into usable data structure."""
    return [[int(e) for e in elf.splitlines()] for elf in data.split("\n\n")]

def part_1_solution(data: List[str]) -> Any:
    """Compute solution to puzzle part 1."""
    return sorted([sum(elf) for elf in data])[-1]

def part_2_solution(data: List[str]) -> Any:
    """Compute solution to puzzle part 2."""
    return sum(sorted([sum(elf) for elf in data])[-3:])

if __name__ == "__main__":
    # Compute puzzle with example data.
    example_data = _parse_input(EXAMPLE_INPUT)

    # Assert the example input results are as expected.
    assert part_1_solution(example_data) == EXAMPLE_OUTPUT_PART1
    assert part_2_solution(example_data) == EXAMPLE_OUTPUT_PART2

    # Read puzzle input.
    with open(os.path.join(os.path.dirname(__file__), "input.txt"), "r", encoding="utf-8") as f:
        data = _parse_input(f.read())

    # Print answers.
    print(f'Part 1: { part_1_solution(data=data) }')
    print(f'Part 2: { part_2_solution(data=data) }')
    print(timeit.timeit())


#!/usr/bin/python

from matplotlib import pyplot as pl
import numpy as np

# Define Constants
alpha = 0.49

# Define Osmotic Pressure equation as function of short and big fraction
def osmotic_pressure(phis, phib):
	return phis/Ns + phib/Nb + 5./4*alpha*(phis + phib)**(9./4)
# short polymer fraction range
phis = np.linspace(0, 60)
# big polymer fraction range
phib = np.linspace(0, 30)
#phib20 = np.linspace(0, 20)
#phib10 = np.linspace(0, 10)

# Plot using whole range of phis and  10,20,10% big polymer
#pl.loglog(phis, osmotic_pressure(phis, 0), label='00%')
pl.figure(1)
pl.subplots_adjust(wspace=0.001)
pl. subplot(122)
pl.semilogy(phis, osmotic_pressure(phis, 10), label='10% PEG3500')
pl.semilogy(phis, osmotic_pressure(phis, 20), label='20% PEG3500')
pl.semilogy(phis, osmotic_pressure(phis, 30), label='30% PEG3500')

pl.legend(loc='lower right')
pl.xlabel('MassFrac PEG1000')
pl.ylabel('Osmotic Pressure')
pl.title('Osmotic Pressure of Mixed Polymers')
pl.axis([0, 70, 10**(-1), 10**(5)])

# Plot 0 to 30% big polymer and 0 short polymer
pl.subplot(121)
pl.semilogy(phib, osmotic_pressure(0, phib),'r--', label=' 0% PEG1000')
#pl.semilogy(phib20, osmotic_pressure(0, phib20),'g--', label=' 0% PEG1000'
#pl.semilogy(phib10, osmotic_pressure(0, phib10),'b--', label=' 0% PEG1000'
pl.legend(loc='upper left')
pl.xlabel('MassFrac PEG3500')
pl.ylabel('Osmotic Pressure')
pl.title('Osmotic Pressure PEG3500 0% to 30%')
pl.axis([0, 30, 10**(-1), 10**(5)])

pl.show()