from capstone import *
from capstone.x86 import *
from Instructions import *

''' Basic code, we will be generating instructions
    inside func(char*)
'''

codeC = '''
#include "environment.h"

void func() {
%s}\n
int main() {
}
'''

''' Just load bytes in desired range '''
def loadCode(filename, start, stop):
    f = open(filename, 'rb')
    code = f.read()
    return code[start:stop]

class CodeGenerator:
    def __init__(self, name, start, stop):
        self.code = loadCode(name, start, stop)
        self.cCode = ''

        # Instruction lookup
        self.cinstr = {
            'mov' : mov,
            'sub' : sub,
            'add' : add,
            'inc' : inc,
            'cmp' : cmp,
            'jne' : jne
        }

        self.jumps = [
            'jmp', 'je', 'jne', 'jz', 'jnz'
        ]

        self.jumpPlaces = {}

        # Init capstone
        self.cs = Cs(CS_ARCH_X86, CS_MODE_64)
        self.cs.detail = True
        self.instructions = [x for x in self.cs.disasm(self.code, 0)]

        self.checkJumps()

        # go go go
        self.generate()

    ''' Just fill C template '''
    def getAsC(self):
        return codeC % self.cCode

    def checkJumps(self):
        for instr in self.instructions:
            if instr.mnemonic in self.jumps:
                addr = int(instr.operands[0].imm)
                found = False
                for _instr in self.instructions:
                    if _instr.address == addr:
                        found = True
                        self.jumpPlaces[addr] = True
                        break
                if not found:
                    print("Jump to nonexisting place...\nQuitting...")
                    exit(0)

    ''' Go over each instruction in range, nothing special '''
    def generate(self):
        for inst in self.instructions:

            if inst.address in self.jumpPlaces:
                self.emit('_%x:' % (inst.address), indent=0)

            # Operands is list of ops, 0, 1, 2 (may more) ops
            ops = [x for x in inst.operands]
            args = [self.buildArg(inst, x) for x in ops]

            # get C implementation
            if inst.mnemonic not in self.cinstr:
                print("Instruction not found...\nQuitting...")
                exit(0)

            c = self.cinstr[ inst.mnemonic ](*args, inst)

            # Emit to C pattern
            self.emit(c)

            print('%x: %s %s -> %s' % (inst.address, inst.mnemonic, inst.op_str, c) )

    ''' Return type of operand (register, memory, immediate)
        and 'value' in C
    '''
    def buildArg(self, instr, operand):
        type = operand.type

        if type == X86_OP_REG:
            return (type, 'regs.' + instr.reg_name(operand.reg))

        if type == X86_OP_MEM:
            baseReg = instr.reg_name(operand.mem.base)
            displacement = operand.mem.disp
            index, scale = (operand.mem.index, operand.mem.scale)
            out = 'regs.' + baseReg
            if index:
                out += '+' + 'regs.' + instr.reg_name(index) + '*' + str(scale)
            if displacement:
                out += '+' + str(displacement)
            return (type, out)

        if type == X86_OP_IMM:
            return (type, str(operand.imm))

        raise "????"

    ''' Spaces FTW '''
    def emit(self, data, macros = '', indent = 1):
        self.cCode += '    ' * indent
        self.cCode += data + ';'
        if(len(macros)):
            self.cCode += macros + ';'
        self.cCode += '\n'


# -*- coding: utf-8 -*-
"""
Created on Fri May  3 15:55:59 2019

@author: 依蒨
"""

import nltk
from bs4 import BeautifulSoup as bs
import requests
import re
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from collections import defaultdict
from pattern.en import lemma
from nltk.corpus import brown

#nltk.download('brown')

"""
[A]

Purpose: Function for Creating Bigram Dictionary
Input: Article in form of list after removing stop words
Output: Bigram dictionary of the filtered (removing stop words) article and the frequency of each word pair
"""
def bigramDict(word_list):
    bigram_list = []
    bigram_dict = dict()

    for i in range(0, len(word_list) - 2):
        bigram_list.append([word_list[i], word_list[i + 1]])

    for (w_1, w_2) in bigram_list:

        if w_1 not in bigram_dict:
            bigram_dict[w_1] = {}

        if w_2 not in bigram_dict[w_1]:
            bigram_dict[w_1][w_2] = 1
        else:
            bigram_dict[w_1][w_2] += 1

    return bigram_dict

"""
[B]

Purpose: Function for determining the category of the website article
Input: Send lemmatized dictionary of collected text
Output: Category of the text from website
Description: If the lemmatized words in web_dict matches the lemmatized words in dictionary of
each category, scores --(frequency in the specific category / total words in the specific category) * the frequency of the word in web_dict --
will be added. The one who gives the highest score is the category of the web article.
"""
def getCategory(web_dict):

    br_categories = brown.categories()
    cat_dict = defaultdict(int)
    stop_words = set(stopwords.words('english'))

    max_score = 0 #max_score stores the current highest score of the matching process
    max_cat = '' #max_cat stores the name of category with current highest max_score

    for category in br_categories:
        cur_score = 0
        for word in brown.words(categories = category):
            if not re.search(r"[.\,:;""\-''?!]", word) and word not in stop_words:
                word = word.lower()
                word = lemma(word)
                cat_dict[word] += 1

        cat_wds = sum(cat_dict.values())

        for web_wd in web_dict.keys():
            if web_wd in cat_dict.keys():
                cur_score += (cat_dict[web_wd] / cat_wds) * web_dict[web_wd]

        if cur_score > max_score:
            max_score = cur_score
            max_cat = category

    return max_cat

"""
[C]

Crawl content from given link and stored the content into lists and dictionaries
"""

link = input('Enter a link: ')
r = requests.get(link)

# Whether or not is downloaded successfully
if r.status_code == requests.codes.ok:

    #Parse HTML code with BeautifulSoup
    soup = bs(r.text, 'html.parser')

    #Crawl the content
    cnn_tag_name = 'div.zn-body__paragraph'
    content = soup.select(cnn_tag_name)
    index = 0
    tag_pattern = re.compile('<[^>]*>') #remove all the html tags

    sym_pattern = re.compile('[.\,:;""\-''?!\d]') #remove all the symbols

    #filtered and unfiltered article; data type --> lsit
    filtered_art = []
    unfiltered_art = []
    art_sent = [] #take [SENTENCE] as a unit / UNfiltered

    #Unfiltered uni_gram freq dict
    all_words_freq = defaultdict(int)

    #Filtered uni_gram freq dict
    uni_gram_freq = defaultdict(int)

    #Lemmatized uni_gram freq dict
    lemma_uni_gram = defaultdict(int)

    stop_words = set(stopwords.words('english'))

    for paragraph in content:
        paragraph = tag_pattern.sub("", str(paragraph))

        #Count sentence number
        for sent in paragraph.split('.')[:-1]:

            sent = sent + '.'
            art_sent.append(sent) #add untokenized sentence to art_sent(list)

            #Remove stop words
            #sentence is now tokenized into words
            sent = sym_pattern.sub("", sent)
            tokenized_sent = word_tokenize(sent.lower()) #Data_type --> list
            unfiltered_art.append(tokenized_sent) #append every tokenized [SENTENCE]

            for word in tokenized_sent:
                if word not in stop_words:
                    filtered_art.append(word) #append [WORDS] to this list

    #Create unfiltered uni_gram dictionary and LEMMATIZED uni_gram freq dict
    for sent in unfiltered_art:
        for word in sent:
            if not re.search(r"[.\,:;""\-''?!]", word):
                lower_word = word.lower()
                all_words_freq[lower_word] += 1

    #Create uni_gram and lemma_uni_gram dictionary -- filtered article
    for word in filtered_art:
        if not re.search(r"[.\,:;""\-''?!]", word):
            uni_gram_freq[word] += 1
            lemma_uni_gram[lemma(word)] += 1

    art_bigram_dict = bigramDict(filtered_art)

"""
[D]

Purpose: Determine the topic of this article: Freq of unigram and bigram
Description:
Retrieve the word and word pair with the highest frequency. If the frequency of the single word equals to
that of the word pair, it is possible that a phrase rather than single words is the topic of this article.
Therefore, if this happen, set the phrase as the topic of the sentence. If not, print the one with higher frequency.

"""

uni_max_freq = max(uni_gram_freq.values())
uni_max_wrd = [k for k, v in uni_gram_freq.items() if v == uni_max_freq]

bi_max_freq = 0
for fst_wd, snd_wd_and_freq in art_bigram_dict.items():
    if max(art_bigram_dict[fst_wd].values()) > bi_max_freq:
        bi_max_freq = max(art_bigram_dict[fst_wd].values())
        for snd_wd, freq in snd_wd_and_freq.items():
            if freq == bi_max_freq:
                bi_max_wds = [fst_wd, snd_wd]

#If the frequencies of uni_gram and bi_gram are the same, the two words might co-occur

topic_str = "  ".join(uni_max_wrd) #Default
if uni_max_freq == bi_max_freq:
    for (wd_1, wd_2) in bi_max_wds:
        if not re.search(r"[.\,:;""\-''?!]", wd_1) and re.search(r"[.\,:;""\-''?!]", wd_2):
            for sent in unfiltered_art:
                wd_between = sent.index(wd_2) - sent.index(wd_1)
                if wd_between > 0 and wd_between < 5:
                    topic_str = "  ".join(sent[sent.index(wd_1):sent.index(wd_1) + 1])
"""
[E]

Call "getCategory" function.
web_cat is a variable that stores the category of this article
"""

web_cat = getCategory(lemma_uni_gram)

print("This article is about %s." % topic_str)
print("Category of this article: %s" % web_cat)


from django.db import models
from cloud.common.models import BaseCommonModel
from django.contrib.auth.models import User
import subprocess
import os

class LogicalVolume(BaseCommonModel):
    DEFAULT_VOLUME_GROUP = 'clients'
    CREATE_COMMAND = 'lvcreate -n u{user_id} -L{space_gb}G {group}'
    GET_DISK_PATH_COMMAND = "lvdisplay | egrep 'LV Name .+ {lv_name}' -B 3 |  grep 'LV Path' | awk '{{print $3}}'"
    DEFAULT_MKFS_COMMAND = 'mkfs.ext4 {path}'
    DEFAULT_MOUNT_COMMAND = 'mount {lv_path} {point}'
    DEFAULT_MOUNT_POINT = '/home/mydaq/web-apps/suckit/static_media/media/u{user_id}'

    user = models.OneToOneField(User, null=True, blank=True)
    space_gb = models.PositiveIntegerField(default=5)
    lv_path = models.CharField(max_length=300)
    mounted_to = models.CharField(max_length=300)

    volume_group = models.CharField(max_length=255, default=DEFAULT_VOLUME_GROUP)

    def sudo(self, command, display="code"):
        if display == 'output':
            command = '/usr/bin/sudo ' + command
            output = subprocess.check_output(command, stderr=subprocess.STDOUT, shell=True)
        elif display == 'code':
            command = ['/usr/bin/sudo'] + command.split()
            output = subprocess.check_call(command, stderr=subprocess.STDOUT)
        else:
            output = None

        return output

    def create_lv(self):
        parameters = {
            'user_id': self.user_id,
            'space_gb': self.space_gb,
            'group': self.volume_group
        }
        code = self.sudo(self.CREATE_COMMAND.format(**parameters), display='code')

        return code

    def mkfs_ext4(self):
        parameters = {
            'lv_name': "u{}".format(self.user_id),
        }

        lv_path = self.sudo(self.GET_DISK_PATH_COMMAND.format(**parameters), display='output')

        if lv_path:
            lv_path = lv_path.replace('\n', '')
            code = self.sudo(self.DEFAULT_MKFS_COMMAND.format(**{'path': lv_path}), display='code')
            if code == 0:
                self.lv_path = lv_path
                self.save()
                return lv_path

        return False

    def mount(self):
        if self.lv_path:
            mount_to = self.DEFAULT_MOUNT_POINT.format(**{'user_id': self.user_id})
            os.mkdir(mount_to)
            command = self.DEFAULT_MOUNT_COMMAND.format(**{
                'lv_path': self.lv_path,
                'point': mount_to
            })
            code = self.sudo(command, display='code')

            if code == 0:
                self.mounted_to = mount_to
                self.save()
                self.sudo('chown -R mydaq:mydaq {}'.format(mount_to), display='output')
                return mount_to

        return False


#-*- coding: utf8 -*-
import numpy as np
import scipy.odr.odrpack as odrpack
#from scipy.optimize import curve_fit
import matplotlib.pyplot as plt
from os import listdir
import pandas
from uncertainties import ufloat
import uncertainties.unumpy as unp
from uncertainties.umath import *
import uncertainties as uc

import sys
sys.path.insert(0,"../../scripts")
import data_manager as dm
import fit_functions as ft
from myPlot import Settings, plot, plot_multi_2, ShorthandFormatter

def get_settings():
    general_sets = Settings()

    general_sets.dataset_folder = "../datasets/"
    general_sets.dataset_file_name = "mittlere_geschwindigkeit.csv"

    general_sets.x_label = r"$X = D^2 \:(m^2)$"
    general_sets.y_label = r"$y = \langle v \rangle \: (m/s)$"

    general_sets.graph_format = ["b.","r-","y--","y--"]
    #general_sets.fitted_graph_label = "Gefittete Kurve: " + r"$y = A\:\cos(\omega t + \phi)\: e^{-\beta t}$"
    general_sets.graph_label = ["", "Linear","",""]
    general_sets.axes_label_fontsize = 20

    return general_sets

def get_data(sets):
    #reads data from datasets specified in Settings
    dataset = dm.csv_to_list(sets.dataset_folder + sets.dataset_file_name)

    distances_vals = dm.return_column(dataset, title = "d")
    distances_err = dm.return_column(dataset, title = "d_err")
    distances_u = unp.uarray(distances_vals, distances_err)

    times_vals = dm.return_column(dataset, title = "t_avg")
    times_err = dm.return_column(dataset, title = "t_dev")
    times_u = unp.uarray(times_vals, times_err)

    return distances_u/100, times_u

def linear(B,x):
    return B[0]*x+B[1]

def plot_line(sets):
    f = ft.linear2
    fit_samples = 10
    distances, times = get_data(sets)
    v_avg=distances/times
    distances2 = distances**2
    distances2_n, times_n, v_avg_n = unp.nominal_values(distances2), unp.nominal_values(times), unp.nominal_values(v_avg)
    distances2_s, times_s, v_avg_s = unp.std_devs(distances2), unp.std_devs(times), unp.std_devs(v_avg)
    print (distances2_s)

    #ODR fitting
    linear_model = odrpack.Model(linear)
    data = odrpack.RealData(distances2_n, v_avg_n, sx=distances2_s, sy=v_avg_s)
    myodr = odrpack.ODR(data,linear_model, beta0=[-1,1])
    output = myodr.run()
    params_n, params_s = output.beta, output.sd_beta
    params = unp.uarray(params_n, params_s)

    x_fit = np.linspace(0, max(distances2_n)+0.1, fit_samples)
    y_fit = linear(params_n, x_fit)
    y_fit_up = linear(params_n+params_s, x_fit)
    y_fit_down = linear(params_n-params_s, x_fit)

    #print (y_fit_up,y_fit_down, y_fit)
    fmtr = ShorthandFormatter()
    a_str = fmtr.format("{0:.1u}",params[0])
    v_str = fmtr.format("{0:.1u}",params[1])

    sets.graph_label[1] = r"$y= kX + v$" + r"$\quad mit\quad y= \langle v \rangle \ und\ X=D^2$" +"\n"+ r"$k={}\:$".format(a_str) + r"$(ms)^{-1}\ und\ $" + r"$v={}\: m/s$".format(v_str)
    #fig, ax = plot_multi_2(sets, x_values = [distances2_n,x_fit,x_fit,x_fit], y_values = [v_avg_n,y_fit,y_fit_up,y_fit_down], x_err = [distances2_s,[],[],[]], y_err = [v_avg_s,[], [],[]])
    fig, ax = plot_multi_2(sets, x_values = [distances2_n,x_fit], y_values = [v_avg_n,y_fit], x_err = [distances2_s,[]], y_err = [v_avg_s,[]])
    ax.fill_between(x_fit,y_fit_up,y_fit_down, facecolor = "r", alpha = 0.1)
    ax.set_xbound(0,2.01)
    ax.set_ybound(0.5,0.5599)
    plt.subplots_adjust(left=0.162, right = 0.975, bottom = 0.15, top = 0.99)
    #ax.spines['top'].set_visible(False)
    #ax.spines['right'].set_visible(False)

if __name__ == "__main__":
    sets = get_settings()
    plot_line(sets)

    plt.show()


# coding=utf-8

import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import linear_kernel, cosine_similarity
import scipy
from nltk.corpus import stopwords

def recommend(pois, pois_user):
    """

    :type pois_user: pandas.core.frame.DataFrame
    :type pois: pandas.core.frame.DataFrame
    """
    #pois = pd.read_json('data/poi.json') # pois from DB
    #pois_user = pd.read_json('data/poi2.json') #pois que usuário mais gostou
    pois_appended = pois.append(pois_user)

    size_pois_user = len(pois_user) * -1

    tfidf = TfidfVectorizer(stop_words=stopwords.words('portuguese'))
    tfidf_matrix = tfidf.fit_transform(pois_appended['text'])

    pois_user_sum = tfidf_matrix[size_pois_user:].sum(axis=0) #soma as linhas referentes ao usuario

    tfidf_matrix_initial = tfidf_matrix[:tfidf_matrix.shape[0] + size_pois_user] #pega as linhas sem as do usuario
    tfidf_matrix = scipy.sparse.vstack((pois_user_sum, tfidf_matrix_initial)).tocsr() #adiciona uma linha com a soma do usuarios

    #tfidf_matrix = tfidf.fit_transform(metadata['overview'])
    cosine_sim = cosine_similarity(tfidf_matrix[0], tfidf_matrix)
    #indices = pd.Series(metadata.index, index=metadata['title']).drop_duplicates()
    sim_scores = list(enumerate(cosine_sim[0]))
    sim_scores = sorted(sim_scores, key=lambda x: x[1], reverse=True)
    sim_scores = sim_scores[1:]

    list_return = []
    for s in sim_scores:
        poi_id = pois.iloc[s[0] - 1]['item_id'] #subtrai o indice por 1 ja que adicionou a linha do usuario
        list_return.append((poi_id, s[1]))
    return list_return

if __name__ == '__main__':
    pois = pd.read_json('data/poi.json')  # pois from DB
    pois_user = pd.read_json('data/poi2.json')
    result = recommend(pois,pois_user)
    print('Finished')

'''
def get_recommendations(title, cosine_sim=cosine_sim):
    # Get the index of the movie that matches the title
    idx = indices[title]

    # Get the pairwsie similarity scores of all movies with that movie
    sim_scores = list(enumerate(cosine_sim[idx]))

    # Sort the movies based on the similarity scores
    sim_scores = sorted(sim_scores, key=lambda x: x[1], reverse=True)

    # Get the scores of the 10 most similar movies
    sim_scores = sim_scores[1:11]

    # Get the movie indices
    movie_indices = [i[0] for i in sim_scores]

    # Return the top 10 most similar movies
    return metadata['title'].iloc[movie_indices]

'''


import sys

'''

기준 배열 [[2, 1], [3, 4], [1, 2], [1, 3], [3, 2]]

1. 첫번째 값 기준 정렬.
Arr.sort(key=lambda x:x[0])

결과 : [[1, 2], [1, 3], [2, 1], [3, 4], [3, 2]]
0번째 인덱스에 대해서 오름차순 정렬은 되었지만
동일한 0번째 인덱스를 가진 1번째 인덱스에 대해서는 정렬이 되지 않았음을 알 수 있다.

2. 두번째 값 기준 정렬.
Arr.sort(key=lambda x: x[1])

결과 : [[1, 3], [1, 2], [2, 1], [3, 4], [3, 2]]
여기서는 먼저 0번째 인덱스에 대해서 오름차순으로 정렬한 뒤,
동일한 값의 경우 내림차순으로 재정렬된다.

3. 기본 정렬.
Arr.sort()

결과 : [[1, 2], [1, 3], [2, 1], [3, 2], [3, 4]]
위에서 했던 작업이 무색하게 0번째 인덱스를 오름차순 정렬하고,
같은 값의 경우 오름차순으로 재정렬까지 시켜준다

4. 두번째 값 기준 정렬 후 같은 값은 첫번째 값 기준으로 정렬.
Arr.sort(key=lambda x: (x[1], x[0]))

여기서는 먼저 두번째 인덱스에 대해서 오름차순으로 정렬한 뒤,
동일한 값의 경우 오름차순으로 재정렬된다.
'''

N = int(sys.stdin.readline())
Arr = [[0]*2 for _ in range(N)]

for i in range(N):
    Arr[i] = list(map(int, sys.stdin.readline().split()))

Arr.sort(key=lambda x: (x[1], x[0]))

for i in range(N):
    print(Arr[i][0], Arr[i][1])


#!/usr/bin/env python3
# -*- encoding: utf-8 -*-

# Imports {{{
import sys
from random import choice as rndchoice
from random import shuffle
from itertools import groupby
from time import sleep

from utils import nextval, first, cmp, iround, nextgroup, flatten
from board import Board, Loc, BaseTile
from commands import BaseCommands
from avkutil import Term

size         = 12
player_chars = '▣⎔'
# ai_players   = '▣⎔'
ai_players   = ''
blank        = '.'
padding      = 4, 2
pause_time   = 0.3
blink_speed  = 0.1
nl           = '\n'

commands     = {
                'a' : "left",
                'd' : "right",
                'w' : "up",
                's' : "down",
                't' : "toggle",
                '\n': "move",
                ' ' : "move",
                'q' : "quit",
                }

# }}}

class Commands(BaseCommands):
    def move(self):
        board = self.board
        loc = board.current
        if board.valid_move(self.player, loc):
            board.hl_visible = False
            return loc
        else:
            print("Invalid move")

class PlayerBase(object):
    def __eq__(self, other):
        return self.char == getattr(other, "char", None)

    def __ne__(self, other):
        return not self==other

class Tile(BaseTile, PlayerBase):
    blank = piece = False

    def __repr__(self):
        return blank if self.blank else self.char

class Blank(Tile):
    char = blank

class Piece(Tile):
    def __init__(self, loc=None, char=None):
        super(Piece, self).__init__(loc)
        self.char = char
        if loc:
            board[loc] = self

    def flip(self):
        self.char = nextval(player_chars, self.char)

class VersiBoard(Board):
    scores_msg = "%s  score: %3s    %s  score: %3s  "

    def __init__(self, *a, **kw):
        super().__init__(*a, **kw)
        self.current = Loc(0,0)
        self.hl_visible = False

    def get_valid_moves(self, player):
        return [loc for loc in self.locations() if self.valid_move(player, loc)]

    def valid_move(self, player, loc):
        return bool(self.get_captured(player, loc))

    def get_captured(self, player, start_loc):
        """If `start_loc` is a valid move, returns a list of locations of captured pieces."""
        if not self[start_loc].blank:
            return []

        getdir = self.capture_direction
        return flatten( [getdir(player, start_loc, dir) for dir in self.dirlist2] )

    def capture_direction(self, player, start, dir):
        """Return the list of enemy tiles to capture in the `dir` direction from `start` location."""
        groups = groupby(self.ray(start, dir))
        group1, group2 = nextgroup(groups), nextgroup(groups)
        if group1 and group2 and (group1.key == player.enemy() and group2.key == player):
            return group1.group
        else:
            return []

    def is_corner(self, loc):
        return loc.x in (0, self.width-1) and loc.y in (0, self.height-1)

    def status(self):
        print(self.scores_msg % (player1, player1.score(), player2, player2.score()), end='')
        sys.stdout.flush()

    def middle(self):
        return iround(self.width/2) - 1, iround(self.height/2) - 1

class Player(PlayerBase):
    def __init__(self, char):
        self.char = char
        self.ai   = char in ai_players

    def __repr__(self):
        return self.char

    def score(self):
        return sum(tile==self for tile in board)

    def enemy(self):
        return nextval(players, self)

    def make_move(self, loc):
        tiles = board.get_captured(self, loc)
        for tile in tiles:
            tile.flip()
        Piece(loc, self.char)
        return [board[loc]] + tiles

    def get_random_move(self):
        """Return location of best move."""
        def by_corner_score(loc):
            return board.is_corner(loc), -len(board.get_captured(self, loc))

        moves = board.get_valid_moves(self)
        shuffle(moves)
        return first(sorted(moves, key=by_corner_score))

class Versi(object):
    winmsg     = "%s wins!"
    tiemsg     = "The game was a tie!"

    def __init__(self):
        x, y = board.middle()
        Piece(Loc(x,y), player1.char)
        Piece(Loc(x+1, y+1), player1.char)
        Piece(Loc(x+1, y), player2.char)
        Piece(Loc(x, y+1), player2.char)

    def game_end(self):
        board.draw()
        winner = cmp(player1.score(), player2.score())
        if not winner:
            print(nl, self.tiemsg)
        else:
            print(nl, self.winmsg % (player1 if winner>0 else player2))
        sys.exit()

class BasicInterface(object):
    def run(self):
        self.term      = Term()
        moves          = board.get_valid_moves
        player         = rndchoice(players)
        player         = first(players)

        while True:
            board.draw()
            move = player.get_random_move() if player.ai else self.get_move(player)
            tiles = player.make_move(move)
            self.blink_tiles(tiles)

            # give next turn to enemy OR end game if no turns left, FALLTHRU: current player keeps the turn
            if moves(player.enemy()):
                player = player.enemy()
            elif not moves(player):
                versi.game_end()

    def get_move(self, player):
        commands.player = player
        while True:
            val = self.term.getch()
            try:
                val = commands[val]()
                if val:
                    return val
            except KeyError:
                print("Invalid move")

    def blink_tiles(self, tiles):
        for _ in range(2):
            for tile in tiles:
                tile.blank = not tile.blank
            sleep(blink_speed)
            board.draw()

if __name__ == "__main__":
    board            = VersiBoard(size, Blank, num_grid=False, padding=padding, pause_time=pause_time)
    players          = [Player(c) for c in player_chars]
    player1, player2 = players
    versi            = Versi()
    commands         = Commands(board, commands)

    try:
        BasicInterface().run()
    except KeyboardInterrupt:
        sys.exit()


from django.urls import path
from . import views

urlpatterns = [
    path('adminprofile', views.adminprofile, name='adminprofile'),
    path('adminindex',views.adminindex,name='adminindex'),
    path('viewdetails', views.viewdetails, name='viewdetails'),
    path('candidateupdate/<int:id>', views.candidateupdate, name='candidateupdate'),
    path('companyview', views.companyview, name='companyview'),
    path('logout',views.logout,name='logout'),
    path('companyupdate/<int:id>', views.companyupdate, name='companyupdate'),
    path('deletecan/<int:id>', views.deletecan, name='deletecan'),
    path('deletecmp/<int:id>',views.deletecmp,name='deletecmp'),
    path('vacancyview', views.vacancyview, name='vacancyview'),
    path('moreinfo/<int:id>',views.moreinfo,name='moreinfo'),
    path('vacupdate/<int:id>',views.vacupdate,name='vacupdate'),
    path('deletevac/<int:id>',views.deletevac,name='deletevac')

]


from __future__ import absolute_import, division, generators, nested_scopes, print_function, unicode_literals, with_statement

from django.contrib.auth import login
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect, Http404
from django.shortcuts import get_object_or_404
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _

from ..forms.clubs import ClubForm, ClubFilterForm, ClubRegistrationForm, ClubRegistrationPublicForm, ClubJournalEntryForm, ClubJournalLeaderEntryForm
from ..models import Club, ClubPeriod, ClubJournalEntry, ClubJournalLeaderEntry, ClubRegistration, Participant

from .generic import FilteredListView, DetailView, CreateView, UpdateView, ConfirmUpdateView, DeleteView, TemplateView, PdfView

class ClubListView(FilteredListView):
    model               = Club
    form_class          = ClubFilterForm
    preview_template    = 'domecek/club_preview.html'
    template_name       = 'domecek/club_list.html'
    message_empty       = _('No clubs matching given filter.')
    paginate_by         = 10

    def get_title(self):
        return _('Clubs in school year {}').format(self.request.school_year)

    def get_queryset(self):
        qs = super(ClubListView, self).get_queryset()
        qs = qs.filter(school_year=self.request.school_year)
        if not self.request.user.is_staff:
            qs = qs.filter(public=True)
        form = self.get_form()
        if form.is_valid():
            qs = form.filter_queryset(self.request, qs)
        return qs

class ClubListMineView(ClubListView):
    def get_queryset(self):
        return super(ClubListMineView, self).get_queryset().filter(leaders=self.request.leader)

    def get_title(self):
        return _('My clubs in school year {}').format(self.request.school_year)

class ClubAlternatingView(TemplateView):
    template_name       = 'domecek/club_alternating.html'

    def get_title(self):
        return _('Alternating in school year {}').format(self.request.school_year)

    def get_context_data(self, **kwargs):
        context = super(ClubAlternatingView, self).get_context_data(**kwargs)
        context['alternate_leader_entries'] = self.request.leader.get_alternate_leader_entries(self.request.school_year)
        return context

class ClubDetailView(DetailView):
    model = Club

    def get_queryset(self):
        qs = super(ClubDetailView, self).get_queryset()
        if not self.request.user.is_staff:
            qs = qs.filter(public=True)
        return qs

class ClubParticipantsView(DetailView):
    model = Club
    template_name_suffix = '_participants'

    def get_queryset(self):
        qs = super(ClubParticipantsView, self).get_queryset()
        if not self.request.user.is_staff:
            qs = qs.filter(leaders=self.request.leader)
        return qs

class ClubJournalView(DetailView):
    model = Club
    template_name_suffix = '_journal'

    def get_queryset(self):
        qs = super(ClubJournalView, self).get_queryset()
        if not self.request.user.is_staff:
            qs = qs.filter(leaders=self.request.leader)
        return qs

class ClubUpdateView(UpdateView):
    model       = Club
    form_class  = ClubForm
    title = _('Change club')

    def get_queryset(self):
        qs = super(ClubUpdateView, self).get_queryset()
        if not self.request.user.is_staff:
            qs = qs.filter(leaders=self.request.leader)
        return qs

    def get_message(self, form):
        return _('The club {} has been updated.').format(self.object)

class ClubJournalEntryCreateView(CreateView):
    model       = ClubJournalEntry
    form_class  = ClubJournalEntryForm
    template_name = 'domecek/clubjournalentry_form.html'
    title = _('New journal entry')

    def dispatch(self, request, *args, **kwargs):
        if self.request.user.is_staff:
            self.club = get_object_or_404(Club,
                id = int(kwargs.pop('club')),
            )
        else:
            self.club = get_object_or_404(Club,
                id = int(kwargs.pop('club')),
                leaders = self.request.leader,
            )
        return super(ClubJournalEntryCreateView, self).dispatch(request, *args, **kwargs)

    def get_form_kwargs(self):
        kwargs = super(ClubJournalEntryCreateView, self).get_form_kwargs()
        kwargs['club'] = self.club
        return kwargs

    def get_message(self, form):
        return _('The journal entry has been created.')

class ClubJournalEntryUpdateView(UpdateView):
    model       = ClubJournalEntry
    form_class  = ClubJournalEntryForm
    template_name = 'domecek/clubjournalentry_form.html'
    title = _('Change journal entry')

    def get_object(self):
        obj = super(ClubJournalEntryUpdateView, self).get_object()
        if (self.request.user.is_staff
            or self.request.leader in obj.club.all_leaders + obj.all_alternates):
            return obj
        else:
            raise Http404()

    def get_message(self, form):
        return _('The journal entry has been updated.')

class ClubJournalEntryDeleteView(DeleteView):
    model = ClubJournalEntry
    title = _('Delete journal entry')

    def get_queryset(self):
        return super(ClubJournalEntryDeleteView, self).get_queryset().filter(
            club__leaders = self.request.leader,
        )

    def get_object(self):
        obj = super(ClubJournalEntryDeleteView, self).get_object()
        if obj.timesheets.filter(submitted = True).exists():
            raise Http404()
        return obj

    def get_question(self):
        return _('Do You really want to delete club journal entry?')

    def get_message(self):
        return _('The journal entry has been deleted.')

class ClubJournalLeaderEntryUpdateView(UpdateView):
    model       = ClubJournalLeaderEntry
    form_class  = ClubJournalLeaderEntryForm
    template_name = 'domecek/clubjournalleaderentry_form.html'
    title = _('Change timesheet entry')

    def get_object(self):
        obj = super(ClubJournalLeaderEntryUpdateView, self).get_object()
        if self.request.user.is_staff \
        or obj.timesheet.leader == self.request.leader \
        or self.request.leader in obj.club_entry.club.all_leaders:
            return obj
        else:
            raise Http404()

    def get_message(self, form):
        return _('The timesheet entry has been updated.')

class ClubJournalLeaderEntryDeleteView(DeleteView):
    model = ClubJournalLeaderEntry
    title = _('Delete timesheet entry')

    def get_queryset(self):
        return super(ClubJournalLeaderEntryDeleteView, self).get_queryset().filter(
            timesheet__leader = self.request.leader,
            timesheet__submitted = False,
        )

    def get_question(self):
        return _('Do You really want to delete timesheet entry?')

    def get_message(self):
        return _('The timesheet entry has been deleted.')

class ClubRegistrationPublicFormView(CreateView):
    model           = ClubRegistration
    form_class      = ClubRegistrationPublicForm
    template_name   = 'domecek/registration_form.html'

    def get_title(self):
        return _('Registration for club {}').format(self.club.name)

    def dispatch(self, request, *args, **kwargs):
        club_kwargs = {
            'id':           int(kwargs.pop('club')),
            'school_year':  self.request.school_year,
            'reg_active':   True,
        }
        if not self.request.user.is_staff:
            club_kwargs['public'] = True
        self.club = get_object_or_404(Club, **club_kwargs)
        if self.request.user.is_authenticated() and not self.request.toolbar.use_draft:
            return HttpResponseRedirect(reverse('domecek:club_detail', args=(self.club.id, )))
        return super(ClubRegistrationPublicFormView, self).dispatch(request, *args, **kwargs)

    def get_form_kwargs(self):
        kwargs  = super(ClubRegistrationPublicFormView, self).get_form_kwargs()
        kwargs['club'] = self.club
        return kwargs

    def form_valid(self, form):
        response = super(ClubRegistrationPublicFormView, self).form_valid(form)
        user = form.instance.participant.user
        user.backend = 'django.contrib.auth.backends.ModelBackend'
        login(self.request, user)
        return response

    def get_message(self, form):
        return _('The registration has been accepted.')

class ClubRegistrationFormView(CreateView):
    model       = ClubRegistration
    form_class  = ClubRegistrationForm

    def get_title(self):
        return _('Registration for club {}').format(self.club.name)

    def dispatch(self, request, *args, **kwargs):
        club_kwargs = {
            'id':           int(kwargs.pop('club')),
            'school_year':  self.request.school_year,
            'reg_active':   True,
        }
        if not self.request.user.is_staff:
            club_kwargs['public'] = True
        self.club = get_object_or_404(Club, **club_kwargs)
        self.participant = get_object_or_404(Participant,
            user    = self.request.user,
            id      = int(kwargs.pop('participant')),
        )
        # user may get back to this page after successful registration
        if self.club.registrations.filter(participant=self.participant).exists():
            return HttpResponseRedirect(reverse('domecek:summary'))
        return super(ClubRegistrationFormView, self).dispatch(request, *args, **kwargs)

    def get_form_kwargs(self):
        kwargs = super(ClubRegistrationFormView, self).get_form_kwargs()
        kwargs['club']          = self.club
        kwargs['participant']   = self.participant
        return kwargs

    def get_message(self, form):
        return _('The registration has been accepted.')

class ClubRegistrationConfirmView(DetailView):
    model = ClubRegistration
    template_name_suffix = '_confirm'

class ClubRegistrationPdfView(PdfView):
    model = ClubRegistration
    template_name_suffix = '_pdf'

class ClubRegistrationCancelView(ConfirmUpdateView):
    model   = ClubRegistration
    title   = _('Cancellation request')

    def get_queryset(self):
        return super(ClubRegistrationCancelView, self).get_queryset().filter(participant__user=self.request.user)

    def get_question(self):
        return _('Are you sure You want to cancel the registration "{}"?').format(self.object)

    def get_message(self, form):
        return _('The cancellation request for {} has been saved.').format(self.object)

    def confirmed(self):
        self.object.cancel_request = True
        self.object.save()


from app.database.engine import Memberships, sess
from app.database.schema import Membership as Schema
from app.database.model import TableModel
from app.services.Service import Service
from sqlalchemy.exc import SQLAlchemyError, DBAPIError

Session = sess()

# table = TableModel(Memberships)

class MembershipServiceTable(TableModel):
  def get_filtered_records(self):
    return Session.query(self._table).filter(
      self._table.is_enabled == True,
      self._table.is_test == False
      ).all()

class MembershipService(Service):
  def get_filtered_records(self):
    records = self._table.get_filtered_records()
    result = self._schema_many.dump(records)
    return result

  def delete_record(self, uuid):
    record = self._table.get_record(uuid)
    try:
      self._table.delete_record(record)
      self._table.commit_record()
    except SQLAlchemyError as err:
      raise(err)
    else:
      response = {"response": "success"}
    return response

table = MembershipServiceTable(Memberships)

MembershipsServiceModel = MembershipService(table, Schema)


import os
import cv2
import time
import numpy as np

import skimage
from scipy.io import loadmat
import matplotlib.pyplot as plt

DEBUG = True

(H, W, C) = (220, 180, 33)

workpath = 'F:/hyperspectral_face/'
sourcedir = 'SampleImages_F'
traindir = 'SampleImages_F_train'
testdir = 'SampleImages_F_test'

def get_time():
    return time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())

def load_train(dsize=None):
    """ 读取训练集数据
    """
    datadir = traindir
    n_subjects = 25; n_sessions = 10 * 3
    n_samples = n_subjects * n_sessions	# 1500

    datapath = os.path.join(workpath, datadir)
    if not os.path.exists(datapath): print("directory doesn't exist!"); return

    if dsize is not None: (w, h) = dsize
    else: (w, h) = (W, H)

    X_train = np.zeros(shape=(n_samples, h, w, C + 1))
    y_train = np.zeros(shape=(n_samples))
    for i_subjects in range(n_subjects):
        for i_sessions in range(n_sessions):
            dataname = os.path.join(
                datapath, "F_{:d}_{:d}.npy".\
                            format(i_subjects + 1, i_sessions))
            i_samples = i_subjects * 20 + i_sessions
            data = np.load(dataname)
            if dsize is not None: data = resize(data, dsize)
            X_train[i_samples] = data
            y_train[i_samples] = i_subjects
        print_log = get_time() + ' subject {} loaded!'.format(i_subjects + 1)
        print(print_log)
    return X_train, y_train

def load_test(dsize=None):
    """ 读取测试集数据
    """
    datadir = testdir
    n_subjects = 25; n_samples = n_subjects

    datapath = os.path.join(workpath, datadir)
    if not os.path.exists(datapath): print("directory doesn't exist!"); return

    if dsize is not None: (w, h) = dsize
    else: (w, h) = (W, H)

    X_test = np.zeros(shape=(n_samples, h, w, C + 1))
    y_test = np.zeros(shape=(n_samples))
    for i_subjects in range(n_subjects):
        dataname = os.path.join(
                datapath, "F_{:d}.npy".\
                        format(i_subjects + 1))
        data = np.load(dataname)
        if dsize is not None: data = resize(data, dsize)
        X_test[i_subjects] = data
        y_test[i_subjects] = i_subjects
        print_log = get_time() + ' subject {} loaded!'.format(i_subjects + 1)
        print(print_log)
    return X_test, y_test

def make_trainset():
    """ 将1-25的样本前3个样本扩增
    Notes:
        - 高光谱数据: `HyperFaceCube_F_<subIdx>_<sessIdx>.mat`
        - 灰度图数据: `F_<subIdx>_<sessIdx>.jpg`
        - 扩增后数据: `F_<subIdx>_<sessIdx>.npy`
        - 归一化至(0, 1)范围内
    """
    srcdir = sourcedir
    dstdir = traindir
    n_subjects = 25; n_sessions = 3; n_augments = 10
    prop = 0.05; degree = 10

    srcpath = os.path.join(workpath, srcdir)
    dstpath = os.path.join(workpath, dstdir)
    if not os.path.exists(srcpath): print("source directory doesn't exist!"); return
    if not os.path.exists(dstpath): os.mkdir(dstpath)

    for i_subjects in range(n_subjects):
        for i_sessions in range(n_sessions):
            matpath = os.path.join(
                        srcpath, 'HyperFaceCube_F_{:d}_{:d}.mat'.format(i_subjects + 1, i_sessions + 1))
            matdata = loadmat(matpath)['SpecCube_F']
            jpgpath = os.path.join(
                        srcpath, 'F_{:d}_{:d}.jpg'.format(i_subjects + 1, i_sessions + 1))
            jpgdata = cv2.imread(jpgpath, cv2.IMREAD_GRAYSCALE)
            matdata = matdata / 0xffff; jpgdata = jpgdata / 0xff
            data = np.dstack((jpgdata, matdata))
            for i_augments in range(n_augments):
                data_i = data.copy()
                dataname = "F_{:d}_{:d}.npy".\
                        format(i_subjects + 1, i_sessions * n_augments + i_augments)
                data_i = flip(data_i)               # 以0.5几率翻转
                data_i = perspective(data_i, prop)  # 以一定范围拉伸
                data_i = rotate(data_i, degree)     # 以±10°旋转
                if DEBUG: plt.figure('datacube'); plt.imshow(data_i[:, :, 0]); plt.show()
                else: np.save(os.path.join(dstpath, dataname), data_i)
            print_log = get_time() + ' subject {}, session {} finished!'.format(i_subjects + 1, i_sessions + 1)
            print(print_log)

def make_testset():
    """ 将1-25的样本第4个样本保存为测试集
    Parameters:
    Notes:
        - 高光谱数据: `HyperFaceCube_F_<subIdx>_<sessIdx>.mat`
        - 灰度图数据: `F_<subIdx>_<sessIdx>.jpg`
        - 保存为数据: `F_<subIdx>_<sessIdx>.npy`
        - 归一化至(0, 1)范围内
    """
    srcdir = sourcedir
    dstdir = testdir
    n_subjects = 25; i_sessions = 1

    srcpath = os.path.join(workpath, srcdir)
    dstpath = os.path.join(workpath, dstdir)
    if not os.path.exists(srcpath): print("source directory doesn't exist!"); return
    if not os.path.exists(dstpath): os.mkdir(dstpath)

    for i_subjects in range(n_subjects):
        dataname = "F_{:d}.npy".\
                        format(i_subjects + 1)
        matpath = os.path.join(
                        srcpath, 'HyperFaceCube_F_{:d}_{:d}.mat'.format(i_subjects + 1, i_sessions + 1))
        matdata = loadmat(matpath)['SpecCube_F']
        jpgpath = os.path.join(
                        srcpath, 'F_{:d}_{:d}.jpg'.format(i_subjects + 1, i_sessions + 1))
        jpgdata = cv2.imread(jpgpath, cv2.IMREAD_GRAYSCALE)
        matdata = matdata / 0xffff; jpgdata = jpgdata / 0xff
        data = np.dstack((jpgdata, matdata))
        if DEBUG: plt.figure('datacube'); plt.imshow(data[:, :, 0]); plt.show()
        else: np.save(os.path.join(dstpath, dataname), data)
        print_log = get_time() + ' subject {}, session {} finished!'.format(i_subjects + 1, i_sessions + 1)
        print(print_log)

def show_cube(datacube):
    """ 显示一组样本的各个通道数据
    """
    (h, w) = datacube.shape[:2]; dtype = datacube.dtype
    imgshow = np.zeros(shape=(6*h, 6*w), dtype=dtype)
    for i in range(C + 1):
        channel = datacube[:, :, i]
        idxH = i // 6; idxW = i % 6
        imgshow[idxH*h:(idxH+1)*h, idxW*w:(idxW+1)*w] = channel
    plt.figure('datacube')
    plt.imshow(imgshow)
    plt.show()

def read_filename(dirname, type=None):
    """ 读取文件夹中的文件名，保存在该目录下`<dirname>.txt`中
    Parameters:
        dirname {str}: name of directory
        type {str}: 'mat', 'jpg', 'npy'
    """
    dirpath = os.path.join(workpath, dirname)
    if not os.path.exists(dirpath): print("directory doesn't exist!"); return

    if type is not None:
        txtpath = os.path.join(workpath, dirname, '{}_{}.txt'.format(dirname, type))
    else:
        txtpath = os.path.join(workpath, dirname, '{}.txt'.format(dirname))
    filenames = os.listdir(dirpath)

    if not DEBUG:
        f = open(txtpath, 'w')
        for filename in filenames:
            if type is None:
                f.write(filename + '\n')
            elif filename.split('.')[1] == type:
                f.write(filename + '\n')
        f.close()

def resize(image, dsize):
    """
    Parameters:
        image {ndarray(H, W, C)}
        dsize {tuple(w, h)}
    Notes:
        注意会调换h和w
    """
    return cv2.resize(image, dsize)
def flip(image):
    """
    Parameters:
        image {ndarray(H, W, C)}
    """
    rand_var = np.random.random()
    image = image[:, ::-1, :] if rand_var > 0.5 else image
    return image
def rotate(image, degree):
    """
    Parameters:
        image {ndarray(H, W, C)}
        degree {float}
    """
    (h, w) = image.shape[:2]
    center = (w // 2, h // 2)
    random_angel = np.random.randint(-degree, degree)
    M = cv2.getRotationMatrix2D(center, random_angel, 1.0)
    image = cv2.warpAffine(image, M, (w, h))
    return image
def perspective(image, prop):
    """ 透射变换
    Parameters:
        image {ndarray(H, W, C)}
        prop {float}: 在四个顶点多大的方格内选取新顶点，方格大小为(H*prop, W*prop)
    Notes:
        在四个顶点周围随机选取新的点进行仿射变换，四个点对应左上、右上、左下、右下
    """
    (h, w) = image.shape[:2]

    ptsrc = np.zeros(shape=(4, 2))
    ptdst = np.array([[0, 0], [0, w], [h, 0], [h, w]])
    for i in range(4):
        hr = np.random.randint(0, int(h*prop))
        wr = np.random.randint(0, int(w*prop))
        if i == 0:
            ptsrc[i] = np.array([hr, wr])
        elif i == 1:
            ptsrc[i] = np.array([hr, w - wr])
        elif i == 2:
            ptsrc[i] = np.array([h - hr, wr])
        elif i == 3:
            ptsrc[i] = np.array([h - hr, w - wr])
    M = cv2.getPerspectiveTransform(ptsrc.astype('float32'), ptdst.astype('float32'))
    image = cv2.warpPerspective(image, M, (w, h))
    return image
def brightcontrast(image, brtadj=0, cstadj=1.0):
    """ adjust bright and contrast value
    Parameters:
        image {ndarray(H, W, C)} dtype =
        brtadj {int}    adjust bright
        cstadj {float}  adjust contrast
    """
    # dtype = image.dtype
    # image = image.astype('int32')*cstadj + brtadj
    # image = np.clip(image, 0, 0xFFFF).astype(dtype)
    # return image
    image = image*cstadj + brtadj
    image = np.clip(image, 0, 1.0)
    return image

if __name__ == '__main__':
    # read_filename("SampleImages_F", 'mat')
    # read_filename("SampleImages_F", 'jpg')
    make_trainset()
    # read_filename("SampleImages_F_train")
    make_testset()
    # read_filename("SampleImages_F_test")
    # X_train, y_train = load_train()
    # X_test,  y_test  = load_test()
    pass


#!/usr/bin/python3

flashes = [0]
octopi = []
with open('11/input.txt', 'r') as f:
    for line in f.readlines():
        row = []
        for r in line.strip():
            row.append(int(r))
        octopi.append(row)
m = len(octopi)
n = len(octopi[0])

def printer():
    for row in octopi:
        s = ''
        for c in row:
            s += str(c)
        print(s)
    print()

def flashed(i, j):
    if octopi[i][j] == 0:
        return
    else:
        octopi[i][j] += 1
        if octopi[i][j] > 9:
            flash(i, j)

def flash(i, j):
    flashes[0] += 1
    octopi[i][j] = 0
    if i > 0:
        flashed(i-1, j)
        if j > 0:
            flashed(i-1, j-1)
        if j < n-1:
            flashed(i-1, j+1)
    if i < m-1:
        flashed(i+1, j)
        if j > 0:
            flashed(i+1, j-1)
        if j < n-1:
            flashed(i+1, j+1)
    if j > 0:
        flashed(i, j-1)
    if j < n-1:
        flashed(i, j+1)

print('Before any steps:')
printer()

steps = 100
for step in range(1, steps+1):
    for i in range(0, m):
        for j in range(0, n):
            octopi[i][j] += 1

    for i in range(0, m):
        for j in range(0, n):
            if octopi[i][j] > 9:
                flash(i, j)

    print('After step 1:')
    printer()
print(flashes)


#已初步掌握Python3语法，本课程以Python3语法为基础
#GUI编程的三个核心问题
#--1、屏幕上应县是那些组建？认识组件的形态
#--2、组件应该放在哪里？学习组件布局
#--3、组件如何交互？组件中的事件
#Tkinter8.5参考手册手册http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/index.html
#Tkinter 8.5 reference: a GUI for Python https://anzeljg.github.io/rin2/book2/2405/docs/tkinter/index.html
#Python 3.9.0 标准库参考https://www.bookstack.cn/read/python3.9-library-zh/a61d1812159c97a1.md
import tkinter as tk

def onclick():
    #一个回调函数
    print("我被电击了")
    print("onclick是一个",type(onclick))

root =tk.Tk() #“画板”----根窗口

label_l = tk.Label(root, text="这是一个标签")
label_l.pack() # 把标签放到主画板上

button_b =tk.Button(root, text="这是一个按钮", command=onclick)#这里的onclick是一个<class 'function'>,函数名称，不写括号
button_b.pack() # 把按钮放到主画板上

root.mainloop() # GUI程序一定要有个主循环


from Class.Migratition import Migration
#from Class.ConnectionHandler import ConnectionHandler
migration=Migration()
print(migration.getPath())
migration.getListPath()
migration.makeMigration()

#handle=ConnectionHandler("LAPTOP-IU8SOAQU\SQLEXPRESS","pythonBD","loboCGV","detonador")

#print(handle.connect())
#print(handle.executeQuery("select * from table1").fetchall())


import random
import easygui as g
import sys
g.msgbox('嗨，欢迎进入第一个界面游戏^_^')
secret = random.randint(1,10)
msg = '不妨猜一下小甲鱼现在心里想的是哪个数字(1-10):'
title = '猜数字小游戏'
guess = g.integerbox(msg,title,lowerbound=1,upperbound=10)
try:
      while True:
            if guess == secret:
                  g.msgbox('哇哦，你是小甲鱼心里的蛔虫吗？！')
                  g.msgbox('哼，猜中了也没奖励！')
                  msg = '你希望重新开始小游戏吗？'
                  title = '请选择'
                  choices = ('是','否')
                  if g.ccbox(msg,title,choices):
                        secret = random.randint(1,10)
                        msg = '不妨猜一下小甲鱼现在心里想的是哪个数字(1-10):'
                        title = '猜数字小游戏'
                        guess = g.integerbox(msg,title,lowerbound=1,upperbound=10)
                  else:
                        sys.exit(0)
            else:
                  if guess > secret:
                        g.msgbox('哥，大了大~~~')
                  else:
                        g.msgbox('黑，小了小小了~~~')
                  guess = g.integerbox(msg,title,lowerbound=1,upperbound=10)
except:
	print('结束了')


################
#Fibonnaci
################
L = []
a = 1
b = 1
for i in range(100):
    c=a+b
    a=b
    b=c

    somme = 0

    for chars in str(c):
        somme += int(chars)

    print("Fibonacci no "+str(i+3)+" : "+str(c)+" sum:"+str(somme))


import numpy as np
from hff import get_hff
from get_freqs import get_freqs
from create_data import create_data
from chisquared_stuff import get_significance
from chi_squared import get_prob
import csv
from scipy import signal
test_stds     = np.arange(0, 0.5, 0.01)
test_freqs    = np.arange(2, 100, 0.5)
time_interval = 0.001
total_time  = 1
csv_freqs = []
csv_ampl  = []
csv_std   = []
csv_dfreq = []
csv_prob  = []
csv_unc   = []

for test_freq in test_freqs:
    for std in test_stds:
        print(f"{test_freq} with {std}")
        amp       = (3 * np.sqrt(2)) / (test_freq * np.pi)  # calculate amplitude of functin with default values
        sig,sig1    = create_data(m_phi=test_freq * np.pi, deviation=std, time_interval=time_interval)
        corr = signal.correlate(sig, sig1, mode='full', method='fft')
        domain     = np.arange(0, total_time, time_interval)
        corr1 = np.array([domain,corr])
        try:
            idx, freq = get_hff(corr)
            if idx % 2 == 0:
                idx = idx // 2 # again, idk why this is but my guess is that it is because i take the real fft in hff

            fhat, _, _ = get_freqs(signal, 0.5)
            prob       = get_significance(fhat, idx)
            unc        = np.sqrt((prob * (1 - prob)) / len(fhat))

            # Previous method
            # prob, unc = get_prob(signal, idx)

        except Exception:
            freq = -1
            prob = -1
            unc  = -1

        csv_freqs.append(test_freq)
        csv_ampl.append(amp)
        csv_std.append(std)
        csv_dfreq.append(freq)
        csv_prob.append(prob)
        csv_unc.append(unc)

with open('general_tests_cross_correlation.csv', mode='w') as test_results:
    writer = csv.writer(test_results, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)

    writer.writerow(['Real frequency', 'Amplitude', 'Error deviation', 'Detected frequency', 'Chi2 prob', 'Uncertainty'])
    for i in range(len(csv_freqs)):
        writer.writerow([csv_freqs[i], csv_ampl[i], csv_std[i], csv_dfreq[i], csv_prob[i], csv_unc[i]])


import os
import gdown
import tarfile
import imageio
from pathlib import Path
from shutil import copyfile

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from matplotlib.backends.backend_agg import FigureCanvasAgg

from torchviz import make_dot
from torch.utils.data import DataLoader, dataset

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

import torchvision.models as models
import torchvision.datasets as ds
import torchvision.transforms as trans

from poutyne import Callback, Dict, CSVLogger, Experiment

os.environ["CUDA_VISIBLE_DEVICES"]="0"

__result_dir__ = 'results'

def save_model(model, file_path):
    """save_model saves the trained model in the specified path

    Args:
        model (nn.Module): the model to be saved.
        file_path (str): file path.
    """

    torch.save(model, file_path)

def load_model(file_path):
    """load_model loads the saved model from specified path.

    Args:
        file_path (str): file path to the trained model.

    Returns:
        nn.Module: the trained model.
    """
    return torch.load(file_path)

def get_torch_device(): return torch.device(
    'cuda:0' if torch.cuda.is_available() else 'cpu')

is_cuda_available = lambda: torch.cuda.is_available()

def get_cub200(dir_path: str):
    # Dowload the dataset
    url = 'https://drive.google.com/uc?id=1GDr1OkoXdhaXWGA8S3MAq3a522Tak-nx'
    dataset_file = 'images.tgz'

    print('Create required directories ...')
    # Make the original dataset directories
    Path(dir_path).mkdir(parents=True, exist_ok=True)
    ds_zipfile = os.path.join(dir_path, dataset_file)

    if not os.path.exists(ds_zipfile):
        print('Download the dataset zip file ...')
        gdown.download(url, output=ds_zipfile, quiet=False)

    # Extract the tgz file into the dataset directory
    print('Extract the zip file ...')
    tar = tarfile.open(ds_zipfile, "r:gz")
    tar.extractall(path=dir_path)
    tar.close()

    return os.path.join(dir_path, 'images')

def prepare_subsets(orig_ds_dir : str, target_dir : str):
    # Make the dataset directory
    training_ds_dir = os.path.join(target_dir, 'training')
    testing_ds_dir = os.path.join(target_dir, 'testing')

    if os.path.isdir(orig_ds_dir):
        print('Make required folder for the dataset ...')
        Path(training_ds_dir).mkdir(parents=True, exist_ok=True)
        Path(testing_ds_dir).mkdir(parents=True, exist_ok=True)
        # Prepare the training and testing subsets
        print('Preparing the training and testing subsets ...')
        separate_train_test(orig_ds_dir, training_ds_dir, testing_ds_dir)

    return training_ds_dir, testing_ds_dir

def make_dir(file_path):
    if not os.path.exists(file_path):
        os.makedirs(file_path)

def separate_train_test(dataset_path, train_path, test_path):
    class_index = 1
    for classname in sorted(os.listdir(dataset_path)):
        if classname.startswith('.'):
            continue
        make_dir(os.path.join(train_path, classname))
        make_dir(os.path.join(test_path, classname))
        i = 0
        for file in sorted(os.listdir(os.path.join(dataset_path, classname))):
            if file.startswith('.'):
                continue
            file_path = os.path.join(dataset_path, classname, file)
            if i < 15:
                copyfile(file_path, os.path.join(test_path, classname, file))
            else:
                copyfile(file_path, os.path.join(train_path, classname, file))
            i += 1

        class_index += 1

ds_filename_valid = lambda filename : not os.path.basename(filename).startswith('.')

def create_model(pretrained=True):
    model = models.resnet18(pretrained=pretrained)
    model = model.cuda() if is_cuda_available() else model
    num_features = model.fc.in_features
    model.fc = nn.Linear(num_features, 200)
    model.fc = model.fc.cuda() if is_cuda_available() else model.fc

    return model

def calculate_normalization_params(dataset_dir : str):
    transforms = trans.Compose([
        trans.Resize((224,224)),
        trans.ToTensor()
    ])

    dataset = ds.ImageFolder(root=dataset_dir, transform=transforms, is_valid_file=ds_filename_valid)
    dl = DataLoader(dataset, batch_size=64, num_workers=2)

    count = 0
    first_moment = torch.empty(3)
    second_moment = torch.empty(3)

    for images, _ in dl:
        b, _, h, w = images.shape
        nump = b * h * w
        sumv = torch.sum(images, dim=[0, 2, 3])
        sum_of_square = torch.sum(images ** 2, dim=[0, 2, 3])
        first_moment = (count * first_moment + sumv) / (count + nump)
        second_moment = (count * second_moment + sum_of_square) / (count + nump)
        count += nump

    mean_ = first_moment
    std_ = torch.sqrt(second_moment - first_moment ** 2)

    return mean_, std_

def get_datasets(ds_path, transforms, batch_size=32, num_workers=2, shuffle=True):
    dataset = ds.ImageFolder(root=ds_path, transform=transforms,
        is_valid_file=ds_filename_valid)
    data_loader = DataLoader(dataset, batch_size=batch_size,
        num_workers=num_workers, shuffle=shuffle)

    return dataset, data_loader

def run_experiment(
    name,
    model,
    transforms,
    training_dir,
    testing_dir,
    learning_rate=0.01,
    momentum=0.9,
    num_epochs=10,
    batch_size=32
):
    _, training_loader = get_datasets(training_dir, transforms, batch_size=batch_size)
    _, testing_loader = get_datasets(testing_dir, transforms)

    # Optimizer Initialization
    optimizer = optim.SGD(model.parameters(), lr=learning_rate, momentum=momentum)
    # Loss Function Initialization
    loss_function = nn.CrossEntropyLoss()

    result_dir = os.path.join(__result_dir__,name)
    Path(result_dir).mkdir(parents=True, exist_ok=True)

    res_photo = os.path.join(result_dir, 'result.png')
    if os.path.isfile(res_photo):
        img = mpimg.imread(res_photo)
        plt.imshow(img)
        return

    callbacks = [
        PlottingCallback(result_dir=result_dir),
        # Save the losses and accuracies for each epoch in a TSV.
        CSVLogger(os.path.join(result_dir, 'log_callback.tsv'), separator='\t'),
    ]

    # Create & Initialize Experiment
    exp_obj = Experiment(
        result_dir, model,
        device=get_torch_device(),
        optimizer=optimizer,
        loss_function=loss_function,
        batch_metrics=['accuracy'],
        epoch_metrics=['f1'],
        task='classif'
    )

    # Use Poutyne experiment to train the model.
    exp_obj.train(train_generator=training_loader, valid_generator=testing_loader,
                epochs=num_epochs, callbacks=callbacks)

    print(exp_obj.test(testing_loader))

    # Save the trained model
    save_model(model, os.path.join(result_dir, name + '.phm'))

def freeze_conv_params(model):
    for name, param in model.named_parameters():
        if name.startswith('conv'):
            param.requires_grad = False
    return model

def freeze_layer1_params(model):
    for name, param in model.named_parameters():
        if name.startswith('layer1'):
            param.requires_grad = False
    return model
    # for param in model.layer1.parameters():
    #     param.requires_grad = False

    # return model

class PlottingCallback(Callback):
    """ PhmPlottingCallback is the class containing the codes for visualizing the model's results.
        The class also extends Callback class makes it able to attach to Poutyne experiment and register for the raised events.
    """

    def __init__(self, result_dir='./'):
        super().__init__()
        self.report = list()
        self.fig_list = list()
        self.fig, (self.loss_ax, self.acc_ax) = plt.subplots(2, 1)
        self.fig.tight_layout(pad=1.0)
        self.fig.suptitle('Statistics of training model')
        self.result_dir = result_dir
        mng = plt.get_current_fig_manager()
        mng.full_screen_toggle()
        plt.ion()
        # plt.show()

    def on_train_begin(self, logs: Dict):
        """ on_train_begin is a method invoked in the begining of each train. """
        pass

    def _plot(self, axis, epoch, train_data, valid_data, label, maxv=1.0, minv=0.0, stepv=0.05):
        axis.clear()
        plt.yticks(fontsize=11)
        axis.plot(epoch, train_data, '.-', label='Train')
        axis.plot(epoch, valid_data, '.-', label='Validation')
        axis.set_xlabel('epoch')
        axis.set_ylabel(label)
        # axis.set_yticks(np.arange(minv, maxv, stepv), minor = True)
        axis.set_yticks(np.arange(min(train_data + valid_data),
                                  max(train_data + valid_data), stepv), minor=True)
        axis.legend()
        axis.grid(True, which='both', axis='both', linestyle='--')

    def on_epoch_end(self, epoch_number: int, logs: Dict):
        """ on_epoch_end is a method invoked in the end of each epoch. """

        self.report.append(logs)

        epoch = [item['epoch'] for item in self.report]
        train_loss = [item['loss'] for item in self.report]
        val_loss = [item['val_loss'] for item in self.report]
        train_acc = [item['acc'] for item in self.report]
        val_acc = [item['val_acc'] for item in self.report]

        canvas = FigureCanvasAgg(self.fig)
        # Loss
        if train_loss is not None and val_loss is not None:
            self._plot(self.loss_ax, epoch, train_loss, val_loss, 'Loss')
        # Accuracy
        if train_acc is not None and val_acc is not None:
            self._plot(self.acc_ax, epoch, train_acc,
                    val_acc, 'Accuracy', 100, 0, 5)

        # plt.show()
        # plt.pause(0.05)

        canvas.draw()
        fimg = np.array(canvas.renderer.buffer_rgba())
        self.fig_list.append(fimg)

    def on_train_end(self, logs: Dict):
        """ on_train_end is the method invoked in the end of each training. """

        if len(self.fig_list) > 0:
            imageio.mimsave(os.path.join(self.result_dir, 'result.gif'), self.fig_list, 'GIF')
            imageio.imwrite(os.path.join(self.result_dir, 'result.png'), self.fig_list[-1])


# from PIL import ImageGrab
import pyautogui
from time import sleep

class lab4:
	def __init__(self):
		self.coordinates = {
			'time': ((1305, 328), (1437, 358)),
			'mass': ((1395, 391), (1320, 391)),
			'radius': ((1395, 450), (1320, 450)),
			'height': ((1395, 506), (1320, 506)),
			'start': (1420, 569),
			'stop': (1420, 623),
			'reset': (1420, 679),
			'googleedit': (674, 275),
		}

	# def get_screen(self, rect):
	# 	return ImageGrab.grab(rect)

	def click(self, coords):
		pyautogui.moveTo(coords)
		sleep(0.1)
		pyautogui.click()
		sleep(0.1)

	def press(self, *keys):
		pyautogui.hotkey(*keys, interval=0.1)
		sleep(0.1)

	def highlight(self, rect):
		pyautogui.moveTo(rect[0][0], rect[0][1])
		sleep(0.5)
		pyautogui.dragTo(rect[1][0], rect[1][1])
		sleep(0.1)

	def erase(self, rect):
		self.highlight(rect)
		self.press('backspace')

	def copy(self, rect):
		self.highlight(rect)
		self.press('ctrl', 'c')

	def write(self, string):
		for char in string:
			self.press(char)

	def save(self, newcolumn=False, newgroup=False):
		self.press('ctrl', 'shift', 'tab')
		self.press('ctrl', 'v')

		self.press('enter')
		self.press('backspace')
		self.press('backspace')
		self.press('backspace')
		self.press('home')
		self.press('delete')
		self.press('delete')

		self.press('enter')

		if newcolumn:
			self.press('right')
			for _ in range(5):
				self.press('up')

		if newgroup:
			self.press('down')
			for _ in range(5):
				self.press('left')

		self.press('ctrl', 'tab')

	def solve(self):
		self.erase(self.coordinates['height'])
		self.write('0.7')

		masses = ['0.1', '0.3', '0.6', '0.9']
		radiuses = ['0.07', '0.1', '0.13', '0.16', '0.19', '0.22']
		sleeps = [
			[10, 12, 14, 16, 17.5, 21],
			[4.5, 5.5, 6.5, 7.5, 9, 10],
			[3.2, 4.2, 4.5, 5.3, 6, 6.8],
			[2.6, 3.2, 3.7, 4.3, 4.8, 5.5]
		]

		for m, mass in enumerate(masses):
			self.erase(self.coordinates['mass'])
			self.write(mass)

			for h, radius in enumerate(radiuses):
				self.erase(self.coordinates['radius'])
				self.write(radius)

				for i in range(5):
					self.click(self.coordinates['start'])
					sleep(sleeps[m][h])

					self.copy(self.coordinates['time'])
					self.save(newcolumn=(i == 4 and radius != radiuses[-1]),
							  newgroup=(radius == radiuses[-1] and i == 4))

					self.click(self.coordinates['reset'])

if __name__ == '__main__':
	laba = lab4()
	sleep(3)
	laba.solve()


from os import path, getcwd, sep, popen
from sys import argv, exit

def printUsage():
	print('Usage:')
	print('    From exercise directory: getNext.py')
	print('    From track directory: getNext.py <exercise>')
	print('    From root directory: getNext.py <track> <exercise>')

if __name__ == '__main__':
	cwd = getcwd()
	parts = cwd.split(sep)
	exercise = parts[-1]
	track = parts[-2]
	if track == 'ExercismSolutions':
		track = exercise
		if len(argv) < 2:
			printUsage()
			exit(1)
		exercise = argv[1]
		cwd = path.join(cwd,exercise)
	elif exercise == 'ExercismSolutions':
		if len(argv) < 3:
			if len(argv) == 2 and path.isdir(argv[1]):
				track,exercise = argv[1].split(sep)
			else:
				printUsage()
				exit(1)
		else:
			track = argv[1]
			exercise = argv[2]
		cwd = path.join(cwd,track,exercise)
	# print('Track: {}'.format(track))
	# print('Exercise: {}'.format(exercise))
	# print(cwd)
	cmd = 'powershell -Command "& exercism li {} | sls -patt "^{}$" -co 0,1"'.format(track, exercise)
	proc = popen(cmd)
	output = proc.readlines()
	print(output[-3].strip())
	# print('\n'.join(output))


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Nov 27 22:03:24 2018

@author: ly
"""
import pandas as pd
import numpy as np
import networkx as nx
import plotly.plotly as py
import plotly.graph_objs as go
import math

def processDF():
    # Read data frame
    df = pd.read_csv('cleaned_data.csv', sep=',',encoding='utf-8-sig',
                     usecols = ['Movie_genre','Movie_name'])
    # Drop duplicated movie name
    df = df.drop_duplicates(subset = 'Movie_name')
    # Split genre column to calculate the number of genres a movie has
    genre = df['Movie_genre']
    genre_list = [i.split(',') for i in genre.tolist()]
    # Add a new column for the nubmer of genres a movie belongs to
    length = [len(i) for i in genre_list]
    df['length'] = length
    # Create e new data frame, repeat movie name for certain times
    # depending on the numbe of genres
    df_temp = pd.DataFrame({'movie':np.repeat(df['Movie_name'], length)})

    # split genre again but into separate columns
    genre_split = genre.str.split(',',expand = True)
    # Transpose the genre columns to add to the new data frame
    row_temp = pd.Series()
    for index, row in genre_split.iterrows():
        row_temp = pd.concat([row_temp,row], ignore_index=True)
    row_temp = row_temp.dropna()
    # Add the new genre column for the temporary data frame
    df_temp['genre'] = row_temp.tolist()

    return df_temp

# The code below is adapted from two example from plotly tutorials
# Reference:
# https://plot.ly/python/network-graphs/
# https://plot.ly/python/custom-buttons/

def plotNetwork(df):
    # Use networkx to create a graph directly from the tailored data frame
    G = nx.from_pandas_edgelist(df, source='movie', target='genre')

    # Set node positions

    pos = nx.kamada_kawai_layout(G)
    for node in G.nodes():
        G.node[node]['pos']= pos[node]

    # Create node trace:
    node_trace = go.Scatter(
                            x=[],
                            y=[],
                            name='Node Trace',
                            text=[],
                            mode='markers',
                            hoverinfo='text',
                            marker=dict(
                                showscale=True,
                                # colorscale options
                                #'Greys' | 'YlGnBu' | 'Greens' | 'YlOrRd' | 'Bluered' | 'RdBu' |
                                #'Reds' | 'Blues' | 'Picnic' | 'Rainbow' | 'Portland' | 'Jet' |
                                #'Hot' | 'Blackbody' | 'Earth' | 'Electric' | 'Viridis' |
                                colorscale='Jet',
                                reversescale= True,
                                color=[],
                                size= [],
                                colorbar=dict(
                                    thickness=15,
                                    title='Node Connections',
                                    xanchor='left',
                                    titleside='right'
                                ),
                                line=dict(width=2)))

    for node in G.nodes():
        x, y = G.node[node]['pos']
        node_trace['x'] += tuple([x])
        node_trace['y'] += tuple([y])

    # Create edge trace:
    edge_trace = go.Scatter(
                            x=[],
                            y=[],
                            name='Edge Trace',
                            line=dict(width=0.5,color='#888'),
                            hoverinfo='none',
                            mode='lines')

    for edge in G.edges():
        x0, y0 = G.node[edge[0]]['pos']
        x1, y1 = G.node[edge[1]]['pos']
        edge_trace['x'] += tuple([x0, x1, None])
        edge_trace['y'] += tuple([y0, y1, None])

    # Color and resize nodes by degree
    for node, adjacencies in enumerate(G.adjacency()):
        node_trace['marker']['color']+=tuple([len(adjacencies[1])])
        node_info = str(adjacencies[0]) + '
'+'# of connections: '+str(len(adjacencies[1]))
        node_trace['text']+=tuple([node_info])
        # Use log transformation to alleviate extremely large values
        # Multiply by 3 so that small nodes are visible
        node_trace['marker']['size']+=tuple([math.log1p(len(adjacencies[1]))*3])
        # math.log1p(x)
        # Return the natural logarithm of 1+x (base e).
        # The result is calculated in a way which is accurate for x near zero.

    # Data is ready!
    data = [edge_trace, node_trace]

    # Set the layout
    layout = go.Layout(
                     title = '
Movies and Genres Network',
                     titlefont = dict(size = 18),
                     showlegend = True,
                     legend=dict(
                        x = 0.8,
                        y = 1.065,
                        font=dict(size=12)
                     ),
                     margin = dict(b = 20, l = 10, r = 5, t = 40),
                     xaxis = dict(showgrid = False,
                                   zeroline = False,
                                   showticklabels = False),
                     yaxis = dict(showgrid = False,
                                   zeroline = False,
                                   showticklabels = False))
    # Create update menus
    updatemenus=list([
            dict(
            # Create buttons for changing node color scale
            buttons=list([
                dict(
                    args=['marker.colorscale','Jet'],
                    label='Jet',
                    method='restyle'
                ),
                dict(
                    args=['marker.colorscale','YlOrRd'],
                    label='YlOrRd',
                    method='restyle'
                ),
                dict(
                    args=['marker.colorscale','Reds'],
                    label='Reds',
                    method='restyle'
                ),
                dict(
                    args=['marker.colorscale','Picnic'],
                    label='Picnic',
                    method='restyle'
                ),
                dict(
                    args=['marker.colorscale','Electric'],
                    label='Electric',
                    method='restyle'
                ),
                dict(
                    args=['marker.colorscale','Viridis'],
                    label='Viridis',
                    method='restyle'
                ),
            ]),
            direction = 'left',
            pad = {'r': 10, 't': 10},
            showactive = True,
            type = 'buttons',
            x = 0.06,
            xanchor = 'left',
            y = 1.065,
            yanchor = 'top'
        ),
        # Create reverse color scale buttons
        dict(
            buttons=list([
                dict(
                    args=['marker.reversescale', True],
                    label='Reverse',
                    method='restyle'
                ),
                dict(
                    args=['marker.reversescale', False],
                    label='Undo',
                    method='restyle'
                )
            ]),
            direction = 'left',
            pad = {'r': 10, 't': 10},
            showactive = True,
            type = 'buttons',
            x = 0.32,
            xanchor = 'left',
            y = 1.13,
            yanchor = 'top'
        ),
        # Create buttons for changing edge color
        dict(
            buttons=list([
                dict(
                    args=['line.color', 'rgba(100,90,80,0.5)'],
                    label='Gray',
                    method='restyle'
                ),
                dict(
                    args=['line.color', 'rgba(0,176,246,0.5)'],
                    label='Blue',
                    method='restyle'
                ),
                dict(
                    args=['line.color', 'rgba(231,107,243,0.5)'],
                    label='Pink',
                    method='restyle'
                )
            ]),
            direction = 'left',
            pad = {'r': 10, 't': 10},
            showactive = True,
            type = 'buttons',
            x = 0.06,
            xanchor = 'left',
            y = 1.13,
            yanchor = 'top'
        )
    ])

    # Add annotations
    annotations = list([
        dict(text='Node Color
Scale', x=0, y=1.05,xref = 'paper', yref='paper', align='left', showarrow=False),
        dict(text='Node Reverse', x=0.24, y=1.11,xref = 'paper',  yref='paper', showarrow=False),
        dict(text='Edge Color', x=0, y= 1.11, xref = 'paper', yref='paper', showarrow=False),
    ])

    layout['updatemenus'] = updatemenus
    layout['annotations'] = annotations
    # Layout is ready!

    # Figure is ready!
    fig = go.Figure(data = data,layout = layout)
    # Plot it!
    py.iplot(fig, filename = 'networkx')

def main():

    df = processDF()
    plotNetwork(df)

if __name__ == "__main__":
    main()

Movie Soundtracks Analysis/Interactive Chart/movie genre network - interactive.py


l = ["id_1234", "gid_23423", "alex_867"]

def manip(l):
    t = []
    for i in l:
        a = ''
        for s in i:
            if s != '_':
                a = a+s
            else:
                break
        t.append(a)
    return t

print(manip(l))


import flair
from flair.embeddings import WordEmbeddings
import sqlite3
import torch
import re
import os
from tqdm import tqdm

class WordEmbeddingsStore:
    """
    class to simulate a WordEmbeddings class from flair.

    Run this to generate a headless (without word embeddings) model as well a stored word embeddings:

    >>> from flair.inference_utils import WordEmbeddingsStore
    >>> from flair.models import SequenceTagger
    >>> import pickle
    >>> tagger = SequenceTagger.load("multi-ner-fast")
    >>> WordEmbeddingsStore.create_stores(tagger)
    >>> pickle.dump(tagger, open("multi-ner-fast-headless.pickle", "wb"))

    Then this can be used as follows:

    >>> from flair.data import Sentence
    >>> tagger = pickle.load(open("multi-ner-fast-headless.pickle", "rb"))
    >>> WordEmbeddingsStore.load_stores(tagger)
    >>> text = "Schade um den Ameisenbären. Lukas Bärfuss veröffentlicht Erzählungen aus zwanzig Jahren."
    >>> sentence = Sentence(text)
    >>> tagger.predict(sentence)
    >>> print(sentence.get_spans('ner'))
    """

    def __init__(self, embedding, verbose=True):
        # some non-used parameter to allow print
        self._modules = dict()
        self.items = ""

        # get db filename from embedding name
        self.name = embedding.name
        self.store_filename = WordEmbeddingsStore._get_store_filename(embedding)
        if verbose:
            print("store filename:", self.store_filename)

        # if embedding database already exists
        if os.path.isfile(self.store_filename):
            self.db = sqlite3.connect(self.store_filename)
            cursor = self.db.cursor()
            cursor.execute("SELECT * FROM embedding LIMIT 1;")
            result = list(cursor)
            self.k = len(result[0]) - 1
            return

        # otherwise, push embedding to database
        self.db = sqlite3.connect(self.store_filename)
        pwe = embedding.precomputed_word_embeddings
        self.k = pwe.vector_size
        self.db.execute(f"DROP TABLE IF EXISTS embedding;")
        self.db.execute(
            f"CREATE TABLE embedding(word text,{','.join('v'+str(i)+' float' for i in range(self.k))});"
        )
        vectors_it = (
            [word] + pwe.get_vector(word).tolist() for word in pwe.vocab.keys()
        )
        if verbose:
            print("load vectors to store")
        self.db.executemany(
            f"INSERT INTO embedding(word,{','.join('v'+str(i) for i in range(self.k))}) \
        values ({','.join(['?']*(1+self.k))})",
            tqdm(vectors_it),
        )
        self.db.execute(f"DROP INDEX IF EXISTS embedding_index;")
        self.db.execute(f"CREATE INDEX embedding_index ON embedding(word);")
        self.db.commit()
        self.db.close()

    def _get_vector(self, word="house"):
        db = sqlite3.connect(self.store_filename)
        cursor = db.cursor()
        word = word.replace('"','')
        cursor.execute(f'SELECT * FROM embedding WHERE word="{word}";')
        result = list(cursor)
        db.close()
        if not result:
            return [0.0] * self.k
        return result[0][1:]

    def embed(self, sentences):
        for sentence in sentences:
            for token in sentence:
                t = torch.tensor(self._get_vector(word=token.text.lower()))
                token.set_embedding(self.name, t)

    @staticmethod
    def _get_store_filename(embedding):
        """
        get the filename of the store
        """
        embedding_filename = re.findall(".flair(/.*)", embedding.name)[0]
        store_filename = str(flair.cache_root) + embedding_filename + ".sqlite"
        return store_filename

    @staticmethod
    def create_stores(model):
        """
        creates database versions of all word embeddings in the model and
        deletes the original vectors to save memory
        """
        for embedding in model.embeddings.embeddings:
            if type(embedding) == WordEmbeddings:
                WordEmbeddingsStore(embedding)
                del embedding.precomputed_word_embeddings

    @staticmethod
    def load_stores(model):
        """
        loads the db versions of all word embeddings in the model
        """
        for i, embedding in enumerate(model.embeddings.embeddings):
            if type(embedding) == WordEmbeddings:
                model.embeddings.embeddings[i] = WordEmbeddingsStore(embedding)

    @staticmethod
    def delete_stores(model):
        """
        deletes the db versions of all word embeddings
        """
        for embedding in model.embeddings.embeddings:
            store_filename = WordEmbeddingsStore._get_store_filename(embedding)
            if os.path.isfile(store_filename):
                print("delete store:", store_filename)
                os.remove(store_filename)


"""
Deepmind FullyConv Agent Implementation from SC2LE paper.
(https://deepmind.com/documents/110/sc2le.pdf)
"""

import torch
import torch.nn as nn
import numpy as np
from pysc2.lib import features
import torch.nn.functional as F

class EncodeSpatial(nn.Module):
    """
    """

    def __init__(self, n_features):
        super().__init__()
        self.preconv_layers = [
            nn.Conv2d(n_f, 1, kernel_size=1, stride=1, padding=0)
            for n_f in n_features]

        self.conv1 = nn.Conv2d(len(n_features), 16,
                               kernel_size=5, stride=1, padding=2)
        self.conv2 = nn.Conv2d(16, 32, kernel_size=3, stride=1, padding=1)

    def forward(self, input):
        """

        :param input: List of 3d tensors of feature
        :return:
        """
        feature_maps = [F.relu(self.preconv_layers[i](x))
                        for i, x in enumerate(input)]
        x = torch.cat(feature_maps, dim=1)

        x = F.relu(self.conv1(x))
        x = F.relu(self.conv2(x))
        return x

class Encode(nn.Module):
    """
    ...
    Args:
        n_screen_features: List of feature sizes of screen
        n_minimap_features: List of feature sizes of minimap
        n_nonspatial_features: Non-spatial feature size
    """

    def __init__(self, n_screen_features, n_minimap_features,
                 n_nonspatial_features):
        super().__init__()
        self.screen = EncodeSpatial(n_screen_features)
        self.minimap = EncodeSpatial(n_minimap_features)
        self.non_spatial_features = nn.Linear(n_nonspatial_features, 64)

    def forward(self, input):
        screen_map = self.screen(input[0])
        mini_map = self.minimap(input[1])
        assert screen_map.shape[-2:] == mini_map.shape[-2:], (
            "Spatial size of screen must"
            " be the same with mini-map's")

        non_spatial = torch.tanh(self.non_spatial_features(input[2]))
        non_spatial = non_spatial.unsqueeze(-1).unsqueeze(-1)
        non_spatial = non_spatial.repeat(1, 1, *screen_map.shape[-2:])

        return torch.cat([screen_map, mini_map, non_spatial], dim=1)

    def obs_to_torch(self, input, device="cpu"):
        spatials = [[torch.from_numpy(
            np.expand_dims(feature_map, 0)).float().to(device)
            for feature_map in obs] for obs in input[:2]]
        return (*spatials, torch.from_numpy(
            np.expand_dims(input[2], 0)).float().to(device))

class Output(nn.Module):
    def __init__(self, input_channels, spatial_y, spatial_x, n_actions):
        super().__init__()
        self.spatial_conv = nn.Conv2d(input_channels, 1, kernel_size=1)
        self.non_spatial_fc = nn.Linear(
            input_channels * spatial_y * spatial_x, 256)
        self.value = nn.Linear(256, 1)
        self.policy = nn.Linear(256, n_actions)

    def forward(self, input):
        spatial = self.spatial_conv(input)
        spatial = spatial.view(
            *spatial.shape[:2], spatial.shape[-1] * spatial.shape[-2])
        spatial = F.softmax(spatial, dim=-1)

        input = input.view(input.shape[0], -1)
        non_spatial = self.non_spatial_fc(input)
        value = self.value(non_spatial)
        policy = self.policy(non_spatial)

        return spatial, value, policy


import tensorflow as tf
import metadata
import preprocess
from tensorflow.python.feature_column import feature_column

def create_feature_columns():
    """Creates tensorflow feature_column definitions based on the metadata of the features.

    the tensorflow feature_column objects are created based on the data types of the features
    defined in the metadata.py module. Extended featured (if any) are created, based on the base features,
    as the preprocess.extend_feature_columns method is called.

    Returns:
      {string: tf.feature_column}: dictionary of name:feature_column .
    """

    numeric_columns = {feature_name: tf.feature_column.numeric_column(feature_name)
                       for feature_name in metadata.NUMERIC_FEATURE_NAMES}

    categorical_column_with_vocabulary = {item[0]: tf.feature_column.categorical_column_with_vocabulary_list(item[0], item[1])
                                          for item in metadata.CATEGORICAL_FEATURE_NAMES_WITH_VOCABULARY.items()}

    categorical_column_with_hash_bucket = {item[0]:tf.feature_column.categorical_column_with_hash_bucket(item[0], item[1])
                                           for item in metadata.CATEGORICAL_FEATURE_NAMES_WITH_HASH_BUCKET.items()}

    feature_columns = {}

    if numeric_columns is not None:
        feature_columns.update(numeric_columns)

    if categorical_column_with_vocabulary is not None:
        feature_columns.update(categorical_column_with_vocabulary)

    if categorical_column_with_hash_bucket is not None:
        feature_columns.update(categorical_column_with_hash_bucket)

    # add extended feature definitions before returning the feature_columns list
    return preprocess.extend_feature_columns(feature_columns)

def get_deep_and_wide_columns(feature_columns, embedding_size=0, use_indicators=True):
    """Creates deep and wide feature column lists.

    given a list of feature_column, each feature_column is categorised as either:
    1) dense, if the column is tf.feature_column._NumericColumn,
    2) categorical, if the column is tf.feature_column._VocabularyListCategoricalColumn or tf.feature_column._BucketizedColumn, or
    3) sparse, if the column is tf.feature_column._HashedCategoricalColumn or tf.feature_column._CrossedColumn.

    if use_indicators=True, then  categorical_columns are converted into indicator_column.
    if embedding_size > 0, then sparse_columns are converted to tf.feature_column.embedding_column (using the embedding_size).

    deep_columns = dense_columns + indicator_columns + embedding_columns
    wide_columns = categorical_columns + sparse_columns

    Args:
        feature_columns: [tf.feature_column] - A list of tf.feature_column objects.
        embedding_size: int - if greater than 0, then sparse_columns are converted to tf.feature_column.embedding_column.
        use_indicators: bool - if True, then categorical_columns are converted into tf.feature_column.indicator_column.
    Returns:
        [tf.feature_column],[tf.feature_column]: deep and wide feature_column lists.
    """
    dense_columns = list(
        filter(lambda column: isinstance(column, feature_column._NumericColumn),
               feature_columns
        )
    )

    categorical_columns = list(
        filter(lambda column: isinstance(column,feature_column._VocabularyListCategoricalColumn) |
                              isinstance(column, feature_column._BucketizedColumn),
                   feature_columns)
    )

    sparse_columns = list(
        filter(lambda column: isinstance(column,feature_column._HashedCategoricalColumn) |
                              isinstance(column, feature_column._CrossedColumn),
               feature_columns)
    )

    indicator_columns = []

    if use_indicators:

        indicator_columns = list(
            map(lambda column: tf.feature_column.indicator_column(column),
                categorical_columns)
        )

    embedding_columns = []

    if embedding_size > 0:

        embedding_columns = list(
            map(lambda sparse_column: tf.feature_column.embedding_column(sparse_column, dimension=embedding_size),
                sparse_columns)
        )

    deep_columns = dense_columns + indicator_columns + embedding_columns
    wide_columns = categorical_columns + sparse_columns

    return deep_columns, wide_columns


#!/usr/bin/env python
# -*- coding: utf-8 -*-
import argparse
import logging
import os

from dwi_ml.data.dataset.utils import prepare_multisubjectdataset
from dwi_ml.experiment_utils.timer import Timer
from dwi_ml.models.projects.transforming_tractography import TransformerSrcAndTgtModel
from dwi_ml.training.projects.transformer_trainer import TransformerTrainer
from dwi_ml.training.utils.batch_loaders import prepare_batch_loader
from dwi_ml.training.utils.batch_samplers import prepare_batch_sampler
from dwi_ml.training.utils.trainer import run_experiment

def prepare_arg_parser():
    p = argparse.ArgumentParser(description=__doc__,
                                formatter_class=argparse.RawTextHelpFormatter)
    p.add_argument('experiments_path',
                   help='Path from where to load your experiment, and where to'
                        'save new results.\nComplete path will be '
                        'experiments_path/experiment_name.')
    p.add_argument('experiment_name',
                   help='If given, name for the experiment. Else, model will '
                        'decide the name to \ngive based on time of day.')

    p.add_argument('--new_patience', type=int, metavar='new_p',
                   help='If a checkpoint exists, patience can be increased '
                        'to allow experiment \nto continue if the allowed '
                        'number of bad epochs has been previously reached.')
    p.add_argument('--new_max_epochs', type=int,
                   metavar='new_max',
                   help='If a checkpoint exists, max_epochs can be increased '
                        'to allow experiment \nto continue if the allowed '
                        'number of epochs has been previously reached.')

    p.add_argument('--logging', dest='logging_choice', default='WARNING',
                   choices=['ERROR', 'WARNING', 'INFO', 'DEBUG'],
                   help="Logging level. Note that, for readability, not all "
                        "debug logs are printed in DEBUG mode.")

    return p

def init_from_checkpoint(args, checkpoint_path):

    # Loading checkpoint
    checkpoint_state = TransformerTrainer.load_params_from_checkpoint(
        args.experiments_path, args.experiment_name)

    # Stop now if early stopping was triggered.
    TransformerTrainer.check_stopping_cause(
        checkpoint_state, args.new_patience, args.new_max_epochs)

    # Prepare data
    args_data = argparse.Namespace(**checkpoint_state['dataset_params'])
    dataset = prepare_multisubjectdataset(args_data)

    # Setting log level to INFO maximum for sub-loggers, else it become ugly
    sub_loggers_level = args.logging_choice
    if args.logging_choice == 'DEBUG':
        sub_loggers_level = 'INFO'

    # Prepare model
    model = TransformerSrcAndTgtModel.load_params_and_state(
        os.path.join(checkpoint_path, 'model'), sub_loggers_level)

    # Prepare batch sampler
    _args = argparse.Namespace(**checkpoint_state['batch_sampler_params'])
    batch_sampler = prepare_batch_sampler(dataset, _args, sub_loggers_level)

    # Prepare batch loader
    _args = argparse.Namespace(**checkpoint_state['batch_loader_params'])
    batch_loader = prepare_batch_loader(dataset, model, _args, sub_loggers_level)

    # Instantiate trainer
    with Timer("\n\nPreparing trainer", newline=True, color='red'):
        trainer = TransformerTrainer.init_from_checkpoint(
            model, args.experiments_path, args.experiment_name,
            batch_sampler,  batch_loader,
            checkpoint_state, args.new_patience, args.new_max_epochs,
            args.logging_choice)
    return trainer

def main():
    p = prepare_arg_parser()
    args = p.parse_args()

    # Setting root logger with high level but we will set trainer to
    # user-defined level.
    logging.getLogger().setLevel(level=logging.INFO)

    # Verify if a checkpoint has been saved.
    checkpoint_path = os.path.join(
            args.experiments_path, args.experiment_name, "checkpoint")
    if not os.path.exists(checkpoint_path):
        raise FileNotFoundError("Experiment's checkpoint not found ({})."
                                .format(checkpoint_path))

    trainer = init_from_checkpoint(args, checkpoint_path)

    run_experiment(trainer)

if __name__ == '__main__':
    main()


import pandas
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit

parameter = 'Weight(pounds)'

def read_data():
    global data, Starting_Pitcher, Relief_Pitcher, Starting_Pitcher_Parameter, Relief_Pitcher_Parameter

    data = pandas.read_csv(
        '../data/footballers.csv',
        delimiter=";"
    )
    Starting_Pitcher = data[(data['Position'] == 'Starting_Pitcher')]
    Starting_Pitcher_Parameter = Starting_Pitcher[parameter]
    Starting_Pitcher_Parameter = Starting_Pitcher_Parameter[~np.isnan(Starting_Pitcher_Parameter)]
    Relief_Pitcher = data[(data['Position'] == 'Relief_Pitcher')]
    Relief_Pitcher_Parameter = Relief_Pitcher[parameter]

def check_weight_parameter():
    W, p = stats.shapiro(data[parameter])
    print("Data is normilized - {}".format(p > 0.05))
    make_levene_test()
    visualize_in_histogram()
    visualize_in_qplot()
    make_shapiro_wilk_test()
    # make_independent_ttest()
    make_wilcoxon_test()
    make_mannwhitney_test()
    show_histogram()
    show_gauss_approximation()

def make_levene_test():
    print(stats.levene(Relief_Pitcher_Parameter, Starting_Pitcher_Parameter))

def visualize_in_histogram():
    Relief_Pitcher_Parameter.plot(kind="hist", title="Relief_Pitcher")
    plt.xlabel(parameter)
    plt.show()

    Starting_Pitcher_Parameter.plot(kind="hist", title="Starting_Pitcher", color="red")
    plt.xlabel(parameter)
    plt.show()

def visualize_in_qplot():
    stats.probplot(Relief_Pitcher_Parameter, dist="norm", plot=plt)
    plt.title("Relief_Pitcher Q-Q Plot")
    plt.grid()
    plt.show()

    stats.probplot(Starting_Pitcher_Parameter, dist="norm", plot=plt)
    plt.title("Starting_Pitcher Q-Q Plot")
    plt.grid()
    plt.show()

def make_shapiro_wilk_test():
    print('Relief_Pitcher: ')
    print(stats.shapiro(Relief_Pitcher_Parameter))
    print('Starting_Pitcher: ')
    print(stats.shapiro(Starting_Pitcher_Parameter))

def make_independent_ttest():
    result = stats.ttest_ind(Relief_Pitcher_Parameter, Starting_Pitcher_Parameter)
    print("ttest_ind {}".format(result[1] * 100))

    if result[1] <= 0.05:
        print("Yes we can use this parameter")
    else:
        print("No, we can't use this parameter")

def make_wilcoxon_test():
    result = stats.ranksums(Relief_Pitcher_Parameter, Starting_Pitcher_Parameter)

    if result[1] <= 0.05:
        print("wilcoxon. Yes we can use this parameter")
    else:
        print("wilcoxon. No, we can't use this parameter")

def make_mannwhitney_test():
    result = stats.mannwhitneyu(Relief_Pitcher_Parameter, Starting_Pitcher_Parameter)

    if result[1] <= 0.05:
        print("mannwhitney. Yes we can use this parameter")
    else:
        print("mannwhitney. No, we can't use this parameter")

def show_histogram():
    data.hist()
    plt.show()

def show_gauss_approximation():
    own_data = data[parameter]
    own_data = own_data[~np.isnan(own_data)]
    x = np.linspace(0, len(own_data), len(own_data))
    y = own_data
    popt, pcov = curve_fit(gauss_func, x, own_data)

    plt.clf()
    plt.plot(x, y, 'g-', label='Data')
    plt.plot(x, gauss_func(x, *popt), 'r-', label='Gauss distribution')
    plt.title(parameter)
    plt.legend(loc='best')
    plt.show()

def gauss_func(x, a, b, c):
    return a * np.exp(-(x - b) ** 2 / (2 * c ** 2))

if __name__ == "__main__":
    read_data()
    check_weight_parameter()


import numpy as np

def get_atp_conc(lum):
    '''Input lum measurement value with gain 135 from a biotek
    Representative of ATP from Cayman Chem firefly luminescence assay
    fluor = fluorescence values, input as list in mM units,
    Output: ATP Concentration, list, in mM units'''
    try:
        # Define variables
        slope = 0.6177860271592077
        intercept = 10.283052798607272
        avg_blank = 11.5
        atp_list = []

        for val in lum:
            # Check inputs
            if val <= 11.5:
                print('Inputs must be greater than the lum value for the blanks, 11.5')

            # Get log correct sample RLU
            correct_lum = val - avg_blank
            log_lum = np.log(correct_lum)

            # Get log atp conc
            log_atp = (log_lum - intercept) / slope

            # Get atp conc
            atp = np.exp(log_atp)
            atp_list.append(atp)

        return atp_list

    except:
        print('Input must be a list and above 11.5')