import os

import sys
import numpy as np
import cPickle as pickle
import argparse
import time
from subprocess import Popen, PIPE

from multiprocessing import Process, Queue

sys.path.append('./')
sys.path.append('../')

from environments import env_utils

import socket

import gc

def deleteIfExists(filename):
    if os.path.exists(filename):
        os.remove(filename)

def validSoln(state,soln,Environment):
    solnState = state
    for move in soln:
        solnState = Environment.next_state(solnState,move)

    return(Environment.checkSolved(solnState))

def fileListener(sock,heuristicFn,Environment):
    sock.listen(1)
    exampleState = np.expand_dims(Environment.generate_envs(1, [0, 0])[0][0],0)
    stateDim = exampleState.shape[1]

    maxBytes = 4096
    connection, client_address = sock.accept()
    while True:
        dataRec = connection.recv(8)
        while not dataRec:
            connection, client_address = sock.accept()
            dataRec = connection.recv(8)

        numBytesRecv = np.frombuffer(dataRec,dtype=np.int64)[0]

        #startTime = time.time()
        numBytesSeen = 0
        dataRec = ""
        while numBytesSeen < numBytesRecv:
            conRec = connection.recv(maxBytes)
            dataRec = dataRec + conRec
            numBytesSeen = numBytesSeen + len(conRec)

        states = np.frombuffer(dataRec,dtype=Environment.dtype)
        states = states.reshape(len(states)/stateDim,stateDim)
        #print("Rec Time: %s" % (time.time()-startTime))

        ### Run nnet
        #startTime = time.time()
        results = heuristicFn(states)
        #print("Heur Time: %s" % (time.time()-startTime))

        ### Write file
        #startTime = time.time()
        connection.sendall(results.astype(np.float32))
        #print("Write Time: %s" % (time.time()-startTime))

def dataListener(dataQueue,resQueue,gpuNum=None):
    nnet = nnet_utils.loadNnet(args.model_loc,args.model_name,useGPU,Environment,gpuNum=gpuNum)
    while True:
        data = dataQueue.get()
        nnetResult = nnet(data)
        resQueue.put(nnetResult)

parser = argparse.ArgumentParser()
parser.add_argument('--input', type=str, required=True, help="Name of input file")

parser.add_argument('--env', type=str, default='cube3', help="Environment: cube3, cube4")

parser.add_argument('--methods', type=str, default="kociemba,nnet", help="Which methods to use. Comma separated list")

parser.add_argument('--combine_outputs', action='store_true')

parser.add_argument('--model_loc', type=str, default="", help="Location of model")
parser.add_argument('--model_name', type=str, default="model.meta", help="Which model to load")

parser.add_argument('--nnet_parallel', type=int, default=200, help="How many to look at, at one time for nnet")
parser.add_argument('--depth_penalty', type=float, default=0.1, help="Coefficient for depth")
parser.add_argument('--bfs', type=int, default=0, help="Depth of breadth-first search to improve heuristicFn")

parser.add_argument('--startIdx', type=int, default=0, help="")
parser.add_argument('--endIdx', type=int, default=-1, help="")

parser.add_argument('--use_gpu', type=int, default=1, help="1 if using GPU")

parser.add_argument('--verbose', action='store_true', default=False, help="Print status to screen if switch is on")

parser.add_argument('--name', type=str, default="", help="Special name to append to file name")

args = parser.parse_args()

Environment = env_utils.getEnvironment(args.env)

if args.env.upper() == 'CUBE3':
    sys.path.append('./solvers/cube3/')
    from solver_algs import Kociemba
    from solver_algs import Optimal
elif args.env.upper() == 'CUBE4':
    sys.path.append('./solvers/cube4/')
    from solver_algs import Optimal
elif args.env.upper() == 'PUZZLE15':
    sys.path.append('./solvers/puzzle15/')
    from solver_algs import Optimal
elif args.env.upper() == 'PUZZLE24':
    sys.path.append('./solvers/puzzle24/')
    from solver_algs import Optimal

useGPU = bool(args.use_gpu)

methods = [x.lower() for x in args.methods.split(",")]
print("Methods are: %s" % (",".join(methods)))

### Load starting states
filename = args.input
outFileLoc_pre = "../testData/%s/%s_nnetPar%s_depthP%s_bfs%s_%s_solved" % (args.env.lower(),filename.split("/")[-1].split(".")[0],args.nnet_parallel,args.depth_penalty,args.bfs,"_".join(methods))

if args.endIdx != -1:
    outFileLoc_pre = "%s_%i_%i" % (outFileLoc_pre,args.startIdx,args.endIdx)

if args.name != "":
    outFileLoc_pre = "%s_%s" % (outFileLoc_pre,args.name)

outFileLoc = "%s.pkl" % (outFileLoc_pre)
print >> sys.stderr, "Input File: %s, Output File: %s" % (filename,outFileLoc)

inputData = pickle.load(open(filename,"rb"))

states = inputData['states']

if args.endIdx == -1:
    args.endIdx = len(states)

states = states[args.startIdx:args.endIdx]

### Load nnet if needed
if "nnet" in methods:
    from ml_utils import nnet_utils
    from ml_utils import search_utils

    os.environ["CUDA_VISIBLE_DEVICES"] = "0,1"
    if len(os.environ['CUDA_VISIBLE_DEVICES']) > 1:
        gpuNums = [int(x) for x in os.environ['CUDA_VISIBLE_DEVICES'].split(",")]
    else:
        gpuNums = [None]
    numParallel = len(gpuNums)

    ### Initialize files
    dataQueues = []
    resQueues = []
    for num in range(numParallel):
        dataQueues.append(Queue(1))
        resQueues.append(Queue(1))

        dataListenerProc = Process(target=dataListener, args=(dataQueues[num],resQueues[num],gpuNums[num],))
        dataListenerProc.daemon = True
        dataListenerProc.start()

    def heuristicFn_nnet(x):
        ### Write data
        parallelNums = range(min(numParallel,x.shape[0]))
        splitIdxs = np.array_split(np.arange(x.shape[0]),len(parallelNums))
        for num in parallelNums:
            dataQueues[num].put(x[splitIdxs[num]])

        ### Check until all data is obtaied
        results = [None]*len(parallelNums)
        for num in parallelNums:
            results[num] = resQueues[num].get()

        results = np.concatenate(results)

        return(results)

    socketName = "%s_socket" % (outFileLoc_pre)
    try:
        os.unlink(socketName)
    except OSError:
        if os.path.exists(socketName):
            raise

    sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
    sock.bind(socketName)

    fileListenerProc = Process(target=fileListener, args=(sock,heuristicFn_nnet,Environment,))
    fileListenerProc.daemon = True
    fileListenerProc.start()

### Get solutions
data = dict()
data["states"] = states
data["solutions"] = dict()
data["times"] = dict()
data["nodesGenerated_num"] = dict()
for method in methods:
    data["solutions"][method] = [None]*states.shape[0]
    data["times"][method] = [None]*states.shape[0]
    data["nodesGenerated_num"][method] = [None]*states.shape[0]

def runMethods(idx_state):
    idx, state = idx_state
    stateStr = " ".join([str(x) for x in state])
    #print(stateStr)
    for method in methods:
        start_time = time.time()
        if method == "kociemba":
            soln = Kociemba.solve(state)
            nodesGenerated_num = 0

            elapsedTime = time.time() - start_time
        elif method == "nnet":
            runType = "python"
            #if (args.env.upper() in ['CUBE3','PUZZLE15','PUZZLE24','PUZZLE35']) or ('LIGHTSOUT' in args.env.upper()):
            #    runType = "cpp"
            #else:
            #    runType = "python"
            if runType == "cpp":
                popen = Popen(['./ml_utils/parallel_weighted_astar',stateStr,str(args.depth_penalty),str(args.nnet_parallel),socketName,args.env], stdout=PIPE, stderr=PIPE, bufsize=1, universal_newlines=True)
                lines = []
                for stdout_line in iter(popen.stdout.readline, ""):
                    stdout_line = stdout_line.strip('\n')
                    lines.append(stdout_line)
                    if args.verbose:
                        sys.stdout.write("%s\n" % (stdout_line))
                        sys.stdout.flush()

                moves = [int(x) for x in lines[-3].split(" ")[:-1]]
                soln = [Environment.legalPlays[x] for x in moves][::-1]
                nodesGenerated_num = int(lines[-1])
            else:
                BestFS_solve = search_utils.BestFS_solve([state],heuristicFn_nnet,Environment,bfs=args.bfs)
                isSolved, solveSteps, nodesGenerated_num = BestFS_solve.run(numParallel=args.nnet_parallel,depthPenalty=args.depth_penalty,verbose=args.verbose)
                BestFS_solve = []
                del BestFS_solve
                gc.collect()

                soln = solveSteps[0]
                nodesGenerated_num = nodesGenerated_num[0]

            elapsedTime = time.time() - start_time
        elif method == "optimal":
            soln, nodesGenerated_num, elapsedTime = Optimal.solve(state)
        else:
            continue

        data["times"][method][idx] = elapsedTime
        data["nodesGenerated_num"][method][idx] = nodesGenerated_num

        assert(validSoln(state,soln,Environment))

        data["solutions"][method][idx] = soln

print("%i total states" % (states.shape[0]))
if 'optimal' in methods:
    solutions, nodesGenerated_num, times = Optimal.solve(states,args.startIdx,args.endIdx,args.combine_outputs)
    data["solutions"]['optimal'] = solutions
    data["times"]['optimal'] = times
    data["nodesGenerated_num"]['optimal'] = nodesGenerated_num
    for state,soln in zip(states,data["solutions"]['optimal']):
        if soln is not None:
            assert(validSoln(state,soln,Environment))
else:
    for idx,state in enumerate(states):
        runMethods((idx,state))

        solveStr = ", ".join(["len/time/#nodes - %s: %i/%.2f/%i" % (method,len(data["solutions"][method][idx]),data["times"][method][idx],data["nodesGenerated_num"][method][idx]) for method in methods])
        print >> sys.stderr, "State: %i, %s" % (idx,solveStr)

### Save data
pickle.dump(data, open(outFileLoc, "wb"), protocol=1)

### Print stats
for method in methods:
    solnLens = np.array([len(soln) for soln in data["solutions"][method]])
    times = np.array([solveTime for solveTime in data["times"][method]])
    nodesGenerated_num = np.array([solveTime for solveTime in data["nodesGenerated_num"][method]])
    print("%s: Soln len - %f(%f), Time - %f(%f), # Nodes Gen - %f(%f)" % (method,np.mean(solnLens),np.std(solnLens),np.mean(times),np.std(times),np.mean(nodesGenerated_num),np.std(nodesGenerated_num)))


from PyQt5 import QtWidgets, QtGui, QtCore
import sys, signal, os
import AlarmEventHandler
import AudioModule
########################## START BUILDING THE CLASS##########################

class GUI(QtWidgets.QWidget):
    def __init__(self):
        super(GUI, self).__init__()
        self.setWindowTitle('Alarm Clock')
        self.setGeometry(100, 100, 300, 210)
        self.setWindowIcon(QtGui.QIcon('alarm-clock-icon.png'))
        self.setStyleSheet('background-color: #00b300')
        self.setFixedSize(300, 210)
        self.show()
        self.all_widgets = []

    def add_time(self, from_time, to_time, x, y):
        time_ = QtWidgets.QComboBox(self)
        time_.setGeometry(x, y, 50, 50)
        time_.setStyleSheet('border: 2px solid black;'
                            'background-color: #33ccff;'
                            'border-radius: 5px;'
                            'font-size: 18px')
        for i in range(from_time, to_time):
            time_.addItem(format_with_zero(i))
        time_.show()
        self.all_widgets.append(time_)

    def add_label(self, name_, x, y, width=50):
        label = QtWidgets.QLabel(name_, self)
        label.setGeometry(x, y, width, 20)
        label.setStyleSheet('border: 2px solid black;'
                            'background-color: lightgreen;'
                            'border-radius: 5px;'
                            'font-size: 15px')
        label.show()
        label.setAlignment(QtCore.Qt.AlignCenter)
        self.all_widgets.append(label)

    def add_button(self,icon_path, text, x, y):
        btn = QtWidgets.QPushButton(self)
        btn.setGeometry(x, y, 50, 50)
        btn.setIcon(QtGui.QIcon(icon_path))
        btn.setIconSize(QtCore.QSize(40, 40))
        btn.show()
        btn.setStyleSheet('border-radius: 10px;\
                           border: 2px solid black;')

        self.all_widgets.append(btn)

    def add_button_event(self, btn, signal):
        btn.clicked.connect(signal)

    def add_ringtone_list(self):
        dd = QtWidgets.QComboBox(self)
        dd.setGeometry(95, 180, 110, 20)
        dd.addItem('Bird')
        dd.addItem('Bird 2')
        dd.addItem('Mario')
        dd.addItem('Rooster')
        dd.addItem('None')
        dd.setStyleSheet('border: 2px solid black;'
                            'background-color: #33ccff;'
                            'border-radius: 5px;'
                            'font-size: 15px')
        dd.show()
        self.all_widgets.append(dd)

    def closeEvent(self, QCloseEvent):
        pid = os.getpid()
        os.kill(pid, signal.SIGKILL)

def format_with_zero(str_):
    result = str(str_)
    if len(result) is 1:
        result = '0' + result
    return result

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    a_clock = GUI()
    a_clock.add_time(from_time=0, to_time=24, x=95, y=35)
    a_clock.add_time(from_time=0, to_time=60, x=155, y=35)

    a_clock.add_label(name_='HH' , x=95, y=10)
    a_clock.add_label(name_='MM', x=155, y=10)

    a_clock.add_button(icon_path='./Icon/play.png', text='Start', x=95, y=90)
    a_clock.add_button(icon_path='./Icon/stop.png', text='Stop', x=155, y=90)

    a_clock.add_label('Alarm Tone', 95, 150, width=110)
    a_clock.add_ringtone_list()

    a_clock.all_widgets[5].setEnabled(False)

    am = AudioModule.PlayTone(a_clock.all_widgets[7])
    a_clock.all_widgets[7].currentIndexChanged.connect(am.play)

    # Create Signals
    cm = AlarmEventHandler.ControllerMenu(a_clock)
    play = cm.play
    pause = cm.stop

    a_clock.add_button_event(a_clock.all_widgets[4], play)
    a_clock.add_button_event(a_clock.all_widgets[5], pause)

    sys.exit(app.exec_())


"""
async mode with rgb camera

save file in D:\mb95541\aeroplane\data\rgb
"""

try:
    from config import *
    from config_control import *
except ImportError:
    raise ImportError('cannot import config file')

def process_rgb(rgb):
    """
    process the image, update surface in pygame
    """
    global surface
    array = np.frombuffer(rgb.raw_data, dtype=np.dtype("uint8"))
    array = np.reshape(array, (rgb.height, rgb.width, 4))
    array = array[:, :, :3]
    array = array[:, :, ::-1]  # switch r,g,b
    array = array.swapaxes(0, 1)  # exchange the width and height
    surface = pygame.surfarray.make_surface(array)  # Copy an array to a new surface

    # rgb.save_to_disk('D:\\mb95541\\aeroplane\\data\\rgb\\%d' % rgb.frame)

def carla_main():
    # --- pygame show --- #
    pygame.init()
    display = pygame.display.set_mode((IMG_WIDTH, IMG_HEIGHT), pygame.HWSURFACE | pygame.DOUBLEBUF)
    font = get_font()
    clock = pygame.time.Clock()
    try:
        # --- client --- #
        client = carla.Client('localhost', 2000)
        client.set_timeout(10.0)

        # --- world and blueprint_library --- #
        world = client.get_world()
        settings = world.get_settings()
        settings.synchronous_mode = False  # Disables synchronous mode
        world.apply_settings(settings)
        blueprint_library = world.get_blueprint_library()
        # --- weather --- #
        world.set_weather(carla.WeatherParameters.ClearNoon)

        # --- start point --- #
        spawn_point = carla.Transform(carla.Location(x=250, y=275, z=3),
                                      carla.Rotation(pitch=0.000000, yaw=270.000, roll=0.000000))
        print('spawn_point:', spawn_point)

        # --- vehicle --- #
        vehicle_bp = generate_vehicle_bp(world, blueprint_library)
        vehicle = world.spawn_actor(vehicle_bp, spawn_point)
        vehicle.set_simulate_physics(True)
        actor_list.append(vehicle)

        # --- rgb-camera sensor --- #
        rgb_camera_bp = generate_rgb_bp(world, blueprint_library)
        rgb_spawn_point = carla.Transform(carla.Location(x=0.5, y=0.0, z=3),
                                      carla.Rotation(pitch=0.0, yaw=0.0, roll=0.0))
        rgb_camera = world.spawn_actor(rgb_camera_bp, rgb_spawn_point, attach_to=vehicle)
        rgb_camera.listen(lambda data: process_rgb(data))
        actor_list.append(rgb_camera)

        # --- controller --- #
        controller = KeyboardControl(vehicle)

        # --- Create a asynchronous mode context ---#
        while True:
            if should_quit():
                return
            clock.tick(60)
            # don't delete ! will crash if surface is None
            if not surface:
                continue
            controller.parse_events(clock)
            vehicle_velocity = get_speed(vehicle)
            display.blit(font.render('% 5d mk/h (velocity)' % vehicle_velocity, True, (0, 0, 0)), (8, 10))
            pygame.display.flip()
            display.blit(surface, (0, 0))

    finally:
        print('destroying actors')
        for actor in actor_list:
            actor.destroy()
        pygame.quit()
        print('done.')

if __name__ == '__main__':
    carla_main()


import yaml
from typing import Dict, Tuple, Union
from pprint import pprint
import os

def load_configuration(config_file: str) -> Dict[str,
                                                 Dict[str,
                                                      Union[str, int, float, bool, Tuple[int, int, int]]]]:
    """Load the default.yaml and override specific instance from the config_file.yaml

    Args:
        config_file: The config_file to load.

    Returns:
        Dictionary with 'model' and 'data' keys that both contain dictionaries of parameters.
    """
    parameters = yaml.load(open("/home/kay/projects/lumbar/configurations/default.yaml", "r"))
    parameters_experiment = yaml.load(open("/home/kay/projects/lumbar/configurations/%s.yaml" % config_file, "r"))

    for sub_key in parameters_experiment:
        for key in parameters_experiment[sub_key]:
            parameters[sub_key][key] = parameters_experiment[sub_key][key]

    return parameters

def load_model(config_file: str):
    from models.lfunetmulti import LfUNetMulti
    from models.lfunetsingle import LfUNetSingle
    import tensorflow as tf

    tf.reset_default_graph()
    parameters = load_configuration(config_file)

    model_parameters = parameters["model"]
    data_parameters = parameters["data"]

    model_type = model_parameters["type"]
    if model_type == "multi":
        return LfUNetMulti(name_ext=model_parameters["name_ext"],
                           resolution=data_parameters["resolution"],
                           nb_blocks=model_parameters["nb_blocks"],
                           nb_channels=model_parameters["nb_channels"],
                           learning_rate=model_parameters["learning_rate"],
                           keep_prob=model_parameters["keep_prob"],
                           bn_decay=model_parameters["bn_decay"],
                           init_schema=model_parameters["init_schema"],
                           optimizer=model_parameters["optimizer"],
                           learning_decay=model_parameters["learning_decay"],
                           supervision=model_parameters["supervision"],
                           activation=model_parameters["activation"],
                           activation_output=model_parameters["activation_output"],
                           batch_normalization=model_parameters["batch_normalization"],
                           channel_inc=model_parameters["channel_inc"],
                           res_skip=model_parameters["res_skip"],
                           long_skip=model_parameters["long_skip"],
                           max_dec_channels=model_parameters["max_dec_channels"],
                           channel_dec=model_parameters["channel_dec"],
                           loss_metric=model_parameters["loss_metric"],
                           loss_metric_weight=model_parameters["loss_metric_weight"],
                           prediction_dropout_hack=model_parameters["prediction_dropout_hack"])

    if model_type == "single":
        return LfUNetSingle(name_ext=model_parameters["name_ext"],
                            resolution=data_parameters["resolution"],
                            nb_blocks=model_parameters["nb_blocks"],
                            nb_channels=model_parameters["nb_channels"],
                            learning_rate=model_parameters["learning_rate"],
                            keep_prob=model_parameters["keep_prob"],
                            bn_decay=model_parameters["bn_decay"],
                            init_schema=model_parameters["init_schema"],
                            learning_decay=model_parameters["learning_decay"],
                            supervision=model_parameters["supervision"],
                            activation=model_parameters["activation"],
                            batch_normalization=model_parameters["batch_normalization"],
                            channel_inc=model_parameters["channel_inc"],
                            res_skip=model_parameters["res_skip"],
                            long_skip=model_parameters["long_skip"])

    else:
        raise ValueError("Unknown model type %s" % model_type)

def load_data_parameters(config_file: str):
    parameters = load_configuration(config_file)
    return parameters["data"]

def _get_configurations(path: str, net_type: str):
    return [(net_type + file_name, None) for file_name in [f.split('.')[0] for f in os.listdir(path)]]

def get_single_configurations():
    return _get_configurations(path="/home/kay/projects/lumbar/configurations/single/", net_type="single/")

def get_multi_configurations():
    return _get_configurations(path="/home/kay/projects/lumbar/configurations/multi/", net_type="multi/")

def get_all_configurations():
    return get_single_configurations() + get_multi_configurations()


N = int(input())
CifreGiustapposte = ''
if N != -1:
    TutteDecimali = bool(1)
    while N != -1:
        if N < 10 and N >= 0:
            CifreGiustapposte += str(N)
        else:
            TutteDecimali = bool(0)
        N = int(input())

    if TutteDecimali:
        print(CifreGiustapposte)
        if int(CifreGiustapposte) % 3 == 0:
            print('SI', end='')
        else:
            print('NO', end='')
    else:
        print('ERRORE', end='')
else:
    print('VUOTO', end='')


from django.contrib import admin
from django.utils.html import format_html

from . import models

def bust_cache(modeladmin, request, queryset):
    for assessment in queryset:
        assessment.bust_cache()
    message = f"Cache for {queryset.count()} assessment(s) busted!"
    modeladmin.message_user(request, message)

bust_cache.short_description = "Clear cache for selected assessments"

@admin.register(models.Assessment)
class AssessmentAdmin(admin.ModelAdmin):
    list_display = ("__str__", "get_managers", "get_team_members", "get_reviewers")
    list_per_page = 10
    list_filter = (
        "editable",
        "public",
    )

    search_fields = (
        "name",
        "project_manager__last_name",
        "team_members__last_name",
        "reviewers__last_name",
    )

    actions = (bust_cache,)

    def queryset(self, request):
        qs = super().queryset(request)
        return qs.prefetch_related("project_manager", "team_members", "reviewers")

    def get_staff_ul(self, mgr):
        ul = ["
"]
        for user in mgr.all():
            ul.append(f"
{user.first_name} {user.last_name}
")

        ul.append("
")
        return format_html(" ".join(ul))

    def get_managers(self, obj):
        return self.get_staff_ul(obj.project_manager)

    def get_team_members(self, obj):
        return self.get_staff_ul(obj.team_members)

    def get_reviewers(self, obj):
        return self.get_staff_ul(obj.reviewers)

    get_managers.short_description = "Managers"
    get_managers.allow_tags = True

    get_team_members.short_description = "Team"
    get_team_members.allow_tags = True

    get_reviewers.short_description = "Reviewers"
    get_reviewers.allow_tags = True

@admin.register(models.DoseUnits)
class DoseUnitsAdmin(admin.ModelAdmin):
    list_display = (
        "name",
        "animal_dose_group_count",
        "epi_exposure_count",
        "invitro_experiment_count",
    )

@admin.register(models.Species)
class SpeciesAdmin(admin.ModelAdmin):
    list_display = (
        "id",
        "name",
    )
    list_display_links = ("name",)

@admin.register(models.Strain)
class StrainAdmin(admin.ModelAdmin):
    list_select_related = ("species",)
    list_display = ("id", "name", "species")
    list_display_links = ("name",)
    list_filter = ("species",)

@admin.register(models.EffectTag)
class EffectTagAdmin(admin.ModelAdmin):
    list_display = ("name", "slug", "id")
    search_fields = ("name",)

@admin.register(models.TimeSpentEditing)
class TimeSpentEditingAdmin(admin.ModelAdmin):
    list_display = (
        "id",
        "seconds",
        "assessment",
        "content_type",
        "object_id",
        "content_object",
        "created",
    )
    search_fields = (
        "assessment",
        "content_type",
    )

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.list_display_links = []


import socket
import json
import sys

def dns(input):
    flag=0
    ##data stored locally
    file1 = open("data.json","r")
    localdata = json.loads(file1.read())
    file1.close()
    ##checking if the requested site's ip exists locally
    for key,val in localdata.items():
        if(input==key):
            addr=val
            flag=1
    ##if data not found locally geting the ip from dns server
    if(flag==0):
        addr = socket.gethostbyname(input)
        localdata[input] = addr            ##adding data to local data
    file1 = open("data.json","w")
    file1.write(json.dumps(localdata))
    file1.close()
    return(addr)

s = socket.socket()
port = int(sys.argv[1])
print("Socket created")
s.bind(('',port))
print("socket binded to port",port)
s.listen(5)
print("Socket is listening")

while True:
    c,addr = s.accept()
    print("Connection received")
    data = c.recv(1024).decode()
    print(data)
    result=str(dns(data))
    print(result)
    c.send(bytes("".join(result),"utf-8"))
    c.close()
exit()


# import libraries
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

# change the default plotting style to something nicer
plt.style.use('ggplot')

# import csv file
# data from https://fred.stlouisfed.org/series/DCOILWTICO/downloaddata
df = pd.read_csv('DCOILWTICO.csv')

# convert date column to datetime
df['DATE'] = pd.to_datetime(df['DATE'])

# setup basic line plot, change line color from red to green
ax = df.plot(x='DATE', y='VALUE', color = '#31a354', linewidth = .5)

# change the legend lable from "VALUE" to "Oil Price ($)"
L = plt.legend(loc = 'upper left', fontsize = 'x-large')
L.get_texts()[0].set_text('Oil Price ($)')

# edit axis labels and font size
ax.set_title("Price of Oil for last 30 years", fontsize = 18)
ax.set_xlabel("Time", fontsize = 14)
ax.set_ylabel("Oil Price ($)", fontsize = 14)

# save the figure at a good resolution
fig = ax.get_figure()
fig.savefig('oilprice.png', dpi=600)

plt.show()


#!/home/zkfr/.local/share/virtualenvs/xf-5EfV3Nly/bin/python
#-*- coding:utf-8 -*-
# @author : MaLei
# @datetime : 2018-12-06 13:54
# @file : get_shopinfo.py
# @software : PyCharm

from datetime import datetime as dt
from dianping import *
import re
from bs4 import  BeautifulSoup

def get_id():
    # 从数据库获取店铺id
    cursor,conn=connect_mysql()
    shopid_sql='SELECT shopId FROM food_rank;'
    cursor.execute(shopid_sql)
    shopid_list=list(cursor.fetchall())
    for each in shopid_list[:]:
        shopid_list.append(each[0])
        shopid_list.remove(each)
    shopid_list=set(shopid_list)
    cursor.close()
    conn.close()
    return shopid_list
    # print(shopid_list,len(shopid_list))

def get_shop_info():
    shopid_list = get_id()
    base_url = 'http://www.dianping.com/shop/'
    for shop_id in shopid_list:
        info_url=base_url+shop_id+'/review_all'
        try:
            res=request_set(info_url)
            good_count = res.xpath('//label[@class="filter-item filter-good"]/span/text()')[0][1:-1]
            middle_count = res.xpath('//label[@class="filter-item filter-middle"]/span/text()')[0][1:-1]
            bad_count = res.xpath('//label[@class="filter-item filter-bad"]/span/text()')[0][1:-1]
            re_num = res.xpath('//span[@class="active"]/em/text()')[0][1:-1]
            pages = res.xpath('//div[@class="reviews-pages"]/a[last()-1]/text()')[0]
            tags=res.xpath('//div[@class="reviews-tags"]/div[@class="content"]//span/a/text()')
            for each in tags[:]:
                re_each=clear_text(each)
                tags.append(re_each)
                tags.remove(each)
            print(good_count,middle_count,bad_count,re_num,pages,tags)

            get_rewiew_info(res)

            if pages>=2:
                for i in range(2,pages+1):
                    rewiew_url=info_url+'/p'+str(i)
                    res = request_set(rewiew_url)
                    get_rewiew_info(res)
        except:
            print('请求异常')

def get_rewiew_info(res):
    texts = []
    try:
        soup = BeautifulSoup(res.text(), 'lxml')
        reviews = soup.find_all(class_='review-words Hide')
        for each in reviews:
            review = each.get_text()[:-7]
            texts.append(review)
        texts = clear_text(texts)
        print(texts)

    except:
        # 后期添加tenserflow识别验证码
        # if text.xpath('//div[@_slider__sliderTitle___119tD]/p') or text.xpath('yodaModuleWrapper'):
        #     print('需要验证码')
        #     sleep(10)
        #     # 如果是滑动验证，模拟滑动
        # else:
        print('页面爬取错误')

from lxml import etree
# get_shop_info()
text=open('test.txt',encoding='utf-8')
text1=etree.HTML(text.read())
get_rewiew_info(text1)


"""Module contenant des opérations liées à la loi normale"""

import numpy as np

def densiteLoiNormale(x, mu, sigma):
    return (1/(sigma*np.sqrt(2*np.pi)))*(np.e**((-1/2)*(((x-mu)/sigma)**2)))

def probaNormCentreeReduite(x):

    """Calcul de la fonction F(x) de la loi normale centrée réduite"""

    # on utilise le fait que F(x) = 1-F(-x)

    if x == 0:
        return 0.5

    u = abs(x)
    # nombre de rectangles, on en veut à peu près un tous les 0.0001 en abscisse
    n = int(u/0.0001)
    # largeur d'un rectangle (pas exactement égal à 0.0001 car on a un nombre entier de rectangles)
    du = u/n

    integrale = 0
    for i in range(n):
        """
        On moyenne l'erreur pour ne pas avoir d'intégrales > 1 ou < 0.
        En effet si on prend à chaque incrémentation une hauteur du rectangle qui vaut i*du alors on ne prend que des rectangles qui surestiment légèrement l'aire.
        En alternant une fois sur deux rectangle qui surestime et rectangle qui sous estime l'aire, alors cette erreur s'annuleself.
        """
        if i%2 == 0:
            ord = i*du
        else:
            ord = (i+1)*du

        integrale += densiteLoiNormale(ord,0,1)

    # on multiplie le tout par du ce qui équivaut à multiplier chaque terme de la somme par du
    integrale *= du
    # on part de 0, l'intégrale vaut alors 0.5, on doit donc ajouter 0.5 à l'intégrale obtenue
    integrale += 0.5

    if x<0:
        integrale = 1-integrale

    return integrale

def centreEtReduit(x, mu, sigma):
    return (x-mu)/sigma

def calcProba(a, b, mu, sigma):

    """Renvoie la probabilité d'être entre a et b"""

    borneSup = probaNormCentreeReduite(centreEtReduit(a, mu, sigma))
    borneInf = probaNormCentreeReduite(centreEtReduit(b, mu, sigma))
    return borneSup-borneInf


import os
import logging

import vk_api
from vk_api.bot_longpoll import VkBotLongPoll
from vk_api.upload import VkUpload
from vk_api.bot_longpoll import VkBotEventType
from vk_api.keyboard import VkKeyboard, VkKeyboardColor

from expiring_dict import ExpiringDict
import COVID19Py

keyboard = VkKeyboard(one_time=False)
keyboard.add_button('Коронавирус в мире', color=VkKeyboardColor.PRIMARY)
keyboard.add_button('Коронавирус в россии', color=VkKeyboardColor.PRIMARY)

corona_dict = ExpiringDict(max_len=2, max_age_seconds=3600)
covid19 = COVID19Py.COVID19()

vk_session = vk_api.VkApi(token=os.getenv('TOKEN'), api_version='5.95')
vk = vk_session.get_api()
longpoll = VkBotLongPoll(vk_session, os.getenv('GROUP_ID'))
upload = VkUpload(vk_session)

logging.basicConfig(format='%(levelname)s %(name)s [%(asctime)s]: %(message)s',
                    datefmt='%d:%m:%Y:%H:%M:%S')

log = logging.getLogger(__name__)
log.setLevel(logging.INFO)
log.info('start')

def get_info():
    global corona_dict
    corona_dict['world'] = covid19.getLatest()
    corona_dict['ru'] = covid19.getLocationByCountryCode("RU")[0]['latest']

for event in longpoll.listen():
    if event.type == VkBotEventType.MESSAGE_NEW:
        text = event.object.text
        log.info(f'message {text} id {event.obj.peer_id}')
        if text == 'Начать':
            message = 'Начали'
        elif text == 'Коронавирус в мире':
            if not corona_dict.get('world'):
                get_info()
            message = f'🤧 Заражено: {corona_dict["world"]["confirmed"]} человек\n\
                        😲 Умерло: {corona_dict["world"]["deaths"]} человек\n\
                        😎 Излечено: {corona_dict["world"]["recovered"]} человек'
        elif text == 'Коронавирус в россии':
            if not corona_dict.get('ru'):
                get_info()
            message = f'🤧 Заражено: {corona_dict["ru"]["confirmed"]} человек\n\
                        😲 Умерло: {corona_dict["ru"]["deaths"]} человек\n\
                        😎 Излечено: {corona_dict["ru"]["recovered"]} человек'
        else:
            message = 'я не знаю такой команды'
        vk.messages.send(peer_id=event.obj.peer_id, message=message, random_id=0, keyboard=keyboard.get_keyboard())


import pygame
import random
import sys

# INITIALIZE PYGAME.
pygame.init()

# CREATE THE GAME SCREEN.
SCREEN_WIDTH_IN_PIXELS = 400
SCREEN_HEIGHT_IN_PIXELS = 400
screen = pygame.display.set_mode((SCREEN_WIDTH_IN_PIXELS, SCREEN_HEIGHT_IN_PIXELS))

# DEFINE THE PLAYER'S INITIAL SCORE.
score = 0

# DEFINE THE PLAYER'S POSITION.
player_x = 200
player_y = 200

# DEFINE INFORMATION ABOUT THE COLLECTIBLE.
collectible_x = 100
collectible_y = 100
collectible_width_in_pixels = 20
collectible_height_in_pixels = 20
collectible_color = pygame.Color(255, 0, 0)

# RUN THE GAME UNTIL THE USER CHOOSES TO EXIT.
while True:
    # HANDLE ANY EVENTS SENT TO THE GAME PROGRAM.
    for event in pygame.event.get():
        # EXIT THE GAME IF THE USER CHOSE TO QUIT.
        if pygame.QUIT == event.type:
            # pygame.quit() may still leave the window up,
            # so sys.exit() is needed to fully close the program.
            pygame.quit()
            sys.exit()

        # MOVE THE PLAYER BASED ON KEYBOARD INPUT.
        PLAYER_MOVEMENT_AMOUNT_IN_PIXELS = 10
        if pygame.KEYDOWN == event.type:
            if pygame.K_UP == event.key:
                player_y -= PLAYER_MOVEMENT_AMOUNT_IN_PIXELS
            if pygame.K_DOWN == event.key:
                player_y += PLAYER_MOVEMENT_AMOUNT_IN_PIXELS
            if pygame.K_LEFT == event.key:
                player_x -= PLAYER_MOVEMENT_AMOUNT_IN_PIXELS
            if pygame.K_RIGHT == event.key:
                player_x += PLAYER_MOVEMENT_AMOUNT_IN_PIXELS

    # UPDATE THE POSITIONS OF OBJECTS IN THE GAME WORLD.
    # These need to be updated each frame because some objects might move.
    PLAYER_SIZE_IN_PIXELS = 20
    player_rectangle = pygame.Rect(
        player_x,
        player_y,
        PLAYER_SIZE_IN_PIXELS,
        PLAYER_SIZE_IN_PIXELS)
    collectible_rectangle = pygame.Rect(
        collectible_x,
        collectible_y,
        collectible_width_in_pixels,
        collectible_height_in_pixels)

    # COLLECT THE COLLECTIBLE IF THE PLAYER TOUCHES IT.
    player_touched_collectible = player_rectangle.colliderect(collectible_rectangle)
    if player_touched_collectible:
        # COUNT THE COLLECTIBLE IN THE PLAYER'S SCORE.
        score += 1
        print("Score: " + str(score))

        # RANDOMLY GENERATE A NEW COLLECTIBLE.
        MIN_COLLECTIBLE_SIZE_IN_PIXELS = 5
        MAX_COLLECTIBLE_SIZE_IN_PIXELS = 50
        collectible_width_in_pixels = random.randrange(
            MIN_COLLECTIBLE_SIZE_IN_PIXELS,
            MAX_COLLECTIBLE_SIZE_IN_PIXELS)
        collectible_height_in_pixels = random.randrange(
            MIN_COLLECTIBLE_SIZE_IN_PIXELS,
            MAX_COLLECTIBLE_SIZE_IN_PIXELS)

        MAX_COLLECTIBLE_X = SCREEN_WIDTH_IN_PIXELS - collectible_width_in_pixels
        collectible_x = random.randrange(MAX_COLLECTIBLE_X + 1)
        MAX_COLLECTIBLE_Y = SCREEN_HEIGHT_IN_PIXELS - collectible_height_in_pixels
        collectible_y = random.randrange(MAX_COLLECTIBLE_Y + 1)

        MAX_COLOR_COMPONENT = 255
        collectible_color.r = random.randrange(MAX_COLOR_COMPONENT + 1)
        collectible_color.g = random.randrange(MAX_COLOR_COMPONENT + 1)
        collectible_color.b = random.randrange(MAX_COLOR_COMPONENT + 1)

    # CLEAR THE SCREEN FOR THE NEW FRAME.
    BACKGROUND_COLOR = pygame.Color(0, 0, 0)
    screen.fill(BACKGROUND_COLOR)

    # DRAW THE PLAYER.
    PLAYER_COLOR = pygame.Color(255, 255, 255)
    pygame.draw.rect(
        screen,
        PLAYER_COLOR,
        player_rectangle)

    # DRAW THE COLLECTIBLE.
    pygame.draw.rect(
        screen,
        collectible_color,
        collectible_rectangle)

    # DISPLAY THE UPDATED FRAME.
    pygame.display.update()


import numpy as np
import av
import matplotlib.pyplot as plt
import cv2
from skimage.color import rgb2gray
from skimage.feature import peak_local_max
from scipy.signal import convolve2d
from PIL import Image, ImageDraw
from sklearn.linear_model import LinearRegression
from scipy.ndimage.filters import gaussian_filter1d
from predict_hip import locate_hip
import os

####################################################
#
# to use this program just change this variable
# to the path of the video you would like to
# process
#

path = '../data/finish-line/20190413_134043.mp4'

#
# it should output an image finish.png and print
# the hip numbers of the runners in the order they
# finished
#
####################################################

# convert numpy arraw to pillow image
def convert_to_pil(arr):
    arr = arr * 256
    return Image.fromarray(arr).convert('RGB')

# save a numpy array as an image
def save_image(arr, path):
    convert_to_pil(arr).save(path)

# gets and saves a small image that contains the hip number for the runner that is finishing in frame.
# args:
# frame: frame of the runner finishing
# base_image: background image
# finish_line: x-coordinate of the finish line
# horz_comp: vertically filtered row showing where the runner is horizontally in the frame
# file_name: file name to store the image as
def get_number_image(frame, base_image, finish_line, horz_comp, file_name):
    horz_comp2 = gaussian_filter1d(horz_comp, sigma=5) # blur to eliminate little gaps

    # find where there stops being runner and use that as the lower bound of the horizontal slice
    lower_bound = finish_line - 20
    for i in range(finish_line - 20, 0, -1):
        if horz_comp2[i] < .1:
            lower_bound = i
            break
    # get subtracted image
    sub = abs(frame - base_image)

    # take the vertical stripe of the image where there is runner
    segment = np.transpose(frame[lower_bound:finish_line+10, :])

    # take the vertical stripe of the difference image
    segment_filter = np.transpose(sub[lower_bound:finish_line+10, :])

    # make a horizontal filter to convolve with the difference segment
    horz_filter = np.ones((1, segment.shape[1])) / segment.shape[1]

    # make a column of pixels whose values are 1 where there is a runner and 0 where there isn't
    horz_filtered = convolve2d(segment_filter, horz_filter, mode='valid').reshape(segment.shape[0])
    horz_filtered_g = horz_filtered > 0.05
    horz_filtered_l = horz_filtered <= 0.05
    horz_filtered[horz_filtered_g] = 1
    horz_filtered[horz_filtered_l] = 0

    # calculate the height of the horizontal stripe we are going to extract from the vertical stripe
    segment_height = int(np.sum(horz_filtered))

    # extract rows where horz_filtered = 1 and add them to output
    output = np.zeros((segment_height, segment.shape[1]))
    cntr = 0
    for i in range(len(horz_filtered)):
        if horz_filtered[i] == 1:
            output[cntr,:] = segment[i,:]
            cntr += 1

    # take only the middle 40% of the image (vertically), because the hip is in the middle of a person
    output = output[int(.3 * output.shape[0]):int(.7 * output.shape[0]),:]

    # flip the image because it got flipped somehow
    output = np.flip(output, axis=1)

    save_image(output, file_name)
    return output

finish_line = 360 # x coordinate of the finish line

# these variables are initialized when the first frame of the video is processed
frames = 0 # numpy array of every frame
horizontal_comp = 0 # array of every vert_filter-convolved row
edges = 0 # array of the edges to use RANSAC line-fitting on
finish_image = 0 # cross-sectional image

# array saving and loading was used to speed up development and can be enabled by removing the 'True or' at the beginning of the if condition
if True or not os.path.isfile('edges.npy') or not os.path.isfile('finish.npy') or not os.path.isfile('horizontal_comp.npy') or not os.path.isfile('frames.npy'):
    video = cv2.VideoCapture(path)
    container = av.open(path)

    num_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))

    # these variables are initialized when processing the first frame
    base_image = 0 # background image
    vert_filter = 0 # vertical filter for convolving down to a single row

    print('loaded video')
    i = 0 # frame counter

    for frame in container.decode(video=0):
        # convert image to grayscale
        image = rgb2gray(np.array(frame.to_image()))
        if i == 0:

            # initialize arrays and vertical filter
            finish_image = np.zeros((image.shape[1], num_frames))
            base_image = image
            vert_filter = np.ones((1, image.shape[1])) / image.shape[1]
            # taper the filter towards the bottom
            for j in range(int(vert_filter.shape[1] * 2 / 3), vert_filter.shape[1]):
                vert_filter[0, j] = vert_filter[0, j] * (1 - 2 * (j - vert_filter.shape[1] * 2 / 3) / vert_filter.shape[1])

            frames = np.zeros((num_frames, image.shape[0], image.shape[1]))
            horizontal_comp = np.zeros((num_frames, image.shape[0]))
        finish_image[:, i] = image[finish_line, :] # take vertical strip of image at finish line
        frames[i,:,:] = image
        image = abs(image - base_image) # subtract out background

        horz_filtered = convolve2d(image, vert_filter, mode='valid') # convolve with vertical filter

        # clamp values to be 0 or 1
        horz_bg = horz_filtered < .03
        horz_person = horz_filtered > .03
        horz_filtered[horz_bg] = 0
        horz_filtered[horz_person] = 1

        # add row to large array
        horizontal_comp[i, :] = horz_filtered.reshape(horz_filtered.shape[0])

        i += 1
    print('processed video')
    # kernel to edge detect the lines of the runners
    edge_kernel = np.ones(15) / 7.5
    edge_kernel[0:4] = -edge_kernel[0:4]
    edge_kernel = edge_kernel.reshape((1, len(edge_kernel)))

    # convoled image
    edges = convolve2d(horizontal_comp - 0.5, edge_kernel)

    #np.save('horizontal_comp.npy', horizontal_comp)
    #np.save('frames.npy', frames)
    #np.save('edges.npy', edges)
    #np.save('finish.npy', finish_image)
else:
    horizontal_comp = np.load('horizontal_comp.npy')
    frames = np.load('frames.npy')
    edges = np.load('edges.npy')
    finish_image = np.load('finish.npy')

# points to fit using RANSAC
x = []
y = []

# prior method for finding finishes
max_finishes = []

# save_image(horizontal_comp, 'edges3.png')
# save_image(edges, 'edges.png')

for i in range(edges.shape[1]):
    # for each row run non-max suppression
    maxes = peak_local_max(edges[:, i], min_distance=15, threshold_abs=.8)
    # update image to only include maxes
    new_col = np.zeros(edges[:,i].shape)
    new_col[maxes] = 1
    for j in range(maxes.shape[0]):
        # add points to data arrays
        x.append(i)
        y.append(maxes[j,0])
        if i == finish_line:
            max_finishes.append(maxes[j,0])
    edges[:,i] = new_col

x = np.array(x)
y = np.array(y)

# save_image(edges, 'edges4.png')

# constants for RANSAC
sample_size = 4 # sample size of points considered on each iteration
inlier_threshold = 5 # distance a point can be from the line to be considered an inlier
num_inliers_threshold = 100 # number of inliers required to accept a line

# image to draw on
pil_edges = convert_to_pil(edges)
edges_draw = ImageDraw.Draw(pil_edges)

lines_drawn = 0
num_iters = 0
regs = [] # list of fitted lines

# i
while len(x) > num_inliers_threshold and lines_drawn < 10 and num_iters < 2000:
    num_iters += 1

    # take a random sample of points
    sample_x = np.zeros(sample_size)
    sample_y = np.zeros(sample_size)

    inds = np.random.choice(len(x), sample_size, False)
    for i in range(sample_size):
        sample_x[i] = x[inds[i]]
        sample_y[i] = y[inds[i]]

    # fit a line to the sample
    reg = LinearRegression().fit(sample_x.reshape((-1, 1)), sample_y)

    # the y values from the line for every x in the set of points
    predict_y = reg.predict(x.reshape((-1, 1)))

    # difference from the real y values
    diff = abs(y - predict_y)

    # find number of inliers
    inliers = diff < inlier_threshold
    num_inliers = inliers.sum()

    # if the line is acceptable
    if num_inliers > num_inliers_threshold:
        regs.append(reg) # add to set of lines
        outliers = diff >= inlier_threshold * 2.5

        # remove inliers from set of points
        x = np.extract(outliers, x)
        y = np.extract(outliers, y)

        # draw the fitted line for visualization purposes
        edges_draw.line([(0, reg.predict(np.array([[0]]))[0]), (edges.shape[1] - 1, reg.predict(np.array([[edges.shape[1] - 1]]))[0])], fill = (255, 255, 0))
        lines_drawn += 1

# cross-sectional image to draw on
finish_pil = convert_to_pil(finish_image)
finish_draw = ImageDraw.Draw(finish_pil)

def finish_time(reg):
    return int(reg.predict(np.array([[finish_line]]))[0])

regs.sort(key=finish_time)

# for each fitted line, draw a line in the cross-sectional image and get an image to
for count, reg in enumerate(regs):
    chest = int(reg.predict(np.array([[finish_line]]))[0])
    finish_draw.line([(chest, 0), (chest, finish_image.shape[0])], fill=(255, 0, 0))
    finisher_image = get_number_image(frames[chest,:,:], frames[0,:,:], finish_line, horizontal_comp[chest,:].reshape(horizontal_comp[chest,:].shape[0]), 'finish_' + str(count) + '.png')
    num = locate_hip(finisher_image)
    print("Results:")
    print(str(count + 1) + ') ' + str(num))

#for finish in max_finishes:
    #finish_draw.line([(finish, 0), (finish, finish_image.shape[0])], fill=(255, 255, 0))

finish_pil.save('finish.png')


import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

def page():
    driver = webdriver.Chrome('/Users/jorge.rios/Downloads/chromedriver')  # Optional argument, if not specified will search path.
    driver.get("http://127.0.0.1:5000/")
    texto = ""
    with open('unonuevo.txt', 'r') as f:
        texto = f.readline()
    #assert "Python" in driver.title
    elem = driver.find_element_by_name("nombre")
    elem.clear()
    elem.send_keys(texto)
    time.sleep(5)
    elem = driver.find_element_by_name("edad")
    elem.send_keys("20")
    elem = driver.find_element_by_name("submit")
    elem.send_keys(Keys.RETURN)
    assert "No results found." not in driver.page_source
    #driver.close()
    time.sleep(5)
    driver.quit()

if __name__ == "__main__":
    page()


# Copyright 2018 The TensorFlow Probability Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

# Dependency imports
import numpy as np

import tensorflow.compat.v1 as tf1
import tensorflow.compat.v2 as tf

from tensorflow_probability.python import distributions as tfd
from tensorflow_probability.python.internal import tensorshape_util
from tensorflow_probability.python.internal import test_util
from tensorflow_probability.python.math import psd_kernels

class _StudentTProcessTest(object):

  def testShapes(self):
    # 5x5 grid of index points in R^2 and flatten to 25x2
    index_points = np.linspace(-4., 4., 5, dtype=np.float32)
    index_points = np.stack(np.meshgrid(index_points, index_points), axis=-1)
    index_points = np.reshape(index_points, [-1, 2])
    # ==> shape = [25, 2]

    # Kernel with batch_shape [2, 4, 1]
    df = np.array(
        [[3., 4., 5., 4.], [7.5, 8, 5., 5.]],
        dtype=np.float32).reshape([2, 4, 1])
    amplitude = np.array([1., 2.], np.float32).reshape([2, 1, 1])
    length_scale = np.array([1., 2., 3., 4.], np.float32).reshape([1, 4, 1])
    batched_index_points = np.stack([index_points]*6)
    # ==> shape = [6, 25, 2]
    if not self.is_static:
      df = tf1.placeholder_with_default(df, shape=None)
      amplitude = tf1.placeholder_with_default(amplitude, shape=None)
      length_scale = tf1.placeholder_with_default(length_scale, shape=None)
      batched_index_points = tf1.placeholder_with_default(
          batched_index_points, shape=None)
    kernel = psd_kernels.ExponentiatedQuadratic(amplitude, length_scale)
    tp = tfd.StudentTProcess(
        df, kernel, batched_index_points, jitter=1e-5, validate_args=True)

    batch_shape = [2, 4, 6]
    event_shape = [25]
    sample_shape = [5, 3]

    samples = tp.sample(sample_shape, seed=test_util.test_seed())

    if self.is_static or tf.executing_eagerly():
      self.assertAllEqual(tp.batch_shape_tensor(), batch_shape)
      self.assertAllEqual(tp.event_shape_tensor(), event_shape)
      self.assertAllEqual(samples.shape,
                          sample_shape + batch_shape + event_shape)
      self.assertAllEqual(tp.batch_shape, batch_shape)
      self.assertAllEqual(tp.event_shape, event_shape)
      self.assertAllEqual(samples.shape,
                          sample_shape + batch_shape + event_shape)
      self.assertAllEqual(tp.mean().shape, batch_shape + event_shape)
      self.assertAllEqual(tp.variance().shape, batch_shape + event_shape)
    else:
      self.assertAllEqual(self.evaluate(tp.batch_shape_tensor()), batch_shape)
      self.assertAllEqual(self.evaluate(tp.event_shape_tensor()), event_shape)
      self.assertAllEqual(
          self.evaluate(samples).shape,
          sample_shape + batch_shape + event_shape)
      self.assertIsNone(tensorshape_util.rank(samples.shape))
      self.assertIsNone(tensorshape_util.rank(tp.batch_shape))
      self.assertEqual(tensorshape_util.rank(tp.event_shape), 1)
      self.assertIsNone(
          tf.compat.dimension_value(tensorshape_util.dims(tp.event_shape)[0]))
      self.assertAllEqual(
          self.evaluate(tf.shape(tp.mean())), batch_shape + event_shape)
      self.assertAllEqual(self.evaluate(
          tf.shape(tp.variance())), batch_shape + event_shape)

  def testVarianceAndCovarianceMatrix(self):
    df = np.float64(4.)
    amp = np.float64(.5)
    len_scale = np.float64(.2)
    jitter = np.float64(1e-4)

    kernel = psd_kernels.ExponentiatedQuadratic(amp, len_scale)

    index_points = np.expand_dims(np.random.uniform(-1., 1., 10), -1)

    tp = tfd.StudentTProcess(
        df=df,
        kernel=kernel,
        index_points=index_points,
        jitter=jitter,
        validate_args=True)

    def _kernel_fn(x, y):
      return amp ** 2 * np.exp(-.5 * (np.squeeze((x - y)**2)) / (len_scale**2))

    expected_covariance = (
        _kernel_fn(np.expand_dims(index_points, 0),
                   np.expand_dims(index_points, 1)))

    self.assertAllClose(expected_covariance,
                        self.evaluate(tp.covariance()))
    self.assertAllClose(np.diag(expected_covariance),
                        self.evaluate(tp.variance()))

  def testMean(self):
    mean_fn = lambda x: x[:, 0]**2
    kernel = psd_kernels.ExponentiatedQuadratic()
    index_points = np.expand_dims(np.random.uniform(-1., 1., 10), -1)
    tp = tfd.StudentTProcess(
        df=3.,
        kernel=kernel,
        index_points=index_points,
        mean_fn=mean_fn,
        validate_args=True)
    expected_mean = mean_fn(index_points)
    self.assertAllClose(expected_mean,
                        self.evaluate(tp.mean()))

  def testCopy(self):
    # 5 random index points in R^2
    index_points_1 = np.random.uniform(-4., 4., (5, 2)).astype(np.float32)
    # 10 random index points in R^2
    index_points_2 = np.random.uniform(-4., 4., (10, 2)).astype(np.float32)

    # ==> shape = [6, 25, 2]
    if not self.is_static:
      index_points_1 = tf1.placeholder_with_default(index_points_1, shape=None)
      index_points_2 = tf1.placeholder_with_default(index_points_2, shape=None)

    mean_fn = lambda x: np.array([0.], np.float32)
    kernel_1 = psd_kernels.ExponentiatedQuadratic()
    kernel_2 = psd_kernels.ExpSinSquared()

    tp1 = tfd.StudentTProcess(
        df=3.,
        kernel=kernel_1,
        index_points=index_points_1,
        mean_fn=mean_fn,
        jitter=1e-5,
        validate_args=True)
    tp2 = tp1.copy(df=4., index_points=index_points_2, kernel=kernel_2)

    event_shape_1 = [5]
    event_shape_2 = [10]

    self.assertEqual(tp1.mean_fn, tp2.mean_fn)
    self.assertIsInstance(tp1.kernel, psd_kernels.ExponentiatedQuadratic)
    self.assertIsInstance(tp2.kernel, psd_kernels.ExpSinSquared)

    if self.is_static or tf.executing_eagerly():
      self.assertAllEqual(tp1.batch_shape, tp2.batch_shape)
      self.assertAllEqual(tp1.event_shape, event_shape_1)
      self.assertAllEqual(tp2.event_shape, event_shape_2)
      self.assertEqual(self.evaluate(tp1.df), 3.)
      self.assertEqual(self.evaluate(tp2.df), 4.)
      self.assertAllEqual(tp2.index_points, index_points_2)
      self.assertAllEqual(tp1.index_points, index_points_1)
      self.assertAllEqual(tp2.index_points, index_points_2)
      self.assertAllEqual(
          tf.get_static_value(tp1.jitter), tf.get_static_value(tp2.jitter))
    else:
      self.assertAllEqual(
          self.evaluate(tp1.batch_shape_tensor()),
          self.evaluate(tp2.batch_shape_tensor()))
      self.assertAllEqual(
          self.evaluate(tp1.event_shape_tensor()), event_shape_1)
      self.assertAllEqual(
          self.evaluate(tp2.event_shape_tensor()), event_shape_2)
      self.assertEqual(self.evaluate(tp1.jitter), self.evaluate(tp2.jitter))
      self.assertEqual(self.evaluate(tp1.df), 3.)
      self.assertEqual(self.evaluate(tp2.df), 4.)
      self.assertAllEqual(self.evaluate(tp1.index_points), index_points_1)
      self.assertAllEqual(self.evaluate(tp2.index_points), index_points_2)

  def testLateBindingIndexPoints(self):
    amp = np.float64(.5)
    len_scale = np.float64(.2)
    kernel = psd_kernels.ExponentiatedQuadratic(amp, len_scale)
    mean_fn = lambda x: x[:, 0]**2
    jitter = np.float64(1e-4)

    tp = tfd.StudentTProcess(
        df=np.float64(3.),
        kernel=kernel,
        mean_fn=mean_fn,
        jitter=jitter,
        validate_args=True)

    index_points = np.random.uniform(-1., 1., [10, 1]).astype(np.float64)

    expected_mean = mean_fn(index_points)
    self.assertAllClose(expected_mean,
                        self.evaluate(tp.mean(index_points=index_points)))

    def _kernel_fn(x, y):
      return amp ** 2 * np.exp(-.5 * (np.squeeze((x - y)**2)) / (len_scale**2))

    expected_covariance = _kernel_fn(np.expand_dims(index_points, -3),
                                     np.expand_dims(index_points, -2))

    self.assertAllClose(expected_covariance,
                        self.evaluate(tp.covariance(index_points=index_points)))
    self.assertAllClose(np.diag(expected_covariance),
                        self.evaluate(tp.variance(index_points=index_points)))
    self.assertAllClose(np.sqrt(np.diag(expected_covariance)),
                        self.evaluate(tp.stddev(index_points=index_points)))

    # Calling mean with no index_points should raise an Error
    with self.assertRaises(ValueError):
      tp.mean()

  def testMarginalHasCorrectTypes(self):
    tp = tfd.StudentTProcess(
        df=3., kernel=psd_kernels.ExponentiatedQuadratic(), validate_args=True)

    self.assertIsInstance(
        tp.get_marginal_distribution(
            index_points=np.ones([1, 1], dtype=np.float32)),
        tfd.StudentT)

    self.assertIsInstance(
        tp.get_marginal_distribution(
            index_points=np.ones([10, 1], dtype=np.float32)),
        tfd.MultivariateStudentTLinearOperator)

@test_util.test_all_tf_execution_regimes
class StudentTProcessStaticTest(_StudentTProcessTest, test_util.TestCase):
  is_static = True

@test_util.test_all_tf_execution_regimes
class StudentTProcessDynamicTest(_StudentTProcessTest, test_util.TestCase):
  is_static = False

if __name__ == "__main__":
  tf.test.main()


# %%
from os import listdir
from os.path import isfile, join, dirname, splitext, abspath
from matplotlib import pyplot as plt
import numpy as np
import pandas as pd
import psignifit as ps
from matplotlib.ticker import ScalarFormatter
from sys import getsizeof, argv

# %% Find The Directory and Open the Files
class fit(object):
    def __init__(self, data, options):
        self.res = ps.psignifit(data,options)
    def __enter__(self):
        return self.res
    def __exit__(self, type, value, trace_back):
        del self.res

def plotPsych(result,
              dataColor      = [0, 105./255, 170./255],
              plotData       = True,
              lineColor      = [0, 0, 0],
              lineWidth      = 2,
              xLabel         = 'Stimulus Level',
              yLabel         = 'Proportion Correct',
              labelSize      = 15,
              fontSize       = 10,
              fontName       = 'Helvetica',
              tufteAxis      = False,
              plotAsymptote  = True,
              plotThresh     = True,
              aspectRatio    = False,
              extrapolLength = .2,
              CIthresh       = False,
              dataSize       = 0,
              axisHandle     = None,
              showImediate   = True):
    """
    This function produces a plot of the fitted psychometric function with
    the data.
    """

    fit = result['Fit']
    data = result['data']
    options = result['options']

    if axisHandle == None: axisHandle = plt.gca()
    try:
        plt.sca(axisHandle)
    except TypeError:
        raise ValueError('Invalid axes handle provided to plot in.')

    if np.isnan(fit[3]): fit[3] = fit[2]
    if data.size == 0: return
    if dataSize == 0: dataSize = 10000. / np.sum(data[:,2])

    if 'nAFC' in options['expType']:
        ymin = 1. / options['expN']
        ymin = min([ymin, min(data[:,1] / data[:,2])])
    else:
        ymin = 0

    # PLOT DATA
    #holdState = plt.ishold()
    #if not holdState:
    #    plt.cla()
    #    plt.hold(True)
    xData = data[:,0]
    if plotData:
        yData = data[:,1] / data[:,2]
        markerSize = np.sqrt(dataSize/2 * data[:,2])
        for i in range(len(xData)):
            plt.plot(xData[i], yData[i], '.', ms=markerSize[i], c=dataColor, clip_on=False)

    # PLOT FITTED FUNCTION
    if options['logspace']:
        xMin = np.log(min(xData))
        xMax = np.log(max(xData))
        xLength = xMax - xMin
        x       = np.exp(np.linspace(xMin, xMax, num=1000))
        xLow    = np.exp(np.linspace(xMin - extrapolLength*xLength, xMin, num=100))
        xHigh   = np.exp(np.linspace(xMax, xMax + extrapolLength*xLength, num=100))
        axisHandle.set_xscale('log')
    else:
        xMin = min(xData)
        xMax = max(xData)
        xLength = xMax - xMin
        x       = np.linspace(xMin, xMax, num=1000)
        xLow    = np.linspace(xMin - extrapolLength*xLength, xMin, num=100)
        xHigh   = np.linspace(xMax, xMax + extrapolLength*xLength, num=100)

    fitValuesLow  = (1 - fit[2] - fit[3]) * options['sigmoidHandle'](xLow,  fit[0], fit[1]) + fit[3]
    fitValuesHigh = (1 - fit[2] - fit[3]) * options['sigmoidHandle'](xHigh, fit[0], fit[1]) + fit[3]
    fitValues     = (1 - fit[2] - fit[3]) * options['sigmoidHandle'](x,     fit[0], fit[1]) + fit[3]

    plt.plot(x,     fitValues,           c=lineColor, lw=lineWidth, clip_on=False)
    plt.plot(xLow,  fitValuesLow,  '--', c=lineColor, lw=lineWidth, clip_on=False)
    plt.plot(xHigh, fitValuesHigh, '--', c=lineColor, lw=lineWidth, clip_on=False)

    # PLOT PARAMETER ILLUSTRATIONS
    # THRESHOLD
    if plotThresh:
        if options['logspace']:
            x = [np.exp(fit[0]), np.exp(fit[0])]
        else:
            x = [fit[0], fit[0]]
        y = [ymin, fit[3] + (1 - fit[2] - fit[3]) * options['threshPC']]
        plt.plot(x, y, '-', c=lineColor)
    # ASYMPTOTES
    if plotAsymptote:
        plt.plot([min(xLow), max(xHigh)], [1-fit[2], 1-fit[2]], ':', c=lineColor, clip_on=False)
        plt.plot([min(xLow), max(xHigh)], [fit[3], fit[3]],     ':', c=lineColor, clip_on=False)
    # CI-THRESHOLD
    if CIthresh:
        CIs = result['confIntervals']
        y = np.array([fit[3] + .5*(1 - fit[2] - fit[3]) for i in range(2)])
        plt.plot(CIs[0,:,0],               y,               c=lineColor)
        plt.plot([CIs[0,0,0], CIs[0,0,0]], y + [-.01, .01], c=lineColor)
        plt.plot([CIs[0,1,0], CIs[0,1,0]], y + [-.01, .01], c=lineColor)

    #AXIS SETTINGS
    plt.axis('tight')
    plt.tick_params(labelsize=fontSize)
    plt.xlabel(xLabel, fontname=fontName, fontsize=labelSize)
    plt.ylabel(yLabel, fontname=fontName, fontsize=labelSize)
    if aspectRatio: axisHandle.set_aspect(2/(1 + np.sqrt(5)))

    plt.ylim([ymin, 1])
    # tried to mimic box('off') in matlab, as box('off') in python works differently
    plt.tick_params(direction='out',right=False,top=False)
    for side in ['top','right']: axisHandle.spines[side].set_visible(False)
    plt.gca().xaxis.set_major_formatter(ScalarFormatter())
    plt.ticklabel_format(style= 'sci',scilimits=(-2,4))

    #plt.hold(holdState)
    if (showImediate):
        plt.show(0)
    return axisHandle

def plotsModelfit(result,
              showImediate   = True):
    """
    Plots some standard plots, meant to help you judge whether there are
    systematic deviations from the model. We dropped the statistical tests
    here though.

    The left plot shows the psychometric function with the data.

    The central plot shows the Deviance residuals against the stimulus level.
    Systematic deviations from 0 here would indicate that the measured data
    shows a different shape than the fitted one.

    The right plot shows the Deviance residuals against "time", e.g. against
    the order of the passed blocks. A trend in this plot would indicate
    learning/ changes in performance over time.

    These are the same plots as presented in psignifit 2 for this purpose.
    """

    fit = result['Fit']
    data = result['data']
    options = result['options']

    minStim = min(data[:,0])
    maxStim = max(data[:,0])
    stimRange = [1.1*minStim - .1*maxStim, 1.1*maxStim - .1*minStim]

    plt.figure(figsize=(15,5))

    ax = plt.subplot(1,3,1)
    # the psychometric function
    x = np.linspace(stimRange[0], stimRange[1], 1000)
    y = fit[3] + (1-fit[2]-fit[3]) * options['sigmoidHandle'](x, fit[0], fit[1])

    plt.plot(x, y, 'k', clip_on=False)
    plt.plot(data[:,0], data[:,1]/data[:,2], '.k', ms=10, clip_on=False)

    plt.xlim(stimRange)
    if options['expType'] == 'nAFC':
        plt.ylim([min(1./options['expN'], min(data[:,1]/data[:,2])), 1])
    else:
        plt.ylim([0,1])
    plt.xlabel('Stimulus Level',  fontsize=14)
    plt.ylabel('Percent Correct', fontsize=14)
    plt.title('Psychometric Function', fontsize=20)
    plt.tick_params(right=False,top=False)
    for side in ['top','right']: ax.spines[side].set_visible(False)
    plt.ticklabel_format(style='sci',scilimits=(-2,4))

    ax = plt.subplot(1,3,2)
    # stimulus level vs deviance
    stdModel = fit[3] + (1-fit[2]-fit[3]) * options['sigmoidHandle'](data[:,0],fit[0],fit[1])
    deviance = data[:,1]/data[:,2] - stdModel
    stdModel = np.sqrt(stdModel * (1-stdModel))
    deviance = deviance / stdModel
    xValues = np.linspace(minStim, maxStim, 1000)

    plt.plot(data[:,0], deviance, 'k.', ms=10, clip_on=False)
    linefit = np.polyfit(data[:,0],deviance,1)
    plt.plot(xValues, np.polyval(linefit,xValues),'k-', clip_on=False)
    linefit = np.polyfit(data[:,0],deviance,2)
    plt.plot(xValues, np.polyval(linefit,xValues),'k--', clip_on=False)
    linefit = np.polyfit(data[:,0],deviance,3)
    plt.plot(xValues, np.polyval(linefit,xValues),'k:', clip_on=False)

    plt.xlabel('Stimulus Level',  fontsize=14)
    plt.ylabel('Deviance', fontsize=14)
    plt.title('Shape Check', fontsize=20)
    plt.tick_params(right=False,top=False)
    for side in ['top','right']: ax.spines[side].set_visible(False)
    plt.ticklabel_format(style='sci',scilimits=(-2,4))

    ax = plt.subplot(1,3,3)
    # block number vs deviance
    blockN = range(len(deviance))
    xValues = np.linspace(min(blockN), max(blockN), 1000)
    plt.plot(blockN, deviance, 'k.', ms=10, clip_on=False)
    linefit = np.polyfit(blockN,deviance,1)
    plt.plot(xValues, np.polyval(linefit,xValues),'k-', clip_on=False)
    linefit = np.polyfit(blockN,deviance,2)
    plt.plot(xValues, np.polyval(linefit,xValues),'k--', clip_on=False)
    linefit = np.polyfit(blockN,deviance,3)
    plt.plot(xValues, np.polyval(linefit,xValues),'k:', clip_on=False)

    plt.xlabel('Block Number',  fontsize=14)
    plt.ylabel('Deviance', fontsize=14)
    plt.title('Time Dependence?', fontsize=20)
    plt.tick_params(right=False,top=False)
    for side in ['top','right']: ax.spines[side].set_visible(False)
    plt.gca().xaxis.set_major_formatter(ScalarFormatter())
    plt.ticklabel_format(style='sci',scilimits=(-2,4))

    plt.tight_layout()
    if (showImediate):
        plt.show(0)

def openDocuments(pathdir=None):
    retorno = []
    if pathdir == None:

        pathdir = abspath('')
    else:

        pathdir = abspath(pathdir)

    for f in listdir(pathdir):
        if isfile(abspath(pathdir)+"\\"+f):
            retorno.append(abspath(pathdir)+"\\"+f)
    return retorno

    # %% Filter just csv files

def filterByFormat(extension, filepaths):
    return [fp for fp in filepaths if splitext(fp)[-1].lower() == extension]

def invertData(d, c="UserSelection"):
    d.loc[(d["VariablePiston"] == 0) & (d["VariableStiffnes"] != "0,65"), [
        c]] = ~d.loc[(d["VariablePiston"] == 0) & (d["VariableStiffnes"] != "0,65"), [c]]

# %% Get The times the user answered true
def GetAxisX(Condition):
    return list(Condition["VariableStiffnes"].head(7).sort_values())

def GetTrueRate(Condition):
    TrueRate = []
    _VariableValues = GetAxisX(Condition)
    for value in _VariableValues:
        _Stiffness = Condition[Condition.VariableStiffnes == value]
        TrueRate.append(_Stiffness.UserSelection.sum())
    return TrueRate

# %%
filescsv = filterByFormat(".csv", openDocuments("Actual"))

truet = np.zeros(7)
truet3 = np.zeros(7)
for file in filescsv:
    readfile = pd.read_csv(file, delimiter=";")
    C2 = readfile[readfile.HringState == "ON"]
    C3 = readfile[readfile.HringState == "RELEASING"]
    invertData(C2)
    invertData(C3)
    TrueVector = GetTrueRate(C2)
    TrueVector3 = GetTrueRate(C3)
    if file==filescsv[8]:
        truet += np.array(TrueVector[::])/len(filescsv)
        truet3 += np.array(TrueVector3[::])/len(filescsv)
    else:
        truet += np.array(TrueVector[::-1])/len(filescsv)
        truet3 += np.array(TrueVector3[::-1])/len(filescsv)

# %%
VariableValues = GetAxisX(C2)
VariableValuesf = [round(float(value.replace(',', '.')), 3)
                  for value in VariableValues]

# %%
Trials = np.ones(len(VariableValues))*5

# %%

data = np.array([VariableValuesf, truet, Trials]).T
data2 = np.array([VariableValuesf, truet3, Trials]).T
# %%
options = dict()
options['sigmoidName'] = 'Weibull'

for style in plt.style.available:
    plt.style.use(style)
plt.style.use(plt.style.available[0])

# %%
#with fit(data,options) as res:
#    plotPsych(res,
#                dataColor      = [255./255, 0, 0],
#                lineColor      = [255./255, 0, 0])
#with fit(data2,options) as res:
#    plotPsych(res,
#                dataColor      = [0, 0, 255./255],
#                lineColor      = [0, 0, 255./255])
#    plt.savefig("WeibullTotal")
## %%
with fit(data,options) as res:
    plotsModelfit(res)
    plt.savefig("WeibullT2")
with fit(data2,options) as res:
    plotsModelfit(res)
    plt.savefig("WeibullT3")


#!/usr/bin/env python
#
# setup.py -- Installation for djblets
#
# Copyright (C) 2008 Christian Hammond
# Copyright (C) 2007-2008 David Trowbridge
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

import subprocess
import sys
import os

try:
    from setuptools import setup, find_packages
except ImportError:
    from ez_setup import use_setuptools
    use_setuptools()
    from setuptools import setup, find_packages

from distutils.core import Command
from setuptools.command.egg_info import egg_info
from setuptools.command.test import test

from djblets import django_version, get_package_version, is_release, VERSION

# Make sure this is a version of Python we are compatible with. This should
# prevent people on older versions from unintentionally trying to install
# the source tarball, and failing.
if sys.hexversion < 0x02060000:
    sys.stderr.write('This version of Djblets is incompatible with your '
                     'version of Python.\n')
    sys.exit(1)

class BuildEggInfo(egg_info):
    def run(self):
        if ('sdist' in sys.argv or
            'bdist_egg' in sys.argv or
            'install' in sys.argv):
            self.run_command('build_media')
            self.run_command('build_i18n')

        egg_info.run(self)

class BuildMedia(Command):
    user_options = []

    def initialize_options(self):
        pass

    def finalize_options(self):
        pass

    def run(self):
        retcode = subprocess.call([
            sys.executable, 'contrib/internal/build-media.py'])

        if retcode != 0:
            raise RuntimeError('Failed to build media files')

class BuildI18n(Command):
    description = 'Compile message catalogs to .mo'
    user_options = []

    def initialize_options(self):
        pass

    def finalize_options(self):
        pass

    def run(self):
        # If we are attempting to build on a system without an
        # existing copy of Djblets installed in a reachable
        # location (such as distribution packaging), we need to
        # ensure that the source directory is in the PYTHONPATH
        # or the import of djblets.util.filesystem will fail.
        current_path = os.getenv("PYTHONPATH")
        if current_path:
            os.putenv("PYTHONPATH", "%s:%s" % (current_path, os.getcwd()))
        else:
            os.putenv("PYTHONPATH", os.getcwd())

        retcode = subprocess.call([
            sys.executable, 'contrib/internal/build-i18n.py'])

        if retcode != 0:
            raise RuntimeError('Failed to build i18n files')

def run_tests(*args):
    import os
    os.system("tests/runtests.py")

test.run_tests = run_tests

cmdclasses = {
    'egg_info': BuildEggInfo,
    'build_media': BuildMedia,
    'build_i18n': BuildI18n,
}

PACKAGE_NAME = 'Djblets'

if is_release():
    download_url = 'http://downloads.reviewboard.org/releases/%s/%s.%s/' % \
                   (PACKAGE_NAME, VERSION[0], VERSION[1])
else:
    download_url = 'http://downloads.reviewboard.org/nightlies/'

setup(name=PACKAGE_NAME,
      version=get_package_version(),
      test_suite="dummy",
      license="MIT",
      description="A collection of useful classes and functions for Django",
      packages=find_packages(exclude=["tests"]),
      cmdclass=cmdclasses,
      install_requires=[
          django_version,
          'django-pipeline>=1.3.23,<1.3.9999',
          'feedparser>=5.1.2',
          'pillowfight',
          'pytz',
      ],
      dependency_links=[
          "http://downloads.reviewboard.org/mirror/",
          download_url,
      ],
      include_package_data=True,
      zip_safe=False,
      maintainer="Christian Hammond",
      maintainer_email="christian@beanbaginc.com",
      url="https://www.reviewboard.org/downloads/djblets/",
      download_url=download_url,
      classifiers=[
          "Development Status :: 4 - Beta",
          "Environment :: Web Environment",
          "Framework :: Django",
          "Intended Audience :: Developers",
          "License :: OSI Approved :: MIT License",
          "Operating System :: OS Independent",
          "Programming Language :: Python",
          "Topic :: Software Development",
          "Topic :: Software Development :: Libraries :: Python Modules",
      ])


# uncompyle6 version 3.7.4
# Python bytecode 2.7 (62211)
# Decompiled from: Python 3.6.9 (default, Apr 18 2020, 01:56:04)
# [GCC 8.4.0]
# Embedded file name: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/oZip/core/text.py
# Compiled at: 2014-09-02 10:16:52
from util import *
from Compression.base import CompressionEngine
from Compression.huffman import *

class TextCompressor(CompressionEngine):

    def __init__(self):
        CompressionEngine.__init__(self, 'Text compressor')

    def compress(self, filename):
        """ Compression function for any non-text file """
        compressor = HuffmanCompressor()
        with open(filename, 'rb') as (file):
            data = file.read().decode('UTF8')
        compressed = compressor(data)
        bin_str = str2bin(compressed)
        new_filename = filename + '.ozip'
        with open(new_filename, 'wb') as (file):
            file.write(bin_str)

    def decompress(self, filename):
        decompressor = HuffmanDecompressor()
        with open(filename, 'rb') as (file):
            data = file.read()
        str_data = bin2str(data)
        decompressed = decompressor(str_data).encode('UTF8')
        new_filename = ('.').join(filename.split('.')[:2])
        with open(new_filename, 'wb') as (file):
            file.write(decompressed)


import json

#Valores del Json
Posibilidades = eInicial = eFinal = False

Mat=[[0,1,2], [3,4,5], [6,7,8]]

with open ("baseC.json","r") as read_file:
	data = json.load(read_file)
	Posibilidades = data['grafo']
	eInicial = data['inicio']
	eFinal = data['final']

#buscar mover el caballo
def bCaballo(x,y,D,Matriz,r):
	e = Mat[x][y]
	for i in Posibilidades:
		if (e == i[0]):
			x = 0
			while(x <= 2):
				y = 0
				while(y <= 2):
					if Mat[x][y] == i[r]:
						Matriz[x][y] = D
						return Matriz
					y = y + 1
				x = x + 1

#Mover los caballos
def cab(MInicial,MFinal,r):
    if r == 0:
        r = 1
    else:
        r = 2
    Matriz=[[0,0,0],[0,0,0],[0,0,0]]
    x = 0
    for x in range(len(Matriz)):
        if x < len(Matriz):
            y = 0
            for y in range(len(Matriz)):
                if y < len(Matriz):
                    if MInicial[x][y] != 0:
                        D = MInicial[x][y]
                        MInicial[x][y] = 0
                        bCaballo(x,y,D, Matriz, r)
                y = y + 1
            x = x + 1

    for e in Matriz:
        print (e)
    print ("========== CAMBIO =============")

    if (Matriz != eFinal):
        return cab(Matriz,MFinal,r)

print("Recorrer derecha escribe 0 o Recorrer izquierda escribe 1: ")
r = int(input())
if (r < 0 or r > 1):
	print ("Escribe un valor correcto.")
else:
    cab(eInicial,eFinal,r)


from __future__ import print_function

import re
import numpy as np
import pandas as pd

from scipy import sparse
from functools import lru_cache
from collections import Counter
from nltk.corpus import stopwords
from pymorphy2 import MorphAnalyzer
from gensim.models import KeyedVectors, Word2Vec
from sklearn.feature_extraction.text import CountVectorizer

STOP_WORDS = list(set(stopwords.words("russian")) | set(stopwords.words("english")))
morph = MorphAnalyzer()
rgx_remove_sym = re.compile(r'[^\w]')

P = "paragraph"  # text
Q = "question"  # text

class FeatureBuilder(object):
    def __init__(self, args):
        # self.w2v_model = KeyedVectors.load_word2vec_format(args.w2v, encoding="utf8", unicode_errors="ignore", binary=True)
        self.w2v_model = None
        self.w2v_pos = ["VERB", "NOUN", "NUMR", "INFN", "GRND", "ADJS", "ADJF", "COMP"]
        self.undefined_pos = Counter()

    def normalize(self, sentence):
        """
        remove symbols -> split by word -> lowercase -> lemmatize -> remove stop words
        :param sentence:
        :return: normalized sentence
        """
        tokens = []
        for word in rgx_remove_sym.sub(" ", sentence).split():
            try:
                token = morph.parse(word.lower())[0]
                if token.normal_form in STOP_WORDS:
                    continue
                tokens.append(token)
            except Exception as ex:
                print("[-] Failed to normalize {} word. Error is \n{}".format(word, ex))
                continue
        return tokens

    @lru_cache()
    def get_norm_pos(self, pos):
        if pos in self.w2v_pos:
            return pos
        elif pos == "NUM":
            return "NUMR"
        else:
            self.undefined_pos.update(pos)
            return None

    def get_log_tf_sum(self, s1, s2):
        tf = Counter()
        [tf.update(x) for x in s1]
        [tf.update(x) for x in s2]

        tf_scores = []
        for w in set(s1) & set(s2):
            w_max = tf.most_common(1)[0][1]
            tf_scores.append(0.5 + 0.5 * (tf.get(w, 0.0) / w_max))

        return np.sum(tf_scores, dtype=np.float32)

    def compute_sim(self, row):
        p_tokens = self.normalize(row[P])
        q_tokens = self.normalize(row[Q])

        q_tokens_size = len(q_tokens)
        cosine_similarities = []

        skipped = 0
        normalized_p = []
        normalized_q = []

        for index, p_token in enumerate(p_tokens):
            try:
                normalized_p.append(p_token.normal_form)

                if index > (q_tokens_size - 1):
                    break
                else:
                    q_token = q_tokens[index]
                    q_pos = self.get_norm_pos(q_token.tag.POS)

                p_pos = self.get_norm_pos(p_token.tag.POS)

                if not p_pos or not q_pos:
                    skipped += 1
                    continue

                p_lemma_poc = "%s_%s" % (p_token.normal_form, p_pos)
                q_lemma_poc = "%s_%s" % (q_token.normal_form, q_pos)

                normalized_q.append(q_token.normal_form)

                w2v_sim_score = self.w2v_model.wv.similarity(p_lemma_poc, q_lemma_poc)
                cosine_similarities.append(w2v_sim_score)
            except Exception as ex:
                skipped += 1
                continue

        cos_sim_sum = np.sum(cosine_similarities, dtype=np.float32)
        idfs_intersection_sum = self.get_log_tf_sum(normalized_p, normalized_q)

        return pd.Series([cos_sim_sum, idfs_intersection_sum], dtype=np.float32)

    def prepare_w2v_model(self, df):
        all_sentences = []
        self.w2v_model = Word2Vec()

    def cosine_sim(self, row):
        pass


import os
import contextlib
import tensorflow as tf
import os
import codecs
import numpy as np
from tqdm import tqdm
import tensorflow as tf
from tensorflow.python import keras
import tensorflow.keras.backend as K
from keras_bert import Tokenizer
from keras_bert import get_custom_objects
from keras_bert import load_trained_model_from_checkpoint
from sklearn.metrics import accuracy_score, f1_score

USE_TPU = False

if USE_TPU:
  TPU_WORKER = 'grpc://' + os.environ['COLAB_TPU_ADDR']
  resolver = tf.contrib.cluster_resolver.TPUClusterResolver(TPU_WORKER)
  tf.contrib.distribute.initialize_tpu_system(resolver)
  strategy = tf.contrib.distribute.TPUStrategy(resolver)

dataset = tf.keras.utils.get_file(
    fname="20news-18828.tar.gz",
    origin="http://qwone.com/~jason/20Newsgroups/20news-18828.tar.gz",
    extract=True,
)

SEQ_LEN = 128
BATCH_SIZE = 16
EPOCHS = 4
LR = 2e-5

pretrained_path = "D:/data/uncased_L-12_H-768_A-12"
config_path = os.path.join(pretrained_path, 'bert_config.json')
checkpoint_path = os.path.join(pretrained_path, 'bert_model.ckpt')
vocab_path = os.path.join(pretrained_path, 'vocab.txt')

token_dict = {}
with codecs.open(vocab_path, 'r', 'utf8') as reader:
    for line in reader:
        token = line.strip()
        token_dict[token] = len(token_dict)

tokenizer = Tokenizer(token_dict)

def load_data(path, tagset):
    """
    Input:
      path: Root directory where the categorical data sit in folders.
      tagset: List of folder-name, category tuples.
    Output:
      train_x / test_x: List with two items (viz token_input, seg_input)
      test_x / test_y: Output labels corresponding to trainx / train_y.
    """
    global tokenizer
    indices, labels = [], []
    for folder, label in tagset:
        folder = os.path.join(path, folder)
        for name in tqdm(os.listdir(folder)):
            with open(os.path.join(folder, name), 'r', encoding="utf-8", errors='ignore') as reader:
                text = reader.read()
            ids, segments = tokenizer.encode(text, max_len=SEQ_LEN)
            indices.append(ids)
            labels.append(label)

    items = list(zip(indices, labels))
    np.random.shuffle(items)
    indices, labels = zip(*items)
    indices = np.array(indices)
    mod = indices.shape[0] % BATCH_SIZE
    if mod > 0:
        indices, labels = indices[:-mod], labels[:-mod]
    return [indices, np.zeros_like(indices)], np.array(labels)

path = os.path.join(os.path.dirname(dataset), '20news-18828')
tagset = [(x, i) for i, x in enumerate(os.listdir(path))]
id_to_labels = {id_: label for label, id_ in tagset}

all_x, all_y = load_data(path, tagset)

train_perc = 0.8
total = len(all_y)

n_train = int(train_perc * total)
n_test = (total - n_train)

test_x = [all_x[0][n_train:], all_x[1][n_train:]]
train_x = [all_x[0][:n_train], all_x[1][:n_train]]

train_y, test_y = all_y[:n_train], all_y[n_train:]

print("# Total: %s, # Train: %s, # Test: %s" % (total, n_train, n_test))

# Load pretrained model
with strategy.scope() if USE_TPU else contextlib.suppress():
    model = load_trained_model_from_checkpoint(
        config_path,
        checkpoint_path,
        training=True,
        trainable=True,
        seq_len=SEQ_LEN,
    )

    # Add dense layer for classification
    inputs = model.inputs[:2]
    dense = model.get_layer('NSP-Dense').output
    outputs = keras.layers.Dense(units=20, activation='softmax')(dense)
    model = keras.models.Model(inputs, outputs)

    model.compile(
        optimizer='adam',
        loss='sparse_categorical_crossentropy',
        metrics=['sparse_categorical_accuracy']
    )

print(model.summary())

# sess = K.get_session()
# uninitialized_variables = set([i.decode('ascii') for i in sess.run(tf.report_uninitialized_variables())])
# init_op = tf.variables_initializer(
#     [v for v in tf.global_variables() if v.name.split(':')[0] in uninitialized_variables]
# )
# sess.run(init_op)

history = model.fit(
    train_x,
    train_y,
    epochs=EPOCHS,
    batch_size=BATCH_SIZE,
    validation_split=0.20,
    shuffle=True,
    verbose=1
)

predicts = model.predict(test_x, verbose=True).argmax(axis=-1)
accuracy = accuracy_score(test_y, predicts)
macro_f1 = f1_score(test_y, predicts, average='macro')
micro_f1 = f1_score(test_y, predicts, average='micro')
weighted_f1 = f1_score(test_y, predicts, average='weighted')

print("Accuracy: %s" % accuracy)
print ('macro_f1: %s\nmicro_f1:%s\nweighted_f1:%s' %(
    macro_f1, micro_f1, weighted_f1)
)