import pandas as pd
import argparse
import json
# Creates json file with the number of times each hot topic appears in the annotaed file

parser = argparse.ArgumentParser()
parser.add_argument('-o', '--outfile')
parser.add_argument('-i', '--coded_file', required=True)
args = parser.parse_args()

# "Hot Topics"
result = {'course-related': '0', 'food-related': '0', 'residence-related': '0', 'other': '0'}

def get_count(coded_file):
    df = pd.read_csv(coded_file, sep='\t', header=0)  # convert infile to dataframe
    count = df['coding'].value_counts()

    for category in list(result.keys()):
        letter = category[:1] # course-related -> c
        if letter in count.index:
            result[category] = int(count[letter])
    return result

def main():
    count_result = get_count(args.coded_file)
    if args.outfile:
        with open(args.outfile,'w') as outfile:
            json.dump(count_result,outfile)
    print(result)

if __name__ == '__main__':
    main()


from flask import Flask, jsonify, request
from calculation import Calc

app = Flask(__name__)

@app.route('/get_time', methods=['POST'])

def give_res():
    do = Calc()
    answer_dict = {}
    n = 1  # переменная для нумерации ответов в словаре answer_dict

    for i, test in enumerate(request.json):      # нумеруем и итерируем данные из нужной нам части запроса
        test_answer = do.appearance(test['data'])   # за расчетами обращаемся к классу в отдельном файле (calculate.py)
#        assert test_answer == test['answer'], f'Error on test case {i}, got {test_answer}, expected {test["answer"]}'
        answer_dict.setdefault(n, test_answer)
        n += 1
#    print(answer_dict)

    return jsonify(answer_dict)

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


import matplotlib.pyplot as plt
import numpy as np

def loadData(fileName):
    """加载数据:解析以tab键分隔的文件中的浮点数

    Args:
        fileName : 数据集文件

    Returns:
        dataMat :   feature 对应的数据集
        labelMat :  feature 对应的分类标签，即类别标签
    """
    # 获取样本特征的总数，不算最后的目标变量
    numFeat = len(open(fileName).readline().split('\t')) - 1
    dataMat = []
    labelMat = []
    fr = open(fileName)
    for line in fr.readlines():
        # 读取每一行
        lineArr = []
        # 删除一行中以tab分隔的数据前后的空白符号
        curLine = line.strip().split('\t')
        # i 从0到2，不包括2
        for i in range(numFeat):
            # 将数据添加到lineArr List中，每一行数据测试数据组成一个行向量
            lineArr.append(float(curLine[i]))
            # 将测试数据的输入数据部分存储到dataMat 的List中
        dataMat.append(lineArr)
        # 将每一行的最后一个数据，即类别，或者叫目标变量存储到labelMat List中
        labelMat.append(float(curLine[-1]))
    return dataMat, labelMat

# 岭回归
def ridgeRegres(xMat, yMat, lam = 0.2):
    """
    Parameters:
        xMat - x数据集
        yMat - y数据集
        lam - 缩减系数
    Returns:
        ws - 回归系数
    """
    xTx = xMat.T * xMat
    denom = xTx + np.eye(np.shape(xMat)[1]) * lam
    if np.linalg.det(denom) == 0.0:
        print("矩阵为奇异矩阵,不能转置")
        return
    ws = denom.I * (xMat.T * yMat)
    return ws

def ridgeTest(xArr, yArr):
    """岭回归测试
    Args:
        xMat - x数据集
        yMat - y数据集
    Returns:
        wMat - 回归系数矩阵
    """
    xMat = np.mat(xArr); yMat = np.mat(yArr).T
    #数据标准化
    yMean = np.mean(yMat, axis = 0)                  #行与行操作，求均值
    yMat = yMat - yMean                              #数据减去均值
    xMeans = np.mean(xMat, axis = 0)                 #行与行操作，求均值
    xVar = np.var(xMat, axis = 0)                    #行与行操作，求方差
    xMat = (xMat - xMeans) / xVar                    #数据减去均值除以方差实现标准化
    numTestPts = 30                                  #30个不同的lambda测试
    wMat = np.zeros((numTestPts, np.shape(xMat)[1])) #初始回归系数矩阵
    for i in range(numTestPts):                      #改变lambda计算回归系数
        ws = ridgeRegres(xMat, yMat, np.exp(i - 10)) #lambda以e的指数变化，最初是一个非常小的数，
        wMat[i, :] = ws.T                            #计算回归系数矩阵
    # 30x8
    return wMat

def plotwMat():
    abX, abY = loadData('data/abalone.txt')
    redgeWeights = ridgeTest(abX, abY)
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.plot(redgeWeights)
    ax_title_text = ax.set_title('The relationship between log(lambada) and regression coefficient')
    ax_xlabel_text = ax.set_xlabel('log(lambada)')
    ax_ylabel_text = ax.set_ylabel('regression coefficient')
    plt.setp(ax_title_text, size = 10, weight = 'bold', color = 'red')
    plt.setp(ax_xlabel_text, size = 10, weight = 'bold', color = 'black')
    plt.setp(ax_ylabel_text, size = 10, weight = 'bold', color = 'black')
    plt.show()

if __name__ == '__main__':
    plotwMat()


#!/usr/bin/env env/bin/python

import argparse
import mido
from timeit import default_timer as timer

PROG_NAME = 'rmidi'
PROG_DESCRIP = 'record midi data from given input to given file'
DEFAULT_INPUT = 'KeyLab mkII 61:KeyLab mkII 61 MIDI'
DEFAULT_BPM = 120
SECONDS_PER_MIN = 60

def get_cmd_args():
    parser = argparse.ArgumentParser(prog=PROG_NAME, description=PROG_DESCRIP)
    parser.add_argument('-b', '--bpm', default=DEFAULT_BPM)
    parser.add_argument('-i', '--input')
    parser.add_argument('-f', '--file')
    parser.add_argument('-s', '--silence', action='store_true')
    parser.add_argument('-v', '--verbose', action='store_true')
    return parser.parse_args()

def get_midi_in():
    inputs = mido.get_input_names()
    default_i = 0
    entry = -1

    while(entry < 0 or entry >= len(inputs)):
        for i, name in enumerate(inputs):
            if DEFAULT_INPUT in name:
                default_i = i
            print('%d: %s' % (i, name))

        raw_entry = input('Input (default: %d): ' % default_i)

        try:
            entry = int(raw_entry)
        except ValueError:
            entry = default_i

    return inputs[entry]

def get_appending_handler(appendable, ticks_per_second, print_msgs):
    def full(x):
        appendable.append(x)
        print(x)

    def append_only(x):
        appendable.append(x)

    if print_msgs:
        return full

    return append_only

def print_input(midi_in):
    process_msgs(lambda x: print(x))

def accumulate_input(midi_in, appendable, ticks_per_second, print_msgs=True):
    handler = get_appending_handler(appendable, ticks_per_second, print_msgs)
    process_msgs(handler, ticks_per_second=ticks_per_second)

def process_msgs(handler, ticks_per_second=0):
    last_time = timer()

    try:
        with mido.open_input(midi_in) as inport:
            for msg in inport:

                end = timer()
                seconds = end - last_time
                last_time = end
                msg.time = round(ticks_per_second * seconds)

                handler(msg)
    except KeyboardInterrupt:
        if print_msgs:
            print() # Just so the "^C" ends up on a separate line from prompt on exit
        pass

def record_to_file(midi_in, filename, bpm, print_msgs, trim_init_silence):
    mid = mido.MidiFile(type=0)
    track = mido.MidiTrack()
    mid.tracks.append(track)

    ticks_per_second = bpm * mid.ticks_per_beat / SECONDS_PER_MIN

    accumulate_input(midi_in, track, ticks_per_second, print_msgs)

    if trim_init_silence:
        track[0].time = 0

    mid.save(filename)

    print('MIDI data written to', filename)

if __name__ == '__main__':
    args = get_cmd_args()
    midi_in = args.input
    filename = args.file
    bpm = args.bpm
    print_msgs = args.verbose
    trim_init_silence = not args.silence

    if not midi_in:
        midi_in = get_midi_in()

    print('Receiving from "%s"' % midi_in)

    if filename:
        record_to_file(midi_in, filename, bpm, print_msgs, trim_init_silence)
    elif print_msgs:
        print_input(midi_in)


import triton
import triton.language as tl

def conv2d_forward_config(
    BLOCK_SIZE_BATCH_HEIGHT_WIDTH: int,
    BLOCK_SIZE_IN_FEAT: int,
    BLOCK_SIZE_OUT_FEAT: int,
    n_warps: int = 4,
    n_stages: int = 2,
    ) -> triton.Config:
    """
    Creates a triton.Config object for conv2d_forward_kernel
    given meta-parameters for auto-tuning.

    Args:
        BLOCK_SIZE_BATCH_HEIGHT_WIDTH: Block size across the batch, height, and
            width dimensions.
        BLOCK_SIZE_IN_FEAT: Block size across the input feature dimension.
        BLOCK_SIZE_OUT_FEAT: Block size across the output feature dimension.
        n_warps: Number of warps to use for the kernel when compiled for GPUs.
        n_stages: Number of stages the compiler uses to software-pipeline.

    Returns:
        Kernel configuration.
    """
    return triton.Config({'BLOCK_SIZE_BATCH_HEIGHT_WIDTH': BLOCK_SIZE_BATCH_HEIGHT_WIDTH,
                          'BLOCK_SIZE_IN_FEAT': BLOCK_SIZE_IN_FEAT,
                          'BLOCK_SIZE_OUT_FEAT': BLOCK_SIZE_OUT_FEAT},
                          num_warps=n_warps, num_stages=n_stages)

@triton.autotune(
    configs=[
        conv2d_forward_config(128, 32, 128, n_warps=8, n_stages=2),
        conv2d_forward_config(256, 32, 64, n_warps=8, n_stages=2),
        conv2d_forward_config(256, 32, 32, n_warps=4, n_stages=4),
        conv2d_forward_config(256, 64, 32, n_warps=4, n_stages=4),
        conv2d_forward_config(256, 32, 16, n_warps=2, n_stages=4),
        conv2d_forward_config(64, 32, 128, n_warps=8, n_stages=4),
        conv2d_forward_config(128, 32, 64, n_warps=4, n_stages=4),
        conv2d_forward_config(64, 32, 64, n_warps=4, n_stages=4),
        conv2d_forward_config(128, 32, 16, n_warps=4, n_stages=4),
        conv2d_forward_config(128, 128, 128, n_warps=8, n_stages=3),
        conv2d_forward_config(256, 128, 64, n_warps=8, n_stages=3),
        conv2d_forward_config(256, 128, 32, n_warps=4, n_stages=4),
        conv2d_forward_config(64, 128, 128, n_warps=4, n_stages=4),
        conv2d_forward_config(128, 128, 64, n_warps=4, n_stages=4),
        conv2d_forward_config(128, 64, 32, n_warps=2, n_stages=4),
        conv2d_forward_config(64, 64, 64, n_warps=2, n_stages=4),
    ],
    key=['batch_dim', 'in_feat_dim', 'in_height', 'in_width',
         'out_feat_dim', 'out_height', 'out_width',
         'kernel_height', 'kernel_width',
         'stride_height', 'stride_width',
         'padding_height', 'padding_width',
         'groups'],
)
@triton.jit
def conv2d_forward_kernel(
    input_pointer, weight_pointer, output_pointer,
    batch_dim, in_feat_dim, in_height, in_width,
    out_feat_dim, out_height, out_width,
    input_batch_stride, input_in_feat_stride, input_height_stride, input_width_stride,
    weight_out_feat_stride, weight_in_feat_stride, weight_height_stride, weight_width_stride,
    output_batch_stride, output_out_feat_stride, output_height_stride, output_width_stride,
    kernel_height: tl.constexpr, kernel_width: tl.constexpr,
    stride_height: tl.constexpr, stride_width: tl.constexpr,
    padding_height: tl.constexpr, padding_width: tl.constexpr,
    groups: tl.constexpr,
    BLOCK_SIZE_BATCH_HEIGHT_WIDTH: tl.constexpr, BLOCK_SIZE_IN_FEAT: tl.constexpr,
    BLOCK_SIZE_OUT_FEAT: tl.constexpr,
    ):
    """
    2D-convolves over the input using weights.

    Args:
        input_pointer: Pointer to the input to convolve over.
            The input must be of shape [batch_dim, in_feat_dim, in_height, in_width].
        weight_pointer: Pointer to the weights input is convolved over by.
            The weights must be of shape [out_feat_dim, in_feat_dim, kernel_height, kernel_width].
        output_pointer: Pointer to a container the result is written to.
            The container must be of shape [batch_dim, out_feat_dim, out_height, out_width].
        batch_dim: Batch dimension of the input and output.
        in_feat_dim: Dimensionality of the input features.
        in_height: Input height.
        in_width: Input width.
        out_feat_dim: Dimensionality of the output features.
        out_height: Output height.
        out_width: Output width.
        input_batch_stride: Stride necessary to jump one element along the
            input's batch dimension.
        input_in_feat_stride: Stride necessary to jump one element along the
            input's feature dimension.
        input_height_stride: Stride necessary to jump one element along the
            input's height dimension.
        input_width_stride: Stride necessary to jump one element along the
            input's width dimension.
        weight_out_feat_stride: Stride necessary to jump one element along the
            weights' output feature dimension.
        weight_in_feat_stride: Stride necessary to jump one element along the
            weights' input feature dimension.
        weight_height_stride: Stride necessary to jump one element along the
            weights' height dimension.
        weight_width_stride: Stride necessary to jump one element along the
            weights' width dimension.
        output_batch_stride: Stride necessary to jump one element along the
            output's batch dimension.
        output_out_feat_stride: Stride necessary to jump one element along the
            output's feature dimension.
        output_height_stride: Stride necessary to jump one element along the
            output's height dimension.
        output_width_stride: Stride necessary to jump one element along the
            output's width dimension.
        kernel_height: Kernel height.
        kernel_width: Kernel width.
        stride_height: Stride of kernel across the height dimension.
        stride_width: Stride of kernel across the width dimension.
        padding_height: Padding applied to the input across the height dimension.
        padding_width: Padding applied to the input across the width dimension.
        groups: Number of groups for the convolution.
        BLOCK_SIZE_BATCH_HEIGHT_WIDTH: Block size across the batch, height, and
            width dimensions.
        BLOCK_SIZE_IN_FEAT: Block size across the input feature dimension.
        BLOCK_SIZE_OUT_FEAT: Block size across the output feature dimension.
    """
    batch_height_width_pid = tl.program_id(0)
    out_feat_pid = tl.program_id(1)
    group_pid = tl.program_id(2)

    in_group_dim = in_feat_dim // groups
    out_group_dim = out_feat_dim // groups

    batch_height_width_offset = (batch_height_width_pid * BLOCK_SIZE_BATCH_HEIGHT_WIDTH +
                                 tl.arange(0, BLOCK_SIZE_BATCH_HEIGHT_WIDTH))
    batch_height_offset = batch_height_width_offset // out_width
    batch_offset = batch_height_offset // out_height

    output_feat_offset = (out_feat_pid * BLOCK_SIZE_OUT_FEAT +
                          tl.arange(0, BLOCK_SIZE_OUT_FEAT))
    output_height_offset = batch_height_offset % out_height
    output_width_offset = batch_height_width_offset % out_width

    input_pointer += (input_batch_stride * batch_offset +
                      input_in_feat_stride * group_pid * in_group_dim)[:, None]
    weight_pointer += (weight_out_feat_stride * output_feat_offset +
                       weight_out_feat_stride * group_pid * out_group_dim)[None, :]

    accum = tl.zeros((BLOCK_SIZE_BATCH_HEIGHT_WIDTH, BLOCK_SIZE_OUT_FEAT),
                     dtype=tl.float32)

    for h in range(kernel_height):
        for w in range(kernel_width):
            for c in range(0, in_group_dim, BLOCK_SIZE_IN_FEAT):
                input_feat_offset = c + tl.arange(0, BLOCK_SIZE_IN_FEAT)
                input_height_offset = (h - padding_height +
                                       stride_height * output_height_offset)
                input_width_offset = (w - padding_width +
                                      stride_width * output_width_offset)

                curr_input_pointer = (input_pointer +
                                     (input_in_feat_stride * input_feat_offset)[None, :] +
                                     (input_height_stride * input_height_offset)[:, None] +
                                     (input_width_stride * input_width_offset)[:, None])
                curr_weight_pointer = (weight_pointer +
                                      (weight_in_feat_stride * input_feat_offset)[:, None] +
                                      (weight_height_stride * h) +
                                      (weight_width_stride * w))

                input_mask = ((batch_offset < batch_dim)[:, None] &
                              (input_feat_offset < in_group_dim)[None, :] &
                              (0 <= input_height_offset)[:, None] &
                              (input_height_offset < in_height)[:, None] &
                              (0 <= input_width_offset)[:, None] &
                              (input_width_offset < in_width)[:, None])
                weight_mask = ((input_feat_offset < in_group_dim)[:, None] &
                               (output_feat_offset < out_group_dim)[None, :])

                input_block = tl.load(curr_input_pointer, mask=input_mask)
                weight_block = tl.load(curr_weight_pointer, mask=weight_mask)

                accum += tl.dot(input_block, weight_block)

    output_pointer += ((output_batch_stride * batch_offset)[:, None] +
                       (output_out_feat_stride * (group_pid * out_group_dim + output_feat_offset))[None, :] +
                       (output_height_stride * output_height_offset)[:, None] +
                       (output_width_stride * output_width_offset)[:, None])
    output_mask = ((batch_offset < batch_dim)[:, None] &
                   (output_feat_offset < out_group_dim)[None, :] &
                   (output_height_offset < out_height)[:, None] &
                   (output_width_offset < out_width)[:, None])

    tl.store(output_pointer, accum, mask=output_mask)


import os
import h5py
import torch
import numpy as np
import pandas as pd

from sklearn.model_selection import train_test_split

from collections import Counter

def write(data, gts, outfile):
    '''
        This function writes the pre-processed image data to a HDF5 file
        Args:
          data: numpy.array, image data as numpy array
          outfile: string, path to write file to
    '''
    print("---------------------------------------")
    print("Saving data")
    print("---------------------------------------\n")
    with h5py.File(outfile, "w") as f:
        f.create_dataset("features", data=data, dtype=data.dtype)
        f.create_dataset("gts", data=gts, dtype=gts.dtype)

def load_data(config):
    all_data   = config['DATA_PATH']

    dfs = []
    for i in range(7):
        paths = [all_data + '/feeding/csv/Feeding_25Hz_',
                 all_data + '/swimming/csv/Swimming_25Hz_',
                 all_data + '/resting/csv/Resting_25Hz_',
                 all_data + '/ndm/csv/NDM_25Hz_']

        df = pd.concat((pd.read_csv(path + str(i+1) + '.csv',
                                    index_col=['Date_Time'],
                                    parse_dates=['Date_Time'],
                                    infer_datetime_format=True) for path in paths), ignore_index=False, sort=False).iloc[:, 1:9]

        df = df.replace(to_replace={"Non directed motion": "NDM"})
        dfs.append(df)

    return dfs

def split_data(dfs, config):
    split_type = config['SPLIT']

    df_dict = {}
    if split_type == 'experiment':
        # Train: 1, 2, 3, 4, 7
        # Val: 6
        # Test: 5
        df_dict['train'] = pd.concat([dfs[0], dfs[1], dfs[2], dfs[3], dfs[6]])
        df_dict['val'] = dfs[5]
        df_dict['test'] = dfs[4]

    elif split_type == 'full':
        data = pd.concat(dfs)

        df_dict['train'], df_dict['test'] = train_test_split(data,
                                                             test_size=0.25,
                                                             random_state=33)

        df_dict['train'], df_dict['val'] = train_test_split(df_dict['train'],
                                                            test_size=0.2,
                                                            random_state=33)

        df_dict['train'] = df_dict['train'].sort_index()
        df_dict['val'] = df_dict['val'].sort_index()
        df_dict['test'] = df_dict['test'].sort_index()

    else:
        print("Not a valid split type (full/experiment)")
        exit()

    print("*"*44)
    print('Train shape:', df_dict['train'].shape)
    print('Val   shape:', df_dict['val'].shape)
    print('Test  shape:', df_dict['test'].shape)
    print("="*44)
    print()

    return df_dict

def normalize(df_dict, features):
    for column in features:
        mean = np.mean(df_dict['train'][column])
        std  = np.std(df_dict['train'][column])

        df_dict['train'][column] = df_dict['train'][column].map(lambda x: (x-mean)/std)
        df_dict['val'][column]   = df_dict['val'][column].map(lambda x: (x-mean)/std)
        df_dict['test'][column]  = df_dict['test'][column].map(lambda x: (x-mean)/std)

    return df_dict

def group_times(df):
    time_diff = df.index.to_series().diff()
    breaks = time_diff > pd.Timedelta('1s')
    groups = breaks.cumsum()

    df['Group'] = groups

    return df

def sample_sequences(df, features, num_samples=None, seq_len=50, dims=1, test=False):
    X = []
    Y = []

    label_list = ['Feeding', 'Swimming', 'Resting', 'NDM']

    for idx, label in enumerate(label_list):
        print(str(idx) + ": " + label)

        class_df = df.loc[df['Label'] == label]
        groups = class_df['Group'].unique()
        if not test:
            X_class = np.zeros((num_samples, seq_len, dims), dtype=np.float32)
            Y_class = np.full((num_samples, 1), idx, dtype=np.int64)

            for i in range(num_samples):
                chunk_idx = groups[np.random.randint(len(groups))]

                data = class_df.loc[class_df['Group'] == chunk_idx][features].to_numpy()

                while(len(data) <= seq_len):
                    chunk_idx = groups[np.random.randint(len(groups))]

                    data = class_df.loc[class_df['Group'] == chunk_idx][features].to_numpy()

                rand = np.random.randint(len(data)-seq_len)

                X_class[i] = data[rand:rand+seq_len]

        else:
            X_class = []
            Y_class = []
            for group in groups:
                data = class_df.loc[class_df['Group'] == group][features].to_numpy()

                num_samples = len(data)//50

                X_group = np.zeros((num_samples, seq_len, dims), dtype=np.float32)
                Y_group = np.full((num_samples, 1), idx, dtype=np.int64)

                for i in range(num_samples):
                    X_group[i] = data[seq_len*i:seq_len*(i+1)]

                X_class.append(X_group)
                Y_class.append(Y_group)

            X_class, Y_class = np.concatenate(X_class), np.concatenate(Y_class)

            print(label + " -- num test points: " + str(Y_class.shape[0]))

        X.append(X_class)
        Y.append(Y_class)

    return np.concatenate(X), np.concatenate(Y)


from django.http import JsonResponse
from jam.spotify_api import url_argument_parse
import pitchfork
import re
import urllib

try:
    import urllib.request as urllib2
except ImportError:
    import urllib2

#######################
# PITCHFORK API WRAPPER
#######################
def search(request, artist, album):
    review = pitchfork.search(url_argument_parse(artist), url_argument_parse(album))
    review_dictionary = {"artist": review.artist(), "album": review.album(), \
        "editorial": repair_editorial(review.editorial()), "label": review.label(), "score": review.score()}
    return JsonResponse(review_dictionary)

def repair_editorial(editorial):
    print(editorial)
    regex = re.compile(r'.[A-Z]')
    repaired_editorial = ""
    for token in editorial.split():
        if regex.search(token) is not None:
            repaired_editorial += token.replace(".", ".\n ")
            repaired_editorial += " "
        else:
            repaired_editorial += token
            repaired_editorial += " "
    print(repaired_editorial)
    return repaired_editorial


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

import sys
from nltk.tag import StanfordNERTagger
from nltk.tokenize import word_tokenize
from sklearn.metrics import precision_recall_fscore_support as score

def computeStatistics(expected_tags, generated_tags):
	unique_tags = list()
	floatFormat = "{:.2f}"

	for tag in generated_tags:
		if tag not in unique_tags:
			unique_tags.append(tag)

	precision, recall, fscore, support = score(expected_tags, generated_tags)

	fw = open("precision_recall_fscore.txt", "w")
	fw.write('{:20} {:10} {:10} {:10}'.format("Tag", "Precision", "Recall", "F-Measure"))
	fw.write("\n")

	for (t, p, r, f) in zip(unique_tags, precision, recall, fscore):
		fw.write('{:20} {:10} {:10} {:10}'.format(str(t), str(floatFormat.format(p)), str(floatFormat.format(r)), str(floatFormat.format(f))))
		fw.write("\n")
	fw.close()

def getTags(filename):
	tag_sequence = list()
	fr = open(filename, 'r')
	for line in fr:
		tokenized_line = word_tokenize(line)
		for token in tokenized_line:
			if '_' in token:
				tag_sequence.append(token.split('_')[1])
			else:
				tag_sequence.append('O')
	fr.close()
	return tag_sequence

def getOutputAsString(classified_text):
	output_str = ""
	for token in classified_text:
		if token[1] == 'O':
			output_str += token[0] + " "
		else:
			output_str += token[0] + "_" + token[1] + " "
	return output_str

def main():
	if len(sys.argv) < 3 or len(sys.argv) > 3:
		print("Usage: python3 111508041_Assign5-Code.py <pretrained_file> <test_file>")
		exit(1)

	expected_tags = getTags(sys.argv[1])
	print(expected_tags)
	try:
		st = StanfordNERTagger('/home/dell/Practicals/NLP/111508041_Assign5/stanford-ner-2018-02-27/classifiers/english.muc.7class.distsim.crf.ser.gz', '/home/dell/Practicals/NLP/111508041_Assign5/stanford-ner-2018-02-27/stanford-ner.jar', encoding="utf-8")
	except LookUpError:
		print("Please change the path to the jar file")
		exit(1)

	fr = open(sys.argv[2], 'r')
	fw = open('NER_labelled_Corpus_111508041.txt', 'w')

	for lines in fr:
		tokenized_text = word_tokenize(lines)
		classified_text = st.tag(tokenized_text)
		output_str = getOutputAsString(classified_text)
		fw.write(output_str)
		fw.write('\n')
		fw.flush()
	fw.close()
	fr.close()

	generated_tags = getTags('output.txt')
	print(generated_tags)
	computeStatistics(expected_tags, generated_tags)

if __name__ == '__main__':
	main()


from .models import BookModel, AuthorModel
from django import forms
import re
from django.utils.translation import gettext_lazy as _
from django.core.exceptions import ValidationError
import datetime

class BookCreateForm(forms.Form):
    authors = forms.CharField(max_length=100, required=True, widget=forms.TextInput(attrs={
        'id': 'authors_field',
        'class': 'input-book',
        'placeholder': 'Введите название авторов через запятую'
    }))
    name = forms.CharField(max_length=100, required=True, widget=forms.TextInput(attrs={
        'class': 'input-book',
        'placeholder': 'Введите название книги'
    }))
    year = forms.IntegerField(max_value=datetime.datetime.now().year, required=False, widget=forms.NumberInput(attrs={
        'class': 'input-book',
        'placeholder': 'Введите год издания книги'
    }))
    language = forms.CharField(max_length=30, required=False, widget=forms.TextInput(attrs={
        'class': 'input-book',
        'placeholder': 'Введите язык книги'
    }))
    description = forms.CharField(max_length=400, required=False, widget=forms.Textarea(attrs={
        'class': 'input-book-description-textarea',
        'placeholder': 'Введите описание книги'
    }))

    image = forms.ImageField(required=False, widget=forms.FileInput(attrs={
        'class': 'file-input',
        'placeholder': 'Описание...',
        'name': "file",
        'id': "choose-file-container",
        'oninvalid': "this.setCustomValidity('Enter User Name Here')",
        'oninput': "this.setCustomValidity('')"
    }))

    def clean_authors(self):
        authors = self.cleaned_data['authors']

        if re.search(r'[^а-яА-Яa-zA-Z-,.\' ]', authors):
            raise forms.ValidationError(
                "Имя автора может состоять из букв русского и английского алфавита, символов -,.' ")

        return authors

    def clean_name(self):
        name = self.cleaned_data['name']

        if re.search(r'[^а-яА-Яa-zA-Z-,1234567890.+!?#$%()/@\' ]', name):
            raise forms.ValidationError(
                "Название книги может состоять из букв русского и английского алфавита, цифр, символов -,.'+!?#$%()/@")

        return name

    def clean_description(self):
        description = self.cleaned_data['description']

        if re.search(r'[^а-яА-Яa-zA-Z-,.()%$#@!&*?+=/;:"0123456789\' ]', description):
            raise forms.ValidationError(
                "Описание может состоять из букв русского и английского алфавита, цифр, символов -,.'()%$#@!&*?+=/;:")

        return description

    def clean_language(self):
        language = self.cleaned_data['language']

        if re.search(r'[^а-яА-Яa-zA-Z-,. ]', language):
            raise forms.ValidationError(
                "Язык может состоять из букв русского и английского алфавита, -,.")

        return language

    def clean_year(self):
        year = self.cleaned_data['year']

        if year:
            cur_year = datetime.datetime.now().year
            if re.search(r'\d\d\d\d\d', str(year)):
                raise forms.ValidationError(
                    "Год может состоять только из цифр (максимум 4 цифры)!")

            if year > cur_year or year <= 0:
                raise forms.ValidationError(
                    "Год не может быть больше текущего и меньше 0")

        return year


import subprocess
import warnings
import sys

import numpy as np

from datetime import datetime

#---------------------------------------------------------------------------------------#
#		Dics and Colors
#---------------------------------------------------------------------------------------#

Dic_Keys 	= {0:'energy',1:'BE2',2:'ME2',3:'rho2E0'}

class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'

#---------------------------------------------------------------------------------------#
#		Read input file
#---------------------------------------------------------------------------------------#

def read_input(self):
	'''Read and parse cbsmodel input file'''

	out_cbs_energies 	= []
	out_cbs_BE2 		= []
	out_cbs_ME2 		= []
	out_cbs_rho2E0 		= []

	cbs_data_file 		= open('%s%s'% (self.input_path,self.input_file))
	lines_cbs_data_file 	= list(cbs_data_file.readlines())

	for num_line,line in enumerate(lines_cbs_data_file):

		if len(line) > 1:
			elements_line = line.split()

			if elements_line[0] == 'A':
				if self.A != int(elements_line[1]):
					warnings.warn('Mass numbers do not coincide! Using A = %i'% self.A,UserWarning)

			elif elements_line[0] == 'Z':
				if self.Z != int(elements_line[1]):
					warnings.warn('Charge numbers do not coincide! Using Z = %i'% self.Z,UserWarning)

			elif elements_line[0] == 'fit':
				if len(elements_line) < 3:
					raise ValueError('cbsmodel fit command should specify the data file\n \
						and at least one fit parameter!')

				self.name_fit_params 	= elements_line[2:]

				splitted_cbs_file 	= elements_line[1].split('/')
				self.cbs_file 		= splitted_cbs_file[-1]

				if len(splitted_cbs_file) == 1:
					self.cbs_path  	= ''
				else:
					self.cbs_path  	= '/'.join(elements_line[1].split('/')[:-1])+'/'

			elif elements_line[0] == 'energy':
				if len(elements_line) != 3:
					raise ValueError('cbsmodel input for energy not correct in %s!\n \
						Must be `energy L s` not `%s`!'% (self.cbs_file,' '.join(elements_line)))

				out_cbs_energies.append([int(elements_line[1]),int(elements_line[2])])

			elif elements_line[0] == 'BE2':
				if int(elements_line[1]) == 0 and int(elements_line[3]) == 0:
					warnings.warn('No E2 gamma transition between two states with J=0 possible.\n \
						Ignoring the transition %s.'% ' '.join(elements_line),UserWarning)
				else:
					out_cbs_BE2.append([int(elements_line[1]),int(elements_line[2]),
							int(elements_line[3]),int(elements_line[4])])

			elif elements_line[0] == 'ME2':
				if int(elements_line[1]) == 0 and int(elements_line[3]) == 0:
					warnings.warn('No E2 gamma transition between two states with J=0 possible.\n \
						Ignoring the transition %s.'% ' '.join(elements_line),UserWarning)
				else:
					out_cbs_ME2.append([int(elements_line[1]),int(elements_line[2]),
							int(elements_line[3]),int(elements_line[4])])

			elif elements_line[0] == 'rho2E0':
				out_cbs_rho2E0.append([int(elements_line[1]),int(elements_line[2]),
							int(elements_line[3]),int(elements_line[4])])

	return np.array(out_cbs_energies),np.array(out_cbs_BE2),np.array(out_cbs_ME2),np.array(out_cbs_rho2E0)

#---------------------------------------------------------------------------------------#
#		Fit CBS to input data
#---------------------------------------------------------------------------------------#

def cbs_fit_data(self,*args):
	'''Fit CBS to data as indicated in cbsmodel input file'''

	run_string  = 'A %i Z %i '% (self.A,self.Z)
	run_string += 'Wu '

	for arg in args:
		run_string += '%s '% arg

	run_string += 'fit %s%s %s '% (self.cbs_path,self.cbs_file,' '.join(self.name_fit_params))
	run_string += 'exit'

	output_cbs = subprocess.run('cbsmodel %s'% run_string,shell=True,capture_output=True).stdout

	return output_cbs

#---------------------------------------------------------------------------------------#
#		Extract CBS parameters
#---------------------------------------------------------------------------------------#

def extract_params(self):
	'''Extract structural parameters (r_beta etc.) from cbsmodel output'''

	#Perform fits to data with different r_beta until solution is found
	for r_beta in np.arange(0.1,1,0.2):

		output_cbs = cbs_fit_data(self,'rb %s'% r_beta)

		if b'Fit successful' in output_cbs:
			self.cbs_fit_success = True
			if self.verbose:
				print('Fit successful!')
			break

		if self.verbose:
			print('Fit with starting value r_beta = %.2f not successful. Continuing...'% r_beta)

	else:
		sys.exit(f'{bcolors.FAIL}Error: Fits in cbsmodel did not converge.{bcolors.ENDC}')

	#Split output by linebreaks
	output_cbs 	= output_cbs.split(b'\n')

	#extract reduced chisquare and fit parameters
	self.red_chi 	= float(output_cbs[16].split(b':')[1])
	self.fit_params = np.zeros((2*len(self.name_fit_params)))

	for i in range(len(self.name_fit_params)):

		string_param = output_cbs[7+i].split(b':')[1].split(b'+-')

		self.fit_params[2*i] 	= float(string_param[0])
		self.fit_params[2*i+1] 	= float(string_param[1])

	return

#---------------------------------------------------------------------------------------#
#		Calculate quantities of interest
#---------------------------------------------------------------------------------------#

def calculate_cbs_quantities(self,in_list,in_keyword):
	'''Use obtained structural parameters to calculate CBS predictions for quantities specified in input file'''

	run_string  = 'A %i Z %i '% (self.A,self.Z)
	run_string += 'Wu simpleoutput '

	for num_param,param in enumerate(self.name_fit_params):
		run_string += '%s %.5f '% (param,self.fit_params[2*num_param])

	for num_quantity,quantity in enumerate(in_list):
		if in_keyword == 'energy':
			run_string += '%s %i %i '% (in_keyword,in_list[num_quantity,0],in_list[num_quantity,1])
		elif in_keyword in ['BE2','ME2','rho2E0']:
			run_string += '%s %i %i %i %i '% (in_keyword,in_list[num_quantity,0],in_list[num_quantity,1],
								in_list[num_quantity,2],in_list[num_quantity,3])
		else:
			warning.warn('keyword %s not known in cbsmodel!'% in_keyword,UserWarning)

	run_string += 'exit'

	output_cbs = subprocess.run('cbsmodel %s'% run_string,shell=True,capture_output=True).stdout

	return output_cbs

#---------------------------------------------------------------------------------------#
#		Extract calculated CBS quantities
#---------------------------------------------------------------------------------------#

def extract_cbs_quantities(self,in_output_cbs):
	'''Extract specified qauntities from cbsmodel output'''

	in_output_cbs 	= in_output_cbs.split()

	out_quantities 	= np.array([float(i) for i in in_output_cbs[2:]])

	return out_quantities

#---------------------------------------------------------------------------------------#
#		Write results to output file
#---------------------------------------------------------------------------------------#

def write_output(self):
	'''Write essential output of cbsmodel to file'''

	out_string  = '#############################\n'
	out_string += '#      Results CBSplot      #\n'
	out_string += '#           %i%s           #\n'% (self.A,self.nucl_name)
	out_string += '#    %s    #\n'% datetime.now().strftime('%d-%m-%Y %H:%M:%S')
	out_string += '#############################\n'
	out_string += '\n'

	#CBS parameters
	for num_param,param in enumerate(self.name_fit_params):
		out_string += '%s\t%.5f +/- %.5f\n'% (param,self.fit_params[2*num_param],self.fit_params[2*num_param+1])

	out_string += '\n'

	#extracted quantities
	for num_quantity,quantity in enumerate([self.cbs_energies,self.cbs_BE2,self.cbs_ME2,self.cbs_rho2E0]):
		if isinstance(quantity,np.ndarray):
			for element in quantity:
				#print(element)
				if num_quantity == 0:
					out_string += '%s %i %i %.2f\n'% (Dic_Keys[num_quantity],element[0],
										element[1],element[2])
				else:
					out_string += '%s %i %i %i %i %.2f\n'% (Dic_Keys[num_quantity],element[0],
										element[1],element[2],element[3],element[4])
			out_string += '\n'

	return out_string

#---------------------------------------------------------------------------------------#
#		Main CBS calculations
#---------------------------------------------------------------------------------------#

def main_cbs_calculations(self):
	'''Perform complete CBS calculation as specified in cbsmodel input file'''

	#extract quantities to be calculated
	cbs_energies,cbs_BE2,cbs_ME2,cbs_rho2E0 	= read_input(self)

	#perform CBS fit to data and extract parameters (r_beta, etc.)
	extract_params(self)

	for num_quantity,quantity in enumerate([cbs_energies,cbs_BE2,cbs_ME2,cbs_rho2E0]):
		if quantity != []:
			output_cbs_quantities = calculate_cbs_quantities(self,quantity,Dic_Keys[num_quantity])

			if num_quantity == 0:
				self.cbs_energies 	= np.zeros((len(cbs_energies),3))
				self.cbs_energies[:,:2]	= cbs_energies
				self.cbs_energies[:,2]	= extract_cbs_quantities(self,output_cbs_quantities)
			elif num_quantity == 1:
				self.cbs_BE2 		= np.zeros((len(cbs_BE2),5))
				self.cbs_BE2[:,:4]	= cbs_BE2
				self.cbs_BE2[:,4]	= extract_cbs_quantities(self,output_cbs_quantities)
			elif num_quantity == 2:
				self.cbs_ME2 		= np.zeros((len(cbs_ME2),5))
				self.cbs_ME2[:,:4]	= cbs_ME2
				self.cbs_ME2[:,4]	= extract_cbs_quantities(self,output_cbs_quantities)
			elif num_quantity == 3:
				self.cbs_rho2E0 	= np.zeros((len(cbs_rho2E0),5))
				self.cbs_rho2E0[:,:4]	= cbs_rho2E0
				self.cbs_rho2E0[:,4]	= extract_cbs_quantities(self,output_cbs_quantities)

	#create output and save/print it if requested
	output = write_output(self)

	if self.write_output:
		out_file = open('%sresults_%i%s.txt'% (self.out_path,self.A,self.nucl_name),'w')
		out_file.write(output)
		out_file.close()

	if self.verbose:
		print(output)

	return


'''
@autor Simone Lima
'''
import pandas as pd
from .files import AppFilesPath
from domain.app.models import VersionModel, ReleaseNoteModel, ReleaseNoteType
from domain.utils.check_value_utils import CheckValuesUtils
from domain.utils.file_utils import CSVColumnsName, CSV_EXTENSION
from django.db import transaction

class VersionService:
    def install(self):
        installed_versions = VersionModel.objects.find_all()
        versions:dict = {}
        df = pd.read_csv(AppFilesPath.VERSIONS)
        for index, row in df.iterrows():
            version_instance = self.__create_version_instance(row)
            if self.__is_installed(version_instance, installed_versions):
                print('Ignorando  a versao: {} porque ja foi instalada! '.format(version_instance.version))
            else:
                print('Preparando  a versao: {} {}'.format(version_instance.version, version_instance.name))
                release_notes = self.__create_release_notes_instances(version_instance)
                versions[version_instance.version] = (version_instance,release_notes)

        self.__create_version_with_related_items(versions)

    # Cria instancia da versao apartir do ficheiro
    def __create_version_instance(self, row):
        version_name = row[CSVColumnsName.NAME.value]
        version_number = row[CSVColumnsName.NUMBER.value]
        CheckValuesUtils.non_empties([version_name,version_number])
        return VersionModel.factory(version_name,version_number)

    # Cria instancias de release notes da versao apartir do ficheiro
    def __create_release_notes_instances(self, version_instance, ):
        rn_df = pd.read_csv('{}{}{}'.format(AppFilesPath.REALESE_NOTES,version_instance.version,CSV_EXTENSION))
        release_notes = []
        for rn_index, rn_row in rn_df.iterrows():
            description = rn_row[CSVColumnsName.DESCRIPTION.value]
            note_type = rn_row[CSVColumnsName.TYPE.value]
            CheckValuesUtils.non_empties([description,note_type])
            ReleaseNoteType.validate(note_type)
            release_notes.append(ReleaseNoteModel.factory(description,note_type))
        return release_notes

    def __is_installed(self,version_instance:VersionModel, installed_versions:list[VersionModel]):
        if not installed_versions:
            return False

        for instaled_version in installed_versions:
            if float(instaled_version.version) == float(version_instance.version):
                return True
            return False

    @transaction.atomic
    def __create_version_with_related_items(self, versions:dict):
        if versions:
            for key, values in versions.items():
                version_instance = values[0]
                print('Instalando  a versao: {} {}'.format(version_instance.version, version_instance.name))
                #print(key," : ",values)

                version_instance.save()
                version_instance.release_notes.set(values[1],bulk=False )
                #print(values[0])
                #print(values[1])
                print('Versao: {} {} instalada com sucesso!'.format(version_instance.version, version_instance.name))


"""
datasets.py
"""
import numpy as np
import pandas as pd
from collections import namedtuple
from gensim.corpora import Dictionary

# Load two dictionary
dct = Dictionary.load_from_text("vocab.txt")
def doc2bow(morphemes):
    """ Converrt strings with non filtered dictionary to vector
    :param morphemes: morphemes
    :return: BOW vector
    """
    global dct
    # We can obtain all of indices and frequency of a morpheme
    # [(ID, frequency)]
    bow_format = dct.doc2bow(morphemes.split())
    return bow_format

def load_dataset():
    """ Load dataset
    :return: data and label
    """
    global dct
    # Load training dataset
    df = pd.read_csv("livedoor_news.csv")
    Dataset = namedtuple("Dataset", ["news", "data", "target", "target_names", "dct"])
    news = [doc for doc in df["news"]]
    data = [doc2bow(doc) for doc in df["news"]]
    target = np.array([label for label in df["class"]]).astype(np.int64)
    target_names = ["dokujo-tsushin","it-life-hack","kaden-channel","livedoor-homme",
                    "movie-enter","peachy","smax","sports-watch","topic-news"]
    ldcc_dataset = Dataset(news, data, target, target_names, dct)
    return ldcc_dataset


import cv2
from matplotlib import pyplot as plt
import numpy as np

input_image = "Prasanna.png"

def display_save(display_name, file_name, img):
    cv2.imshow(display_name, img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    cv2.imwrite(file_name, img)

def grayscale():
    img = cv2.imread(input_image, cv2.IMREAD_GRAYSCALE)
    display_save("Gray", "prasanna_gray.png", img)

def blur():
    img = cv2.imread(input_image)
    kernel = np.ones((20,20),np.float32)/400
    blur = cv2.filter2D(img,-1,kernel)
    display_save("Blurred", "prasanna_blurred.png", blur)

def change_color_space():
    img = cv2.imread(input_image)
    op = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    display_save("BGR2HSV", "prasanna_hsv.png", op)

def mix_color_scheme():
    img = cv2.imread(input_image)
    rows,cols,channels = img.shape
    img[:int(rows),:2*int(cols/3),:] = cv2.cvtColor(img[:int(rows),:2*int(cols/3),:],cv2.COLOR_BGR2RBG)
    img[:int(rows),int(cols/3):2*int(cols/3),:] = cv2.cvtColor(img[:int(rows),:int(cols/3):2*int(cols/3),:],cv2.COLOR_LAB2BGR)
    img[:int(rows),2*int(cols/3):,:] = cv2.cvtColor(img[:int(rows),2*int(cols/3):,:],cv2.COLOR_RGB2YCrCb)
    display_save("Mix Color", "prasanna_mix_color.png", img)

def erosion():
    img = cv2.imread(input_image)
    kernel = np.ones((8,8),np.uint8)
    erosion = cv2.erode(img,kernel,iterations = 1)
    display_save("erosion", "prasanna_eroded.png", erosion)

def morph_gradient():
    img = cv2.imread(input_image)
    kernel = np.ones((10,10),np.uint8)
    gradient = cv2.morphologyEx(img, cv2.MORPH_GRADIENT, kernel)
    display_save("gradient", "prasanna_gradient.png", gradient)

def mask_face():
    img = cv2.imread(input_image, cv2.IMREAD_GRAYSCALE)
    mask = np.zeros(img.shape[:2], np.uint8)
    mask[200:780, 300:750] = 255
    masked_img = cv2.bitwise_and(img,img,mask = mask)
    display_save("Masked Image", "masked_img.png", masked_img)

def histogram_equalization():
    img = cv2.imread(input_image, cv2.IMREAD_GRAYSCALE)
    equ = cv2.equalizeHist(img)
    display_save("Histogram Equalization", "prasanna_gray_histequ.png", equ)

def affine_transformation():
    img = cv2.imread(input_image, cv2.IMREAD_GRAYSCALE)
    rows,cols = map(int, img.shape)
    pts1 = np.float32([[70,50],[200,70],[70,200]])
    pts2 = np.float32([[100,100],[200,50],[100,250]])
    M = cv2.getAffineTransform(pts1,pts2)
    dst = cv2.warpAffine(img,M,(cols,rows))
    display_save("Affline Transformation", "prasanna_affline1.png", dst)

def edge_detection():
    img = cv2.imread(input_image, cv2.IMREAD_GRAYSCALE)
    edges = cv2.Canny(img,100,200)
    display_save("Edges", "prasanna_edges.png", edges)

def image_thresholding():
    img = cv2.imread(input_image, cv2.IMREAD_GRAYSCALE)
    ret,thresh1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
    display_save("IMG_THRESHOLDING", "prasanna_thresholding.png", thresh1)

'''
grayscale()
blur() #1
change_color_space() #2
switch_color_scheme() #3
erosion() #4
morph_gradient() #5
mask_face() #6
histogram_equalization() #7
affine_transformation() #8
edge_detection() #9
image_thresholding() #10
'''
blur()


import streamlit as st
from PIL import Image
import random
import pandas as pd

def app():
	image = Image.open('./picture/khtn.PNG')
	st.image(image, width=500)

	st.markdown("------")

	st.markdown("""

	""", unsafe_allow_html=True)

	st.markdown('
Bài 15: List (mở rộng)
', unsafe_allow_html=True)

	st.markdown("## Hãy xắp xếp hình theo hình mẫu")
	st.image(Image.open('./picture/van.jpg'), width=800)

	st.write("### Hình cần xắp xếp: ")
	col1, col2, col3, col4 = st.columns(4)

	# left = 5
	# top = height / 4
	# right = 164
	# bottom = 3 * height / 4

	# im1 = im.crop((left, top, right, bottom))

	imwidth = 1920
	imheight = 1604

	onethid_imw = imwidth/3   # = 640
	onethid_height = imheight/3  # = 534.6

	with col1:

		pic6 = st.image(Image.open('./picture/van.jpg').crop((onethid_imw*2, onethid_height, onethid_imw*3, onethid_height*2)), width=350)
		#st.write("## a")
		st.markdown('
 a ', unsafe_allow_html=True)

		pic5 = st.image(Image.open('./picture/van.jpg').crop((onethid_imw, onethid_height, onethid_imw*2, onethid_height*2)), width=350)
		st.markdown('
 d ', unsafe_allow_html=True)
		pic3 = st.image(Image.open('./picture/van.jpg').crop((onethid_imw*2, 0, onethid_imw*3, onethid_height)), width=350)
		st.markdown('
 g ', unsafe_allow_html=True)
		#st.markdown('
 d
', unsafe_allow_html=True)
		#st.markdown('
 g
', unsafe_allow_html=True)

	with col2:

		pic9 = st.image(Image.open('./picture/van.jpg').crop((onethid_imw*2, onethid_height*2, onethid_imw*3, onethid_height*3)), width=350)
		st.markdown('
 b ', unsafe_allow_html=True)

		pic7 = st.image(Image.open('./picture/van.jpg').crop((0, onethid_height*2, onethid_imw, onethid_height*3)), width=350)
		st.markdown('
 e ', unsafe_allow_html=True)

		pic1 = st.image(Image.open('./picture/van.jpg').crop((0, 0, onethid_imw, onethid_height)), width=350)
		st.markdown('
 h ', unsafe_allow_html=True)

	with col3:

		pic4 = st.image(Image.open('./picture/van.jpg').crop((0, onethid_height, onethid_imw, onethid_height*2)), width=350)
		st.markdown('
 c ', unsafe_allow_html=True)

		pic8 = st.image(Image.open('./picture/van.jpg').crop((onethid_imw, onethid_height*2, onethid_imw*2, onethid_height*3)), width=350)
		st.markdown('
 f ', unsafe_allow_html=True)

		pic2 = st.image(Image.open('./picture/van.jpg').crop((onethid_imw, 0, onethid_imw*2, onethid_height)), width=350)
		st.markdown('
 i ', unsafe_allow_html=True)

	with col4:
		pass

	st.write("## Hãy xắp xếp hình trên vào thứ tự trong khung để hoàn thiện")
	st.image(Image.open('./picture/frame.PNG'), width=400)
	selection = st.multiselect(" ", options=["a", "b", "c", "d", "e", "f", "g", "h", "i"])

	if st.button("Xem kết quả: "):

		if selection == ["h", "i", "g", "c", "d", "a", "e", "f", "b"]:
			st.write("#### Chính xác chúc mừng bạn :tada:")

			col5, col6, col7, col8, col9, col10 = st.columns(6)

			with col5:

				pic1 = st.image(Image.open('./picture/van.jpg').crop((0, 0, onethid_imw, onethid_height)), width=200)

				pic4 = st.image(Image.open('./picture/van.jpg').crop((0, onethid_height, onethid_imw, onethid_height*2)), width=200)

				pic7 = st.image(Image.open('./picture/van.jpg').crop((0, onethid_height*2, onethid_imw, onethid_height*3)), width=200)

			with col6:

				pic2 = st.image(Image.open('./picture/van.jpg').crop((onethid_imw, 0, onethid_imw*2, onethid_height)), width=200)

				pic5 = st.image(Image.open('./picture/van.jpg').crop((onethid_imw, onethid_height, onethid_imw*2, onethid_height*2)), width=200)
				pic8 = st.image(Image.open('./picture/van.jpg').crop((onethid_imw, onethid_height*2, onethid_imw*2, onethid_height*3)), width=200)

			with col7:
				pic3 = st.image(Image.open('./picture/van.jpg').crop((onethid_imw*2, 0, onethid_imw*3, onethid_height)), width=200)

				pic6 = st.image(Image.open('./picture/van.jpg').crop((onethid_imw*2, onethid_height, onethid_imw*3, onethid_height*2)), width=200)

				pic9 = st.image(Image.open('./picture/van.jpg').crop((onethid_imw*2, onethid_height*2, onethid_imw*3, onethid_height*3)), width=200)

			with col8:
				pass
			with col9:
				pass

			with col10:
				pass
			st.write("----")
			st.write("#### Bức ảnh hoàn thiện ")
			st.image(Image.open('./picture/van.jpg'), width=800)
			st.write("""##### Thông tin tranh vẽ
				Tên: Fields near the Alpilles
	Tác giả: Vincent van Gogh’s
	Năm: 1889
	Địa điểm: Pháp""")

		else:
			st.write("#### Bạn sai rồi hãy xắp xếp lại nhé :cry:")


# -*- coding: utf-8 -*-
"""
Created on 02/17/2019
NSC - AD440 CLOUD PRACTICIUM
@author: Dao Nguyen

Changed ownership on 03/01/2019
@author: Michael Leon
"""

import urllib.request
import re
import os
import json
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import selenium.webdriver.chrome.service as service
from bs4 import BeautifulSoup
import bs4 as bs
import time
from dateutil.parser import parse
from datetime import datetime
import datetime as dt
import boto3
import uuid

dynamodb = boto3.resource('dynamodb', 'us-east-1')
f = open("sslog.log", "w")
OUTPUT = []
ADDRESS = ['Renton: Lindbergh HS Pool: 16740 128th Ave SE Renton, WA 98058',
           'Shoreline Pool: 19030 1st Ave NE Shoreline, WA 98155',
           'Juanita Aquatic Center: 01 NE 132nd St Kirkland, WA 98034 425-936-1627',
           'Hazen High School: 1101 Hoquiam Ave NE Renton, WA 98059 425-204-4230']
#This script scrapes a website and pulls specific data.
def main():
    print("Starting SS Scraper; " + str(datetime.now()), file=f)
    try:
        print("Connecting to http://www.shadowsealsswimming.org/Calendar.html; success", file=f)
        source = urllib.request.urlopen('http://www.shadowsealsswimming.org/Calendar.html').read()
        soup = bs.BeautifulSoup(source,'html.parser')

        table = soup.table
        table = soup.find('table')
        table_rows = table.find_all('tr')
        for tr in table_rows:
            td = tr.find_all('td')
            row = [i.text for i in td]
            data = {}

            find_date = row[0].replace(' - ', '-')
            find_date = find_date.replace(',', '').split(' ')
            find_location = row[2].split(' ')

            target_title = ""
            for find_title in find_location:
                if find_title != "-":
                    target_title = target_title + find_title + " "
                else:
                    break
            data["Title"] = target_title.strip('\xa0').strip('\n')

            if len(find_date) >= 3:
                date_string = find_date[0] + ' ' + find_date[1] + ' ' + find_date[2].strip('\n')
                if '-' in find_date[1]:
                    date_operation = find_date[1].split('-')

                    for x in range(int(date_operation[0]), int(date_operation[1]) + 1):
                        new_date_string = ""
                        new_date_string = new_date_string + find_date[0] + ' ' + \
                            str(x) + ' ' + find_date[2].strip('\n')

                        date_object = validate_date(new_date_string)
                        if date_object:
                            data["Date"] = date_object.strftime(
                                '%Y-%m-%d')
                        for i in td:
                            time = ""
                            event_des = ""
                            if ("pm" in row[1].lower() or "am" in row[1].lower()) and any(c.isdigit() for c in row[1]):
                                time += row[1]
                                data["Time"] = time.replace('\n', '')

                            for location in find_location:
                                for address in ADDRESS:
                                    if location in address:
                                        data["Location"] = address
                            data["Desription"] = row[2].replace('\n', '').replace('\xa0', '')
                        #print(data)
                        OUTPUT.append(data)

                else:
                    date_object = validate_date(date_string)
                    if date_object:
                        data["Date"] = date_object.strftime(
                            '%Y-%m-%d')
                    else:
                        data["Date"] = row[0].strip('\xa0')

                    for i in td:
                        time = ""
                        event_des = ""
                        if ("pm" in row[1].lower() or "am" in row[1].lower()) and any(c.isdigit() for c in row[1]):
                            time += row[1]
                            data["Time"] = time.replace('\n','')

                        for location in find_location:
                            for address in ADDRESS:
                                if location in address:
                                    data["Location"] = address
                        data["Description"] = row[2].replace('\n', '').replace('\xa0', '')
                #print(row)
                    if "Location" not in data:
                        data["Location"] = "Unknown"
                    if "Time" not in data:
                        data["Time"] = "Unknown"
                    data["URL"] = "http://www.shadowsealsswimming.org/Calendar.html"
                    data["ID"] = str(uuid.uuid3(uuid.NAMESPACE_DNS, data["Title"] + data["Date"]))
                    print("Found event " + data["Title"], file=f)
                    to_dynamo(data)
    except:
        print("Connecting to http://www.shadowsealsswimming.org/Calendar.html; failed", file=f)
    print("Ending SS Scraper; " + str(datetime.now()), file=f)

def to_dynamo(data):
    table = dynamodb.Table('events')
    table.put_item(Item={'ID': data['ID'],
                        'URL': data['URL'],
                        'Title': data['Title'],
                        'Description': data['Description'],
                        'Location': data['Location'],
                        'Date': data['Date']})
    #s3.Object('mjleontest', 'browser_event_data.json').put(Body=open('browser_event_data.json', 'rb'))

# This function to check the date in string and return if date is valid or not
def validate_date(date_text):
    date_string = date_text
    new_date_string = ""
    fmts = ('%Y','%b %d, %Y','%b %d, %Y','%B %d, %Y','%B %d %Y','%m/%d/%Y','%m/%d/%y','%b %Y','%B%Y','%b %d,%Y', '%b %d %Y')
    month = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec')

    date_string = date_string.split(' ')
    for find_month in month:
        if find_month in date_string[0]:
            set_month = find_month
            new_date_string += set_month + ' ' + date_string[1] + ' ' + date_string[2]

    for fmt in fmts:
        try:
            t = dt.datetime.strptime(new_date_string, fmt)
                #print(t)
            return t
            break
        except ValueError as err:
            pass

if __name__ == '__main__':
    main()


"""Unit tests for metrics module."""
from ldp.utils import linalg

import torch
import torch.linalg

def test_effective_rank():
    """Test effective_rank matches intuition."""
    matrix = torch.diag(torch.tensor([100, 100, 1e-6]))
    actual = linalg.effective_rank(matrix)
    assert torch.allclose(torch.tensor(actual), torch.tensor(2.))

def test_truncate():
    """Test truncate matches intuition."""
    matrix = torch.eye(3)
    actual = linalg.truncate(matrix, 2)
    assert torch.linalg.matrix_rank(actual) == 2

def test_rowspace():
    """Test rowspace returns projection onto rowspace."""
    matrix = torch.tensor([[3., 1., 0.], [0., 2., 0.], [1., 0., 0.]])
    expected = torch.eye(3)
    expected[-1, -1] = 0
    actual = linalg.rowspace(matrix)

    assert actual.allclose(expected, atol=1e-7)
    assert actual.mm(actual).allclose(actual, atol=1e-7)

def test_rowspace_close_to_zero():
    """Test rowspace returns zeros when all elements close to 0."""
    matrix = torch.ones(10, 10) / 1e10
    actual = linalg.rowspace(matrix)
    assert actual.equal(torch.zeros_like(actual))

def test_nullspace():
    """Test nullspace returns projection onto nullspace."""
    matrix = torch.tensor([[3., 1., 0.], [0., 2., 0.], [1., 0., 0.]])

    expected = torch.zeros(3, 3)
    expected[-1, -1] = 1

    actual = linalg.nullspace(matrix)
    assert actual.allclose(expected, atol=1e-6)
    assert actual.mm(actual).allclose(actual, atol=1e-6)


import os
import tornado
import importlib
from ..base.handlers import default_handlers as default_base_handlers
from ..services.kernels.pool import ManagedKernelPool
from .cell.parser import APICellParser
from .swagger.handlers import SwaggerSpecHandler
from .handlers import NotebookAPIHandler, parameterize_path, NotebookDownloadHandler
from notebook.utils import url_path_join
from traitlets import Bool, Unicode, Dict, default
from traitlets.config.configurable import LoggingConfigurable

class NotebookHTTPPersonality(LoggingConfigurable):
    """Personality for notebook-http support, creating REST endpoints
    based on the notebook's annotated cells
    """
    cell_parser_env = 'KG_CELL_PARSER'
    cell_parser = Unicode('kernel_gateway.notebook_http.cell.parser',
        config=True,
        help="""Determines which module is used to parse the notebook for endpoints and
            documentation. Valid module names include 'kernel_gateway.notebook_http.cell.parser'
            and 'kernel_gateway.notebook_http.swagger.parser'. (KG_CELL_PARSER env var)
            """
    )

    @default('cell_parser')
    def cell_parser_default(self):
        return os.getenv(self.cell_parser_env, 'kernel_gateway.notebook_http.cell.parser')

    # Intentionally not defining an env var option for a dict type
    comment_prefix = Dict({
        'scala': '//',
        None: '#'
    }, config=True, help='Maps kernel language to code comment syntax')

    allow_notebook_download_env = 'KG_ALLOW_NOTEBOOK_DOWNLOAD'
    allow_notebook_download = Bool(config=True,
        help="Optional API to download the notebook source code in notebook-http mode, defaults to not allow"
    )

    @default('allow_notebook_download')
    def allow_notebook_download_default(self):
        return os.getenv(self.allow_notebook_download_env, 'False') == 'True'

    static_path_env = 'KG_STATIC_PATH'
    static_path = Unicode(None, config=True, allow_none=True,
        help="Serve static files on disk in the given path as /public, defaults to not serve"
    )

    @default('static_path')
    def static_path_default(self):
        return os.getenv(self.static_path_env)

    def __init__(self, *args, **kwargs):
        super(NotebookHTTPPersonality, self).__init__(*args, **kwargs)
        # Import the module to use for cell endpoint parsing
        cell_parser_module = importlib.import_module(self.cell_parser)
        # Build the parser using the comment syntax for the notebook language
        func = getattr(cell_parser_module, 'create_parser')
        try:
            kernel_language = self.parent.seed_notebook['metadata']['language_info']['name']
        except (AttributeError, KeyError):
            kernel_language = None
        prefix = self.comment_prefix.get(kernel_language, '#')
        self.api_parser = func(parent=self, log=self.log,
                               comment_prefix=prefix,
                               notebook_cells=self.parent.seed_notebook.cells)
        self.kernel_language = kernel_language

    def init_configurables(self):
        """Create a managed kernel pool"""
        self.kernel_pool = ManagedKernelPool(
            self.parent.prespawn_count,
            self.parent.kernel_manager
        )

    def create_request_handlers(self):
        """Create handlers and redefine them off of the base_url path. Assumes
        init_configurables() has already been called, and that the seed source
        was available there.
        """
        handlers = []
        # Register the NotebookDownloadHandler if configuration allows
        if self.allow_notebook_download:
            path = url_path_join('/', self.parent.base_url, r'/_api/source')
            self.log.info('Registering resource: {}, methods: (GET)'.format(path))
            handlers.append((
                path,
                NotebookDownloadHandler,
                {'path': self.parent.seed_uri}
            ))

        # Register a static path handler if configuration allows
        if self.static_path is not None:
            path = url_path_join('/', self.parent.base_url, r'/public/(.*)')
            self.log.info('Registering resource: {}, methods: (GET)'.format(path))
            handlers.append((
                path,
                tornado.web.StaticFileHandler,
                {'path': self.static_path}
            ))

        # Discover the notebook endpoints and their implementations
        endpoints = self.api_parser.endpoints(self.parent.kernel_manager.seed_source)
        response_sources = self.api_parser.endpoint_responses(self.parent.kernel_manager.seed_source)
        if len(endpoints) == 0:
            raise RuntimeError('No endpoints were discovered. Check your notebook to make sure your cells are annotated correctly.')

        # Cycle through the (endpoint_path, source) tuples and register their handlers
        for endpoint_path, verb_source_map in endpoints:
            parameterized_path = parameterize_path(endpoint_path)
            parameterized_path = url_path_join('/', self.parent.base_url, parameterized_path)
            self.log.info('Registering resource: {}, methods: ({})'.format(
                parameterized_path,
                list(verb_source_map.keys())
            ))
            response_source_map = response_sources[endpoint_path] if endpoint_path in response_sources else {}
            handler_args = { 'sources' : verb_source_map,
                'response_sources' : response_source_map,
                'kernel_pool' : self.kernel_pool,
                'kernel_name' : self.parent.kernel_manager.seed_kernelspec,
                'kernel_language' : self.kernel_language or ''
            }
            handlers.append((parameterized_path, NotebookAPIHandler, handler_args))

        # Register the swagger API spec handler
        path = url_path_join('/', self.parent.base_url, r'/_api/spec/swagger.json')
        handlers.append((
            path,
            SwaggerSpecHandler, {
                'notebook_path' : self.parent.seed_uri,
                'source_cells': self.parent.seed_notebook.cells,
                'cell_parser' : self.api_parser
        }))
        self.log.info('Registering resource: {}, methods: (GET)'.format(path))

        # Add the 404 catch-all last
        handlers.append(default_base_handlers[-1])
        return handlers

    def should_seed_cell(self, code):
        """Determines whether the given code cell source should be executed when
        seeding a new kernel."""
        # seed cells that are uninvolved with the presented API
        return (not self.api_parser.is_api_cell(code) and not self.api_parser.is_api_response_cell(code))

    def shutdown(self):
        """Stop all kernels in the pool."""
        self.kernel_pool.shutdown()

def create_personality(*args, **kwargs):
    return NotebookHTTPPersonality(*args, **kwargs)


from django.urls import path

from .views import (case_detail, add_case, update_case,
                    delete_case, add_task,upload_files,
                    new_case,  pending_list, completed_list, CaseList, CaseCreateView, CategoryListView,
                    delete_cat, cat_update, cat_add, add_argument, cat_detail, file_list,
                    delete_files, task_view, task_list_view, completed, delete_task, argument_list, argument_detail, arg_update, arg_delete, case_filter, complete_case,
                    court_list, add_court, court_detail, update_court, court_delete,

                    add_session, update_session, detail_session, delete_session,bill_case,

                    add_process, edit_task,generate_process,generate_process1,list_processes,process_detail,update_process,delete_process,add_to_archives,
                    upload_archives,update_archive,archive_detail,
                    add_action_report,list_action_reports,update_action,post_action_detail,delete_action,
                    add_rep,rep_detail,update_rep,delete_rep,rep_list,
                    case_history,
                    request_file,request_list,approve_request,make_request,all_files,start_timer,case_hours,
                    add_expense,ExpenseDetailView,ExpenseListView,update_expense,delete_expense,
                    RenumerationList,approve_renumeration,request_renumeration,
                    request_client_payment,update_client_payment,client_payment_list,client_payment_detail,edit_start_timer, addpercharge, PendingExpenseListView, accept_expense,
                    decline_expense,resend_expense,generate_invoice_total, generate_invoice_total_save,calendar,all_user_logs,all_user_logs_per_case,upload_terms_files,terms_files_list,terms_edit

                    )

app_name = "cases"

urlpatterns = [

    path('cases/list/', CaseList.as_view(), name='case_list'),
    path('calendar/', calendar, name='calendar'),
    path('cases/list/filter-cases', case_filter, name='case_filter'),

    path("cases/pending-list", pending_list, name="pending_list"),

    path("cases/completed-list", completed_list, name="completed_list"),
    ############################### ARCHIVED CASES #####################################################
    path("cases/archived_cases", add_to_archives, name="add_archive"),
    path("cases/archives/<int:pk>/archive-details", archive_detail, name="archive_detail"),
    path("cases/archives/<int:pk>/update-archive",update_archive, name="archive_update"),
    path("cases/archives/<int:pk>/upload-files",upload_archives, name="archive_upload"),

    path("cases/<int:pk>/case-detail", case_detail, name="case_detail"),
    path("cases/add-case", add_case, name="add_case"),

    path("cases/<int:pk>/case-update", update_case, name="case_update"),
    path("cases/<int:pk>/case-delete", delete_case, name="case_delete"),
    path("cases/tasks/<int:pk>/add-task", add_task, name="add_task"),
    path("cases/files/<int:pk>/add-files", upload_files, name="upload_files"),

    ############################### TERMS OF ENGAGEMENT ########################################################
    path("cases/termsofengagement/<int:pk>/add-files", upload_terms_files, name="upload_terms_files"),
    path("cases/termsofengagement/<int:pk>/list-files", terms_files_list, name="terms_files_list"),
    path("cases/termsofengagement/terms_edit", terms_edit, name="terms_edit"),

    ############################### CATEGORY ########################################################
    path("cases/category/list", CategoryListView.as_view(), name="cat_list"),
    path("cases/category/<int:pk>/update",
         cat_update, name="cat_update"),
    path("cases/category/<int:pk>/delete", delete_cat, name="cat_delete"),
    path("cases/category/add", cat_add, name="cat_add"),
    path("cases/argument/<int:pk>/add", add_argument, name="add_arg"),
    path("cases/category/<int:pk>/detail", cat_detail, name="cat_detail"),
    path("cases/<int:pk>/case-files", file_list, name="file_list"),
    path("cases/<int:pk>/delete_file", delete_files, name="delete_file"),
    path("cases/tasks/<int:pk>/list", task_view, name="task_list"),
    path("cases/tasks/<int:pk>/list-tasks", task_list_view, name="list_tasks"),
    path("cases/tasks/<int:pk>/completed", completed, name="completed"),
    path("cases/tasks/<int:pk>/delete", delete_task, name="delete_task"),
    path("cases/<int:pk>/legal_arguments/list",
         argument_list, name="argument_list"),
    path("cases/<int:pk>/legal_arguments/detail",
         argument_detail, name="argument_detail"),
    path("cases/<int:pk>/legal_arguments/update",
         arg_update, name="argument_update"),
    path("cases/<int:pk>/legal_arguments/delete",
         arg_delete, name="argument_delete"),

    path("cases/<int:pk>/case-update-status",
         complete_case, name="complete_case"),
    ########################################################## COURT ######################################################################
    path('cases/courts/list', court_list, name='court_list'),
    path('cases/courts/<int:pk>/court-detail',
         court_detail, name="court_detail"),
    path('cases/courts/<int:pk>/court-update',
         update_court, name="court_update"),
    path('cases/courts/add', add_court, name="court_add"),
    path('cases/courts/<int:pk>/court-delete',
         court_delete, name="court_delete"),

    #################################################### COURT SESSIONS #################################################################

    path('cases/court-sessions/<int:pk>/add', add_session, name="session_add"),
    path('cases/court-sessions/<int:pk>/details',
         detail_session, name="session_detail"),
    path('cases/court-sessions/<int:pk>/update',
         update_session, name="session_update"),
    path('cases/court-sessions/<int:pk>/delete',
         delete_session, name="session_delete"),

    ############################################### PROCESSES ####################################
    path('cases/processes/add', add_process, name="process_add"),
    path('cases/processes/list', list_processes, name="list_process"),

    path('cases/processes/<int:pk>/generate-process', generate_process1, name="generate_process"),
    path('cases/processes/<int:pk>/process-detail', process_detail, name="process_detail"),
    path('cases/processes/<int:pk>/process-update', update_process, name="process_update"),
    path('cases/processes/<int:pk>/delete-process', delete_process, name="process_delete"),

    ############################################### BILL CASE ####################################
    path('cases/<int:pk>/bill-case',bill_case,name='bill_case'),

    path('cases/tasks/<int:pk>/edit-task',edit_task,name='edit_task'),

    ############################################### POST ACTION REPORT ##########################
    path('cases/post-action-report/<int:pk>/add-new',add_action_report,name="add_action"),
    path('cases/post-action-report/<int:pk>/report-list',list_action_reports,name='list_report'),
    path('cases/post-action-report/<int:pk>/report-detail',post_action_detail,name="action_detail"),
    path('cases/post-action-report/<int:pk>/update-report',update_action,name='action_update'),
    path('cases/post-action-report/<int:pk>/delete-report',delete_action,name='action_delete'),

##################################### REPRESENTATION ############################################
    path('cases/representation/add',add_rep,name="add_rep"),
    path('cases/representation/list',rep_list,name="rep_list"),
    path('cases/representation/<int:pk>/details',rep_detail,name="rep_detail"),
    path('cases/representation/<int:pk>/update',update_rep,name="update_rep"),
    path('cases/representation/<int:pk>/delete',delete_rep,name="delete_rep"),
    path("cases/<int:pk>/case-history",case_history,name='case_history'),

    ################################### ARCHIVE REQUESTS #########################################
    path('cases/archives/request',request_file,name='request_file'),
    path('cases/archives/requests/list',request_list,name='request_list'),
    path('cases/archives/<int:pk>/request',make_request,name='make_request'),
    path('cases/archives/<int:pk>/request/approve',approve_request,name='approve_request'),

    ################################### CASE FILES ###############################################
    path('cases/files/all-files',all_files,name="all_files"),
    path('cases/start-timer',start_timer,name="start_timers"),
    path('cases/edit_start_timer',edit_start_timer,name="edit_start_timer"),
    path('cases/<int:pk>/timer/elapsed-time',case_hours,name="case_hours"),
    path('cases/addpercharge',addpercharge,name="addpercharge"),
    path('cases/delete_case/<int:pk>',delete_case,name="delete_case"),

     ################################## EXPENSES #################################################
    path('cases/expenses/', ExpenseListView.as_view(), name='expenses'),
    path('cases/pending_expenses/', PendingExpenseListView.as_view(), name='pending_expenses'),
    path('cases/expenses/<int:pk>/detail', ExpenseDetailView.as_view(), name='expense_detail'),
    path('cases/expenses/<int:pk>/update', update_expense, name='update_expense'),
    path('cases/expenses/<int:pk>/delete', delete_expense, name='delete_expense'),
    path('cases/expenses/add-expense',add_expense,name='add_expense'),
    path('cases/accept_expense/<int:pk>',accept_expense,name='accept_expense'),
    path('cases/decline_expense/<int:pk>',decline_expense,name='decline_expense'),
    path('cases/resend_expense/<int:pk>',resend_expense,name='resend_expense'),

    ################################### Renumeration #############################################
    path('case/renumeration/list',RenumerationList.as_view(),name='renumerations'),
    path('case/renumeration/<int:pk>/approve',approve_renumeration,name='approve_renumeration'),
    path('case/renumeration/<int:pk>/request',request_renumeration,name='request_renumeration'),

    ################################## REQUEST CLIENT PAYMENT INFORMATION ########################
    path('cases/<int:pk>/client-payment',request_client_payment,name='request_payment_info'),
    path('cases/<int:pk>/update-client-payment',update_client_payment,name='update_payment_info'),
    path('cases/client-payment',client_payment_list,name='client_payment_list'),
    path('cases/<int:pk>/client-payment-details',client_payment_detail,name='client_payment_detail'),

    ################################## INVOICE ########################
    path('cases/generate_invoice_total/<int:pk>',generate_invoice_total,name='generate_invoice_total'),
    path('cases/generate_invoice_total_save/<int:pk>',generate_invoice_total_save,name='generate_invoice_total_save'),

    ############################################### USERLOGS ####################################

    path('userlogs/', all_user_logs, name='all_user_logs'),
    path('caselogs/<int:pk>', all_user_logs_per_case, name='all_user_logs_per_case'),

    ]


import os
import csv
from PIL import Image
import torch
from torch.utils.data import Dataset
from typing import Any, Callable, Optional, Tuple

class Emotions(Dataset):
    def __init__(self, cvs_file, root_dir, transform=None):
        self.root_dir = root_dir
        self.transform=transform
        data_file=os.path.join(root_dir, cvs_file)
        with open(data_file, 'r', newline='') as file:
            self._samples = [
                (
                torch.tensor([int(idx) for idx in row [' pixels'].split()],
                                          dtype=torch.uint8).reshape(48, 48),
                                          int(row['emotion']) if 'emotion' in row else None,
                                          )
                                          for row in csv.DictReader(file)
                                          ]

    def __len__(self):
        return len(self._samples)

    def __getitem__(self, idx: int):
        image_tensor, target = self._samples[idx]
        image = Image.fromarray(image_tensor.numpy())

        if self.transform:
            image=self.transform(image)
        return image, target


#!/usr/bin/env python
# coding: utf-8
# Data Analysis of Unemployment in India
# In[1]:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import plotly.express as px

# In[2]:

# importing the given dataset
df = pd.read_csv("C:/Users/ASUS/Downloads/Unemployment in India.csv")
df

# In[3]:

df.isnull().sum()

# In[4]:

#df_dropped = df.dropna()
df.fillna(0, inplace=True)
df.fillna(df.mean(), inplace=True)

# In[5]:

df.isnull().sum()

# In[6]:

df = df.rename(columns={df.columns[0]:'Region',df.columns[3]:'Unemployment_rate',df.columns[4]:'Employed', df.columns[5]:'labour_participation_rate', df.columns[6]:'area'})
df.head()

# In[7]:

df["Region"].unique()

# In[8]:

df2 = pd.read_csv("C:/Users/ASUS/Downloads/Unemployment_Rate_upto_11_2020.csv")

df2

# In[9]:

# create a new column for month

#df2['Date'] = pd.to_datetime(df2['Date'], dayfirst=True)
#df2['month_int'] =  df2['Date'].dt.month
#df2['month'] =  df2['month_int'].apply(lambda x: calendar.month_abbr[x])
#df2.head()

# In[10]:

df2 = df2.rename(columns={df2.columns[0]:'Region',df2.columns[3]:'Unemployment_rate',df2.columns[4]:'Employed', df2.columns[5]:'labour_participation_rate', df2.columns[6]:'area'})
df2.head()

# In[11]:

heat_maps = df[['Unemployment_rate','Employed','labour_participation_rate']]

heat_maps = heat_maps.corr()

plt.figure(figsize=(12,7))
sns.set_context('notebook',font_scale=1)
sns.heatmap(heat_maps, annot=True,cmap='winter');

# In[12]:

df2.columns= ["Region","Date","Frequency","Unemployment_rate","Employed","labour_participation_rate","area","longitude","latitude"]

plt.figure(figsize=(14, 18))
plt.title("Unemployment_rate")
sns.histplot(x="Unemployment_rate", hue="Region", data=df)
plt.show()

# In[13]:

region = df2.groupby(["Region"])[['Unemployment_rate', "Employed", "labour_participation_rate"]].mean()
region = pd.DataFrame(region).reset_index()

fig = px.bar(region, x="Region", y="Employed", color="Region", title="Average Employed Rate by Region")
fig.update_layout(xaxis={'categoryorder':'total descending'})
fig.show()

# In[14]:

region = df2.groupby(["Region"])[['Unemployment_rate', "Employed", "labour_participation_rate"]].mean()
region = pd.DataFrame(region).reset_index()

fig = px.bar(region, x="Region", y="Unemployment_rate", color="Region", title="Average Employed Rate by Region")
fig.update_layout(xaxis={'categoryorder':'total descending'})
fig.show()

# In[15]:

unemployment = df2[["Region", "area", "Unemployment_rate"]]

fig = px.sunburst(unemployment, path=['area','Region'], values='Unemployment_rate',
                  title= 'Unemployment rate in every State and Region', height=700)
fig.show()

# In[ ]:

# In[ ]:

# In[ ]:


# Read text from a file, and count the occurence of words in that text
# Example:
# count_words("The cake is done. It is a big cake!")
# --> {"cake":2, "big":1, "is":2, "the":1, "a":1, "it":1}
from collections import Counter
import re

def read_file_content(filename):

    # Reading the text file
    with open(filename, "r") as f:

        return f.read()

def count_words():
    text = read_file_content("./story.txt")

    # [assignment] Add your code here
    text = text.lower()
    text = re.sub(r'[^\w\s]', '', text)

    return Counter(text.split())

print(count_words())


import math
import string
from nltk.corpus import stopwords
from collections import Counter
from nltk.stem.porter import *
from sklearn.feature_extraction.text import TfidfVectorizer
import jieba

def tfidf_calc(text, corpus, k, file_path):
    # corpus = ['This is the first document.',
    #           'This is the second second document.',
    #           'And the third one.',
    #           'Is this the first document?', ]
    tv = TfidfVectorizer(use_idf=True, smooth_idf=True, norm=None)
    tv_fit = tv.fit_transform(corpus)
    new_corpus = tv.get_feature_names()[-1::-1]
    print(new_corpus)
    with open(file_path, 'w', encoding='utf_8') as f:
        for i in range(k):
            undetermined_words = synonym_replace(new_corpus[i])
            if len(undetermined_words) == 0:
                continue
            for j in range(len(undetermined_words)):
                f.write(text.replace(
                    new_corpus[i], undetermined_words[j]) + '\n')

def divide_text(text):
    seg_text = jieba.cut(text, cut_all=False)
    return ' '.join(seg_text)

def synonym_replace(word):
    with open('synonym.txt', 'r', encoding='utf_8') as f:
        synonym_database = list(
            map(lambda x: x.strip().split()[1:], f.readlines()))
    words = []
    flag = False
    for i in range(len(synonym_database)):
        for j in range(len(synonym_database[i])):
            if synonym_database[i][j] == word:
                for k in range(len(synonym_database[i])):
                    if synonym_database[i][k] == word:
                        continue
                    words.append(synonym_database[i][k])
                    flag = True
                    break
            if flag:
                break
        if flag:
            break
    return words

if __name__ == '__main__':
    with open('query.txt', 'r', encoding='utf_8') as f:
        text = f.readlines()
    corpus = list(map(divide_text, text))
    tfidf_calc(text[0], corpus, 3, 'new_query.txt')


"""
@Time : 2021/1/31 18:23
@Author : Steven Chen
@File : 8.selenium_cookies.py
@Software: PyCharm
"""
# 目标：
# 方法：
from selenium import webdriver

url = 'https://www.baidu.com'

driver = webdriver.Chrome()
driver.get(url)

cookies = {data["name"]: data["value"] for data in driver.get_cookies()}
print(cookies)


from decimal import Decimal

from django.contrib.gis.db.backends.base import BaseSpatialOperations
from django.contrib.gis.db.backends.util import SpatialFunction
from django.contrib.gis.geometry.backend import Geometry
from django.contrib.gis.measure import Distance
from django.utils import six
from sql_server.pyodbc.operations import DatabaseOperations

from .models import SpatialRefSys

class MSSqlBoolMethod(SpatialFunction):
    """SQL Server (non-static) spatial functions are treated as methods,
    for eg g.STContains(p)"""

    sql_template = '%(geo_col)s.%(function)s(%(geometry)s) = 1'

    def __init__(self, function, **kwargs):
        super(MSSqlBoolMethod, self).__init__(function, **kwargs)

class MSSqlDistanceFunc(SpatialFunction):
    """Implements distance comparison lookups, eg distance_lte"""

    sql_template = ('%(geo_col)s.%(function)s(%(geometry)s) '
                    '%(operator)s %(result)s')

    def __init__(self, op):
        super(MSSqlDistanceFunc, self).__init__('STDistance',
                                                operator=op,
                                                result='%s')

class MSSqlBBBoolMethod(MSSqlBoolMethod):
    """SQL Server has no native bounding-box methods, but we can emulate
    them with a slightly more complicated expression.  The call will
    be translated into something like
    col.STEnvelope().STOverlaps(geom.STEnvelope())
    where STEnvelope() first simplifies the geometries to their
    bounding rectangles."""

    sql_template = '%(geo_col)s.STEnvelope().%(function)s(%(geometry)s.STEnvelope()) = 1'

    def __init__(self, function, **kwargs):
        super(MSSqlBoolMethod, self).__init__(function, **kwargs)

class MSSqlAdapter(str):
    """This adapter works around an apparent bug in the pyodbc driver
    itself.  We only require the wkt adapter, but if we use
    django.contrib.gis.db.backends.adapter.WKTAdapter then
    cursor.execute() fails because it doesn't call str() on unrecognised
    types.  So we make sure that our adaper *is* a string."""

    def __new__(cls, geom):
        geostr = str.__new__(cls, geom.wkt)
        geostr.srid = geom.srid
        return geostr

    def __eq__(self, other):
        if not isinstance(other, MSSqlAdapter):
            return False
        return super(MSSqlAdapter, self).__eq__(other) and \
            self.srid == other.srid

    def prepare_database_save(self, unused):
        return self

# Valid distance types and substitutions
dtypes = (Decimal, Distance, float) + six.integer_types

class MSSqlOperations(DatabaseOperations, BaseSpatialOperations):

    name = 'SQL Server'
    select = '%s.STAsText()'

    Adapter = MSSqlAdapter
    Adaptor = Adapter  # Backwards-compatibility alias.

    compiler_module = 'django_pyodbc_gis.compiler'

    geometry = True
    geography = True

    # Clearly a bald-faced lie, but the limitations of SQL Server are
    # actually similar to those of mysql, and coupled with some
    # baked-in mysql checks in the framework this lie enables the
    # admin to run.  See geometry_columns() and spatial_ref_sys().
    mysql = True

    # NOTE: we should get a default added for core code
    mssql = True

    # 'bbcontains'
    # 'bboverlaps'
    # 'contained'
    # 'contains'
    # 'contains_properly'
    # 'coveredby'
    # 'covers'
    # 'crosses'
    # 'disjoint'
    # 'distance_gt'
    # 'distance_gte'
    # 'distance_lt'
    # 'distance_lte'
    # 'dwithin'
    # 'equals'
    # 'exact'
    # 'intersects'
    # 'overlaps'
    # 'relate'
    # 'same_as'
    # 'touches'
    # 'within'
    # 'left'
    # 'right'
    # 'overlaps_left'
    # 'overlaps_right'
    # 'overlaps_above'
    # 'overlaps_below'
    # 'strictly_above'
    # 'strictly_below'

    geometry_functions = {
        'bbcontains': MSSqlBBBoolMethod('STContains'),
        'bboverlaps': MSSqlBBBoolMethod('STOverlaps'),
        'contained': MSSqlBBBoolMethod('STWithin'),
        'contains': MSSqlBoolMethod('STContains'),
        'crosses': MSSqlBoolMethod('STCrosses'),
        'disjoint': MSSqlBoolMethod('STDisjoint'),
        'equals': MSSqlBoolMethod('STEquals'),  # can we also implement exact, same_as like this?
        'intersects': MSSqlBoolMethod('STIntersects'),
        'overlaps': MSSqlBoolMethod('STOverlaps'),
        'touches': MSSqlBoolMethod('STTouches'),
        'within': MSSqlBoolMethod('STWithin'),
    }

    distance_functions = {
        'distance_gt': (MSSqlDistanceFunc('>'), dtypes),
        'distance_gte': (MSSqlDistanceFunc('>='), dtypes),
        'distance_lt': (MSSqlDistanceFunc('<'), dtypes),
        'distance_lte': (MSSqlDistanceFunc('<='), dtypes),
    }
    geometry_functions.update(distance_functions)

    geography_functions = {
        'contains': MSSqlBoolMethod('STContains'),
        'disjoint': MSSqlBoolMethod('STDisjoint'),
        'equals': MSSqlBoolMethod('STEquals'),
        'intersects': MSSqlBoolMethod('STIntersects'),
        'overlaps': MSSqlBoolMethod('STOverlaps'),
        'within': MSSqlBoolMethod('STWithin'),
    }
    geography_functions.update(distance_functions)

    gis_terms = set(geometry_functions) | set(['isnull'])

    collect = 'CollectionAggregate'
    extent = 'EnvelopeAggregate'
    unionagg = 'UnionAggregate'
    distance = 'STDistance'

    valid_aggregates = dict([(k, None) for k in
                             ('Collect', 'Extent', 'Union')])

    def spatial_lookup_sql(self, lvalue, lookup_type, value, field, qn):
        alias, col, db_type = lvalue

        geo_col = '%s.%s' % (qn(alias), qn(col))

        if field.geography:
            if lookup_type not in self.geography_functions:
                raise TypeError("Got invalid lookup_type for geography: %s" %
                                lookup_type)
        else:
            if lookup_type not in self.geometry_functions:
                raise TypeError("Got invalid lookup_type for geometry: %s" %
                                lookup_type)

        if lookup_type == 'isnull':
            return "%s IS %sNULL" % (geo_col, ('' if value else 'NOT ')), []

        else:
            if field.geography:
                op = self.geography_functions[lookup_type]
            else:
                op = self.geometry_functions[lookup_type]

            # if lookup_type is a tuple then we expect the value to be
            # a tuple as well:
            if isinstance(op, tuple):
                dist_op, arg_type = op

                # Ensuring that a tuple _value_ was passed in from the user
                if not isinstance(value, tuple):
                    raise ValueError('Tuple required for `%s` lookup type.' %
                                     lookup_type)
                if len(value) != 2:
                    raise ValueError('2-element tuple required for %s lookup type.' %
                                     lookup_type)

                # Ensuring the argument type matches what we expect.
                if not isinstance(value[1], arg_type):
                    raise ValueError('Argument type should be %s, got %s instead.' %
                                     (arg_type, type(value[1])))

                geom = value[0]
                return dist_op.as_sql(geo_col, self.get_geom_placeholder(field, geom))

            return op.as_sql(geo_col, self.get_geom_placeholder(field, value))

    def check_aggregate_support(self, aggregate):
        """
        Checks if the given aggregate name is supported (that is, if it's
        in `self.valid_aggregates`).
        """
        agg_name = aggregate.__class__.__name__
        return agg_name in self.valid_aggregates

    def spatial_aggregate_sql(self, agg):
        """
        Returns the spatial aggregate SQL template and function for the
        given Aggregate instance.
        """
        agg_name = agg.__class__.__name__
        if not self.check_aggregate_support(agg):
            raise NotImplementedError('%s spatial aggregate is not implmented '
                                      'for this backend.' % agg_name)
        agg_name = agg_name.lower()
        if agg_name == 'union':
            agg_name += 'agg'
        # We need to namespace the function depending on whether it's
        # for a geography or geometry (which requires digging into the
        # Aggregate), but the function name is the same for both:
        ns = 'geography' if agg.source.geography else 'geometry'
        sql_template = ns + '::%(function)s(%(field)s).ToString()'
        sql_function = getattr(self, agg_name)
        return sql_template, sql_function

    def convert_extent(self, poly):
        """
        Returns a 4-tuple extent for the `Extent` aggregate by converting
        the bounding box text returned by SQL Server (`poly` argument), for
        example: "POLYGON ((0 0, 2 0, 2 3, 0 3, 0 0))".
        """
        crnrs = poly[10:-2].split(',')
        xmin, ymin = map(float, crnrs[0].strip().split(' '))
        xmax, ymax = map(float, crnrs[2].strip().split(' '))
        return xmin, ymin, xmax, ymax

    def convert_geom(self, hex, geo_field):
        """
        Converts the geometry returned from aggregate queries.
        """
        if hex:
            return Geometry(hex)
        else:
            return None

    # GeometryField operations
    def geo_db_type(self, f):
        # We only have the one geometry type (especially since we
        # don't currently support geography):
        return 'geometry'

    def get_distance(self, f, value, lookup_type):
        """
        Returns the distance parameters for the given geometry field,
        lookup value, and lookup type.  This is based on the Spatialite
        backend, since we don't currently support geography operations.
        """
        if not value:
            return []
        value = value[0]
        if isinstance(value, Distance):
            if f.geodetic(self.connection):
                raise ValueError('The SQL Server backend does not support '
                                 'distance queries on geometry fields with '
                                 'a geodetic coordinate system. Distance '
                                 'objects; use a numeric value of your '
                                 'distance in degrees instead.')
            else:
                dist_param = getattr(value, Distance.unit_attname(f.units_name(self.connection)))
        else:
            dist_param = value
        return [dist_param]

    def get_geom_placeholder(self, f, value):
        """
        Because SQL Server does not support spatial transformations,
        there is no need to modify the placeholder based on the
        contents of the given value.  We do need to specify the SRID
        however, since this argument is required.
        """
        if hasattr(value, 'expression'):
            placeholder = self.get_expression_column(value)
        else:
            ns = 'geography' if f.geography else 'geometry'
            placeholder = '%s::STGeomFromText(%%s,%s)' % (ns, f.srid)
        return placeholder

    # Routines for getting the OGC-compliant models --- SQL Server
    # does not have OGC-compliant tables
    def geometry_columns(self):
        raise NotImplementedError

    def spatial_ref_sys(self):
        return SpatialRefSys


# coding:utf-8
import serial
import time
import atexit
import signal

class Laser(object):

    def __init__(self, com):
        self.rate = 230400
        self.com = com
        self.port = serial.Serial(port=self.com, baudrate=self.rate, timeout=2)
        self.stop_cmd = b'e'
        self.start_cmd = b'b'

        self.range = 42
        self.data_len = 42 * 60
        self.item_len = 6

        self.rpm = 0
        self.avr_rpm = 0

        atexit.register(self.stop)
        signal.signal(signal.SIGINT, lambda x, y: self.stop())
        signal.signal(signal.SIGTERM, lambda x, y: self.stop())

    def start(self):
        if not self.port.is_open:
            self.port.open()
        self.port.write(self.start_cmd)

    def stop(self):
        if not self.port.is_open:
            self.port.open()
        self.port.write(self.stop_cmd)
        self.port.close()

    def main_loop(self):
        print("Ctrl+c to stop!")
        while self.port.is_open:
            for data in self.read_data():
                print(data)
            time.sleep(0.02)
            print("-" * 30)

    def read_data(self):
        sync_flag = False
        head_flag = False

        # 搜索开始数据包  0度 数据包  OXFA 0XA0  开头为0xfa 0xa0,0xfa 0xa1,0xfa 0xa2 ...
        while not sync_flag or not head_flag:
            b = self.port.read(1)
            if b and b[0] == 0xfa:
                sync_flag = True
                head_flag = False
            elif b and sync_flag and b[0] == 0xa0:
                head_flag = True
            else:
                sync_flag = False
                head_flag = False

        # 读出剩下的数据包
        data = self.port.read(self.data_len - 2)

        data = b"\xfa\xa0" + data
        rpms = 0

        frame_ok = 0
        for index, split in enumerate(range(0, self.data_len, self.range)):
            seg = data[split:split + self.range]   # 单个42字节的数据包

            check_sum = (0xff - sum(seg[:40])) & 0xff  # 校验数据
            if seg[40] != seg[41] or seg[40] != check_sum or seg[41] != check_sum:
                print(
                    "bad frame.checksum:{}, num1:{} num2:{}".format(
                        check_sum, seg[40], seg[41]))
                continue

            if seg[0] == 0xfa and seg[1] == (
                    0xa0 + index):  # seg[0]开始标志 0xfa,    seg[1]角度下标  实际角度 = (seg[1]-0xa0) * 6 + offset
                self.rpm = ((seg[3] << 8) + seg[2]) / 10.0
                rpms += self.rpm
                frame_ok += 1

                for r_index, deg_split in enumerate(
                        range(4, 40, self.item_len)):
                    deg_data = seg[deg_split: deg_split +
                                   self.item_len]  # 单个1度 距离数据包  6个字节
                    intensity = (deg_data[1] << 8) + deg_data[0]  # 反射强度
                    length = (
                        (deg_data[3] << 8) + deg_data[2]) / 1000.0  # 距离 毫米转换为米
                    deg = (seg[1] - 0XA0) * 6 + r_index  # 角度

                    yield deg, length, intensity, self.rpm, rpms

            else:
                print("Data miss!")

        self.avr_rpm = rpms / frame_ok