##  asp_parsing.py
##
##  /usr/bin/python2.7
##

import os
# import subprocess
from subprocess import Popen, PIPE
from subprocess import *
import re
import random
import time
import numpy as np
import csv
#import cPickle as Pickle

# go to SPARC solver directory
os.chdir('/home/maija/maija.fil@gmail.com/THESIS/ASP')

# --------------------------------------------------------------------------------- #
#                                  Helper Functions
# --------------------------------------------------------------------------------- #

def jarwrapper(*args):
    """Starts a sparc solver process, taking in the
     regular sparc input arguments, and returns the
     output of the solver."""
    process = Popen(['java', '-jar', 'sparc.jar'] + list(args), stdout=PIPE, stderr=PIPE)
    ret = []
    while process.poll() is None:
        line = process.stdout.readline()
        if line != '' and line.endswith('\n'):
            ret.append(line[:-1])
    stdout, stderr = process.communicate()
    ret += stdout.split('\n')
    if stderr != '':
        ret += stderr.split('\n')
        # runtime_error = True # uncomment & return to use error msg.
    ret.remove('')
    return ret

def rm_header(result):
    """Removes the header from program output"""
    o = filter(None, result)
    o = [x for x in o if "SPARC" not in x]
    o = [x for x in o if "program translated" not in x]
    return o

# TODO
# create a method to detect and deal with runtime errors

def pick_goal(g):
    """Set must be a list of possible goals in
    the form of a literal"""
    #g = random.choice(ans_set)
    g = list(g)  # convert to list
    g[-2] = 'I'  # insert chosen horizon value
    goal_str = ''.join(g)  # create line to be inserted
    return goal_str

def find_line_id(string, f):
    """Returns line number of a specific string (flag) to be searched for.
    In this case, the flag is a line inserted before the specified goal in
    ASP programs for this project."""
    return [num for [num, line] in enumerate(f) if line.startswith(string)]

def set_goal(inFile, outFile, goal):
    """Adds a line setting the goal to the program
    specified by inFile, and saves it to an output
    file specified by outFile."""

    with open(inFile) as prog:
        lines = prog.readlines()

    # Define Goal flag:
    g_flag = '% Execution Goal'

    line_num = find_line_id(g_flag, lines)

    # Add goal and save program
    lines[line_num[0] + 1] = 'goal(I):-' + goal + '.' + '\n'

    with open(outFile, 'w') as prog:
        prog.writelines(lines)

def out_to_list(sparc_output):
    """Separates the output returned by jarwrapper
    into a list of lists. """
    # check if output is empty
    if len(sparc_output)!=0:
        sparc_output = [i[1:-1] for i in sparc_output]
        sparc_output = [i.split(', ') for i in sparc_output]
    return sparc_output

def run_goal_gen(asp_filename):
    """Finds the answer set of the
    selected goal generation program,
    returns a list of all fluents."""
    args = [asp_filename, '-A']
    # OUTPUT
    result = jarwrapper(*args)
    fluent_set = rm_header(result)
    fluent_set = fluent_set[0]
    # remove braces
    fluent_set = fluent_set[1:-1]
    # add extra paren so it doesn't get removed
    fluent_set = fluent_set+')'
    # split into a list
    fluent_ls = re.split('\s', fluent_set)
    # remove comma
    fluent_ls = [i[:-1] for i in fluent_ls]
    return fluent_ls

class TrialData:
    """A simple object for data collection.
    Keeps the goal literal, execution time,
    plan arity, set of plans, and number
    of plans returned in lists.
    This layout should be improved once it
    is clear what structure is preferrable
    for data analysis, e.g. to  access
    individual entries (trials). """
    def __init__(self):
        self.trial = []
        self.goal = []
        self.success = []
        self.exe_t = []
        self.arity = []
        self.plans = []
        self.no_plans = []
        self.missing_ax = []    # missing axioms
        self.no_of_missing_ax = []  # number of missing axioms
        self.init_cond = []  # initial conditions
        self.goalID = []    # simple code for goal literals
        self.plan = []      # plans
        self.correct = []   # ground truth success
        self.expl = []      # diagnostics output

#class TrialData:
#    """A simple object for data collection in the second experiment.
#    Keeps all categories of ExpCOndition, and adds success, plan
#    correctness, and explanations for failed plans. """
#    def __init__(self):
#        self.goal = []      # goal literal
#        self.goalID = []    # simple code for goal literals
#        self.plan = []      # plans
#        self.success = []   # goal achieved
#        self.correct = []   # ground truth success
#        self.init_cond = []  # initial conditions
#        self.expl = []      # diagnostics output

def find_plan_length(plan_list):
    """Finds plan length indicated by the predicate
    plan_length(i), where i is the first time step
    when the goal holds."""
    if len(plan_list) == 0:
        # no plans were returned
        plan_length = 0
    else:
        # plan length is always the first item in the output
        plan_len_pred = plan_list[0][1]
        # scan it for a number
        re_out = re.search(r"\d+", plan_len_pred)
        # access the match and convert to an integer
        plan_length = int(re_out.group(0))
        if plan_length == 0:
            plan_length = 999
    return plan_length

def get_step(literal):
    """Finds the time step of a given literal.
    It is assumed that time steps are always
    the last item of the literal."""
    # scan input for numbers
    re_out = re.findall(r"\d+", literal)
    # access the match and convert to an integer
    step = int(re_out[-1])
    return step

def get_actors(plan):
    """Returns a list of obj. and agents
    appearing in the plan."""
    words = []
    for i in plan:
        words.extend(re.findall(r"\b[\d\w]{2,}\b", i))
    # remove occurs / plan_length
    actors = []
    actors = [i for i in words if not 'occurs' in i]
    actors = [i for i in actors if not 'plan_length' in i]
    actors = [i for i in actors if not 'goal' in i]
    actors = [i for i in actors if not 'room' in i]
    actors = [i for i in actors if not 'corridor' in i]
    # 3. return list
    return actors

def hist_search(history, occ_plan):
    pln = [i for i in occ_plan if 'occurs' in i]
    steps = len(pln)
    # create a list of all steps
    hist_time = []
    # remove success from hist:
    history = [i for i in history if not 'success' in i]
    for item in history:
        hist_time.append(get_step(item))
    # init empty list of ordered history items
    ordered_hist = []
    # add all items from 0th timestep:
    bool_arr = (np.array(hist_time)==0)
    zeroth = [i for (i, b) in zip(history, bool_arr) if b]
    ordered_hist.extend(zeroth)
    for step in range(1,steps):
        # get relevant actors for that step
        current_steps = pln[step-1:step]
        current_actors = get_actors(current_steps)
        # get history from this step
        bool_arr = np.array(hist_time)==step
        current_hist = [i for (i, b) in zip(history, bool_arr) if b]
        # relevant history:
        for i in current_hist:
            if any(s in i for s in current_actors):
                ordered_hist.append(i)
    return ordered_hist

def add_init_state(program, out_prog, state):
    """"""
    # 1.
    f = open(program, "r")
    contents = f.readlines()
    f.close()
    line_num = find_line_id("%&%& Received initial condition:", contents)
    end_ln = find_line_id("%&%& End of starting state", contents)
    # new initial conditions:
    init_state = [i + '. \n' for i in state]
    # remove lines & add new state
    contents_w = contents[0:line_num[0] + 1]
    contents_w.extend(init_state[0:])
    contents_w.extend(contents[end_ln[0]:-1])
    # write to file
    f = open(out_prog, "w")
    contents_w = "".join(contents_w)
    f.write(contents_w)
    f.close()

def add_plan(plan, in_prog, out_prog):
    # add plan to program:
    f = open(in_prog, "r")
    contents = f.readlines()
    f.close()
    line_num = find_line_id("%&%& Received plan:", contents)
    end_ln = find_line_id("%&%& End of plan", contents)
    # take only occurs
    newpln = [i for i in plan if 'occurs' in i]
    newpln = [i + '. \n' for i in newpln]
    # remove lines inbetween (i.e. old plan) & add new plan
    contents_w = contents[0:line_num[0] + 1]
    contents_w.extend(newpln[0:])
    contents_w.extend(contents[end_ln[0]:-1])
    # write to file
    f = open(out_prog, "w")
    contents_w = "".join(contents_w)
    f.write(contents_w)
    f.close()

def determine_success(asp_output):
    s = 'success'
    if s in asp_output:
        sb = 1
    else:
        sb = 0
    return sb

def plan_check(formatted_output):
    """Check if plan is pointless, i.e.
    true at t=0. """

def occ_filter(plan_ls):
    new_ls = []
    for plan in plan_ls:
        plan = [i for i in plan if 'occurs' in i]
        new_ls.append(plan)
    return new_ls

class Experiment1:
    """Loop through deletion conditions efficiently"""

    def __init__(self):

        self.asp_init = 'init_gen.sp'
        self.asp_goal_set = 'goal-gen-with-constraints.sp'
        self.asp_complete = 'bw-translation-from-al-3.sp'
        self.asp_partial = 'bw-al-3-partial.sp'
        #  Generate all possible goals for this domain
        self.goal_ls = run_goal_gen(self.asp_goal_set)

        # Get a random set of initial conditions
        self.init_out = jarwrapper(self.asp_init, '-A ', '-n', '1')
        self.init_out = rm_header(init_out)
        self.init_list = out_to_list(init_out)
        # remove can_support :
        self.init_list = [i for i in init_list[0] if not 'can_support' in i]

        # Choose file names for programs with altered goals,
        # the source programs are left unchanged.
        self.target_cdk = 'rand-goal-cdk.sp'
        self.target_pdk = 'rand-goal-pdk.sp'

        self.ec_to_test = [3,5,8]
        self.ar_to_test = [13, 14, 15, 16, 21, 28, 31, 32] # not sure about 31 and 32

    def create_pdk(self, in_prog, out_prog, axiom_type, n_del, *args):
        # Create Partial domain knowledge script and set deletion of information:
        """ axiom type - aff. relations or exec. conditions,
        n_del - number of axioms to delete
        del_idx - (optional) list of axiom indices to delete"""
        # read in complete file:
        f = open(in_prog, 'r')
        script = f.readlines()

        # get aff-rels. / exec. conds. (through flags)
        ec_ln = find_line_id('%&%& E.c.:', script)
        ec_end = find_line_id('%% AFFORDANCE AXIOMS END', script)
        ar_ln = find_line_id('%&%& A.R.:', script)
        ar_end = find_line_id('%&%& A.R. end', script)

        exec_conds = script[ec_ln[0]:ec_end[0]]
        aff_rels = script[ar_ln[0]:ar_end[0]]

        # connect into 1:
        exec_conds_s = "".join(exec_conds)
        aff_rels_s = "".join(aff_rels)
        # Regex by /d.
        ec_ls = re.split('%\s\*', exec_conds_s)
        aff_rel_ls = re.split('%\s(\d+\.)', aff_rels_s)

        aff_IDs = []
        aff_rule = []

        for item in aff_rel_ls:
            if item[0].isdigit():
                aff_IDs.append(int(item[0:2]))
            else:
                aff_rule.append(item)

        aff_rule = aff_rule[1:]

        to_del_ar = None
        to_del_ec = None

        if axiom_type == 'ar':
            if args:
                to_del_ar = args[0]
            #else: to_del_ar = random.sample(self.ar_to_test, n_del)
        elif axiom_type=='ec':
            if args:
                to_del_ec = args[0]
            #else: to_del_ec = random.sample(self.ec_to_test, n_del)

        # write the rest to file (do they still have /n?)
        ec_w = [l for i,l in enumerate(ec_ls) if not i in to_del_ec]
        ar_w = [aff_rule[i] for (i,l) in enumerate(aff_IDs) if not l in to_del_ar]
        ec_w = exec_conds_s #"".join(ec_w)
        ar_w = "".join(ar_w)

        # pdk = cdk + ec+w + lines inbetween + ar+w + remaining lines
        pdk_script = script[0:ec_ln[0]]
        pdk_script.extend(ec_w)
        pdk_script.extend(script[ec_end[0]:ar_ln[0]])
        pdk_script.extend(ar_w)
        pdk_script.extend(script[ar_end[0]:])
        pdk_script = "".join(pdk_script)
        # this is the pdk domain.

        f = open(out_prog, 'w')
        f.write(pdk_script)
        f.close()

    #def loop_through_goals(self):

#%%
# --------------------------------------------------------------------------------- #
#                       Data recording and program parameters
# --------------------------------------------------------------------------------- #

# instantiate data collection for complete and incomplete domain knowledge conditions
complete_dk = TrialData()
partial_dk = TrialData()

# set program file names for the 'oracle' (complete domain knowledge) program,
# the 'robot' program which is missing several axioms, thus having incomplete
# domain knowledge, the goal generating program and a program for generating
# random initial conditions.
asp_init = 'init_gen.sp'
asp_goal_set = 'goal-gen-with-constraints.sp'
asp_complete = 'bw-translation-from-al-3.sp'
asp_partial = 'bw-al-3-partial.sp'

#  Generate all possible goals for this domain
goal_ls = run_goal_gen(asp_goal_set)

# Get a random set of initial conditions
init_out = jarwrapper(asp_init, '-A ', '-n', '1')
init_out = rm_header(init_out)
init_list = out_to_list(init_out)
# remove can_support :
init_list = [i for i in init_list[0] if not 'can_support' in i]

# Choose file names for programs with altered goals,
# the source programs are left unchanged.
target_cdk = 'rand-goal-cdk.sp'
target_pdk = 'rand-goal-pdk.sp'

#%%
# ----------------------------------------------------- #
#                  Experiment 1
# ----------------------------------------------------- #

iters = len(goal_ls)

# save goal list:
with open('goals.tsv', "wb") as tsv_file:
        writer = csv.writer(tsv_file, delimiter='\t')
        writer.writerow(goal_ls)

c_all = open('x_1.tsv', 'wb')

#head = ['trial', 'goal', 'init_cond', 'missing_ax', 'miss_ax_n', 'pdk_exe_t', 'cdk_exe_t',
#        'pdk.success', 'cdk.success', 'pdk.arity', 'cdk.arity', 'pdk.plans', 'cdk.plans',
#        'pdk.n.plans', 'cdk.n.plans']

head = ['trial', 'goal','pdk_exe_t', 'cdk_exe_t',
        'pdk.success', 'cdk.success', 'pdk.arity', 'cdk.arity', 'pdk.plans', 'cdk.plans',
        'pdk.n.plans', 'cdk.n.plans']

writer = csv.writer(c_all, delimiter='\t')
writer.writerow(head)

#%% Run when all desired conditions completed:

#c_all.close()

#%% Run as an Experiment1 instance:
complete_dk = TrialData()
partial_dk = TrialData()

expData = Experiment1()

affordance_ax_conditions = []
ec_axiom_conditions = []

iters = range(len(goal_ls))

levels = [1, 2, 3] # simultaneous levels of deletion
level = levels[0] # select current level

epochs = range(len(expData.ar_to_test))

for e in epochs:
    deleted_ax = expData.ar_to_test[e]
    expData.create_pdk(expData.asp_complete, expData.asp_partial, 'ar', level, [deleted_ax])

    for i in iters:
        # Choose a goal
        goal = pick_goal(goal_ls[i])
        trial = i
        #goal = pick_goal(gls)

        # Write the goal to the target program files.
        set_goal(asp_complete, target_cdk, goal)
        set_goal(asp_partial, target_pdk, goal)

        # Set starting conditions:
        init_out = jarwrapper(asp_init, '-A ', '-n', '1')
        init_out = rm_header(init_out)
        init_list = out_to_list(init_out)
        # remove can_support :
        init_list = [i for i in init_list[0] if not 'can_support' in i]
        add_init_state(target_cdk,target_cdk,init_list)
        add_init_state(target_pdk,target_pdk,init_list)

        # Execute program, save output
        t = time.clock()
        cdk_out = jarwrapper(target_cdk, '-A')
        t_end = time.clock()

        t2 = time.clock()
        pdk_out = jarwrapper(target_pdk, '-A')
        t2_end = time.clock()

        cdk_out = rm_header(cdk_out)
        pdk_out = rm_header(pdk_out)

        cdk_plan_ls = out_to_list(cdk_out)
        pdk_plan_ls = out_to_list(pdk_out)

        # find plan length
        cdk_p_len = find_plan_length(cdk_plan_ls)
        pdk_p_len = find_plan_length(pdk_plan_ls)

        pdkSucc = determine_success(cdk_out)
        cdkSucc = determine_success(pdk_out)
        complete_dk.success.append(cdkSucc)
        partial_dk.success.append(pdkSucc)

        complete_dk.goal.append(goal)
        complete_dk.arity.append(cdk_p_len)
        complete_dk.exe_t.append(t_end - t)
        cdk_plan_ls = occ_filter(cdk_plan_ls)
        complete_dk.plans.append(cdk_plan_ls)
        complete_dk.no_plans.append(len(cdk_plan_ls))

        partial_dk.goal.append(goal)
        partial_dk.arity.append(pdk_p_len)
        partial_dk.exe_t.append(t2_end - t2)
        pdk_plan_ls = occ_filter(pdk_plan_ls)
        partial_dk.plans.append(pdk_plan_ls)
        partial_dk.no_plans.append(len(pdk_plan_ls))

        partial_dk.missing_ax.append(deleted_ax)    # missing axioms
        partial_dk.no_of_missing_ax.append(level)  # number of missing axioms
        partial_dk.init_cond.append(init_list)  # initial conditions
        complete_dk.missing_ax.append(deleted_ax)    # missing axioms
        complete_dk.no_of_missing_ax.append(level)  # number of missing axioms
        complete_dk.init_cond.append(init_list)  # initial conditions
        partial_dk.trial.append(trial)
        complete_dk.trial.append(trial)

        #if cdk_p_len!=999:
        #    writer.writerow([trial, goal, pdkSucc, cdkSucc,
        #                     pdk_p_len, cdk_p_len, pdk_plan_ls, cdk_plan_ls,
        #                     len(pdk_plan_ls), len(cdk_plan_ls)])

#%% Write data to csv separately

# get rid of all items in plan list apart from occurs
#new_pl = []
#for plists in partial_dk.plans:
#    tr = []
#    for plan in plists:
#        plan = [i for i in plan if 'occurs' in i]
#        tr.append(plan)
#    new_pl.append(tr)
#
#
#new_cpl = []
#for plists in complete_dk.plans:
#    tr = []
#    for plan in plists:
#        plan = [i for i in plan if 'occurs' in i]
#        tr.append(plan)
#    new_cpl.append(tr)
#
    # won't be needed for next run

rtsv = open('rfile_exp1.tsv', 'wb')
#rtsv_pdk = open('pdk_r.tsv', 'wb')
#head = ['trial', 'goal', 'init_cond', 'missing_ax', 'miss_ax_n', 'pdk_exe_t', 'cdk_exe_t',
#        'pdk.success', 'cdk.success', 'pdk.arity', 'cdk.arity', 'pdk.plans', 'cdk.plans',
#        'pdk.n.plans', 'cdk.n.plans']

head = ['trial', 'goal', 'arity', 'exe_t', 'step', 'action',
            'no_plans', 'success', 'missing_ax', 'n_missing', 'condition']

writer_r = csv.writer(rtsv, delimiter='\t')
#writer_c = csv.writer(rtsv_cdk, delimiter='\t')

writer_r.writerow(head)
#writer_c.writerow(head)

# length of struct. to save:
iters = range(len(partial_dk.goal))

# find all trials where plan length = 999
indices = [i for i, x in enumerate(partial_dk.arity) if x == 999]
plan = 1
# s
for i in iters:

    condition = 'PDK_exp1'
    trial = partial_dk.trial[i]
    #trial = i
    v1=partial_dk.goal[i]
    v2=partial_dk.arity[i]
    v3=partial_dk.exe_t[i]
    v4=partial_dk.plans[i]
    v5=partial_dk.no_plans[i]
    v6=partial_dk.success[i]
    v7=partial_dk.missing_ax[i]
    v8=partial_dk.no_of_missing_ax[i]

    if v2!=999:
        for j, curr_plan in enumerate(v4):
            if not curr_plan:  # if its empty
                writer_r.writerow([plan, trial, v1, v2, v3, 'nan', 'nan', v5, v6, v7, v8, condition])
            else:
                for act in curr_plan:
                    if not act: # this is probably impossible if the above evaluates to else
                        writer_r.writerow([plan, trial, v1, v2, v3, 'nan', 'nan', v5, v6, v7, v8, condition])
                    else:
                        writer_r.writerow([plan, trial, v1, v2, v3, j, act, v5, v6, v7, v8, condition])
            plan=+1

    condition = 'CDK_exp1'
    trial = complete_dk.trial[i]
    v1=complete_dk.goal[i]
    v2=complete_dk.arity[i]
    v3=complete_dk.exe_t[i]
    v4=complete_dk.plans[i]
    v5=complete_dk.no_plans[i]
    v6=complete_dk.success[i]
    v7=complete_dk.missing_ax[i]
    v8=complete_dk.n_missing[i]
    if v2!=999:
        for k, curr_plan in enumerate(v4):
            if not curr_plan:  # if its empty
                writer_r.writerow([plan, trial, v1, v2, v3, 'nan', 'nan', v5, v6, v7, v8, condition])
            else:
                for act in curr_plan:
                    if not act: # this is probably impossible if the above evaluates to else
                        writer_r.writerow([plan, trial, v1, v2, v3, 'nan', 'nan', v5, v6, v7, v8, condition])
                    else:
                        writer_r.writerow([plan, trial, v1, v2, v3, k, act, v5, v6, v7, v8, condition])
        plan=+1

#%%
# save data
#import pickle
#with open('results/partial_dk_results_1.pkl', 'wb') as output:
#    pickle.dump(partial_dk, output, pickle.HIGHEST_PROTOCOL)
#
#with open('results/complete_dk_results_1.pkl', 'wb') as output:
#    pickle.dump(complete_dk, output, pickle.HIGHEST_PROTOCOL)

#%%
# ----------------------------------------------------- #
#                  Experiment 2
# ----------------------------------------------------- #

# 1. Create experiment object:
ex2PDK = TrialData()
ex2CDK = TrialData()

iters = range(len(partial_dk.plans))

# 2. create an extended struct of experiment 1 where each plan has a row:
for i in iters:
    # get trial variables:
    trial = partial_dk.trial[i]
    v1=partial_dk.goal[i]
    v2=partial_dk.arity[i]
    v3=partial_dk.exe_t[i]
    v4=partial_dk.plans[i]
    v5=partial_dk.no_plans[i]
    v6=partial_dk.success[i]
    v7=partial_dk.missing_ax[i]
    v8=partial_dk.no_of_missing_ax[i]
    v9=partial_dk.init_cond[i]

    if v2!=999:
        for j, curr_plan in enumerate(v4):
            ex2PDK.goal.append(v1)
            ex2PDK.trial.append(trial)
            ex2PDK.arity.append(v2)
            ex2PDK.exe_t.append(v3)
            ex2PDK.plans.append(v4)
            ex2PDK.no_plans.append(v5)
            ex2PDK.success.append(v6)
            ex2PDK.missing_ax.append(v7)
            ex2PDK.no_of_missing_ax.append(v8)
            ex2PDK.init_cond.append(v9)
            ex2PDK.plan.append(curr_plan)

iters = range(len(complete_dk.plans))

for i in iters:
    # get trial variables:
    #trial = complete_dk.trial[i]
    v1=complete_dk.goal[i]
    v2=complete_dk.arity[i]
    v3=complete_dk.exe_t[i]
    v4=complete_dk.plans[i]
    v5=complete_dk.no_plans[i]
    v6=complete_dk.success[i]
    #v7=complete_dk.missing_ax[i]
    #v8=complete_dk.n_missing[i]
    #v9=complete_dk.init_cond[i]

    if v2!=999:
        for j, curr_plan in enumerate(v4):
            ex2CDK.goal.append(v1)
            #ex2CDK.trial.append(trial)
            ex2CDK.arity.append(v2)
            ex2CDK.exe_t.append(v3)
            ex2CDK.plans.append(v4)
            ex2CDK.no_plans.append(v5)
            ex2CDK.success.append(v6)
            #ex2CDK.missing_ax.append(v7)
            #ex2CDK.no_of_missing_ax.append(v8)
            #ex2CDK.init_cond.append(v9)
            #ex2CDK.plan.append(curr_plan)

# Choose input files
asp_sim = 'world-sim.sp'
sim = 'sim_test.sp'
asp_diag = 'diag-obs.sp'
diag = 'diag_test.sp'

# Choose number of fluents to show in history with all variables
required_fluents = ["location", "on", "in_hand"]

#%%
iters = range(len(ex2PDK.trial))

for i in iters:
    # get all variables for current trial:
    goal = ex2PDK.goal[i]
    trial = ex2PDK.trial[i]
    horz = ex2PDK.arity[i]
    plan = ex2PDK.plans[i]
    #ex2PDK.success[i]
    ax_del = ex2PDK.missing_ax[i]
    n_del = ex2PDK.no_of_missing_ax[i]
    state = ex2PDK.init_cond[i]

    # Set starting state:
    add_init_state(asp_sim, sim, state)
    add_init_state(asp_diag, diag, state)
    # Set altered knowledge:
    expData.create_pdk(asp_diag, diag, 'ar', n_del, [14]) # not sure if missing ax isn't already a list

    # add plan to program:
    add_plan(plan, asp_sim, sim)

    # execute to get feedback as list of fluents
    history_f = run_goal_gen(sim)
    # get relevant history
    test = hist_search(history_f, plan)

    # add history and obs to diagnostics program
    f = open(asp_diag, "r")
    contents = f.readlines()
    f.close()
    line_num = find_line_id("%&%& Received history:", contents)
    end_ln = find_line_id("%&%& End of history", contents)
    histln=[]
    #histln = [i[:-1] for i in test]
    histln = [i + '. \n' for i in test]
    # remove old history & add new history and plan
    contents_w = []
    contents_w = contents[0:line_num[0] + 1]
    #contents_w.extend(newln[0:])
    contents_w.extend(histln)
    contents_w.extend(contents[end_ln[0]:-1])
    # write to file
    f = open(diag, "w")
    contents_w = "".join(contents_w)
    f.write(contents_w)
    f.close()

    # execute to get feedback
    diag_out = jarwrapper(diag, '-A')
    diag_out = rm_header(diag_out)
    diag_out = out_to_list(diag_out)

    # 1. Was it correct?
    correct = 1 if 'success' in history_f else 0
    ex2PDK.correct.append(correct)
    # 2. What's the explanation for failure?
    ex2PDK.expl.append(diag_out)
    # 3. time of first failure

# is diag obs being written to correctly?

# # save data
# with open('results/partial_dk_results_1.pkl', 'wb') as output:
#     Pickle.dump(partial_dk, output, Pickle.HIGHEST_PROTOCOL)
#
# with open('results/complete_dk_results_1.pkl', 'wb') as output:
#     Pickle.dump(complete_dk, output, Pickle.HIGHEST_PROTOCOL)

# -------------------------------------------- #
#               Data analysis                  #
# -------------------------------------------- #

# load data if necessary
#with open('results/complete_dk_results_1.pkl', 'rb') as input:
#    complete_dk = pickle.load(input)

# make a data frame with non-impossible goals only
# i.e. discard cases where both ans. sets are empty.

# no of goals achieved

# histogram of plan length

# no of plans found for successful goals

# find relevant actors:
# hist_act = []
# # 1. Select all with relevant actors
# for item in history_f:
#     if any(s in item for s in actors):
#         hist_act.append(item)
# # 2. Select only the chosen fluents
# fluents = ["in_hand", "on", "location"]
# final_hist = []
# for item in hist_act:
#     if any(s in item for s in fluents):
#         final_hist.append(item)
# # 3. append all hpd's
# hpds = [i for i in history_f if 'hpd' in i]
# final_hist.extend(hpds)

#%%
from IPython import get_ipython
get_ipython().magic('reset -sf')

#%%

# Create Partial domain knowledge script and set deletion of information:
""" axiom type - aff. relations or exec. conditions,
n_del - number of axioms to delete
del_idx - (optional) list of axiom indices to delete"""
# read in complete file:
args = [[13]]
axiom_type = 'ar'
f = open(asp_diag, 'r')
script = f.readlines()

# get aff-rels. / exec. conds. (through flags)
ec_ln = find_line_id('%&%& E.c.:', script)
ec_end = find_line_id('%% AFFORDANCE AXIOMS END', script)
ar_ln = find_line_id('%&%& A.R.:', script)
ar_end = find_line_id('%&%& A.R. end', script)

exec_conds = script[ec_ln[0]:ec_end[0]]
aff_rels = script[ar_ln[0]:ar_end[0]]

# connect into 1:
exec_conds_s = "".join(exec_conds)
aff_rels_s = "".join(aff_rels)
# Regex by /d.
ec_ls = re.split('%\s\*', exec_conds_s)
aff_rel_ls = re.split('%\s(\d+\.)', aff_rels_s)

aff_IDs = []
aff_rule = []

for item in aff_rel_ls:
    if item[0].isdigit():
        aff_IDs.append(int(item[0:2]))
    else:
        aff_rule.append(item)

aff_rule = aff_rule[1:]

to_del_ar = []
to_del_ec = []

if axiom_type == 'ar':
    if args:
        to_del_ar = args[0]
    #else: to_del_ar = random.sample(self.ar_to_test, n_del)
elif axiom_type=='ec':
    if args:
        to_del_ec = args[0]
    #else: to_del_ec = random.sample(self.ec_to_test, n_del)

# write the rest to file (do they still have /n?)
ec_w = [l for i,l in enumerate(ec_ls) if not i in to_del_ec]
ar_w = [aff_rule[i] for (i,l) in enumerate(aff_IDs) if not l in to_del_ar]

ec_w = exec_conds_s #"".join(ec_w)
ar_w = "".join(ar_w)

# pdk = cdk + ec+w + lines inbetween + ar+w + remaining lines
pdk_script = script[0:ec_ln[0]]
pdk_script.extend(ec_w)
pdk_script.extend(script[ec_end[0]:ar_ln[0]])
pdk_script.extend(ar_w)
pdk_script.extend(script[ar_end[0]:])
pdk_script = "".join(pdk_script)
# this is the pdk domain.

f = open(diag, 'w')
f.write(pdk_script)
f.close()


from copy import deepcopy
import hashlib
import json
from inspect import signature as sig
from collections import OrderedDict
from zeus.common.config import Config
from zeus.common import ClassFactory, ClassType, SearchSpaceType

class Serializable(object):
    """Base class of Serializable."""

    def __new__(cls, *args, **kwargs):
        """Record params."""
        desc = {}
        params_sig = sig(cls.__init__).parameters
        param_names = list(params_sig.keys())
        if len(param_names) > len(args):
            # not dynamic parameter for connections
            for idx, arg in enumerate(args):
                arg_name = param_names[idx + 1]
                desc[arg_name] = arg
        if kwargs:
            desc.update(kwargs)
        instance = super(Serializable, cls).__new__(cls)
        instance._deep_level = 0
        instance._target_level = None
        if desc:
            instance.desc = Config(desc)
        return instance

    def to_desc(self, level=None):
        """Convert Module to desc dict."""
        raise NotImplementedError

    @classmethod
    def from_desc(cls, desc):
        """Create Model from desc."""
        raise NotImplementedError

    @property
    def md5(self):
        """MD5 value of network description."""
        return self.get_md5(self.to_desc(1))

    @classmethod
    def get_md5(cls, desc):
        """Get desc's short md5 code.

        :param desc: network description.
        :type desc: str.
        :return: short MD5 code.
        :rtype: str.

        """
        code = hashlib.md5(json.dumps(desc, sort_keys=True).encode('utf-8')).hexdigest()
        return code[:8]

    @property
    def model_name(self):
        """Return model name."""
        return self.__class__.__name__

    def define_props(self, key, default_value, dtype=None, params=None):
        """Define a prop and get value."""
        value = self.desc.get(key) or default_value
        return Props(key, value, dtype, params).value

class OperatorSerializable(Serializable):
    """Seriablizable for Operator class."""

    def to_desc(self, level=None):
        """Convert Operator to desc dict.

        :param level: Specifies witch level to convert. all conversions are performed as default.
        """
        desc = dict(type=self.model_name)
        desc.update(self.desc)
        return desc

    @classmethod
    def from_desc(cls, desc):
        """Create Operator class by desc."""
        return ClassFactory.get_instance(ClassType.NETWORK, desc)

class ModuleSerializable(Serializable):
    """Seriablizable Module class."""

    def to_desc(self, level=None):
        """Convert Module to desc dict.

        :param level: Specifies witch level to convert. all conversions are performed as default.
        """
        if level is not None:
            self._target_level = level
        if self._target_level and self._target_level == self._deep_level:
            desc = {'type': self.__class__.__name__}
            desc.update(self.desc)
            return desc
        desc = {'modules': [], "type": self.__class__.__name__}
        if self._losses:
            desc['loss'] = self._losses
        child_level = self._deep_level + 1
        for name, module in self.named_children():
            module._deep_level = child_level
            module._target_level = self._target_level
            sub_desc = module.to_desc()
            desc['modules'].append(name)
            desc[name] = sub_desc
        return desc

    def update_from_desc(self, desc):
        """Update desc according to desc."""
        for key, value in desc.items():
            if key == 'type' or not hasattr(self, key):
                continue
            child_module = getattr(self, key)
            if hasattr(child_module, 'add_module'):
                self.add_module(key, value)
            else:
                child_module.update_from_desc(value)

    @classmethod
    def from_desc(cls, desc):
        """Create Model from desc."""
        desc = deepcopy(desc)
        module_groups = desc.get('modules', [])
        module_type = desc.get('type', 'Sequential')
        loss = desc.get('loss')
        if 'props' in desc:
            Props.update(desc.pop('props'))
        modules = OrderedDict()
        for group_name in module_groups:
            module_desc = deepcopy(desc.get(group_name))
            if 'modules' in module_desc:
                module = cls.from_desc(module_desc)
            else:
                cls_name = module_desc.get('type')
                if not ClassFactory.is_exists(ClassType.NETWORK, cls_name):
                    raise ValueError("Network {} not exists.".format(cls_name))
                module = ClassFactory.get_instance(ClassType.NETWORK, module_desc)
            modules[group_name] = module
        if not module_groups and module_type:
            model = ClassFactory.get_instance(ClassType.NETWORK, desc)
        else:
            if ClassFactory.is_exists(SearchSpaceType.CONNECTIONS, module_type):
                connections = ClassFactory.get_cls(SearchSpaceType.CONNECTIONS, module_type)
            else:
                connections = ClassFactory.get_cls(SearchSpaceType.CONNECTIONS, 'Sequential')
            model = list(modules.values())[0] if len(modules) == 1 else connections(modules)
        if loss:
            model.add_loss(ClassFactory.get_cls(ClassType.LOSS, loss))
        return model

class Props(object):
    """Set proxy property in module.

    When a variable is changed by an external program, the value of the invoker can be changed synchronously.
    """

    _values = {}

    def __init__(self, key, default_value, d_type=None, params=None):
        self.key = key
        self.default_value = default_value
        self.d_type = d_type
        self.params = params
        self._add_prop()
        self._check()

    def _add_prop(self):
        if self.key in self._values and self._values.get(self.key):
            result = self._values.get(self.key)
        else:
            result = self.default_value
            self._values[self.key] = self.default_value
        return result

    @property
    def value(self):
        """Get values."""
        value = self._values.get(self.key)
        if self.d_type == ClassType.NETWORK:
            if isinstance(value, str):
                cls = ClassFactory.get_cls(ClassType.NETWORK, value)
                value = cls() if self.params is None else cls(**self.params)
            else:
                value = ClassFactory.get_instance(ClassType.NETWORK, value)
        return value

    @classmethod
    def update(cls, props):
        """Update props."""
        cls._values.update(props)

    def _check(self):
        if self.d_type is None:
            return

    def reset(self):
        """Reset props."""
        self._values = {}


import tensorflow as tf
from tensorflow import keras
import matplotlib.pyplot as plt
from tensorflow.keras.applications import ResNet101V2, ResNet50, ResNet50V2, DenseNet121, DenseNet201, ResNet152V2, VGG16, NASNetMobile, EfficientNetB3, VGG19, DenseNet169
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from keras.layers import Input
from sklearn.metrics import classification_report
import numpy as np

train_dir = '/trainingsetdirectory'

validation_dir = '/validationdirectory'

test_dir = '/testdirectory'

batch_size = 32
num_classes = 331

hyperparameters = {
    'learning_rate':[0.1, 0.01],
    'decay':[1e-2, 1e-4, 1e-6, 1e-8],
    'color':['grayscale','rgb'],
    'size':[75,150,224]
}

for color in hyperparameters['color']:
    for size in hyperparameters['size']:
        if color in 'grayscale':
            channel = 1
        else:
            channel = 3
        input_shape = Input(shape=(size, size, channel))
        cnn_models = [
            VGG16(include_top=True, weights=None, input_shape=input_shape, pooling='max', classes=num_classes),
            ResNet50V2(include_top=True, weights=None, input_shape=input_shape, pooling='avg', classes=num_classes),
            ResNet101V2(include_top=True, weights=None, input_shape=input_shape, pooling='avg', classes=num_classes),
            ResNet152V2(include_top=True, weights=None, input_shape=input_shape, pooling='avg', classes=num_classes),
            DenseNet121(include_top=True, weights=None, input_shape=input_shape, pooling='avg', classes=num_classes),
            DenseNet169(include_top=True, weights=None, input_shape=input_shape, pooling='avg', classes=num_classes),
            DenseNet201(include_top=True, weights=None, input_shape=input_shape, pooling='avg', classes=num_classes),
            EfficientNetB3(include_top=True, weights=None, input_shape=input_shape, pooling='avg', classes=num_classes)
        ]
        for model in cnn_models:
            for lr in hyperparameters['learning_rate']:
                for decay in hyperparameters['decay']:
                    train_data = ImageDataGenerator(rescale=1.0 / 255)
                    train_data = train_data.flow_from_directory(directory=train_dir,
                                                                target_size=(size, size),
                                                                batch_size=batch_size,
                                                                class_mode='categorical',
                                                                color_mode=color
                                                                )

                    validation_data = ImageDataGenerator(rescale=1.0/255)
                    validation_data = validation_data.flow_from_directory(
                        directory=validation_dir,
                        target_size=(size, size),
                        batch_size=batch_size,
                        class_mode='categorical',
                        color_mode=color
                    )

                    path = '/savemodeldir/' + '_color_' + str(color) + '_size_' + str(size) + '_model_' + str(model.name) + '_lr_' + str(lr) + '_decay_' + str(decay)

                    es = tf.keras.callbacks.EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=60)
                    save = tf.keras.callbacks.ModelCheckpoint(path+'.h5', monitor='val_loss', mode='min', save_best_only=True)
                    callback = [save, es]
                    opt = keras.optimizers.SGD(learning_rate=lr, momentum=0.9, decay=decay)

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

                    history = model.fit(train_data,
                                        validation_data=validation_data,
                                        epochs=200
                                        ,
                                        callbacks=callback
                                        )

                    model.save(path+'.h5')

                    plt.figure(figsize=(20, 5))
                    plt.subplot(1, 2, 1)
                    plt.plot(history.history['accuracy'], 'black')
                    plt.plot(history.history['val_accuracy'], 'gray')
                    plt.legend(['train_accuracy', 'validation_accuracy'], loc='lower right')

                    plt.subplot(1, 2, 2)
                    plt.plot(history.history['loss'], 'black')
                    plt.plot(history.history['val_loss'], 'gray')
                    plt.legend(['train_loss', 'validation_loss'], loc='upper right')
                    # plt.show()
                    plt.savefig(path+'.png')
    test_data = ImageDataGenerator(rescale=1.0 / 255)
    test_data = test_data.flow_from_directory(directory=test_dir,
                                              target_size=size,
                                              batch_size=batch_size,
                                              shuffle=False,
                                              class_mode='categorical',
                                              color_mode=color
                                              )
    scores = model.evaluate(test_data)
    Y_pred = model.predict_generator(test_data)
    classes = test_data.classes[test_data.index_array]
    y_pred = np.argmax(Y_pred, axis=-1)
    print(classification_report(y_pred, classes))
    a = model.history
    print(a)


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

# In[23]:

import json

train_folds = {}
for i in range(4):
    filename = f'fold-{i}'
    with open(filename, 'rt') as f:
        for l in f.readlines():
            d = json.loads(l)
            train_folds.update(d)

len(train_folds)

with open("pretrain_split.json", 'wt') as f:
    json.dump(train_folds, f)

with open('fold-4', 'rt') as f:
    cv_dict = json.load(f)

keys = list(cv_dict.keys())

from random import shuffle
shuffle(keys)

L = len(keys)//5
for j in range(5):
    fold_keys = keys[j * size: (j+1) * size]
    fold_dict = {}
    for k in fold_keys:
        fold_dict[k] = cv_dict[k]
    with open(f'cv_fold-{j}.json', 'wt') as f:
        json.dump(fold_dict, f)

# In[ ]:


def makefits(myimagebase, cleanup=True):
    impbcor(imagename=myimagebase+'.image', pbimage=myimagebase+'.pb', outfile=myimagebase+'.image.pbcor', overwrite=True) # perform PBcorr
    exportfits(imagename=myimagebase+'.image.pbcor', fitsimage=myimagebase+'.image.pbcor.fits', dropdeg=True, overwrite=True) # export the corrected image
    exportfits(imagename=myimagebase+'.psf', fitsimage=myimagebase+'.psf.fits', dropdeg=True, overwrite=True) # export the PSF image
    exportfits(imagename=myimagebase+'.model', fitsimage=myimagebase+'.model.fits', dropdeg=True, overwrite=True)
    exportfits(imagename=myimagebase+'.residual', fitsimage=myimagebase+'.residual.fits', dropdeg=True, overwrite=True)

    if cleanup:
        for suffix in ('psf', 'weight', 'sumwt', 'pb', 'model', 'residual',
                       'mask', 'image', 'workdirectory'):
            os.system('rm -rf {0}.{1}'.format(myimagebase, suffix))

cell='0.004arcsec' # cell size for imaging.
imsize = [512,512] # size of image in pixels.

#contvis = ['band6_continuum_ms{0}.ms'.format(ii) for ii in range(2)]
#if not os.path.exists(contvis[0]):
#    make_cont_ms()
#contvis = 'B6_calibrated_final_cont.ms'

restfreq_GHz = 215.59595

vis = 'band6.ms'
siovis = 'band6_siov1j5-4.ms'
if not os.path.exists(siovis):
    split(vis=vis, outputvis=siovis,
          spw='2:{0}~{1}GHz'.format(restfreq_GHz*(1-30/3e5),
                                    restfreq_GHz*(1+40/3e5),
                                   ),
          datacolumn='data'
         )

flagdata(vis=siovis, mode='manual', autocorr=True, uvrange='0~12m')

for imagename, depth, ii in [('Orion_SourceI_B6_SiO_v=1_J=5-4.noselfcal', '1Jy', 0),
                             ('Orion_SourceI_B6_SiO_v=1_J=5-4.selfcal.phase0', '300mJy', 1),
                             ('Orion_SourceI_B6_SiO_v=1_J=5-4.selfcal.phase1', '300mJy', 2),
                             ('Orion_SourceI_B6_SiO_v=1_J=5-4.selfcal.phase2', '200mJy', 3),
                             ('Orion_SourceI_B6_SiO_v=1_J=5-4.selfcal.phase3', '200mJy', 4),
                             ('Orion_SourceI_B6_SiO_v=1_J=5-4.selfcal.phase4', '100mJy', 5),
                             ('Orion_SourceI_B6_SiO_v=1_J=5-4.selfcal.phase5', '100mJy', 6),
                            ]:

    for suffix in ('', '.tt0', '.tt1', '.tt2'):
        for ext in ['.flux','.image','.mask','.model','.pbcor','.psf','.residual','.flux.pbcoverage','.pb','.wtsum']:
            todel = '{0}{1}{2}'.format(imagename, ext, suffix)
            if os.path.exists(todel):
                os.system('rm -rf {0}'.format(todel))

    tclean(vis=siovis,
           imagename=imagename,
           field='Orion_BNKL_source_I',
           specmode='cube',
           imsize = imsize,
           cell= cell,
           weighting = 'briggs',
           robust = -2,
           niter = int(1e5),
           threshold = '1.0Jy',
           interactive = False,
           outframe='LSRK',
           veltype='radio',
           savemodel='modelcolumn',
           uvrange='10~36000m',
           restfreq='{0}GHz'.format(restfreq_GHz),
           mask=['masercleancircle.crtf'],
          )

    makefits(imagename)

    rmtables(['sio_phase_{0}.cal'.format(ii)])
    gaincal(vis=siovis, caltable='sio_phase_{0}.cal'.format(ii), solint='int', gaintype='G',
            calmode='p')

    plotcal('sio_phase_{0}.cal'.format(ii), xaxis='time', yaxis='phase',
            iteration='antenna',
            subplot=331, timerange='2017/09/19/12:00:00~2017/09/20/12:00:00',
            figfile='sio_phase_{0}_vs_time.png'.format(ii))

    clearcal(vis=siovis)

    applycal(vis=siovis, gaintable=["phase_{0}.cal".format(ii)],
             interp="linear", applymode='calonly', calwt=False)


#!/usr/bin/env python
import copy

def split_train_override_patch(patch, train_parameter_list:list):
    """
    This function returns a list of tuples containing only those parameters that are in the train_parameter_list,
    and a second list of tuples with the overridden parameters
    :param patch: original patch - list of tuples
    :param train_parameter_list: list of trainable parameters (parameter IDs)
    :return: Two lists of tuples: overridden parameters, trainable parameters
    """

    # List of unique trainable parameters
    param_list = set(train_parameter_list)

    # Create deep copies from original patch as to not edit original patch
    temp_trainable = copy.deepcopy(patch)
    temp_overridden = copy.deepcopy(patch)

    # Remove trainable parameters from list of overridden parameters (from last to first)
    for element in sorted(param_list, reverse=True):
        temp_overridden.pop(element)

    override_parameter_tuples = temp_overridden
    train_parameter_tuples = list(set(temp_trainable)-set(override_parameter_tuples))
    return override_parameter_tuples, train_parameter_tuples


import io
from celery import task
from django.contrib.auth.models import User
from django.core.files.base import ContentFile
from django.core.mail import send_mail
from .models import Plot
from . import Mathparse
import matplotlib
matplotlib.use('Agg')
from matplotlib import pyplot
from PIL import Image, ImageDraw, ImageFont

@task
def createPlotinAdmin(equation,left,right,step,user_id):
    if (left != '' and right != '' and step != ''):
        data, data_x, ok = Mathparse.mainMathParse(equation, float(left), float(right), float(step))
    else:
        data, data_x, ok = Mathparse.mainMathParse(equation)
    if(ok == ""):
        fig, ax = pyplot.subplots()  # Получаю 2 области, области внутри осей и за осями
        ax.set(facecolor='#191930')
        ax.tick_params(axis='both', which='minor', labelcolor='#00CED1')
        pyplot.plot(data,data_x, color='#00CED1')
        pyplot.grid(color='w', linewidth=0.5)
        buf = io.BytesIO()
        fig.savefig(buf, format="png", facecolor='#38486F')
        figure = Plot()
        image_name = "plot.png"
        figure.image = ContentFile(buf.getvalue(),image_name)
        figure.user = User.objects.get(id = user_id)
        figure.function = equation
        figure.interval = step
        figure.step = step
        figure.save()
    else:
        W, H = (300, 120)
        img = Image.new("RGB", (W, H), (255, 255, 255))
        idraw = ImageDraw.Draw(img)
        text = "High Tatras"
        font = ImageFont.truetype("arial.ttf", size=18)
        w, h = idraw.textsize(text, font=font)
        idraw.text(((W - w) / 2, (H - h) / 2), text, font=font, fill=(0, 0, 0))

        buf = io.BytesIO()
        img.savefig(buf, format="png", facecolor='#38486F')
        figure = Plot()
        image_name = "plot.png"
        figure.image = ContentFile(buf.getvalue(), image_name)
        figure.user = User.objects.get(id=user_id)
        figure.function = equation
        figure.interval = step
        figure.step = step
        figure.save()
    return 1

#celery -A <module> worker -l info -P eventlet


from tkinter import *

expression = ""
def press(char):
    global expression
    expression = expression + str(char)
    equation.set(expression)

def equalpress():
    global expression
    try:
        total = str(eval(expression))
        equation.set(total)
        expression = ""

    except:
        total = "Error"
        equation.set(total)
        expression = ""

def clrscr():
    global expression
    expression = ""
    equation.set(expression)

if __name__=="__main__":
    root = Tk()
    root.title("Simple Calculator")
    root.geometry("300x150")
    equation = StringVar()
    equation.set("Enter your expression")
    lab1 = Entry(root,textvariable=equation)
    lab1.pack(fill = 'x')

    #result.pack(fill ='y', ipadx=10)
    button1 = Button(root,text='1',command=lambda:press(1),height=1, width=4)
    button1.grid(row=1,column=0)
    button2 = Button(root,text='2',command=lambda:press(2),height=1, width=4)
    button2.grid(row=1,column=1)
    button3 = Button(root,text='3',command=lambda:press(3),height=1, width=4)
    button3.grid(row=1,column=2)
    button4 = Button(root,text='4',command=lambda:press(4),height=1, width=4)
    button4.grid(row=1,column=3)
    result = Button(root,text='=',command=equalpress,height=6, width=4)
    result.grid(row=1,column=4)
    button5 = Button(root,text='5',command=lambda:press(5),height=1, width=4)
    button5.grid(row=3,column=0)
    button6 = Button(root,text='6',command=lambda:press(6),height=1, width=4)
    button6.grid(row=3,column=1)
    button7 = Button(root,text='7',command=lambda:press(7),height=1, width=4)
    button7.grid(row=3,column=2)
    button8 = Button(root,text='8',command=lambda:press(8),height=1, width=4)
    button8.grid(row=3,column=3)
    button9 = Button(root,text='9',command=lambda:press(9),height=1, width=4)
    button9.grid(row=4,column=0)
    button10 = Button(root,text='0',command=lambda:press(0),height=1, width=4)
    button10.grid(row=4,column=1)
    button11 = Button(root,text='.',command=lambda:press('.'),height=1, width=4)
    button11.grid(row=4,column=2)
    clear = Button(root,text='Clear',command=clrscr,height=1, width=4)
    clear.grid(row=4,column=3)
    plus = Button(root,text='+',command=lambda:press(text),height=1, width=4)
    plus.grid(row=5,column=0)
    minus = Button(root,text='-',command=lambda:press('-'),height=1, width=4)
    minus.grid(row=5,column=1)
    multiply = Button(root,text='*',command=lambda:press('*'),height=1, width=4)
    multiply.grid(row=5,column=2)
    div = Button(root,text='/',command=lambda:press('/'),height=1, width=4)
    div.grid(row=5,column=3)

    root.mainloop()


# Определяет схемы URL для learning_logs.

from django.urls import path
from . import views

app_name = 'learning_logs'
urlpatterns = [
    # Home page
    path('', views.index, name='index'),
    # Страница со списком всех тем
    path('topics/', views.topics, name='topics'),
    # Страница с подробной информацие по отдельной теме
    path('topics/<int:topic_id>/', views.topic, name='topic'),
    # Страница для добавления новой темы
    path('new_topic/', views.new_topic, name='new_topic'),
    # Страница для добавления новой записи
    path('new_entry/<int:topic_id>/', views.new_entry, name='new_entry'),
    path('edit_entry/<int:entry_id>/', views.edit_entry, name='edit_entry'),
    # Страница пользователя
    path('profile/', views.profile, name='profile'),
]


import sys
import numpy as np
import math
import random
import matplotlib.pyplot as plt
import statistics

import gym
import gym_race
from QLearning import QLearningAgent
from Sarsa import SarsaAgent

class Game():
    def __init__(self):
        self.env = gym.make("Pyrace-v0")
        self.numOfTraining = 9999999
        self.numOfStep = 2000
        self.numOfAction = self.env.action_space.n
        self.minEpsilon = 0.005
        self.stateDRange = 11

    def playQLearning(self, alpha, epsilon, gamma, decayFactor, trainingThreshold):
        algoTitle = "Q-Learning"
        trainingRewards = []
        meanRewardArray = []
        reachedGoalFirst = False # flag for the status that the agent has beat the game for the first time
        reachedGoalFirstENum = 0 # the the episode number the agent first reached the goal
        reachedGoalStable = False # flag for the status that the agent has learned to continuously beat the game

        # initialize UI window
        self.env.set_view(True)

        # initialize a q-learning agent
        qLearningAgent = QLearningAgent(gamma, alpha, epsilon, self.numOfAction, self.stateDRange)

        for episode in range(self.numOfTraining):
            trainingRewards, meanRewardArray, reachedGoalStable = self.updateRewardPlot(trainingRewards, meanRewardArray,
                                                                 qLearningAgent.getTotalReward(), reachedGoalStable)

            # agent has completed training and plot the second graph
            if reachedGoalStable:
                self.plotGraph(trainingRewards, episode-1, algoTitle)
                print("=======================================================")
                print(algoTitle + " training completed!")
                print("First time reaching the goal: Episode " + str(reachedGoalFirstENum))
                print("Time point of training completion: Episode " + str(episode-1))
                return

            # initialize a starting state for each new episode
            state = tuple(self.env.reset())

            # reset totalReward to 0 for each new training episode
            qLearningAgent.resetTotalReward()

            for t in range(self.numOfStep):
                # get the next action
                action = qLearningAgent.getAction(state)
                observation, reward, trainingCompleted, garbage = self.env.step(action)
                nextState = tuple(observation) # format nextState

                # update total reward in the current training episode
                qLearningAgent.updateTotalReward(reward)

                # update q-value with (s,a,r,s')
                qLearningAgent.update(state, action, nextState, reward)

                # update state for the next iteration
                state = nextState

                # hide UI during initial training
                if episode >= trainingThreshold:
                    self.env.render()

                # end the episode if the agent crashed or has exceeded the max number of steps allowed
                if trainingCompleted or t == self.numOfStep - 1:
                    self.displayEpisodeSummary(episode, t, qLearningAgent.getTotalReward())
                    break

            # plot the 1st graph - if it is the first time the agent has reached the goal
            if qLearningAgent.getTotalReward() == 10000 and not reachedGoalFirst:
                self.plotGraph(trainingRewards, episode, algoTitle)
                reachedGoalFirst = True
                reachedGoalFirstENum = episode

            # update epsilon for the next episode
            # qLearningAgent.updateEpsilon(self.minEpsilon, episode)
            qLearningAgent.updateEpsilon(self.minEpsilon, decayFactor)

    def playSarsa(self, alpha, epsilon, gamma, decayFactor, trainingThreshold):
        algoTitle = "Sarsa"
        trainingRewards = []
        meanRewardArray = []
        reachedGoalFirst = False  # flag for the status that the agent has beat the game for the first time
        reachedGoalFirstENum = 0  # the the episode number the agent first reached the goal
        reachedGoalStable = False  # flag for the status that the agent has learned to continuously beat the game

        # initialize UI window
        self.env.set_view(True)

        # initialize a q-learning agent
        sarsaAgent = SarsaAgent(gamma, alpha, epsilon, self.numOfAction, self.stateDRange)

        for episode in range(self.numOfTraining):
            trainingRewards, meanRewardArray, reachedGoalStable = self.updateRewardPlot(trainingRewards, meanRewardArray,
                                                                 sarsaAgent.getTotalReward(), reachedGoalStable)

            # agent has completed training
            if reachedGoalStable:
                self.plotGraph(trainingRewards, episode-1, algoTitle)
                print("=======================================================")
                print(algoTitle + " training completed!")
                print("First time reaching the goal: Episode " + str(reachedGoalFirstENum))
                print("Time point of training completion: Episode " + str(episode-1))
                return

            # initialize a starting state for each new episode
            state = tuple(self.env.reset())

            # reset totalReward to 0 for each new training episode
            sarsaAgent.resetTotalReward()

            action = sarsaAgent.getAction(state)
            for t in range(self.numOfStep):
                # get the next action
                observation, reward, trainingCompleted, garbage = self.env.step(action)
                nextState = tuple(observation) # format nextState

                # update total reward in the current training episode
                sarsaAgent.updateTotalReward(reward)

                # get the next Action
                nextAction = sarsaAgent.getAction(nextState)

                # update q-value with (s,a,r,s')
                sarsaAgent.update(state, action, nextState, reward, nextAction)

                # update state and action for the next iteration
                state = nextState
                action = nextAction

                # hide UI during initial training
                if episode >= trainingThreshold:
                    self.env.render()

                # end the episode if the agent crashed or has exceeded the max number of steps allowed
                if trainingCompleted or t == self.numOfStep - 1:
                    self.displayEpisodeSummary(episode, t, sarsaAgent.getTotalReward())
                    break

            # plot the 1st graph - if it is the first time the agent has reached the goal
            if sarsaAgent.getTotalReward() == 10000 and not reachedGoalFirst:
                self.plotGraph(trainingRewards, episode, algoTitle)
                reachedGoalFirst = True
                reachedGoalFirstENum = episode

            # update epsilon for the next episode
            sarsaAgent.updateEpsilon(self.minEpsilon, decayFactor)

    # def saveTable(self, q_table):
    #     self.q_values = q_table

    # plot graph
    def plotGraph(self, rewards, episode, algoTitle):
        plt.plot(rewards)
        plt.ylabel('Episode Rewards')
        plt.xlabel('Episode Units (x10)')
        plt.title(algoTitle + " - " + str(episode) + " Episode")
        plt.show()

    # display the summarized result of a episode
    def displayEpisodeSummary(self, episode, t, r):
        print("Episode %d || Time steps: %i | Total reward: %f" % (episode, t, round(r, 2)))

    # update overall training reward for graph plotting
    def updateRewardPlot(self, trainingRewards, meanRewardArray, r, reachedGoalStable):
        meanRewardArray.append(r)
        if len(meanRewardArray) == 10:
            mean = statistics.mean(meanRewardArray)
            if mean == 10000:
                reachedGoalStable = True
            trainingRewards.append(mean)
            meanRewardArray = []
        return (trainingRewards, meanRewardArray, reachedGoalStable)

if __name__ == "__main__":
    alpha = 0.15
    epsilon = 1
    gamma = 0.99
    decayFactor = 0.99
    trainingThreshold = 999999 # initial training to be completed fast (without UI)

    game = Game()
    game.playQLearning(alpha, epsilon, gamma, decayFactor, trainingThreshold)
    # game.playSarsa(alpha, epsilon, gamma, decayFactor, trainingThreshold)


import jieba
import tqdm
f = open("./wiki_zh_10_sim.txt","r",encoding='UTF-8')
lines = f.readlines()
result = open('results.txt', 'w', encoding='UTF-8')
jieba.load_userdict('./test.txt')
#读取全部内容 ，并以列表方式返回
for line in lines:
    words = jieba.lcut(line)
    result.write('  '.join(words))
result.close()


from tkinter import *
from tkinter import ttk

import windows
import xmlParser
import re

class addItems(object):
    def __init__(self, root):
        self.window = Toplevel(root)
        self.window.wm_title("Paste types")
        self.window.grab_set()

        self.mods = windows.getMods()
        self.addInfoFrame = Frame(self.window)
        self.addInfoFrame.grid(row=2, pady=3, sticky="w")
        Label(self.addInfoFrame, text="Enter Mod name:").grid(row=0, column=0, padx=3, pady=3)
        self.modSelector = ttk.Combobox(self.addInfoFrame, values=self.mods)
        self.modSelector.grid(row=0, column=1)

        self.useNewVal = IntVar()
        self.useNewVal.set(0)

        Checkbutton(self.addInfoFrame, text="Use Values of Duplicate Items", variable=self.useNewVal) \
            .grid(row=0, column=2, padx=10)

        self.text = Text(self.window)
        self.text.grid(row=1, padx=3, pady=3)

        self.buttons = Frame(self.window)
        self.buttons.grid(row=3, sticky="w")
        Button(self.buttons, text="OK", height=1, width=10, command=self.confirm) \
            .grid(padx=10, pady=10)

        windows.center(self.window)
        self.window.wait_window()

    def confirm(self):
        selectedMod = re.sub(r"[^a-zA-Z0-9]+", ' ', self.modSelector.get()).strip()
        if selectedMod == "":
            selectedMod = self.mods[0]

        text = self.text.get(1.0, END).strip()

        err = xmlParser.checkIfTypesXML(text)
        if err == 1:
            windows.showError(self.window, "Beginning of type is wrong", "type has to start with <type ")

        if err == 2:
            windows.showError(self.window, "End of type is wrong", "Ending is wrong,\n"
                                                                   "Block possibly not closed")
        if err == 0:
            if text.startswith("<type n"):
                types = "<types>\n  " + text + "\n</types>"
            # Outsource this to own logic
            windows.appendTypesToDatabase(types, self.window, selectedMod, True if self.useNewVal.get() == 1 else False)

        self.window.destroy()

def testWindow():
    window = Tk()
    addItems(window)
    window.mainloop()


import tkinter as tk
from tkinter import ttk, filedialog
from tkinter import messagebox
import os
from datetime import datetime
import winsound

fileName = ""  # Variável para armazenar o nome do arquivo da agenda
fileDir = ""  # Variável para armazenar o diretório do arquivo da agenda

def atualizar_titulo():
    if fileName:
        root.title(f"TLC TaskSync 2023 - {fileName}")
    else:
        root.title("TLC TaskSync 2023")

def fechar_splash():
    janela_splash.destroy()

def mostrar_tela_principal():
    root.deiconify()
    fechar_splash()

# Funções para as opções do menu
def nova_agenda():
    global fileName
    fileName = ""
    resposta = messagebox.askquestion("Gerar Nova Agenda", "Deseja gerar uma nova agenda em branco?")
    if resposta == 'yes':
        for item in tabela.get_children():
            tabela.delete(item)

    atualizar_titulo()

def salvar_agenda():
    global fileName  # Utilize a variável global fileName
    global fileDir

    if not fileName:  # Se fileName estiver vazio
        filepath = filedialog.asksaveasfilename(defaultextension=".agn", filetypes=(("Agenda Files", "*.agn"), ("All Files", "*.*")))
        if filepath:
            fileName = os.path.basename(filepath)  # Atualiza o fileName com o nome do arquivo
            fileDir = os.path.dirname(filepath)
            print(fileDir)
            with open(filepath, 'w') as file:
                for item in tabela.get_children():
                    values = tabela.item(item)['values']
                    file.write(','.join(values) + '\n')
    else:
        # Se fileName não estiver vazio, salva por cima do arquivo existente
        filepath = os.path.join(fileDir, fileName)
        with open(filepath, 'w') as file:
            for item in tabela.get_children():
                values = tabela.item(item)['values']
                file.write(','.join(values) + '\n')
                print(filepath)

    atualizar_titulo()

def salvar_agenda_como():
    global fileName  # Utilize a variável global fileName

    filepath = filedialog.asksaveasfilename(defaultextension=".agn", filetypes=(("Agenda Files", "*.agn"), ("All Files", "*.*")))
    if filepath:
        fileName = os.path.basename(filepath)  # Atualiza o fileName com o nome do arquivo
        with open(filepath, 'w') as file:
            for item in tabela.get_children():
                values = tabela.item(item)['values']
                file.write(','.join(values) + '\n')

    atualizar_titulo()

def carregar_agenda():
    global fileName

    filepath = filedialog.askopenfilename(filetypes=(("Agenda Files", "*.agn"), ("All Files", "*.*")))
    fileName = os.path.basename(filepath)
    if filepath:
        # Limpar a tabela
        for item in tabela.get_children():
            tabela.delete(item)

        # Carregar dados do arquivo selecionado para a tabela
        with open(filepath, 'r') as file:
            for line in file:
                values = line.strip().split(',')
                tabela.insert("", "end", values=values)

    atualizar_titulo()

def sair():
    root.destroy()

def ajuda():
    # Lógica para fornecer ajuda
    pass

def sobre():
    janela_sobre = tk.Toplevel(root)
    janela_sobre.title("Sobre")
    janela_sobre.geometry("450x150")  # Defina o tamanho desejado
    janela_sobre.resizable(False, False)  # Impede o redimensionamento da janela

    largura_tela = root.winfo_screenwidth()
    altura_tela = root.winfo_screenheight()

    largura_janela = 450
    altura_janela = 150

    # Calcula a posição para centralizar a janela
    pos_x = (largura_tela - largura_janela) // 2
    pos_y = (altura_tela - altura_janela) // 2

    # Define a geometria da janela para que ela seja centralizada
    janela_sobre.geometry(f"{largura_janela}x{altura_janela}+{pos_x}+{pos_y}")

    # Carregar a imagem
    imagem = tk.PhotoImage(file="agenda.png")

    # Exibir a imagem à esquerda
    label_imagem = tk.Label(janela_sobre, image=imagem)
    label_imagem.image = imagem  # Mantém uma referência à imagem

    label_imagem.grid(row=0, column=0, padx=10, pady=10, rowspan=3)

    # Textos
    label_nome_app = tk.Label(janela_sobre, text="TLC TaskSync 2023", font=("Arial", 14, "bold"))
    label_nome_app.grid(row=0, column=1, padx=10, pady=10)

    label_desenvolvedor = tk.Label(janela_sobre, text="Desenvolvido por Tiago Custódio", font=("Arial", 12))
    label_desenvolvedor.grid(row=1, column=1, padx=10, pady=5)

    # Função para fechar a janela "Sobre"
    def fechar_janela():
        janela_sobre.destroy()

    # Botão OK para fechar a janela
    botao_ok = tk.Button(janela_sobre, text="OK", command=fechar_janela)
    botao_ok.grid(row=2, column=1, padx=10, pady=10)

# Função para adicionar a tarefa
def adicionar_tarefa():
    nome = entrada_nome.get()
    data = entrada_data.get()
    hora = entrada_hora.get()
    descricao = entrada_descricao.get()
    lembrete = "Sim" if ativar_lembrete.get() else "Não"

    if nome and data and hora and descricao:
        if validar_data(data) and validar_hora(hora):
            item = tabela.insert("", "end", values=(nome, data, hora, descricao, lembrete, "X"))
            entrada_nome.delete(0, "end")
            entrada_data.delete(0, "end")
            entrada_hora.delete(0, "end")
            entrada_descricao.delete(0, "end")
            ativar_lembrete.set(False)

            if lembrete == "Sim":
                agendar_lembrete(nome, data, hora, descricao)  # Agendar lembrete se estiver ativado
        else:
            messagebox.showwarning("Erro", "Formato de data ou hora inválido.")
    else:
        messagebox.showwarning("Erro", "Por favor, preencha todos os campos.")

def agendar_lembrete(nome, data, hora, descricao):
    # Formatando a data e hora para criar um objeto de data e hora
    data_hora_str = f"{data} {hora}"
    data_hora = datetime.strptime(data_hora_str, "%d/%m/%Y %H:%M")

    # Obtendo a diferença entre a data atual e a data agendada
    diferenca = (data_hora - datetime.now()).total_seconds()
    print(diferenca)
    # Agendando o lembrete para ser exibido quando a hora da tarefa chegar
    root.after(int(diferenca * 1000), exibir_lembrete, nome, data, hora, descricao)

def exibir_lembrete(nome, data, hora, descricao):
    # Check if the task still exists in the table
    task_exists = False

    for item in tabela.get_children():
        values = tabela.item(item)['values']
        if values[:4] == [nome, data, hora, descricao]:
            task_exists = True
            break

    if task_exists:
        popup = tk.Toplevel(root)
        popup.title("ALERTA DE TAREFA")
        popup.geometry("400x300")  # Set initial size of the window
        popup.resizable(False, False)  # Prevent resizing
        popup.attributes("-topmost", True)  # Ensure the popup stays on top

        # Calculate the position to center the popup
        x_pos = root.winfo_x() + (root.winfo_width() // 2) - (400 // 2)
        y_pos = root.winfo_y() + (root.winfo_height() // 2) - (300 // 2)

        popup.geometry(f"400x300+{x_pos}+{y_pos}")  # Center the popup window

        # Play system alert sound
        winsound.MessageBeep(winsound.MB_ICONEXCLAMATION)

        # Texts for the reminder
        label_grande = tk.Label(popup, text="ALERTA DE TAREFA", font=("Arial", 16, "bold"))
        label_grande.pack()

        label_nome = tk.Label(popup, text=f"Nome da Tarefa: {nome}")
        label_nome.pack()

        label_data = tk.Label(popup, text=f"Data: {data}")
        label_data.pack()

        label_hora = tk.Label(popup, text=f"Hora: {hora}")
        label_hora.pack()

        label_descricao = tk.Label(popup, text=f"Descrição: {descricao}")
        label_descricao.pack()

        # Button OK
        botao_ok = tk.Button(popup, text="OK", command=popup.destroy)
        botao_ok.pack()

        # Function to focus the window (make it come to the front) when the popup is displayed
        def focus_popup():
            popup.focus_force()

        # Schedule the popup to be shown
        root.after(0, focus_popup)
    else:
        print("Tarefa não encontrada", "A tarefa foi removida ou não existe mais na lista.")

# Função para ordenar as tarefas por data e horário
def ordenar_tarefas():
    # Função auxiliar para converter a data no formato 'dd/mm/yyyy' para um formato que possa ser ordenado corretamente
    def converter_data_formato(data):
        partes_data = data.split('/')
        return int(partes_data[2]), int(partes_data[1]), int(partes_data[0])

    tarefas = [(tabela.item(item)['values'][1], tabela.item(item)['values'][2], item) for item in tabela.get_children()]
    tarefas_ordenadas = sorted(tarefas, key=lambda x: (converter_data_formato(x[0]), int(x[1].replace(':', ''))))

    # Reinsere as tarefas na tabela na ordem classificada
    for index, (data, hora, item) in enumerate(tarefas_ordenadas):
        tabela.move(item, '', index)

# Função para verificar se a data está no formato correto
def validar_data(data):
    # Verifica se a data tem o formato dd/mm/yyyy e se o dia e mês estão dentro dos limites
    try:
        dia, mes, ano = map(int, data.split('/'))
        return 1 <= dia <= 31 and 1 <= mes <= 12
    except ValueError:
        return False

# Função para verificar se a hora está no formato correto
def validar_hora(hora):
    # Verifica se a hora tem o formato hh:mm e se a hora e minutos estão dentro dos limites
    try:
        horas, minutos = map(int, hora.split(':'))
        return 0 <= horas <= 23 and 0 <= minutos <= 59
    except ValueError:
        return False

# Função para excluir uma tarefa
def excluir_tarefa(event):
    coluna_clicada = tabela.identify_column(event.x)  # Identifica a coluna clicada
    print("Coluna clicada é " + coluna_clicada)
    if coluna_clicada == "#6":  # Verifica se a coluna é a de exclusão
        item = tabela.identify_row(event.y)  # Obtém o item (linha) selecionado
        tabela.delete(item)  # Remove o item selecionado da tabela

# Função para formatar data no formato dd/mm/yyyy enquanto o usuário digita
def formatar_data(event):
    data = entrada_data.get()
    if len(data) == 2 or len(data) == 5:
        entrada_data.insert(tk.END, '/')
    elif len(data) >= 10:
        entrada_data.delete(10, tk.END)  # Limite de 10 caracteres, exclui qualquer entrada adicional

# Função para formatar hora no formato hh:mm enquanto o usuário digita
def formatar_hora(event):
    hora = entrada_hora.get()
    if len(hora) == 2:
        entrada_hora.insert(tk.END, ':')
    elif len(hora) >= 5:
        entrada_hora.delete(5, tk.END)  # Limite de 5 caracteres, exclui qualquer entrada adicional

# Cria a janela principal
root = tk.Tk()
root.title("TLC TaskSync 2023")
root.iconbitmap('agenda.ico')

# Define as dimensões da janela e impede a maximização e redimensionamento
root.resizable(False, False)  # Impede o redimensionamento da janela

largura_janela = 1100
altura_janela = 450

largura_tela = root.winfo_screenwidth()
altura_tela = root.winfo_screenheight()

# Calcula a posição para centralizar a janela
pos_x = (largura_tela - largura_janela) // 2
pos_y = (altura_tela - altura_janela) // 2

# Define a geometria da janela para que ela seja centralizada
root.geometry(f"{largura_janela}x{altura_janela}+{pos_x}+{pos_y}")

# Configurações para desativar a janela principal temporariamente
root.withdraw()

# Cria a janela da Splash Screen
janela_splash = tk.Toplevel(root)
janela_splash.overrideredirect(True)  # Remove a barra de título
janela_splash.geometry("300x200")  # Defina o tamanho desejado
janela_splash.configure(background='white')  # Define a cor de fundo desejada

# Configure as dimensões da Splash Screen
largura = 500
altura = 200
largura_tela = root.winfo_screenwidth()
altura_tela = root.winfo_screenheight()
x = (largura_tela - largura) // 2
y = (altura_tela - altura) // 2
janela_splash.geometry(f"{largura}x{altura}+{x}+{y}")

# Carregar a imagem "agenda.png"
imagem = tk.PhotoImage(file="agenda.png")
label_imagem = tk.Label(janela_splash, image=imagem)
label_imagem.image = imagem
label_imagem.pack(side="left")

# Texto TLC TaskSync 2023
label_nome_app = tk.Label(janela_splash, text="TLC TaskSync 2023", font=("Arial", 20, "bold"))
label_nome_app.pack()

# Texto Desenvolvido por Tiago Custodio
label_desenvolvedor = tk.Label(janela_splash, text="Desenvolvido por Tiago Custodio", font=("Arial", 12))
label_desenvolvedor.pack()

# Texto Please Wait
label_desenvolvedor = tk.Label(janela_splash, text="Please Wait....", font=("Arial", 8))
label_desenvolvedor.pack()

# Defina o tempo para exibir a tela de Splash e em seguida fechá-la
janela_splash.after(3000, mostrar_tela_principal)  # 500ms = 0,5 segundos

# Cria a barra de menus
barra_menu = tk.Menu(root)

# Menu Arquivo
menu_arquivo = tk.Menu(barra_menu, tearoff=0)
menu_arquivo.add_command(label="Novo", command=nova_agenda)
menu_arquivo.add_command(label="Salvar", command=salvar_agenda)
menu_arquivo.add_command(label="Salvar Como", command=salvar_agenda_como)
menu_arquivo.add_command(label="Carregar", command=carregar_agenda)
menu_arquivo.add_separator()
menu_arquivo.add_command(label="Sair", command=sair)
barra_menu.add_cascade(label="Arquivo", menu=menu_arquivo)

# Menu Editar
menu_editar = tk.Menu(barra_menu, tearoff=0)
# Aqui você pode adicionar opções para o menu Editar, se necessário
barra_menu.add_cascade(label="Editar", menu=menu_editar)

# Menu Ferramentas
menu_ferramentas = tk.Menu(barra_menu, tearoff=0)
# Aqui você pode adicionar opções para o menu Ferramentas, se necessário
barra_menu.add_cascade(label="Ferramentas", menu=menu_ferramentas)

# Menu Ajuda
menu_ajuda = tk.Menu(barra_menu, tearoff=0)
menu_ajuda.add_command(label="Ajuda", command=ajuda)
menu_ajuda.add_command(label="Sobre", command=sobre)
barra_menu.add_cascade(label="Ajuda", menu=menu_ajuda)

root.config(menu=barra_menu)

# Frame para campos de entrada
frame_campos = tk.Frame(root, padx=10, pady=10)
frame_campos.pack()

# Campos de entrada
tk.Label(frame_campos, text="Nome da Tarefa:").grid(row=0, column=0, sticky='w')
entrada_nome = tk.Entry(frame_campos)
entrada_nome.grid(row=0, column=1, padx=5, pady=5, sticky='w')

tk.Label(frame_campos, text="Data da Tarefa (dd/mm/yyyy):").grid(row=1, column=0, sticky='w')
entrada_data = tk.Entry(frame_campos)
entrada_data.grid(row=1, column=1, padx=5, pady=5, sticky='w')
entrada_data.bind('<KeyRelease>', formatar_data)

tk.Label(frame_campos, text="Horário da Tarefa (hh:mm):").grid(row=2, column=0, sticky='w')
entrada_hora = tk.Entry(frame_campos)
entrada_hora.grid(row=2, column=1, padx=5, pady=5, sticky='w')
entrada_hora.bind('<KeyRelease>', formatar_hora)

tk.Label(frame_campos, text="Descrição:").grid(row=3, column=0, sticky='w')
entrada_descricao = tk.Entry(frame_campos, width=45)
entrada_descricao.grid(row=3, column=1, padx=5, pady=5, sticky='w')

# Campo de checkbox para ativar lembrete
tk.Label(frame_campos, text="Ativar Lembrete:").grid(row=4, column=0, sticky='w')
ativar_lembrete = tk.BooleanVar()  # Variável para armazenar o estado do checkbox
checkbox_lembrete = tk.Checkbutton(frame_campos, variable=ativar_lembrete)
checkbox_lembrete.grid(row=4, column=1, sticky='w')

# Botão para adicionar a tarefa
botao_inserir = tk.Button(frame_campos, text="Inserir Tarefa", command=adicionar_tarefa)
botao_inserir.grid(row=5, column=1, padx=5, pady=5, sticky='w')

# Botão para ordenar as tarefas
botao_ordenar = tk.Button(frame_campos, text="Ordenar Tarefas", command=ordenar_tarefas)
botao_ordenar.grid(row=5, column=1, padx=5, pady=5)

# Frame para a tabela de tarefas
frame_tabela = tk.Frame(root, padx=10, pady=10)
frame_tabela.pack()

# Cria a tabela
colunas = ("Nome da Tarefa", "Data", "Horário", "Descrição", "Lembrete", "")
tabela = ttk.Treeview(frame_tabela, columns=colunas, show="headings")

# Define os títulos das colunas
for col in colunas:
    tabela.heading(col, text=col)
    tabela.column(col, anchor="center")

tabela.column("Data", width=90, anchor="center")
tabela.column("Horário", width=90, anchor="center")
tabela.column("Descrição", width=400, anchor="center")
tabela.column("Lembrete", width=100, anchor="center")
tabela.column("", width=50, anchor="center")  # Definindo a largura da coluna com os botões "X"

tabela.pack()

# Vincula a função para excluir à tabela
tabela.bind('<Button-1>', excluir_tarefa)

root.mainloop()


import numpy as np
import tensorflow as tf
import pandas as pd
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt

x_data = np.linspace(0.0, 10.0, 1000000)
noise = np.random.randn(len(x_data))

#y=mx+b

y_true = (0.5*x_data) + 5 + noise

x_df = pd.DataFrame(data=x_data, columns= ['X DATA'])
y_df = pd.DataFrame(data=y_true, columns= ['Y'])
my_data = pd.concat([x_df, y_df], axis=1) #concatenate along the columns side by side

# my_data.sample(n=250).plot(kind='scatter', x='X DATA', y="Y")

#feed batches of data (batch by batch)
batch_size = 8

m = tf.Variable(0.81)
b = tf.Variable(0.17)

#placeholders

xph = tf.placeholder(tf.float32, [batch_size])
yph = tf.placeholder(tf.float32, [batch_size])

#define the operation/ graph

y_model = m * xph + b

#loss function

error = tf.reduce_sum(tf.square(yph-y_model)) #tf.square is the same as squaring

optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.001)
train = optimizer.minimize(error)

init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    batches = 1000

    for i in range(batches):
        #choose the set of 8 random data for 1000 batches
        rand_index = np.random.randint(0, len(x_data), size= batch_size)
        feed = {xph: x_data[rand_index], yph: y_true[rand_index]}
        sess.run(train, feed_dict=feed)
    model_m, model_b = sess.run([m,b])
    print(model_m)
    print(model_b)

y_hat = x_data* model_m + model_b
# my_data.sample(n=250).plot(kind='scatter', x='X DATA', y="Y")
# plt.plot(x_data, y_hat, 'r')
# plt.show()

## ESTIMATOR API

# set up feature columns
feat_cols = [tf.feature_column.numeric_column('x',shape=[1])]

#Estimator model
estimator = tf.estimator.LinearRegressor(feature_columns=feat_cols)

x_train, x_eval, y_train, y_eval = train_test_split(x_data,y_true,test_size=0.3, random_state = 101)

#set up estimator inputs

# Can also do .pandas_input_fn
input_func = tf.estimator.inputs.numpy_input_fn({'x':x_train},y_train,batch_size=4,num_epochs=None,shuffle=True)

train_input_func = tf.estimator.inputs.numpy_input_fn({'x':x_train},y_train,batch_size=4,num_epochs=1000,shuffle=False)

eval_input_func = tf.estimator.inputs.numpy_input_fn({'x':x_train},y_train,batch_size=4,num_epochs=1000,shuffle=False)

## train the estimator

estimator.train(input_fn=input_func, steps=1000)

#evaluate

train_metrics = estimator.evaluate(input_fn=train_input_func, steps=1000)

eval_metrics = estimator.evaluate(input_fn=eval_input_func, steps=1000)

# Predict

brand_new_data = np.linspace(0, 10, 10)

input_fn_predict = tf.estimator.inputs.numpy_input_fn({'x':brand_new_data}, shuffle=False)

predictions = []

for pred in estimator.predict(input_fn=input_fn_predict):
    predictions.append(pred['predictions'])

#plot wrt the previous scatter plot data
my_data.sample(n=250).plot(kind='scatter',x='X DATA',y='Y')
plt.plot(np.linspace(0,10,10),predictions,'r')
plt.show()


from voting_app.models import *
from django.utils import timezone

def is_active_vote(vote):
    if vote.end_date < timezone.now() or timezone.now() < vote.start_date :
        return False
    if vote.vote_for_character_set.latest('votes_number').votes_number >= vote.votes_to_win:
        return False
    return True

def is_completed_vote(vote):
    return not (is_active_vote(vote)) and timezone.now() > vote.start_date

def active_votes():
    list_of_active_votes = []
    for vote in Vote.objects.all():
        if is_active_vote(vote):
            list_of_active_votes.append(vote)
    return list_of_active_votes

def completed_votes():
    list_of_completed_votes = []
    for vote in Vote.objects.all():
        if is_completed_vote(vote):
            list_of_completed_votes.append(vote)
    return list_of_completed_votes

def get_list_of_votes(type_of_vote = "active"):
    list_of_votes = []
    predicate = is_active_vote if type_of_vote == "active" else is_completed_vote
    for vote in Vote.objects.all():
        if predicate(vote):
            list_of_votes.append(vote)
    return list_of_votes


def Yes(input):
    agreements = {"ДА", "ЯВЛЯЕТСЯ", "ДА, ПРИНАДЛЕЖИТ", "ПРИНАДЛЕЖИТ", "СОГЛАСЕН", "УГУ", "АГА",
                  "ПРИНАДЛЕЖИТ", "ПРИНАДЛЕЖИТ T0", "ПРИНАДЛЕЖИТ T1", "ПРИНАДЛЕЖИТ S", "ПРИНАДЛЕЖИТ M", "ПРИНАДЛЕЖИТ L",
                  "СОХРАНЯЕТ 0", "СОХРАНЯЕТ НОЛЬ", "T0",
                  "СОХРАНЯЕТ 1", "СОХРАНЯЕТ ЕДИНИЦУ", "T1",
                  "САМОДВОЙСТВЕННАЯ", "S",
                  "ЛИНЕЙНАЯ", "L",
                  "МОНОТОННАЯ", "M"}
    if input in (agreements):
        return True
    else:
        return False

def No(input):
    disagreements = {"НЕТ", "НЕ ЯВЛЯЕТСЯ", "НЕТ, НЕ ПРИНАДЛЕЖИТ", "НЕ ПРИНАДЛЕЖИТ", "НЕ СОГЛАСЕН", "НЕ", "НЕА",
                     "НЕ ПРИНАДЛЕЖИТ T0", "НЕ ПРИНАДЛЕЖИТ T1", "НЕ ПРИНАДЛЕЖИТ S", "НЕ ПРИНАДЛЕЖИТ M",
                     "НЕ ПРИНАДЛЕЖИТ L",
                     "НЕ СОХРАНЯЕТ 0", "НЕ СОХРАНЯЕТ НОЛЬ", "НЕ T0",
                     "НЕ СОХРАНЯЕТ 1", "НЕ СОХРАНЯЕТ ЕДИНИЦУ", "НЕ T1",
                     "НЕ САМОДВОЙСТВЕННАЯ", "НЕ S",
                     "НЕ ЛИНЕЙНАЯ", "НЕ L",
                     "НЕ МОНОТОННАЯ", "НЕ M"}
    if input in (disagreements):
        return True
    else:
        return False

def Right(input):
    agreements = {"ДА", "СОГЛАСЕН", "УГУ", "АГА",
                  "ПРАВИЛЬНО", "ПРАВ", "Я ПРАВ", "Я ОТВЕТИЛ ПРАВИЛЬНО", "Я БЫЛ ПРАВ", "ОТВЕТИЛ ПРАВИЛЬНО"}
    if input in (agreements):
        return True
    else:
        return False

def Wrong(input):
    disagreements = {"НЕТ", "НЕ СОГЛАСЕН", "НЕ", "НЕА",
                     "НЕ РАВИЛЬНО", "НЕ ПРАВ", "Я НЕ ПРАВ", "Я ОТВЕТИЛ НЕПРАВИЛЬНО", "Я БЫЛ НЕ ПРАВ",
                     "ОТВЕТИЛ НЕПРАВИЛЬНО"}
    if input in (disagreements):
        return True
    else:
        return False

def Practice(input):
    practice = {"ПРАКТИКА", "ЗАДАЧИ", "РЕШАТЬ ЗАДАЧИ", "ПРАКТИКОВАТЬСЯ"}
    if input in (practice):
        return True
    else:
        return False

def Theory(input):
    theory = {"ТЕОРИЯ", "ОПРЕДЕЛЕНИЯ", "ВОПРОСЫ", "ИЗУЧАТЬ ТЕКОРИЮ"}
    if input in (theory):
        return True
    else:
        return False

def Exit(input):
    exit = {"ВЫХОД", "ЗАКАНЧИВАЕМ", "КОНЕЦ", "ВЫЙТИ", "ВЫХОДИМ", "МНЕ НАДОЕЛО"}
    if input in (exit):
        return True
    else:
        return False

def Ready(input):
    ready = {"ГТОВ", "Я ГОТОВ", "ДАВАЙ", "НАЧИНАЕМ"}
    if input in (ready):
        return True
    else:
        return False

def GoOn(input):
    ready = {"ПРОДОЛЖИМ", "ДАЛЕЕ", "ДАВАЙ", "НАЧИНАЕМ"}
    if input in (ready):
        return True
    else:
        return False


import random
import time
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--iterations", help="how many iterations the algorithm will be run", type=int, default=7)
args = parser.parse_args()
n=args.iterations

print("Fetching training images...")
time.sleep(1)
print("Fetching test images...")
time.sleep(1)
print("Fetching metadata from Spark...")
time.sleep(2)
print("Starting CNN training w/ TF")
time.sleep(1)
accuracy = 0
i=0
while i<n:
	increment = random.random()
	accuracy += increment/1000
	print("{\"step\":", i+1,", \"accuracy\":",accuracy," }")
	i+=1
	time.sleep(2*random.random())


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

sns.set(font_scale=1.6, style='whitegrid')

def resultplot(datasets, which="both"):
    """Creates the outcome plots for the feature importance scores, either for kmeans, fuzzy c means, or for both in the same plot.

    Args:
        datasets (list): List of dataset names
        which (str, optional): Has three modes, Kmeans, Fuzzycmean, or both. Defaults to "both".
    """
    # Formatting plot
    sns.set_style('whitegrid')
    paper_rc = {'lines.linewidth': 1, 'lines.markersize': 7}
    sns.set_context('paper', font_scale=1.8, rc=paper_rc)

    # For progress bar
    numberplots = 0
    totalplots = len(datasets)

    #Creation of plot by dataset
    for dataset in datasets:
        # loading the performance scores dataset
        df = pd.read_excel('./error_scores/error_scores_'+dataset+'.xlsx')
        df = df.iloc[:,1:]

        # Creating the lines in the plot
        if which == "Kmeans" or which == "both":
            sns.lineplot(x = range(0,len(df)), y=df["SHAP KMeans"], marker='o', markersize=10, label = "SHAP KMeans")
            sns.lineplot(x = range(0,len(df)), y=df["PFBI KMeans"], marker='o', markersize=10, label = "PBFI KMeans")
            plt.gca().fill_between(range(0,len(df)), df["SHAP KMeans"], df["PFBI KMeans"], alpha=0.2, color='grey')

        if which == "Fuzzycmeans" or which == "both":
            sns.lineplot(x = range(0,len(df)), y=df["SHAP FuzzyCMeans"], marker='D', markersize=10, label = "SHAP FuzzyCMeans")
            sns.lineplot(x = range(0,len(df)), y=df["PBFI FuzzyCMeans"], marker='D', markersize=10, label = "PBFI FuzzyCMeans")
            plt.gca().fill_between(range(0,len(df)), df["SHAP FuzzyCMeans"], df["PBFI FuzzyCMeans"], alpha=0.2, color='pink')

        # Creating the other items of the plot
        plt.tick_params(axis='x', labelsize=18)
        plt.tick_params(axis='y', labelsize=18)
        plt.xticks(np.arange(0, len(df), 1.0))

        plt.xlabel('rank', fontsize=18)
        plt.ylabel('performance', fontsize=18)

        # Saving the folder
        plt.savefig("./plots/"+dataset+which+'.pdf', bbox_inches='tight')
        numberplots +=1
        print(f"Completed figure {numberplots} out of {totalplots} total")
        plt.close()


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

# In[9]:

import pandas as pd
from datetime import datetime
from IPython.display import display, HTML

# In[10]:
# import os
# directory_in_str = os.getcwd() + '/Datasets'
# print(directory_in_str)
journeysDF = pd.DataFrame(columns = ['JourneyID','Route', 'StartTime','StartStop', 'Endtime', 'EndStop', 'Duration'])
# directory = os.fsencode(directory_in_str)

# for file in os.listdir(directory):
#     filename = os.fsdecode(file)

data = pd.read_csv("Datasets/siri.20121106.csv", names = ['Timestamp','LineID','Direction','JourneyPatternID','TimeFrame','VehicleJourneyID','Operator','Congestion','Long','Lat','Delay','BlockID ','VehicleID','StopID','AtStop'])

# In[11]:

vehicles = data.VehicleID.unique()

for v in vehicles:
    pings = data[data.VehicleID == v]
    output = pings[['LineID', 'Direction', 'JourneyPatternID', 'VehicleJourneyID', 'StopID', 'AtStop', 'Timestamp']].copy()
    output['TimeF'] = pings.Timestamp.apply(lambda x: datetime.utcfromtimestamp(x/1000000).strftime('%Y-%m-%d %H:%M:%S'))
    journeys = output.VehicleJourneyID.unique()
    o = pd.DataFrame(columns = ['JourneyID','Route', 'StartTime','StartStop', 'Endtime', 'EndStop', 'Duration', 'RawDuration'])
    for j in journeys:
        journey = output[output.VehicleJourneyID == j]
        route = journey.iloc[0].LineID
        startTime = journey.iloc[0].TimeF
        startStop = journey.iloc[0].StopID
        endTime = journey.iloc[-1].TimeF
        endStop = journey.iloc[-1].StopID
        x = journey.iloc[-1].Timestamp - journey.iloc[0].Timestamp
        duration = datetime.utcfromtimestamp(x/1000000).strftime('%H:%M:%S')
        journeysDF = journeysDF.append({'JourneyID': j, 'Route': route, 'StartTime': startTime, 'StartStop': startStop, 'Endtime': endTime, 'EndStop': endStop, 'Duration': duration, 'RawDuration' : x/1000000}, ignore_index = True)

journeysDF.to_csv("AllJourneys.csv")
            # In[12]:

            # display(HTML(journeysDF.head().to_html()))

            # In[15]:

            # print(journeysDF.shape[0])

            # In[ ]:


import json
import logging
import pycurl

from gi.repository import GObject

import fb_error
from fb_object import FbObject
import fb_types

class FbPhoto(FbObject):
    PHOTOS_URL = "https://graph.facebook.com/me/photos"
    COMMENTS_URL = "https://graph.facebook.com/%s/comments"

    __gsignals__ = {
        'photo-created': (GObject.SignalFlags.RUN_FIRST, None, ([str])),
        'photo-create-failed': (GObject.SignalFlags.RUN_FIRST, None, ([str])),
        'comment-added': (GObject.SignalFlags.RUN_FIRST, None, ([str])),
        'comment-add-failed': (GObject.SignalFlags.RUN_FIRST, None, ([str])),
        'comments-downloaded': (GObject.SignalFlags.RUN_FIRST, None, ([object])),
        'comments-download-failed': (GObject.SignalFlags.RUN_FIRST, None, ([str])),
        'likes-downloaded': (GObject.SignalFlags.RUN_FIRST, None, ([object])),
    }

    def create(self, image_path):
        GObject.idle_add(self._create, image_path)

    def add_comment(self, comment):
        self.check_created('add_comment')
        GObject.idle_add(self._add_comment, comment)

    def refresh_comments(self):
        """ raise an exception if no one is listening """
        self.check_created('refresh_comments')
        GObject.idle_add(self._refresh_comments)

    def check_created(self, method_name):
        if self.fb_object_id is None:
            errmsg = "Need to call create before calling %s" % (method_name)
            raise fb_error.FbObjectNotCreatedException(errmsg)

    def _add_comment(self, comment):
        url = self.COMMENTS_URL % (self.fb_object_id)

        response = []
        def write_cb(buf):
            response.append(buf)

        res = self._http_call(url, [('message', comment)], write_cb, True,
                              fb_types.FB_COMMENT)
        if res == 200:
            try:
                comment_id = self._id_from_response("".join(response))
                self.emit('comment-added', comment_id)
            except fb_error.FbBadCall as ex:
                self.emit('comment-add-failed', str(ex))
        else:
            logging.debug("_add_comment failed, HTTP resp code: %d" % (res))
            self.emit('comment-add-failed', "Add comment failed: %d" % (res))

    def _create(self, image_path):
        c = pycurl.Curl()
        params = [('source', (c.FORM_FILE, image_path))]

        response = []
        def write_cb(buf):
            response.append(buf)

        result = self._http_call(self.PHOTOS_URL, params, write_cb,
                                 True, fb_types.FB_PHOTO)
        if result == 200:
            photo_id = self._id_from_response("".join(response))
            self.fb_object_id = photo_id
            self.emit('photo-created', photo_id)
        else:
            logging.debug("_create failed, HTTP resp code: %d" % result)

            if result == 400:
                failed_reason = "Expired access token."
            elif result == 6:
                failed_reason = "Network is down."
                failed_reason += \
                    "Please connect to the network and try again."
            else:
                failed_reason = "Failed reason unknown: %s" % (str(result))

            self.emit('photo-create-failed', failed_reason)

    def _id_from_response(self, response_str):
        response_object = json.loads(response_str)

        if not "id" in response_object:
            raise fb_error.FbBadCall(response_str)

        fb_object_id = response_object['id'].encode('ascii', 'replace')
        return fb_object_id

    def _refresh_comments(self):
        """ this blocks """
        url = self.COMMENTS_URL % (self.fb_object_id)

        logging.debug("_refresh_comments fetching %s" % (url))

        response_comments = []
        def write_cb(buf):
            response_comments.append(buf)

        ret =  self._http_call(url, [], write_cb, False, fb_types.FB_COMMENT)
        if ret != 200:
            logging.debug("_refresh_comments failed, HTTP resp code: %d" % ret)
            self.emit('comments-download-failed',
                      "Comments download failed: %d" % (ret))
            return

        logging.debug("_refresh_comments: %s" % ("".join(response_comments)))

        try:
            response_data = json.loads("".join(response_comments))
            if 'data' not in response_data:
                logging.debug("No data inside the FB response")
                self.emit('comments-download-failed',
                          "Comments download failed with no data")
                return
        except Exception as ex:
            logging.debug("Couldn't parse FB response: %s" % str(ex))
            self.emit('comments-download-failed',
                      "Comments download failed: %s" % (str(ex)))
            return

        comments = []
        for c in response_data['data']:
            comment = {}  # this should be an Object
            comment['from'] = c['from']['name']
            comment['message'] = c['message']
            comment['created_time'] = c['created_time']
            comment['like_count'] = c['like_count']
            comment['id'] = c['id']
            comments.append(comment)

        if len(comments) > 0:
            self.emit('comments-downloaded', comments)
        else:
            self.emit('comments-download-failed', 'No comments found')


# comandos.py Contiene la logica de los comandos y sus nombres

import re
import sys
import json
import socket
import random
import config as cfg

COMANDOS = {
    "Timeout": r"robot",
    "Saludo": r"!hola",
    "Aleatorio": r"!aleatorio",
    "Terminar": r"!bye",
    "Fecha": r"!hora",
    "Adivina": r"!adivina",
    "NO_Adivina": r"!stop",
    "Abre_Cuenta": r"!abrircuenta",
    "Checa_Cuenta": r"!checar"
}

ARCHIVO_CUENTAS = "monedas.json"

def chat(sock, msg):
    """Manda mensaje al servidor de Twitch"""
    mensaje = "PRIVMSG {} :{} \r\n".format(cfg.CHAN, msg)
    print(mensaje)
    sock.send(mensaje.encode())

def ban(sock, user):
    """Banea a un usuario"""
    chat(sock, ".ban {}".format(user))

def timeout(sock, user, secs=600):
    """Da timeout a un usuario por el tiempo especificado"""
    mensaje = ".timeout {}".format(user, secs)
    chat(sock, mensaje)

def hola(sock, user):
    """Saluda a un usuario"""
    mensaje = "Bienvenido {0}!".format(user)
    chat(sock, mensaje)

def numero_magico(sock):
    """Regresa un numero aleatorio a un usuario"""
    numero = random.randint(1,11)
    mensaje = "Tu numero magico es: {0}".format(numero)
    chat(sock, mensaje)

def hora(sock):
    """Regresa la fecha a un usuario"""
    tiempo = datetime.datetime.now()
    chat(sock, tiempo)

def terminar(sock):
    """Exterminar el bot"""
    sys.exit()

def empieza_numero(sock):
    """Empieza una rifa"""
    numero = random.randint(1, 20)
    chat(sock, "Adivina el numero magico! (1 al 20)")
    return numero

def abre_cuenta(sock, usuario):
    """Usuario entra al sistema de ponejonedas"""
    data = {}

    with open(ARCHIVO_CUENTAS, 'r') as data_file:
        data = json.load(data_file)

    if usuario in data:
        chat(sock, "El usuario {0} ya tiene una cuenta.".format(usuario))
        return

    data[usuario] = 10

    with open(ARCHIVO_CUENTAS, 'w') as data_file:
        data_file.write(json.dumps(data, indent=2))

    chat(sock, "El usuario {0} ha abierto una cuenta".format(usuario))

def checa_cuenta(sock, usuario):
    """Checar cuanto hay en una cuenta"""
    data = {}

    with open(ARCHIVO_CUENTAS, 'r') as data_file:
        data = json.load(data_file)

    if usuario in data:
        chat(sock, "El usuario {0} tiene {1} ponejonedas.".format(usuario, data[usuario]))
    else:
        chat(sock, "El usuario {0} no tiene una cuenta.".format(usuario))