# -*- coding: utf-8 -*-
import json
import re
import sys

import redis
import scrapy

from iqiyiCrawler.items import IqiyiTVItem

reload(sys)
sys.setdefaultencoding('utf-8')
conn = redis.Redis(host="10.4.255.129", port=6379, db=0)

class IqiyiSpider(scrapy.Spider):
    name = 'iqiyiTV'
    cache_urls = "http://cache.video.iqiyi.com/jp/vi/{tvid}/{vid}/"
    # http://mixer.video.iqiyi.com/jp/mixin/videos/1033851800
    urls = "http://mixer.video.iqiyi.com/jp/recommend/videos?referenceId={referenceId}&albumId={albumId}&channelId=2&cookieId=496db0c22d9a27a875d612af58b6e7c6&withRefer=false&area=bee&size=36&type=video&pru=&locale=&userId=&playPlatform=PC_QIYI&isSeriesDramaRcmd="
    url = "http://www.iqiyi.com/v_19rrco0ypo.html"
    jiming_url = "http://mixer.video.iqiyi.com/jp/mixin/videos/avlist?albumId={albumId}&size={size}&page=1"
    gender_url = "https://uaa.if.iqiyi.com/video_index/v2/get_user_profile?album_id={albumId}"
    tv_url = "http://mixer.video.iqiyi.com/jp/mixin/videos/{albumId}?select=user,credit,focus,star,cast,gender"

    def start_requests(self):
        yield scrapy.Request(url=self.url, callback=self.parse_index)
        # for url in self.start_urls:
        #     yield scrapy.Request(url=url, callback=self.parse)

    def parse(self, response):
        data = response.meta["data"]
        result = response.text.strip("var tvInfoJs=")
        # print(result)
        jj = json.loads(result)
        # print(jj.get("albumQipuId"))
        data["keyword"] = jj.get("an")
        # print(jj.get("qiyiPlayStrategy"))
        data["tg"] = jj.get("tg")
        data["info"] = jj.get("info")
        data["d"] = jj.get("d")
        data["ma"] = jj.get("ma")
        # print(jj.get("nurl"))
        # print(jj.get("videoQipuId"))
        albumid = data.get("albumid")
        # print(self.gender_url.format(albumId=data.get("albumid")))
        yield scrapy.Request(url=self.gender_url.format(albumId=albumid), meta={"data": data},
                             callback=self.parse_gender, dont_filter=True)

    def parse_gender(self, response):
        data = response.meta["data"]
        albumid = data.get("albumid")
        tvid = data.get("tvid")
        result = json.loads(response.text)
        # print(albumid)
        gender = result.get("data").get("details")[0].get(albumid).get("gender")
        age = result.get("data").get("details")[0].get(albumid).get("age")
        data["gender"] = gender
        data["age"] = age
        yield scrapy.Request(url=self.tv_url.format(albumId=tvid), meta={"data": data}, callback=self.parse_score,
                             dont_filter=True, priority=1)

    def parse_score(self, response):
        data = response.meta["data"]
        result = json.loads(response.text.strip("var tvInfoJs="))
        score = result.get("score")
        playCount = result.get("playCount")
        data["score"] = score
        data["playCount"] = playCount
        item = IqiyiTVItem()
        item["url"] = data.get("url")
        item["name"] = data.get("keyword")
        item["directors"] = data.get("d")
        item["Actors"] = data.get("ma")
        item["period"] = data.get("time")
        item["description"] = data.get("info")
        item["pay"] = data.get("vip")
        item["score"] = score
        item["starTotal"] = playCount
        item["tags"] = data.get("tg")
        item["type"] = data.get("genres")
        item["region"] = data.get("region")
        item["positive"] = "正片"
        item["sex"] = data.get("gender")
        item["ages"] = data.get("age")
        item["subtitle"] = data.get("subtitle")
        item["numTotal"] = str(data.get("status"))
        # print(item["numTotal"])
        yield item

    def parse_item(self, response):
        # print(response.meta["data"])
        albumid = response.meta["data"].get("albumid")
        data_page = response.meta["data"].get("page")
        data = response.meta["data"]
        # print(data)
        yield scrapy.Request(
            url=self.jiming_url.format(albumId=albumid, size=str(data_page * 30)),
            meta={"data": data}, callback=self.parse_num, dont_filter=True, priority=1)
        # print(response.text)
        result = response.text.strip("var tvInfoJs=")
        jj = json.loads(result)
        for tt in jj.get("mixinVideos"):
            url = tt.get("url")
            # print(tt.get("url"))
            # print(tt.get("description"))
            # print(tt.get("tvId"))
            # print(tt.get("albumId"))
            # print(tt.get("crumbList")[1].get("title"))
            # print(tt.get("crumbList")[2].get("title"))
            if conn.sadd("Iqiyi:tv", url) == 1:
                yield scrapy.Request(url=url, callback=self.parse_index, dont_filter=True)

    def parse_index(self, response):
        sel = scrapy.Selector(response)
        result = response.text
        # print(result)
        albumid = re.findall("param\['albumid']\s=\s(.*?);", result)
        tvid = re.findall("param\['tvid']\s=\s(.*?);", result)
        vid = re.findall("param\['vid']\s=\s(.*?);", result)
        status = sel.xpath('//div[@class="playList-update-tip "]/p/text()').extract_first(default="")
        data_page = sel.xpath('//div[@data-tab-page="body"]').extract()
        site = sel.xpath('//div[@class="mod-juji-album-vip"]').extract()
        # print(site)
        vip = "免费"
        if len(site) == 0:
            vip = "收费"
        region = re.findall('rseat="707181_region">(.*?)', result)
        genres = re.findall('rseat="707181_genres">(.*?)', result)
        time = re.findall('rseat="707181_time"\starget="_blank">(.*?)', result)
        if len(region) == 0:
            region = ["None"]
        if len(genres) == 0:
            genres = ["None"]
        if len(time) == 0:
            time = ["None"]
        if len(albumid) == 0:
            yield scrapy.Request(url=response.url, callback=self.parse_index, dont_filter=True)
        else:
            # print(region[0])  # 地区
            # print(genres[0])  # 种类
            # print(time[0])  # 年代
            # print(len(data_page))  # 页数
            data = {"url": response.url, "status": status, "vip": str(vip), "vid": vid[0].strip("\""),
                    "tvid": tvid[0].strip("\""),
                    "albumid": albumid[0].strip("\""),
                    "region": region[0].strip("\""),
                    "genres": genres[0].strip("\""),
                    "time": time[0].strip("\""),
                    "page": len(data_page)}
            # yield scrapy.Request(
            #     url=self.jiming_url.format(albumId=str(albumid[0]).strip("\""), size=str(len(data_page) * 30)),
            #     callback=self.parse_num)
            yield scrapy.Request(
                url=self.urls.format(referenceId=str(tvid[0]).strip("\""), albumId=str(albumid[0]).strip("\"")),
                meta={"data": data}, callback=self.parse_item, dont_filter=True)

    def parse_num(self, response):
        result = response.text.strip("var tvInfoJs=")
        # print(result)
        jj = json.loads(result)
        data = response.meta["data"]
        subtitle = ""
        for i in range(len(jj.get("mixinVideos"))):
            subtitle = subtitle + "第" + str(i + 1) + "集:" + jj.get("mixinVideos")[i].get("subtitle") + ";"
        data["subtitle"] = subtitle
        tvid = response.meta["data"].get("tvid")
        vid = response.meta["data"].get("vid")
        # print(self.cache_urls.format(tvid=tvid, vid=vid))
        yield scrapy.Request(url=self.cache_urls.format(tvid=tvid, vid=vid), meta={"data": data},
                             callback=self.parse, dont_filter=True)


# matplotlit : ploting library. 그래프(차트) 생성을 위한 다양한 함수 지원
import numpy as np
import matplotlib.pyplot as plt

# 폰트 설정으로 한글깨짐 방지
plt.rc('font', family = 'malgun gothic')
# 한글깨짐 방지이후 음수(-) 깨짐방지    <- 위에 한글깨짐 방지를 사용해서 생김
plt.rcParams['axes.unicode_minus'] = False

'''
x = ["서울","인천","수원"]
y = [5, 3, 7]
plt.xlabel("지역") # x축의 명칭
plt.xlim([-1, 3])   # 축의 값에 대한 제한을 걸어줌
plt.yticks(list(range(0, 11, 3)))   # y축에대한 보여지는 좌표값 설정
plt.ylim([0, 10])
plt.plot(x, y)
plt.show()
'''

#----------------------------
'''
data = np.arange(1, 11, 2)
print(data) #[1 3 5 7 9]
plt.plot(data)  # y축이 값이됨
x = [0,1,2,3,4]
for a,b in zip(x, data):
    plt.text(a, b, str(b))
plt.show()

plt.plot(data)
plt.plot(data, data, 'r')
plt.show()
'''

'''
x = np.arange(10)
y = np.sin(x)
print(x, y)
#plt.plot(x, y, 'bo')    # style 옵션 적용
#plt.plot(x, y, 'r+')
plt.plot(x, y, 'go--', linewidth = 2, markersize= 10)
plt.show()
'''

'''
# 홀드 : 그림 겹쳐 보기
x = np.arange(0, np.pi * 3, 0.1)
#print(x)
y_sin = np.sin(x)
y_cos = np.cos(x)
plt.plot(x, y_sin, 'r')
plt.scatter(x, y_cos)
plt.xlabel('x축')
plt.ylabel('y축')
plt.legend(['사인','코사인'])
plt.title('차트제목')
plt.show()
'''

# subplot : Figure를 여러 행열로 분리
x = np.arange(0, np.pi * 3, 0.1)
y_sin = np.sin(x)
y_cos = np.cos(x)
plt.subplot(2, 1, 1)
plt.plot(x, y_sin)
plt.title('사인')

plt.subplot(2, 1, 2)
plt.plot(x, y_cos)
plt.title('사인')

plt.show()

irum = ['a','b','c','d','e']
kor = [80, 50, 70, 70, 90]
eng = [60, 70, 80, 70, 60]
plt.plot(irum, kor, 'ro-')
plt.plot(irum, eng, 'gs-')
plt.ylim(0,100)
plt.legend(['국어', '영어'], loc = 4)
plt.grid(True)

fig=plt.gcf()   # 이미지 저장 준비
plt.show()
fig.savefig('test.png') # 실제로 사진을 저장

from matplotlib.pyplot import imread
img = imread('test.png')
plt.imshow(img)
plt.show()


def checkStr(input1, input2):
	i=0
	for sublist in input1:
		j=0
		for element in sublist:
			value=checkAround(input1,input2,i,j,-1,-1,0)
			j=j+1
			if(value==1):
				return True
		i=i+1
	return False

def checkAround(arr,search,x,y,prevX,prevY,pos):
	for i in range(-1,1):
		for j in range(-1,1):
			newX=x+i
			newY=y+j
			if(not (i == 0 and j == 0) and(newX >= 0 and newY >= 0 and newX < len(arr) and newY < len(arr[newX]) )and not(newX == prevX and newY == prevY)):
				if(arr[newX][newY]==search[pos]):
					if((pos==len(search)-1)or (checkAround(arr,search,newX,newY,x,y,pos+1)==1)):
						return 1

	return 0

if __name__ == '__main__':
  input1=[['f','g','t'],['q','w','e'],['a','s','q']]
  input2='fqwsq'
  x=checkStr(input1,input2)
  print("fqwsq returns "+str(x))


from Medius.lib.Fond import Fond
import pandas as pd

class Insights(object):

    _intervalles_maturite = [
                    {'min': 1, 'max': 365}, {'min': 365, 'max': 365*2},
                    {'min': 365*2, 'max': 365*3}, {'min': 365*3, 'max': 365*5},
                    {'min': 365*5, 'max': 365*7}, {'min': 365*7, 'max': 365*10},
                    {'min': 365*10, 'max': 365*15}, {'min': 365*15, 'max': 365*20},
                    {'min': 365*20, 'max': 365*25}, {'min': 365*25, 'max': 365*30}]

    def __init__(self, code_fond, date_valeur):
        self.code_fond = code_fond
        self.date_valeur = date_valeur
        self.fond = Fond(self.code_fond, self.date_valeur)

    def concentration_emetteurs(self):
        inventaire = self.fond.inventaire.items('obligation')['obligation']
        etat = self.fond.etat()
        actif_net = etat['actif_net']
        df = pd.DataFrame(inventaire, columns=inventaire[0].keys())
        df['valo'] = df['prix'] * df['quantite']
        dfg = df.groupby(['emetteur', 'garantie'])
        res = dfg['valo'].sum() / actif_net
        res.sort(ascending=False)
        return pd.DataFrame(res)

    def titres_par_maturite(self):
        inventaire = self.fond.inventaire.items('obligation')['obligation']
        cols = ['prix', 'quantite', 'maturite_residuelle']
        df = pd.DataFrame(inventaire, columns=cols)
        df['valo'] = df['prix'] * df['quantite']
        total_valo = df['valo'].sum()
        dico = {}
        for el in self._intervalles_maturite:
            records = df[
                (df['maturite_residuelle'] >= el['min']) & (df['maturite_residuelle'] < el['max'])]
            key = '-'.join([str(el['min']/365), str(el['max']/365)])
            dico.update({key: records['valo'].sum()/total_valo})
        return dico

    def spread_moyen_fond(self):
        inventaire = self.fond.inventaire.items('obligation')['obligation']
        df = pd.DataFrame(inventaire, columns=['spread'])
        return df['spread'].mean()

    def spread_moyen_dette_privee(self):
        inventaire = self.fond.inventaire.items('obligation')['obligation']
        df = pd.DataFrame(inventaire, columns=['spread'])
        moy_dette_privee = df[df['spread'] > 0]['spread'].mean()
        return moy_dette_privee

    def poids_par_categorie(self):
        etat = self.fond.etat()
        actif_net = etat['actif_net']
        cols = ['categorie', 'classe', 'prix', 'quantite']
        inventaire = self.fond.inventaire.items('obligation')['obligation']
        df = pd.DataFrame(inventaire, columns=cols)
        df['valo'] = df['prix'] * df['quantite']
        df['poids'] = df['valo'] / actif_net
        dfg = df.groupby('classe')
        res = dfg['poids'].sum()
        return res

    def description_par_categorie(self):
        pass

if __name__ == '__main__':
    i = Insights('CMROBLIG', '28/08/2015')
    ce = i.concentration_emetteurs()
    sm = i.spread_moyen_fond()
    poids = i.poids_par_categorie()
    print(ce)
    print(poids)


from elasticsearch import Elasticsearch
import json

INDEX = "runbook_index"
DOC_TYPE = "r3d3_type"
LOG_SNIPPET = "java.lang.RuntimeException: Cannot reserve additional contiguous bytes in the vectorized reader"
es = Elasticsearch([{'host': 'localhost', 'port': 9200}])

# GET document id
# r = es.get(index=INDEX, doc_type=DOC_TYPE, id=1)
# print(r)
res = es.search(index=INDEX, body={"query": {"match": {"Log snippet": LOG_SNIPPET}}})
print(json.dumps(res, indent=2, sort_keys=True))
# print(res['hits']['total'])
# total_results = res['hits']['total']['value']
# print("Total results found: %d" % total_results)
# if total_results > 0:
#     print("search results:")
#     # Print all log snippets and action to do
#     items = res['hits']['hits']
#     for item in items:
#         source = item['_source']
#         print("full log snippet:")
#         print(source['Log snippet'])
#         print("Action TO DO:")
#         print(source['Action To Do'])
#         print("-".center(30, '-'))
# else:
#     print("No results found")


# -*- coding: utf-8 -*-
"""
Created on Mon Apr 16 14:20:01 2018

@author: Proprietario
http://scikit-image.org/docs/dev/auto_examples/segmentation/plot_segmentations.html#sphx-glr-auto-examples-segmentation-plot-segmentations-py
"""

#from __future__ import print_function

import matplotlib.pyplot as plt
import numpy as np

from skimage.data import astronaut
from skimage.color import rgb2gray
from skimage.filters import sobel
from skimage.segmentation import felzenszwalb, slic, quickshift, watershed
from skimage.segmentation import mark_boundaries
from skimage.util import img_as_float
import cv2

class segmScikit():

    def __init__(self, imgpath):
        self.img = cv2.imread(imgpath)

    def makeSegmentation(self):

        segments_fz = felzenszwalb(self.img, scale=1000, sigma=0.5, min_size=250)
        #segments_slic = slic(self.img, n_segments=4, compactness=10, sigma=1)
        segments_quick = quickshift(self.img, kernel_size=3, max_dist=100, ratio=0.5)
        gradient = sobel(rgb2gray(self.img))
        segments_watershed = watershed(gradient, markers=10, compactness=0.001)

        print("Felzenszwalb number of segments: {}".format(len(np.unique(segments_fz))))
        #print('SLIC number of segments: {}'.format(len(np.unique(segments_slic))))
        print('Quickshift number of segments: {}'.format(len(np.unique(segments_quick))))

        fig, ax = plt.subplots(2, 2, figsize=(10, 10), sharex=True, sharey=True)

        ax[0, 0].imshow(mark_boundaries(self.img, segments_fz))
        ax[0, 0].set_title("Felzenszwalbs's method")
        #ax[0, 1].imshow(mark_boundaries(self.img, segments_slic))
        ax[0, 1].set_title('SLIC')
        ax[1, 0].imshow(mark_boundaries(self.img, segments_quick))
        ax[1, 0].set_title('Quickshift')
        ax[1, 1].imshow(mark_boundaries(self.img, segments_watershed))
        ax[1, 1].set_title('Compact watershed')

        for a in ax.ravel():
            a.set_axis_off()

        plt.tight_layout()
        plt.show()

path= "D:/aaa/a.jpg"
a = segmScikit(path)
a.makeSegmentation()


import os
import sys

this_dir = os.path.abspath(os.path.dirname(__file__))
sys.path.append(os.path.join(this_dir, 'TestUtils'))
sys.path.append(os.path.join(this_dir, 'PageObjects'))

from BaseTestCase import BaseTestCase
from LoadVariablePopUp import LoadVariablePopUp
from Canvas import Canvas

import unittest

class TestAdjustAxes(BaseTestCase):

    def test_adjust_axes_1(self):
        '''
        load 'clt.nc', load 'u' variable, adjust slider, load, and plot.
        '''
        print("\n\n...test_adjust_axes_1...")

        left_side_bar = self.load_data_file("clt.nc")

        load_variable_pop_up = LoadVariablePopUp(self.driver)
        load_variable_pop_up.click_on_var('u')
        load_variable_pop_up.click_on_var_axes('u')

        # adjust the min slider by 20 percent and max slider by 20 percent.
        load_variable_pop_up.adjust_var_axes_slider('u', 'latitude1', 20, -20)
        load_variable_pop_up.load()
        left_side_bar.click_on_plot()
        canvas = Canvas(self.driver)
        canvas.check_plot()

    def test_adjust_axes_2(self):
        '''
        load 'clt.nc', load 'u' variable, load 'v' variable, adjust sliders, load, and plot.
        '''
        print("\n\n...test_adjust_axes_2...")

        left_side_bar = self.load_data_file("clt.nc")

        load_variable_pop_up = LoadVariablePopUp(self.driver)
        load_variable_pop_up.click_on_var('u')
        load_variable_pop_up.click_on_var_axes('u')
        # adjust the min slider by 20 percent and max slider by 20 percent.
        load_variable_pop_up.adjust_var_axes_slider('u', 'longitude1', 20, -20)

        load_variable_pop_up.click_on_var('v')
        load_variable_pop_up.click_on_var_axes('v')
        # adjust the min slider by 20 percent and max slider by 20 percent.
        load_variable_pop_up.adjust_var_axes_slider('v', 'longitude2', 20, -20)

        load_variable_pop_up.load()
        left_side_bar.select_plot_type("streamline (default)")

        left_side_bar.click_on_plot()
        canvas = Canvas(self.driver)
        canvas.check_plot()

if __name__ == '__main__':
    unittest.main(verbosity=2)

# nosetests -s tests/test_adjust_axes.py:TestAdjustAxes.test_adjust_axes_2


import torch

import time

from lib.utils import AvgrageMeter, accuracy
from lib.config import cfg

def validate(model, test_loader, criterion, epoch, logger, attention_logger, writer):
    if cfg.ddp.local_rank == 0:
        print('evaluation ...')
    model.eval()

    top1 = AvgrageMeter()
    top5 = AvgrageMeter()
    losses = AvgrageMeter()

    step = 0
    with torch.no_grad():
        sta_time = time.time()
        for i, (data, target) in enumerate(test_loader):
            if cfg.test and cfg.disp_attention and cfg.ddp.local_rank == 0:
                attention_logger.info(f"test iter {i}")

            N = data.size(0)
            if cfg.cuda:
                data, target = data.cuda(), target.cuda()
            output = model(data)

            loss = criterion(output, target)
            prec1, prec5 = accuracy(output, target, topk=(1, 5))

            losses.update(loss.item(), N)
            top1.update(prec1.item(), N)
            top5.update(prec5.item(), N)

            step += 1
            if step % cfg.train.disp == 0 and cfg.ddp.local_rank == 0:
                logger.info("Test: Epoch {}/{}  Time: {:.3f} Loss {losses.avg:.3f} "
                            "Prec@(1,5) ({top1.avg:.1%}, {top5.avg:.1%})".format(
                    epoch, cfg.train.epoch, (time.time() - sta_time) / cfg.train.disp,
                    losses=losses, top1=top1, top5=top5))

                writer.add_scalar('Loss/vaild', losses.avg, epoch * len(test_loader) + i)
                writer.add_scalar('Accuracy/vaild', top1.avg, epoch * len(test_loader) + i)

    return top1.avg


# coding: utf-8

import os, glob, time
import ipywidgets as ipyw
from IPython.display import display, HTML, clear_output
# This try-except should not be necessary anymore.
# The testing is now done in ../tests.
try:
    from ._utils import js_alert
except Exception:
    # only used if testing in this directory without installing
    from _utils import js_alert

class FileSelectorPanel:

    """Files and directories selector
    """

    #If ipywidgets version 5.3 or higher is used, the "width="
    #statement should change the width of the file selector. "width="
    #doesn't appear to work in earlier versions.
    select_layout = ipyw.Layout(width="750px")
    select_multiple_layout = ipyw.Layout(width="750px", display="flex", flex_flow="column")
    button_layout = ipyw.Layout(margin="5px 40px")
    label_layout = ipyw.Layout(width="250px")
    layout = ipyw.Layout()

    def __init__(self, instruction, start_dir=".", type='file', next=None, multiple=False):
        """
        Create FileSelectorPanel instance

        Parameters
        ----------
        instruction : str
            instruction to users for file/dir selection
        start_dir : str
            starting directory path
        type : str
            type of selection. "file" or "directory"
        multiple: bool
            if True, multiple files/dirs can be selected
        next : function
            callback function to execute after the selection is selected
        """
        if not type in ['file', 'directory']:
            raise ValueError("type must be either file or directory")
        self.instruction = instruction
        self.type = type
        self.multiple = multiple
        self.createPanel(os.path.abspath(start_dir))
        self.next = next
        return

    def createPanel(self, curdir):
        self.curdir = curdir
        explanation = ipyw.Label(self.instruction,layout=self.label_layout)
        entries_files = sorted(os.listdir(curdir))
        entries_paths = [os.path.join(curdir, e) for e in entries_files]
        entries_ftime = create_file_times(entries_paths)
        entries = create_nametime_labels(entries_files, entries_ftime)
        self._entries = entries = [' .', ' ..', ] + entries
        if self.multiple:
            value = []
            self.select = ipyw.SelectMultiple(
                value=value, options=entries,
                description="Select",
                layout=self.select_multiple_layout)
        else:
            value = entries[0]
            self.select = ipyw.Select(
                value=value, options=entries,
                description="Select",
                layout=self.select_layout)
        """When ipywidgets 7.0 is released, the old way that the select or select multiple
           widget was set up (see below) should work so long as self.select_layout is changed
           to include the display="flex" and flex_flow="column" statements. In ipywidgets 6.0,
           this doesn't work because the styles of the select and select multiple widgets are
           not the same.

        self.select = widget(
            value=value, options=entries,
            description="Select",
            layout=self.select_layout) """
        # enter directory button
        self.enterdir = ipyw.Button(description='Enter directory', layout=self.button_layout)
        self.enterdir.on_click(self.handle_enterdir)
        # select button
        self.ok = ipyw.Button(description='Select', layout=self.button_layout)
        self.ok.on_click(self.validate)
        buttons = ipyw.HBox(children=[self.enterdir, self.ok])
        self.widgets = [explanation, self.select, buttons]
        self.panel = ipyw.VBox(children=self.widgets, layout=self.layout)
        return

    def handle_enterdir(self, s):
        v = self.select.value
        v = del_ftime(v)
        if self.multiple:
            if len(v)!=1:
                js_alert("Please select a directory")
                return
            v = v[0]
        p = os.path.abspath(os.path.join(self.curdir, v))
        if os.path.isdir(p):
            self.remove()
            self.createPanel(p)
            self.show()
        return

    def validate(self, s):
        v = self.select.value
        v = del_ftime(v)
        # build paths
        if self.multiple:
            vs = v
            paths = [os.path.join(self.curdir, v) for v in vs]
        else:
            path = os.path.join(self.curdir, v)
            paths = [path]
        # check type
        if self.type == 'file':
            for p in paths:
                if not os.path.isfile(p):
                    js_alert("Please select file(s)")
                    return
        else:
            assert self.type == 'directory'
            for p in paths:
                if not os.path.isdir(p):
                    js_alert("Please select directory(s)")
                    return
        # set output
        if self.multiple:
            self.selected = paths
        else:
            self.selected = paths[0]
        # clean up
        self.remove()
        # next step
        if self.next:
            self.next(self.selected)
        return

    def show(self):
        display(HTML("""
        <style type="text/css">
        .jupyter-widgets select option {
            font-family: "Lucida Console", Monaco, monospace;
        }
        </style>
        """))
        display(self.panel)

    def result(self):
        return ['yo','ya','dfdf']

    def remove(self):
        for w in self.widgets: w.close()
        self.panel.close()

def create_file_times(paths):
    "returns a list of file modify time"
    ftimes = []
    for f in paths:
        if os.path.isdir(f):
            ftimes.append("Directory")
        else:
            ftime_sec = os.path.getmtime(f)
            ftime_tuple = time.localtime(ftime_sec)
            ftime = time.asctime(ftime_tuple)
            ftimes.append(ftime)
    return ftimes

def create_nametime_labels(entries, ftimes):
    if not entries: return []
    max_len = max(len(e) for e in entries)
    n_spaces = 5
    fmt_str = ' %-' + str(max_len+n_spaces) + "s|" + ' '*n_spaces + '%s'
    label_list = [fmt_str % (e, f) for e, f in zip(entries, ftimes)]
    return label_list

def del_ftime(file_label):
    "file_label is either a str or a tuple of strings"
    if isinstance(file_label, tuple):
        return tuple(del_ftime(s) for s in file_label)
    else:
        file_label_new = file_label.strip()
        if file_label_new != "." and file_label_new != "..":
            file_label_new = file_label_new.split("|")[0].rstrip()
    return(file_label_new)

def test1():
    panel = FileSelectorPanel("instruction", start_dir=".")
    print('\n'.join(panel._entries))
    panel.handle_enterdir(".")
    return

def test2():
    s = " __init__.py          |     Tue Jun 13 23:24:05 2017"
    assert del_ftime(s) == '__init__.py'
    s = ' . '
    assert del_ftime(s) == '.'
    s = (" __init__.py          |     Tue Jan 13 23:24:05 2017",
         " _utils.py            |     Mon Feb 11 12:00:00 2017")
    dels = del_ftime(s)
    expected = ("__init__.py", "_utils.py")
    for e, r in zip(dels, expected):
        assert e == r
    return

def main():
    #test1()
    test2()
    return

if __name__ == '__main__': main()


import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

import tensorflow as tf
from tensorflow import keras
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import numpy as np
import pickle

import rbn_cov_datagen as dg

def train_model(N = 16, K = 4, L = 16, freq = 2.4e9, training_size = 500000, validation_size = 0.1, learning_rate = 0.001, snr = [5, 30], sort = False):
    training_labels, training_data = dg.data_initialization(training_size, N, K, L, freq, snr)

    dg.normalize_add_wgn(training_labels, training_data, snr)

    training_data, validation_data, training_labels, validation_labels = train_test_split(training_data, training_labels, test_size=validation_size, shuffle=True)

    # define model
    model = keras.Sequential([
            keras.layers.Dense(128, 'relu'),
            keras.layers.Dense(128, 'relu'),
            keras.layers.Dense(K, activation='sigmoid')
            ])

    adaptive_learning_rate = lambda epoch: learning_rate/(2**np.floor(epoch/10))

    adam = keras.optimizers.Adam(learning_rate=learning_rate, beta_1=0.9, beta_2=0.999)
    sgd = keras.optimizers.SGD(learning_rate=learning_rate)

    lrate = tf.keras.callbacks.LearningRateScheduler(adaptive_learning_rate)
    stopping = tf.keras.callbacks.EarlyStopping(monitor='val_loss', patience=5, min_delta=1e-5)

    model.compile(optimizer=sgd,
                  loss='mse')

    m = model.fit(training_data, training_labels, batch_size=32, epochs=300, validation_data=(validation_data, validation_labels), callbacks=[stopping, lrate])

    with open(f"history/RBN_cov_N={N}_K={K}_L={L}", 'wb') as f:
        pickle.dump(m.history, f)

    model.save(f"models/RBN_cov_N={N}_K={K}_L={L}")

    return model


#!/usr/local/bin/python3
# -*- coding: utf-8 -*-

class Solution(object):
    def reachNumber(self, target):
        target = abs(target)
        k = 0
        sum = 0
        while(sum < target):
            k += 1
            sum += k
        if ((target - sum) % 2 == 0):
            return k
        if (k % 2):
            return k + 2
        return k+1

s = Solution()
print(s.reachNumber(12))


from glob import glob
from random import randint

import cv2 as cv
import numpy as np

def get_real_and_img_pts(
    pattern_size: tuple,
    img_name_pattern: str,
    real_square_size: float = 1.0,
    show_progress: bool = False
) -> tuple:
    """Get 3D points in real world space, 2D points in image place and the shape of the used images.
    Adaptation from: https://docs.opencv.org/master/dc/dbb/tutorial_py_calibration.html

    Args:
    - `pattern_size:tuple`: Chess pattern grid to be considerer as `(width, height)`
    - `img_name_pattern:str`: Image name pattern that contains a chessboard
    - `real_square_size:float`: Real measure from the squares into the chessboard
    - `show_progress:bool`: Control the image exhibition as they been process

    Returns:
    - (`real_pts`, `img_pts`, `img_gray.shape`, `img_names`)
    """
    # termination criteria
    criteria = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER, 30, .0001)

    # prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
    real_pt = np.zeros((pattern_size[0] * pattern_size[1], 3), np.float32)
    real_pt[:, :2] = np.mgrid[:pattern_size[1], :pattern_size[0]].T.reshape(-1, 2)
    # real square measure
    real_pt *= real_square_size

    # 3D points and 2D points
    real_pts, img_pts = [], []
    img_gray = np.zeros((1, 1))

    img_names = glob(img_name_pattern)
    for img_name in img_names:
        img = cv.imread(img_name)
        img_gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)

        # Find the chess board corners
        status_ok, corners = cv.findChessboardCorners(
            img_gray, pattern_size,
            flags=cv.CALIB_CB_NORMALIZE_IMAGE + cv.CALIB_CB_ADAPTIVE_THRESH
        )

        if not status_ok:
            continue

        real_pts.append(real_pt)
        img_pts.append(corners)

        if show_progress:
            corners2 = cv.cornerSubPix(img_gray, corners, (11,11), (-1,-1), criteria)
            cv.drawChessboardCorners(img, pattern_size, corners2, True)
            cv.imshow('img', img)
            cv.waitKey(500)

    cv.destroyAllWindows()

    return real_pts, img_pts, img_gray.shape, img_names

def get_camera_matrices(real_pts: list, img_pts: list, img_shape: tuple) -> dict:
    """Get general camera matrices: intrinsic params and distortion coefficients; and
    specific camera matrices: rotation, translation and projection.

    Source: https://docs.opencv.org/master/dc/dbb/tutorial_py_calibration.html

    Args:
    - `real_pts:list`: 3D points in real world space
    - `img_pts:list`: 2D points in image place
    - `img_shape:tuple`: Used images' shape

    Returns:
    - Matrices in dict
    """
    _, intrinsics, distortion, rot_vecs, trans_vecs = cv.calibrateCamera(
        real_pts, img_pts, img_shape[::-1], None, None
    )

    pos = randint(0, len(rot_vecs) - 1)
    rotation, _ = cv.Rodrigues(rot_vecs[pos])
    translation = trans_vecs[pos]

    extrinsic = cv.hconcat((rotation, translation))
    projection = np.matmul(intrinsics, extrinsic)

    return {
        'intrinsics': intrinsics,
        'distortion': distortion,
        'specific': {
            'sorted': pos,
            'rotation': rotation,
            'translation': translation,
            'extrinsic': extrinsic,
            'projection': projection
        }
    }


#!/usr/bin/env python -W ignore::DeprecationWarning
"""
Grasping Policy for EE106B grasp planning lab
Author: Chris Correa
"""
import numpy as np

# Autolab imports
from autolab_core import RigidTransform
import trimesh
from visualization import Visualizer3D as vis3d
import random
import matplotlib.pyplot as plt

# 106B lab imports
from lab2.metrics import (
    compute_force_closure,
    compute_gravity_resistance,
    compute_custom_metric,
    robust_force_closure
)
from lab2.utils import length, normalize, rotation_3d

MAX_HAND_DISTANCE = 0.085
MIN_HAND_DISTANCE = 0.045
CONTACT_MU = 0.5
CONTACT_GAMMA = 0.1
finger_length = 0.1

# TODO
OBJECT_MASS = {'gearbox': .25, 'nozzle': .25, 'pawn': .25}

class GraspingPolicy():
    def __init__(self, n_vert, n_grasps, n_execute, n_facets, metric_name):
        """
        Parameters
        ----------
        n_vert : int
            We are sampling vertices on the surface of the object, and will use pairs of
            these vertices as grasp candidates
        n_grasps : int
            how many grasps to sample.  Each grasp is a pair of vertices
        n_execute : int
            how many grasps to return in policy.action()
        n_facets : int
            how many facets should be used to approximate the friction cone between the
            finger and the object
        metric_name : string
            name of one of the function in src/lab2/metrics/metrics.py
        """
        self.n_vert = n_vert
        self.n_grasps = n_grasps
        self.n_facets = n_facets
        self.n_execute = n_execute
        # This is a function, one of the functions in src/lab2/metrics/metrics.py
        self.metric = eval(metric_name)
        self.metric_name = metric_name

    def vertices_to_baxter_hand_pose(self, grasp_vertices, approach_direction, obj_name):
        """
        takes the contacts positions in the object frame and returns the hand pose T_obj_gripper
        BE CAREFUL ABOUT THE FROM FRAME AND TO FRAME.  the RigidTransform class' frames are
        weird.

        Parameters
        ----------
        grasp_vertices : 2x3 :obj:`numpy.ndarray`
            position of the fingers in object frame
        approach_direction : 3x' :obj:`numpy.ndarray`
            there are multiple grasps that go through contact1 and contact2.  This describes which
            orientation the hand should be in

        Returns
        -------
        :obj:`autolab_core:RigidTransform` Hand pose in the object frame
        """
        # parameters required to create a autolab_core:RigidTransform:
        # - rotation (aka 3x3 rotation matrix)
        # - translation (aka 3x1 vector)
        # - from_frame (aka str)
        # - to_frame (aka str)

        midpoint = (grasp_vertices[0] + grasp_vertices[1]) / 2

        gripper_half_width = MAX_HAND_DISTANCE / 2

        z = normalize(approach_direction)
        y = normalize(grasp_vertices[0] - grasp_vertices[1])
        x = np.cross(y, z)

        rot_mat_opposite = np.array([x, y, z]).T
        p_opposite = midpoint

        rot_mat = rot_mat_opposite.T
        p = - np.matmul(rot_mat_opposite.T, p_opposite)

        rigid_trans = RigidTransform(rot_mat_opposite, p_opposite, to_frame='right_gripper', from_frame=obj_name)

        return rigid_trans

    def sample_grasps(self, vertices, normals):
        """
        Samples a bunch of candidate grasps.  You should randomly choose pairs of vertices and throw out
        pairs which are too big for the gripper, or too close too the table.  You should throw out vertices
        which are lower than ~3cm of the table.  You may want to change this.  Returns the pairs of
        grasp vertices and grasp normals (the normals at the grasp vertices)

        Parameters
        ----------
        vertices : nx3 :obj:`numpy.ndarray`
            mesh vertices
        normals : nx3 :obj:`numpy.ndarray`
            mesh normals
        T_ar_object : :obj:`autolab_core.RigidTransform`
            transform from the AR tag on the paper to the object

        Returns
        -------
        n_graspsx2x3 :obj:`numpy.ndarray`
            grasps vertices.  Each grasp containts two contact points.  Each contact point
            is a 3 dimensional vector and there are n_grasps of them, hence the shape n_graspsx2x3
        n_graspsx2x3 :obj:`numpy.ndarray`
            grasps normals.  Each grasp containts two contact points.  Each vertex normal
            is a 3 dimensional vector, and there are n_grasps of them, hence the shape n_graspsx2x3
        """
        grasp_vertices = []
        grasp_normals = []
        nbr_grasps_found = 0
        z_table = min(vertices[:, 2]) #not exactly true but close
        #print('z_table', z_table)

        for i in range(self.n_grasps):
            hasFoundValidGrasp = False
            while not hasFoundValidGrasp:
                # pick two random points
                idx1 = random.randint(0, len(vertices)-1)
                idx2 = random.randint(0, len(vertices)-1)

                if idx1 == idx2:
                    continue

                # now we compute the distance
                distance = np.linalg.norm(vertices[idx1] - vertices[idx2])
                if distance > MAX_HAND_DISTANCE or distance < MIN_HAND_DISTANCE:
                    continue
                # checking if too close to ground
                # if vertices[idx1][2] < 0.0 or vertices[idx2][2] < 0.0: #has to be changed when we apply the transform to the mesh
                if vertices[idx1][2] < z_table + 0.03 or vertices[idx2][2] < z_table + 0.03:
                    continue

                # at this point it means we have a valid pair of points
                hasFoundValidGrasp = True
            curr_grasp_vertices = [vertices[idx1], vertices[idx2]]
            curr_grasp_normals = [normals[idx1], normals[idx2]]
            grasp_vertices.append(curr_grasp_vertices)
            grasp_normals.append(curr_grasp_normals)

        grasp_vertices = np.array(grasp_vertices)
        grasp_normals = np.array(grasp_normals)

        return grasp_vertices, grasp_normals

    def score_grasps(self, grasp_vertices, grasp_normals, object_mass):
        """
        takes mesh and returns pairs of contacts and the quality of grasp between the contacts, sorted by quality

        Parameters
        ----------
        grasp_vertices : n_graspsx2x3 :obj:`numpy.ndarray`
            grasps.  Each grasp containts two contact points.  Each contact point
            is a 3 dimensional vector, and there are n_grasps of them, hence the shape n_graspsx2x3
        grasp_normals : mx2x3 :obj:`numpy.ndarray`
            grasps normals.  Each grasp containts two contact points.  Each vertex normal
            is a 3 dimensional vector, and there are n_grasps of them, hence the shape n_graspsx2x3

        Returns
        -------
        :obj:`list` of int
            grasp quality for each
        """
        grasp_qualities = []

        if self.metric_name == 'compute_force_closure':
            for i in range(grasp_vertices.shape[0]):
                grasp_qualities.append(compute_force_closure(grasp_vertices[i], grasp_normals[i], self.n_facets, CONTACT_MU, CONTACT_GAMMA, object_mass, MIN_HAND_DISTANCE, MAX_HAND_DISTANCE))
        elif self.metric_name == 'compute_gravity_resistance':
            for i in range(grasp_vertices.shape[0]):
                grasp_qualities.append(compute_gravity_resistance(grasp_vertices[i], grasp_normals[i], self.n_facets, CONTACT_MU, CONTACT_GAMMA, object_mass))
        else:
            for i in range(grasp_vertices.shape[0]):
                if i % 10 == 0:
                    print('testing vertex {} for robust force closure'.format(i))
                grasp_qualities.append(compute_custom_metric(grasp_vertices[i], grasp_normals[i], self.n_facets, CONTACT_MU, CONTACT_GAMMA, object_mass, MIN_HAND_DISTANCE, MAX_HAND_DISTANCE))

        return grasp_qualities

    def vis(self, mesh, grasp_vertices, grasp_qualities, grasp_normals):
        """
        Pass in any grasp and its associated grasp quality.  this function will plot
        each grasp on the object and plot the grasps as a bar between the points, with
        colored dots on the line endpoints representing the grasp quality associated
        with each grasp

        Parameters
        ----------
        mesh : :obj:`Trimesh`
        grasp_vertices : mx2x3 :obj:`numpy.ndarray`
            m grasps.  Each grasp containts two contact points.  Each contact point
            is a 3 dimensional vector, hence the shape mx2x3
        grasp_qualities : mx' :obj:`numpy.ndarray`
            vector of grasp qualities for each grasp
        """
        vis3d.mesh(mesh)

        middle_of_part = np.mean(np.mean(grasp_vertices, axis=1), axis=0)
        print(middle_of_part)
        vis3d.points(middle_of_part, scale=0.003)

        dirs = normalize(grasp_vertices[:,0] - grasp_vertices[:,1], axis=1)

        midpoints = (grasp_vertices[:,0] + grasp_vertices[:,1]) / 2
        grasp_endpoints = np.zeros(grasp_vertices.shape)
        grasp_endpoints[:,0] = midpoints + dirs*MAX_HAND_DISTANCE/2
        grasp_endpoints[:,1] = midpoints - dirs*MAX_HAND_DISTANCE/2

        n0 = np.zeros(grasp_endpoints.shape)
        n1 = np.zeros(grasp_endpoints.shape)

        normal_scale = 0.01
        n0[:, 0] = grasp_vertices[:, 0]
        n0[:, 1] = grasp_vertices[:, 0] + normal_scale * grasp_normals[:, 0]
        n1[:, 0] = grasp_vertices[:, 1]
        n1[:, 1] = grasp_vertices[:, 1] + normal_scale * grasp_normals[:, 1]

        for grasp, quality, normal0, normal1 in zip(grasp_endpoints, grasp_qualities, n0, n1):
            color = [min(1, 2*(1-quality)), min(1, 2*quality), 0, 1]
            vis3d.plot3d(grasp, color=color, tube_radius=.001)
            vis3d.plot3d(normal0, color=(0, 0, 0), tube_radius=.002)
            vis3d.plot3d(normal1, color=(0, 0, 0), tube_radius=.002)
        vis3d.show()

    def vis_transform(self, mesh, G_transform, vertices):
        """
        Pass in any grasp and its associated grasp quality.  this function will plot
        each grasp on the object and plot the grasps as a bar between the points, with
        colored dots on the line endpoints representing the grasp quality associated
        with each grasp

        Parameters
        ----------
        mesh : :obj:`Trimesh`
        grasp_vertices : mx2x3 :obj:`numpy.ndarray`
            m grasps.  Each grasp containts two contact points.  Each contact point
            is a 3 dimensional vector, hence the shape mx2x3
        grasp_qualities : mx' :obj:`numpy.ndarray`
            vector of grasp qualities for each grasp
        """
        L = MAX_HAND_DISTANCE / 2 # gripper half width

        # transform from gripper to contact 1
        G_gc1 = np.array([[1,  0,  0,    0],
                         [0,  0,  1, -1*L],
                         [0, -1,  0,    0],
                         [0,  0,  0,    1]])

        # transform from gripper to contact 2
        G_gc2 = np.array([[1,  0,  0,    0],
                         [0,  0, -1,    L],
                         [0,  1,  0,    0],
                         [0,  0,  0,    1]])

        G = G_transform.matrix

        print('G')
        print(G)

        G_oc1 = np.matmul(G, G_gc1)
        G_oc2 = np.matmul(G, G_gc2)

        scale = 0.01
        o = np.array([0, 0, 0, 1])
        x = np.array([scale, 0, 0, 1])
        y = np.array([0, scale, 0, 1])
        z = np.array([0, 0, scale, 1])

        ot = np.matmul(G, o)
        xt = np.matmul(G, x)
        yt = np.matmul(G, y)
        zt = np.matmul(G, z)

        o1 = np.matmul(G_oc1, o)
        x1 = np.matmul(G_oc1, x)
        y1 = np.matmul(G_oc1, y)
        z1 = np.matmul(G_oc1, z)

        o2 = np.matmul(G_oc2, o)
        x2 = np.matmul(G_oc2, x)
        y2 = np.matmul(G_oc2, y)
        z2 = np.matmul(G_oc2, z)

        vis3d.mesh(mesh, style='wireframe')

        #Plot origin axes
        x_axis = np.array([o, x])[:, :3]
        y_axis = np.array([o, y])[:, :3]
        z_axis = np.array([o, z])[:, :3]

        x_axis_t = np.array([ot, xt])[:, :3]
        y_axis_t = np.array([ot, yt])[:, :3]
        z_axis_t = np.array([ot, zt])[:, :3]

        x_axis_1 = np.array([o1, x1])[:, :3]
        y_axis_1 = np.array([o1, y1])[:, :3]
        z_axis_1 = np.array([o1, z1])[:, :3]

        x_axis_2 = np.array([o2, x2])[:, :3]
        y_axis_2 = np.array([o2, y2])[:, :3]
        z_axis_2 = np.array([o2, z2])[:, :3]

        vis3d.plot3d(x_axis, color=(0.5,0,0), tube_radius=0.001)
        vis3d.plot3d(y_axis, color=(0,0.5,0), tube_radius=0.001)
        vis3d.plot3d(z_axis, color=(0,0,0.5), tube_radius=0.001)

        vis3d.plot3d(x_axis_t, color=(255,0,0), tube_radius=0.001)
        vis3d.plot3d(y_axis_t, color=(0,255,0), tube_radius=0.001)
        vis3d.plot3d(z_axis_t, color=(0,0,255), tube_radius=0.001)

        vis3d.plot3d(x_axis_1, color=(255,0,0), tube_radius=0.001)
        vis3d.plot3d(y_axis_1, color=(0,255,0), tube_radius=0.001)
        vis3d.plot3d(z_axis_1, color=(0,0,255), tube_radius=0.001)

        vis3d.plot3d(x_axis_2, color=(255,0,0), tube_radius=0.001)
        vis3d.plot3d(y_axis_2, color=(0,255,0), tube_radius=0.001)
        vis3d.plot3d(z_axis_2, color=(0,0,255), tube_radius=0.001)

        vis3d.points(vertices[0], scale=0.003)
        vis3d.points(vertices[1], scale=0.003)

        vis3d.show()

    def compute_approach_direction(self, mesh, grasp_vertices, grasp_quality, grasp_normals):

        ## initalizing stuff ##
        visualize = True
        nb_directions_to_test = 6
        normal_scale = 0.01
        plane_normal = normalize(grasp_vertices[0] - grasp_vertices[1])

        midpoint = (grasp_vertices[0] + grasp_vertices[1]) / 2

        ## generating a certain number of approach directions ##
        theta = np.pi / nb_directions_to_test
        rot_mat = rotation_3d(-plane_normal, theta)

        horizontal_direction = normalize(np.cross(plane_normal, np.array([0, 0, 1])))
        directions_to_test = [horizontal_direction] #these are vectors
        approach_directions = [np.array([midpoint, midpoint + horizontal_direction * normal_scale])] #these are two points for visualization

        for i in range(nb_directions_to_test-1):
            directions_to_test.append(normalize(np.matmul(rot_mat, directions_to_test[-1])))
            approach_directions.append(np.array([midpoint, midpoint + directions_to_test[-1] * normal_scale]) )

        ## computing the palm position for each approach direction ##
        palm_positions = []
        for i in range(nb_directions_to_test):
            palm_positions.append(midpoint + finger_length * directions_to_test[i])

        if visualize:
            ## plotting the whole mesh ##
            vis3d.mesh(mesh, style='wireframe')

            ## computing and plotting midpoint and gripper position ##
            dirs = (grasp_vertices[0] - grasp_vertices[1]) / np.linalg.norm(grasp_vertices[0] - grasp_vertices[1])
            grasp_endpoints = np.zeros(grasp_vertices.shape)
            grasp_endpoints[0] = midpoint + dirs*MAX_HAND_DISTANCE/2
            grasp_endpoints[1] = midpoint - dirs*MAX_HAND_DISTANCE/2

            color = [min(1, 2*(1-grasp_quality)), min(1, 2*grasp_quality), 0, 1]
            vis3d.plot3d(grasp_endpoints, color=color, tube_radius=.001)
            vis3d.points(midpoint, scale=0.003)

            ## computing and plotting normals at contact points ##
            n0 = np.zeros(grasp_endpoints.shape)
            n1 = np.zeros(grasp_endpoints.shape)
            n0[0] = grasp_vertices[0]
            n0[1] = grasp_vertices[0] + normal_scale * grasp_normals[0]
            n1[0] = grasp_vertices[1]
            n1[1] = grasp_vertices[1] + normal_scale * grasp_normals[1]
            vis3d.plot3d(n0, color=(0, 0, 0), tube_radius=.002)
            vis3d.plot3d(n1, color=(0, 0, 0), tube_radius=.002)

            ## plotting normals the palm positions for each potential approach direction ##
            for i in range(nb_directions_to_test):
                vis3d.points(palm_positions[i], scale=0.003)

            vis3d.show()

        directions_to_test = [directions_to_test[3], directions_to_test[2], directions_to_test[4], directions_to_test[1], directions_to_test[5], directions_to_test[0]]
        palm_positions = [palm_positions[3], palm_positions[2], palm_positions[4], palm_positions[1], palm_positions[5], palm_positions[0]]

        ## checking if some approach direction is valid ##
        for i in range(nb_directions_to_test):
            if len(trimesh.intersections.mesh_plane(mesh, directions_to_test[i], palm_positions[i])) == 0:
                # it means the palm won't bump with part
                return directions_to_test[i]

        # it means all approach directions will bump with part
        return -1

    def top_n_actions(self, mesh, obj_name, vis=True):
        """
        Takes in a mesh, samples a bunch of grasps on the mesh, evaluates them using the
        metric given in the constructor, and returns the best grasps for the mesh.  SHOULD
        RETURN GRASPS IN ORDER OF THEIR GRASP QUALITY.

        You should try to use mesh.mass to get the mass of the object.  You should check the
        output of this, because from this
        https://github.com/BerkeleyAutomation/trimesh/blob/master/trimesh/base.py#L2203
        it would appear that the mass is approximated using the volume of the object.  If it
        is not returning reasonable results, you can manually weight the objects and store
        them in the dictionary at the top of the file.

        Parameters
        ----------
        mesh : :obj:`Trimesh`
        vis : bool
            Whether or not to visualize the top grasps

        Returns
        -------
        :obj:`list` of :obj:`autolab_core.RigidTransform`
            the matrices T_grasp_world, which represents the hand poses of the baxter / sawyer
            which would result in the fingers being placed at the vertices of the best grasps
        """
        # Some objects have vertices in odd places, so you should sample evenly across
        # the mesh to get nicer candidate grasp points using trimesh.sample.sample_surface_even()

        ## old stuff ##
        # vertices, ids = trimesh.sample.sample_surface(mesh, self.n_vert)
        # vertices, ids = trimesh.sample.sample_surface_even(mesh, self.n_vert)
        # convex_hull = trimesh.convex.convex_hull(mesh)
        # intersection = trimesh.boolean.intersection([mesh, convex_hull], engine='scad')

        ## computing vertices ##
        print('SAMPLING VERTICES')
        vertices, ids = trimesh.sample.sample_surface_even(mesh, self.n_vert)
        normals = mesh.face_normals[ids] #face or vertex ????
        normals = -1 * normals

        ## sampling some grasps ##
        print('SAMPLING GRASPS')

        grasp_vertices, grasp_normals = self.sample_grasps(vertices, normals)
        object_mass = OBJECT_MASS[obj_name]

        ## computing grasp qualities and finding the n best ##
        print('COMPUTING GRASP QUALITIES')
        grasp_qualities = self.score_grasps(grasp_vertices, grasp_normals, object_mass)

        ## visualizing all grasps ##
        #self.vis(mesh, grasp_vertices, np.array(grasp_qualities), np.array(grasp_normals))

        ## keeping only the best n_execute ##
        grasp_vertices = list(grasp_vertices)
        grasp_normals = list(grasp_normals)
        best_grasp_vertices = []
        best_grasp_normals = []
        best_grasp_qualities = []
        nbr_best_found = 0

        while nbr_best_found < self.n_execute:
            idx_max = np.argmax(grasp_qualities)
            best_grasp_vertices.append(grasp_vertices[idx_max])
            best_grasp_normals.append(grasp_normals[idx_max])
            best_grasp_qualities.append(grasp_qualities[idx_max])
            grasp_vertices.pop(idx_max)
            grasp_normals.pop(idx_max)
            grasp_qualities.pop(idx_max)
            nbr_best_found += 1

        best_grasp_vertices = np.array(best_grasp_vertices)
        best_grasp_qualities = np.array(best_grasp_qualities)
        best_grasp_normals = np.array(best_grasp_normals)

        print('BEST GRASPS:')
        print(best_grasp_qualities)
        print(best_grasp_vertices[0])
        print(best_grasp_vertices[1])
        print(best_grasp_vertices[2])
        print(best_grasp_vertices[3])
        print(best_grasp_vertices[4])
        ## visualizing the best grasps ##
        self.vis(mesh, best_grasp_vertices, best_grasp_qualities, best_grasp_normals)

        ## generating the hand poses ##
        print('GENERATING HAND POSES')
        hand_poses = []

        for i in range(self.n_execute):
            approach_dir = self.compute_approach_direction(mesh, best_grasp_vertices[i], best_grasp_qualities[i], best_grasp_normals[i])
            # WARNING: maybe we should take the opposite of approach_dir -> need to visualize it to make sure
            approach_dir = - approach_dir

            if type(approach_dir) == int:
                # it means the palm will bump in the part no matter from where it arrives
                print('Grasp not doable')
                raise Exception

            hand_poses.append(self.vertices_to_baxter_hand_pose(best_grasp_vertices[i], approach_dir, obj_name))
            self.vis_transform(mesh, hand_poses[-1], best_grasp_vertices[i])

        return hand_poses

    def parameter_sweep(self, mesh, obj_name):
        print('------------------------')
        print('RUNNING PARAMETER SWEEP')
        print('------------------------')

        print('SAMPLING VERTICES')
        vertices, ids = trimesh.sample.sample_surface_even(mesh, self.n_vert)
        normals = mesh.face_normals[ids] #face or vertex ????
        normals = -1 * normals

        ## sampling some grasps ##
        print('SAMPLING GRASPS FROM GEARBOX')

        grasp_vertices, grasp_normals = self.sample_grasps(vertices, normals)
        object_mass = OBJECT_MASS[obj_name]

        print('vertices:')
        print(grasp_vertices.shape)

        ## computing grasp qualities and finding the n best ##
        print('COMPUTING GRASP QUALITIES')
        grasp_qualities = []

        stds = np.array([0.005, 0.005, 0.1])
        for i in range(grasp_vertices.shape[0]):
            if i % 10 == 0:
                print('testing vertex {} for robust force closure'.format(i))
            grasp_qualities.append(robust_force_closure(grasp_vertices[i], grasp_normals[i], self.n_facets, CONTACT_MU, CONTACT_GAMMA, object_mass, MIN_HAND_DISTANCE, MAX_HAND_DISTANCE, stds))
        grasp_qualities = np.array(grasp_qualities)

        v1 = grasp_vertices[:,0,:]
        v2 = grasp_vertices[:,1,:]
        q = np.array([grasp_qualities]).T

        data=np.hstack((v1,v2,q))
        print('v1x,v1y,v1z,v2x,v2y,v2z,quality')
        print('data:')
        print(data.shape)

        ind = np.argmax(grasp_qualities)
        print('index of max: {}'.format(ind))

        best_vertices = grasp_vertices[ind]
        best_normals = grasp_normals[ind]
        best_quality = grasp_qualities[ind]

        print('BEST RESULT:')
        print('vertices:')
        print(grasp_vertices[ind])
        print('normals')
        print(grasp_normals[ind])
        print('quality')
        print(grasp_qualities[ind])

        best_vertices = grasp_vertices[ind]
        best_normals = grasp_normals[ind]
        best_quality = grasp_qualities[ind]

        #EXPERIMENTAL RESULT FOR THIS GRASP
        SUCCESS_RATE = 0.8

        # sweep coefficient of friction
        num_test_points = 15
        min_len_test = np.linspace(stds[0]-0.005, stds[0]+0.005, num_test_points)
        max_len_test = np.linspace(stds[1]-0.005, stds[1]+0.005, num_test_points)
        mu_test =      np.linspace(stds[2]-0.1, stds[2]+0.1, num_test_points)

        new_qualities = []
        square_errors = []

        for mu in mu_test:
            new_stds = np.array([0.005, 0.005, mu])
            new_quality = robust_force_closure(best_vertices, best_normals, self.n_facets, CONTACT_MU, CONTACT_GAMMA, object_mass, MIN_HAND_DISTANCE, MAX_HAND_DISTANCE, new_stds)
            new_qualities.append(new_quality)
            square_errors.append((SUCCESS_RATE - new_quality) ** 2)
        new_qualities = np.array(new_qualities)
        square_errors = np.array(square_errors)

        min_index = np.argmin(square_errors)
        print('best std of mu: {}'.format(mu_test[min_index]))

        plt.figure()
        plt.grid(True)
        plt.axvline(color='k')
        plt.axhline(color='k')
        plt.title('Varying standard deviation of coefficient of friction')
        plt.xlabel('std of mu')
        plt.ylabel('square-error')
        plt.plot(mu_test, square_errors, color='r')
        #plt.show()

        new_qualities = []
        square_errors = []

        for m in min_len_test:
            new_stds = np.array([m, 0.005, 0.1])
            new_quality = robust_force_closure(best_vertices, best_normals, self.n_facets, CONTACT_MU, CONTACT_GAMMA, object_mass, MIN_HAND_DISTANCE, MAX_HAND_DISTANCE, new_stds)
            new_qualities.append(new_quality)
            square_errors.append((SUCCESS_RATE - new_quality) ** 2)
        new_qualities = np.array(new_qualities)
        square_errors = np.array(square_errors)

        min_index = np.argmin(square_errors)
        print('best std of min gripper length: {}'.format(min_len_test[min_index]))

        plt.figure()
        plt.grid(True)
        plt.axvline(color='k')
        plt.axhline(color='k')
        plt.title('Varying standard deviation of min gripper length')
        plt.xlabel('std of len')
        plt.ylabel('square-error')
        plt.plot(min_len_test, square_errors, color='r')

        new_qualities = []
        square_errors = []

        for m in max_len_test:
            new_stds = np.array([0.005, m, 0.1])
            new_quality = robust_force_closure(best_vertices, best_normals, self.n_facets, CONTACT_MU, CONTACT_GAMMA, object_mass, MIN_HAND_DISTANCE, MAX_HAND_DISTANCE, new_stds)
            new_qualities.append(new_quality)
            square_errors.append((SUCCESS_RATE - new_quality) ** 2)
        new_qualities = np.array(new_qualities)
        square_errors = np.array(square_errors)

        min_index = np.argmin(square_errors)
        print('best std of max gripper length: {}'.format(max_len_test[min_index]))

        plt.figure()
        plt.grid(True)
        plt.axvline(color='k')
        plt.axhline(color='k')
        plt.title('Varying standard deviation of max gripper length')
        plt.xlabel('std of len')
        plt.ylabel('square-error')
        plt.plot(min_len_test, square_errors, color='r')

        plt.show()


# 암호 스캔 코드
# 선생님 코드

def f(N, M, s):
    used = [[0]*M for _ in range(N)] # 확인된 암호패턴 영역을 표시
    total = 0
    for i in range(N):
        j = M*4-1 # 비트 패턴의 끝을 탐색
        while j > 0:
            if s[i][j] == '1' and used[i][j] == 0: # 패턴이 시작되면
                pwd = [0]*8
                col = j
                for k in range(7, -1, -1):
                    cnt = [0, 0, 0] # 1, 0, 1의 각 개수 저장
                    while s[i][col] == '0': # 1이 나올 때까지 0인 구간 스킵
                        col -= 1
                    while s[i][col] == '1': # 1이 나오면 1인 구간의 길이 확인
                        cnt[0] += 1
                        col -= 1
                    while s[i][col] == '0': # 0인 구간의 길이
                        col[1] += 1
                        col -= 1
                    while s[i][col] == '1': # 1인 구간의 길이
                        cnt[2] += 1
                        col -= 1
                    ratio = min(cnt)
                    cnt[0] //= ratio
                    cnt[1] //= ratio
                    cnt[2] //= ratio
                    idx = cnt[0]*100 + cnt[1]*10 + cnt[2]
                    pwd[k] = pat.index(idx)
                check = (pwd[0]+pwd[2]+pwd[4]+pwd[6])*3+pwd[1]+pwd[3]+pwd[5]+pwd[7]
                if check % 10 == 0:
                    total += sum(pwd)

                row = i
                while s[row][j] == '1':
                    for k in range(col, j+1):
                        used[row][k] = 1
                    row += 1
                j = col
            else:
                j -= 1
    return total

b = ['0000', '0001', '0010', '0011',
     '0100', '0101', '0110', '0111',
     '1000', '1001', '1010', '1011',
     '1100', '1101', '1110', '1111']

pat = [112, 122, 221, 114, 231, 132, 411, 213, 312, 211]

T = int(input())
for tc in range(1, T+1):
    N, M = map(int, input().split())
    a = [input() for _ in range(N)]
    s = ['']*N

    for i in range(N): # N개의 16진수 라인
        for j in range(M):
            s[i] += b[int(a[i][j], 16)] # 2진수의 문자열 형태로 저장
    print('#{} {}'.format(tc, f(N, M, s)))


# Copyright (c) 2015 Satlab Aps <satlab@satlab.com>
# Released under the MIT license (See COPYING for details)

# Hameg HMP2030 Power supply control

import time
import serial

class HMP2030Exception(Exception):
    pass

class HMP2030():
    def __init__(self, port, baud=9600, timeout=1):
        self.serial = serial.Serial(port, baud, timeout=timeout)

        self.get_ident()

    def __del__(self):
        try:
            self.serial.close()
        except:
            pass

    def _write(self, data):
        self.serial.write(data)
        time.sleep(0.1)

    def get_ident(self):
        self._write('*IDN?\r\n')
        idn = self.serial.readline()

        if not 'HMP2030' in idn:
            raise HMP2030Exception('Failed to identify HMP2030 power supply')

        return idn

    def set_channel(self, channel):
        if channel < 1 or channel > 3:
            raise HMP2030Exception('Channel must be between 1 and 3')

        self._write('INST:NSEL {0}\r\n'.format(channel))

    def measure_voltage(self, channel):
        self.set_channel(channel)
        self._write('MEAS:VOLT?\r\n')
        volt = self.serial.readline()
        if len(volt) < 1:
            raise HMP2030Exception('Failed to read voltage')

        return float(volt)

    def measure_current(self, channel):
        self.set_channel(channel)
        self._write('MEAS:CURR?\r\n')
        curr = self.serial.readline()
        if len(curr) < 1:
            raise HMP2030Exception('Failed to read current')

        return float(curr)

    def measure_power(self, channel, average=1):
        total = 0
        peak = 0
        for m in range(average):
            volt = self.measure_voltage(channel)
            curr = self.measure_current(channel)
            power = volt * curr
            if power > peak:
                peak = power
            total += power

        total = total / average

        return float(total), float(peak)

    def activate_channel(self, channel, state):
        self.set_channel(channel)

        nstate = '1' if state == True else '0'
        self._write('OUTP:SEL {0}\r\n'.format(nstate))

    def activate_output(self, state):
        nstate = '1' if state == True else '0'
        self._write('OUTP:GEN {0}\r\n'.format(nstate))


from colonialismdb.education.models import MainDataEntry, EducationExpenditureType, SchoolType
from colonialismdb.common.models import SpatialAreaUnit, Currency
from colonialismdb.sources.models import Table
from colonialismdb.common.admin import BaseSubmitAdmin, BaseMergeableCategoryAdmin, BaseMainDataEntryAdmin
from django.contrib import admin

class MainDataEntryAdmin(BaseMainDataEntryAdmin) :
  fieldsets = [
      (None,
        {'fields' : ['active', 'submitted_by', 'datetime_created', ]}),

      ('Location Information',
        {'fields' : ['location', 'original_location_name', 'alternate_location_name', 'spatial_area', 'spatial_area_unit']}),

      ('Date Range',
        {'fields' : ['begin_date', 'end_date', 'circa']}),

      ('Currency',
        {'fields' : ['currency', 'currency_exchange_rate']}),

      ('Education Expenditure',
        {'fields' : ['education_expenditure', 'education_expenditure_type', 'is_total_expenditure']}),

      ('Schools',
        {'fields' : ['num_schools', 'is_total_schools', 'school_type', 'pub_priv', 'rel_sec']}),

      ('Students',
        {'fields' : ['num_students', 'student_gender', 'is_total_students']}),

      ('Teachers',
        {'fields' : ['num_teachers', 'teacher_gender', 'is_total_teachers']}),

      ('Source Information',
        {'fields' : ['source', 'page_num', 'primary_source_obj', 'primary_source_text']}),

      ('Other Information',
        {'fields' : ['remarks', ]}),
  ]

  #radio_fields = { 'value_precision' : admin.HORIZONTAL, 'individ_fam' : admin.HORIZONTAL, 'population_gender' : admin.HORIZONTAL, 'value_unit' : admin.HORIZONTAL, }

  activate_perm = 'education.activate_maindataentry'

class EducationExpenditureTypeAdmin(BaseMergeableCategoryAdmin):
    activate_perm = 'education.activate_educexpendtype'
    merge_perm = 'education.merge_educexpendtype'

class SchoolTypeAdmin(BaseMergeableCategoryAdmin):
    activate_perm = 'education.activate_schooltype'
    merge_perm = 'education.merge_schooltype'

admin.site.register(MainDataEntry, MainDataEntryAdmin)
admin.site.register(EducationExpenditureType, EducationExpenditureTypeAdmin)
admin.site.register(SchoolType, SchoolTypeAdmin)


import logging
import sys
from abc import abstractmethod
from typing import TextIO

import pandas as pd
from datapackage import Package
from pandas_datapackage_reader import read_datapackage
from sqlalchemy import create_engine

from otoole import read_packaged_file
from otoole.preprocess.excel_to_osemosys import CSV_TO_EXCEL

logger = logging.getLogger(__name__)

class DataPackageTo(object):
    """Convert a data package to another format

    Arguments
    ---------
    datapackage: str
        The path to the databackage
    datafilepath: str
        The path to the destination file or folder
    sql: bool, default=False
        Flag to set whether the source datapackage is in sqlite format
    """

    def __init__(self, datapackage: str, datafilepath: str, sql: bool = False):

        self.datapackage = datapackage
        self.datafilepath = datafilepath
        self.sql = sql
        self.package = self._get_package()
        self.default_values = self._get_default_values()
        self.config = read_packaged_file("config.yaml", "otoole.preprocess")

    def _get_package(self):

        if self.sql:
            engine = create_engine("sqlite:///{}".format(self.datapackage))
            package = Package(storage="sql", engine=engine)
        else:
            package = read_datapackage(self.datapackage)  # typing: datapackage.Package

        return package

    def _get_default_values(self):
        default_resource = (
            self.package.pop("default_values").set_index("name").to_dict()
        )
        return default_resource["default_value"]

    def convert(self):
        """Perform the conversion from datapackage to destination format
        """

        handle = self._header()
        logger.debug(self.default_values)

        for name, df in self.package.items():
            logger.debug(name)

            if df.empty:
                columns = [x["name"] for x in df._metadata["schema"]["fields"]]
                df = pd.DataFrame(columns=columns)

            df = df.reset_index()
            if "index" in df.columns:
                df = df.drop(columns="index")

            logger.debug("Number of columns: %s, %s", len(df.columns), df.columns)
            if len(df.columns) > 1:
                default_value = self.default_values[name]
                self._write_parameter(df, name, handle, default=default_value)

            else:
                self._write_set(df, name, handle)

        self._footer(handle)

        handle.close()

    @abstractmethod
    def _header(self) -> TextIO:
        raise NotImplementedError()

    @abstractmethod
    def _write_parameter(
        self, df: pd.DataFrame, parameter_name: str, handle: TextIO, default: float
    ) -> pd.DataFrame:
        """Write parameter data"""
        raise NotImplementedError()

    @abstractmethod
    def _write_set(self, df: pd.DataFrame, set_name, handle: TextIO) -> pd.DataFrame:
        """Write set data"""
        raise NotImplementedError()

    @abstractmethod
    def _footer(self, handle: TextIO):
        raise NotImplementedError()

class DataPackageToDatafile(DataPackageTo):
    """Write datapackage to a GMPL datafile
    """

    def _header(self):
        filepath = open(self.datafilepath, "w")
        msg = "# Model file written by *otoole*\n"
        filepath.write(msg)
        return filepath

    def _form_parameter(self, df: pd.DataFrame, default: float):

        df = df[df.VALUE != default]
        return df

    def _write_parameter(
        self, df: pd.DataFrame, parameter_name: str, handle: TextIO, default: float
    ):
        """Write parameter data to a GMPL datafile, omitting data with default value

        Arguments
        ---------
        filepath : StreamIO
        df : pandas.DataFrame
        parameter_name : str
        handle: TextIO
        default : int
        """
        df = self._form_parameter(df, default)
        handle.write("param default {} : {} :=\n".format(default, parameter_name))
        df.to_csv(path_or_buf=handle, sep=" ", header=False, index=False)
        handle.write(";\n")

    def _write_set(self, df: pd.DataFrame, set_name, handle: TextIO):
        """Write set data to a GMPL datafile

        Arguments
        ---------
        df : pandas.DataFrame
        set_name : str
        handle: TextIO
        """
        handle.write("set {} :=\n".format(set_name))
        df.to_csv(path_or_buf=handle, sep=" ", header=False, index=False)
        handle.write(";\n")

    def _footer(self, handle: TextIO):
        handle.write("end;\n")
        handle.close()

class DataPackageToExcel(DataPackageTo):
    def _header(self):
        return pd.ExcelWriter(self.datafilepath, mode="w")

    def _form_parameter(
        self, df: pd.DataFrame, parameter_name: str, default: float
    ) -> pd.DataFrame:
        """Converts data into wide format

        Arguments
        ---------
        df: pd.DataFrame
        parameter_name: str
        default: float

        Returns
        -------
        pandas.DataFrame
        """

        if not df.empty:

            names = df.columns.to_list()
            if len(names) > 2:
                logger.debug(
                    "More than 2 columns for {}: {}".format(parameter_name, names)
                )
                rows = names[0:-2]
                columns = names[-2]
                values = names[-1]
                logger.debug("Rows: {}; columns: {}; values: {}", rows, columns, values)
                logger.debug("dtypes: {}".format(df.dtypes))
                pivot = pd.pivot_table(
                    df, index=rows, columns=columns, values=values, fill_value=default
                )
            elif len(names) == 2:
                logger.debug("Two columns for {}: {}".format(parameter_name, names))
                values = names[-1]
                rows = names[0:-2]
                logger.debug("Rows: {}; values: {}", rows, values)
                pivot = pd.pivot_table(
                    df, index=rows, values=values, fill_value=default
                )
            else:
                logger.debug("One column for {}: {}".format(parameter_name, names))
                pivot = df.copy()
                pivot = pivot.reset_index(drop=True)

        else:
            logger.debug("Dataframe {} is empty".format(parameter_name))
            pivot = df.copy()

        return pivot

    def _write_parameter(
        self,
        df: pd.DataFrame,
        parameter_name: str,
        handle: pd.ExcelWriter,
        default: float,
    ):
        try:
            name = CSV_TO_EXCEL[parameter_name]
        except KeyError:
            name = parameter_name
        df = self._form_parameter(df, parameter_name, default)
        df.to_excel(handle, sheet_name=name, merge_cells=False)

    def _write_set(self, df: pd.DataFrame, set_name, handle: pd.ExcelWriter):
        df.to_excel(handle, sheet_name=set_name, merge_cells=False, index=False)

    def _footer(self, handle=pd.ExcelWriter):
        handle.close()

def convert_datapackage_to_datafile(path_to_datapackage, path_to_datafile):
    dp = DataPackageToDatafile(path_to_datapackage, path_to_datafile)
    dp.convert()

def convert_datapackage_to_excel(path_to_datapackage, path_to_excel):
    dp = DataPackageToExcel(path_to_datapackage, path_to_excel)
    dp.convert()

if __name__ == "__main__":
    logging.basicConfig(level=logging.DEBUG)
    path_to_datapackage = sys.argv[1]
    path_to_datafile = sys.argv[2]

    DataPackageToDatafile(path_to_datapackage, path_to_datafile)


#
# [532] K-diff Pairs in an Array
#
# https://leetcode.com/problems/k-diff-pairs-in-an-array/description/
#
# algorithms
# Easy (28.36%)
# Total Accepted:    42.3K
# Total Submissions: 149.2K
# Testcase Example:  '[3,1,4,1,5]\n2'
#
#
# Given an array of integers and an integer k, you need to find the number of
# unique k-diff pairs in the array. Here a k-diff pair is defined as an integer
# pair (i, j), where i and j are both numbers in the array and their absolute
# difference is k.
#
#
#
# Example 1:
#
# Input: [3, 1, 4, 1, 5], k = 2
# Output: 2
# Explanation: There are two 2-diff pairs in the array, (1, 3) and (3,
# 5).Although we have two 1s in the input, we should only return the number of
# unique pairs.
#
#
#
# Example 2:
#
# Input:[1, 2, 3, 4, 5], k = 1
# Output: 4
# Explanation: There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4)
# and (4, 5).
#
#
#
# Example 3:
#
# Input: [1, 3, 1, 5, 4], k = 0
# Output: 1
# Explanation: There is one 0-diff pair in the array, (1, 1).
#
#
#
# Note:
#
# The pairs (i, j) and (j, i) count as the same pair.
# The length of the array won't exceed 10,000.
# All the integers in the given input belong to the range: [-1e7, 1e7].
#

class Solution:
    def findPairs(self, nums, k):
        """
        :type nums: List[int]
        :type k: int
        :rtype: int
        """
        if k < 0:
            return 0
        dic, res = dict(), 0
        visited = set()
        for n in nums:
            if n not in dic:
                dic[n] = 0
            dic[n] += 1
            if k == 0:
                if (n, n) not in visited and dic[n] != 1:
                    res += 1
                    visited.add((n, n))
            else:
                cur = tuple(sorted([n, n-k]))
                if n-k in dic and cur not in visited:
                    res += 1
                    visited.add(cur)
                cur = tuple(sorted([n, n+k]))
                if n+k in dic and cur not in visited:
                    res += 1
                    visited.add(cur)
        return res


# Setup file, to execute the hole or a part of the prediction process
# Author: Youssef Hmamouche

import os
import sys
import glob

def usage ():
	print ("Usage: python run.py arg0 arg1 arg2")
	print ("arg0: operation type (-pre_selection, -selection, -prediction, -evaluation, -plot)")
	print ("arg1: data_path")
	print ("arg2: script name (optional), if it is not given, all scripts of this step will be executed")

# Generic funtion to
# Execute one script on a file or multiple files in data_path,
# and put results on output_directory
def execute_script (data_path, output_directory, script_path):

	script_name = script_path.split ('/')[-1]

	if not os.path.exists(script_path):
		print ("Error: the script does not exist!")
		exit (1)

	if script_name.endswith('.py'):
		query = "python " + script_path + " " + data_path + " " + output_directory

	elif script_name.endswith('.R'):
		query = "Rscript " + script_path  + " " + data_path + " " + output_directory

	else:
		print ("Current verstion accept just python and R files.")
		return

	try:
		os.system (query)
	except ValueError:
		print ("Error in executing the script " + script_path + " on " + data_path)

def pre_selection (argv):

	data_path = argv [1]

	if not os.path.exists(data_path):
		print ("Error: data path does not exist")
		exit (1)

	data_name = data_path.split ('/')[-1].split ('.')[0]
	output_directory = 'results/pre_selection/' + data_name + "/"

	if not os.path.exists (output_directory):
			os.makedirs (output_directory)

	graphs_path = "src/pre_selection/"

	if len (argv) == 2:
		graph_names = [fn for fn in os.listdir(graphs_path)
              if any(fn.endswith(ext) for ext in ['.py', '.R'])]

		for script_name in graph_names:
			execute_script (data_path, output_directory, graphs_path + script_name)

	elif len (argv) == 3:
		script_name = argv [2]
		execute_script (data_path, output_directory,  script_name)

	else:
		usage ()

def selection (argv):

	data_path = argv [1]

	if not os.path.exists(data_path):
		print ("Error: data path does not exist")
		exit (1)

	data_name = data_path.split ('/')[-1].split ('.')[0]
	output_directory = 'results/selection/' + data_name + "/"

	if not os.path.exists (output_directory):
			os.makedirs (output_directory)

	reduction_methods_path = "src/selection/"

	if len (argv) == 2:

		reduction_methods = [fn for fn in os.listdir(reduction_methods_path)
              if any(fn.endswith(ext) for ext in ['.py', '.R'])]

		for script_name in reduction_methods:
			execute_script (data_path, output_directory, reduction_methods_path + script_name)

	elif len (argv) == 3:
		script_name = argv [2]
		execute_script (data_path, output_directory,  script_name)

	else:
		usage ()

def prediction (argv):

	data_path = argv [1]

	if not os.path.exists(data_path):
		print ("Error: data path does not exist")
		exit (1)

	data_name = data_path.split ('/')[-1].split ('.')[0]
	output_directory = 'results/prediction/' + data_name + "/"

	if not os.path.exists (output_directory):
			os.makedirs (output_directory)

	#selection_files = glob.glob ("results/selection/" + data_name  + "/*.csv*")
	selection_files_path = "results/selection/" + data_name + '/'

	if len (argv) == 3:
		script_name = argv [2]
		script_name = script_name .split ('/')[-1]

		if script_name in ['var_shrinkage.py', 'auto_arima.R']:
			execute_script (data_path, output_directory, "src/prediction/" + script_name)

		elif script_name in ['lstm.py', 'vecm.R']:
			execute_script (selection_files_path, output_directory, "src/prediction/" + script_name)

	else:
		usage ()

def pre_evaluation (argv):

	data_path = argv [1]
	data_name = data_path.split ('/')[-1].split ('.')[0]

	if not os.path.exists(data_path):
		print ("Error: data path does not exist")
		exit (1)

	output_directory = "results/pre_evaluation/" + data_name + '/'
	os.system ("rm -r " +  output_directory)
	os.makedirs (output_directory)

	script = "src/pre_evaluation/pre_evaluation.py"

	query = "python " + script + ' ' + data_path + ' ' + output_directory
	os. system (query)

def evaluation (argv):

	data_path = argv [1]
	data_name = data_path.split ('/')[-1].split ('.')[0]

	if not os.path.exists(data_path):
		print ("Error: data path does not exist")
		exit (1)

	output_directory = "results/evaluation/" + data_name + '/'
	if not os.path.exists (output_directory):
			os.makedirs (output_directory)

	scripts = glob.glob ("src/evaluation/*.py")

	for script in scripts:

		query = "python " + script + ' ' + data_path + ' ' + output_directory
		os. system (query)

def make_figures (data):

	if not os.path.exists ("plots/pdf"):
			os.makedirs ("plots/pdf")

	if not os.path.exists ("plots/csv"):
			os.makedirs ("plots/csv")

	try:
		os.system ("python plots/matrix-eval.py " + data)
	except (ValueError):
		print ("Error in generating reduction methods plots")

	try:
		os.system ("python plots/models-eval.py " + data)
	except (ValueError):
		print ("Error in generating reduction methods plots")

if __name__ == '__main__':

	if len (sys.argv) == 1:
		print ("Insufficient arguments, use -h for help")

	# help
	elif len (sys.argv) == 2 and sys.argv[1] == '-h':
		usage ()

	elif len (sys.argv) == 5 and sys.argv[1] == '-g':
		execute_script (sys.argv[2], sys.argv[3], sys.argv[4])

	elif len (sys.argv) == 5 and sys.argv[1] == '-mlf':
		for i in range (2, len (sys.argv)):
			# sys.argv[2]: script, sys.argv[3]: outout dir
			execute_script (sys.argv[i], sys.argv[3], sys.argv[2])

	# Generate plots for all datasets
	elif len (sys.argv) == 2 and sys.argv[1] in ['-pl', '-plot']:
		datasets = glob.glob ("data/*.csv")
		for data in datasets:
			make_figures (data)

	# Generate plots for one datasets passed as 3th argument
	elif len (sys.argv) == 3 and sys.argv[1] in ['-pl', '-plot']:
		data = sys.argv [2]
		data_path = "data/" + data.split ('/')[-1]

		if not os.path.exists (data_path):
			print ("Error, data does not exist.")
			exit (1)

		make_figures (data_path)

	# pre_selection step : computing the causality graphs
	elif sys.argv [1] in ['-pre_selection','-ps']  :
		pre_selection (sys.argv[1:])

	# the selection step
	elif sys.argv [1] in ['-selection', '-s']:
		selection (sys.argv[1:])

	# the prediction step
	elif sys.argv [1] in ['-prediction', '-p']:
		prediction (sys.argv[1:])

	# the evaluation steps

	elif sys.argv [1] in ['-pre_evaluation', '-pe']:
		pre_evaluation (sys.argv[1:])

	elif sys.argv [1] in ['-evaluation', '-e']:
		evaluation (sys.argv[1:])

	else:
		usage ()


#!/usr/bin/python
# -*- coding: utf-8 -*-
import pygame
pygame.init()

class Sprite:

      # Constructor (Entering file location, x position, y position, screen surface)

    def __init__(
        self,
        img,
        x,
        y,
        screen,
        x_width=0,
        y_width=0,
        x_spam=0,
        y_spam=0,
        ):

        self.img = img
        self.x = x
        self.y = y

        self.sprite = pygame.image.load(self.img)
        self.screen = screen

            # Set Rect parameters to 0 by default

        if x_width is None or y_width is None or x_spam is None \
            or y_spam is None:
            pass
        else:
            self.x_width = x_width
            self.y_width = y_width
            self.x_spam = x_spam
            self.y_spam = y_spam

        self.x_num = 0
        self.y_num = 0

    def __init__(
        self,
        img,
        x,
        y,
        screen,
        x_spam=0,
        y_spam=0,
        ):
        self.img = img
        self.x = x
        self.y = y

        self.sprite = pygame.image.load(self.img)
        self.screen = screen

            # Set Rect parameters to 0 by default

        if x_width is None or y_width is None or x_spam is None \
            or y_spam is None:
            pass
        else:
            self.x_width = self.img.get_width() / x_spam
            self.y_width = self.img.get_height() / y_spam
            self.x_spam = x_spam
            self.y_spam = y_spam

      # Flip sprite, Enter True on x to flip horizontally; Enter True on y to flip vertically

    def flip(self, x_flip, y_flip):
        if x_flip:
            self.sprite = pygame.transform.flip(self.sprite, True,
                    False)
        elif y_flip:
            self.sprite = pygame.transform.flip(self.sprite, False,
                    True)
        else:
            self.sprite = pygame.transform.flip(self.sprite, True, True)

      # Change scale of the sprite, enter the width of x and width of y

    def scale(self, x_scale, y_scale):
        self.sprite = pygame.transform.scale(self.sprite, (x_scale,
                y_scale))

      # Draw function, draw sprite using blit function

    def draw(self):
        self.screen.blit(self.sprite, (self.x, self.y))

      # Change image function, enter new image location

    def image(self, image):
        self.sprite = pygame.image.load(image)

      # Change x_position

    @property
    def x_position(self):
        return self.x

    @x_position.setter
    def x_position(self, new_x):
        self.x = new_x

      # Change y_position

    @property
    def y_position(self):
        return self.y

    @y_position.setter
    def y_position(self, new_y):
        self.y = new_y

      # Draw sprite sheet

    def draw_sheet(self):
        self.screen.blit(self.sprite, (self.x, self.y), (self.x_num
                         * self.x_width, self.y_num * self.y_width,
                         self.x_width, self.y_width))

      # Update position

    def update(self):
        if self.x_num < self.x_spam - 1:
            self.x_num += 1
        else:

            self.x_num = 0
            if self.y_num < self.y_spam - 1:
                self.y_num += 1
            else:
                self.y_num = 0


import bottoken
import imgscan

import telebot
import os

# Creating bot
bot = telebot.TeleBot(bottoken.bottoken)

# Start command
@bot.message_handler(commands=['start'])
def start(message):
	bot.send_message(message.from_user.id, "Welcome to TelegramImageReader!\nFor command list send me /help!")

# Help command
@bot.message_handler(commands=['help'])
def start(message):
	bot.send_message(message.from_user.id, "Command list: \n/start - Start work with bot.\n/help - send this message.\n/img2txt - Read text form image.")

# Img2txt command
@bot.message_handler(commands=['img2txt'])
def start(message):
	bot.send_message(message.from_user.id, "Send image to read text from: ")

# Reciving image
@bot.message_handler(content_types=['document'])
def handle_docs_photo(message):
    bot.reply_to(message, "Processing... Result will appear soon!")

    chat_id = message.chat.id

    # Downloading image
    file_info = bot.get_file(message.document.file_id)
    downloaded_file = bot.download_file(file_info.file_path)
    f = open('img1.png', 'wb')
    f.write(downloaded_file)
    f.close()

    # Reading text from image
    txtFromImg = imgscan.textFromImg('img1.png')

    # Deleting image
    os.remove('img1.png')

    # Sending result to user
    bot.send_message(message.from_user.id, 'Text from image: \n\n' + txtFromImg)

# Launching bot
bot.polling()


""" Comment serializers """

# Djanto REST Framework
from rest_framework import serializers

# Models
from publications.models import Comment

# Serializers
from users.serializers import UserModelSerializer

class CommentModelSerializer(serializers.ModelSerializer):

	user = serializers.StringRelatedField()

	class Meta:
		model = Comment
		fields = ('user', 'text', 'publication')

class CreateCommentSerializer(serializers.ModelSerializer):

	class Meta:
		model = Comment
		fields = ('user', 'text', 'publication')


"""
A diamond is an abstract representation for telling which bases are occupied and empty. There should be 1 diamond per
scoreboard.
A diamond is also capable of moving around baserunners with methods corresponding to game events
"""
class Base:
    def __init__(self, player):
        self.player = player

class Diamond:

    """ A diamond has 3 bases, each base has a player """
    def __init__(self, atbat, first, second, third):
        self.atbat = Base(atbat)
        self.first = Base(first)
        self.second = Base(second)
        self.third = Base(third)

    def __str__(self):
        string = ""
        if self.atbat.player:
            string += "At Bat: " + self.atbat.player.name + "\n"
        if self.first.player:
            string += "First: " + self.first.player.name + "\n"
        if self.second.player:
            string += "Second: " + self.second.player.name + "\n"
        if self.third.player:
            string += "Third: " + self.third.player.name

        return string

    """ Move all runners up 1 base ONLY if they would be pushed to the next base """
    def base_on_balls(self):
        runs = []
        if self.get_total_runners() == 3:
            runs.append(self.third.player)

        if self.second.player and self.first.player:
            new_third = self.second
        else:
            new_third = self.third

        if self.first.player:
            new_second = self.first
        else:
            new_second = self.second

        self.third = new_third
        self.second = new_second
        self.first = self.atbat
        self.atbat = Base(None)
        return runs

    """ Move all the runners up 1 base """
    def single(self):
        runs = []
        if self.third.player is not None:
            runs.append(self.third.player)
        self.third = self.second
        self.second = self.first
        self.first = self.atbat
        self.atbat = Base(None)
        return runs

    """ Move all the runners up 2 bases """
    def double(self):
        runs = []
        if self.third.player is not None:
            runs.append(self.third.player)
        if self.second.player is not None:
            runs.append(self.second.player)
        self.third = self.first
        self.first = Base(None)
        self.second = self.atbat
        self.atbat = Base(None)
        return runs

    """ Score all runners, put a guy on 3rd """
    def triple(self):
        runs = []
        if self.third.player is not None:
            runs.append(self.third.player)
        if self.second.player is not None:
            runs.append(self.second.player)
        if self.first.player is not None:
            runs.append(self.first.player)
        self.first = Base(None)
        self.second = Base(None)
        self.third = self.atbat
        self.atbat = Base(None)
        return runs

    """ Score all runners plus the batter """
    def homerun(self):
        runs = []
        if self.third.player is not None:
            runs.append(self.third.player)
        if self.second.player is not None:
            runs.append(self.second.player)
        if self.first.player is not None:
            runs.append(self.first.player)
        runs.append(self.atbat.player)
        self.first = Base(None)
        self.second = Base(None)
        self.third = Base(None)
        self.atbat = Base(None)
        return runs

    def error(self):
        runs = []
        if self.third.player is not None:
            runs.append(self.third.player)
        self.third = self.second
        self.second = self.first
        self.first = self.atbat
        self.atbat = Base(None)
        return runs

    def reset(self):
        self.first = Base(None)
        self.second = Base(None)
        self.third = Base(None)
        self.atbat = Base(None)

    def get_total_runners(self):
        count = 0
        if self.first.player:
            count += 1
        if self.second.player:
            count += 1
        if self.third.player:
            count += 1

        return count


# -*- coding: utf-8 -*-
import datetime
import os
import sys

from .meta import Meta
from .parser import Parser
from .printer import *
from .tokenizer import Token, tokenize
from .utils import *

class Target(object):
    def __init__(self, gmdoc_dir, project_dir, *args, **kwargs):
        self.gmdoc_dir = gmdoc_dir
        self.project_dir = project_dir
        self.meta_path = os.path.join(project_dir, "gmdoc.json")
        self.docs_src_dir = os.path.join(project_dir, "docs_src")
        self.template_dir = os.path.join(gmdoc_dir, "template")

    def execute(self, *args, **kwargs):
        pass

class HelpTarget(Target):
    MESSAGE = (
        "Usage: gmdoc TARGET\n"
        "\n"
        "  TARGET - init  - Initialize gmdoc in the current directory.\n"
        "         - build - Build documentation.\n"
        "         - help  - Display this message.\n"
    )

    def execute(self, *args, **kwargs):
        print(HelpTarget.MESSAGE)

class InitTarget(Target):
    def execute(self, *args, **kwargs):
        meta = Meta.new()
        meta.save(self.meta_path)
        meta_serialized = meta.serialize()

        _docs_src_dest = os.path.join(self.project_dir, "docs_src")
        os.makedirs(_docs_src_dest, exist_ok=True)

        copytree(
            os.path.join(self.gmdoc_dir, "docs_src"),
            _docs_src_dest,
        )

        for root, _, files in os.walk(_docs_src_dest):
            for name in files:
                fname = os.path.join(root, name)
                with open(fname, "r") as f:
                    cnt = f.read().format(**meta_serialized)
                with open(fname, "w") as f:
                    f.write(cnt)

class BuildTarget(Target):
    def flatten_toc(self, toc):
        flattened = []

        def get_name(v):
            return os.path.splitext(os.path.basename(v))[0] + ".html"

        for _, v in toc.items():
            if isinstance(v, dict):
                flattened.append(get_name(v["file"]))
                flattened += self.flatten_toc(v.get("pages", {}))
            else:
                flattened.append(get_name(v))

        return flattened

    def parse_resources(self, prefix):
        parsed = {}

        for root, _, files in os.walk(self.project_dir):
            for file in files:
                if not file.startswith(prefix):
                    continue
                if file[-4:] != ".gml":
                    continue
                fpath = os.path.join(root, file)
                print("Parsing", fpath)
                tokens = []
                with open(fpath) as f:
                    tokens += tokenize(f)
                parsed = merge(Parser(tokens).parse(), parsed)

        return parsed

    def flatten_resources(self, parsed):
        resources = []
        for _, v in parsed.items():
            for r in v:
                resources.append(r)
        resources.sort(key=lambda r: r["name"])
        return resources

    def execute(self, *args, **kwargs):
        if len(sys.argv) > 2:
            docs_dir = sys.argv[2]
        else:
            docs_dir = os.path.join(self.project_dir, "docs_build")
            os.makedirs(docs_dir, exist_ok=True)

        print("Loading meta")
        meta = Meta.load(self.meta_path)

        prefix = meta.prefix
        toc = meta.toc
        _now = datetime.datetime.now()
        datestr = _now.strftime("%B %d, %Y")
        yearstr = _now.strftime("%Y")

        try:
            print("Deleting {}".format(docs_dir))
            shutil.rmtree(docs_dir)
        except:
            pass

        print("Copying resources from {} to {}".format(
            self.template_dir, docs_dir))
        shutil.copytree(self.template_dir, docs_dir)

        print("Loading template")
        with open(os.path.join(self.template_dir, "index.html")) as f:
            template = f.read()

        print("Parsing scripting API documentation")
        parsed = self.parse_resources(prefix)
        resources = self.flatten_resources(parsed)

        out_dir = os.path.join(self.docs_src_dir, "ScriptingAPI")
        os.makedirs(out_dir, exist_ok=True)

        scripting_api_toc = {
            "file": "ScriptingAPI.md",
            "pages": {}
        }

        for r in resources:
            name = r["name"]

            md = resource_to_markdown(r)
            if md is None:
                print("Skipping {name} of type {_type}".format(**r))
                continue

            print("Generating Markdown for", name)
            fname = os.path.abspath("{}/{}.md".format(out_dir, name))
            with open(fname, "w") as f:
                f.write(md)

            scripting_api_toc["pages"][name] = fname

        toc["Scripting API"] = scripting_api_toc

        make_pages(
            meta,
            self.flatten_toc(toc),
            docs_src_dir=self.docs_src_dir,
            docs_dir=docs_dir,
            template=template,
            datestr=datestr,
            yearstr=yearstr
        )


import discord

async def run(client, message, args, prefix, db):
    if message.author.id in (311542309252497409, 300191414569009153):
        if len(message.channel_mentions) == 0:
            chn = message.channel
            msg = " ".join(args)
        else:
            chn = message.channel_mentions[0]
            msg = " ".join(args[1:])

        await message.delete()
        embed = discord.Embed.from_dict(eval(" ".join(args[1:])))
        embed.set_thumbnail(url=message.guild.icon_url)
        await chn.send(embed=embed)


#!/usr/bin/env python3
'''
try:
        userInput=int(input('enter a number:'))
except ValueError:
        print('Not a valid Number!')
'''
try:
        a='1'+1
except TypeError:
        print('Type Error Occured')
try:
        import test
except SyntaxError:
        print('wow that shit you imported broke')


from datetime import datetime

class Types:

    @staticmethod
    def isDate(sstr, fmt="%d/%m/%Y"):
        """
        Return whether the string can be interpreted as a date.

        :param string: str, string to check for date
        """
        try:
            datetime.strptime(sstr, fmt)
            return True

        except:
            return False

    @staticmethod
    def getType(val):
        #print(val)
        #print("input: " + str(type(val)) )
        if isinstance(val, int):
            return "i"
        elif isinstance(val, float):
            return "f"
        elif Types.isDate(val):
            return "d"
        elif isinstance(val, str):
            return "s"
        else:
            assert False, "Unknown type for: " + str(val)

if __name__ == "__main__":
    print(Types.getType(1.0))
    print(Types.getType(1))
    print(Types.getType("1.0"))
    print(Types.getType("02/06/1998"))

    #print(Types.isDate("01/june/1988"))


import threading
from inprocess_queue_reader_writer import InProcessReaderWriter
from aws_sqs_reader import AwsQueueReader

def start_simulation():
    print("start simulation")

    reader_writer1 = InProcessReaderWriter("reader1")
    reader_writer2 = InProcessReaderWriter("reader2")

    threading.Thread(target=reader_writer1.read_queue_messages, daemon=True).start()
    threading.Thread(target=reader_writer2.read_queue_messages, daemon=True).start()

    # send thirty task requests to the workers
    for item in range(30):
        reader_writer1.put_message(str(item))
    print('All task requests sent\n', end='')

    # block until all tasks are done
    InProcessReaderWriter.queue.join()
    print('All work completed')

def start_aws_queue_reader():

    aws_reader = AwsQueueReader()

    aws_reader.read_queue_messages()

if __name__ == '__main__':
    #start_simulation()

    start_aws_queue_reader()