import os
import time
import numpy as np
import cv2
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
from utils import CrossEntropyLoss2d
from models import reactive_net, reinforcement_net, reinforcement_static_net
from scipy import ndimage
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression

class Trainer(object):
    def __init__(self, robot, training_target, future_reward_discount, frames,
                 is_testing, load_snapshot, snapshot_file, force_cpu, load_static_snapshot, static_snapshot_file):
        self.robot = robot
        self.training_target = training_target

        if training_target!="static_grasp":
            self.xy_moving_timetable = np.load(os.path.join(os.getcwd(), "timetables", "xy_timetable.npy"))
            self.rotate_timetable = np.load(os.path.join(os.getcwd(), "timetables", "rotate_timetable.npy"))
            z_timetable = np.load(os.path.join(os.getcwd(), "timetables", "z_timetable.npy"))[1:]

            self.z_time_predictor = LinearRegression()
            self.z_time_predictor.fit(np.array([i+1 for i in range(len(z_timetable))]).reshape(-1, 1), z_timetable.reshape(-1, 1))

        # Check if CUDA can be used
        if torch.cuda.is_available() and not force_cpu:
            print("CUDA detected. Running with GPU acceleration.")
            self.use_cuda = True
        elif force_cpu:
            print("CUDA detected, but overriding with option '--cpu'. Running with only CPU.")
            self.use_cuda = False
        else:
            print("CUDA is *NOT* detected. Running with only CPU.")
            self.use_cuda = False

        self.static_model = reinforcement_static_net(self.use_cuda)
        self.model = reinforcement_net(self.use_cuda, frames)
        self.future_reward_discount = future_reward_discount

        # Initialize Huber loss
        self.criterion = torch.nn.SmoothL1Loss(reduce=False) # Huber loss
        if self.use_cuda:
            self.criterion = self.criterion.cuda()

        # Load pre-trained model
        if load_static_snapshot:
            self.static_model.load_state_dict(torch.load(static_snapshot_file))
            print('Pre-trained model snapshot loaded from: %s' % (static_snapshot_file))

        if load_snapshot:
            self.model.load_state_dict(torch.load(snapshot_file))
            print('Pre-trained model snapshot loaded from: %s' % (snapshot_file))

        # Convert model from CPU to GPU
        if self.use_cuda:
            self.model = self.model.cuda()
            self.static_model = self.static_model.cuda()

        # Set model to training mode
        if self.training_target=="static_grasp":
            self.static_model.train()
            self.optimizer = torch.optim.SGD(self.static_model.parameters(), lr=1e-4, momentum=0.9, weight_decay=2e-5)
        else:
            self.model.train()
            self.static_model.eval()
            self.optimizer = torch.optim.SGD(self.model.parameters(), lr=1e-4, momentum=0.9, weight_decay=2e-5)
        self.iteration = 0
        # if training_target=='static_grasp':
        #     if static_snapshot_file:
        #         self.iteration = int(snapshot_file.split("/")[-1].split("-")[1][:6])
        # else:
        #     if snapshot_file:
        #         self.iteration = int(snapshot_file.split("/")[-1].split("-")[1][:6])

        # Initialize lists to save execution info and RL variables
        self.executed_action_log = []
        self.label_value_log = []
        self.reward_value_log = []
        self.predicted_value_log = []
        self.use_heuristic_log = []
        self.is_exploit_log = []
        self.clearance_log = []

    # Pre-load execution info and RL variables
    def preload(self, transitions_directory):
        self.executed_action_log = np.load(os.path.join(transitions_directory, 'executed-action.log.npy'), allow_pickle=True)
        self.iteration = self.executed_action_log.shape[0] - 2
        self.executed_action_log = self.executed_action_log[0:self.iteration,:]
        self.executed_action_log = self.executed_action_log.tolist()
        self.label_value_log = np.load(os.path.join(transitions_directory, 'label-value.log.npy'), allow_pickle=True)
        self.label_value_log = self.label_value_log[0:self.iteration]
        self.label_value_log.shape = (self.iteration,1)
        self.label_value_log = self.label_value_log.tolist()

        # self.label_grasp_value_log = np.load(os.path.join(transitions_directory, 'label-grasp-value.log.npy'), allow_pickle=True)
        # self.label_grasp_value_log = self.label_grasp_value_log[0:self.iteration]
        # self.label_grasp_value_log.shape = (self.iteration,1)
        # self.label_grasp_value_log = self.label_grasp_value_log.tolist()

        self.predicted_value_log = np.load(os.path.join(transitions_directory, 'predicted-value.log.npy'), allow_pickle=True)
        self.predicted_value_log = self.predicted_value_log[0:self.iteration]
        self.predicted_value_log.shape = (self.iteration,1)
        self.predicted_value_log = self.predicted_value_log.tolist()
        self.reward_value_log = np.load(os.path.join(transitions_directory, 'reward-value.log.npy'), allow_pickle=True)
        self.reward_value_log = self.reward_value_log[0:self.iteration]
        self.reward_value_log.shape = (self.iteration,1)
        self.reward_value_log = self.reward_value_log.tolist()
        self.use_heuristic_log = np.load(os.path.join(transitions_directory, 'use-heuristic.log.npy'), allow_pickle=True)
        self.use_heuristic_log = self.use_heuristic_log[0:self.iteration]
        self.use_heuristic_log.shape = (self.iteration,1)
        self.use_heuristic_log = self.use_heuristic_log.tolist()
        self.is_exploit_log = np.load(os.path.join(transitions_directory, 'is-exploit.log.npy'), allow_pickle=True)
        self.is_exploit_log = self.is_exploit_log[0:self.iteration]
        self.is_exploit_log.shape = (self.iteration,1)
        self.is_exploit_log = self.is_exploit_log.tolist()
        if os.path.exists(os.path.join(transitions_directory, 'clearance.log.npy')):
            self.clearance_log = np.load(os.path.join(transitions_directory, 'clearance.log.npy'), allow_pickle=True)
            self.clearance_log.shape = (self.clearance_log.shape[0],1)
            self.clearance_log = self.clearance_log.tolist()

    def get_analitical_velocity(self, prediction, depth_heightmap):
        best_pix_x = prediction[2]
        best_pix_y = prediction[1]
        z_coo = depth_heightmap[best_pix_y][best_pix_x] + self.robot.workspace_limits[2][0]
        z_coo = max(z_coo - 0.04, self.robot.workspace_limits[2][0] + 0.02)
        z_time = self.z_time_predictor.predict(np.array(z_coo).reshape(-1, 1)).item()
        t = self.xy_moving_timetable[tuple(prediction[1:])] + self.rotate_timetable[prediction[0]] + z_time -2

        a = 0.05
        x = 0.5

        v0 = self.robot.get_conveyor_velocity()

        base_velocity = min(a*t + v0 + np.sqrt((a*t+v0)**2 - (2*a*x+v0**2)), a*t + v0 - np.sqrt((a*t+v0)**2 - (2*a*x+v0**2)))
        if np.isnan(base_velocity):
            base_velocity = (a*t + v0)
        return base_velocity

    # Compute forward pass through model to compute affordances/Q
    def preprocessing_input_img(self, color_heightmap, depth_heightmap):
        # Apply 2x scale to input heightmaps
        color_heightmap_2x = ndimage.zoom(color_heightmap, zoom=[2,2,1], order=0)
        depth_heightmap_2x = ndimage.zoom(depth_heightmap, zoom=[2,2], order=0)

        assert(color_heightmap_2x.shape[0:2] == depth_heightmap_2x.shape[0:2])

        # Add extra padding (to handle rotations inside network)
        diag_length = float(color_heightmap_2x.shape[0]) * np.sqrt(2)
        diag_length = np.ceil(diag_length/32)*32
        padding_width = int((diag_length - color_heightmap_2x.shape[0])/2)
        color_heightmap_2x_r =  np.pad(color_heightmap_2x[:,:,0], padding_width, 'constant', constant_values=0)
        color_heightmap_2x_r.shape = (color_heightmap_2x_r.shape[0], color_heightmap_2x_r.shape[1], 1)
        color_heightmap_2x_g =  np.pad(color_heightmap_2x[:,:,1], padding_width, 'constant', constant_values=0)
        color_heightmap_2x_g.shape = (color_heightmap_2x_g.shape[0], color_heightmap_2x_g.shape[1], 1)
        color_heightmap_2x_b =  np.pad(color_heightmap_2x[:,:,2], padding_width, 'constant', constant_values=0)
        color_heightmap_2x_b.shape = (color_heightmap_2x_b.shape[0], color_heightmap_2x_b.shape[1], 1)
        color_heightmap_2x = np.concatenate((color_heightmap_2x_r, color_heightmap_2x_g, color_heightmap_2x_b), axis=2)
        depth_heightmap_2x =  np.pad(depth_heightmap_2x, padding_width, 'constant', constant_values=0)

        # Pre-process color image (scale and normalize)
        image_mean = [0.485, 0.456, 0.406]
        image_std = [0.229, 0.224, 0.225]
        input_color_image = color_heightmap_2x.astype(float)/255
        for c in range(3):
            input_color_image[:,:,c] = (input_color_image[:,:,c] - image_mean[c])/image_std[c]

        # Pre-process depth image (normalize)
        image_mean = 0.01
        image_std = 0.03
        depth_heightmap_2x.shape = (depth_heightmap_2x.shape[0], depth_heightmap_2x.shape[1], 1)
        input_depth_image = depth_heightmap_2x.copy()
        input_depth_image = (input_depth_image - image_mean)/image_std

        # Construct minibatch of size 1 (b,c,h,w)
        input_color_image.shape = (input_color_image.shape[0], input_color_image.shape[1], input_color_image.shape[2], 1)
        input_depth_image.shape = (input_depth_image.shape[0], input_depth_image.shape[1], input_depth_image.shape[2], 1)
        input_color_data = torch.from_numpy(input_color_image.astype(np.float32)).permute(3,2,0,1)
        input_depth_data = torch.from_numpy(input_depth_image.astype(np.float32)).permute(3,2,0,1)
        return input_color_data, input_depth_data, padding_width, color_heightmap_2x

    def forward(self, color_heightmaps, depth_heightmaps, is_volatile=False, specific_rotation=-1, nonlocal_variables=None, teacher_student=False):
        input_color_data = []
        input_depth_data = []
        for color_heightmap, depth_heightmap in zip(color_heightmaps, depth_heightmaps):
            color, depth, padding_width, color_heightmap_2x = self.preprocessing_input_img(color_heightmap, depth_heightmap)
            input_color_data.append(color)
            input_depth_data.append(depth)

        # Pass input data through model
        if self.training_target == "static_grasp":
            output_prob, state_feat = self.static_model.forward(input_color_data[0], input_depth_data[0], is_volatile, specific_rotation)
        else:
            #static_modelからgrasp位置を決める
            index = 0
            if teacher_student:
                index = -1
            with torch.no_grad():
                output_prob, _ = self.static_model.forward(input_color_data[index], input_depth_data[index], is_volatile, specific_rotation)
            for rotate_idx in range(len(output_prob)):
                if rotate_idx == 0:
                    # regulate_predictions = output_prob[rotate_idx][0].cpu().data.numpy()[:,0,int(padding_width/2):int(color_heightmap_2x.shape[0]/2 - padding_width/2),int(padding_width/2):int(color_heightmap_2x.shape[0]/2 - padding_width/2)]
                    static_grasp_predictions = output_prob[rotate_idx][1].cpu().data.numpy()[:,0,int(padding_width/2):int(color_heightmap_2x.shape[0]/2 - padding_width/2),int(padding_width/2):int(color_heightmap_2x.shape[0]/2 - padding_width/2)]
                else:
                    # regulate_predictions = np.concatenate((regulate_predictions, output_prob[rotate_idx][0].cpu().data.numpy()[:,0,int(padding_width/2):int(color_heightmap_2x.shape[0]/2 - padding_width/2),int(padding_width/2):int(color_heightmap_2x.shape[0]/2 - padding_width/2)]), axis=0)
                    static_grasp_predictions = np.concatenate((static_grasp_predictions, output_prob[rotate_idx][1].cpu().data.numpy()[:,0,int(padding_width/2):int(color_heightmap_2x.shape[0]/2 - padding_width/2),int(padding_width/2):int(color_heightmap_2x.shape[0]/2 - padding_width/2)]), axis=0)
            static_grasp_idx = np.unravel_index(np.argmax(static_grasp_predictions), static_grasp_predictions.shape)
            base_velocity = self.get_analitical_velocity(static_grasp_idx, depth_heightmap)
            output_prob, state_feat = self.model.forward(input_color_data, input_depth_data, is_volatile, specific_rotation, base_velocity)

        if self.training_target == "static_grasp":
            # Return Q values (and remove extra padding)
            for rotate_idx in range(len(output_prob)):
                if rotate_idx == 0:
                    # regulate_predictions = output_prob[rotate_idx][0].cpu().data.numpy()[:,0,int(padding_width/2):int(color_heightmap_2x.shape[0]/2 - padding_width/2),int(padding_width/2):int(color_heightmap_2x.shape[0]/2 - padding_width/2)]
                    grasp_predictions = output_prob[rotate_idx][1].cpu().data.numpy()[:,0,int(padding_width/2):int(color_heightmap_2x.shape[0]/2 - padding_width/2),int(padding_width/2):int(color_heightmap_2x.shape[0]/2 - padding_width/2)]
                else:
                    # regulate_predictions = np.concatenate((regulate_predictions, output_prob[rotate_idx][0].cpu().data.numpy()[:,0,int(padding_width/2):int(color_heightmap_2x.shape[0]/2 - padding_width/2),int(padding_width/2):int(color_heightmap_2x.shape[0]/2 - padding_width/2)]), axis=0)
                    grasp_predictions = np.concatenate((grasp_predictions, output_prob[rotate_idx][1].cpu().data.numpy()[:,0,int(padding_width/2):int(color_heightmap_2x.shape[0]/2 - padding_width/2),int(padding_width/2):int(color_heightmap_2x.shape[0]/2 - padding_width/2)]), axis=0)
            return None, grasp_predictions, state_feat

        else:
            # Return Q values (and remove extra padding)
            for rotate_idx in range(len(output_prob)):
                if rotate_idx == 0:
                    regulate_predictions = output_prob[rotate_idx][0].cpu().data.numpy()[:,0,int(padding_width/2):int(color_heightmap_2x.shape[0]/2 - padding_width/2),int(padding_width/2):int(color_heightmap_2x.shape[0]/2 - padding_width/2)]
                    grasp_predictions = output_prob[rotate_idx][1].cpu().data.numpy()[:,0,int(padding_width/2):int(color_heightmap_2x.shape[0]/2 - padding_width/2),int(padding_width/2):int(color_heightmap_2x.shape[0]/2 - padding_width/2)]
                else:
                    regulate_predictions = np.concatenate((regulate_predictions, output_prob[rotate_idx][0].cpu().data.numpy()[:,0,int(padding_width/2):int(color_heightmap_2x.shape[0]/2 - padding_width/2),int(padding_width/2):int(color_heightmap_2x.shape[0]/2 - padding_width/2)]), axis=0)
                    grasp_predictions = np.concatenate((grasp_predictions, output_prob[rotate_idx][1].cpu().data.numpy()[:,0,int(padding_width/2):int(color_heightmap_2x.shape[0]/2 - padding_width/2),int(padding_width/2):int(color_heightmap_2x.shape[0]/2 - padding_width/2)]), axis=0)

        return regulate_predictions, grasp_predictions, base_velocity, static_grasp_predictions

    def get_label_value(self, primitive_action, regulate_success, grasp_success, regulate_velocity, prev_grasp_predictions, next_color_heightmap, next_depth_heightmap, nonlocal_variables, multi_steps=False):
        touch_object = nonlocal_variables['touch_object']

        # Compute current reward
        current_reward = 0

        if grasp_success:
            if multi_steps:
                current_reward += sum([item["grasp_success"] for item in nonlocal_variables["multi_pix_ind"] if item['grasp_success']]) / len(nonlocal_variables["multi_pix_ind"])
            else:
                current_reward += 1
        elif touch_object:
            current_reward += touch_object//3
                # touch_object = 0
        if self.training_target == "static_grasp":
            next_regulate_predictions, next_grasp_predictions, next_state_feat = self.forward(next_color_heightmap, next_depth_heightmap, is_volatile=True, nonlocal_variables=nonlocal_variables)
        else:
            next_regulate_predictions, next_grasp_predictions,_,  next_state_feat = self.forward(next_color_heightmap, next_depth_heightmap, is_volatile=True, nonlocal_variables=nonlocal_variables)
        future_reward = np.max(next_grasp_predictions)

        print(primitive_action, 'Current reward: %f' % (current_reward))
        print(primitive_action, 'Future reward: %f' % (future_reward))
        expected_reward = current_reward + self.future_reward_discount * future_reward
        print(primitive_action, 'Expected reward: %f + %f x %f = %f' % (current_reward, self.future_reward_discount, future_reward, expected_reward))
        print()
        return expected_reward, current_reward

    def multi_steps_backprop(self, color_heightmap, depth_heightmap, primitive_action, best_pix_ind, label_value, regulate_residual=None, nonlocal_variables=None):
        loss_value = 0

        # Compute labels
        for rotate in range(self.model.num_rotations):
            _x = [item["x"] for item in nonlocal_variables["multi_pix_ind"] if item["rotate"]==rotate and item["grasp_success"]]
            if len(_x)==0:
                continue
            _y = [item["y"] for item in nonlocal_variables["multi_pix_ind"] if item["rotate"]==rotate and item["grasp_success"]]

            label = np.zeros((1,320,320))
            action_area = np.zeros((224,224))
            action_area[_y, _x] = 1

            # blur_kernel = np.ones((5,5),np.float32)/25
            # action_area = cv2.filter2D(action_area, -1, blur_kernel)
            tmp_label = np.zeros((224,224))
            if regulate_residual:
                tmp_label[action_area > 0] = regulate_residual
            else:
                tmp_label[action_area > 0] = label_value

            label[0,48:(320-48),48:(320-48)] = tmp_label

            # Compute label mask
            label_weights = np.zeros(label.shape)
            tmp_label_weights = np.zeros((224,224))
            tmp_label_weights[action_area > 0] = 1
            label_weights[0,48:(320-48),48:(320-48)] = tmp_label_weights

            # Compute loss and backward pass
            self.optimizer.zero_grad()

                # Do forward pass with specified rotation (to save gradients)
            if self.training_target == "static_grasp":
                regulate_predictions, grasp_predictions, state_feat = self.forward(color_heightmap, depth_heightmap, is_volatile=False, specific_rotation=rotate, nonlocal_variables=nonlocal_variables)
            else:
                regulate_predictions, grasp_predictions, _, state_feat = self.forward(color_heightmap, depth_heightmap, is_volatile=False, specific_rotation=rotate, nonlocal_variables=nonlocal_variables)

            if self.training_target == "static_grasp":
                output_prob = self.static_model.output_prob
            else:
                output_prob = self.model.output_prob

            if self.use_cuda:
                loss = self.criterion(output_prob[0][1].view(1,320,320), Variable(torch.from_numpy(label).float().cuda())) * Variable(torch.from_numpy(label_weights).float().cuda(),requires_grad=False)
            else:
                loss = self.criterion(output_prob[0][1].view(1,320,320), Variable(torch.from_numpy(label).float())) * Variable(torch.from_numpy(label_weights).float(),requires_grad=False)
            loss = loss.sum()

            loss.backward()
            loss_value = loss.cpu().data.numpy()

            opposite_rotate_idx = (rotate + self.model.num_rotations/2) % self.model.num_rotations

            if self.training_target == "static_grasp":
                regulate_predictions, grasp_predictions, state_feat = self.forward(color_heightmap, depth_heightmap, is_volatile=False, specific_rotation=opposite_rotate_idx, nonlocal_variables=nonlocal_variables)
                output_prob = self.static_model.output_prob
            else:
                regulate_predictions, grasp_predictions, _, state_feat = self.forward(color_heightmap, depth_heightmap, is_volatile=False, specific_rotation=opposite_rotate_idx, nonlocal_variables=nonlocal_variables)
                output_prob = self.model.output_prob

            if self.use_cuda:
                loss = self.criterion(output_prob[0][1].view(1,320,320), Variable(torch.from_numpy(label).float().cuda())) * Variable(torch.from_numpy(label_weights).float().cuda(),requires_grad=False)
            else:
                loss = self.criterion(output_prob[0][1].view(1,320,320), Variable(torch.from_numpy(label).float())) * Variable(torch.from_numpy(label_weights).float(),requires_grad=False)

            loss = loss.sum()
            loss.backward()
            loss_value = loss.cpu().data.numpy()

            loss_value += loss_value/2

            print('Training loss: %f' % (loss_value))
            self.optimizer.step()
            return loss_value

    # Compute labels and backpropagate
    def backprop(self, color_heightmap, depth_heightmap, primitive_action, best_pix_ind, label_value, regulate_residual=None, nonlocal_variables=None, teacher_student=False):
        # Compute labels
        label = np.zeros((1,320,320))
        action_area = np.zeros((224,224))
        action_area[best_pix_ind[1]][best_pix_ind[2]] = 1

        # blur_kernel = np.ones((5,5),np.float32)/25
        # action_area = cv2.filter2D(action_area, -1, blur_kernel)
        tmp_label = np.zeros((224,224))
        if regulate_residual:
            tmp_label[action_area > 0] = regulate_residual
        else:
            tmp_label[action_area > 0] = label_value

        label[0,48:(320-48),48:(320-48)] = tmp_label

        # Compute label mask
        label_weights = np.zeros(label.shape)
        tmp_label_weights = np.zeros((224,224))
        tmp_label_weights[action_area > 0] = 1
        label_weights[0,48:(320-48),48:(320-48)] = tmp_label_weights

        # Compute loss and backward pass
        self.optimizer.zero_grad()
        loss_value = 0
        if 'regulate' in primitive_action :
            # Do forward pass with specified rotation (to save gradients)
            if self.training_target=="static_grasp":
                regulate_predictions, grasp_predictions, state_feat = self.forward(color_heightmap, depth_heightmap, is_volatile=False, specific_rotation=best_pix_ind[0], nonlocal_variables=nonlocal_variables)
            else:
                regulate_predictions, grasp_predictions, _, state_feat = self.forward(color_heightmap, depth_heightmap, is_volatile=False, specific_rotation=best_pix_ind[0], nonlocal_variables=nonlocal_variables)

            if self.use_cuda:
                loss = self.criterion(self.model.output_prob[0][0].view(1,320,320), Variable(torch.from_numpy(label).float().cuda())) * Variable(torch.from_numpy(label_weights).float().cuda(),requires_grad=False)
            else:
                loss = self.criterion(self.model.output_prob[0][0].view(1,320,320), Variable(torch.from_numpy(label).float())) * Variable(torch.from_numpy(label_weights).float(),requires_grad=False)
            loss = loss.sum()
            loss.backward()
            loss_value = loss.cpu().data.numpy()

        elif primitive_action == 'grasp':

            # Do forward pass with specified rotation (to save gradients)
            if self.training_target == "static_grasp":
                regulate_predictions, grasp_predictions, state_feat = self.forward(color_heightmap, depth_heightmap, is_volatile=False, specific_rotation=best_pix_ind[0], nonlocal_variables=nonlocal_variables)
            else:
                regulate_predictions, grasp_predictions, _, static_grasp_predictions = self.forward(color_heightmap, depth_heightmap, is_volatile=False, specific_rotation=best_pix_ind[0], nonlocal_variables=nonlocal_variables)
            if teacher_student:
                label[0,48:(320-48),48:(320-48)] = static_grasp_predictions[0]

            if self.training_target == "static_grasp":
                output_prob = self.static_model.output_prob
            else:
                output_prob = self.model.output_prob
            if self.use_cuda:
                loss = self.criterion(output_prob[0][1].view(1,320,320), Variable(torch.from_numpy(label).float().cuda())) * Variable(torch.from_numpy(label_weights).float().cuda(),requires_grad=False)
            else:
                loss = self.criterion(output_prob[0][1].view(1,320,320), Variable(torch.from_numpy(label).float())) * Variable(torch.from_numpy(label_weights).float(),requires_grad=False)
            loss = loss.sum()
            loss.backward()
            loss_value = loss.cpu().data.numpy()

            opposite_rotate_idx = (best_pix_ind[0] + self.model.num_rotations/2) % self.model.num_rotations

            if self.training_target == "static_grasp":
                regulate_predictions, grasp_predictions, state_feat = self.forward(color_heightmap, depth_heightmap, is_volatile=False, specific_rotation=opposite_rotate_idx, nonlocal_variables=nonlocal_variables)
                output_prob = self.static_model.output_prob
            else:
                regulate_predictions, grasp_predictions, _, state_feat = self.forward(color_heightmap, depth_heightmap, is_volatile=False, specific_rotation=opposite_rotate_idx, nonlocal_variables=nonlocal_variables)
                output_prob = self.model.output_prob

            if self.use_cuda:
                loss = self.criterion(output_prob[0][1].view(1,320,320), Variable(torch.from_numpy(label).float().cuda())) * Variable(torch.from_numpy(label_weights).float().cuda(),requires_grad=False)
            else:
                loss = self.criterion(output_prob[0][1].view(1,320,320), Variable(torch.from_numpy(label).float())) * Variable(torch.from_numpy(label_weights).float(),requires_grad=False)

            loss = loss.sum()
            loss.backward()
            loss_value = loss.cpu().data.numpy()

            loss_value = loss_value/2
        else:
            raise NotImplementedError()

        print('Training loss: %f' % (loss_value))
        self.optimizer.step()
        return loss_value

    def min_max(self, x, axis=None):
        min, max = x.min(), x.max()
        return (x-min)/(max-min)

    # def visualization_for_find_grasp_point(self, predictions):
    #     predictions = torch.tensor(self.min_max(predictions))
    #     predictions = nn.MaxPool2d(kernel_size=kernel_size, padding=padding)(predictions).numpy()

    #     predictions = np.array([cv2.resize(p, (224, 224)) for p in predictions])
    #     predictions = np.where(predictions>0.8, 1, 0)

    def get_multi_targets_pix_ind(self, predictions_, best_pix_ind, explore_actions=None,kernel_size=(11, 11), stride=None, padding=5):
        predictions = torch.tensor(self.min_max(predictions_))
        predictions = nn.MaxPool2d(kernel_size=kernel_size, padding=padding)(predictions).numpy()

        predictions = np.array([cv2.resize(p, (224, 224)) for p in predictions])
        predictions = np.where(predictions>0.6, 1, 0)

        predictions = np.array([cv2.applyColorMap((prediction*255).astype(np.uint8), cv2.COLORMAP_JET) for prediction in predictions])

        # if explore_actions:
        #     explore = np.random.randint(0, 16)
        #     centers_x, centers_y = self.get_objects_center_of_gravity(predictions[explore])
        #     print(f'*explore: use {explore} rotation instead of {best_pix_ind[0]}')
        # else:
        centers_x, centers_y = self.get_objects_center_of_gravity(predictions[best_pix_ind[0]])

        grasp_points = []
        for x,y in zip(centers_x, centers_y):
            value = -np.inf

            for i, prediction in enumerate(predictions_):
                if value < prediction[(y, x)]:
                    value = prediction[(y, x)]
                    idx = i
            grasp_points.append({"rotate":idx, "x":x, "y":y, "grasp_success":None})

        return grasp_points

    def get_objects_center_of_gravity(self, img):
        ret_x, ret_y = [], []
        gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
        gray = np.where(gray==gray.min(), 255, 0).astype(np.uint8)

        controus, _ = cv2.findContours(gray, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
        for c in controus:
            m = cv2.moments(c)
            ret_x.append(int(m["m10"]/(m["m00"]+1e-6)))
            ret_y.append(int(m["m01"]/(m["m00"]+1e-6)))
        return ret_x, ret_y

    def get_multi_prediction_vis(self, predictions, color_heightmap, best_pix_ind, multi_pix_ind=None, type=None, kernel_size=(11, 11), stride=None, padding=5):
        predictions = torch.tensor(self.min_max(predictions))
        predictions = nn.MaxPool2d(kernel_size=kernel_size, padding=padding)(predictions).numpy()

        predictions = np.array([cv2.resize(p, (224, 224)) for p in predictions])
        predictions = np.where(predictions>0.6, 1, 0)
        # predictions = self.visualization_for_find_grasp_point(predictions)

        canvas = None
        num_rotations = predictions.shape[0]
        for canvas_row in range(int(num_rotations/4)):
            tmp_row_canvas = None
            for canvas_col in range(4):
                rotate_idx = canvas_row*4+canvas_col
                prediction_vis = predictions[rotate_idx,:,:].copy()
                # prediction_vis[prediction_vis < 0] = 0 # assume probability
                # prediction_vis[prediction_vis > 1] = 1 # assume probability
                if multi_pix_ind:
                    grasp_points_x = [item["x"] for item in multi_pix_ind if item["rotate"]==rotate_idx]
                    grasp_points_y = [item["y"] for item in multi_pix_ind if item["rotate"]==rotate_idx]

                if type=="conveyor":
                    prediction_vis = (prediction_vis).copy()
                elif type!=None:
                    prediction_vis = np.clip(prediction_vis, 0, 1)
                prediction_vis.shape = (predictions.shape[1], predictions.shape[2])
                prediction_vis = cv2.applyColorMap((prediction_vis*255).astype(np.uint8), cv2.COLORMAP_JET)
                objects_center_x, objects_center_y = self.get_objects_center_of_gravity(prediction_vis)

                for x, y in zip(objects_center_x, objects_center_y):
                    # plt.plot(x, y, marker='.')
                    prediction_vis = cv2.circle(prediction_vis, (int(x), int(y)), 1, (0,255,0), 2)
                if multi_pix_ind:
                    for x,y in zip(grasp_points_x, grasp_points_y):
                        prediction_vis = cv2.circle(prediction_vis, (int(x), int(y)), 1, (255,255,255), 2)
                if type=="picture":
                    prediction_vis = color_heightmap.copy()
                if rotate_idx == best_pix_ind[0]:
                    # some grasp points are duplicated, so execute counts and visual points look different numbers.
                    prediction_vis = cv2.circle(prediction_vis, (int(best_pix_ind[2]), int(best_pix_ind[1])), 7, (0,0,255), 2)

                prediction_vis = ndimage.rotate(prediction_vis, rotate_idx*(360.0/num_rotations), reshape=False, order=0)
                background_image = ndimage.rotate(color_heightmap, rotate_idx*(360.0/num_rotations), reshape=False, order=0)
                prediction_vis = (0.5*cv2.cvtColor(background_image, cv2.COLOR_RGB2BGR) + 0.5*prediction_vis).astype(np.uint8)
                if tmp_row_canvas is None:
                    tmp_row_canvas = prediction_vis
                else:
                    tmp_row_canvas = np.concatenate((tmp_row_canvas,prediction_vis), axis=1)
            if canvas is None:
                canvas = tmp_row_canvas
            else:
                canvas = np.concatenate((canvas,tmp_row_canvas), axis=0)

        return canvas

    def get_prediction_vis(self, predictions, color_heightmap, best_pix_ind, type=None):

        canvas = None
        num_rotations = predictions.shape[0]
        for canvas_row in range(int(num_rotations/4)):
            tmp_row_canvas = None
            for canvas_col in range(4):
                rotate_idx = canvas_row*4+canvas_col
                prediction_vis = predictions[rotate_idx,:,:].copy()
                # prediction_vis[prediction_vis < 0] = 0 # assume probability
                # prediction_vis[prediction_vis > 1] = 1 # assume probability
                if type=="conveyor":
                    prediction_vis = (prediction_vis).copy()
                else:
                    prediction_vis = np.clip(prediction_vis, 0, 1)
                prediction_vis.shape = (predictions.shape[1], predictions.shape[2])
                prediction_vis = cv2.applyColorMap((prediction_vis*255).astype(np.uint8), cv2.COLORMAP_JET)
                if type=="picture":
                    prediction_vis = color_heightmap.copy()
                if rotate_idx == best_pix_ind[0]:
                    prediction_vis = cv2.circle(prediction_vis, (int(best_pix_ind[2]), int(best_pix_ind[1])), 7, (0,0,255), 2)
                prediction_vis = ndimage.rotate(prediction_vis, rotate_idx*(360.0/num_rotations), reshape=False, order=0)
                background_image = ndimage.rotate(color_heightmap, rotate_idx*(360.0/num_rotations), reshape=False, order=0)
                prediction_vis = (0.5*cv2.cvtColor(background_image, cv2.COLOR_RGB2BGR) + 0.5*prediction_vis).astype(np.uint8)
                if tmp_row_canvas is None:
                    tmp_row_canvas = prediction_vis
                else:
                    tmp_row_canvas = np.concatenate((tmp_row_canvas,prediction_vis), axis=1)
            if canvas is None:
                canvas = tmp_row_canvas
            else:
                canvas = np.concatenate((canvas,tmp_row_canvas), axis=0)

        return canvas

    def grasp_heuristic(self, depth_heightmap):

        num_rotations = 16

        for rotate_idx in range(num_rotations):
            rotated_heightmap = ndimage.rotate(depth_heightmap, rotate_idx*(360.0/num_rotations), reshape=False, order=0)
            valid_areas = np.zeros(rotated_heightmap.shape)
            valid_areas[np.logical_and(rotated_heightmap - ndimage.interpolation.shift(rotated_heightmap, [0,-25], order=0) > 0.02, rotated_heightmap - ndimage.interpolation.shift(rotated_heightmap, [0,25], order=0) > 0.02)] = 1
            # valid_areas = np.multiply(valid_areas, rotated_heightmap)
            blur_kernel = np.ones((25,25),np.float32)/9
            valid_areas = cv2.filter2D(valid_areas, -1, blur_kernel)
            tmp_grasp_predictions = ndimage.rotate(valid_areas, -rotate_idx*(360.0/num_rotations), reshape=False, order=0)
            tmp_grasp_predictions.shape = (1, rotated_heightmap.shape[0], rotated_heightmap.shape[1])

            if rotate_idx == 0:
                grasp_predictions = tmp_grasp_predictions
            else:
                grasp_predictions = np.concatenate((grasp_predictions, tmp_grasp_predictions), axis=0)

        best_pix_ind = np.unravel_index(np.argmax(grasp_predictions), grasp_predictions.shape)
        return best_pix_ind


import discord
from ..boot import *
from discord.ext import commands

@sage.command()
async def avatar(ctx,*, member: discord.Member):
    pfp= member.avatar_url
    embed= discord.Embed(title=f"{member}'s avatar", colour=0xff0000)
    embed.set_image(url=pfp)
    await ctx.reply(embed=embed)

@avatar.error
async def avatar(ctx, error):
    message = error
    await ctx.reply(message)


"""Set of methods to build Transducer encoder architecture."""

from typing import Any, Dict, List, Optional, Union

from espnet2.asr_transducer.activation import get_activation
from espnet2.asr_transducer.encoder.blocks.branchformer import Branchformer
from espnet2.asr_transducer.encoder.blocks.conformer import Conformer
from espnet2.asr_transducer.encoder.blocks.conv1d import Conv1d
from espnet2.asr_transducer.encoder.blocks.conv_input import ConvInput
from espnet2.asr_transducer.encoder.blocks.ebranchformer import EBranchformer
from espnet2.asr_transducer.encoder.modules.attention import (  # noqa: H301
    RelPositionMultiHeadedAttention,
)
from espnet2.asr_transducer.encoder.modules.convolution import (  # noqa: H301
    ConformerConvolution,
    ConvolutionalSpatialGatingUnit,
    DepthwiseConvolution,
)
from espnet2.asr_transducer.encoder.modules.multi_blocks import MultiBlocks
from espnet2.asr_transducer.encoder.modules.positional_encoding import (  # noqa: H301
    RelPositionalEncoding,
)
from espnet2.asr_transducer.normalization import get_normalization
from espnet.nets.pytorch_backend.transformer.positionwise_feed_forward import (
    PositionwiseFeedForward,
)

def build_main_parameters(
    pos_wise_act_type: str = "swish",
    conv_mod_act_type: str = "swish",
    pos_enc_dropout_rate: float = 0.0,
    pos_enc_max_len: int = 5000,
    simplified_att_score: bool = False,
    norm_type: str = "layer_norm",
    conv_mod_norm_type: str = "layer_norm",
    after_norm_eps: Optional[float] = None,
    after_norm_partial: Optional[float] = None,
    blockdrop_rate: float = 0.0,
    dynamic_chunk_training: bool = False,
    short_chunk_threshold: float = 0.75,
    short_chunk_size: int = 25,
    num_left_chunks: int = 0,
    **activation_parameters,
) -> Dict[str, Any]:
    """Build encoder main parameters.

    Args:
        pos_wise_act_type: X-former position-wise feed-forward activation type.
        conv_mod_act_type: X-former convolution module activation type.
        pos_enc_dropout_rate: Positional encoding dropout rate.
        pos_enc_max_len: Positional encoding maximum length.
        simplified_att_score: Whether to use simplified attention score computation.
        norm_type: X-former normalization module type.
        conv_mod_norm_type: Conformer convolution module normalization type.
        after_norm_eps: Epsilon value for the final normalization.
        after_norm_partial: Value for the final normalization with RMSNorm.
        blockdrop_rate: Probability threshold of dropping out each encoder block.
        dynamic_chunk_training: Whether to use dynamic chunk training.
        short_chunk_threshold: Threshold for dynamic chunk selection.
        short_chunk_size: Minimum number of frames during dynamic chunk training.
        num_left_chunks: Number of left chunks the attention module can see.
                         (null or negative value means full context)
        **activation_parameters: Parameters of the activation functions.
                                 (See espnet2/asr_transducer/activation.py)

    Returns:
        : Main encoder parameters

    """
    main_params = {}

    main_params["pos_wise_act"] = get_activation(
        pos_wise_act_type, **activation_parameters
    )

    main_params["conv_mod_act"] = get_activation(
        conv_mod_act_type, **activation_parameters
    )

    main_params["pos_enc_dropout_rate"] = pos_enc_dropout_rate
    main_params["pos_enc_max_len"] = pos_enc_max_len

    main_params["simplified_att_score"] = simplified_att_score

    main_params["norm_type"] = norm_type
    main_params["conv_mod_norm_type"] = conv_mod_norm_type

    (
        main_params["after_norm_class"],
        main_params["after_norm_args"],
    ) = get_normalization(norm_type, eps=after_norm_eps, partial=after_norm_partial)

    main_params["blockdrop_rate"] = blockdrop_rate

    main_params["dynamic_chunk_training"] = dynamic_chunk_training
    main_params["short_chunk_threshold"] = max(0, short_chunk_threshold)
    main_params["short_chunk_size"] = max(0, short_chunk_size)
    main_params["num_left_chunks"] = max(0, num_left_chunks)

    return main_params

def build_positional_encoding(
    block_size: int, configuration: Dict[str, Any]
) -> RelPositionalEncoding:
    """Build positional encoding block.

    Args:
        block_size: Input/output size.
        configuration: Positional encoding configuration.

    Returns:
        : Positional encoding module.

    """
    return RelPositionalEncoding(
        block_size,
        configuration.get("pos_enc_dropout_rate", 0.0),
        max_len=configuration.get("pos_enc_max_len", 5000),
    )

def build_input_block(
    input_size: int,
    configuration: Dict[str, Union[str, int]],
) -> ConvInput:
    """Build encoder input block.

    Args:
        input_size: Input size.
        configuration: Input block configuration.

    Returns:
        : ConvInput block function.

    """
    return ConvInput(
        input_size,
        configuration["conv_size"],
        configuration["subsampling_factor"],
        vgg_like=configuration["vgg_like"],
        output_size=configuration["output_size"],
    )

def build_branchformer_block(
    configuration: List[Dict[str, Any]],
    main_params: Dict[str, Any],
) -> Branchformer:
    """Build Branchformer block.

    Args:
        configuration: Branchformer block configuration.
        main_params: Encoder main parameters.

    Returns:
        : Branchformer block function.

    """
    hidden_size = configuration["hidden_size"]
    linear_size = configuration["linear_size"]

    dropout_rate = configuration.get("dropout_rate", 0.0)

    conv_mod_norm_class, conv_mod_norm_args = get_normalization(
        main_params["conv_mod_norm_type"],
        eps=configuration.get("conv_mod_norm_eps"),
        partial=configuration.get("conv_mod_norm_partial"),
    )

    conv_mod_args = (
        linear_size,
        configuration["conv_mod_kernel_size"],
        conv_mod_norm_class,
        conv_mod_norm_args,
        dropout_rate,
        main_params["dynamic_chunk_training"],
    )

    mult_att_args = (
        configuration.get("heads", 4),
        hidden_size,
        configuration.get("att_dropout_rate", 0.0),
        main_params["simplified_att_score"],
    )

    norm_class, norm_args = get_normalization(
        main_params["norm_type"],
        eps=configuration.get("norm_eps"),
        partial=configuration.get("norm_partial"),
    )

    return lambda: Branchformer(
        hidden_size,
        linear_size,
        RelPositionMultiHeadedAttention(*mult_att_args),
        ConvolutionalSpatialGatingUnit(*conv_mod_args),
        norm_class=norm_class,
        norm_args=norm_args,
        dropout_rate=dropout_rate,
    )

def build_conformer_block(
    configuration: List[Dict[str, Any]],
    main_params: Dict[str, Any],
) -> Conformer:
    """Build Conformer block.

    Args:
        configuration: Conformer block configuration.
        main_params: Encoder main parameters.

    Returns:
        : Conformer block function.

    """
    hidden_size = configuration["hidden_size"]
    linear_size = configuration["linear_size"]

    pos_wise_args = (
        hidden_size,
        linear_size,
        configuration.get("pos_wise_dropout_rate", 0.0),
        main_params["pos_wise_act"],
    )

    conv_mod_norm_args = {
        "eps": configuration.get("conv_mod_norm_eps", 1e-05),
        "momentum": configuration.get("conv_mod_norm_momentum", 0.1),
    }

    conv_mod_args = (
        hidden_size,
        configuration["conv_mod_kernel_size"],
        main_params["conv_mod_act"],
        conv_mod_norm_args,
        main_params["dynamic_chunk_training"],
    )

    mult_att_args = (
        configuration.get("heads", 4),
        hidden_size,
        configuration.get("att_dropout_rate", 0.0),
        main_params["simplified_att_score"],
    )

    norm_class, norm_args = get_normalization(
        main_params["norm_type"],
        eps=configuration.get("norm_eps"),
        partial=configuration.get("norm_partial"),
    )

    return lambda: Conformer(
        hidden_size,
        RelPositionMultiHeadedAttention(*mult_att_args),
        PositionwiseFeedForward(*pos_wise_args),
        PositionwiseFeedForward(*pos_wise_args),
        ConformerConvolution(*conv_mod_args),
        norm_class=norm_class,
        norm_args=norm_args,
        dropout_rate=configuration.get("dropout_rate", 0.0),
    )

def build_conv1d_block(
    configuration: List[Dict[str, Any]],
    causal: bool,
) -> Conv1d:
    """Build Conv1d block.

    Args:
        configuration: Conv1d block configuration.

    Returns:
        : Conv1d block function.

    """
    return lambda: Conv1d(
        configuration["input_size"],
        configuration["output_size"],
        configuration["kernel_size"],
        stride=configuration.get("stride", 1),
        dilation=configuration.get("dilation", 1),
        groups=configuration.get("groups", 1),
        bias=configuration.get("bias", True),
        relu=configuration.get("relu", True),
        batch_norm=configuration.get("batch_norm", False),
        causal=causal,
        dropout_rate=configuration.get("dropout_rate", 0.0),
    )

def build_ebranchformer_block(
    configuration: List[Dict[str, Any]],
    main_params: Dict[str, Any],
) -> EBranchformer:
    """Build E-Branchformer block.

    Args:
        configuration: E-Branchformer block configuration.
        main_params: Encoder main parameters.

    Returns:
        : E-Branchformer block function.

    """
    hidden_size = configuration["hidden_size"]
    linear_size = configuration["linear_size"]

    dropout_rate = configuration.get("dropout_rate", 0.0)

    pos_wise_args = (
        hidden_size,
        linear_size,
        configuration.get("pos_wise_dropout_rate", 0.0),
        main_params["pos_wise_act"],
    )

    conv_mod_norm_class, conv_mod_norm_args = get_normalization(
        main_params["conv_mod_norm_type"],
        eps=configuration.get("conv_mod_norm_eps"),
        partial=configuration.get("conv_mod_norm_partial"),
    )

    conv_mod_args = (
        linear_size,
        configuration["conv_mod_kernel_size"],
        conv_mod_norm_class,
        conv_mod_norm_args,
        dropout_rate,
        main_params["dynamic_chunk_training"],
    )

    mult_att_args = (
        configuration.get("heads", 4),
        hidden_size,
        configuration.get("att_dropout_rate", 0.0),
        main_params["simplified_att_score"],
    )

    depthwise_conv_args = (
        hidden_size,
        configuration.get(
            "depth_conv_kernel_size", configuration["conv_mod_kernel_size"]
        ),
        main_params["dynamic_chunk_training"],
    )

    norm_class, norm_args = get_normalization(
        main_params["norm_type"],
        eps=configuration.get("norm_eps"),
        partial=configuration.get("norm_partial"),
    )

    return lambda: EBranchformer(
        hidden_size,
        linear_size,
        RelPositionMultiHeadedAttention(*mult_att_args),
        PositionwiseFeedForward(*pos_wise_args),
        PositionwiseFeedForward(*pos_wise_args),
        ConvolutionalSpatialGatingUnit(*conv_mod_args),
        DepthwiseConvolution(*depthwise_conv_args),
        norm_class=norm_class,
        norm_args=norm_args,
        dropout_rate=dropout_rate,
    )

def build_body_blocks(
    configuration: List[Dict[str, Any]],
    main_params: Dict[str, Any],
    output_size: int,
) -> MultiBlocks:
    """Build encoder body blocks.

    Args:
        configuration: Body blocks configuration.
        main_params: Encoder main parameters.
        output_size: Architecture output size.

    Returns:
        MultiBlocks function encapsulation all encoder blocks.

    """
    fn_modules = []
    extended_conf = []

    for c in configuration:
        if c.get("num_blocks") is not None:
            extended_conf += c["num_blocks"] * [
                {c_i: c[c_i] for c_i in c if c_i != "num_blocks"}
            ]
        else:
            extended_conf += [c]

    for i, c in enumerate(extended_conf):
        block_type = c["block_type"]

        if block_type == "branchformer":
            module = build_branchformer_block(c, main_params)
        elif block_type == "conformer":
            module = build_conformer_block(c, main_params)
        elif block_type == "conv1d":
            module = build_conv1d_block(c, main_params["dynamic_chunk_training"])
        elif block_type == "ebranchformer":
            module = build_ebranchformer_block(c, main_params)
        else:
            raise NotImplementedError

        fn_modules.append(module)

    return MultiBlocks(
        [fn() for fn in fn_modules],
        output_size,
        norm_class=main_params["after_norm_class"],
        norm_args=main_params["after_norm_args"],
        blockdrop_rate=main_params["blockdrop_rate"],
    )


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

"""TrainingPreparator engine action.

Use this module to add the project main code.
"""

from sklearn.cross_validation import train_test_split

from .._compatibility import six
from .._logging import get_logger

from marvin_python_toolbox.engine_base import EngineBaseDataHandler

__all__ = ['TrainingPreparator']

logger = get_logger('training_preparator')

class TrainingPreparator(EngineBaseDataHandler):

    def __init__(self, **kwargs):
        super(TrainingPreparator, self).__init__(**kwargs)

    def execute(self, params, **kwargs):
        # Joined dataset
        train, test = train_test_split(self.marvin_initial_dataset, test_size=params["test_size"])

        train_X = train[['SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm', 'PetalWidthCm']]
        train_y = train.Species

        test_X = test[['SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm', 'PetalWidthCm']]
        test_y = test.Species

        # Separeted dataset
        petal = self.marvin_initial_dataset[['PetalLengthCm', 'PetalWidthCm', 'Species']]
        sepal = self.marvin_initial_dataset[['SepalLengthCm', 'SepalWidthCm', 'Species']]

        train_p, test_p = train_test_split(petal, test_size=params["test_size"], random_state=params["random_state"])
        train_x_p = train_p[['PetalWidthCm', 'PetalLengthCm']]
        train_y_p = train_p.Species
        test_x_p = test_p[['PetalWidthCm', 'PetalLengthCm']]
        test_y_p = test_p.Species

        train_s, test_s = train_test_split(sepal, test_size=params["test_size"], random_state=params["random_state"])
        train_x_s = train_s[['SepalWidthCm', 'SepalLengthCm']]
        train_y_s = train_s.Species
        test_x_s = test_s[['SepalWidthCm', 'SepalLengthCm']]
        test_y_s = test_s.Species

        self.marvin_dataset = {
            'petals': {'train_X': train_x_p, 'train_y': train_y_p, 'test_X': test_x_p, 'test_y': test_y_p},
            'sepals': {'train_X': train_x_s, 'train_y': train_y_s, 'test_X': test_x_s, 'test_y': test_y_s},
            'joined': {'train_X': train_X, 'train_y': train_y, 'test_X': test_X, 'test_y': test_y}
        }


import datetime
today = datetime.date.today()
year = today.year
month = today.month

class Obywatel:
    def __init__(abc,imie,nazwisko,dataUrodzenia):
        abc.imie=imie
        abc.nazwisko=nazwisko
        abc.dU=dataUrodzenia
        abc.dataUrodzenia=(dataUrodzenia.replace("."," ")).split()
        abc.miesiac=int(abc.dataUrodzenia[1])
        abc.rok=int(abc.dataUrodzenia[2])
        if month-abc.miesiac<0:
            abc.wiek=year-abc.rok-1
        if month-abc.miesiac>=0:
            abc.wiek=year-abc.rok

    def __str__(abc):
        return(f"Obywatel/ka {abc.imie} {abc.nazwisko}, {abc.wiek} lat, urodzony {abc.dU}")

osoba=Obywatel("Bruno","Maruwka","5.9.2003")
print(osoba.__str__())


import os
import random
from datetime import datetime, timedelta

import numpy as np
import pandas as pd
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import StratifiedKFold
from tqdm import tqdm

from config import DATA_DIR, TRAIN_PATH, TEST_PATH, PARAMS

def seed_everything(seed=0):
    random.seed(seed)
    os.environ['PYTHONHASHSEED'] = str(seed)
    np.random.seed(seed)

def load_data(data_type):
    indentity = pd.read_csv(os.path.join(DATA_DIR, f'{data_type}_identity.csv'))
    transaction = pd.read_csv(os.path.join(DATA_DIR, f'{data_type}_transaction.csv'))
    return pd.merge(transaction, indentity, on='TransactionID', how='left')

def update_dtypes(df, dtypes):
    start_mem = df.memory_usage().sum() / 1024**2
    for col, dtype in tqdm(dtypes.items()):
        df[col] = df[col].astype(dtype)
    end_mem = df.memory_usage().sum() / 1024**2
    print('Memory usage: {:5.2f} Mb -> {:5.2f} Mb ({:.1f} % reduction)'.format(start_mem, end_mem, 100 * (start_mem - end_mem) / start_mem))

def reduce_mem_usage(train, test, verbose=True):
    merged = pd.concat([train, test])
    numerics = ['int16', 'int32', 'int64', 'float16', 'float32', 'float64']
    dtypes = {}
    for col in merged.columns:
        col_type = merged[col].dtypes
        if col_type in numerics:
            c_min = merged[col].min()
            c_max = merged[col].max()
            if str(col_type).startswith('int'):
                if c_min > np.iinfo(np.int8).min and c_max < np.iinfo(np.int8).max:
                    dtypes[col] = np.int8
                elif c_min > np.iinfo(np.int16).min and c_max < np.iinfo(np.int16).max:
                    dtypes[col] = np.int16
                elif c_min > np.iinfo(np.int32).min and c_max < np.iinfo(np.int32).max:
                    dtypes[col] = np.int32
                elif c_min > np.iinfo(np.int64).min and c_max < np.iinfo(np.int64).max:
                    dtypes[col] = np.int64
            else:
                if c_min > np.finfo(np.float16).min and c_max < np.finfo(np.float16).max:
                    dtypes[col] = np.float16
                elif c_min > np.finfo(np.float32).min and c_max < np.finfo(np.float32).max:
                    dtypes[col] = np.float32
                else:
                    dtypes[col] = np.float64

    print('Updating train dtypes...')
    update_dtypes(train, dtypes)
    print('Updating test dtypes...')
    update_dtypes(test, dtypes)

def find_cols_to_drop(df):
    one_value = [col for col in df.columns if df[col].nunique() <= 1]
    many_null = [col for col in df.columns if df[col].isnull().sum() / df.shape[0] > 0.9]
    big_top_value = [col for col in df.columns if df[col].value_counts(dropna=False, normalize=True).values[0] > 0.9]
    return list(set(one_value + many_null + big_top_value))

def process_email(df):
    emails = {'gmail': 'google', 'att.net': 'att', 'twc.com': 'spectrum', 'scranton.edu': 'other', 'optonline.net': 'other',
              'hotmail.co.uk': 'microsoft', 'comcast.net': 'other', 'yahoo.com.mx': 'yahoo', 'yahoo.fr': 'yahoo',
              'yahoo.es': 'yahoo', 'charter.net': 'spectrum', 'live.com': 'microsoft', 'aim.com': 'aol', 'hotmail.de': 'microsoft',
              'centurylink.net': 'centurylink', 'gmail.com': 'google', 'me.com': 'apple', 'earthlink.net': 'other',
              'gmx.de': 'other', 'web.de': 'other', 'cfl.rr.com': 'other', 'hotmail.com': 'microsoft', 'protonmail.com': 'other',
              'hotmail.fr': 'microsoft', 'windstream.net': 'other', 'outlook.es': 'microsoft', 'yahoo.co.jp': 'yahoo',
              'yahoo.de': 'yahoo', 'servicios-ta.com': 'other', 'netzero.net': 'other', 'suddenlink.net': 'other',
              'roadrunner.com': 'other', 'sc.rr.com': 'other', 'live.fr': 'microsoft', 'verizon.net': 'yahoo',
              'msn.com': 'microsoft', 'q.com': 'centurylink', 'prodigy.net.mx': 'att', 'frontier.com': 'yahoo',
              'anonymous.com': 'other', 'rocketmail.com': 'yahoo', 'sbcglobal.net': 'att', 'frontiernet.net': 'yahoo',
              'ymail.com': 'yahoo', 'outlook.com': 'microsoft', 'mail.com': 'other', 'bellsouth.net': 'other',
              'embarqmail.com': 'centurylink', 'cableone.net': 'other', 'hotmail.es': 'microsoft', 'mac.com': 'apple',
              'yahoo.co.uk': 'yahoo', 'netzero.com': 'other', 'yahoo.com': 'yahoo', 'live.com.mx': 'microsoft', 'ptd.net': 'other',
              'cox.net': 'other', 'aol.com': 'aol', 'juno.com': 'other', 'icloud.com': 'apple'}

    us_emails = ['gmail', 'net', 'edu']

    for c in ['P_emaildomain', 'R_emaildomain']:
        df[c + '_bin'] = df[c].map(emails)
        df[c + '_suffix'] = df[c].map(lambda x: str(x).split('.')[-1])
        df[c + '_suffix'] = df[c + '_suffix'].map(lambda x: x if str(x) not in us_emails else 'us')

def process_domain(df):
    p = 'P_emaildomain'
    r = 'R_emaildomain'
    uknown = 'email_not_provided'
    df[p] = df[p].fillna(uknown)
    df[r] = df[r].fillna(uknown)
    df['email_check'] = np.where((df[p] == df[r]) & (df[p] != uknown), 1, 0)
    df[p + '_prefix'] = df[p].map(lambda x: x.split('.')[0])
    df[r + '_prefix'] = df[r].map(lambda x: x.split('.')[0])

def process_time(df):
    START_DATE = datetime.strptime('2017-11-30', '%Y-%m-%d')
    df['TransactionDT'] = df['TransactionDT'].fillna(df['TransactionDT'].median())
    df['dt'] = df['TransactionDT'].map(lambda x: (START_DATE + timedelta(seconds=x)))
    df['month'] = (df['dt'].dt.year - 2017) * 12 + df['dt'].dt.month
    df['woy'] = (df['dt'].dt.year - 2017) * 52 + df['dt'].dt.weekofyear
    df['dow'] = (df['dt'].dt.year - 2017) * 365 + df['dt'].dt.dayofyear

    df['hour'] = df['dt'].dt.hour
    df['dow'] = df['dt'].dt.dayofweek
    df['day'] = df['dt'].dt.day
    df.drop('dt', axis=1, inplace=True)

def process_D(df):
    df['D8'] = df['D8'].fillna(-1).astype(int)
    df['D8_not_same_day'] = np.where(df['D8'] >= 1, 1, 0)
    df['D9_not_na'] = np.where(df['D9'].isna(), 0, 1)
    df['D8_D9_decimal_dist'] = df['D8'].fillna(0) - df['D8'].fillna(0).astype(int)
    df['D8_D9_decimal_dist'] = ((df['D8_D9_decimal_dist'] - df['D9'])**2)**0.5

def impute_float(train, test):
    floats = ['float16', 'float32', 'float64']
    for col in train.columns:
        if train[col].dtype in floats or train[col].dtype in floats:
            merged = pd.concat([train[col], test[col]])
            train[col].fillna(merged.mean(), inplace=True)
            test[col].fillna(merged.mean(), inplace=True)

def label_encoding(train, test):
    train.fillna(-999, inplace=True)
    test.fillna(-999, inplace=True)

    for col in train.columns:
        if train[col].dtype == 'object' or test[col].dtype == 'object':
            le = LabelEncoder()
            le.fit(np.hstack((train[col].astype(str).values, test[col].astype(str).values)))
            train[col] = le.transform(train[col].astype(str).values)
            test[col] = le.transform(test[col].astype(str).values)

def freq_encoding(train, test):
    columns = ['card1', 'card2', 'card3', 'card5',
               'C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7', 'C8', 'C9', 'C10', 'C11', 'C12', 'C13', 'C14',
               'D1', 'D2', 'D3', 'D4', 'D5', 'D6', 'D7', 'D8',
               'addr1', 'addr2',
               'dist1', 'dist2',
               'P_emaildomain', 'R_emaildomain',
               'DeviceInfo', 'device_name',
               'id_30', 'id_33',
               'uid', 'uid2', 'uid3',
               ]

    for col in columns:
        if col in train.columns and col in test.columns:
            merged = pd.concat([train[col], test[col]])
            vc = merged.value_counts(dropna=False)
            train[col + '_freq_enc'] = train[col].map(vc)
            test[col + '_freq_enc'] = test[col].map(vc)

def target_encoding(X_train, y_train, X_test):
    columns = ['D1', 'D2', 'D3']
    skf = StratifiedKFold(**PARAMS['fold'])
    for idx_tr, idx_val in skf.split(X_train, y_train):
        X_tr, y_tr = X_train.iloc[idx_tr, :], y_train.iloc[idx_tr]
        X_val, _ = X_train.iloc[idx_val, :], y_train.iloc[idx_val]

        for col in columns:
            mapper = X_tr.assign(target=y_tr).groupby(col)['target'].mean()
            X_train.loc[idx_val, f'{col}_target_enc'] = X_val[col].map(mapper)

def assert_columns_equal(train, test):
    ncols_tr, ncols_te = len(train.columns), len(test.columns)
    assert ncols_tr == ncols_te, 'columns length must be equal: (train: {}, test: {}).'.format(ncols_tr, ncols_te)

    diff_cols = [(col_tr, col_te) for col_tr, col_te in zip(train.columns, test.columns) if col_tr != col_te]
    assert len(diff_cols) == 0, 'columns order must be equal: {}.'.format(diff_cols)

def assert_dtypes_equal(train, test):
    diff_dtypes = [(col, dt_tr, dt_te) for col, dt_tr, dt_te in zip(train.columns, train.dtypes, test.dtypes) if dt_tr != dt_te]
    assert len(diff_dtypes) == 0, 'dtypes must be equal: {}'.format(diff_dtypes)

def preprocess():
    train = load_data('train')
    y_train = train.pop('isFraud')
    test = load_data('test')
    assert_columns_equal(train, test)

    reduce_mem_usage(train, test)
    assert_dtypes_equal(train, test)

    cols_to_drop = list(set(find_cols_to_drop(train) + find_cols_to_drop(test)))
    train.drop(cols_to_drop, axis=1, inplace=True)
    test.drop(cols_to_drop, axis=1, inplace=True)
    print(f'Dropped {len(cols_to_drop)} columns.')

    # functions which can be applied separately
    funcs_single = [
        process_domain,
        process_email,
        process_time,
        process_D,
    ]

    for func in funcs_single:
        print('Applying:', func.__name__)
        func(train)
        func(test)

    cols = ['TransactionID', 'TransactionDT']
    train.drop(cols, axis=1, inplace=True)
    test.drop(cols, axis=1, inplace=True)

    # functions which can't be applied separately
    funcs_both = [
        impute_float,
        label_encoding,
        freq_encoding,
    ]

    for func in funcs_both:
        print('Applying:', func.__name__)
        func(train, test)

    target_encoding(train, y_train, test)

    reduce_mem_usage(train, test)
    train['isFraud'] = y_train.astype(np.int8)

    train.to_pickle(TRAIN_PATH)
    test.to_pickle(TEST_PATH)

if __name__ == '__main__':
    preprocess()


from Gäste import Gast
from Zimmern import *
from datetime import date
class Rezervierungen:
    def __init__(self,vorname,nachname,nummerzimmer,anfang,end):
        self.nummerzimmer = nummerzimmer
        self.anfang = anfang
        self.end = end
        self.vorname = vorname
        self.nachname = nachname

    def __str__(self):
        return f'Rezervierungen({self.vorname},{self.nachname},{self.nummerzimmer},{self.anfang},{self.end})'

class Rezlist:
    def __init__(self):
        self.rezlist = []

    def addRez(self,rez):
        self.rezlist.append(rez)

    def rezlistanzeige(self):
        for rez in self.rezlist:
            print(f'Vorname:{rez.vorname},Nachname:{rez.nachname},Nummerzimmer:{rez.nummerzimmer},DateAnfang:{rez.anfang},End:{rez.end}')

    def rezlistvonheute(self):
        d = date.today()
        print(d)
        for rez in self.rezlist:
            if str(rez.anfang) >= str(d):
                print(f'Vorname:{rez.vorname},Nachname:{rez.nachname},Nummerzimmer:{rez.nummerzimmer},DateAnfang:{rez.anfang},End:{rez.end}')

    def rezheutefrei(self):
        d = date.today()
        for rez in self.rezlist:
            if str(rez.anfang) > str(d) or str(rez.end) < str(d):
                print("Nummerzimmmer:"+ rez.nummerzimmer)

    def loschungrezlist(self,x):
        for i,rez in enumerate(self.rezlist):
            if rez.nummerzimmer == x:
                self.rezlist.pop(i)

def menurezervierungen(list2,list3):
    while True:
        print("""
        1-Macheinerezervierung
        2-rezeranzeige
        3-filterzimmer mit Preis and Meerblick
        4-rez loschung
        5-rezheutefrei
        6-exit
        """)
        opt = int(input("opt="))
        if opt==1:
            vorname = input("vorname: ")
            nachname = input("nachname: ")
            nummerzimmer = input("nummerzimmer:")
            anfang = input("anfang(yahr-monat-day):")
            end = input("end(yahr-monat-day):")
            list3.addRez(Rezervierungen(vorname,nachname,nummerzimmer,anfang,end))
        if opt==2:
            list3.rezlistvonheute()
        pickle.dump(list3, open("rezlist.txt", "wb"))
        if opt==3:
            preis = input("Zimmer billiger als preis: ")
            meer = input("Meerblick Ya/Nein: ")
            list2.filterzimmer(preis,meer)
        if opt==4:
            num = input("num=")
            list3.loschungrezlist(num)
        if opt==5:
            list3.rezheutefrei()
        pickle.dump(list3, open("rezlist.txt", "wb"))
        if opt==6:
            return 0


"""Create codes table

Revision ID: 2b4a2510bba3
Revises: 8ac42d3270f5
Create Date: 2023-06-20 14:13:01.153994

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '2b4a2510bba3'
down_revision = '8ac42d3270f5'
branch_labels = None
depends_on = None

def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('codes',
    sa.Column('code', sa.String(), nullable=False),
    sa.Column('ends_at', sa.Integer(), nullable=False),
    sa.PrimaryKeyConstraint('code')
    )
    # ### end Alembic commands ###

def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('codes')
    # ### end Alembic commands ###


import csv
import logging

import numpy as np

import camog._cfastcsv as cfastcsv

import _testhelper as th

_logger = logging.getLogger(__name__)

_maxlen = 6
_chars = '1.+-e '

def _divmod(n, m):
    return n // m, n % m

def _equal(v1, v2):
    """Same type as well."""
    if isinstance(v2, float) and np.isinf(v2):
        return True
    if isinstance(v2, str):
        v2 = th.string(v2)
    return v1 == v2

def _parse(s, excel_quotes=True):
    res = cfastcsv.parse_csv(s, ',', 1, 1 if excel_quotes else 0)
    return res[1][0][0]

def _py_csv_parse(s, excel_quotes=True):
    reader = csv.reader(th.string_io(s), dialect='excel')
    val = list(reader)[0][0]

    if not excel_quotes and s and s[0] == '"':
        return th.string(val)

    if val == ' ' * len(val):
        return 0

    try:
        vf = float(val)
    except ValueError:
        return th.string(val)

    try:
        vi = int(val)
    except ValueError:
        return vf

    return vi

def _assert_parse_same(s):
    res = _parse(s, True)
    py_res = _py_csv_parse(s, True)
    assert _equal(res, py_res), '%r => %r vs %r' % (s, res, py_res)

    res = _parse(s, False)
    py_res = _py_csv_parse(s, False)
    assert _equal(res, py_res), '%r => %r vs %r' % (s, res, py_res)

def test_py_csv_parse_1():
    s = '"123""456"789'
    assert _equal(_parse(s, True), '123"456789')
    assert _equal(_parse(s, False), '123"456789')
    assert _equal(_py_csv_parse(s, True), '123"456789')
    assert _equal(_py_csv_parse(s, False), '123"456789')

def test_py_csv_parse_2():
    s = '"123456"789'
    assert _equal(_parse(s, True), 123456789)
    assert _equal(_parse(s, False), '123456789')
    assert _equal(_py_csv_parse(s, True), 123456789)
    assert _equal(_py_csv_parse(s, False), '123456789')

def test_sign_1():
    s = '+123'
    assert _equal(_parse(s, True), 123)

def test_funny_plus():
    _assert_parse_same('+ ')

def test_missing_expo():
    assert _equal(_parse('1e', True), '1e')

def test_expo():
    for num_sign in ('', '+', '-'):
        for expo_sign in ('', '+', '-'):
            _assert_parse_same('%s1e%s2' % (num_sign, expo_sign))

def test_rounding():
    _assert_parse_same('.11111')

def test_negative_expo_1():
    _assert_parse_same('1e-300')

def test_negative_expo_2():
    _assert_parse_same('1e-320')

def test_negative_expo_3():
    _assert_parse_same('1.1111e-304')

def test_negative_expo_4():
    _assert_parse_same('1.1111e-315')

def test_float_neg_zero():
    csv_str = '''-0
1.0
'''

    res = cfastcsv.parse_csv(csv_str, ',', 1)[1]

    assert np.all(res[0] == np.array([-0.0, 1.0]))

def test_int_neg_zero():
    csv_str = '''-0
1
'''

    res = cfastcsv.parse_csv(csv_str, ',', 1)[1]

    assert np.all(res[0] == np.array([0, 1]))

def test_missing_int_val():
    csv_str = '\n'

    res = cfastcsv.parse_csv(csv_str, ',', 1, 0, 0, 123, 0.0)[1]

    assert res[0].dtype.kind == 'i'
    assert len(res[0]) == 1
    assert np.all(res[0] == np.array([123]))

def test_missing_int_val_2():
    csv_str = '1\n1\n1,3\n'

    res = cfastcsv.parse_csv(csv_str, ',', 1, 0, 0, 123, 0.0)[1]

    assert res[0].dtype.kind == 'i'
    assert res[1].dtype.kind == 'i'
    assert len(res[0]) == 3
    assert len(res[1]) == 3
    assert np.all(res[0] == np.array([1, 1, 1]))
    assert np.all(res[1] == np.array([123, 123, 3]))

def test_missing_float_val():
    csv_str = '\n12.'

    res = cfastcsv.parse_csv(csv_str, ',', 1, 0, 0, 0, 456.0)[1]

    assert res[0].dtype == float
    assert len(res[0]) == 2
    assert np.all(res[0] == np.array([456.0, 12.0]))

def test_missing_float_val2():
    csv_str = '1\n1\n1,3.0\n'

    res = cfastcsv.parse_csv(csv_str, ',', 1, 0, 0, 0, 456.0)[1]

    assert res[0].dtype.kind == 'i'
    assert res[1].dtype == float
    assert len(res[0]) == 3
    assert len(res[1]) == 3
    assert np.all(res[0] == np.array([1, 1, 1]))
    assert np.all(res[1] == np.array([456.0, 456.0, 3.0]))

def test_nan():
    csv_str = 'nan'

    res = cfastcsv.parse_csv(csv_str, ',', 1, 0, 0, 0, 456.0)[1]

    assert res[0].dtype == float
    assert np.all(np.isnan(res[0]))

def test_nan_space():
    csv_str = 'nan  '

    res = cfastcsv.parse_csv(csv_str, ',', 1, 0, 0, 0, 456.0)[1]

    assert res[0].dtype == float
    assert np.all(np.isnan(res[0]))

def test_inf():
    csv_str = 'inf'

    res = cfastcsv.parse_csv(csv_str, ',', 1, 0, 0, 0, 456.0)[1]

    assert res[0].dtype == float
    assert np.all(np.isinf(res[0]))
    assert np.all(res[0] > 0.0)

def test_minus_inf():
    csv_str = '-inf'

    res = cfastcsv.parse_csv(csv_str, ',', 1, 0, 0, 0, 456.0)[1]

    assert res[0].dtype == float
    assert np.all(np.isinf(res[0]))
    assert np.all(res[0] < 0.0)

def test_exact():
    csv_str = '1.0000000000000007'

    res = cfastcsv.parse_csv(csv_str, ',', 1, 0, 0)[1]

    assert res[0].dtype == float
    assert res[0][0] == 1.0000000000000007

def test_max_int_1():
    csv_str = str((1 << 63) - 1)

    res = cfastcsv.parse_csv(csv_str, ',', 1, 0, 0)[1]

    assert res[0].dtype.kind == 'i'
    assert res[0][0] == (1 << 63) - 1

def test_max_int_2():
    csv_str = str(1 << 63)

    res = cfastcsv.parse_csv(csv_str, ',', 1, 0, 0, 123, 456.0)[1]

    assert res[0].dtype.kind == 'i'
    assert res[0][0] == 123

def test_min_int_1():
    csv_str = str(-(1 << 63))

    res = cfastcsv.parse_csv(csv_str, ',', 1, 0, 0)[1]

    assert res[0].dtype.kind == 'i'
    assert res[0][0] == -(1 << 63)

def test_min_int_2():
    csv_str = str(-(1 << 63) - 1)

    res = cfastcsv.parse_csv(csv_str, ',', 1, 0, 0, 123, 456.0)[1]

    assert res[0].dtype.kind == 'i'
    assert res[0][0] == 123

def _do_parse(i):
    v = i
    v, n = _divmod(v, _maxlen)
    n += 1
    s = []
    for j in range(n):
        v, k = _divmod(v, len(_chars))
        s.append(_chars[k])

    csv_str = ''.join(s)

    _logger.debug('%r', csv_str)

    _assert_parse_same(csv_str)

def test_combinations():
    n = _maxlen * len(_chars) ** _maxlen
    _logger.info('n = %s', n)
    for i in range(n):
        _do_parse(i)


import numpy as np

from matplotlib import pyplot as plt
from optparse import OptionParser

parser = OptionParser()
parser.add_option("-f", "--filename", dest="filename",
                                  help="Result file to plot", metavar="FILE")

parser.add_option("--f1", "--filename1", dest="filename",
                                  help="Result file to plot", metavar="FILE")

parser.add_option("--l1", "--legend1", dest="legend1", default="Data 1",
                                  help="Label for data being plotted to appear in the legend")

parser.add_option("--f2", "--filename2", dest="filename2",
                                  help="Result file to plot", metavar="FILE")

parser.add_option("--l2", "--legend2", dest="legend2", default="Data 2",
                                  help="Label for data being plotted to appear in the legend")

parser.add_option("--f3", "--filename3", dest="filename3",
                                  help="Result file to plot", metavar="FILE")

parser.add_option("--l3", "--legend3", dest="legend3", default="Data 3",
                                  help="Label for data being plotted to appear in the legend")

parser.add_option("-o", "--outfile", dest="outfile",
                                  help="output name for plot", metavar="FILE")

parser.add_option("-q", "--quiet", dest="verbose", action="store_false", default=True,
                                    help = "Don't display plots")
(option, args) = parser.parse_args()

data = np.genfromtxt(option.filename)
ind  = np.argsort(data[:,-2])

# Plot the first file as a function of height
fig1 = plt.figure(figsize=(3.62, 2.76))
#fig1 = plt.figure()
ax = fig1.add_subplot(111)
ax.set_xlabel('Monument Height (m)')
ax.set_ylabel('RMS  (mm)')

# If we have two files then we want to do a comparison of results
if option.filename3 :
    data2 = np.genfromtxt(option.filename2)
    data3 = np.genfromtxt(option.filename3)
    ind2 = np.argsort(data2[:,-2])
    ind3  = np.argsort(data3[:,-2])

    ax.plot(data[ind,-2], data[ind,1], data2[ind2,-2], data2[ind2,1], data3[ind3,-2], data3[ind3,1])#,linewidth=2)
    ax.legend([option.legend1,option.legend2,option.legend3],fontsize=8)

elif option.filename2 :
    data2 = np.genfromtxt(option.filename2)
    ind2 = np.argsort(data2[:,-2])

    ax.plot(data[ind,-2], data[ind,1], data2[ind2,-2], data2[ind2,1],linewidth=2)
    ax.legend([option.legend1,option.legend2],fontsize=8)
else:
    ax.plot(data[ind,-2], data[ind,1],linewidth=2)

# vertical line from 143 to
ax.plot([-60, -60], [0, 6], 'k--', lw=1)
ax.plot([60, 60], [0, 6], 'k--', lw=1)

#plt.xticks([-90,-60,-30,0,30,60,90],['-90','-60','-30','0','30','60','90'])
plt.xticks([-90,-60,-30,0,30,60,90])

ax.set_xlabel('Latitude (degrees)')
ax.set_xlim([-90, 90])
ax.set_ylabel('RMS  (mm)')

for item in ([ax.title, ax.xaxis.label, ax.yaxis.label] +
                         ax.get_xticklabels() + ax.get_yticklabels()):
                    item.set_fontsize(8)

plt.tight_layout()

if option.outfile :
    fig1.savefig(option.outfile)

if option.verbose:
    plt.show()


import sys
import uuid

import insightface

from lib.faceswapper import FaceSwapper

assert insightface.__version__ >= '0.7'

if __name__ == '__main__':
    if len(sys.argv) != 3:
        print("usage: python main.py image_to_swap image_with_face")
        print(sys.argv)
        exit(1)

    image_to_swap = sys.argv[1]
    image_with_face = sys.argv[2]
    output = "./result/" + str(uuid.uuid4()) + ".jpg"

    FaceSwapper('lib/inswapper_128.onnx').swap(image_with_face, image_to_swap, output)


#import school_scores
#list_of_record = school_scores.get_all()

#print(list_of_record)

import suicide_attacks
import matplotlib.pyplot as plt
import numpy as np

list_of_attack = suicide_attacks.get_attacks()
ages = []
male = 0
female = 0
unknown = 0

for idx in range(len(list_of_attack)):
    gender = list_of_attack[idx]['attacker']['demographics']['gender']
    if gender == "Male":
        male += 1
    if gender == "Female":
        female += 1
    if gender == "Unknown":
        unknown += 1

objects = ("Male", "Female", "Unknown")

genders = [male, female, unknown]

y_pos = np.arange(len(objects))

s
plt.bar(y_pos, genders, align='center', alpha=0.5)
plt.xticks(y_pos, objects)
plt.ylabel("Number of People")
plt.title('Number of Attackers')
plt.show()


from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv

from .http_api import HttpApi
from .manifest import manifest

DOMAIN = manifest.domain
NAME = manifest.name
CONFIG_SCHEMA = cv.deprecated(DOMAIN)

async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
    entry_id = entry.entry_id
    url_path = f'/{entry_id}-local'
    hass.http.register_static_path(url_path, hass.config.path("custom_components/" + DOMAIN + "/www"))

    hass.components.frontend.async_register_built_in_panel("iframe",
        NAME, "mdi:folder", entry_id, {"url": f'{url_path}/index.html?v={manifest.version}'},
        entry.data.get('require_admin')
    )

    hass.http.register_view(HttpApi)
    return True

async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
    hass.components.frontend.async_remove_panel(entry.entry_id)
    return True


#!/bin/python

import os
from PIL import Image
from resizeimage import resizeimage
from flask import *
from werkzeug.utils import *

UPLOAD_FOLDER = '.\\img'
ALLOWED_EXTENSIONS = {'jpg','jpeg','png'}

# Flask app object...
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

@app.route('/', methods=['GET'])
def index():
    return render_template('index.html', page='Home Page')

@app.route('/resize/', methods=['POST'])
def resize():
    # Load image file
    data = request.files['img-upload']
    filename = secure_filename(data.filename)
    data.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))

    # Resize image file
    with open(os.path.join(app.config['UPLOAD_FOLDER'], filename), 'rb') as f:
        with Image.open(f) as image:
            try:
                cover = resizeimage.resize_cover(image, [500, 500])
            except resizeimage.ImageSizeError as e:
                return jsonify({'error' : e.message})
            cover.save(os.path.join(app.config['UPLOAD_FOLDER'], filename), image.format)

    # Send it back to front-end
    safe_path = safe_join(app.config['UPLOAD_FOLDER'], filename)
    return send_file(safe_path, as_attachment=True)

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


from __future__ import print_function

__author__ = ["Tomas Mendez Echenagucia"]
__copyright__ = "Copyright 2020, Design Machine Group - University of Washington"
__license__ = "MIT License"
__email__ = "tmendeze@uw.edu"
__version__ = "0.1.0"

import os
import json
import compas_energyplus
from compas_energyplus.datastructures import Construction

c1 = Construction()
c1.name = 'R13WALL'
c1.layers = ['R13LAYER',
             ]

c2 = Construction()
c2.name = 'FLOOR'
c2.layers = ['C5 - 4 IN HW CONCRETE',
             ]

c3 = Construction()
c3.name = 'ROOF31'
c3.layers = ['R31LAYER',
             ]

c4 = Construction()
c4.name = 'Generic Double Pane'
c4.layers = ['Generic Low-e Glass',
             'Generic Window Air Gap',
             'Generic Clear Glass',
             ]

constructions = [c1, c2, c3, c4]
data = {}
for con in constructions:
    data[con.name] = con.data

filepath = os.path.join(compas_energyplus.DATA, 'constructions', 'construction_library_simple.json')
with open(filepath, 'w+') as fp:
    json.dump(data, fp)


import datetime

from sqlalchemy import BigInteger, DateTime, ForeignKey
from sqlalchemy.orm import Mapped, mapped_column, relationship

from backend.db.base_class import Base

class Event(Base):
    __tablename__ = "events"
    id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
    repeat: Mapped[bool] = mapped_column(default=False)
    start: Mapped[datetime.datetime] = mapped_column()
    end: Mapped[datetime.datetime] = mapped_column(nullable=True)
    name: Mapped[str] = mapped_column()
    description: Mapped[str] = mapped_column(nullable=True)
    colour: Mapped[str] = mapped_column()
    user_id: Mapped[int] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"))

    user: Mapped["User"] = relationship(back_populates="events")


import sqlite3

nome_banco = "classe.db"
con = sqlite3.connect(nome_banco)
cur = con.cursor()

Classe = [
     (None,'João','branco', 17,True,True,'masculino',4,0 ),
     (None,'Dorival','pardo',18,True,True,'masculino',6,1 ),
     (None,'Edmundo','preto',13,True,True,'masculino',3,0 ),
     (None,'Carlos','branco', 15,False,True,'masculino',5,0),
     (None,'Eunice','amarelo',16,False,True,'feminino',3,1),
     (None,'Ana','preto',15,True,True,'feminino',5,0),
     (None,'Manuel','branco',13,False,False,'masculino',3,0 ),
     (None,'Ramyro', 'amarelo',14,True,True,'masculino',5,0 ),
     (None,'Joaquim','indigena',13,True,True,'masculino',6,0 ) ,
     (None,'Floriano','pardo',16,True, True,'masculino',3,0),
     (None,'Janaína','pardo',17,True,True,'feminino',3,0 ),
     (None,'César','branco',15,True,True,'masculino',5,0 ),
     (None,'Américo','pardo',17,False,False,'masculino',5,2 ),
     (None,'Eunice','preto',12,False,False,'feminino',4,0 ),
     (None,'Maria','pardo',16,True,False,'feminino',5,0 ),
     (None,'Roberto','indigena',14,False,True,'masculino',6,0),
     (None,'Rogério','preto',15 ,True, True,'masculino',4,0),
     (None,'Iago','preto',13,True,True ,'masculino',6 ,0),
     (None,'Carlos','branco',13,False,False,'masculino',3,0),
     (None,'Gustavo' ,'pardo',12,True,True,'masculino',5,0 ),
     (None,'Heitor','preto',12,True,True,'masculino',3,0),
     (None,'Márcia','preto',18,True,True,'feminino',4,1),
     (None,'Manuela','branco',13,True,True,'feminino',4,0),
     (None,'Clara','branco',15,False,True,'feminino',5,0),
     (None,'Ana Luiza','amarelo',16,True,True,'feminino',3,0),
     (None, 'Helena','branco',12,False,False,'feminino',5,0),
     (None,'Cristina','pardo',14,True,True,'feminino',4,0),
     (None,'Roselaine','preto',15 ,False,True,'feminino',5,0),
]

cur.executemany("INSERT INTO Classe VALUES (?,?,?,?,?,?,?,?,?)", Classe)

con.commit()
con.close


from pathlib import Path
import shutil
from uuid import uuid4

from django.conf import settings
import django.core.files
from django.core.management import call_command
from django.test import RequestFactory
from explorer.models import Query
from explorer.tests.factories import UserFactory as ExplorerUserFactory
from factory import Sequence, SubFactory
from factory.django import DjangoModelFactory
import pytest

from biospecdb.util import find_package_location
from uploader.models import SpectralData, UploadedFile, Center
from user.models import Center as UserCenter

DATA_PATH = Path(__file__).parent / "data"

@pytest.fixture(scope="function", autouse=True)
def exec_custom_markers(request, monkeypatch):
    auto_find = request.node.get_closest_marker("auto_find_previous_visit")
    if auto_find:
        monkeypatch.setattr(settings, "AUTO_FIND_PREVIOUS_VISIT", auto_find.args[0])

    # TODO: add all the other existing ones in here.

class CenterFactory(DjangoModelFactory):

    class Meta:
        model = UserCenter

    id = uuid4()
    name = Sequence(lambda n: 'name %03d' % n)
    country = Sequence(lambda n: 'country %03d' % n)

class UserFactory(ExplorerUserFactory):
    center = SubFactory(CenterFactory)

def rm_dir(path):
    if path.exists() and path.is_dir():
        shutil.rmtree(path)

def rm_all_media_dirs():
    from catalog.models import Dataset
    # Tidy up any created files.
    media_root = Path(settings.MEDIA_ROOT)
    rm_dir(media_root / UploadedFile.meta_data_file.field.upload_to)
    rm_dir(media_root / UploadedFile.spectral_data_file.field.upload_to)
    rm_dir(media_root / SpectralData.data.field.upload_to)
    rm_dir(media_root / Dataset.file.field.upload_to)

class SimpleQueryFactory(DjangoModelFactory):

    class Meta:
        model = Query

    title = Sequence(lambda n: f'My simple query {n}')
    sql = "select * from uploader_spectraldata"
    description = "Stuff"
    connection = settings.EXPLORER_DEFAULT_CONNECTION
    created_by_user = SubFactory(UserFactory)

@pytest.fixture(scope="function")
def django_request(center):
    request = RequestFactory()
    user = UserFactory()
    user.center = UserCenter.objects.get(name="SSEC")
    request.user = user
    return request

@pytest.fixture(scope="function")
def centers(django_db_blocker):
    with django_db_blocker.unblock():
        call_command('loaddata', "centers")
        call_command("loaddata",  "--database=bsr", "centers")

@pytest.fixture(scope="function")
def center(centers):
    return Center.objects.get(name="SSEC")

@pytest.fixture(scope="function")
def sql_views(django_db_blocker):
    with django_db_blocker.unblock():
        call_command('update_sql_views')

@pytest.fixture(scope="function")
def bio_sample_types(django_db_blocker):
    with django_db_blocker.unblock():
        call_command('loaddata', "--database=bsr", 'biosampletypes.json')

@pytest.fixture(scope="function")
def spectra_measurement_types(django_db_blocker):
    with django_db_blocker.unblock():
        call_command('loaddata', "--database=bsr", 'spectrameasurementtypes.json')

@pytest.fixture(scope="function")
def observables(django_db_blocker):
    with django_db_blocker.unblock():
        call_command('loaddata', "--database=bsr", 'observables.json')

@pytest.fixture(scope="function")
def instruments(django_db_blocker):
    with django_db_blocker.unblock():
        call_command('loaddata', "--database=bsr", 'instruments.json')

@pytest.fixture(scope="function")
def patients(django_db_blocker, centers):
    with django_db_blocker.unblock():
        call_command('loaddata', "--database=bsr",
                     str(find_package_location() / 'apps/uploader/tests/data/patients.json'))

@pytest.fixture(scope="function")
def visits(patients, django_db_blocker):
    with django_db_blocker.unblock():
        call_command('loaddata', "--database=bsr",
                     str(find_package_location() / 'apps/uploader/tests/data/visits.json'))

@pytest.fixture(scope="function")
def qcannotators(db, django_db_blocker):
    with django_db_blocker.unblock():
        call_command('loaddata', "--database=bsr", 'qcannotators.json')

@pytest.fixture(scope="function")
def mock_data(db, django_db_blocker, centers):
    # NOTE: Since this loads directly to the DB without any validation and thus call to loaddata(), no data files are
    # present. If you need actual spectral data, use ``mock_data_from_files`` below instead.
    with django_db_blocker.unblock():
        call_command('loaddata', "--database=bsr", 'test_data.json')

def bulk_upload():
    meta_data_path = (DATA_PATH / "meta_data").with_suffix(UploadedFile.FileFormats.XLSX)
    spectral_file_path = (DATA_PATH / "spectral_data").with_suffix(UploadedFile.FileFormats.XLSX)
    with meta_data_path.open(mode="rb") as meta_data:
        with spectral_file_path.open(mode="rb") as spectral_data:
            data_upload = UploadedFile(meta_data_file=django.core.files.File(meta_data,
                                                                             name=meta_data_path.name),
                                       spectral_data_file=django.core.files.File(spectral_data,
                                                                                name=spectral_file_path.name),
                                       center=Center.objects.get(name="SSEC"))
            data_upload.clean()
            data_upload.save()

@pytest.fixture(scope="function")
def mock_data_from_files(request,
                         monkeypatch,
                         db,
                         centers,
                         observables,
                         django_db_blocker,
                         instruments,
                         bio_sample_types,
                         spectra_measurement_types):
    # patch MEDIA_ROOT
    media_root = request.node.get_closest_marker("media_root")
    if media_root:
        monkeypatch.setattr(settings, "MEDIA_ROOT", media_root.args[0])

    # Turn off auto annotation functionality so that it isn't always being tested.
    auto_annotate = False if getattr(request, "param", None) is None else request.param
    monkeypatch.setattr(settings, "AUTO_ANNOTATE", auto_annotate)

    with django_db_blocker.unblock():
        bulk_upload()

    yield

    # Tidy up any created files.
    rm_all_media_dirs()


#!/usr/bin/env python

import logging
import binance
import email_sender
from datetime import datetime
from binance import Client
from typing import Dict, List, Optional, Any

class SendCryptoCurrencyEmail:
    """An SDK for working with binance API and sending email with your current crypto details.
    Full API documentation is available here: https://python-binance.readthedocs.io/en/latest/
    """

    def __init__(self):

        #CONSTANS
        #TODO usuń to przed wrzuceniem na gita
        self.api_key = ''
        self.api_secret = ''

        #INITIALIZE
        self.binance_client: Optional[binance.client.Client] = None

        #GET
        self.wallet_crypto_details: Optional[List[dict]] = []
        self.wallet_crypto_change24: Optional[List[dict]] = []

        #PREPARE
        #self.sosa

    def main(self) -> bool:
        if not self.prepare_payload_for_email():
            print("Failed to prepare email data! Exiting...")
        elif not self.send_crypto_news_email():
            print("Failed to send email! Exiting...")
        else:
            return True
        return False

    def initialize_binance(self, binance_client_obj: Optional[binance.client.Client] = None) -> bool:
        if binance_client_obj:
            self.binance_client = binance_client_obj
        elif not binance_client_obj:
            self.binance_client = binance.Client(self.api_key, self.api_secret)
        return True

    def get_wallet_data(self) -> bool:
        self.initialize_binance()
        for crypto_dict in self.binance_client.get_account().get('balances', []):
            if float(crypto_dict.get('free', '0')) > 0 or float(crypto_dict.get('locked', '0')) > 0:
                self.wallet_crypto_details.append(crypto_dict)
        print(self.wallet_crypto_details)
        return True

    def get_crypto_info24(self) -> bool:
        self.get_wallet_data()
        for crypto in self.wallet_crypto_details:
            if crypto.get("asset", "BTC") == 'BUSD':
                self.wallet_crypto_change24.append(self.binance_client.get_ticker(symbol=crypto.get("asset", "BUSD") + 'USDT'))
            else:
                self.wallet_crypto_change24.append(self.binance_client.get_ticker(symbol=crypto.get("asset", "BTC") + 'USDC'))
        print(self.wallet_crypto_change24)
        return True

    def prepare_payload_for_email(self) -> bool:
        """Prepares the message to sends it via e-mail

        :return bool: True if successfully prepared e-mail data
        """
        self.get_crypto_info24()

        return True

    def send_crypto_news_email(self) -> bool:
        """Docstring

        :return: bool: True if successfully send e-mail
        """
        with open("email.txt", "r") as file:
            email_list = file.readlines()
        send_obj = email_sender.SendEmail()
        send_obj.send_mail(motivation_massage=massage)

        return True

    @staticmethod
    def get_current_date() -> str:
        """Prepares the current date in the following format: Month day, year
        For example: "January 31, 2022", "May 3, 2000"

        :return str:  today's date formatted
        """
        try:
            return datetime.now().strftime('%B %-d, %Y')
        except ValueError:  # Previous convention for Linux, following convention for Windows
            return datetime.now().strftime('%B %#d, %Y')

cos = SendCryptoCurrencyEmail()
cos.get_wallet_data()
cos.get_crypto_info24()


import sys

#comment if you used pip install else append the appropriate paths
# sys.path.append('../')
# sys.path.append('../nuscenes_devkit/python_sdk/')

from collections import Counter
from collections import OrderedDict
from itertools import chain
from typing import List
import numpy as np
import pandas as pd
import json
import cv2
import os
import csv
from torch.utils.data import Dataset
import imageio
from nuscenes.nuscenes import NuScenes
from nuscenes.map_expansion.map_api import NuScenesMap
from nuscenes.eval.prediction.splits import get_prediction_challenge_split
from nuscenes.prediction import PredictHelper
from nuscenes.utils.splits import create_splits_scenes

import matplotlib.pyplot as plt
# %matplotlib inline
from nuscenes.prediction.input_representation.static_layers import StaticLayerRasterizer
from nuscenes.prediction.input_representation.agents import AgentBoxesWithFadedHistory
from nuscenes.prediction.input_representation.interface import InputRepresentation
from nuscenes.prediction.input_representation.combinators import Rasterizer

DATA_PATH = "../data/sets/nuscenes/" #path to data stored
DATA_VERSION = 'v1.0-mini'
NUM_AGENTS = 2
TRAJECTORY_TIME_INTERVAL = 6 #length (in time) per data point

class nuScenesdata(Dataset):

    def __init__(self, set_name="mini_train"):

        #assert statements
        set_paths = ['train', 'val', 'test', 'mini_train', 'mini_val', 'train_detect', 'train_track']
        assert set_name in set_paths, "Incorrect set_name"

        #Initialize data and Prediction Helper classes
        self.data_path = DATA_PATH
        self.nusc = NuScenes(version=DATA_VERSION, dataroot=self.data_path, verbose=True)
        self.helper = PredictHelper(self.nusc)

        #get all the scenes
        self.scenes = create_splits_scenes()

        #get all the scenes in the trainset
        self.set_name = set_name
        self.trainset = self.scenes[self.set_name] #List of scenes as part of training set
        self.prediction_scenes = json.load(open(self.data_path+"maps/prediction_scenes.json", "r")) #Dictionary containing list of instance and sample tokens for each scene

        print("Number of samples in train set: %d" % (len(self.trainset)))

    def __len__(self):
        return len(self.trainset) #return length of labels or input should be the same

    def __getitem__(self, test_idx):

        #get the scene
        scene = self.trainset[test_idx]

        #get all the tokens in the scene
        #List of scene tokens in the given scene where each item comprises of an instance token and a sample token seperated by underscore
        scene_tokens = self.prediction_scenes[scene]

        #Return if fewer than 2 tokens in this scene
        if len(scene_tokens) < 2:
            print("Not enough agents in the scene")
            return []

        #get the tokens in the scene: we will be using the instance tokens as that is the agent in the scene
        tokens = [scene_tok.split("_") for scene_tok in scene_tokens]

        #List of instance tokens and sample tokens
        instance_tokens, sample_tokens = list(list(zip(*tokens))[0]), list(list(zip(*tokens))[1])

        assert len(instance_tokens) == len(sample_tokens), "Instance and Sample tokens count does not match"

        '''
        1. Convert list of sample and instance tokens into an ordered dict where sample tokens are the keys
        2. Iterate over all combinations (of length TRAJECOTRY_TIME_INTERVAL) of consecutive samples
        3. Form a list of data points where each data point has TRAJECOTRY_TIME_INTERVAL sample tokens where
            each sample token has data for all instance tokens identified in step 2
        4. Create 3 numy arrays each for coordinates, heading_change_rate and map with appropriate shapes
        5. Iterate: per sample per instance and fill in numpy arrays with respective data
        6. Form a dict containing the 3 numpyarrays and return it
        '''

        ordered_tokens = OrderedDict(zip(sample_tokens, instance_tokens))

        print("Printing Ordered_tokens: ", ordered_tokens)
        return []

        #Dictionary containing count for number of samples per token
        token_count = Counter(instance_tokens)

        #used to find n agents with highest number of sample_tokens
        minCount = sorted(list(token_count.values()), reverse=True)[NUM_AGENTS-1]

        #Convert isntance and sample tokens to dict format
        instance_sample_tokens = {}
        for instance_token, sample_token in zip(instance_tokens, sample_tokens):
            if token_count[instance_token] >= minCount:
                try:
                    instance_sample_tokens[instance_token].append(sample_token)
                except:
                    instance_sample_tokens[instance_token] = [sample_token]

#         print("Instance:samples ===============================================================================")
#         print(instance_sample_tokens)

        if len(list(instance_sample_tokens.keys())) != NUM_AGENTS:
            print()
#             print("Instance_sample_tokens: \n", instance_sample_tokens)
        '''
        Format:
        {coordinates: [[coord_at_t0, coord_at_t1, coord_at_t2, ..., coord_at_tTAJECTORY_TIME_INTERVAL],...numDatapointsInScene ],
         heading_change_rate; [[h_at_t0, h_at_t1, h_at_t2, ..., h_at_tTAJECTORY_TIME_INTERVAL], ...numDatapointaInScene]
        }
        '''

        #Initialize map rasterizers
        static_layer_rasterizer = StaticLayerRasterizer(self.helper)
        agent_rasterizer = AgentBoxesWithFadedHistory(self.helper, seconds_of_history=2.5)
        mtp_input_representation = InputRepresentation(static_layer_rasterizer, agent_rasterizer, Rasterizer())

        #Initialize Output data
        output_data = {"coordinates": np.zeros((len(instance_sample_tokens.keys()), 1)), "heading_change_rate": np.zeros((len(instance_sample_tokens.keys()), 1)), "map":[0]*len(instance_sample_tokens.keys())}

        for t,instance_token in enumerate(instance_sample_tokens.keys()):

            instance_coordinates = np.zeros((int(len(instance_sample_tokens[instance_token]) / TRAJECTORY_TIME_INTERVAL),TRAJECTORY_TIME_INTERVAL,3))
            instance_heading_change_rate = np.zeros((int(len(instance_sample_tokens[instance_token]) / TRAJECTORY_TIME_INTERVAL),TRAJECTORY_TIME_INTERVAL))

            print("Shape of instance_coordinates: ", instance_coordinates.shape)
            idx = 0                       #0 --> numData points for this instance (dimension 1)
            num = 0                       #0 --> TRAJECTORY_TIME_INTERVAL (dimension 2)
            for sample_token in (instance_sample_tokens[instance_token]):
#                 print(idx, "     ", num)
#                 print(self.nusc.get('sample', sample_token)["timestamp"])

                #how to get the annotation for the instance in the sample
                annotation = self.helper.get_sample_annotation(instance_token, sample_token)
                instance_coordinates[idx][num] = annotation["translation"]

                #get the heading change rate of the agent
                heading_change_rate = self.helper.get_heading_change_rate_for_agent(instance_token, sample_token)
                instance_heading_change_rate[idx][num] = heading_change_rate

                num = num + 1

                #reached the number of records per sample
                if num == TRAJECTORY_TIME_INTERVAL:
                    idx = idx+1
                    num = 0

                if idx == instance_coordinates.shape[0]:
                    break

                img = mtp_input_representation.make_input_representation(instance_token, sample_token)
#                 cv2.imshow("map",img)

            output_data["map"][t]=(img)
#             plt.imsave('test'+str(test_idx)+str(t)+'.jpg',img)
            output_data["coordinates"][t]=instance_coordinates
            output_data["heading_change_rate"][t]=instance_heading_change_rate

#         test = pd.DataFrame(output_data,columns=["coordinates", "heading_change_rate", "map"])
#         test.to_csv('test'+str(test_idx)+'.csv')

        print("Printing Output data")
        print((output_data["coordinates"]))
        print(len(output_data["heading_change_rate"]))
        print(len(output_data["coordinates"]))

        return output_data