from datetime import datetime
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import seaborn as sns
import numpy as np
savedir = "/Users/tburch/Documents/gitDevelopment/thesis/progressTracking/"

df = pd.read_csv('/Users/tburch/Documents/gitDevelopment/thesis/progressTracking/progress.csv')
df['timestamp_fixed'] = pd.to_datetime(df['timestamp'], format="%Y-%m-%d %H:%M:%S")
df = df.set_index('timestamp_fixed')

current_pagecount = df["pagecount"].iloc[-1]
current_wordcount = df["wordcount"].iloc[-1]

pc_color="royalblue"

def make_pages_plot(axis):
    df.plot(y='pagecount',legend=None, color=pc_color, ax=axis,label="Raw Data")
    done_resample_df =  df.resample('w').max()
    done_resample_df['pagecount'].plot(figsize=(12,8), color='salmon',label="Rolling 7-day Max", ax=axis)
    slope = pd.Series(np.gradient(done_resample_df["pagecount"].values), done_resample_df.index, name='slope')
    plt.sca(axis)
    plt.plot(done_resample_df['pagecount'].diff(), color="forestgreen",label="Rolling 7-Day Change")
    plt.ylabel("Pages",fontsize=14)
    plt.annotate("{:,d} Pages".format(current_pagecount), xy=(.05,.88), xycoords="axes fraction", fontsize=16)

def make_words_plot(axis):
    df.plot(y='wordcount',legend=None, color=pc_color, ax=axis,label="Raw Data")
    done_resample_df =  df.resample('w').max()
    done_resample_df['wordcount'].plot(figsize=(12,8), color='salmon',label="Rolling 7-day Max",ax=axis)
    slope = pd.Series(np.gradient(done_resample_df["wordcount"].values), done_resample_df.index, name='slope')
    plt.sca(axis)
    plt.plot(done_resample_df['wordcount'].diff(), color="forestgreen",label="Rolling 7-Day Change")
    plt.ylabel("Words",fontsize=14)
    plt.annotate("{:,d} Words".format(current_wordcount), xy=(.05,.88), xycoords="axes fraction", fontsize=16)

def make_format():
    sns.despine()
    plt.ylim(bottom=0)
    plt.xticks(fontsize=12)
    plt.yticks(fontsize=12)

fig, (ax1,ax2) = plt.subplots(ncols=1, nrows=2, sharex=True)
make_pages_plot(ax1)
make_format()
make_words_plot(ax2)
make_format()
plt.xlabel("Date",fontsize=14)
plt.legend(bbox_to_anchor=(1.2,1), loc="center",frameon=False,fontsize=14)

days = mdates.DayLocator()
months = mdates.MonthLocator()  # every month
years_fmt = mdates.DateFormatter('%b %Y')
ax2.xaxis.set_major_locator(months)
ax2.xaxis.set_major_formatter(years_fmt)
plt.minorticks_off()
plt.tight_layout()
plt.savefig('plots/page_with_delta')


from partigames.environment.zoo_env import parallel_env
from interaction_learning.core.util import  AgentPositionGenerator
import matplotlib.pyplot as plt
import numpy as np
import pickle
import torch

plt.style.use("seaborn-whitegrid")

num_eval = 100

agent_position_generator = AgentPositionGenerator(num_eval * 10)
agent_reward = ["x"]
max_steps = 1000
ghost_agents = 0
render = False
num_agents = 2

env = parallel_env(num_agents=num_agents, agent_position_generator=agent_position_generator, agent_reward=agent_reward,
                   max_steps=max_steps, ghost_agents=ghost_agents, render=render)

obs = env.reset()
p_obs = obs["player_0"]
p_obs[3] = 0.1
p_obs[4] = 0.9

with open("impact_learner/all_ego_and_impact_task.agent", "rb") as input_file:
    interaction_agent = pickle.load(input_file)

with open(f"impact_learner/all_ego_task.agent", "rb") as input_file:
    other_agent = pickle.load(input_file)

with open(f"impact_learner/joint_learner_mis_goals.agent", "rb") as input_file:
    joint_agent = pickle.load(input_file)

task = "ta"
impact_task = "ie0"

x, y = np.meshgrid(np.arange(0.0, 1.025, .025), np.arange(0.0, 1.025, .05))

# Koordinaten und Richtungen definieren
z = np.zeros_like(x)
u = np.zeros_like(x)
v = np.zeros_like(x)
# 0 Still 1 right 2 down 3 left 4 up
for a in range(x.shape[0]):
    for b in range(x.shape[1]):
        p_obs[0] = x[a][b]
        p_obs[1] = y[a][b]
        # print(f"x: {x[a][b]} || y: {y[a][b]}")
        with torch.no_grad():
            state = torch.FloatTensor(p_obs).unsqueeze(0).to("cpu")
            q_task = interaction_agent.task_models[task].get_q(state)
            dist_task = interaction_agent.task_models[task].get_dist(q_task).cpu().numpy().flatten()
            support = len(dist_task)
            entropy = interaction_agent.calc_entropy(dist_task)
            linear_entropy = 1 - np.power(support, entropy) / support
            u[a][b] = linear_entropy

fig, ax = plt.subplots()
c = ax.pcolormesh(x, y, u, cmap='hot')
ax.axis([0, 1, 0, 1])
ax.set_title("Task Entropy")
fig.colorbar(c, ax=ax)
plt.show()


import datetime as dt

class TokenData:

    """
    Class to save token data coming from an oauth request.
    It represents an oauth response. Contains access_token, refresh_token, and expiry date.
    """

    datetime_format = "%Y-%m-%d %H:%M:%S"

    def __init__(self, access_token, refresh_token, expiry_date):
        self.access_token = access_token
        self.refresh_token = refresh_token
        self.expiry_date = expiry_date

    def is_access_token_expired(self) -> bool:
        datetime_formatted = dt.datetime.strptime(
            self.expiry_date, self.datetime_format
        )
        return datetime_formatted <= dt.datetime.now()

    def is_valid(self) -> bool:
        return (
            bool(self.access_token)
            and bool(self.refresh_token)
            and bool(self.expiry_date)
        )


from flask import Flask, current_app
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate

import config

db = SQLAlchemy()
migrate = Migrate()

def create_app() :
    app = Flask(__name__)
    app.config.from_object(config)
    db.init_app(app)
    migrate.init_app(app, db)

    import views.test_view as test_view
    app.register_blueprint(test_view.bp)

    # /는 서버 주소를 의미 -> 127.0.0.1:5000/
    @app.route('/')
    def test_view() :
        return '123123'

    # 사람 - 이름, 나이, 주소
    # 1. 홍길동 20 대전
    # 2. 홍길동 20 대전

    p1 = Person(name='홍길동', age=30, address='대전')
    app.app_context().push()
    db.session.add(p1)
    db.session.commit()

    return app

class Person(db.Model) :
    no = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(30))
    age = db.Column(db.Integer)
    address = db.Column(db.String(50))


from collections import OrderedDict

def collect_devices(workbook):
    devices = []
    sheet = workbook.sheet_by_name('Sheet1')
    for row_id in range(1, sheet.nrows):
        if not sheet.cell(row_id, 0).value: continue #empty cell
        device = OrderedDict()
        device['name'] = sheet.cell(row_id, 0).value.lower()
        device['type'] = sheet.cell(row_id, 1).value.lower()

        actions = sheet.cell(row_id, 2).value.lower().split(',')
        device['action_0'] = " ".join([action.strip().split('/')[0] for action in actions])
        device['action_1'] = " ".join([action.strip().split('/')[1] for action in actions])
        devices.append(device)

    return devices

def collect_requests(workbook):
    requests = []
    sheet = workbook.sheet_by_name('Sheet1')
    for row_id in range(sheet.nrows):
        request = sheet.cell(row_id, 0).value.lower()
        requests.append(request)

    return requests


'''
Created on 29 Oct 2019

@author: oqb
'''
import numpy as np
import matplotlib.pyplot as plt
from aa import data, fetcher, js
from tzlocal import get_localzone
from datetime import datetime
from scipy.signal import savgol_filter
from scipy import interpolate

if __name__ == '__main__':
    #Set up archive
    tz = get_localzone()
    ArchiverBessy = 'archiver.bessy.de'

    jfetcher_Bessy = js.JsonFetcher(ArchiverBessy,80)
    # gather data on move to create time window
    UE112Gap = 'UE112ID7R:BasePmGap.A'
    UE112Phase = 'UE112ID7R:SBasePmGap.B'

    tuneH = 'TUNEZR:rdH'
    tuneV =  'TUNEZR:rdV'
    THzbox0 = 'THZH11FD6L:BOX0'

    Vtunetarget = 979.0
    Htunetarget = 882.0
    THztarget = 1

    toistart = tz.localize(datetime(2019, 10, 27, 8 , 25))
    toiend = tz.localize(datetime(2019, 10, 27, 8, 33))

    phasemove = jfetcher_Bessy.get_values(UE112Phase, toistart, toiend)

    #Have a quick look
    plt.plot(phasemove._timestamps, phasemove._values)
    plt.show()

    # set window

    #find _values array point wwhere the move starts and stops #can be wrapped up
    #find the stepsize of value array
    tmpphasediff = np.diff(phasemove._values[:,0])
    #create a True/False array of 'real steps' i.e. above 0.1 micron. Find the first true
    datastartindex = np.argmax(tmpphasediff>1e-4)+1
    #find the first false after the first true
    datastopindex = np.argmin(tmpphasediff[datastartindex:]>1e-4)+2
    #Local Timestamps for TuneH, Tune V and Box0
    phasemovestart = tz.localize(datetime.fromtimestamp((phasemove._timestamps[datastartindex]))) #start a second before
    phasemoveend = tz.localize(datetime.fromtimestamp((phasemove._timestamps[datastopindex]+1))) #finish a second after
    # gather more data (tune, Box0)
    tuneHdata =  jfetcher_Bessy.get_values(tuneH, phasemovestart, phasemoveend)
    tuneVdata = jfetcher_Bessy.get_values(tuneV, phasemovestart, phasemoveend)
    THzbox0data = jfetcher_Bessy.get_values(THzbox0, phasemovestart, phasemoveend)

    #gather data for detrending THz signal
    THzdetrendstart = tz.localize(datetime(2019, 10, 27, 8 , 14))
    THzdetrendend = tz.localize(datetime(2019, 10, 27, 8 , 24))
    THztrenddata = jfetcher_Bessy.get_values(THzbox0, THzdetrendstart, THzdetrendend)

    #create linear fit
    THztargetfit = np.polyfit(THztrenddata._timestamps, THztrenddata._values[:,0], 1)
    print(THztargetfit)
    THztarget = np.poly1d(THztargetfit)

    FMN = THzbox0data._values-THztarget(THzbox0data._timestamps)

    plt.figure()
    plt.plot(tuneHdata._timestamps, tuneHdata._values-np.mean(tuneHdata._values))
    plt.plot(tuneVdata._timestamps, tuneVdata._values-np.mean(tuneVdata._values))
    plt.plot(THzbox0data._timestamps, 500*(FMN))

    plt.show()
    # fit curves
    #fit gap curve also with time
    #(VTune determine main oscillation frequency and strip it out?) Worth looking at? Or comes out in the fit wash?
    #THz tune number remove linear fit from first to final point CAN ONLY BE DONE AFTER BOTH WAYS LOOKED AT OR LINEAR FIT FROM PREVIOUS MOTION FREE TIMES USED
    #HTune Smooth
    smTuneHData = savgol_filter(tuneHdata._values[:,0], 5, 2)
    #VTune Smooth
    smTuneVData = savgol_filter(tuneVdata._values[:,0], 51, 2)
    #THz Smooth
    smTHzData = savgol_filter(THzbox0data._values[:,0], 151, 2)
    tmp2 = savgol_filter(FMN, 151, 2)

    ###Here be fits
    xnew = np.linspace(phasemove._timestamps[datastartindex], (phasemove._timestamps[datastopindex]), 51)

    #Htune Fit
    #Htune = np.linspace(phasemove._timestamps[datastartindex], phasemove._timestamps[datastopindex], len(smTuneHData))
    tckHtune = interpolate.splrep(np.linspace(phasemove._timestamps[datastartindex], phasemove._timestamps[datastopindex], len(smTuneHData)),smTuneHData, s=0.0)
    Htunefit = interpolate.splev(xnew, tckHtune, der=0)

    plt.figure()
    plt.plot(tuneHdata._timestamps, tuneHdata._values,tuneHdata._timestamps, smTuneHData, xnew, Htunefit)
    #plt.plot(tuneHdata._timestamps, smTuneHData)

    plt.show()

    #Vtune Fit
    tckVtune = interpolate.splrep(np.linspace(phasemove._timestamps[datastartindex], phasemove._timestamps[datastopindex], len(smTuneVData)),smTuneVData, s=0.0)
    Vtunefit = interpolate.splev(xnew, tckVtune, der=0)

    plt.figure()
    plt.plot(tuneVdata._timestamps, tuneVdata._values,tuneVdata._timestamps, smTuneVData, xnew, Vtunefit)

    plt.show()

    #THz Fit
    tckTHz = interpolate.splrep(np.linspace(phasemove._timestamps[datastartindex], phasemove._timestamps[datastopindex], len(smTHzData)),smTHzData, s=0.0)
    THzfit = interpolate.splev(xnew, tckTHz, der=0)

    plt.figure()
    plt.plot(THzbox0data._timestamps, THzbox0data._values,THzbox0data._timestamps, smTHzData,xnew, THzfit)

    plt.show()

    #Gap Fit
    #TODO Needs Improving
    tckGap = interpolate.splrep(np.linspace(phasemove._timestamps[datastartindex], phasemove._timestamps[datastopindex], len(phasemove._values[datastartindex:datastopindex])),phasemove._values[datastartindex:datastopindex], s=0.0)
    Gapfit = interpolate.splev(xnew, tckGap, der=0)

    plt.figure()
    plt.plot(phasemove._timestamps[datastartindex:datastopindex], phasemove._values[datastartindex:datastopindex],xnew, Gapfit)

    plt.show()

    # create tables
    #Bring fitted lines together
    gapresponse = np.transpose(np.vstack((Gapfit, Htunefit-Htunetarget, Vtunefit-Vtunetarget, THzfit)))

    #Create inverse matrix
    responsematrix = np.transpose([[35.75,67.5, 600],[-157.3, -22.5, -22.75], [121,207.5, 190.5]])
    invresponse = np.linalg.inv(responsematrix)
    print (np.asmatrix(responsematrix))
    print(invresponse)

    print(np.dot(invresponse,np.transpose(-gapresponse[50,1:])))

    #create quadrupole array solution.
    quadoffsets = np.zeros(gapresponse.shape)
    quadoffsets[:,0] = gapresponse[:,0]
    quadoffsets[:,1:]=np.transpose(np.dot(invresponse,np.transpose(-gapresponse[:,1:])))

    #export tables in expected form..

    #make a quadrupole array solution based on existing points and linear interpolation
    # compare tables

    #point to stop for debug
    print (quadoffsets)


from models.player import Player
from controllers.grab import Input
from views.view import View
from views.home_menu_view import HomeMenuView
from utils.menus import Menu

class PlayerController:

    """
        Création de l'instance d'un joueur.
    """

    def __init__(self):
        self.player = None
        self.view = View()
        self.input = Input()
        self.menu = Menu()
        self.viewmenu = HomeMenuView(self.menu)

        self.display_information = {"first_name": "Le prénom du joueur :",
                                    "last_name": "Le nom :",
                                    "gender": "Le genre : 'H' si c'est un homme, 'F' si c'est une femme :",
                                    "elo": "les points ELO du joueur :",
                                    "birthday": "Veuillez saisir la date de naissance dans le format 01/01/1901",
                                    "check": "les informations sont-elles correctes ? \n 1 pour oui, 2 pour non :",
                                    "invalid_info": "Qu'elles informations ne sont pas valide ?",
                                    "info": "'1' le prénom, '2' le nom, '3' le genre, '4' l'anniversaire, '5' l'elo "
                                    }

    def add_new_player(self):

        """
            Méthode qui demande à l'utilisateur de rentrer les informations d'un joueur lors de sa création.
            Renvoie une instance de joueur.
        """

        self.view.show(self.display_information["first_name"])
        first_name = self.input.get_input_str()
        self.view.show(self.display_information["last_name"])
        last_name = self.input.get_input_str()
        self.view.show(self.display_information["gender"])
        gender = self.input.get_gender()
        self.view.show(self.display_information["elo"])
        elo = self.input.get_input_int()
        self.view.show(self.display_information["birthday"])
        birthday = self.input.get_input_date()

        self.player = Player(first_name=first_name,
                             last_name=last_name,
                             gender=gender,
                             elo=elo,
                             ranking_points=0,
                             birthday=str(birthday),
                             ID=Player.player_id
                             )
        Player.player_id += 1
        self.check_player_info()
        self.player.serialize()
        return self.player

    def add_database_player(self, player_id):

        """
            Méthode qui récupère l'ID d'un joueur, pour créer une instance de joueur à partir de la base de donnée.
            Cette méthode est utilisée pour ajouter un joueur à un tournoi.
            Renvoie une instance de joueur.

            Argument:
                player_id = L'id du joueur qui correspond à son doc_id de la base de donnée.
        """

        player = Player().add_player_from_database(player_id=player_id)
        return player

    def check_player_info(self):

        """
            Méthode qui affiche les attributs d'un joueur pour que l'utilisateur valide
            ou modifie si besoin.
        """

        self.view.check_info_player(ID=self.player.ID,
                                    first_name=self.player.first_name,
                                    last_name=self.player.last_name,
                                    gender=self.player.gender,
                                    birthday=self.player.birthday,
                                    elo=self.player.elo,
                                    )

        self.view.show(self.display_information["check"])
        valid_info = self.input.get_input_int()
        if valid_info == 1:
            pass
        if valid_info == 2:
            self.view.show(self.display_information["invalid_info"])
            self.view.show(self.display_information["info"])
            info = self.input.get_input_int()
            if info == 1:
                self.view.show(self.display_information["first_name"])
                self.player.first_name = self.input.get_input_str()
            elif info == 2:
                self.view.show(self.display_information["last_name"])
                self.player.last_name = self.input.get_input_str()
            elif info == 3:
                self.view.show(self.display_information["gender"])
                self.player.gender = self.input.get_gender()
            elif info == 4:
                self.view.show(self.display_information["birthday"])
                self.player.birthday = self.input.get_input_date()
            elif info == 5:
                self.view.show(self.display_information["elo"])
                self.player.elo = self.input.get_input_int()

    def display_players_from_database(self):

        """
            Méthode qui affiche la liste de tous les joueurs de la base de donnée.
        """

        for player in Player().deserialize_players_in_database():
            self.view.check_info_player(ID=player.ID,
                                        first_name=player.first_name,
                                        last_name=player.last_name,
                                        gender=player.gender,
                                        birthday=player.birthday,
                                        elo=player.elo,
                                        )

    def deserialize__players_from_tournament(self, player_list):

        """
            Méthode qui crée les instances de joueurs d'un tournoi à partir de la liste
            de joueurs serialisés du tournoi.
            Renvoie une liste d'instances de joueurs

            Argument:
                player_list = liste des joueurs serialisés d'un tournoi
        """

        all_player_instance = list()
        for elem in player_list:
            player = Player(ID=elem['ID'],
                            first_name=elem['prenom'],
                            last_name=elem['nom'],
                            gender=elem['genre'],
                            birthday=elem['anniversaire'],
                            elo=elem['ELO'],
                            ranking_points=elem['points de tournoi']
                            )
            all_player_instance.append(player)
        return all_player_instance


from flask import Flask, request, render_template, session, redirect
import common

app = Flask(__name__)

@app.route('/')
@app.route('/list')
def route_index():
    datatable = common.get_table_from_file('data.csv')
    return render_template('list.html', datatable= datatable)

@app.route('/story')
def route_story():
    return render_template('form.html')

@app.route('/save_story', methods=['POST'])
def route_save_story():
    table = common.get_table_from_file('data.csv')
    if request.method == 'POST':
        story_title = request.form['story_title']
        user_story = request.form['user_story']
        acceptance_criteria = request.form['acceptance_criteria']
        business_value = str(request.form['business_value']) + ' points'
        estimation = str(request.form['estimation']) + ' h'
        new_id = str(int(table[-1][0]) + 1)
        new_story_list = [new_id, story_title, user_story, acceptance_criteria, business_value, estimation, 'planning']
        table.append(new_story_list)
        common.write_table_to_file('data.csv', table)
    return redirect('/')

@app.route('/story/<int:story_id>')
def route_update_story(story_id):
    table = common.get_table_from_file('data.csv')
    for line in table:
        if line[0] == str(story_id):
            story_title = line[1]
            user_story = line[2]
            acceptance_criteria = line[3]
            business_value = line[4].split()
            business_value = int(business_value[0])
            estimation = line[5].split()
            estimation = float(estimation[0])
            status = line[6]

            return render_template('update.html', story_id= story_id, story_title= story_title, user_story= user_story,
                                   acceptance_criteria= acceptance_criteria, business_value= business_value,
                                   estimation= estimation, status= status)

@app.route('/save_updated_story/<story_id>', methods=['POST'])
def route_save_updated_story(story_id):
    table = common.get_table_from_file('data.csv')
    if request.method == 'POST':
        story_title = request.form['story_title']
        user_story = request.form['user_story']
        acceptance_criteria = request.form['acceptance_criteria']
        business_value = str(request.form['business_value']) + ' points'
        estimation = str(request.form['estimation']) + ' h'
        status = request.form['status']
        for line in table:
            if line[0] == str(story_id):
                line.clear()
                updated_story_list = [story_id, story_title, user_story, acceptance_criteria, business_value, estimation, status]
                for i in updated_story_list:
                    line.append(i)
                common.write_table_to_file('data.csv', table)
                return redirect('/')

if __name__ == '__main__':
    app.secret_key = 'super_secret_key'
    app.run(debug= True, port= 5000)


# from crypt import methods
from multiprocessing import dummy
import sqlite3
from flask import *
import os
import sqlite3
from numpy import recfromcsv
import pandas as pd
from re import search
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import tensorflow as tf

from sqlalchemy import false

app = Flask(__name__)
app.static_folder = 'static'

@app.route('/')
def home():
    return render_template('index.html')

@app.route('/index.html')
def index():
    return render_template('index.html')

@app.route('/thirtyGames', methods = ['POST'])
def thirtyGames():
    search = request.get_json()
    df = pd.read_json(r'static\json_files\full_json.json').dropna().drop_duplicates()

    first_run = True
    for category in search:
        # print(category)
        for term in search[f'{category}']:
            term = term.strip()
            search_result = df.loc[df[category].str.contains(f"{term}", case=False)]
            if first_run:
                complete_df = search_result
                first_run = False
            else:
                complete_df = pd.concat([complete_df,search_result]).drop_duplicates()
            # print(term)

    complete_df = complete_df.sort_values(by='userscore', ascending=False).head(100)

    #method 1
    data = complete_df.sample(n=30)['title'].to_list()

    return jsonify(data)

@app.route('/videogame_buddy.html')
def vgbuddy():
    return render_template('videogame_buddy.html')

@app.route('/browse_games.html')
def browse():
    return render_template('browse_games.html')

@app.route('/results.html')
def results():
    return render_template('results.html')

@app.route('/finalresults.html')
def finalresults():
    return render_template('finalresults.html')

@app.route('/data.html')
def data():
    return render_template('data.html')

@app.route('/trainAI', methods = ['POST'])
def train():
    input = request.get_json()

    df = pd.read_json(r'static\json_files\full_json.json').dropna().drop_duplicates(subset='title')
    df.drop('summary',axis=1,inplace=True)

    dummy_master = pd.get_dummies(df)

    train_df = pd.DataFrame()
    search_result = pd.DataFrame()

    for title in input['name']:

        try:
            search_result = dummy_master.loc[dummy_master[f'title_{title}'] == 1]
        except:
            print(f"Couldn't find {title}")

        train_df = pd.concat([search_result,train_df])

    # print(len(train_df))
    # print(len(input['status']))

    train_df['like'] = input['status']

    # print(train_df.head(5))

    train_df.drop(['Unnamed: 0'], axis=1,inplace=True)

    # dummy_df = pd.get_dummies(train_df)
    the_input_dim = train_df.shape[1] - 1

    X = train_df.drop('like',axis=1)
    y = train_df['like']

    dummy_master.drop('Unnamed: 0',axis=1,inplace=True)

    # print(f"X shape: {X.shape}")
    # print(f"dummy_master shape: {dummy_master.shape}")

    X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42, stratify=y)

    scaler = StandardScaler()
    X_scaler = scaler.fit(X_train)
    X_train_scaled = X_scaler.transform(X_train)
    X_test_scaled = X_scaler.transform(X_test)

# stop = False
# while stop == False:
    nn = tf.keras.models.Sequential()
    nn.add(tf.keras.layers.Dense(units=60, activation='relu', input_dim=the_input_dim))
    nn.add(tf.keras.layers.Dense(units=30, activation='relu'))
    nn.add(tf.keras.layers.Dense(units=1, activation='sigmoid'))
    nn.summary()

    nn.compile(loss="binary_crossentropy", optimizer="adam", metrics=["accuracy"])

    # fit_model = nn.fit(X_train_scaled, y_train, epochs=10)

    model_loss, model_accuracy = nn.evaluate(X_test_scaled,y_test,verbose=2)
    print(f"Loss: {model_loss}, Accuracy: {model_accuracy}")

    dummy_master['like'] = nn.predict(x=dummy_master)

    predict_result = dummy_master.loc[dummy_master['like'] > .3].sample(n=5)

    for column in predict_result.columns:
        empty = True
        for value in predict_result[column]:
            if(value == 1):
                empty=False
        if(empty == True):
            predict_result.drop(column,axis=1,inplace=True)

    recommended_games = predict_result.columns[0:5].to_list()

    for x in range(5):
        recommended_games[x] = recommended_games[x].split('_',1)[1]
        print(recommended_games[x])

    # print(predict_result)
    # print(predict_result.shape)
    return jsonify(recommended_games)

@app.route('/gameSearch', methods = ['POST'])
def search():
    input = request.get_json()
    df = pd.read_json(r'static\json_files\full_json.json').dropna().drop_duplicates(subset='title')

    search_result = df.loc[df['title'].str.contains(f"{input}", case=False)]

    return (search_result.to_html())

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


# python-telegram-bot==13.15
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, ConversationHandler
# /start, /komanda
from googletrans import Translator

tarjimon = Translator()

def tarjima(matn, b_til, i_til):
    t_matn = tarjimon.translate(matn, src=b_til, dest=i_til)
    return t_matn

TOKEN = '6044418045:AAGsrn5tB8ZHLw2I8bhWl0uNzzVEhmQqRPo'

def start(update, context):
    ism = update.message.from_user.first_name
    update.message.reply_html(f'<b>{ism}</b>, botimizga xush kelibsiz')
    return 1

def salom(update, context):
    update.message.reply_text('Assalomu aleykum')
    return 2

def translate(update, context):
    matn = update.message.text
    if matn == '/start':
        start(update, context)
        return 1
    else:
        r = matn.split(',')  # uz,ru,matn
        t_matn = tarjima(r[2], r[0], r[1])
        update.message.reply_text(t_matn.text)
        return 2

def main():
    updater = Updater(TOKEN, use_context=True)

    disp = updater.dispatcher

    conv_hand = ConversationHandler(
        entry_points=[CommandHandler('start', start)],
        states={
            1: [
                CommandHandler('salom', salom)
            ],
            2:[
                MessageHandler(Filters.text, translate)
            ]
        },
        fallbacks=[CommandHandler('start', start)]
    )
    disp.add_handler(conv_hand)
    # disp.add_handler(CommandHandler('start', start))
    # disp.add_handler(CommandHandler('salom', salom))
    # disp.add_handler(MessageHandler(Filters.text, translate))

    updater.start_polling()
    updater.idle()

main()


from typing import List, Dict

N: int = int(input())
A: List[int] = list(map(int, input().split()))
B: List[int] = list(map(int, input().split()))

C: List[int] = A.copy()
for i in range(N):
    if C[i] - B[i] > 0:
        C[i] -= B[i]
    else:
        C[i+1] = C[i+1] - (B[i] - C[i]) if C[i+1] - (B[i] - C[i]) > 0 else 0
        C[i] = 0

beaten_monster: int = sum(A) - sum(C)
print(beaten_monster)


import collections
import unittest

class Solution(object):
    def findLHS(self,nums):
        count = collections.Counter(nums)
        result = 0
        for x in count:
            if x + 1 in count:
                result = max(result,count[x] + count[x + 1])
        return result

class Test(unittest.TestCase):
    def test(self):
        self._test([1,1,1,2,3], 4)

    def _test(self, li, expected):
        actual = Solution().findLHS(li)
        self.assertEqual(actual, expected)

if __name__ == '__main__':
    unittest.main()


import logging
import sys

logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))
logger = logging.getLogger(__name__)

import os
import streamlit as st
from streamlit_chat import message

from openai.error import OpenAIError

from gpt_index import (
    GPTListIndex,
    GPTSimpleVectorIndex
)

from langchain.agents import Tool, initialize_agent
from langchain.llms import OpenAI
# from langchain import OpenAI
from langchain.chains.conversation.memory import ConversationBufferMemory

st.set_page_config(page_title="YogaTherapyAdvisor", page_icon="ðŸ§˜", layout="wide")
st.header("ðŸ§˜Yoga Therapy Advisor")

if 'generated' not in st.session_state:
    st.session_state['generated'] = []

if 'past' not in st.session_state:
    st.session_state['past'] = []

def set_openai_api_key():
    st.session_state["OPENAI_API_KEY"] = os.environ["OPENAI_API_KEY"]

def gen_sidebar():
    with st.sidebar:
        st.markdown("# About")
        st.header(
            "ðŸ™‹Ask questions about what ails you "
            "and get instant answers from "
            "trusted sources with citations"
        )
        st.markdown(
            "This tool is a work in progress. "
            "You can contribute to the project on [GitHub](https://github.com/aurotripathy/yoga_therapy_advisor) "
            "with your feedback and suggestionsðŸ’¡"
        )
        st.markdown("---")
        st.markdown(
            "## How to use\n"
            "1. Enter your [OpenAI API key](https://platform.openai.com/account/api-keys) belowðŸ”‘\n"
            "2. Ask a question what ails youðŸ’¬\n"
        )

        api_key_input = True  # Forced
        set_openai_api_key()

        st.markdown("---")
        st.markdown("Made by [auro tripathy](https://www.linkedin.com/in/aurotripathy/)")

def get_text():
    c1, c2, c3 = st.columns([1, 4, 4])  # This moves the input box to the right
    with c3:
        input_text = st.text_input("You: ", placeholder="Ask me a question about yoga therapy and health.", key="input")
    return input_text

#main
gen_sidebar()

logger.info(f'Loading the indexes...')
vector_index_file = "../vector-indexes/yoga_qa.json"
index = GPTSimpleVectorIndex.load_from_disk(vector_index_file)
logger.info(f'Done loading the indexes.')

st.session_state["api_key_configured"] = True

query = get_text()

if query:
    st.session_state["submit"] = True
    # Output Columns
    answer_col, sources_col = st.columns(2)

    tools = [
        Tool(
            name = "GPT Index",
            func=lambda q: str(index.query(q)),
            description="Useful for when you want to answer questions about yoga. As an assistant, you must always use this tool first, no exceptions.",
            return_direct=True
        ),
    ]

    memory = ConversationBufferMemory(memory_key="chat_history")
    llm = OpenAI(temperature=0)

    agent = initialize_agent(tools, llm, agent="conversational-react-description",
                             memory=memory,
                             verbose=True)
    result = agent.run(query)
    print(f'Result: {result}')

    st.session_state.past.append(query)
    st.session_state.generated.append(result)

if st.session_state['generated']:

    for i in range(len(st.session_state['generated'])-1, -1, -1):
        message(st.session_state["generated"][i], key=str(i))
        message(st.session_state['past'][i], is_user=True, key=str(i) + '_user')


__author__ = 'aymgal'

import numpy as np
import numpy.testing as npt
import pytest
import unittest
import copy

from slitronomy.Optimization.model_operators import ModelOperators
from slitronomy.Lensing.lensing_operator import LensingOperator
from slitronomy.Util import util

from lenstronomy.Data.imaging_data import ImageData
from lenstronomy.LensModel.lens_model import LensModel
from lenstronomy.LightModel.light_model import LightModel
from lenstronomy.Data.psf import PSF
from lenstronomy.ImSim.Numerics.numerics_subframe import NumericsSubFrame
import lenstronomy.Util.util as l_util

np.random.seed(18)

class TestModelOperators(object):
    """
    tests the Lensing Operator classes
    """
    def setup(self):
        self.num_pix = 49  # cutout pixel size
        self.subgrid_res_source = 2
        self.num_pix_source = self.num_pix * self.subgrid_res_source

        delta_pix = 0.24
        _, _, ra_at_xy_0, dec_at_xy_0, _, _, Mpix2coord, _ \
            = l_util.make_grid_with_coordtransform(numPix=self.num_pix, deltapix=delta_pix, subgrid_res=1,
                                                         inverse=False, left_lower=False)

        self.image_data = np.random.rand(self.num_pix, self.num_pix)
        kwargs_data = {
            'ra_at_xy_0': ra_at_xy_0, 'dec_at_xy_0': dec_at_xy_0,
            'transform_pix2angle': Mpix2coord,
            'image_data': self.image_data,
        }
        data = ImageData(**kwargs_data)

        lens_model = LensModel(['SPEP'])
        kwargs_lens = [{'theta_E': 1, 'gamma': 2, 'center_x': 0, 'center_y': 0, 'e1': -0.05, 'e2': 0.05}]

        # PSF
        kernel_pixel = np.zeros((self.num_pix, self.num_pix))
        kernel_pixel[int(self.num_pix/2), int(self.num_pix/2)] = 1  # just a dirac here
        kwargs_psf = {'psf_type': 'PIXEL', 'kernel_point_source': kernel_pixel}
        psf = PSF(**kwargs_psf)

        # wavelets scales for lens and source
        self.n_scales_source = 4
        self.n_scales_lens = 3

        # list of source light profiles
        source_model = LightModel(['SLIT_STARLETS'])
        self.kwargs_source = [{'n_scales': self.n_scales_source}]

        # list of lens light profiles
        lens_light_model = LightModel(['SLIT_STARLETS'])
        self.kwargs_lens_light = [{'n_scales': self.n_scales_lens}]

        # define some mask
        likelihood_mask = np.ones((self.num_pix, self.num_pix))

        # get grid classes
        self.numerics = NumericsSubFrame(data, psf)
        image_grid_class = self.numerics.grid_class
        source_numerics = NumericsSubFrame(data, psf, supersampling_factor=self.subgrid_res_source)
        source_grid_class = source_numerics.grid_class

        # get a lensing operator
        self.lensing_op = LensingOperator(lens_model, image_grid_class, source_grid_class, self.num_pix)
        self.lensing_op.update_mapping(kwargs_lens)

        self.model_op = ModelOperators(data, self.lensing_op, self.numerics)
        self.model_op._set_likelihood_mask(likelihood_mask)
        self.model_op.add_source_light(source_model)
        self.model_op.add_lens_light(lens_light_model)
        self.model_op_nolens = ModelOperators(data, self.lensing_op, self.numerics)
        self.model_op_nolens._set_likelihood_mask(likelihood_mask)
        self.model_op_nolens.add_source_light(source_model)

        # define some test images in direct space
        self.X_s = np.random.rand(self.num_pix_source, self.num_pix_source)  # source light
        self.X_l = np.random.rand(self.num_pix, self.num_pix)  # lens light

        # define some test images in wavelets space
        self.alpha_s = np.random.rand(self.n_scales_source, self.num_pix_source, self.num_pix_source)  # source light
        self.alpha_l = np.random.rand(self.n_scales_lens, self.num_pix, self.num_pix)  # lens light

    def test_set_wavelet_scales(self):
        self.model_op.set_source_wavelet_scales(self.n_scales_source)
        Phi_T_s_X = self.model_op.Phi_T_s(self.X_s)
        self.model_op.set_lens_wavelet_scales(self.n_scales_lens)
        Phi_T_l_X = self.model_op.Phi_T_l(self.X_l)
        # test that transformed image has the right shape in terms of number of scales
        assert Phi_T_s_X.shape[0] == self.n_scales_source
        assert Phi_T_l_X.shape[0] == self.n_scales_lens

    def test_subtract_from_data_and_reset(self):
        image_to_subtract = np.eye(self.num_pix, self.num_pix)
        self.model_op.subtract_from_data(image_to_subtract)
        npt.assert_equal(self.model_op.Y_tilde, self.model_op.Y_eff)
        npt.assert_equal(self.model_op.Y_tilde, self.image_data)
        npt.assert_equal(self.model_op.Y_p, self.image_data - image_to_subtract)
        self.model_op.reset_partial_data()
        npt.assert_equal(self.model_op.Y_tilde, self.image_data)
        npt.assert_equal(self.model_op.Y_p, self.image_data)

    def test_spectral_norm_source(self):
        self.model_op.set_source_wavelet_scales(self.n_scales_source)
        npt.assert_almost_equal(self.model_op.spectral_norm_source, 0.97, decimal=2)

    def test_spectral_norm_lens(self):
        self.model_op.set_lens_wavelet_scales(self.n_scales_lens)
        npt.assert_almost_equal(self.model_op.spectral_norm_lens, 0.99, decimal=2)

    def test_data_terms(self):
        npt.assert_equal(self.model_op.Y_tilde, self.image_data)
        npt.assert_equal(self.model_op.Y_p, self.image_data)

    def test_convolution(self):
        H_X_s = self.model_op.H(self.X_s)
        npt.assert_equal(H_X_s, self.numerics.convolution_class.convolution2d(self.X_s))
        H_T_X_s = self.model_op.H_T(self.X_s)
        conv_transpose = self.numerics.convolution_class.copy_transpose()
        npt.assert_equal(H_T_X_s, conv_transpose.convolution2d(self.X_s))

    def test_lensing(self):
        F_X_s = self.model_op.F(self.X_s)
        npt.assert_equal(F_X_s, self.lensing_op.source2image_2d(self.X_s))
        F_T_X_l = self.model_op.F_T(self.X_l)
        npt.assert_equal(F_T_X_l, self.lensing_op.image2source_2d(self.X_l))

    def test_wavelet_transform(self):
        # TODO : do more accurate tests here
        self.model_op.set_source_wavelet_scales(self.n_scales_source)
        self.model_op.set_lens_wavelet_scales(self.n_scales_lens)
        Phi_alpha_s = self.model_op.Phi_s(self.alpha_s)
        Phi_alpha_l = self.model_op.Phi_l(self.alpha_l)
        assert Phi_alpha_s.shape == (self.num_pix*self.subgrid_res_source, self.num_pix*self.subgrid_res_source)
        assert Phi_alpha_l.shape == (self.num_pix, self.num_pix)
        Phi_T_X_s = self.model_op.Phi_T_s(self.X_s)
        Phi_T_X_l = self.model_op.Phi_T_l(self.X_l)
        assert Phi_T_X_s.shape == (self.n_scales_source, self.num_pix*self.subgrid_res_source, self.num_pix*self.subgrid_res_source)
        assert Phi_T_X_l.shape == (self.n_scales_lens, self.num_pix, self.num_pix)

class TestRaise(unittest.TestCase):

    def __init__(self, *args, **kwargs):
        super(TestRaise, self).__init__(*args, **kwargs)
        self.num_pix = 10
        _, _, ra_at_xy_0, dec_at_xy_0, _, _, Mpix2coord, _ \
            = l_util.make_grid_with_coordtransform(numPix=self.num_pix, deltapix=0.5, subgrid_res=1,
                                                   inverse=False, left_lower=False)
        kwargs_data_nonsquare = {
            'ra_at_xy_0': ra_at_xy_0, 'dec_at_xy_0': dec_at_xy_0,
            'transform_pix2angle': Mpix2coord,
            'image_data': np.zeros((self.num_pix, self.num_pix+10)),  # non-square image
        }
        kwargs_data = {
            'ra_at_xy_0': ra_at_xy_0, 'dec_at_xy_0': dec_at_xy_0,
            'transform_pix2angle': Mpix2coord,
            'image_data': np.zeros((self.num_pix, self.num_pix)),  # non-square image
        }
        self.data_nonsquare = ImageData(**kwargs_data_nonsquare)
        self.data = ImageData(**kwargs_data)
        psf = PSF('NONE')
        self.numerics = NumericsSubFrame(self.data, psf)
        lens_model = LensModel(['SPEP'])
        self.source_model_class = LightModel(['SLIT_STARLETS'])
        self.lens_light_model_class = LightModel(['SLIT_STARLETS'])
        # get grid classes
        image_grid_class = self.numerics.grid_class
        source_numerics = NumericsSubFrame(self.data, psf, supersampling_factor=1)
        source_grid_class = source_numerics.grid_class
        self.lensing_op = LensingOperator(lens_model, image_grid_class, source_grid_class, self.num_pix)
        self.model_op = ModelOperators(self.data, self.lensing_op, self.numerics)
        self.model_op.add_lens_light(self.lens_light_model_class)
        self.model_op_nolens = ModelOperators(self.data, self.lensing_op, self.numerics)

    def test_raise(self):
        with self.assertRaises(ValueError):
            # no wavelet scales was set, so should raise errors
            X_s = np.random.rand(self.num_pix, self.num_pix)
            Phi_T_s_X = self.model_op.Phi_T_s(X_s)
        with self.assertRaises(ValueError):
            # no wavelet scales was set, so should raise errors
            X_l = np.random.rand(self.num_pix, self.num_pix)
            Phi_T_l_X = self.model_op.Phi_T_l(X_l)
        with self.assertRaises(ValueError):
            # no wavelet scales was set, so should raise errors
            alpha_s = np.random.rand(3, self.num_pix, self.num_pix)
            Phi_s_alpha = self.model_op.Phi_s(alpha_s)
        with self.assertRaises(ValueError):
            # no wavelet scales was set, so should raise errors
            alpha_l = np.random.rand(3, self.num_pix, self.num_pix)
            Phi_l_alpha = self.model_op.Phi_l(alpha_l)
        with self.assertRaises(ValueError):
            # no lens profile was set, so should raise an error
            X_l = np.random.rand(self.num_pix, self.num_pix)
            Phi_T_l_X = self.model_op_nolens.Phi_T_l(X_l)
        with self.assertRaises(ValueError):
            # no lens profile was set, so should raise an error
            alpha_l = np.random.rand(3, self.num_pix, self.num_pix)
            Phi_l_alpha = self.model_op_nolens.Phi_l(alpha_l)
        with self.assertRaises(ValueError):
            # ModelOperators init should raise an error with non square data image
            model_op_error = ModelOperators(self.data_nonsquare, self.lensing_op, self.numerics)

if __name__ == '__main__':
    pytest.main()


# Import required libraries
import numpy as np
from matplotlib import pyplot as plt
from scipy.sparse import spdiags, csr_matrix, linalg, lil_matrix, diags, block_diag

# Read source, target and mask for a given id
def Read(id, path = ""):
    source = plt.imread(path + "source_" + id + ".jpg")
    info = np.iinfo(source.dtype) # get information about the image type (min max values)
    source = source.astype(np.float32) / info.max # normalize the image into range 0 and 1
    target = plt.imread(path + "target_" + id + ".jpg")
    info = np.iinfo(target.dtype) # get information about the image type (min max values)
    target = target.astype(np.float32) / info.max # normalize the image into range 0 and 1
    mask   = plt.imread(path + "mask_" + id + ".jpg")
    info = np.iinfo(mask.dtype) # get information about the image type (min max values)
    mask = mask.astype(np.float32) / info.max # normalize the image into range 0 and 1

    return source, mask, target

# Adjust parameters, source and mask for negative offsets or out of bounds of offsets
def AlignImages(mask, source, target, offset):
    sourceHeight, sourceWidth, _ = source.shape
    targetHeight, targetWidth, _ = target.shape
    xOffset, yOffset = offset

    if (xOffset < 0):
        mask    = mask[abs(xOffset):, :]
        source  = source[abs(xOffset):, :]
        sourceHeight -= abs(xOffset)
        xOffset = 0
    if (yOffset < 0):
        mask    = mask[:, abs(yOffset):]
        source  = source[:, abs(yOffset):]
        sourceWidth -= abs(yOffset)
        yOffset = 0
    # Source image outside target image after applying offset
    if (targetHeight < (sourceHeight + xOffset)):
        sourceHeight = targetHeight - xOffset
        mask    = mask[:sourceHeight, :]
        source  = source[:sourceHeight, :]
    if (targetWidth < (sourceWidth + yOffset)):
        sourceWidth = targetWidth - yOffset
        mask    = mask[:, :sourceWidth]
        source  = source[:, :sourceWidth]

    maskLocal = np.zeros_like(target)
    maskLocal[xOffset:xOffset + sourceHeight, yOffset:yOffset + sourceWidth] = mask
    sourceLocal = np.zeros_like(target)
    sourceLocal[xOffset:xOffset + sourceHeight, yOffset:yOffset + sourceWidth] = source

    return sourceLocal, maskLocal

# Pyramid Blend
def PyramidBlend(source, mask, target):

    return source * mask + target * (1 - mask)

# Poisson Blend

def PoissonBlend(source, mask, target, isMix):
    # Get dimensions
    height, width, channel = target.shape
    num_pixels = height * width

    # Compute matrix A and vector b

    # Loop through the mask to find A. We will also precompute its content since traversing it is slow due to being linked list
    A_data, A_row, A_col = [], [], []
    for y in range(height):
        for x in range(width):
            i = y * width + x
            if (mask[y, x] == 0).any():
                A_data.append(1)
                A_row.append(i)
                A_col.append(i)
            else:
                A_data.append(4)
                A_row.append(i)
                A_col.append(i)
                if x > 0:               #A[i, i - 1] = -1
                    A_data.append(-1)
                    A_row.append(i)
                    A_col.append(i - 1)
                if x < width - 1:       #A[i, i + 1] = -1
                    A_data.append(-1)
                    A_row.append(i)
                    A_col.append(i + 1)
                if y > 0:               #A[i, i - width] = -1
                    A_data.append(-1)
                    A_row.append(i)
                    A_col.append(i - width)
                if y < height - 1:      #A[i, i + width] = -1
                    A_data.append(-1)
                    A_row.append(i)
                    A_col.append(i + width)

    # Construct sparse matrix A
    A = csr_matrix((A_data, (A_row, A_col)), shape=(num_pixels, num_pixels))

    # Loop through each channel to find b respective to the channel
    blend = np.zeros(target.shape)
    for c in range(channel):
        b = np.zeros((num_pixels))
        for y in range(height):
            for x in range(width):
                i = y * width + x

                if (mask[y,  x] == 0).any():
                    b[i] = target[y, x, c]
                else:
                    # use derived equation to fill in b. also need to check bounds to avoid errors
                    b[i] = 4 * source[y, x, c]
                    if x > 0:
                        b[i] -= source[y, x - 1, c]
                    if y > 0:
                        b[i] -= source[y - 1, x, c]
                    if x < width - 1:
                        b[i] -= source[y, x + 1, c]
                    if y < height - 1:
                        b[i] -= source[y + 1, x, c]

        # Solve Ax = b
        x = linalg.spsolve(A, b)

        # Reshape x to target size
        blend[:, :, c] = np.reshape(x, target[:, :, c].shape)

    return np.clip(blend, 0, 1)

if __name__ == '__main__':
    # Setting up the input output paths
    inputDir = '../Images/'
    outputDir = '../Results/'

    # False for source gradient, true for mixing gradients
    isMix = False

    # Source offsets in target
    offsets = [[210, 10], [10, 28], [140, 80], [-40, 90], [60, 100], [20, 20], [-28, 88]]

    # main area to specify files and display blended image
    for index in range(len(offsets)):
        # Read data and clean mask
        source, maskOriginal, target = Read(str(index+1).zfill(2), inputDir)

        # Cleaning up the mask
        mask = np.ones_like(maskOriginal)
        mask[maskOriginal < 0.5] = 0

        # Align the source and mask using the provided offest
        source, mask = AlignImages(mask, source, target, offsets[index])

        ### The main part of the code ###

        # Implement the PoissonBlend function

        poissonOutput = PoissonBlend(source, mask, target, isMix)

        # Writing the result

        if not isMix:
            plt.imsave("{}poisson_{}.jpg".format(outputDir, str(index+1).zfill(2)), poissonOutput)
        else:
            plt.imsave("{}poisson_{}_Mixing.jpg".format(outputDir, str(index+1).zfill(2)), poissonOutput)


import random
import numpy as np
import matplotlib
from matplotlib import pyplot as plt
import seaborn as sns
from collections import deque
import networkx as nx
from networkx.algorithms.flow import shortest_augmenting_path

def create_random_graph(n, p):
    G = []
    skipDict = {}
    for i in range(n):
        adjList = []
        skipDict[i] = []
        currentVerticesLen = len(G)
        for m in range(n):
            if i == m:
                continue
            else:
                if m < currentVerticesLen and i in G[m]:
                    adjList.append(m)
                else:
                    if skipDict.get(m) is None or (skipDict.get(m) is not None and i not in skipDict[m]):
                        randVal = np.random.uniform(0.0, 1.0, 1)
                        if randVal <= p:
                            adjList.append(m)
                        else:
                            skipDict[i].append(m)
        G.append(adjList)
    return G

def search(G, visited, vertIndex):
    visited[vertIndex] = 1
    neighbors = G[vertIndex]
    for nIndex in neighbors:
        if visited[nIndex] == 0:
            search(G, visited, nIndex)

def find_components(G):
    numConnectedComponents = 0
    n = len(G)
    visited = [0 for i in range(n)]
    for vertIndex in range(n):
        if visited[vertIndex] == 0:
            numConnectedComponents += 1
            search(G, visited, vertIndex)

    return numConnectedComponents

def simulate_connectivity(n, p, nTrials):
    probabilityMap = {}
    for pi in range(nTrials):
        nTotalComps = 0
        nConnectedGraphs = 0
        for i in range(nTrials):
            G = create_random_graph(n, p)
            nComps = find_components(G)
            nTotalComps += nComps
            if nComps == 1:
                nConnectedGraphs += 1
        print(f'Parameter p: [{p}]')
        averageConnComps = nTotalComps/nTrials
        empProbability = nConnectedGraphs/nTrials
        print(f'Average number of CC: {averageConnComps}')
        print(f'Connectivity: {empProbability}')
        probabilityMap[pi] = [averageConnComps, empProbability]
        p += 1.0e-3
    return probabilityMap

def main():

    Graph = create_random_graph(50, 0.05)
    numComponents = find_components(Graph)
    print(f'Number of connected components for n=50, p=0.5: {numComponents}')

    n = 100
    p = 0.001
    nTrials = 100
    probabilityMap = simulate_connectivity(n, p, nTrials)
    x_values = [0.001+i*0.001 for i in range(nTrials)]
    y1_values = [probabilityMap[i][0] for i in range(nTrials)]
    y2_values = [probabilityMap[i][1] for i in range(nTrials)]

    plt.ylabel('Average connected components')
    plt.xlabel('probability')
    plt.title(f'Average # of connected components vs Probability')
    plt.plot(x_values, y1_values)
    plt.show()

    plt.ylabel('Empirical probability')
    plt.xlabel('probability')
    plt.title(f'Empirical probability vs Probability')
    plt.plot(x_values, y2_values)
    plt.show()

if __name__ == '__main__': main()


import numpy

from pandas import DataFrame
from pandas import concat

from sklearn.preprocessing import MinMaxScaler

def extract_file_content_binary_float64(filename, shape, offset=0):
    """
    extract the content of a binary file which contains 64-bit float data into a 2-d array
    :param filename: the name of the file that needs to be extracted
    :param shape: the shape of the 2-d array
    :param offset: the offset of the file from where the content is read
    :return: a 2-d array contains the file content
    """
    dt = numpy.dtype(numpy.float64).newbyteorder('>')
    with open(filename, 'rb') as bytestream:
        if offset != 0:
            bytestream.seek(offset)
        buf = bytestream.read(shape[0] * shape[1] * 8)
        data = numpy.frombuffer(buf, dtype=dt)
        data = data.reshape(shape)
        return data

def extract_file_content_float64(filename, shape):
    """
    extract the content of a csv file which contains 64-bit float data into a 2-d array
    :param filename: the name of the file that needs to be extracted
    :param shape: the shape of the 2-d array
    :return: a 2-d array contains the file content
    """
    dt = numpy.dtype(numpy.float64).newbyteorder('>')
    data = numpy.ndarray(shape=(0, 0), dtype=dt)
    with open(filename, 'r') as file_stream:
        for line in file_stream:
            data = numpy.append(data, numpy.array(line.split(',')).astype(dt))
    data = data.reshape(shape)
    return data

def convert2csv_float64(src_filename, dest_filename, shape):
    """
    convert a binary file which contains 64-bit float data into a csv file
    :param src_filename: the name of the file to be converted
    :param dest_filename: the name of the file to be generated
    :param shape: the shape of the data set
    :return:
    """
    data = extract_file_content_binary_float64(src_filename, shape)

    with open(dest_filename, 'w') as file_stream:
        for i in range(0, shape[0]):
            file_stream.write(",".join(data[i, ...].astype('str')) + "\n")

def write_csv_file(file_name, data):
    with open(file_name, 'w') as file_stream:
        for row in data:
            file_stream.write(','.join(row.astype(numpy.str)) + '\n')

def modify_x(train_x, num):
    x = train_x.copy()
    for i in range(num - 1):
        data = numpy.roll(train_x, -(i + 1), axis=0)
        x = numpy.concatenate((x, data), axis=1)
    return x.reshape(train_x.shape[0], num, train_x.shape[1])

def time_series_to_supervised(data_frame, lag=1):
    """
    frame a sequence as a supervised learning problem
    :param data_frame:
    :param lag:
    :return:
    """
    df = DataFrame(data_frame)
    columns = [df.shift(i) for i in range(1, lag + 1)]
    columns.append(df)
    df = concat(columns, axis=1)
    df.fillna(0, inplace=True)
    return df

def difference(data_set, interval=1):
    """
    create a difference series
    :param data_set:
    :param interval:
    :return:
    """
    diff = list()
    for i in range(interval, len(data_set)):
        value = data_set[i] - data_set[i - interval]
        diff.append(value)
    return numpy.array(diff).reshape(data_set.shape[0] - interval, -1)

def inverse_difference(history, y_hat, interval=1):
    """
    invert difference value
    :param history:
    :param y_hat:
    :param interval:
    :return:
    """
    return y_hat + history[-interval]

def gen_scaler(min_, max_):
    scaler = MinMaxScaler(feature_range=(-1, 1))
    scaler.fit(numpy.concatenate((numpy.ones((1, 2)) * min_, numpy.ones((1, 2)) * max_), axis=0))
    return scaler


import os
from setuptools import setup, find_packages

here = os.path.abspath(os.path.dirname(__file__))
log_path = here + '/blog/logs'
if not os.path.exists(log_path):
  os.makedirs(log_path+'/access_logs')
  os.makedirs(log_path+'/api_logs')

with open(os.path.join(here, 'README.rst')) as f:
    README = f.read()
requires = [
    'pyramid',
    'pyramid_chameleon',
    'pyramid_debugtoolbar',
    'pyramid_tm',
    'SQLAlchemy',
    'transaction',
    'zope.sqlalchemy',
    'waitress',
    'paste',
    'pyramid_exclog',
    'mysql-python',
    'cornice',
    'colander'
    ]

setup(name='blog',
      version=0.1,
      description='blog app',
      long_description=README,
      classifiers=[
          "Programming Language :: Python",
          "Framework :: Pylons",
          "Topic :: Internet :: WWW/HTTP",
          "Topic :: Internet :: WWW/HTTP :: WSGI :: Application"
      ],
      keywords="web services",
      author='Atul Kumar',
      author_email='atul.kumar0401@gmail.com',
      url='',
      packages=find_packages(),
      include_package_data=True,
      zip_safe=False,
      install_requires=requires,
      entry_points = """\
      [paste.app_factory]
      main=blog:main
      """,
      paster_plugins=['pyramid'])


from django.contrib import admin
from django.urls import path,include
from dot_learn_app.views import *
urlpatterns = [
    path('',display_data,name='home'),
    path('branch/<str:branch_slug>/',display_data,name="branch"),
    path('branch/<str:branch_slug>/subject/<str:subject_slug>/',display_data,name="branch+sub"),
    path('branch/<str:branch_slug>/subject/<str:subject_slug>/chapter/<str:chapter_slug>',display_data,name="branch+sub+chap"),
    path("topic/<str:topic_slug>/",display_data,name="branch+sub+chap+topic"),
    path('practice',temp,name='practice'),
    path('material',temp,name='material'),
    path('contact',temp,name='contact'),
    path('topic/',question_list,name='topic'),
]


from aiogram import types, Dispatcher
from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton

from config import bot

async def quiz_2(call: types.CallbackQuery):
    markup = InlineKeyboardMarkup()
    button_2 = InlineKeyboardButton("NEXT", callback_data="button_2")
    question = "Сколько областей в Кыргызстане?"
    markup.add(button_2)
    answer = [
        "12",
        "3",
        "7",
        "0",
        "-10",
        "999",
    ]
    await bot.send_poll(
        chat_id=call.from_user.id,
        question=question,
        options=answer,
        is_anonymous=False,
        type='quiz',
        correct_option_id=2,
        explanation="Стыдно не знать",
        open_period=5,
        reply_markup=markup
    )

async def quiz_3(call: types.CallbackQuery):
    question = "сколько вам будет лет если вы родились бы 9 лет назад?"
    answer = [
        '10',
        '9',
        '5',
        '2',
        '5',
    ]

    await bot.send_poll(
        chat_id=call.from_user.id,
        question=question,
        options=answer,
        is_anonymous=False,
        type='quiz',
        correct_option_id=1,
        explanation="Стыдно не знать",
        # open_period=5,
    )

def register_handlers_callback(dp: Dispatcher):
    dp.register_callback_query_handler(quiz_2, text="button_1")
    dp.register_callback_query_handler(quiz_3, text="button_2")


from airflow.hooks.postgres_hook import PostgresHook
from airflow.contrib.hooks.aws_hook import AwsHook
from airflow.models import BaseOperator
from airflow.utils.decorators import apply_defaults
from helpers import SqlQueries

import logging
log = logging.getLogger(__name__)

class StageToRedshiftOperator(BaseOperator):
    '''
    Copy files from s3 to redshift.
    '''
    ui_color = '#358140'
    @apply_defaults

    def __init__(self,
                 redshift_conn_id="redshift",
                 aws_conn_id="aws_credentials",
                 source_location="",
                 targ_table="",
                 file_type="json",
                 json_path="",
                 *args, **kwargs):

        super(StageToRedshiftOperator, self).__init__(*args, **kwargs)
        self.redshift_conn_id = redshift_conn_id
        self.aws_conn_id = aws_conn_id
        self.source_location = source_location
        self.targ_table = targ_table
        self.file_type = file_type
        self.json_path = json_path

    def execute(self, context):
        if self.file_type in ["json", "csv"]:
            redshift_hook = PostgresHook(self.redshift_conn_id)
            aws_hook = AwsHook(self.aws_conn_id)
            credentials = aws_hook.get_credentials()

            self.log.info(f'StageToRedshiftOperator loading data to staging table.')
            redshift_hook.run(SqlQueries.truncate_table.format(self.targ_table))
            if self.file_type == "json":
                if self.json_path != "":
                    redshift_hook.run (
                        SqlQueries.copy_json_with_json_path_to_redshift.format (
                            self.targ_table,
                            self.source_location,
                            credentials.access_key,
                            credentials.secret_key,
                            self.json_path
                        )
                    )
                else:
                    redshift_hook.run (
                        SqlQueries.copy_json_to_redshift.format (
                            self.targ_table,
                            self.source_location,
                            credentials.access_key,
                            credentials.secret_key
                        )
                    )
            elif self.file_type == "csv":
                redshift_hook.run (
                    SqlQueries.copy_csv_to_redshift.format (
                    self.targ_table,
                    self.source_location,
                    credentials.access_key,
                    credentials.secret_key)
                )

            self.log.info(f'StageToRedshiftOperator completed data load to staging table.')
        else:
            raise ValueError("file_type must be json or csv")


import os
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler

# Define paths
DOWNLOAD_DIR = os.path.expanduser("~\Downloads")
BRAIN_DIR = os.path.join(DOWNLOAD_DIR, "Brain")
BODY_DIR = os.path.join(DOWNLOAD_DIR, "Body")

def get_folder_name_from_extension(extension):
    # Strip the dot and capitalize
    return extension[1:].upper() + " Files"

def organize_files(directory):
    for filename in os.listdir(directory):
        file_path = os.path.join(directory, filename)
        if os.path.isfile(file_path):
            file_ext = os.path.splitext(filename)[1]
            folder_name = get_folder_name_from_extension(file_ext)

            dest_folder = os.path.join(BODY_DIR, folder_name)
            if not os.path.exists(dest_folder):
                os.makedirs(dest_folder)

            dest_path = os.path.join(dest_folder, filename)
            os.rename(file_path, dest_path)
            safe_string = f"Moved {filename} to {folder_name}".encode('cp1252', 'replace').decode('cp1252')
            print(safe_string)

class WatcherHandler(FileSystemEventHandler):
    def on_modified(self, event):
        if event.is_directory or event.src_path.startswith(BRAIN_DIR):
            # Do not process directory changes or files in BRAIN_DIR
            return
        organize_files(DOWNLOAD_DIR)

def main():
    # Ensure BODY_DIR exists
    if not os.path.exists(BODY_DIR):
        os.makedirs(BODY_DIR)

    # Organize existing files
    organize_files(DOWNLOAD_DIR)

    # Setup the watcher
    event_handler = WatcherHandler()
    observer = Observer()
    observer.schedule(event_handler, DOWNLOAD_DIR, recursive=False)  # No recursive, we only monitor DOWNLOAD_DIR
    observer.start()

    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()

if __name__ == "__main__":
    main()


from fastapi import FastAPI
import uvicorn

from estamp import Stamp

app = FastAPI()

@app.get("/")
def root():
    stamp = Stamp(pdf_inputname='C8950.pdf', stamp_page='L')
    outfile = stamp.run()
    return{'outputfile':outfile}

if __name__ == '__main__':
    uvicorn.run('main:app', reload=True)


#!/usr/bin/env python3

from datetime import datetime

from sqlalchemy import Table, Column, Integer, String, DateTime, Boolean, ForeignKey, and_,or_
from sqlalchemy.orm import relationship, joinedload, backref
from sqlalchemy.ext.declarative import declared_attr

from .basemodel import BaseModel
from .startstop_mixin import StartStopMixin
# from .user import User
# from .post import Post

import logging

MAX_LEN = dict(
    title=300
)

THREAD_TYPE = dict(
    PUBLIC=1,
    COMMENT=2,
    PRIVATE_CHAT=3,
    GROUP_CHAT=4,
    CHAT_OFFER=5,
)

class PostThread(BaseModel):

    __tablename__ = 'threads'

    id = Column('thread_id', Integer, primary_key=True)
    title = Column(String(MAX_LEN['title']))
    type = Column(Integer, default=THREAD_TYPE['PUBLIC'])

    posts = relationship('Post', primaryjoin = 'PostThread.id == Post.thread_id', lazy='noload')

    def add_post(self, post, users_read, last=False):
        self.posts.append(post)
        if users_read or last:
            for u2t in self.user2thread:
                if u2t.is_current():
                    if last:
                        u2t.last_post = post
                    if u2t.user_id in users_read:
                        u2t.last_readed_post = post

    @staticmethod
    def users_id_to_key(users_id):
        '''
            [3,1,54] => '1,3,54'
        '''
        return ','.join(map(str, sorted(set(users_id))))

    @staticmethod
    def key_to_users_id(key):
        '''
            '1,3,54' => [1,3,54]
        '''
        return [int(i) for i in key.split(',')]

    @staticmethod
    def get_chat_between_users(users, session):
        return session.query(
                PostThread
            ).join(
                User2Thread
            ).options(
                joinedload(PostThread.user2thread)
            ).filter(
                User2Thread.key == PostThread.users_id_to_key(users)
            ).filter(
                PostThread.type == THREAD_TYPE['PRIVATE_CHAT']
            ).filter(
                User2Thread.is_current()
            )

    @staticmethod
    def get_actual_chat(users, session, create=False):
        '''
            creates and returns a chat thread between users
            if the thread does already exist just returns
        '''
        thread = None
        key = PostThread.users_id_to_key(users)
        user2threads = {u_id: None for u_id in users}
        logging.debug(users)
        logging.debug(user2threads)
        # get thread if exists
        result = PostThread.get_chat_between_users(
            users,
            session
        ).all()
        # the thread was found
        if len(result):
            # thread = result[0].thread
            thread = result[0]
            # write user2thread instance to thread_members if they are current
            logging.error('hello')
            for u2t in thread.user2thread:
                logging.error(u2t._start_date)
                logging.error(u2t._stop_date)
                if u2t.is_current():
                    user2threads[u2t.user_id] = u2t
        # the thread wasn't found
        else:
            # if create flag wasn't set, returns nothing
            if not create:
                return
            # creating thread
            thread = PostThread(type=THREAD_TYPE['PRIVATE_CHAT'])
            session.add(thread)
        # create new user2thread isinstances if it's needed
        for u_id, u2t in user2threads.items():
            if not u2t and create:
                thread.user2thread.append(
                    User2Thread(
                        user_id=u_id,
                        key=key,
                        _start_date = datetime.utcnow(),
                        _stop_date = User2Thread.max_date()
                    )
                )
        return thread

class User2ThreadAbstract(BaseModel, StartStopMixin):

    __abstract__ = True

    id = Column('u2t_id', Integer, primary_key=True)

    @declared_attr
    def user_id(self):
        return Column(Integer, ForeignKey('users.user_id'))

    @declared_attr
    def thread_id(self):
        return Column(Integer, ForeignKey('threads.thread_id'))

    @declared_attr
    def user(cls):
        return relationship(
            'User',
            primaryjoin='%s.user_id == User.id' % cls.__name__,
            backref=cls.__name__.lower()
        )

    @declared_attr
    def thread(cls):
        return relationship(
            'PostThread',
            backref=cls.__name__.lower()
        )

class User2Thread(User2ThreadAbstract):

    __tablename__ = 'user2thread'

    key = Column(String, index=True)
    last_post_id = Column(Integer, ForeignKey('posts.post_id'))
    private_with_id = Column(Integer, ForeignKey('users.user_id'), index=True)
    last_readed_post_id = Column(Integer, ForeignKey('posts.post_id'), nullable=True)

    last_post = relationship(
        'Post',
        primaryjoin = 'User2Thread.last_post_id == Post.id',
    )
    last_readed_post = relationship(
        'Post',
        primaryjoin = 'User2Thread.last_readed_post_id == Post.id'
    )
    private_with = relationship(
        'User',
        primaryjoin="User2Thread.private_with_id == User.id",
    )

    allow_add_posts = Column(Boolean, default=True)
    allow_del_posts = Column(Boolean, default=False)
    allow_add_comments = Column(Boolean, default=True)
    allow_show_comments = Column(Boolean, default=True)

    @staticmethod
    def users_id_to_key(users_id):
        '''
            [3,1,54] => '1,3,54'
        '''
        return ','.join(map(str, sorted(set(users_id))))

    @property
    def chat_members(self):
        '''
            parse users_id from self.key
        '''
        logging.debug(self.key)
        # logging.error([self.user_id == int(i) for i in self.key])
        return [int(i) for i in self.key.split(',') if int(i) != self.user_id]

class IgnoredPosts(BaseModel):

    __tablename__ = 'ignored_posts'

    u2t_id = Column(Integer, ForeignKey('user2thread.u2t_id'), primary_key=True)
    post_id = Column(Integer, ForeignKey('posts.post_id'), primary_key=True)
    del_time = Column(DateTime(timezone=False), nullable=False, default=datetime.utcnow)
    del_user_id = Column(Integer, nullable=False)

    user2thread = relationship('User2Thread', backref='ignored_posts')
    post = relationship('Post', backref='ignored_posts')


#/usr/bin/env python3
#Author: Zhu Yong-kai (yongkai_zhu@hotmail.com)
import argparse
import os

import numpy as np
import pandas as pd
from PIL import Image
from astropy.io import fits
from matplotlib import pyplot as plt

class PSD:
    """
    Caculate the 2-dimension power spectral density.
    """
    def __init__(self, image, pixel=1.0, step=1):
        self.image = np.array(image, dtype = float)
        self.shape = self.image.shape
        self.pixel = pixel
        if self.shape[0] % 2 ==0:
            self.center = [self.shape[0] / 2, self.shape[1] / 2]
        else:
            self.center = [(self.shape[0] - 1) / 2, (self.shape[1] - 1) / 2]
        self.step = step

    def radii(self):
        shape = self.shape
        points1 = np.arange(0, shape[0])
        points2 = np.arange(0, shape[1])
        x, y = np.meshgrid(points1, points2)
        x_center, y_center = self.center
        distances = np.sqrt((x-x_center)**2+(y-y_center)**2)
        return distances

    def freq(self, r, d=1.0):
        n = self.shape[0]
        f = r / n / d
        return f

    def cal_psd2d(self):
        I = self.image
        Ifft2 = np.fft.fft2(I)
        Pxx_shift = np.fft.fftshift(Ifft2)
        Pxx = (abs(Pxx_shift))**2 / Pxx_shift.shape[0] / Pxx_shift.shape[1]\
            / self.pixel / self.pixel
        self.psd2d = Pxx
        return self.psd2d

    def cal_psd1d(self):
        OD = []
        nu = []
        rho = self.radii()
        Pxx = self.cal_psd2d()
        for r in np.arange(0, self.shape[0] / 2, self.step):
            r1 = r
            r2 = r + 1
            if r2 <= self.shape[0]:
                R = (rho >= r1).astype(int) * (rho < r2).astype(int)
                Pxx_R = R * Pxx
                nu.append(r)
                OD.append(np.median(Pxx_R[R.nonzero()[0],R.nonzero()[1]]))
        nu = np.array(nu)
        OD = np.array(OD)
        k = self.freq(nu)
        self.k = k
        self.psd1d = OD
        return self.k, self.psd1d

    def plot(self,outfile):
        """
        plot the 1-D radial power spectrum.
        """
        k = self.k
        OD = self.psd1d
        fig = plt.figure()
        plt.loglog(k, OD, '.-')
        plt.savefig(outfile)
        plt.show()

    def save(self, outfile):
        dataframe = pd.DataFrame({'k':self.k, 'K*K':self.psd1d})
        dataframe.to_csv(outfile, index=False, sep='\t')

def main():
    parser = argparse.ArgumentParser(
       description="Caculate the power spectrum distribution of\
       the input image")
    parser.add_argument("-i", "--inputfile", dest="inputfile",
                        required=True,
                        help="inputfile image file")
    parser.add_argument("-oc", "--outfile1", dest="outfile1",
                        required=True,
                        help="output CSV file")
    parser.add_argument("-oj", "--outfile2", dest="outfile2",
                        required=True,
                        help="output JPG file")
    parser.add_argument("-s", "--step", dest="step",
                        type=float,
                        help="sampling interval")
    parser.add_argument("-p", "--pixel", dest="pixel",
                        type=float,
                        help="pixel of the image")
    parser.add_argument("-c", "--clobber",
                        action='store_true',
                        default=False,
                        help="Overwrite original file.( default:False)")
    args = parser.parse_args()
    for i in [args.outfile1, args.outfile2]:
        if os.path.exists(i) and not args.clobber:
            raise FileExistsError(f'file already exists:{i}')

    img = fits.open(args.inputfile)
    img = fits.open(inputfile)
    img = img[0].data
    psd = PSD(image=img, pixel=args.pixel, step=args.step)
    psd.cal_psd1d()
    psd.plot(args.outfile2)
    psd.save(args.outfile1)

if __name__=="__main__":
    main()


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

import xml.dom.minidom as xml
import Config
import os

class XMLWriter():

    def __init__(self,isAppend=False,udid=None,mappingudid=None,recordFilePath=None):
        if not isAppend:
            self.implementation = xml.getDOMImplementation()
            self.document = self.implementation.createDocument(None,'Record',None)
            self.rootElement = self.document.documentElement
            self.deviceElement = self.document.createElement(Config.RECORD_XML_TAG_DEVICE)
            self.deviceElement.setAttribute(Config.RECORD_XML_DEVICE_ATTRIBUTE_ID,udid)
            self.deviceElement.setAttribute(Config.RECORD_XML_DEVICE_ATTRUBUTE_MAPPINGID,mappingudid)
            self.setps = [];
        else:
            self.recordFilePath = recordFilePath
            self.document = xml.parse(recordFilePath)
            self.rootElement = self.document.documentElement

    def handleScriptStart(self,scriptName=None,scriptStartTime=None,scriptCurrentLoop=None,scriptRoundFolder=None):
        self.scriptElement = self.document.createElement(Config.RECORD_XML_TAG_SCRIPT)
        self.scriptElement.setAttribute(Config.RECORD_XML_SCRIPT_ATTRIBUTE_NAME,scriptName)
        self.scriptElement.setAttribute(Config.RECORD_XML_SCRIPT_ATTRIBUTE_STARTTIME,scriptStartTime)
        self.scriptElement.setAttribute(Config.RECORD_XML_SCRIPT_ATTRIBUTE_LOOP,scriptCurrentLoop)
        self.scriptElement.setAttribute(Config.RECORD_XML_SCRIPT_ATTRIBUTE_ROUND,scriptRoundFolder)
        self.scriptResultElement = self.document.createElement(Config.RECORD_XML_TAG_SCRIPTRESULT)
        self.scriptLogElement = self.document.createElement(Config.RECORD_XML_TAG_LOG)
        self.scriptLogElement.setAttribute(Config.RECORD_XML_LOG_ATTRIBUTE_FOLDER,'Time[' + scriptStartTime + ']_Loop[' + scriptCurrentLoop + ']')
        self.scriptOKInfoElement = self.document.createElement(Config.RECORD_XML_TAG_OKINFO)
        self.scriptErrorInfoElement = self.document.createElement(Config.RECORD_XML_TAG_ERROR_INFO)

    def handleScriptEnd(self,scriptResult=None,scriptEndTime=None):
        self.scriptElement.setAttribute(Config.RECORD_XML_SCRIPT_ATTRIBUTE_ENDTIME,scriptEndTime)
        self.scriptResultElement.setAttribute(Config.RECORD_XML_SCRIPTRESULT_ATTRIBUTE_VALUE,scriptResult)

    def handleAppendScriptEnd(self,scriptResult=None,scriptEndTime=None):
        scriptElements = self.document.getElementsByTagName(Config.RECORD_XML_TAG_SCRIPT)
        scriptElements[0].setAttribute(Config.RECORD_XML_SCRIPT_ATTRIBUTE_ENDTIME,scriptEndTime)
        resultElemens = self.document.getElementsByTagName(Config.RECORD_XML_TAG_SCRIPTRESULT)
        resultElemens[0].setAttribute(Config.RECORD_XML_SCRIPTRESULT_ATTRIBUTE_VALUE,scriptResult)

    def handleScriptExecute(self,stepContent=None,stepResult=None,relatedDevice=None):
        stepElement = self.document.createElement(Config.RECORD_XML_TAG_SCRIPTSTEP)
        stepElement.setAttribute(Config.RECORD_XML_SCRIPTSTEP_ATTRIBUTE_CONTENT,stepContent)
        stepElement.setAttribute(Config.RECORD_XML_SCRIPTSTEP_ATTRIBUTE_RESULT,str(stepResult))
        stepElement.setAttribute(Config.RECORD_XML_SCRIPTSTEP_ATTRIBUTE_DEVICE,str(relatedDevice))
        self.setps.append(stepElement)

    def handleAppendScriptExecute(self,stepContent=None,stepResult=None,relatedDevice=None):
        scriptElements = self.rootElement.getElementsByTagName(Config.RECORD_XML_TAG_SCRIPT)
        stepElement = self.document.createElement(Config.RECORD_XML_TAG_SCRIPTSTEP)
        stepElement.setAttribute(Config.RECORD_XML_SCRIPTSTEP_ATTRIBUTE_CONTENT,stepContent)
        stepElement.setAttribute(Config.RECORD_XML_SCRIPTSTEP_ATTRIBUTE_RESULT,str(stepResult))
        stepElement.setAttribute(Config.RECORD_XML_SCRIPTSTEP_ATTRIBUTE_DEVICE,str(relatedDevice))
        scriptElements[0].appendChild(stepElement)

    def handleErrorInfo(self,error=None):
        errorInfoNode = self.document.createTextNode(error)
        self.scriptErrorInfoElement.appendChild(errorInfoNode)

    def handleAppendedErrorInfo(self,error=None):
        errorInfoElements = self.rootElement.getElementsByTagName(Config.RECORD_XML_TAG_ERROR_INFO)
        errorInfoNode = self.document.createTextNode(error)
        errorInfoElements[0].appendChild(errorInfoNode)

    def handleOKInfo(self,ok=None):
        okInfoNode = self.document.createTextNode(ok)
        self.scriptOKInfoElement.appendChild(okInfoNode)

    def generateRecord(self,recordPath=None):
        self.scriptElement.appendChild(self.scriptResultElement)
        self.scriptElement.appendChild(self.scriptLogElement)
        self.scriptElement.appendChild(self.scriptOKInfoElement)
        self.scriptElement.appendChild(self.scriptErrorInfoElement)
        for verStep in self.setps:
            self.scriptElement.appendChild(verStep)
        self.deviceElement.appendChild(self.scriptElement)
        self.rootElement.appendChild(self.deviceElement)
        file = open(os.path.join(recordPath,Config.RECORD_XML_NAME),'a')
        self.document.writexml(file)
        file.close()

    def saveRecord(self):
        file = open(self.recordFilePath,'w')
        self.document.writexml(file)
        file.close()


import torch
import numpy as np
from args import get_parser
import pickle
import os
from torchvision import transforms
from model import get_model, label2onehot
import json
import sys
import random
from modules.utils import LogResults, Accuracy, store_features, maintain_cnn_features, print_arg_info, unit_test_portions_units
from data_modules.recipe_loader import get_recipe_loader
from data_modules.ingr_loader import get_ingr_loader
from data_modules.data_aux import transformation_functions

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
map_loc = None if torch.cuda.is_available() else 'cpu'

# -------------------------------------------

def main(args):

    checkpoints_dir = args.save_dir
    args.use_predicted = True

    vars_to_replace = ['batch_size', 'eval_split', 'save_dir', 'aux_data_dir',
                       'recipe1m_dir', 'use_lmdb', 'ckpt_epoch', 'subsequent_calorie',
                       'use_predicted', 'evaluate_given_images', 'images_folder']

    store_dict = {}
    for var in vars_to_replace:
        store_dict[var] = getattr(args, var)

    if not args.store_cnn_feat:
        do_calorie_scale = args.do_calorie_scale
        splits = args.splits
        model_output = args.model_output
        subsequent_calorie = args.subsequent_calorie
        args = pickle.load(open(os.path.join(checkpoints_dir, 'args.pkl'), 'rb'))
        args.splits = splits
        args.do_calorie_scale = do_calorie_scale
        args.model_output = model_output
        args.subsequent_calorie = subsequent_calorie

    for var in vars_to_replace:
        setattr(args, var, store_dict[var])

    model_path = os.path.join(checkpoints_dir, 'model{}.ckpt'.format(args.ckpt_epoch))

    torch.set_printoptions(profile="full")

    data_dir = args.recipe1m_dir

    train_transform = transformation_functions(args, run_mode="train")
    eval_transform = transformation_functions(args, run_mode="eval")
    transforms = {"train": train_transform, "eval": eval_transform}

    if "simple" in args.model_type:
        is_simple = True
        data_loader, dataset = get_ingr_loader(data_dir, args.aux_data_dir, args.splits,
                                      run_mode='eval', transforms=transforms,
                                      batch_size=args.batch_size, shuffle=True, num_workers=args.num_workers,
                                      drop_last=False, suff=args.suff, args=args)
    else:
        is_simple = False
        data_loader, dataset = get_recipe_loader(data_dir, args.aux_data_dir, args.splits,
                                      run_mode='eval', transforms=transforms,
                                      batch_size=args.batch_size, shuffle=True, num_workers=args.num_workers,
                                      drop_last=False, suff=args.suff, args=args)

    ingr_vocab_size = dataset.get_ingrs_vocab_size()
    if args.dataset=="recipe1m":
        unit_vocab_size = dataset.get_unit_vocab_size()
    else:
        unit_vocab_size = -1
    dish_vocab_size = dataset.get_dish_vocab_size()
    states_vocab_size = dataset.get_state_vocab_size()
    calorie_classes_num = dataset.get_calorie_classes_num()

    # Build the model
    model = get_model(args, ingr_vocab_size, unit_vocab_size, states_vocab_size, calorie_classes_num, dish_vocab_size, dataset.bottom_up_feat_size, dataset.pretrained_emb, loss_weights_matrix=dataset.weights_tensor, flattened_weights=dataset.flattened_weights)

    if not args.store_cnn_feat:
        # Load the trained model parameters
        model.load_state_dict(torch.load(model_path, map_location=map_loc))
        print ("loaded epoch {} checkpoint of the model.".format(args.ckpt_epoch))

    log_results = LogResults(dataset, is_simple, dataset.run_mode, args.evaluate_given_images)

    # prints settings of the current run
    print_arg_info(args)

    if args.just_plot_error:
        log_results.plot_errors()
        exit()

    model = model.to(device)
    model.eval()

    # dictionary for storing cnn features for each instance in the dataset
    if args.store_cnn_feat:
        imgid_to_features = {}
        stored_imgids = set({})
        store_batch_num = 1

    accuracy_log = Accuracy(args, dataset.data_size, is_simple, dataset.run_mode, dataset.unscale_batch)
    for step, (ques_idx, tar_idx, tar_values, tar_calories, tar_states, tar_calorie, mean_prior_calories_gt, portion_distrib, calorie_class_idx, dish_idxs, image_input, img_id, qids, path) in enumerate(data_loader):

        if args.store_cnn_feat:
            update_batch_num = store_features(args.features_dir, dataset.run_mode, imgid_to_features, store_batch_num)
            if update_batch_num!=store_batch_num:
                store_batch_num = update_batch_num
                imgid_to_features = {}

        # ---------- move all data loaded from dataloader to gpu
        # input indice referring to ingredients
        if not ques_idx is None:
            ques_idx = ques_idx.to(device)
        # input indice referring to a dish name
        if not dish_idxs is None:
            dish_idxs = dish_idxs.to(device)
        # target state idx
        if not tar_states is None:
            tar_states = tar_states.to(device)
        # input image or image features
        if not image_input is None:
            image_input = image_input.to(device)
        # target indice referring to unit classes (N=6)
        if not tar_idx is None:
            tar_idx = tar_idx.to(device)
        # target portion values per input (ingredient)
        if not tar_values is None:
            tar_values = tar_values.to(device)
        # target portion outputs referring to each unit type (N=6)
        if not portion_distrib is None:
            portion_distrib = portion_distrib.to(device)
        # target calorie class associated with the calorie value.
        if not calorie_class_idx is None:
            calorie_class_idx = calorie_class_idx.to(device)
        # target per recipe calorie
        if not tar_calorie is None:
            tar_calorie = tar_calorie.to(device)
        # individual calories referring to ingredients
        if not tar_calories is None:
            tar_calories = tar_calories.to(device)
        # ----------
        if not mean_prior_calories_gt is None:
            mean_prior_calories_gt = mean_prior_calories_gt.to(device)

        if "simple" in args.model_type:
            ingr_calorie_vec_mask = label2onehot(ques_idx, dataset.pad_value)
            ingr_calorie_vec = label2onehot(ques_idx, dataset.pad_value, tar_calorie.squeeze())
        else:
            ingr_calorie_vec, ingr_calorie_vec_mask = None, None

        # create a mask for image featuers if the number of image feature vectors used is variable
        if args.image_encoder_type=="bottom-up":
            image_mask = (torch.sum(torch.abs(image_input), dim=-1) == 0)
        else:
            image_mask = None

        # create mask for ingredients
        ques_mask = (ques_idx == dataset.pad_value)

        # feed-forward data in the model
        losses, outputs = model(ques_idx, ques_mask, None, tar_idx, tar_values, tar_calorie, tar_calories, tar_states,
                                mean_prior_calories_gt, portion_distrib, dish_idxs, image_input, image_mask,
                                ingr_calorie_vec_mask, ingr_calorie_vec, calorie_class_idx=calorie_class_idx)

        if args.store_cnn_feat:
            maintain_cnn_features(img_id, outputs, imgid_to_features, stored_imgids)
            continue

        # logging acuracy
        pred_idx = accuracy_log.update(outputs, tar_idx, tar_values, tar_calorie, tar_calories, tar_states, ques_mask)

        log_results.update(outputs, pred_idx, tar_idx, tar_values, tar_calorie, tar_calories, tar_states, qids, img_id, ques_idx, is_simple)

    if args.store_cnn_feat:
        store_features(args.features_dir, dataset.run_mode, imgid_to_features, store_batch_num, last_batch=True)

    log_results.finalize(args, is_simple)

    # mean accuracy over all testing data
    accuracy_log.epoch_finalize()

    print ('******************* Testing Ended *******************')

if __name__ == '__main__':
    args = get_parser()
    torch.manual_seed(1234)
    torch.cuda.manual_seed(1234)
    random.seed(1234)
    np.random.seed(1234)
    main(args)


import unittest
import json
import os
from memcnn.experiment.factory import load_experiment_config, experiment_config_parser
from memcnn.config import Config
import memcnn.config

class ConfigTestCase(unittest.TestCase):

    class ConfigTest(Config):
        @staticmethod
        def get_filename():
            return os.path.join(Config.get_dir(), "config.json.example")

    def setUp(self):
        self.config = ConfigTestCase.ConfigTest()

        self.config_fname = os.path.join(os.path.dirname(__file__), "..", "config.json.example")
        self.experiments_fname = os.path.join(os.path.dirname(__file__), "..", "experiments.json")

        def load_json_file(fname):
            with open(fname, 'r') as f:
                data = json.load(f)
            return data

        self.load_json_file = load_json_file

    def test_loading_main_config(self):
        self.assertTrue(os.path.exists(self.config.get_filename()))
        data = self.config
        self.assertTrue(isinstance(data, dict))
        self.assertTrue("data_dir" in data)
        self.assertTrue("results_dir" in data)

    def test_loading_experiments_config(self):
        self.assertTrue(os.path.exists(self.experiments_fname))
        data = self.load_json_file(self.experiments_fname)
        self.assertTrue(isinstance(data, dict))

    def test_experiment_configs(self):
        data = self.load_json_file(self.experiments_fname)
        config = self.config
        keys = data.keys()
        for key in keys:
            result = load_experiment_config(self.experiments_fname, [key])
            self.assertTrue(isinstance(result, dict))
            if "dataset" in result:
                experiment_config_parser(result, config['data_dir'])

    def test_config_get_filename(self):
        self.assertEqual(Config.get_filename(), os.path.join(os.path.dirname(memcnn.config.__file__), "config.json"))

    def test_config_get_dir(self):
        self.assertEqual(Config.get_dir(), os.path.dirname(memcnn.config.__file__))

    def test_verbose(self):
        ConfigTestCase.ConfigTest(verbose=True)

if __name__ == '__main__':
    unittest.main()


from flask import Flask, jsonify, request
import pickle
import pandas as pd
from sklearn.neighbors import NearestNeighbors

app = Flask("__name__")

# Load model, user data, and restaurant data
with open("model.pkl", "rb") as f:
    model = pickle.load(f)

user_df = pd.read_parquet("../dataset/user.parquet")
restaurant_df = pd.read_parquet("../dataset/restaurant.mapping.parquet").set_index("index")

def recommend_restaurants(user_id):
    # Find 20 nearest neighbors to recommend restaurants
    user_data = user_df[user_df["user_id"] == user_id].drop(columns="user_id")

    if user_data.empty:
        return {"error": "User not found"}, 404

    dist, ind = model.kneighbors(user_data, n_neighbors=20)
    recommended_restaurant_ids = restaurant_df.iloc[ind[0]]["restaurant_id"].tolist()
    return {"restaurant": recommended_restaurant_ids}, 200

@app.route('/recommend/<user_id>', methods=['GET'])
def recommend(user_id):
    if request.method == 'GET':
        result, status_code = recommend_restaurants(user_id)
        return jsonify(result), status_code

@app.route('/ready', methods=['GET'])
def ready():
    return 'OK'

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0', port=5005)