# DVS image reconstruction demo
#
# Given DVS events and camera poses (rotational motion), reconstruct the
# gradient map that caused the events.
#
# Celine Nauer, Institute of Neuroinformatics, University of Zurich
# Joel Bachmann, ETH
# Nik Dennler, Institute of Neuroinformatics, University of Zurich
#
# Original MATLAB code by
#  Guillermo Gallego
#  Robotics and Perception Group
#  University of Zurich

import os
import time
import math

from scipy import io
import numpy as np
import pandas as pd
import matplotlib

matplotlib.use("TkAgg")
import matplotlib.pyplot as plt
import pyquaternion
import pylab

# Provide function handles to convert from rotation matrix to axis-angle and vice-versa
import sample.helpers.integration_methods as integration_methods
import sample.helpers.coordinate_transforms as coordinate_transforms
import sample.helpers.helpers as helpers

## Run settings:
num_events_batch = 3000
num_events_display = 600000
scale_res = 1  # Use Zweierpotenz
plot_events_animation = False  # True
plot_events_pm_animation = False  # True

# Methods used:
# 1) Select measurement function used for brightness gradient estimation
#    via Extended Kalman Filter (EKF). Options are:
#    'contrast'    : Constrast criterion (Gallego et al. arXiv 2015)
#    'event_rate'  : Event rate criterion (Kim et al. BMVC 2014)
measurement_criterion = "contrast"

## ___Dataset___

time_0 = time.time()
# Data directories
data_dir = "../data/Datasets/FPU/Regal_z"
data_dir = "../data/Datasets/RedRoom/second"
# data_dir = '../data/Datasets/BigRoom/2019-04-29-17-20-59'
calibration_dir = "../data/calibration"
output_dir = "../output/ourdataset"
images_dir = os.path.join(
    output_dir, "{0}pbatch_{1}".format(num_events_batch, measurement_criterion)
)
print(images_dir)
if not os.path.exists(images_dir):
    os.makedirs(images_dir)

# Calibration data
dvs_calibration = io.loadmat(
    os.path.join(calibration_dir, "DVS_synth_undistorted_pixels_rroom.mat")
)
# Dictionary with entries ['__header__', '__version__', '__globals__',
#                          'Kint', 'dist_coeffs', 'image_height', 'image_width',
#                          'pixels_grid', 'undist_pix', 'undist_pix_calibrated',
#                          'undist_pix_mat_x', 'undist_pix_mat_y'])

dvs_parameters = {
    "sensor_height": 260,
    "sensor_width": 346,
    "contrast_threshold": 0.202,
}

## ___Main algorithmic parameters___

# Size of the output reconstructed image mosaic (panorama)
output_height = int(1024 / scale_res)
output_width = 2 * output_height

# Set the expected noise level (variance of the measurements).
if measurement_criterion == "contrast":
    var_R = 0.035 ** 2  # units[C_th] ^ 2
if measurement_criterion == "event_rate":
    var_R = 1e2 ** 2  # units[1 / second] ^ 2

# 2) Select the gradient integration method. Options are:
#    'poisson_dirichlet'   : Requires the MATLAB PDE toolbox
#    'poisson_neumann'     : Requires the MATLAB Image Processing toolbox
#    'frankotchellappa'    : Does not require a MATLAB toolbox
integration_method = "frankotchellappa"

## Loading Events
print("Loading Events")
filename_events = os.path.join(data_dir, "events.txt")
filename_events = os.path.join(data_dir, "events_cropped.txt")

events = pd.read_csv(
    filename_events, delimiter=" ", header=None, names=["time", "x", "y", "pol"]
)
print(events.head())

# print("Head: \n", events.head(10))
num_events = events.size
print("Number of events in file: ", num_events)
first_event = events.loc[0, "time"]
print(first_event)
events["t"] = events["time"] - first_event
events = events[["t", "x", "y", "pol"]]
print(events.head())

# print("Head: \n", events.head(10))
# print("Tail: \n", events.tail(10))

##Loading Camera poses
# print("Loading Camera Orientations")
filename_poses = os.path.join(data_dir, "imu.txt")
# filename_poses = os.path.join(data_dir, 'poses_orb_euler.txt')

poses = helpers.load_poses_angvel(
    filename_poses=filename_poses, includes_translations=True, t_first_event=first_event
)
# print(poses.head())
# exit()

# poses = pd.read_csv(filename_events, delimiter=' ', header=None, names=['time', 'x', 'y', 'qx', 'qy', 'qz', 'qw'])
num_poses = poses.size
print("Number of poses in file: ", num_poses)

# print(first_event)
# poses['t'] = poses['time'] - poses['time'].loc[0]
# poses = poses[['t', 'qw', 'qx', 'qy', 'qz']] # Quaternions
print("Head: \n", poses.head(10))
print("Tail: \n", poses.tail(10))

# exit()
# Convert quaternions to rotation matrices and save in a dictionary TODO: UGLY AS HELL!!
rotmats_dict = coordinate_transforms.angvel2R_dict(
    poses
)  # TODO: Not tested, but seems reasonable
# print(rotmats_dict)
# exit()

## Image reconstruction using pixel-wise EKF
# Input: events, contrast threshold and camera orientation (discrete poses + interpolation)
# Output: reconstructed intensity image
# profile('on','-detail','builtin','-timer','performance')
starttime = time.time()

# num_events_batch = 300
# num_events_display = 100000
num_batches_display = math.floor(num_events_display / num_events_batch)

# Variables related to the reconstructed image mosaic. EKF initialization
# Gradient map
grad_map = {}
grad_map["x"] = np.zeros((output_height, output_width))
grad_map["y"] = np.zeros((output_height, output_width))
# % Covariance matrix of each gradient pixel
grad_initial_variance = 50
grad_map_covar = {}
grad_map_covar["xx"] = np.ones((output_height, output_width)) * grad_initial_variance
grad_map_covar["xy"] = np.zeros((output_height, output_width))
grad_map_covar["yx"] = np.zeros((output_height, output_width))
grad_map_covar["yy"] = np.ones((output_height, output_width)) * grad_initial_variance

# For efficiency, a structure, called event map, contains for every pixel
# the time of the last event and its rotation at that time.
s = {"sae": -1e-6, "rotation": np.zeros((3, 3))}
s["rotation"].fill(np.NaN)
event_map = np.zeros((dvs_parameters["sensor_height"], dvs_parameters["sensor_width"]))
event_map = event_map.tolist()
for i in range(dvs_parameters["sensor_height"]):
    for j in range(dvs_parameters["sensor_width"]):
        event_map[i][j] = s.copy()
event_map = np.array(event_map)
print(event_map.shape)

rotmats_1stkey = list(rotmats_dict.keys())[0]
rot0 = rotmats_dict[rotmats_1stkey]  # to later center the map around the first pose
one_vec = np.ones((num_events_batch, 1))  # ??
Id = np.eye(2)  # ??

## Processing events
print("Processing events")
# plt.figure()
# fig_show_evol = plt.figure(facecolor='w')
# units = normalized should be true
first_plot = True  # for efficient plotting

iEv = 0  # event counter
iBatch = 1  # packet-of-events counter

i = 0
# counter = -1
while True:
    # counter += 1
    # print("Here")
    # print(iEv)
    # print(num_events_batch)
    # print(num_events.loc['time'])
    # exit()
    if iEv + num_events_batch > num_events:
        print("No more events")
        break

    #% Get batch of events
    events_batch = events[iEv : iEv + num_events_batch]
    iEv = iEv + num_events_batch

    events_batch_pos = events_batch[events_batch["pol"] == 1]
    events_batch_neg = events_batch[events_batch["pol"] == 0]

    t_events_batch = events_batch["t"]
    x_events_batch = events_batch["x"].astype(int)
    y_events_batch = events_batch["y"].astype(int)
    pol_events_batch = 2 * (events_batch["pol"] - 0.5)

    ## Get the two map points correspondig to each event and update the event map (time and rotation of last event)

    # Get time of previous event at same DVS pixel
    idx_to_mat = x_events_batch * dvs_parameters["sensor_height"] + y_events_batch
    # print(idx_to_mat)

    t_prev_batch = np.array(
        [
            event_map[int(y), int(x)]["sae"]
            for x, y in zip(x_events_batch, y_events_batch)
        ]
    ).T

    # Get (interpolated) rotation of current event
    first_idx = t_events_batch.index[0]
    last_idx = t_events_batch.index[-1]
    t_ev_mean = (t_events_batch.loc[first_idx] + t_events_batch.loc[last_idx]) * 0.5
    if t_ev_mean > poses["t"].iloc[-1]:
        print("Event later than last known pose")
        break  # event later than last known pose

    Rot = coordinate_transforms.rotation_interpolation(
        poses["t"], rotmats_dict, t_ev_mean
    )

    try:
        bearing_vec = np.vstack(
            (
                dvs_calibration["undist_pix_calibrated"][idx_to_mat, :].T[0],
                dvs_calibration["undist_pix_calibrated"][idx_to_mat, :].T[1],
                one_vec[:, 0],
            )
        )  # 3xN
    except ValueError:
        print(dvs_calibration["undist_pix_calibrated"][idx_to_mat, :].T[0].shape)
        print(dvs_calibration["undist_pix_calibrated"][idx_to_mat, :].T[1].shape)
        print(one_vec[:, 0].shape)
        print("Event  # {}".format(iEv))
        break

    # Get map point corresponding to current event
    # print(Rot)
    # print(rot0)
    # print(bearing_vec)
    # exit()
    rotated_vec = rot0.T.dot(Rot).dot(bearing_vec)
    pm = coordinate_transforms.project_equirectangular_projection(
        rotated_vec, output_width, output_height
    )

    #  Get map point corresponding to previous event at same pixel
    rotated_vec_prev = np.zeros(rotated_vec.shape)
    for ii in range(num_events_batch):
        Rot_prev = event_map[y_events_batch.iloc[ii]][x_events_batch.iloc[ii]][
            "rotation"
        ].copy()
        rotated_vec_prev[:, ii] = rot0.T.dot(Rot_prev).dot(bearing_vec[:, ii])
        # Update last rotation and time of event(SAE)
        event_map[y_events_batch.iloc[ii]][x_events_batch.iloc[ii]][
            "sae"
        ] = t_events_batch.iloc[ii]
        event_map[y_events_batch.iloc[ii]][x_events_batch.iloc[ii]]["rotation"] = Rot

    pm_prev = coordinate_transforms.project_equirectangular_projection(
        rotated_vec_prev, output_width, output_height
    )

    if (t_prev_batch[-1] < 0) or (t_prev_batch[-1] < poses["t"][0]):
        continue  # initialization phase. Fill in event_map

    #  Discard nan values
    mask_uninitialized = np.isnan(pm_prev[0, :]) | np.isnan(pm_prev[1, :])
    num_uninitialized = sum(mask_uninitialized)

    if True:  # (num_uninitialized > 0):
        # % Delete uninitialized events
        # print('Deleting {} points'.format(str(num_uninitialized)))
        t_events_batch = np.array(t_events_batch.iloc[~mask_uninitialized].tolist())
        t_prev_batch = np.array(t_prev_batch[~mask_uninitialized].tolist())

        pol_events_batch = np.array(pol_events_batch[~mask_uninitialized].tolist())
        pm_ = pm.copy()
        pm_prev_ = pm_prev.copy()
        pm = []
        pm_prev = []
        for row in pm_:
            pm.append(row[~mask_uninitialized].tolist())
        for row in pm_prev_:
            pm_prev.append(row[~mask_uninitialized].tolist())
        pm = np.array(pm)
        pm_prev = np.array(pm_prev)

    # Get time since previous event at same pixel
    tc = t_events_batch - t_prev_batch
    event_rate = 1.0 / (tc + 1e-12)  #% measurement or observation(z)

    # Get velocity vector
    vel = (
        pm - pm_prev
    ) * event_rate  # TODO: Check for a point after the first to evaluate

    ## Extended Kalman Filter (EKF) for the intensity gradient map.
    # Get gradient and covariance at current map points pm
    ir = np.floor(pm[1, :]).astype(int)  # row is y coordinate
    ic = np.floor(pm[0, :]).astype(int)  # col is x coordinate

    gm = np.array([[grad_map["x"][i][j], grad_map["y"][i][j]] for i, j in zip(ir, ic)])
    Pg = np.array(
        [
            [
                grad_map_covar["xx"][i][j],
                grad_map_covar["xy"][i][j],
                grad_map_covar["yx"][i][j],
                grad_map_covar["yy"][i][j],
            ]
            for i, j in zip(ir, ic)
        ]
    )

    # EKF update
    if measurement_criterion == "contrast":
        # Use contrast as measurement function
        dhdg = (
            vel.T * np.array([tc * pol_events_batch, tc * pol_events_batch]).T
        )  # derivative of measurement function

        nu_innovation = dvs_parameters["contrast_threshold"] - np.sum(dhdg * gm, axis=1)
    else:
        # Use the event rate as measurement function
        dhdg = (
            vel.T
            / np.array(
                [
                    dvs_parameters["contrast_threshold"] * pol_events_batch,
                    dvs_parameters["contrast_threshold"] * pol_events_batch,
                ]
            ).T
        )  # deriv. of measurement function
        nu_innovation = event_rate - np.sum(dhdg * gm, axis=1)

    Pg_dhdg = np.array(
        [
            Pg[:, 0] * dhdg[:, 0] + Pg[:, 1] * dhdg[:, 1],
            Pg[:, 2] * dhdg[:, 0] + Pg[:, 3] * dhdg[:, 1],
        ]
    ).T

    S_covar_innovation = dhdg[:, 0] * Pg_dhdg[:, 0] + dhdg[:, 1] * Pg_dhdg[:, 1] + var_R
    Kalman_gain = Pg_dhdg / np.array([S_covar_innovation, S_covar_innovation]).T
    # Update gradient and covariance
    gm = gm + Kalman_gain * np.array([nu_innovation, nu_innovation]).T
    Pg = (
        Pg
        - np.array(
            [
                Pg_dhdg[:, 0] * Kalman_gain[:, 0],
                Pg_dhdg[:, 0] * Kalman_gain[:, 1],
                Pg_dhdg[:, 1] * Kalman_gain[:, 0],
                Pg_dhdg[:, 1] * Kalman_gain[:, 1],
            ]
        ).T
    )

    k = 0
    for i, j in zip(ir, ic):
        grad_map["x"][i][j] = gm[k, 0]
        grad_map["y"][i][j] = gm[k, 1]
        grad_map_covar["xx"][i][j] = Pg[k, 0]
        grad_map_covar["xy"][i][j] = Pg[k, 1]
        grad_map_covar["yx"][i][j] = Pg[k, 2]
        grad_map_covar["yy"][i][j] = Pg[k, 3]
        k += 1

    iBatch = iBatch + 1

    if plot_events_animation or plot_events_pm_animation:
        print("Update display: Event # {}".format(iEv))
        idx_pos = pol_events_batch > 0
        idx_neg = pol_events_batch < 0

        trace_map = grad_map_covar["xx"] + grad_map_covar["yy"]
        grad_map_clip = {}
        grad_map_clip["x"] = grad_map["x"]
        grad_map_clip["y"] = grad_map["y"]
        mask = trace_map > 0.05  # % reconstruct only gradients with small covariance
        grad_map_clip["x"][mask] = 0
        grad_map_clip["x"][mask] = 0

        rec_image = integration_methods.frankotchellappa(
            grad_map_clip["x"], grad_map_clip["y"]
        )
        rec_image = rec_image - np.mean(rec_image)

        if first_plot:
            first_plot = False

            if plot_events_animation:
                # Plot events on sensor image
                fig_events_raw = plt.figure(0)
                ax_events_raw = fig_events_raw.add_subplot(111)
                events_p, = ax_events_raw.plot(
                    events_batch_pos["x"], events_batch_pos["y"], ",b"
                )
                events_n, = ax_events_raw.plot(
                    events_batch_neg["x"], events_batch_neg["y"], ",r"
                )
                plt.xlim([0, dvs_parameters["sensor_width"]])
                plt.ylim([0, dvs_parameters["sensor_height"]])
                plt.title("Events on Sensor")

                plt.ion()
                plt.show()

            if plot_events_pm_animation:
                # Plot points on panoramic image, colored according to polarity
                fig_events = plt.figure(1)
                ax_events = fig_events.add_subplot(111)
                h_map_pts_p, = ax_events.plot(pm[0, idx_pos], pm[1, idx_pos], ",b")
                h_map_pts_n, = ax_events.plot(pm[0, idx_neg], pm[1, idx_neg], ",r")
                plt.xlim([0, output_width])
                plt.ylim([0, output_height])
                plt.title("Map points from events")

                plt.ion()
                plt.show()

            # Display reconstructed image
            # fig_reconstructed = plt.figure(2)
            # ax_reconstructed = fig_reconstructed.add_subplot(111)
            # maximum = np.max(np.abs(rec_image))
            # minimum = np.min(np.abs(rec_image))
            # h_img = plt.imshow(rec_image / maximum, cmap=plt.cm.binary, vmin=-1, vmax=1)

            # Display one of the gradient images
            # fig_gradient = plt.figure(3)
            # ax_gradient = fig_gradient.add_subplot(111)
            # h_gx = plt.imshow(grad_map['x'] / np.std(grad_map['x']), cmap=plt.cm.binary, vmin=-5, vmax=5)
            # # plt.xlim([0, output_width])
            # # plt.ylim([0, output_height])
            # plt.xlim([0, output_width])
            # plt.ylim([0, output_height])
            # plt.ion()
            # plt.show()

            # print("MAX: ", np.max([np.abs(np.max(rec_image[0])), np.abs(np.min(rec_image[0]))]))
            # print(np.max(np.abs(rec_image[0])))

        else:
            if plot_events_animation:
                events_p.set_data(events_batch_pos["x"], events_batch_pos["y"])
                events_n.set_data(events_batch_neg["x"], events_batch_neg["y"])
                plt.savefig(
                    output_dir
                    + "/animation/events_raw/"
                    + "fig_events_{}.png".format(str(iEv).zfill(10))
                )

            if plot_events_pm_animation:
                h_map_pts_p.set_data(pm[0, idx_pos], pm[1, idx_pos])
                h_map_pts_n.set_data(pm[0, idx_neg], pm[1, idx_neg])
                plt.savefig(
                    output_dir
                    + "/animation/events_pm/"
                    + "fig_events_{}.png".format(str(iEv).zfill(10))
                )

            # maximum = np.max(np.abs(rec_image))
            # minimum = np.min(np.abs(rec_image))
            # h_img.set_data(rec_image / maximum)

            # h_gx.set_data(grad_map['x'] / np.std(grad_map['x']))
            # ax_events.relim()

endtime = time.time()
print("Done")
print("Elapsed time: {} seconds".format(endtime - time_0))

# Display in separate figure
print("Total summed Events # {}".format(iEv))
idx_pos = pol_events_batch > 0
idx_neg = pol_events_batch < 0

trace_map = grad_map_covar["xx"] + grad_map_covar["yy"]

import pickle

pickle_out = open("grad_map.pickle", "wb")
pickle.dump(grad_map, pickle_out)
pickle_out.close()

pickle_out = open("trace_map.pickle", "wb")
pickle.dump(trace_map, pickle_out)
pickle_out.close()

grad_map_clip = {}
grad_map_clip["x"] = grad_map["x"]
grad_map_clip["y"] = grad_map["y"]
mask = trace_map > 0.05  # % reconstruct only gradients with small covariance

grad_map_clip["x"][mask] = 0
grad_map_clip["y"][mask] = 0

# grad_map_clip.to_csv('grad_map_clip.csv')
#
# grad_map_clip['x'] = grad_map_clip['x'].loc[1750:2000]
# grad_map_clip['y'] = grad_map_clip['y'].loc[400:700]

rec_image = integration_methods.frankotchellappa(grad_map_clip["x"], grad_map_clip["y"])
rec_image = rec_image - np.mean(rec_image)

rec_image_normalized = rec_image / np.max(np.abs(rec_image))
fig_normalized = plt.figure(1)
plt.imshow(rec_image_normalized, cmap=plt.cm.binary)
plt.title("Reconstructed image (log)")
plt.savefig(os.path.join(images_dir, "reconstructed_log.pdf"), dpi=350)
plt.show()
#
rec_image_exp = np.exp(0.001 + rec_image)
fig_normalized_linear = plt.figure(2)
plt.imshow(rec_image_exp, cmap=plt.cm.binary)
plt.title("Reconstructed image (linear)")
plt.savefig(os.path.join(images_dir, "reconstructed_linear.pdf"), dpi=350)
plt.show()

fig_gradientx = plt.figure(3)
h_gx = plt.imshow(
    grad_map["x"] / np.std(grad_map["x"]), cmap=plt.cm.binary, vmin=-5, vmax=5
)
plt.title("Gradient in X")
plt.savefig(os.path.join(images_dir, "gradient_x.pdf"), dpi=350)
plt.show()

fig_gradienty = plt.figure(4)
h_gx = plt.imshow(
    grad_map["y"] / np.std(grad_map["y"]), cmap=plt.cm.binary, vmin=-5, vmax=5
)
plt.title("Gradient in Y")
plt.savefig(os.path.join(images_dir, "gradient_y.pdf"), dpi=350)
plt.show()

fig_tracemap = plt.figure(5)
h_gx = plt.imshow(trace_map / np.max(trace_map), cmap=plt.cm.binary, vmin=0, vmax=1)
plt.title("Trace of Covariance")
plt.savefig(os.path.join(images_dir, "trace.pdf"), dpi=350)
plt.show()

# g_ang = -1*np.arctan2(grad_map['y'], grad_map['x'])
# g_grad = np.sqrt(np.power(grad_map['x'], 2) + np.power(grad_map['y'], 2))
# g_grad_unit = g_grad/1.
# g_grad_unit[g_grad_unit > 1.0] = 1.0
# g_ang_unit = g_ang/360. + 0.5

np.save("intensity_map.npy", rec_image)


#!/usr/bin/env python3

#Submit a nextflow pipeline script
#Submit a 1 node job that lasts 72 hours and runs the nextflow script.

import sys
sys.path.append("bin/scripts")
import funcs_cluster as FC

command_list = list()
command_list.append("ml load java")
command = " ".join(["nextflow", "run", "run_pipeline_3.nf", "-profile" , "sherlock", "-w", "/scratch/PI/hbfraser/ryo/nextflow_pe"])
command_list.append(command)
FC.submit_to_cluster("nextflow", command_list, walltime="24:00:00", submit_flag=1)


import urllib2
import codecs
import BeautifulSoup
import re
import pdb
import os

def traverse(url,component):
    page = urllib2.urlopen(url + component).read()
    os.makedirs(component)
    os.chdir(component)
    soup = BeautifulSoup.BeautifulSoup(page)
    for i in soup.findAll('a'):
        link = str(i['href'])
        if '..' in link:
            continue
        if '.' not in link or link[-1] == '/':
            traverse(url + component + '/',link)
        else:
            ifile = urllib2.urlopen(url + component + '/' + link).read()
            f = open(link,'wb')
            f.write(ifile)
            f.close()
    os.chdir('..')

traverse('http://galleray.com/','static')


# Licensed under a 3-clause BSD style license - see LICENSE.rst

"""
Power law model variants
"""

from __future__ import (absolute_import, unicode_literals, division,
                        print_function)

import numpy as np

from .core import Fittable1DModel
from .parameters import Parameter

__all__ = sorted(['PowerLaw1D', 'BrokenPowerLaw1D',
                  'ExponentialCutoffPowerLaw1D', 'LogParabola1D'])

class PowerLaw1D(Fittable1DModel):
    """
    One dimensional power law model.

    Parameters
    ----------
    amplitude : float
        Model amplitude at the reference point
    x_0 : float
        Reference point
    alpha : float
        Power law index

    See Also
    --------
    BrokenPowerLaw1D, ExponentialCutoffPowerLaw1D, LogParabola1D

    Notes
    -----
    Model formula (with :math:`A` for ``amplitude`` and :math:`\\alpha` for ``alpha``):

        .. math:: f(x) = A (x / x_0) ^ {-\\alpha}

    """

    amplitude = Parameter(default=1)
    x_0 = Parameter(default=1)
    alpha = Parameter(default=1)

    @staticmethod
    def evaluate(x, amplitude, x_0, alpha):
        """One dimensional power law model function"""

        xx = x / x_0
        return amplitude * xx ** (-alpha)

    @staticmethod
    def fit_deriv(x, amplitude, x_0, alpha):
        """One dimensional power law derivative with respect to parameters"""

        xx = x / x_0

        d_amplitude = xx ** (-alpha)
        d_x_0 = amplitude * alpha * d_amplitude / x_0
        d_alpha = -amplitude * d_amplitude * np.log(xx)

        return [d_amplitude, d_x_0, d_alpha]

class BrokenPowerLaw1D(Fittable1DModel):
    """
    One dimensional power law model with a break.

    Parameters
    ----------
    amplitude : float
        Model amplitude at the break point
    x_break : float
        Break point
    alpha_1 : float
        Power law index for x < x_break
    alpha_2 : float
        Power law index for x > x_break

    See Also
    --------
    PowerLaw1D, ExponentialCutoffPowerLaw1D, LogParabola1D

    Notes
    -----
    Model formula (with :math:`A` for ``amplitude`` and :math:`\\alpha_1`
    for ``alpha_1`` and :math:`\\alpha_2` for ``alpha_2``):

        .. math::

            f(x) = \\left \\{
                     \\begin{array}{ll}
                       A (x / x_{break}) ^ {-\\alpha_1} & : x < x_{break} \\\\
                       A (x / x_{break}) ^ {-\\alpha_2} & :  x > x_{break} \\\\
                     \\end{array}
                   \\right.
    """

    amplitude = Parameter(default=1)
    x_break = Parameter(default=1)
    alpha_1 = Parameter(default=1)
    alpha_2 = Parameter(default=1)

    @staticmethod
    def evaluate(x, amplitude, x_break, alpha_1, alpha_2):
        """One dimensional broken power law model function"""

        alpha = np.where(x < x_break, alpha_1, alpha_2)
        xx = x / x_break
        return amplitude * xx ** (-alpha)

    @staticmethod
    def fit_deriv(x, amplitude, x_break, alpha_1, alpha_2):
        """One dimensional broken power law derivative with respect to parameters"""

        alpha = np.where(x < x_break, alpha_1, alpha_2)
        xx = x / x_break

        d_amplitude = xx ** (-alpha)
        d_x_break = amplitude * alpha * d_amplitude / x_break
        d_alpha = -amplitude * d_amplitude * np.log(xx)
        d_alpha_1 = np.where(x < x_break, d_alpha, 0)
        d_alpha_2 = np.where(x >= x_break, d_alpha, 0)

        return [d_amplitude, d_x_break, d_alpha_1, d_alpha_2]

class ExponentialCutoffPowerLaw1D(Fittable1DModel):
    """
    One dimensional power law model with an exponential cutoff.

    Parameters
    ----------
    amplitude : float
        Model amplitude
    x_0 : float
        Reference point
    alpha : float
        Power law index
    x_cutoff : float
        Cutoff point

    See Also
    --------
    PowerLaw1D, BrokenPowerLaw1D, LogParabola1D

    Notes
    -----
    Model formula (with :math:`A` for ``amplitude`` and :math:`\\alpha` for ``alpha``):

        .. math:: f(x) = A (x / x_0) ^ {-\\alpha} \\exp (-x / x_{cutoff})

    """

    amplitude = Parameter(default=1)
    x_0 = Parameter(default=1)
    alpha = Parameter(default=1)
    x_cutoff = Parameter(default=1)

    @staticmethod
    def evaluate(x, amplitude, x_0, alpha, x_cutoff):
        """One dimensional exponential cutoff power law model function"""

        xx = x / x_0
        return amplitude * xx ** (-alpha) * np.exp(-x / x_cutoff)

    @staticmethod
    def fit_deriv(x, amplitude, x_0, alpha, x_cutoff):
        """One dimensional exponential cutoff power law derivative with respect to parameters"""

        xx = x / x_0
        xc = x / x_cutoff

        d_amplitude = xx ** (-alpha) * np.exp(-xc)
        d_x_0 = alpha * amplitude * d_amplitude / x_0
        d_alpha = -amplitude * d_amplitude * np.log(xx)
        d_x_cutoff = amplitude * x * d_amplitude / x_cutoff ** 2

        return [d_amplitude, d_x_0, d_alpha, d_x_cutoff]

class LogParabola1D(Fittable1DModel):
    """
    One dimensional log parabola model (sometimes called curved power law).

    Parameters
    ----------
    amplitude : float
        Model amplitude
    x_0 : float
        Reference point
    alpha : float
        Power law index
    beta : float
        Power law curvature

    See Also
    --------
    PowerLaw1D, BrokenPowerLaw1D, ExponentialCutoffPowerLaw1D

    Notes
    -----
    Model formula (with :math:`A` for ``amplitude`` and :math:`\\alpha` for ``alpha`` and :math:`\\beta` for ``beta``):

        .. math:: f(x) = A \\left(\\frac{x}{x_{0}}\\right)^{- \\alpha - \\beta \\log{\\left (\\frac{x}{x_{0}} \\right )}}

    """

    amplitude = Parameter(default=1)
    x_0 = Parameter(default=1)
    alpha = Parameter(default=1)
    beta = Parameter(default=0)

    @staticmethod
    def evaluate(x, amplitude, x_0, alpha, beta):
        """One dimensional log parabola model function"""

        xx = x / x_0
        exponent = -alpha - beta * np.log(xx)
        return amplitude * xx ** exponent

    @staticmethod
    def fit_deriv(x, amplitude, x_0, alpha, beta):
        """One dimensional log parabola derivative with respect to parameters"""

        xx = x / x_0
        log_xx = np.log(xx)
        exponent = -alpha - beta * log_xx

        d_amplitude = xx ** exponent
        d_beta = -amplitude * d_amplitude * log_xx ** 2
        d_x_0 = amplitude * d_amplitude * (beta * log_xx / x_0 - exponent / x_0)
        d_alpha = -amplitude * d_amplitude * log_xx
        return [d_amplitude, d_x_0, d_alpha, d_beta]


from icrawler.builtin import GoogleImageCrawler
import argparse

def arg_parse():
    parser = argparse.ArgumentParser(description='CNN implemented with Keras')
    parser.add_argument('--key', dest='key', type=str)
    parser.add_argument('--dir', dest='dir', type=str)
    parser.add_argument("--N", dest="N", type=int)
    args = parser.parse_args()
    return args

args = arg_parse()

crawler = GoogleImageCrawler(storage={"root_dir": args.dir})
crawler.crawl(keyword=args.key, max_num=args.N)


################################################################################
##                      Nodes Constellation Animation                         ##
##                                                                            ##
##              Programmed by Ryun Valdez                                     ##
##              Class: CS301                                                  ##
##              Instructor: Dean Zeller                                       ##
##                                                                            ##
##              Description: This is an animation of a constellation          ##
##                                                                            ##
################################################################################

from tkinter import *
c = Canvas(width=600, height=750, bg='black')
c.pack(expand=YES, fill=BOTH)
import random

##Functions & Classes##
#
class Star:
    def __init__(self, size, x, y):
        self.size = size
        self.x = x
        self.y = y
    def insert(self):
        c.create_oval(self.x-self.size/2,self.y-self.size/2, self.x+self.size-self.size/2,
                    self.y+self.size-self.size/2,fill="white",outline="white",
                      tag="star")
    def connect(self, s2):
        for i in range(1,61):
            c.after(8)
            c.create_line(self.x,self.y, self.x-(i*((self.x-s2.x)/60)),
                          self.y-(i*((self.y-s2.y)/60)), fill="white", tag="line")
            c.update()
#
def insertBg(size, x, y):
    c.create_oval(x-size/2,y-size/2, x+size-size/2,y+size-size/2, fill="white",
                  outline="white", tag="bg")
##End Functions & Classes##

#Draw Random Background
for i in range(1,125):
    x=random.random()*600
    y=random.random()*750
    insertBg(random.random()*10, x, y)

#Create Stars (Constellation)
#Shoulders
s1 = Star(15, 150, 320)
s2 = Star(15, 350, 340)
#Belt
s3 = Star(10, 220, 540)
s4 = Star(10, 250, 530)
s5 = Star(10, 280, 520)
#Feet
s6 = Star(15, 150, 730)
s7 = Star(15, 350, 700)
#Head
s8 = Star(10, 270, 250)
#Bow
s9 = Star(10, 525, 150)
s10 = Star(10, 530, 250)
s11 = Star(10, 570, 330)
s12 = Star(10, 550, 460)
s13 = Star(10, 510, 480)
#Arm
s14 = Star(10, 160, 20)
s15 = Star(10, 90, 25)
s16 = Star(10, 30, 110)
s17 = Star(10, 30, 160)
s18 = Star(10, 90, 270)

#Insert Stars
s1.insert()
s2.insert()
s3.insert()
s4.insert()
s5.insert()
s6.insert()
s7.insert()
s8.insert()
s9.insert()
s10.insert()
s11.insert()
s12.insert()
s13.insert()
s14.insert()
s15.insert()
s16.insert()
s17.insert()
s18.insert()

c.create_rectangle(0,0, 1,1)
c.update()
c.after(5000)
c.update()

##Draw Lines##Order is rearrangeable##
s14.connect(s15)
s15.connect(s16)
s16.connect(s17)
s17.connect(s18)
s18.connect(s1)
s1.connect(s8)
s1.connect(s3)
s8.connect(s2)
s3.connect(s6)
s2.connect(s5)
s6.connect(s7)
s7.connect(s5)
s5.connect(s4)
s3.connect(s4)
s2.connect(s11)
s11.connect(s12)
s11.connect(s10)
s12.connect(s13)
s10.connect(s9)

##Highlight##
c.itemconfig("line", fill="Gold", width=2)
c.itemconfig("star", outline="Gold", width=2)
title = c.create_text(300,100,text="ORION", width=600,fill='Gold',
        font=("Helvetica",1),justify=CENTER, anchor=CENTER)
c.tag_raise("star")
c.update()

for i in range(1,48):
    c.after(50)
    c.itemconfig(title, font=("Helvetica",2+i))
    c.update()


#! /usr/bin/env python
import sys

def powI(power, base):
    result = 1;
    for x in range(0, power):
        result = result * base
    return result

def powF(power, base):
    if(power == 0):
        return 1;
    else:
        return base * powF(power - 1, base)

if len(sys.argv) != 3:
    print("%s usage: [BASE] [POWER]" % sys.argv[0])
    exit()

print(powI(int(sys.argv[2]), int(sys.argv[1])))
print(powF(int(sys.argv[2]), int(sys.argv[1])))


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

import xlwt
from xlwt import Font
from xlwt import XFStyle

class XlsExporter(object):

    @staticmethod
    def export(match_list, file_path):
        wb = xlwt.Workbook()
        ws = wb.add_sheet('Ranking')

        # writing header
        header = ['Ranking',
                  'Name',
                  'Rating',
                  'Reviews',
                  'Price',
                  'Cuisines',
                  'Edenred Name',
                  'Match',
                  'Edenred Address']
        font = Font()
        font.name = 'Arial'
        font.bold = True
        style = XFStyle()
        style.font = font
        for index in range(len(header)):
            ws.write(0, index, header[index], style)

        # writing restaurants
        for index in range(len(match_list)):
            match = match_list[index]
            ws.write(index+1, 0, int(match.restaurant1.ranking))
            ws.write(index+1, 1, xlwt.Formula('HYPERLINK("%s"; "%s")' % (match.restaurant1.url, match.restaurant1.name)))
            ws.write(index+1, 2, float(match.restaurant1.rating.replace(',', '.')))
            ws.write(index+1, 3, int(match.restaurant1.review_count))
            ws.write(index+1, 4, match.restaurant1.price)
            ws.write(index+1, 5, ', '.join(match.restaurant1.cuisines))
            ws.write(index+1, 6, match.restaurant2.name)
            ws.write(index+1, 7, int(100.0 * match.score))
            address = match.restaurant2.address + ', ' + match.restaurant2.postcode
            map_link = 'https://www.google.com/maps?f=q&source=s_q&hl=es&q=%s' % address
            ws.write(index+1, 8, xlwt.Formula('HYPERLINK("%s"; "%s")' % (map_link, address)))
        wb.save(file_path)


import torch.nn as nn

class LockedDropout(nn.Module):
    def __init__(self):
        super(LockedDropout, self).__init__()

    def forward(self, inputs, dropout=0.5):
        if not self.training or not dropout:
            return inputs
        m = inputs.data.new(1, inputs.size(1), inputs.size(2)).bernoulli_(1 - dropout)
        mask = m.div_(1 - dropout)
        mask = mask.expand_as(inputs)
        return mask * inputs


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
import uuid
from django.conf import settings

class Migration(migrations.Migration):

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ('Site', '0002_auto_20150801_1851'),
    ]

    operations = [
        migrations.CreateModel(
            name='TokenLogin',
            fields=[
                ('id_token', models.UUIDField(serialize=False, default=uuid.uuid4, primary_key=True, editable=False)),
                ('data_login', models.DateTimeField(auto_now_add=True)),
                ('fk_usuario', models.ForeignKey(to=settings.AUTH_USER_MODEL)),
            ],
        ),
    ]


import cv2
import datetime
cap = cv2.VideoCapture(0)
print("Original Height",cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
print("Original Width",cap.get(cv2.CAP_PROP_FRAME_WIDTH))

cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 240.0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 320.0)

print("\nAfter modified HIGHT and WIGHT")
print(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
print(cap.get(cv2.CAP_PROP_FRAME_WIDTH))

while (cap.isOpened()):
    _, frame = cap.read()
    frame_flip = cv2.flip(frame, 1)
##    color = cv2.cvtColor(frame, cv2.IMREAD_COLOR )
    font = cv2.FONT_HERSHEY_COMPLEX_SMALL
    frame = cv2.putText(frame_flip,str(datetime.datetime.now()),(0,20),font,.5, (161,174,179),1,cv2.LINE_AA)
    cv2.imshow('Videos', frame)
    if cv2.waitKey(1) == ord('q'):
        break

cap.release() # None
cv2.destroyAllWindows() # destroy windows.


import math
import itertools

def digits(t, b): #assumes t < b
	mantissa = b
	while True:
		mantissa /= b
		digit = math.floor(t/mantissa)
		t -= digit * mantissa
		yield digit

template = "\draw[thick, {3}, {{[-)}}] ({0}, {2}) -- ({1}, {2});"
def pt(ints, y, colorfun):
    for ((a, b), d) in ints:
        print(template.format(a*10,b*10,y, colorfun(d)))

def intervals(t, b, depth):
    digs = list(itertools.islice(digits(t,b), depth))
    digs[-1] += 1
    x = 0
    for i, d in enumerate(digs):
        for k in range(d):
            yield ((x, x+b**(-i)), i)
            x += b**(-i)


#!/usr/bin/env python

"""Update parameters which directly depends on the dataset.
"""

__author__ = 'Anna Kukleva'
__date__ = 'November 2018'

import os

from utils.arg_pars import opt
from utils.util_functions import update_opt_str
from utils.logging_setup import path_logger

def update():
    opt.dataset_root = '/media/data/kukleva/lab/Breakfast'

    data_subfolder = ['kinetics', 'data', 's1', 'video', 'OPN', 'videovector', 'videovector', 'videodarwin'][opt.data_type]
    opt.data = os.path.join(opt.dataset_root, 'feat', data_subfolder)

    opt.gt = os.path.join(opt.data, opt.gt)

    opt.ext = ['gz', 'gz', 'txt', 'avi', 'txt', 'txt', 'txt', 'txt'][opt.data_type]
    opt.feature_dim = [400, 64, 64, 0, 1024, 4096, 64, 128][opt.data_type]
    opt.subfolder = 'ascii'

    if opt.data_type in [5, 6]:
        # ascii_subsampled ascii_imgnet ascii_target ascii_context
        i = 0 if opt.data_type == 5 else 1
        opt.subfolder = ['ascii_%s', 'ascii_subsampled'][i]
        if i == 0:
            data_type = ['target', 'context'][0]
            resume_iter = [25, 50][1]
            opt.subfolder = opt.subfolder % data_type
            opt.prefix = opt.prefix + '.%s.%d' % (data_type, resume_iter)
        opt.frame_frequency = 5

    if opt.data_type in [4, 5, 6, 7]:
        exception_list = ['P03_webcam02_P03_tea',
                          'P26_webcam02_P26_cereals',
                          'P27_webcam02_P27_coffee',
                          'P34_cam01_P34_coffee',
                          'P51_webcam01_P51_coffee',
                          'P52_stereo01_P52_sandwich']

        for root, dirs, files in os.walk(os.path.join(opt.data, opt.subfolder)):
            if files:
                for filename in files:
                    if 'P51_webcam01_P51_coffee' in filename:
                        print('P51_webcam01_P51_coffee')
                    for exception in exception_list:
                        if exception in filename:
                            try:
                                os.remove(os.path.join(root, filename))
                                os.remove(os.path.join(opt.gt, filename))
                            except FileNotFoundError:
                                pass

    if opt.data_type == 6:
        opt.model_name = 'mlp'

    if opt.data_type in [0, 4, 5, 7]:
        opt.model_name = 'nothing'

    opt.bg = False
    opt.gr_lev = ''

    if opt.all:
        opt.subaction = 'all'

    update_opt_str()

    logger = path_logger()

    vars_iter = list(vars(opt))
    for arg in sorted(vars_iter):
        logger.debug('%s: %s' % (arg, getattr(opt, arg)))


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

def isPrime(x):
    if x == 2:
        return True

    if x % 2 == 0:
        return False

    for i in range(3, x):
        if x % i == 0:
            return False

    return True

def get_prime(max_value):
    result = []
    for i in range(2, max_value + 1):
        if isPrime(i):
            result.append(i)

    return result

if __name__ == '__main__':
    n = int( input() )

    array = []
    for i in range(n):
        array.append( int(input()) )

    max_value = max(array)

    prime_list = get_prime(max_value)

    print( len(prime_list) )


from tkinter import *

class Test(Frame):
    def __init__(self, window):
        Frame.__init__(self, window, bg='red')

        window.rowconfigure(0, weight=1)
        window.columnconfigure(0, weight=1)

        self.rowconfigure(0, weight=1)
        self.columnconfigure(0, weight=1)
        self.grid(row=0, column=0, sticky='news')

        self.message_textmode = Message(self, text="Coucou", width=100, justify='left',
                                        highlightcolor='blue')
        self.message_textmode.grid(row=0, column=0, padx=20, pady=20, sticky='news')

root = Tk()
test = Test(root)
test.mainloop()


import sys
import ctypes.util

if sys.version_info[0] < 3:
    string_type = unicode
    byte_type = str
else:
    string_type = str
    byte_type = byte

libpath = ctypes.util.find_library('houdini')
if not libpath:
    raise ImportError("Couldn't find libhoudini with ctypes.util.find_library")
_libhoudini = ctypes.CDLL(libpath)

table_ptr = ctypes.POINTER(ctypes.c_char)

class _GHBuffer(ctypes.Structure):
    _fields_ = [
        ('ptr', ctypes.c_char_p),
        ('asize', ctypes.c_size_t),
        ('size', ctypes.c_size_t),
    ]

    def __init__(self, initial_size=0):
        _libhoudini.gh_buf_init(ctypes.byref(self),
                                ctypes.c_size_t(initial_size))

    def __str__(self):
        return self.ptr[:self.size]

def escaper(etype):
    gh_pointer = ctypes.POINTER(_GHBuffer)
    escaper_func = getattr(_libhoudini, 'houdini_' + etype)
    escaper_func.argtypes = [gh_pointer, ctypes.c_char_p, ctypes.c_size_t]

    def escape(string):
        is_string = isinstance(string, string_type)
        encoded = string.encode('utf-8') if is_string else string
        src = ctypes.c_char_p(encoded)
        size = ctypes.c_size_t(len(encoded))
        output_buffer = _GHBuffer()
        res = escaper_func(ctypes.byref(output_buffer), src, size)
        if not res:
            return string
        output_bytes = byte_type(output_buffer)
        return output_bytes.decode('utf-8') if is_string else output_bytes

    escape.__name__ = etype
    return escape

def table_escaper(etype):
    gh_pointer = ctypes.POINTER(_GHBuffer)
    escaper_func = getattr(_libhoudini, 'houdini_' + etype)
    escaper_func.argtypes = [gh_pointer, ctypes.c_char_p, ctypes.c_size_t]

    def escape(string, safe_table):
        is_string = isinstance(string, string_type)
        encoded = string.encode('utf-8') if is_string else string
        src = ctypes.c_char_p(encoded)
        size = ctypes.c_size_t(len(encoded))
        output_buffer = _GHBuffer()
        res = escaper_func(ctypes.byref(output_buffer), src, size, safe_table)
        if not res:
            return string
        output_bytes = byte_type(output_buffer)
        return output_bytes.decode('utf-8') if is_string else output_bytes

    escape.__name__ = etype
    return escape

def table_builder(etype):
    builder_func = getattr(_libhoudini, 'houdini_' + etype)
    builder_func.argtypes = [ctypes.c_char_p, ctypes.c_size_t]
    builder_func.restype = table_ptr

    def build_table(safe):
        is_string = isinstance(safe, string_type)
        encoded = safe.encode('utf-8') if is_string else safe
        src = ctypes.c_char_p(encoded)
        size = ctypes.c_size_t(len(encoded))
        res = builder_func(src, size)
        if not res:
            raise RuntimeError('failed to create safe character table')
        return res

    build_table.__name__ = etype
    return build_table

escape_html = escaper('escape_html')
unescape_html = escaper('unescape_html')
escape_xml = escaper('escape_xml')
escape_uri = escaper('escape_uri')
escape_url = escaper('escape_url')
escape_href = escaper('escape_href')
unescape_uri = escaper('unescape_uri')
unescape_url = escaper('unescape_url')
escape_js = escaper('escape_js')
unescape_js = escaper('unescape_js')
uri_safe_table = table_builder('uri_safe_table')
url_safe_table = table_builder('url_safe_table')
escape_uri_safe_table = table_escaper('escape_uri_safe_table')
escape_url_safe_table = table_escaper('escape_url_safe_table')


from modules.base_transform import BaseTransform
import modules.util as mu

import pandas as pd
import numpy as np
import os
import math
import sys
from factor_analyzer import FactorAnalyzer

class LBTransform(BaseTransform):
    """
    地方競馬に関するデータ変換処理をまとめたクラス
    """

    def __init__(self, start_date, end_date):
        """ LocalBaozExtractオブジェクトを生成してデータを呼び出す準備をする

        :param str start_date:
        :param str end_date:
        """
        self.start_date = start_date
        self.end_date = end_date

    def factory_analyze_raceuma_result_df(self, race_df, input_raceuma_df, dict_folder):
        """ RaceUmaの因子分析を行うためのデータを取得 """
        print("-- check! this is BaseTransform class: " + sys._getframe().f_code.co_name)
        temp_df = pd.merge(input_raceuma_df, race_df, on="競走コード")
        X = temp_df[
            ['競走コード', '馬番',  'タイム指数', '単勝オッズ', '先行率', 'ペース偏差値', '距離増減', '斤量比', '追込率', '平均タイム',
             "距離", "頭数", "非根幹", "上り係数", "逃げ勝ち", "内勝ち", "外勝ち", "短縮勝ち", "延長勝ち", "人気勝ち"]]

        mmsc_columns = ["頭数"]
        mmsc_dict_name = "sc_fa_race_mmsc"
        stdsc_columns = ["距離"]
        stdsc_dict_name = "sc_fa_race_stdsc"
        X = mu.scale_df_for_fa(X, mmsc_columns, mmsc_dict_name, stdsc_columns, stdsc_dict_name, dict_folder)

        X_fact = X.drop(["競走コード", "馬番"], axis=1)

        X_fact = X_fact.replace(np.inf, np.nan).fillna(X_fact.median()).fillna(0)
        X_fact.iloc[0] = X_fact.iloc[0] + 0.000001

        dict_name = "fa_raceuma_result_df"
        filename = dict_folder + dict_name + '.pkl'
        if os.path.exists(filename):
            fa = mu.load_dict(dict_name, dict_folder)
        else:
            fa = FactorAnalyzer(n_factors=3, rotation='promax', impute='drop')
            fa.fit(X_fact)
            mu.save_dict(fa, dict_name, dict_folder)

        fa_np = fa.transform(X_fact)
        fa_df = pd.DataFrame(fa_np, columns=["fa_1", "fa_2", "fa_3"])
        fa_df = pd.concat([X[["競走コード", "馬番"]], fa_df], axis=1)
        X_fact = pd.merge(input_raceuma_df, fa_df, on=["競走コード", "馬番"])
        return X_fact

    def create_feature_race_base_df(self, race_df):
        """ 特徴となる値を作成する。月日→月、距離→根幹、距離グループを作成して列として付与する。

        :param dataframe race_df:
        :return: dataframe
        """
        temp_race_df = race_df.copy()
        temp_race_df.loc[:, '月'] = race_df['月日'].apply(lambda x: x.month)
        temp_race_df.loc[:, "非根幹"] = race_df["距離"].apply(lambda x: 0 if x % 400 == 0 else 1)
        temp_race_df.loc[:, "距離グループ"] = race_df["距離"] // 400
        return temp_race_df

    def create_feature_race_df(self, race_df):
        """ 特徴となる値を作成する。月日→月、距離→根幹、距離グループを作成して列として付与する。

        :param dataframe race_df:
        :return: dataframe
        """
        temp_race_df = race_df.copy()
        temp_race_df.loc[:, '月'] = race_df['月日'].apply(lambda x: x.month)
        temp_race_df.loc[:, "非根幹"] = race_df["距離"].apply(lambda x: 0 if x % 400 == 0 else 1)
        temp_race_df.loc[:, "距離グループ"] = race_df["距離"] // 400
        return temp_race_df

    def create_feature_raceuma_base_df(self, raceuma_df):
        """  raceuma_dfの特徴量を作成する。馬番→馬番グループを作成して列を追加する。

        :param dataframe raceuma_df:
        :return: dataframe
        """
        temp_raceuma_df = raceuma_df.copy()
        temp_raceuma_df.loc[:, "馬番グループ"] = raceuma_df["馬番"] // 4
        return temp_raceuma_df

    def group_prev_raceuma_df(self, raceuma_prev_df, raceuma_base_df):
        """
        | set_all_prev_raceuma_dfで生成された過去レコードに対して、条件毎にタイム指数の最大値と平均値を取得してdataframeとして返す。normalize_group_prev_raceuma_dfで最終的に偏差値化する。
        | 条件は、同場、同距離、同根幹、同距離グループ、同馬番グループ

        :param dataframe raceuma_prev_df: dataframe（過去走のdataframe)
        :param dataframe raceuma_base_df: dataframe（軸となるdataframe)
        :return: dataframe
        """
        # ベースとなるデータフレームを作成する
        raceuma_base_df = raceuma_base_df[["競走コード", "馬番"]]
        # print(raceuma_prev_df.head())
        # 集計対象の条件のみに絞り込んだデータを作成し、平均値と最大値を計算する
        same_ba_df = raceuma_prev_df.query("場コード_x == 場コード_y").groupby(["競走コード", "馬番"])["タイム指数"].agg(['max', 'mean']).rename(columns=lambda x: '同場_' + x).reset_index()
        same_kyori_df = raceuma_prev_df.query("距離_x == 距離_y").groupby(["競走コード", "馬番"])["タイム指数"].agg(['max', 'mean']).rename(columns=lambda x: '同距離_' + x).reset_index()
        same_konkan_df = raceuma_prev_df.query("非根幹_x == 非根幹_y").groupby(["競走コード", "馬番"])["タイム指数"].agg(['max', 'mean']).rename(columns=lambda x: '同根幹_' + x).reset_index()
        same_kyori_group_df = raceuma_prev_df.query("距離グループ_x == 距離グループ_y").groupby(["競走コード", "馬番"])["タイム指数"].agg(['max', 'mean']).rename(columns=lambda x: '同距離グループ_' + x).reset_index()
        same_umaban_group_df = raceuma_prev_df.query("馬番グループ_x == 馬番グループ_y").groupby(["競走コード", "馬番"])["タイム指数"].agg(['max', 'mean']).rename(columns=lambda x: '同馬番グループ_' + x).reset_index()
        # 集計したデータをベースのデータフレームに結合追加する
        raceuma_base_df = pd.merge(raceuma_base_df, same_ba_df, on=["競走コード", "馬番"], how='left')
        raceuma_base_df = pd.merge(raceuma_base_df, same_kyori_df, on=["競走コード", "馬番"], how='left')
        raceuma_base_df = pd.merge(raceuma_base_df, same_konkan_df, on=["競走コード", "馬番"], how='left')
        raceuma_base_df = pd.merge(raceuma_base_df, same_kyori_group_df, on=["競走コード", "馬番"], how='left')
        raceuma_base_df = pd.merge(raceuma_base_df, same_umaban_group_df, on=["競走コード", "馬番"], how='left')

        # 集計値を標準化する
        # 偏差値計算するための平均値と偏差を計算する
        grouped_df = raceuma_base_df[['競走コード', '同場_max', '同場_mean', '同距離_max', '同距離_mean', '同根幹_max', '同根幹_mean', '同距離グループ_max',
                                 '同距離グループ_mean', '同馬番グループ_max', '同馬番グループ_mean']].groupby('競走コード').agg(['mean', 'std']).reset_index()
        # 計算するために列名を変更する
        grouped_df.columns = ['競走コード', '同場_max_mean', '同場_max_std', '同場_mean_mean', '同場_mean_std', '同距離_max_mean', '同距離_max_std', '同距離_mean_mean', '同距離_mean_std', '同根幹_max_mean', '同根幹_max_std',
                              '同根幹_mean_mean', '同根幹_mean_std', '同距離グループ_max_mean', '同距離グループ_max_std', '同距離グループ_mean_mean', '同距離グループ_mean_std', '同馬番グループ_max_mean', '同馬番グループ_max_std', '同馬番グループ_mean_mean', '同馬番グループ_mean_std']
        # 偏差値計算するためのデータフレームを作成する
        merged_df = pd.merge(raceuma_base_df, grouped_df, on='競走コード')
        # ベースとなるデータフレームを作成する
        df = merged_df[["競走コード", "馬番"]].copy()
        # 各偏差値をベースとなるデータフレームに追加していく
        df.loc[:, '同場_max'] = (merged_df['同場_max'] - merged_df['同場_max_mean']) / merged_df['同場_max_std'] * 10 + 50
        df.loc[:, '同場_mean'] = (merged_df['同場_mean'] - merged_df['同場_mean_mean']) / merged_df['同場_mean_std'] * 10 + 50
        df.loc[:, '同距離_max'] = (merged_df['同距離_max'] - merged_df['同距離_max_mean']) / merged_df['同距離_max_std'] * 10 + 50
        df.loc[:, '同距離_mean'] = (merged_df['同距離_mean'] - merged_df['同距離_mean_mean']) / merged_df['同距離_mean_std'] * 10 + 50
        df.loc[:, '同根幹_max'] = (merged_df['同根幹_max'] - merged_df['同根幹_max_mean']) / merged_df['同根幹_max_std'] * 10 + 50
        df.loc[:, '同根幹_mean'] = (merged_df['同根幹_mean'] - merged_df['同根幹_mean_mean']) / merged_df['同根幹_mean_std'] * 10 + 50
        df.loc[:, '同距離グループ_max'] = (merged_df['同距離グループ_max'] - merged_df['同距離グループ_max_mean']) / merged_df['同距離グループ_max_std'] * 10 + 50
        df.loc[:, '同距離グループ_mean'] = (merged_df['同距離グループ_mean'] - merged_df['同距離グループ_mean_mean']) / merged_df['同距離グループ_mean_std'] * 10 + 50
        df.loc[:, '同馬番グループ_max'] = (merged_df['同馬番グループ_max'] - merged_df['同馬番グループ_max_mean']) / merged_df['同馬番グループ_max_std'] * 10 + 50
        df.loc[:, '同馬番グループ_mean'] = (merged_df['同馬番グループ_mean'] - merged_df['同馬番グループ_mean_mean']) / merged_df['同馬番グループ_mean_std'] * 10 + 50
        return df

    def choose_race_result_column(self, race_df):
        """ レースデータから必要な列に絞り込む。列はデータ区分、主催者コード、競走コード、月日、距離、場コード、頭数、予想勝ち指数、予想決着指数, 競走種別コード

        :param dataframe race_df:
        :return: dataframe
        """
        print("-- check! this is BaseTransform class: " + sys._getframe().f_code.co_name)
        temp_race_df = race_df[
            ['データ区分', '主催者コード', '競走コード', '月日', '距離', '場コード', '頭数', '天候コード', '馬場状態コード', '競走種別コード', 'ペース', '後３ハロン']]
        return temp_race_df

    def create_feature_race_result_df(self, race_df, race_winner_df):
        """  race_ddfのデータから特徴量を作成して列を追加する。月日→月、距離→非根幹、距離グループを作成

        :param dataframe race_df:
        :return: dataframe
        """
        print("-- check! this is BaseTransform class: " + sys._getframe().f_code.co_name)
        temp_race_df = race_df.copy()
        temp_merge_df = pd.merge(race_df, race_winner_df, on="競走コード")
        temp_race_df.loc[:, 'cos_day'] = race_df['月日'].dt.dayofyear.apply(
            lambda x: np.cos(math.radians(90 - (x / 365) * 360)))
        temp_race_df.loc[:, 'sin_day'] = race_df['月日'].dt.dayofyear.apply(
            lambda x: np.sin(math.radians(90 - (x / 365) * 360)))
        temp_merge_df.loc[:, "上り係数"] = temp_merge_df.apply(
            lambda x: 1 if x["後３ハロン"] == 0 else (x["後３ハロン"] / 600) / (x["タイム"] / x["距離"]), axis=1)
        temp_merge_df.loc[:, "逃げ勝ち"] = temp_merge_df["コーナー順位4"].apply(lambda x: 1 if x == 1 else 0)
        temp_merge_df.loc[:, "内勝ち"] = temp_merge_df["枠番"].apply(lambda x: 1 if x in (1, 2, 3) else 0)
        temp_merge_df.loc[:, "外勝ち"] = temp_merge_df["枠番"].apply(lambda x: 1 if x in (6, 7, 8) else 0)
        temp_merge_df.loc[:, "短縮勝ち"] = temp_merge_df["距離増減"].apply(lambda x: 1 if x < 0 else 0)
        temp_merge_df.loc[:, "延長勝ち"] = temp_merge_df["距離増減"].apply(lambda x: 1 if x > 0 else 0)
        temp_merge_df.loc[:, "人気勝ち"] = temp_merge_df["単勝人気"].apply(lambda x: 1 if x == 1 else 0)
        merge_df = pd.merge(temp_race_df,
                            temp_merge_df[["競走コード", "上り係数", "逃げ勝ち", "内勝ち", "外勝ち", "短縮勝ち", "延長勝ち", "人気勝ち"]],
                            on="競走コード")
        return merge_df

    def encode_race_df(self, race_df):
        """  列をエンコードする処理（ラベルエンコーディング、onehotエンコーディング等）。デフォルトでは特にないのでそのまま返す。

        :param dataframe race_df:
        :return: dataframe
        """
        temp_race_df = race_df.copy()
        return temp_race_df

    def choose_raceuma_result_column(self, raceuma_df):
        """  レース馬データから必要な列に絞り込む。

        :param dataframe raceuma_df:
        :return: dataframe
        """
        temp_raceuma_df = raceuma_df[
            ['データ区分', '競走コード', '馬番', '年月日', '血統登録番号', 'タイム指数', '単勝オッズ', '単勝人気', '確定着順', '着差', '休養週数', '先行率', 'タイム',
             'ペース偏差値', '展開コード', 'クラス変動', '騎手所属場コード', '騎手名', 'テン乗り', '負担重量', '馬体重', '馬体重増減', 'コーナー順位4', '距離増減', '所属', '調教師所属場コード',
             '転厩', '斤量比', '騎手ランキング', '調教師ランキング', 'デフォルト得点', '得点V1', '得点V2', '得点V3']].copy()
        return temp_raceuma_df

    def encode_raceuma_result_df(self, raceuma_df, dict_folder):
        """  列をエンコードする処理。騎手名、所属、転厩をラベルエンコーディングして値を置き換える。learning_modeがTrueの場合は辞書生成がされる。

        :param dataframe raceuma_df:
        :return: dataframe
        """
        print("-- check! this is BaseTransform class: " + sys._getframe().f_code.co_name)
        temp_raceuma_df = raceuma_df.copy()
        temp_raceuma_df.loc[:, '騎手名'] = mu.label_encoding(raceuma_df['騎手名'], '騎手名', dict_folder).astype(str)
        temp_raceuma_df.loc[:, '所属'] = mu.label_encoding(raceuma_df['所属'], '所属', dict_folder).astype(str)
        temp_raceuma_df.loc[:, '転厩'] = mu.label_encoding(raceuma_df['転厩'], '転厩', dict_folder).astype(str)
        return temp_raceuma_df.copy()

    def normalize_raceuma_result_df(self, raceuma_df):
        """ 数値系データを平準化する処理。偏差値に変換して置き換える。対象列は負担重量、予想タイム指数、デフォルト得点、得点V1、得点V2、得点V3。偏差がない場合は５０に設定

        :param dataframe raceuma_df:
        :return: dataframe
        """
        grouped_df = raceuma_df[['競走コード', '負担重量', 'タイム指数', 'デフォルト得点', '得点V1', '得点V2', '得点V3']].groupby('競走コード').agg(
            ['mean', 'std']).reset_index()
        grouped_df.columns = ['競走コード', '負担重量_mean', '負担重量_std', 'タイム指数_mean', 'タイム指数_std', 'デフォルト得点_mean',
                              'デフォルト得点_std', '得点V1_mean', '得点V1_std', '得点V2_mean', '得点V2_std', '得点V3_mean',
                              '得点V3_std']
        merged_df = pd.merge(raceuma_df, grouped_df, on='競走コード')
        merged_df['負担重量偏差'] = (merged_df['負担重量'] - merged_df['負担重量_mean']) / merged_df['負担重量_std'] * 10 + 50
        merged_df['タイム指数偏差'] = (merged_df['タイム指数'] - merged_df['タイム指数_mean']) / merged_df['タイム指数_std'] * 10 + 50
        merged_df['デフォルト得点偏差'] = (merged_df['デフォルト得点'] - merged_df['デフォルト得点_mean']) / merged_df[
            'デフォルト得点_std'] * 10 + 50
        merged_df['得点V1偏差'] = (merged_df['得点V1'] - merged_df['得点V1_mean']) / merged_df['得点V1_std'] * 10 + 50
        merged_df['得点V2偏差'] = (merged_df['得点V2'] - merged_df['得点V2_mean']) / merged_df['得点V2_std'] * 10 + 50
        merged_df['得点V3偏差'] = (merged_df['得点V3'] - merged_df['得点V3_mean']) / merged_df['得点V3_std'] * 10 + 50
        merged_df.drop(
            ['負担重量_mean', '負担重量_std', 'タイム指数_mean', 'タイム指数_std', 'デフォルト得点_mean', 'デフォルト得点_std', '得点V1_mean',
             '得点V1_std',
             '得点V2_mean', '得点V2_std', '得点V3_mean', '得点V3_std', '負担重量', 'タイム指数', 'デフォルト得点', '得点V1', '得点V2', '得点V3'],
            axis=1, inplace=True)
        raceuma_df = merged_df.rename(columns={'負担重量偏差': '負担重量', 'タイム指数偏差': 'タイム指数',
                                               'デフォルト得点偏差': 'デフォルト得点', '得点V1偏差': '得点V1', '得点V2偏差': '得点V2',
                                               '得点V3偏差': '得点V3'}).copy()
        raceuma_df.replace([np.inf, -np.inf], np.nan)
        raceuma_df.fillna({'負担重量': 50, 'タイム指数': 50, 'デフォルト得点': 50, '得点V1': 50, '得点V2': 50, '得点V3': 50},
                          inplace=True)
        return raceuma_df.copy()

    def create_feature_raceuma_result_df(self,  race_df, raceuma_df):
        """  raceuma_dfの特徴量を作成する。馬番→馬番グループを作成して列を追加する。

        :param dataframe raceuma_df:
        :return: dataframe
        """
        print("-- check! this is BaseTransform class: " + sys._getframe().f_code.co_name)
        temp_raceuma_df = raceuma_df.copy()
        temp_merge_df = pd.merge(raceuma_df, race_df, on="競走コード")
        temp_raceuma_df.loc[:, "同場騎手"] = (temp_merge_df["騎手所属場コード"] == temp_merge_df["場コード"]).astype(int)
        temp_raceuma_df.loc[:, "同所属場"] = (temp_merge_df["調教師所属場コード"] == temp_merge_df["場コード"]).astype(int)
        temp_raceuma_df.loc[:, "同所属騎手"] = (temp_merge_df["騎手所属場コード"] == temp_merge_df["調教師所属場コード"]).astype(int)
        temp_raceuma_df.loc[:, "追込率"] = (temp_merge_df["コーナー順位4"] - temp_merge_df["確定着順"]) / temp_merge_df["頭数"]
        temp_raceuma_df.loc[:, "平均タイム"] = temp_merge_df["タイム"] / temp_merge_df["距離"] * 200
        return temp_raceuma_df

    def encode_raceuma_before_df(self, raceuma_df, dict_folder):
        """  列をエンコードする処理。騎手名、所属、転厩をラベルエンコーディングして値を置き換える。learning_modeがTrueの場合は辞書生成がされる。

        :param dataframe raceuma_df:
        :return: dataframe
        """
        temp_raceuma_df = raceuma_df.copy()
        temp_raceuma_df.loc[:, '騎手名'] = mu.label_encoding(raceuma_df['騎手名'], '騎手名', dict_folder).astype(str)
        temp_raceuma_df.loc[:, '調教師名'] = mu.label_encoding(raceuma_df['調教師名'], '調教師名', dict_folder).astype(str)
        temp_raceuma_df.loc[:, '所属'] = mu.label_encoding(raceuma_df['所属'], '所属', dict_folder).astype(str)
        temp_raceuma_df.loc[:, '転厩'] = mu.label_encoding(raceuma_df['転厩'], '転厩', dict_folder).astype(str)
        return temp_raceuma_df.copy()

    def normalize_raceuma_df(self, raceuma_df):
        """ 数値系データを平準化する処理。偏差値に変換して置き換える。対象列は負担重量、予想タイム指数、デフォルト得点、得点V1、得点V2、得点V3。偏差がない場合は５０に設定

        :param dataframe raceuma_df:
        :return: dataframe
        """
        grouped_df = raceuma_df[['競走コード', '負担重量', '予想タイム指数', 'デフォルト得点', '得点V1', '得点V2', '得点V3']].groupby('競走コード').agg(['mean', 'std']).reset_index()
        grouped_df.columns = ['競走コード', '負担重量_mean', '負担重量_std', '予想タイム指数_mean', '予想タイム指数_std', 'デフォルト得点_mean',
                              'デフォルト得点_std', '得点V1_mean', '得点V1_std', '得点V2_mean', '得点V2_std', '得点V3_mean', '得点V3_std']
        merged_df = pd.merge(raceuma_df, grouped_df, on='競走コード')
        merged_df['負担重量偏差'] = (merged_df['負担重量'] - merged_df['負担重量_mean']) / merged_df['負担重量_std'] * 10 + 50
        merged_df['予想タイム指数偏差'] = (merged_df['予想タイム指数'] - merged_df['予想タイム指数_mean']) / merged_df['予想タイム指数_std'] * 10 + 50
        merged_df['デフォルト得点偏差'] = (merged_df['デフォルト得点'] - merged_df['デフォルト得点_mean']) / merged_df['デフォルト得点_std'] * 10 + 50
        merged_df['得点V1偏差'] = (merged_df['得点V1'] - merged_df['得点V1_mean']) / merged_df['得点V1_std'] * 10 + 50
        merged_df['得点V2偏差'] = (merged_df['得点V2'] - merged_df['得点V2_mean']) / merged_df['得点V2_std'] * 10 + 50
        merged_df['得点V3偏差'] = (merged_df['得点V3'] - merged_df['得点V3_mean']) / merged_df['得点V3_std'] * 10 + 50
        merged_df.drop(['負担重量_mean', '負担重量_std', '予想タイム指数_mean', '予想タイム指数_std', 'デフォルト得点_mean', 'デフォルト得点_std', '得点V1_mean', '得点V1_std',
                        '得点V2_mean', '得点V2_std', '得点V3_mean', '得点V3_std', '負担重量', '予想タイム指数', 'デフォルト得点', '得点V1', '得点V2', '得点V3'], axis=1, inplace=True)
        raceuma_df = merged_df.rename(columns={'負担重量偏差': '負担重量', '予想タイム指数偏差': '予想タイム指数',
                                               'デフォルト得点偏差': 'デフォルト得点', '得点V1偏差': '得点V1', '得点V2偏差': '得点V2', '得点V3偏差': '得点V3'}).copy()
        raceuma_df.fillna({'負担重量': 50, '予想タイム指数': 50, 'デフォルト得点': 50, '得点V1': 50, '得点V2': 50, '得点V3': 50}, inplace=True)
        return raceuma_df.copy()

    def drop_columns_raceuma_df(self, raceuma_df):
        return raceuma_df

    def standardize_raceuma_df(self, raceuma_df):
        """ 数値データを整備する。無限大(inf）をnanに置き換える

        :param dataframe raceuma_df:
        :return: dataframe
        """
        temp_raceuma_df = raceuma_df.copy()
        temp_raceuma_df.loc[:, '予想タイム指数'] = raceuma_df['予想タイム指数'].replace([np.inf, -np.inf], np.nan)
        return temp_raceuma_df.copy()

    def create_feature_raceuma_df(self, raceuma_df):
        """  raceuma_dfの特徴量を作成する。馬番→馬番グループを作成して列を追加する。

        :param dataframe raceuma_df:
        :return: dataframe
        """
        temp_raceuma_df = raceuma_df.copy()
        temp_raceuma_df.loc[:, "馬番グループ"] = raceuma_df["馬番"] // 4
        temp_raceuma_df.loc[:, "予想人気グループ"] = raceuma_df["予想人気"].apply(lambda x : self._convert_ninki_group(x))
        return temp_raceuma_df

    def _convert_ninki_group(self, ninki):
        if ninki == 1:
            return 1
        elif ninki in (2,3):
            return 2
        elif ninki in (4,5,6):
            return 3
        else:
            return 4

    def standardize_raceuma_result_df(self, raceuma_df):
        """ 数値データを整備する。無限大(inf）をnanに置き換える

        :param dataframe raceuma_df:
        :return: dataframe
        """
        temp_raceuma_df = raceuma_df.copy()
        temp_raceuma_df.loc[:, 'タイム指数'] = raceuma_df['タイム指数'].replace([np.inf, -np.inf], np.nan)
        return temp_raceuma_df.copy()

    def choose_horse_column(self, horse_df):
        """ 馬データから必要な列に絞り込む。対象は血統登録番号、繁殖登録番号１、繁殖登録番号５、東西所属コード、生産者コード、馬主コード

        :param dataframe raceuma_df:
        :return: dataframe
        """
        temp_horse_df = horse_df[['血統登録番号', '繁殖登録番号1', '繁殖登録番号5', '東西所属コード', '生産者コード', '馬主コード']]
        return temp_horse_df

    def normalize_prev_merged_df(self, merged_df):
        """ 前走レース馬データ用に値を平準化した値に置き換える。頭数で割る or 逆数化。対象は単勝人気、確定着順、コーナー順位４、騎手ランキング、調教師ランキング

        :param dataframe merged_df:
        :return: dataframe
        """
        temp_merged_df = merged_df.copy()
        temp_merged_df.loc[:, '単勝人気'] = merged_df.apply(lambda x: 0 if x['頭数'] == 0 else x['単勝人気'] / x['頭数'], axis=1)
        temp_merged_df.loc[:, '確定着順'] = merged_df.apply(lambda x: 0 if x['頭数'] == 0 else x['確定着順'] / x['頭数'], axis=1)
        temp_merged_df.loc[:, 'コーナー順位4'] = merged_df.apply(lambda x: 0 if x['頭数'] == 0 else x['コーナー順位4'] / x['頭数'], axis=1)
        #temp_merged_df.loc[:, '騎手ランキング'] = merged_df.apply(lambda x: np.nan if x['騎手ランキング'] == 0 else 1 / x['騎手ランキング'], axis=1)
        #temp_merged_df.loc[:, '調教師ランキング'] = merged_df.apply(lambda x: np.nan if x['調教師ランキング'] == 0 else 1 / x['調教師ランキング'], axis=1)
        return temp_merged_df


#// Time Complexity : O(maximum element in the given array nums)
#// Space Complexity : O(maximum element in the given array nums)
#// Did this code successfully run on Leetcode : Yes
#// Any problem you faced while coding this : No

#// Your code here along with comments explaining your approach
#In this problem first we take the maximum element in the list and create a new array with that size and fill the newly created array at #the index where element value is equal to index and keep on adding the value to that index till we are donw with all the elements.with #the newly created array we can use the same logic used for the problem House_robber.we get the maximum number of points by taking the #maximum of the last row.

class Solution(object):
    def deleteAndEarn(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        if nums == None or len(nums) == 0:
            return 0

        out=[0]*(max(nums)+1)

        for i in range(len(nums)):
            out[nums[i]] = out[nums[i]]+nums[i]
        out1=[[None for i in range(2)] for j in range(len(out))]

        for i in range(len(out1)):
            if i==0:
                out1[i][0]=0
                out1[i][1]=out[i]
            else:
                out1[i][0] = max(out1[i-1][0], out1[i-1][1])
                out1[i][1] = out1[i-1][0] + out[i]
        return max(out1[len(out)-1][0], out1[len(out)-1][1])


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

"""
Remodeling tasks based on their relationships

Parameters
----------
mappings ：all assigned tasks get from the `task_mining`
samples : input samples

Returns
-------
models : List of groups which including at least 1 task.
"""

from typing import List

import numpy as np
import pandas as pd

from sedna.datasources import BaseDataSource
from sedna.common.class_factory import ClassFactory, ClassType

__all__ = ('DefaultTaskRemodeling',)

@ClassFactory.register(ClassType.MTL)
class DefaultTaskRemodeling:
    """
    Assume that each task is independent of each other
    """

    def __init__(self, models: list, **kwargs):
        self.models = models

    def __call__(self, samples: BaseDataSource, mappings: List):
        """
        Grouping based on assigned tasks
        """
        mappings = np.array(mappings)
        data, models = [], []
        d_type = samples.data_type
        for m in np.unique(mappings):
            task_df = BaseDataSource(data_type=d_type)
            _inx = np.where(mappings == m)
            if isinstance(samples.x, pd.DataFrame):
                task_df.x = samples.x.iloc[_inx]
            else:
                task_df.x = np.array(samples.x)[_inx]
            if d_type != "test":
                if isinstance(samples.x, pd.DataFrame):
                    task_df.y = samples.y.iloc[_inx]
                else:
                    task_df.y = np.array(samples.y)[_inx]
            task_df.inx = _inx[0].tolist()
            if samples.meta_attr is not None:
                task_df.meta_attr = np.array(samples.meta_attr)[_inx]
            data.append(task_df)
            model = self.models[m] or self.models[0]
            models.append(model)
        return data, models


# 5个挑选的节点对
from __future__ import division
import pandas as pd
import csv
import numpy as np
import time
import datetime
from collections import defaultdict
import networkx as nx
import scipy
import os

import numpy as np
from keras.callbacks import EarlyStopping, ModelCheckpoint
from keras.callbacks import TensorBoard
from keras.layers import Input, Dropout
from keras.models import Model
from keras.optimizers import Adam
from keras.regularizers import l2
from keras.models import Sequential
from keras.layers import Dense, LSTM, RepeatVector, Layer
from keras.layers import Concatenate, Reshape
import keras
import tensorflow as tf

from graph_attention_layer import GraphAttention
from utils import load_data, preprocess_features

from sklearn.preprocessing import MinMaxScaler

import attention

scaler = MinMaxScaler(feature_range=(0, 1))

name = open('./data/name_node_pairs_2_quchong_with12_without_notran.csv')
df_name_node_pairs = pd.read_csv(name)
name_node_pairs = df_name_node_pairs['name_node_pairs']

#====================================建图================================
Graph_all = np.load('./data/GAT_preprocess/Graph_all.npy', allow_pickle=True)
temp = np.zeros((739, 739))
A_all = [temp for i in range(5)]
for i in range(5):
    # A_all[i]
    # Graph_all[i]
    for j in range(739):
        if Graph_all[i][j]:

            for k in range(len(Graph_all[i][j])):
                # 第i个时间段，第j个节点对，
                A_all[i][j][Graph_all[i][j][k]] = 1
        else:
            # 若是第i个时间段，第j个节点对，如果该节点在该时间段没有邻接的节点，则跳过，因为初始化就是零
            continue

    A_all[i] = scipy.sparse.csr_matrix(A_all[i])
    A_all[i] = A_all[i] + np.eye(A_all[i].shape[0])

# print('A_all[4]')
# print(A_all[4])
# print('nx.to_dict_of_dicts(A_all[4])')
# print(nx.to_dict_of_dicts(A_all[4]))
# i = 0
# while i<10:
#     i = i-1

# Read data
# X = np.load('E:\\exchange_0x3f5ce5fbfe3e9af3971dd833d26ba9b5c936f0be_90\\GAT_preprocess\\'+str(0)+'\\all.npy')
# print(X)
# X_out = np.load('E:\\exchange_0x3f5ce5fbfe3e9af3971dd833d26ba9b5c936f0be_90\\GAT_preprocess\\'+str(3)+'\\all.npy')
# X = scipy.sparse.csr_matrix(X)
# X = X.tolil()
# X_out = scipy.sparse.csr_matrix(X_out)
# X_out = X_out.tolil()

data = []
for i in range(5):
    print(i)
    temp = np.load('./data/GAT_preprocess/'+str(i)+'/all.npy')
    print(temp)
    temp = scipy.sparse.csr_matrix(temp)
    # print(temp)
    temp = temp.tolil()
    data.append(temp)
# ----------------------------
# data_train = []
# data_test = []
# # print('data.shape[0]')
# # print(data.shape[0]) 5
# for i in range(2):
#     data_train.append(data[i])
# for i in range(2,4):
#     data_test.append(data[i])
# data_test = np.array(data_test)
# data_train = np.array(data_train)

# X1 = np.load('E:\\exchange_0x3f5ce5fbfe3e9af3971dd833d26ba9b5c936f0be_90\\GAT_preprocess\\'+str(0)+'\\all.npy')
# print(X1)
# # X1_out = np.load('E:\\exchange_0x3f5ce5fbfe3e9af3971dd833d26ba9b5c936f0be_90\\GAT_preprocess\\'+str(3)+'\\all.npy')
# X1 = scipy.sparse.csr_matrix(X1)
# # X1 = X1.tolil()
# # X1_out = scipy.sparse.csr_matrix(X1_out)
# # X1_out = X1_out.tolil()
#
# X2 = np.load('E:\\exchange_0x3f5ce5fbfe3e9af3971dd833d26ba9b5c936f0be_90\\GAT_preprocess\\'+str(1)+'\\all.npy')
# print(X2)
# # X2_out = np.load('E:\\exchange_0x3f5ce5fbfe3e9af3971dd833d26ba9b5c936f0be_90\\GAT_preprocess\\'+str(3)+'\\all.npy')
# X2 = scipy.sparse.csr_matrix(X2)
# # X2 = X2.tolil()
# # X2_out = scipy.sparse.csr_matrix(X2_out)
# # X2_out = X2_out.tolil()
#
# X3 = np.load('E:\\exchange_0x3f5ce5fbfe3e9af3971dd833d26ba9b5c936f0be_90\\GAT_preprocess\\'+str(2)+'\\all.npy')
# print(X3)
# # X3_out = np.load('E:\\exchange_0x3f5ce5fbfe3e9af3971dd833d26ba9b5c936f0be_90\\GAT_preprocess\\'+str(3)+'\\all.npy')
# X3 = scipy.sparse.csr_matrix(X3)
# # X3 = X3.tolil()
# # X3_out = scipy.sparse.csr_matrix(X3_out)
# # X3_out = X3_out.tolil()
#
# X4 = np.load('E:\\exchange_0x3f5ce5fbfe3e9af3971dd833d26ba9b5c936f0be_90\\GAT_preprocess\\'+str(3)+'\\all.npy')
# print(X4)
# # X4_out = np.load('E:\\exchange_0x3f5ce5fbfe3e9af3971dd833d26ba9b5c936f0be_90\\GAT_preprocess\\'+str(3)+'\\all.npy')
# X4 = scipy.sparse.csr_matrix(X4)
# # X4 = X4.tolil()
# # X4_out = scipy.sparse.csr_matrix(X4_out)
# # X4_out = X4_out.tolil()
#
# X5 = np.load('E:\\exchange_0x3f5ce5fbfe3e9af3971dd833d26ba9b5c936f0be_90\\GAT_preprocess\\'+str(4)+'\\all.npy')
# print(X5)
# # X5_out = np.load('E:\\exchange_0x3f5ce5fbfe3e9af3971dd833d26ba9b5c936f0be_90\\GAT_preprocess\\'+str(3)+'\\all.npy')
# X5 = scipy.sparse.csr_matrix(X5)
# # X5 = X5.tolil()
# # X5_out = scipy.sparse.csr_matrix(X5_out)
# # X5_out = X5_out.tolil()

#=============================超参数========================================
N = data[0].shape[0]                # Number of nodes in the graph
F = data[0].shape[1]                # Original feature dimension

# n_classes = Y_train.shape[1]  # Number of classes
F_ = F                        # Output size of first GraphAttention layer
n_attn_heads = 8              # Number of attention heads in first GAT layer
dropout_rate = 0.6            # Dropout rate (between and inside GAT layers)
l2_reg = 5e-4/2               # Factor for l2 regularization
learning_rate = 5e-3          # Learning rate for Adam
epochs = 10000                # Number of training epochs
es_patience = 100             # Patience fot early stopping
# print('A')
# print(A)

# ===================    A    ===============================

# A = nx.adjacency_matrix(nx.from_dict_of_lists(graph))

# A = A.A
# A = A.tolist()
# print('A--------------')
# print(A)
#
# A = [A for i in range(2)]
# A = np.array(A)
# A = A.reshape(2*N,N)
#
# A = scipy.sparse.csr.csr_matrix(A)

# A = A + np.eye(A.shape[0])  # Add self-loops

#=========================  X      =======================
#==================================归一化===================================

data_pre = []
data_train = []
data_test = []
# Preprocessing operations
A_list = A_all

for i in range(5):
    data_pre.append(preprocess_features(data[i]))

for i in range(2):
    data_train.append(data_pre[i])

for i in range(2,4):
    data_test.append(data_pre[i])

# for i in range(5):
#     data_pre.append(preprocess_features(data[i]))
# for i in range(3):
#     data_train.append([data_pre[i], A])
# for i in range(2,4):
#     data_test.append([data_pre[i], A])

#--ValueError: could not broadcast input array from shape (12,7) into shape (12)
# for i in range(5):
#     data_pre.append(preprocess_features(data[i]))
# for i in range(3):
#     data_train.append(np.array([np.array(data_pre[i]).ravel(), np.array(A).ravel()]))
# for i in range(2,4):
#     data_test.append([data_pre[i], A])
# =====================提取交易额作为ytrain==============================

dataset_total = []
for i in range(len(name_node_pairs)):
    file = open('./data/temporal link features_5_7days_739/' +
                name_node_pairs[i] + '_temp_link_ft.csv')
    df = pd.read_csv(file)
    new_data = pd.DataFrame(df, columns=['tran_sum'])
    dataset = new_data.values # 得到nparray
    dataset = dataset.reshape(1, -1)
    # print('dataset shape')
    # print(dataset.shape) (1, 5)
    dataset_total.append(dataset)
dataset_total = np.array(dataset_total)
scaler = MinMaxScaler(feature_range=(0, 1))
dataset_total = np.array(dataset_total)
dataset_total = np.reshape(dataset_total, (N, 5))

y_train = []
for i in range(2): # 2 train samples
    temp = [a[1+i] for a in dataset_total]
    y_train.append(temp)
y_train = np.array(y_train)
# y_train = scaler.fit_transform(y_train)
print(y_train.shape) #(2, 12)
#===================真实交易额====================================

transum = []
for i in range(2): # 2test samples
    temp =  [a[3+i] for a in dataset_total]
    transum.append(temp)
transum = np.array(transum)
print(transum)
scaler.fit_transform(transum)

# X1 = preprocess_features(X1)
# X2 = preprocess_features(X2)
# X3 = preprocess_features(X3)
# X4 = preprocess_features(X4)
# X5 = preprocess_features(X5)

#=====================模型构建========================================

# Model definition (as per Section 3.3 of the paper)
# X1_in = Input(shape=(F,))
# X2_in = Input(shape=(F,))
# X3_in = Input(shape=(F,))
# X4_in = Input(shape=(F,))
# X5_in = Input(shape=(F,))
X_in = Input(shape=(F,), batch_shape=(N, F))
A_in = Input(shape=(N,), batch_shape=(N, N))
dropout = Dropout(dropout_rate)(X_in)

# dropout1 = Dropout(dropout_rate)(X1_in)
# dropout2 = Dropout(dropout_rate)(X2_in)
# dropout3 = Dropout(dropout_rate)(X3_in)
# dropout4 = Dropout(dropout_rate)(X4_in)
# dropout5 = Dropout(dropout_rate)(X5_in)

graph_attention_1 = GraphAttention(N=N,
                                   F_=F_,
                                   attn_heads=n_attn_heads,
                                   attn_heads_reduction='average',
                                   dropout_rate=dropout_rate,
                                   activation='elu',
                                   kernel_regularizer=l2(l2_reg),
                                   attn_kernel_regularizer=l2(l2_reg))([X_in, A_in])
print('graph_attention_1')
print(graph_attention_1)
# graph_attention_1 = tf.reshape(graph_attention_1, [-1,N,F])
graph_attention_1 = Reshape((-1, F))(graph_attention_1)
print('--------graph_attention_1-------------')
print(graph_attention_1)

gat_lstm = LSTM(units=50, input_shape=[N, F], return_sequences=False)(graph_attention_1)

# --------------------------------GAT_LSTM_GAT-------------------------------------------------------------------------------------
#
# gat_lstm_gat = GraphAttention(N=N,
#                           F_=F_,
#                           attn_heads=n_attn_heads,
#                           attn_heads_reduction='average',
#                           dropout_rate=dropout_rate,
#                           activation='elu',
#                           kernel_regularizer=l2(l2_reg),
#                           attn_kernel_regularizer=l2(l2_reg))([gat_lstm, A_in])
# gat_lstm = Dense(1)(gat_lstm_gat)
# ------------------------------------------------------------------------------------------------------------

# ---------------------------------GAT_LSTM----------------------------------------------------------------------------

gat_lstm = Dense(1)(gat_lstm)
# ---------------------------------GAT_LSTM_att----------------------------------
# 需要将前面的LSTM的returnsequence设为true
# gat_lstm = attention.attention_3d_block(gat_lstm)
# gat_lstm = Dense(1)(gat_lstm)
# ----------------------------------------------------------------------------------------------------------

model = Model(inputs=[X_in, A_in], outputs=gat_lstm)
model.compile(loss='mse', optimizer='rmsprop')

#=======================拟合模型,预测==================================
# print('data_train')
# print(data_train)
print('y_train')
print(y_train)

print('model')
print(model.summary())

for i in range(2):
    model.fit([data_train[i], A_list[i]], y_train[i], batch_size=N)

value = []
for i in range(2):
    value.append(model.predict([data_test[i], A_list[i+3]], batch_size=N))

value = np.array(value).reshape(2,739)
print(np.array(value).reshape(2,739))
# value = scaler.inverse_transform(value)
# 出现负值置零
value[value<0]=0
print(value)

data_pred = pd.DataFrame({'pred_1': value[0], 'pred_2': value[1], 'transum_1': transum[0].tolist(), 'transum_2': transum[1].tolist()})

# -----------------------GAT_LSTM----------------------------------------------------------------

data_pred.to_csv('./data/temporal link prediction_GAT_LSTM/prediciton_GAT_LSTM.csv', index=False)

# -------------------------GAT_LSTM_GAT-------------------------------------------------------

# data_pred.to_csv('E:\\exchange_0x3f5ce5fbfe3e9af3971dd833d26ba9b5c936f0be_12\\temporal link prediction_GAT_LSTM_GAT\\prediciton_GAT_LSTM_GAT.csv', index=False)

# ---------------------------------------------------------------------------------------------
rmse = 0
rmse = np.sqrt(np.mean(np.power((value - transum),2)))
print(rmse)
#==========================后面先不管=============================================
# model = Sequential
# model.add(graph_attention_1)
# model.add(LSTM(units=50, input_shape=[N,F], return_sequences=False))
# model.add(Dense(12))
# model.compile(loss='mse', optimizer='rmsprop')

# graph_attention_2 = GraphAttention(F_,
#                                    attn_heads=n_attn_heads,
#                                    attn_heads_reduction='average',
#                                    dropout_rate=dropout_rate,
#                                    activation='elu',
#                                    kernel_regularizer=l2(l2_reg),
#                                    attn_kernel_regularizer=l2(l2_reg))([X_in, A_in])
# print(graph_attention_2)

# graph_attention_3 = GraphAttention(F_,
#                                    attn_heads=n_attn_heads,
#                                    attn_heads_reduction='average',
#                                    dropout_rate=dropout_rate,
#                                    activation='elu',
#                                    kernel_regularizer=l2(l2_reg),
#                                    attn_kernel_regularizer=l2(l2_reg))([X_in, A_in])
# print(graph_attention_3)

# graph_attention_4 = GraphAttention(F_,
#                                    attn_heads=n_attn_heads,
#                                    attn_heads_reduction='average',
#                                    dropout_rate=dropout_rate,
#                                    activation='elu',
#                                    kernel_regularizer=l2(l2_reg),
#                                    attn_kernel_regularizer=l2(l2_reg))([X_in, A_in])
# print(graph_attention_4)

# graph_attention_5 = GraphAttention(F_,
#                                    attn_heads=n_attn_heads,
#                                    attn_heads_reduction='average',
#                                    dropout_rate=dropout_rate,
#                                    activation='elu',
#                                    kernel_regularizer=l2(l2_reg),
#                                    attn_kernel_regularizer=l2(l2_reg))([X_in, A_in])
# print(graph_attention_5)

# Build model
# optimizer = Adam(lr=learning_rate)
# model = Model(inputs=[X1_in, A_in], outputs=graph_attention_1)
# model.compile(optimizer=optimizer,
#               loss='mean_squared_error',
#               weighted_metrics=['acc'])
# print(graph_attention_1)

# model = Model(inputs=[X_in, A_in], outputs=graph_attention_1)
#
# optimizer = Adam(lr=learning_rate)
# model.compile(optimizer=optimizer,
#               loss='mean_squared_error',
#               weighted_metrics=['acc'])
# model.summary()

# # Callbacks
# es_callback = EarlyStopping(monitor='val_weighted_acc', patience=es_patience)
# tb_callback = TensorBoard(batch_size=N)
# mc_callback = ModelCheckpoint('logs/best_model.h5',
#                               monitor='val_weighted_acc',
#                               save_best_only=True,
#                               save_weights_only=True)

# Train model
# validation_data = ([X, A], Y_val, idx_val)
# model.fit([X, A],
#           Y_train,
#           sample_weight=idx_train,
#           epochs=epochs,
#           batch_size=N,
#           validation_data=validation_data,
#           shuffle=False,  # Shuffling data means shuffling the whole graph
#           callbacks=[es_callback, tb_callback, mc_callback])
#
# # Load best model
# model.load_weights('logs/best_model.h5')
#
# # Evaluate model
# eval_results = model.evaluate([X, A],
#                               Y_test,
#                               sample_weight=idx_test,
#                               batch_size=N,
#                               verbose=0)
# print('Done.\n'
#       'Test loss: {}\n'
#       'Test accuracy: {}'.format(*eval_results))


import pandas as pd
import numpy as np
import networkx as nx

# 顯示所有的行
pd.set_option('display.max_columns', None)
# 顯示所有的列
pd.set_option('display.max_rows', None)
# 顯示長度 ＝ 100 ,預設50
pd.set_option('max_colwidth',100)

arr = []
ev_msg_body = [429,1,2,429,1,2,3,429,1,2]
df = pd.DataFrame(columns=['switch_id', 'live_port', 'hw_addr'])
edges = pd.DataFrame({'source' : [0, 1],
                      'target' : [1, 2],
                      'weight' : [100, 50]})

a = pd.Series(1)
b = pd.Series(2)
c=[1,2,3]
d=[4,5,6]
i= 1

# df=df.append({'switch_id':1}, ignore_index=True)
# df=df.append({'live_port':4294967}, ignore_index=True)
# df=df.append({'hw_addr':'2e:f2:9f:aa:62:4d:'}, ignore_index=True)
# df=df.append({'live_port':1, 'hw_addr':'e2:64:98:d2:8b:d7'}, ignore_index=True)
# df=df.append({'live_port':2, 'hw_addr':'d2:03:07:ee:ad:a4:'}, ignore_index=True)
# df=df.append({'switch_id':3}, ignore_index=True)

df=df.append({'switch_id':1, 'live_port':4294967, 'hw_addr':'2e:f2:9f:aa:62:4d:'}, ignore_index=True)
df=df.append({'switch_id':1, 'live_port':1, 'hw_addr':'e2:64:98:d2:8b:d7'}, ignore_index=True)
df=df.append({'switch_id':1, 'live_port':2, 'hw_addr':'d2:03:07:ee:ad:a4:'}, ignore_index=True)

df=df.append({'switch_id':2, 'live_port':4294967, 'hw_addr':'de:7e:b4:a7:f0:42'}, ignore_index=True)
df=df.append({'live_port':1, 'hw_addr':'26:50:fb:81:1f:df'}, ignore_index=True)
df=df.append({'live_port':2, 'hw_addr':'56:7c:81:82:c1:07'}, ignore_index=True)
# df=df.append({'live_port':3, 'hw_addr':'12:fb:02:12:67:77'}, ignore_index=True)
# df=df.append({'live_port':4, 'hw_addr':'6a:ec:db:25:c0:f1'}, ignore_index=True)

# df=df.append({'switch_id':3, 'live_port':4294967, 'hw_addr':'7a:5f:99:46:04:4d'}, ignore_index=True)
# df=df.append({'live_port':1, 'hw_addr':'ee:fe:46:c4:29:01'}, ignore_index=True)
# df=df.append({'live_port':4, 'hw_addr':'8a:6d:2f:7e:a1:ac'}, ignore_index=True)
# df=df.append({'live_port':2, 'hw_addr':'9a:4c:7e:21:31:38'}, ignore_index=True)
# df=df.append({'live_port':3, 'hw_addr':'ae:1a:9b:8f:72:99'}, ignore_index=True)

# print(df)
# print(df.isnull().any(axis = 0))
df['switch_id'] = df['switch_id'].fillna(method='ffill')

# print(df[df['hw_addr'].isnull() & df['live_port'].notnull()])
df1 = df[df['hw_addr'].isnull() & df['live_port'].notnull()]
# print(df[df['hw_addr'].notnull() & df['live_port'].isnull()])
df2 = df[df['hw_addr'].notnull() & df['live_port'].isnull()]

df1.set_index(df2.index, inplace=True)
# print(df1)
# print(df1.index)
# print(df2)
# print(df2.index)

df2 = df2.combine_first(df1)
# print(df2.combine_first(df1))
df = df.combine_first(df2)
# print(df)
df = df.dropna(axis=0).reset_index().drop(columns='index')
# print(df)

# df=df.append({'switch_id':4, 'live_port':8}, ignore_index=True)
# df=df.append({'switch_id':5}, ignore_index=True)
# df=df.append({'switch_id':6, 'live_port':[7,8,9]}, ignore_index=True)
# df.iloc[-1,-1] = 666
# df.ix[:, 'live_port'] = 456
# print('df.loc[0,live_port]: ', df.loc[0,'live_port'])
# print('原本的dataframe: ')
# print(df)
#
# if df.isnull().loc[1, 'live_port']:
#     print('isnull: ', df.isnull().loc[1, 'live_port'])
#     # df.loc[2,'live_port'] = [frozenset(c)] # frozenset()存不可變序列
#     # df._set_value(1,'live_port', c) # 存可變序列
#     df.loc[i, 'live_port'] = [1,2,3]
#     df.loc[2, 'live_port'] = d
#
# # df = df['switch_id'].append([2])
# # df = df['switch_id'].append([3])
#
# print('後來的dataframe: ')
# print(df)

# print(edges)

import networkx as nx
net = nx.DiGraph()

src_dpid = 1
dst_dpid = 3
port = 2

links=[(src_dpid, dst_dpid, {'port': port})]
net.add_edges_from(links)

net.add_node('aa:bb:cc')
net.add_edge(1, 'aa:bb:cc', attr_dict={'port': 3})
net.add_edge('aa:bb:cc', 1)

# self.network.add_node(src)
# self.network.add_edge(dpid, src, attr_dict={'port': in_port})
# self.network.add_edge(src, dpid)
# print(net.edges)

tmp2_df = pd.DataFrame(columns=['request_sid', 'request_port', 'receive_sid', 'receive_port'])
tmp2_df=tmp2_df.append({'request_sid': 1, 'request_port': 2, 'receive_sid': 2, 'receive_port': 2}, ignore_index=True)
tmp2_df=tmp2_df.append({'request_sid': 2, 'request_port': 2, 'receive_sid': 1, 'receive_port': 2}, ignore_index=True)
# tmp2_df=tmp2_df.append({'request_sid': 3, 'request_port': 2, 'receive_sid': 2, 'receive_port': 2}, ignore_index=True)
# tmp2_df=tmp2_df.append({'request_sid': 3, 'request_port': 1, 'receive_sid': 1, 'receive_port': 3}, ignore_index=True)
# tmp2_df=tmp2_df.append({'request_sid': 2, 'request_port': 1, 'receive_sid': 1, 'receive_port': 2}, ignore_index=True)
# tmp2_df=tmp2_df.append({'request_sid': 3, 'request_port': 3, 'receive_sid': 4, 'receive_port': 4}, ignore_index=True)
# tmp2_df=tmp2_df.append({'request_sid': 1, 'request_port': 4, 'receive_sid': 4, 'receive_port': 3}, ignore_index=True)
# tmp2_df=tmp2_df.append({'request_sid': 3, 'request_port': 4, 'receive_sid': 5, 'receive_port': 3}, ignore_index=True)
# tmp2_df=tmp2_df.append({'request_sid': 2, 'request_port': 3, 'receive_sid': 5, 'receive_port': 2}, ignore_index=True)
# tmp2_df=tmp2_df.append({'request_sid': 4, 'request_port': 5, 'receive_sid': 5, 'receive_port': 4}, ignore_index=True)

print(df)
print(tmp2_df)
# print(df.info())
# print(tmp2_df.info())
# for row in tmp2_df.itertuples():
#     print(getattr(row, 'request_sid'))
    # if (df['switch_id'] == getattr(row, 'request_sid')) & (df['live_port'] == getattr(row, 'request_port')):
    # if (df.iloc[0] == getattr(row, 'request_sid')) & (df.iloc[1] == getattr(row, 'request_port')):
    # if (df.loc['switch_id'] == 1):
    #     print('test')
# print(tmp2_df.request_sid)
# print(tmp2_df[tmp2_df['request_sid'] == 1])
df3 = df.copy()
# for i, o in tmp2_df.request_sid, tmp2_df.request_port:
#     print(i, o)
#     print(df3[(df3['switch_id'] == i) & (df3['live_port'] == o)])

for i in range(0, len(tmp2_df), 1):
    print(df3[(df3['switch_id'] == tmp2_df.at[i, 'request_sid']) & (df3['live_port'] == tmp2_df.at[i, 'request_port'])])
    request_index = df3[(df3['switch_id'] == tmp2_df.at[i, 'request_sid']) & (df3['live_port'] == tmp2_df.at[i, 'request_port'])].index
    receive_index = df3[(df3['switch_id'] == tmp2_df.at[i, 'receive_sid']) & (df3['live_port'] == tmp2_df.at[i, 'receive_port'])].index
    controller_index = df3[(df3['live_port'] > 429)].index
    # print(type(index))
    df3.drop(index=request_index, inplace=True)
    df3.drop(index=receive_index, inplace=True)
    df3.drop(index=controller_index, inplace=True)
    df3.reset_index(drop=True, inplace=True)
print(df3)

# print(df[(df['switch_id'] == tmp2_df['request_sid'])])
# print(df.loc[(df['switch_id'] == 1) & (df['live_port'] == 1)])
# print(pd.concat([df, tmp2_df], axis=0))

# tmp  = dict({3: {3: [3], 1: [3, 1], 5: [3, 5], 4: [3, 4], 2: [3, 2]}, 1: {1: [1], 3: [1, 3], 4: [1, 4], 2: [1, 2], 5: [1, 3, 5]}, 5: {5: [5], 3: [5, 3], 4: [5, 4], 2: [5, 2], 1: [5, 3, 1]}, 4: {4: [4], 3: [4, 3], 5: [4, 5], 1: [4, 1], 2: [4, 3, 2]}, 2: {2: [2], 1: [2, 1], 3: [2, 3], 5: [2, 5], 4: [2, 1, 4]}}
# )
# print('原始networkx所有資料: ', tmp)
# print('資料的所有key: ', tmp.keys())
# print('資料的所有values: ', tmp.values())
# print('資料key = 3的所有 資料: ', tmp[3])
# print('資料key = 3的所有key: ', tmp[3].keys())
# print('資料key = 3的所有資料', tmp[3].values())

# tmp_df = pd.DataFrame(tmp[3].values())
# print(tmp_df)
# tmp_df = pd.DataFrame(tmp)
# print('原始資料塞進dataframe: \n', tmp_df)
# print(tmp_df.loc[3])
# for m in tmp_df.index:
#     for i in tmp_df.loc[m]:
#         if len(i) == 2:
#             print(type(i), i[0], i[1])
#             # print(tmp2_df[(tmp2_df['request_sid'] == i[0]) & (tmp2_df['receive_sid'] == i[1]) |
#             #      (tmp2_df['request_sid'] == i[1]) & (tmp2_df['receive_sid'] == i[0])].iloc[0, 1])
#             # print(tmp2_df[(tmp2_df['request_sid'] == i[0]) & (tmp2_df['receive_sid'] == i[1]) |
#             #               (tmp2_df['request_sid'] == i[1]) & (tmp2_df['receive_sid'] == i[0])].iloc[0, 3])
#         if len(i) == 3:
#             print(type(i), i[0], i[1], i[2])
#         #     print(tmp2_df[(tmp2_df['request_sid'] == i[0]) & (tmp2_df['receive_sid'] == i[1]) |
#         #          (tmp2_df['request_sid'] == i[1]) & (tmp2_df['receive_sid'] == i[0])].iloc[0, 3])

# print(tmp_df.loc[[3]])
# print(type(tmp_df.loc[3]), type(tmp_df.loc[[3]]))

# # 轉成上三角矩陣
# m,n = tmp_df.shape
# tmp_df[:] = np.where(np.arange(m)[:,None] >= np.arange(n),np.nan,tmp_df)
# # 轉成完整矩陣
# tmp_df = tmp_df.stack().reset_index()
# tmp_df.columns = ['start_sid', 'end_sid', 'link']
# print(tmp_df)
#
# for i in range(0, len(tmp_df), 1):
#     test = tmp_df.loc[i, 'link']
#     print('第一個for: ', tmp_df.loc[i, 'link'])
#     # print(test[n:n+2])
#     n = 2
#     for link in [test[i:i + n] for i in range(0, len(test), 1)]:
#         if len(link)%2 == 0:
#             print('第二個for: ', link)
#             print('第二個for: ', link[0], link[1])


# Copyright (c) 2010-2012, GEM Foundation.
#
# OpenQuake is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# OpenQuake is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with OpenQuake.  If not, see <http://www.gnu.org/licenses/>.

"""Asynchronous task handler functions. See
:function:`openquake.utils.tasks.distribute` for more information.
"""

import time

from openquake import logs
from openquake.utils import stats

def completed_task_count(job_id):
    """Given the ID of a currently running calculation, query the stats
    counters in Redis to get the number of completed
    :function:`compute_uhs_task` task executions.

    Successful and failed executions are included in the count.

    :param int job_id:
        ID of the current job.
    :returns:
        Number of completed :function:`compute_uhs_task` task executions so
        far.
    """
    success_count = stats.pk_get(job_id, "nhzrd_done")
    fail_count = stats.pk_get(job_id, "nhzrd_failed")

    return (success_count or 0) + (fail_count or 0)

def remaining_tasks_in_block(job_id, num_tasks, start_count):
    """Figures out the numbers of remaining tasks in the current block. This
    should only be called during an active job.

    Given the ID of a currently running calculation, query the stats
    counters in Redis and determine when N :function:`compute_uhs_task` tasks
    have been completed (where N is ``num_tasks``).

    The count includes successful task executions as well as failures.

    This function is implemented as a generator which yields the remaining
    number of tasks to be execute in this block. When the target number of
    tasks is reached, a :exception:`StopIteration` is raised.

    :param int job_id:
        ID of the current job.
    :param int num_tasks:
        Number of :function:`compute_uhs_task` tasks in this block.
    :param int start_count:
        The starting total of :function:`compute_uhs_task` tasks completed.

        At the beginning of the calculation, this will be 0 of course. At the
        beginning of subsequent blocks, it needs to be computed _before_
        starting both the block calculation and the async task handler (to
        avoid a possible race condition with the task counters).
    :yields:
        The remaining number of tasks to be executed in this block.
    :raises:
        :exception:`StopIteration` when all block tasks are complete
        (successful or not).
    """
    target = start_count + num_tasks
    while completed_task_count(job_id) < target:
        yield target - completed_task_count(job_id)  # number remaining

def uhs_task_handler(job_id, num_tasks, start_count):
    """Async task handler for counting calculation results and determining when
    a batch of tasks is complete.

    This function periodically polls the task counters in Redis and blocks
    until the current block of tasks is finished.

    :param int job_id:
        The ID of the currently running job.
    :param int num_tasks:
        The number of tasks in the current block.
    :param int start_count:
        The number of tasks completed so far in the job.
    """
    remaining_gen = remaining_tasks_in_block(job_id, num_tasks, start_count)

    while True:
        time.sleep(0.5)
        try:
            remaining_gen.next()
        except StopIteration:
            # No more tasks remaining in this batch.
            break
        logs.log_percent_complete(job_id, "hazard")


def diff(num):
	number=str(num)
	reverse=number[::-1]
	rev=[]
	for digit in reverse:
		rev.append(digit)
	i=0
	while i<len(rev):
		if rev[i+1] == '0':
			del rev[i+1]
			i=0
		else:
			break
		i+=1
	print(rev)

i=input("Unesite broj:")

diff(i)