"""
Created on 01:54, June. 4th, 2021
Author: fassial
Filename: expr1.py
Description:
    Changes in spatial patterning (Ω), temporal patterning (cv) and
    cross correlation patterning (cor) as a function of weight (g_w)
    and spikelet (g_k) of the GJ network.
"""
import gc
import numpy as np
import brainpy as bp
from copy import deepcopy
# local dep
import os
import sys
sys.path.append(os.path.join("..", ".."))
import model
import stimulus
import utils

# macro
DIR_ROOT = os.getcwd()
DIR_FIGS = os.path.join(DIR_ROOT, "figs")
if not os.path.exists(DIR_FIGS): os.mkdir(DIR_FIGS)
DIR_OUTPUTS = os.path.join(DIR_ROOT, "outputs")
if not os.path.exists(DIR_OUTPUTS): os.mkdir(DIR_OUTPUTS)
DIR_OUTPUTS_STIM = os.path.join(DIR_OUTPUTS, "stimulus")
if not os.path.exists(DIR_OUTPUTS_STIM): os.mkdir(DIR_OUTPUTS_STIM)
DIR_OUTPUTS_SPIKE = os.path.join(DIR_OUTPUTS, "spike")
if not os.path.exists(DIR_OUTPUTS_SPIKE): os.mkdir(DIR_OUTPUTS_SPIKE)

## default params
# default stim_params
default_stim_params = {
    "white": stimulus.stim_params(
        name = "white",
        height = 50,
        width = 50,
        duration = 300,
        others = {
            "noise": 0.,
        }
    ),
    "black": stimulus.stim_params(
        name = "black",
        height = 50,
        width = 50,
        duration = 300,
        others = {
            "noise": 0.,
        }
    ),
    "normal": stimulus.stim_params(
        name = "normal",
        height = 50,
        width = 50,
        duration = 300,
        others = {
            "freqs": np.full((50, 50), 20., dtype = np.float32),
            "noise": 0.,
        }
    ),
    "frate_increase": stimulus.stim_params(
        name = "frate_increase",
        height = 50,
        width = 50,
        duration = 300,
        others = {
            "freqs": np.full((50, 50), 20., dtype = np.float32),
            "factor": 4.,    # (1,16)
            "ratio": .2,
            "noise": 0.,
        }
    ),
}
# default net_params
default_net_params = {
    "neurons": {
        ## neurons params
        # shape params
        "size": (50, 50),
        # dynamic params
        "V_init": "gaussian",
        "tau": 5.,
        "t_refractory": 1.,
        "noise": .2,
    },
    "GJ": {
        # gap junction
        "weight": .5,
        "k_spikelet": .1,
        "conn": bp.connect.GridEight(include_self = False),
    },
}

def expr(gj_w, gj_k, run_params, dt = 0.01):
    print("processing expr(" +\
        str(gj_w) + "," + str(gj_k) + ")..."
    )

    # init seed
    np.random.seed(0)

    # init expr_curr & stim
    expr_curr = run_params["expr_curr"]
    stim = run_params["stim"]

    ## prepare expr
    # init net_params
    net_params = deepcopy(default_net_params)
    net_params["GJ"]["weight"] = gj_w; net_params["GJ"]["k_spikelet"] = gj_k; print(net_params)

    ## exec expr
    # inst GJ2DNet
    net = model.GJ2DNet(net_params = net_params, run_params = {
        "inputs": stim,
        "dt": dt,
        "duration": default_stim_params[expr_curr].duration,
    })
    # net run
    net.run(report = True)
    # show net.mon
    net_monitors = net.get_monitors()
    net.show(img_fname = os.path.join(DIR_FIGS,
        expr_curr + "-" +\
        str(gj_w) + "-" + str(gj_k) + ".png"
    ))
    net.save(spike_fname = os.path.join(DIR_OUTPUTS_SPIKE,
        expr_curr + "-" +\
        str(gj_w) + "-" + str(gj_k) + ".csv"
    ))

    ## compute statistic-values
    # compute cor
    cor = bp.measure.cross_correlation(
        spikes = net_monitors.spike,
        bin = 10,
        dt = dt
    )
    # compute omega & cv
    spike = net_monitors.spike.T; print(spike.shape)
    omega = utils.get_omega(
        spike = spike,
        bin = 100,
        dt = dt,
        N = 100
    )
    cv = np.mean(utils.get_cv(
        spike = spike,
        dt = dt
    ))

    # rm vars
    del(net_params); del(net); del(net_monitors); del(spike); gc.collect()

    return (omega, cv, cor)

def main(dt = 0.01):
    # init omegas & cvs & cors
    omegas = []; cvs = []; cors = []

    # init gj_ws & gj_ks
    gj_ws = [.05, .1, .15, .2, .25, .3, .35, .4, .45, .5]
    gj_ks = [.05, .1, .15, .2, .25, .3, .35, .4, .45, .5]

    ## init backend
    bp.backend.set(dt = dt)
    bp.backend.set(backend = "numba")
    ## init expr_curr
    expr_curr = "white"
    ## get stim
    # get stim_fname
    stim_fname = os.path.join(
        DIR_OUTPUTS_STIM,
        expr_curr + "-" + str(default_stim_params[expr_curr].duration) + ".csv"
    )
    # get stim
    if os.path.exists(stim_fname):
        # load stim
        stim = np.loadtxt(
            fname = stim_fname,
            delimiter = ","
        )
    else:
        # get stim
        stim, _ = stimulus.stimulus.get(
            stim_params = default_stim_params[expr_curr]
        )
        # save stim
        np.savetxt(fname = stim_fname, X = stim, delimiter = ",")
    # rescale stim
    stim *= .9; print(stim.shape)

    # set omegas & cvs & cors
    for gj_w in gj_ws:
        omega = []; cv = []; cor = []
        for gj_k in gj_ks:
            res = expr(
                gj_w = gj_w,
                gj_k = gj_k,
                run_params = {
                    "expr_curr": expr_curr,
                    "stim": stim,
                },
                dt = dt
            ); omega.append(res[0]); cv.append(res[1]); cor.append(res[2])
        omegas.append(omega); cvs.append(cv); cors.append(cor)
    omegas = np.array(omegas); cvs = np.array(cvs); cors = np.array(cors)

    # save omegas & cvs & cors
    np.savetxt(
        fname = os.path.join(DIR_OUTPUTS, "omegas.csv"),
        X = omegas,
        delimiter = ","
    )
    np.savetxt(
        fname = os.path.join(DIR_OUTPUTS, "cvs.csv"),
        X = cvs,
        delimiter = ","
    )
    np.savetxt(
        fname = os.path.join(DIR_OUTPUTS, "cors.csv"),
        X = cors,
        delimiter = ","
    )

if __name__ == "__main__":
    main()


# Definition for an interval.
class Interval(object):
    def __init__(self, s=0, e=0):
        self.start = s
        self.end = e

class Solution(object):
    def merge(self, intervals):
        """
        :type intervals: List[Interval]
        :rtype: List[Interval]
        """

        n = len(intervals)
        if n<=1:
            return intervals

        startIndex = []
        endIndex = []

        for i in range(n):
            startIndex.append(intervals[i].start)
            endIndex.append(intervals[i].end)

        list.sort(startIndex)
        list.sort(endIndex)

        result = []
        j = 0
        for i in range(n):
            if i==n-1 or startIndex[i+1]>endIndex[i]:
                result.append(Interval(startIndex[j],endIndex[i]))
                j = i+1

        return result


from PDP import PDP
from JinEnv import JinEnv
from casadi import *
import math
import scipy.io as sio
import numpy as np
import time

# --------------------------- load environment ----------------------------------------
uav = JinEnv.Quadrotor()
uav.initDyn(c=0.01)
uav.initCost(wthrust=0.1)

# --------------------------- load demos data ----------------------------------------
data = sio.loadmat('../quadrotor/data/uav_demos.mat')
trajectories = data['trajectories']
true_parameter = data['true_parameter']
dt = data['dt']

# reorganize data into numpy-preferred
n_demo = trajectories.shape[1]
n_state = trajectories[0, 0]['state_traj_opt'][0, 0].shape[1]
n_control = trajectories[0, 0]['control_traj_opt'][0, 0].shape[1]

state_trajs = []
control_trajs = []
costate_trajs = []
for i in range(n_demo):
    state_traj = trajectories[0, i]['state_traj_opt'][0, 0]
    state_trajs += [state_traj]
    control_traj = trajectories[0, i]['control_traj_opt'][0, 0]
    control_trajs += [control_traj]
    costate_traj = trajectories[0, i]['costate_traj_opt'][0, 0]
    costate_trajs += [costate_traj]

# ---------------------------------------- define the KKT loss function -----------------------
# first establish a pdp object, because there are some utilities in pdp which can be used to define KKT loss
uavoc = PDP.OCSys()
uavoc.setAuxvarVariable(vertcat(uav.dyn_auxvar, uav.cost_auxvar))
uavoc.setControlVariable(uav.U)
uavoc.setStateVariable(uav.X)
dyn = uav.X + dt * uav.f
uavoc.setDyn(dyn)
uavoc.setPathCost(uav.path_cost)
uavoc.setFinalCost(uav.final_cost)
uavoc.diffPMP()
lqr_solver = PDP.LQR()

# define loss function, which is the norm of KKT residual, please find the related work for more details
KKT_loss = 0
parameter_theta = uavoc.auxvar
parameter_theta_guess = true_parameter.flatten().tolist()
parameter_lambda = []
parameter_lambda_guess = []  # in fact, this will store the true value of lambda (costates)
for i in range(n_demo):
    state_traj = state_trajs[i]
    control_traj = control_trajs[i]
    costates_traj = costate_trajs[i]
    horizon = control_traj.shape[0]
    # define costate variable
    LAM = SX.sym('lambda_1' + str(i), n_state)
    parameter_lambda += [LAM]
    parameter_lambda_guess += costates_traj[0, :].flatten().tolist()
    for t in range(0, horizon - 1):
        x_curr = state_traj[t + 1, :]
        u_curr = control_traj[t + 1, :]
        # define unknown costate
        LAM_next = SX.sym('lambda_' + str(t + 2) + str(i), n_state)
        parameter_lambda += [LAM_next]
        parameter_lambda_guess += costates_traj[t + 1, :].flatten().tolist()
        # compute the current loss
        loss_curr_1 = dot(LAM - uavoc.dHx_fn(x_curr, u_curr, LAM_next, parameter_theta),
                          LAM - uavoc.dHx_fn(x_curr, u_curr, LAM_next, parameter_theta))
        loss_curr_2 = dot(uavoc.dHu_fn(x_curr, u_curr, LAM_next, parameter_theta),
                          uavoc.dHu_fn(x_curr, u_curr, LAM_next, parameter_theta))
        KKT_loss = KKT_loss + loss_curr_1 + loss_curr_2
        # update
        LAM = LAM_next
    # final cost term
    KKT_loss = KKT_loss + dot(LAM - uavoc.dhx_fn(state_traj[-1, :], parameter_theta),
                              LAM - uavoc.dhx_fn(state_traj[-1, :], parameter_theta))

#  define KKT loss function
parameter_lambda = vcat(parameter_lambda)
parameter = vertcat(parameter_theta, parameter_lambda)
parameter_guess = parameter_theta_guess + parameter_lambda_guess
KKT_loss_fn = Function('loss_fn', [parameter], [KKT_loss])
grad_KKT_loss_fn = Function('grad_loss_fn', [parameter], [jacobian(KKT_loss, parameter)])

# ------------------------ inverse KKT: learn both the theta and costate (lambda) --------------------------

lr = 1e-7  # learning rate
for j in range(10):  # loop for trials
    start_time = time.time()
    traj_loss_trace, theta_trace, kkt_loss_trace = [], [], []

    sigma = 0.08
    initial_parameter = np.array(parameter_guess) # note we use the true parameter to initilize because otherwise the method is not working well.
    initial_parameter = initial_parameter + sigma * np.random.randn(np.size(initial_parameter)) - sigma / 2
    current_parameter = initial_parameter

    for k in range(int(2e4)):  # loop for iteration
        # update for parameter
        dp = grad_KKT_loss_fn(current_parameter).full().flatten()
        current_parameter = current_parameter - lr * dp
        current_theta = current_parameter[0:parameter_theta.numel()]
        theta_trace += [current_theta]  # only take theta
        kkt_loss = KKT_loss_fn(current_parameter).full().flatten().item()
        kkt_loss_trace += [kkt_loss]

        # solve the trajectory loss
        current_loss = 0
        for i in range(n_demo):
            # take out the demos i
            demo_state_traj = state_trajs[i]
            demo_control_traj = control_trajs[i]
            initial_state = demo_state_traj[0, :]
            horizon = demo_control_traj.shape[0]
            #  current trajectory based on current theta guess
            current_traj = uavoc.ocSolver(initial_state, horizon, current_theta)
            current_state_traj = current_traj['state_traj_opt']
            current_control_traj = current_traj['control_traj_opt']
            dldx_traj = current_state_traj - demo_state_traj
            dldu_traj = current_control_traj - demo_control_traj
            current_loss = current_loss + numpy.linalg.norm(dldx_traj) ** 2 + numpy.linalg.norm(dldu_traj) ** 2
        traj_loss_trace += [current_loss / n_demo]

        # print and terminal check
        if k % 1 == 0:
            print('trial:', j, 'iter:',k, ' loss: ', traj_loss_trace[-1])

    # save
    save_data = {'trail_no': j,
                 'loss_trace': traj_loss_trace,
                 'kkt_loss_trace': kkt_loss_trace,
                 'learning_rate': lr,
                 'parameter_trace': theta_trace,
                 'time_passed': time.time() - start_time}
    sio.savemat('./data/KKT_results_trial_' + str(j) + '.mat', {'results': save_data})


import discord
from discord.ext import commands
import random

class Channels(commands.Cog):

    def __init__(self, bot):
        self.bot = bot

    @commands.Cog.listener()
    async def on_ready(self):
        print(f"{self.__class__.__name__} Cog has been loaded\n-----")

    @commands.command(aliases=['cs'])
    @commands.bot_has_guild_permissions(manage_channels=True)
    async def channelstats(self, ctx):
        """
        Sends a nice fancy embed with some channel stats
        !channelstats
        """
        channel = ctx.channel
        embed = discord.Embed(title=f"Stats for **{channel.name}**", description=f"{'Category: {}'.format(channel.category.name) if channel.category else 'This channel is not in a category'}", color=random.choice(self.bot.color_list))
        embed.add_field(name="Channel Guild", value=ctx.guild.name, inline=False)
        embed.add_field(name="Channel Id", value=channel.id, inline=False)
        embed.add_field(name="Channel Topic", value=f"{channel.topic if channel.topic else 'No topic.'}", inline=False)
        embed.add_field(name="Channel Position", value=channel.position, inline=False)
        embed.add_field(name="Channel Slowmode Delay", value=channel.slowmode_delay, inline=False)
        embed.add_field(name="Channel is nsfw?", value=channel.is_nsfw(), inline=False)
        embed.add_field(name="Channel is news?", value=channel.is_news(), inline=False)
        embed.add_field(name="Channel Creation Time", value=channel.created_at, inline=False)
        embed.add_field(name="Channel Permissions Synced", value=channel.permissions_synced, inline=False)
        embed.add_field(name="Channel Hash", value=hash(channel), inline=False)

        await ctx.send(embed=embed)

def setup(bot):
    bot.add_cog(Channels(bot))


seg = eval(input("Escreva um número de segundos:\n"))

dias = 0
horas = 0
minutos = 0

while seg > dias * 86400:
    dias = dias + 1

seg = seg - (dias - 1) * 86400

while seg > horas * 3600:
    horas = horas + 1

seg = seg - (horas - 1) * 3600

while seg > minutos * 60:
    minutos = minutos + 1

seg = seg - minutos - 58

print("dias:", dias - 1, "horas:", horas - 1,"mins:", minutos - 1,"segundos:", seg)


import json
import boto3

codecommit_client = boto3.client('codecommit')
codepipeline_client = boto3.client('codepipeline')

non_essential_files = [
    "readme.md",
    "emr-master/views/layouts/main.handlebars",
]

def lambda_handler(event, context):
    print(event)
    old_commit_id = event["detail"]["oldCommitId"]
    new_commit_id = event["detail"]["commitId"]

    codecommit_response = codecommit_client.get_differences(
        repositoryName="EMR-REPO",
        beforeCommitSpecifier=str(old_commit_id),
        afterCommitSpecifier=str(new_commit_id)
    )

    print(codecommit_response["differences"])

    changes = len(codecommit_response["differences"])
    non_essential_changes = 0

    for difference in codecommit_response["differences"]:
        try:
            file_name = difference["afterBlob"]["path"].lower()

            if file_name in non_essential_files:
                non_essential_changes += 1

        except:
            pass

    if changes != non_essential_changes:
        print("PERFORMING TESTS")
        response = codepipeline_client.start_pipeline_execution(name='EMR-PIPELINE-TESTS')
    else:
        print("SKIPPING TESTS")
        response = codepipeline_client.start_pipeline_execution(name='EMR-PIPELINE-SKIP-TESTS')


#coding:utf8
'''

1、从raw AndroidManifest.xml文件或APK文件中获取暴露组件
2、从raw AndroidManifest.xml文件或APK文件中获取明文AndroidManifest.xml

'''
# 加密zip ChilkatZip

import bytecode

from axmlprinter import AXMLPrinter
from bytecode import SV

import zipfile, StringIO
from struct import pack, unpack
from xml.dom import minidom

class APKCook:
    def __init__(self, filename, single=False, text=False):

        self.filename = filename
        self.xml = {}
        self.package = ""
        self.androidversion = {}
        self.raw_manifest = ""

        fd = open(filename, "rb")
        self.__raw = fd.read()
        fd.close()

        if single:
            self.raw_manifest = self.__raw
        else:
            self.zip = zipfile.ZipFile(StringIO.StringIO(self.__raw))
            for i in self.zip.namelist():
                if i == "AndroidManifest.xml":
                    self.raw_manifest = self.zip.read(i)
        if text:
            self.xml = minidom.parseString(self.raw_manifest)
        else:
            self.xml = minidom.parseString(AXMLPrinter(self.raw_manifest).getBuff())

        self.package = self.xml.documentElement.getAttribute("package")
        self.androidversion["Code"] = self.xml.documentElement.getAttribute("android:versionCode")
        self.androidversion["Name"] = self.xml.documentElement.getAttribute("android:versionName")

    def get_package(self):
        return self.package

    def get_androidversion_code(self):
        return self.androidversion["Code"]

    def get_androidversion_name(self):
        return self.androidversion["Name"]

    def get_permission(self):
        out = []
        for item in self.xml.getElementsByTagName("permission"):
            name = item.getAttribute("android:name")
            pl = item.getAttribute("android:protectionLevel")
            if pl == "0x00000000":
                name += "@normal"
            elif pl == "0x00000001":
                name += "@dangerous"
            elif pl == "0x00000002":
                name += "@signature"
            elif pl == "0x00000003":
                name += "@signatureOrSystem"

            out.append(name)

        return out;

    def get_element(self, tag_name, attribute):
        for item in self.xml.getElementsByTagName(tag_name):
            value = item.getAttribute(attribute)

            if len(value) > 0:
                return value
        return None

    def get_min_sdk_version(self):
        return self.get_element("uses-sdk", "android:minSdkVersion")

    def get_target_sdk_version(self):
        return self.get_element("uses-sdk", "android:targetSdkVersion")

    def get_activities(self):
        out = []
        for item in self.xml.getElementsByTagName("activity"):
            exported = item.getAttribute("android:exported")
            name = ""
            if exported == "true":
                name = item.getAttribute("android:name")
            elif exported != "false":
                #未设置exported属性，则检查是否有intent-filter
                if len(item.getElementsByTagName("intent-filter")) > 0:
                    name = item.getAttribute("android:name")

            #未开启
            if item.getAttribute("android:enabled") == "false":
                if name != "":
                    name = "!disabled!"+name

            #要求权限
            if item.getAttribute("android:permission") != "":
                if name != "":
                    name += "@"+item.getAttribute("android:permission")

            if name != "":
                out.append(name)

        for item in self.xml.getElementsByTagName("activity-alias"):
            exported = item.getAttribute("android:exported")
            name = ""
            if exported == "true":
                name = item.getAttribute("android:name")
            elif exported != "false":
                #未设置exported属性，则检查是否有intent-filter
                if len(item.getElementsByTagName("intent-filter")) > 0:
                    name = item.getAttribute("android:name")

            #未开启
            if item.getAttribute("android:enabled") == "false":
                if name != "":
                    name = "!disabled!"+name

            #要求权限
            if item.getAttribute("android:permission") != "":
                if name != "":
                    name += "@"+item.getAttribute("android:permission")

            if name != "":
                out.append(name)

        return out

    def get_services(self):
        out = []
        for item in self.xml.getElementsByTagName("service"):
            exported = item.getAttribute("android:exported")
            name = ""
            if exported == "true":
                name = item.getAttribute("android:name")
            elif exported != "false":
                #未设置exported属性，则检查是否有intent-filter
                if len(item.getElementsByTagName("intent-filter")) > 0:
                    name = item.getAttribute("android:name")

            #未开启
            if item.getAttribute("android:enabled") == "false":
                if name != "":
                    name = "!disabled!"+name

            #要求权限
            if item.getAttribute("android:permission") != "":
                if name != "":
                    name += "@"+item.getAttribute("android:permission")

            if name != "":
                out.append(name)

        return out

    def get_receivers(self):
        out = []
        for item in self.xml.getElementsByTagName("receiver"):
            exported = item.getAttribute("android:exported")
            name = ""
            if exported == "true":
                name = item.getAttribute("android:name")
            elif exported != "false":
                #未设置exported属性，则检查是否有intent-filter
                if len(item.getElementsByTagName("intent-filter")) > 0:
                    name = item.getAttribute("android:name")

            #未开启
            if item.getAttribute("android:enabled") == "false":
                if name != "":
                    name = "!disabled!"+name

            #要求权限
            if item.getAttribute("android:permission") != "":
                if name != "":
                    name += "@"+item.getAttribute("android:permission")

            if name != "":
                out.append(name)

        return out

    def get_providers(self):
        out = []
        for item in self.xml.getElementsByTagName("provider"):
            exported = item.getAttribute("android:exported")
            name = ""
            if exported == "true":
                name = item.getAttribute("android:name")
            elif exported != "false":
                #未设置exported属性，则检查是否有intent-filter
                if len(item.getElementsByTagName("intent-filter")) > 0:
                    name = item.getAttribute("android:name")
            #####新增####临时访问权限
            elif item.getAttribute("android:grantUriPermissions") == "true":
                name = item.getAttribute("android:name")

            #未开启
            if item.getAttribute("android:enabled") == "false":
                if name != "":
                    name = "!disabled!"+name

            #要求权限
            if item.getAttribute("android:permission") != "":
                if name != "":
                    name += "@"+item.getAttribute("android:permission")

            if name != "":
                out.append(name)

        return out

    def show(self):
        print ("===暴露组件===(注意调用权限，动态recevier未检测)")
        print ("Package: "+self.get_package())
        print ("VersionName: "+self.androidversion["Name"]+" VersionCode: "+self.androidversion["Code"])
        print ("Min_sdk: "+self.get_min_sdk_version()+" Target_sdk: "+self.get_target_sdk_version())
        print ("==Activity:\n"+"\n".join(self.get_activities()))
        print ("==Service:\n"+"\n".join(self.get_services()))
        print ("==Receive:\n"+"\n".join(self.get_receivers()))
        print ("==Provider:\n"+"\n".join(self.get_providers()))
        print ("==Permission:\n"+"\n".join(self.get_permission()))

    def output(self):
        print(AXMLPrinter(self.raw_manifest).getBuff())

if __name__ == "__main__":
    apkcook = APKCook('../../oemapp/thememanager.apk')
    apkcook.show()

    # apkcook = APKCook('AndroidManifest.xml' ,True)
    # apkcook.show()


#!/usr/bin/python

# This is the topology used for the routing experiments

import sys

from mininet.log import setLogLevel, info
from mn_wifi.link import wmediumd, adhoc
from mn_wifi.cli import CLI_wifi
from mininet.node import Controller, RemoteController
from mn_wifi.net import Mininet_wifi
from mn_wifi.wmediumdConnector import interference

from positions import positions

def topology(autoTxPower):
    "Create a network."
    net = Mininet_wifi(link=wmediumd, wmediumd_mode=interference)

    info("*** Creating nodes\n")

    sta1 = net.addStation('sta1', mac='00:00:00:00:00:01', ip='10.0.0.1/8', antennaHeight='1', antennaGain='5', range=800)
    sta2 = net.addStation('sta2', mac='00:00:00:00:00:02', ip='10.0.0.2/8', antennaHeight='1', antennaGain='5', range=800)
    sta3 = net.addStation('sta3', mac='00:00:00:00:00:03', ip='10.0.0.3/8', antennaHeight='1', antennaGain='5', range=800)
    sta4 = net.addStation('sta4', mac='00:00:00:00:00:04', ip='10.0.0.4/8', antennaHeight='1', antennaGain='5', range=800)
    sta5 = net.addStation('sta5', mac='00:00:00:00:00:05', ip='10.0.0.5/8', antennaHeight='1', antennaGain='5', range=800)
    sta6 = net.addStation('sta6', mac='00:00:00:00:00:06', ip='10.0.0.6/8', antennaHeight='1', antennaGain='5', range=800)

    #ap1 = net.addAccessPoint('ap1', ssid='ap1', mode='a', channel='36', position='50,50,0')

    # c0 = net.addController(name='c0',
    #                   controller=InbandController,
    #                   ip='10.0.0.2',
    #                   protocol='tcp',
    #                   port=6653)

    net.setPropagationModel(model="logDistance", exp=5.5)

    info("*** Configuring wifi nodes\n")
    net.configureWifiNodes()

    info("*** Creating links\n")
    #MANET routing protocols supported by proto: olsr and batman
    net.addLink(sta1, cls=adhoc, ssid='adhocNet', proto='olsr',
                mode='g', channel=5, ht_cap='HT40+')
    net.addLink(sta2, cls=adhoc, ssid='adhocNet', proto='olsr',
                mode='g', channel=5, ht_cap='HT40+')
    net.addLink(sta3, cls=adhoc, ssid='adhocNet', proto='olsr',
                mode='g', channel=5, ht_cap='HT40+')
    net.addLink(sta4, cls=adhoc, ssid='adhocNet', proto='olsr',
                mode='g', channel=5, ht_cap='HT40+')
    net.addLink(sta5, cls=adhoc, ssid='adhocNet', proto='olsr',
                mode='g', channel=5, ht_cap='HT40+')
    net.addLink(sta6, cls=adhoc, ssid='adhocNet', proto='olsr',
                mode='g', channel=5, ht_cap='HT40+')

    #net.plotGraph(max_x = 4000, max_y = 2000)

    net.setMobilityModel(time=0, model='RandomWayPoint', max_x=4000, max_y=2000,min_v=5, max_v=20, seed=5)

    info("*** Starting network\n")
    net.build()

    info("*** Addressing...\n")
    sta1.setIPv6('2001::1/64', intf="sta1-wlan0")
    sta2.setIPv6('2001::2/64', intf="sta2-wlan0")
    sta3.setIPv6('2001::3/64', intf="sta3-wlan0")
    sta4.setIPv6('2001::4/64', intf="sta4-wlan0")
    sta5.setIPv6('2001::5/64', intf="sta5-wlan0")
    sta6.setIPv6('2001::6/64', intf="sta6-wlan0")

    nodes = net.stations

    positions(nodes,single=True)

    info("*** Running CLI\n")
    CLI_wifi(net)

    info("*** Stopping network\n")
    net.stop()

if __name__ == '__main__':
    setLogLevel('info')
    autoTxPower = True if '-a' in sys.argv else False
    topology(autoTxPower)


import numpy as np
import matplotlib.pyplot as plt

def kernel(x, i):
    return x**i

# データの読み込み
tmp = np.load("samples.npz")
x, y = tmp["arr_0"], tmp["arr_1"]

# sinカーブの描画
x_min, x_max = 0, 10
xx = np.linspace(x_min, x_max, 100)
tt = np.sin(xx)

# 計画行列の作成
p = x.shape[0]
ones = np.ones(p)
phi = np.c_[ones, kernel(x,1), kernel(x,2), kernel(x,3), kernel(x,4), kernel(x,5)]

# パラメータの計算

# tmp = [i*0.1 for i in range(100)]# +
tmp = [10**i for i in range(10, 50)]
for lambda_ in tmp:
    A = np.dot(phi.T, phi)
    I = np.eye(A.shape[0])
    w = np.dot(np.dot(np.linalg.inv(A+ lambda_ * I) , phi.T), y)
    w0, w1, w2, w3, w4, w5 = w

    xxx = np.linspace(0, 10, 100)
    yyy = w0 + w1 * kernel(xxx, 1) + w2 * kernel(xxx, 2) + w3 * kernel(xxx, 3) + w4 * kernel(xxx, 4) + w5 * kernel(xxx,5)
    plt.plot(xxx, yyy, color="red")
    plt.scatter(x, y, color="blue", marker="x")
    plt.plot(xx, tt, color="green")
    plt.title("lambda={}".format(lambda_))
    plt.xlim([0, 8])
    plt.ylim([-2, +2])

    plt.savefig("./img4/fitting_polynomial_{}_lambda_{}".format(p, str(lambda_).replace(".","_")))
    plt.close()
#    plt.show()


import tensorflow as tf
import tensorflow.contrib.slim as slim
import tensorflow.contrib.layers as layers

from ..ops import correlation
from .image_warp import image_warp
import numpy as np

from .flow_util import flow_to_color
from ..ops import backward_warp, forward_warp
from .losses import occlusion, DISOCC_THRESH, create_outgoing_mask

FLOW_SCALE = 5.0
POSE_SCALING = 0.001

def flownet(im1, im2, flownet_spec='S', full_resolution=False, train_all=False,
            backward_flow=False, pose_prediction = False):
    num_batch, height, width, _ = tf.unstack(tf.shape(im1))
    flownet_num = len(flownet_spec)
    assert flownet_num > 0
    flows_fw = []
    flows_bw = []
    flows2_fw = []
    flows2_bw = []
    poses_fw = []
    poses_bw = []
    for i, name in enumerate(flownet_spec):
        assert name in ('C', 'c', 'S', 's')
        channel_mult = 1 if name in ('C', 'S') else 3 / 8
        full_res = full_resolution and i == flownet_num - 1

        def scoped_block():
            if name.lower() == 'c':
                assert i == 0, 'FlowNetS must be used for refinement networks'

                with tf.variable_scope('flownet_c_features'):
                    _, conv2_a, conv3_a = flownet_c_features(im1, channel_mult=channel_mult)
                    _, conv2_b, conv3_b = flownet_c_features(im2, channel_mult=channel_mult, reuse=True)

                with tf.variable_scope('flownet_c') as scope:
                    results = flownet_c(conv3_a, conv3_b, conv2_a,
                                        full_res=full_res,
                                        pose_pred = pose_prediction,
                                        channel_mult=channel_mult)
                    flows_fw.append(results[0])
                    if pose_prediction:
                        flows2_fw.append(results[1])
                        poses_fw.append(results[2])
                    if backward_flow:
                        scope.reuse_variables()
                        results = flownet_c(conv3_b, conv3_a, conv2_b,
                                            full_res=full_res,
                                            pose_pred = pose_prediction,
                                            channel_mult=channel_mult)
                        flows_bw.append(results[0])
                        if pose_prediction:
                            flows2_bw.append(results[1])
                            poses_bw.append(results[2])

            elif name.lower() == 's':
                def _flownet_s(im1, im2, flow=None):
                    if flow is not None:
                        flow = tf.image.resize_bilinear(flow, [height, width]) * 4 * FLOW_SCALE
                        warp = image_warp(im2, flow)
                        diff = tf.abs(warp - im1)
                        if not train_all:
                            flow = tf.stop_gradient(flow)
                            warp = tf.stop_gradient(warp)
                            diff = tf.stop_gradient(diff)

                        inputs = tf.concat([im1, im2, flow, warp, diff], axis=3)
                        inputs = tf.reshape(inputs, [num_batch, height, width, 14])
                    else:
                        inputs = tf.concat([im1, im2], 3)
                    return flownet_s(inputs,
                                     full_res=full_res,
                                     pose_pred=pose_prediction,
                                     channel_mult=channel_mult)
                stacked = len(flows_fw) > 0
                with tf.variable_scope('flownet_s') as scope:
                    results = _flownet_s(im1, im2, flows_fw[-1][0] if stacked else None)
                    flows_fw.append(results[0])
                    if pose_prediction:
                        flows2_fw.append(results[1])
                        poses_fw.append(results[2])

                    if backward_flow:
                        scope.reuse_variables()
                        results = _flownet_s(im2, im1, flows_bw[-1][0]  if stacked else None)
                        flows_bw.append(results[0])
                        if pose_prediction:
                            flows2_bw.append(results[1])
                            poses_bw.append(results[2])
        if i > 0:
            scope_name = "stack_{}_flownet".format(i)
            with tf.variable_scope(scope_name):
                scoped_block()
        else:
            scoped_block()

    if backward_flow :
        return flows_fw, flows_bw, flows2_fw, flows2_bw, poses_fw, poses_bw
    return flows_fw, flows2_fw, poses_fw

def _leaky_relu(x):
    with tf.variable_scope('leaky_relu'):
        return tf.maximum(0.1 * x, x)

def _flownet_upconv(conv6_1, conv5_1, conv4_1, conv3_1, conv2, conv1=None, inputs=None,
                    channel_mult=1, full_res=False, pose_pred=False, channels=2, channels_p=9):
    m = channel_mult

    flow6 = slim.conv2d(conv6_1, channels, 3, scope='flow6',
                        activation_fn=None)
    deconv5 = slim.conv2d_transpose(conv6_1, int(512 * m), 4, stride=2,
                                   scope='deconv5')
    flow6_up5 = slim.conv2d_transpose(flow6, channels, 4, stride=2,
                                     scope='flow6_up5',
                                     activation_fn=None)
    concat5 = tf.concat([conv5_1, deconv5, flow6_up5], 1)
    flow5 = slim.conv2d(concat5, channels, 3, scope='flow5',
                       activation_fn=None)

    deconv4 = slim.conv2d_transpose(concat5, int(256 * m), 4, stride=2,
                                   scope='deconv4')
    flow5_up4 = slim.conv2d_transpose(flow5, channels, 4, stride=2,
                                     scope='flow5_up4',
                                     activation_fn=None)
    concat4 = tf.concat([conv4_1, deconv4, flow5_up4], 1)
    flow4 = slim.conv2d(concat4, channels, 3, scope='flow4',
                       activation_fn=None)

    deconv3 = slim.conv2d_transpose(concat4, int(128 * m), 4, stride=2,
                                   scope='deconv3')
    flow4_up3 = slim.conv2d_transpose(flow4, channels, 4, stride=2,
                                     scope='flow4_up3',
                                     activation_fn=None)
    concat3 = tf.concat([conv3_1, deconv3, flow4_up3], 1)
    flow3 = slim.conv2d(concat3, channels, 3, scope='flow3',
                       activation_fn=None)

    deconv2 = slim.conv2d_transpose(concat3, int(64 * m), 4, stride=2,
                                   scope='deconv2')
    flow3_up2 = slim.conv2d_transpose(flow3, channels, 4, stride=2,
                                     scope='flow3_up2',
                                     activation_fn=None)
    concat2 = tf.concat([conv2, deconv2, flow3_up2], 1)
    flow2 = slim.conv2d(concat2, channels, 3, scope='flow2',
                       activation_fn=None)

    if full_res:
        with tf.variable_scope('full_res'):
            deconv1 = slim.conv2d_transpose(concat2, int(32 * m), 4, stride=2,
                                           scope='deconv1')
            flow2_up1 = slim.conv2d_transpose(flow2, channels, 4, stride=2,
                                             scope='flow2_up1',
                                             activation_fn=None)
            concat1 = tf.concat([conv1, deconv1, flow2_up1], 1)
            flow1 = slim.conv2d(concat1, channels, 3, scope='flow1',
                                activation_fn=None)

            deconv0 = slim.conv2d_transpose(concat1, int(16 * m), 4, stride=2,
                                           scope='deconv0')
            flow1_up0 = slim.conv2d_transpose(flow1, channels, 4, stride=2,
                                             scope='flow1_up0',
                                             activation_fn=None)
            concat0 = tf.concat([inputs, deconv0, flow1_up0], 1)
            flow0 = slim.conv2d(concat0, channels, 3, scope='flow0',
                                activation_fn=None)

    if pose_pred:
        def _meshgrid(batch, height, width):
            with tf.variable_scope('_meshgrid'):
                # This should be equivalent to:
                #  x_t, y_t = np.meshgrid(np.linspace(-1, 1, width),
                #                         np.linspace(-1, 1, height))
                #  ones = np.ones(np.prod(x_t.shape))
                #  grid = np.vstack([x_t.flatten(), y_t.flatten(), ones])
                x_t = tf.matmul(tf.ones(shape=tf.stack([height, 1])),
                                tf.transpose(tf.expand_dims(tf.linspace(-1.0, 1.0, width), 1), [1, 0]))
                y_t = tf.matmul(tf.expand_dims(tf.linspace(-1.0, 1.0, height), 1),
                                tf.ones(shape=tf.stack([1, width])))
                grid = tf.expand_dims(tf.stack(axis=0, values=[x_t, y_t]), 0)
                grid = tf.tile(grid, tf.stack([batch, 1, 1, 1]))
                return grid

        with tf.variable_scope('pose_pred'):
            b, _, h2, w2 = tf.unstack(tf.shape(flow2))
            meshgrid2 = _meshgrid(b, h2, w2)
            b, _, h3, w3 = tf.unstack(tf.shape(flow3))
            meshgrid3 = _meshgrid(b, h3, w3)
            b, _, h4, w4 = tf.unstack(tf.shape(flow4))
            meshgrid4 = _meshgrid(b, h4, w4)
            b, _, h5, w5 = tf.unstack(tf.shape(flow5))
            meshgrid5 = _meshgrid(b, h5, w5)
            b, _, h6, w6 = tf.unstack(tf.shape(flow6))
            meshgrid6 = _meshgrid(b, h6, w6)

            if full_res:
                b, _, h, w = tf.unstack(tf.shape(flow0))
                meshgrid0 = _meshgrid(b, h, w)
                b, _, h, w = tf.unstack(tf.shape(flow1))
                meshgrid1 = _meshgrid(b, h, w)

                concat0 = tf.concat([flow0, meshgrid0], 1)
                cnv0_1 = slim.conv2d(concat0, 16, [3, 3], stride=1, scope='cnv0_1', activation_fn=_leaky_relu)
                cnv0_2 = slim.conv2d(cnv0_1, 32, [3, 3], stride=1, scope='cnv0_2', activation_fn=None)
                cnv0_3 = slim.conv2d(cnv0_2, 64, [3, 3], stride=1, scope='cnv0_3', activation_fn=None)
                pose0 = slim.conv2d(cnv0_3, channels_p, 3, scope='pose0',
                                    activation_fn=None)
                concat0_p = tf.concat([pose0, meshgrid0], 1)
                cnvp0_2 = slim.conv2d(concat0_p, 32, [3, 3], stride=1, scope='cnvp0_2', activation_fn=None)
                cnvp0_3 = slim.conv2d(cnvp0_2, 64, [3, 3], stride=1, scope='cnvp0_3', activation_fn=None)
                flow0_2 = slim.conv2d(cnvp0_3, channels, 1, scope='flow0', activation_fn=None)

                pose0_dwn1 = slim.conv2d(pose0, channels_p, [3, 3], stride=2, scope='pose0_dwn1', activation_fn=None)
                flow0_dwn1 = slim.conv2d(flow0_2, channels, [3, 3], stride=2, scope='flow0_dwn1', activation_fn=None)
                concat1 = tf.concat([pose0_dwn1, flow0_dwn1, flow1, meshgrid1], 1)
                cnv1_2 = slim.conv2d(concat1, 32, [3, 3], stride=1, scope='cnv1_2', activation_fn=None)
                cnv1_3 = slim.conv2d(cnv1_2, 64, [3, 3], stride=1, scope='cnv1_3', activation_fn=None)
                pose1 = slim.conv2d(cnv1_3, channels_p, 3, scope='pose1',
                                    activation_fn=None)
                concat1_p = tf.concat([pose1, meshgrid1], 1)
                cnvp1_2 = slim.conv2d(concat1_p, 32, [3, 3], stride=1, scope='cnvp1_2', activation_fn=None)
                cnvp1_3 = slim.conv2d(cnvp1_2, 64, [3, 3], stride=1, scope='cnvp1_3', activation_fn=None)
                flow1_2 = slim.conv2d(cnvp1_3, channels, 1, scope='flow1', activation_fn=None)

                pose1_dwn2 = slim.conv2d(pose1, channels_p, [3, 3], stride=2, scope='pose1_dwn2', activation_fn=None)
                flow1_dwn2 = slim.conv2d(flow1_2, channels, [3, 3], stride=2, scope='flow1_dwn2', activation_fn=None)
                concat2 = tf.concat([pose1_dwn2, flow1_dwn2, flow2, meshgrid2], 1)
            else:
                concat2 = tf.concat([flow2, meshgrid2], 1)

            cnv2_1 = slim.conv2d(concat2, 16, [3, 3], stride=1, scope='cnv2_1', activation_fn=_leaky_relu)
            cnv2_2 = slim.conv2d(cnv2_1, 32, [3, 3], stride=1, scope='cnv2_2', activation_fn=None)
            cnv2_3 = slim.conv2d(cnv2_2, 64, [3, 3], stride=1, scope='cnv2_3', activation_fn=None)
            pose2 = slim.conv2d(cnv2_3, channels_p, 3, scope='pose2',
                                activation_fn=None)
            concat2_p = tf.concat([pose2, meshgrid2], 1)
            cnvp2_2 = slim.conv2d(concat2_p, 32, [3, 3], stride=1, scope='cnvp2_2', activation_fn=None)
            cnvp2_3 = slim.conv2d(cnvp2_2, 64, [3, 3], stride=1, scope='cnvp2_3', activation_fn=None)
            flow2_2 = slim.conv2d(cnvp2_3, channels, 1, scope='flow2',
                                activation_fn=None)

            cnv3 = slim.conv2d(cnv2_3, 16, [3, 3], stride=2, scope='cnv3', activation_fn=_leaky_relu)
            concat3 = tf.concat([cnv3, flow3, meshgrid3], 1)
            cnv3_2 = slim.conv2d(concat3, 32, [3, 3], stride=1, scope='cnv3_2', activation_fn=None)
            cnv3_3 = slim.conv2d(cnv3_2, 64, [3, 3], stride=1, scope='cnv3_3', activation_fn=None)
            pose3 = slim.conv2d(cnv3_3, channels_p, 3, scope='pose3',
                                activation_fn=None)
            concat3_p = tf.concat([pose3, meshgrid3], 1)
            cnvp3_2 = slim.conv2d(concat3_p, 32, [3, 3], stride=1, scope='cnvp3_2', activation_fn=None)
            cnvp3_3 = slim.conv2d(cnvp3_2, 64, [3, 3], stride=1, scope='cnvp3_3', activation_fn=None)
            flow3_2 = slim.conv2d(cnvp3_3, channels, 1, scope='flow3',
                                activation_fn=None)

            cnv4 = slim.conv2d(cnv3_3, 16, [3, 3], stride=2, scope='cnv4', activation_fn=_leaky_relu)
            concat4 = tf.concat([cnv4, flow4, meshgrid4], 1)
            cnv4_2 = slim.conv2d(concat4, 32, [3, 3], stride=1, scope='cnv4_2', activation_fn=None)
            cnv4_3 = slim.conv2d(cnv4_2, 64, [3, 3], stride=1, scope='cnv4_3', activation_fn=None)
            pose4 = slim.conv2d(cnv4_3, channels_p, 3, scope='pose4',
                                activation_fn=None)
            concat4_p = tf.concat([pose4, meshgrid4], 1)
            cnvp4_2 = slim.conv2d(concat4_p, 32, [3, 3], stride=1, scope='cnvp4_2', activation_fn=None)
            cnvp4_3 = slim.conv2d(cnvp4_2, 64, [3, 3], stride=1, scope='cnvp4_3', activation_fn=None)
            flow4_2 = slim.conv2d(cnvp4_3, channels, 1, scope='flow4',
                                activation_fn=None)

            cnv5 = slim.conv2d(cnv4_3, 16, [3, 3], stride=2, scope='cnv5', activation_fn=_leaky_relu)
            concat5 = tf.concat([cnv5, flow5, meshgrid5], 1)
            cnv5_2 = slim.conv2d(concat5, 32, [3, 3], stride=1, scope='cnv5_2', activation_fn=None)
            cnv5_3 = slim.conv2d(cnv5_2, 64, [3, 3], stride=1, scope='cnv5_3', activation_fn=None)
            pose5 = slim.conv2d(cnv5_3, channels_p, 3, scope='pose5',
                                activation_fn=None)
            concat5_p = tf.concat([pose5, meshgrid5], 1)
            cnvp5_2 = slim.conv2d(concat5_p, 32, [3, 3], stride=1, scope='cnvp5_2', activation_fn=None)
            cnvp5_3 = slim.conv2d(cnvp5_2, 64, [3, 3], stride=1, scope='cnvp5_3', activation_fn=None)
            flow5_2 = slim.conv2d(cnvp5_3, channels, 1, scope='flow5',
                                activation_fn=None)

            cnv6 = slim.conv2d(cnv5_3, 16, [3, 3], stride=2, scope='cnv6', activation_fn=_leaky_relu)
            concat6 = tf.concat([cnv6, flow6, meshgrid6], 1)
            cnv6_2 = slim.conv2d(concat6, 32, [3, 3], stride=1, scope='cnv6_2', activation_fn=None)
            cnv6_3 = slim.conv2d(cnv6_2, 64, [3, 3], stride=1, scope='cnv6_3', activation_fn=None)
            pose6 = slim.conv2d(cnv6_3, channels_p, 3, scope='pose6',
                                activation_fn=None)
            concat6_p = tf.concat([pose6, meshgrid6], 1)
            cnvp6_2 = slim.conv2d(concat6_p, 32, [3, 3], stride=1, scope='cnvp6_2', activation_fn=None)
            cnvp6_3 = slim.conv2d(cnvp6_2, 64, [3, 3], stride=1, scope='cnvp6_3', activation_fn=None)
            flow6_2 = slim.conv2d(cnvp6_3, channels, 1, scope='flow6',
                                activation_fn=None)

    flows = [flow2, flow3, flow4, flow5, flow6]
    if full_res:
            flows = [flow0, flow1] + flows
    if pose_pred:
        poses = [pose2, pose3, pose4, pose5, pose6]
        flows2 = [flow2_2, flow3_2, flow4_2, flow5_2, flow6_2]
        if full_res:
            poses = [pose0, pose1] + poses
            flows2 = [flow0_2, flow1_2] + poses
        return flows2, flows, poses
    return flows

def nhwc_to_nchw(tensors):
    return [tf.transpose(t, [0, 3, 1, 2]) for t in tensors]

def nchw_to_nhwc(tensors):
    return [tf.transpose(t, [0, 2, 3, 1]) for t in tensors]

def flownet_s(inputs, channel_mult=1, full_res=False, pose_pred=False):
    """Given stacked inputs, returns flow predictions in decreasing resolution.

    Uses FlowNetSimple.
    """
    m = channel_mult
    inputs = nhwc_to_nchw([inputs])[0]

    with slim.arg_scope([slim.conv2d, slim.conv2d_transpose],
                        data_format='NCHW',
                        weights_regularizer=slim.l2_regularizer(0.0004),
                        weights_initializer=layers.variance_scaling_initializer(),
                        activation_fn=_leaky_relu):
        conv1 = slim.conv2d(inputs, int(64 * m), 7, stride=2, scope='conv1')
        conv2 = slim.conv2d(conv1, int(128 * m), 5, stride=2, scope='conv2')
        conv3 = slim.conv2d(conv2, int(256 * m), 5, stride=2, scope='conv3')
        conv3_1 = slim.conv2d(conv3, int(256 * m), 3, stride=1, scope='conv3_1')
        conv4 = slim.conv2d(conv3_1, int(512 * m), 3, stride=2, scope='conv4')
        conv4_1 = slim.conv2d(conv4, int(512 * m), 3, stride=1, scope='conv4_1')
        conv5 = slim.conv2d(conv4_1, int(512 * m), 3, stride=2, scope='conv5')
        conv5_1 = slim.conv2d(conv5, int(512 * m), 3, stride=1, scope='conv5_1')
        conv6 = slim.conv2d(conv5_1, int(1024 * m), 3, stride=2, scope='conv6')
        conv6_1 = slim.conv2d(conv6, int(1024 * m), 3, stride=1, scope='conv6_1')

        if pose_pred:
            flows1, flows2, poses = _flownet_upconv(conv6_1, conv5_1, conv4_1, conv3_1, conv2, conv1, inputs,
                                  channel_mult=channel_mult, full_res=full_res, pose_pred=pose_pred)
            return [nchw_to_nhwc(flows1), nchw_to_nhwc(flows2), nchw_to_nhwc(poses)]
        else:
            res = _flownet_upconv(conv6_1, conv5_1, conv4_1, conv3_1, conv2, conv1, inputs,
                                  channel_mult=channel_mult, full_res=full_res)
            return [nchw_to_nhwc(res)]

def flownet_c_features(im, channel_mult=1, reuse=None):
    m = channel_mult
    im = nhwc_to_nchw([im])[0]
    with slim.arg_scope([slim.conv2d],
                        data_format='NCHW',
                        weights_regularizer=slim.l2_regularizer(0.0004),
                        weights_initializer=layers.variance_scaling_initializer(),
                        activation_fn=_leaky_relu):
        conv1 = slim.conv2d(im, int(64 * m), 7, stride=2, scope='conv1', reuse=reuse)
        conv2 = slim.conv2d(conv1, int(128 * m), 5, stride=2, scope='conv2', reuse=reuse)
        conv3 = slim.conv2d(conv2, int(256 * m), 5, stride=2, scope='conv3', reuse=reuse)
        return conv1, conv2, conv3

def flownet_c(conv3_a, conv3_b, conv2_a, channel_mult=1, full_res=False, pose_pred=False):
    """Given two images, returns flow predictions in decreasing resolution.

    Uses FlowNetCorr.
    """
    m = channel_mult

    with slim.arg_scope([slim.conv2d, slim.conv2d_transpose],
                        data_format='NCHW',
                        weights_regularizer=slim.l2_regularizer(0.0004),
                        weights_initializer=layers.variance_scaling_initializer(),
                        activation_fn=_leaky_relu):
        corr = correlation(conv3_a, conv3_b,
                           pad=20, kernel_size=1, max_displacement=20, stride_1=1, stride_2=2)

        conv_redir = slim.conv2d(conv3_a, int(32 * m), 1, stride=1, scope='conv_redir')

        conv3_1 = slim.conv2d(tf.concat([conv_redir, corr], 1), int(256 * m), 3,
                              stride=1, scope='conv3_1')
        conv4 = slim.conv2d(conv3_1, int(512 * m), 3, stride=2, scope='conv4')
        conv4_1 = slim.conv2d(conv4, int(512 * m), 3, stride=1, scope='conv4_1')
        conv5 = slim.conv2d(conv4_1, int(512 * m), 3, stride=2, scope='conv5')
        conv5_1 = slim.conv2d(conv5, int(512 * m), 3, stride=1, scope='conv5_1')
        conv6 = slim.conv2d(conv5_1, int(1024 * m), 3, stride=2, scope='conv6')
        conv6_1 = slim.conv2d(conv6, int(1024 * m), 3, stride=1, scope='conv6_1')

        if pose_pred:
            flows1, flows2, poses = _flownet_upconv(conv6_1, conv5_1, conv4_1, conv3_1, conv2_a,
                                  channel_mult=channel_mult, full_res=full_res, pose_pred=pose_pred)
            return [nchw_to_nhwc(flows1), nchw_to_nhwc(flows2), nchw_to_nhwc(poses)]
        else:
            flows = _flownet_upconv(conv6_1, conv5_1, conv4_1, conv3_1, conv2_a,
                                  channel_mult=channel_mult, full_res=full_res, pose_pred=pose_pred)
            return [nchw_to_nhwc(flows)]


# lifted from https://deepctr-torch.readthedocs.io/en/latest/Quick-Start.html for quick testing
import pandas as pd
import torch

from sklearn.metrics import log_loss, roc_auc_score
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder, MinMaxScaler

from deepctr_torch.inputs import SparseFeat, DenseFeat, get_feature_names
from deepctr_torch.models import DeepFM
data = pd.read_csv('data/criteo_sample.txt')

sparse_features = ['C' + str(i) for i in range(1, 27)]
dense_features = ['I' + str(i) for i in range(1, 14)]

# filling in empty fields
data[sparse_features] = data[sparse_features].fillna('-1', )
data[dense_features] = data[dense_features].fillna(0, )
target = ['label']

# simple pre-processing
for feat in sparse_features:
    lbe = LabelEncoder()
    data[feat] = lbe.fit_transform(data[feat])

fixlen_feature_columns = [SparseFeat(feat, vocabulary_size=data[feat].nunique(),embedding_dim=4)
                       for i,feat in enumerate(sparse_features)] + [DenseFeat(feat, 1,)
                      for feat in dense_features]

mms = MinMaxScaler(feature_range=(0, 1))
data[dense_features] = mms.fit_transform(data[dense_features])

dnn_feature_columns = fixlen_feature_columns
linear_feature_columns = fixlen_feature_columns

feature_names = get_feature_names(linear_feature_columns + dnn_feature_columns)

train, test = train_test_split(data, test_size=0.2)

train_model_input = {name:train[name] for name in feature_names}

test_model_input = {name:test[name] for name in feature_names}

device = 'cpu'
use_cuda = True
if use_cuda and torch.cuda.is_available():
    print('cuda ready...')
    device = 'cuda:0'
model = DeepFM(linear_feature_columns,dnn_feature_columns,task='binary',device=device)
model.compile("adam", "binary_crossentropy",
              metrics=['binary_crossentropy'], )

history = model.fit(train_model_input,train[target].values,batch_size=256,epochs=10,verbose=2,validation_split=0.2)
pred_ans = model.predict(test_model_input, batch_size=256)

print("")
print("test LogLoss", round(log_loss(test[target].values, pred_ans), 4))
print("test AUC", round(roc_auc_score(test[target].values, pred_ans), 4))


'''
Name of file:  Relationship
Author: Jun Yang, Yingxin Liang, Yan Pan, Puxing Zhao, Shaoqing Zhang
Purpose: This module is to scrape data of hero relationship via json data
Module imports it: main
'''
import json
import requests

def Relationship():
    #define the header for the user request
    headers = {
        'user-agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'
    }

    url = 'https://graphics.straitstimes.com/STI/STIMEDIA/Interactives/2018/04/marvel-cinematic-universe-whos-who-interactive/data/marvel-data.json'
    response = requests.get(url=url, headers=headers) #get the response from the website
    result = json.loads(response.text)

    num = 0
    names = []
    item = {0: 'friend', 1: 'enemy', 2: 'creation', 3: 'family', 4: 'work', 5: 'love'} #create dictionary for analysis

    #Collect the data based on json data
    #Use key to locate the value
    for i in result['relationship']:
        #extract data by using the key
        subject = result['relationship'][i]['id']
        object = result['relationship'][i]['target_id']

        #create the name list of all characters
        if subject not in names:
            names.append(subject)
        if object not in names:
            names.append(object)

        #create a file to store all relationship for all characters
        relation = int(result['relationship'][i]['relationship'])
        with open('relation_message.csv', 'a+') as f:
            f.write(subject + ',' + object + ',' + item[relation] + '\n')

    #create a file to store ID and character names
    for j in names:
        num += 1
        with open('names_message.csv', 'a+') as f:
            f.write(j + ',' + str(num) + '\n')

    #create a file to store id, name,status and species for all characters
    for k in result['characters']:
        id = result['characters'][k]['id']
        name = result['characters'][k]['name']
        status = result['characters'][k]['status']
        species = result['characters'][k]['species']
        with open('message.csv', 'a+') as f:
            f.write(id + ',' + name + ',' + status + ',' + species + '\n')

    print("The data has been stored in the files.")


from odoo import api, fields, models
from odoo.tools import get_lang
import time
from datetime import datetime
from dateutil.relativedelta import relativedelta
from odoo import api, fields, models, _
from odoo.exceptions import UserError
from odoo.tools import get_lang
import base64
from odoo import fields, models, api
from io import BytesIO
from odoo.tools import xlsxwriter
from datetime import datetime
from pytz import timezone
import pytz

class AccountingReport(models.TransientModel):
    _name = "accounting.report"
    _inherit = "account.common.report"
    _description = "Accounting Report"

    @api.model
    def _get_account_report(self):
        reports = []
        if self._context.get('active_id'):
            menu = self.env['ir.ui.menu'].browse(self._context.get('active_id')).name
            reports = self.env['account.financial.report'].search([('name', 'ilike', menu)])
        return reports and reports[0] or False

    enable_filter = fields.Boolean(string='Enable Comparison')
    account_report_id = fields.Many2one('account.financial.report', string='Account Reports', required=True, default=_get_account_report)
    label_filter = fields.Char(string='Column Label', help="This label will be displayed on report to show the balance computed for the given comparison filter.")
    filter_cmp = fields.Selection([('filter_no', 'No Filters'), ('filter_date', 'Date')], string='Filter by', required=True, default='filter_no')
    date_from_cmp = fields.Date(string='Start Date')
    date_to_cmp = fields.Date(string='End Date')
    debit_credit = fields.Boolean(string='Display Debit/Credit Columns', help="This option allows you to get more details about the way your balances are computed. Because it is space consuming, we do not allow to use it while doing a comparison.")
    datas = fields.Binary('File', readonly=True)
    datas_fname = fields.Char('Filename', readonly=True)

    def _build_comparison_context(self, data):
        result = {}
        result['journal_ids'] = 'journal_ids' in data['form'] and data['form']['journal_ids'] or False
        result['state'] = 'target_move' in data['form'] and data['form']['target_move'] or ''
        if data['form']['filter_cmp'] == 'filter_date':
            result['date_from'] = data['form']['date_from_cmp']
            result['date_to'] = data['form']['date_to_cmp']
            result['strict_range'] = True
        return result

    def _compute_account_balance(self, accounts,company_id):
        print('yes here checked 2')

        """ compute the balance, debit and credit for the provided accounts
        """
        print(self)
        print(accounts)
        mapping = {
            'balance': "COALESCE(SUM(debit),0) - COALESCE(SUM(credit), 0) as balance",
            'debit': "COALESCE(SUM(debit), 0) as debit",
            'credit': "COALESCE(SUM(credit), 0) as credit",
        }

        res = {}
        for account in accounts:
            res[account.id] = dict.fromkeys(mapping, 0.0)
        if accounts:
            tables, where_clause, where_params = self.env['account.move.line']._query_get()
            tables = tables.replace('"', '') if tables else "account_move_line"
            wheres = [""]
            if where_clause.strip():
                wheres.append(where_clause.strip())
            filters = " AND ".join(wheres)
            request = "SELECT account_id as id, " + ', '.join(mapping.values()) + \
                       " FROM " + tables + \
                       " WHERE account_move_line.company_id="\
                            + str(company_id) +\
                          " AND account_id IN %s " \
                            + filters + \
                       " GROUP BY account_id"
            params = (tuple(accounts._ids),) + tuple(where_params)
            self.env.cr.execute(request, params)
            for row in self.env.cr.dictfetchall():
                res[row['id']] = row
        return res

    def _compute_report_balance(self, reports,company_id):
        print('yes here checked 3')

        '''returns a dictionary with key=the ID of a record and value=the credit, debit and balance amount
           computed for this record. If the record is of type :
               'accounts' : it's the sum of the linked accounts
               'account_type' : it's the sum of leaf accoutns with such an account_type
               'account_report' : it's the amount of the related report
               'sum' : it's the sum of the children of this record (aka a 'view' record)'''
        res = {}
        fields = ['credit', 'debit', 'balance']
        for report in reports:
            if report.id in res:
                continue
            res[report.id] = dict((fn, 0.0) for fn in fields)
            if report.type == 'accounts':
                # it's the sum of the linked accounts
                res[report.id]['account'] = self._compute_account_balance(report.account_ids,company_id)
                for value in res[report.id]['account'].values():
                    for field in fields:
                        res[report.id][field] += value.get(field)
            elif report.type == 'account_type':
                # it's the sum the leaf accounts with such an account type
                accounts = self.env['account.account'].search([('user_type_id', 'in', report.account_type_ids.ids)])
                res[report.id]['account'] = self._compute_account_balance(accounts,company_id)
                for value in res[report.id]['account'].values():
                    for field in fields:
                        res[report.id][field] += value.get(field)
            elif report.type == 'account_report' and report.account_report_id:
                # it's the amount of the linked report
                res2 = self._compute_report_balance(report.account_report_id,company_id)
                for key, value in res2.items():
                    for field in fields:
                        res[report.id][field] += value[field]
            elif report.type == 'sum':
                # it's the sum of the children of this account.report
                res2 = self._compute_report_balance(report.children_ids,company_id)
                for key, value in res2.items():
                    for field in fields:
                        res[report.id][field] += value[field]
        return res

    def get_account_lines(self, data):
        print('yes here checked 2')
        company_id =data["company_id"][0]
        lines = []
        account_report = self.env['account.financial.report'].search([('id', '=', data['account_report_id'][0])])
        print('account_report is',account_report)
        child_reports = account_report._get_children_by_order()
        res = self.with_context(data.get('used_context'))._compute_report_balance(child_reports,company_id)
        if data['enable_filter']:
            comparison_res = self.with_context(data.get('comparison_context'))._compute_report_balance(child_reports,company_id)
            for report_id, value in comparison_res.items():
                res[report_id]['comp_bal'] = value['balance']
                report_acc = res[report_id].get('account')
                if report_acc:
                    for account_id, val in comparison_res[report_id].get('account').items():
                        report_acc[account_id]['comp_bal'] = val['balance']

        for report in child_reports:
            vals = {
                'name': report.name,
                'balance': res[report.id]['balance'] * float(report.sign) or 0.0,
                'type': 'report',
                'level': bool(report.style_overwrite) and report.style_overwrite or report.level,
                'account_type': report.type or False, #used to underline the financial report balances
            }
            if data['debit_credit']:
                vals['debit'] = res[report.id]['debit']
                vals['credit'] = res[report.id]['credit']

            if data['enable_filter']:
                vals['balance_cmp'] = res[report.id]['comp_bal'] * float(report.sign)

            lines.append(vals)
            if report.display_detail == 'no_detail':
                #the rest of the loop is used to display the details of the financial report, so it's not needed here.
                continue

            if res[report.id].get('account'):
                sub_lines = []
                for account_id, value in res[report.id]['account'].items():
                    #if there are accounts to display, we add them to the lines with a level equals to their level in
                    #the COA + 1 (to avoid having them with a too low level that would conflicts with the level of data
                    #financial reports for Assets, liabilities...)
                    flag = False
                    account = self.env['account.account'].browse(account_id)
                    vals = {
                        'name': account.code + ' ' + account.name,
                        'balance': value['balance'] * float(report.sign) or 0,
                        'type': 'account',
                        'level': report.display_detail == 'detail_with_hierarchy' and 4,
                        'account_type': account.internal_type,
                    }
                    if data['debit_credit']:
                        vals['debit'] = value['debit']
                        vals['credit'] = value['credit']
                        if not account.company_id.currency_id.is_zero(vals['debit']) or not account.company_id.currency_id.is_zero(vals['credit']):
                            flag = True
                    if not account.company_id.currency_id.is_zero(vals['balance']):
                        flag = True
                    if data['enable_filter']:
                        vals['balance_cmp'] = value['comp_bal'] * float(report.sign)
                        if not account.company_id.currency_id.is_zero(vals['balance_cmp']):
                            flag = True
                    if flag:
                        sub_lines.append(vals)
                lines += sorted(sub_lines, key=lambda sub_line: sub_line['name'])
        return lines

    # def check_report(self):
    #     res = super(AccountingReport, self).check_report()
    #     data = {}
    #     data['form'] = self.read(['account_report_id', 'date_from_cmp', 'date_to_cmp', 'journal_ids', 'filter_cmp', 'target_move'])[0]
    #     for field in ['account_report_id']:
    #         if isinstance(data['form'][field], tuple):
    #             data['form'][field] = data['form'][field][0]
    #     comparison_context = self._build_comparison_context(data)
    #     res['data']['form']['comparison_context'] = comparison_context
    #     return res

    def _print_report(self, data):
        data['form'].update(self.read(['date_from_cmp', 'debit_credit', 'date_to_cmp', 'filter_cmp', 'account_report_id', 'enable_filter', 'label_filter', 'target_move'])[0])
        return self.env.ref('gts_financial_pdf_report.action_report_financial').report_action(self, data=data, config=False)

    def print_excel_report(self):
        self.ensure_one()
        data = {}
        data['ids'] = self.env.context.get('active_ids', [])
        data['model'] = self.env.context.get('active_model', 'ir.ui.menu')
        data['form'] = self.read(['date_from', 'date_to', 'journal_ids', 'target_move', 'company_id'])[0]
        used_context = self._build_contexts(data) # build_context is a function of parent class,so you can browse this
        data['form']['used_context'] = dict(used_context, lang=get_lang(self.env).code)
        # return self.with_context(discard_logo_check=True)._print_report(data)
        print('The ultimate data is', data)
        print('Yes here checked')
        print('checked checked')

        data['form'].update(self.read(['date_from_cmp', 'debit_credit', 'date_to_cmp', 'filter_cmp', 'account_report_id', 'enable_filter', 'label_filter', 'target_move'])[0])
        print('the last data is',data)
        print('excel report')
        # self.model = self.env.context.get('active_model')
        # docs = self.env[self.model].browse(self.env.context.get('active_id'))
        # print('docs is',docs)
        report_lines = self.get_account_lines(data.get('form')) # 'get_account_lines' is a function of abstract model, in PDF it call into abstract model but in excel report it can't, for that reason we have to write all function of abstract model inside the transient model class
        print('report lines is', report_lines)
        # return {
        #     'doc_ids': self.ids,
        #     'doc_model': self.model,
        #     'data': data['form'],
        #     'docs': docs,
        #     'time': time,
        #     'get_account_lines': report_lines,
        # }

        report_name = data['form']['account_report_id'][1]
        filename = '%s' % (report_name)

        fp = BytesIO()
        workbook = xlsxwriter.Workbook(fp)
        wbf, workbook = self.add_workbook_format(workbook)
        worksheet = workbook.add_worksheet(report_name)
        worksheet.merge_range('A2:I3', report_name, wbf['title_doc'])

        # column_product = [
        #     ('Partners',24,'char','char'),
        #     ('Not due',12,'float','float'),
        #     ('0-30',13,'float','float'),
        #     ('30-60',13,'float','float'),
        #     ('60-90',14,'float','float'),
        #     ('90-120',14,'float','float'),
        #     ('+120',10,'float','float'),
        #     ('Total',17,'float','float')
        # ]
        row = 5
        worksheet.merge_range('A%s:B%s' % (row - 1, row - 1), 'Target Moves', wbf['header_orange'])
        # worksheet.merge_range('D%s:E%s' % (row - 1, row - 1), 'Period Length(days)', wbf['header_orange'])
        row += 1
        if data['form']['target_move'] == 'all':
            worksheet.merge_range('A%s:B%s' % (row - 1, row - 1), 'All Entries',wbf['header_detail'])
        if data['form']['target_move'] == 'posted':
            worksheet.merge_range('A%s:B%s' % (row - 1, row - 1), 'All Posted Entries',wbf['header_detail'])
        row += 1

        worksheet.merge_range('A%s:B%s' % (row-1,row-1), 'Date from : ' + str(data['form']['date_from']),wbf['header_orange'])
        row += 1
        worksheet.merge_range('A%s:B%s' % (row-1,row-1), 'Date to : ' + str(data['form']['date_to']), wbf['header_orange'])
        # worksheet.merge_range('C%s:D%s' % (row-1,row-1), str(data['form']['date_to']))
        row += 1
        worksheet.merge_range('A%s:B%s' % (row-1,row-1), 'Branch: ' + data['form']['used_context']['branch_ids'],wbf['header_orange'])
        # worksheet.merge_range('C%s:D%s' % (row-1,row-1), data['form']['used_context']['branch_ids'])
        row += 1

        Debit_credit_column = [
            ('Name',29,'char','char'),
            ('Debit',17,'float','float'),
            ('Credit',17,'float','float'),
            ('Balance',18,'float','float')
        ]

        Column = [
            ('Name',40,'char','char'),
            ('Balance',47,'float','float')
        ]

        if data['form']['debit_credit']:
            col2 = 0
            for header in Debit_credit_column:
                header_name = header[0]
                header_width = header[1]
                worksheet.set_column(col2,col2,header_width)
                worksheet.write(row-1,col2,header_name,wbf['header_detail'])
                col2 += 1
            row += 1

            for line in report_lines:
                col3 = 0
                if line['level']:
                    worksheet.write(row-1,col3,line['name'])
                    col3 += 1
                    worksheet.write(row-1,col3,line['debit'])
                    col3 += 1
                    worksheet.write(row-1,col3,line['credit'])
                    col3 += 1
                    worksheet.write(row-1,col3,line['balance'])
                    row += 1

        if not data['form']['enable_filter'] and not data['form']['debit_credit']:
            col4 = 0
            for head in Column:
                head_name = head[0]
                head_width = head[1]
                worksheet.set_column(col4,col4,head_width)
                worksheet.write(row-1,col4,head_name,wbf['header_detail'])
                col4 += 1
            row += 1

            for line2 in report_lines:
                col5 = 0
                if line2['level']:
                    worksheet.write(row-1,col5,line2['name'])
                    col5 += 1
                    worksheet.write(row-1,col5,line2['balance'])
                    row += 1

        if data['form']['enable_filter'] and not data['form']['debit_credit']:
            col6 = 0
            for head2 in Column:
                head2_name = head2[0]
                head2_width = head2[1]
                worksheet.set_column(col6,col6,head2_width)
                worksheet.write(row-1,col6,head2_name,wbf['header_detail'])
                col6 += 1
            row += 1

            for line3 in report_lines:
                col7 = 0
                if line3['level']:
                    worksheet.write(row-1,col7,line3['name'])
                    col7 += 1
                    worksheet.write(row-1,col7,line3['balance'])
                    col7 += 1
                    worksheet.write(row-1,col7,line3['balance_emp'])
                    row += 1

        workbook.close()
        out = base64.encodestring(fp.getvalue())
        self.write({'datas': out, 'datas_fname': filename})
        fp.close()
        filename += '%2Exlsx'
        return {
            'type': 'ir.actions.act_url',
            'target': 'new',
            'url': 'web/content/?model=' + self._name + '&id=' + str(
                self.id) + '&field=datas&download=true&filename=' + filename,
        }

    def add_workbook_format(self, workbook):
        colors = {
            'white_orange': '#FFFFDB',
            'orange': '#FFC300',
            'red': '#FF0000',
            'yellow': '#F6FA03',
        }

        wbf = {}
        wbf['header'] = workbook.add_format(
            {'bold': 1, 'align': 'center', 'bg_color': '#FFFFDB', 'font_color': '#000000', 'font_name': 'Georgia'})
        wbf['header'].set_border()

        wbf['header_orange'] = workbook.add_format(
            {'bold': 1, 'align': 'center', 'bg_color': colors['orange'], 'font_color': '#000000',
             'font_name': 'Georgia'})
        wbf['header_orange'].set_border()

        wbf['header_yellow'] = workbook.add_format(
            {'bold': 1, 'align': 'center', 'bg_color': colors['yellow'], 'font_color': '#000000',
             'font_name': 'Georgia'})
        wbf['header_yellow'].set_border()

        wbf['header_no'] = workbook.add_format(
            {'bold': 1, 'align': 'center', 'bg_color': '#FFFFDB', 'font_color': '#000000', 'font_name': 'Georgia'})
        wbf['header_no'].set_border()
        wbf['header_no'].set_align('vcenter')

        wbf['footer'] = workbook.add_format({'align': 'left', 'font_name': 'Georgia'})

        wbf['content_datetime'] = workbook.add_format({'num_format': 'yyyy-mm-dd hh:mm:ss', 'font_name': 'Georgia'})
        wbf['content_datetime'].set_left()
        wbf['content_datetime'].set_right()

        wbf['content_date'] = workbook.add_format({'num_format': 'yyyy-mm-dd', 'font_name': 'Georgia'})
        wbf['content_date'].set_left()
        wbf['content_date'].set_right()

        wbf['title_doc'] = workbook.add_format({
            'bold': True,
            'align': 'center',
            'valign': 'vcenter',
            'font_size': 20,
            'font_name': 'Georgia',
        })

        wbf['company'] = workbook.add_format({'align': 'left', 'font_name': 'Georgia'})
        wbf['company'].set_font_size(11)

        wbf['content'] = workbook.add_format()
        wbf['content'].set_left()
        wbf['content'].set_right()

        wbf['content_float'] = workbook.add_format({'align': 'right', 'num_format': '#,##0.00', 'font_name': 'Georgia'})
        wbf['content_float'].set_right()
        wbf['content_float'].set_left()

        wbf['content_number'] = workbook.add_format({'align': 'right', 'num_format': '#,##0', 'font_name': 'Georgia'})
        wbf['content_number'].set_right()
        wbf['content_number'].set_left()

        wbf['content_percent'] = workbook.add_format({'align': 'right', 'num_format': '0.00%', 'font_name': 'Georgia'})
        wbf['content_percent'].set_right()
        wbf['content_percent'].set_left()

        wbf['total_float'] = workbook.add_format(
            {'bold': 1, 'bg_color': colors['white_orange'], 'align': 'right', 'num_format': '#,##0.00',
             'font_name': 'Georgia'})
        wbf['total_float'].set_top()
        wbf['total_float'].set_bottom()
        wbf['total_float'].set_left()
        wbf['total_float'].set_right()

        wbf['total_number'] = workbook.add_format(
            {'align': 'right', 'bg_color': colors['white_orange'], 'bold': 1, 'num_format': '#,##0',
             'font_name': 'Georgia'})
        wbf['total_number'].set_top()
        wbf['total_number'].set_bottom()
        wbf['total_number'].set_left()
        wbf['total_number'].set_right()

        wbf['total'] = workbook.add_format(
            {'bold': 1, 'bg_color': colors['white_orange'], 'align': 'center', 'font_name': 'Georgia'})
        wbf['total'].set_left()
        wbf['total'].set_right()
        wbf['total'].set_top()
        wbf['total'].set_bottom()

        wbf['total_float_yellow'] = workbook.add_format(
            {'bold': 1, 'bg_color': colors['yellow'], 'align': 'right', 'num_format': '#,##0.00',
             'font_name': 'Georgia'})
        wbf['total_float_yellow'].set_top()
        wbf['total_float_yellow'].set_bottom()
        wbf['total_float_yellow'].set_left()
        wbf['total_float_yellow'].set_right()

        wbf['total_number_yellow'] = workbook.add_format(
            {'align': 'right', 'bg_color': colors['yellow'], 'bold': 1, 'num_format': '#,##0', 'font_name': 'Georgia'})
        wbf['total_number_yellow'].set_top()
        wbf['total_number_yellow'].set_bottom()
        wbf['total_number_yellow'].set_left()
        wbf['total_number_yellow'].set_right()

        wbf['total_yellow'] = workbook.add_format(
            {'bold': 1, 'bg_color': colors['yellow'], 'align': 'center', 'font_name': 'Georgia'})
        wbf['total_yellow'].set_left()
        wbf['total_yellow'].set_right()
        wbf['total_yellow'].set_top()
        wbf['total_yellow'].set_bottom()

        wbf['total_float_orange'] = workbook.add_format(
            {'bold': 1, 'bg_color': colors['orange'], 'align': 'right', 'num_format': '#,##0.00',
             'font_name': 'Georgia'})
        wbf['total_float_orange'].set_top()
        wbf['total_float_orange'].set_bottom()
        wbf['total_float_orange'].set_left()
        wbf['total_float_orange'].set_right()

        wbf['total_number_orange'] = workbook.add_format(
            {'align': 'right', 'bg_color': colors['orange'], 'bold': 1, 'num_format': '#,##0', 'font_name': 'Georgia'})
        wbf['total_number_orange'].set_top()
        wbf['total_number_orange'].set_bottom()
        wbf['total_number_orange'].set_left()
        wbf['total_number_orange'].set_right()

        wbf['total_orange'] = workbook.add_format(
            {'bold': 1, 'bg_color': colors['orange'], 'align': 'center', 'font_name': 'Georgia'})
        wbf['total_orange'].set_left()
        wbf['total_orange'].set_right()
        wbf['total_orange'].set_top()
        wbf['total_orange'].set_bottom()

        wbf['header_detail_space'] = workbook.add_format({'font_name': 'Georgia'})
        wbf['header_detail_space'].set_left()
        wbf['header_detail_space'].set_right()
        wbf['header_detail_space'].set_top()
        wbf['header_detail_space'].set_bottom()

        wbf['header_detail'] = workbook.add_format({'bg_color': '#E0FFC2', 'font_name': 'Georgia'})
        wbf['header_detail'].set_left()
        wbf['header_detail'].set_right()
        wbf['header_detail'].set_top()
        wbf['header_detail'].set_bottom()

        return wbf, workbook

class AccountingReport(models.TransientModel):
    _inherit = "accounting.report"

    branch_ids = fields.Many2one('res.branch', string='Branch')


from logic import *
from rule import Rule
from input_generator import *
from supervisor import acgreedy

#Original table
data = [('New Yorks',      'NY'),
        ('New York',       'NY'),
        ('San Francisco',  'SF'),
        ('San Francisco',  'SF'),
        ('San Jose',       'SJ'),
        ('New York',       'NY'),
        ('San Francisco',  'SFO'),
        ('Berkeley City',  'Bk'),
        ('San Mateo',      'SMO'),
        ('Albany',         'AB'),
        ('San Mateo',      'SM')]

print("Original Table:")
print(data)
print("\n")

input_gen = InputGenerator(data)
data = input_gen.generate()

print("New Table:")
print(data)
print("\n")

#you need unique keys for all entries
d = {i : v for i,v in enumerate(data)}

#some property you want the db to satisfy
#E.g., one to one map between cities and codes
rule = Rule(lambda s,t: iff(eq(s,t,0), eq(s,t,1)))

#get state-action list
print("\n")
print(acgreedy(rule, d)[0])

#E.g., all codes must be len 2
rule = Rule(lambda s: len(s[1]) == 2)
print("\n")
print(acgreedy(rule, d)[0])
print("\n")


# Contest 132 - https://labs.dots.org.ua/ddots/contests?id=132
# Problem 3012 - https://labs.dots.org.ua/ddots/problems?id=3012

import sys

def solve(number, debug) -> str:

	ERROR_MESSAGE = 'Error'

	modulus = number % 7
	if debug:
		print('%7 = {}'.format(modulus))

	if 0 < modulus:
		raise ValueError(ERROR_MESSAGE)

	result = (number//7 - 27)

	modulus = number % 3
	if debug:
		print('%3 = {}'.format(modulus))

	if 0 < modulus:
		raise ValueError(ERROR_MESSAGE)

	result = result // 3

	if debug:
		print(result)

	return result

# Get Input from STDIN
for line in sys.stdin:

	try:
		number = solve(int(line), False)
		print(number)
	except ValueError as error:
		print(error)

	sys.exit(0)

####
# 210 (X = 1)
# 399 (X = 3)
# 84 (X = -5)
# -2142 (X = -111)


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

import re

class markdown(object):
    """

    """

    def __init__(self, text):
        self.text = self.preprocessing(text)
        self.count = 0

    def preprocessing(self, text, tab=4):
        """预处理文本

        对文本中的换行符、空格等替换成标准格式

        Args:
            text: 原有的markdown原文文本
            tab: 与换行符等价的空格数量，默认为4

        Returns:
            经过预处理替换的文本
        """

        text = re.sub(r'\r\n|\r|\n', '\n', text)
        text = text.replace('\t', ' ' * tab)
        text = text.replace('\u00a0', ' ')
        return text.replace('\u2424', '\n')
        # 是否要替换行首空格？
        # pattern = re.compile(r'^ +$', re.M)
        # return pattern.sub('', text)

    def code_blocks_lexer(self):
        """

        Returns:

        """

        code_blocks_pattern = re.compile(r'^```(.*?)\n([\s\S]+?)\n```', re.M)
        code_blocks_delete_pattern = re.compile(r'^```.*?\n[\s\S]+?\n```', re.M)
        code_blocks = re.findall(code_blocks_pattern, self.text)
        self.text = re.sub(code_blocks_delete_pattern, '\n{code_block}\n', self.text)
        return code_blocks

    def title_blocks_lexer(self):
        """

        Returns:

        """

        title_blocks_pattern = re.compile(r'^#{1,6}([\s\S]+?)\n', re.M)
        title_blocks_delete_pattern = re.compile(r'^#{1,6}[\s\S]+?\n', re.M)
        title_blocks = re.findall(title_blocks_pattern, self.text)
        self.text = re.sub(title_blocks_delete_pattern, '\n{title_block}\n', self.text)
        return title_blocks

    def ol_blocks_lexer(self):
        """

        Returns:

        """

        ol_blocks_pattern = re.compile(r'^(\* [\s\S]+?)\n\n', re.M)
        ol_blocks_delete_pattern = re.compile(r'^\* [\s\S]+?\n\n', re.M)
        ol_blocks = re.findall(ol_blocks_pattern, self.text)
        self.text = re.sub(ol_blocks_delete_pattern, '\n{ol_block}\n', self.text)
        return ol_blocks

    def blockquote_blocks_lexer(self):
        """

        Returns:

        """

        blockquote_blocks_pattern = re.compile(r'^(> [\s\S]+?)\n\n', re.M)
        blockquote_blocks_delete_pattern = re.compile(r'^> [\s\S]+?\n\n', re.M)
        blockquote_blocks = re.findall(blockquote_blocks_pattern, self.text)
        self.text = re.sub(blockquote_blocks_delete_pattern, '\n{blockquote_blocks}\n', self.text)
        return blockquote_blocks

    def hr_blocks_lexer(self):
        """

        Returns:

        """

        hr_blocks_pattern = re.compile(r'^([\*-]{3,})$', re.M)
        hr_blocks_delete_pattern = re.compile(r'^([\*-]{3,})$', re.M)
        hr_blocks = re.findall(hr_blocks_pattern, self.text)
        self.text = re.sub(hr_blocks_delete_pattern, '\n{hr_blocks}\n', self.text)
        return hr_blocks

    def block_to_inline(self):
        """

        Returns:

        """

        self.text = re.sub('\n+', '\n', self.text)

    def md_escape_global(self):
        """

        Returns:

        """

        md_eacape_pattern = re.compile(r'\\([\\`*_{}\[\]()#+\-.!~])')
        md_eacape_delete_pattern = re.compile(r'\\[\\`*_{}\[\]()#+\-.!~]')
        md_eacape = re.findall(md_eacape_pattern, self.text)
        self.text = re.sub(md_eacape_delete_pattern, '{md_eacape}', self.text)
        return md_eacape

    def strong_em_inline_lexer(self):
        """

        Returns:

        """

        strong_em_inline_pattern = re.compile(r'[\*]{3}([^\s]+?)[\*]{3}')
        strong_em_inline_delete_pattern = re.compile(r'[\*]{3}[^\s]+?[\*]{3}')
        strong_em_inline = re.findall(strong_em_inline_pattern, self.text)
        self.text = re.sub(strong_em_inline_delete_pattern, '{strong_em_inline}', self.text)
        return strong_em_inline

    def strong_inline_lexer(self):
        """

        Returns:

        """

        strong_inline_pattern = re.compile(r'[\*]{2}([^\s]+?)[\*]{2}')
        strong_inline_delete_pattern = re.compile(r'[\*]{2}[^\s]+?[\*]{2}')
        strong_inline = re.findall(strong_inline_pattern, self.text)
        self.text = re.sub(strong_inline_delete_pattern, '{strong_inline}', self.text)
        return strong_inline

    def em_inline_lexer(self):
        """

        Returns:

        """

        em_inline_pattern = re.compile(r'[\*]([^\s])+?[\*]')
        em_inline_delete_pattern = re.compile(r'[\*][^\s]+?[\*]')
        em_inline = re.findall(em_inline_pattern, self.text)
        self.text = re.sub(em_inline_delete_pattern, '{em_inline}', self.text)
        return em_inline

    def code_inline_lexer(self):
        """

        Returns:

        """

        code_inline_pattern = re.compile(r'`(.+?)`')
        code_inline_delete_pattern = re.compile(r'`.+?`')
        code_inline = re.findall(code_inline_pattern, self.text)
        self.text = re.sub(code_inline_delete_pattern, '{code_inline}', self.text)
        return code_inline

    def link_inline_lexer(self):
        """

        Returns:

        """

        link_inline_pattern = re.compile('\[([^\s]+?)]\[(?P<id>[^\s]+?)].*?\n\[(?P=id)]: ([\S]+)', re.M)
        link_inline_delete_pattern = re.compile('(\[[^\s]+?]\[(?P<id>[^\s]+?)])(.*?\n)\[(?P=id)]: [\S]+', re.M)
        link_inline = re.findall(link_inline_pattern, self.text)
        self.text = re.sub(link_inline_delete_pattern, '{link_inline}\g<3>', self.text)
        return link_inline

    def img_inline_lexer(self):
        """

        Returns:

        """

        img_inline_pattern = re.compile('!\[([^\s]+?)]\[(?P<id>[^\s]+?)].*?\n\[(?P=id)]: ([\S]+)', re.M)
        img_inline_delete_pattern = re.compile('(!\[[^\s]+?]\[(?P<id>[^\s]+?)])(.*?\n)\[(?P=id)]: [\S]+', re.M)
        img_inline = re.findall(img_inline_pattern, self.text)
        self.text = re.sub(img_inline_delete_pattern, self.text)
        return img_inline

if __name__ == '__main__':

    with open('test', 'rt') as f:
        text = f.read()

    md = markdown(text)
    md_eacape = md.md_escape_global()
    code_blocks = md.code_blocks_lexer()
    title_blocks = md.title_blocks_lexer()
    ol_blocks = md.ol_blocks_lexer()
    blockquote_blocks = md.blockquote_blocks_lexer()
    hr_blocks = md.hr_blocks_lexer()
    md.block_to_inline()
    strong_em_inline = md.strong_em_inline_lexer()
    strong_inline = md.strong_inline_lexer()
    em_inline = md.em_inline_lexer()
    code_inline = md.code_inline_lexer()
    a = 1


import csv
from math import log
from numpy import unique
from sklearn.feature_extraction.text import CountVectorizer

def evaluate(X, y, classifier, sample_weight=None, verbose=False):
    """Evaluate the classifier's performance on the given test set.

    Parameters:
    X - A list of documents
    y - A list of class labels
    classifier - A classifier (of type TextClassifier)
    sample_weight - Optional sample weights
    verbose (default False) - If True, print the results of the evaluation

    Returns:
    A dictionary with the following key-value pairs:
        accuracy - The ratio of correctly classified instances
        weightedaccuracy - The ratio of weights of correctly classified
        instances
        confusionmatrix - A "two-dimensional dictionary" where matrix[A][B]
        yields the number of instances of class A that were classified
        as class B by the classifier
    """
    confusion_matrix = {}
    column_width = {}
    results = classifier.predict(X, y, sample_weight)
    correct = sum([1 if results[i] == y[i] else 0 for i in range(len(y))])
    accuracy = correct / len(y)

    for c1 in classifier.classes_:
        confusion_matrix[c1] = {}

        for c2 in classifier.classes_:
            confusion_matrix[c1][c2] = 0

    for i in range(len(results)):
        if results[i] == y[i]:
            correct += 1

        confusion_matrix[y[i]][results[i]] += 1

        if verbose and y[i] not in column_width:
            column_width[y[i]] = len(y[i])

    sum_accuracies = 0.0
    adjustment = 0.0

    for c1 in confusion_matrix:
        TC = confusion_matrix[c1][c1]
        C = 0.0

        for c2 in confusion_matrix[c1]:
            C += confusion_matrix[c1][c2]

        if C > 0.0:
            sum_accuracies += TC / C
        else:
            adjustment += 1

    weighted_acc = sum_accuracies / (len(confusion_matrix) - adjustment)

    if verbose:
        classes = sorted(list(classifier.classes_))

        print(("Accuracy: %0.2f" % (100 * accuracy)) + "%")
        print(("Weighted Accuracy: %0.2f" % (100 * weighted_acc)) + "%")
        print("Confusion Matrix:")

        for class_value, distribution in confusion_matrix.items():
            for prediction, count in distribution.items():
                if prediction not in column_width:
                    width = max(len(prediction), len(str(count)))
                    column_width[prediction] = width
                elif prediction in column_width:
                    if len(str(count)) > column_width[prediction]:
                        column_width[prediction] = len(str(count))

        row = ""

        for prediction in classes:
            width = column_width[prediction] - len(str(prediction)) + 1

            for i in range(0, width):
                row += " "

            row += prediction

        print(row + " <- Classified As")

        for class_value in classes:
            row = ""

            for prediction in classes:
                str_val = str(confusion_matrix[class_value][prediction])
                width = column_width[prediction] - len(str_val) + 1

                for i in range(0, width):
                    row += " "

                row += str(confusion_matrix[class_value][prediction])

            print(row + " " + class_value)

    return {"accuracy": accuracy, "weightedaccuracy": weighted_acc,
            "confusionmatrix": confusion_matrix}

def parse_arff_file(filename):
    X = []
    y = []
    sample_weight = []

    with open(filename, newline="", errors="ignore") as file:
        parsing_data = False
        attr = []

        for line in file:
            line = line.strip()

            if len(line) == 0:
                continue
            elif not parsing_data and \
                    line.upper().startswith("@ATTRIBUTE"):
                if line.find("{") >= 0:
                    data_type = "NOMINAL"
                else:
                    data_type = line[line.rfind(" ") + 1:].upper()

                attr.append(data_type)
            elif not parsing_data and line.upper() == "@DATA":
                parsing_data = True
            elif parsing_data:
                curr = 0
                value = ""
                features = []
                weight = 1
                in_quotes = False

                if line.endswith("}"):
                    index = line.rfind(",{")

                    if index >= 0:
                        weight = float(line[index + 2:len(line) - 1])
                        line = line[:index]

                index = line.rfind(",")
                label = line[index + 1:]
                line = line[:index]

                for i in range(0, len(line)):
                    if line[i] == "'" and (i == 0 or line[i - 1] != "\\"):
                        in_quotes = not in_quotes
                    elif not in_quotes and line[i] == ",":
                        if attr[curr] == "STRING" or attr[curr] == "NOMINAL":
                            features.append(value)
                        else:
                            features.append(float(value))

                        value = ""
                        curr += 1
                    elif line[i] != "\\":
                        value += line[i]

                if attr[curr] == "STRING" or attr[curr] == "NOMINAL":
                    features.append(value)
                else:
                    features.append(float(value))

                X.append(features)
                y.append(label)
                sample_weight.append(weight)

    if len(X[0]) == 1 and isinstance(X[0][0], str):
        X = [features[0] for features in X]

    if len(y) == 0:
        return X

    if len(sample_weight) == 0:
        return X, y

    return X, y, sample_weight

def parse_csv_file(filename, delimiter=",", quotechar='"'):
    X = []
    y = []
    sample_weight = []

    with open(filename, newline="", errors="ignore") as file:
        for row in csv.reader(file, delimiter=delimiter, quotechar=quotechar):
            X.append(row[0])

            if len(row) > 1:
                y.append(row[1])

            if len(row) > 2:
                sample_weight.append(float(row[2]))

    if len(y) == 0:
        return X

    if len(sample_weight) == 0:
        return X, y

    return X, y, sample_weight

def top_information_gain_words(X, y, top_k=0, min_ig=0):
    def __entropy(y):
        freq = [sum([1 if l1 == l2 else 0 for l2 in y]) for l1 in set(y)]
        ent = 0

        for frequency in freq:
            ratio = frequency / len(y)
            ent -= ratio * log(ratio, 2)

        return ent

    vectorizer = CountVectorizer(min_df=2, stop_words="english")
    X = vectorizer.fit_transform(X)
    words = vectorizer.get_feature_names()
    top_words = []
    ent = __entropy(y)

    for column in range(X.shape[1]):
        values, counts = unique(list(X[:, column].data), return_counts=True)
        dictionary = dict(zip(values, counts))
        dictionary[0] = X[:, column].shape[0] - len(X[:, column].data)
        sum_counts = sum(dictionary.values())
        subset_entropy = 0

        for value in dictionary.keys():
            value_probability = dictionary[value] / sum_counts
            subset = [y[i] for i in range(X.shape[0]) if X[i, column] == value]
            subset_entropy += value_probability * __entropy(subset)

        information_gain = ent - subset_entropy

        if information_gain > min_ig:
            top_words.append((words[column], information_gain))

    top_words.sort(key=lambda word: word[1], reverse=True)

    limit = min(top_k, len(top_words)) if top_k > 0 else len(top_words)
    return top_words[:limit]


# -*- coding:utf-8 -*-
"""
"""
__author__ = "Cliff.wang"
import configparser
import logging
import os, sys

class Settings():

    def __init__(self):
        # 获取编程环境下的代码路径
        self.path = os.path.abspath(os.path.dirname(__file__))
        # 获取打包后的可执行文件路径
        # self.path = os.path.dirname(sys.executable)

        self.logger = self._getLogger()

        config = configparser.ConfigParser()
        confFile = os.path.join(os.path.dirname(self.path), "srv.conf")
        if not os.path.exists(confFile):
            confFile = os.path.join(self.path, "srv.conf")
        config.read(confFile, encoding="utf-8")

        # 数据库服务器
        self.serverHost = config.get("dbServer", "host")         # 服务端地址
        if self.serverHost[-1:] == "/":
            self.serverHost = self.serverHost[:-1]
        self.serverUser = config.get("dbServer", "user")         # 服务端用户名
        self.serverPwd = config.get("dbServer", "password")      # 服务端密码
        self.serverDb = config.get("dbServer", "database")       # 服务端数据库名

        # web服务器
        self.webHost = config.get("webServer", "host")          # web服务器
        self.webPort = config.getint("webServer", "port")          # web端口

        # 业务控制
        self.vipClass = config.get("business", "vipclass")          # 默认会员类别
        self.branchNo = config.get("business", "branchNo")          # 默认会员类别
        self.rptTitle = config.get("business", "rptTitle")          # 报表数据标题

    def _getLogger(self):
        logger = logging.getLogger("[DataInterCatering]")
        handler = logging.FileHandler(os.path.join(self.path, "service.log"))
        formatter = logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
        handler.setFormatter(formatter)
        logger.addHandler(handler)
        logger.setLevel(logging.INFO)

        return logger

if __name__ == "__main__":
    sett = Settings(r"E:\AI\Python\proDataInter\dataInterKingdee\config")
    i = 1
    i += 1


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

from rest_framework import serializers

from admin_app.places.models import Place
from .core import ContentSerializer
from .fields import LocationField
from .payments import PaymentSerializer
from .serializers import JoinSerializer
from .serializers import RateSerializer
from .serializers import ContactsSerializer
from .schedule.common import ScheduleSerializer

class PlacePricesSerializer(JoinSerializer):
    # 'from' is using for import
    from_ = serializers.FloatField(source='price_from')
    till = serializers.FloatField(source='price_till')

    class Meta:
        model = Place
        fields = ('from_', 'till')

class LocationSerializer(JoinSerializer):
    city = serializers.StringRelatedField()
    location = LocationField()
    metro_ids = serializers.PrimaryKeyRelatedField(
        source='metro', many=True, read_only='True'
    )

    class Meta:
        model = Place
        fields = ('city', 'address', 'location', 'metro_ids')

class WorkTimeSerializer(JoinSerializer):
    from_ = serializers.TimeField(source='w_time_from')
    till = serializers.TimeField(source='w_time_till')

    class Meta:
        model = Place
        fields = ('from_', 'till', 'timetable')

class PlaceViewNameScheduleSerializer(ScheduleSerializer):

    class Meta(ScheduleSerializer.Meta):
        alternative = {
            'params': {
                'source': '*',
                'read_only': True,
                'view_name': 'place-schedule',
                'lookup_field': 'slug',
            }
        }

class PlaceSerializer(ContentSerializer):
    location = LocationSerializer()
    work_time = WorkTimeSerializer()
    contacts = ContactsSerializer(model=Place)
    rate = RateSerializer(model=Place)
    prices = PlacePricesSerializer()
    payment = PaymentSerializer()
    schedule = PlaceViewNameScheduleSerializer(source='sessions', many=True)

    class Meta:
        model = Place
        fields = ContentSerializer.Meta.fields + (
            'location',
            'work_time',
            'contacts',
            'rate',
            'prices',
            'payment',
            'schedule',
        )
        alternative = {
            'params': {
                'read_only': True,
                'lookup_field': 'slug',
                'view_name': 'place-detail',
            }
        }