import streamlit as st
import json
import requests
import pandas as pd
import numpy as np
import re
from datetime import datetime as dt
from datetime import time
import matplotlib.pyplot as plt
import seaborn as sns
import matplotlib

st.set_page_config(layout="centered",
                   page_title="DataCracy Dashboard",
                   page_icon = '💙')

#SLACK_BEARER_TOKEN = os.environ.get('SLACK_BEARER_TOKEN') ## Get in setting of Streamlit Share
SLACK_BEARER_TOKEN = st.secrets["TOKEN"]
DTC_GROUPS_URL = ('https://raw.githubusercontent.com/anhdanggit/atom-assignments/main/data/datacracy_groups.csv')
#st.write(json_data['SLACK_BEARER_TOKEN'])

@st.cache
def load_users_df():
    # Slack API User Data
    endpoint = "https://slack.com/api/users.list"
    headers = {"Authorization": "Bearer {}".format(SLACK_BEARER_TOKEN)}
    response_json = requests.post(endpoint, headers=headers).json()
    user_dat = response_json['members']

    # Convert to CSV
    user_dict = {'user_id':[],'name':[],'display_name':[],'real_name':[],'title':[],'is_bot':[]}
    for i in range(len(user_dat)):
      user_dict['user_id'].append(user_dat[i]['id'])
      user_dict['name'].append(user_dat[i]['name'])
      user_dict['display_name'].append(user_dat[i]['profile']['display_name'])
      user_dict['real_name'].append(user_dat[i]['profile']['real_name_normalized'])
      user_dict['title'].append(user_dat[i]['profile']['title'])
      user_dict['is_bot'].append(int(user_dat[i]['is_bot']))
    user_df = pd.DataFrame(user_dict)
    # Read dtc_group hosted in github
    dtc_groups = pd.read_csv(DTC_GROUPS_URL)
    user_df = user_df.merge(dtc_groups, how='left', on='name')
    return user_df

@st.cache
def load_channel_df():
    endpoint2 = "https://slack.com/api/conversations.list"
    data = {'types': 'public_channel,private_channel'} # -> CHECK: API Docs https://api.slack.com/methods/conversations.list/test
    headers = {"Authorization": "Bearer {}".format(SLACK_BEARER_TOKEN)}
    response_json = requests.post(endpoint2, headers=headers, data=data).json()
    channel_dat = response_json['channels']
    channel_dict = {'channel_id':[], 'channel_name':[], 'is_channel':[],'creator':[],'created_at':[],'topics':[],'purpose':[],'num_members':[]}
    for i in range(len(channel_dat)):
        channel_dict['channel_id'].append(channel_dat[i]['id'])
        channel_dict['channel_name'].append(channel_dat[i]['name'])
        channel_dict['is_channel'].append(channel_dat[i]['is_channel'])
        channel_dict['creator'].append(channel_dat[i]['creator'])
        channel_dict['created_at'].append(dt.fromtimestamp(float(channel_dat[i]['created'])))
        channel_dict['topics'].append(channel_dat[i]['topic']['value'])
        channel_dict['purpose'].append(channel_dat[i]['purpose']['value'])
        channel_dict['num_members'].append(channel_dat[i]['num_members'])
    channel_df = pd.DataFrame(channel_dict)
    return channel_df

@st.cache(allow_output_mutation=True)
def load_msg_dict():
    endpoint3 = "https://slack.com/api/conversations.history"
    headers = {"Authorization": "Bearer {}".format(SLACK_BEARER_TOKEN)}
    msg_dict = {'channel_id':[],'msg_id':[], 'msg_ts':[], 'user_id':[], 'latest_reply':[],'reply_user_count':[],'reply_users':[],'github_link':[],'text':[]}
    for channel_id, channel_name in zip(channel_df['channel_id'], channel_df['channel_name']):
        print('Channel ID: {} - Channel Name: {}'.format(channel_id, channel_name))
        try:
            data = {"channel": channel_id}
            response_json = requests.post(endpoint3, data=data, headers=headers).json()
            msg_ls = response_json['messages']
            for i in range(len(msg_ls)):
                if 'client_msg_id' in msg_ls[i].keys():
                    msg_dict['channel_id'].append(channel_id)
                    msg_dict['msg_id'].append(msg_ls[i]['client_msg_id'])
                    msg_dict['msg_ts'].append(dt.fromtimestamp(float(msg_ls[i]['ts'])))
                    msg_dict['latest_reply'].append(dt.fromtimestamp(float(msg_ls[i]['latest_reply'] if 'latest_reply' in msg_ls[i].keys() else 0))) ## -> No reply: 1970-01-01
                    msg_dict['user_id'].append(msg_ls[i]['user'])
                    msg_dict['reply_user_count'].append(msg_ls[i]['reply_users_count'] if 'reply_users_count' in msg_ls[i].keys() else 0)
                    msg_dict['reply_users'].append(msg_ls[i]['reply_users'] if 'reply_users' in msg_ls[i].keys() else 0)
                    msg_dict['text'].append(msg_ls[i]['text'] if 'text' in msg_ls[i].keys() else 0)
                    ## -> Censor message contains tokens
                    text = msg_ls[i]['text']
                    github_link = re.findall('(?:https?://)?(?:www[.])?github[.]com/[\w-]+/?', text)
                    msg_dict['github_link'].append(github_link[0] if len(github_link) > 0 else None)
        except:
            print('====> '+ str(response_json))
    msg_df = pd.DataFrame(msg_dict)
    return msg_df

def process_msg_data(msg_df, user_df, channel_df):
    ## Extract 2 reply_users
    msg_df['reply_user1'] = msg_df['reply_users'].apply(lambda x: x[0] if x != 0 else '')
    msg_df['reply_user2'] = msg_df['reply_users'].apply(lambda x: x[1] if x != 0 and len(x) > 1 else '')
    ## Merge to have a nice name displayed
    msg_df = msg_df.merge(user_df[['user_id','name','DataCracy_role']].rename(columns={'name':'submit_name'}), \
        how='left',on='user_id')
    msg_df = msg_df.merge(user_df[['user_id','name']].rename(columns={'name':'reply1_name','user_id':'reply1_id'}), \
        how='left', left_on='reply_user1', right_on='reply1_id')
    msg_df = msg_df.merge(user_df[['user_id','name']].rename(columns={'name':'reply2_name','user_id':'reply2_id'}), \
        how='left', left_on='reply_user2', right_on='reply2_id')
    ## Merge for nice channel name
    msg_df = msg_df.merge(channel_df[['channel_id','channel_name','created_at']], how='left',on='channel_id')
    ## Format datetime cols
    msg_df['created_at'] = msg_df['created_at'].dt.strftime('%Y-%m-%d')
    msg_df['msg_date'] = msg_df['msg_ts'].dt.strftime('%Y-%m-%d')
    msg_df['msg_time'] = msg_df['msg_ts'].dt.strftime('%H:%M')
    msg_df['wordcount'] = msg_df.text.apply(lambda s: len(s.split()))
    return msg_df

def convert_date(s):
  if s == 0:
    d = 'Mon'
  elif s == 1:
    d = 'Tue'
  elif s == 2:
    d = 'Wed'
  elif s ==3:
    d = 'Thu'
  elif s == 4:
    d = 'Fri'
  elif s == 5:
    d = 'Sat'
  elif s == 6:
    d = 'Sun'
  return d
user_df = load_users_df()
channel_df = load_channel_df()
msg_df = load_msg_dict()
msg_df = process_msg_data(msg_df, user_df, channel_df)

submit_df = msg_df[msg_df.channel_name.str.contains('assignment')]
submit_df = submit_df[submit_df.DataCracy_role.str.contains('Learner')]
latest_ts = submit_df.groupby(['channel_name', 'user_id']).msg_ts.idxmax() ## -> Latest ts
submit_df = submit_df.loc[latest_ts]

len_submit = len(submit_df)
len_review = len(submit_df[submit_df['reply_users']!= 0])

submit_gr1_df = submit_df.groupby('DataCracy_role').get_group('Learner_Gr1')

len_submit_1 = len(submit_gr1_df)
len_review_1 = len(submit_gr1_df[submit_gr1_df['reply_users']!= 0])

submit_gr2_df = submit_df.groupby('DataCracy_role').get_group('Learner_Gr2')

len_submit_2 = len(submit_gr2_df)
len_review_2 = len(submit_gr2_df[submit_gr2_df['reply_users']!= 0])

submit_gr3_df = submit_df.groupby('DataCracy_role').get_group('Learner_Gr3')

len_submit_3 = len(submit_gr3_df)
len_review_3 = len(submit_gr3_df[submit_gr3_df['reply_users']!= 0])

submit_gr4_df = submit_df.groupby('DataCracy_role').get_group('Learner_Gr4')

len_submit_4 = len(submit_gr4_df)
len_review_4 = len(submit_gr4_df[submit_gr4_df['reply_users']!= 0])

submit_df['msg_date'] = pd.to_datetime(submit_df['msg_date'])
submit_date = submit_df['msg_date'].dt.weekday
submit_date = submit_date.apply(convert_date)
submit_date = submit_date.value_counts().reindex(['Mon','Tue','Wed','Thu','Fri','Sat','Sun'])

channel_submit = submit_df.groupby('channel_name').size()
channel_submit = channel_submit.reset_index().rename(columns={0:'count'})
st.title("DataCracy Submit Dashboard")

matplotlib.rcParams['font.family'] = "sans-serif"
matplotlib.rcParams['font.sans-serif'] = "Open Sans"
plt.rcParams['patch.edgecolor'] = 'black'

select = list(pd.unique(submit_df['DataCracy_role']))
select.append('All Group')
option = st.selectbox(
    'Choose submit by group',
    sorted(select))

if option == 'Learner_Gr1':
    st.write(f'Group 1 submited {len_submit_1} assignments')
    st.write(f'Group 1 have {len_review_1} submit be reviewed')
    sizes_all =np.array([len_review, len_submit-len_review])
    sizes_gr1 =np.array([len_review_1, len_submit_1-len_review_1])
    def func(pct, allvals):
        absolute = int(round(pct/100.*np.sum(allvals)))
        return "{:.1f}%\n({:d})".format(pct, absolute)
    # sizes =[len_submit, len_submit-len_review]

    pie, (ax1,ax2) = plt.subplots(1,2)
    ax1.pie(sizes_all,autopct=lambda pct: func(pct, sizes_all), colors=['#ffffff','#e6e6e6'],textprops={'fontsize': 7})
    ax1.set_title('All group submit', fontdict={'fontsize': 10})

    ax2.pie(sizes_gr1,autopct=lambda pct: func(pct, sizes_gr1), colors=['#ffffff','#e6e6e6'],textprops={'fontsize': 7})
    ax2.set_title('Group 1 submit',fontdict={'fontsize': 10})

    pie.legend(['reviewed', 'non reviewed'])

    st.pyplot(pie)
elif option == 'Learner_Gr2':
    st.write(f'Group 2 submited {len_submit_2} assignments')
    st.write(f'Group 2 have {len_review_2} submit be reviewed')
    sizes_all =np.array([len_review, len_submit-len_review])
    sizes_gr2 =np.array([len_review_2, len_submit_2-len_review_2])
    def func(pct, allvals):
        absolute = int(round(pct/100.*np.sum(allvals)))
        return "{:.1f}%\n({:d})".format(pct, absolute)
    # sizes =[len_submit, len_submit-len_review]

    pie, (ax1,ax2) = plt.subplots(1,2)
    ax1.pie(sizes_all,autopct=lambda pct: func(pct, sizes_all), colors=['#ffffff','#e6e6e6'],textprops={'fontsize': 7})
    ax1.set_title('All group submit', fontdict={'fontsize': 10})

    ax2.pie(sizes_gr2,autopct=lambda pct: func(pct, sizes_gr2), colors=['#ffffff','#e6e6e6'],textprops={'fontsize': 7})
    ax2.set_title('Group 2 submit',fontdict={'fontsize': 10})

    pie.legend(['reviewed', 'non reviewed'])

    st.pyplot(pie)
elif option == 'Learner_Gr3':
    st.write(f'Group 3 submited {len_submit_3} assignments')
    st.write(f'Group 3 have {len_review_3} submit be reviewed')
    sizes_all =np.array([len_review, len_submit-len_review])
    sizes_gr3 =np.array([len_review_3, len_submit_3-len_review_3])
    def func(pct, allvals):
        absolute = int(round(pct/100.*np.sum(allvals)))
        return "{:.1f}%\n({:d})".format(pct, absolute)
    # sizes =[len_submit, len_submit-len_review]

    pie, (ax1,ax2) = plt.subplots(1,2)
    ax1.pie(sizes_all,autopct=lambda pct: func(pct, sizes_all), colors=['#ffffff','#e6e6e6'],textprops={'fontsize': 7})
    ax1.set_title('All group submit', fontdict={'fontsize': 10})

    ax2.pie(sizes_gr3,autopct=lambda pct: func(pct, sizes_gr3), colors=['#ffffff','#e6e6e6'],textprops={'fontsize': 7})
    ax2.set_title('Group 2 submit',fontdict={'fontsize': 10})

    pie.legend(['reviewed', 'non reviewed'])

    st.pyplot(pie)
elif option == 'Learner_Gr4':
    st.write(f'Group 4 submit {len_submit_4} assignments')
    st.write(f'Group 4 have {len_review_4} submit be reviewed')
    sizes_all =np.array([len_review, len_submit-len_review])
    sizes_gr4 =np.array([len_review_4, len_submit_4-len_review_4])
    def func(pct, allvals):
        absolute = int(round(pct/100.*np.sum(allvals)))
        return "{:.1f}%\n({:d})".format(pct, absolute)
    # sizes =[len_submit, len_submit-len_review]

    pie, (ax1,ax2) = plt.subplots(1,2)
    ax1.pie(sizes_all,autopct=lambda pct: func(pct, sizes_all), colors=['#ffffff','#e6e6e6'],textprops={'fontsize': 7})
    ax1.set_title('All group submit', fontdict={'fontsize': 10})

    ax2.pie(sizes_gr4,autopct=lambda pct: func(pct, sizes_gr4), colors=['#ffffff','#e6e6e6'],textprops={'fontsize': 7})
    ax2.set_title('Group 2 submit',fontdict={'fontsize': 10})

    pie.legend(['reviewed', 'non reviewed'])

    st.pyplot(pie)
elif option == 'All Group':
    st.write(f'All learners submited {len_submit} assignments')
    st.write(f'Have {len_review} submit be reviewed ')
    sizes_all =np.array([len_review, len_submit-len_review])
    def func(pct, allvals):
        absolute = int(round(pct/100.*np.sum(allvals)))
        return "{:.1f}%\n({:d} )".format(pct, absolute)
    # sizes =[len_submit, len_submit-len_review]

    pie, ax1 = plt.subplots()
    ax1.pie(sizes_all,autopct=lambda pct: func(pct, sizes_all), colors=['#ffffff','#e6e6e6'],textprops={'fontsize': 7})
    ax1.set_title('All group submit', fontdict={'fontsize': 10})

    pie.legend(['reviewed', 'non reviewed'])

    st.pyplot(pie)

period = st.slider(
    'Select a range of hours submit',
    0,23,(0,23))
period = list(period)
time_submit = submit_df['msg_time'].apply(lambda x: x.split(':')[0]).value_counts().sort_index()[period[0]:period[1]+1]

sns.set_style("ticks")
bar = plt.figure()
sns.barplot(time_submit.index,time_submit.values,facecolor=(1, 1, 1, 0),edgecolor=".2")
plt.title('Time submit distribution',fontsize=15)
plt.xlabel('Hour')
plt.ylabel('Submit')
st.pyplot(bar)

option1 = st.selectbox(
    'Submit by',
    ['Week','Weekday'])
if option1 == 'Week':
    submit_channel = plt.figure()
    ax1 = sns.barplot(channel_submit.index+1,channel_submit['count'],facecolor=(1, 1, 1, 0),edgecolor=".2")
    for p in ax1.patches:
        ax1.annotate(format(p.get_height(), '.1f'),
                   (p.get_x() + p.get_width() / 2., p.get_height()),
                   ha = 'center', va = 'center',
                   size=10,
                   xytext = (0, -12),
                   textcoords = 'offset points')
    plt.title('Submit distribution by week',fontsize=15)
    plt.xlabel('Assignment week')
    plt.ylabel('Submit')
    st.pyplot(submit_channel)
elif option1 == 'Weekday':
    bar_date = plt.figure()

    ax = sns.barplot(submit_date.index,submit_date.values,facecolor=(1, 1, 1, 0),edgecolor=".2", order= submit_date.index)
    for p in ax.patches:
        ax.annotate(format(p.get_height(), '.1f'),
                    (p.get_x() + p.get_width() / 2., p.get_height()),
                    ha = 'center', va = 'center',
                    size=10,
                    xytext = (0, -12),
                    textcoords = 'offset points')
    plt.title('Submit distribution by weekday',fontsize=15)
    plt.xlabel('Days of week')
    plt.ylabel('Submit')
    st.pyplot(bar_date)


from functools import wraps

def fsm_log_by(func):
    @wraps(func)
    def wrapped(*args, **kwargs):
        arg_list = list(args)
        instance = arg_list.pop(0)

        if kwargs.get('by', False):
            instance.by = kwargs['by']

        if kwargs.get('description', False):
            instance.description = kwargs['description']

        out = func(instance, *arg_list, **kwargs)

        if kwargs.get('by', False):
            delattr(instance, 'by')

        if kwargs.get('description', False):
            delattr(instance, 'description')

        return out

    return wrapped


from dateutil import parser
from datetime import datetime

def convert_from_str_timestamps(original_timestamps, target_format):
    """
    Converts list of timestapms from string format to datetime or unix
    """

    if target_format == "datetime":
        output_timestamps = [parser.parse(elem)
                             for elem in original_timestamps]

    elif target_format == "unix":
        output_timestamps \
            = [round(datetime.strptime(elem, "%Y-%m-%d %H:%M:%S")
                     .timestamp())
               for elem in original_timestamps]
    else:
        raise ValueError("Invalid target_format name")

    return output_timestamps

def convert_from_unix_timestamps(original_timestamps, target_format):
    """
    Converts list of timestapms from unix format to datetime or string
    """

    if target_format == "datetime":
        output_timestamps = [datetime.fromtimestamp(elem)
                             for elem in original_timestamps]
    elif target_format == "str":
        output_timestamps \
            = [datetime.fromtimestamp(elem).strftime("%Y-%m-%d %H:%M:%S")
               for elem in original_timestamps]
    else:
        raise ValueError("Invalid target_format name")

    return output_timestamps


import datetime
import threading
import random

from . database import Database
from . user import User
from . pages import Pages

class Newsfeed:
    def __init__(self):
        self.database = Database()
        self.user = User()
        self.pages = Pages()

    def get_newsfeed(self, access_token, uid):
        user_pages_data = self.get_update_user_likes_pages(access_token, uid)
        random_pages = random.sample(user_pages_data,50)
        update_current_newsfeed_thread = threading.Thread(target= self.database.update_user_newsfeed, args= (uid ,random_pages[10:]), daemon= True)
        update_current_newsfeed_thread.start()
        return {
                'newsfeed' : {
                    'data' : self.get_pages_post(access_token, uid, random_pages[:10])
                },
                'next' : 10 < len(random_pages)
            }

    def get_next_newsfeed(self, access_token, uid):
        newsfeed = self.database.get_next_newsfeed(uid)
        return {
                'newsfeed' : {
                    'data' : self.get_pages_post(access_token, uid, newsfeed)
                },
                'next' : newsfeed != []
            }

    def get_update_user_likes_pages(self, access_token, uid):
        likes_data = self.database.get_user_likes_pages(uid)
        if likes_data == None:
            likes_data = self.user.get_like_pages(access_token, uid)
            if 'error_subcode' not in likes_data: self.database.update_user_likes_pages(uid, likes_data)
        else:
            if (datetime.datetime.now() - likes_data['time_stamp']).days > 7:
                likes_data = self.user.get_like_pages(access_token, uid)
                if 'error_subcode' not in likes_data: self.database.update_user_likes_pages(uid, likes_data)
            else:
                likes_data = likes_data['pages']
        return likes_data

    def get_pages_post(self, access_token, uid, list_of_pages):
        upper_newsfeed = []
        lower_newsfeed = []
        inactive_pages = []
        for page in list_of_pages:
            page_post = self.check_pages_validator(access_token, page['id'])
            if page_post == None: inactive_pages.append(page['id'])
            elif 'list_of_posts' in page_post:
                if len(page_post['list_of_posts']) == 2:
                    upper_newsfeed.append(page_post['list_of_posts'][0])
                    lower_newsfeed.append(page_post['list_of_posts'][1])
                elif len(page_post['list_of_posts']) == 1:
                    upper_newsfeed.append(page_post['list_of_posts'][0])
        remove_inactive_page_threading = threading.Thread(target=self.database.remove_inactive_page, args=(uid, inactive_pages), daemon=True)
        remove_inactive_page_threading.start()
        random.shuffle(upper_newsfeed)
        random.shuffle(lower_newsfeed)
        upper_newsfeed.extend(lower_newsfeed)
        return upper_newsfeed

    def check_pages_validator(self, access_token, page_id):
        page = self.database.get_page_from_cache(page_id)
        if page == None:
            page = self.pages.get_page_post(access_token, page_id)
            if 'error' not in page:
                self.database.insert_page_to_cache(page_id, page)
        else:
            if page['isActive']:
                diff_time = datetime.datetime.now() - page['time_stamp']
                if(0 < diff_time.days or page['priority'] < diff_time.seconds):
                    page = self.pages.get_page_post(access_token, page_id)
                    if 'error' not in page:
                        self.database.update_page_in_cache(page_id, page)
            else: return None
        return page

    def collect_like_posts(self, uid, metadata):
        self.database.collect_like_posts(uid, metadata)
        return {'msg' : 'collect data success'}

    def collect_dislike_posts(self, uid, metadata):
        self.database.collect_dislike_posts(uid, metadata)
        return {'msg' : 'collect data success'}


from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackQueryHandler
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from telegram import KeyboardButton, ReplyKeyboardMarkup, ReplyKeyboardRemove
from weather import get_forecasts

# pengecekan pesan bay --> polling
updater = Updater(token="621603636:AAEVtXeRzaOqH8fhvlZypmhWjj9_GPXP_cU")

# mengijinkan registrasu handler -> command, text, video, audio etc
dispatcher = updater.dispatcher

# mendefinisikan callback function

def start(bot, update):
    bot.send_message(chat_id=update.message.chat_id, text="Hai, Selamat datang di HerhoWeatherBot! \n Kirimkan perintah /info untuk mengetahui info cuaca terkini di wilayah anda!")

# membuat perintah handler
start_handler = CommandHandler("start", start)

# menambahkan perintah handler ke dispatcher
dispatcher.add_handler(start_handler)

def echo(bot, update):
    bot.send_message(chat_id=update.message.chat_id, text=update.message.text.upper())

# membuat text handler
echo_handler = MessageHandler(Filters.text, echo)

dispatcher.add_handler(echo_handler)

def option(bot, update):
    button = [
        [InlineKeyboardButton("Pilihan 1", callback_data="1"),
         InlineKeyboardButton("Pilihan 2", callback_data="2")],
        [InlineKeyboardButton("Pilihan 3", callback_data="3")]
    ]
    reply_markup = InlineKeyboardMarkup(button)

    bot.send_message(chat_id=update.message.chat_id,
                     text="Pilih salah satu...",
                     reply_markup=reply_markup)

option_handler = CommandHandler("pilihan", option)
dispatcher.add_handler(option_handler)

def get_location(bot, update):
    button = [
        [KeyboardButton("Bagikan lokasi", request_location=True)]
    ]
    reply_markup = ReplyKeyboardMarkup(button)
    bot.send_message(chat_id=update.message.chat_id,
                     text="Apakah anda keberatan membagikan lokasi anda saat ini?",
                     reply_markup=reply_markup)

get_location_handler = CommandHandler("info", get_location)
dispatcher.add_handler(get_location_handler)

def location(bot, update):
    lat = update.message.location.latitude
    lon = update.message.location.longitude
    forecasts = get_forecasts(lat, lon)
    bot.send_message(chat_id=update.message.chat_id,
                     text=forecasts,
                     reply_markup=ReplyKeyboardRemove())

location_handler = MessageHandler(Filters.location, location)
dispatcher.add_handler(location_handler)

# memulai polling
updater.start_polling()


# -*- coding: utf-8 -*-
"""
Created on Tue Sep  8 09:07:24 2015

@author: rodrigopena

Rotation Based Projector

"""

#%% Import libraries

import numpy as np

import scipy.interpolate
import scipy.misc
import scipy.ndimage.interpolation

#%% Open the image file

phantom = scipy.misc.imread("Phantom.jpg")

#%% Setup variables
angle = 0.5
n_projections = 180 / angle
image = np.copy(phantom)

#%% Loop on the number of projections
for p in range(int(n_projections)):

    #%% Rotate the image
    rotatedImage = scipy.ndimage.interpolation.rotate(image, angle, order=3,
                                                      reshape=False,
                                                      mode='constant',
                                                      cval=0.0)

    #%% Sum across columns
    imageSum = np.sum(rotatedImage, axis=0)

    #%% Stack projection views
    if p == 0:
        sinogram = np.copy(imageSum)
    else:
        sinogram = np.vstack((sinogram, imageSum))

    #%% Set current rotation as the next image
    image = np.copy(rotatedImage)

#%% Save resulting image

# Normalize
sinogram = sinogram.astype('float')
sinogram -= np.min(sinogram.ravel())
sinogram /= np.max(sinogram.ravel())
sinogram *= 255

scipy.misc.imsave("Sinogram.jpg", sinogram)


import sys
import logging
from pyspark.sql import SparkSession

import thoughtworks.citibike.CitibikeTransformer as CitiBikeTransformer

def main(args):

    if len(args) < 4:
        logging.warning("Input source and output path are required")
        sys.exit(1)
    else:
        input_path = args[2]
        output_path = args[3]

    spark = SparkSession.builder.appName("Citibike Transformer").getOrCreate()
    logging.info("Application Initialized: " + spark.sparkContext.appName)
    CitiBikeTransformer.run(spark, input_path, output_path)
    logging.info("Application Done: " + spark.sparkContext.appName)

    spark.stop()


from fractions import gcd
p = 1000000007

def power(x, y, diff):
    res = 1
    x = x % diff
    while y > 0:
        if y & 1:
            res = (res*x) % diff

        y = y >> 1
        x = (x*x) % diff

    return res

t = int(input())
while t:
    a,b,n = map(int,input().split(' '))
    d = a - b
    if d == 0:
        s = power(a, n, p) + power(b, n, p)
        s = s % p
        print(s)
    else:
        s = (power(a, n, d) + power(b, n, d)) % d
        #if s == 0:
        #    res = d % p
        #    print(res)
        #    continue
        g = gcd(s, d)
        g = g % p
        print(g)
    t = t-1


import datetime
import pytz
from .models import UserDetails, InterviewDetails

class validators:

    def __init__(self, startTime, endTime, participants):
        self.startTime = startTime
        self.endTime = endTime
        self.participants = participants
        self.valid = True
        self.errorMessage = ""
        self.leastParticipantsAllowed = 2

    def isvalid(self):
        return self.valid

    def setvalid(self, value):
        self.valid = value

    def getErrorMessage(self):
        return self.errorMessage

    def appendErrorMessage(self, message):
        if self.errorMessage == "":
            self.errorMessage = message
        else:
            self.errorMessage = self.errorMessage + ', ' + message

    def validateOverlappings(self, oldDataId=-1):
        # checking overlapping inerviews
        for user in self.participants:
            scheduledInterviews = InterviewDetails.objects.filter(
                participants=user)
            for interview in scheduledInterviews:
                if oldDataId != interview.id and self.checkOverlapping(interview) == False:
                    self.setvalid(False)
                    self.appendErrorMessage(
                        "User with userId = "+str(user.id)+" has overlapping Interviews")

    def checkOverlapping(self, slot):
        # checking overlaps
        if slot.startTime >= self.endTime or slot.endTime <= self.startTime:
            return True
        else:
            return False

    def validateCountofParticipants(self):
        # checking if there are less than 2 participants
        if len(self.participants) < self.leastParticipantsAllowed:
            self.setvalid(False)
            self.appendErrorMessage("There must be at least 2 users")

    def validateTime(self):
        # checks if the time mentioned is correct or not
        if self.startTime > self.endTime:
            self.setvalid(False)
            self.appendErrorMessage("Start Time can't be > End Time")

        # checks if interview is scheduled in the past time
        currentTime = datetime.datetime.now().replace(tzinfo=pytz.UTC)
        if self.startTime < currentTime or self.endTime < currentTime:
            self.setvalid(False)
            self.appendErrorMessage(
                "Interviews in the past can't be scheduled")


#!/usr/bin/env python

import numpy as np

import bregnn.ffwd as ffwd
import bregnn.io as io

import os
import json

from pprint import pprint

from sklearn.model_selection import train_test_split

from optparse import OptionParser, make_option

##
default_features = ['Jet_pt',
                    'Jet_eta',
                    'rho',
                    'Jet_mt',
                    'Jet_leadTrackPt',
                    'Jet_leptonPtRel',
                    'Jet_leptonDeltaR',
                    'Jet_neHEF',
                    'Jet_neEmEF',
                    'Jet_vtxPt',
                    'Jet_vtxMass',
                    'Jet_vtx3dL',
                    'Jet_vtxNtrk',
                    'Jet_vtx3deL',
                    'Jet_energyRing_dR0_em_Jet_e',
                    'Jet_energyRing_dR1_em_Jet_e',
                    'Jet_energyRing_dR2_em_Jet_e',
                    'Jet_energyRing_dR3_em_Jet_e',
                    'Jet_energyRing_dR4_em_Jet_e',
                    'Jet_energyRing_dR0_neut_Jet_e',
                    'Jet_energyRing_dR1_neut_Jet_e',
                    'Jet_energyRing_dR2_neut_Jet_e',
                    'Jet_energyRing_dR3_neut_Jet_e',
                    'Jet_energyRing_dR4_neut_Jet_e',
                    'Jet_energyRing_dR0_ch_Jet_e',
                    'Jet_energyRing_dR1_ch_Jet_e',
                    'Jet_energyRing_dR2_ch_Jet_e',
                    'Jet_energyRing_dR3_ch_Jet_e',
                    'Jet_energyRing_dR4_ch_Jet_e',
                    'Jet_energyRing_dR0_mu_Jet_e',
                    'Jet_energyRing_dR1_mu_Jet_e',
                    'Jet_energyRing_dR2_mu_Jet_e',
                    'Jet_energyRing_dR3_mu_Jet_e',
                    'Jet_energyRing_dR4_mu_Jet_e',
                    'Jet_numDaughters_pt03']

## command_line options
parser = OptionParser(option_list=[
    make_option("--inp-dir",type='string',dest="inp_dir",default=os.environ['SCRATCH']+'/bregression'),
    make_option("--out-dir",type='string',dest="out_dir",default='.'),
    make_option("--inp-file",type='string',dest='inp_file',default='ttbar_unweighted_full80M_selected_train.hd5'),
    make_option("--features",type='string',dest='features',default=''),
    make_option("--normalize-target",action="store_true",dest="normalize_target",default=True),
    make_option("--no-normalize-target",action="store_false",dest="normalize_target"),
    make_option("--loss",type='string',dest="loss",default="mse"),
    make_option("--valid-frac",type='float',dest='valid_frac',default=0.05),
    make_option("--batch-size",type='int',dest='batch_size',default=1024),
    make_option("--epochs",type='int',dest='epochs',default=20),
    make_option("--hparams",type='string',dest='hparams',default=None),
    make_option("--seed",type='int',dest='seed',default=98543),
])

## parse options
(options, args) = parser.parse_args()

if options.features == '':
    features = default_features
    options.features = ','.join(features)
else:
    features = options.features.split(',')

if os.path.exists(options.inp_file):
    inp_file = options.inp_file
else:
    inp_file = options.inp_dir+'/'+options.inp_file

hparams = {}
if options.hparams is not None:
    with open(options.hparams) as hf:
        hparams = json.loads(hf.read())

## read data
columns = features + ['Jet_mcPt']
data = io.read_data(inp_file, columns = columns)

X = data[features].values
y = (data['Jet_mcPt']/data['Jet_pt']).values.reshape(-1,1)

y_mean = np.median(y)
y_std = y.std()
print(y_mean,y_std)

# normalize target
if options.normalize_target:
    y -= y_mean
    y /= y_std

# split data
X_train,X_valid,y_train,y_valid = train_test_split(X,y,test_size=options.valid_frac,random_state=options.seed)

# sort out model parameters
def get_kwargs(fn,**kwargs):
    params = set(fn.__code__.co_varnames[:fn.__code__.co_argcount]+tuple(kwargs.keys()))
    for par in params:
        if hasattr(options,par):
            kwargs[par] = getattr(options,par)
        if par in hparams:
            kwargs[par] = hparams.pop(par)
    return kwargs

init_kwargs = get_kwargs(ffwd.FFWDRegression.__init__,monitor_dir=options.out_dir)
fit_kwargs = get_kwargs(ffwd.FFWDRegression.fit,batch_size=options.batch_size,epochs=options.epochs)

print(init_kwargs)
print(fit_kwargs)

reg = ffwd.FFWDRegression('ffwd',X.shape[1:],**init_kwargs)

pprint(reg.get_params())

model = reg()

print(model.summary())

if not os.path.exists(options.out_dir):
    os.mkdir(options.out_dir)

store = dict( y_mean=float(y_mean), y_std=float(y_std),
              model_params = reg.get_params(),
              fit_kwargs = fit_kwargs,
              options = options.__dict__
              )
with open(options.out_dir+'/config.json','w+') as fo:
    fo.write( json.dumps( store, indent=4 ) )
    fo.close()

reg.fit(X_train,y_train,
        validation_data=(X_valid,y_valid),
        **fit_kwargs)


import numpy as np
import tensorflow.keras.backend as K
import matplotlib.pyplot as plt
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing import image

K.clear_session()
model_best = load_model('Model/best_model_3class.hdf5', compile=False)
food_list = ['donuts', 'pizza', 'samosa']

def predict_class(model, images, show=True):
    for img in images:
        img = image.load_img(img, target_size=(299, 299))
        img = image.img_to_array(img)
        img = np.expand_dims(img, axis=0)
        img /= 255.

        pred = model.predict(img)
        index = np.argmax(pred)
        food_list.sort()
        pred_value = food_list[index]
        if show:
            plt.imshow(img[0])
            plt.axis('off')
            plt.title(pred_value)
            plt.show()

def main():
    images = []
    samos = "Sample/Homemade Samosa.jpg"
    # images.append('donut.jpg')
    # images.append('pizza.jpg')
    images.append(samos)
    predict_class(model_best, images, True)

if __name__ == "__main__":
    main()


import numpy as np
import pygame as pg
import pandas as pd
import pylab as plt
import itertools as it
import math
import os
import pathos.multiprocessing as mp

from anytree import AnyNode as Node
from anytree import RenderTree
from collections import OrderedDict

# Local import
from algorithms.stochasticPW import ScoreChild, SelectAction, SelectNextState, InitializeChildren, Expand, ExpandNextState, \
        PWidening, RollOut, backup, OutputAction, PWMultipleTrees

from simple1DEnv import TransitionFunction, RewardFunction, Terminal
from visualize import draw
import stochasticAgentsMotionSimulationByAccerelationActionBurnTimeWithProbabilityDistractor as ag
import Attention
import calPosterior
import stochasticBeliefAndAttentionSimulationBurnTimeUpdateIdentityMax as ba
import envWithProbabilityDistractor as env
import reward
import trajectoriesSaveLoad as tsl

class MakeDiffSimulationRoot():
    def __init__(self, isTerminal, updatePhysicalStateByBelief):
        self.isTerminal = isTerminal
        self.updatePhysicalStateByBelief = updatePhysicalStateByBelief
    def __call__(self, action, state):
        stateForSimulationRoot = self.updatePhysicalStateByBelief(state)
        while self.isTerminal(stateForSimulationRoot):
            stateForSimulationRoot = self.updatePhysicalStateByBelief(state)
        rootNode = Node(id={action: stateForSimulationRoot}, numVisited=0, sumValue=0, is_expanded = False)
        return rootNode

class RunMCTSTrjactory:
    def __init__(self, maxRunningSteps, numTree, planFrequency, actionUpdateFrequecy, transitionFunctionInPlay, isTerminal, makeDiffSimulationRoot, render):
        self.maxRunningSteps = maxRunningSteps
        self.numTree = numTree
        self.planFrequency = planFrequency
        self.actionUpdateFrequecy = actionUpdateFrequecy
        self.transitionFunctionInPlay = transitionFunctionInPlay
        self.isTerminal = isTerminal
        self.makeDiffSimulationRoot = makeDiffSimulationRoot
        self.render = render

    def __call__(self, mcts):
        state, action = None, None
        currState = self.transitionFunctionInPlay(state, action)
        trajectory = []
        for runningStep in range(self.maxRunningSteps):

            if self.isTerminal(currState):
                trajectory.append([currState, None])
                break
            if runningStep % (self.planFrequency * self.actionUpdateFrequecy) == 0:
                rootNodes = [self.makeDiffSimulationRoot(action, currState) for treeIndex in range(self.numTree)]
                actions = mcts(rootNodes)
                #actionSpace = [(np.cos(degreeInPolar), np.sin(degreeInPolar)) for degreeInPolar in np.arange(0, 360, 8)/180 * math.pi]
                #actions = [actionSpace[np.random.choice(range(len(actionSpace)))]]
            action = actions[int(runningStep/self.actionUpdateFrequecy) % self.planFrequency]
            physicalState, beliefAndAttention = currState
            hypothesisInformation, positionOldTimeDF = beliefAndAttention
            probabilityOnHypothesisAttention = np.exp(hypothesisInformation['logP'])
            posteriorOnHypothesisAttention = probabilityOnHypothesisAttention/probabilityOnHypothesisAttention.sum()
            probabilityOnAttentionSlotByGroupbySum = posteriorOnHypothesisAttention.groupby(['wolfIdentity','sheepIdentity']).sum().values
            posterior = probabilityOnAttentionSlotByGroupbySum/np.sum(probabilityOnAttentionSlotByGroupbySum)
            attentionStatusOnJointSpace = hypothesisInformation['attentionStatus']
            attentionStatus = attentionStatusOnJointSpace.groupby(['wolfIdentity', 'sheepIdentity']).mean().values

            stateToRecord = [physicalState, posterior, attentionStatus, [np.array(list(rootNode.id.values())[0])[0][3] for rootNode in rootNodes]]
            trajectory.append([stateToRecord, action])
            nextState = self.transitionFunctionInPlay(currState, action)
            #print('***', physicalState[0][0], action)
            #print('***', np.linalg.norm(physicalState[1][0]), np.linalg.norm(physicalState[1][10]))
            currState = nextState
        return trajectory

class RunOneCondition:
    def __init__(self, getTrajectorySavePathByCondition, getCSVSavePathByCondition):
        self.getTrajectorySavePathByCondition = getTrajectorySavePathByCondition
        self.getCSVSavePathByCondition = getCSVSavePathByCondition

    def __call__(self, condition):

        getSavePath = self.getTrajectorySavePathByCondition(condition)
        getCSVSavePath = self.getCSVSavePathByCondition(condition)
        attentionType = condition['attentionType']
        alpha = condition['alphaForStateWidening']
        C = condition['CForStateWidening']
        minAttentionDistance = condition['minAttentionDistance']
        rangeAttention = condition['rangeAttention']
        numTree = condition['numTrees']
        numSimulations = condition['numSimulationTimes']
        actionRatio = condition['actionRatio']
        cBase = condition['cBase']
        burnTime = condition['burnTime']

        numSub = 2
        allResultsIdentity = []
        allResultsAttention = []
        allResultsIdentitySampled = []
        possibleTrialSubtleties = [11.0, 3.3]#[500.0, 11.0, 3.3, 1.83, 0.92, 0.31, 0.001]
        for subIndex in range(numSub):
            meanIdentityPerceptionOnConditions = {}
            meanAttentionPerceptionOnConditions = {}
            meanIdentitySampledOnConditions = {}
            for chasingSubtlety in possibleTrialSubtleties:

                print(numTree, chasingSubtlety, numSimulations, attentionType)
                numAgent = 25
                sheepId = 0
                suspectorIds = list(range(1, numAgent))

                resetWolfIdAndSubtlety = ag.ResetWolfIdAndSubtlety(suspectorIds, [chasingSubtlety])
                distanceToVisualDegreeRatio = 20
                minInitSheepWolfDistance = 9 * distanceToVisualDegreeRatio
                minInitSheepDistractorDistance = 2.5 * distanceToVisualDegreeRatio  # no distractor in killzone when init
                isLegalInitPositions = ag.IsLegalInitPositions(sheepId, minInitSheepWolfDistance, minInitSheepDistractorDistance)
                xBoundary = [0, 640]
                yBoundary = [0, 480]
                resetAgentPositions = ag.ResetAgentPositions(xBoundary, yBoundary, numAgent, isLegalInitPositions)
                resetPhysicalState = ag.ResetPhysicalState(sheepId, numAgent, resetAgentPositions, resetWolfIdAndSubtlety)

                numFramePerSecond = 40
                numMDPTimeStepPerSecond = 5
                numFrameWithoutActionChange = int(numFramePerSecond/numMDPTimeStepPerSecond)

                sheepActionUpdateFrequency = 1
                minSheepSpeed = int(17.4 * distanceToVisualDegreeRatio/numFramePerSecond)
                maxSheepSpeed = int(23.2 * distanceToVisualDegreeRatio/numFramePerSecond)
                warmUpTimeSteps = int(10 * numMDPTimeStepPerSecond)
                sheepPolicy = ag.SheepPolicy(sheepActionUpdateFrequency, minSheepSpeed, maxSheepSpeed, warmUpTimeSteps, burnTime)

                wolfActionUpdateFrequency = int(0.2 * numMDPTimeStepPerSecond)
                minWolfSpeed = int(8.7 * distanceToVisualDegreeRatio/numFramePerSecond)
                maxWolfSpeed = int(14.5 * distanceToVisualDegreeRatio/numFramePerSecond)
                wolfPolicy = ag.WolfPolicy(wolfActionUpdateFrequency, minWolfSpeed, maxWolfSpeed, warmUpTimeSteps)
                distractorActionUpdateFrequency = int(0.2 * numMDPTimeStepPerSecond)
                minDistractorSpeed = int(8.7 * distanceToVisualDegreeRatio/numFramePerSecond)
                maxDistractorSpeed = int(14.5 * distanceToVisualDegreeRatio/numFramePerSecond)
                distractorPolicy = ag.DistractorPolicy(distractorActionUpdateFrequency, minDistractorSpeed, maxDistractorSpeed, warmUpTimeSteps)
                preparePolicy = ag.PreparePolicy(sheepId, numAgent, sheepPolicy, wolfPolicy, distractorPolicy)
                updatePhysicalState = ag.UpdatePhysicalState(sheepId, numAgent, preparePolicy)

                xBoundary = [0, 640]
                yBoundary = [0, 480]
                checkBoundaryAndAdjust = ag.CheckBoundaryAndAdjust(xBoundary, yBoundary)
                transiteMultiAgentMotion = ag.TransiteMultiAgentMotion(checkBoundaryAndAdjust)

                minDistance = 0.0 * distanceToVisualDegreeRatio
                isTerminal = env.IsTerminal(sheepId, minDistance)
               # screen = pg.display.set_mode([xBoundary[1], yBoundary[1]])
               # screenColor = np.array([0, 0, 0])
               # sheepColor = np.array([0, 255, 0])
               # wolfColor = np.array([255, 0, 0])
               # circleSize = 10
               # saveImage = True
               # saveImageFile = 'image3'
               # render = env.Render(numAgent, screen, xBoundary[1], yBoundary[1], screenColor, sheepColor, wolfColor, circleSize, saveImage, saveImageFile, isTerminal)
                render = None
                renderOnInSimulation = False
                transiteStateWithoutActionChangeInSimulation = env.TransiteStateWithoutActionChange(numFrameWithoutActionChange, isTerminal, transiteMultiAgentMotion, render, renderOnInSimulation)
                renderOnInPlay = False
                transiteStateWithoutActionChangeInPlay = env.TransiteStateWithoutActionChange(numFrameWithoutActionChange, isTerminal, transiteMultiAgentMotion, render, renderOnInPlay)

                if attentionType == 'idealObserver':
                    attentionLimitation= 4
                    precisionPerSlot=500.0
                    precisionForUntracked=500.0
                    memoryratePerSlot=1.0
                    memoryrateForUntracked=1.0
                if attentionType == 'preAttention':
                    attentionLimitation= 4
                    precisionPerSlot=2.5
                    precisionForUntracked=2.5
                    memoryratePerSlot=0.45
                    memoryrateForUntracked=0.45
                if attentionType == 'attention3':
                    attentionLimitation= 3
                    precisionPerSlot=8.0
                    precisionForUntracked=0.01
                    memoryratePerSlot=0.7
                    memoryrateForUntracked=0.01
                if attentionType == 'hybrid3':
                    attentionLimitation= 3
                    precisionPerSlot=8.0
                    precisionForUntracked=2.5
                    memoryratePerSlot=0.7
                    memoryrateForUntracked=0.45
                if attentionType == 'attention4':
                    attentionLimitation= 4
                    precisionPerSlot=8.0
                    precisionForUntracked=0.01
                    memoryratePerSlot=0.7
                    memoryrateForUntracked=0.01
                if attentionType == 'hybrid4':
                    attentionLimitation= 4
                    precisionPerSlot=8.0
                    precisionForUntracked=2.5
                    memoryratePerSlot=0.7
                    memoryrateForUntracked=0.45

                if attentionType == 'preAttentionMem0.25':
                    attentionLimitation= 4
                    precisionPerSlot=2.5
                    precisionForUntracked=2.5
                    memoryratePerSlot=0.25
                    memoryrateForUntracked=0.25
                if attentionType == 'preAttentionMem0.65':
                    attentionLimitation= 4
                    precisionPerSlot=2.5
                    precisionForUntracked=2.5
                    memoryratePerSlot=0.65
                    memoryrateForUntracked=0.65
                if attentionType == 'preAttentionPre0.5':
                    attentionLimitation= 4
                    precisionPerSlot=0.5
                    precisionForUntracked=0.5
                    memoryratePerSlot=0.45
                    memoryrateForUntracked=0.45
                if attentionType == 'preAttentionPre4.5':
                    attentionLimitation= 4
                    precisionPerSlot=4.5
                    precisionForUntracked=4.5
                    memoryratePerSlot=0.45
                    memoryrateForUntracked=0.45

                attention = Attention.AttentionToPrecisionAndDecay(precisionPerSlot, precisionForUntracked, memoryratePerSlot, memoryrateForUntracked)
                transferMultiAgentStatesToPositionDF = ba.TransferMultiAgentStatesToPositionDF(numAgent)
                possibleSubtleties = [500.0, 11.0, 3.3, 1.83, 0.92, 0.31, 0.001]
                resetBeliefAndAttention = ba.ResetBeliefAndAttention(sheepId, suspectorIds, possibleSubtleties, attentionLimitation, transferMultiAgentStatesToPositionDF, attention)

                maxAttentionDistance = minAttentionDistance + rangeAttention
                attentionMinDistance = minAttentionDistance * distanceToVisualDegreeRatio
                attentionMaxDistance = maxAttentionDistance * distanceToVisualDegreeRatio
                numStandardErrorInDistanceRange = 4
                calDistancePriorOnAttentionSlot = Attention.CalDistancePriorOnAttentionSlot(attentionMinDistance, attentionMaxDistance, numStandardErrorInDistanceRange)
                attentionSwitch = Attention.AttentionSwitch(attentionLimitation, calDistancePriorOnAttentionSlot)
                computePosterior = calPosterior.CalPosteriorLog(minDistance)

                print(attentionLimitation, attentionMinDistance/distanceToVisualDegreeRatio, attentionMaxDistance/distanceToVisualDegreeRatio)

                attentionSwitchFrequencyInSimulation = np.inf
                beliefUpdateFrequencyInSimulation = np.inf
                updateBeliefAndAttentionInSimulation = ba.UpdateBeliefAndAttentionState(attention, computePosterior, attentionSwitch, transferMultiAgentStatesToPositionDF,
                        attentionSwitchFrequencyInSimulation, beliefUpdateFrequencyInSimulation, burnTime)

                attentionSwitchFrequencyInPlay = int(0.6 * numMDPTimeStepPerSecond)
                beliefUpdateFrequencyInPlay = int(0.2 * numMDPTimeStepPerSecond)
                updateBeliefAndAttentionInPlay = ba.UpdateBeliefAndAttentionState(attention, computePosterior, attentionSwitch, transferMultiAgentStatesToPositionDF,
                        attentionSwitchFrequencyInPlay, beliefUpdateFrequencyInPlay, burnTime)

                updatePhysicalStateByBeliefFrequencyInSimulationRoot = int(0.2 * numMDPTimeStepPerSecond)
                updatePhysicalStateByBeliefInSimulationRoot = ba.UpdatePhysicalStateImagedByBelief(updatePhysicalStateByBeliefFrequencyInSimulationRoot)
                updatePhysicalStateByBeliefFrequencyInSimulation = np.inf
                updatePhysicalStateByBeliefInSimulation = ba.UpdatePhysicalStateImagedByBelief(updatePhysicalStateByBeliefFrequencyInSimulation)

                updatePhysicalStateByBeliefFrequencyInPlay = np.inf
                updatePhysicalStateByBeliefInPlay = ba.UpdatePhysicalStateImagedByBelief(updatePhysicalStateByBeliefFrequencyInPlay)

                transitionFunctionInSimulation = env.TransitionFunction(resetPhysicalState, resetBeliefAndAttention, updatePhysicalState, transiteStateWithoutActionChangeInSimulation,
                        updateBeliefAndAttentionInSimulation, updatePhysicalStateByBeliefInSimulation)

                transitionFunctionInPlay = env.TransitionFunction(resetPhysicalState, resetBeliefAndAttention, updatePhysicalState, transiteStateWithoutActionChangeInPlay,
                        updateBeliefAndAttentionInPlay, updatePhysicalStateByBeliefInPlay)

                maxRollOutSteps = 5
                aliveBouns = 1/maxRollOutSteps
                deathPenalty = -1
                rewardFunction = reward.RewardFunctionTerminalPenalty(sheepId, aliveBouns, deathPenalty, isTerminal)
                rewardRollout = lambda state, action, nextState: rewardFunction(state, action)

                numActionSpace = 8
                actionInterval = int(360/(numActionSpace))
                actionMagnitude = actionRatio * minSheepSpeed
                actionSpace = [(np.cos(degreeInPolar) * actionMagnitude, np.sin(degreeInPolar) * actionMagnitude) for degreeInPolar in np.arange(0, 360, actionInterval)/180 * math.pi]
                getActionPrior = lambda state : {action: 1/len(actionSpace) for action in actionSpace}

                cInit = 1
                #cBase = 50
                scoreChild = ScoreChild(cInit, cBase)
                selectAction = SelectAction(scoreChild)
                selectNextState = SelectNextState(selectAction)

                initializeChildren = InitializeChildren(actionSpace, transitionFunctionInSimulation, getActionPrior)
                expand = Expand(isTerminal, initializeChildren)
                pWidening = PWidening(alpha, C)
                expandNewState = ExpandNextState(transitionFunctionInSimulation, pWidening)

                rolloutPolicy = lambda state: actionSpace[np.random.choice(range(numActionSpace))]
                rolloutHeuristic = lambda state: 0
                estimateValue = RollOut(rolloutPolicy, maxRollOutSteps, transitionFunctionInSimulation, rewardRollout, isTerminal, rolloutHeuristic)

                numActionPlaned = 1
                outputAction = OutputAction(numActionPlaned, actionSpace)
                #numSimulations = int(numTotalSimulationTimes/numTree)

                #sheepColorInMcts = np.array([0, 255, 0])
                #wolfColorInMcts = np.array([255, 0, 0])
                #distractorColorInMcts = np.array([255, 255, 255])
                #saveImageMCTS = True
                #mctsRender = env.MctsRender(numAgent, screen, xBoundary[1], yBoundary[1], screenColor, sheepColorInMcts, wolfColorInMcts, distractorColorInMcts, circleSize, saveImageMCTS, saveImageFile)
                #mctsRenderOn = False
                #mctsRender = None
                #pg.init()
                #mcts = MCTS(numSimulations, selectChild, expand, rollout, backup, selectAction, mctsRender, mctsRenderOn)
                pwMultipleTrees = PWMultipleTrees(numSimulations, selectAction, selectNextState, expand, expandNewState, estimateValue, backup, outputAction)

                maxRunningSteps = int(25 * numMDPTimeStepPerSecond)
                makeDiffSimulationRoot = MakeDiffSimulationRoot(isTerminal, updatePhysicalStateByBeliefInSimulationRoot)
                runMCTSTrjactory = RunMCTSTrjactory(maxRunningSteps, numTree, numActionPlaned, sheepActionUpdateFrequency, transitionFunctionInPlay, isTerminal, makeDiffSimulationRoot, render)

                rootAction = actionSpace[np.random.choice(range(numActionSpace))]
                numTrial = 10
                trajectories = [runMCTSTrjactory(pwMultipleTrees) for trial in range(numTrial)]

                savePath = getSavePath({'chasingSubtlety': chasingSubtlety, 'subIndex': subIndex})
                #tsl.saveToPickle(trajectories, savePath)
                #def getTrueWolfIndentityAcc(trajectory):
                #    AccTrial = []
                #    for timeStepIndex in range(len(trajectory)):
                #        timeStep = trajectory[timeStepIndex]
                #        wolfId = trajectory[0][0][0][3][0]
                #        wolfIdInEach = timeStep[0][0][3][0]
                #        #print(wolfId, '**', wolfIdInEach)
                #        if (timeStepIndex % 3 == 0) and timeStepIndex >= 11:
                #            AccTrial.append(timeStep[0][1][int(wolfIdInEach) - 1])
                #    meanIdentityAcc = np.mean(AccTrial)
                #    #meanIdentityAcc = np.mean(np.array([timeStep[0][1][int(timeStep[0][0][3][0] - 1)] for timeStep in trajectory])[11:])
                #    return meanIdentityAcc
                getTrueWolfIndentityAcc = lambda trajectory: np.array([timeStep[0][1][int(timeStep[0][0][3][0] - 1)] for timeStep in trajectory])[:]
                identityPerceptionTimeSeries = np.mean([getTrueWolfIndentityAcc(trajectory) for trajectory in trajectories], axis = 0)
                resultsTimeSeries = pd.DataFrame([identityPerceptionTimeSeries], columns = list(range(len(identityPerceptionTimeSeries))))
                savePathIdentitySeries = getCSVSavePath({'chasingSubtlety': chasingSubtlety, 'measure': 'identity'})
                if subIndex == 0:
                    resultsTimeSeries.to_csv(savePathIdentitySeries, mode='a')
                else:
                    resultsTimeSeries.to_csv(savePathIdentitySeries, mode='a', header=False)
                meanIdentityPerception = np.mean([getTrueWolfIndentityAcc(trajectory) for trajectory in trajectories])
                meanIdentityPerceptionOnConditions.update({chasingSubtlety: meanIdentityPerception})
                print(meanIdentityPerceptionOnConditions)

                getTrueWolfAttentionNumber = lambda trajectory: np.array([timeStep[0][2][int(timeStep[0][0][3][0] - 1)] for timeStep in trajectory])[:]
                attentionNumberTimeSeries = np.mean([getTrueWolfAttentionNumber(trajectory) for trajectory in trajectories], axis = 0)
                resultsAttentionTimeSeries = pd.DataFrame([attentionNumberTimeSeries], columns = list(range(len(attentionNumberTimeSeries))))
                savePathAttentionSeries = getCSVSavePath({'chasingSubtlety': chasingSubtlety, 'measure': 'attentionNumber'})
                if subIndex == 0:
                    resultsAttentionTimeSeries.to_csv(savePathAttentionSeries, mode='a')
                else:
                    resultsAttentionTimeSeries.to_csv(savePathAttentionSeries, mode='a', header=False)
                meanAttentionPerception = np.mean([getTrueWolfAttentionNumber(trajectory) for trajectory in trajectories])
                meanAttentionPerceptionOnConditions.update({chasingSubtlety: meanAttentionPerception})

                getSampledWolfIndentityAcc = lambda trajectory: np.array([int(int(timeStep[0][3][0][0]) == int(timeStep[0][0][3][0]))
                    for timeStep in trajectory])[:]
                identitySampledTimeSeries = np.mean([getSampledWolfIndentityAcc(trajectory) for trajectory in trajectories], axis = 0)
                resultsSampledTimeSeries = pd.DataFrame([identitySampledTimeSeries], columns = list(range(len(identitySampledTimeSeries))))
                savePathIdentitySampledSeries = getCSVSavePath({'chasingSubtlety': chasingSubtlety, 'measure': 'identitySampled'})
                if subIndex == 0:
                    resultsSampledTimeSeries.to_csv(savePathIdentitySampledSeries, mode='a')
                else:
                    resultsSampledTimeSeries.to_csv(savePathIdentitySampledSeries, mode='a', header=False)
                meanIdentitySampled = np.mean([getSampledWolfIndentityAcc(trajectory) for trajectory in trajectories])
                meanIdentitySampledOnConditions.update({chasingSubtlety: meanIdentitySampled})

            allResultsIdentity.append(meanIdentityPerceptionOnConditions)
            resultsIdentity = pd.DataFrame(allResultsIdentity)
            csvSavePathIdentity = getCSVSavePath({'measure': 'identity'})
            resultsIdentity.to_csv(csvSavePathIdentity)

            allResultsAttention.append(meanAttentionPerceptionOnConditions)
            resultsAttention = pd.DataFrame(allResultsAttention)
            csvSavePathAttention = getCSVSavePath({'measure': 'attentionNumber'})
            resultsAttention.to_csv(csvSavePathAttention)

            allResultsIdentitySampled.append(meanIdentitySampledOnConditions)
            resultsIdentitySampled = pd.DataFrame(allResultsIdentitySampled)
            csvSavePathIdentitySampled = getCSVSavePath({'measure': 'identitySampled'})
            resultsIdentitySampled.to_csv(csvSavePathIdentitySampled)

def drawPerformanceline(dataDf, axForDraw):
    plotDf = dataDf.reset_index()
    plotDf.plot(x = "subtleties", y = "escapeRate", ax = axForDraw)

def main():
    manipulatedVariables = OrderedDict()
    manipulatedVariables['alphaForStateWidening'] = [0.25]
    #manipulatedVariables['attentionType'] = ['idealObserver', 'hybrid4']
    manipulatedVariables['attentionType'] = ['hybrid4']
    #manipulatedVariables['attentionType'] = ['preAttention']
    #manipulatedVariables['attentionType'] = ['idealObserver', 'preAttention', 'attention4', 'hybrid4']
    #manipulatedVariables['attentionType'] = ['preAttentionMem0.65', 'preAttentionMem0.25', 'preAttentionPre0.5', 'preAttentionPre4.5']
    manipulatedVariables['CForStateWidening'] = [2]
    manipulatedVariables['minAttentionDistance'] = [10.0]#[5.0, 10.0, 20.0, 40.0]
    manipulatedVariables['rangeAttention'] = [10.0]#[5.0, 10.0, 20.0, 40.0]
    manipulatedVariables['cBase'] = [44]
    manipulatedVariables['numTrees'] = [1]
    manipulatedVariables['numSimulationTimes'] = [1]
    manipulatedVariables['actionRatio'] = [0.2]
    manipulatedVariables['burnTime'] = [0]

    productedValues = it.product(*[[(key, value) for value in values] for key, values in manipulatedVariables.items()])
    parametersAllCondtion = [dict(list(specificValueParameter)) for specificValueParameter in productedValues]

    DIRNAME = os.path.dirname(__file__)
    trajectoryDirectory = os.path.join(DIRNAME, '..', 'data', 'mcts',
                                'trajectories')
    if not os.path.exists(trajectoryDirectory):
        os.makedirs(trajectoryDirectory)

    trajectoryExtension = '.pickle'
    getTrajectorySavePathByCondition = lambda condition: tsl.GetSavePath(trajectoryDirectory, trajectoryExtension, condition)
    measurementEscapeExtension = '.csv'
    getCSVSavePathByCondition = lambda condition: tsl.GetSavePath(trajectoryDirectory, measurementEscapeExtension, condition)
    runOneCondition = RunOneCondition(getTrajectorySavePathByCondition, getCSVSavePathByCondition)

    #runOneCondition(parametersAllCondtion[0])
    numCpuCores = os.cpu_count()
    numCpuToUse = int(numCpuCores)
    runPool = mp.Pool(numCpuToUse)
    runPool.map(runOneCondition, parametersAllCondtion)

    precisionToSubtletyDict={500:0,50:5,11:30,3.3:60,1.83:90,0.92:120,0.31:150,0.001: 180}

    #levelValues = [numTotalSimulationTimes, numTrees, subtleties]
    #levelNames = ["numTotalSimulationTimes", "numTrees", "subtleties"]

    #modelIndex = pd.MultiIndex.from_product(levelValues, names=levelNames)

    #toSplitFrame = pd.DataFrame(index = modelIndex)

    #modelResultDf = toSplitFrame.groupby(levelNames).apply(evaluateMCTSInEscape)
    #fig = plt.figure()
    #plotLevels =levelNames [:1]
    #plotRowNum = len(levelValues[0])
    #plotColNum = len(levelValues[1])
    #plotCounter = 1

    #for (key, dataDf) in modelResultDf.groupby(["numTotalSimulationTimes", "numTrees"]):
    #    axForDraw = fig.add_subplot(plotRowNum, plotColNum, plotCounter)
    #    drawPerformanceline(dataDf, axForDraw)
    #    plotCounter+=1

    #plt.show()
    #fig.savefig('escapeRateActUpdateSheep2Wolf3Att95Tree3Sime36.png')
    #print("Finished evaluating")

if __name__ == "__main__":
    main()


import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import sys
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_absolute_error

testSet = []
testCoords = []

def evaluate_individual_performances(outputPath, trainSetCoordsPath, testSetCoordsPath, aruco, train_test_data):

    global testSet, testCoords
    # aruco = np.array(aruco)
    # m_id = str(aruco[0])
    m_id = aruco
    # Regression Degree
    DEG = 2

    # Train Set
    # Slow Feature Values
    trainSetPath = outputPath + '/Evaluation_Arrays/' + 'train_'+m_id+'_slowFeatures.npy'

    # Marker detection results
    detectionResultsTrainSetPath = outputPath + '/' + 'result_train.csv'

    # Load Files
    trainSet = np.load(trainSetPath)
    # trainSetCoords = np.loadtxt(trainSetCoordsPath, delimiter=',', usecols=(4, 5))
    trainSetCoords = np.loadtxt(trainSetCoordsPath)[:, 0:2]
    detectionResultsTrainSet = pd.read_csv(detectionResultsTrainSetPath)

    markerSquaresTrainSet = detectionResultsTrainSet[
        [m_id + '_bb_x1', m_id + '_bb_y1', m_id + '_bb_x2', m_id + '_bb_y2', m_id + '_bb_x3', m_id + '_bb_y3', m_id +
         '_bb_x4', m_id + '_bb_y4']].values

    # Keep only the coordinates with valid marker detection (Train/Test Set)
    trainCoords = []
    for i in range(trainSetCoords.shape[0]):
        if markerSquaresTrainSet[i].sum() != 0:
            trainCoords.append(trainSetCoords[i, :])
    trainCoords = np.array(trainCoords)

    print("\nActual coordinates\n ")
    print("Train Set: (%d,%d) " % trainSetCoords.shape)
    print("Train Set after marker detection: (%d,%d) " % trainCoords.shape)

    # Test set
    if train_test_data:
        testSetPath = outputPath + '/Evaluation_Arrays/' + 'test_'+m_id+'_slowFeatures.npy'
        detectionResultsTestSetPath = outputPath + '/' + 'result_test.csv'
        testSet = np.load(testSetPath)
        # testSetCoords = np.loadtxt(testSetCoordsPath, delimiter=',', usecols=(4, 5))
        testSetCoords = np.loadtxt(testSetCoordsPath)[:, 0:2]
        detectionResultsTestSet = pd.read_csv(detectionResultsTestSetPath)
        markerSquaresTestSet = detectionResultsTestSet[
            [m_id + '_bb_x1', m_id + '_bb_y1', m_id + '_bb_x2', m_id + '_bb_y2', m_id + '_bb_x3', m_id + '_bb_y3',
             m_id +
             '_bb_x4', m_id + '_bb_y4']].values
        testCoords = []
        for i in range(testSetCoords.shape[0]):
            if markerSquaresTestSet[i].sum() != 0:
                testCoords.append(testSetCoords[i, :])
        testCoords = np.array(testCoords)
        print("Test Set: (%d,%d) " % testSetCoords.shape)
        print("Test Set  after marker detection: (%d,%d) " % testCoords.shape)

    # Perform Regression
    #####################################################
    polyRegressor = PolynomialFeatures(degree=DEG)
    fig, ax = plt.subplots()

    if train_test_data:
        polyFeatureTrainingSet = polyRegressor.fit_transform(trainSet)
        polyFeaturesTestSet = polyRegressor.fit_transform(testSet)

        regressor_x = LinearRegression()
        regressor_x.fit(polyFeatureTrainingSet, trainCoords[:, 0])  # x-coordinates

        regressor_y = LinearRegression()
        regressor_y.fit(polyFeatureTrainingSet, trainCoords[:, 1])  # y-coordinates

        predicted_X = regressor_x.predict(polyFeaturesTestSet)
        predicted_Y = regressor_y.predict(polyFeaturesTestSet)

        prediction_X = predicted_X.reshape(predicted_X.shape[0], 1)
        prediction_Y = predicted_Y.reshape(predicted_Y.shape[0], 1)
        predictedCoordinates = np.hstack([prediction_X, prediction_Y])
        # Calculate Error
        ######################################################
        MAE = mean_absolute_error(testCoords, predictedCoordinates)
        print("\nMean Absolute Error: %f [m]\n" % MAE)
        # Visualize
        ######################################################
        ax.plot(testCoords[:, 0], testCoords[:, 1], 'b.', lw=1, label='Ground truth')
        ax.plot(prediction_X, prediction_Y, 'r.', lw=1, label='Estimation')

    else:
        polyFeatureTrainingSet = polyRegressor.fit_transform(trainSet)
        # polyFeaturesTestSet = polyRegressor.fit_transform(testSet)

        X_train, X_test, y_train, y_test = train_test_split(polyFeatureTrainingSet, trainCoords, test_size=0.33,
                                                            random_state=42)

        regressor_x = LinearRegression()
        regressor_x.fit(X_train, y_train[:, 0])  # x-coordinates

        regressor_y = LinearRegression()
        regressor_y.fit(X_train, y_train[:, 1])  # y-coordinates

        predicted_X = regressor_x.predict(X_test)
        predicted_Y = regressor_y.predict(X_test)

        prediction_X = predicted_X.reshape(predicted_X.shape[0], 1)
        prediction_Y = predicted_Y.reshape(predicted_Y.shape[0], 1)
        predictedCoordinates = np.hstack([prediction_X, prediction_Y])

        MAE = mean_absolute_error(y_test, predictedCoordinates)
        print("\nMean Absolute Error: %f [m]\n" % MAE)

        ax.plot(y_test[:, 0], y_test[:, 1], 'b.', lw=1, label='Ground truth')
        ax.plot(prediction_X, prediction_Y, 'r.', lw=1, label='Estimation')

    plt.title('Mean Absolute Error: ' + "{:.2f}".format(MAE), fontweight='bold')
    legend = ax.legend(loc='lower right', shadow=True)
    plt.xlabel('X [m]', fontsize=16)
    plt.ylabel('Y [m]', fontsize=16)
    plt.tick_params(top='off', bottom='on', left='on', right='off', labelleft='on', labelbottom='on')
    plt.rc('font', weight='bold')
    plt.rc('legend', **{'fontsize': 8})
    plt.gca().set_aspect('equal', adjustable='box')
    plt.tick_params(labelsize=12)
    plt.savefig(outputPath + '/' + 'marker_'+m_id+'_result.pdf', dpi=1200, bbox_inches='tight')  # Save?
    plt.show()

if __name__ == "__main__":
    if len(sys.argv) != 6:
        print("usage: python executeTestSet")
        print("     image folder        # path to the folder containing images")
        print("     total Number of Images in the folder containing images     ")
        print("     image suffix        # suffix of the image file")
        print("     Model Name          # Trained model name")
        print("\nExample: python executeTestSet.py images/ 1600 .png train\n")
        sys.exit()
    evaluate_individual_performances(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5])


#!/usr/bin/python
# -*- coding: utf-8 -*-

from xml.etree.ElementTree import ElementTree
import xml.etree
import xml.dom.minidom

import json

class salt_xml:
    tree = ElementTree()

    def __init__(self):
        json_data = open('insert.json')
        data = json.load(json_data)
        self.tree.parse('config-original.xml')
        self.read_json(data, "")
        self.tree.write('config.xml', method="xml", xml_declaration=False)

    def read_json(self, data, find):
        for key, value in data.iteritems():
            if isinstance(value, dict):
                if find is "None":
                    self.read_json(value, str(key))
                else:
                    self.read_json(value, str(find) + "/" + str(key) )
            else:
                self.tree.find(str(key)).text = str(value)

    def render(self):
        return xml.dom.minidom.parse('config.xml').toprettyxml()

def test():
    render = salt_xml()
    print(render.render())

test()


"""
@description: 命名实体识别模块
@author: Cui Rui long
@email: xiaocuikindle@163.com
@time: 2019-02-27
@version: 0.0.1
"""
from collections import defaultdict

import jieba
import logging

from sementic_server.source.ner_task.entity_code import EntityCode
from sementic_server.source.ner_task.system_info import SystemInfo
from sementic_server.source.ner_task.model_tf_serving import ModelServing

logger = logging.getLogger("server_log")

class SemanticSearch(object):
    """
    通过调用 sentence_ner_entities 函数实现对：人名、组织结构名、地名和日期 的识别
    """

    def __init__(self, test_mode=False):

        self.system_info = SystemInfo(is_test=test_mode)

        self.client = ModelServing(self.system_info.MODE_NER, is_test=test_mode)

        self.config = self.system_info.get_config()

        self.entity_code = EntityCode()
        self.ner_entities = self.entity_code.get_ner_entities()
        self.code = self.entity_code.get_entity_code()

        self.labels_list = []
        self.labels_list_split = []
        self.__init_specific_label_combine()
        self.__init_jieba()

    def __init_specific_label_combine(self):
        """
        初始化labels_list和labels_list_split列表
        用于将出现的此类标签：“NAMECOMPANY” 分开成 “NAME#COMPANY”
        :return:
        """
        entities = self.entity_code.get_entities()
        for i in range(0, len(entities)):
            for j in range(0, len(entities)):
                if i != j:
                    self.labels_list.append(entities[i] + entities[j])
                    self.labels_list_split.append((entities[i] + "#" + entities[j]))

                    self.labels_list.append(entities[j] + entities[i])
                    self.labels_list_split.append((entities[j] + "#" + entities[i]))

    def __init_jieba(self):
        """
        可以给分词工具加入领域词汇辅助分词，加入公司名称可以有效提升分词工具对公司名称分词的准确度
        :return:
        """
        entities = self.entity_code.get_entities()
        for label in entities:
            jieba.add_word(label)

    def __combine_label(self, entities, label=None):
        """
        合并实体列表中相连且相同的label
        :param entities:
        :param label:
        :return:
        """
        pre_label = False
        first_label = None
        entities_copy = []
        for i in range(len(entities)):
            if entities[i][1] != label:
                pre_label = False
                if first_label is not None:
                    entities_copy.append(first_label)
                    first_label = None
                entities_copy.append(entities[i])
            elif pre_label is False and entities[i][1] == label:
                pre_label = True
                first_label = entities[i]
            elif pre_label and first_label is not None and entities[i][1] == label:
                temp = first_label
                first_label = [temp[0] + entities[i][0], temp[1]]

        if first_label is not None:
            entities_copy.append(first_label)

        return entities_copy

    def __combine_com_add(self, entities):
        """
        合并 COMPANYADDR 和 ADDRCOMPANY 这类实体为 COMPANY
        :param entities:
        :return:
        """
        company_index = -1
        addr_index = -1

        for i, entity in enumerate(entities):
            if self.ner_entities['COMPANY'] == entity[1]:
                company_index = i
            if self.ner_entities['ADDR'] == entity[1]:
                addr_index = i

        if company_index != -1 and addr_index != -1:
            if company_index == addr_index + 1:
                entities[company_index][0] = entities[addr_index][0] + entities[company_index][0]
                entities.remove(entities[addr_index])
            elif company_index == addr_index - 1:
                entities[company_index][0] = entities[company_index][0] + entities[addr_index][0]
                entities.remove(entities[addr_index])

    def __split_diff_labels(self, template_sen):
        """
        检测模板句中是否有不同的label相互连接的情况，eg. "ADDRNAME"，这种情况分词工具无法正确分词
        如果存在相连的label，使用“#”将两个label分开
        :param template_sen: 模板句子
        :return:
        """
        for i, label in enumerate(self.labels_list):
            if label in template_sen:
                template_sen = template_sen.replace(label, self.labels_list_split[i])
        return template_sen

    def __convert_output_data_format(self, data_param):
        """
        将 data_param 数据转换成问答图模块需要的数据格式
        :param data_param:
        :return:
        """
        output = defaultdict()
        output["query"] = data_param["raw_input"]
        output["template"] = data_param["new_input"]
        entity = []
        for key, values in data_param["labels"].items():
            for v in values:
                begin = data_param["raw_input"].find(v)
                entity.append(
                    {"type": key, "value": v, "code": self.code[key], "begin": begin,
                     "end": begin + len(v) + 1 if begin != -1 else -1})
        output["entity"] = entity
        return output

    def sentence_ner_entities(self, result):
        """
        使用 BERT 模型对句子进行实体识别，返回标记实体的句子
        :param result: account_label 模块返回的结果
        :return:
            entities: 列表，存储的是 BERT 识别出来的实体信息:(word, label)
            result: account_label 模块返回的结果
        """
        sentence = result["new_input"]

        sentence, pred_label_result = self.client.send_grpc_request_ner(sentence)

        word = ""
        label = ""
        entities = []

        if sentence is None or pred_label_result is None:
            return entities, result

        for i in range(len(sentence)):
            temp_label = pred_label_result[i]
            if temp_label[0] == 'B':
                if word != "":
                    if "##" in word:
                        word = word.replace('##', '')
                    entities.append([word, label])
                    word = ""

                if temp_label[2:] == 'ORG':
                    label = self.ner_entities['COMPANY']
                elif temp_label[2:] == 'PER':
                    label = self.ner_entities['NAME']
                elif temp_label[2:] == 'DATE':
                    label = self.ner_entities['DATE']
                else:
                    label = self.ner_entities['ADDR']

                word += sentence[i]
            elif temp_label[0] == 'I' and word != "":
                word += sentence[i]
            elif temp_label == 'O' and word != "":
                if "##" in word:
                    word = word.replace('##', '')
                entities.append([word, label])
                word = ""
                label = ""
        if word != "":
            if "##" in word:
                word = word.replace('##', '')
            entities.append([word, label])

        if len(entities) != 0:
            entities = self.__combine_label(entities, label=self.ner_entities['ADDR'])
            entities = self.__combine_label(entities, label=self.ner_entities['COMPANY'])
            entities = self.__combine_label(entities, label=self.ner_entities['NAME'])
            entities = self.__combine_label(entities, label=self.ner_entities['DATE'])

            self.__combine_com_add(entities)
            entities = self.__combine_label(entities, label=self.ner_entities['COMPANY'])

        for (word, label) in entities:
            result["new_input"] = result["new_input"].replace(word, label)
            result["labels"].setdefault(label, []).append(word)

        result = self.__convert_output_data_format(result)
        return result


from datetime import datetime
import tensorflow as tf
from tensorflow.keras.applications import DenseNet121
import numpy as np
import os
#######################################
config = tf.compat.v1.ConfigProto()   #
config.gpu_options.allow_growth = True#
tf.compat.v1.Session(config=config)   #
#######################################
import PIL
import PIL.Image
import tensorflow_datasets as tfds

from os import listdir
from os.path import isfile, join

import csv

import pandas as pd
import tensorflow.keras as keras
import time
import tensorboard
import matplotlib.pyplot as plt
from tensorflow import keras
import subprocess

currentDir = os.getcwd().replace("source", "")
dataset_path = currentDir + "dataset/train_images/train_images/"

imagesTrainPath = "../dataset/train_images/train_images/"
labelsPath = "../dataset/train_images/train_label.txt"
TRAINING_FILENAMES = [f for f in listdir(imagesTrainPath) if isfile(join(imagesTrainPath, f))]
#################################################
# Import the necessary libraries
from PIL import Image
from numpy import asarray

CLASSES = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']

def load_labels(labelsPath, classes):
    df = pd.read_csv(labelsPath)
    lst = df.values.tolist()
    labels = [classes.index(i[0]) for i in lst]
    return labels

def to_oneHot(labels):
    a = labels.astype(int)
    b = np.zeros((a.size, a.max()+1))
    b[np.arange(a.size),a] = 1
    return b

def get_class(modelOutput): #takes the max probability index of a row and turns it into a one hot vector
    b = np.zeros(modelOutput.shape)
    b[np.arange(b.shape[0]), np.argmax(a, axis=1)] = 1
    return b

def one_hot_to_lables(onehotData):
    return np.argmax(onehotData, axis = 1)

def write_to_file(data, outputFile = "output.csv"):
    myFile = open(outputFile, 'w', newline='')
    with myFile:
        writer = csv.writer(myFile)
        data = [["id", "classes"]] + data
        writer.writerows(data)
    print("Writing to ", outputFile," completed")

# print('Tensorflow version ' + tf.__version__)
# print(TRAINING_FILENAMES[0:10])

# os.popen(cp data'.txt' /label)
imgsize = (32,32)
batchsize = 500

train_ds = tf.keras.preprocessing.image_dataset_from_directory(
    "../dataset/train_images/train_images",
    labels = "inferred",
    label_mode = 'categorical',
    validation_split=0.10,
    subset="training",
    shuffle=True,
    seed=321,
    image_size = imgsize,
    batch_size=batchsize)

val_ds = tf.keras.preprocessing.image_dataset_from_directory(
    "../dataset/train_images/train_images",
    labels = "inferred",
    label_mode = 'categorical',
    validation_split=0.10,
    subset="validation",
    seed=321,
    image_size=imgsize,
    batch_size=batchsize)

AUTOTUNE = tf.data.experimental.AUTOTUNE

train_ds = train_ds.cache().prefetch(buffer_size=AUTOTUNE)
val_ds = val_ds.cache().prefetch(buffer_size=AUTOTUNE)

logdir = "logs/scalars/" + datetime.now().strftime("%Y%m%d-%H%M%S")
tensorboard_callback = keras.callbacks.TensorBoard(log_dir=logdir)

EPOCHS = 15
# LR_START = 0.00001
# LR_MAX = 0.00005
# LR_MIN = 0.00001
# LR_RAMPUP_EPOCHS = 5
# LR_SUSTAIN_EPOCHS = 0
# LR_EXP_DECAY = .8

LR_START = 0.00001      # bfore 0.0001
LR_MAX = 0.00045 # bfore 0.0005
LR_MIN = 0.00001
LR_RAMPUP_EPOCHS = 8
LR_SUSTAIN_EPOCHS = 2 # before 2
LR_EXP_DECAY = .8

# LR_START = 0.00001
# LR_MAX = 0.00015
# LR_MIN = 0.00001
# LR_RAMPUP_EPOCHS = 8
# LR_SUSTAIN_EPOCHS = 2
# LR_EXP_DECAY = .9

def lrfn(epoch):
    if epoch < LR_RAMPUP_EPOCHS:
        lr = (LR_MAX - LR_START) / LR_RAMPUP_EPOCHS * epoch + LR_START
    elif epoch < LR_RAMPUP_EPOCHS + LR_SUSTAIN_EPOCHS:
        lr = LR_MAX
    else:
        lr = (LR_MAX - LR_MIN) * LR_EXP_DECAY**(epoch - LR_RAMPUP_EPOCHS - LR_SUSTAIN_EPOCHS) + LR_MIN
    return lr
# def lrfn(epoch):
#     if epoch < LR_RAMPUP_EPOCHS:
#         lr = (LR_MAX - LR_START) / LR_RAMPUP_EPOCHS * epoch + LR_START
#     elif epoch < LR_RAMPUP_EPOCHS + LR_SUSTAIN_EPOCHS:
#         lr = LR_MAX
#     elif epoch > (LR_RAMPUP_EPOCHS + LR_SUSTAIN_EPOCHS)/2:
#          lr = LR_MAX/2
#     else:
#         lr = (LR_MAX - LR_MIN) * LR_EXP_DECAY**(epoch - LR_RAMPUP_EPOCHS - LR_SUSTAIN_EPOCHS) + LR_MIN
#     return lr

lr_callback = tf.keras.callbacks.LearningRateScheduler(lrfn, verbose = True)
rng = [i for i in range(25 if EPOCHS<25 else EPOCHS)]
y = [lrfn(x) for x in rng]
# plt.plot(y)
# plt.show()
import random
def random_invert_img(x, p=0.5):
  if  random.random() < p:
    x = (255-x)
  else:
    x
  return x

def random_invert(factor=0.5):
  return tf.keras.layers.Lambda(lambda x: random_invert_img(x, factor))

# def random_greyscale_convert_img(x, p=0.5):
#   if  random.random() < p:
#     x = tf.image.rgb_to_grayscale(x)
#   else:
#     x
#   return x

# def random_greyscale(factor = 0.5):
#     return tf.keras.layers.Lambda(lambda x: random_greyscale_convert_img(x, factor ))

class convNeuralNet:
    def __init__(self):
        self.model = self.modelInit()

    def modelInit(self):
        rnet = DenseNet121(
            input_shape=(32,32,3),
            weights="imagenet",
            include_top=False,
            pooling=max
        )
        rnet.trainable = True
        model = keras.models.Sequential()

        model.add(random_invert(factor=0.1))
        model.add(tf.keras.layers.GaussianNoise(0.12)) #before 0.1
        model.add(tf.keras.layers.experimental.preprocessing.Rescaling(1./255))
        model.add(keras.layers.experimental.preprocessing.RandomFlip("horizontal")) #84.5 horizontal
        model.add(tf.keras.layers.experimental.preprocessing.RandomContrast(0.01))

        model.add(rnet)
        model.add(keras.layers.GlobalMaxPooling2D())

        model.add(keras.layers.Dense(10, activation = 'sigmoid'))

        model.compile(loss = 'categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
        return model

    def modelLoad(self, path):
        self.model = keras.models.load_model(path)

    def train(self, dataset, valset, nEpochs):
        self.model.fit(dataset, validation_data=val_ds, epochs = nEpochs, callbacks=[lr_callback, tensorboard_callback],verbose=2)

    def predict(self, data_x):
        return self.model.predict(data_x)

    def ajustDropout(self, new_rates):
        if(len(new_rates)==3):
            self.model.layers[2].rate = new_rates[0]
            self.model.layers[5].rate = new_rates[1]
            self.model.layers[11].rate = new_rates[2]
    def ajustRotation(self, newrate):
        self.model.layers[1].factor = newrate

tensorboard_callback = keras.callbacks.TensorBoard(log_dir=logdir)

modelConv = convNeuralNet()
modelConv.train(train_ds,val_ds, 20)

testDir = "../dataset/test_images/content/data/test_imagess/"

test_list = []
for i in range(10000):
    img = Image.open(testDir +"Image_"+str(i+1) + ".png")
    array = asarray(img)
    test_list.append(array)

test_list = np.array(test_list)

prediction = modelConv.predict(test_list)

i = np.arange(prediction.shape[0])
labels = one_hot_to_lables(prediction)
output = np.stack((i, labels), axis = 1)
print(len(labels))
write_to_file(output.tolist(), outputFile = "prediction.csv")


#Zach Wu 2016
#Note: NK Testing does not account for stop codons or deletrions

import numpy as np
import sys
import random
import NK_Landscape
from scipy.stats import poisson

sample_parent = [random.randint(0,19) for i in range(10)]
num_AA = 20

def single_mutant_walk(parent, n):
    '''sequence should be in [19,3,0,1] format
        n is length of walk
    '''
    current = parent
    seq_list = [current]

    for i in range(n):
        #print(current)
        mut_index = random.randint(0,len(parent)-1)
        curr_AA = current[mut_index]
        poss_AA = set([j for j in range(num_AA)])
        poss_AA.remove(curr_AA)
        mut_AA = list(poss_AA)[random.randint(0,len(poss_AA)-1)]
        current[mut_index] = mut_AA
        seq_list.append(current)
    print(seq_list[-1])
    if n == 1: #if n == 1, probably using this for epm.
        return current
    else:
        return seq_list

def epm(parent, library_size, rate = 1):
    '''parent sequence should be in [19,3,0,1] format
        rate is the average number of mutations
        I'm just going to assume a poisson distribution for now
        check http://www.ncbi.nlm.nih.gov/pubmed/15939434

        -Fox assumes gamma distribution
    '''
    dist_countL, num_mutL = EPM_Poisson_countd(rate, library_size)

    print(str(dist_countL) + ' ' + str(num_mutL))

    seq_list = [] #does not include parent
    parent_temp = parent

    for i,num_mut in enumerate(num_mutL):
        for j in range(dist_countL[i]):
            seq_list.append(mutate_sequence(parent_temp, num_mut))
    return seq_list

def mutate_sequence(parent2, num_mutations):
#     mut_seq = parent
#     mut_index = random.randint(0,len(parent)-1)
#     curr_AA = mut_seq[mut_index]
#     poss_AA = set([j for j in range(20)])

#     list_mut = []
    mut_seq = parent2
    for i in range(num_mutations):
        mut_seq = single_mutant_walk(mut_seq,1)
    print(mut_seq)

    return mut_seq

def EPM_Poisson_countd(mu, library_size):
    #returns the Poisson mutation rate distribution for a given library size

    probs_list = []
    mut_list = []
    alpha = 1-1/(library_size*10)
    a,b = poisson.interval(alpha, mu, loc=0)
    a = int(a)
    b = int(b)
    for k in range(a,b+1):
        k_count = int(round(poisson.pmf(k,mu)*library_size,0))
        if k_count != 0:
            probs_list.append(k_count)
            mut_list.append(k)
    dif = sum(probs_list) - library_size
#     print(dif)
    mutation_list = [i for i in range(a,b+1)]
    index = mutation_list.index(mu)
    probs_list[index] -= dif

#     print(probs_list)
#     print(sum(probs_list))
    return probs_list, mut_list

def NK_Featurize(seqList):
    featurized_seq = []
    for i, seq in enumerate(seqList):
        temp_seq = [0] * len(seq) * num_AA
        for j, AA in enumerate(seq):
            temp_seq[j*20 + AA] = 1
        featurized_seq.append(temp_seq)
    return featurized_seq
###############################################################
###############################################################
###############################################################

import time
n = 10                #sequence length
library_size = 3*n

beg = time.clock()
test = NK_Landscape.NKLandscape(n,1, savespace = False, epi_dist = 'gamma')
interactions_list = test.nk_interactions()
epistatic_list = test.nk_epistatic()
end = time.clock()
print(-beg+end)

parent = [np.random.randint(num_AA) for i in range(n)]
print('Parent:' + str(parent))
#library1 = epm(parent, library_size, rate=3)
library2 = single_mutant_walk(parent,library_size)
print('end')

#library1_features = NK_Featurize(library1)
library2_features = NK_Featurize(library2)

library1_fitnessL = []
library2_fitnessL = []

# print(interactions_list)
# for i in range(len(library1_features)):
#     t1 = test.get_Energy(library1[i])
print(library2)


'''Various configuration parameters and functions
'''

import logging
import os

# ----------------------------------------------------------------------
# Default parameters for use with matplotlib.pyplot.rcParams.update

_MPL_CONFIG = {
    "figure.figsize": [6.0, 4.5],
    "grid.linewidth": 1.2,
    "grid.linestyle": "--",
    "axes.linewidth": 1.2,
    "lines.linewidth": 1.2,
    "figure.dpi": 80,
    "savefig.dpi": 200,
    "font.size": 12,
    "legend.fontsize": 12,
    "figure.titlesize": 12,
    "patch.force_edgecolor": "True",
    "patch.facecolor": "b",
    "font.sans-serif": "Times New Roman",
    "font.family": "sans-serif"
    }

# ----------------------------------------------------------------------
# Default parameters for special plots

_SETTLING_BAND_STYLES = dict(
    color='g',
    linestyle='--'
    )

_SETTING_TIME_STYLES = dict(
    color='r',
    linestyle='--'
    )

_RISE_SETTLE_TEXT_STYLES = dict(
    horizontalalignment='center',
    verticalalignment='center',
    bbox=dict(facecolor='white', alpha=0.8)
    )

# ----------------------------------------------------------------------
# Default folder structure and functions relative to specified root /

PSSPLOT_FOLDERS = {
    'out_files': r'data\out_files',
    'excel_files': r'data\excel',
    'output_folder': r'output',
    'plot_folder': r'plot_files'
    }

# ----------------------------------------------------------------------
# Logger initialisation

def init_logger(name):
    'Create logger with set parameters'
    _logger = logging.getLogger(name)
    _format = '[%(asctime)s]:%(levelname)s:%(name)s:%(funcName)s:%(message)s'
    logging.basicConfig(
        level=logging.INFO,
        format=_format,
        datefmt='%Y-%m-%d %H:%M:%S'
        )
    return _logger


from models.user import User
from models.message import Message
from routes import *
from routes.user import current_user
import json
import redis

main = Blueprint('chat', __name__)

red = redis.Redis(host='localhost', port=6379, db=0)

# 发布聊天广播的 redis 频道
chat_channel = 'chat'

def stream():
    '''
    监听 redis 广播并 sse 到客户端
    '''
    # 对每一个用户 创建一个[发布订阅]对象
    pubsub = red.pubsub()
    # 订阅广播频道
    pubsub.subscribe(chat_channel)
    # 监听订阅的广播
    for message in pubsub.listen():
        print(message)
        if message['type'] == 'message':
            data = message['data'].decode('utf-8')
            # 用 sse 返回给前端
            yield 'data: {}\n\n'.format(data)

@main.route('/subscribe')
def subscribe():
    return Response(stream(), mimetype="text/event-stream")

@main.route('/')
def index_view():
    cu = current_user()
    ms = Message.query.all()
    return render_template('index.html', messages=ms, cu=cu)

def current_time():
    return int(time.time())

@main.route('/chat/add', methods=['POST'])
def chat_add():
    cu = current_user()
    if cu is None:
        abort(401)
    msg = request.get_json()
    msg['user_id'] = cu.id
    m = Message(msg)
    m.save()
    r = {
        'name': m.user.username,
        'avatar': m.user.avatar,
        'content': m.content,
        'channel': m.channel,
        'created_time': m.created_time.strftime("%Y-%m-%d %H:%M"),
    }
    message = json.dumps(r, ensure_ascii=False)
    print('debug', message)
    # 用 redis 发布消息
    red.publish(chat_channel, message)
    return 'OK'


from tkinter import *
import tkinter as tk

import pymysql
from tkinter import messagebox, ttk,Tk,Canvas
from tkinter.messagebox import askquestion

from PIL import ImageTk

class Add_Category:

    def __init__(self):
        self.my_frame = tk.Toplevel()

        self.my_frame.title("Add Login Type")
        width = 400
        height = 200
        screen_width = self.my_frame.winfo_screenwidth()
        screen_height = self.my_frame.winfo_screenheight()
        x = (screen_width / 2) - (width / 2)
        y = (screen_height / 2) - (height / 2)
        self.my_frame.geometry("%dx%d+%d+%d" % (width, height, x, y))

        self.imagelb1=Label(self.my_frame,text="Add Ctegory",font=("georgia",50,"bold"))
        self.lb1 = Label(self.my_frame, text="Category")

        self.ment=StringVar()
        self.t1 = Entry(self.my_frame, width=20,textvariable=self.ment)

        self.t1.focus()

        self.btn1 = Button(self.my_frame, text="Save", padx=10, command=self.savedata, width=30)

        self.lb1.place(x=50, y=70)
        self.t1.place(x=150, y=70)

        self.btn1.place(x=100, y=100)

#btn2.place(x=170, y=500)

        self.myimage1 = PhotoImage(file='C:\\Users\\ashvi\PycharmProjects\\automobile_management\\costum.png')
        self.imagelabel2 = Label(self.my_frame, image=self.myimage1)
        self.imagelabel2.place(x=650,y=190)
        self.imagelb1.place(x=610,y=50)

        self.my_frame.mainloop()
    def savedata(self):

        try:
            myobj=pymysql.connect(host="localhost", user="root",
                              password="", db="automobile")

            with myobj.cursor() as myconn:

                myconn.execute("insert into "
                           "logmain(logintype) "
                           "values(%s)",

                     (self.t1.get()))

                if (self.t1.get() == "") :
                    messagebox.showerror("fill box", "complete the entries", parent=self.my_frame)
                #
                #
                else:

                    myobj.commit()
                    myobj.close()
                    messagebox.showinfo("Success", "Record Saved Successfully",parent=self.my_frame)
        except Exception as ex:
            messagebox.showerror("Error Occured", "Error occured due to " + str(ex),parent=self.my_frame)


import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable

from src.model.attention import Attention
from src.model.mixture_of_experts import MoE

class Decoder(nn.Module):
    '''
    Attention based decoder.
    Decodes encoded visual features of a video with previous word prediction in order to generate captions.
    Architecture is given by:
        ATTENTION - GRU - DROPOUT - MIXTURE-OF-EXPERTS-LAYER (zero-shot approach) - LOG-SOFTMAX
    '''
    def __init__(self, decoder_hid_dem, encoder_hid_dem, vocab, vocab_size, recipe1M_embeddings, embedding_dim,
                 decoder_input_size, linear_input_size, bidirectional_encoder, bidirectional_decoder, dropout,
                 moe=False, num_experts=1, device = torch.device('cuda')):
        '''
        Initialization of the decoder.

        Input:
        :param decoder_hid_dem: integer, hidden dimension of decoder
        :param encoder_hid_dem: integer, hidden dimension of encoder
        :param vocab: used vocabulary of the data
        :param vocab_size: integer, number of words in the vocabulary
        :param recipe1M_embeddings: boolean, True if recipe1M_embedding is considered (for zero-shot approach), else False
        :param embedding_dim: integer, dimension of vocabulary embedding
        :param decoder_input_size: integer, input dimension of decoder
        :param linear_input_size: integer, input dimension of fully connected layer after GRU
        :param bidirectional_encoder: boolean, True if encoder is bidirectional, else False
        :param bidirectional_decoder: boolean, True is decoder is bidirectional, else False
        :param dropout: double, probability of dropout
        :param moe: boolean, True if Mixture of Experts (MoE) layer is considered (for zero-shot approach), else False
        :param num_experts: integer, number of Experts in MoE layer, just for moe=True
        :param device: string, specifies availability of CUDA
        '''
        super(Decoder, self).__init__()
        self.encoder_hid_dem = encoder_hid_dem
        self.decoder_hid_dem = decoder_hid_dem
        self.attention = Attention(encoder_hid_dem, decoder_hid_dem, bidirectional_encoder)
        self.dropout = dropout
        self.output_dim = vocab_size
        self.embedding_dim = embedding_dim
        self.num_experts = num_experts
        self.device = device

        # Determine number of directions
        self.decoder_n_direction = 1
        if bidirectional_decoder == True:
            self.decoder_n_direction = 2

        self.bidirectional_encoder = bidirectional_encoder

        self.GRU_layer_out = nn.GRU(decoder_input_size, decoder_hid_dem * 2)
        self.out_layer = nn.Linear(in_features=linear_input_size, out_features=vocab_size)
        self.dropout = nn.Dropout(dropout)

        if recipe1M_embeddings:
            self.embedding_layer, self.embedding_dim = create_recipe1M_embedding_layer(
                torch.Tensor(vocab.weight_matrix), True)
        else:
            self.embedding_layer = nn.Embedding(vocab_size, self.embedding_dim)

        self.moe = moe
        if self.moe:
            self.MoE = MoE(num_experts, decoder_hid_dem, embedding_dim, None, self.decoder_n_direction,
                           device=self.device).to(self.device)

    def forward(self, hidden, actual_word, encoder_outputs, recipe_type):
        '''
        Performs the forward pass of the decoder.

        Inputs:
        :param hidden: torch.tensor, hidden decoder state of previous step
        :param actual_word: torch.tensor, word prediction of previous step
        :param encoder_outputs: torch.tensor, output vector of encoder
        :param recipe_type: torch.tensor, recipe types of samples
        :return: torch.tensor, torch.tensor: softmax probabilities for each word in vocab, hidden decoder state
        '''

        # hidden: [batch_size, dec_hid_dim]
        # actual_word: [batch_size, 1, embedding_dim]
        # encoder_outputs: [batch_size , seq_len, encoder_hid_dim * encoder_n_directional]

        a = self.attention(hidden, encoder_outputs)

        input_char = torch.cat((actual_word, a), 2)

        # [1, batch_size, decoder_input_size]
        input_char = input_char.permute(1, 0, 2)

        # [1 batch_size decoder_hid_dem]
        hidden = hidden.unsqueeze(0)

        # output: [1, batch_size , decoder_n_direction*decoder_hid_dim]
        # hidden: [n_layer*n_direction, batch_size, decoder_hid_dim]
        output, hidden = self.GRU_layer_out(input_char, hidden)

        # Apply Mixture of Experts layer - zero-shot approach
        if self.moe:
            hidden = hidden.to(self.device)
            recipe_type = recipe_type.to(self.device)
            output = self.MoE(hidden.squeeze(dim=0), recipe_type)
            output = torch.cat((output.squeeze(), a.squeeze(1), actual_word.squeeze(1)), dim=1)
            pre_out = self.out_layer(output)
        else:
            output = self.dropout(output)
            output = torch.cat((output.squeeze(0), a.squeeze(1), actual_word.squeeze(1)), dim=1)
            pre_out = self.out_layer(output)

        # prediced_output: [ batch_size , vocab_size ]
        predicted_output = F.log_softmax(pre_out, dim=1)

        return predicted_output, hidden.squeeze(0)

    def add_topic_embedding(self, topic_embedding, non_trainable=False):
        '''
        Add TFIDF-topic embedding

        Input:
        :param topic_embedding: torch.tensor, calculated weights for tfidf topic embedding
        :param non_trainable: boolean, False if embedding will not be optimized during training, else True
        '''
        # Determine dimensions for tfidf topic embedding
        num_embeddings, embedding_dim = topic_embedding.size()

        #Set up embedding with initial tfidf weights
        self.topic_embedding = nn.Embedding(num_embeddings, embedding_dim)
        self.topic_embedding.load_state_dict({'weight': topic_embedding})
        if non_trainable:
            self.topic_embedding.weight.requires_grad = False

        # Inform MoE Layer about tfidf embedding
        if self.moe:
            self.MoE = MoE(
                self.num_experts, self.decoder_hid_dem, embedding_dim, self.topic_embedding, self.decoder_n_direction,
                device=self.device).to(self.device)

def create_recipe1M_embedding_layer(weights_matrix, non_trainable=False):
    '''
    Creates Recipe1M vocabulary embedding (for zero-shot approach)

    Input:
    :param weights_matrix: torch.tensor, initial weights for vocabulary embedding
    :param non_trainable: boolean, False if embedding will not be optimized during training, else True

    Output:
    :return: torch.tensor.nn.embedding, integer, emebdding with its dimension
    '''
    # Determine dimensions for vocabulary embedding
    num_embeddings, embedding_dim = weights_matrix.size()

    # Set up emebdding with initial vocabulary weights
    emb_layer = nn.Embedding(num_embeddings, embedding_dim)
    emb_layer.load_state_dict({'weight': weights_matrix})
    if non_trainable:
        emb_layer.weight.requires_grad = False

    return emb_layer, embedding_dim