import pandas as pd
from glob import glob
from datetime import datetime
import os

def removeDups(file):
    df = pd.read_excel(file)
    # Keep only FIRST record from set of duplicates
    df_first_record = df.drop_duplicates(subset="Date/Time", keep="first")
    #creates an excel file with sorted times
    if glob("noDupsTime.xlsx"):
        pass
    else:
        df_first_record.to_excel("./downloads/noDupsTime.xlsx", index=False)

# removeDups()

def create_dict():
    os.chdir('./downloads')
    df=pd.read_excel("noDupsTime.xlsx")
    names_list=list(df['Name'])
    dates_list=list(df['Date/Time'])
    custom_dict={}#dictionary of names as keys and al lthe datetime as values
    modified_dict={}
    for name,date_time  in zip(names_list,dates_list):
        if name not in custom_dict.keys():
            custom_dict[name]=[date_time]
        else:
            custom_dict[name].append(date_time)
    for name in custom_dict.keys():
        #for each name go through each date,then split date_time into date and time,
        #create a dictionary with each day as the key and the values an array of times
        date_dict={}
        for dateTime in custom_dict[name]:
        #iterate over datetimes of each person
            date = dateTime.split()[0]
            time = dateTime.split()[-1]
            if date not in date_dict.keys():
                date_dict[date]=[time]
            else:
                date_dict[date].append(time)
        modified_dict[name]=date_dict#create new dictionary with the name as the key
    print(modified_dict)
    return modified_dict

def create_report(create_dict):

    data=create_dict()
    lst_of_names=[]
    length=0
    lst_of_dates=[]
    lst_of_timein=[]
    lst_of_timeout=[]
    lst_of_durations=[]
    total=0
    #iterate over all the names
    for name in data.keys():
        length+=1
        days=data[name]
        dates=data[name].keys()#gets dates for each name
        print(f'each_day:{type(days)}')
        no_of_names=len(dates)
        lst_of_names.extend([name for i in range(no_of_names)])#make name array same size as dates array
        lst_of_dates.extend(dates)
        for day in days.values():
            print('day:',day)
            total+=1
            first_time = datetime.strptime(day[0], '%H:%M:%S')
            last_time = datetime.strptime(day[-1], '%H:%M:%S')
            time_diff_Hours = (last_time - first_time).seconds//3600
            rem_minutes = ((last_time-first_time).seconds% 3600)//60
            time_diff = str(time_diff_Hours) + ":" + str(rem_minutes)

            lst_of_timein.append(first_time.time())
            lst_of_timeout.append(last_time.time())
            lst_of_durations.append(time_diff)

        #lst_of_durations.extend(data[name].values()[0])

    print(f'total:{total}')
    print(f"old length:{length}\nnew length:{len(lst_of_names)}")
    print(f"no of dates:{len(lst_of_dates)}")
    print(f'{len(lst_of_timein)}')
    print(f'{len(lst_of_timeout)}')
    df=pd.DataFrame({"Names":lst_of_names,"Date":lst_of_dates,"Time_in":lst_of_timein,"Time_out":lst_of_timeout,"Time Spent":lst_of_durations})
    #df2=pd.DataFrame({"Time_in":lst_of_timein,"Time_out":lst_of_timeout})
    #df=pd.DataFrame({"Names":lst_of_names,"Date":lst_of_dates})
    df.to_excel('report.xlsx')
    #df2.to_excel('fingers2.xlsx')
# create_report(create_dict)

def report(file):
    removeDups(file)
    create_report(create_dict)


#!/usr/bin/python3

import sys, pygame
from pygame.locals import *

black = (0, 0, 0)
white = (255,255,255)
red = (255,0,0)
green = (0, 255, 0)
blue = (0, 0, 255)
pygame.init()

pygame.display.set_caption("drawing") # set the title of the window
surface = pygame.display.set_mode((400, 300)) # return pygame.Surface

surface.fill(white)    #  <=== white the surface

# draw polygon
pygame.draw.polygon(surface, green, ((123, 0), (234,132), (269, 211), (77, 66)), 0)

# draw line
pygame.draw.line(surface, red, (70, 200), (80,100), 20)

pygame.draw.circle(surface, black, (30, 50), 15, 10)
pygame.draw.ellipse(surface, black, (30, 50, 100, 60), 10)

pygame.draw.rect(surface, blue, (30, 50, 100, 60))

# event loop: handling event, update game state(variables), rendering graphics
while True:
    for event in pygame.event.get():

        # QUIT etc, defined in pygame.locals
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
    pygame.display.update()  # render surface into screen

# Question:
# what's the different if we move Line 14 ~ 18 into loop before pygame.display.update() ?

"""
Surface:   2D rectangle

Color : (r, g, b, a)

Rect: (x0, y0, width, height)
"""


from PIL import Image

import cv2

# 選擇第二隻攝影機
cap = cv2.VideoCapture(0)
while(True):
  # 從攝影機擷取一張影像
    ret, frame = cap.read()
    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    edges = cv2.Canny(frame, 100 , 200)
    # img_fc, contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

    # hierarchy = hierarchy[0]
    # found = []
    # for i in range(len(contours)):
    #     k = i
    #     c = 0
    # while hierarchy[k][2] != -1:
    #     k = hierarchy[k][2]
    #     c = c + 1
    # if c >= 5:
    #     found.append(i)

    # for i in found:
    #     cv2.drawContours(frame, contours, i, (0, 255, 0), 3)

    (_, cnts, _) = cv2.findContours(blurred.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    if len(cnts) > 0:
        for cnt in cnts:
          # compute the (rotated) bounding box around then
          # contour and then draw it
          rect = np.int32(cv2.boxPoints(cv2.minAreaRect(cnt)))
          cv2.drawContours(self.frame, [rect], -1, (0, 255, 0), 2)

     # 顯示圖片
    cv2.imshow('frame', edges)
    cv2.waitKey(1)
    if cv2.getWindowProperty('frame', cv2.WND_PROP_AUTOSIZE) == -1:
        break
# 釋放攝影機
cap.release()

# 關閉所有 OpenCV 視窗
cv2.destroyAllWindows()


from abc import ABC, abstractmethod

from app.schemas.base import BaseModel

class BaseRepository(ABC):
    def __init__(self, model: BaseModel, *args, **kwargs):
        self.model = model

    @abstractmethod
    async def get_all(self):
        raise NotImplementedError


import numpy as np
from PyQt5.QtWidgets import QWidget, QApplication, QVBoxLayout
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg
from matplotlib.figure import Figure

from SO3 import SO3

from rotation import Ui_Form

class My_window(QWidget, Ui_Form):
    def __init__(self, parent=None, *args, **kwargs):
        super().__init__(parent, *args, **kwargs)
        self.setupUi(self)
        self.retranslateUi(self)

        self.current_rotation = SO3()

        self.figure = Figure()
        self.canvas = FigureCanvasQTAgg(self.figure)
        self.layout = QVBoxLayout(self.widget_2)
        self.layout.addWidget(self.canvas)

        self.display_current_rotation()

    def slot_matrix2all(self):
        pass

    def slot_quat2all(self):
        q = np.empty(4)
        q[0] = self.quatw.value()
        q[1] = self.quatx.value()
        q[2] = self.quaty.value()
        q[3] = self.quatz.value()

        self.current_rotation = SO3.from_quaternion(q)

        self.display_current_rotation()

    def slot_anglevec2all(self):
        vector = np.empty(4)
        vector[0] = self.vectorx.value()
        vector[1] = self.vectory.value()
        vector[2] = self.vectorz.value()
        vector[3] = self.angle.value()
        self.current_rotation = SO3.from_axis_angle(vector)

        self.display_current_rotation()

    def display_current_rotation(self):
        # matrix
        matrix = self.current_rotation.rotation_matrix
        print(matrix)
        for i in range(3):
            for j in range(3):
                exec("self.matrix{}{}.setValue({})".format(str(i), str(j), matrix[i][j]))

        # quat
        quat = self.current_rotation.to_quaternion()
        self.quatw.setValue(quat[0])
        self.unitw.setValue(quat[0])
        self.quatx.setValue(quat[1])
        self.unitx.setValue(quat[1])
        self.quaty.setValue(quat[2])
        self.unity.setValue(quat[2])
        self.quatz.setValue(quat[3])
        self.unitz.setValue(quat[3])

        # angle_vector
        vector = self.current_rotation.to_axis_angle()
        self.angle.setValue(vector[3])
        self.vectorx.setValue(vector[0])
        self.vectory.setValue(vector[1])
        self.vectorz.setValue(vector[2])

        self.textBrowser.setText(str(self.current_rotation))

        # plot
        self.figure.clear()
        self.current_rotation.plot_coordinate_system(self.figure)
        self.canvas.draw()

if __name__ == '__main__':
    import sys

    app = QApplication(sys.argv)
    window = My_window()
    window.show()
    sys.exit(app.exec_())


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

#performance.py

from __future__ import print_function

import numpy as np
import pandas as pd

def create_sharpe_ratio(returns,periods=252):
    """
    计算策略的Sharpe比率，基于基准为0，也就是假设无风险利率为0
    """
    return np.sqrt(periods)*(np.mean(returns)/np.std(returns))

def create_drawdowns(pnl):
    """
    计算PnL曲线的最大回撤（从最大收益到最小收益之间的距离），以及回撤的时间
    这里需要参数pnl_returns是一个pandas的Series
    """
    hwm=[0]
    idx=pnl.index
    drawdown=pd.Series(index=idx)
    duration=pd.Series(index=idx)

    for t in range(1,len(idx)):
        hwm.append(max(hwm[t-1],pnl[t]))
        drawdown[t]=(hwm[t]-pnl[t])
        duration[t]=(0 if drawdown[t]==0 else duration[t-1]+1)

    return drawdown,drawdown.max(),duration.max()


"""
Beautify Images Utils
"""

import random
import operator

import heapq
import math
from scipy.interpolate import UnivariateSpline

import cv2
import pilgram
from PIL import Image, ImageStat
import numpy as np

from src.utils.image_process import (
    do_we_need_to_sharpen,
    sharpen_my_image,
    adjust_contrast_brightness,
)

def get_top_frames(scores, num, fps, dispersed=True):
    """
    Returns list of indexes for number frames with the highest scores as
    specified by the user.

    Users can define the 'dispersed' function if they wish to have num images
    taken from different parts of the video. In this instance, we randomly sample
    10% of the frames from the video and score these frames.

    Otherwise the function just returns the best num images from the frames scored.
    """
    if len(scores) <= 1000:
        dispersed = False

    if dispersed:

        tmp = []

        while True:
            if len(tmp) == int(0.1 * len(scores)):
                break

            sampled_frame = random.choice(scores)

            if len(tmp) == 0:
                tmp.append(sampled_frame)
            else:
                flag = False

                for i in tmp:
                    if i - fps <= sampled_frame <= i + fps:
                        flag = True
                    break

                if flag == False:
                    tmp.append(sampled_frame)

        idx = sorted(
            list(zip(*heapq.nlargest(num, enumerate(tmp), key=operator.itemgetter(1))))[
                0
            ]
        )

        return sorted([scores.index(j) for j in [tmp[i] for i in idx]])

    else:
        return sorted(
            list(
                zip(*heapq.nlargest(num, enumerate(scores), key=operator.itemgetter(1)))
            )[0]
        )

def get_top_n_idx(filtered_scores, filtered_idx, sampling_size=0.1, n=10):
    """
    Random sample from scores and get the indices of the top n scores
    from original video

    Args:
    filtered_scores (np.array): scores filtered from object detection that pass a threshold
    filtered_idx (np.array): the indices of scores that pass the threshold, from original video
    sampling_size (float): proportion of samples to choose from num_frames of original video
    n (int): top n scores to choose from

    Return:
    top_n_idx (np.array): indices of top n scores from the sample,
    corresponding to indices from original video
    """

    # sample from filtered_scores & filtered_idx arrays
    n_sample = int(np.ceil(len(filtered_scores) * sampling_size))
    if n_sample <= n:
        n_sample = len(filtered_scores)
    rand_sample = np.random.choice(len(filtered_scores), n_sample, replace=False)
    rand_sample_scores = filtered_scores[rand_sample]
    rand_sample_idx = filtered_idx[rand_sample]

    # get the indices of the top n scores from the sample
    top_n_idx = rand_sample_idx[rand_sample_scores.argsort()[::-1][: min(n, n_sample)]]

    return top_n_idx

def brightness(im_file):
    """
    Returns perceived brightness of image
    https://www.nbdtech.com/Blog/archive/2008/04/27/Calculating-the-Perceived-Brightness-of-a-Color.aspx
    """
    stat = ImageStat.Stat(im_file)
    r, g, b = stat.mean
    return math.sqrt(0.241 * (r**2) + 0.691 * (g**2) + 0.068 * (b**2))

def LookupTable(x, y):
    spline = UnivariateSpline(x, y)
    return spline(range(256))

def Summer(img):
    increaseLookupTable = LookupTable([0, 64, 128, 256], [0, 80, 160, 256])
    decreaseLookupTable = LookupTable([0, 64, 128, 256], [0, 50, 100, 256])
    blue_channel, green_channel, red_channel = cv2.split(img)
    red_channel = cv2.LUT(red_channel, increaseLookupTable).astype(np.uint8)
    blue_channel = cv2.LUT(blue_channel, decreaseLookupTable).astype(np.uint8)
    sum = cv2.merge((blue_channel, green_channel, red_channel))
    return sum

def Winter(img):
    increaseLookupTable = LookupTable([0, 64, 128, 256], [0, 80, 160, 256])
    decreaseLookupTable = LookupTable([0, 64, 128, 256], [0, 50, 100, 256])
    blue_channel, green_channel, red_channel = cv2.split(img)
    red_channel = cv2.LUT(red_channel, decreaseLookupTable).astype(np.uint8)
    blue_channel = cv2.LUT(blue_channel, increaseLookupTable).astype(np.uint8)
    win = cv2.merge((blue_channel, green_channel, red_channel))
    return win

def beautify(beauti_img, filter="hudson"):
    """
    Beautifies selected images.
    Input arguments:
    1) beauti_img (np.array) - array of images in cv2/BGR format
    2) filter (str) - instagram filter to apply
    3) The default filter is Hudson.
    List of Instagram filters: https://github.com/akiomik/pilgram/tree/master/pilgram
    """
    if filter:
        try:
            pilgram_filter = getattr(pilgram, filter.lower())

        except:
            raise ValueError(
                """
        That was not a correct filter. The list of correct filters are:
        _1977
        aden
        brannan
        brooklyn
        clarendon
        earlybird
        gingham
        hudson
        inkwell
        kelvin
        lark
        lofi
        maven
        mayfair
        moon
        nashville
        perpetua
        reyes
        rise
        slumber
        stinson
        toaster
        valencia
        walden
        willow
        xpro2
        Here's some showcases of filtered images:
        https://github.com/akiomik/pilgram/blob/master/screenshots/screenshot.png
        """
            )

    for idx, img in enumerate(beauti_img):

        ## Reduce blue light ##
        if filter and filter.lower() == "hudson":
            img = Summer(img)

        # Adjust brightness and contrast
        lux = brightness(Image.fromarray(img))
        if lux <= 130:
            beta = 137.5 - lux
        elif lux > 145:
            beta = 137.5 - lux
        else:
            beta = 0
        img = adjust_contrast_brightness(img, contrast=1.2, brightness=beta)

        ## Check and sharpen ##
        if do_we_need_to_sharpen(img):
            img = sharpen_my_image(img)

        ## Apply instagram filter ##
        if filter:
            img = np.array(pilgram_filter(Image.fromarray(img)))

        beauti_img[idx] = img

    return beauti_img

def check_filter(filter):

    error_msg = """
    That was not a correct filter. The list of correct filters are: \n
    _1977
    aden
    brannan
    brooklyn
    clarendon
    earlybird
    gingham
    hudson
    inkwell
    kelvin
    lark
    lofi
    maven
    mayfair
    moon
    nashville
    perpetua
    reyes
    rise
    slumber
    stinson
    toaster
    valencia
    walden
    willow
    xpro2
    \nHere's some showcases of filtered images:
    https://github.com/akiomik/pilgram/blob/master/screenshots/screenshot.png
    """

    try:
        pilgram_filter = getattr(pilgram, filter.lower())
    except:
        return False, error_msg

    return True, error_msg


import time

import utilities.custom_logger as cl
import logging
from base.basepage import BasePage
from base.selenium_driver import SeleniumDriver

class Register_courses_page(BasePage):

    log = cl.customLogger(logging.DEBUG)

    #Locators

    _search_box_id = "search-courses"
    _course_xpath = "/html/body/div/div/div/div[2]/div/div/div[1]/a/div/div[2]"
    _search_btn_id = "search-course-button"
    _enroll_button_id = "enroll-button-top" # type is id
    _cc_id= "payment_method_credit_card" #type is id
    _credit_card_num_name = "cardnumber" #type is id
    _cc_exp_name = "exp-date"
    _cc_cvc_name = "cvc"
    _postal_field_name = "postal"
    _submit_enroll_id = "confirm-purchase" #id
    _enroll_error_message_class = "cc__error alert-danger" # class

    def __init__(self, driver):
        super(Register_courses_page, self).__init__(driver)
        self.driver = driver

    def enterCourseToEnroll(self, Name):
        self.sendKeys(Name, self._search_box_id, "id")
        self.elementClick(self._search_btn_id, "id")
        #put a wait command here,becoz it will load

    def selectCourseToEnroll(self):
        # not sure about the link text
        self.elementClick(self._course_xpath, "xpath")
        #put wait statements
        self.elementClick(self._enroll_button_id, "id")

    def enterCardNumber(self, cardNumber):
        #__privateStripeFrame6

        self.webScroll("down", self._postal_field_name, "name")
        #self.webScroll("down")
        #switch to frame using id
        time.sleep(1)
        self.driver.switch_to_frame(self.getElement("__privateStripeFrame3", "name"))
        time.sleep(2)
        self.sendKeys(cardNumber, self._credit_card_num_name, "name")
        self.driver.switch_to_default_content()

    def enterCardExp(self,exp):
        #time.sleep(1)
        self.driver.switch_to_frame(self.getElement("__privateStripeFrame4", "name"))
        time.sleep(2)
        self.sendKeys(exp, self._cc_exp_name,"name")
        self.driver.switch_to_default_content()

    def enterCardCvc(self,cvc):
        #time.sleep(1)
        self.driver.switch_to_frame(self.getElement("__privateStripeFrame5", "name"))
        time.sleep(2)
        self.sendKeys(cvc, self._cc_cvc_name,"name")
        self.driver.switch_to_default_content()

    def enterpostalcode(self,postalcode):
        self.driver.switch_to_frame(self.getElement("__privateStripeFrame6", "name"))
        time.sleep(2)
        self.sendKeys(postalcode, self._postal_field_name,"name")
        self.driver.switch_to_default_content()

    def enrollInCourse(self):
        self.elementClick(self._submit_enroll_id, "id")

    def enterCreditCardinformation(self, cardNumber, exp, cvc, postal_code):
        self.enterCardNumber(cardNumber)
        self.enterCardExp(exp)
        self.enterCardCvc(cvc)
        self.enterpostalcode(postal_code)

    def captureErrorMsg(self):
        error_msg = "Hint : check for exception"
        try:
            error_msg = self.getElement(self._enroll_error_message_class, "class")
        except:
            raise
        return error_msg


import sys

def ft_filter(check_function, _list):
	"""filter(function or None, iterable) --> filter object

Return an iterator yielding those items of iterable for which function(item)
is true. If function is None, return the items that are true."""
	filtered_list = []
	for elem in _list:
		if check_function(elem) == True:
			filtered_list.append(elem)
	return filtered_list

def check_even(number):
    if number % 2 == 0:
          return True
    return False

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

def main():
	print(ft_filter(check_even, numbers))
#	print(filter.__doc__)
#	print(ft_filter.__doc__)

if __name__ == "__main__":
	main()


import tensorflow as tf
from transformers import GPT2Tokenizer, TFGPT2LMHeadModel
import wikipediaapi

# Set up Wikipedia API
wiki = wikipediaapi.Wikipedia('en')

# Set up tokenizer and model
tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
model = TFGPT2LMHeadModel.from_pretrained('gpt2', pad_token_id=tokenizer.eos_token_id)

# Define training parameters
batch_size = 4
epochs = 2
learning_rate = 1e-4

# Define function to preprocess input text
def preprocess(text):
    text = text.strip().replace('\n', ' ')
    tokens = tokenizer.encode(text, add_special_tokens=True, max_length=512)
    input_ids = tf.convert_to_tensor(tokens[:-1], dtype=tf.int32)
    target_ids = tf.convert_to_tensor(tokens[1:], dtype=tf.int32)
    return input_ids, target_ids

# Define function to fetch training data from Wikipedia
def fetch_training_data():
    titles = [
        'Artificial intelligence',
        'Machine learning',
        'Natural language processing',
        'Recurrent neural network',
        'Transformer (machine learning)',
        'Generative Pre-trained Transformer 2'
    ]
    text = ''
    for title in titles:
        page = wiki.page(title)
        if page.exists():
            text += page.text
    return text

# Fetch training data from Wikipedia
training_data = fetch_training_data()

# Preprocess the training data and convert it to a TensorFlow dataset
dataset = tf.data.Dataset.from_tensor_slices(training_data).map(preprocess).shuffle(10000).batch(batch_size)

# Set up optimizer and loss function
optimizer = tf.keras.optimizers.Adam(learning_rate=learning_rate)
loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)

# Train the model
for epoch in range(epochs):
    epoch_loss = 0.0
    for batch in dataset:
        with tf.GradientTape() as tape:
            logits = model(batch[0], training=True)[0]
            loss = loss_fn(batch[1], logits)
        gradients = tape.gradient(loss, model.trainable_variables)
        optimizer.apply_gradients(zip(gradients, model.trainable_variables))
        epoch_loss += loss.numpy()
    print('Epoch {} Loss: {:.4f}'.format(epoch+1, epoch_loss/len(dataset)))

# Save the trained model and tokenizer to files
model.save_pretrained('my_chatgpt_model')
tokenizer.save_pretrained('my_chatgpt_model')


#coding: UTF-8
class Dog:
    name = ""
    def brak(self):
        m = self.name + " : Bow-wow!"
        print(m)

pochi = Dog()
pochi.name = "Pochi"
pochi.brak()

hachi = Dog()
hachi.name = "Hachi"
hachi.brak()


"""Extract hourly real-time EIA data from the bulk-download zip file."""

import pandas as pd
import json
from os.path import join
import os
import zipfile
import requests
import logging
from electricitylci.globals import data_dir

def download_EBA():
    """Add docstring."""
    url = 'http://api.eia.gov/bulk/EBA.zip'
    print(f"Downloading eia bulk data from {url}...", end="")
    r = requests.get(url)
    os.makedirs(join(data_dir, 'bulk_data'), exist_ok=True)
    output = open(join(data_dir, 'bulk_data', 'EBA.zip'), 'wb')
    output.write(r.content)
    output.close()
    print(f"complete.")

path = join(data_dir, 'bulk_data', 'EBA.zip')

if __name__=="__main__":
    try:
        z = zipfile.ZipFile(path, 'r')
        with z.open('EBA.txt') as f:
            raw_txt = f.readlines()
    except FileNotFoundError:
        download_EBA()
        z = zipfile.ZipFile(path, 'r')
        with z.open('EBA.txt') as f:
            raw_txt = f.readlines()

# REGION_NAMES = [
#    'California', 'Carolinas', 'Central',
#    'Electric Reliability Council of Texas, Inc.', 'Florida',
#    'Mid-Atlantic', 'Midwest', 'New England ISO',
#    'New York Independent System Operator', 'Northwest', 'Southeast',
#    'Southwest', 'Tennessee Valley Authority'
# ]
#
# REGION_ACRONYMS = [
#    'TVA', 'MIDA', 'CAL', 'CAR', 'CENT', 'ERCO', 'FLA',
#    'MIDW', 'ISNE', 'NYIS', 'NW', 'SE', 'SW',
# ]
#
# TOTAL_INTERCHANGE_ROWS = [
#    json.loads(row) for row in raw_txt if b'EBA.TI.H' in row
# ]
#
# NET_GEN_ROWS = [
#    json.loads(row) for row in raw_txt if b'EBA.NG.H' in row
# ]
#
# DEMAND_ROWS = [
#    json.loads(row) for row in raw_txt if b'EBA.D.H' in row
# ]
#
# EXCHANGE_ROWS = [
#    json.loads(row) for row in raw_txt if b'EBA.ID.H' in row
# ]
#
# BA_TO_BA_ROWS = [
#    row for row in EXCHANGE_ROWS
#    if row['series_id'].split('-')[0][4:] not in REGION_ACRONYMS
# ]

def row_to_df(rows, data_type):
    """
    Turn rows of a single type from the bulk data text file into a dataframe
    with the region, datetime, and data as columns
    Parameters
    ----------
    rows : list
        rows from the EBA.txt file
    data_type : str
        name to use for the data column (e.g. demand or total_interchange)
    Returns
    -------
    dataframe
        Data for all regions in a single df with datatimes converted and UTC
    """
    tuple_list = []
    for row in rows:

        # "data" is of form:
        # [['20190214T04Z', -102],
        # ['20190214T03Z', -107],
        # ['20190214T02Z', -108],
        # ['20190214T01Z', -103]]
        try:
            datetime = pd.to_datetime([x[0] for x in row['data']],
                                      utc=True, format='%Y%m%dT%HZ')
        except ValueError:
            try:
                datetime = pd.to_datetime([x[0]+":00" for x in row['data']],
                                          format='%Y%m%dT%H%z')
            except ValueError:
                continue
        data = [x[1] for x in row['data']]
        region = row['series_id'].split('-')[0][4:]

#        df_data = {
#            'region': region,
#            'datetime': datetime,
#            data_type: data,
#        }
#        region_list=[region for x in datetime]
#        _df = pd.DataFrame(df_data)
#        tuple_list.append(_df)
        tuple_data=[x for x in zip([region]*len(datetime), list(datetime), data)]
        tuple_list.extend(tuple_data)
    df=pd.DataFrame(tuple_list, columns=["region", "datetime", data_type])
#    df = pd.concat(df_list).reset_index(drop=True)

    return df

def ba_exchange_to_df(rows, data_type='ba_to_ba'):
    """
    Turn rows of a single type from the bulk data text file into a dataframe
    with the region, datetime, and data as columns
    Parameters
    ----------
    rows : list
        rows from the EBA.txt file
    data_type : str
        name to use for the data column (e.g. demand or total_interchange)
    Returns
    -------
    dataframe
        Data for all regions in a single df with datatimes converted and UTC
    """
    tuple_list = []
    for row in rows:

        # "data" is of form:
        # [['20190214T04Z', -102],
        # ['20190214T03Z', -107],
        # ['20190214T02Z', -108],
        # ['20190214T01Z', -103]]
        try:
            datetime = pd.to_datetime([x[0] for x in row['data']],
                                      utc=True, format='%Y%m%dT%HZ')
        except ValueError:
            try:
                datetime = pd.to_datetime([x[0]+"00" for x in row['data']],
                                          format='%Y%m%dT%H%z')
            except ValueError:
                continue
        data = [x[1] for x in row['data']]
        from_region = row['series_id'].split('-')[0][4:]
        to_region = row['series_id'].split('-')[1][:-5]

#        df_data = {
#            'from_region': from_region,
#            'to_region': to_region,
#            'datetime': datetime,
#            data_type: data,
#        }
        tuple_data = [x for x in zip([from_region]*len(datetime), [to_region]*len(datetime), datetime, data)]
        tuple_list.extend(tuple_data)
#        _df = pd.DataFrame(df_data)
#        df_list.append(_df)

#    df = pd.concat(df_list).reset_index(drop=True)
    df=pd.DataFrame(tuple_list, columns=["from_region", "to_region", "datetime", data_type])
    return df


import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0,10,100)
y = []
up_limit = 0.8
for i in x:
    if i < 6:
        y.append(0)
    elif i < 9:
        y.append((i-6)/3 * up_limit)
    else:
        y.append(up_limit)

plt.plot(x,y)
plt.show()


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

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('inicio', '0006_auto_20160820_2050'),
    ]

    operations = [
        migrations.CreateModel(
            name='cargo',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('nombre_cargo', models.CharField(unique=True, max_length=100)),
                ('fecha', models.DateTimeField(auto_now=True)),
                ('estado', models.IntegerField(default=0)),
            ],
            options={
            },
            bases=(models.Model,),
        ),
        migrations.RemoveField(
            model_name='adminstrador',
            name='carrera',
        ),
        migrations.AddField(
            model_name='adminstrador',
            name='cargo',
            field=models.ForeignKey(default='1', to='inicio.cargo'),
            preserve_default=False,
        ),
    ]


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

# In[45]:

# Choquet adaptive thresholding: two step algorithm
import progressbar
from time import sleep
import math
import cv2
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import PIL
from skimage import measure
from pynverse import inversefunc
import time
import scipy.misc
get_ipython().run_line_magic('matplotlib', 'inline')
import sys
import warnings
import numpy as np
#Otsu trhesholding
from skimage import data
from skimage import filters
from skimage import exposure

#format the output in a readable format
float_formatter = lambda x: "%.2f" % x
np.set_printoptions(precision=0,formatter={'float_kind':float_formatter})

if not sys.warnoptions:
    warnings.simplefilter("ignore")

# In[46]:

#function section
### import img
def namestr(obj, namespace):
    return [name for name in namespace if namespace[name] is obj]

def plot_it(img):
    plt.figure(figsize = [8,8])
    arr = np.asarray(img)
    plt.imshow(arr, cmap='gray', vmin=0, vmax=arr.max())
    plt.title(namestr(img, globals()))
    plt.show()

def import_img(img_path):
    img = cv2.imread(img_path, 0)
    img_reverted= cv2.bitwise_not(img)
    norm_img = img_reverted / 255.0
    #plot_it(norm_img)
    print(norm_img)
    print(norm_img.shape)
    print(norm_img.size)
    return(norm_img)

### cumulative G function (sum-table algorithm)

def compute_summed_area_table(image):
    # image is a 2-dimensional array containing ints or floats, with at least 1 element.
    height = len(image)
    width = len(image[0])
    new_image = [[0.0] * width for _ in range(height)] # Create an empty summed area table
    for row in range(0, height):
        for col in range(0, width):
            if (row > 0) and (col > 0):
                new_image[row][col] = image[row][col] +                     new_image[row][col - 1] + new_image[row - 1][col] -                     new_image[row - 1][col - 1]
            elif row > 0:
                new_image[row][col] = image[row][col] + new_image[row - 1][col]
            elif col > 0:
                new_image[row][col] = image[row][col] + new_image[row][col - 1]
            else:
                new_image[row][col] = image[row][col]
    return new_image

def get_int_img_m1(input_img):
    h, w = input_img.shape
    #integral img
    int_img = np.zeros_like(input_img, dtype=np.uint32)
    for col in range(w):
        for row in range(h):
            int_img[row,col] = input_img[0:row+1,0:col+1].sum()
    return int_img

def cdf_image(input_img):
    nh, binn = np.histogram(input_img)
    cdf = np.cumsum(nh)
    return([cdf, nh, binn])

# In[ ]:

# In[47]:

# Adaptive choquet
# OPT= 0 Hamacher
# OPT= 1 Discrete Choquet
# Opt= 2 Discrete Choquet with F1,F2 on the distributive property

def compute_choquet(choquet_order, fuzzy_mu, opt=0):
    C=0
    if opt==0:   # Choquet Hamacher
        for i in range(len(choquet_order)-1):
            j = i +1
            C = C + (choquet_order[j] * fuzzy_mu[i])/(choquet_order[j] + fuzzy_mu[i] - (choquet_order[j] * fuzzy_mu[i]))
    if opt==1:  #Choquet
        for i in range(len(choquet_order)-1):
            j = i +1
            C = C + ((choquet_order[j] - choquet_order[j-1] )*fuzzy_mu[i])
    if opt ==2: #Choquet F1 F2
        for i in range(len(choquet_order)-1):
            j = i +1
            C = C + (np.sqrt(choquet_order[j]*fuzzy_mu[i]) -  max( (choquet_order[j]+fuzzy_mu[i] -1) , 0))
    return(C)

def compute_sugeno(sugeno_order, fuzzy_mu):
    S = np.empty((1), float)

    for i in range(len(sugeno_order)):
        S = np.append(S, min(sugeno_order[i], fuzzy_mu[i]))
        #print(S)
        #print('sugeno: ' + str(choquet_order[j]) + " " + str(fuzzy_mu[i]) + " " + str(max(S)))
    return(max(S))

## Integral Choquet and Sugeno image.
def adaptive_choquet_itegral(input_img, int_img, opt,log=False):

    h, w = input_img.shape
    th_mat = np.zeros(input_img.shape)
    choquet_mat =  np.zeros(input_img.shape)
    sugeno_mat =  np.zeros(input_img.shape)
    count_matrix =  np.zeros(input_img.shape)

    for col in range(w):       #i
        for row in range(h):   #j
            #SxS region

            y0 = int(max(row-1, 0))
            y1 = int(min(row, h-1))
            x0 = int(max(col-1, 0))
            x1 = int(min(col, w-1))

            count = (y1-y0)*(x1-x0)
            count_matrix[row, col] = count
            choquet_order = -1
            sum_ = -1
            fuzzy_mu = -1
            if count == 0:
                if x0 == x1 and y0 == y1:
                    sum_ = int_img[y0, x0]
                    C_   = sum_
                    S_   = sum_
                if x1 == x0 and y0 != y1:
                    sum_ = (int_img[y1, x1] + int_img[y0, x1])/2
                    choquet_order = np.asarray([0,int_img[y0, x1], int_img[y1, x1]])
                    sugeno_order  = np.asarray([int_img[y0, x1], int_img[y1, x1]])
                    fuzzy_mu = np.asarray([1, 0.5])
                    C_ = compute_choquet(choquet_order, fuzzy_mu,opt)
                    S_  = compute_sugeno(sugeno_order, fuzzy_mu)
                if y1 == y0 and x1 != x0:
                    sum_ = (int_img[y1, x1] + int_img[y1, x0])/2
                    choquet_order = np.asarray([0,int_img[y1, x0], int_img[y1, x1]])
                    sugeno_order  = np.asarray([int_img[y1, x0], int_img[y1, x1]])
                    fuzzy_mu = np.asarray([1, 0.5])
                    C_ = compute_choquet(choquet_order, fuzzy_mu,opt)
                    S_  = compute_sugeno(sugeno_order, fuzzy_mu)
            else:
                sum_ = int_img[y1, x1] - int_img[y0, x1] - int_img[y1, x0] + int_img[y0, x0]
                if(int_img[y0, x1] > int_img[y1, x0] ):
                    choquet_order = np.asarray([0,int_img[y0, x0], int_img[y1, x0], int_img[y0, x1], int_img[y1, x1]])
                    sugeno_order = np.asarray([int_img[y0, x0], int_img[y1, x0], int_img[y0, x1], int_img[y1, x1]])
                else:
                    choquet_order = np.asarray([0,int_img[y0, x0], int_img[y0, x1], int_img[y1, x0], int_img[y1, x1]])
                    sugeno_order  = np.asarray([int_img[y0, x0], int_img[y0, x1], int_img[y1, x0], int_img[y1, x1]])

                fuzzy_mu = np.asarray([1, 0.75, 0.50, 0.25])
                C_ = compute_choquet(choquet_order, fuzzy_mu,opt)
                S_  = compute_sugeno(sugeno_order, fuzzy_mu)

            th_mat[row,col]      = sum_
            choquet_mat[row,col] = C_
            sugeno_mat[row,col]  = S_

            if(log):
                coords_window = np.zeros_like(input_img)

                #coords_window[x0:x1,y0:y1] = 1.0
                coords_window[y0, x0] = 0.2
                coords_window[y1, x0] = 0.4
                coords_window[y0, x1] = 0.6
                coords_window[y1, x1] = 0.8
                plot_it(coords_window)

                print("Search_region")
                print("x0:" + str(x0) + " x1:"+ str(x1) + " y0:" + str(y0) + " y1:" + str(y1) )
                print("Row:" + str(row) + " Col:" + str(col))
                print("Count: " + str(count))
                print("choquet fixed ordered and fuzzy mu")
                print(choquet_order)
                print(fuzzy_mu)
                print("choquet calculus")
                print(C_)
                print("sugeno calculus")
                print(S_)
                print("Input mat")
                print(input_img)
                print("Int img")
                print(int_img)
                print("I integral mat: ")
                print(th_mat)
                print("C_ choquet")
                print(choquet_mat)
                print("S_ sugeno")
                print(sugeno_mat)
                print("Count matrix")
                print(count_matrix)
                print("-------")

    return  choquet_mat, sugeno_mat, count_matrix

# In[ ]:

# In[48]:

## Classic Bradley Apprroach
def adaptive_thresh(input_img, int_img, a1=8, a2=2, T=0.15):
    out_img = np.zeros_like(input_img)
    h, w = input_img.shape
    S = w/a1
    s2 = S/a2
    th_mat = np.zeros(input_img.shape)
    for col in range(w):
        for row in range(h):
            #SxS region
            y0 = int(max(row-s2, 0))
            y1 = int(min(row+s2, h-1))
            x0 = int(max(col-s2, 0))
            x1 = int(min(col+s2, w-1))

            count = (y1-y0)*(x1-x0)
            sum_ = int_img[y1, x1] - int_img[y0, x1] - int_img[y1, x0] + int_img[y0, x0]

            th_mat[row,col] = sum_/count

            if input_img[row, col]*count < sum_*(1.-T)/1.:
                out_img[row,col] = 0
            else:
                out_img[row,col] = 1

    return np.asarray(out_img), th_mat

#Novel choquet adaptive approach
def adaptive_thresh2(input_img, int_img, a1=4, a2=1, T=0, log=False):
    if T==0:
        T = filters.threshold_otsu(input_img)
        T = T

    out_img_choquet = np.zeros_like(input_img)
    out_img_sugeno  = np.zeros_like(input_img)
    choquet_mat     = np.zeros_like(input_img)
    sugeno_mat      = np.zeros_like(input_img)
    h, w = input_img.shape
    S = w/a1
    s2 = S/a2

    for col in range(w):
        for row in range(h):
            y0 = int(max(row-s2, 0))
            y1 = int(min(row+s2, h-1))
            x0 = int(max(col-s2, 0))
            x1 = int(min(col+s2, w-1))
            count = (y1-y0)*(x1-x0)
            sum_ = -1
            fuzzy_mu = -1
            if count == 0:
                if x0 == x1 and y0 == y1:
                    sum_ = int_img[y0, x0]
                    S_   = sum_
                if x1 == x0 and y0 != y1:
                    sum_ = int_img[y1, x1] - int_img[y0, x1]
                    sugeno_order  = np.asarray([int_img[y0, x1], int_img[y1, x1]])
                    fuzzy_mu = np.asarray([1, 0.5])

                    S_  = compute_sugeno(sugeno_order, fuzzy_mu)
                if y1 == y0 and x1 != x0:
                    sum_ = int_img[y1, x1] - int_img[y1, x0]
                    sugeno_order  = np.asarray([int_img[y1, x0], int_img[y1, x1]])
                    fuzzy_mu = np.asarray([1, 0.5])

                    S_  = compute_sugeno(sugeno_order, fuzzy_mu)
            else:
                sum_ = int_img[y1, x1] - int_img[y0, x1] - int_img[y1, x0] + int_img[y0, x0]
                if(int_img[y0, x1] > int_img[y1, x0] ):
                     sugeno_order = np.asarray([int_img[y0, x0], int_img[y1, x0], int_img[y0, x1], int_img[y1, x1]])
                else:
                     sugeno_order  = np.asarray([int_img[y0, x0], int_img[y0, x1], int_img[y1, x0], int_img[y1, x1]])
                fuzzy_mu = np.asarray([1, 0.75, 0.50, 0.25])
                S_  = compute_sugeno(sugeno_order, fuzzy_mu)

            choquet_mat[row,col] = sum_/count

            if input_img[row, col]*count  < sum_ * (1.-T)/1.:
                out_img_choquet[row,col] = 0
            else:
                out_img_choquet[row,col] = 1

            sugeno_mat[row,col] = S_/count
            #note is not only T
            if input_img[row, col]*count   <  S_ * (1.- T)/1.:
                out_img_sugeno[row,col] = 0
            else:
                out_img_sugeno[row,col] = 1

    return out_img_choquet, out_img_sugeno, choquet_mat, sugeno_mat, T

# In[ ]:

#Qualitative comparisons

# Compute the mean squared error and structural similarity
# index for the images
def compare_images(img1, img2):
    m = mse(img1, img2)
    s = measure.compare_ssim(img1, img2,  data_range=img2.max() - img2.min(), multichannel=False)
    ret = np.array([m,s])
    #the higher the ssim, the more "similar"
    return(ret)

def mse(img1, img2):
    err = np.sum((img1.astype("float") - img2.astype("float")) ** 2)
    err /= float(img1.shape[0] * img2.shape[1])
    #the lower the error, the more "similar"
    return(err)

#simple listing class in order to collect the results
class results_collector(object):
    def __init__(self, name, original_img, choquet_mat, sugeno_mat, count_matrix,
                 out_img_adapt_choquet,out_img_sugeno,out_img_bradley, c_m, s_m, T, elapsed_time,
                 mse_choquet, mse_sugeno, mse_bradley, ssim_choquet, ssim_sugeno, ssim_bradley,
                 th, a1, a2):
        self.name = name,
        self.img = original_img,
        self.choquet_mat = choquet_mat,
        self.sugeno_mat = sugeno_mat,
        self.count_matrix = count_matrix,
        self.out_img_adapt_choquet = out_img_adapt_choquet,
        self.out_img_sugeno = out_img_sugeno,
        self.out_img_bradley = out_img_bradley
        self.c_m = c_m,
        self.s_m = s_m,
        self.T  = T,
        self.elapsed_time = elapsed_time,
        self.mse_choquet  = mse_choquet,
        self.mse_sugeno   = mse_sugeno,
        self.mse_bradley  = mse_bradley,
        self.ssim_choquet = ssim_choquet,
        self.ssim_sugeno  = ssim_sugeno,
        self.ssim_bradley = ssim_bradley,
        self.th = th,
        self.a1 = a1,
        self.a2 = a2

#Embedded method for comparisons between groundtruth and Choquet thresholded images
def compute_multi_thresh(test_images, gt_images, opt = 0, T=0, a1=2, a2=2):
    count=0
    resc = []
    for i in test_images:
        test_image  =  i
        #plot_it(test_image)

        S1 = np.asarray(compute_summed_area_table(test_image))
        #S1 = get_int_img_m1(test_image)
        choquet_mat, sugeno_mat, count_matrix =                              adaptive_choquet_itegral(np.asarray(test_image),
                            S1,
                            opt,
                            log=False )
        #Choquet Adaptive Thresh
        out_img_adapt_choquet, out_img_sugeno, c_m, s_m, T =          adaptive_thresh2(np.asarray(test_image),
                            np.asarray(choquet_mat),
                            a1 = a1,
                            a2 = a2,
                            T= T,
                            log=False )     #with compute_summed_area table doesn't work.

        #Bradley Adaptive Thresh
        S1 = get_int_img_m1(test_image)
        out_img_bradley, bradley_int_mat =          adaptive_thresh(np.asarray(test_image),
                        S1 ,
                        a1=a1,
                        a2=a2,
                        T=T)

        #compare it
        mse_choquet, ssim_choquet = compare_images(gt_images[count], out_img_adapt_choquet)
        mse_sugeno,  ssim_sugeno  = compare_images(gt_images[count], out_img_sugeno)
        mse_bradley, ssim_bradley = compare_images(gt_images[count], out_img_bradley)

        #

        resc.append(results_collector("Comparisons", i, choquet_mat,
                                                    sugeno_mat,count_matrix,
                                                    out_img_adapt_choquet,
                                                    out_img_sugeno,
                                                    out_img_bradley,
                                                    c_m,
                                                    s_m,
                                                    T,
                                                    elapsed_time,
                                                    mse_choquet,
                                                    mse_sugeno,
                                                    mse_bradley,
                                                    ssim_choquet,
                                                    ssim_sugeno,
                                                    ssim_bradley,
                                                    T,
                                                    a1,
                                                    a2))

        count += 1

    return(resc)

def add_random_noise(small_image, perc=1):
    np.random.seed(1)
    mu, sigma = 0, 1 # mean and standard deviation
    s = np.random.normal(mu, sigma, small_image.shape)
    img_n = np.abs(s/s.max()) * perc
    img_ret = small_image + img_n
    return(img_ret)

# In[82]:

### Testing Grad/Glaze images vs Groundtruth / GT noise vs GT / Test+noise vs GT

def test_exp(test_images, gt_images, a1=7, a2=7, opt=0, scale = 0.01, noise_gt = -1, noise_test=-1):
    resc_a = []
    elapsed_time=0

    ### Add noise on the GroundTruth
    if noise_gt > 0:
        noise_img = []
        for i in range(len(gt_images)):
            noise_img.append(add_random_noise(gt_images[i], noise_gt))
        test_images = noise_img

    #Add noise on the test images
    if noise_test > 0:
        noise_img = []
        for i in range(len(test_images)):
            noise_img.append(add_random_noise(test_images[i], noise_test))
        test_images = noise_img
    # Test test_images or noised ones with respect the GT.
    for i in range(a1):
        for j in range(a2):
            x = scale
            if(i >= j ):
                print("Testing image conf ( i: " + str(i) + " j: " + str(j) + ")")
                t1 = time.process_time()
                while(x <= 1.01):
                    resc = compute_multi_thresh(test_images,gt_images,
                                    opt = opt,
                                    T=x,
                                    a1=i+1,
                                    a2=j+1)
                    x = x + scale
                    resc_a.append(resc)
                elapsed_time = time.process_time() - t1
                print('Out: {} images processed in {} seconds'.format(str(len(resc)), round(elapsed_time ,3)))
    return(resc_a)

##  Simple testing prints
##  It should return the list of the stuff
def search_results(resc_b, ssim_th = 0.5, attention_image = -1):
    count=0
    for i in range(len(resc_b)):
        for j in range(len(resc_b[-1])):
            if(resc_b[i][j].ssim_choquet[0] > resc_b[i][j].ssim_bradley[0]
                and resc_b[i][j].ssim_choquet[0] > ssim_th
                and resc_b[i][j].a1[0] != resc_b[i][j].a2):
                count= count+1
                print('{}-th image -------------------\n mse: C {} S {} B {}, \nssid: C {} S {} B {} \na1: {}, a2: {}, th: {}'.format(
                str(j),
                round(resc_b[i][j].mse_choquet[0],3),
                round(resc_b[i][j].mse_sugeno[0],3),
                round(resc_b[i][j].mse_bradley[0],3),
                round(resc_b[i][j].ssim_choquet[0],3),
                round(resc_b[i][j].ssim_sugeno[0],3),
                round(resc_b[i][j].ssim_bradley[0],3),
                str(resc_b[i][j].a1[0]),
                str(resc_b[i][j].a2),
                round(resc_b[i][j].th[0], 4) ))
                if(attention_image >= 0):
                    if(j==attention_image):
                        print("**********************************************************************************")
    print("Percentage of coverage around all the possible configurations" + str(count/(len(resc_b)*len(resc_b[-1]))))

# In[83]:

################################################################################
#### Test on a single image:
################################################################################

small_image  =  1.0 - import_img('./original/00.bmp')
plot_it(small_image)
S1 = np.asarray(compute_summed_area_table(small_image))
cdf_img   = cdf_image(small_image)
int_img   = get_int_img_m1(small_image)   # common
#int_img2  = get_int_img_m2(small_image, cum_distr)   #choquet int img

print("Image")
print(np.asarray(small_image))

print("summed area table")
print(np.asarray(summ_at))

print("integral image")
print(int_img)

plt.plot(np.asarray(cdf_img[0]), np.asarray( cdf_img[2][0:len(cdf_img[2])-1]), 'r--')

print("cumulative distribution of the image")
print(np.asarray(cdf_img[0]))

print("histogram")
print(np.asarray(cdf_img[1]))

print("range values")
print(np.asarray(cdf_img[2]))

choquet_mat, sugeno_mat, count_matrix =                      adaptive_choquet_itegral(np.asarray(small_image),
                    S1,
                    1,
                    log=False )
print("C mat")
plot_it(choquet_mat)

print("S mat")
plot_it(sugeno_mat)

print("-----------------------------------------------------------------------------------")
#Otsu T parameter
print("Image thresholded with the choquet integral image and an automatic Otsu threshold")
out_img_adapt_choquet, out_img_sugeno, c_m, s_m, T =  adaptive_thresh2(np.asarray(small_image),
                    np.asarray(choquet_mat),
                    a1 = 16,
                    a2 = 2, #Leave T = 0 for the Otsu
                    log=False )     #con compute_summed_area table doesn't work.

print("Threshold  " + str(T))
plot_it(out_img_adapt_choquet)
plot_it(out_img_sugeno)
plot_it(c_m)
plot_it(s_m)

print("-----------------------------------------------------------------------------------")
#Manual Parameter
print("Image thresholded with the choquet integral image and a fixed manual threshold.")
out_img_adapt_choquet, out_img_sugeno, c_m, s_m, T =  adaptive_thresh2(np.asarray(small_image),
                    np.asarray(choquet_mat),
                    a1 = 16,
                    a2 = 2,
                    T  = 0.2,
                    log=False )     #con compute_summed_area table doesn't work.

print("Threshold  " + str(T))
plot_it(out_img_adapt_choquet)
plot_it(out_img_sugeno)
plot_it(c_m)
plot_it(s_m)

# In[84]:

################################################################################
#### Toy dataset # Testing complex gradients, glazes, additive noise, smoothness
################################################################################

#Prepare the list data structures
#Groundtruth images
gt_images        = []
# Smothed, glazed GT images
test_images      = []

# In[85]:

###
# Definition of the toy dataset
###
small_image1 = [[0, 0, 0, 0, 0, 0,0,0,0],
                [0, 1, 0, 1, 0, 0,0,0,0],
                [1, 1, 1, 1, 1, 0,0,0,0],
                [0, 1, 0, 1, 0, 0,0,0,0],
                [0, 0, 1, 1, 1, 0,0,0,0],
                [0, 0, 0, 1, 0, 0,0,0,0],
                [0, 0, 1, 1, 1, 0,0,0,0],
                [0, 0, 0, 1, 0, 0,0,0,0],
                [0, 0, 1, 1, 1, 0,0,0,0]]

small_image1 = np.asarray(small_image1, dtype="float32")
gt_images.append(small_image1)
plot_it(small_image1)

small_image1 = [[0.2, 0.2, 0.1, 0.2, 0.15, 0.14,0.13,0.12,0.11],
                [0.16, 0.6, 0.2, 0.3, 0.15, 0.14,0.13,0.12,0.11],
                [0.6, 0.5, 0.6, 0.7, 0.8, 0.14,0.13,0.12,0.11],
                [0.14, 0.5, 0.2, 0.3, 0.15, 0.14,0.13,0.12,0.11],
                [0.15, 0.12, 0.3, 0.4, 0.3, 0.14,0.13,0.12,0.11],
                [0.14, 0.13, 0.2, 0.4, 0.15, 0.14,0.13,0.12,0.11],
                [0.15, 0.12, 0.3, 0.3, 0.3, 0.14,0.13,0.12,0.11],
                [0.14, 0.13, 0.2, 0.26, 0.1, 0.14,0.13,0.12,0.11],
                [0.15, 0.12, 0.25, 0.25, 0.25, 0.14,0.13,0.12,0.11]]

small_image1 = np.asarray(small_image1, dtype="float32")
test_images.append(small_image1)
plot_it(small_image1)

small_image2 = [[0, 0, 0, 0, 1,0,0,0,0],
                [0, 0, 0, 1, 0,1,0,0,0],
                [0, 0, 1, 0, 0,0,1,0,0],
                [0, 1, 0, 0, 0,0,0,1,0],
                [0, 1, 0, 0, 0,0,0,1,0],
                [0, 0, 1, 0, 0,0,1,0,0],
                [0, 0, 0, 1, 0,1,0,0,0],
                [0, 0, 0, 0, 1,0,0,0,0]]

small_image2 = np.asarray(small_image2, dtype="float32")
gt_images.append(small_image2)
plot_it(small_image2)

small_image2 = [[0.22, 0.19, 0.19, 0.18, 0.5, 0.11, 0.08, 0.06,0.02],
                [0.22, 0.19, 0.19, 0.5,  0.15,0.6,  0.08, 0.06,0.02],
                [0.22, 0.19, 0.40, 0.18, 0.15,0.11, 0.7,  0.06,0.02],
                [0.22, 0.30, 0.19, 0.18, 0.15,0.11, 0.08, 0.8,0.02],
                [0.22, 0.30, 0.19, 0.18, 0.15,0.11, 0.08, 0.8,0.02],
                [0.22, 0.19, 0.40, 0.18, 0.15,0.11, 0.7,  0.06,0.02],
                [0.22, 0.19, 0.19, 0.5,  0.15,0.6,  0.08, 0.06,0.02],
                [0.22, 0.19, 0.19, 0.18, 1   ,0.11, 0.08, 0.06,0.02]]

small_image2 = np.asarray(small_image2, dtype="float32")
plot_it(small_image2)
test_images.append(small_image2)

small_image3 = [[0,0,0, 0,  0, 0, 0,0],
                [0,0,0, 0,  0, 0, 0,0],
                [0,0,0, 1,  0, 0, 0,0],
                [0,0,1, 1,  1, 0, 0,0],
                [0,0,0, 1,  0, 1, 0,0],
                [0,0,0, 1,  1, 1, 1,0],
                [0,0,0, 1,  0, 1, 0,0],
                [0,0,0, 1,  0, 0, 0,0]]

small_image3 = np.asarray(small_image3, dtype="float32")
plot_it(small_image3)
gt_images.append(small_image3)

small_image3 = [[0.18,0.22, 0.15, 0.22,   0.20,   0.17, 0.15,0.14],
                [0.18,0.22, 0.15, 0.22,   0.20,   0.15, 0.17,0.1],
                [0.18,0.22, 0.15, 0.45,   0.20,   0.17, 0.15,0.14],
                [0.17,0.21, 0.35, 0.45,   0.55,   0.15, 0.17,0.1],
                [0.17,0.20, 0.15, 0.45,   0.20,   0.65, 0.15,0.14],
                [0.18,0.21, 0.15, 0.45,   0.55,   0.65, 0.75,0.1],
                [0.19,0.22, 0.15, 0.45,   0.20,   0.65, 0.15,0.14],
                [0.18,0.22, 0.15, 0.35,   0.20,   0.15, 0.17,0.1]]

small_image3 = np.asarray(small_image3, dtype="float32")
plot_it(small_image3)
test_images.append(small_image3)

small_image4 = [[0, 0,   0,   0, 0,0,0,0],
                [0, 0,   0,   0, 1,1,1,0],
                [0, 0,   0,   1, 1,1,1,0],
                [0, 0,   1,   1, 1,1,1,0],
                [0, 1,   1,   1, 1,1,1,0],
                [0, 0,   1,   1, 1,1,1,0],
                [0, 0,   0,   1, 1,1,1,0],
                [0, 0,   0,   0, 1,1,1,0]
               ]

small_image4 = np.asarray(small_image4, dtype="float32")
small_image6 = np.asarray(np.transpose(small_image4), dtype="float32")
plot_it(small_image4)
gt_images.append(small_image4)
gt_images.append(small_image6)

small_image4 = [[0.1, 0.1,   0.3,   0.2, 0.2,0.1,0,  0],
                [0.1, 0.15,   0.3,   0.2, 0.4,0.6,0.6,0],
                [0.1, 0.15,   0.3,   0.5, 0.5,0.5,0.6,0],
                [0.2, 0.15,   0.6,   0.5, 0.55,0.5,0.6,0],
                [0.2, 0.8,   0.7,   0.5, 0.55,0.5,0.6,0],
                [0.2, 0.15,   0.6,   0.5, 0.55,0.5,0.6,0],
                [0.1, 0.1,   0.3,   0.5, 0.5,0.5,0.6,0],
                [0.1, 0.1,   0.3,   0.2, 0.5,0.5,0.6,0]
               ]

small_image4 = np.asarray(small_image4, dtype="float32")
small_image6 = np.asarray(np.transpose(small_image4), dtype="float32")
plot_it(small_image4)
test_images.append(small_image4)
test_images.append(small_image6)

small_image5 = [[1., 0., 0., 0., 0., 0., 0., 0.],
                [0., 1., 0., 0., 0., 0., 0., 0.],
                [0., 0., 1., 0., 0., 0., 0., 0.],
                [0., 0., 0., 0., 1., 0,  0., 0.],
                [0., 0., 0., 1., 1., 1., 0., 0.],
                [0., 0., 0., 0., 1., 0., 0., 0.],
                [0., 0., 0., 0., 0., 0., 0., 0.],
                [0., 0., 0., 0., 0., 0., 0., 0.]
               ]

small_image5 = np.asarray(small_image5, dtype="float32")
plot_it(small_image5)
gt_images.append(small_image5)

small_image5 = [[0.4, 0. , 0.,   0., 0., 0., 0., 0.],
                [0. , 0.5, 0.,   0., 0., 0., 0., 0.],
                [0. , 0. , 0.6, 0.1, 0.1, 0.1, 0.1, 0.],
                [0. , 0. , 0.1, 0.1, 1.0, 0.1, 0.1, 0.],
                [0. , 0. , 0.1, 0.7, 0.8, 0.9, 0.1, 0.],
                [0. , 0. , 0.1, 0.1, 0.9, 0.1, 0.1, 0.],
                [0. , 0. , 0.1, 0.2, 0.1, 0.1, 0.1, 0.],
                [0. , 0. ,  0., 0., 0., 0., 0., 0.]

               ]

small_image5 = np.asarray(small_image5, dtype="float32")
plot_it(small_image5)
test_images.append(small_image5)

small_image7 = [[0,0,  0, 0,   0, 0,0,0],
                [0,0,  0, 0,   0, 0,0,0],
                [0,0,  0, 1,   0, 0,0,0],
                [0,0,  1, 0  , 1, 0,0,0],
                [0,0,  0, 1,   0, 0,0,0],
                [0,0,  1, 0,   1, 0,0,0],
                [0,0,  0, 1,   0, 0,0,0],
                [0,0,  0, 0,   0, 0,0,0], ]

small_image7 = np.asarray(small_image7, dtype="float32")
plot_it(small_image7)
gt_images.append(small_image7)

small_image7 = [[0,0.1, 0.2, 0.2,   0.2, 0.1,0,0],
                [0,0.1, 0.2, 0.2,   0.2, 0.1,0,0],
                [0,0.1, 0.2, 0.7,   0.2, 0.1,0,0],
                [0,0.3,  0.6, 0.2  , 0.6, 0.1,0,0],
                [0,0.3,  0.2, 0.8,   0.2, 0.25,0,0],
                [0,0.1,  0.7, 0.2,   0.6, 0.1,0,0],
                [0,0.1,  0.1, 0.6,   0.2, 0.2,0,0],
                [0,0.1,  0.1, 0.1,   0.2, 0.2,0,0]
                ]

small_image7 = np.asarray(small_image7, dtype="float32")
plot_it(small_image7)
test_images.append(small_image7)

small_image8 = [
               [0, 0, 0,  0,  0,0,1,0],
               [0, 0, 0,  0,  0,1,0,0],
               [0, 0, 0,  0,  1,0,0,0],
               [ 0, 0, 0,  1, 0,0,0,0],
               [ 0, 0, 1,  0, 0,0,0,0],
               [ 0, 1, 0,  0, 0,0,0,0],
               [ 1, 0, 0,  0, 0,0,0,0],
               [0, 0, 0, 0, 0, 0, 0, 0]]

small_image8 = np.asarray(small_image8, dtype="float32")
plot_it(small_image8)
gt_images.append(small_image8)

small_image8 = [[0,  0,  0,   0,  0,        0.4,  1, 0.5],
               [0,  0,      0,   0,   0.3,  0.95,  0.4, 0.5],
               [0,  0,       0, 0.3,  0.9,  0.4,  0,   0],
               [0,  0,     0.3, 0.8,  0.3,  0,    0,   0],
               [0,   0.2,  0.8, 0.3,  0,    0,    0,   0],
               [0.2, 0.8,  0.3,  0,  0,     0,    0,   0],
               [0.8, 0.2,  0,    0,  0,     0,    0,   0],
               [0.2,   0,  0,    0,  0,     0,    0,   0]]

small_image8 = np.asarray(small_image8, dtype="float32")
plot_it(small_image8)
test_images.append(small_image8)

# In[86]:

### OPT 0,1,2 on Testing imgs vs GT
test_0a = test_exp(test_images, gt_images, a1=7, a2=7, opt=0, scale = 0.2)
test_0b = test_exp(test_images, gt_images, a1=7, a2=7, opt=1, scale = 0.2)
test_0c = test_exp(test_images, gt_images, a1=7, a2=7, opt=2, scale = 0.2)

search_results(test_0a, ssim_th = 0.3, attention_image = 2)
search_results(test_0b, ssim_th = 0.3, attention_image = 2)
search_results(test_0c, ssim_th = 0.3, attention_image = 2)

### OPT 0,1,2 on GT noised vs GT +20%
test_1a = test_exp(test_images, gt_images, a1=7, a2=7, opt=0, scale = 0.2, noise_gt = 0.2)
test_1b = test_exp(test_images, gt_images, a1=7, a2=7, opt=1, scale = 0.2, noise_gt = 0.2)
test_1c = test_exp(test_images, gt_images, a1=7, a2=7, opt=2, scale = 0.2, noise_gt = 0.2)

search_results(test_1a, ssim_th = 0.3, attention_image = 2)
search_results(test_1b, ssim_th = 0.3, attention_image = 2)
search_results(test_1c, ssim_th = 0.3, attention_image = 2)

### OPT 0,1,2 on Testing imgs noised vs GT + 20%
test_2a = test_exp(test_images, gt_images, a1=7, a2=7, opt=0, scale = 0.2, noise_test = 0.2)
test_2b = test_exp(test_images, gt_images, a1=7, a2=7, opt=1, scale = 0.2, noise_test = 0.2)
test_2c = test_exp(test_images, gt_images, a1=7, a2=7, opt=2, scale = 0.2, noise_test = 0.2)

search_results(test_2a, ssim_th = 0.3, attention_image = 2)
search_results(test_2b, ssim_th = 0.3, attention_image = 2)
search_results(test_2c, ssim_th = 0.3, attention_image = 2)

# In[23]:

#####################
### Berkeley Dataset

imgs =    ['./original/00.bmp', './original/01.bmp','./original/02.bmp',
           './original/03.bmp', './original/04.bmp','./original/05.bmp',
           './original/06.bmp', './original/07.bmp','./original/08.bmp',
           './original/09.bmp' ]
imgs_gt = [  './gtruth/00.bmp',  './gtruth/01.bmp',  './gtruth/02.bmp',
             './gtruth/03.bmp',  './gtruth/04.bmp',  './gtruth/05.bmp',
             './gtruth/06.bmp',  './gtruth/07.bmp',  './gtruth/08.bmp',
             './gtruth/09.bmp' ]

test_images2  = []
test_images2.append(1.0 - import_img(imgs[0]))
test_images2.append(1.0 - import_img(imgs[1]))
test_images2.append(1.0 - import_img(imgs[2]))
test_images2.append(1.0 - import_img(imgs[3]))
test_images2.append(1.0 - import_img(imgs[4]))
test_images2.append(1.0 - import_img(imgs[5]))
test_images2.append(1.0 - import_img(imgs[6]))
test_images2.append(1.0 - import_img(imgs[7]))
test_images2.append(1.0 - import_img(imgs[8]))
test_images2.append(1.0 - import_img(imgs[9]))

test_images_gt_2  = []
test_images_gt_2.append(1.0 - import_img(imgs_gt[0]))
test_images_gt_2.append(1.0 - import_img(imgs_gt[1]))
test_images_gt_2.append(1.0 - import_img(imgs_gt[2]))
test_images_gt_2.append(1.0 - import_img(imgs_gt[3]))
test_images_gt_2.append(1.0 - import_img(imgs_gt[4]))
test_images_gt_2.append(1.0 - import_img(imgs_gt[5]))
test_images_gt_2.append(1.0 - import_img(imgs_gt[6]))
test_images_gt_2.append(1.0 - import_img(imgs_gt[7]))
test_images_gt_2.append(1.0 - import_img(imgs_gt[8]))
test_images_gt_2.append(1.0 - import_img(imgs_gt[9]))

# In[ ]:

###Berkeley
### Testing Grad/Glaze images vs Groundtruth - t-norm choquet
### From a range from 0 to 1 it requires 2963.407 seconds
### better fixing a 16/2 and not trying all the possible combs.
brk_resc = []
t1 = time.process_time()
x=0
while(x <= 1.00):
    resc = compute_multi_thresh(test_images2, test_images_gt_2,
                                opt = 0,
                                T=x,
                                a1=16,
                                a2=2)
    x = x + 0.01
    brk_resc.append(resc)
    elapsed_time = time.process_time() - t1
    print(x)
    print('Images processed in {} seconds'.format(round(elapsed_time ,3)))

# In[110]:

print(len(brk_resc))

# In[114]:

for i in range(len(brk_resc)):
    for j in range(len(brk_resc[-1])):
        if(brk_resc[i][j].ssim_choquet[0] > brk_resc[i][j].ssim_bradley[0]
            and brk_resc[i][j].ssim_choquet[0] >0.3
            and brk_resc[i][j].a1[0] != brk_resc[i][j].a2):
            count= count+1
            print('{}-th image -------------------\n mse: C {} S {} B {}, \nssid: C {} S {} B {} \na1: {}, a2: {}, th: {}'.format(
            str(j),
            round(brk_resc[i][j].mse_choquet[0],3),
            round(brk_resc[i][j].mse_sugeno[0],3),
            round(brk_resc[i][j].mse_bradley[0],3),
            round(brk_resc[i][j].ssim_choquet[0],3),
            round(brk_resc[i][j].ssim_sugeno[0],3),
            round(brk_resc[i][j].ssim_bradley[0],3),
            str(brk_resc[i][j].a1[0]),
            str(brk_resc[i][j].a2),
            round(brk_resc[i][j].th[0], 4) ))

# In[176]:

#### Example of the chessboard
test_image = 1.0 - import_img('./original/chessboard.png')

#Choquet Adaptive Thresh
choquet_mat, _, _ =  adaptive_choquet_itegral(np.asarray(test_image),
                            S1,
                            0,                   #t-norm version
                            log=False )
out_img_adapt_choquet, _, _, _, T =  adaptive_thresh2(np.asarray(test_image),
                            np.asarray(choquet_mat),
                            a1 = 16,
                            a2 = 2,
                            T= 0.095,
                            log=False )     #con compute_summed_area table doesn't work.

#Choquet Adaptive Thresh
choquet_mat, _, _ =  adaptive_choquet_itegral(np.asarray(test_image),
                            S1,
                            1,                  #choquet int version
                            log=False )
out_img_adapt_choquet2, _, _, _, T =  adaptive_thresh2(np.asarray(test_image),
                            np.asarray(choquet_mat),
                            a1 = 16,
                            a2 = 2,
                            T= 0.095,
                            log=False )     #con compute_summed_area table doesn't work.

#Bradley Adaptive Thresh
S1 = get_int_img_m1(test_image)
out_img_bradley, bradley_int_mat =  adaptive_thresh(np.asarray(test_image),
                        S1 ,
                        a1=16,
                        a2=2,
                        T=T)

# In[177]:

#Choquet Adaptive Thresh
plot_it(test_image)
plot_it(out_img_adapt_choquet2)
plot_it(out_img_adapt_choquet)
plot_it(out_img_bradley)

print(compare_images(out_img_adapt_choquet, out_img_bradley))
print(compare_images(out_img_adapt_choquet2, out_img_bradley))

# In[ ]:

# In[ ]:


#!/usr/bin/env python
import rbd
import rados
import json
import subprocess
from itertools import chain
from texttable import Texttable, get_color_string, bcolors

def f(x):
    if x=="quota_max_bytes":
        return str(pool[x]/1024/1024)
    else:
        return str(pool[x])

p = subprocess.check_output('ceph osd dump -f json-pretty', shell=True)
pools       = json.loads(p)['pools']
pools_table = Texttable()
header      = [ "Id", "Pool", "Size", "Min_size", "Pg_num", "Pgp_num", "Crush","Quota (MB)", "Quota (obj)" ]
keys        = [ "pool", "pool_name", "size", "min_size", "pg_num", "pg_placement_num", "crush_ruleset","quota_max_bytes","quota_max_objects" ]
pools_table.header(map(lambda x: get_color_string(bcolors.YELLOW, x), header))
for pool in pools:
    pools_table.add_row(map(f, keys))

table = Texttable()
table.set_deco(Texttable.BORDER | Texttable.HEADER | Texttable.VLINES)
table.set_cols_align( [ "l", "l", "l", "l", "l", "l", "l" ])
table.set_cols_valign([ "m", "m", "m", "m", "m", "m", "m" ])
table.set_cols_width([ "20", "20", "8","8","20","8","8"])
header = [ "Pool", "Image", "Size(Mb)", "Features", "Lockers", "Str_size", "Str_cnt" ]
keys   = [ "features", "list_lockers", "stripe_unit", "stripe_count" ]
table.header(map(lambda x: get_color_string(bcolors.YELLOW, x), header))

with rados.Rados(conffile='/etc/ceph/ceph.conf') as cluster:
    pool_list = cluster.list_pools()
    for pool in pool_list:
        table.add_row([  get_color_string(bcolors.GREEN, pool) , "", "", "", "", "", "" ])
        with cluster.open_ioctx(pool) as ioctx:
            rbd_inst = rbd.RBD()
            image_list = rbd_inst.list(ioctx)
            for image_name in image_list:
                with rbd.Image(ioctx, image_name) as image:
                    image_size = str(image.size()/1024**2)
                    table.add_row(["", image_name, image_size] + map(lambda x: str(getattr(image,x)()), keys))
        if pool != pool_list[-1]:
            table.add_row([ "-"*20, "-"*20,"-"*8,"-"*8,"-"*20,"-"*8,"-"*8 ])

print(pools_table.draw())
print
print(table.draw())


from odoo import api, fields, models, _
from odoo.exceptions import UserError, ValidationError, Warning
import ipdb
from odoo.addons import decimal_precision as dp

class DepositoMakeInvoice(models.TransientModel):
    _name = 'deposito.make.invoice'
    _description = 'Deposito Crear Factura'

    def cargar_campos_impresion(self, partner, invoice):
        invoice.print_output_reference = partner.print_output_reference
        invoice.print_origin_destiny_grouped = partner.print_origin_destiny_grouped
        invoice.print_cont_grouped = partner.print_cont_grouped
        invoice.print_product_grouped = partner.print_product_grouped
        invoice.print_invoice_load = partner.print_invoice_load
        invoice.print_invoice_product = partner.print_invoice_product
        invoice.print_date_start = partner.print_date_start
        invoice.print_ms_in_out = partner.print_ms_in_out
        invoice.print_mic = partner.print_mic
        invoice.print_crt = partner.print_crt
        invoice.print_consignee = partner.print_consignee
        invoice.print_purchase_order = partner.print_purchase_order
        invoice.print_origin_destiny = partner.print_origin_destiny
        invoice.print_container_number = partner.print_container_number
        invoice.print_container_size = partner.print_container_size
        invoice.print_booking = partner.print_booking
        invoice.print_gex = partner.print_gex
        invoice.print_sender = partner.print_sender
        invoice.print_dua = partner.print_dua
        invoice.print_packages = partner.print_packages
        invoice.print_kg = partner.print_kg
        invoice.print_volume = partner.print_volume
        invoice.print_extra_info = partner.print_extra_info
        invoice.show_extra_info = partner.show_extra_info

    def calcular_diario(self, partner_id):
        journal_obj = self.env['account.journal']
        if partner_id.vat_type == '2' and partner_id.country_id.code == 'UY':
            # e-Factura
            journal_id = journal_obj.search([('code', '=', 'EF')]).id
        if (partner_id.vat_type == '4' and partner_id.country_id.code != 'UY') or partner_id.vat_type == '3':
            # e-Ticket
            journal_id = journal_obj.search([('code', '=', 'ET')]).id

        return journal_id

    def generar_costos(self, products=None):
        cost_obj = self.env['rt.service.product.supplier']
        tax_obj = self.env['account.tax']

        if not products:
            return
        for prod in products:
            taxes = tax_obj.search([('name', '=', 'IVA Directo Op  Grav B')])
            if prod.product_id.name == 'Alquiler':
                taxes = tax_obj.search([('name', '=', 'Compras Exentos IVA')])
            if prod.product_type == 'terceros' and prod.is_outgoing:
                if prod.supplier_id:
                    if prod.valor_compra:
                        if prod.valor_compra_currency_id:
                            if prod.supplier_ids:
                                #vamos a borrar los costos
                                for prd in prod.supplier_ids:
                                    if not prd.invoice_id:
                                        prd.unlink()
                            line_dict = {}
                            line_dict['deposito_id'] = prod.deposito_srv_id.id
                            line_dict['supplier_id'] = prod.supplier_id.id
                            line_dict['currency_id'] = prod.valor_compra_currency_id.id
                            line_dict['amount'] = prod.valor_compra
                            if prod.product_id.name == 'Alquiler':
                                line_dict['price_subtotal'] = prod.valor_compra
                            else:
                                line_dict['price_subtotal'] = prod.valor_compra * 1.22
                            line_dict['ref'] = prod.deposito_srv_id.referencia
                            line_dict['rt_service_id'] = False
                            line_dict['rt_consol_product_id'] = False
                            line_dict['rt_marfrig_product_id'] = False
                            line_dict['rt_deposito_product_id'] = prod.id
                            line_dict['service_state'] = prod.state
                            line_dict['tax_ids'] = [(6, 0, taxes.ids)]
                            line_dict['service_date'] = prod.start
                            line_dict['origin_id'] = prod.origin_id.id
                            line_dict['destiny_id'] = prod.destiny_id.id
                            line_dict['product_id'] = prod.product_id.id
                            line_dict['output_reference'] = prod.name
                            line_dict['partner_invoice_id'] = prod.deposito_srv_id.partner_invoice_id.id
                            result = cost_obj.create(line_dict)

    @api.multi
    def make_invoices(self):
        inv_obj = self.env['account.invoice']
        if not self._context.get('active_ids'):
            return {'type': 'ir.actions.act_window_close'}
        product_service = self.env['deposito.service.products'].browse(self._context.get('active_ids'))
        self.generar_costos(products=product_service)
        tax_obj = self.env['account.tax']
        account_obj = self.env['account.account']
        operation_taxes = {
                           'exento': False,
                           'asimilado': tax_obj.search([('name', '=', 'IVA Venta asimilado a exportación')]),
                           'gravado': tax_obj.search([('name', '=', 'IVA Ventas (22%)')])
        }
        lineas = []
        for line in product_service:
            taxes = operation_taxes['gravado']
            account = account_obj.search([('code', '=', '41021001')])
            line_dict = {}
            line_dict['name'] = line.name
            line_dict['account_id'] = account.id
            line_dict['price_unit'] = line.importe
            line_dict['uom_id'] = line.product_id.uom_id.id
            line_dict['product_deposito_srv_id'] = line.id
            line_dict['product_id'] = line.product_id.id
            line_dict['invoice_line_tax_ids'] = [(6, 0, taxes.ids)]
            lineas.append((0, 0, line_dict))
            #Facturado
            line.invoiced = True
            line.deposito_srv_id.state = 'invoiced'

        journal_id = self.calcular_diario(line.partner_invoice_id)

        invoice = inv_obj.create({
            'name': line.partner_invoice_id.name or '',
            'origin': line.name,
            'type': 'out_invoice',
            'account_id': line.partner_invoice_id.property_account_receivable_id.id,
            'partner_id': line.partner_invoice_id.id,
            'journal_id': journal_id,
            'currency_id': line.currency_id.id,
            'fiscal_position_id': line.partner_invoice_id.property_account_position_id.id,
            'company_id': line.deposito_srv_id.company_id.id,
            'user_id': line.deposito_srv_id.user_id.id,
            'deposito_operation_id': line.deposito_srv_id.id,
            'invoice_line_ids': lineas
        })

        # line.invoices_ids += invoice
        # line.deposito_srv_id.invoices_ids

        partner = line.partner_invoice_id
        self.cargar_campos_impresion(partner, invoice)

        if self._context['open_invoices']:
            return {
                'domain': [('id', 'in', invoice.ids)],
                'name': 'Invoices',
                'view_type': 'form',
                'view_mode': 'tree,form',
                'res_model': 'account.invoice',
                'view_id': False,
                'views': [(self.env.ref('account.invoice_tree').id, 'tree'),
                          (self.env.ref('account.invoice_form').id, 'form')],
                'context': "{'type':'out_invoice'}",
                'type': 'ir.actions.act_window'
            }
        else:
            return {'type': 'ir.actions.act_window_close'}


from typing import Dict

from tools.coco_dataset_metrics import COCODatasetMetrics
from tools.tools import load_json_data
from detectron2_metrics import TrainingMetrics, InferenceMetrics

def load_annotations(annotation_paths: Dict):
    """
    Load annotations from path and assign them to corresponding key.
    :param annotation_paths: Dict
    :return: Dict - {key: annotations}
    """

    annotations = {}
    for key, path in annotation_paths.items():
        annotations[key] = load_json_data(path)
    return annotations

if __name__ == '__main__':
    # Setup path variables
    annotation_org_dataset_path = {
        'Original dataset': '../data/testing_annotations/labels_household_object_detection_newest.json'
    }

    annotation_split_dataset_path = {
        'Split data - train': '../data/labels/train_coco_annotations.json',
        'Split data - validation': '../data/labels/val_coco_annotations.json',
        'Split data - test': '../data/labels/test_coco_annotations.json'
    }

    annotation_generated_split_dataset_path = {
        'Generated data - train': '../generated_data/labels/train_coco_annotations.json',
        'Generated data - validation': '../generated_data/labels/val_coco_annotations.json',
        'Generated data - test': '../generated_data/labels/test_coco_annotations.json'
    }

    all_annotations = {
        'original_dataset': annotation_org_dataset_path,
        'original_split_dataset': annotation_split_dataset_path,
        'generated_split_dataset': annotation_generated_split_dataset_path
    }

    # Iterate over the annotations and plot corresponding dataset distributions
    for key, annotation_paths in all_annotations.items():
        annotations = load_annotations(annotation_paths)

        dataset_metrics = COCODatasetMetrics(annotations)
        dataset_metrics.plot_metrics(key)

    # Plot training and inference metrics for all training runs
    training_metrics = TrainingMetrics()
    inference_metrics = InferenceMetrics()

    training_metrics.plot_training_metrics()
    inference_metrics.plot_inference_metrics()


A, B = map(int, input().split())
sosu_list = []

for i in range(A, B + 1):
    if i == 1:
        continue
    for j in range(2, int(i**0.5)+1):
        if i % j == 0:  # 너 소수아님
            break
    else:
        print(i)


import sys, copy

from udp_interface import udp_interface
import numpy as np
import os
# from ppo.run import train
# from baselines.common import tf_util as U
from utils.action_filter import ActionFilterButter
from utils.reference_generator import ReferenceMotionGenerator
from collections import deque
from utils.utility import *
from utils.quaternion_function import euler2quat
import time
from sshkeyboard import listen_keyboard
import threading
import torch
from rsl_rl.modules import ActorCritic
from dataclasses import dataclass, field

@dataclass
class RobotState:
    trans_vel: np.ndarray
    trans_acc: np.ndarray
    rot_quat: np.ndarray
    rot_vel: np.ndarray
    motor_pos: np.ndarray
    motor_vel: np.ndarray

NUM_MOTORS=12
class ExpEnv():
    def __init__(self, ref_file='../motions/MotionLibrary/LiftingMotion_Simulator.motionlib', model_path=None, cfg=None,
                    recv_IP=None, recv_port=None,send_IP=None,send_port=None):

        self.robot = udp_interface(recv_IP=recv_IP,
                                recv_port=recv_port,
                                send_IP=send_IP,
                                send_port=send_port)
        self.dir = os.path.dirname(os.path.abspath(__file__))

        self.rt_freq = 1000
        self.exp_env_freq = 30
        self.num_sims_per_env_step = self.rt_freq // self.exp_env_freq
        self.secs_per_policy_step = self.num_sims_per_env_step / self.rt_freq
        self.policy_freq = 1 / self.secs_per_policy_step

        self.motor_kps = [100.0]*12
        self.motor_kds = 4*[1.0,2.0,2.0]

        self.motor_vel_idx = [i+6 for i in range(NUM_MOTORS)]

        '''a1, but also applied on mini-cheetah'''
        self.default_target_positions = [0.0,1.0661,-2.1869, 0.0,1.0661,-2.1869,
                                         0.0,1.0661,-2.1869, 0.0,1.0661,-2.1869]

        self.action_bounds = np.array([[-0.7767,  0.7767],
                                        [-0.3011,  3.7045],
                                        [-2.8500, -0.1500],
                                        [-0.7767,  0.7767],
                                        [-0.3011,  3.7045],
                                        [-2.8500, -0.1500],
                                        [-0.7767,  0.7767],
                                        [-0.3011,  3.7045],
                                        [-2.8500, -0.1500],
                                        [-0.7767,  0.7767],
                                        [-0.3011,  3.7045],
                                        [-2.8500, -0.1500]]).T

        self.history_len = 15
        self.action_filter_order = 2
        self.action_filter = ActionFilterButter(lowcut=None, highcut=[4], sampling_rate=self.policy_freq,order=self.action_filter_order,num_joints=NUM_MOTORS)

        self.selected_policy = 0
        self.use_planner = True
        self.ever_jump = False
        self.policy_running = False

        self.init_model(model_path)
        self.init_robot_state()
        self.previous_obs = deque(maxlen=self.history_len)
        self.previous_acs = deque(maxlen=self.history_len)
        self.reference_generator = ReferenceMotionGenerator(ref_file, 2000, self.secs_per_policy_step)
        self.__reset()
        if not self.use_planner:
            self.reference_generator.set_policy(self.selected_policy)

        self.low_obs_act = []

    def init_robot_state(self):
        trans_vel = np.zeros((3,))
        trans_acc = np.zeros((3,))
        rot_vel = np.zeros((3,))
        rot_quat = np.zeros((4,))
        motor_pos = np.zeros((NUM_MOTORS,))
        motor_vel = np.zeros((NUM_MOTORS,))
        self.obs_robot_state = RobotState(trans_vel=trans_vel, trans_acc=trans_acc, rot_quat=rot_quat, rot_vel=rot_vel, motor_pos=motor_pos, motor_vel=motor_vel)

    def init_model(self, model_path):
        self.pi = []
        if isinstance(model_path, str):
            selection = input("Single policy experiment, press 1 or 2 to select policy, any key to exit: ")
            selection = int(selection)
            if selection == 1 or selection == 2:
                self.selected_policy = selection
                self.use_planner = False
                path = [None, None]
                path[self.selected_policy - 1] = model_path
                model_path = path
            else:
                raise NotImplementedError
        for model in model_path:
            pi = ActorCritic( 499, 536, NUM_MOTORS, actor_hidden_dims=[512, 256, 128], #499, 536
                            critic_hidden_dims=[512, 256, 128])
            if model is not None:
                loaded_dict = torch.load(model, map_location=torch.device('cpu'))
                pi.load_state_dict(loaded_dict['model_state_dict'])
            pi.eval()
            self.pi.append(pi)

    def __process_recv_package(self, obs):
        self._raw_state = obs
        # Convert quaternion from wxyz to xyzw, which is default for Pybullet.
        rpy = self._raw_state[0:3]
        q = euler2quat(rpy[0], rpy[1], rpy[2])
        self.obs_robot_state.motor_pos = np.array(self._raw_state[6:18])
        self.obs_robot_state.rot_quat = np.copy(np.array([q[1], q[2], q[3], q[0]]))
        # print(self.obs_robot_state.rot_quat)

        ''' Thigh and Calf joints are reversed on the real robot '''
        self.obs_robot_state.motor_pos[[1,2,4,5,7,8,10,11]] *= -1

    def __get_observation(self, acs = np.zeros(NUM_MOTORS), step = False):
        __acs = np.copy(acs)
        ref_dict_1 = self.reference_generator.getReferenceMotion(look_forward=1)
        ref_dict_4 = self.reference_generator.getReferenceMotion(look_forward=4)
        ref_dict_7 = self.reference_generator.getReferenceMotion(look_forward=7)

        ob1 = ref_dict_1["joints_rot"]
        ob4 = ref_dict_4["joints_rot"]
        ob7 = ref_dict_7["joints_rot"]

        ob_curr = np.concatenate([self.obs_robot_state.rot_quat, self.obs_robot_state.motor_pos])

        bezier_param = np.concatenate([self.reference_generator.get_bezier_coefficients(), self.reference_generator.get_fixed_motion_duration(),
                                       self.reference_generator.get_motion_t_norm(), self.reference_generator.get_motion_phase()]) # motion_type: 0,1,2,3,4

        feet_pos = np.concatenate([ref_dict_1["foot_pos_bezier"], ref_dict_4["foot_pos_bezier"], ref_dict_7["foot_pos_bezier"]])

        if self.timestep == 0:
            [self.previous_obs.append(ob_curr) for i in range(self.history_len)]
            [self.previous_acs.append(self.default_target_positions) for i in range(self.history_len)]

        ob_prev = np.concatenate([np.array(self.previous_obs).flatten(), np.array(self.previous_acs).flatten()])

        # print(bezier_param)
        if step:
            self.previous_obs.append(ob_curr)
            self.previous_acs.append(__acs)

        self.curr_obs = np.concatenate([ob_prev, ob_curr, ob1, ob4, ob7, bezier_param, feet_pos])

    def process_send_cmd(self, motor_commands):
        return motor_commands

    def acs_norm2actual(self, acs):
        return self.action_bounds[0] + (acs + 1)/2.0 * (self.action_bounds[1] - self.action_bounds[0])

    def acs_actual2norm(self, actual_acs):
        return (actual_acs - self.action_bounds[0])*2 / (self.action_bounds[1] - self.action_bounds[0]) - 1

    def __get_action(self):
        print(self.selected_policy, self.curr_obs[-15:-9])
        if self.selected_policy == 0:
            acs = np.copy(self.acs_actual2norm(self.default_target_positions))

        else:
            acs = self.pi[self.selected_policy - 1].act_inference(torch.from_numpy(self.curr_obs).to(torch.float32).unsqueeze(0))[0]
            acs = acs.detach().numpy()
            acs = np.clip(np.copy(acs), -1, 1)

        if self.selected_policy == 1 or self.selected_policy == 2:
            if self.reference_generator.time_in_sec < 0.33:
                acs[[6,9]] = np.clip(acs[[6,9]], -0.2, 0.2)
            else:
                acs[[6,9]] = np.clip(acs[[6,9]], -0.8, 0.8)
        assert acs.shape[0] == 12 and -1.0 <= acs.all() <= 1.0

        if self.timestep == 0: # prevent zero action output
            default_action = np.array(self.default_target_positions)
            self.actual_pTs_filtered = default_action
            self.action_filter.init_history(self.acs_actual2norm(default_action))

        pTs_filtered = np.copy(self.action_filter.filter(np.copy(acs)))
        actual_pTs_filtered = np.copy(self.acs_norm2actual(pTs_filtered))

        return actual_pTs_filtered, np.copy(self.curr_obs), np.copy(acs)

    def __env_update(self):
        # if self.timestep<3:
        self.timestep += 1
        self.time_in_sec = (self.timestep*self.num_sims_per_env_step) / self.rt_freq
        self.reference_generator.update_step(self.timestep)

    def __reset(self):
        self.action_filter.reset()
        self.timestep = 0.0
        self.est_timestep = 0
        self.time_in_sec = 0.0
        self.actual_pTs = np.zeros(NUM_MOTORS)
        self.actual_pTs_filtered = np.zeros(NUM_MOTORS)

    def pid_ctrl(self):
        policy_count = 0
        previous_time = time.time()
        t = threading.currentThread()
        a1_default_target_positions = np.array([0.0,0.9,-1.8, 0.0,0.9,-1.8,
                                         0.0,0.9,-1.8, 0.0,0.9,-1.8])
        while getattr(t, "do_run", True):
            obs = self.robot.receive_observation()
            self.__process_recv_package(np.copy(obs))
            if policy_count % 1 == 0:
                self.__get_observation(np.copy(self.default_target_positions), step=False)
                self.actual_pTs_filtered_sent = np.copy(a1_default_target_positions)
                for i in range(4):
                    self.actual_pTs_filtered_sent[3*i+1] = -self.actual_pTs_filtered_sent[3*i+1]
                    self.actual_pTs_filtered_sent[3*i+2] = -self.actual_pTs_filtered_sent[3*i+2]
                cmd=self.process_send_cmd(np.concatenate((self.actual_pTs_filtered_sent,[0.0, 0.0, 0.0],np.zeros((12,)), [0.0,0.0,0.0])))

            self.robot.send_command(cmd)
            policy_count += 1
            current_time = time.time()
            # print("proc", "Frequency: ", 1/(current_time - previous_time + 1e-10))
            previous_time = current_time
            delay = 0.6
            _ = time.perf_counter() + delay/1000
            while time.perf_counter() < _:
                pass

    def pid_ctrl_squat_prep(self):
        policy_count = 0
        previous_time = time.time()
        t = threading.currentThread()
        a1_default_target_positions = np.array([0.0,0.9,-1.8, 0.0,0.9,-1.8,
                                         0.0,0.9,-1.8, 0.0,0.9,-1.8])

        while getattr(t, "do_run", True):
            obs = self.robot.receive_observation()
            self.__process_recv_package(np.copy(obs))
            if policy_count > 30:
                self.actual_pTs_filtered_sent = np.copy(self.default_target_positions)
                for i in range(4):
                    self.actual_pTs_filtered_sent[3*i+1] = -self.actual_pTs_filtered_sent[3*i+1]
                    self.actual_pTs_filtered_sent[3*i+2] = -self.actual_pTs_filtered_sent[3*i+2]
                break

            else:
                self.__get_observation(np.copy(self.default_target_positions), step=False)
                self.actual_pTs_filtered_sent = (30 - policy_count) / 30 * a1_default_target_positions + policy_count / 30 * np.array(self.default_target_positions)
                for i in range(4):
                    self.actual_pTs_filtered_sent[3*i+1] = -self.actual_pTs_filtered_sent[3*i+1]
                    self.actual_pTs_filtered_sent[3*i+2] = -self.actual_pTs_filtered_sent[3*i+2]
                cmd=self.process_send_cmd(np.concatenate((self.actual_pTs_filtered_sent,[0.0, 0.0, 0.0],np.zeros((12,)), [0.0,0.0,0.0])))

                self.robot.send_command(cmd)
                policy_count += 1
                current_time = time.time()
                # print("proc", "Frequency: ", 1/(current_time - previous_time + 1e-10))
                previous_time = current_time
                delay = 0.6
                _ = time.perf_counter() + delay/1000
                while time.perf_counter() < _:
                    pass

    def press(self, key):
        print("Doing nothing")

    def set_actions_from_policy(self, planner_actions=None):
        import time
        # print("delay: ", time.time()-planner_actions[-1])
        planner_actions = planner_actions[:-1]

        if not self.use_planner:
            raise NotImplementedError
        else:
            if not self.reference_generator.action_enabled:
                self.selected_policy = int(planner_actions[-1])

        if self.selected_policy == 1:
            self.ever_jump = True

        # if self.reference_generator.motion_phase == 2:
        #     if self.ever_jump:
        #         self.selected_policy = 1
        #     else:
        #         self.selected_policy = 2

        planner_actions[-1] = self.selected_policy

        self.reference_generator.set_actions_from_policy(planner_actions, self.timestep)

    def get_robot_states(self, planner_actions=None):
        ob_curr = np.concatenate([self.obs_robot_state.rot_quat, self.obs_robot_state.motor_pos])
        robot_states = np.concatenate([np.array(self.previous_obs)[-5:].flatten(), ob_curr])
        robot_actions = np.concatenate([np.array(self.previous_acs)[-5:].flatten(), self.actual_pTs_filtered])
        reference_params = np.concatenate([self.reference_generator.get_fixed_motion_duration(),
                                    self.reference_generator.get_motion_t_norm(), self.reference_generator.get_motion_phase()])
        return robot_states, robot_actions, reference_params

    def run_policy(self):
        proc = threading.Thread(target=self.pid_ctrl)
        proc.start()

        listen_keyboard(on_press=self.press, until='space')
        proc.do_run = False
        previous_time = time.perf_counter()

        # proc_squat_prep = threading.Thread(target=self.pid_ctrl_squat_prep)
        # proc_squat_prep.start()

        # listen_keyboard(on_press=self.press, until='space')
        # proc_squat_prep.do_run = False
        self.pid_ctrl_squat_prep()

        while(True):
            if not self.policy_running:
                self.policy_running = True
            obs = self.robot.receive_observation()
            self.__process_recv_package(obs)
            if self.est_timestep % 1 == 0:
                # print("self.est_timestep", self.est_timestep)
                # print("self.num_sims_per_env_step", self.num_sims_per_env_step)

                # if self.timestep < 30:
                #     self.reference_generator.set_policy(0, self.timestep)
                #     self.selected_policy = 0
                # else:
                #     self.reference_generator.set_policy(1, self.timestep)
                #     self.selected_policy = 1

                if self.timestep == 0:
                    self.actual_pTs_filtered = np.zeros(12)
                self.__get_observation(np.copy(self.actual_pTs_filtered), step=True)
                self.actual_pTs_filtered, ob, ac= self.__get_action()

                self.low_obs_act.append((np.copy(self.curr_obs), np.copy(self.actual_pTs_filtered)))

                self.actual_pTs_filtered = np.round(self.actual_pTs_filtered,5)

                self.actual_pTs_filtered_sent = np.copy(self.actual_pTs_filtered)

                for i in range(4):
                    # self.actual_pTs_filtered_sent[3*i] = -self.actual_pTs_filtered_sent[3*i]
                    self.actual_pTs_filtered_sent[3*i+1] = -self.actual_pTs_filtered_sent[3*i+1]
                    self.actual_pTs_filtered_sent[3*i+2] = -self.actual_pTs_filtered_sent[3*i+2]

                self.robot.send_command(self.actual_pTs_filtered_sent)

                self.est_timestep = 0
                self.__env_update()

            else:
                # send previous action package
                self.robot.send_command(self.actual_pTs_filtered_sent)
                time.sleep(0.00001)

            current_time = time.time()
            # print("Frequency: ", 1/(current_time - previous_time))
            previous_time = current_time
            self.est_timestep += 1

    def pid_ctrl_restore_stand(self):
        policy_count = 0
        a1_default_target_positions = np.array([0.0,0.9,-1.8, 0.0,0.9,-1.8,
                                         0.0,0.9,-1.8, 0.0,0.9,-1.8])
        obs = self.robot.receive_observation()
        self.__process_recv_package(np.copy(obs))
        landing_joint_pos = np.array(obs[6:18])
        landing_joint_pos[[1,2,4,5,7,8,10,11]] *= -1
        restore_duration = 60
        while policy_count < 60:
            obs = self.robot.receive_observation()
            self.__process_recv_package(np.copy(obs))
            if policy_count > restore_duration:
                self.actual_pTs_filtered_sent = np.copy(a1_default_target_positions)
                for i in range(4):
                    self.actual_pTs_filtered_sent[3*i+1] = -self.actual_pTs_filtered_sent[3*i+1]
                    self.actual_pTs_filtered_sent[3*i+2] = -self.actual_pTs_filtered_sent[3*i+2]
                cmd=self.process_send_cmd(np.concatenate((self.actual_pTs_filtered_sent,[0.0, 0.0, 0.0],np.zeros((12,)), [0.0,0.0,0.0])))

            else:
                self.actual_pTs_filtered_sent = (restore_duration - policy_count) / restore_duration * landing_joint_pos + policy_count / restore_duration * np.array(a1_default_target_positions)
                for i in range(4):
                    self.actual_pTs_filtered_sent[3*i+1] = -self.actual_pTs_filtered_sent[3*i+1]
                    self.actual_pTs_filtered_sent[3*i+2] = -self.actual_pTs_filtered_sent[3*i+2]
                cmd=self.process_send_cmd(np.concatenate((self.actual_pTs_filtered_sent,[0.0, 0.0, 0.0],np.zeros((12,)), [0.0,0.0,0.0])))

            self.robot.send_command(cmd)
            policy_count += 1
            current_time = time.time()
            # print("proc", "Frequency: ", 1/(current_time - previous_time + 1e-10))
            previous_time = current_time
            delay = 0.6
            _ = time.perf_counter() + delay/1000
            while time.perf_counter() < _:
                pass