# from .dir import Dir
import pygame
import sys
import os
import math as Math
import numpy as np
import random
import bisect
import time as tijd
import matplotlib.pyplot as plt
from scipy import stats
from pygame import *
from pygame.locals import *
from pygame.sprite import *
from vehicle import Vehicle
from road import Road

# Open the simulation in the upper left corner
os.environ['SDL_VIDEO_WINDOW_POS'] = '0,0'

WIDTH = 1920
HEIGHT = 100
road_length = 12
pygame.display.set_caption('Traffic Simulator')

# Convert meters to pixels
def meter_to_pixel(distance):
    one_m = WIDTH / (road_length * 1000)
    dist = distance * one_m
    return dist

# Convert pixels to meters
def pixel_to_meter(pixels):
    one_p = (road_length * 1000) / WIDTH
    dist = one_p * pixels
    return dist

# Create vehicles
def vehicle_spawn(road, all_cars, max_speed, car_density):
    # Chance for spawning a vehicle
    chance = random.uniform(0, 1)
    if chance < car_density:
        # Chance that the spawned vehicle is a truck
        truck_chance = random.uniform(0,1)
        if truck_chance < 0.80:
            vehicle = Vehicle('car', max_speed, (255, 0, 0), [24/2, 12/2], 10, random.choice(road.pos_lanes), 100 + random.randrange(-10, 10, 2))
        else:
            # Trucks spawning more on the right lane
            choice = random.choices(population = road.pos_lanes, weights = [0, 0.01, 0.1, 0.85])
            vehicle = Vehicle('truck', max_speed, (0, 0, 255), [98/2, 14/2], 10, choice[0], 80 + random.randrange(-5, 5, 1))

        # Don't spawn vehicles on top of eachother
        if not spritecollideany(vehicle, all_cars):
            all_cars.add(vehicle)

            # Put car in the right lane and keep track of which lane the car is
            for number in range(len(road.pos_lanes)):
                if vehicle.y == road.pos_lanes[number]:
                    road.lanes[number].insert(0, vehicle)

    return all_cars

# Check if a vehicle should and can switch lanes
def lane_switching(car, road, all_cars):
    left = True
    right = True
    index = None
    left_follower = None
    left_leader = None
    right_follower = None
    right_leader = None
    car.can_switch = False

    # Failsave so no random switches happen
    left_acc = -200
    right_acc = -200
    follower_left_acc = -200
    follower_right_acc = -200

    # Get the x positions of cars in the adjecent lanes
    if car.lane != 1 and car.lane != 4:
        cars_x_pos_left = ([x_pos.x for x_pos in
                            road.lanes[int(car.lane - 1)-1]])
        cars_x_pos_right = ([x_pos.x for x_pos in
                                road.lanes[int(car.lane + 1)-1]])
        index_left = bisect.bisect(cars_x_pos_left, car.x)
        index_right = bisect.bisect(cars_x_pos_right, car.x)

        left_follower, left_leader = left_right_neighbours(
            index_left, cars_x_pos_left, all_cars)
        right_follower, right_leader = left_right_neighbours(
            index_right, cars_x_pos_right, all_cars)

    # If car is in left most lane
    if car.lane == 1:
        left = False
        cars_x_pos_right = ([x_pos.x for x_pos in
                            road.lanes[int(car.lane + 1)-1]])
        index_right = bisect.bisect(cars_x_pos_right, car.x)
        right_follower, right_leader = left_right_neighbours(
            index_right, cars_x_pos_right, all_cars)

    # If car is in right most lane
    if car.lane == 4:
        right = False
        cars_x_pos_left = ([x_pos.x for x_pos in
                            road.lanes[int(car.lane - 1)-1]])
        index_left = bisect.bisect(cars_x_pos_left, car.x)
        left_follower, left_leader = left_right_neighbours(
            index_left, cars_x_pos_left, all_cars)

    # Leading and following vehicle in current lane.
    leader, _ = neighbour_cars(road, car)

    # Compute current gap and acceleration
    if leader == None:
        current_acc = car.comp_acc(10000, car.max_speed)
    else:
        current_gap = compute_gap(car, leader)
        current_acc = car.comp_acc(current_gap, leader.speed)

    # If car is not in left most lane and has a follower and leader, compute acc
    if left == True and left_follower is not None and left_leader is not None:
        if compute_gap(car, left_leader) > car.gap_want and compute_gap(left_follower, car) > car.gap_want:
            # Check if new follower does not have to break to much
            follower_gap_left = compute_gap(left_follower, car)
            follower_left_acc = left_follower.comp_acc(follower_gap_left, car.speed)

            # Check if there is an increase in acc
            left_gap = compute_gap(car, left_leader)
            left_acc = car.comp_acc(left_gap, left_leader.speed)
    else:
        left_acc = -200
        follower_left_acc = -200

    # If car is not in right most lane and has a follower and leader, compute acc
    if right == True and right_follower is not None and right_leader is not None:
        if compute_gap(car, right_leader) > car.gap_want and compute_gap(right_follower, car) > car.gap_want:
            # Check if new follower does not have to break to much
            follower_gap_right = compute_gap(right_follower, car)
            follower_right_acc = right_follower.comp_acc(
                follower_gap_right, car.speed)

            # Check if there is an increase in acc
            right_gap = compute_gap(car, right_leader)
            right_acc = car.comp_acc(right_gap, right_leader.speed)
    else:
        right_acc = -200
        follower_right_acc = -200

    # Slower vehicles have a tendency to go to the right lane
    if car.max_speed < 80:
        bias_right = car.bias_right - 1
    else:
        bias_right = car.bias_right

    # Check which acceleration is the biggest.
    if left_acc > (current_acc + car.a_thres + car.bias_left) and follower_left_acc > -4:
        car.can_switch = True
        car.left_right = -1
        index = index_left

        # Is right acc more than left acc, thus more than current acc
        if right_acc >= (left_acc + car.a_thres) and follower_right_acc > -4:
            car.left_right = 1
            index = index_right

    # Right acc more than left acc
    if right_acc > (current_acc + car.a_thres + bias_right) and follower_right_acc > -4:
        car.can_switch = True
        car.left_right = 1
        index = index_right

    # It is possible to switch
    if car.can_switch == True:
        car.is_switching = True

        # Update attributes of switching car to new lane
        if car in road.lanes[int(car.lane-1)]:
            # Visualisation for switching lanes
            car.image.fill((0, 255, 0))
            road.lanes[int(car.lane - 1)].remove(car)

            car.lane = car.lane + car.left_right
            road.lanes[int(car.lane - 1)].insert(index, car)

# Returns gap from bumper to bumper in meters.
def compute_gap(follower, leader):
    follower_bumper = follower.x + (follower.size[0] / 2)
    leader_bumper = leader.x - (leader.size[0] / 2)
    if leader.model == 'truck' or follower.model == 'truck':
        gap = leader_bumper - follower_bumper - 20
    else:
        gap = leader_bumper - follower_bumper
    if gap < 0:
        gap = 0.00000001
    return pixel_to_meter(gap)

# Find neighbours of adjacent lane
def left_right_neighbours(index, cars_x_positions, all_cars):
    follower = None
    leader = None

    for check_car in all_cars:
        # If the cars has a leader.
        if index is not len(cars_x_positions):
            if check_car.x == cars_x_positions[index]:
                leader = check_car

        # If the car has a follower.
        if index is not 0:
            if check_car.x == cars_x_positions[index - 1]:
                follower = check_car
    return follower, leader

# Find neighbours of current lane
def neighbour_cars(road, car):
    next_car = None
    prev_car = None

    if car.can_switch == False:
        find_index = road.lanes[int(car.lane - 1)].index(car)

        if find_index + 1 is not len(road.lanes[int(car.lane - 1)]):
            next_car = road.lanes[int(car.lane - 1)][find_index + 1]
        if find_index is not 0:
            prev_car = road.lanes[int(car.lane - 1)][find_index - 1]

    return next_car, prev_car

# This function performs the two-tail Student T-Test on three arrays of data.
# The t and p values are written to a textfile
def stat_an(title, samples1, samples2, samples3):
    t1, p1 = stats.ttest_ind(samples1, samples2, axis=0, equal_var=False)
    t2, p2 = stats.ttest_ind(samples2, samples3, axis=0, equal_var=False)
    t3, p3 = stats.ttest_ind(samples3, samples1, axis=0, equal_var=False)

    file = open(str(title) + "T-Test.txt","w")
    file.write("Below you find the p and t values of the student t-test executed on the three different speeds.\n")
    file.write("80 vs 100 \t | \t t = " + str(t1) + "\t | \t p = " + str(p1) + "\n")
    file.write("100 vs 130 \t | \t t = " + str(t2) + "\t | \t p = " + str(p2) + "\n")
    file.write("130 vs 80 \t | \t t = " + str(t3) +" \t | \t p = " + str(p3) + "\n")
    file.close()

# Main function that simulates the traffic
def traffic(max_speed, car_density):
    # Set the duration of one simulation in seconds
    length_of_simulation = 60
    # Time interval in seconds for datapoint saving
    t = 2

    # Initiate the simulation
    pygame.init()
    clock = pygame.time.Clock()
    clock.tick(60)
    frame = pygame.display.set_mode((WIDTH, HEIGHT), pygame.RESIZABLE)
    background_image = pygame.image.load("Images/4lanes.png")

    start_ticks=pygame.time.get_ticks()
    traf_count = 0
    traf_counts = []
    timestamps = []

    all_cars = Group()

    # Make the road
    road = Road(4)

    while True:
        tijd.sleep(0.005)
        seconds = (pygame.time.get_ticks() - start_ticks)/1000

        # Increment counters when an interval has been completed
        if int(seconds) % t == 0 and int(seconds) not in timestamps:
            timestamps.append(int(seconds))
            traf_counts.append(traf_count)
            traf_count = 0

        # Spawn a vehicle
        all_cars = vehicle_spawn(road, all_cars, max_speed, car_density)

        for car in all_cars:
            next_car, _ = neighbour_cars(road, car)

            # Check if car is already switching
            if car.is_switching is False:
                lane_switching(car, road, all_cars)

            # Y changing from the car to new lane
            if car.can_switch == True:
                car.y += car.left_right

            # Lane switch complete
            if car.can_switch == True:
                if car.y in road.pos_lanes:

                    # Reset attributes
                    car.can_switch = False
                    car.is_switching = False

                    # Visualisation for car back in lane
                    if car.model == 'car':
                        car.image.fill((255, 0, 0))
                    else:
                        car.image.fill((0, 0, 255))

            # Change speed of the car depending on the leading car
            if next_car is not None:
                gap = compute_gap(car, next_car)
                acc = car.comp_acc(gap, next_car.speed)
                car.speed += acc

                # prevent cars from going backwards.
                if car.speed < 0:
                    car.speed = 0
            else:
                # No leading car (free flow)
                gap = 10000
                car.speed += car.comp_acc(gap, car.max_speed)

                if car.speed < 0:
                    car.speed = 0

            # Move the car
            car.move()

            # Remove cars that exit the screen
            if car.x > WIDTH:
                traf_count += 1
                road.lanes[int(car.lane - 1)].pop()
                all_cars.remove(car)

        # End the simulation when the set time has been reached
        if seconds > length_of_simulation:
            pygame.quit()
            # total average number of vehicles per time interval
            trafficflow = (np.sum(traf_counts) / seconds) * t
            return trafficflow, traf_counts , timestamps

        # End the simulation when esc is pressed or the window is closed
        for event in pygame.event.get():
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    pygame.quit()
                if event.key == K_SPACE:
                    tijd.sleep(4)

            if event.type == pygame.QUIT:
                pygame.quit()

        # Make pygame
        frame.blit(background_image, [0, 0])
        all_cars.draw(frame)
        display.update()
        pygame.display.flip()

if __name__ == '__main__':

    # Set to True or False depending on what simulation you want to run.
    # When set to True, more cars will be spawned on the road. When set to False, less cars will be created
    rush_hour = False

    # Set variables depending on Rush Hour for user friendliness
    if rush_hour == True:
        spawn_rate = 0.3
        # plt.title("Traffic Flow at Different Speeds During Rush Hour")
        title = "RushHour"
    elif rush_hour == False:
        spawn_rate = 0.1
        # plt.title("Traffic Flow at Different Speeds Outside Rush Hour")
        title = "NoRushHour"

    # Initialize arrays to save datapoints, length depends on duration of the simulation and the timestamps
    length = 31
    intervals1 = np.array([0] * length)
    intervals2 = np.array([0] * length)
    intervals3 = np.array([0] * length)
    tf80 = []
    tf100 = []
    tf130 = []

    # Run each different speed ten times. Save the total traffic flow and the traffic flow during each interval
    for i in range(10):
        print("You are now running round ", i, " of 10")

        tf_l, interval_l, time_l = traffic(80, spawn_rate)
        tf80.append(tf_l)
        intervals1 = np.add(intervals1, np.array(interval_l))

        tf_l, interval_l, time_l = traffic(100, spawn_rate)
        tf100.append(tf_l)
        intervals2 = np.add(intervals2, np.array(interval_l))

        tf_l, interval_l, time_l = traffic(130, spawn_rate)
        tf130.append(tf_l)
        intervals3 = np.add(intervals3, np.array(interval_l))

    # Create plot figure
    plt.figure(figsize=(20, 10))
    plt.rcParams.update({'font.size': 22})

    # Plot the intervals and the average traffic flow of each speed
    plt.plot(time_l, intervals1 / 10, label="Trafficflow per time unit at %i km/h" % (80), c="green")
    plt.plot(time_l, [np.mean(tf80)] * len(time_l), label="Average trafficflow at %i km/h" % (80), c="lightgreen")

    plt.plot(time_l, intervals2 / 10, label="Trafficflow per time unit at %i km/h" % (100), c="blue")
    plt.plot(time_l, [np.mean(tf100)] * len(time_l), label="Average trafficflow at %i km/h" % (100), c="lightblue")

    plt.plot(time_l, intervals3 / 10, label="Trafficflow per time unit at %i km/h" % (130), c="red")
    plt.plot(time_l, [np.mean(tf130)]  * len(time_l), label="Average trafficflow at %i km/h" % (130), c="pink")

    # Set graph attributes
    if rush_hour == True:
        plt.title("Traffic Flow at Different Speeds During Rush Hour")
    elif rush_hour == False:
        plt.title("Traffic Flow at Different Speeds Outside Rush Hour")
    plt.xlabel("Time in seconds")
    plt.ylabel("Number of vehicles")
    plt.legend(loc=4)
    plt.show()
    plt.savefig('TrafficFlows'+str(title)+'.png')

    # Perform statistical analysis
    stat_an(title, intervals1, intervals2, intervals3)


from cfmodel import CfModel
from sklearn.metrics import recall_score, make_scorer
from utils import *

class MLP(CfModel):
    """
    Create a MLP model object
    """

    def __init__(self, l90d: pd.DataFrame, n180d: pd.DataFrame, growth_data: pd.DataFrame, stable_data: pd.DataFrame,
                 features: List[str], subset_type: str):
        super().__init__(l90d, n180d, growth_data, stable_data, features, subset_type)
        self.get_x_train_y_train()
        self.model, self.y_pred = self.train()
        self.pred, self.true = self.get_subset_pred_true()
        self.filtered_pred = filter_predicted_growth_subset(self.pred) if self.subset_type == 'growth' else \
            filter_predicted_stable_subset(self.pred)

        print('MLP Model has finished training')

    def find_best_params(self):
        params_simple_nn = {
            'neurons': (10, 100),
            'activation': (0, 8),
            'learning_rate': (0.01, 1),
            'batch_size': (200, 1000),
            'epochs': (20, 100),
            'layers1': (1, 5)
        }

        scorer_rec = make_scorer(recall_score)
        bo_simple_nn(self.X_train, self.y_train, scorer=scorer_rec, params=params_simple_nn)

    def train(self) -> (KerasClassifier, pd.DataFrame):
        params = {
            'stable': {'learning_rate': 0.04374, 'neurons': 95, 'layers1': 4, 'epochs': 94, 'batch_size': 958,
                       'activation': 'softsign'},
            'growth': {'learning_rate': 0.01, 'neurons': 68, 'layers1': 5, 'epochs': 100, 'batch_size': 973,
                       'activation': 'softsign'}
        }

        # Set default values if subset_type is not 'stable'
        chosen_params = params[self.subset_type]

        learning_rate = chosen_params['learning_rate']
        neurons = chosen_params['neurons']
        layers1 = chosen_params['layers1']
        epochs = chosen_params['epochs']
        batch_size = chosen_params['batch_size']
        activation = chosen_params['activation']

        def nn_cl_fun():
            opt = Adam(learning_rate=learning_rate)
            nn = Sequential()
            nn.add(Dense(neurons, input_dim=len(self.X_train.columns), activation=activation))
            for i in range(layers1):
                nn.add(Dense(neurons, activation=activation))
            nn.add(Dense(1, activation='sigmoid'))
            nn.compile(loss='binary_crossentropy', optimizer=opt, metrics=['accuracy', recall])
            return nn

        nn = KerasClassifier(build_fn=nn_cl_fun, epochs=epochs, batch_size=batch_size,
                             verbose=0)
        nn.fit(self.X_train, self.y_train, validation_data=(self.X_test, self.y_test), verbose=0)
        y_pred = nn.predict(self.X_test)
        return nn, y_pred


import torch
import torch.nn as nn
from torch.optim import Adam
from torch.utils.data import DataLoader, TensorDataset
from os import listdir
import numpy as np
from tqdm import tqdm

class ActorNetwork(nn.Module):
    def __init__(self, state_dim: int, hidden_dim: int):
        super().__init__()
        self.model = nn.Sequential(
            nn.Linear(state_dim, hidden_dim),
            nn.ReLU(),
            nn.Linear(hidden_dim, hidden_dim),
            nn.ReLU(),
            nn.Linear(hidden_dim, hidden_dim),
            nn.ReLU(),
            nn.Linear(hidden_dim, 1)
        )

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        return self.model(x)

def get_data_part(path: str):
    file_names = listdir(path)
    ids = np.array(list(set([int(file_name.split(":")[0]) for file_name in file_names])))

    indices = np.random.permutation(np.arange(len(ids)))
    ids = ids[indices]

    for id in ids:
        action_file_name = str(id) + ":actions.npy"
        state_file_name = str(id) + ":states.npy"

        X = torch.FloatTensor(np.load(f"{path}/{state_file_name}"))
        y = torch.FloatTensor(np.load(f"{path}/{action_file_name}"))

        yield X, y

def train(model: ActorNetwork, num_epochs: int, data_path: str, save_path: str):
    optimizer = Adam(params=model.parameters(), weight_decay=1e-4)
    mse_loss = nn.MSELoss()

    for epoch in range(num_epochs):
        total_loss = 0.
        num_parts = 0

        for X, y in tqdm(get_data_part(data_path)):

            num_batches = 0
            batch_total_loss = 0.
            train_dataloader = DataLoader(TensorDataset(X, y), batch_size=512, shuffle=True)

            for X_batch, y_batch in train_dataloader:
                optimizer.zero_grad()

                logits = model(X_batch)
                loss = mse_loss(logits, y_batch)
                loss.backward()
                optimizer.step()

                num_batches += 1
                batch_total_loss += loss.item()

            total_loss += (batch_total_loss / num_batches)
            num_parts += 1

        print(f"Epoch: {epoch}")
        print(f"Average MSE loss: {round(total_loss / num_parts, 4)}")
        torch.save(model.state_dict(), save_path)

SAVE_SIZE = 100_000
PREY_FEATURE_SIZE = 42
PREDATOR_FEATURE_SIZE = 52
HIDDEN_DIM = 64
NUM_EPOCHS = 50
DATA_PATH = 'imitation/prey'
SAVE_PATH = 'imitation/prey_model.pkl'

if __name__ == "__main__":
    model = ActorNetwork(PREY_FEATURE_SIZE, HIDDEN_DIM)
    train(model, num_epochs=NUM_EPOCHS, data_path=DATA_PATH, save_path=SAVE_PATH)
    print(f"Model successfully saved!")


#!/usr/bin/env python
from imutils.video import VideoStream
import rospy
import datetime
import argparse
import imutils
import time
import cv2

ap = argparse.ArgumentParser()
ap.add_argument("-p", "--picamera", type=int, default=-1,
	help="whether or not the Raspberry Pi camera should be used")

args2 = ap.parse_args(rospy.myargv()[1:])
args = vars(ap.parse_args())

rospy.init_node('sdk_navigator', anonymous=True)

vs = VideoStream(usePiCamera=args["picamera"] > 0).start()
time.sleep(2.0)

while True:
	# grab the current frame and initialize the status text
	frame = vs.read()
	frame = imutils.resize(frame, width=400)
	status = "No Targets"

	gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
	blurred = cv2.GaussianBlur(gray, (5, 5), 0)
	thresh = cv2.threshold(blurred, 110, 255, cv2.THRESH_BINARY_INV)[1]

	# find contours in the edge map
	cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
		cv2.CHAIN_APPROX_SIMPLE)
	cnts = imutils.grab_contours(cnts)

	for c in cnts:
		# approximate the contour
		peri = cv2.arcLength(c, True)
		approx = cv2.approxPolyDP(c, 0.01 * peri, True)

		# ensure that the approximated contour is "roughly" rectangular
		if len(approx) >= 4 and len(approx) <= 6:
			# compute the bounding box of the approximated contour and
			# use the bounding box to compute the aspect ratio
			(x, y, w, h) = cv2.boundingRect(approx)
			aspectRatio = w / float(h)

			# compute the solidity of the original contour
			area = cv2.contourArea(c)
			hullArea = cv2.contourArea(cv2.convexHull(c))
			solidity = area / float(hullArea)

			# compute whether or not the width and height, solidity, and
			# aspect ratio of the contour falls within appropriate bounds
			keepDims = w > 25 and h > 25
			keepSolidity = solidity > 0.9
			keepAspectRatio = aspectRatio >= 0.8 and aspectRatio <= 1.2

			# ensure that the contour passes all our tests
			if keepDims and keepSolidity and keepAspectRatio:
				# draw an outline around the target and update the status
				# text
				cv2.drawContours(frame, [approx], -1, (0, 0, 255), 4)
				status = "Target(s) Acquired"

				# compute the center of the contour region and draw the
				# crosshairs
				M = cv2.moments(approx)
				(cX, cY) = (int(M["m10"] // M["m00"]), int(M["m01"] // M["m00"]))
				(startX, endX) = (int(cX - (w * 0.15)), int(cX + (w * 0.15)))
				(startY, endY) = (int(cY - (h * 0.15)), int(cY + (h * 0.15)))
				cv2.line(frame, (startX, cY), (endX, cY), (0, 0, 255), 3)
				cv2.line(frame, (cX, startY), (cX, endY), (0, 0, 255), 3)
				print(cX, cY)

	cv2.putText(frame, status, (20, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
		(0, 0, 255), 2)

	# show the frame and record if a key is pressed
	cv2.imshow("Frame", frame)
	key = cv2.waitKey(1) & 0xFF

	# if the 'q' key is pressed, stop the loop
	if key == ord("q"):
		break

# cleanup the camera and close any open windows
camera.release()
cv2.destroyAllWindows()


from faker import Faker

from authelio_sdk.client import Client

def test_FUNC_client_user_create_WITH_valid_user_attributes_EXPECT_user_created(
        sdk_client: Client,
        faker: Faker
) -> None:
    """
    Check whether a new user can be created.

    :param sdk_client: Authelio SDK client fixture.
    :param faker: Faker fixture.

    :return: No return.
    """
    user = sdk_client.user.create(
        email=faker.email(),
        username=faker.user_name(),
        first_name=faker.first_name(),
        last_name=faker.last_name()
    )

    assert user.is_active, user

    sdk_client.user.delete(user.user_id)


import config
import rules.welcome
import datetime
from bson.json_util import dumps
from constants import states, actions
from telnetlib import STATUS
from durable.lang import update_state, post, get_state
from spade.agent import Agent
from spade.behaviour import PeriodicBehaviour, OneShotBehaviour, TimeoutBehaviour
from spade.message import Message
from spade.template import Template

INITIAL_CONTEXT = {
  "status": states.WELCOME,
  "prev_status": "",
  "person": "",
  "message": "",
  "reply": "",
  "action": "",
  "response": "",
  "requirements": "",
  "budget": 0,
  "selected_cluster": "",
  "satisfaction": ""
}

estado= 0
nombre = ''
es_nombre = False

class InitConversatopmBehav(TimeoutBehaviour):
  async def run(self):
    update_state("welcome", INITIAL_CONTEXT)
    ## Agregamos behaviour para recibir mensajes desde el usuario
    recv_user_message_behv = RecvUserMessageBehav(period=0.1)
    recv_user_message_behv_template = Template()
    recv_user_message_behv_template.to = config.AGENT_MAIN_USER
    self.agent.add_behaviour(recv_user_message_behv)

    reply_user_message_behv = ReplyUserMessageBehav(period=0.1)
    self.agent.add_behaviour(reply_user_message_behv)

    rules_actions_behav = RulesActionsBehav(period=0.1)
    self.agent.add_behaviour(rules_actions_behav)

"""
Behavior para recibir los mensajes desde el usuario final
y actualizar el estado global del motor de reglas
"""
class RecvUserMessageBehav(PeriodicBehaviour):
  async def run(self):
    msg_received = await self.receive()
    if msg_received and config.END_USER in str(msg_received.sender):

      ## Obtengo el state actual para actualizarlo
      state = get_state("welcome")
      state["message"] = msg_received.body.lower()

      ## Actualizamos el status global
      update_state("welcome", state)

"""
Behavior para enviar mensajes al usuario basado en la propiedad "reply"
desde el estado global del motor de reglas
"""
class ReplyUserMessageBehav(PeriodicBehaviour):
  async def run(self):
    state = get_state("welcome")
    if state["reply"]:
      ## Enviamos Reply al usuario
      msg_reply = Message(to=config.END_USER)
      msg_reply.body = state["reply"]
      await self.send(msg_reply)

      ## Actualizamos el status global
      state["reply"] = ""
      update_state("welcome", state)

"""
Behavior para ejecutar acciones dado por el motor de reglas
"""
class RulesActionsBehav(PeriodicBehaviour):
  async def run(self):
    state = get_state("welcome")
    if state["action"] == actions.RESET:
      update_state("welcome", INITIAL_CONTEXT)
    elif state["action"] == actions.EXTRACT_NAME and state["message"] != "":
      msg = Message(to=config.AGENT_LANG_USER)
      msg.set_metadata("action", actions.EXTRACT_NAME)
      msg.body = state["message"]
      await self.send(msg)

      state["message"] = ""
      update_state("welcome", state)
    elif state["action"] == actions.EXTRACT_REQUIREMENTS and state["message"] != "":
      msg = Message(to=config.AGENT_LANG_USER)
      msg.set_metadata("action", actions.EXTRACT_REQUIREMENTS)
      msg.body = state["message"]
      await self.send(msg)

      state["message"] = ""
      update_state("welcome", state)
    elif state["action"] == actions.LOOK_FOR_REQUIREMENT and state["requirements"] != "":
      msg = Message(to=config.AGENT_DATA_USER)
      msg.set_metadata("action", actions.LOOK_FOR_REQUIREMENT)
      msg.body = state["requirements"]
      await self.send(msg)

      state["action"] = actions.LOOK_FOR_REQUIREMENT_RESPONSE
      update_state("welcome", state)
    elif state["action"] == actions.LOOK_FOR_EDGE_COMPUTERS:
      msg = Message(to=config.AGENT_DATA_USER)
      msg.set_metadata("action", actions.LOOK_FOR_EDGE_COMPUTERS)
      await self.send(msg)

      state["action"] = actions.LOOK_FOR_EDGE_COMPUTERS_RESPONSE
      update_state("welcome", state)
    elif state["action"] == actions.INSERT_REQUIREMENTS:
      msg = Message(to=config.AGENT_DATA_USER)
      msg.set_metadata("action", actions.INSERT_REQUIREMENTS)
      msg.body = dumps({
        "cluster": state["selected_cluster"],
        "requirements": state["requirements"],
      })
      await self.send(msg)

      state["action"] = actions.INSERT_REQUIREMENTS_RESPONSE
      state["message"] = ""
      update_state("welcome", state)
    elif state["action"] == actions.LOOK_FOR_COMPUTERS_RECOMMEND:
      msg = Message(to=config.AGENT_DATA_USER)
      msg.set_metadata("action", actions.LOOK_FOR_COMPUTERS_RECOMMEND)
      msg.body = dumps({
        "requirements": state["requirements"],
        "budget": state["budget"],
      })
      await self.send(msg)

      state["message"] = ""
      state["response"] = ""
      state["action"] = actions.LOOK_FOR_COMPUTERS_RECOMMEND_RESPONSE
      update_state("welcome", state)
    elif state["action"] == actions.INSERT_SATISFACTION and state["reply"] == "":
      msg = Message(to=config.AGENT_DATA_USER)
      msg.set_metadata("action", actions.INSERT_SATISFACTION)
      msg.body = dumps({
        "satisfaction": int(state["satisfaction"]),
      })
      await self.send(msg)

      state["message"] = ""
      state["response"] = ""
      update_state("welcome", state)

"""
Behavior para capturar los mensajes por parte del agente de datos
"""
class RecvDataBehav(PeriodicBehaviour):
  async def run(self):
    msg_received = await self.receive()
    if msg_received and msg_received.get_metadata("action") == actions.LOOK_FOR_REQUIREMENT:
      ## Actualizar el estado global del motor de reglas
      state = get_state("welcome")
      state["response"] = msg_received.body
      update_state("welcome", state)
    elif msg_received and msg_received.get_metadata("action") == actions.LOOK_FOR_EDGE_COMPUTERS:
      ## Actualizar el estado global del motor de reglas
      state = get_state("welcome")
      state["response"] = msg_received.body
      update_state("welcome", state)
    elif msg_received and msg_received.get_metadata("action") == actions.INSERT_REQUIREMENTS:
      ## Actualizar el estado global del motor de reglas
      state = get_state("welcome")
      state["response"] = msg_received.body
      update_state("welcome", state)
    elif msg_received and msg_received.get_metadata("action") == actions.LOOK_FOR_COMPUTERS_RECOMMEND:
      ## Actualizar el estado global del motor de reglas
      state = get_state("welcome")
      state["response"] = msg_received.body
      update_state("welcome", state)
    elif msg_received and msg_received.get_metadata("action") == actions.INSERT_SATISFACTION:
      ## Actualizar el estado global del motor de reglas
      state = get_state("welcome")
      state["response"] = msg_received.body
      update_state("welcome", state)

"""
Behavior para capturar los mensajes por parte del agente language
"""
class RecvLangBehav(PeriodicBehaviour):
  async def run(self):
    msg_received = await self.receive()
    if msg_received and msg_received.get_metadata("action") == actions.EXTRACT_NAME:
      ## Actualizar el estado global del motor de reglas
      state = get_state("welcome")
      state["response"] = msg_received.body
      update_state("welcome", state)
    elif msg_received and msg_received.get_metadata("action") == actions.EXTRACT_REQUIREMENTS:
      ## Actualizar el estado global del motor de reglas
      state = get_state("welcome")
      state["response"] = msg_received.body
      update_state("welcome", state)

class MainAgent(Agent):
  class BehavSubscribe(OneShotBehaviour):
    def on_available(self, jid, stanza):
      print("[{}] Agent {} is available.".format(self.agent.name, jid.split("@")[0]))

    def on_subscribed(self, jid):
      print("[{}] Agent {} has accepted the subscription.".format(self.agent.name, jid.split("@")[0]))
      print("[{}] Contacts List: {}".format(self.agent.name, self.agent.presence.get_contacts()))

    def on_subscribe(self, jid):
      print("[{}] Agent {} asked for subscription. Let's aprove it.".format(self.agent.name, jid.split("@")[0]))
      self.presence.approve(jid)

    async def run(self):
      # print("Intentar subscribir")
      self.presence.on_subscribe = self.on_subscribe
      self.presence.on_subscribed = self.on_subscribed
      self.presence.on_available = self.on_available
      self.presence.set_available()
      # self.presence.subscribe(config.AGENT_LANG_USER)
      self.presence.subscribe(config.END_USER)

  async def setup(self):
    print("Hola!. Soy el agente Asistente. Mi ID es \"{}\"".format(str(self.jid)))

    self.add_behaviour(self.BehavSubscribe())

    start_at = datetime.datetime.now() + datetime.timedelta(seconds=5)
    init_user_conversation_behv = InitConversatopmBehav(start_at=start_at)
    self.add_behaviour(init_user_conversation_behv)

    recv_lang_behav = RecvLangBehav(period=0.1)
    recv_lang_behav_template = Template()
    recv_lang_behav_template.to = config.AGENT_MAIN_USER
    recv_lang_behav_template.sender = config.AGENT_LANG_USER
    self.add_behaviour(recv_lang_behav, recv_lang_behav_template)

    recv_data_behav = RecvDataBehav(period=0.1)
    recv_data_behav_template = Template()
    recv_data_behav_template.to = config.AGENT_MAIN_USER
    recv_data_behav_template.sender = config.AGENT_DATA_USER
    self.add_behaviour(recv_data_behav, recv_data_behav_template)

  def stop(self):
    print("Deteniendo agente \"{}\"".format(str(self.jid)))
    return Agent.stop(self)


"""
pip3 install pillow
"""

from PIL import Image, ImageDraw,ImageFont

img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))

draw = ImageDraw.Draw(img, mode='RGB')

font = ImageFont.truetype("Monaco.ttf", 28)

draw.text([0, 0], 'python', "red", font=font)

# 保存在本地
with open('code.png', 'wb') as f:
    img.save(f, format='png')


from __future__ import print_function

import code0_hyperParameter as code0
import code1_data as code1
import code2_model as code2
import code3_runEpoch as code3
import uril_tools as aux
import tensorflow as tf
import numpy as np
import pandas as pd

import datetime

np.set_printoptions(threshold=np.inf)

def run_model(run_type,include_model,cv = 1):
    from time import time
    start = time()

    timeStampe = datetime.datetime.now().strftime("%m-%d-%H-%M")
    result_file_name_train = 'resources/result_'+str(timeStampe)+'_'+str(run_type)+'_'+str(include_model)+'_train.csv'
    result_file_name_test = 'resources/result_'+str(timeStampe)+'_'+str(run_type)+'_'+str(include_model)+'_test.csv'
    result_file_name_est = 'resources/result_' + str(timeStampe) + '_' + str(run_type) + '_' + str(
        include_model) + '_test_estimates.csv'
    result_tmp = pd.DataFrame(columns=('rmse', 'auc', 'r2','cv','epoch'))
    result_tmp.to_csv(result_file_name_train)
    result_tmp.to_csv(result_file_name_test)

    code0.RUN_TYPE = run_type
    code0.INCLUDE_MODEL = include_model

    aux.check_directories()

    dt = code1.ReadData()
    dataset, labels = dt.create_label_and_delete_last_one()
    tuple_data = dt.convert_data_labels_to_tuples(dataset, labels)

    skill_num = max(dataset['skill_id'].unique()) + 1
    skill_set = dataset['skill_id'].unique()
    seq_width = len(dt.features_only_names)

    config = code0.ModelParamsConfig(skill_num, seq_width, skill_set)
    eval_config = code0.ModelParamsConfig(skill_num, seq_width, skill_set)

    if code0.NUM_STEPS != 0:
        config.num_steps = code0.NUM_STEPS
    else:
        config.num_steps = aux.get_num_step(dataset['user_id'])

    eval_config.num_steps = config.num_steps
    eval_config.batch_size = 2

    config.print_config('traing')
    eval_config.print_config('testing')

    # name_list = ['cv', 'epoch', 'type', 'rmse', 'auc', 'r2', 'inter_rmse', 'inter_auc', 'inter_r2', 'intra_rmse',
    #              'intra_auc', 'intra_r2']
    # result_data = pd.DataFrame(columns=name_list)
    if cv ==1:
        CVname = ['c1']
    elif cv ==5:
        CVname = ['c1', 'c2', 'c3', 'c4', 'c5']
    size = len(tuple_data)

    for index, cv_num_name in enumerate(CVname):
        # timeStampe = datetime.datetime.now().strftime("%m-%d-%H:%M")

        train_tuple_rows = tuple_data[:int(index * 0.2 * size)] + tuple_data[int((index + 1) * 0.2 * size):]
        test_tuple_rows = tuple_data[int(index * 0.2 * size): int((index + 1) * 0.2 * size)]

        with tf.Graph().as_default(), tf.Session() as session:
            initializer = tf.random_uniform_initializer(-config.init_scale, config.init_scale)
            # training model
            print("\n==> Load Training model")
            with tf.variable_scope("model", reuse=None, initializer=initializer):
                m = code2.Model(is_training=True, config=config)

            # testing model
            print("\n==> Load Testing model")
            with tf.variable_scope("model", reuse=True, initializer=initializer):
                mtest = code2.Model(is_training=False, config=eval_config)

            # tf.initialize_all_variables().run()
            tf.global_variables_initializer().run()

            print("==> begin to run epoch...")
            for i in range(config.max_max_epoch):
                lr_decay = config.lr_decay ** max(i - config.max_epoch, 0)
                m.assign_lr(session, config.learning_rate * lr_decay)

                rt = session.run(m.lr)
                train_result, estimates = code3.run_epoch(session, m, train_tuple_rows, m.train_op, verbose=True)

                # for e in range(10):
                #     print(estimates[e])

                print('Train ', cv_num_name, "-" * 10, str(i), '/',
                          str(config.max_max_epoch ), "-" * 10)
                print(train_result)

                train_result['cv'] = cv_num_name
                train_result['epoch'] = i
                train_result.to_csv(result_file_name_train, mode='a',header=False)

                display = 5
                if ((i + 1) % display == 0):
                    print ("\n")
                    print('Test ', cv_num_name, "=" * 30, str(int((i + 1) / display)), '/',
                          str(int((config.max_max_epoch + 1) / display)), "=" * 30)
                    test_result, estimates = code3.run_epoch(session, mtest, test_tuple_rows, tf.no_op())
                    test_result['cv'] = cv_num_name
                    test_result['epoch'] = int((i + 1) / display)
                    test_result.to_csv(result_file_name_test, mode='a',header=False)
                    print(test_result)
                    print('END--', "=" * 80)
                    # if int((i + 1) / display)>=8:
                    #     test_result['cv'] = cv_num_name
                    #     test_result['epoch'] = int((i + 1) / display)>=8
                    #     test_result.to_csv(result_file_name_test, mode='a',header=False)
                    #     print('++> save result to ', result_file_name_test)
    print("==> Finsih! whole process!")
    print("\n\n\nTotal Runtime: {:<.2f}s".format(time()-start))

def main(unused_args):
    for run_type in [1]:  #these file name can be found in the file: code1_data.py
        for include_model in [4]:
            try:
                run_model(run_type,include_model,cv=5)
            except Exception:
                timeStampe = datetime.datetime.now().strftime("%m-%d-%H-%M")
                result_file_name = 'resources/result_'+str(timeStampe)+'_'+str(run_type)+'_'+str(include_model)+'.csv'
                result = pd.DataFrame({'info':'ERROR'})
                result.to_csv(result_file_name)

if __name__ == "__main__":
    tf.app.run()


# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

'Commands exposed to commandlines'

import logging
from argparse import ArgumentParser
from json import dump as json_dump
import os
import sys

from compare_locales import mozpath
from compare_locales import version
from compare_locales.paths import EnumerateApp, TOMLParser, ConfigNotFound
from compare_locales.compare import compareProjects

class CompareLocales:
    """Check the localization status of gecko applications.
The first arguments are paths to the l10n.toml or ini files for the
applications, followed by the base directory of the localization repositories.
Then you pass in the list of locale codes you want to compare. If there are
no locales given, the list of locales will be taken from the l10n.toml file
or the all-locales file referenced by the application\'s l10n.ini."""

    def __init__(self):
        self.parser = self.get_parser()

    def get_parser(self):
        """Get an ArgumentParser, with class docstring as description.
        """
        parser = ArgumentParser(description=self.__doc__)
        parser.add_argument('--version', action='version',
                            version='%(prog)s ' + version)
        parser.add_argument('-v', '--verbose', action='count',
                            default=0, help='Make more noise')
        parser.add_argument('-q', '--quiet', action='count',
                            default=0, help='''Show less data.
Specified once, don't show obsolete entities. Specified twice, also hide
missing entities. Specify thrice to exclude warnings and four times to
just show stats''')
        parser.add_argument('--validate', action='store_true',
                            help='Run compare-locales against reference')
        parser.add_argument('-m', '--merge',
                            help='''Use this directory to stage merged files,
use {ab_CD} to specify a different directory for each locale''')
        parser.add_argument('config_paths', metavar='l10n.toml', nargs='+',
                            help='TOML or INI file for the project')
        parser.add_argument('l10n_base_dir', metavar='l10n-base-dir',
                            help='Parent directory of localizations')
        parser.add_argument('locales', nargs='*', metavar='locale-code',
                            help='Locale code and top-level directory of '
                                 'each localization')
        parser.add_argument('--json',
                            help='''Serialize to JSON. Value is the name of
the output file, pass "-" to serialize to stdout and hide the default output.
''')
        parser.add_argument('-D', action='append', metavar='var=value',
                            default=[], dest='defines',
                            help='Overwrite variables in TOML files')
        parser.add_argument('--full', action="store_true",
                            help="Compare sub-projects that are disabled")
        parser.add_argument('--return-zero', action="store_true",
                            help="Return 0 regardless of l10n status")
        parser.add_argument('--clobber-merge', action="store_true",
                            default=False, dest='clobber',
                            help="""WARNING: DATALOSS.
Use this option with care. If specified, the merge directory will
be clobbered for each module. That means, the subdirectory will
be completely removed, any files that were there are lost.
Be careful to specify the right merge directory when using this option.""")
        return parser

    @classmethod
    def call(cls):
        """Entry_point for setuptools.
        The actual command handling is done in the handle() method of the
        subclasses.
        """
        cmd = cls()
        args = cmd.parser.parse_args()
        return cmd.handle(**vars(args))

    def handle(
        self,
        quiet=0, verbose=0,
        validate=False,
        merge=None,
        config_paths=[], l10n_base_dir=None, locales=[],
        defines=[],
        full=False,
        return_zero=False,
        clobber=False,
        json=None,
    ):
        """The instance part of the classmethod call.

        Using keyword arguments as that is what we need for mach
        commands in mozilla-central.
        """
        # log as verbose or quiet as we want, warn by default
        logging_level = logging.WARNING - (verbose - quiet) * 10
        logging.basicConfig()
        logging.getLogger().setLevel(logging_level)

        config_paths, l10n_base_dir, locales = self.extract_positionals(
            validate=validate,
            config_paths=config_paths,
            l10n_base_dir=l10n_base_dir,
            locales=locales,
        )

        # when we compare disabled projects, we set our locales
        # on all subconfigs, so deep is True.
        locales_deep = full
        configs = []
        config_env = {
            'l10n_base': l10n_base_dir
        }
        for define in defines:
            var, _, value = define.partition('=')
            config_env[var] = value
        for config_path in config_paths:
            if config_path.endswith('.toml'):
                try:
                    config = TOMLParser().parse(config_path, env=config_env)
                except ConfigNotFound as e:
                    self.parser.exit('config file %s not found' % e.filename)
                if locales_deep:
                    if not locales:
                        # no explicit locales given, force all locales
                        config.set_locales(config.all_locales, deep=True)
                    else:
                        config.set_locales(locales, deep=True)
                configs.append(config)
            else:
                app = EnumerateApp(config_path, l10n_base_dir)
                configs.append(app.asConfig())
        try:
            observers = compareProjects(
                configs,
                locales,
                l10n_base_dir,
                quiet=quiet,
                merge_stage=merge, clobber_merge=clobber)
        except OSError as exc:
            print("FAIL: " + str(exc))
            self.parser.exit(2)

        if json is None or json != '-':
            details = observers.serializeDetails()
            if details:
                print(details)
            if len(configs) > 1:
                if details:
                    print('')
                print("Summaries for")
                for config_path in config_paths:
                    print("  " + config_path)
                print("    and the union of these, counting each string once")
            print(observers.serializeSummaries())
        if json is not None:
            data = [observer.toJSON() for observer in observers]
            stdout = json == '-'
            indent = 1 if stdout else None
            fh = sys.stdout if stdout else open(json, 'w')
            json_dump(data, fh, sort_keys=True, indent=indent)
            if stdout:
                fh.write('\n')
            fh.close()
        rv = 1 if not return_zero and observers.error else 0
        return rv

    def extract_positionals(
        self,
        validate=False,
        config_paths=[], l10n_base_dir=None, locales=[],
    ):
        # using nargs multiple times in argparser totally screws things
        # up, repair that.
        # First files are configs, then the base dir, everything else is
        # locales
        all_args = config_paths + [l10n_base_dir] + locales
        config_paths = []
        # The first directory is our l10n base, split there.
        while all_args and not os.path.isdir(all_args[0]):
            config_paths.append(all_args.pop(0))
        if not config_paths:
            self.parser.error('no configuration file given')
        for cf in config_paths:
            if not os.path.isfile(cf):
                self.parser.error('config file %s not found' % cf)
        if not all_args:
            self.parser.error('l10n-base-dir not found')
        l10n_base_dir = mozpath.abspath(all_args.pop(0))
        if validate:
            # signal validation mode by setting locale list to [None]
            locales = [None]
        else:
            locales = all_args

        return config_paths, l10n_base_dir, locales


__author__ = 'luowen'

"this is a log demo"

import logging

"""
logging.warning("Watch out")
logging.info("this is a information")
logging.debug("this is a debug information")
logging.error("error.")
"""

logging.basicConfig(filename="logDemo.log", format="%(levelname)s: %(asctime)s : %(message)s", level=logging.DEBUG, filemode="w")

logging.info("application startt")

logging.info("application do somesting.")

logging.info("application finished")


from django.urls import path
from .views import ProductList, ProductCreate, ProductDestroy, ProductUpdate, ProductRetrieveUpdateDestroy

urlpatterns = [
    path('v1/', ProductList.as_view(), name='api-home'),
    path('v1/create/', ProductCreate.as_view(), name='create'),
    path('v1/delete/<int:id>/', ProductDestroy.as_view(), name='delete'),
    # path('v1/update/<int:id>/', ProductUpdate.as_view(), name='update'),
    path('v1/update/<int:id>/', ProductRetrieveUpdateDestroy.as_view(), name='update'),

]


import os
import sys
from   setuptools import setup, find_packages
from   setuptools.command.test import test as TestCommand

def read(fname):
    return open(os.path.join(os.path.dirname(__file__), fname)).read()

class PyTest(TestCommand):
    user_options = [('pytest-args=', 'a', "Arguments to pass to py.test")]

    def initialize_options(self):
        TestCommand.initialize_options(self)
        self.pytest_args = []

    def finalize_options(self):
        TestCommand.finalize_options(self)
        self.test_args = []
        self.test_suite = True

    def run_tests(self):
        #import here, cause outside the eggs aren't loaded
        import pytest
        errno = pytest.main(self.pytest_args)
        sys.exit(errno)

setup(
    name="pyjokes",
    version="0.5.0",
    author="Pyjokes Society",
    description="One line jokes for programmers (jokes as a service)",
    license="BSD",
    keywords=[
        "pyjokes",
        "jokes",
    ],
    url="https://github.com/pyjokes/pyjokes",
    packages=find_packages(),
    long_description=read('README.rst'),
    scripts=['scripts/pyjoke',
             'scripts/pyjokes'],
    classifiers=[
        "Development Status :: 4 - Beta",
        "Topic :: Utilities",
        "License :: OSI Approved :: BSD License",
        "Programming Language :: Python :: 2",
        "Programming Language :: Python :: 3",
    ],
    #test requirements and class specification:
    tests_require=['pytest'],
    cmdclass={ 'test' : PyTest },
)


import sys
from collections import deque
N = int(sys.stdin.readline().rstrip())
queue = deque()

def push(x):
    queue.append(x)

def pop():
    if len(queue) == 0:
        return -1
    else:
        return queue.popleft()

def size():
    length = len(queue)
    return length

def empty():
    if len(queue) == 0:
        return 1
    else:
        return 0

def front():
    if len(queue) != 0:
        return queue[0]
    else:
        return -1

def back():
    if len(queue) != 0:
        return queue[-1]
    else:
        return -1

for i in range(N):
    cmd = sys.stdin.readline().rstrip().split()
    order = cmd[0]
    if order == "push":
        push(cmd[1])
    elif order == "pop":
        print(pop())
    elif order == "size":
        print(size())
    elif order == "empty":
        print(empty())
    elif order == "front":
        print(front())
    elif order == "back":
        print(back())


import json

class Workflow:
    def __init__(self, id, title):
        self.id = id
        self.title = title
        self.kpis = []
        self.kpisName=[]
        self.charts = []
        self.chartName=[]

    def charger_workflow(self,name_kpis,name_charts) :
        self.kpisName=name_kpis
        self.chartName=name_charts

    def add_kpi(self, KPI):
        self.kpis.append(KPI)

    def add_chart(self, Chart) :
        self.charts.append(Chart)

    def compute(self,data) :
        for name in self.kpisName :
            kpi = KPI(name, self.title)
            kpi.calculate(data)
            self.add_kpi(kpi)
        for name in self.chartName:
            chart = Chart(name,self.title)
            chart.calculate(data)
            self.add_chart(chart)

    def get_result(self) :
        result=[]
        for kpi in self.kpis :
            result.append(kpi.to_dict())
        for chart in self.charts :
            result.append(chart.to_dict())
        return result

    def to_dict(self):
        return {'id': self.id, 'title': self.title, 'kpis': json.dumps([kpi.to_dict() for kpi in self.kpis])}

#-----------------------------------------------------------------------------------------------------------------------------
import importlib

class KPI:
    def __init__(self, name,workflowName):
        self.name = name
        self.workflow = workflowName
        self.calculate_func = None
        self.value=None
        self.unit=None

    def calculate(self, data):
        if not self.calculate_func:
            udf_module = importlib.import_module('Service.UDF')
            self.calculate_func = getattr(udf_module, f"calculate_{self.name}")
        result = self.calculate_func(data)
        (self.value,self.unit)=result
        return result
    def to_dict(self):
        return { 'name': self.name, 'workflow':self.workflow , 'valeur': self.value , 'unit':self.unit}

#-----------------------------------------------------------------------------------------------------------------------------
class Chart:
    def __init__(self, name,workflowName):
        self.name = name
        self.workflow = workflowName
        self.calculate_func = None
        self.value=None
        self.type=None
        self.unit=None

    def calculate(self, data):
        if not self.calculate_func:
            udf_module = importlib.import_module('Service.UDF')
            self.calculate_func = getattr(udf_module, f"calculate_{self.name}")
        result = self.calculate_func(data)
        (self.value,self.unit,self.type)=result
        return result
    def to_dict(self):
        return { 'name': self.name, 'workflow':self.workflow , 'valeur': self.value , 'unit':self.unit , 'type' : self.type}


import numpy
import cv2
import os

my_img=cv2.imread("fig02.tif",0)
r,c = numpy.shape(my_img)
cv2.imshow('in',my_img)

img, mask = cv2.threshold(my_img, 10, 255, cv2.THRESH_BINARY)
im2, contours, hierarchy = cv2.findContours(mask,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
    gray=cv2.drawContours(mask,[cnt],0,255,-1)

cv2.imshow('a',gray)


import numpy as np
from Perfil_de_carga import PerfilCarga
from matplotlib import (use, is_interactive)
import matplotlib.pyplot as plt

use('Qt5Agg')

class Energías(PerfilCarga):
    """
    Esta clase se encarga de calcular las energías que se encuentran
    en la operación de un sistema de potencia.
    Energía no suministradas, ya sea por probabilididad de falla, por
    sobrecarga del sistema.
    Energía por generación distribuida.
    Energía por pérdidas.
    En esta clase se deben utilizar la mayoría de los valores obtenidos con
    un software que permita el estudio de sistemas de potencia.
    """

    def __init__(self, Param_DNA_red_n, DNA_red_n_1, Tasa_de_fallas,
                 Long_cond_n_1,path):
        """
        Se toman todos los valores necesarios para calcular los distintos
        tipos de energías.

        Param_DNA_red_n: Son los parámetros de la función lineal o cuadrática
        que delimita la DNA en red n a través de los años.
        DNA_red_n_1: Matriz de demanda no abastecida segun lugar de falla y
        escenario presentado a la hora de la falla. En las columnas van los
        escenarios y las filas representan cada falla. La primer columna
        representa al escenario con menor demanda y la última el de mayor.
        Tasa_de_fallas: Diccionario en el cual se tienen las tasas de
        fallas de las líneas segun el tipo y de transformadores.
        Long_cond_n_1: long de los conductores en los cuales se presentaron
        DNA en alguno de los escenarios.
        path: Ruta en donde se encuentra el perfil de carga en formato excel,
        respetando el formato base de ejemplo. UTILIZAR DOBLE BARRA INVERTIDA y
        DOBLE COMILLAS PARA LA DIRECCIÓN DE LA CARPETA.
        """
        PerfilCarga.__init__(self, path)
        self.param_red_n = Param_DNA_red_n
        self.DNA_n_1 = np.array(DNA_red_n_1)
        self.Tasa_fallas = Tasa_de_fallas
        self.Long_cond_n_1 = Long_cond_n_1

        Horas_fallas = np.zeros(len(self.Long_cond_n_1))
        for i in range(len(self.Long_cond_n_1)):
            for x in list(self.Tasa_fallas.keys()):
                if self.Long_cond_n_1[i][0] == x:
                    Horas_fallas_num = self.Long_cond_n_1[i][1]*self.Tasa_fallas[self.Long_cond_n_1[i][0]][0]*self.Tasa_fallas[self.Long_cond_n_1[i][0]][1]/100
                    Horas_fallas[i] = Horas_fallas_num

        self.Horas_fallas = Horas_fallas

    def ENS_n_1(self, Lim_Escenarios,Años,Tasa,Ppico_New):
        Probabilidad = self.Matriz_Probabilidad(Lim_Escenarios, Años, Tasa, Ppico_New).T
        Aux = np.zeros(self.DNA_n_1.shape)
        for i in range(Lim_Escenarios.size):
            Aux[i] = self.DNA_n_1[i]*self.Horas_fallas[i]
        Aux2 = np.zeros([Probabilidad.shape[0],self.DNA_n_1.shape[0]])
        for i in range(Probabilidad.shape[0]):
            for x in range(Aux.shape[0]):
                Aux2[i][x]=Aux[x].dot(np.flip(Probabilidad[i]))
        self.ENS_n_1 = Aux2
        return Aux2

    def ENS_n_1_TotalAño(self, Lim_Escenarios,Años,Tasa,Ppico_New):

        Aux2 = self.ENS_n_1(self, Lim_Escenarios,Años,Tasa,Ppico_New)
        Aux3 = np.zeros(Aux2.shape[0])
        for i in range(Aux2.shape[0]):
            Aux3[i] = Aux2[i].sum()

        return Aux3

    def Plot_ENS_n_1(self, Lim_Escenarios,Años,Tasa,Ppico_New):

        Aux2 = self.ENS_n_1(Lim_Escenarios,Años,Tasa,Ppico_New)
        Abs_Años = [0 for i in range(Años)]
        for i in range(Años):
            A = str(i)
            B = "Año " + A
            Abs_Años[i] = B
        indice = np.arange(len(Abs_Años))
        Aux2 = Aux2.T
        x=1
        for i in range(Aux2.shape[0]):
            if i==0:
                plt.bar(indice,Aux2[i])
            if i!=0:
                while x==i:
                    plt.bar(indice,Aux2[i],bottom=Aux2[x])
                    x+=1

        plt.legend()
        plt.show()


from bs4 import BeautifulSoup
import requests
import urllib
from headers import HEADERS
import random
import os
import time
from multiprocessing import Pool, cpu_count
import subprocess

homepage = 'http://www.pngmart.com/'
categories_link = 'http://www.pngmart.com/categories'
proxies = {'http':  'socks5h://127.0.0.1:9050',
           'https': 'socks5h://127.0.0.1:9050'}

root_dir = 'pngmart'

def get_sub_file_iterator(_dir, endswith=''):
    for entry in os.scandir(_dir):
        if entry.is_file() and entry.path.endswith(endswith):
            yield entry
        elif entry.is_dir():
            yield from get_sub_file_iterator(entry.path, endswith)

def get_page(url):
    header = random.choice(HEADERS)
    try:
        page = requests.get(url, proxies=proxies, headers=header, timeout=120)
    except Exception as e:
        print('get {} failed!'.format(url))
        print(e)
    return page

def parse_all_categories(page):
    soup = BeautifulSoup(page.content, "html.parser")
    categories = soup.find('ul', {'class': 've-cat-widget-listing'})
    if categories == None:
        print('Parse all categories failed!')
        return []
    return [category['href'] for category in categories.find_all('a', href=True)]

def crawl_categories(url):
    categories_page = get_page(url)
    if categories_page != None:
        categories = parse_all_categories(categories_page)
    else:
        return None
    if categories != None:
        if not os.path.exists(root_dir):
            os.makedirs(root_dir)
    return categories

def parse_all_child_categories(page):
    soup = BeautifulSoup(page.content, "html.parser")
    categories = soup.find('div', {'class': 'category_tags'})
    if categories == None:
        print('Parse all categories failed!')
        return []
    return [category['href'] for category in categories.find_all('a', href=True)]

def crawl_all_child_categories(url):
    category_name = url.split('/')[-1]
    child_categories_page = get_page(url)
    if child_categories_page != None:
        child_categories = parse_all_child_categories(child_categories_page)
    else:
        return None
    if child_categories != None:
        path = os.path.join(root_dir, category_name)
        if not os.path.exists(path):
            os.makedirs(path)
    return child_categories

def parse_next_page(page):
    soup = BeautifulSoup(page.content, "html.parser")
    child_page = soup.find('a', {'class': 'nextpostslink'})
    if child_page == None:
        return None
    return child_page['href']

def parse_all_image_links(page):
    soup = BeautifulSoup(page.content, "html.parser")
    image_boxes = soup.find_all('div', {'class': 'featuredimage'})
    if image_boxes == None:
        print('Parsing all images may failed!')
        return []
    return [image.find('a', href=True)['href'] for image in image_boxes]

def crawl_all_image_links(url, category_name):
    image_links = []
    child_category_name = url.split('/')[-1]
    image_links_page = get_page(url)
    if image_links_page != None:
        image_links += parse_all_image_links(image_links_page)
    else:
        return None
    if image_links != None:
        path = os.path.join(root_dir, category_name, child_category_name)
        if not os.path.exists(path):
            os.makedirs(path)

    while parse_next_page(image_links_page) != None:
        next_page = parse_next_page(image_links_page)
        image_links_page = get_page(next_page)
        if image_links_page != None:
            next_image_links = parse_all_image_links(image_links_page)
            image_links += next_image_links
        else:
            return None
    return image_links

def crawl_link(categories):
    for cat in categories:
        category_name = cat.split('/')[-1]
        child_categories = crawl_all_child_categories(cat)

        if child_categories != None:
            for child_cat in child_categories:
                try:
                    crawled = [line.strip() for line in open('pngmart/{}/{}/links.txt'.format(cat.split('/')[-1], child_cat.split('/')[-1]), 'r') if len(line.strip()) > 0]
                    if len(crawled) > 0:
                        print('--------- skip {} --- {}'.format(cat, child_cat))
                        continue
                except Exception as e:
                    print(e)
                child_category_name = child_cat.split('/')[-1]
                image_links = crawl_all_image_links(child_cat, category_name)

                print('-----------------------------------------------------')

                if image_links != None:
                    with open(os.path.join(root_dir, category_name, child_category_name, 'links.txt'), 'w') as file:
                        for link in image_links:
                            file.write(link + '\n')
                    print(category_name, child_category_name)
                else:
                    with open('error_crawl_pngmart_child_catrgories.txt', 'a') as file_child_cat:
                        file_child_cat.write('{}\n'.format(child_cat))
        else:
            with open('error_crawl_pngmart_child_catrgories.txt', 'a') as file_cat:
                file_cat.write('{}\n'.format(cat))

if __name__ == '__main__':
    categories = crawl_categories(categories_link)
    with open('crawl_pngmart_categories.txt', 'w') as file:
       for cat in categories:
           file.write('{}\n'.format(cat))
    # categories = [cat.strip() for cat in open('crawl_pngmart_categories.txt', 'r')]
    print(categories)
    crawl_link(categories)


from django.forms import ModelForm, TextInput
from datetime import date
from .models import Record,Category

class RecordForm(ModelForm):

    def __init__(self,user,*args,**kwargs):
        super().__init__(*args, **kwargs)
        by_user = Category.objects.filter(user=user) | Category.objects.filter(pk=39)
        self.fields['category'].queryset = by_user

    class Meta:
        model = Record
        fields = ['date', 'description', 'category', 'cash', 'balance_type'] #日期, 描述, 分類, 金額, 收支
        widgets = {
            'date':TextInput(
                attrs={
                    'id':'datepicker1',
                    'value': date.today().strftime('%Y-%m-%d')
                }
            )
        }


# -*- coding: utf-8 -*-
"""
Created on Mon Oct  4 10:17:53 2021

@author: chunman zuo
"""

import stlearn as st
import numpy as np
import pandas as pd
import json
import cv2
from pathlib import Path
import os
import time

from stMVC.utilities import parameter_setting

print('Start processing cell segmentation')

start           =  time.time()
parser          =  parameter_setting()
args            =  parser.parse_args()

imageSeg_dir    = args.basePath + 'image_segmentation/'
imageSeg_d      = Path( imageSeg_dir )
imageSeg_d.mkdir(parents=True, exist_ok=True)

image_loc_in    = pd.read_csv(args.basePath + 'spatial/Spot_location.csv', header = 0, index_col = 0)

args.jsonFile   = 'tissue_hires_image.json'
image_width     = 2000
image_height    = 2000
crop_size       = 40
shape           = (image_width, image_height)
sh_fname        = os.path.join(imageSeg_dir, args.jsonFile)

with open(sh_fname, 'r') as f:
	sh_json = json.load(f)

shape           = (image_width, image_height)
check_or_not    = [False] * len(image_loc_in.index)
cell_type_dict  = {}
region_pro      = 0.5

for sh in sh_json['shapes']:
	polys  = []
	geom   = np.squeeze(np.array(sh['points']))
	pts    = geom.astype(int)
	polys.append(pts)
	mask   = np.zeros(shape)
	ss     = cv2.fillPoly(mask, polys, 1)
	mask   = mask.astype(int)
	temp_celltype = sh['label']
	print(temp_celltype)
	count_status  = -1
	for barcode, imagerow, imagecol in zip(image_loc_in.index, image_loc_in["imagerow"], image_loc_in["imagecol"]):
		count_status   = count_status + 1
		imagerow_down  = imagerow - crop_size / 2
		imagerow_up    = imagerow + crop_size / 2
		imagecol_left  = imagecol - crop_size / 2
		imagecol_right = imagecol + crop_size / 2
		spots = []
		spot  = np.array([[imagecol_left, imagerow_up],[imagecol_left, imagerow_down],
						 [imagecol_right,imagerow_down], [imagecol_right,imagerow_up]])
		pts   = spot.astype(int)
		spots.append(pts)
		mask1 = np.zeros(shape)
		ss1   = cv2.fillPoly(mask1, spots, 1)
		mask1 = mask1.astype(int)
		mask2 = mask + mask1

		if np.sum(mask2>1)/np.sum(mask1>0) > region_pro:
			if check_or_not[count_status] is False:
				if cell_type_dict.__contains__(temp_celltype):
					temp_sopt_list = cell_type_dict[temp_celltype]
					temp_sopt_list.append(barcode)
				else:
					temp_spo_l = []
					temp_spo_l.append( barcode )
					cell_type_dict[temp_celltype] = temp_spo_l
				check_or_not[count_status] = True

cell_types = []
cell_names = []
for cell_type in cell_type_dict.keys():
	temp_barcodes = cell_type_dict[cell_type]
	cell_names.extend( temp_barcodes )
	cell_types.extend( [cell_type] * len(temp_barcodes) )

remain_cells = list(set(image_loc_in.index.tolist()) - set(cell_names))
cell_names.extend(remain_cells)
cell_types.extend( ['cluster'+str((len(cell_type_dict)+1))] * len(remain_cells) )
df           = {'Cell_type': cell_types }
data_frame   = pd.DataFrame(data=df, index = cell_names ).to_csv( imageSeg_dir + 'Image_cell_segmentation_0.5.csv' )

duration     = time.time() - start
print('Finish training, total time is: ' + str(duration) + 's' )


import re
import random
import jieba
import jieba.posseg as pos
jieba.add_word("放假", tag="n")

def get_rule_list(rule):
    rule_list = []
    for k in list(rule.keys()):
        rule_list.append(''.join(re.findall('x(.*?)\?', k)))

    for i in range(len(rule_list) - 1):  # 冒泡排序，最大匹配
        for j in range(len(rule_list) - 1):
            if len(rule_list[j]) < len(rule_list[j + 1]):
                rule_list[j], rule_list[j + 1] = rule_list[j + 1], rule_list[j]
    return rule_list

def pat_match(saying, rule, rule_list):
    n_list = []
    answer = ''
    for i in rule_list:
        if i in saying:
            for k in list(rule.keys()):
                if i in k:
                    answer += ''.join(random.choice(list(rule[k])))
                    break
            for s in saying.split(i):
                if s == '':
                    n_list.append('noun')
                    continue
                for w, f in pos.cut(s):
                    if f == 'n':
                         n_list.append(w)
            break
    pat = dict(zip(['?x', '?y'], n_list))
    for p in pat.keys():
        answer = answer.replace(p, pat[p])
    return answer

rules = {'?x我想?y': ['你觉得?y有什么意义呢？', '为什么你想?y？', '你可以想想你很快就可以?y了。'],
         '?x我想要?y': ['不，你不想要?y', '你写完作业才能?y']}

def main():
    rule_list = get_rule_list(rules)
    saying = '老师，我想放假。'
    print(saying)
    answer = pat_match(saying, rules, rule_list)
    print(answer)
    saying1 = '我想要出去玩。'
    print(saying1)
    answer1 = pat_match(saying1, rules, rule_list)
    print(answer1)

if __name__ == '__main__':
    main()


from django.shortcuts import render

from rest_framework import generics, permissions
from rest_framework.response import Response
from django.core.mail import EmailMessage
import string
import random

from .serializers import *
from .models import *

class UserList(generics.ListAPIView):
    serializer_class = UserSerializer
    queryset = User.objects.all()

class UserCreate(generics.CreateAPIView):
    permission_classes = [permissions.AllowAny]
    serializer_class = UserSerializer

class FindUser(generics.RetrieveAPIView):
    permission_classes = [permissions.AllowAny]
    serializer_class = FindUserNameSerializer
    queryset = User.objects.all()
    def retrieve(self, request, *args, **kwargs):
        # print(request.data)
        # 1. request.data.get('email')
        email_data = request.data.get('email')
        # 2. request.data.get('phone_number')
        phone_number_data = request.data.get('phone_number')
        queryset = self.get_queryset()
        user = queryset.filter(email=email_data, phone_number=phone_number_data)
        if user.exists():
            # return user data
            # serializer = UserSerializer(user.first())
            serializer = ShowUserNameSerializer(user.first())
            return Response(serializer.data)
        else:
            # return Response(UserSerializer().data)
            return Response(ShowUserNameSerializer().data)
    def post(self, request, *args, **kwargs):
        return self.retrieve(request, *args, **kwargs)

class CheckUser(generics.RetrieveAPIView):
    permission_classes = [permissions.AllowAny]
    serializer_class = CheckUserSerializer
    queryset = User.objects.all()
    def retrieve(self, request, *args, **kwargs):
        username_data = request.data.get('username')
        email_data = request.data.get('email')
        print("email", email_data)
        phone_number_data = request.data.get('phone_number')
        queryset = self.get_queryset()
        user = queryset.filter(username=username_data, email=email_data, phone_number=phone_number_data)
        if user.exists():
            length = 8
            string_pool = string.ascii_letters + string.digits
            auth_code = ""
            for i in range(length):
                auth_code += random.choice(string_pool)
            email = EmailMessage('B2GETHER 비밀번호 변경 요청 인증', 'B2GETHER 비밀번호 변경 요청 인증 번호는 ' + auth_code + ' 입니다!', from_email='sanghyupjungxav2@gmail.com', to=[email_data])
            email.send()
            serializer = ShowUserNameSerializer(user.first())
            # print(serializer.data)
            # print(Response(serializer.data))
            return Response([serializer.data, auth_code])
        else:
            return Response(ShowUserNameSerializer().data)
    def post(self, request, *args, **kwargs):
        return self.retrieve(request, *args, **kwargs)

class UpdatePassword(generics.UpdateAPIView):
    permission_classes = [permissions.AllowAny]
    serializer_class = UserSerializer
    queryset = User.objects.all()

class StoryList(generics.ListAPIView):
    permission_classes = [permissions.AllowAny]
    serializer_class = StorySerializer
    queryset = Story.objects.all()

class StoryCreate(generics.CreateAPIView):
    permission_classes = [permissions.AllowAny]
    serializer_class = StorySerializer

class HashtagList(generics.ListAPIView):
    serializer_class = HashtagSerializer
    queryset = Hashtag.objects.all()

class HashtagCreate(generics.CreateAPIView):
    permission_classes = [permissions.AllowAny]
    serializer_class = HashtagSerializer

class TodolistList(generics.ListAPIView):
    permission_classes = [permissions.AllowAny]
    serializer_class = TodolistSerializer
    queryset = Todolist.objects.all()

class TodolistCreate(generics.CreateAPIView):
    permission_classes = [permissions.AllowAny]
    serializer_class = TodolistSerializer


# Util
# Author: Yuanjie Yuanjie
# Date: 11/20/19
import struct
import config
import random

# Udp socket read data
def udp_read_data(socket):
    return socket.recvfrom(config.MAX_BUFFER_SIZE)

# Udp socket send data to the given addreass
def udp_write_data(socket, data, addr):
    return socket.sendto(data, addr)

# Parse the msg from clients
def parse_client_msg(msg):
    offset = 0
    msg_type = int(struct.unpack_from('!B', msg, offset)[0])
    offset += 1
    len_game_id = int(struct.unpack_from('!B', msg, offset)[0])
    offset += 1
    len_nick_name = int(struct.unpack_from('!B', msg, offset)[0])
    offset += 1
    game_id = msg[offset : offset + len_game_id].decode()
    offset += len_game_id
    nick_name = msg[offset : offset + len_nick_name].decode()
    offset += len_nick_name
    port_or_dir = b''
    if msg_type == config.CREATE_GAME_MSG or msg_type == config.JOIN_GAME_MSG:
        port_or_dir = struct.unpack_from('!h', msg, offset)[0]
    elif msg_type == config.CHANGE_DIR_MSG:
        port_or_dir = int(struct.unpack_from('!B', msg, offset)[0])
    return msg_type, game_id, nick_name, port_or_dir

# Generate random col and row num of a position
def generate_random_pos(width, height):
    col = random.randrange(width)
    row = random.randrange(height)
    return col, row

# Generate game information msg based on the params provided.
def generate_info_msg(seq_num, a_snake_positions, b_snake_positions, apple_pos):
    msg = bytearray()
    msg += struct.pack('!B', config.GAME_INFO_MSG)
    msg += struct.pack('!B', seq_num)
    msg += struct.pack('!B', apple_pos[1])
    msg += struct.pack('!B', apple_pos[0])
    print('--> apple is: ', 'col -', apple_pos[0], ', row -', apple_pos[1])
    a_snake_binaries = convert_positions_to_binary(a_snake_positions)
    b_snake_binaries = convert_positions_to_binary(b_snake_positions)
    print('--> a snake is:\n')
    print_game_board(a_snake_binaries)
    print('--> b snake is:\n')
    print_game_board(b_snake_binaries)
    a_snake = concate_binaries(a_snake_binaries)
    b_snake = concate_binaries(b_snake_binaries)
    msg += a_snake
    msg += b_snake
    return bytes(msg)

# Convert the tuple positions to an 32 bits integer.
def convert_positions_to_binary(positions):
    binaries = [0 for i in range(config.WIDTH)]
    for col, row in positions:
        bit_mask = 1 << (31 - col)
        binaries[row] = binaries[row] | bit_mask
    return binaries

# Concate the all the numbers in the given binaries as bytes
def concate_binaries(binaries):
    data = bytearray()
    for binary in binaries:
        data += struct.pack('!I', binary)
    return bytes(data)

# Print all the integer in the binaries in binary format.
def print_game_board(binaries):
    for i in binaries:
        print('{:032b}'.format(i))

# Update the game information based on the params provided.
def update_game_info(game_info, name_dict, game_id, nick_name, direction):
    # get the relative snake, and update the snake according to the direction
    names = name_dict[game_id]
    name_index = 0 if names[0] == nick_name else 1
    current_snake = game_info[name_index]
    opponent_snake = game_info[1 - name_index]
    apple = game_info[2]
    current_snake.direction = direction
    # Move the current snake to the given direction
    game_status = current_snake.move(opponent_snake, apple, direction)
    # Generate random positions for the apple when it is eaten by the current snake.
    if current_snake.head() == game_info[2]:
        while game_info[2] in current_snake.body or game_info[2] in opponent_snake.body:
            game_info[2] = generate_random_pos(config.WIDTH, config.HEIGHT)
    return game_status

# Generate game over msg based on the given game status
def generate_game_over_msg(game_status, names, nick_name):
    # it's a tie or nick name is the loser
    msg = bytearray()
    msg += struct.pack('!B', config.GAME_OVER_MSG)
    winner_name = ''
    if game_status == config.LOSE:
        msg += struct.pack('!B', config.LOSE)
        winner_name = names[1] if names[0] == nick_name else names[0]
        msg += struct.pack('!B', len(winner_name))
    elif game_status == config.TIE:
        msg += struct.pack('!B', config.TIE)
        msg += struct.pack('!B', 0)
    msg += winner_name.encode()
    return bytes(msg)

# Generate the prefix for all client msg
def generate_client_msg_prefix(type, game_id, nick_name):
    msg = bytearray()
    msg += struct.pack('!B', type)
    msg += struct.pack('!B', len(game_id))
    msg += struct.pack('!B', len(nick_name))
    msg += game_id.encode()
    msg += nick_name.encode()
    return bytes(msg)

# Generate create or join msg of the clients
def generate_create_or_join_msg(type, game_id, nick_name, port_number):
    msg = generate_client_msg_prefix(type, game_id, nick_name)
    msg += struct.pack('!h', port_number)
    return msg

# Generate change direction msg
def generate_change_dir_msg(type, game_id, nick_name, dir):
    msg = generate_client_msg_prefix(type, game_id, nick_name)
    msg += struct.pack('!B', dir)
    return msg

# Read game over msg from the msg sent from the server
def read_game_over_msg(msg):
    offset = 0
    msg_type = int(struct.unpack_from('!B', msg, offset)[0])
    offset += 1
    result = struct.unpack_from('!B', msg, offset)[0]
    offset += 1
    name_len = int(struct.unpack_from('!B', msg, offset)[0])
    offset += 1
    winner_name = msg[offset : offset + name_len].decode()
    return msg_type, result, winner_name

# Read game information msg
def read_game_info_msg(msg):
    offset = 0
    msg_type = int(struct.unpack_from('!B', msg, offset)[0])
    offset += 1
    seq_num = int(struct.unpack_from('!B', msg, offset)[0])
    offset += 1
    apple_row = int(struct.unpack_from('!B', msg, offset)[0])
    offset += 1
    apple_col = int(struct.unpack_from('!B', msg, offset)[0])
    offset += 1
    a_snake, b_snake = [],[]
    for i in range(config.WIDTH):
        bitmap = struct.unpack_from('!I', msg, offset)[0]
        a_snake.append(bitmap)
        offset += 4
    for i in range(config.WIDTH):
        bitmap = struct.unpack_from('!I', msg, offset)[0]
        b_snake.append(bitmap)
        offset += 4
    print('--> a snake is:\n')
    print_game_board(a_snake)
    print('--> b snake is:\n')
    print_game_board(b_snake)
    return msg_type, seq_num, apple_row, apple_col, a_snake, b_snake

# Parse the given integer format of snake into positions
def parse_snake_pos(snake):
    positions = []
    rows = len(snake)
    for r in range(rows):
        row = snake[r]
        # row is an integer, find out all the indices of set bit
        for c in range(32):
            mask = 1 << (31 - c)
            if row & mask:
                pos = (c, r)
                positions.append(pos)
    return positions


import numpy as np
from tqdm import tqdm
import pickle as pkl
import json
from nltk import word_tokenize
import re
from torch.utils.data.dataset import Dataset
import numpy as np
from copy import deepcopy
import nltk
import jieba
import pickle
import os
import ipdb
import random

class CRSdataset(Dataset):
    def __init__(self, logger, subset, filename, args, tokenizer):
        super(CRSdataset, self).__init__()
        if args.model_type == 'Ours':
            self._init_Ours(logger, subset, filename, args, tokenizer)

    def _init_Ours(self,
                   logger,
                   subset,
                   filename,
                   args,
                   tokenizer,
                   use_size=-1):
        raw = args.raw
        self.batch_size = args.batch_size
        self.max_c_length = args.max_c_length
        self.args = args

        # load data
        only_first_movie = False
        if self.args.use_size == -1:
            f = pickle.load(open(filename, 'rb'))[:]
        else:
            f = pickle.load(open(filename, 'rb'))[:self.args.use_size]

        self.load_movie()
        self.unk_movie_id = len(self.db2id)
        self.movie_num = len(self.db2id) + 1

        logger.info("[Load {} movies(+1)]".format(self.movie_num))

        self.tokenizer = tokenizer
        self.cls_id = self.tokenizer.convert_tokens_to_ids('[CLS]')
        self.sep_id = self.tokenizer.convert_tokens_to_ids('[SEP]')
        self.pad_id = self.tokenizer.convert_tokens_to_ids('[PAD]')
        self.unk_id = self.tokenizer.convert_tokens_to_ids('[UNK]')
        self.sent_split_id = self.tokenizer.convert_tokens_to_ids(
            '[unused1]')  #1
        self.word_split_id = self.tokenizer.convert_tokens_to_ids(
            '[unused2]')  #2
        self.word_split = '[unused2]'  #2

        self.both_data = {}  # identity: sample, 能够根据索引抽取，后面会加入user history
        save_file = 'data/data_p_{}/{}processed_data.pkl'.format(
            args.model_type, subset)
        empty_conv_ids_file = open('empty_conv_ids.txt', 'a')

        if not raw:
            self.data = pickle.load(open(save_file, 'rb'))
            if use_size != -1:
                self.data = self.data[:use_size]
            logger.info(f"[Load {len(self.data)} cases, from {save_file}]")

        else:
            for conv in tqdm(f):
                # contexts_token = ["[CLS]"]  # list of token, ['[CLS]' UTTER1  "[SEP]"  UTTER2  "[SEP]" ]
                conv_id = conv['conv_id']
                contexts_index = [
                ]  # list of token, ['[CLS]' UTTER1  UTTER2  "[SEP]" Target ]

                for message in conv['messages']:
                    message_id, content, role = message['local_id'], message[
                        'content'], message['role']
                    # 如果这一步要推荐电影，就产生一个sample：上文context + 当前要推荐的电影 这个pair
                    if role == 'Recommender' and message_id in conv[
                            'mentionMovies']:
                        # 将douban-id映射为global-id
                        movie_id = int(conv['mentionMovies'][message_id][0])
                        movie_id = self.db2id[movie_id]
                        # 获得context对应的两种mask: seg_id, att_mask # 短则补齐，长则切断
                        cur_contexts_index = contexts_index[:-1] + [
                            self.sep_id
                        ]  #？是不是加重复了
                        self.max_len_inside = self.max_c_length - 1  # cls

                        if len(cur_contexts_index) < self.max_len_inside:
                            cur_contexts_index = [self.cls_id
                                                  ] + cur_contexts_index

                            cur_contexts_index = cur_contexts_index + [0] * (
                                self.max_c_length - len(cur_contexts_index))

                            types = [0] * len(cur_contexts_index) + [1] * (
                                self.max_c_length - len(cur_contexts_index)
                            )  # mask部分 segment置为1
                            masks = [1] * len(cur_contexts_index) + [0] * (
                                self.max_c_length - len(cur_contexts_index))
                        else:
                            cur_contexts_index = [
                                self.cls_id
                            ] + cur_contexts_index[-self.max_len_inside:]
                            types = [0] * len(cur_contexts_index)
                            masks = [1] * len(cur_contexts_index)

                        assert len(cur_contexts_index) == self.max_c_length
                        assert cur_contexts_index[0] == self.cls_id
                        assert cur_contexts_index[
                            -1] == self.sep_id or cur_contexts_index[
                                -1] == self.pad_id

                        case = [cur_contexts_index, types, masks, movie_id]
                        identity = str(conv_id) + '/' + str(message_id)
                        self.both_data[identity] = case

                        if only_first_movie:
                            break

                    content_token = tokenizer.tokenize(content)
                    content_index = tokenizer.convert_tokens_to_ids(
                        content_token) + [self.sent_split_id]
                    contexts_index.extend(content_index)

            # 以上是bert用的，以下是sasrec用的

            data_base = '../../data/'
            user_long_history = os.path.join(data_base,
                                             'user_long_history.json')
            conv_long_history = os.path.join(data_base,
                                             'conv_long_history.json')
            user_long_history = json.load(open(
                user_long_history, 'r'))  # {user: [(id, rate, time)]}
            conv_long_history = json.load(
                open(conv_long_history,
                     'r'))  # {user: {conv_id:(start_index, length)}

            conv2user = pickle.load(open('../../data/0619conv2user.pkl',
                                         'rb'))  # 检验无误

            num_history = 0
            history_total_num = 0
            self.max_len = self.args.max_seq_length

            for conv in f[:]:
                conv_id = conv['conv_id']  # int
                # ipdb.set_trace()
                user = conv2user[str(conv_id)]  # str
                conv_movie_list = []
                for message_id, (movieId,
                                 m_name) in conv['mentionMovies'].items():
                    # 每个要推荐的位置生成一个history，与context的data结合在一起
                    identity = str(conv_id) + '/' + str(message_id)
                    # ipdb.set_trace()
                    if user not in conv_long_history or str(
                            conv_id) not in conv_long_history[user]:
                        seq = []
                    else:
                        num_history += 1
                        start_index, length = conv_long_history[user][str(
                            conv_id)]
                        seq = user_long_history[user][start_index:start_index +
                                                      length]
                        # seq = [self.db2id.get(int(movie_id), len(self.db2id)+1) for (movie_id, rate, time_) in seq] # todo
                        seq = [
                            self.db2id.get(int(movie_id), self.unk_movie_id)
                            for (movie_id, rate, time_) in seq
                        ]  # todo

                    movie_list = seq + conv_movie_list  # list of int, long-history + short history
                    history_total_num += len(movie_list)
                    input_ids = movie_list
                    target_pos = movie_list  # no use

                    input_mask = [1] * len(input_ids)

                    sample_negs = []  # no use
                    seq_set = set(input_ids)
                    for _ in input_ids:
                        sample_negs.append(self.neg_sample(seq_set))  #用于训练的

                    if len(input_ids) < self.max_len:
                        pad_len = self.max_len - len(input_ids)
                        input_ids = [0] * pad_len + input_ids
                        target_pos = [0] * pad_len + target_pos
                        input_mask = [0] * pad_len + input_mask
                        sample_negs = [0] * pad_len + sample_negs
                    else:
                        input_ids = input_ids[-self.max_len:]
                        target_pos = target_pos[-self.max_len:]
                        input_mask = input_mask[-self.max_len:]
                        sample_negs = sample_negs[-self.max_len:]

                    assert len(input_ids) == self.max_len
                    assert len(target_pos) == self.max_len
                    assert len(input_mask) == self.max_len
                    assert len(sample_negs) == self.max_len
                    if identity in self.both_data:
                        assert self.db2id[int(
                            movieId)] == self.both_data[identity][-1]
                        self.both_data[identity].extend(
                            [input_ids, target_pos, input_mask, sample_negs])
                    else:
                        logger.info(identity, file=empty_conv_ids_file)
                    conv_movie_list.append(self.db2id[int(movieId)])

            logger.info("Load {} user-history, including {} movies".format(
                num_history, history_total_num))

            # convert both_data to data
            self.data = [[identity] + sample for identity, sample in self.both_data.items()]
            pickle.dump(self.data, open(save_file, 'wb'))
            if use_size != -1:
                self.data = self.data[:use_size]
            logger.info(f"[Save processed data to {save_file}]")
            empty_conv_ids_file.close()

    def load_movie(self, path='../../data/movies_with_mentions.csv'):
        import csv
        self.name2id = {}
        self.db2id = {}
        reader = csv.reader(open(path, 'r', encoding='utf-8-sig'))
        next(reader)
        for line in reader:
            global_id, name_time, db_id, _ = line
            name = name_time.split('(')[0]
            self.name2id[name] = int(global_id)
            self.db2id[int(db_id)] = int(global_id)

    def neg_sample(self, item_set):
        item = random.randint(1, self.movie_num)
        while item in item_set:
            item = random.randint(1, self.movie_num)
        return item

    def __getitem__(self, index):
        if len(self.data[index]) == 8+1:
            identity, contexts_index, types, masks, movie_id, input_ids, target_pos, input_mask, sample_negs = self.data[
                index]

            return identity, np.array(contexts_index), np.array(types), \
                np.array(masks), movie_id, \
                np.array(input_ids), np.array(target_pos), \
                np.array(input_mask), np.array(sample_negs)

        elif len(self.data[index]) == 4+1:
            identity, contexts_index, types, masks, movie_id = self.data[index]
            return np.array(contexts_index), np.array(types), np.array(
                masks), movie_id, movie_id, movie_id, movie_id, movie_id

    def __len__(self):
        return len(self.data)