from gensim.models import KeyedVectors as Word2Vec
from tqdm import tqdm

import sys
sys.path.append('../../for_matchZoo')
from tools4text import clean

def w2v_model_accuracy(model, questions):

    accuracy = model.accuracy(questions)

    sum_corr = len(accuracy[-1]['correct'])
    sum_incorr = len(accuracy[-1]['incorrect'])
    total = sum_corr + sum_incorr
    percent = lambda a: a / total * 100

    print('Total sentences: {}, Correct: {:.2f}%, Incorrect: {:.2f}%'.format(total, percent(sum_corr), percent(sum_incorr)))

# read the evaluation file, get it at:
# https://word2vec.googlecode.com/svn/trunk/questions-words.txt
questions = 'questions-words.txt'
evals = open(questions, 'r').readlines()
num_sections = len([l for l in evals if l.startswith(':')])
print('total evaluation sentences: {} '.format(len(evals) - num_sections))
# total evaluation sentences: 19544

# clean questions
print("Preprocessing questions ...")
out = open("pre_processed_"+questions, "w")
for l in tqdm(evals):
	if l.startswith(':'):
		out.write(l)
	else:
		pre_processed = clean(l, "krovetz", [])
		out.write(pre_processed+"\n")

questions = "pre_processed_"+questions

# load the pre-trained model of GoogleNews dataset (100 billion words), get it at:
# https://code.google.com/p/word2vec/#Pre-trained_word_and_phrase_vectors
google = Word2Vec.load_word2vec_format('/home/thiziri/Documents/DOCTORAT/osirim_data/projets/iris/PROJETS/WEIR/collections/constructed/local_embeddings/Robust/Robust_skipgram_wordEmbedding_dim300_win10_minCount5.txt', binary=False) #('GoogleNews-vectors-negative300.bin', binary=True)
# test the model accuracy*
w2v_model_accuracy(google, questions)
#Total sentences: 7614, Correct: 74.26%, Incorrect: 25.74%

# *took around 1hr45mins on Mac Book Pro (3.1 GHz Intel Core i7)


from django.urls import path
from . import views

urlpatterns = [
    path('', views.home, name='home'),
    path('view_profile/<username>/', views.view_user_profile, name='view_user_profile'),
    path('friend_list/', views.friend_list, name='friend_list'),
    path('connect/<operation>/<username>/',views.change_friend, name='change_friend'),

]


#!/usr/bin/env python

""" Assignment 3, Exercise 1, INF1340, Fall, 2015. DBMS
Test module for exercise3.py
"""

__author__ = 'Farid Gassanov, Dusan Miletic, Tessie Riggs'
__email__ = "farid.gassanov@mail.utoronto.ca, dusan.miletic@utoronto.ca, tessie.riggs@gmail.com"
__copyright__ = "2015 Farid Gassanov, Dusan Miletic, Tessie Riggs"

from exercise1 import selection, project, cross_product

###########
# TABLES ##
###########

STUDENTS = [["FirstName", "Surname", "IQ", "GPA"],
             ["Hoban", "Washburne", 94, 3.2],
             ["Zoe", "Washburne", 110, 3.5],
             ["Malcolm", "Reynolds", 81, 2.6],
             ["Jayne", "Cobb", 55, 1.1],
             ["Inara", "Serra", 158, 4.0]]

EMPTY_LIST = [["FirstName", "Surname", "IQ", "GPA"]]

COMPLETELY_EMPTY1 = []

COMPLETELY_EMPTY2 = []

R1 = [["Student", "Class"],
      ["Zoe", "Programming"],
      ["Jayne", "Web Design"],
      ["Inara", "Programming"]]

R2 = [["Class", "Professor"],
      ["Programming", "Sim"],
      ["Web Design", "Yu"]]

#####################
# HELPER FUNCTIONS ##
#####################
def is_equal(t1, t2):

    t1.sort()
    t2.sort()

    return t1 == t2

#####################
# FILTER FUNCTIONS ##
#####################
def filter_students(row):
    """
    Check if student represented by row has an IQ
    of at least 90 and a GPA of more than 3.4
    :param row: A List in the format:
    [{First name}, {Surname}, {IQ}, {GPA}]
    :return: True if the row satisfies the
    condition.
    """
    return row[-2] >= 90 and row[-1] > 3.4

###################
# TEST FUNCTIONS ##
###################

def test_selection():
    """
    Test the selection operation.
    """

    result = [["FirstName", "Surname", "IQ", "GPA"],
              ["Zoe", "Washburne", 110, 3.5],
              ["Inara", "Serra", 158, 4.0]]

    assert is_equal(result, selection(STUDENTS, filter_students))

def test_empty_list_selection():
    """
    Test selection operation when using a list that is empty.
    """
    try:
        selection(EMPTY_LIST, filter_students)
    except AttributeError:
        assert True

def test_project():
    """
    Test the projection operation.
    """

    result = [["FirstName", "Surname"],
              ["Hoban", "Washburne"],
              ["Zoe", "Washburne"],
              ["Malcolm", "Reynolds"],
              ["Jayne", "Cobb"],
              ["Inara", "Serra"]]

    assert is_equal(result, project(STUDENTS, ["FirstName", "Surname"]))

def test_empty_list_projection():
    """
    Test projection operation when using a list that is empty.
    """

    try:
        project(COMPLETELY_EMPTY1, ["Surname"])
    except IndexError:
        assert True

def test_wrong_column_projection():
    """
    Test the projection operation when faced with a column head that did not exist, i.e. an
    Unknown Attribute.
    """
    try:
        project(EMPTY_LIST, ["Does Not Exist"])
    except AttributeError:
        assert True

def test_cross_product():
    """
    Test cross product operation.
    """

    result = [["Student", "Class", "Class", "Professor"],
              ["Zoe", "Programming", "Programming", "Sim"],
              ["Zoe", "Programming", "Web Design", "Yu"],
              ["Jayne", "Web Design", "Web Design", "Yu"],
              ["Jayne", "Web Design", "Programming", "Sim"],
              ["Inara", "Programming", "Programming", "Sim"],
              ["Inara", "Programming", "Web Design", "Yu"]]

    assert is_equal(result, cross_product(R1, R2))

def test_empty_cross_product():
    """
    Test cross product operation if uses on empty lists.
    """

    assert cross_product(EMPTY_LIST, COMPLETELY_EMPTY1) is None


from collections import defaultdict

try:
    from itertools import izip_longest
except ImportError:
    from itertools import zip_longest

from io import StringIO
from os import stat

import pandas as pd
import numpy as np

import pytest

from rpy2.robjects import r, pandas2ri
from triform.create_bed import create_bed

@pytest.fixture
def chip():
    bed_file = "tests/test_data/iranges_to_bigwig.bed"
    df = r["read.table"](bed_file, sep=" ")

    treatment = {bed_file: {
        "chrY":
        r("function(df) {IRanges(start=df$start, width=df$width)}")(df),
        "chr1": r("function(df) {IRanges(start=df$start, width=df$width)}")(df)
    }}

    return treatment

@pytest.fixture
def input():
    bed_file = "tests/test_data/input_iranges_to_bigwig.bed"
    df = r["read.table"](bed_file, sep=" ", header=1)

    control = {bed_file: {
        "chrY":
        r("function(df) {IRanges(start=df$start, width=df$width)}")(df),
        "chr1": r("function(df) {IRanges(start=df$start, width=df$width)}")(df)
    }}

    return control

@pytest.fixture
def enriched_regions():

    result = u"""CHROMOSOME START END QVAL NLQ NLP MAX.NLP LOC WIDTH CVG SURL SURR FORM
chrY 27096723 27096953 0.0030190321570660633 2.520132261086883 3.984008558280453 5.638949274037698 27096861 231 19.0 2.0 24.0 2
chr1 2709562 2719659 0.12753262199776602 0.8943787113752741 1.9970405839422427 4.269576380346016 2709612 98 12.0 3.0 9.0 2"""

    return pd.read_table(StringIO(result), index_col=[0, 1, 2], sep=" ", header=0)

@pytest.fixture
def expected_result():
    result = u"""Chromosome      Start        End      Qval  logFC      Loc
chr1  2709562.0  2719659.0  0.127533    0.0  2709612"""

    return pd.read_table(StringIO(result), header=0, index_col=[0, 1, 2], sep="\s+")

@pytest.mark.current
def test_create_bed(chip, input, enriched_regions, expected_result, args):
    print(chip, "chip " * 3)
    print(input, "input " * 3)
    print(expected_result)

    result = create_bed(chip, input, enriched_regions, args)
    print(result, "result")
    print(expected_result, "expected")

    assert np.allclose(result, expected_result)
# /local/home/endrebak/anaconda3/lib/python3.5/site-packages/rpy2/robjects/functions.py:106: UserWarning: Error in .Call2("Rle_constructor", values, lengths, check, 0L, PACKAGE = "S4Vectors") :
#   integer overflow while summing elements in 'lengths'

#   res = super(Function, self).__call__(*new_args, **new_kwargs)
# /local/home/endrebak/anaconda3/lib/python3.5/site-packages/rpy2/robjects/functions.py:106: UserWarning: In addition:
#   res = super(Function, self).__call__(*new_args, **new_kwargs)
# /local/home/endrebak/anaconda3/lib/python3.5/site-packages/rpy2/robjects/functions.py:106: UserWarning: There were 24 warnings (use warnings() to see them)
#   res = super(Function, self).__call__(*new_args, **new_kwargs)
# /local/home/endrebak/anaconda3/lib/python3.5/site-packages/rpy2/robjects/functions.py:106: UserWarning:

#   res = super(Function, self).__call__(*new_args, **new_kwargs)
# Traceback (most recent call last):
#   File "/local/home/endrebak/anaconda3/bin//triform2", line 4, in <module>
#     __import__('pkg_resources').run_script('triform2==0.0.5', 'triform2')
#   File "/local/home/endrebak/anaconda3/lib/python3.5/site-packages/setuptools-20.7.0-py3.5.egg/pkg_resources/__init__.py", line 719, in run_script
#   File "/local/home/endrebak/anaconda3/lib/python3.5/site-packages/setuptools-20.7.0-py3.5.egg/pkg_resources/__init__.py", line 1504, in run_script
#   File "/local/home/endrebak/anaconda3/lib/python3.5/site-packages/triform2-0.0.5-py3.5.egg/EGG-INFO/scripts/triform2", line 157, in <module>
#     run_triform(args)
#   File "/local/home/endrebak/anaconda3/lib/python3.5/site-packages/triform2-0.0.5-py3.5.egg/triform/run_triform.py", line 96, in run_triform
#     create_bed(treatment_iranges, args.bed, args)
#   File "/local/home/endrebak/anaconda3/lib/python3.5/site-packages/triform2-0.0.5-py3.5.egg/triform/create_bed.py", line 20, in create_bed
#     _create_bed(iranges, outpath, args)
#   File "/local/home/endrebak/anaconda3/lib/python3.5/site-packages/triform2-0.0.5-py3.5.egg/triform/create_bed.py", line 37, in _create_bed
#     genome = r["RleList"](genome_coverage_rpkm)
#   File "/local/home/endrebak/anaconda3/lib/python3.5/site-packages/rpy2/robjects/functions.py", line 178, in __call__
#     return super(SignatureTranslatedFunction, self).__call__(*args, **kwargs)
#   File "/local/home/endrebak/anaconda3/lib/python3.5/site-packages/rpy2/robjects/functions.py", line 106, in __call__
#     res = super(Function, self).__call__(*new_args, **new_kwargs)
# rpy2.rinterface.RRuntimeError: Error in .Call2("Rle_constructor", values, lengths, check, 0L, PACKAGE = "S4Vectors") :
#   integer overflow while summing elements in 'lengths'


from flask import Flask,Blueprint,render_template,abort,session, request
from Classes.oauth_config import oauth
from Classes.login_manager_config import login_required, current_user
from Models.Identity import db, User, Client
from pprint import pprint

oauth_endpoint = Blueprint('oauth_endpoint',__name__,template_folder='templates')

@oauth_endpoint.route('/oauth/authorize/', methods=['GET', 'POST'])
@login_required
@oauth.authorize_handler
def authorize(*args, **kwargs):
    if request.method == 'GET':
        client_id = kwargs.get('client_id')
        client = Client.query.filter_by(client_id=client_id).first()
        kwargs['client'] = client
        return render_template('/oauth/oauthorize.html', user= current_user, **kwargs)

    confirm = request.form.get('confirm', 'no')
    return confirm == 'yes'

@oauth_endpoint.route('/oauth/token/', methods=["POST"])
@oauth.token_handler
def access_token():
    return None


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

import numpy as np
from scipy import sparse
from . import Graph

class Grid2d(Graph):
    r"""
    Create a 2 dimensional grid graph.

    Parameters
    ----------
    Nv : int
        Number of vertices along the first dimension (default is 16)
    Mv : int
        Number of vertices along the second dimension (default is Nv)

    Examples
    --------
    >>> from pygsp import graphs
    >>> G = graphs.Grid2d(Nv=32)

    """

    def __init__(self, Nv=16, Mv=None, **kwargs):
        if not Mv:
            Mv = Nv

        # Create weighted adjacency matrix
        K = 2*(Nv - 1)
        J = 2*(Mv - 1)

        i_inds = np.zeros((K*Mv + J*Nv), dtype=float)
        j_inds = np.zeros((K*Mv + J*Nv), dtype=float)

        tmpK = np.arange(K, dtype=int)
        tmpNv1 = np.arange(Nv - 1)
        for i in range(Mv):
            i_inds[i*K + tmpK] = i*Nv + \
                np.concatenate((tmpNv1, tmpNv1 + 1))
            j_inds[i*K + tmpK] = i*Nv + \
                np.concatenate((tmpNv1 + 1, tmpNv1))

        tmp2Nv = np.arange(2*Nv, dtype=int)
        tmpNv = np.arange(Nv)
        for i in range(Mv-1):
            i_inds[(K*Mv) + i*2*Nv + tmp2Nv] = \
                np.concatenate((i*Nv + tmpNv, (i + 1)*Nv + tmpNv))

            j_inds[(K*Mv) + i*2*Nv + tmp2Nv] = \
                np.concatenate(((i + 1)*Nv + tmpNv, i*Nv + tmpNv))

        W = sparse.csc_matrix((np.ones((K*Mv + J*Nv)), (i_inds, j_inds)),
                              shape=(Mv*Nv, Mv*Nv))

        xtmp = np.kron(np.ones((Mv, 1)), (np.arange(Nv)/float(Nv)).reshape(Nv,
                                                                           1))
        ytmp = np.sort(np.kron(np.ones((Nv, 1)),
                               np.arange(Mv)/float(Mv)).reshape(Mv*Nv, 1),
                       axis=0)

        coords = np.concatenate((xtmp, ytmp), axis=1)

        self.Nv = Nv
        self.Mv = Mv
        plotting = {"vertex_size": 30,
                    "limits": np.array([-1./self.Nv, 1 + 1./self.Nv,
                                        1./self.Mv, 1 + 1./self.Mv])}

        super(Grid2d, self).__init__(W=W, gtype='2d-grid', coords=coords,
                                     plotting=plotting, **kwargs)


#!/usr/bin/env python3

'''
   Date: September 21, 2021
   From: Mark Mollere, mmollere@alta3.com
Subject: push_it.py
     To: You

push_it.py is a simple process to knock out a "git push" to GitHub. This code is
imperfect. The intent is for you to gaze upon the building blocks and engage in
critcal thinking for the next iteration...
'''
# <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
import getopt
import os
import sys
# <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
def get_arguments(git_comment):
  argumentList = sys.argv[1:]
  options      = "c:hmsv"
  long_options = ["comment","help","man-page","symbolic-link","version"]
  version      = '1.6'
  try:
    arguments, values = getopt.getopt(argumentList, options, long_options)
    for currentArgument, currentValue in arguments:
      if currentArgument in ("-c", "--comment"):
        return currentValue
      elif currentArgument in ("-h", "--help"):
        print ("WIP: Help me...")
      elif currentArgument in ("-m", "--man-page"):
        print ("WIP: Install " + os.path.basename(sys.argv[0]) + " man page.")
      elif currentArgument in ("-s", "--symbolic-link"):
        print ("WIP: Symbolic link...")
      elif currentArgument in ("-v", "--version"):
        print (os.path.basename(sys.argv[0]), version)
  except:
    print("Error 101")
# <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
def run_the_4_commands(git_comment):
  if len(sys.argv) == 1 or git_comment:
    if bool(git_comment):
      commit_message    = git_comment
    else:
      commit_message    = input('Commit Comment: ')
    working_directory = '/home/student/mycode'
    git_add           = 'git add *'
    git_commit        = 'git commit -m "' + commit_message + '"'
    git_push          = 'git push origin'

    os.chdir(working_directory) # Command 1.
    os.system(git_add)          # Command 2.
    os.system(git_commit)       # Command 3.
    os.system(git_push)         # Command 4.
# <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
def main():
  run_the_4_commands(get_arguments(""))
# <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
if __name__ == "__main__":
    main()
# <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


N = int(input())
W = list(map(int, input().split()))

ans = 10**9
for T in range(1, N+1):
    S1 = 0
    S2 = 0
    for i in range(0,len(W)):
        if (i <= T):
            S1 += W[i]
        else:
            S2 += W[i]
    ans = min(ans, abs(S1-S2))

print(ans)


from flask import Flask, render_template, request
from prometheus_flask_exporter import PrometheusMetrics
from prometheus_flask_exporter.multiprocess import GunicornInternalPrometheusMetrics
import os

app = Flask(__name__)
is_gunicorn = "gunicorn" in os.environ.get("SERVER_SOFTWARE", "")
if is_gunicorn:
    metrics = GunicornInternalPrometheusMetrics(app)
else:
    metrics = PrometheusMetrics(app)

@app.route('/')
def homepage():
    return render_template("main.html")

if __name__ == "__main__":
    app.run(debug=False)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""====================================
# @Time    : 2019/8/10 20:36
# @Author  : Jing
# @FileName: 213. House Robber II.py
# @IDE: PyCharm
======================================="""
# https://leetcode.com/problems/house-robber-ii/
# Solution 1: O(N^2) Time and O(N) Space
# Solution 2: O(N^2) Time and O(1) Space
# Solution 3: O(N) Time and O(N) Space
# Solution 4: O(N) Time and O(1) Space

class Solution:
    def helper1(self, nums):
        n = len(nums)
        if n == 0:
            return 0
        res = [0] * (n + 1)
        res[1] = nums[0]
        for i in range(2, n + 1):
            res[i] = max(res[i - 1], res[i - 2] + nums[i - 1])
        return res[n]

    def helper2(self, nums):
        n = len(nums)
        if n == 0:
            return 0
        a, b = 0, nums[0]
        for i in range(2, n+1):
            a, b = b, max(b, a+nums[i-1])
        return b

    def rob1(self, nums):
        n = len(nums)
        if n == 0:
            return 0
        if n <= 3:
            return max(nums)
        dp = [0] * (n)
        for i in range(n):
            if i <= 1:
                dp[i] = nums[i] + self.helper1(nums[i + 2:n + i - 1])
            if 1 < i <= n - 3:
                dp[i] = nums[i] + self.helper1(nums[i + 2:n] + nums[0:i - 1])
            if i > n - 3:
                dp[i] = nums[i] + self.helper1(nums[i + 2 - n:i - 1])
        return max(dp)

    def rob2(self, nums):
        n = len(nums)
        if n == 0:
            return 0
        if n <= 3:
            return max(nums)
        dp = [0] * (n)
        for i in range(n):
            if i <= 1:
                dp[i] = nums[i] + self.helper2(nums[i + 2:n + i - 1])
            if 1 < i <= n - 3:
                dp[i] = nums[i] + self.helper2(nums[i + 2:n] + nums[0:i - 1])
            if i > n - 3:
                dp[i] = nums[i] + self.helper2(nums[i + 2 - n:i - 1])
        return max(dp)

    def rob3(self, nums):
        n = len(nums)
        if n == 0:
            return 0
        if n <= 3:
            return max(nums)
        return max(self.helper1(nums[:-1]), self.helper1(nums[1:]))

    def rob4(self, nums):
        n = len(nums)
        if n == 0:
            return 0
        if n <= 3:
            return max(nums)
        return max(self.helper2(nums[:-1]), self.helper2(nums[1:]))

if __name__ == '__main__':
    a = [1,2,3,1]
    s = Solution()
    print(s.rob4(a))

Dynamic Programming/213. House Robber II.py


def setup():
    global schip
    global f, f1, f2
    schip = loadImage("schip.png")
    f = createFont("Harrington",40)
    f1 = createFont("Harrington",80)
    f2 = createFont("Harrington",30)

    global backCo
    backX = width/7
    backY = height-height/4
    backW = 350
    backH = 100
    backCo = [backX,backY,backW,backH]

    global contCo
    contX = width/7
    contY = height/4-height/20
    contW = 350
    contH = 100
    contCo = [contX,contY,contW,contH]

    global guideCo
    guideX = width/7
    guideY = contY + contH + height/40
    guideW = 350
    guideH = 100
    guideCo = [guideX,guideY,guideW,guideH]

    global mainColor,secondColor
    mainColor = color(234,222,191)
    secondColor = color(134,122,91)

def draw():
    image(schip,0,0,width,height)
    fill(0)
    textFont(f1)
    text("Vlaggenjacht",width/4,height/10)
    textFont(f)
    fill(secondColor,63)
    fill(134,122,91,63)

    rect(*contCo)
    rect(*guideCo)
    rect(*backCo)
    fill(0)
    textSize(55)
    text("Start Spel",contCo[0]+contCo[2]/2,contCo[1]+contCo[3]*0.75)
    text("Handleiding",guideCo[0]+guideCo[2]/2,guideCo[1]+guideCo[3]*0.75)
    text("Verlaat Spel",backCo[0]+backCo[2]/2,backCo[1]+backCo[3]*0.75)

def hollowRect(x,y,w,h):
    line(x,y,x+w,y)
    line(x,y,x,y+h)
    line(x,y+h,x+w,y+h)
    line(x+w,y,x+w,y+h)


import json

import numpy
import string
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.utils import to_categorical
from glove.EmbeddingLoader import EmbeddingLoader
from keras_contrib.layers import CRF
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Flatten
from keras.layers import Embedding
from keras.layers import Bidirectional
from keras.layers import LSTM
from keras.layers import TimeDistributed
from keras.layers import Dense
from evaluation.Evaluater import Evaluater
from tensorflow import keras
from seqeval.metrics import precision_score, recall_score, f1_score, classification_report
from keras.models import load_model

GOLD_TERMS_DATASET_FILE_PATH='/home/ikaraman/Desktop/oxfordDictionary/biologyOxford.txt'
SENTENCES_DATASET='/archive/EnglishSentencesDataset/Biology.txt'
FIELD_NAME='biology'

# GOLD_TERMS_DATASET_FILE_PATH = '/home/ikaraman/Desktop/oxfordDictionary/chemistry.txt'
# SENTENCES_DATASET = '/archive/EnglishSentencesDataset/Chemistry.txt'
# FIELD_NAME = 'chemistry'

MAX_SENTENCE_COUNT = 40000
EPOCH = 5

################# DEFINE TAGS #####################
BILOU_TAGS = ["B", "I", "L", "O", "U"]
n_tags = len(BILOU_TAGS)
tag2idx = {t: i for i, t in enumerate(BILOU_TAGS)}

################# RANDOM SEED #####################
# fix random seed for reproducibility
numpy.random.seed(7)

############### READ TERMS FROM DATASET #################
dataset1GramTerms = []
dataset2GramTerms = []
dataset3GramTerms = []
dataset4GramTerms = []
termsDatasetFile = open(GOLD_TERMS_DATASET_FILE_PATH, 'r')
goldTermCount = 0
for term in termsDatasetFile.readlines():
    # if goldTermCount==2000:
    #     break
    if term.strip():
        term = term.translate(str.maketrans('', '', string.punctuation)).lower().strip()
        if len(term.strip()) < 3:
            continue
        goldTermCount = goldTermCount + 1
        ## TODO replace numbers like (1849-1936)
        wordCount = len(term.split())
        if wordCount is 1:
            dataset1GramTerms.append(term)
        if wordCount is 2:
            dataset2GramTerms.append(term)
        if wordCount is 3:
            dataset3GramTerms.append(term)
        if wordCount is 4:
            dataset4GramTerms.append(term)

# ################ READ SENTENCES ####################
# file = open('/home/ikaraman/Desktop/myfile.txt', 'r')
file = open(SENTENCES_DATASET, 'r')
sentences = file.readlines()

################### TAGGING #############################
sentencesAndTags = []
sentenceCount = 0
for sentence in sentences:
    print("Tagging Progress: ", str(sentenceCount) + "/" + str(len(sentences)))
    if sentenceCount == MAX_SENTENCE_COUNT:
        break
    sentence = sentence.translate(str.maketrans('', '', string.punctuation)).lower().strip()
    ######## if one word in the sentence, discard
    words = sentence.split()
    if len(words) == 1:
        continue
    #### Add <SOS> to begining, Add <EOS> to end
    sentence = " <SOS> " + sentence + " <EOS>"
    # +2 for SOS and EOS
    # initialize BILOU tags as all non terms
    tags = ['O' for i in range(len(words) + 2)]

    copySentence = str(sentence)
    for term4gram in dataset4GramTerms:
        term4gram = " " + str(term4gram) + str(" ")
        if term4gram in copySentence:
            copySentence = copySentence.replace(term4gram, " <B> <I> <I> <L> ")
    for term3gram in dataset3GramTerms:
        term3gram = " " + str(term3gram) + str(" ")
        if term3gram in copySentence:
            copySentence = copySentence.replace(term3gram, " <B> <I> <L> ")
    for term2gram in dataset2GramTerms:
        term2gram = " " + str(term2gram) + str(" ")
        if term2gram in copySentence:
            copySentence = copySentence.replace(term2gram, " <B> <L> ")
    for term1gram in dataset1GramTerms:
        term1gram = " " + str(term1gram) + str(" ")
        if term1gram in copySentence:
            copySentence = copySentence.replace(term1gram, " <U> ")

    # +2 for SOS and EOS
    # initialize BILOU tags as all non terms
    tags = ['O' for i in range(len(words) + 2)]
    i = 0
    for word in copySentence.split():
        if word in ["<B>", "<I>", "<L>", "<U>"]:
            tags[i] = word.replace("<", "").replace(">", "")
        else:
            print(word)
        i = i + 1
    sentencesAndTags.append((sentence, tags))
    sentenceCount = sentenceCount + 1

print("TAGGING FINISHED.")

# split data
numpy.random.shuffle(sentencesAndTags)
trainingSet, validationSet, testSet = numpy.split(sentencesAndTags,
                                                  [int(len(sentencesAndTags) * 0.8), int(len(sentencesAndTags) * 0.9)])

trainingSet = numpy.concatenate([trainingSet, validationSet])
print("Training set:", len(trainingSet))
print("Test set:", len(testSet))

# find max sentence length
maxLength = 0
for sentencesAndTags in trainingSet:
    wordCount = len(str(sentencesAndTags[0]).split())
    if wordCount > maxLength:
        maxLength = wordCount
if maxLength > 250:
    maxLength = 250

########################### TRAININING PREPARATION ############################
# encode sentences
trainingDocs = [i[0] for i in trainingSet]
# prepare tokenizer
# encode unknown words as 1
t = Tokenizer(oov_token=1)
t.fit_on_texts(trainingDocs)
vocab_size = len(t.word_index) + 1
# integer encode the documents
training_encoded_docs = t.texts_to_sequences(trainingDocs)
print(training_encoded_docs)
# encode tags
trainingTags = [[tag2idx[w] for w in s] for s in [x[1] for x in trainingSet]]

# pad documents to a max length of words
padded_docs = pad_sequences(training_encoded_docs, maxlen=maxLength, padding='post')
# pad also to tag sequence
padded_tags = pad_sequences(maxlen=maxLength, sequences=trainingTags, padding="post", value=3)

padded_tags = [to_categorical(i, num_classes=n_tags) for i in padded_tags]

###################### EMBEDDING ##############################
embeddingLoader = EmbeddingLoader()
embedding_matrix = embeddingLoader.getEmbeddingMatrix('/archive/glove.6B.300d.txt', t)
print("Embedding matrix created.")

############### MODEL CREATION #############################

# define model
model = Sequential()
# add embedding layer to model
model.add(Embedding(vocab_size, 300, weights=[embedding_matrix], input_length=maxLength, trainable=False))
# bidirectionalLSTMLayer = LSTM(units=200, return_sequences=True, recurrent_dropout=0.1)  # variational biLSTM
bidirectionalLSTMLayer=Bidirectional(LSTM(units=200, return_sequences=True, recurrent_dropout=0.1))  # variational biLSTM
outputLayer = TimeDistributed(Dense(n_tags, activation="softmax"))

#
model.add(bidirectionalLSTMLayer)
## TODO iki tane bidirectional layer nasıl olur ?
# model.add(bidirectionalLSTMLayer2)
model.add(outputLayer)

evaluator = Evaluater()
opt = keras.optimizers.Adam(learning_rate=0.001)
### CRFsiz network için
# model.compile(optimizer=opt, loss="categorical_crossentropy",
#               metrics=['acc', evaluator.f1_m, evaluator.precision_m, evaluator.recall_m])
### CRF li network için
model.compile(optimizer=opt, loss='categorical_crossentropy',
              metrics=['acc', evaluator.f1_m, evaluator.precision_m, evaluator.recall_m])
from seqeval.callbacks import F1Metrics

id2label = {0: 'B', 1: 'I', 2: 'L', 3: 'O', 4: 'U'}
callbacks = [F1Metrics(id2label)]

history = model.fit(padded_docs, numpy.array(padded_tags), batch_size=20, epochs=EPOCH, validation_split=0.1,
                    verbose=1,
                    callbacks=callbacks)
print("Model created.")

# #### save model
# model.save('/archive/LSTMModels/modelByBiology.h5')  # creates a HDF5 file 'my_model.h5'
# #
# ### save tokenizer
# import pickle
# # saving
# with open('/archive/LSTMModels/biologyTokenizer.pickle', 'wb') as handle:
#     pickle.dump(t, handle, protocol=pickle.HIGHEST_PROTOCOL)
# tokenizer_json = t.to_json()
# # with open('/archive/LSTMModels/biologyTokenizer.json', 'w', encoding='utf-8') as f:
# #     f.write(json.dumps(tokenizer_json, ensure_ascii=False))

########################### TEST PREPERATION ########################
# encode sentences
testDocs = [i[0] for i in testSet]
# integer encode the documents
test_encoded_docs = t.texts_to_sequences(testDocs)
print(test_encoded_docs)
# encode tags
testTags = [[tag2idx[w] for w in s] for s in [x[1] for x in testSet]]

# pad documents to a max length of words
test_padded_docs = pad_sequences(test_encoded_docs, maxlen=maxLength, padding='post')
# pad also to tag sequence
test_padded_tags = pad_sequences(maxlen=maxLength, sequences=testTags, padding="post", value=3)

####||||||||||||||| evaluate on TEST DATA ########################
target_test = test_padded_tags.tolist()
y_pred = model.predict_classes(test_padded_docs)
y_pred = y_pred.tolist()

flat_target_list = []
for sublist in target_test:
    for item in sublist:
        flat_target_list.append(item)

flat_prediction_list = []
for sublist in y_pred:
    for item in sublist:
        flat_prediction_list.append(item)

for i in range(0, len(flat_target_list)):
    flat_target_list[i] = BILOU_TAGS[flat_target_list[i]]
for i in range(0, len(flat_prediction_list)):
    flat_prediction_list[i] = BILOU_TAGS[flat_prediction_list[i]]
# using naive method to
# perform conversion
print(classification_report(flat_target_list, flat_prediction_list))

extractedTerms = []
i = 0
for pred in y_pred:
    testSentence = testDocs[i]
    testSentenceWords = testSentence.split()
    tagPredictions = y_pred[i]
    j = 0
    previousTag = ""
    for tagPrediction in tagPredictions:
        term = ""
        if 'B' == BILOU_TAGS[tagPrediction]:
            extractedTerms.append(testSentenceWords[j])
            previousTag = 'B'
        if 'I' == BILOU_TAGS[tagPrediction]:
            if previousTag in ['B', 'I']:
                lastWord = extractedTerms[len(extractedTerms) - 1]
                extractedTerms[len(extractedTerms) - 1] = lastWord + " " + (testSentenceWords[j])
            previousTag = 'I'
        if 'L' == BILOU_TAGS[tagPrediction]:
            if previousTag in ['B', 'I']:
                lastWord = extractedTerms[len(extractedTerms) - 1]
                extractedTerms[len(extractedTerms) - 1] = lastWord + " " + (testSentenceWords[j])
            previousTag = 'L'
        if 'U' == BILOU_TAGS[tagPrediction]:
            extractedTerms.append(testSentenceWords[j])
            previousTag = 'U'
        j = j + 1
    i = i + 1

extractedTerms = set(extractedTerms)
print(set(extractedTerms))
outputFilePath = '/home/ikaraman/Desktop/ExtractedTermsWithLSTM/' + FIELD_NAME + 'Terms.txt'
with open(outputFilePath, 'w') as outputFile:
    for term in extractedTerms:
        outputFile.write(term + '\n')

print("Written file path:",outputFilePath)
print("Extracted term count:", len(extractedTerms))
print("Training sentence count:", len(trainingSet))
print("Epoch:",EPOCH)


import logging
from pathlib import Path
import time

def timeLogger(func):
    currDate = time.strftime("%Y-%m-%d", time.localtime())
    currTime = time.strftime("%H-%M-%S", time.localtime())
    Path(f"/var/log/ownlog/python-{currDate}/").mkdir(parents=True, exist_ok=True)
    outputPath = f"/var/log/ownlog/python-{currDate}/{currTime}.log"

    logging.basicConfig(filename=outputPath,
                        level=logging.DEBUG,
                        datefmt="%Y-%m-%d %H:%M:%S",
                        format="time function called by %(message)s at %(asctime)s"
                        )
    logging.info(f'{func} {func.__name__}')
    return func

@timeLogger
def func():
    print('done')

if __name__ == '__main__':
    func()


# coding: utf-8

# In[40]:

################################JP 29th august 2015
################################This code takes and runs stuff on the cluster!!!!
import os
import re
import json
from time import ctime
import string
import shutil
maxSplaunchRun=30
currFolder = os.getcwd() + '/'
targetFolder = 'data/proxDist/'
outputFolder = currFolder+'outputReports'
if (os.path.exists(outputFolder) != 1): ###Here we check that the folder exists.
        os.mkdir(outputFolder)

# In[ ]:

# In[41]:

#################################################READ JSON FILE
jsonString = open('clusterRegRunnerConfig.json').read()
parsedJson = json.loads(jsonString)
parsedJson.keys()

##################################################CONTROL OUTPUT FILE:
fileToSaveOutputRoot = 'outputFileTransfer' ####Create a better name
time= ctime()
time = time.translate(None, string.punctuation)
time = time.replace(" ", "")
fileToSaveOutputTime = time
fileToSaveOutputSuffix = '_OUT.txt'
fileToSaveOutput= fileToSaveOutputRoot + fileToSaveOutputTime + fileToSaveOutputSuffix

fileToSaveOutputFullPath = currFolder + fileToSaveOutput

if (os.path.isfile(fileToSaveOutputFullPath) == True): ###Here we check that the folder exists.
    os.remove(fileToSaveOutputFullPath)

####Need to delete it if it already exists

outFile = open(fileToSaveOutputFullPath,'a')

outFile.write('This is a test')

# In[42]:

foldersToSearch = os.listdir(targetFolder)
##Now we filter the files so that they are only files that we are interested in:
matchPattern = '[A-Z]{1,4}[0-9]{1,4}'  ##the outline for acceptable files  [A-D]{1-4}_
foldersToSearch = [elem for elem in foldersToSearch if re.search(matchPattern, elem) != None]

#print number summary
outputMessage = '-----There are the following number of mice: ' + str(len(foldersToSearch)) + '\n'

outFile.write(outputMessage)
#####Here we print output.
outputMessage = '-----Mice being searched: ' + str(foldersToSearch) + '\n' #new line is critical.
outFile.write(outputMessage)
miceToSearch = foldersToSearch

for mouse in range(len(miceToSearch)):
    print(foldersToSearch[mouse])
    sessionFolder = targetFolder+miceToSearch[mouse]+'/'
    sessionsToSearch = os.listdir(sessionFolder)
    ##Now we filter the files so that they are only files that we are interested in:
    matchPattern = '[A-Z]{1,4}[0-9]{1,4}_S[0-9]{1,3}_F[0-9]{1,3}'  ##the outline for acceptable files  [A-D]{1-4}_
    sessionsToSearch = [elem for elem in sessionsToSearch if re.search(matchPattern, elem) != None]

    #print number summary
    outputMessage = '----------There are the following number of sessions: ' + str(len(sessionsToSearch)) + '\n'

    outFile.write(outputMessage)
    #####Here we print output.
    outputMessage = '----------Sessions being searched: ' + str(sessionsToSearch) + '\n' #new line is critical.
    outFile.write(outputMessage)

    print(sessionsToSearch)

    ############################################Now we iterate through each individual sessions registeredDirectory....

    for session in range(len(sessionsToSearch)):

        regFolder = sessionFolder + sessionsToSearch[session] + '/registeredDirectory/'
        filesToSearch = os.listdir(regFolder)
        ##Now we filter the files so that they are only files that we are interested in:
        matchPattern = 'OUTPUTREPORT'  ##the outline for acceptable files  [A-D]{1-4}_
        filesToSearch = [elem for elem in filesToSearch if re.search(matchPattern, elem) != None]

        #print tnumber summary
        outputMessage = '----------There are the following number of output reports: ' + str(len(filesToSearch)) + '\n'
        print(outputMessage)
        outFile.write(outputMessage)
        #####Here we print output.
        outputMessage = '----------Sessions being searched: ' + str(filesToSearch) + '\n' #new line is critical.
        outFile.write(outputMessage)

        #######################Now we move these things to the report
        for fileId in range(len(filesToSearch)):
            fileToMove = regFolder+filesToSearch[fileId]

            shutil.copy(fileToMove, outputFolder)

# In[43]:

outFile.close() ####Close the text output file.


"""
Define the recurrent entity network model.
"""
from __future__ import absolute_import
from __future__ import print_function
from __future__ import division

from functools import partial
import time

import tensorflow as tf
from tensorflow.contrib.learn import ModelFnOps, ModeKeys

from .dynamic_relational_memory_cell import DynamicRelationalMemoryCell
from .dynamic_memory_cell import DynamicMemoryCell, dmc_output_module
from .model_ops import cyclic_learning_rate, \
                       get_sequence_length, \
                       count_parameters, \
                       prelu

OPTIMIZER_SUMMARIES = [
    "learning_rate",
    "loss",
    "gradients",
    "gradient_norm",
]

DTYPE = tf.float32

def get_input_encoding(inputs, initializer=None, scope=None):
    """
    Implementation of the learned multiplicative mask from Section 2.1, Equation 1.
    This module is also described in [End-To-End Memory Networks](https://arxiv.org/abs/1502.01852)
    as Position Encoding (PE). The mask allows the ordering of words in a sentence to affect the
    encoding.
    """
    with tf.variable_scope(scope, 'Encoding', initializer=initializer):
        _, _, max_sentence_length, embedding_size = inputs.get_shape().as_list()
        positional_mask = tf.get_variable(
            name='positional_mask',
            shape=[max_sentence_length, embedding_size],
            dtype=DTYPE)
        encoded_input = tf.reduce_sum(inputs * positional_mask, axis=2)
        return encoded_input

def simple_output_module(
        last_state,    # ([batch_size, num_blocks * block_size], [batch_size, num_blocks, num_blocks, block_size]) tuple
        entity_indexes,  # [batch_size, num_blocks, num_blocks] mask tensor. 1 element of each num_blocks dim set
        num_blocks,
        output_size,   # number of labels
        activation=tf.nn.relu,
        initializer=None,
        scope=None):
    with tf.variable_scope(scope, 'Output', initializer=initializer):
        ent_state, rel_state = last_state
        # ent_state = [batch_size, num_blocks, block_size]
        ent_state = tf.stack(tf.split(ent_state, num_blocks, axis=1), axis=1)
        _, _, block_size = ent_state.get_shape().as_list()
        # C converts the concatenated [entity i memory, entity j memory, ij rel_memory] memory vectors into a single
        # block_size vector
        C = tf.get_variable('C', [3 * block_size, block_size], dtype=DTYPE)

        # rel_memories is a list of num_blocks lists of num_blocks tensors with shape [batch_size, block_size]
        # rel_memories[i][j] is a tensor with shape [batch_size, block_size] representing the relational memory r_ij
        rel_memories = [tf.unstack(rel_memory_i, num_blocks, axis=1)
                        for rel_memory_i in tf.unstack(rel_state, num_blocks, axis=1)]
        full_memory = []
        # ent_memories is a list of num_blocks tensors with shape [batch_size, block_size]
        ent_memories = tf.unstack(ent_state, num_blocks, axis=1)
        for i, memory_i in enumerate(ent_memories):
            full_memory_i = []
            for j, memory_j in enumerate(ent_memories):
                full_memory_i.append(tf.matmul(tf.concat([memory_i, memory_j, rel_memories[i][j]], 1), C))
            full_memory.append(tf.stack(full_memory_i, axis=1))
        # full_memory = p = [batch_size, num_blocks, num_blocks, block_size] tensor
        full_memory = tf.stack(full_memory, axis=1)

        # select relational memory for two related entities
        # [batch_size, block_size]
        query = tf.boolean_mask(full_memory, tf.cast(entity_indexes, tf.bool))
        print('query: %s' % query)

        Z = tf.get_variable('Z', [block_size, output_size], dtype=DTYPE)
        y = tf.nn.relu(tf.matmul(query, Z, name='logits'))
        return y

def get_output_module(
        last_state,    # ([batch_size, num_blocks * block_size], [batch_size, num_blocks, num_blocks, block_size]) tuple
        entity_indexes,  # [batch_size, num_blocks] mask tensor. Exactly two elements of each batch should be 1.
        num_blocks,
        output_size,   # number of labels
        activation=tf.nn.relu,
        initializer=None,
        scope=None):
    """
    Implementation of Output Module in section 2 of the RelNet paper.
    """
    with tf.variable_scope(scope, 'Output', initializer=initializer):
        ent_state, rel_state = last_state
        # ent_state = [batch_size, num_blocks, block_size]
        ent_state = tf.stack(tf.split(ent_state, num_blocks, axis=1), axis=1)
        _, _, block_size = ent_state.get_shape().as_list()
        # C converts the concatenated [entity i memory, entity j memory, ij rel_memory] memory vectors into a single
        # block_size vector
        C = tf.get_variable('C', [3 * block_size, block_size], dtype=DTYPE)

        # rel_memories is a list of num_blocks lists of num_blocks tensors with shape [batch_size, block_size]
        # rel_memories[i][j] is a tensor with shape [batch_size, block_size] representing the relational memory r_ij
        rel_memories = [tf.unstack(rel_memory_i, num_blocks, axis=1)
                        for rel_memory_i in tf.unstack(rel_state, num_blocks, axis=1)]
        full_memory = []
        # ent_memories is a list of num_blocks tensors with shape [batch_size, block_size]
        ent_memories = tf.unstack(ent_state, num_blocks, axis=1)
        for i, memory_i in enumerate(ent_memories):
            full_memory_i = []
            for j, memory_j in enumerate(ent_memories):
                full_memory_i.append(tf.matmul(tf.concat([memory_i, memory_j, rel_memories[i][j]], 1), C))
            full_memory.append(tf.stack(full_memory_i, axis=1))
        # full_memory = p = [batch_size, num_blocks, num_blocks, block_size] tensor
        full_memory = tf.stack(full_memory, axis=1)

        # select the two related entities
        query = tf.boolean_mask(ent_state, tf.cast(entity_indexes, tf.bool))
        query = tf.stack(tf.split(query, 2, axis=0, num=2), axis=1)
        # query = ent_state * tf.expand_dims(tf.cast(entity_indexes, tf.float32), axis=-1)

        # average the two remaining entity memories together
        # query = [batch_size, 1, block_size]
        query = tf.reduce_sum(query, axis=1, keep_dims=True) / 2.0
        # TODO: learn a combination of the two remaining entity memories together
        # # query = [batch_size, 1, block_size]
        # Q = tf.get_variable('Q', [])

        # Use the encoded_query to attend over memories
        # (hidden states of dynamic last_state cell blocks)
        # alignments = [batch_size, num_blocks, num_blocks]
        alignments = tf.reduce_sum(full_memory * tf.expand_dims(query, axis=1), axis=-1)

        # Subtract max for numerical stability (softmax is shift invariant)
        alignments_max = tf.reduce_max(alignments, axis=-1, keep_dims=True)
        alignments = tf.nn.softmax(alignments - alignments_max)
        # alignments = [batch_size, num_blocks, num_blocks, 1]
        alignments = tf.expand_dims(alignments, axis=-1)

        # Weight memories by attention vectors
        # attention = u = [batch_size, block_size]
        attention = tf.reduce_sum(tf.reduce_sum(full_memory * alignments, axis=2), axis=1)

        # Z acts as the decoder matrix to convert from internal state to the output vocabulary size
        Z = tf.get_variable('Z', [block_size, output_size], dtype=DTYPE)
        H = tf.get_variable('H', [block_size, block_size], dtype=DTYPE)

        q = tf.squeeze(query, axis=1)
        y = tf.matmul(activation(q + tf.matmul(attention, H)), Z)
        return y

def get_outputs(inputs, params):
    """
    Return the outputs from the model which will be used in the loss function.
    :param inputs:
    :param params:
    :return:
    """
    use_attrs = params['use_attrs']
    embedding_size = params['embedding_size']
    num_blocks = params['num_blocks']
    vocab_size = params['vocab_size']
    num_labels = params['num_labels']
    use_full_network = not params['ent_net']
    do_norm = params['do_norm']
    sum_attrs = params['sum_attrs']

    story = inputs['story']  # [?, num_sentences, max_sentence_len]
    entity_indexes = inputs['entity_indexes']  # [?, num_blocks, num_blocks]
    key_indexes = inputs['keys']  # [?, num_blocks]

    batch_size = tf.shape(story)[0]

    normal_initializer = tf.random_normal_initializer(stddev=0.1)
    ones_initializer = tf.constant_initializer(1.0, dtype=DTYPE)

    # Extend the vocab to include keys for the dynamic memory cell,
    # allowing the initialization of the memory to be learned.
    vocab_size = vocab_size + num_blocks

    with tf.variable_scope('RelNet', initializer=normal_initializer):
        # PReLU activations have their alpha parameters initialized to 1
        # so they may be identity before training.
        alpha = tf.get_variable(
            name='alpha',
            shape=embedding_size,
            initializer=ones_initializer)
        activation = partial(prelu, alpha=alpha)

        # Embeddings
        embedding_params = tf.get_variable(
            name='embedding_params',
            shape=[vocab_size, embedding_size],
            dtype=DTYPE)

        # The embedding mask forces the special "pad" embedding to zeros.
        embedding_mask = tf.constant(
            value=[0 if i == 0 else 1 for i in range(vocab_size)],
            shape=[vocab_size, 1],
            dtype=DTYPE)
        embedding_params_masked = embedding_params * embedding_mask

        # [?, num_sentences, max_sentence_len, embedding_size]
        story_embedding = tf.nn.embedding_lookup(embedding_params_masked, story)
        if use_attrs:
            # in this case, story embedding is [?, num_sentences, max_sentence_len, num_attrs+1, embedding_size]
            if sum_attrs:
                # sum over the attr dimension to get [?, num_sentences, max_sentence_len, embedding_size]
                story_embedding = tf.reduce_sum(story_embedding, axis=3)
            else:
                # after the concat operation it is [?, num_sentences, max_sentence_len, (num_attrs+1) * embedding_size]
                story_embedding = tf.concat(tf.unstack(story_embedding, axis=-1), axis=-1)
                # finally, we have [?, num_sentences, max_sentence_len, embedding_size]
                story_embedding = tf.layers.dense(story_embedding, embedding_size, activation=None,
                                                  name='attr_embedding')
        print('story: %s', story_embedding)

        # Input Module - [?, num_sentences, embeddings_size]
        encoded_story = get_input_encoding(
            inputs=story_embedding,
            initializer=ones_initializer,
            scope='StoryEncoding')
        if use_full_network:
            rel_encoded_story = get_input_encoding(
                inputs=story_embedding,
                initializer=ones_initializer,
                scope='RelStoryEncoding')
            print('enc stories: %s, %s' % (encoded_story, rel_encoded_story))

        # Memory Module
        keys = tf.nn.embedding_lookup(embedding_params_masked, key_indexes)  # [?, num_blocks, block_size]
        if use_attrs:
            # in this case, keys is [?, num_blocks, num_attrs+1, block_size]
            if sum_attrs:
                keys = tf.reduce_sum(keys, axis=2)
            else:
                # after the concat operation it is [?, num_blocks, (num_attrs+1) * block_size]
                keys = tf.concat(tf.unstack(keys, axis=-1), axis=-1)
                # finally, we have [?, num_blocks, block_size]
                keys = tf.layers.dense(keys, embedding_size, activation=None, name='attr_embedding', reuse=True)
        print('keys: %s' % keys)
        keys = tf.unstack(keys, num_blocks, axis=1)
        # keys = tf.split(keys, num_blocks, axis=0)
        # keys = [tf.squeeze(key, axis=0) for key in keys]

        if use_full_network:
            cell = DynamicRelationalMemoryCell(
                num_blocks=num_blocks,
                num_units_per_block=embedding_size,
                keys=keys,
                initializer=normal_initializer,
                recurrent_initializer=normal_initializer,
                activation=activation,
                rel_activation=tf.nn.relu,
                dtype=DTYPE,
                do_norm=do_norm)
        else:
            cell = DynamicMemoryCell(
                num_blocks=num_blocks,
                num_units_per_block=embedding_size,
                keys=keys,
                initializer=normal_initializer,
                recurrent_initializer=normal_initializer,
                activation=activation,
                dtype=DTYPE)

        # Recurrence
        initial_state = cell.zero_state(batch_size, DTYPE)

        # with drnn
        inputs = (encoded_story, rel_encoded_story) if use_full_network else encoded_story
        sequence_length = get_sequence_length(encoded_story)
        _, final_state = tf.nn.dynamic_rnn(
            cell=cell,
            inputs=inputs,
            sequence_length=sequence_length,
            initial_state=initial_state)

        # Output Module
        if use_full_network:
            outputs = simple_output_module(
                last_state=final_state,
                entity_indexes=entity_indexes,
                num_blocks=num_blocks,
                output_size=num_labels,
                initializer=normal_initializer,
                activation=activation)
        else:
            outputs = dmc_output_module(
                last_state=final_state,
                entity_indexes=entity_indexes,
                num_blocks=num_blocks,
                output_size=num_labels,
                initializer=normal_initializer,
                activation=activation)
        parameters = count_parameters()
        print('Parameters: {}'.format(parameters))

        return outputs

def get_predictions(outputs):
    """Return the actual predictions for use with evaluation metrics or TF Serving."""
    predictions = tf.argmax(outputs, axis=-1)
    return predictions

def get_loss(outputs, labels, mode):
    """Return the loss function which will be used with an optimizer."""

    loss = None
    if mode == ModeKeys.INFER:
        return loss

    loss = tf.losses.sparse_softmax_cross_entropy(
        logits=outputs,
        labels=labels)

    return loss

def get_train_op(loss, params, mode):
    """Return the training operation which will be used to train the model."""

    train_op = None
    if mode != ModeKeys.TRAIN:
        return train_op

    global_step = tf.contrib.framework.get_or_create_global_step()

    learning_rate = cyclic_learning_rate(
        learning_rate_min=params['learning_rate_min'],
        learning_rate_max=params['learning_rate_max'],
        step_size=params['learning_rate_step_size'],
        mode='triangular',
        global_step=global_step)
    tf.summary.scalar('learning_rate', learning_rate)

    train_op = tf.contrib.layers.optimize_loss(
        loss=loss,
        global_step=global_step,
        learning_rate=learning_rate,
        optimizer='Adam',
        clip_gradients=params['clip_gradients'],
        gradient_noise_scale=params['gradient_noise_scale'],
        summaries=OPTIMIZER_SUMMARIES)

    return train_op

def model_fn(features, labels, mode, params):
    """Return ModelFnOps for use with Estimator."""

    start = time.time()
    outputs = get_outputs(features, params)
    predictions = get_predictions(outputs)
    loss = get_loss(outputs, labels, mode)
    train_op = get_train_op(loss, params, mode)
    print('Model built. Took %s seconds' % (time.time() - start))

    return ModelFnOps(
        predictions=predictions,
        loss=loss,
        train_op=train_op,
        mode=mode)


import os
import sys
currentPath = os.path.dirname(os.path.realpath(__file__))
sys.path.append(currentPath.replace("\manualreview", ""))
from manualreview import rawscriptsmenu
from PyQt5 import QtWidgets
from manualreview import client
import sys
import atexit
import datetime
import configparser
from manualreview import settings
from datetime import timedelta
from manualreview import videoscriptcore
currentPath = os.path.dirname(os.path.realpath(__file__))

def loadVideoScripts():
    pass

class App():
    def __init__(self):
        application = QtWidgets.QApplication(sys.argv)
        application.processEvents()
        loginWindow = client.LoginWindow()
        loginWindow.show()

        #new = rawscriptsmenu.ScriptsMenu()
        #new.show()
        sys.exit(application.exec_())
        client.safeDisconnect()

sys._excepthook = sys.excepthook
def exception_hook(exctype, value, traceback):
    sys._excepthook(exctype, value, traceback)
    print("loooooooooooool")
    sys.exit(1)
sys.excepthook = exception_hook

def init():

    os.chdir(currentPath)

    if not os.path.exists(settings.thumbnailpath):
        os.mkdir(settings.thumbnailpath)

    if not os.path.exists(settings.scriptsaves):
        os.mkdir(settings.scriptsaves)

    app = App()

def exit_handler():
    print("Safe Exit")
    client.sock.close()

if __name__ == "__main__":
    atexit.register(exit_handler)
    settings.generateConfigFile()
    init()


import turtle

def main():
    i = 1
    flag = False
    k = 0.5

    turtle.left(90)

    while i < 9:
        draw_circle(k)

        if flag:
            k += 0.1
            flag = False
        else:
            flag = True
            i += 1

def draw_circle(k):
    i = 0

    turtle.shape('turtle')

    turtle.left(180)

    while i < 360:
        turtle.forward(k)
        turtle.left(1)

        i += 1

main()
input()


# -*- encoding: utf-8 -*-

"""
@File    : CardPole.py
@Time    : 2020/8/26 下午7:02
@Author  : dididididi
@Email   :
@Software: PyCharm
"""

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.optimizers import Adam
from collections import deque
import numpy as np
import random
import gym

episode = 5000

class DQNAgent:
    def __init__(self, state_size, action_size):
        self.state_size = state_size
        self.action_size = action_size
        self.memory = deque(maxlen=2000)
        self.gamma = 0.95
        self.epsilon_max = 1.
        self.epsilon_min = 0.01
        self.epsilon_decay = 0.995
        self.learning_rate = 0.001
        self.model = self.build_model()

    def build_model(self):
        # 创建一个构造器
        model = Sequential()
        model.add(Dense(24, input_dim=self.state_size, activation='relu'))
        model.add(Dense(24, activation='relu'))
        model.add(Dense(self.action_size, activation='linear'))
        model.compile(loss='mse', optimizer=Adam(lr=self.learning_rate), metrics=['accuracy'])

        return model

    def remember(self, state, action, reward, next_state, done):
        self.memory.append((state, action, reward, next_state, done))

    def act(self, state):
        if np.random.rand() <= self.epsilon_max:
            return random.randrange(self.action_size)
        else:
            q_values = self.model.predict(state)[0]
            return np.argmax(q_values)

    def replay(self, batch_size):
        mini_batch = random.sample(self.memory, batch_size)
        for step, action, reward, next_state, done in mini_batch:
            target = reward
            if not done:
                target = (reward + self.gamma * np.amax(self.model.predict(next_state)[0]))
            target_f = self.model.predict(state)
            target_f[0][action] = target
            self.model.fit(state, target_f, epochs=1, verbose=0)
        if self.epsilon_max > self.epsilon_min:
            self.epsilon_max = self.epsilon_max * self.epsilon_decay

if __name__ == '__main__':
    env = gym.make('CartPole-v1')
    state_size = env.observation_space.shape[0]
    action_size = env.action_space.n
    agent = DQNAgent(state_size, action_size)

    done = False
    batch_size = 8

    for e in range(episode):
        state = env.reset()
        state = np.reshape(state, [1, state_size])
        for time in range(500):
            env.render()
            # 选择动作
            action = agent.act(state)
            # 得到状态和回报
            next_state, reward, done, _ = env.step(action)
            reward = reward if not done else -10
            next_state = np.reshape(next_state, [1, state_size])

            # 记录信息到Q表
            agent.remember(state, action, reward, next_state, done)

            # 更新状态
            state = next_state

            if done:
                print("episode: {}/{}, score: {}, e: {:.2}"
                      .format(e, episode, time, agent.epsilon_max))
                break

            if len(agent.memory) > batch_size:
                agent.replay(batch_size)


#Prints those PDB ID's whose secondary details are not present in 6_A_CHAIN_SECSTR.txt( A chain details extracted fron ss.txt)

with open('6_A_CHAIN_SECSTR.txt','r') as f:
    haystack = f.read()

if not haystack:
    sys.exit("Could not read haystack data")

with open('1_Pisces_40ID_A_CHAIN_ID_only.txt','r') as f1:
	i=0
	j=0
	always_print=False
	for needle in (line.strip() for line in f1):
		if needle not in haystack:
			print(needle)


import spacy
import medspacy
from medspacy.ner import TargetRule
from medspacy.visualization import visualize_ent
import pandas as pd
nlp = medspacy.load()

df = pd.read_csv("saved_csv/event_data.csv", sep = "|")
text = df.text

target_matcher = nlp.get_pipe("medspacy_target_matcher")

target_rules = [
    TargetRule("Hypopyon", "PROBLEM"),
    TargetRule("Endophthalmitis", "PROBLEM", pattern=[{"LOWER": "afib"}]),
    TargetRule("pneumonia", "PROBLEM"),
    TargetRule("Type II Diabetes Mellitus", "PROBLEM",
              pattern=[
                  {"LOWER": "type"},
                  {"LOWER": {"IN": ["2", "ii", "two"]}},
                  {"LOWER": {"IN": ["dm", "diabetes"]}},
                  {"LOWER": "mellitus", "OP": "?"}
              ]),
    TargetRule("Phacoemulsification", "MEDICATION")
]

target_matcher.add(target_rules)

doc = nlp(text[1])
visualize_ent(doc)


import datetime

def print_header():
    print('-----------------------')
    print('BIRTHDAY CALCULATOR APP')
    print('-----------------------')
    print()

def get_birthday_from_user():
    print("When were you born")
    year = int(input("Year: "))
    month = int(input ("month: "))
    day = int(input("day: "))

    return datetime.date(year, month, day)

def compute_days_between_dates(original_date, target_date):
    current_year = datetime.date(target_date.year, original_date.month, original_date.day)
    days = current_year - target_date
    return days.days

def print_birthday_information(days):
    if days < 0:
        print("Your birthday was {} days ago".format(-days))
    elif days > 0:
        print("Your birthday is {} days from now".format(days))
    else:
        print("Happy Birthday!")

def main():
    print_header()
    birthday = get_birthday_from_user()
    current_date = datetime.date.today()
    number_of_days = compute_days_between_dates(current_date, birthday)
    print_birthday_information(number_of_days)

main()


# Create a program that takes some text and returns a list of
# all the characters in the text that are not vowels, sorted in
# alphabetical order.
#
# You can either enter the text from the keyboard or
# initialise a string variable with the string.

# text = input("Enter some text: ")
text = "This is some text"
textSet = set(text)
textSet.discard(' ')
vowels = ('a', 'e', 'i', 'o', 'u')
fSet = frozenset(vowels)

consonant = sorted(set(textSet).difference(fSet))
print(consonant)

found = {}
for char in text:
    if char in vowels:
        found.setdefault(char, 0)
        found[char] += 1

print(found)

Dicts and Sets/Sets - Challenge/Sets - Challenge.py


#!/usr/bin/python3
import pymongo

my_client = pymongo.MongoClient("mongodb://localhost:27017/")
my_db = my_client["runoobdb"]
my_cal = my_db["sites"]

x = my_cal.find_one()
print(x)
print("--------------------------------------------")

# 查找所有
my_cal = my_db["site2"]
lists = my_cal.find()
for list in lists:
    print(list)
print("--------------------------------------------")

# 筛选字段
lists = my_cal.find({}, { "_id": 0, "name": 1, "address": 1})
for list in lists:
    print(list)
print("--------------------------------------------")

# 条件查找
condition = {"name" : "Google"}
lists = my_cal.find(condition)
for list in lists:
    print(list)
print("--------------------------------------------")


#!/usr/bin/env python
import os
import platform
import argparse
import sys
from errors import *

# First of all, we need to get both the complete user's input and the arguments and assign these values to variables
executable = sys.argv[0].split('/')[-1]
arguments = ' '.join(sys.argv[1:])
userInput = executable + ' ' + arguments

# And then, we get our current directory and assign it to the 'path' variable
path = os.getcwd()

# Now, we fill the 'blacklist' with the entrances already in 'blacklist.txt'(if there are any)
# We first check if blacklist.txt already exists in the current directory.
# If it's missing, we create it as a blank file
filename = "blacklist.txt"
if not os.path.isfile(filename):
    open(filename, 'w').close()

# After checking if the file exists we remove any existing blank lines(if there are any)
open("new" + filename, 'w').close()
with open(filename) as fin, open("new" + filename, 'w') as fOut:
    for line in fin:
        if not line.strip():
            continue
        fOut.write(line)
os.remove(filename)
os.rename("new" + filename, filename)

# And then we can proceed to adding the lines in the file blacklist.txt to a list called blacklist
# If the file is empty, we simply create a blank list to be filled later
blacklist = [line.rstrip('\n') for line in open(filename)]

# Now we're going to setup the Argument Parser for this program
parser = argparse.ArgumentParser(description="Update or create CMakeLists.txt on the current directory.")
parser.add_argument('option', metavar='option', type=str, nargs='?',
                    help='Run just "cmakelists.py"(no quotes) if you just want to create or update '
                         'CMakeLists.txt.\n If you want to exclude files from CMakeLists.txt enter '
                         '"cmakelists.py rm files"(no quotes)\n (e.g., cmakelists.py rm '
                         'CMakeCXXCompilerId.cpp feature_tests.c). This command will create a file named '
                         'blacklist.txt(if it does not already exists) and will write the files you gave as '
                         'arguments to it. By doing so, these files will be removed from CMakeLists.txt, if they are '
                         'already in there, otherwise they will just not be added to it. If you want the files you '
                         'excluded from CMakeLists.txt by using the "rm" argument back into it you will have to add '
                         'them manually or you can use the "add" argument followed by the file-names. '
                         '(e.g., cmakelists.py add main.cpp matrix.cpp)')
parser.add_argument('files', type=str, nargs='*',
                    help='If you used the "rm" argument, follow it by the name(s) of the file(s) that you '
                         'want to exclude from CMakeLists.txt \n(e.g., cmakelists.py rm '
                         'CMakeCXXCompilerId.cpp feature_tests.c). And if you used the "add" argument follow it by '
                         'the name(s) of the file(s) that you want back into CMakeLists.txt (e.g., '
                         'cmakelists.py add main.cpp matrix.cpp)')

args = parser.parse_args()

# If the option argument was given but no files argument(s) were given, we print the Argument Parser help
#  and raise an InvalidArgumentError
validArguments = ["add", "rm"]

def files_validation(files):
    """

    :type files: list
    """
    for f in files:
        if f[-2:] != '.c' and f[-4:] != '.cpp':
            return True
    return False

# Now, if any of the arguments is invalid, we print the Argument Parser help and raise an InvalidArgumentError
if args.option is not None and (args.option not in validArguments or files_validation(args.files)):
    parser.print_help()
    InvalidArgument = InvalidArgumentError(arguments, "Invalid(s) argument(s) were given. Please read the help above "
                                                      "or enter 'cmakelists.py -h' or 'cmakelists.py --help' "
                                                      "to read it again")
    raise InvalidArgument
# And if no argument is given for 'files', we print the Argument Parser help and raise an MissingArgumentError
elif not args.files and args.option is not None:
    parser.print_help()
    MissingArgument = MissingArgumentError(arguments, "Missing 'files' argument(s). Please read the help above "
                                                      "or enter 'cmakelists.py -h' or 'cmakelists.py --help' "
                                                      "to read it again")
    raise MissingArgument

# If the 'option' argument used was "rm", we add the files used as arguments to the blacklist
if args.option == "rm":
    for file in args.files:
        blacklist.append(file)
# And if the 'option' argument used was "add" we remove the files used as arguments from the blacklist
elif args.option == "add":
    for file in args.files:
        try:
            blacklist.remove(file)
        except ValueError:
            pass

# Before updating blacklist.txt, we remove all possible duplicates in the blacklist
new_blacklist = []
for n in blacklist:
    if n not in new_blacklist:
        new_blacklist.append(n)
blacklist = new_blacklist

# Now we can update blacklist.txt by adding the files passed as arguments
with open(filename, 'w+') as fin:
    for file in blacklist:
        fin.write(file + '\n')

# We need to now check whether blacklist is empty or not. If it's indeed empty, we remove blacklist.txt
if not blacklist:
    os.remove(filename)

# Begin of the creation and edition of the file CMakeLists.txt"
filename = "CMakeLists.txt"
open(filename, 'w').close()
fin = open(filename, "w+")
fin.write("cmake_minimum_required(VERSION 3.7)\n")
COrCppFound = False
for dirpath, dirnames, filenames in os.walk(path):
    for name in filenames:
        if platform.system() == "Windows":
            dirpath = dirpath.replace('\\', '/')
        if name[-2:] == ".c":
            if name not in blacklist:
                COrCppFound = True
                fin.write("add_executable (" + name[:-2] + " " + dirpath + "/" + name + ")\n")
        elif name[-4:] == ".cpp":
            if name not in blacklist:
                COrCppFound = True
                fin.write("add_executable (" + name[:-4] + " " + dirpath + "/" + name + ")\n")

fin.close()

# Now we check if C or C++ files were found(excluding the ones on the blacklist)
if not COrCppFound and not blacklist:
    fileError = COrCppFileNotFoundError(userInput, "No C(.c) or C++(.cpp) files were found "
                                                   "in the current directory:" + path + " "
                                                   "Please make sure to run this program on a "
                                                   "directory that contains C or C++ source files. "
                                                   "Accepted extensions are: .c for C source files and .cpp for C++ "
                                                   "source files")
    raise fileError


import logging
from sys import maxsize
from typing import List

from solvers.madrich.problems.vrp_demo.models import Problem, Route, Job

def insert_greedy(job: Job, routes: List[Route], problem: Problem) -> bool:
    logging.info('Greedy insert started')
    for route in routes:
        for i in range(len(route) + 1):
            jobs = route.jobs[:i] + [job] + route.jobs[i:]
            state = problem.get_state(route.new_route(jobs))
            if state is None:
                continue

            route.jobs = jobs
            logging.info('Greedy insert positive')
            return True
    logging.info('Greedy insert negative')
    return False

def insert_best(job: Job, routes: List[Route], problem: Problem) -> bool:
    logging.info('Insert started')
    best_insert = (-1, -1)
    best_time = maxsize

    for i, route in enumerate(routes):
        old_tt = route.travel_time
        for j in range(len(route) + 1):
            jobs = route.jobs[:j] + [job] + route.jobs[j:]
            state = problem.get_state(route.new_route(jobs))
            if state is None:
                continue
            new_time = state.travel_time - old_tt
            if new_time < best_time:
                best_time = new_time
                best_insert = (i, j)

    if best_insert[0] == -1:
        logging.info('Insert negative')
        return False

    i, j = best_insert
    route = routes[i]
    route.jobs = route.jobs[:j] + [job] + route.jobs[j:]
    logging.info('Insert positive')
    return True


from cassandra.cluster import Cluster
import sys

def lecture(numtest, numprocess):

	rows = session.execute("""
		SELECT registre
		FROM mrc_system.mrc
	""")

	total = 0

	for row in rows:
		total += row[0]

	print (total)

cluster = Cluster()
session = cluster.connect()

try:
	numprocess = int(sys.argv[1])
	numtest = int(sys.argv[2])
	lecture(numtest,numprocess)
except IndexError:
	print ("Erreurs dans la notation des arguments")

cluster.shutdown()


import webbrowser

# This Movie model was based on Movie class wrote on Udacity class.
class Movie():

    """
    This class provides a way to store movie related information

    This Movie model was based on Movie class wrote on Udacity class.

    Attributes:
        attr1 (str): This attribute represents the movie's title.
        attr2 (str): This attribute represents the movie's storyline.
        attr3 (str): This attribute represents the movie's poster image url
                     With this attribute is possible to show the
                     movie poster.
        attr4 (str): This attribute represents the movie's youtube url
                     With this attribute is possible to show the movie
                     trailer.
    """

    def __init__(self, title, storyline, poster_image_url,
                 trailer_youtube_url):
        self.title = title
        self.storyline = storyline
        self.poster_image_url = poster_image_url
        self.trailer_youtube_url = trailer_youtube_url

    def show_trailer(self):
        webbrowser.open(self.trailer_youtube_url)


import gym.spaces
import matplotlib.pyplot as plt
import numpy as np

from qlearning_agent import QLearningAgent
from sarsa_agent import SarsaAgent
from expected_v_sarsa_agent import ExpectedValueSarsaAgent

def play_and_train(env, agent, t_max=10 ** 4):
    """ This function should
    - run a full game (for t_max steps), actions given by agent
    - train agent whenever possible
    - return total reward
    """
    total_reward = 0.0
    state = env.reset()
    for step in range(t_max):
        a = agent.get_action(state)
        new_state, reward, done, _ = env.step(a)
        agent.update(state, a, new_state, reward)
        total_reward += reward
        state = new_state
        if done:
            break

    return total_reward

if __name__ == '__main__':
    max_iterations = 5000
    visualize = False
    # Create Taxi-v2 env
    env = gym.make('Taxi-v2')
    env.reset()
    # env.render()

    n_states = env.env.nS
    n_actions = env.env.nA

    print('States number = %i, Actions number = %i' % (n_states, n_actions))

    # create q learning agent with
    alpha=0.5
    epsilon=0.9
    epsilon_threshold=0.01
    discount=0.99
    get_legal_actions = lambda s: range(n_actions)
    epsilon_ratio = 0.995

    ql_agent = QLearningAgent(alpha, epsilon, discount, get_legal_actions)
    sarsa_agent = SarsaAgent(alpha, epsilon, discount, get_legal_actions)
    expected_sarsa_agent = ExpectedValueSarsaAgent(alpha, epsilon, discount, get_legal_actions)

    plt.figure(figsize=[10, 4])
    rewards_qlearning = []
    rewards_sarsa = []
    rewards_expected_sarsa = []

    # Testing loop
    n = 1
    r_qlearning = []
    r_sarsa = []
    r_expected_sarsa = []
    for _ in range(n):
        # Training loop
        for i in range(max_iterations):
            # Play & train game
            initial_state = env.reset()
            rewards_qlearning.append(play_and_train(env, ql_agent))
            rewards_sarsa.append(play_and_train(env, sarsa_agent))
            rewards_expected_sarsa.append(play_and_train(env, expected_sarsa_agent))
            print(rewards_qlearning[-1], " ", rewards_sarsa[-1], " ", rewards_expected_sarsa[-1])
            # Decay agent epsilon
            epsilon *= epsilon_ratio
            ql_agent.epsilon = epsilon if epsilon >= epsilon_threshold else epsilon_threshold
            sarsa_agent.epsilon = epsilon if epsilon >= epsilon_threshold else epsilon_threshold
            expected_sarsa_agent.epsilon = epsilon if epsilon >= epsilon_threshold else epsilon_threshold

            if False and i % 100 == 0:
                print('Iteration {}, Average reward Q={:.2f} | Sarsa={:.2f} | Expected Value Sarsa={:.2f}, Epsilon {:.3f}'.format(
                    i, np.mean(rewards_qlearning), np.mean(rewards_sarsa), np.mean(rewards_expected_sarsa), ql_agent.epsilon))

            l = len(rewards_qlearning)-10
            r_qlearning.append(np.mean(rewards_qlearning[l:]))
            r_sarsa.append(np.mean(rewards_sarsa[l:]))
            r_expected_sarsa.append(np.mean(rewards_expected_sarsa[l:]))

            if visualize:
                plt.subplot(1, 2, 1)
                plt.plot(rewards_qlearning, color='b', alpha=0.5)
                plt.plot(rewards_sarsa, color='r', alpha=0.5)
                plt.plot(rewards_expected_sarsa, color='g', alpha=0.5)
                plt.xlabel('Iterations')
                plt.ylabel('Total Reward')

                plt.subplot(1, 2, 2)
                plt.hist(rewards_qlearning, bins=20, range=[-700, +20], color='blue', label='rewards_qlearning distribution', alpha=0.7)
                plt.hist(rewards_sarsa, bins=20, range=[-700, +20], color='red', label='rewards_sarsa distribution', alpha=0.7)
                plt.hist(rewards_expected_sarsa, bins=20, range=[-700, +20], color='green', label='rewards_expected_value_sarsa distribution', alpha=0.7)
                plt.xlabel('Reward')
                plt.ylabel('p(Reward)')
                plt.draw()
                plt.pause(0.05)
                plt.cla()

    print('Total: Q={:.2f} | S={:.2f} | ES={:.2f}'.format(np.mean(r_qlearning), np.mean(r_expected_sarsa), np.mean(r_expected_sarsa)))


#!/usr/bin/env python

from state_provider import SmachStateProvider
import smach
import rospy
import ast

def convert_datamodel_to_userdata(ud, initial_state, *args):
    for data_id,data_value in args[0].iteritems():
        try:
            ud[data_id] = ast.literal_eval(data_value)
        except Exception as e:
            ud[data_id] = data_value

class SmachBuilder():
    def __init__(self, interface, provider):
        self._interface = interface
        self._provider = provider
        self.root_SM = None
        self.states_instances = {}

    def create_root_state_machine(self):
        rootInterface = self._interface.rootState
        self.root_SM = smach.StateMachine(rootInterface.final_states)
        with self.root_SM:
            for child_state in rootInterface.states:
                child_state_interface = self.find_matching_interface(child_state)
                self.root_SM.add(child_state_interface.id, self.states_instances[child_state_interface.id], child_state_interface.transitions)
                #for keys in self.states_instances[child_state.id].get_registered_output_keys() + self.states_instances[child_state.id].get_registered_input_keys():
                    #self.root_SM.register_io_keys(keys)
        if(len(rootInterface.data)>0):
            self.root_SM.register_start_cb(convert_datamodel_to_userdata,[rootInterface.data])
        self.root_SM.set_initial_state([rootInterface.initial_state_id], smach.UserData())
        return self.root_SM

    def create_all_compound_states(self):
        states = {}
        for stateInterface in self._interface.compoundStates:
            id, state = self.create_compound_state(stateInterface)
            states[id] = state
        return states

    def find_matching_interface(self, id):
        for interface in self._interface.simpleStates:
            if(interface.id == id):
                return interface
        for interface in self._interface.compoundStates:
            if(interface.id == id):
                return interface

    def create_compound_state(self, stateInterface):
        #PROVIDER
        state = smach.StateMachine(stateInterface.get_outcomes())
        with state:
            for child_state in stateInterface.states:
                child_state_interface = self.find_matching_interface(child_state)
                state.add(child_state_interface.id, self.states_instances[child_state_interface.id], child_state_interface.transitions)
                ##Register IO Keys
                #for keys in self.states_instances[child_state.id].get_registered_output_keys() + self.states_instances[child_state.id].get_registered_input_keys():
                    #state.register_io_keys(keys)

        state.set_initial_state([stateInterface.initial_state_id], smach.UserData())
        if(len(stateInterface.data)>0):
            state.register_start_cb(convert_datamodel_to_userdata,[stateInterface.data])
        return stateInterface.id, state

    def create_all_simple_states(self):
        states = {}
        for stateInterface in self._interface.simpleStates:
            id, state = self.create_simple_state(stateInterface)
            states[id] = state
        return states

    def get_python_state_automatch(self, stateInterface):
            ##Use Auto matched
        matching_states = []
        for provider in self._provider:
            match_state = provider.get_state(stateInterface.id)
            if(match_state is not None):
                matching_states.append(match_state)

        if(len(matching_states) == 0):
            ##No state found in the registered provider
            ##Raise Error Code
            raise NameError("No State matching name found for the %s state. Please check Providers."%(stateInterface.id))
        elif(len(matching_states) > 1):
            ## more than one matching
            raise NameError("More than one state matching name found for the %s state. Please consider making it explicit in the SCXML file.")
        else:
            ##Match found
             return matching_states[0]

    def get_python_state(self, stateInterface):
        found_state = None
        if("python_state" in stateInterface.data):
            for provider in self._provider:
                found_state = provider.get_state(stateInterface.data["python_state"])
                rospy.logdebug("State '%s' linked to '%s' python state set."%(stateInterface.id, stateInterface.data["python_state"]))
            if found_state == None:
                #No matching found for the user define state
                raise NameError("The linked Python State '%s' for the State '%s' was not found. Please check Providers.")
        else:
            found_state = self.get_python_state_automatch(stateInterface)

        if(found_state is not None):
            if not(isinstance(found_state, smach.State)):
                ##Not a smachState for the smach  builder
                raise TypeError("State '%s' linked Python State is not Smach State instances"%(stateInterface.id))
            else:
                return found_state

    def create_simple_state(self, stateInterface):
        #PROVIDER
        ##Use define matching
        print(stateInterface)
        state = self.get_python_state(stateInterface)
        outcomes = list(state.get_registered_outcomes())
        for outcome in outcomes:
            if not(outcome in stateInterface.get_outcomes()):
                ##Raise error
                raise NameError("Outcome '%s' from the Python class associate with the State '%s' is not defined in the SCXML."%(outcome, stateInterface.Id))
        for outcome in stateInterface.get_outcomes():
            if not(outcome in outcomes):
                ##Raise error
                raise NameError("Outcome '%s' defined in the SCXML State '%s' is not defined in is linked Python class."%(outcome, stateInterface.Id))
        return stateInterface.id, state

    def build_state_machine(self):
        simplestates = self.create_all_simple_states()
        self.states_instances = simplestates
        compoundstates = self.create_all_compound_states()
        states_instances = compoundstates.copy()
        self.states_instances.update(states_instances)
        return self.create_root_state_machine()


from django.shortcuts import render
from django.views import View
from .models import Artist, Song
from .forms import ArtistForm
from django.shortcuts import redirect

# Create your views here.
def index(request):
    return render(request, 'music/index.html')

def top_songs(request):
    return render(request, 'music/top_songs.html')

class Index(View):

    template = "music/index.html"

    def get(self, request):
        return render(request, self.template)

class TopSongs(View):

    template =  "music/top_songs.html"

    def get(self, request):
        """GET method."""
        songs = Song.objects.all()
        to_play_id = request.GET.get("to_play", 1)
        songs_to_play = Song.objects.filter(id = to_play_id)
        if songs_to_play.count() == 0:
            to_play = Song.objects.first()
        else:
            to_play = songs_to_play.first()

        context = {"songs": songs, "to_play": to_play}
        return render(request, self.template, context)

class Artists(View):

    template = "music/artist.html"

    def get(self, request):
        singers = Artist.objects.all();
        context = {"singers" : singers}
        return render(request, self.template, context)

class CreateArtist(View):

    template = "music/create_artist.html"

    def get(self, request):
        form = ArtistForm()
        context = {'form': form}
        return render(request, self.template, context)

    def post(self, request):
        form = ArtistForm(request.POST)
        if form.is_valid():
            form.save();
            return redirect('/artist/')
        context = {'form': form}
        return render(request, self.template, context)