#  try:
#      from benchexec.tools.divine4 import Tool as DivineTool
#  except ImportError:
#import symbiotic.benchexec.util as util
import symbiotic.benchexec.result as result
#from symbiotic.benchexec.tools.template import BaseTool
from .. benchexec.tools.divine4 import Tool as DivineTool
from . tool import SymbioticBaseTool

class SymbioticTool(DivineTool, SymbioticBaseTool):
    """
    DIVINE integrated into Symbiotic
    """

    def __init__(self, opts):
        SymbioticBaseTool.__init__(self, opts)
        self._memsafety = self._options.property.memsafety()

    def llvm_version(self):
        return '7.0.1'

    def set_environment(self, env, opts):
        """
        Set environment for the tool
        """
        if opts.devel_mode:
            env.prepend('PATH', '{0}/divine'.\
                        format(env.symbiotic_dir))

    def cc(self):
        #return ['divine', 'cc']
        return ['clang', '--target=x86_64-unknown-none-elf',
                '-fgnu89-inline', '-Os']

    def actions_before_slicing(self, symbiotic):
        symbiotic.link_undefined(['__VERIFIER_atomic_begin',
                                  '__VERIFIER_atomic_end'])

   # not needed anymore?
   #def actions_before_verification(self, symbiotic):
   #    # link the DiOS environment
   #    newfile = symbiotic.curfile[:-3] + '-cc' + symbiotic.curfile[-3:]
   #    symbiotic.command(['divine', 'cc', symbiotic.curfile, '-o', newfile])
   #    symbiotic.curfile = newfile

    def cmdline(self, executable, options, tasks, propertyfile=None, rlimits={}):
        #return DivineTool.cmdline(self, executable, options, tasks, propertyfile, rlimits)

        prp = self._options.property
        cmd = ['divine', 'check',
               '--lart', 'stubs', '--lart', 'lowering',
               '--lart', 'svc-trace-nondets',
               '-o', 'ignore:exit', '-o', 'ignore:control',
               '--svcomp', '--sequential']
        if prp.unreachcall():
            cmd.extend(['-o', 'ignore:memory'])
        elif prp.memsafety():
            cmd.extend(['-o', 'ignore:diosassert',
                        '--leakcheck', 'exit,state'])
        cmd.extend(tasks)
        return cmd

    def determine_result(self, returncode, returnsignal, output, isTimeout):
        """
        Parse the output of the tool and extract the verification result.
        This method always needs to be overridden.
        If the tool gave a result, this method needs to return one of the
        benchexec.result.RESULT_* strings.
        Otherwise an arbitrary string can be returned that will be shown to the user
        and should give some indication of the failure reason
        (e.g., "CRASH", "OUT_OF_MEMORY", etc.).
        """
        if not output:
            return "no output"

        has_error = False
        has_memerr = False
        memerr = None
        res='no result'
        for line in map(str, output):
            if "error found: yes" in line:
                has_error = True
            if "error found: boot" in line:
                return result.RESULT_UNKNOWN
            if "error found: no" in line:
                if has_error:
                    return 'parsing error'
                has_error = False
            if "not implemented in userspace" in line:
                res = result.RESULT_UNKNOWN
            if "memory error in userspace" in line:
                has_memerr = True
            if "__vm_obj_free" in line:
                memerr = result.RESULT_FALSE_FREE
            if "out of bounds" in line:
                memerr = result.RESULT_FALSE_DEREF
            if "memory leak in userspace" in line:
                #if "heap" in fault:
                memerr = result.RESULT_FALSE_MEMTRACK
            if "assertion violation in userspace" in line:
                res = result.RESULT_FALSE_REACH
            if "verifier error called" in line:
                res = result.RESULT_FALSE_REACH

        if has_error:
            if has_memerr and memerr is not None:
                return memerr
            return res
        elif returncode != 0:
            return f'returned {returncode}'
        elif returnsignal != 0:
            return f'signal {returnsignal}'
        elif res == 'no result' and not has_memerr and memerr is None:
            return result.RESULT_TRUE_PROP

        return result.RESULT_UNKNOWN


'''
RunPod | CLI | Utils | SSH Command

Connect and run commands over SSH.
'''
import threading
import subprocess
import colorama
import paramiko

from .rp_info import get_pod_ssh_ip_port
from .rp_userspace import find_ssh_key_file
from .rp_runpodignore import get_ignore_list

colorama.init(autoreset=True) # Initialize colorama

class SSHConnection:
    ''' Connect and run commands over SSH. '''

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

        self.pod_ip, self.pod_port = get_pod_ssh_ip_port(pod_id)
        self.key_file = find_ssh_key_file(self.pod_ip, self.pod_port)

        self.ssh = paramiko.SSHClient()
        self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        self.ssh.connect(self.pod_ip, port=self.pod_port,
                         username='root', key_filename=self.key_file)

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        del exc_type, exc_value, traceback
        self.close()

    def _get_ssh_options(self):
        """ Get the SSH options for connecting to the pod. """
        return [
            "-o", "StrictHostKeyChecking=no",
            "-o", "LogLevel=ERROR",
            "-p", str(self.pod_port),
            "-i", self.key_file
        ]

    def run_commands(self, commands):
        ''' Runs a list of bash commands over SSH. '''
        def handle_stream(stream, color, prefix):
            for line in stream:
                if line:
                    print(color + f"[{prefix}]", line.strip())

        for command in commands:
            command = f'source /root/.bashrc && {command}'
            command = f'source /etc/rp_environment && {command}'
            command = f'while IFS= read -r -d \'\' line; do export "$line"; done < /proc/1/environ && {command}' # pylint: disable=line-too-long
            _, stdout, stderr = self.ssh.exec_command(command)

            stdout_thread = threading.Thread(
                target=handle_stream, args=(stdout, colorama.Fore.GREEN, self.pod_id))
            stderr_thread = threading.Thread(
                target=handle_stream, args=(stderr, colorama.Fore.RED, self.pod_id))

            stdout_thread.start()
            stderr_thread.start()

            stdout_thread.join()
            stderr_thread.join()

    def put_file(self, local_path, remote_path):
        ''' Copy local file to remote machine over SSH. '''
        with self.ssh.open_sftp() as sftp:
            sftp.put(local_path, remote_path)

    def get_file(self, remote_path, local_path):
        ''' Fetch a remote file to local machine over SSH. '''
        with self.ssh.open_sftp() as sftp:
            sftp.get(remote_path, local_path)

    def launch_terminal(self):
        ''' Launch an interactive terminal over SSH. '''
        cmd = ["ssh"] + self._get_ssh_options() + [f"root@{self.pod_ip}"]

        subprocess.run(cmd, check=True)

    def rsync(self, local_path, remote_path, quiet=False):
        """ Sync a local directory to a remote directory over SSH.

        A .runpodignore file can be used to ignore files and directories.
        This file should be placed in the root of the local directory to sync.

        Args:
            local_path (str): The local directory to sync.
            remote_path (str): The remote directory to sync.
        """
        rsync_cmd = ["rsync", "-avz", "--no-owner", "--no-group"]

        for pattern in get_ignore_list():
            rsync_cmd.extend(["--exclude", pattern])

        if quiet:
            rsync_cmd.append("--quiet")

        rsync_cmd.extend([
            "-e", f"ssh {' '.join(self._get_ssh_options())}",
            local_path,
            f"root@{self.pod_ip}:{remote_path}"
        ])

        return subprocess.run(rsync_cmd, check=True)

    def close(self):
        ''' Close the SSH connection. '''
        self.ssh.close()


import logging
from asgiref.sync import async_to_sync
from channels.layers import get_channel_layer
from django.db import models
from django.db.models.signals import post_save
from django.contrib.auth import get_user_model

MESSAGE = "You have received a new notification"
USR = get_user_model()

class Notifications(models.Model):
    user = models.ForeignKey(
        USR, on_delete=models.CASCADE, blank=True, null=True)
    text = models.CharField(max_length=255, default=MESSAGE)
    read = models.BooleanField(default=False)
    redirect = models.CharField(max_length=2083, default="/")

    def __str__(self):
        return self.text

    @property
    def unread_count(self):
        return self.__class__.objects.filter(user=self.user).count()

def notification_receiver(sender, instance, created, *args, **kwargs):
    if created:
        group = "notification"
        if instance.user:
            username = getattr(instance.user, instance.user.USERNAME_FIELD)
            group += f"-{username}"
        channel_layer = get_channel_layer()
        logging.critical(f"SCOPE {channel_layer}")
        assert channel_layer != None, "Channel layer is None."
        async_to_sync(channel_layer.group_send)(group, {
            "type": "new.notification",
            "event": "Notification",
            "text": instance.text,
            "redirect_url": instance.redirect,
            "unread_count": instance.unread_count,
        })

post_save.connect(notification_receiver, sender=Notifications)


import torch
from nflows.distributions.normal import StandardNormal
from nflows.flows.base import Flow
from nflows.nn import nets as nets
from nflows.transforms import (
    CompositeTransform,
    MaskedAffineAutoregressiveTransform,
    RandomPermutation,
)
from nflows.transforms.coupling import (
    AdditiveCouplingTransform,
    AffineCouplingTransform,
)
from nflows.transforms.normalization import BatchNorm
from torch.nn import functional as F

# Turn off parallelism
torch.set_num_threads(1)
torch.set_num_interop_threads(1)

class NVPFlow(Flow):
    """A simplified version of Real NVP for 1-dim inputs.

    This implementation uses 1-dim checkerboard masking but doesn't use
    multi-scaling.
    Reference:
    > L. Dinh et al., Density estimation using Real NVP, ICLR 2017.

    This class has been modified from the example found at:
    https://github.com/bayesiains/nflows/blob/master/nflows/flows/realnvp.py
    """

    def __init__(
        self,
        features,
        hidden_features,
        num_layers,
        num_blocks_per_layer,
        use_volume_preserving=False,
        activation=F.relu,
        dropout_probability=0.0,
        batch_norm_within_layers=False,
        batch_norm_between_layers=False,
        random_permutation=True,
    ):

        if use_volume_preserving:
            coupling_constructor = AdditiveCouplingTransform
        else:
            coupling_constructor = AffineCouplingTransform

        mask = torch.ones(features)
        mask[::2] = -1

        def create_resnet(in_features, out_features):
            return nets.ResidualNet(
                in_features,
                out_features,
                hidden_features=hidden_features,
                num_blocks=num_blocks_per_layer,
                activation=activation,
                dropout_probability=dropout_probability,
                use_batch_norm=batch_norm_within_layers,
            )

        layers = []
        for _ in range(num_layers):
            transform = coupling_constructor(
                mask=mask, transform_net_create_fn=create_resnet
            )
            layers.append(transform)
            mask *= -1
            if batch_norm_between_layers:
                layers.append(BatchNorm(features=features))

        if random_permutation:
            layers.append(RandomPermutation(features=features))

        super().__init__(
            transform=CompositeTransform(layers),
            distribution=StandardNormal([features]),
        )

class BasicFlow(Flow):
    def __init__(self, features):
        transform = CompositeTransform(
            [
                MaskedAffineAutoregressiveTransform(
                    features=features, hidden_features=2 * features
                ),
                RandomPermutation(features=features),
            ]
        )
        distribution = StandardNormal(shape=[features])
        super().__init__(
            transform=transform,
            distribution=distribution,
        )


import json
from typing import Iterator
import numpy
import random
import torch
from transformers import T5Tokenizer
import torch.nn.functional as F
from itertools import chain
from torch.utils.data import Dataset, IterableDataset, DataLoader

with open('/home/jade/ACOSI/data/20220111_attribute_lexicon.json', 'r') as f:
    attribute_lexicon = json.load(f)
    attribute_lexicon = list(attribute_lexicon.values())

class DKG_reader(IterableDataset):

    def __init__(self, file, lexicon) -> None:
        super().__init__()
        self.file = file
        self.lexicon = lexicon
        print('loading the DKG reader')

    def __iter__(self) -> Iterator:
        with open(self.lexicon, 'r') as f:
            attribute_lexicon = json.load(f)
            attribute_lexicon = [i for i in list(attribute_lexicon.values())]
            attribute_lexicon = list(chain.from_iterable(attribute_lexicon))

        with open(self.file, 'r') as f:
            data = []
            for line in f:
                if line != '':
                    tmp_dic = json.loads(line)
                    data.append(tmp_dic)
        for x in data:
            en_DKG_idx = []
            de_DKG_idx = []
            for idx, word in enumerate(x['review'].split()):
                if word in attribute_lexicon:
                    en_DKG_idx.append(idx)

            if type(eval(x['labels'])) is not list:
                print(type(eval(x['labels'])), x['labels'])
                for idx, word in enumerate(eval(x['labels'].strip())):
                    if word in attribute_lexicon:
                        de_DKG_idx.append(idx)
            heiheihei = eval(x['labels'])
            for i in range(len(heiheihei)):
                for idx, a_word in enumerate(heiheihei[i]):
                    if idx == 0:
                        heiheihei[i][idx] = '<A>' + heiheihei[i][idx]
                    if idx == 1:
                        heiheihei[i][idx] = '<C>' + heiheihei[i][idx]
                    if idx == 2:
                        heiheihei[i][idx] = '<S>' + heiheihei[i][idx]
                    if idx == 3:
                        heiheihei[i][idx] = '<O>' + heiheihei[i][idx]
                    if idx == 4:
                        heiheihei[i][idx] = '<I>' + heiheihei[i][idx]

            if type(eval(x['labels'])) is not list:
                yield x['review'], '<labels>'.join([' '.join (i) for i in heiheihei]), str(en_DKG_idx), str(de_DKG_idx)

            else:
                yield x['review'], '<labels>'.join([' '.join (i) for i in heiheihei]), str(en_DKG_idx)

class T5_reader(IterableDataset):
    def __init__(self, file):
        super(T5_reader).__init__()
        self.file = file
        print('Load T5_reader succsessfully')

    def __iter__(self):

        with open(self.file, 'r') as f:
            data = []
            for line in f:
                if line != '':
                    tmp_dic = {}
                    review, label = line.strip().split('####')
                    tmp_dic['review'] = review.strip()
                    tmp_dic['labels'] = eval(label.strip())
                    data.append(tmp_dic)
        for x in data:
            # print(x['review'], '<labels>' + '<labels>'.join([' '.join (i) for i in x['labels']]))
            for i in range(len(x['labels'])):
                for idx, a_word in enumerate(x['labels'][i]):
                    if idx == 0:
                        x['labels'][i][idx] = '<A>' + x['labels'][i][idx]
                    if idx == 1:
                        x['labels'][i][idx] = '<C>' + x['labels'][i][idx]
                    if idx == 2:
                        x['labels'][i][idx] = '<S>' + x['labels'][i][idx]
                    if idx == 3:
                        x['labels'][i][idx] = '<O>' + x['labels'][i][idx]
                    if idx == 4:
                        x['labels'][i][idx] = '<I>' + x['labels'][i][idx]

            yield x['review'], '<labels>'.join([' '.join (i) for i in x['labels']])
            # yield x['review'], '<labels>' + '</s>'.join([' '.join (i) for i in x['labels']])


__author__ = 'carlos.matsumoto'

from django.conf.urls import patterns, url

import views

urlpatterns = patterns('',
    url(r'^$', views.index, name='index'),
    url(r'^login/', views.login_user, name='login'),
    url(r'^home/', views.home, name='home'),
    url(r'^register/', views.register, name='register'),
    url(r'^upload/', views.upload, name='upload'),
    url(r'^results/', views.results, name='results'),
    url(r'^logout/', 'django.contrib.auth.views.logout', {'next_page':'/sales/login/'}),

)


#Dillon Maltese
#4.6B Exercise

import random

guessed = False
counter = 0

rnum1 = random.randint(1,9)
rnum2 = random.randint(1,9)
rnum3 = random.randint(1,9)
rnum4 = random.randint(1,9)

print(str(rnum1) + str(rnum2) + str(rnum3) + str(rnum4))
print("Welcome to Code Breaker. \nThe objective of the game is to break the code. \nThe code will be 4 numbers.")

while(guessed == False):
    g1 = False
    g2 = False
    g3 = False
    g4 = False
    num1 = int(input("Enter number 1: "))
    num2 = int(input("Enter number 2: "))
    num3 = int(input("Enter number 3: "))
    num4 = int(input("Enter number 4: "))

    if(rnum1 == num1):
        print("The first number that you guessed, " + str(num1) + ", is correct")
        g1 = True
    if(rnum2 == num2):
        print("The second number that you guessed, " + str(num2) + ", is correct")
        g2 = True
    if(rnum3 == num3):
        print("The third number that you guessed, " + str(num3) + ", is correct")
        g3 = True
    if(rnum4 == num4):
        print("The fourth number that you guessed, " + str(num3) + ", is correct")
        g4 = True

    if(g1 == g2 == g3 == g4 == False):
        print("Sorry you got nothing correct")

    counter += 1

    if(g1 == g2 == g3 == g4 == True):
        print("Nice job, you have won. \nThe secret number was " + str(rnum1) + str(rnum2) + str(rnum3) + str(rnum4) + "\nIt took you " + str(counter) + " tries")
        guessed = True


import pandas as pd
import numpy as np
from numpy import nan
import re
import os
from dotenv import load_dotenv
import requests
from bs4 import BeautifulSoup

def getAverageOceanTemperatureByDecade():
    """
    This is a web scraping function. It extracts the content from a table in the URL and it stores it a dictionary.
    Arggs:
    ()
    Returns:
     Dictionary
    """
    URL = "https://www.currentresults.com/Environment-Facts/changes-in-earth-temperature.php"
    page = requests.get(URL)
    results = BeautifulSoup(page.content, "html.parser")
    table = results.find('table', class_='articletable tablecol-1-left revcolr')

    a = {}

    for row in table.tbody.find_all('tr'):
        columns = row.find_all('td')
        a[int(columns[0].text.strip()[:-1])] = columns[1].text.strip()

    return a

def buildDictionaryOfPopulationFrom(decadeStart, decadeEnd, df):
    """
    This function stores in a dictionary the decade and average values for the population of each decade.
    Arggs:
     decadeStart, decadeEnd, df
    Returns:
     Dictionary
    """
    d = {}
    for i in range(decadeStart, decadeEnd, 10):
        d[i] = average_on_decade(df, i)
    return d

def average_on_decade(df, decade):
    """
    This function calculates the average of 10 values in a range from a df.
    Arggs:
    ()
    Returns:
    a)Average in a decade (total value of the items in the range divided by the number of items).
    b)Average in a decade (Total value of the items in the range divided by one less of the number of items).
    """
    total_items = 10
    count_total = 0
    for i in range(decade, decade + 9):
        try:
            count_total += df.loc[i].Population
        except KeyError:
            total_items -= 1

    return count_total / total_items

def buildCompleteDataframe(temperatura: dict, poblacion: dict, since, till):
    """
    This function builds a df from dictionaries and a list in a df.
    Arggs:
    - (Temperature dictionary(2000 : "13.5"), population dictionary (2000 : 7003495863047), since(year), till(year)
    Returns:
    a)Total value of the items in the range divided by the number of items.
    b)Total value of the items in the range divided by one less of the number of items.
    """
    dictionary = {}
    index_counter = 0

    for i in range(since, till, 10):
               # this is the list of decade
        list_of_temp_population = []
       # to fill the first value of the row
        list_of_temp_population.append(i)

        if temperatura.get(i) is not None:
            list_of_temp_population.append(temperatura.get(i))
        else:
            list_of_temp_population.append("0")

        if poblacion.get(i) is not None:
            list_of_temp_population.append(poblacion.get(i))
        else:
            list_of_temp_population.append("0")

          # {decada: año, temperatura, poblacion}
        dictionary[index_counter] = list_of_temp_population
        #{0 : ["1950", "13.5", 123456789]}
        index_counter+=1

    return pd.DataFrame.from_dict(dictionary, orient='index')


'''采用自组CNN模型进行猫狗图片二分类'''
import os, shutil
import tensorflow as tf
from keras import layers
from keras import models
from keras import optimizers
from keras.preprocessing.image import ImageDataGenerator
import matplotlib.pyplot as plt
import math

# 自动分配显存，并且限制在50%内
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
gpu_options = tf.compat.v1.GPUOptions(allow_growth=True, )
config = tf.compat.v1.ConfigProto(gpu_options=gpu_options, )
config.gpu_options.per_process_gpu_memory_fraction = 0.5
session = tf.compat.v1.Session(config=config)

# 原始数据集的路径
original_dataset_dir = '/home/Howie/alldataset/catsdogsdataset'

# 项目数据集存放路径
base_dir = '/home/Howie/kerasproject/cats_dogs/dataset'

# 创建训练集、验证集、测试集目录
train_dir = os.path.join(base_dir, 'train')
#os.mkdir(train_dir)

validation_dir = os.path.join(base_dir, 'validation')
#os.mkdir(validation_dir)

test_dir = os.path.join(base_dir, 'test')
#os.mkdir(test_dir)

# 创建猫狗目录
train_cats_dir = os.path.join(train_dir, 'cats')
validation_cats_dir = os.path.join(validation_dir, 'cats')
test_cats_dir = os.path.join(test_dir, 'cats')
#os.mkdir(train_cats_dir)
#os.mkdir(validation_cats_dir)
#os.mkdir(test_cats_dir)

train_dogs_dir = os.path.join(train_dir, 'dogs')
validation_dogs_dir = os.path.join(validation_dir, 'dogs')
test_dogs_dir = os.path.join(test_dir, 'dogs')
#os.mkdir(train_dogs_dir)
#os.mkdir(validation_dogs_dir)
#os.mkdir(test_dogs_dir)

#数据集复制
fnames = ['cat.{}.jpg'.format(i) for i in range(2500)]
for fname in fnames:
    scr = os.path.join(original_dataset_dir, fname)
    dst = os.path.join(train_cats_dir, fname)
    shutil.copyfile(scr, dst)

fnames = ['cat.{}.jpg'.format(i) for i in range(2500, 3000)]
for fname in fnames:
    scr = os.path.join(original_dataset_dir, fname)
    dst = os.path.join(validation_cats_dir, fname)
    shutil.copyfile(scr, dst)

fnames = ['cat.{}.jpg'.format(i) for i in range(3000, 3500)]
for fname in fnames:
    scr = os.path.join(original_dataset_dir, fname)
    dst = os.path.join(test_cats_dir, fname)
    shutil.copyfile(scr, dst)

fnames = ['dog.{}.jpg'.format(i) for i in range(2500)]
for fname in fnames:
    scr = os.path.join(original_dataset_dir, fname)
    dst = os.path.join(train_dogs_dir, fname)
    shutil.copyfile(scr, dst)

fnames = ['dog.{}.jpg'.format(i) for i in range(2500, 3000)]
for fname in fnames:
    scr = os.path.join(original_dataset_dir, fname)
    dst = os.path.join(validation_dogs_dir, fname)
    shutil.copyfile(scr, dst)

fnames = ['dog.{}.jpg'.format(i) for i in range(3000, 3500)]
for fname in fnames:
    scr = os.path.join(original_dataset_dir, fname)
    dst = os.path.join(test_dogs_dir, fname)
    shutil.copyfile(scr, dst)

# 建立CNN模型
def generate_model():
    model = models.Sequential()
    model.add(layers.Conv2D(32, (5,5), activation='relu',
    input_shape=(150,150,3)))
    model.add(layers.MaxPooling2D(2,2))
    model.add(layers.Conv2D(64, (5,5), activation='relu'))
    model.add(layers.MaxPooling2D(2,2))
    model.add(layers.Conv2D(128, (3,3), activation='relu'))
    model.add(layers.MaxPooling2D(2,2))
    model.add(layers.Conv2D(128, (3,3), activation='relu'))
    model.add(layers.MaxPooling2D(2,2))

    model.add(layers.Flatten())
    model.add(layers.Dropout(0.5))
    model.add(layers.Dense(512, activation='relu'))
    model.add(layers.Dense(1, activation='sigmoid'))

    model.summary()
    return model

def cosine(base_lr, epoch):
    return base_lr * (math.cos((epoch%20) * math.pi/20) + 1 ) / (epoch//20 + 1)

# 图像生成器
train_datagen = ImageDataGenerator(rescale=1./255)

'''train_datagen = ImageDataGenerator(
rescale=1./255,
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,)'''

validation_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(train_dir,
 target_size=(150,150), batch_size=256, class_mode='binary')
validation_generator = validation_datagen.flow_from_directory(validation_dir,
 target_size=(150,150), batch_size=256, class_mode='binary')

for data_batch, labels_batch in train_generator:
    print('data batch shape:', data_batch.shape)
    print('labels batch shape:', labels_batch.shape)
    break

model = generate_model()
acc, val_acc, loss, val_loss = [], [], [], []

model.compile(optimizer=optimizers.RMSprop(lr=5e-5),
loss='binary_crossentropy', metrics=['acc'])
# 训练模型
history_bin = model.fit(train_generator,
epochs=120, validation_data=validation_generator)

acc = acc + history_bin.history['acc']
val_acc = val_acc + history_bin.history['val_acc']
loss = loss + history_bin.history['loss']
val_loss = val_loss + history_bin.history['val_loss']

 #保存模型
model.save('/home/Howie/kerasproject/cats_dogs/cats_and_dogs_small_3.h5')

# 训练过程可视化
epoch = range(1, len(acc)+1)

plt.subplot(211)
plt.plot(epoch, acc, 'b', label='Training accuracy')
plt.plot(epoch, val_acc, 'y', label='Validation_accuracy')
plt.xlabel('epoch')
plt.ylabel('accuracy')
plt.legend()

plt.subplot(212)
plt.plot(epoch, loss, 'b', label='Training loss')
plt.plot(epoch, val_loss, 'y', label='Validation loss')
plt.xlabel('epoch')
plt.ylabel('loss')
plt.legend()

plt.show()
plt.savefig("/home/Howie/kerasproject/cats_dogs/cats_and_dogs_small_3.png")


#!usr/bin/python

#Single Line Comments : #,''," ",''' ''',&""" """
#Multiline Comments   : ''' ''',&""" """

'''
Name of the program is Variables
Author  : Chaithanya
Platform : windows
'''

'Rule of creating variables'
"""
1.A-Z
2.a-z
3.A-Za-z
4._Special Character
"""

#Note : A variable can not start with integer value(0-9)

'Creating Variables in python'

FIRSTNAME = 'Chaitanya'
middlename= "praveen"
LastName  = 'Thumma'
_Software = 'Python'
_005      = 3.6

'Accessing variables in python'
print(FIRSTNAME)
print(middlename)
print(LastName )
print(_Software )
print(_005 )


#!/usr/bin/env python
# encoding: utf-8

"""
@version: python3.6
@Author  : Zhangfusheng
@Time    : 2019/8/27 19:53
@File    : meituan2019_kaoshicelue
@Software: PyCharm
"""
if __name__ == "__main__":
    M = int(input())
    temp = []
    for i in range(M):
        temp.append(list(map(int, input().split(" "))))
    t = 120
    result = [[0 for _ in range(121)] for _ in range(M+1)]
    for i in range(1, M+1):
        for j in range(121):
            if temp[i-1][0] <= j < temp[i-1][2]:
                result[i][j] = max(result[i-1][j], result[i-1][j-temp[i-1][0]] + temp[i-1][1])
            elif j >= temp[i-1][2]:
                result[i][j] = max(result[i-1][j], result[i-1][j-temp[i-1][0]] + temp[i-1][1],
                                   result[i-1][j-temp[i-1][2]] + temp[i-1][3])
            else:
                result[i][j] = result[i - 1][j]
    print(result[M][120])


# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import random
try:
    from tkinter import *
except ImportError:
    from Tkinter import *

class Block(object):
    def __init__(self, mmap, x, y, direction=None):
        super(Block, self).__init__()
        self.walls = [True, True, True, True]  # top,right,bottom,left
        self.mmap = mmap
        if mmap:
            mmap.mmap[x][y] = self
        self.x, self.y = x, y
        if direction is not None:
            direction = (direction + 2) % 4
            self.walls[direction] = False

    def __unicode__(self):
        return "%s" % [1 if e else 0 for e in self.walls]

    def __str__(self):
        return unicode(self).encode('utf-8')

    def get_next_block_pos(self, direction):
        x = self.x
        y = self.y
        if direction == 0:  # Top
            y -= 1
        elif direction == 1:  # Right
            x += 1
        if direction == 2:  # Bottom
            y += 1
        if direction == 3:  # Left
            x -= 1
        return x, y

    def get_next_block(self):
        directions = list(range(4))
        random.shuffle(directions)
        for direction in directions:
            x, y = self.get_next_block_pos(direction)
            if x >= self.mmap.max_x or x < 0 or y >= self.mmap.max_y or y < 0:
                continue
            if self.mmap.mmap[x][y]:  # if walked
                continue
            self.walls[direction] = False
            return Block(self.mmap, x, y, direction)
        return None

class Map(object):
    def __init__(self):
        super(Map, self).__init__()

    def reset_map(self):
        self.gen_map(self.max_x, self.max_y)

    def gen_map(self, max_x=10, max_y=10):
        self.max_x, self.max_y = max_x, max_y
        self.mmap = [[None for j in range(self.max_y)] for i in range(self.max_x)]
        self.solution = []
        block_stack = [Block(self, 0, 0)]  # a unused block
        while block_stack:
            block = block_stack.pop()
            next_block = block.get_next_block()
            if next_block:
                block_stack.append(block)
                block_stack.append(next_block)
                if next_block.x == self.max_x - 1 and next_block.y == self.max_y - 1:  # is end
                    for o in block_stack:
                        self.solution.append((o.x, o.y))

    def __unicode__(self):
        out = ""
        for y in range(self.max_y):
            for x in range(self.max_x):
                out += "%s" % self.mmap[x][y]
            out += "\n"
        return out

    def __str__(self):
        return unicode(self).encode('utf-8')

class DrawMap(object):
    def __init__(self, mmap, cell_width=10):
        super(DrawMap, self).__init__()
        self.mmap = mmap
        self.cell_width = cell_width

    def get_map_size(self):
        # width, height
        return (self.mmap.max_x + 2) * self.cell_width, (self.mmap.max_y + 2) * self.cell_width

    def create_line(self, x1, y1, x2, y2, **kwarg):
        raise NotImplemented()

    def create_solution_line(self, x1, y1, x2, y2):
        self.create_line(x1, y1, x2, y2)

    def draw_start(self):
        raise NotImplemented()

    def draw_end(self):
        raise NotImplemented()

    def get_cell_center(self, x, y):
        w = self.cell_width
        return (x + 1) * w + w // 2, (y + 1) * w + w // 2

    def draw_solution(self):
        pre = (0, 0)
        for o in self.mmap.solution:
            p1 = self.get_cell_center(*pre)
            p2 = self.get_cell_center(*o)
            self.create_solution_line(p1[0], p1[1], p2[0], p2[1])
            pre = o

    def draw_cell(self, block):
        width = self.cell_width
        x = block.x + 1
        y = block.y + 1
        walls = block.walls
        if walls[0]:
            self.create_line(x * width, y * width, (x + 1) * width + 1, y * width)
        if walls[1]:
            self.create_line((x + 1) * width, y * width, (x + 1) * width, (y + 1) * width + 1)
        if walls[2]:
            self.create_line(x * width, (y + 1) * width, (x + 1) * width + 1, (y + 1) * width)
        if walls[3]:
            self.create_line(x * width, y * width, x * width, (y + 1) * width + 1)

    def draw_map(self):
        for y in range(self.mmap.max_y):
            for x in range(self.mmap.max_x):
                self.draw_cell(self.mmap.mmap[x][y])
        self.draw_start()
        self.draw_end()

class TKDrawMap(DrawMap):
    def __init__(self, mmap):
        super(TKDrawMap, self).__init__(mmap, cell_width=10)
        master = Tk()
        width, height = self.get_map_size()
        self.w = Canvas(master, width=width, height=width)
        self.w.pack()
        self.draw_map()
        mainloop()

    def draw_start(self):
        r = self.cell_width // 3
        x, y = self.get_cell_center(0, 0)
        start = self.w.create_oval(x - r, y - r, x + r, y + r, fill="red")
        self.w.tag_bind(start, '<ButtonPress-1>', lambda e: self.draw_solution())

    def draw_end(self):
        r = self.cell_width // 3
        x, y = self.get_cell_center(self.mmap.max_x - 1, self.mmap.max_y - 1)
        end = self.w.create_oval(x - r, y - r, x + r, y + r, fill="red")
        self.w.tag_bind(end, '<ButtonPress-1>', lambda e: self.reset_map())

    def reset_map(self):
        self.mmap.reset_map()
        self.w.delete('all')
        self.draw_map()

    def create_line(self, x1, y1, x2, y2, **kwargs):
        self.w.create_line(x1, y1, x2, y2, **kwargs)

    def create_solution_line(self, x1, y1, x2, y2):
        self.create_line(x1, y1, x2, y2, fill="red")

def main():
    m = Map()
    m.gen_map(20, 20)
    TKDrawMap(m)

if __name__ == '__main__':
    main()


# Exercise 1
#student_details=[("Sharon",44,95), ("Abraham", 50, 87), ("Tim",19,66),("Francine", 16,80)]

raw_student_details = input("Enter a list in the following format: (name1,age1,score1),(name2,age2,score2):")
student_details = []

for tup in raw_student_details.split('),('):
    #tup looks like `(a,a` or `b,b`
    tup = tup.replace(')','').replace('(','')
    #tup looks like `a,a` or `b,b`
    single_student=tuple(tup.split(','))
    print(single_student)
    name,age,score=single_student

    age=int(age)
    score=float(score)
    single_student_fix=(name,age,score)
    student_details.append(single_student_fix)

print(student_details)

student_details_by_name=sorted(student_details)
print("Student details in alphabetical order:",student_details_by_name)
from operator import itemgetter
student_details_by_age=sorted(student_details, key=itemgetter(1))
print("Student details in age order:",student_details_by_age)
student_details_by_score=sorted(student_details, key=itemgetter(2))
print("Student details in score order:",student_details_by_score)

# Exercise 2

customer_name=input("Please enter the customer's name:")

waiter_name=input("Please enter the name of the waiter:")

item_name=input("Please enter the item ordered:")

item_price=input("Please enter the price of the item:")
item_price=float(item_price)

item_number=input("Please enter the number of items ordered:")
item_number=int(item_number)

item_discount=input("Please enter the percent discount:")
item_discount=int(item_discount)

bill=item_price*item_number*((100-item_discount)/100)
print("***********************************")
print("Customer name: {}" .format(customer_name))
print("Waiter name: {}" .format(waiter_name))
print("***********************************")
print("Item ordered: {}    Price: {}    Number of items: {}" .format(item_name,item_number,item_price))
print("Discount: {} %" .format(item_discount))
print("***********************************")
print("Your total, {}, is {}." .format(customer_name, bill))
print("***********Have a nice day!***************")

# Exercise 3
nums=input("Enter three numbers separated by commas:")

a, b, c = (int(x) for x in nums.split(','))
nums_list=(a,b,c)
nums_list=sorted(nums_list)
print("The numbers in order:",nums_list)
d,e,f=nums_list
print("The biggest number is:",f)

# Exercise 4

letter=input("Enter a letter in the alphabet:")
letter=letter.lower()
alphabet="abcdefghijklmnopqrstuvwxyz"
#check letter
while len(letter) != 1 or alphabet.count(letter)<1:
    letter=input("Invalid entry. Enter a letter in the alphabet:")
vowels="aeiou"
if vowels.count(letter)==1:
    print(letter,"is a vowel.")
else:
    print(letter,"is a consonant.")

# Exercise 5
import random
random_num=random.randint(1,9)
turns=0
check="no"
while turns<5 and check=="no":
    guess=input("Guess a number:")
    guess=int(guess)
    if guess==random_num:
        check="yes"
        print("You guessed the number! It's",random_num)
    else:
        if turns<5:
            print("Wrong number!")
        turns=turns+1
if check=="no":
    print("Sorry, you didn't guess the number. The number was",random_num)

# Exercise 6

for x in range(20):
  print(x+1)

# Exercise 7 & 8
million=[]
for z in range(1,1000001):
    million.append(z)

print(min(million))
print(max(million))
print(sum(million))

#for m in million:
#   print(m)

# Exercise 9
item_list=["red","orange","yellow","green","blue","purple","red","orange","yellow"]
index_list = [i for i, value in enumerate(item_list) if value == "orange"]
print("The list is:",item_list)
print("red is found in the following indeces:",index_list)

# Exercise 10
list_odd=[1,3,5,9,11]
list_even=[2,4,6,8,10]
done="no"
i=0
full_list=[]
while i<len(list_odd)+len(list_even):
    if i<len(list_odd):
        full_list.append(list_odd[i])
    else:
        full_list.append(list_even[i-(len(list_odd))])
    i=i+1
print("List of odd nums:",list_odd)
print("LIst of even nums:",list_even)
print("List of all nums:",full_list)


try:
    from urllib.request import urlopen,urlretrieve
except:
    from urllib import urlopen,urlretrieve
import threading
import time
import ssl

ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE

start = time.time()
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.options import Options

from contextlib import contextmanager
from selenium.common.exceptions import WebDriverException
from selenium.webdriver.support.ui import WebDriverWait
import os, errno

@contextmanager
def wait_for_page_load(driver, timeout=30.0):
    source = driver.page_source
    yield
    WebDriverWait(driver, timeout, ignored_exceptions=(WebDriverException,)).until(lambda d: source != d.page_source)
def require_dir(path):
    try:
        os.makedirs(path)
    except OSError as exc:
        if exc.errno != errno.EEXIST:
            raise

def get_catagory(catagory):
    options = Options()
    options.add_argument("--headless")
    driver = webdriver.Firefox(firefox_options=options)
    for pageno in range(1,100):
        url = "https://www.artsy.net/collect?page={}&medium={}".format(pageno,catagory)
        driver.get(url)
        data = driver.page_source
        soup = BeautifulSoup(data,"html.parser")
        content=soup.find("div", { "class" : "cf-artworks" })
        img_links= content.findAll("img")
        for img in img_links:
            link=img['src']
            filename=img['alt']
            name ="".join([c for c in filename if c.isalpha() or c.isdigit() or c==' ']).rstrip()
            directory = os.path.join(os.path.dirname(os.path.abspath(__file__)), catagory)
            require_dir(directory)
            filename = os.path.join(directory, name)
            print (filename)
            try:
                with urlopen(link, context=ctx) as u, \
                    open("{}.jpg".format(filename), 'wb') as f:
                        f.write(u.read())
                f.close()
            except:
                print("error in {}".format(filename))
            #urlretrieve(link, ,context=ctx)
    driver.quit()

catagories=["painting", "photography", "drawing", "sculpture"]
for catagory in catagories:
    threads = [threading.Thread(target=get_catagory, args=(catagory,)) for catagory in catagories]
for thread in threads:
    thread.start()
for thread in threads:
    thread.join()


from setuptools import setup

package_name = 'reactive_moving_by_hcsr04'

setup(
    name=package_name,
    version='0.0.0',
    packages=[package_name],
    data_files=[
        ('share/ament_index/resource_index/packages',
            ['resource/' + package_name]),
        ('share/' + package_name, ['package.xml']),
    ],
    install_requires=['setuptools'],
    zip_safe=True,
    maintainer='taisyu',
    maintainer_email='t_shiba117@yahoo.co.jp',
    description='TODO: Package description',
    license='TODO: License declaration',
    tests_require=['pytest'],
    entry_points={
        'console_scripts': [
            'automoving_by_hcsr04= reactive_moving_by_hcsr04.automoving_by_hcsr04:main'
        ],
    },
)


import numpy as np
import csv

f = open("train.csv", 'r', encoding='big5')
lines = csv.reader(f, delimiter=",")

data = []
for i in range(18):
	data.append([])

k = 0
start = 1
for line in lines:
    if start == 0:
        for i in range(3, 27):
            if line[i] == "NR":
                    data[k].append( float(0) )
            else:
                data[k].append( float(line[i]) )
        k += 1
        if k == 18:
            k = 0
    if start == 1:
        start = 0
train_data_x = []
train_data_y = []

data = np.array(data)
length = 20*24 - 9

for m in range(12):
    for i in range(length):
        tmp = []
        tmp.append( float(1) )
        for j in range(18):
            for k in range(9):
                tmp.append( data[j][480*m+i+k] )
        train_data_x.append( tmp )
        train_data_y.append( data[9][480*m+i+9] )

train_data_x = np.array(train_data_x)
train_data_y = np.array(train_data_y)
#print(train_data_x.shape[1])

dim = train_data_x.shape[1]
w = np.zeros(dim)
pre_grad = np.zeros(dim)
x_trans = train_data_x.transpose()
x_trans_y = np.dot(x_trans, train_data_y)
x_trans_x = np.dot(x_trans, train_data_x)

learning_rate = 10
iteration = 100000

for i in range(iteration):
    '''
    y_ = np.dot(train_data_x, w)
    loss = y_ - train_data_y
    grad = 2*np.dot(x_trans, loss)
    '''
    grad = 2*( np.dot(x_trans_x, w) - x_trans_y )
    pre_grad += grad**2
    ada = np.sqrt(pre_grad)
    w -= learning_rate * grad / ada

np.save('model.npy', w)


# -*- coding: utf-8 -*-
"""
Created on Thu Jul 30 09:24:56 2020

@author: mape
"""
import win32com.client.dynamic
import tkinter as tk
from tkinter import filedialog as fd
from pathlib import Path
from tkinter import ttk

class MainApplication(tk.Frame):
    def __init__(self, master, *args, **kwargs):
        tk.Frame.__init__(self, master, *args, **kwargs)
        self.master = master

        ##connection to path
        self.f = open(Path.home() / 'python32' / 'python_dir.txt', mode='r')
        self.path = Path.joinpath(Path(self.f.readline()),'VISUM_Tools','Tkinter.txt')
        self.f = self.path.read_text().split('\n')

        ##menubar
        self.menubar = tk.Menu(self.master, bg='white')
        self.filemenu = tk.Menu(self.menubar, tearoff=0)
        self.filemenu.add_command(label = "Save as...")
        self.menubar.add_cascade(label = "File", menu = self.filemenu)
        self.menubar.add_cascade(label = "Help", menu = self.filemenu)
        self.master.config(menu = self.menubar)

        ##master window
        self.master.title("Changes to VISUM networks")
        self.master.geometry("520x200")

        ##frame1
        self.fr1 = tk.Frame(self.master)
        tk.Label(self.fr1,text = "VISUM", pady=10, relief = "groove", font='Helvetica 12 bold').grid(
            row=0, column=0, pady=3, columnspan=3, sticky=("N", "S", "E", "W")) ##geometrymanager grid

        #entry
        self.ent1 = tk.Entry(self.fr1, font=40, width = 40)
        self.ent1.grid(row=1,column=0)

        #button
        tk.Button(self.fr1, text="browse", font='Helvetica 8 bold', command = self.open_file).grid(
            row=1, column=1, padx=5, pady=5)
        tk.Button(self.fr1, text="start", font='Helvetica 8 bold', command = self.VISUM_start).grid(
            row=1, column=2, padx=5, pady=5)
        tk.Button(self.fr1, text="close \n VISUM", font='Helvetica 8 bold', command = self.VISUM_end).grid(
            row=2, column=1, sticky="wens", padx=5, pady=5)
        tk.Button(self.fr1, text="end()", font='Helvetica 8 bold', command = self.end).grid(
            row=3, column=1, sticky="wens", padx=5, pady=5)

        #pack Frame 1
        self.fr1.grid(row=0, column=0, padx=5, pady=5)

    def open_file(self):
        try:
            self.dat_in = fd.askopenfilename(initialdir = self.f[0],title = "Dateiauswahl",
                filetypes = [("Versionsdatei", ".ver")])
            self.ent1.insert(tk.END, self.dat_in)
        except IOError: pass

    def VISUM_start (self):
        self.VISUM = win32com.client.dynamic.Dispatch("Visum.Visum.20")
        try: self.VISUM.loadversion(self.dat_in) ##Netz,Szenrario,VISUM-Instanz
        except: pass

    def VISUM_end (self):
        del self.VISUM

    def end(self):
        self.master.destroy()

if __name__ == "__main__":
    root = tk.Tk()
    MainApplication(root)
    root.iconify()
    root.update()
    root.deiconify()
    root.mainloop()

del root


from db.db_functions import DbFunctions

class NotifierCalculations:

    def __init__(self, db: DbFunctions):
        self.db = db

    def _get_target_value(self, aim_id: int, task_id: int, value_to_divide: int):
        return self.db.select_task_statistics(
            aim_id=aim_id,
            task_id=task_id,
            value_for_division=value_to_divide
        )

    def get_notification_to_send(self, aim_id: int, task_id: int, value_to_divide: int):
        target_value_info = self._get_target_value(
            aim_id=aim_id,
            task_id=task_id,
            value_to_divide=value_to_divide
        )
        tv_task_id, tv_task_name, tv_target_value, tv_residue_value, \
        tv_residue_days_or_weeks, tv_target_by_period = target_value_info
        if value_to_divide == 1:
            text = f"Целевые показатели на день для задачи {tv_task_name}:\n" \
                   f"Целевое значение: {tv_target_value}\n" \
                   f"Осталось до целевого значения: {tv_residue_value}\n" \
                   f"Осталось до дедлайна: {tv_residue_days_or_weeks}\n" \
                   f"За день нужно сделать: {tv_target_by_period}"
            return text
        elif value_to_divide == 7:
            text = f"Целевые показатели на неделю для задачи {tv_task_name}:\n" \
                   f"Целевое значение: {tv_target_value}\n" \
                   f"Осталось до целевого значения: {tv_residue_value}\n" \
                   f"Осталось недель до дедлайна: {tv_residue_days_or_weeks}\n" \
                   f"За неделю нужно сделать: {tv_target_by_period}"
            return text


import json
from odoo import http
from odoo.tools.config import config
from odoo.http import request, Response
import re

class PureControllerMixin(object):
    @staticmethod
    def patch_for_json(path_re):
        # this is to avoid Odoo, which assumes json always means json+rpc,
        # complaining about "function declared as capable of handling request
        # of type 'http' but called with a request of type 'json'"
        path_re = re.compile(path_re)
        orig_get_request = http.Root.get_request

        def get_request(self, httprequest):
            if path_re.match(httprequest.path):
                return http.HttpRequest(httprequest)
            return orig_get_request(self, httprequest)

        http.Root.get_request = get_request

class SaleOrderRequest(http.Controller, PureControllerMixin):

    # it means all response from endpoint with path /api/... will got purificiation from standard odoo POST/PUT HTTP response
    PureControllerMixin.patch_for_json("^/api/")

    @http.route('/api/order', auth='public', csrf=False, methods=['PUT'])
    def update_order(self):
        data = json.loads((request.httprequest.data).decode())
        data_response = {}
        success_status = False
        headers_json = {'Content-Type': 'application/json'}
        access_token = str(request.env['ir.config_parameter'].sudo().get_param('efishery.static_token'))
        headers = http.request.httprequest.headers
        # check authorize by static token
        if headers.get('Authorization') == access_token:
            request_update = data.get('data')
            if not request_update:
                response_status = "400 Bad Request"
                response_message = "Payload Request is empty"
            else:
                # search sale order by id
                sale_order = request.env['sale.order'].sudo().browse(request_update.get('id'))

                if sale_order.state != "draft":
                    response_status = "400 Bad Request"
                    response_message = "sales order {}/{} state is {}, you can only update the sale order when state in draft/Quotation".format(sale_order.name, sale_order.external_reference, sale_order.state)
                else:
                    new_order_line = []
                    deleted_order_line = []
                    save_order_line = []
                    # check existing products in order line
                    for update_line in request_update.get('order_line'):
                        existing_product = sale_order.order_line.filtered(lambda x:x.product_id.id == update_line['product_id'] and x.product_uom.id == update_line['product_uom'])
                        if existing_product:
                            if existing_product.price_unit != update_line['price_unit'] or existing_product.product_uom_qty != update_line['product_uom_qty']:
                                existing_product.price_unit = update_line['price_unit']
                                existing_product.product_uom_qty = update_line['product_uom_qty']
                        else:
                            new_order_line.append((0, 0, {
                                'product_id': update_line['product_id'],
                                'product_uom': update_line['product_uom'],
                                'product_uom_qty': update_line['product_uom_qty'],
                                'price_unit': update_line['price_unit']
                            }))

                    # update sale order
                    sale_order.sudo().write({
                        'partner_id': request_update.get('partner_id'),
                        'date_order': request_update.get('date_order'),
                        'external_reference': request_update.get('external_reference'),
                        'order_line': new_order_line
                    })

                    # after we update the sale order, and the sale order is updated with new data,
                    # now we find the product and uom in sale order that isnt in order line request body
                    # to remove it from sale order
                    for update_line in request_update.get('order_line'):
                        existing_product = sale_order.order_line.filtered(lambda x:x.product_id.id == update_line['product_id'] and x.product_uom.id == update_line['product_uom'])
                        if existing_product:
                            save_order_line.append(existing_product.id)

                    for remove_line in sale_order.order_line:
                        if remove_line.id not in save_order_line:
                            deleted_order_line.append((2, remove_line.id))

                    sale_order.order_line = deleted_order_line

                    success_status = True
                    response_status = "200 OK"
                    response_message = "sales order updated"
                    data_response = self.data_response_sale_order(sale_order)
        else:
            response_status = "401 Unauthorized"
            response_message = "Token Not Found"

        result = {
            "success": success_status,
            "message": response_message,
            "data": data_response
        }

        # return response
        return Response(json.dumps(result), headers=headers_json, status=response_status)

    def data_response_sale_order(self, sale_order):
        result = {
            'so_id': sale_order.id,
            'partner_id': sale_order.partner_id.id,
            'date_order': str(sale_order.date_order),
            'external_reference': sale_order.external_reference,
            'so_number': sale_order.name,
            'order_line': [{
                'order_line_id': o_line.id,
                'product_id': o_line.product_id.id,
                'product_uom': o_line.product_uom.id,
                'product_uom_qty': o_line.product_uom_qty,
                'price_unit': o_line.price_unit
            } for o_line in sale_order.order_line]
        }

        return result

    @http.route('/api/order', auth='public', csrf=False, methods=['POST'])
    def create_order(self):
        data = json.loads((request.httprequest.data).decode())
        data_response = {}
        success_status = False
        headers_json = {'Content-Type': 'application/json'}
        access_token = str(request.env['ir.config_parameter'].sudo().get_param('efishery.static_token'))
        headers = http.request.httprequest.headers
        # check authorize by static token
        if headers.get('Authorization') == access_token:
            request_order = data.get('data')
            if request_order:
                if not request_order.get('partner_id') or not request_order.get('partner_id') \
                    or not request_order.get('date_order') or not request_order.get('company_id') \
                    or not request_order.get('order_line') or not request_order.get('external_reference'):
                    response_status = "400 Bad Request"
                    response_message = "Payload Request is not complete"
                else:
                    order_lines = [(0, 0, {
                        'product_id': line['product_id'],
                        'product_uom': line['product_uom'],
                        'product_uom_qty': line['product_uom_qty'],
                        'price_unit': line['price_unit']
                    }) for line in request_order.get('order_line')]

                    # create new Sale Order.. we use admin as sales person, since we create it from external odoo UI
                    sale_order = request.env['sale.order'].sudo().create({
                        'partner_id': request_order.get('partner_id'),
                        'date_order': request_order.get('date_order'),
                        'external_reference': request_order.get('external_reference'),
                        'user_id': 2,
                        'company_id': request_order.get('company_id'),
                        'order_line': order_lines
                    })

                    success_status = True
                    response_status = "200 OK"
                    response_message = "sales order created"
                    data_response = self.data_response_sale_order(sale_order)
            else:
                response_status = "400 Bad Request"
                response_message = "Payload Request is empty"
        else:
            response_status = "401 Unauthorized"
            response_message = "Token Not Found"

        result = {
            "success": success_status,
            "message": response_message,
            "data": data_response
        }

        # return response
        return Response(json.dumps(result), headers=headers_json, status=response_status)

    def query_get_order_details(self, order_ids):
        # we use sql query to search data cause it's faster than odoo ORM..
        # on odoo 11-13 when we search data by using many table relation,
        # odoo ORM is slower than we use sql query and inject it to the database
        # dunno about odoo14 but when i check the repo they still use some sql query on their code
        request.env.cr.execute("""
            SELECT
                so.id as order_id
                , so.name
                , so.partner_id
                , so.date_order
                , so.company_id
                , json_build_object(
                    'partner', json_build_object(
                        'id', so.partner_id,
                        'name', rp.name,
                        'address', CONCAT(rp.street, ', ', rp.city)
                    ),
                    'company', json_build_object(
                        'id', so.company_id,
                        'name', rc.name,
                        'address', CONCAT(rcp.street, ', ', rcp.city)
                    )
                ) as relationship
                , jsonb_agg(
                    json_build_object(
                        'product', json_build_object(
                            'id', sol.product_id,
                            'name', pt.name,
                            'sku_code', pt.default_code,
                            'description', pt.description
                        ),
                        'uom', json_build_object(
                            'id', sol.product_uom,
                            'name', uom.name,
                            'description', uc.name
                        ),
                        'product_uom_qty', sol.product_uom_qty,
                        'price_unit', sol.price_unit
                    )
                ) as order_lines
            FROM sale_order so
            JOIN res_partner rp ON so.partner_id = rp.id
            JOIN res_company rc ON so.company_id = rc.id
            JOIN res_partner rcp on rc.partner_id = rcp.id
            JOIN sale_order_line sol ON sol.order_id = so.id
            JOIN product_product pp ON sol.product_id = pp.id
            JOIN product_template pt ON pp.product_tmpl_id = pt.id
            JOIN uom_uom uom ON sol.product_uom = uom.id
            JOIN uom_category uc ON uom.category_id = uc.id
            WHERE
                so.id IN ({})
            GROUP BY
                so.id, rp.id, rc.id, rcp.id
        """.format(
                (",".join(["'%s'" % (so_id) for so_id in order_ids]))
            )
        )

        return request.env.cr.dictfetchall()

    def get_order_details_state(self, order_ids):
        data = {}
        success_status = False
        response_message = ''
        if not order_ids:
            response_status = "400 Bad request"
            response_message = "order id not found"
        else:

            get_order_details = self.query_get_order_details(order_ids)

            # check if data that we search is found
            if get_order_details:
                success_status = True
                response_status = "200 OK"
                response_message = "Data found"
                data = get_order_details

                for d in data:
                    # convert date time format to string, i got error serialize on date time field so i need to convert it to string first
                    d['date_order'] = str(d['date_order'])
            else:
                response_status = "400 Bad request"
                response_message = "Sale Order by order id %s is not found" % (order_id)

        return {
            'response_status': response_status,
            'response_message': response_message,
            'success_status': success_status,
            'data': data
        }

    @http.route('/api/order', auth='public', methods=['GET'])
    def get_order_bulk(self):
        '''
            GET Sale Order Details by order id bulky
        '''
        data = json.loads((request.httprequest.data).decode())
        data_response = {}
        success_status = False
        headers_json = {'Content-Type': 'application/json'}
        access_token = str(request.env['ir.config_parameter'].sudo().get_param('efishery.static_token'))
        headers = http.request.httprequest.headers
        # check authorize by static token
        if headers.get('Authorization') == access_token:
            # check if order_ids sent is zero/empty
            order_ids = data.get('id')
            check_order_details = self.get_order_details_state(order_ids)
            response_status = check_order_details['response_status']
            response_message = check_order_details['response_message']
            success_status = check_order_details['success_status']
            data_response = check_order_details['data']
        else:
            response_status = "401 Unauthorized"
            response_message = "Token Not Found"

        result = {
            "success": success_status,
            "message": response_message,
            "data": data_response
        }

        # return response
        return Response(json.dumps(result), headers=headers_json, status=response_status)

    @http.route('/api/order/<order_id>', auth='public', methods=['GET'])
    def get_order(self, order_id=0):
        '''
            GET Sale Order Details by order id
        '''
        data = {}
        success_status = False
        headers_json = {'Content-Type': 'application/json'}
        access_token = str(request.env['ir.config_parameter'].sudo().get_param('efishery.static_token'))
        headers = http.request.httprequest.headers
        # check authorize by static token
        if headers.get('Authorization') == access_token:
            # check if order_id sent is zero/empty
            order_ids = [order_id]
            check_order_details = self.get_order_details_state(order_ids)
            response_status = check_order_details['response_status']
            response_message = check_order_details['response_message']
            success_status = check_order_details['success_status']
            data = check_order_details['data'][0]
        else:
            response_status = "401 Unauthorized"
            response_message = "Token Not Found"

        result = {
            "success": success_status,
            "message": response_message,
            "data": data
        }

        # return response
        return Response(json.dumps(result), headers=headers_json, status=response_status)


import os, sys
os.environ["CUDA_VISIBLE_DEVICES"]="1"

import tensorflow as tf
import sonnet as snt

from config import FLAGS
from input_2 import Input
from model_wgan import Adaptor, ReducedClassifier, Classifier
from utils import LossClassification, Summaries, Metrics, reset_metrics
from optimizer import Adam
# from optimizer import SGD
from tensorflow.python.platform import app

def train(data_path, adaptor, classifier, summ):

    input_ = Input(FLAGS.train_batch_size, FLAGS.num_points)
    waves, labels = input_(data_path)

    # Calculate the loss of the model.
    if FLAGS.adp:
        logits = tf.stop_gradient(adaptor(waves))
        # logits = adaptor(waves)
        logits = classifier(logits)
    else:
        logits = classifier(waves, expand_dims = True)

    loss = LossClassification(FLAGS.num_classes)(logits, labels)

    opt = Adam(FLAGS.learning_rate, lr_decay = True, lr_decay_steps = FLAGS.lr_decay_steps,
               lr_decay_factor = FLAGS.lr_decay_factor)

    graph_regularizers = tf.get_collection(tf.GraphKeys.REGULARIZATION_LOSSES)
    total_regularization_loss = tf.reduce_sum(graph_regularizers)

    train_op = opt(loss + total_regularization_loss)

    summ.register('train', 'train_loss', loss)

    train_summ_op = summ('train')

    return loss, train_op, train_summ_op

def xval(data_path, adaptor, classifier, summ):

    input_ = Input(FLAGS.xval_batch_size, FLAGS.num_points)
    waves, labels = input_(data_path)

    # Calculate the loss of the model.
    if FLAGS.adp:
        logits = adaptor(waves)
        logits = classifier(logits)
    else:
        logits = classifier(waves, expand_dims = True)

    logits = tf.argmax(logits, axis = -1)

    metrics = Metrics("accuracy")
    with tf.control_dependencies([tf.assert_equal(tf.rank(labels), tf.rank(logits))]):
        score, xval_accu_op = metrics(labels, logits)

    assert summ, "invalid summary helper object"
    summ.register('xval', 'accuracy', score)
    xval_summ_op = summ('xval')

    return xval_accu_op, xval_summ_op

def main(unused_argv):
    summ = Summaries()

    if FLAGS.data_dir == '' or not os.path.exists(FLAGS.data_dir):
        raise ValueError('invalid data directory {}'.format(FLAGS.data_dir))

    train_data_path = os.path.join(FLAGS.data_dir, 'eeg-train.tfr')
    xval_data_path = os.path.join(FLAGS.data_dir, 'eeg-test.tfr')

    if FLAGS.output_dir == '':
        raise ValueError('invalid output directory {}'.format(FLAGS.output_dir))
    elif not os.path.exists(FLAGS.output_dir):
        os.makedirs(FLAGS.output_dir)

    if FLAGS.checkpoint_dir == '':
        raise ValueError('invalid checkpoint directory {}'.format(FLAGS.output_dir))

    event_log_dir = os.path.join(FLAGS.output_dir, '')

    checkpoint_path = os.path.join(FLAGS.output_dir, 'model.ckpt')

    print('Constructing models.')

    if FLAGS.adp:
        adaptor = Adaptor()
        classifier = ReducedClassifier()

        train_loss, train_op, train_summ_op = \
            train(train_data_path, adaptor, classifier, summ)
        xval_op, xval_summ_op = xval(xval_data_path, adaptor, classifier, summ)
    else:
        classifier = Classifier(FLAGS.num_points, FLAGS.sampling_rate)

        train_loss, train_op, train_summ_op = \
            train(train_data_path, None, classifier, summ)
        xval_op, xval_summ_op = xval(xval_data_path, None, classifier, summ)

    print('Constructing saver.')

    if FLAGS.adp:
        variables = snt.get_variables_in_module(adaptor) + snt.get_variables_in_module(classifier)
        saver_adaptor = tf.train.Saver(snt.get_variables_in_module(adaptor))
        saver = tf.train.Saver(variables)
    else:
        saver = tf.train.Saver(snt.get_variables_in_module(classifier))

    # Start running operations on the Graph. allow_soft_placement must be set to
    # True to as some of the ops do not have GPU implementations.
    config = tf.ConfigProto(allow_soft_placement = True, log_device_placement = False)

    assert (FLAGS.gpus != ''), 'invalid GPU specification'
    config.gpu_options.visible_device_list = FLAGS.gpus

    # Build an initialization operation to run below.
    init = [tf.global_variables_initializer(), tf.local_variables_initializer()]

    with tf.Session(config = config) as sess:
        sess.run(init)

        if FLAGS.adp:
            ckpt = tf.train.get_checkpoint_state(FLAGS.checkpoint_dir)
            if ckpt and ckpt.model_checkpoint_path:
                # Restores from checkpoint
                saver_adaptor.restore(sess, ckpt.model_checkpoint_path)
                # Assuming model_checkpoint_path looks something like:
                #   /my-favorite-path/cifar10_train/model.ckpt-0,
                # extract global_step from it.
                global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]
            else:
                print('No checkpoint file found')
                return

        writer = tf.summary.FileWriter(event_log_dir, graph = sess.graph)

        # Run training.
        for itr in range(FLAGS.num_iterations):
            cost, _, train_summ_str = sess.run([train_loss, train_op, train_summ_op])
            # Print info: iteration #, cost.
            print(str(itr) + ' ' + str(cost))

            if itr % FLAGS.validation_interval == 1:
                # Run through validation set.
                sess.run(xval_op)
                val_summ_str = sess.run(xval_summ_op)
                writer.add_summary(val_summ_str, itr)
                reset_metrics(sess)

            if itr % FLAGS.summary_interval == 1:
                writer.add_summary(train_summ_str, itr)

        # coord.request_stop()
        # coord.join(threads)

        tf.logging.info('Saving model.')
        saver.save(sess, checkpoint_path)
        tf.logging.info('Training complete')

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


"""This module is used to apply logging related functions"""

import ast
from datetime import datetime
from side_funcs import date_difference

def format_request(requested: list) -> str:
    """Applying log format for requests"""
    if isinstance(requested, list):
        log_info = 'REQUESTS: '
        for index, item in enumerate(requested):
            log_info += f"{item}"
            if index != len(requested) - 1:
                log_info += " - "
        return log_info
    return str(requested)

def restore_briefings(log_file):
    """Used to restore hourly briefings"""
    with open(log_file) as logfile:
        brief_time = ''
        for line in logfile.readlines():
            if '__main__ - INFO' in line[:15]:
                # searching for the called briefings in log lines
                if line[46:59] == 'BRIEF CALLED:':
                    brief_time = line[60:].strip()
        open(log_file, 'w').close()

        try:
            # formatting the date
            brief_time = datetime.strptime(brief_time, '%B %d, %H:%M')
            last_time = brief_time.strftime('%B %d, %H:%M')
            brief_time = brief_time.strftime(f"{datetime.today().strftime('%Y')}-%m-%d %H:%M")
            return date_difference(brief_time), last_time
        except ValueError:
            return -3600, brief_time  # past hour is reported in case no 'BRIEF CALLED:' was found

def restore_alarms(log_file):
    """Used to restore previous alarms"""
    with open(log_file) as logfile:
        added_alarms = []
        removed_alarms = []
        for line in logfile.readlines():
            if '__main__ - INFO' in line[:15]:
                # searching for alarm / notification related log lines
                if line[46:56] == 'NEW ALARM:':
                    added_alarms.append(ast.literal_eval(line[57:].strip()))
                elif line[46:60] == 'REMOVED ALARM:':
                    removed_alarms.append(ast.literal_eval(line[61:].strip()))
                elif line[46:57] == 'NEW NOTIFY:':
                    removed_alarms.append(ast.literal_eval(line[58:].strip()))

        added_alarms = filtered(added_alarms, removed_alarms)
        return formatted(added_alarms)

def filtered(alarms: list, removed: list) -> list:
    """Filtering removed out of alarms"""
    filtered_alarms = []
    for alarm in alarms:
        for remove in removed:
            if alarm['id'] == remove['id']:
                filtered_alarms.append(alarm)
                break
    return [i for i in alarms if i not in filtered_alarms]

def formatted(alarms: list) -> list:
    """Applying date format to retrieved alarms"""
    for alarm in alarms:
        alarm['content'] = alarm['content'][5:]
        alarm['content'] = datetime.strptime(alarm['content'], '%B %d, %Y %H:%M')
        alarm['content'] = alarm['content'].strftime("%Y-%m-%d %H:%M")
    return alarms


"""helper for results_sidebar.py"""
from copy import deepcopy

def build_pruned_tree(tree, cases):
    """
    Build new tree with only the specifiied cases

    Parameters
    ----------
    tree : list[...]
        the sidebar tree
    cases : list[int]
        the cases to keep

    Returns
    -------
    tree_final : list[...]
        the updated sidebar tree

    Examples
    --------
    form = [
        [u'Geometry', None, [
            (u'NodeID', 0, []),
            (u'ElementID', 1, []),
            (u'PropertyID', 2, []),
            (u'MaterialID', 3, []),
            (u'E', 4, []),
            (u'Element Checks', None, [
                (u'ElementDim', 5, []),
                (u'Min Edge Length', 6, []),
                (u'Min Interior Angle', 7, []),
                (u'Max Interior Angle', 8, [])],
            ),],
        ],
    ]
    cases = [1, 2, 5]
    tree2 = prune_tree(form, cases)
    >>> tree2
    [
        [u'Geometry', None, [
            (u'ElementID', 1, []),
            (u'PropertyID', 2, []),
            (u'Element Checks', None, [
                (u'ElementDim', 5, []),
            ),],
        ],
    ]

    """
    tree_final = []
    is_results, tree_final = _build_pruned_tree(tree, cases, tree_final)
    return tree_final

def _build_pruned_tree(tree, cases, tree2):
    """helper method for ``build_pruned_tree``"""
    is_results = False
    if isinstance(tree[0], str):
        try:
            name, icase, cases2 = tree
        except ValueError:
            print(tree)
            raise

        if isinstance(icase, int):
            assert cases2 == [], tree
            if icase in cases:
                is_results = True
                tree2.append(tree)
        else:
            assert icase is None, tree
            tree3 = []
            for case in cases2:
                is_resultsi, tree3 = _build_pruned_tree(case, cases, tree3)
                if is_resultsi:
                    is_results = True
            if is_results:
                tree2.append((name, icase, tree3))
    else:
        tree3 = []
        for case in tree:
            is_resultsi, tree3 = _build_pruned_tree(case, cases, tree3)
            if is_resultsi:
                is_results = True
        tree2 = tree3
    return is_results, tree2

def get_cases_from_tree(tree):
    """
    Get the cases found in the tree

    Parameters
    ----------
    tree : list[...]
        the sidebar tree

    Returns
    -------
    cases : list[int]
        the cases in the tree

    Examples
    --------
    form = [
        [u'Geometry', None, [
            (u'NodeID', 0, []),
            (u'ElementID', 1, []),
            (u'PropertyID', 2, []),
            (u'MaterialID', 3, []),
            (u'E', 4, []),
            (u'Element Checks', None, [
                (u'ElementDim', 5, []),
                (u'Min Edge Length', 6, []),
                (u'Min Interior Angle', 7, []),
                (u'Max Interior Angle', 8, [])],
            ),],
        ],
    ]
    cases = get_cases_from_tree(form)
    >>> cases
    [0, 1, 2, 3, 4, 5, 6, 7, 8]

    """
    return _get_cases_from_tree(tree)[0]

def _get_cases_from_tree(tree, path={}, path2=[], level=0):
    """
    Get the cases found in the tree

    Parameters
    ----------
    tree : list[...]
        the sidebar tree

    Returns
    -------
    cases : list[int]
        the cases in the tree

    Examples
    --------
    form = [
        [u'Geometry', None, [
            (u'NodeID', 0, []),
            (u'ElementID', 1, []),
            (u'PropertyID', 2, []),
            (u'MaterialID', 3, []),
            (u'E', 4, []),
            (u'Element Checks', None, [
                (u'ElementDim', 5, []),
                (u'Min Edge Length', 6, []),
                (u'Min Interior Angle', 7, []),
                (u'Max Interior Angle', 8, [])],
            ),],
        ],
    ]
    cases, path = get_cases_from_tree(form)
    >>> cases
    [0, 1, 2, 3, 4, 5, 6, 7, 8]
    >>> path
    {
        0 : ['Geometry', 'NodeID'],
    }

    """
    cases = []
    nspaces = level * '  '
    if isinstance(tree[0], str):
        try:
            name, icase, cases2 = tree
        except ValueError:  # pragma: no cover
            print(tree)
            raise

        if isinstance(icase, int):
            path[icase] = (name, path2 + [name])
            #print(f'{nspaces}adding icase={icase} name={name!r}')
            assert cases2 == [], tree
            cases.append(icase)
        else:
            #print(f'{nspaces}digging...name={name!r}')
            assert icase is None or icase == '', tree
            icase = None
            path2.append(name)
            #path.append(tree[0])
            for case in cases2:
                cases2, path, path2 = _get_cases_from_tree(case, path, deepcopy(path2), level+1)
                cases += cases2
    else:
        #path.append(tree[0])
        for case in tree:
            cases2, path, path2 = _get_cases_from_tree(case, path, path2)
            cases += cases2
    return cases, path, path2


# -*- coding: utf-8 -*-
import scrapy

class AskaaronleeSpider(scrapy.Spider):
    name = "askaaronlee"
    start_urls = ["https://askaaronlee.com/"]

    def parse(self, response):
        for page in response.xpath(
            "//div[contains(@class,'pagination-next')]/a/@href"
        ).extract():
            yield scrapy.Request(url=page, callback=self.parse)
        for post in response.xpath("//a[@class='entry-title-link']/@href").extract():
            yield scrapy.Request(url=post, callback=self.parse_post)

    def parse_post(self, response):
        yield {
            "Date": response.xpath("//time[@class='entry-time']/@datetime")
            .extract_first()
            .split("T")[0],
            "Site url": self.start_urls[0],
            "Article url": response.url,
            "Author": response.xpath(
                "//span[@class='entry-author-name']/text()"
            ).extract_first(),
            "Title": response.xpath("//h1/text()").extract_first(),
            "Article body text": " ".join(
                response.xpath("//div[@class='entry-content']//text()").extract()
            ),
        }


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Song Jin
# 画仿真实验的图，8张图放两排，每个位置画一张
import os
import subprocess
import argparse
from datetime import datetime
from shutil import copyfile

import numpy as np
from colorama import init, Fore
import matplotlib.pyplot as plt
from matplotlib import rc
from mpl_toolkits.mplot3d import Axes3D

import exp_utils as eu

import matplotlib
import rospkg
print(matplotlib.__version__)

_save_ext = ".pdf"

rc('font', **{'family': 'serif', 'serif': ['Cardo'], 'size': 20})
# rc('text', usetex=True)

def normalize(x):
    vmax = np.max(x)
    vmin = np.min(x)
    return np.array([(v - vmin)/(vmax-vmin) for v in x.tolist()])

def plotSingle(ax, vos_pos, points, plt_rng, res, nm):
    ax.scatter(points[0:-1:3, 0], points[0:-1:3, 1], points[0:-1:3, 2])
    ax.set_xlim(-plt_rng/2, plt_rng/2)
    ax.set_ylim(-plt_rng/2, plt_rng/2)
    ax.set_zlim(-plt_rng/2, plt_rng/2)
    colors = [[0, v, 0] for v in res['nvalues']]
    arrow_colors = colors + [v for v in colors for _ in (0, 1)]
    # arrow_colors = colors + [[1, 0, 1] for v in colors for _ in (0, 1)]
    ax.quiver(vox_pos[:, 0], vox_pos[:, 1], vox_pos[:, 2],
              res['views'][:, 0], res['views'][:, 1], res['views'][:, 2],
              length=0.3, color=arrow_colors,
              arrow_length_ratio=0.7)
    ax.set_title(nm)
    ax.view_init(azim=0, elev=90)

def plotAllinSingel(ax, vos_pos, index, map_points, view_points, plt_rng, res, nm):
    #ax.scatter(map_points[0:-1:3, 0], map_points[0:-1:3, 1], map_points[0:-1:3, 2], c='blue')
    ax.scatter(view_points[0:-1:3, 0], view_points[0:-1:3, 1], view_points[0:-1:3, 2], c='red')
    ax.set_xlim(-plt_rng/2, plt_rng/2)
    ax.set_ylim(-plt_rng/2, plt_rng/2)
    ax.set_zlim(-plt_rng/2, plt_rng/2)
    # colors = [[0, v, 0] for v in res['nvalues']]
    # arrow_colors = colors + [v for v in colors for _ in (0, 1)]
    # arrow_colors = colors + [[1, 0, 1] for v in colors for _ in (0, 1)]
    ax.quiver(vox_pos[index, 0], vox_pos[index, 1], vox_pos[index, 2],
              res['trace']['views'][index, 0], res['trace']['views'][index, 1], res['trace']['views'][index, 2],
              length=1, color='red', pivot='tail',
              arrow_length_ratio=0.7)
    ax.quiver(vox_pos[index, 0], vox_pos[index, 1], vox_pos[index, 2],
              res['det']['views'][index, 0], res['det']['views'][index, 1], res['det']['views'][index, 2],
              length=1, color='green',pivot='tail',
              arrow_length_ratio=0.7)
    ax.quiver(vox_pos[index, 0], vox_pos[index, 1], vox_pos[index, 2],
              res['mineig']['views'][index, 0], res['mineig']['views'][index, 1], res['mineig']['views'][index, 2],
              length=1, color='blue',pivot='tail',
              arrow_length_ratio=0.7)
    # plot circle
    theta = np.linspace(-np.pi/2, np.pi/2, 201)
    x = 1.35*np.cos(theta)
    y = 1.35*np.sin(theta)
    ax.plot(x, y, color='black',label="trajectory")

    ax.scatter(vox_pos[index, 0], vox_pos[index, 1], vox_pos[index, 2], s=80, marker='^')
    ax.set_title(nm)
    ax.view_init(azim=0, elev=90)

def plotAll(ax, vos_pos, map_points, plt_rng, res, nm):
    ax.scatter(map_points[0:-1:3, 0], map_points[0:-1:3, 1], map_points[0:-1:3, 2], c='blue')
    ax.set_xlim(-plt_rng/2, plt_rng/2)
    ax.set_ylim(-plt_rng/2, plt_rng/2)
    ax.set_zlim(-plt_rng/2, plt_rng/2)
    # colors = [[0, v, 0] for v in res['nvalues']]
    # arrow_colors = colors + [v for v in colors for _ in (0, 1)]
    # arrow_colors = colors + [[1, 0, 1] for v in colors for _ in (0, 1)]
    ax.quiver(vox_pos[:, 0], vox_pos[:, 1], vox_pos[:, 2],
              res['trace']['views'][:, 0], res['trace']['views'][:, 1], res['trace']['views'][:, 2],
              length=0.5, color='red',pivot='tail',
              arrow_length_ratio=0.7)
    ax.quiver(vox_pos[:, 0], vox_pos[:, 1], vox_pos[:, 2],
              res['det']['views'][:, 0], res['det']['views'][:, 1], res['det']['views'][:, 2],
              length=0.5, color='green',pivot='tail',
              arrow_length_ratio=0.7)
    ax.quiver(vox_pos[:, 0], vox_pos[:, 1], vox_pos[:, 2],
              res['mineig']['views'][:, 0], res['mineig']['views'][:, 1], res['mineig']['views'][:, 2],
              length=0.5, color='blue',pivot='tail',
              arrow_length_ratio=0.7)
    # plot circle
    theta = np.linspace(-np.pi/2, np.pi/2, 201)
    x = 1.35*np.cos(theta)
    y = 1.35*np.sin(theta)
    ax.plot(x, y, color='black',label="trajectory")
    ax.scatter(vox_pos[:, 0], vox_pos[:, 1], vox_pos[:, 2], s=80, marker='^')
    ax.set_title(nm)
    ax.view_init(azim=0, elev=90)

def plotErrMap(ax, vox_pos, plt_rng, err, nm):
    ax.set_xlim(-plt_rng/2, plt_rng/2)
    ax.set_ylim(-plt_rng/2, plt_rng/2)
    ax.set_zlim(-plt_rng/2, plt_rng/2)
    n_err = normalize(np.array(err))
    colors = np.array([[v, 0, 0] for v in n_err])
    ax.scatter(vox_pos[:, 0], vox_pos[:, 1], vox_pos[:, 2],
               c=colors)
    ax.set_title(nm)
    ax.view_init(azim=0, elev=90)

init(autoreset=True)
_save_ext = ".pdf"
rc('font', **{'family': 'serif', 'serif': ['Cardo'], 'size': 20})
# rc('text', usetex=True)

parser = argparse.ArgumentParser()

parser.add_argument('--xrange', default=4.0, type=float,
                    help='Zero centered x range.')
parser.add_argument('--yrange', default=4.0, type=float,
                    help='Zero centered y range.')
parser.add_argument('--zrange', default=2.0, type=float,
                    help='Zero centered z range.')
parser.add_argument('--animated', action='store_true',
                    help='whether to visualize it in animation')
parser.add_argument('--dt', default=0.01, type=float,
                    help='animation delta t')
args = parser.parse_args()
print(args)

rospack = rospkg.RosPack()
act_map = os.path.join(rospack.get_path("act_map"))
top_trace_dir = os.path.join(act_map, "trace")
abs_trace_dir = os.path.join(top_trace_dir,"four_walls_bestView")

print(Fore.RED + ">>>>> Start analysis.")

test_mtypes = ['trace', 'det', 'mineig']
ceres = 'ceres_optimization'
ext = '.txt'
view_pre = 'optim_view_'
val_pre = 'optim_value_'

vox_pos = np.loadtxt(abs_trace_dir + "/vox_pos" + ext)
map_points = np.loadtxt(abs_trace_dir + "/map_points" + ext)

points0 = np.loadtxt(abs_trace_dir + "/vis_point0" + ext)
points1 = np.loadtxt(abs_trace_dir + "/vis_point1" + ext)
points2 = np.loadtxt(abs_trace_dir + "/vis_point2" + ext)
points3 = np.loadtxt(abs_trace_dir + "/vis_point3" + ext)
points4 = np.loadtxt(abs_trace_dir + "/vis_point4" + ext)
points5 = np.loadtxt(abs_trace_dir + "/vis_point5" + ext)
points6 = np.loadtxt(abs_trace_dir + "/vis_point6" + ext)

gt_res = {}
app_res = {}
for mtype in test_mtypes:
    gt_nm_i = mtype + '_' + ceres
    gt_res_i = {}
    gt_res_i['views'] = np.loadtxt(os.path.join(abs_trace_dir,
                                                view_pre+gt_nm_i+ext))
    gt_res_i['values'] = np.loadtxt(os.path.join(abs_trace_dir,
                                                 val_pre+gt_nm_i+ext))
    gt_res[mtype] = gt_res_i

gt_res['trace']['nvalues'] =\
    normalize(np.log(gt_res['trace']['values']))

gt_res['det']['nvalues'] =\
    normalize(np.log(gt_res['det']['values']))

gt_res['mineig']['nvalues'] =\
    normalize(np.log(gt_res['mineig']['values']))

print(gt_res['mineig']['views'])
### paper plots

# fig_save_trace_app = plt.figure(figsize=(6, 6))
# fig_save_mineig_app = plt.figure(figsize=(6, 6))
# fig_save_det_app = plt.figure(figsize=(6, 6))

# fig_save_trace_exa = plt.figure(figsize=(6, 6))
# fig_save_mineig_exa = plt.figure(figsize=(6, 6))
# fig_save_det_exa = plt.figure(figsize=(6, 6))

# ax = fig_save_mineig_app.add_subplot(111, projection='3d')
# plotSingle(ax, vox_pos, points, args.xrange, app_res['mineig'],
           # r'$\sigma_{min}$ poly.')
# plt.tight_layout()
# fig_save_mineig_app.savefig(plot_dirs + '/mineigh_comp_app' + _save_ext, bbox_inches='tight')
# ax = fig_save_mineig_exa.add_subplot(111, projection='3d')
# plotSingle(ax, vox_pos, points, args.xrange, gt_res['mineig'],
           # r'$\sigma_{min}$')
# plt.tight_layout()
# fig_save_mineig_exa.savefig(plot_dirs + '/mineigh_comp_exa' + _save_ext, bbox_inches='tight')

# ax = fig_save_trace_app.add_subplot(111, projection='3d')
# plotSingle(ax, vox_pos, points, args.xrange, app_res['trace'],
           # r'Trace poly.')
# plt.tight_layout()
# fig_save_trace_app.savefig(plot_dirs + '/trace_comp_app' + _save_ext, bbox_inches='tight')
# ax = fig_save_trace_exa.add_subplot(111, projection='3d')
# plotSingle(ax, vox_pos, points, args.xrange, gt_res['trace'],
           # r'Trace')
# plt.tight_layout()
# fig_save_trace_exa.savefig(plot_dirs + '/trace_comp_exa' + _save_ext, bbox_inches='tight')

# ax = fig_save_det_exa.add_subplot(111, projection='3d')
# plotSingle(ax, vox_pos, points, args.xrange, app_res['det'],
           # r'Det poly.')
# plt.tight_layout()
# fig_save_det_exa.savefig(plot_dirs + '/det_comp_exa' + _save_ext, bbox_inches='tight')
# ax = fig_save_det_app.add_subplot(111, projection='3d')
# plotSingle(ax, vox_pos, points, args.xrange, gt_res['det'],
           # r'Det')
# plt.tight_layout()
# fig_save_det_app.savefig(plot_dirs + '/det_comp_app' + _save_ext, bbox_inches='tight')

### visualiation for compare
axes = []
fig = plt.figure(figsize=(27, 14))

ax = fig.add_subplot(241, projection='3d')
plotAllinSingel(ax, vox_pos, 0, map_points, points0, args.xrange, gt_res,
           r'Position 1')
ax.set_xticklabels([])
ax.set_yticklabels([])
ax.set_zticklabels([])
axes.append(ax)

ax = fig.add_subplot(242, projection='3d')
plotAllinSingel(ax, vox_pos, 1, map_points, points1, args.xrange, gt_res,
           r'Position 2')
ax.set_xticklabels([])
ax.set_yticklabels([])
ax.set_zticklabels([])
axes.append(ax)

ax = fig.add_subplot(243, projection='3d')
plotAllinSingel(ax, vox_pos, 2, map_points, points2, args.xrange, gt_res,
           r'Position 3')
ax.set_xticklabels([])
ax.set_yticklabels([])
ax.set_zticklabels([])
axes.append(ax)

ax = fig.add_subplot(244, projection='3d')
plotAllinSingel(ax, vox_pos, 3, map_points, points3, args.xrange, gt_res,
           r'Position 4')
ax.set_xticklabels([])
ax.set_yticklabels([])
ax.set_zticklabels([])
axes.append(ax)

ax = fig.add_subplot(245, projection='3d')
plotAllinSingel(ax, vox_pos, 4, map_points, points4, args.xrange, gt_res,
           r'Position 5')
ax.set_xticklabels([])
ax.set_yticklabels([])
ax.set_zticklabels([])
axes.append(ax)

ax = fig.add_subplot(246, projection='3d')
plotAllinSingel(ax, vox_pos, 5, map_points, points5, args.xrange, gt_res,
           r'Position 6')
ax.set_xticklabels([])
ax.set_yticklabels([])
ax.set_zticklabels([])
axes.append(ax)

ax = fig.add_subplot(247, projection='3d')
plotAllinSingel(ax, vox_pos, 6, map_points, points6, args.xrange, gt_res,
           r'Position 7')
ax.set_xticklabels([])
ax.set_yticklabels([])
ax.set_zticklabels([])
axes.append(ax)

ax = fig.add_subplot(248, projection='3d')
plotAll(ax, vox_pos, map_points, args.xrange, gt_res,
           r'All Position')
ax.set_xticklabels([])
ax.set_yticklabels([])
ax.set_zticklabels([])
axes.append(ax)

plt.tight_layout()
plt.subplots_adjust(wspace =0, hspace =0)#调整子图间距
if args.animated:
    azim = np.arange(0, 360, 2)

    p_elev = np.arange(50, 75, 0.5)
    n_elev = np.flip(p_elev)
    elev = np.hstack((p_elev, n_elev))
    n_elev = elev.shape[0]

    fixed_elev = 60
    for i in range(azim.shape[0]):
        a = azim[i]
        e = elev[i % n_elev]
        # print("Azim {0} and elev {1}".format(a, e))
        for ax in axes:
            ax.view_init(azim=a, elev=fixed_elev)
        plt.ion()
        plt.show()
        plt.pause(args.dt)
else:
    plt.show()

# ax = fig.add_subplot(337, projection='3d')
# plotErrMap(ax, vox_pos, args.xrange, err_min_eig, 'mineig error')

# ax = fig.add_subplot(338, projection='3d')
# plotErrMap(ax, vox_pos, args.xrange, err_trace, 'trace error')

# ax = fig.add_subplot(339, projection='3d')
# plotErrMap(ax, vox_pos, args.xrange, err_det, 'det error')

# fig = plt.figure()

# ax = fig.add_subplot(231)
# plt.hist(err_min_eig, bins=eu.hist_bins)
# ax.set_title('mineig: appr vs exact')

# ax = fig.add_subplot(232)
# plt.hist(err_trace, bins=eu.hist_bins)
# ax.set_title('trace: appr vs exact')

# ax = fig.add_subplot(233)
# plt.hist(err_det, bins=eu.hist_bins)
# ax.set_title('det: appr vs exact')

# ax = fig.add_subplot(234)
# plt.hist(err_diff_app, bins=eu.hist_bins)
# ax.set_title('appr: trace vs mineig')

# ax = fig.add_subplot(235)
# plt.hist(err_diff_exact1, bins=eu.hist_bins)
# ax.set_title('exact: trace vs mineig')

# ax = fig.add_subplot(236)
# plt.hist(err_diff_exact2, bins=eu.hist_bins)
# ax.set_title('exact: trace vs det')

## specific compare on the trace
# fig = plt.figure()
# ax = fig.add_subplot(221, projection='3d')
# plotSingle(ax, vox_pos, points, args.xrange, gt_res['trace'],
           # 'trace exact')
# ax = fig.add_subplot(222, projection='3d')
# plotSingle(ax, vox_pos, points, args.xrange, app_res['trace'],
           # 'trace approx')
# ax = fig.add_subplot(223, projection='3d')
# plotSingle(ax, vox_pos, points, args.xrange, closed_trace_res,
           # 'appr trace zero deriv')
# ax = fig.add_subplot(224, projection='3d')
# plotSingle(ax, vox_pos, points, args.xrange, worst_trace_res,
           # 'appr trace worst')
# plt.show()

print(Fore.GREEN + "<<<<< Analysis done.")


# functions for project_full_dag.py tasks

# test if ti needs to be arg

def printer(msg):
    import logging

    logging.info(msg)

def parse_py(name, ext, gs):
    """
    parse download links from csv file for city
    """
    import csv, os

    printer(f'---------WE ARE IN {os.getcwd()}-------')

    prefix = 'include/'
    with open(prefix + name + ext, 'r') as read_file:
        content = csv.DictReader(read_file)
        details = [ (row['dataset'].replace(' ', '_').replace(':', ''), row['download_url']) for row in content ]
    fnames, urls = zip(*details)

    return {'name': name, 'fnames': fnames, 'urls': urls, 'gs': gs, 'ext': ext}

def parse_bash(url_dict):
    """
    parse download-upload commands from list of links for city
    """
    gs_path = url_dict['gs'] + '/raw/' + url_dict['name']
    up_command = f'gcloud storage cp - {gs_path}'

    delay = 10
    curls = []
    urls = url_dict['urls']
    fnames = url_dict['fnames']
    for i in range(len(urls)):
        curl = f"curl {urls[i]} | {up_command}/{fnames[i]}{url_dict['ext']}"
        curl += f' && sleep {delay}'
        curls.append(curl)
        printer(f'-----{curl}----------')

    return curls

def print_py(outputs):
    printer(f'---------type of curls {type(outputs)} of length {len(outputs)}-------')
#    printer(f'---------index is {outputs.index()}-------')
#    printer(f'---------callables is {outputs.callables()}-------')
#    printer(f'---------value is {outputs.value()}-------')
    for item in ['name', 'fnames', 'gs', 'ext', '0', '1', '2', 'urls']:
        printer(f'---------count is {outputs.value[item]}-------')
#    for k, v in outputs.items():
#        printer(f'---------item is {k},{v} of type {type(v)} and length {len(v)}-------')
    return outputs


import tkinter as tk
from tkinter import ttk

BUTTON = [
    ["AC", "C", "%", "/"],
    ["7", "8", "9", "*"],
    ["4", "5", "6", "-"],
    ["1", "2", "3", "+"],
    ["0", "00", ".", "="],
]

SYMBOL = ['+', '-', '*', '/']

class CalcGui(object):
    def __init__(self, app=None):
        # 表示式
        self.formula = ""

        # 画面設定
        app.title("Calculator")
        app.geometry("400x600")

        # 枠設定
        calc_frame = ttk.Frame(app, width=400, height=150)
        calc_frame.propagate(False)
        calc_frame.pack(side=tk.TOP, padx=15, pady=30)
        button_frame = ttk.Frame(app, width=400, height=450)
        button_frame.propagate(False)
        button_frame.pack(side=tk.BOTTOM)

        # 文字設定
        self.calc_var = tk.StringVar()
        calc_label = tk.Label(calc_frame, textvariable=self.calc_var, font=("", 20))
        self.ans_var = tk.StringVar()
        ans_label = tk.Label(calc_frame, textvariable=self.ans_var, font=("",25))

        calc_label.pack(anchor=tk.E)
        ans_label.pack(anchor=tk.E)

        for i, row in enumerate(BUTTON, 1):
            for j, num in enumerate(row):
                button = tk.Button(button_frame, text=num, font=("", 15), width=6, height=3)
                button.grid(row=i, column=j)
                button.bind("<Button-1>", self.click_button)

    def click_button(self, event):
        check = event.widget["text"]

        if check == "=":
            if self.formula[-1:] in SYMBOL:
                self.formula = self.formula[:-1]

            res = '= ' + str(eval(self.formula))
            self.ans_var.set(res)
        elif check == 'C':
            self.formula = ''
            self.ans_var.set('')
        elif check in SYMBOL:
            if self.formula[-1:] not in SYMBOL and self.formula[-1:] != '':
                self.formula += check
            elif self.formula[-1:] in SYMBOL:
                self.formula = self.formula[:-1] + check
        else:
            self.formula += check

        self.calc_var.set(self.formula)

def main():
    app = tk.Tk()
    app.resizable(width=False, height=False)
    CalcGui(app)

    app.mainloop()

if __name__ == "__main__":
    main()