class Solution:
    def findRotateSteps(self, ring: str, key: str) -> int:
        n, Q, visited = len(ring), deque(), set()
        Q.append([0,0,0]) #[node, rotations, current_index]

        while Q:
            i, rotations, current = Q.popleft()
            # If we have seen the current state already previously, we just avoid reworking on it.
            if (i, current) in visited:
                continue
            visited.add((i,current))
            # IF the current alligned character is equal to key we increment current index until duplicate adjacent characters are there
            # Think of any key where adjacent characters are equal like "aaaaabbbcde", so if we are already alligned at "a", we don't need to rotate
            while current < len(key) and key[current] == ring[i]:
                current += 1
            # BREAK - CONDITION
            # The the key is always present i.e an answer always exists
            # if we have found all the characters of key and the current index becomes equal to length of key
            if current == len(key):
                # We also need to "press the centre" after the character is alligned, so there would always be clicks equal to length of key
                clicks = len(key)
                return rotations + clicks
            # Appending next adjacent rotations based on index i.e. if index is 0 then the next level in BFS would be index 1 and last-index
            # for last index the next level in BFS would be previous index and the 0th index
            # else if the current index is somewhere in between we would check the left and right index
            if i == 0:
                Q.append([i+1, rotations + 1, current])
                Q.append([n-1, rotations + 1, current])
            elif i == n-1:
                Q.append([i-1, rotations + 1, current])
                Q.append([0, rotations + 1, current])
            else:
                Q.append([i+1, rotations + 1, current])
                Q.append([i-1, rotations + 1, current])


import argparse
import numpy as np
import random
import os, sys
import time
import matplotlib
matplotlib.use('agg')  # use matplotlib without GUI support
import matplotlib.pyplot as plt

import torch
from torch.utils.data import DataLoader
import torch.optim as optim

sys.path.append('./auxiliary/')
from auxiliary.model import PoseEstimator
from auxiliary.dataset import Pascal3D
from auxiliary.utils import AverageValueMeter, load_checkpoint, KaiMingInit, rotation_acc, get_pred_from_cls_output
from auxiliary.loss import DeltaLoss, CELoss, SmoothCELoss
from evaluation import val

# =================PARAMETERS=============================== #
parser = argparse.ArgumentParser()

## training param
parser.add_argument('--resume', type=str, default=None, help='optional resume model path')
parser.add_argument('--save_dir', type=str, default='save_models', help='save directory')
parser.add_argument('--lr', type=float, default=0.0001, help='learning rate of optimizer')
parser.add_argument('--batch_size', type=int, default=16, help='input batch size')
parser.add_argument('--workers', type=int, help='number of data loading workers', default=4)
parser.add_argument('--n_epoch', type=int, default=200, help='number of epochs to train for')
parser.add_argument('--lr_step', type=int, default=200, help='step to decrease lr')
parser.add_argument('--print_freq', type=int, default=100, help='frequency of output print')

## dataset param
parser.add_argument('--setting', type=str, default=None, choices=['IntraDataset', 'InterDataset'])
parser.add_argument('--root_dir_train', type=str, default=None, help='training dataset directory')
parser.add_argument('--annot_train', type=str, default=None, help='training dataset annotation file')
parser.add_argument('--novel', action='store_true', help='whether to exclude novel classes during training')
parser.add_argument('--keypoint', action='store_true', help='use only samples with keypoint annotations')
parser.add_argument('--shot', type=int, default=None, help='few shot number')

## method param
parser.add_argument('--input_dim', type=int, default=224, help='input image dimension')
parser.add_argument('--img_feature_dim', type=int, default=512, help='feature dimension for images')
parser.add_argument('--point_num', type=int, default=2500, help='number of points used in each sample')
parser.add_argument('--shape_feature_dim', type=int, default=512, help='feature dimension for shapes')
parser.add_argument('--bin_size', type=int, default=15, help='bin size for the euler angle classification')
parser.add_argument('--smooth', type=float, default=0.2, help='activate label smoothing in classification')

opt = parser.parse_args()
print(opt)
# ========================================================== #

# =================CREATE DATASET=========================== #
if opt.novel:
    print('Excluding novel classes in training')
else:
    print('Including novel classes in training')

# 20 novel classes on ObjectNet3D
if opt.setting == 'IntraDataset':
    test_cats = ['bed', 'bookshelf', 'calculator', 'cellphone', 'computer', 'door', 'filing_cabinet', 'guitar', 'iron',
                 'knife', 'microwave', 'pen', 'pot', 'rifle', 'shoe', 'slipper', 'stove', 'toilet', 'tub', 'wheelchair']
    dataset_train = Pascal3D(train=True, root_dir=opt.root_dir_train, annotation_file=opt.annot_train,
                             cls_choice=test_cats, input_dim=opt.input_dim, point_num=opt.point_num,
                             keypoint=opt.keypoint, novel=opt.novel, shot=opt.shot)

# 12 novel classes on Pascal3D
elif opt.setting == 'InterDataset':
    test_cats = ['aeroplane', 'bicycle', 'boat', 'bottle', 'bus', 'car',
                 'chair', 'diningtable', 'motorbike', 'sofa', 'train', 'tvmonitor']
    dataset_train = Pascal3D(train=True, root_dir=opt.root_dir_train, annotation_file=opt.annot_train,
                             cls_choice=test_cats, input_dim=opt.input_dim, point_num=opt.point_num,
                             keypoint=opt.keypoint, novel=opt.novel, shot=opt.shot)
else:
    sys.exit('Wrong setting!')

train_loader = DataLoader(dataset_train, batch_size=opt.batch_size, shuffle=True, num_workers=opt.workers, drop_last=True)

print('train data consist of {} samples'.format(len(dataset_train)))
# ========================================================== #

# ================CREATE NETWORK============================ #
azi_classes, ele_classes, inp_classes = int(360 / opt.bin_size), int(180 / opt.bin_size), int(360 / opt.bin_size)

model = PoseEstimator(shape_feature_dim=opt.shape_feature_dim, img_feature_dim=opt.img_feature_dim,
                      azi_classes=azi_classes, ele_classes=ele_classes, inp_classes=inp_classes)
model.cuda()
if opt.resume is not None:
    load_checkpoint(model, opt.resume)
else:
    model.apply(KaiMingInit)
# ========================================================== #

# ================CREATE OPTIMIZER AND LOSS================= #
optimizer = optim.Adam(filter(lambda p: p.requires_grad, model.parameters()), lr=opt.lr, weight_decay=5e-4)
lrScheduler = optim.lr_scheduler.MultiStepLR(optimizer, [opt.lr_step], gamma=0.1)

criterion_azi = SmoothCELoss(360, 24, opt.smooth) if opt.smooth is not None else CELoss(360)
criterion_ele = SmoothCELoss(180, 12, opt.smooth) if opt.smooth is not None else CELoss(180)
criterion_inp = SmoothCELoss(360, 24, opt.smooth) if opt.smooth is not None else CELoss(360)
criterion_reg = DeltaLoss(opt.bin_size)
# ========================================================== #

# =============DEFINE stuff for logs ======================= #
result_path = os.path.join(os.getcwd(), opt.save_dir)
if not os.path.exists(result_path):
    os.makedirs(result_path)
logname = os.path.join(result_path, 'training_log.txt')
with open(logname, 'a') as f:
    f.write(str(opt) + '\n' + '\n')
    f.write('training set: ' + str(len(dataset_train)) + '\n')
    # f.write('evaluation set: ' + str(len(dataset_eval)) + '\n')

# arrays for saving the losses and accuracies
losses = np.zeros((opt.n_epoch, 2))  # training loss and validation loss
accuracies = np.zeros((opt.n_epoch, 2))  # train and val accuracy for classification and viewpoint estimation
# ========================================================== #

# =================== DEFINE TRAIN ========================= #
def train(data_loader, model, bin_size, criterion_azi, criterion_ele, criterion_inp, criterion_reg, optimizer):
    train_loss = AverageValueMeter()
    train_acc_rot = AverageValueMeter()

    model.train()

    data_time = AverageValueMeter()
    batch_time = AverageValueMeter()
    end = time.time()
    for i, data in enumerate(data_loader):
        # load data and label
        im, shapes, label, _ = data
        im, shapes, label = im.cuda(), shapes.cuda(), label.cuda()
        data_time.update(time.time() - end)

        # forward pass
        out = model(im, shapes)

        # compute losses and update the meters
        loss_azi = criterion_azi(out[0], label[:, 0])
        loss_ele = criterion_ele(out[1], label[:, 1])
        loss_inp = criterion_inp(out[2], label[:, 2])
        loss_reg = criterion_reg(out[3], out[4], out[5], label.float())
        loss = loss_azi + loss_ele + loss_inp + loss_reg
        train_loss.update(loss.item(), im.size(0))

        # compute rotation matrix accuracy
        preds = get_pred_from_cls_output([out[0], out[1], out[2]])
        for n in range(len(preds)):
            pred_delta = out[n + 3]
            delta_value = pred_delta[torch.arange(pred_delta.size(0)), preds[n].long()].tanh() / 2
            preds[n] = (preds[n].float() + delta_value + 0.5) * bin_size
        acc_rot = rotation_acc(torch.cat((preds[0].unsqueeze(1), preds[1].unsqueeze(1), preds[2].unsqueeze(1)), 1),
                               label.float())
        train_acc_rot.update(acc_rot.item(), im.size(0))

        # compute gradient and do SGD step
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        # measure bacth time
        batch_time.update(time.time() - end)
        end = time.time()

        if (i + 1) % opt.print_freq == 0:
            print("\tEpoch %3d --- Iter [%d/%d] Train loss: %.2f || Train accuracy: %.2f" %
                  (epoch, i + 1, len(data_loader), train_loss.avg, train_acc_rot.avg))
            print("\tData loading time: %.2f (%.2f)-- Batch time: %.2f (%.2f)\n" %
                  (data_time.val, data_time.avg, batch_time.val, batch_time.avg))

    return [train_loss.avg, train_acc_rot.avg]
# ========================================================== #

# =============BEGIN OF THE LEARNING LOOP=================== #
# initialization
best_acc = 0.

for epoch in range(opt.n_epoch):
    # update learning rate
    lrScheduler.step()

    # train
    train_loss, train_acc_rot = train(train_loader, model, opt.bin_size,
                                      criterion_azi, criterion_ele, criterion_inp, criterion_reg, optimizer)

    # save checkpoint
    torch.save({'epoch': epoch,
                'model': model.state_dict(),
                'optimizer': optimizer.state_dict(),
                'losses': losses},
               os.path.join(result_path, 'checkpoint.pth'))

    # save losses and accuracies into log file
    with open(logname, 'a') as f:
        text = str('Epoch: %03d || train_loss %.2f || train_acc %.2f \n \n' % (epoch, train_loss, train_acc_rot))
        f.write(text)
# ========================================================== #

import pickle
import collections

data_loader = DataLoader(dataset_train, batch_size=1, shuffle=False, num_workers=1)

with torch.no_grad():
    model.eval()

    class_data = collections.defaultdict(list)
    for i, data in enumerate(data_loader):
        # load data and label
        im, shapes, label, cls = data
        im, shapes = im.cuda(), shapes.cuda()

        cls_data = model(im, shapes, get_mean_cls_data=True)

        class_data[cls[0]].append(cls_data.squeeze())

# calculate mean attention vectors of every class
mean_class_data = {k: sum(v) / len(v) for k, v in class_data.items()}

with open(os.path.join(result_path, 'mean_class_data.pkl'), 'wb') as f:
    pickle.dump(mean_class_data, f, pickle.HIGHEST_PROTOCOL)
print('save mean class data done!')


class node:
    def __init__(self, val, head=None):
        self.val = val
        self.head = head

    def __str__(self):
        return str(self.val)

class singly:
    def __init__(self, front=None):
        self.front = front

    def add(self, n):
        end = self.last()
        if not end:
            self.front = n
        else:
            end.head = n

    def last(self):
        tmp = self.front

        if not tmp:
            return None

        while tmp.head:
            tmp = tmp.head

        return tmp

    def reverse(self):
        prev = self.front
        cur = prev.head
        prev.head = None

        while cur:
            tmp = cur.head
            cur.head = prev
            prev = cur
            cur = tmp

        self.front = prev

    def reverse_words(self):
        return 0

    def __str__(self):
        res = []
        tmp = self.front

        while tmp:
            res.append(tmp)
            tmp = tmp.head

        return str(map(str, res))

if __name__ == "__main__":
    a = singly(node(1, node(2, node(3, node(4)))))
    print(a)
    a.reverse()
    print(a)
    b = singly()
    b.add(node("a"))
    b.add(node("b"))
    b.add(node("c"))
    b.add(node(" "))
    b.add(node("d"))
    b.add(node("e"))
    print(b)


'''
Idea:
    QuickSort(A)
        if len(A) <= 1
            return

        Pick some x = A[i] at random - pivot
        Partition the rest of A into
            L (less than x)
            R (more than x)
        Replace A with [L, x, R]
        QuickSort(L)
        QuickSort(R)

Pseudo Code:

function QuickSort(A)
    quickSortHelper(A, 0, len(A)-1)

function quickSortHelper(A, start, end)
    if start >= end
        return

    // Pick a random index for choosing pivot
    randindex = randominteger(start, end)
    swap(A[randindex], A[start])

    // Lomuto's Partitioning: Time = O(n), Space = in-place
    // - Place Pivot at A[0]
    // - Scan from Left to Right
    // - Move bigger(orange) index one at a time
    // - Move smaller(green) index only when expand green sub-array
    // - End with placing Pivot to right-most of green sub-array since Pivot @ A[0]
    pivot = A[start]
    smaller = start
    bigger = start

    for bigger in (start+1) to end:
        if A[bigger] < pivot:
            smaller++
            swap(A[smaller], A[bigger])

    // Pivot at  A[0], swap to the largest smaller sub-array
    swap(A[start], A[smaller])
    // End of Lomuto's Partitioning

    quickSortHelper(A, start, smaller-1)
    quickSortHelper(A, smaller+1, end)

Notion:
    Orange region: vals < pivot --> smaller
    Green region: vals > pivot --> bigger
    Orange index: last check and expected to be < pivot
        Hence, if > pivot, waiting for swapping
    Green index: last check and expected to be > pivot
        Hence, if < pivot, waiting for swapping
Hoare's Partitioning: Time = O(n), Space = in-place, but less ...
1. Orange index starts at left-most - 1
2. Green index starts at right-most + 1
3. Orange region grows from left to right as long as cval is < pivot
   - stop and wait if cval is > pivot
4. Green region green from right to left as long as cval is > pivot
   - stop and wait if cval is < pivot
5. With 2 stops from 3 and 4 AND oIdx < gIdx, swap them to create contiguous orange, green
   - if 2 stops from 3 and 4 BUT orange Index > green Index, stop!
6. if orange index < green index ==> not crossing each other yet
      Goto step 3,4,5,6 again
   else // orange index > green index ==> crossing

Thawee's note: Need to identify the only needed place for index crossing chk

Next swap(Pivot, max(Orange))
      Done!
'''

'''
function quickSortHelper(A, start, end)
    if start >= end
        return

    // Pick a random index for choosing pivot
    randindex = randominteger(start, end)
    swap(A[randindex], A[start])

    // Lomuto's Partitioning: Time = O(n), Space = in-place
    // - Place Pivot at A[0]
    // - Scan from Left to Right
    // - Move bigger(orange) index one at a time
    // - Move smaller(green) index only when expand green sub-array
    // - End with placing Pivot to right-most of green sub-array since Pivot @ A[0]
    pivot = A[start]
    smaller = start
    bigger = start

    for bigger in (start+1) to end:
        if A[bigger] < pivot:
            smaller++
            swap(A[smaller], A[bigger])

    // Pivot at  A[0], swap to the largest smaller sub-array
    swap(A[start], A[smaller])
    // End of Lomuto's Partitioning

    quickSortHelper(A, start, smaller-1)
    quickSortHelper(A, smaller+1, end)
'''
import math
import random

def quickSortHelper(A, start, end):
    #print("A=", A[start:end])
    if (start >= end):
        return

    rIdx = random.randint(start, end)
    #print("rIdx=", rIdx)
    A[rIdx], A[start] = A[start], A[rIdx]

    pivot = A[start]
    #print("A=",A[start,end], "pivot=",pivot)
    oIdx = start
    gIdx = start

    for gIdx in range(start+1, end+1):
        if (A[gIdx] < pivot):
            oIdx += 1
            A[oIdx], A[gIdx] = A[gIdx], A[oIdx]
    A[start], A[oIdx] = A[oIdx], A[start]

    quickSortHelper(A, 0, oIdx-1)
    quickSortHelper(A, oIdx+1, end)

def QuickSort(A):
    quickSortHelper(A, 0, len(A)-1)

I = [ 3 ,1, 7, 2, 10]
print(I)
#import pdb; pdb.set_trace()
QuickSort(I)
print(I)

X = [ 30, 5, 39, 4, 2, 10, 3, 9, 8, 7]
print(X)
#import pdb; pdb.set_trace()
QuickSort(X)
print(X)

Y = [ 1, 2, 3, 4, 5]
print(Y)
QuickSort(Y)
print(Y)


from libavg import avg, app, player

SIZE = avg.Bitmap('rgb24-64x64.png').getSize()

class MainDiv(app.MainDiv):
    MIN_DRAW_DIST = 10.0 # minimal length of draw line segments

    def onInit(self):
        # white background
        avg.RectNode(parent=self, size=self.size, fillopacity=1.0)
        # foreground image
        self._fg = avg.ImageNode(parent=self, href='rgb24-64x64.png',
                size=self.size, masksize=self.size)

        # mask drawing canvas
        self._maskCanvas = player.createCanvas(id='mask', size=self.size,
                handleevents=False, autorender=False)
        # image to put the canvas on-screen (invisible)
        self._maskImage = avg.ImageNode(parent=self, href='canvas:mask',
                opacity=0.0)
        # black mask on start (full transparency)
        avg.RectNode(parent=self._maskCanvas.getRootNode(), size=self.size,
                color='000000', fillcolor='000000', fillopacity=1.0)
        # poly line to draw white on black mask (adding opacity)
        self._drawLine = None

        self._updateMask()

        self._maskImage.subscribe(self._maskImage.CURSOR_DOWN, self._onDown)
        self._maskImage.subscribe(self._maskImage.CURSOR_MOTION, self._onMotion)
        self._maskImage.subscribe(self._maskImage.CURSOR_UP, self._onUp)

    def _onDown(self, event):
        self._drawLine = avg.PolyLineNode(parent=self._maskCanvas.getRootNode(),
                pos=[event.pos], color='FFFFFF', strokewidth=42.0, opacity=0.25)

    def _onMotion(self, event):
        if not self._drawLine:
            return
        pos = self._drawLine.pos
        if (pos[-1] - event.pos).getNorm() < MainDiv.MIN_DRAW_DIST:
            return
        pos.append(event.pos)
        self._drawLine.pos = pos
        self._updateMask()

    def _onUp(self, _):
        self._drawLine = None

    def _updateMask(self):
        self._maskCanvas.render()
        self._fg.setMaskBitmap(self._maskCanvas.screenshot())

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


from score.dimensions.dimension import Dimension
from score.types_ import TranslationsDict
from score.constants import FileTypes

import re as regex

PROPOSED, SOLUTION = FileTypes

class StandardFieldNaming(Dimension):
  """
  Quantifies whether the correct standard field names
  (e.g. "chilled_water_flowrate_sensor")
  were selected in the proposed file.
  """
  def __init__(self, *, translations: TranslationsDict):
    super().__init__(translations=translations)

    correct_subfields = []
    correct_ceiling: int = 0
    incorrect_subfields = []

    for s_field, s_value in translations[SOLUTION]:
      s_subfields = set(
          filter(lambda subfield: not bool(regex.match('[0-9]+', subfield)),
                 s_field.split('_')))
      correct_ceiling += len(s_subfields)

      for p_field, p_value in translations[PROPOSED]:
        if p_value.raw_field_name == s_value.raw_field_name:
          p_subfields = set(
              filter(lambda subfield: not bool(regex.match('[0-9]+', subfield)),
                     p_field.split('_')))

          correct_subfields += p_subfields.intersection(s_subfields)
          incorrect_subfields += p_subfields.difference(s_subfields)

    self.correct_reporting = len(correct_subfields)
    self.correct_ceiling_reporting = correct_ceiling
    self.incorrect_reporting = len(incorrect_subfields)


class Queue:
    def __init__(self, queue_size: int = 10):
        self.arr        = [0] * queue_size
        self.next_index = 0
        self.front_index = -1
        self.queue_size = 0

    def enqueue(self, value):
        if self.queue_size == len(self.arr):
            # do something
            self._handle_queue_capacity_full()

        self.arr[self.next_index] = value
        if self.front_index < 0:
            self.front_index = 0

        self.next_index = (self.next_index+1) % len(self.arr)
        self.queue_size +=1

    def dequeue(self):
        if self.is_empty():
            return None

        value = self.front()

        self.front_index = (self.front_index + 1) % len(self.arr)
        self.queue_size -=1

        if self.is_empty():
            self.front_index    = -1
            self.next_index     = 0

        return value

    def _handle_queue_capacity_full(self):
        old_arr = self.arr
        self.arr = [0] * (len(self.arr) * 2)

        self.next_index = (self.next_index - 1) % len(old_arr) # point to last value
        pointer = 0         # index of new array

        while self.front_index < self.next_index:
            self.arr[pointer] = old_arr[self.front_index]
            self.front_index = (self.front_index + 1) % len(old_arr)
            pointer +=1

        # add last value
        self.arr[pointer] = old_arr[self.next_index]
        pointer +=1

        # reseating
        self.next_index = pointer
        self.front_index = 0

    def size(self):
        return self.queue_size

    def is_empty(self):
        return self.queue_size == 0

    def front(self):
        return self.arr[self.front_index] if not self.is_empty() else None

q = Queue()

print('queue', q.arr)
print('is empty', q.is_empty())
print('front ', q.front())
q.enqueue(10)
q.enqueue(20)
q.enqueue(40)
q.enqueue(50)
q.enqueue(60)
q.enqueue(70)
q.enqueue(80)
q.enqueue(90)
q.enqueue(100)
print('queue', q.arr)
print('is empty', q.is_empty())
print('front ', q.front())
q.enqueue(110)
print('queue', q.arr)
print('is empty', q.is_empty())
print('front ', q.front())
q.enqueue(120)
print('queue', q.arr)
print('is empty', q.is_empty())
print('front ', q.front())


import json

def read_json(file):
    jf=open(file)
    read_jf=jf.read()
    jf.close()
    json_data=json.loads(read_jf)
    return json_data

def write_json(file,data):
    jw=open(file,"w")
    write_jf = json.dumps(data, indent=4)
    jw.write(write_jf)

def delete_data(file,id):
    print(file,id)
    json_data=read_json(file)
    is_deleted=False
    for data in json_data['Task_list']:
        if(data['id']==id):
            json_data['Task_list'].remove(data)
            is_deleted=True
    if (is_deleted):
        for no in range(len(json_data['Task_list'])):
            json_data['Task_list'][no]['id']=no+1
    write_json(file,json_data)

def update_data(file,id):
    print(file,id)
    json_data=read_json(file)
    for data in json_data['Task_list']:
        if(data['id']==id):
            data['task_details'] = 'Done'
    write_json(file,json_data)


import pytest
import torch

from hysteresis.meshing import create_triangle_mesh
from hysteresis.states import (
    get_states,
    switch,
    sweep_up,
    sweep_left,
    predict_batched_state,
)

class TestStateCalc:
    def test_grad(self):
        h = torch.tensor(1.0, requires_grad=True)
        dummy = h * 4.0
        dummy.backward()

    def test_switches(self):
        mesh = torch.tensor(create_triangle_mesh(1.0))
        h = torch.ones(1, requires_grad=True)
        flip_flag = switch(h, mesh[:, 1])

        flip_flag[0].backward()
        assert h.grad is not None

    def test_sweeps(self):
        mesh = torch.tensor(create_triangle_mesh(0.1))
        h = torch.tensor(0.5, requires_grad=True)
        initial_state = torch.ones_like(mesh[:, 1]) * -1.0
        second_state = sweep_up(h, mesh, initial_state)

        h2 = torch.tensor(0.75, requires_grad=True)
        thrid_state = sweep_up(h2, mesh, second_state)

        h3 = torch.tensor(0.4, requires_grad=True)
        final_state = sweep_left(h3, mesh, thrid_state)

        # final_state[0].backward()
        total = torch.sum(final_state)
        total.backward()
        assert h.grad is not None
        assert h2.grad is not None
        assert h3.grad is not None
        assert not torch.isnan(h.grad)
        assert not torch.isnan(h2.grad)
        assert not torch.isnan(h3.grad)

    def test_get_states(self):
        mesh = torch.tensor(create_triangle_mesh(0.1))
        h = torch.tensor((0.5, 0.75, 0.4, 0.5), requires_grad=True)
        states = get_states(h, mesh)

        total = torch.sum(states[-1])
        total.backward()
        assert not torch.any(torch.isnan(h.grad))

        # plt.tripcolor(*mesh.T, states[-1].detach())
        # plt.show()

    def test_error_handling(self):
        mesh = torch.tensor(create_triangle_mesh(0.1))
        h = torch.tensor((0.5, 0.75, 0.4, 0.5, 1.0, 0.0))
        states = get_states(h, mesh_points=mesh)

        h = torch.tensor((0.5, 1.75, 0.4, 0.5))
        with pytest.raises(RuntimeError):
            states = get_states(h, mesh_points=mesh)

        h = torch.tensor((-0.5, 0.75, 0.4, 0.5))
        with pytest.raises(RuntimeError):
            states = get_states(h, mesh_points=mesh)

    def test_batched(self):
        mesh = torch.tensor(create_triangle_mesh(0.5))
        current_state = torch.ones(mesh.shape[0]) * -1.0
        current_field = torch.tensor(-0.01)
        h = torch.linspace(0.0, 1.0, 20, requires_grad=True)
        out = predict_batched_state(
            h.reshape(20, 1), mesh, current_state, current_field
        )
        assert out.shape == torch.Size([20, 1, len(mesh)])
        out[0][0][0].backward()
        assert not torch.any(torch.isnan(h.grad))

        # test different sized tensors for h - requires last dim to be 1
        tests = [torch.rand(10, 20), torch.rand(10), torch.rand(2, 4, 5)]
        for t in tests:
            out = predict_batched_state(t, mesh, current_state, current_field)
            assert out.shape == torch.Size([*t.shape, len(mesh)])


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

from scrapy.contrib.spiders import CrawlSpider, Rule
from scrapy.contrib.linkextractors.sgml import SgmlLinkExtractor
from scrapy.selector import HtmlXPathSelector
from cwcraigs.items import CraigslistSampleItem

class CraigsSpider(CrawlSpider):
    name = "craigs"
    allowed_domains = ["sfbay.craigslist.org"]
    start_urls = ["http://sfbay.craigslist.org/search/vnn"]

    # the below is next page url
    # 'http://sfbay.craigslist.org/search/npo?s=100'
    # 'http://sfbay.craigslist.org/search/npo?s=200'

    rules = (
        # Rule(SgmlLinkExtractor(allow=(), restrict_xpaths=('//div[@class="pageNumbers"]/a',)), callback="parse_items", follow= True),
        Rule(SgmlLinkExtractor(allow=(), restrict_xpaths=('//span[@class="buttons"]/a',)), callback="parse_items",
             follow=True),
    )

    def __init__(self, name=None, **kwargs):
        from cwcraigs.DBUtils import DBUtils
        self.dbutils = DBUtils(kwargs['mongo_uri'], kwargs['mongo_db'], kwargs['collection_name'])
        self.dbutils.open_spider()
        super(CraigsSpider, self).__init__(name, **kwargs)

    @classmethod
    def from_crawler(cls, crawler, *args, **kwargs):
        return super(CraigsSpider, cls).from_crawler(crawler,
                                                         args,
                                                         mongo_uri=crawler.settings.get('MONGODB_SERVER'),
                                                         mongo_db=crawler.settings.get('MONGODB_DB', 'items'),
                                                         collection_name=(
                                                             "__history" + crawler.settings.get('MONGODB_COLLECTION'))
                                                         )

    def parse_items(self, response):
        hxs = HtmlXPathSelector(response)
        titles = hxs.xpath('//span[@class="pl"]')
        items = []

        count = 0
        for title in titles:
            item = CraigslistSampleItem()
            item["url"] = (response.url+"-{0}").format(count)
            item["title"] = title.xpath('a/span/text()').extract()
            item["link"] = title.xpath("a/@href").extract()
            items.append(item)

            count += 1
        return(items)


import pickle
import argparse
import os
from omni.isaac.kit import SimulationApp

# add argparse arguments
parser = argparse.ArgumentParser("Welcome to Orbit: Omniverse Robotics Environments!")
parser.add_argument("--headless", action="store_true", default=False, help="Force display off at all times.")
parser.add_argument("--num_envs", type=int, default=3, help="Number of environments to spawn.")
args_cli = parser.parse_args()

# launch omniverse app
config = {"headless": args_cli.headless}
simulation_app = SimulationApp(config)

"""Rest everything follows."""
from PIL import Image as im
# import os
import torch
import cv2
import numpy as np
import open3d as o3d
import random
from scipy import signal
from omni.isaac.core.prims import RigidPrim,GeometryPrim
from omni.isaac.orbit.utils.array import convert_to_torch
import omni.isaac.core.utils.prims as prim_utils
from omni.isaac.core.simulation_context import SimulationContext
from omni.isaac.core.utils.carb import set_carb_setting
from omni.isaac.core.utils.viewports import set_camera_view
from omni.isaac.orbit.utils.assets import ISAAC_NUCLEUS_DIR
import omni.isaac.orbit.utils.kit as kit_utils
from omni.isaac.orbit.robots.config.franka import FRANKA_PANDA_ARM_WITH_PANDA_HAND_CFG
from omni.isaac.orbit.robots.single_arm import SingleArmManipulator
from omni.isaac.orbit.sensors.camera import Camera, PinholeCameraCfg
from omni.isaac.orbit.sensors.camera.utils import create_pointcloud_from_rgbd
from omni.isaac.core.objects import DynamicCuboid, FixedCuboid
from omni.isaac.orbit.objects import RigidObjectCfg,RigidObject
from PIL import Image
import matplotlib.pyplot as plt
from omni.isaac.orbit.controllers.differential_inverse_kinematics import (
    DifferentialInverseKinematics,
    DifferentialInverseKinematicsCfg,
)
import asyncio
from omni.isaac.orbit.objects.rigid import RigidObject, RigidObjectCfg
from omni.isaac.orbit.utils.math import convert_quat
import scipy.spatial.transform as tf
from shapely import Polygon, STRtree, area, contains
from omni.isaac.orbit_envs.isaac_env_cfg import EnvCfg, IsaacEnvCfg, PhysxCfg, SimCfg, ViewerCfg

import carb
#################
from place_new_obj import place_new_obj_fun
"""
Main
"""
class sim_cfg:
    sim: SimCfg = SimCfg(
            dt=0.01,
            substeps=4,
            physx=PhysxCfg(
                # num_position_iterations=8,
                gpu_max_rigid_contact_count=1024**2, #1024**2*2,
                gpu_max_rigid_patch_count=160*2048*10, #160*2048*10, #160*2048*10,
                gpu_found_lost_pairs_capacity = 1024 * 1024 * 2 * 2,#1024 * 1024 * 2 * 1, #1024 * 1024 * 2 * 8,
                gpu_found_lost_aggregate_pairs_capacity=1024 * 1024 * 32 * 1,#1024 * 1024 * 32 * 1, #1024 * 1024 * 32,
                gpu_total_aggregate_pairs_capacity=1024 * 1024 * 2 *1, #1024 * 1024 * 2 *1, #1024 * 1024 * 2 * 8
                friction_correlation_distance=0.0025,
                friction_offset_threshold=0.04,
                bounce_threshold_velocity=0.5,
                gpu_max_num_partitions=8,

            ),

        )

def point_cloud_process(pcd):

    plane_model, inliers = pcd.segment_plane(distance_threshold=0.01,
                                         ransac_n=3,
                                         num_iterations=1000)
    outlier_cloud = pcd.select_by_index(inliers, invert=True)
    plane_model, inliers = outlier_cloud.segment_plane(distance_threshold=0.01,
                                         ransac_n=3,
                                         num_iterations=1000)
    print(plane_model)
    return plane_model
def _configure_simulation_flags( sim_params: dict = None):
        """Configure simulation flags and extensions at load and run time."""
        # acquire settings interface
        carb_settings_iface = carb.settings.get_settings()
        # enable hydra scene-graph instancing
        # note: this allows rendering of instanceable assets on the GUI
        carb_settings_iface.set_bool("/persistent/omnihydra/useSceneGraphInstancing", True)
        # change dispatcher to use the default dispatcher in PhysX SDK instead of carb tasking
        # note: dispatcher handles how threads are launched for multi-threaded physics
        carb_settings_iface.set_bool("/physics/physxDispatcher", True)
        # disable contact processing in omni.physx if requested
        # note: helpful when creating contact reporting over limited number of objects in the scene
        if sim_params["disable_contact_processing"]:
            carb_settings_iface.set_bool("/physics/disableContactProcessing", True)

def main():
    """Spawns lights in the stage and sets the camera view."""
    sim_params = sim_cfg.sim.to_dict()
    if sim_params is not None:
        if "physx" in sim_params:
            physx_params = sim_params.pop("physx")
            sim_params.update(physx_params)
    # set flags for simulator
    _configure_simulation_flags(sim_params)
    # create a simulation context to control the simulator
    sim = SimulationContext(
        stage_units_in_meters=1.0,
        physics_dt=0.01,
        rendering_dt=0.1,
        backend="torch",
        sim_params=sim_params,
        # physics_prim_path="/physicsScene",
        device='cuda:0',
    )
    # Load kit helper
    # sim = SimulationContext(physics_dt=0.01, rendering_dt=1, backend="torch",device='cuda:0',)
    # Set main camera
    set_camera_view([0, 2, 3.], [0.0, 0.0, 0])
    # Enable GPU pipeline and flatcache
    if sim.get_physics_context().use_gpu_pipeline:
        sim.get_physics_context().enable_flatcache(True)
    # PhysicsContext.set_gpu_total_aggregate_pairs_capacity(4000)
    # Enable hydra scene-graph instancing
    set_carb_setting(sim._settings, "/persistent/omnihydra/useSceneGraphInstancing", True)

    ####################################################
    ####################################################
    ################# SCENE ############################
    # Ground-plane
    kit_utils.create_ground_plane("/World/defaultGroundPlane",z_position=-0.5,
        static_friction=0.5,
        dynamic_friction=0.5,
        restitution=0.8,
        improve_patch_friction=True,)
    # Lights-1
    prim_utils.create_prim(
        "/World/Light/GreySphere",
        "SphereLight",
        translation=(4.5, 3.5, 10.0),
        attributes={"radius": 2.5, "intensity": 600.0, "color": (0.75, 0.75, 0.75)},
    )
    # Lights-2
    prim_utils.create_prim(
        "/World/Light/WhiteSphere",
        "SphereLight",
        translation=(-4.5, 3.5, 10.0),
        attributes={"radius": 2.5, "intensity": 600.0, "color": (1.0, 1.0, 1.0)},
    )
    #################### create table
    table_path = f"{ISAAC_NUCLEUS_DIR}/Props/Shapes/cube.usd"
    Table = FixedCuboid(prim_path="/World/Table",position=(0,0,-0.25),scale=(0.5,0.5,0.5))
    # Table.set_mass(10000000)
    sideTable = FixedCuboid(prim_path="/World/sideTable",position=(0.35,-0.9,-0.3),scale=(0.4,0.4,0.4))
    # sideTable.set_mass(10)
    #################### robot base
    prim_utils.create_prim("/World/Robotbase", usd_path=table_path,position=(0,-0.45,-0.2),scale=(0.3,0.26,0.4))
    #################### ycb path
    ######################################### load ycb objects
    # ycb_usd_paths = {
    #     "crackerBox": f"{ISAAC_NUCLEUS_DIR}/Props/YCB/Axis_Aligned_Physics/003_cracker_box.usd",
    #     "sugarBox": f"{ISAAC_NUCLEUS_DIR}/Props/YCB/Axis_Aligned_Physics/004_sugar_box.usd",
    #     "tomatoSoupCan": f"{ISAAC_NUCLEUS_DIR}/Props/YCB/Axis_Aligned_Physics/005_tomato_soup_can.usd",
    #     "mustardBottle": f"{ISAAC_NUCLEUS_DIR}/Props/YCB/Axis_Aligned_Physics/006_mustard_bottle.usd",
    #     "mug":f"{ISAAC_NUCLEUS_DIR}/Props/YCB/Axis_Aligned/025_mug.usd",
    #     "largeMarker":f"{ISAAC_NUCLEUS_DIR}/Props/YCB/Axis_Aligned/040_large_marker.usd",
    #     "tunaFishCan":f"{ISAAC_NUCLEUS_DIR}/Props/YCB/Axis_Aligned/007_tuna_fish_can.usd",
    #     "banana":f"{ISAAC_NUCLEUS_DIR}/Props/YCB/Axis_Aligned/011_banana.usd",
    #     # "pitcherBase":f"{ISAAC_NUCLEUS_DIR}/Props/YCB/Axis_Aligned/019_pitcher_base.usd",
    #     "bowl":f"{ISAAC_NUCLEUS_DIR}/Props/YCB/Axis_Aligned/024_bowl.usd",
    #     "largeClamp":f"{ISAAC_NUCLEUS_DIR}/Props/YCB/Axis_Aligned/051_large_clamp.usd",
    #     "scissors":f"{ISAAC_NUCLEUS_DIR}/Props/YCB/Axis_Aligned/037_scissors.usd",
    # }
    # ycb_name = ['crackerBox','sugarBox','tomatoSoupCan','mustardBottle','mug','largeMarker','tunaFishCan',
    #             'banana','bowl','largeClamp','scissors']
    # ycb_usd_paths = {
    #     "crackerBox": f"{ISAAC_NUCLEUS_DIR}/Props/YCB/Axis_Aligned_Physics/003_cracker_box.usd",
    #     "sugarBox": f"{ISAAC_NUCLEUS_DIR}/Props/YCB/Axis_Aligned_Physics/004_sugar_box.usd",
    #     # "tomatoSoupCan": f"{ISAAC_NUCLEUS_DIR}/Props/YCB/Axis_Aligned_Physics/005_tomato_soup_can.usd",
    #     "mustardBottle": f"{ISAAC_NUCLEUS_DIR}/Props/YCB/Axis_Aligned_Physics/006_mustard_bottle.usd",
    # }
    # ycb_name = ['crackerBox','sugarBox','mustardBottle']
    ycb_usd_paths = {
        # "crackerBox": f"{ISAAC_NUCLEUS_DIR}/Props/YCB/Axis_Aligned_Physics/003_cracker_box.usd",
        "sugarBox": f"{ISAAC_NUCLEUS_DIR}/Props/YCB/Axis_Aligned_Physics/004_sugar_box.usd",
        # "tomatoSoupCan": f"{ISAAC_NUCLEUS_DIR}/Props/YCB/Axis_Aligned_Physics/005_tomato_soup_can.usd",
        # "mustardBottle": f"{ISAAC_NUCLEUS_DIR}/Props/YCB/Axis_Aligned_Physics/006_mustard_bottle.usd",
    }
    ycb_name = ['sugarBox']
    obj1 = []
    obj_name_list = []
    for i in range(9):
        obj_cfg1 = RigidObjectCfg()
        obj_cfg1.meta_info = RigidObjectCfg.MetaInfoCfg(usd_path=ycb_usd_paths[ycb_name[0]],scale=(1.0, 1.0, 1.0),)
        obj_cfg1.init_state = RigidObjectCfg.InitialStateCfg(
        pos=(2-0.25*i, 0.8, -0.4), rot=(1.0, 0.0, 0.0, 0.0), lin_vel=(0.0, 0.0, 0.0), ang_vel=(0.0, 0.0, 0.0)
        )
        obj_cfg1.rigid_props = RigidObjectCfg.RigidBodyPropertiesCfg(
            solver_position_iteration_count=16,
            solver_velocity_iteration_count=1,
            max_angular_velocity=0.5,
            max_linear_velocity=0.5,
            max_depenetration_velocity=0.5,
            disable_gravity=False,
        )
        obj_cfg1.physics_material = RigidObjectCfg.PhysicsMaterialCfg(
            static_friction=0.5, dynamic_friction=0.5, restitution=0.0, prim_path="/World/Materials/cubeMaterial"
        )
        obj1.append(RigidObject(obj_cfg1))
        obj_name_list.append(ycb_name[0]+str(i))
    ################################ robot setting
    robot_cfg = FRANKA_PANDA_ARM_WITH_PANDA_HAND_CFG
    robot_cfg.data_info.enable_jacobian = True
    robot_cfg.rigid_props.disable_gravity = True
    robot = SingleArmManipulator(cfg=robot_cfg)
    robot.spawn("/World/Robot", translation=(0.0, -.45, 0))

    ###################################### controller
    ik_control_cfg = DifferentialInverseKinematicsCfg(
        command_type="pose_abs",
        ik_method="dls",
        position_offset=robot.cfg.ee_info.pos_offset,
        rotation_offset=robot.cfg.ee_info.rot_offset,
    )
    ik_controller = DifferentialInverseKinematics(cfg=ik_control_cfg, num_robots=1, device=sim.device)
    ###################################### spawn items
    for i,obj_t in enumerate(obj1):
        obj_t.spawn(f"/World/Objs/obj1/obj_{i}")
    ###################################### sensor extension camera

    camera_cfg = PinholeCameraCfg(
        sensor_tick=0,
        height=240,
        width=240,
        data_types=["rgb", "distance_to_image_plane", "normals", "motion_vectors"],
        usd_params=PinholeCameraCfg.UsdCameraCfg(
            focal_length=24.0, focus_distance=400.0, horizontal_aperture=20.955, clipping_range=(0.1, 1.0e5)
        ),
    )
    camera = Camera(cfg=camera_cfg, device="cuda")
    # hand_camera = Camera(cfg=camera_cfg,device='cuda')
    # hand_camera.spawn("/World/Robot/panda_hand/hand_camera", translation=(0.1, 0.0, 0.0),orientation=(0,0,1,0))
    # hand_camera.spawn("/World/hand_camera")
    # Spawn camera
    camera.spawn("/World/CameraSensor")
    sim.reset()
    ##########################################
    ##########################################
    for _ in range(14):
        sim.render()
    # hand_camera.initialize()
    camera.initialize()
    robot.initialize()
    ik_controller.initialize()
    for i,obj_t in enumerate(obj1):
            obj_t.initialize(f"/World/Objs/obj1/obj_{i}")
    # Reset states
    robot.reset_buffers()
    ik_controller.reset_idx()
    position = [0, 0, 1.2]
    orientation = [0, 0, -1, 0]
    camera.set_world_pose_ros(position, orientation)
    # hand_camera.set_world_pose_ros([0.35,-0.9,0.55], orientation)
    Table.initialize()
    sideTable.initialize()
    sideTable.set_collision_enabled(True)
    Table.set_collision_enabled(True)
    for i,obj_t in enumerate(obj1):
            obj_t.update_buffers(0.01)
    for _ in range(10):
        sim.render()
    ##################################################################### get plane model of the table
    camera.update(dt=0.0)
    pcd = get_pcd(camera)
    plane_model = point_cloud_process(pcd)
    plane_model_ori = plane_model
    plane_model = np.array([plane_model[0],plane_model[1],plane_model[2]])
    ##################################################################### load ycb
    obj_dict = dict()
    table_obj_pos_rot = dict()
    for i in range(1000):
        print('i')
        print(i)
        ## initialize parameters
        for _ in ycb_name:
            obj_dict[_] = 0
        table_obj_pos_rot = dict()
        ################## random drop one item
        randi = np.random.randint(0,len(ycb_name))
        obj_name_i = ycb_name[randi]
        angle = np.random.randint(0,180)
        translation = torch.rand(2).tolist()
        translation = [0.3*translation[0]-0.15,0.3*translation[1]-0.15,0.07]
        rot = convert_quat(tf.Rotation.from_euler("XYZ", (0,0,angle), degrees=True).as_quat(), to="wxyz")
        root_state = obj1[0].get_default_root_state()
        root_state[:,:3] = torch.tensor(translation).cuda()
        root_state[:,3:7] = torch.tensor(rot).cuda()
        obj1[0].set_root_state(root_state)
        obj1[0].update_buffers(0.01)
        table_obj_pos_rot[obj_name_i] = [(translation,rot)]
        for j in range(10):
            sim.step()
        obj_dict[obj_name_i] += 1
        #################### start the loop to drop item iteratively
        for j in range(1,9):
            print(j)
            if simulation_app.is_running():
                print('running')
            if sim.step(render=not args_cli.headless):
                print('pause')
                sim.play()
                continue
            camera.update(dt=0.01)
            for _ in range(10):
                sim.render()
            camera.update(dt=0.01)
            pcd = get_pcd(camera)
            pointcloud_w = np.array(pcd.points)
            select_m = np.dot(pointcloud_w,plane_model) + float(plane_model_ori[3])
            index_inliers = np.argwhere((select_m >=-0.01)).reshape(-1).astype(int)
            inliers = pointcloud_w[index_inliers]
            select_m = np.dot(inliers,plane_model) + float(plane_model_ori[3])
            index_inliers = np.argwhere((select_m <=0.3)).reshape(-1).astype(int)
            inliers = inliers[index_inliers]
            index_inliers = np.argwhere((inliers[:,1]>=-0.25)).reshape(-1).astype(int)
            inliers = inliers[index_inliers]
            index_inliers = np.argwhere((inliers[:,1]<=0.25)).reshape(-1).astype(int)
            inliers = inliers[index_inliers]
            index_inliers = np.argwhere((inliers[:,0]>=-0.25)).reshape(-1).astype(int)
            inliers = inliers[index_inliers]
            index_inliers = np.argwhere((inliers[:,0]<=0.25)).reshape(-1).astype(int)
            inliers = inliers[index_inliers]
            select_m = np.dot(inliers,plane_model) + float(plane_model_ori[3])
            index_objects = np.argwhere((select_m>=0.001)).reshape(-1).astype(int)
            objects_point = inliers[index_objects].copy()
            objects_pcd = o3d.geometry.PointCloud()
            objects_pcd.points = o3d.utility.Vector3dVector(objects_point)
            # o3d.visualization.draw_geometries([objects_pcd])
            pcd = o3d.geometry.PointCloud()
            pcd.points = o3d.utility.Vector3dVector(inliers)
            # o3d.visualization.draw_geometries([pcd])
            pts_tab = np.array(pcd.points)
            Nx,Ny = 50,50
            x = np.linspace(np.min(pts_tab[:,0]), np.max(pts_tab[:,0]), Nx)
            y = np.linspace(np.min(pts_tab[:,1]), np.max(pts_tab[:,1]), Ny)
            xv, yv = np.meshgrid(x, y)
            pts = np.array(objects_pcd.points)
            u = (pts[:,0] - np.min(pts_tab[:,0]))/ ( np.max(pts_tab[:,0])-np.min(pts_tab[:,0]) )
            v = (pts[:,1] - np.min(pts_tab[:,1]))/ ( np.max(pts_tab[:,1])-np.min(pts_tab[:,1]) )
            u = (Nx-1)*u
            v = (Ny-1)*v
            occupancy = np.zeros( (Ny,Nx) )
            u = u.astype(int)
            v = v.astype(int)
            u_ind = np.where(u<Nx)
            u = u[u_ind]
            v = v[u_ind]
            v_ind = np.where(v<Ny)
            u = u[v_ind]
            v = v[v_ind]
            u_ind = np.where(u>=0)
            u = u[u_ind]
            v = v[u_ind]
            v_ind = np.where(v>=0)
            u = u[v_ind]
            v = v[v_ind]
            occupancy[v,u] = 1
            occupancy = np.fliplr(occupancy)
            randi = np.random.randint(0,len(ycb_name))
            obj_name_i = ycb_name[randi]
            vertices_new_obj = get_new_obj_info(obj_name_i)
            flag_found, new_poly_vetices,occu_tmp,new_obj_pos = place_new_obj_fun(occupancy,vertices_new_obj)
            if flag_found:
                obj_dict[obj_name_i] += 1
                if obj_name_i in ["mug","tomatoSoupCan","pitcherBase","tunaFishCan","bowl","banana"]:
                    rot = convert_quat(tf.Rotation.from_euler("XYZ", (-90,np.rad2deg(new_obj_pos[2]),0), degrees=True).as_quat(), to="wxyz")
                else:
                    rot = convert_quat(tf.Rotation.from_euler("XYZ", (0,0,-np.rad2deg(new_obj_pos[2])), degrees=True).as_quat(), to="wxyz")
                print(new_obj_pos)
                translation = [(Nx/2-new_obj_pos[1])*1./100.,(new_obj_pos[0]-Ny/2)*1./100.,0.05]
                print(translation)
                root_state = obj1[j].get_default_root_state()
                root_state[:,:3] = torch.tensor(translation).cuda()
                root_state[:,3:7] = torch.tensor(rot).cuda()
                obj1[j].set_root_state(root_state)
                for k in range(250):
                    sim.step
                obj1[j].update_buffers(0.01)
                print('current state')
                print(obj1[j].data.root_pos_w)
                if obj_name_i not in table_obj_pos_rot:
                    table_obj_pos_rot[obj_name_i] = [(translation,rot)]
                else:
                    table_obj_pos_rot[obj_name_i].append((translation,rot))
                for k in range(250):
                    sim.step
                for k in range(20):
                    sim.render()
                print('current state')
                print(obj1[j].data.root_pos_w)
            else:
                file_name_ori = "dict_"
                file_list = os.listdir("generated_table/")
                print('done')
                print(table_obj_pos_rot)
                for k in table_obj_pos_rot:
                    print(len(table_obj_pos_rot[k]))
                num_file = 1
                while True:
                    file_name = file_name_ori+str(num_file)+".pkl"
                    if file_name in file_list:
                        num_file +=1
                    else:
                        file_path = "generated_table/"+file_name
                        f_save = open(file_path,'wb')
                        table_obj_pos_rot = [table_obj_pos_rot,obj_name_i]
                        pickle.dump(table_obj_pos_rot,f_save)
                        f_save.close()
                        break
                # f_save = open('generated_table/dict_file.pkl','wb')
                # pickle.dump(table_obj_pos_rot,f_save)
                # f_save.close()
                break
        #################### reset
        for i,obj_t in enumerate(obj1):
            root_state = obj_t.get_default_root_state()
            obj_t.set_root_state(root_state)
        for j in range(200):
            sim.step()

def get_pcd(camera):
    camera.update(dt=0.0)
    pointcloud_w, pointcloud_rgb = create_pointcloud_from_rgbd(
            camera.data.intrinsic_matrix,
            depth=camera.data.output["distance_to_image_plane"],
            rgb=camera.data.output["rgb"],
            position=camera.data.position,
            orientation=camera.data.orientation,
            normalize_rgb=True,
            num_channels=4,
    )
    if not isinstance(pointcloud_w, np.ndarray):
        pointcloud_w = pointcloud_w.cpu().numpy()
    pcd = o3d.geometry.PointCloud()
    pcd.points = o3d.utility.Vector3dVector(pointcloud_w)
    rgb=camera.data.output["rgb"]
    rgb = convert_to_torch(rgb, device='cuda:0', dtype=torch.float32)
    rgb = rgb[:, :, :3].cpu().data.numpy()

    img = Image.fromarray((rgb).astype(np.uint8))
    plt.imshow(img)
    plt.show()
    # o3d.visualization.draw_geometries([pcd])
    return pcd
def get_new_obj_info(obj_type):
# def get_new_obj_info(camera,size,hand_plane_model,obj_type):
    '''
    pcd = get_pcd(camera)
    # o3d.visualization.draw_geometries([pcd])
    plane_model, inliers = pcd.segment_plane(distance_threshold=0.01,
                                         ransac_n=3,
                                         num_iterations=1000)
    outlier_cloud = pcd.select_by_index(inliers, invert=True)
    plane_model, inliers = outlier_cloud.segment_plane(distance_threshold=0.005,
                                         ransac_n=3,
                                         num_iterations=1000)
    inlier_cloud = outlier_cloud.select_by_index(inliers)
    plane_model_ori = plane_model
    plane_model = np.array([plane_model[0],plane_model[1],plane_model[2]])
    pointcloud_w = np.array(outlier_cloud.points)

    # o3d.visualization.draw_geometries([inlier_cloud])
    # o3d.visualization.draw_geometries([outlier_cloud])

    select_m = np.dot(pointcloud_w,plane_model) + float(plane_model_ori[3])
    index_objects = np.argwhere((select_m>=0.01)).reshape(-1).astype(int)
    objects_point = pointcloud_w[index_objects].copy()
    # objects_pcd = outlier_cloud.select_by_index(inliers,invert = True)
    objects_pcd = o3d.geometry.PointCloud()
    objects_pcd.points = o3d.utility.Vector3dVector(objects_point)
    # o3d.visualization.draw_geometries([objects_pcd])
    pcd = inlier_cloud
    # o3d.visualization.draw_geometries([pcd])
    pts_tab = np.array(pcd.points)
    # pts_tab[:,2] = 0
    # pcd.points = o3d.utility.Vector3dVector(pts_tab)
    aabb = objects_pcd.get_oriented_bounding_box()
    print(np.array(aabb.get_box_points()))
    aabb_points = np.array(aabb.get_box_points()).reshape((-1,3))
    aabb.color = (1, 0, 0)
    # o3d.visualization.draw_geometries([objects_pcd, aabb])
    Nx = size[1]
    Ny = size[0]
    x = np.linspace(np.min(pts_tab[:,0]), np.max(pts_tab[:,0]), Nx)
    y = np.linspace(np.min(pts_tab[:,1]), np.max(pts_tab[:,1]), Ny)
    xv, yv = np.meshgrid(x, y)
    pts = np.array(objects_pcd.points)
    u = (pts[:,0] - np.min(pts_tab[:,0]))/ ( np.max(pts_tab[:,0])-np.min(pts_tab[:,0]) )
    v = (pts[:,1] - np.min(pts_tab[:,1]))/ ( np.max(pts_tab[:,1])-np.min(pts_tab[:,1]) )
    u = (size[0]-1)*u
    v = (size[1]-1)*v
    occupancy = np.zeros( (Ny,Nx) )
    u = u.astype(int)
    v = v.astype(int)
    u_ind = np.where(u<size[0])
    u = u[u_ind]
    v = v[u_ind]
    v_ind = np.where(v<size[1])
    u = u[v_ind]
    v = v[v_ind]
    u_ind = np.where(u>=0)
    u = u[u_ind]
    v = v[u_ind]
    v_ind = np.where(v>=0)
    u = u[v_ind]
    v = v[v_ind]
    occupancy[v,u] = 1
    occupancy = np.fliplr(occupancy)
    file_list = os.listdir("obj_mask/")
    file_name = obj_type +"_mask.pkl"
    if file_name not in file_list:
        file_path = "obj_mask/"+file_name
        f_save = open(file_path,'wb')
        pickle.dump(occupancy,f_save)
        f_save.close()
    '''
    occupancy = np.zeros((40,40))
    file_list = os.listdir("obj_mask/")
    for i in range(len(file_list)):
        if obj_type in file_list[i]:
            fileObject2 = open('obj_mask/'+file_list[i], 'rb')
            occupancy=  pickle.load(fileObject2)

            fileObject2.close()
    # plt.imshow(occupancy)
    # plt.show()
    vertices_new_obj = get_new_obj_contour_bbox(occupancy)
    return vertices_new_obj
def get_new_obj_contour_bbox(occu:np.array):
    mask = occu.copy()
    shape_occu = occu.shape
    Nx = shape_occu[1]
    Ny = shape_occu[0]
    mask = np.array((mask-np.min(mask))*255/(np.max(mask)-np.min(mask)),dtype=np.uint8)
    ret,mask = cv2.threshold(mask,50,255,0)
    contours,hierarchy = cv2.findContours(mask,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    max_area = 0
    cnt = []
    for i in contours:
        area_tmp = cv2.contourArea(i)
        if area_tmp>max_area:
            max_area = area_tmp
            cnt = i
    # approx = cv2.minAreaRect(cnt)
    x,y,w,h = cv2.boundingRect(cnt)
    # print(approx)
    # box = cv2.boxPoints(approx)
    # approx = np.int0(box)
    if x+w >=occu.shape[1]:
        w = occu.shape[1]-x-1
    if y+h >=occu.shape[0]:
        h = occu.shape[0]-1-y
    approx = np.array([[x,y],[x+w,y],[x+w,y+h],[x,y+h]])
    vertices_new_obj = []
    mask_tmp = mask.copy()
    if len(approx) >=2:
        approx = approx.reshape((-1,2))
        for i in range(len(approx)):
            mask_tmp[approx[i][1],approx[i][0]] = 130
        vertices_new_obj = approx
        # print(vertices_new_obj)
        vertices_new_obj = vertices_new_obj - np.array([Nx/2,Ny/2])
        # print(vertices_new_obj)
        # plt.imshow(mask_tmp)
        # plt.show()

        l = []
        for i in range(2):
            l.append(np.linalg.norm(vertices_new_obj[i]-vertices_new_obj[i+1]))
        # print(l)
        return vertices_new_obj
    else:
        return None
def get_new_obj_pcd(camera,size,hand_plane_model):
    pcd = get_pcd(camera)
    # o3d.visualization.draw_geometries([pcd])
    plane_model, inliers = pcd.segment_plane(distance_threshold=0.01,
                                         ransac_n=3,
                                         num_iterations=1000)
    outlier_cloud = pcd.select_by_index(inliers, invert=True)
    plane_model, inliers = outlier_cloud.segment_plane(distance_threshold=0.005,
                                         ransac_n=3,
                                         num_iterations=1000)
    inlier_cloud = outlier_cloud.select_by_index(inliers)
    plane_model_ori = plane_model
    plane_model = np.array([plane_model[0],plane_model[1],plane_model[2]])
    pointcloud_w = np.array(outlier_cloud.points)

    # o3d.visualization.draw_geometries([inlier_cloud])
    # o3d.visualization.draw_geometries([outlier_cloud])

    select_m = np.dot(pointcloud_w,plane_model) + float(plane_model_ori[3])
    index_objects = np.argwhere((select_m>=0.01)).reshape(-1).astype(int)
    objects_point = pointcloud_w[index_objects].copy()
    # objects_pcd = outlier_cloud.select_by_index(inliers,invert = True)
    objects_pcd = o3d.geometry.PointCloud()
    objects_pcd.points = o3d.utility.Vector3dVector(objects_point)
    # o3d.visualization.draw_geometries([objects_pcd])
    pcd = inlier_cloud
    o3d.visualization.draw_geometries([pcd])
    pts_tab = np.array(pcd.points)
    # pts_tab[:,2] = 0
    # pcd.points = o3d.utility.Vector3dVector(pts_tab)
    aabb = objects_pcd.get_oriented_bounding_box()
    # print(np.array(aabb.get_box_points()))
    aabb_points = np.array(aabb.get_box_points()).reshape((-1,3))
    aabb.color = (1, 0, 0)
    o3d.visualization.draw_geometries([objects_pcd, aabb])
    return aabb_points
def place_new_object(occu,ycb_list,ycb_path,num_new,obj_dict):
    angle = 0
    while True:
        randi = np.random.randint(0,len(ycb_list))
        # angle = np.random.randint(0,180)
        key_ori = ycb_list[randi]
        usd_path = ycb_path[key_ori]
        if key_ori not in obj_dict:
            obj_dict[key_ori] = 1
        else:
            if obj_dict[key_ori]<9:
                obj_dict[key_ori] +=1
                break

    print(obj_dict)
    key = key_ori+str(obj_dict[key_ori])
    translation = 0
    translation = [0.35,-0.9,0.1]
    rot = convert_quat(tf.Rotation.from_euler("XYZ", (0,0,angle), degrees=True).as_quat(), to="wxyz")
    if key_ori in ["mug","tomatoSoupCan","pitcherBase","tunaFishCan","bowl","banana"]:
        rot = convert_quat(tf.Rotation.from_euler("XYZ", (-90,angle,0), degrees=True).as_quat(), to="wxyz")
    prim_utils.create_prim(f"/World/newObjects/{key}", usd_path=usd_path, translation=translation,orientation=rot)
    new_obj = GeometryPrim(f"/World/newObjects/{key}",collision=True)
    RigidPrim(f"/World/newObjects/{key}",mass=0.3)
    new_obj_path = f"/World/newObjects/{key}"
    return obj_dict,new_obj,key_ori,new_obj_path
    # prim_utils.delete_prim(f"/World/newObjects/{key+str(num_new)}")

def bound_detect(occu):
    occu_p = occu.copy()/np.max(occu)
    occu_p = np.array(occu*255,dtype=np.uint8)
    # data = im.fromarray(occu_p)
    # data.save('pic/occu.png')
    # img = cv2.imread('pic/occu.png')
    # gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    ret,thresh = cv2.threshold(occu_p,50,255,0)
    contours,hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    print("Number of contours detected:",len(contours))
    shape_dict = dict()
    i = 0
    polygons = []
    for cnt in contours:
        i += 1
        # approx = cv2.approxPolyDP(cnt,0.1*cv2.arcLength(cnt,True),True)
        # approx = cv2.convexHull(cnt)
        approx = cv2.minAreaRect(cnt)
        (x,y) = cnt[0,0]
        # print(approx)
        box = cv2.boxPoints(approx)
        approx = np.int0(box)
        # print(box)
        if len(approx) >=2:
            # img = cv2.drawContours(img,[approx],-1,(0,255,255),3)
            # print(approx)
            approx = np.array(approx).reshape((-1,2))
            shape_dict[i] = approx
            polygons.append(Polygon(approx))
    print(polygons)
    tree = STRtree(polygons)
    print("remove containing boxes")
    del_ind = []
    for i,poly in enumerate(polygons):
        indice = tree.query(poly, predicate="contains").tolist()
        print(i)
        print(tree.geometries.take(indice))
        print(tree.geometries.take(i))
        if len(indice) >0:
            for j in indice:
                if contains(poly,tree.geometries.take(j)) and area(poly)>area(tree.geometries.take(j)):
                    if j+1 in shape_dict:
                        del(shape_dict[j+1])
                        del_ind.append(int(j))
    print(del_ind)
    for i in shape_dict:
        points_tmp = shape_dict[i].copy()
        points_tmp[:,0] = points_tmp[:,1]
        points_tmp[:,1] = shape_dict[i][:,0].copy()
        for j in range(len(points_tmp)):
            p_s = points_tmp[j]
            if j < len(points_tmp)-1:
                p_e = points_tmp[j+1]
            else:
                p_e = points_tmp[0]
            line = p_e - p_s
            length = np.linalg.norm(line)
            # print(p_s,p_e,length)
            for k in range(int(np.ceil(length))):
                if np.ceil(p_s[0]+k*line[0]/length) < occu.shape[0] and np.ceil(p_s[1]+k*line[1]/length)<occu.shape[1] and np.ceil(p_s[1]+k*line[1]/length)>=0 and np.ceil(p_s[0]+k*line[0]/length)>=0:
                    occu[int(np.ceil(p_s[0]+k*line[0]/length)),int(np.ceil(p_s[1]+k*line[1]/length))] = 2
    polygons = []
    for i in shape_dict:
        polygons.append(shape_dict[i])
    print(polygons)
    plt.imshow(occu)
    plt.show()
    return shape_dict

if __name__ == "__main__":
    # Run empty stage
    # for i in range(3):
        # simulation_app = SimulationApp(config)
        main()
        # Close the simulator
        simulation_app.close()


from jan20_CrossSections import CrossSections
#from ZieglerFiles_new import ziegler_files
#from feb20_zieglerfiles import ziegler   #newest with all cleaned files
#from single_decay_A0 import *
from des19_BeamCurrent import *

from BC_ziegler_run import *

#from feb20_activity import *
from foil_info import *

from simulated_CrossSectionData import SimCrossSectionData

#func=Cu_62Zn(); A0_guess=4000
#func=Ni_58Co(); A0_guess=[5000, 1000]
#func = Ni_61Cu(); A0_guess=4000
#func = Ni_56Co(); A0_guess=[150, 1500]
#func = Ni_58Co(); A0_guess=[8000, 1000]
#func = Ir_193mPt(); A0_guess=1500
#ACT = Activity(func, A0_guess, foilnumb=0)
#ACT.get_vals()
#ACT.knownparent_activity(Ni_56Ni())
#ACT.get_parent_activity(Ni_56Ni())

#from foil_info import *
#from beam_current_FoilReact import *
#from ZieglerFiles import ziegler_files

### GOOD LINES ARE
### 179 B+10%_D+0,5
### 139 B0%_D-1%
### 221 B+2,5%_D+3,5%
#BC= BeamCurrent(files[50])

#files,names = ziegler_files()

f_list_FilesNames = 'ziegler_FilesNames.csv'
files = np.genfromtxt(f_list_FilesNames, dtype="str", delimiter='|', usecols=[0])
names = np.genfromtxt(f_list_FilesNames, dtype="str", delimiter='|', usecols=[1])
files = list(files)
names = list(names)

good_beamcurrents = ['B_+10_D_-2', 'B_+0,75_D_-3,25', 'B_+1_D_-1,75', 'B_+0,75_D_-2,75', 'B_+0,75_D_-3', 'B_+0,5_D_-4,25',
					'B_+0,5_D_-3,75', 'B_+0,25_D_-4,75', 'B_+0,5_D_-4', 'B_+0,25_D_-5', 'B_-2,5_D_+4', 'B_+1_D_+1,25', 'B_+1,25_D_+2',
					'B_+1,5_D_+2,5', 'B_+1,5_D_+2,75', 'B_+2,25_D_+5', 'B_+2_D_+3,75', 'B_+1,75_D_+3,25', 'B_+2,5_D_+4,5', 'B_+2,25_D_+4,75',
					'B_+0,5_D_+1,25', 'B_+1,5_D_+3', 'B_+2,5_D_+7,5', 'B_+1,75_D_+3,5', 'B_+2_D_+4,25', 'B_+10_D_+3', 'B_-2,25_D_-1,75', 'B_+10_D_+2,5',
					'B_+1_D_+2', 'B_+1_D_+2', 'B_+1_D_+2']

#files, names = ziegler()
#print(names[8])
#print(type(files))
#
def find_index(list, element):
    return list.index(element)

#files = files[:10]
#names = names[:10]
#print(names[3])

"""
#COMPARTMENT 9 BEST CHI^2
selected_names = ['B_+1_D_+2', 'B_+10_D_+2,5', #'B_-2,25_D_-0,75', 'B_-2,25_D_-1,75',
'B_+10_D_+3', 'B_+2_D_+4,25', 'B_+1,75_D_+3,75',
'B_+2_D_+4', #'B_-2,25_D_-1,25',
'B_+1,5_D_+3,25', 'B_+1,25_D_+2,75', 'B_+1,75_D_+3,5', 'B_+2,5_D_+7,5', 'B_+1,5_D_+3', 'B_+0,5_D_+1,25']

# GOOD CANDIDATES: B_+1,5_D_+3,5

COMPARTMENT 6 BEST CHI^2
selected_names = ['B_+2,25_D_+4,75','B_+2_D_+4','B_+2,25_D_+4,5','B_+1,75_D_+3,25','B_+2_D_+3,75','B_+1,75_D_+3,5',
'B_+2,5_D_+7,5','B_+2,25_D_+5','B_+2_D_+4,25','B_+1,5_D_+2,75','B_+1,5_D_+2,5','B_+1,25_D_+2','B_+1_D_+1,25']

#COMPARTMENT 3 BEST CHI^2
selected_names = [#'B_-2,5_D_+4','B_+0,25_D_-5',  'B_+0,5_D_-4','B_+0,25_D_-4,75','B_+0,5_D_-3,75','B_+0,5_D_-4,25',
#'B_+0,75_D_-3', 'B_+0,75_D_-2,75','B_+1_D_-1,75','B_+0,75_D_-3,25',
'B_+10_D_-2']

selected_names = ['B_0_D_0']
"""

#scaling='B_+2_D_+4,25'     #THE ONE USED SO FAR. TURN OF OTHER IF WANT AS NORMAL
scaling='B_+0,5_D_+1,25'
#scaling = 'B_+1,25_D_+2,75'
#scaling='B_+1,75_D_+3,75'
#index = find_index(names, scaling); title= 'Beam current - Energy increase 2%, density increase 4.25%' #the one used so far.
index = find_index(names, scaling)#; title= 'Beam current - Energy increase 0.5%, density increase 1.25%'
#index = find_index(names, scaling); title= 'Beam current - Energy increase 1.25%, density increase 2.75%'
#index = find_index(names, scaling); title= 'Beam current - Energy increase 1.75%, density increase 3.75%'
#print(files[index])
#RZ = Run_Ziegler(files, names)
csv_filename = './' + files[index]

BC = BeamCurrent(files[index])
#BC.CurrentPlot_compartment(names[index], title=title)
## Just energy-flux distributions
#BC.plot_distribution('Ir', scaling)
#BC.plot_distribution('Cu', scaling)
#BC.plot_distribution('Ni', scaling)
#BC.plot_distribution('Fe', scaling)
#BC.plot_distribution('all',scaling)

#print(weighted_average_BC)
#BC.CurrentPlot_compartment(names[index], WABC=csv_filename)
CS = CrossSections(files[index])

#not important
#BC.calculate_beam_current('Ni', 'Ni_56Co', print_terms=True)
#BC.calculate_beam_current('Ni', 'Ni_58Co', print_terms=True)

# just collecting A0, dA0, lamb, mass_density, sigma_mass_density, reaction_integral, ucertainty_integral, irr_time, sigma_irr_time
### Testing that the program works, nothing else..
#BC.calling_parameters_to_weightedaverage_func('Cu', 'Cu_62Zn')
#BC.calling_parameters_to_weightedaverage_func('Cu', 'Cu_63Zn')
#BC.calling_parameters_to_weightedaverage_func('Cu', 'Cu_65Zn')
#BC.calling_parameters_to_weightedaverage_func('Ni', 'Ni_61Cu')
#BC.calling_parameters_to_weightedaverage_func('Ni', 'Ni_56Co')   ### prob
#BC.calling_parameters_to_weightedaverage_func('Ni', 'Ni_58Co')
#BC.calling_parameters_to_weightedaverage_func('Fe', 'Fe_56Co')

#BC.variance_minimization(9, names[index], include_56Co=True, MakePlot=True)
# returns WE_Ni, chi_sq, I_est, sigma_I_est

#### positive B and positive D compensate each other- same for neg neg

#index = find_index(names, 'B_-2,5_D_-7,5')
#index = find_index(names, 'B_+10_D_0')
#index = find_index(names, 'B_0_D_-5')
#index = find_index(names, 'B_0_D_0')
#print(index)
#RZ = Run_Ziegler(files, names)
#RZ.run_beam_current()
#RZ.plot_ChiSq(3, chi_tol=2)
#RZ.plot_ChiSq(7, chi_tol=2)
#RZ.plot_ChiSq(9, chi_tol=2)
RZ.plot_ChiSq(6, chi_tol=1)

#path_to_Chisq = os.getcwd() + '/BeamCurrent/chisq_dir_new/'

###   This is where the csv file with the beam current is written  (assigned indexed ziegler file)
csv_filename = './' + files[index]
print("**", csv_filename)

"""
BC = BeamCurrent(files[index])
#assigning variables for weighted_average.py
A0, sigma_A0, lambda_, mass_density, sigma_mass_density, reaction_integral, uncertainty_integral, irr_time, sigma_irr_time = BC.reshaping_parameters()

#print("A0: ", A0)
#print("dA0: ", sigma_A0)
#print("lamb: ", lambda_ )
#print("mass density: ", mass_density)
#print("sigma mass density: ", sigma_mass_density)
#print("reaction int: ", reaction_integral)
#print("uncert integral: ", uncertainty_integral)
#print(irr_time, sigma_irr_time)

weighted_average_BC, sigma_weighted_average_BC = Average_BeamCurrent(A0, sigma_A0, mass_density, sigma_mass_density,  lambda_, reaction_integral, uncertainty_integral, irr_time, sigma_irr_time, csv_filename=csv_filename)
print(weighted_average_BC)
print(sigma_weighted_average_BC)
"""

#print(weighted_average_BC)
#BC.CurrentPlot_compartment(names[index], WABC=csv_filename)
#CS = CrossSections(files[index])

"""
CS.mon_CS_test(Fe_56Co(), 'Fe', 'Fe_56Co.csv', 3, 'Fe_56Co', names[index], csv_filename)
CS.mon_CS_test(Ni_61Cu(), 'Ni', 'Ni_61Cu.csv', 10, 'Ni_61Cu', names[index], csv_filename)
CS.mon_CS_test(Ni_56Co(), 'Ni', 'Ni_56Co.csv', 10, 'Ni_56Co', names[index], csv_filename)
CS.mon_CS_test(Ni_58Co(), 'Ni', 'Ni_58Co.csv', 10, 'Ni_58Co', names[index], csv_filename)
CS.mon_CS_test(Cu_62Zn(), 'Cu', 'Cu_62Zn.csv', 10, 'Cu_62Zn', names[index], csv_filename)
CS.mon_CS_test(Cu_63Zn(), 'Cu', 'Cu_63Zn.csv', 10, 'Cu_63Zn', names[index], csv_filename)
CS.mon_CS_test(Cu_65Zn(), 'Cu', 'Cu_65Zn.csv', 10, 'Cu_65Zn', names[index], csv_filename)
"""

#BC.CurrentPlot(names[index], SaveFig=True)

#BC.calculate_beam_current('Fe', 'Fe_56Co', print_terms=True)
#BC.calculate_beam_current('Fe', 'Fe_56Co', print_terms=True)
#BC.specified_currents()
#from weighted_average import *

#print(weighted_average_BC)
#CS.exfordata_npat()

#BC = BeamCurrent(files[index])

#BC.CurrentPlot_compartment(names[index], title='Beam current - Energy increase 10%, density increase 4.25%')
#BC.variance_minimization(9, names[index], MakePlot=True )
#BC.CurrentPlot(names[index])
#BC.current_for_CS()

#Cu_57Ni()
#single_decay_data(Cu_57Ni(), "Cu_57Ni", 10, Save_csv=True)    #EXCELLENT
#single_decay_data(Cu_64Cu(), "Cu_64Cu", 10, Save_csv=True)      #EXCELLENT
#two_step_up_npat(Ni_58Co(), "Ni_58mCo_npat", "Ni_58Co_npat", 10, '58COm', '58COg', Save_csv=True)
#two_step_up_data(Ni_58Co(),"Ni_58mCo", "Ni_58Co", 10, Save_csv= True)
#two_step_kp_data(Ni_56Ni(), Ni_56Co(), "Ni_56Co", 10, Save_csv= True)

#index = find_index(names, 'B_+2_D_+4,25')   #the one used so far.
#csv_filename = './' + files[index]
#print(csv_filename)

#csv_filename = './' + names[index] + '.csv'
#print(csv_filename)
#I = np.genfromtxt(csv_filename, delimiter=',', usecols=[1])  #### NOT RIGHT
#print("***")
#print(I)

#print("***", csv_filename)
#csv_filename = os.getcwd() + '/../ziegler_B_+2_D_+4,25_fluxes.csv'
#print("***", csv_filename)
CS = CrossSections(files[index])

### Monitor reactions

# In general: good looking, but I have some problems with finding the correct authors on EXFOR. last step.

#CS.mon_CS_test(Fe_56Co(), 'Fe', 'Fe_56Co.csv', 3, 'Fe_56Co', names[index], csv_filename)   #Finished. but lacking authors: Zavorka 2012
#CS.mon_CS_test(Ni_61Cu(), 'Ni', 'Ni_61Cu.csv', 10, 'Ni_61Cu', names[index], csv_filename)  # lacking authors: haddad 2013. Ask Andrew how to download the data.
#CS.mon_CS_test(Ni_56Co(), 'Ni', 'Ni_56Co.csv', 10, 'Ni_56Co', names[index], csv_filename)
#CS.mon_CS_test(Ni_58Co(), 'Ni', 'Ni_58Co.csv', 10, 'Ni_58Co', names[index], csv_filename)
#CS.mon_CS_test(Cu_62Zn(), 'Cu', 'Cu_62Zn.csv', 10, 'Cu_62Zn', names[index], csv_filename)
#CS.mon_CS_test(Cu_63Zn(), 'Cu', 'Cu_63Zn.csv', 10, 'Cu_63Zn', names[index], csv_filename)
#CS.mon_CS_test(Cu_65Zn(), 'Cu', 'Cu_65Zn.csv', 10, 'Cu_65Zn', names[index], csv_filename)

### Ni reactions FINISHED

#CS.make_CS(Ni_52Mn(), 'Ni', 'Ni_52Mn.csv', 10, 'Ni_52Mn', csv_filename, '25', '52', independent=False, ylimit=4, CS_colonne_ALICE=5, file_ending='.tot', isomer_state='m+g')   # using 1434 keV line
#CS.make_CS(Ni_52Mn(), 'Ni', 'Ni_52gMn.csv', 10, 'Ni_52Mn', csv_filename, '25', '52', independent=True, ylimit=4, CS_colonne_ALICE=5, file_ending='.tot')   # using 1434 keV line
#CS.make_CS_isomerSub('Ni', 10, csv_filename, 'Ni_52mMn', Z='25', A='52', react_func_cumulative=Ni_52Mn(), reaction_cumulative='Ni_52m+gMn', filename_cumulative='Ni_52Mn.csv', react_func_groundstate=Ni_52gMn(), reaction_groundstate='Ni_52gMn', filename_groundstate='Ni_52gMn.csv', ylimit=4, independent=True, BR=0.0178, isomer_state='m', file_ending='.L01', CS_colonne_ALICE=7, save_text=True, feeding=None)

#CS.make_CS(Ni_54Mn(), 'Ni', 'Ni_54Mn.csv', 10, 'Ni_54Mn', csv_filename, '25', '54', ylimit=40, independent=True, CS_colonne_ALICE=5)
#CS.make_CS(Ni_59Fe(), 'Ni', 'Ni_59Fe.csv', 10, 'Ni_59Fe', csv_filename, '26', '59', independent=False,CS_colonne_ALICE=5, ylimit=1)   # first in decay chain
#CS.make_CS(Ni_60Cu(), 'Ni', 'Ni_60Cu.csv', 10, 'Ni_60Cu', csv_filename, '29', '60',CS_colonne_ALICE=5)
#CS.make_CS(Ni_64Cu(), 'Ni', 'Ni_64Cu.csv', 10, 'Ni_64Cu', csv_filename, '29', '64',CS_colonne_ALICE=5)
#CS.make_CS(Ni_60Co(), 'Ni', 'Ni_60Co.csv', 10, 'Ni_60Co', csv_filename, '27', '60', ylimit=55, independent=False,CS_colonne_ALICE=5) # first in decay chain
#CS.make_CS(Ni_65Ni(), 'Ni', 'Ni_65Ni.csv', 10, 'Ni_65Ni', csv_filename, '28', '65', independent=False,CS_colonne_ALICE=5) # first in decay chain
#CS.make_CS(Ni_55Co(), 'Ni', 'Ni_55Co.csv', 10, 'Ni_55Co', csv_filename, '27', '55', ylimit=30,independent=False,CS_colonne_ALICE=5) # first in decay chain
#CS.make_CS(Ni_57Ni(), 'Ni', 'Ni_57Ni.csv', 10, 'Ni_57Ni', csv_filename, '28', '57', independent=False,CS_colonne_ALICE=5) # first in decay chain)

#CS.make_CS(Ni_56Ni(), 'Ni', 'Ni_56Ni.csv', 10, 'Ni_56Ni', csv_filename, '28', '56', ylimit=4, independent=False, BR=None, CS_colonne_ALICE=5) # first in decay chain)
#CS.make_CS_subtraction('daughter', 'Ni', 10, csv_filename, Ni_56Ni(), 'Ni_56Ni', 'Ni_56Ni.csv', '28', '56',  Ni_56Co(), 'Ni_56Co', 'Ni_56Co.csv', '27', '56', BR_daughter=1.0, ylimit=40, isomer_state=None, independent=False, file_ending='.tot', save_text=True, feeding='beta+')  # Necessary when subtracting
#CS.make_CS(Ni_56Co(), 'Ni', 'Ni_56Co.csv', 10, 'Ni_56Co', csv_filename, '27', '56', independent=True, CS_colonne_ALICE=5) # first in decay chain)

### Cu reactions

#CS.make_CS(Cu_64Cu(), 'Cu', 'Cu_64Cu.csv', 10, 'Cu_64Cu', csv_filename, '29', '64', CS_colonne_ALICE=5)
#CS.make_CS(Cu_65Ni(), 'Cu', 'Cu_65Ni.csv', 10, 'Cu_65Ni', csv_filename, '28', '65', CS_colonne_ALICE=5)
#CS.make_CS(Cu_59Fe(), 'Cu', 'Cu_59Fe.csv', 10, 'Cu_59Fe', csv_filename, '26', '59', ylimit=1, independent=False, CS_colonne_ALICE=5) # first in decay chain
#CS.make_CS(Cu_60Co(), 'Cu', 'Cu_60Co.csv', 10, 'Cu_60Co', csv_filename, '27', '60', ylimit=25, independent=False, CS_colonne_ALICE=5) # first in decay chain)
#CS.make_CS(Cu_61Co(), 'Cu', 'Cu_61Co.csv', 10, 'Cu_61Co', csv_filename, '27', '61', ylimit=4, independent=False,CS_colonne_ALICE=5) # first in decay chain)
#CS.make_CS(Cu_61Cu(), 'Cu', 'Cu_61Cu.csv', 10, 'Cu_61Cu', csv_filename, '29', '61', ylimit=110, independent=False, CS_colonne_ALICE=5) # first in decay chain)
#CS.make_CS(Cu_64Cu(), 'Cu', 'Cu_64Cu.csv', 10, 'Cu_64Cu', csv_filename, '29', '64',CS_colonne_ALICE=5)

### Fe reactions
#CS.make_CS(Fe_48V(), 'Fe', 'Fe_48V.csv', 3, 'Fe_48V', csv_filename, '23', '48', independent=True, file_ending='.tot', ylimit=0.176)
#CS.make_CS(Fe_51Cr(), 'Fe', 'Fe_51Cr.csv', 3, 'Fe_51Cr', csv_filename, '24', '51', independent=True, ylimit=20)
#CS.make_CS(Fe_52Mn(), 'Fe', 'Fe_52Mn.csv', 3, 'Fe_52Mn', csv_filename, '25', '52', ylimit=50, independent=False)
#CS.make_CS(Fe_53Fe(), 'Fe', 'Fe_53Fe.csv', 3, 'Fe_53Fe', csv_filename, '26', '53', independent=False)
#CS.make_CS(Fe_54Mn(), 'Fe', 'Fe_54Mn.csv', 3, 'Fe_54Mn', csv_filename, '25', '54', independent=True, ylimit=125)
#CS.make_CS(Fe_55Co(), 'Fe', 'Fe_55Co.csv', 3, 'Fe_55Co', csv_filename, '27', '55', independent=True)
#CS.make_CS(Fe_57Co(), 'Fe', 'Fe_57Co.csv', 3, 'Fe_57Co', csv_filename, '27', '57', independent=True)
#CS.make_CS(Fe_58Co(), 'Fe', 'Fe_58Co.csv', 3, 'Fe_58Co', csv_filename, '27', '58', independent=False)
#CS.make_CS(Fe_59Fe(), 'Fe', 'Fe_59Fe.csv', 3, 'Fe_59Fe', csv_filename, '26', '59', independent=True)   # only produced via 58Fe(d,n). abundance is low so had to redo energy. Ask Andrew about energy.

### Ir reactions

#CS.make_CS(Ir_188Pt(), 'Ir', 'Ir_188Pt.csv', 10, 'Ir_188Pt', csv_filename, '78', '188', ylimit=300, independent=True)
#CS.make_CS(Ir_188Ir(), 'Ir', 'Ir_188Ir.csv', 10, 'Ir_188Ir', csv_filename, '77', '188', ylimit=15, independent=False, isomer_state='m1+g', CS_colonne_ALICE=4, feeding='beta+', BR=1.0)

###Independent of 188Ir, subtraction of 188Ir_cum - 188Pt_ind = 188Ir_ind
#CS.make_CS_subtraction('daughter', 'Ir', 10, csv_filename, Ir_188Pt(), 'Ir_188Pt', 'Ir_188Pt.csv', '78', '188',  Ir_188Ir(), 'Ir_188Ir', 'Ir_188Ir.csv', '77', '188', ylimit=15, BR_daughter=1.0, isomer_state='m1+g', independent=True, file_ending='.tot', CS_colonne_ALICE=4, save_text=True, feeding=None)  # Necessary when subtracting

# make 188 Ir independent by subtracting 188Ir cumulative-188Pt

#CS.make_CS(Ir_189Pt(), 'Ir', 'Ir_189Pt.csv', 10, 'Ir_189Pt', csv_filename, '78', '189', ylimit=520, independent=True)    #
#CS.make_CS(Ir_189Ir(), 'Ir', 'Ir_189Ir.csv', 10, 'Ir_189Ir', csv_filename, '77', '189', independent=False, feeding='beta+', BR=1.0)    # need work on activity
#CS.make_CS_subtraction('daughter', 'Ir', 10, csv_filename, Ir_189Pt(), 'Ir_189Pt', 'Ir_189Pt.csv', '78', '189',  Ir_189Ir(), 'Ir_189Ir', 'Ir_189Ir.csv', '77', '189', ylimit=500, independent=True, file_ending='.tot', CS_colonne_ALICE=4, BR_daughter=1.0)  # Necessary when subtracting

#CS.make_CS(Ir_190Ir(), 'Ir', 'Ir_190Ir.csv', 10, 'Ir_190Ir', csv_filename, '77', '190', independent=False, CS_colonne_ALICE=4)     # file_ending=.tot because of decay from m1 m2 isomer.
#CS.make_CS(Ir_190m2Ir(), 'Ir', 'Ir_190m2Ir.csv', 10, 'Ir_190m2Ir', csv_filename, '77', '190', file_ending='.L37', isomer_state='m2', independent=False, CS_colonne_ALICE=6)   # 0.0860
#CS.make_CS_subtraction('daughter', 'Ir', 10, csv_filename, Ir_190m2Ir(), 'Ir_190m2Ir', 'Ir_190m2Ir.csv', '77', '190',  Ir_190Ir(), 'Ir_190Ir', 'Ir_190Ir.csv', '77', '190', BR_daughter=0.0860, ylimit=None, isomer_state='m1+g', independent=True)#independent='_cumulative_190m1+190Ir', file_ending='.tot')  # Necessary when subtracting

#CS.make_CS(Ir_191Pt(), 'Ir', 'Ir_191Pt.csv', 10, 'Ir_191Pt', csv_filename, '78', '191', independent=True)

#CS.make_CS(Ir_192Ir(), 'Ir', 'Ir_192Ir.csv', 10, 'Ir_192Ir', csv_filename, '77', '192', file_ending='.tot', independent=False)

#CS.make_CS(Ir_193mPt(), 'Ir', 'Ir_193mPt.csv', 10, 'Ir_193mPt', csv_filename, '78', '193', file_ending='.L05', independent=True, CS_colonne_ALICE=6)

#CS.make_CS(Ir_194Ir(), 'Ir', 'Ir_194Ir.csv', 10, 'Ir_194Ir', csv_filename, '77', '194', file_ending='.L00', independent=False)
#CS.make_CS(Ir_194m2Ir(), 'Ir', 'Ir_194m2Ir.csv', 10, 'Ir_194m2Ir', csv_filename, '77', '194', file_ending='not', isomer_state='m2', independent=True, CS_colonne_ALICE=None)     #talys=.L34

#FALSE PEAKS
#CS.make_CS(Ni_56Mn(), 'Ni', 'Ni_56Mn.csv', 10, 'Ni_56Mn', csv_filename, '25', '56')   #BAD
#CS.make_CS(Cu_52Mn(), 'Cu', 'Cu_52Mn.csv', 10, 'Cu_52Mn', csv_filename, '25', '52')   #Most likely false. Not in talys or exfor.
##CS.make_CS(Cu_56Co(), 'Cu', 'Cu_56Co.csv', 10, 'Cu_56Co', csv_filename, '27', '56')   #Most likely false. In talys but no match, not in exfor.
##CS.make_CS(Cu_57Ni(), 'Cu', 'Cu_57Ni.csv', 10, 'Cu_57Ni', csv_filename, '28', '57')    #Most likely false too, Qval is too low at these energies.
##CS.make_CS(Cu_57Co(), 'Cu', 'Cu_57Co.csv', 10, 'Cu_57Co', csv_filename, '27', '57')    #Most likely false too, Qval is too low at these energies.
#CS.make_CS(Ir_189Re(), 'Ir', 'Ir_189Re.csv', 10, 'Ir_189Re', csv_filename, '75', '189')     # needs work
#CS.make_CS(Ir_190mRe(), 'Ir', 'Ir_90mReRe.csv', 10, 'Ir_190mRe', csv_filename, '75', '190')     # needs work
#CS.make_CS(Ir_188Re(), 'Ir', 'Ir_188Re.csv', 10, 'Ir_188Re', csv_filename, '75', '188') # not working
#CS.make_CS(Ir_188mRe(), 'Ir', 'Ir_188mRe.csv', 10, 'Ir_188mRe', csv_filename, '75', '188')   not working
##CS.make_CS(Ir_183Ta(), 'Ir', 'Ir_183Ta.csv', 10, 'Ir_183Ta', csv_filename, '73', '183')
#CS.make_CS(Ir_186Re(), 'Ir', 'Ir_186Re.csv', 10, 'Ir_186Re', csv_filename, '75', '186')
#CS.make_CS(Ir_186Ta(), 'Ir', 'Ir_186Ta.csv', 10, 'Ir_186Ta', csv_filename, '73', '186')
#CS.make_CS(Ir_187W(), 'Ir', 'Ir_187W.csv', 10, 'Ir_187W', csv_filename, '74', '187')
#CS.make_CS(Ir_193Os(), 'Ir', 'Ir_193Os.csv', 10, 'Ir_193Os', csv_filename, '76', '193', file_ending='.tot', independent=True, CS_colonne_ALICE=4)
#CS.make_CS(Ir_191Os(), 'Ir', 'Ir_191Os.csv', 10, 'Ir_191Os', csv_filename, '76', '191', file_ending='.tot', independent=True)

#get_vals(Cu_64Cu(), 'Cu', 'Cu_64Cu.csv', 10, 'Cu_64Cu', csv_filename)

#I = BC.current_for_CS()
#print(I)

#RZ.plot_ChiSq(3, 0.05)
#RZ.plot_ChiSq(7, 0.01)
#RZ.plot_ChiSq(9, 3)
#RZ.run_beam_current()
#print('Cu')
#RZ.flux_distribution('Cu')
#print('Ni')
#RZ.flux_distribution('Ni')
#print('Ir')
#RZ.flux_distribution('Ir')
#print('Fe')
#RZ.flux_distribution('Fe')

if __name__ == "__main__":
    print(__name__)
else:
	print(main.py)


import typing, netaddr
from box import Box

from netsim import api

"""
Enable 'proxy_arp' attribute per VRF
"""
def init(topology: Box) -> None:
  topology.defaults.attributes.vrf.proxy_arp = None

"""
Add 'proxy_arp' flag and trigger custom config file for any interfaces that are
part of a VRF with proxy ARP enabled
"""
def post_transform(topology: Box) -> None:

  if not topology.get('vrfs',[]):
    return

  target_vrfs = [ v for v,d in topology.vrfs.items() if 'proxy_arp' in d ]

  config_name = api.get_config_name(globals())

  # Iterate over node[x].interfaces that are part of a VRF with proxy_arp enabled
  for n, ndata in topology.nodes.items():
    for i in ndata.interfaces:
      if i.type in ['lan','p2p','svi'] and 'vrf' in i and i.vrf in target_vrfs \
        and ('ipv4' in i or 'ipv6' in i):
          i.proxy_arp = True
          api.node_config(ndata,config_name)


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''
drawRegion.py
Draw a ROI region by click the left button of the mouse.
'''

import numpy as np
import cv2 as cv

def on_mouse(event, x, y, flags, param):
    '''
    Callback function to respond the mouse left button down action
    Args:
        event:  mouse event code
        x:      image col index of the mouse point location
        y:      image col index of the mouse point location
    '''
    if event == cv.EVENT_LBUTTONDOWN:
        '''respond the left button click action'''
        ptls.append([x, y])
        cmimg = cmsrc.copy()
        cmmask = np.zeros((cmW, cmH), np.uint8)
        if len(ptls) > 1:
            for i in range(len(ptls)-1):
                # draw lines if the number of clicked points lager than 1
                cv.line(cmimg, tuple(ptls[i]), tuple(ptls[i+1]), green, 5)
            if len(ptls) > 2:
                # draw polygon if the number of clicked points lager than 2
                cv.polylines(cmimg, np.array([ptls], np.int32), True, black, 3)
                cv.fillPoly(cmmask, np.array([ptls], np.int32), white, 1)
                '''draw the polygon by the clicked point of the image'''
                image, contours, hierarchy = cv.findContours(cmmask, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_NONE)
                cv.drawContours(cmmask, contours, -1, white, -1)
                cv.drawContours(cmimg, contours, -1, white, 1)
                '''
                Find the extreme outer contours of the polygon to fill the overlap holes.
                Comment the upper 3 lines if you want to show the overlap holes.
                '''
                cmdst = cv.bitwise_and(cmsrc, cmsrc, mask=cmmask)
                cv.imshow('dst', cmdst)
                cv.imshow('mask', cmmask)
            cv.imshow('src', cmimg)
            cv.waitKey(0)

if __name__ == '__main__':
    ptls = []
    green = (0, 255, 0)
    black = (0, 0, 0)
    white = (255, 255, 255)
    '''define the line colors'''
    cmsrc = cv.imread('lena.jpg')
    cmW, cmH, cmC = cmsrc.shape
    cmimg = cmsrc.copy()
    '''load the image'''
    cv.imshow('src', cmimg)
    cv.setMouseCallback('src', on_mouse)
    '''show image and set the callback function'''
    cv.waitKey(0)

    cv.destoryAllWindows()


# pylint: disable=missing-docstring

aapl = [ 10, 154.12 ]
goog = [  2, 812.56 ]
tsla = [ 12, 342.12 ]
fb   = [ 18, 209.0  ]

portfolio = [ aapl, goog, tsla, fb ]

print(portfolio[3][0])
#            AAPL     GOOG    TSLA      FB
market  = [ 198.84, 1217.93, 267.66, 179.06 ]
pnl = 0
for i in range(len(portfolio)):
    pnl += portfolio[i][0]*(market[i]-portfolio[i][1])

print(pnl)


import json
import re
from lxml import etree
import pymongo
import time
import requests
import ssl
context = ssl._create_unverified_context()
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

client = pymongo.MongoClient('mongodb://localhost:27017')
db = client['douban']
table = db['douban-250']

url = ['https://movie.douban.com/top250?start={}'.format(str(i)) for i in range(0,250,25)]
header = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'}

def download_urls(url):
    response = requests.get(url,headers=header,verify=False)
    selector = etree.HTML(response.text)
    url_list = selector.xpath('//a[@class=""]/@href')
    return url_list

def get_content(url):
    #print(url)
    response = requests.get(url,headers=header,verify=False)
    selector = etree.HTML(response.text)
    name = selector.xpath('//span[@property="v:itemreviewed"]/text()')[0]
    director = selector.xpath('//a[@rel="v:directedBy"]/text()')[0]
    #主演只取前三个
    actor = selector.xpath('//a[@rel="v:starring"]/text()')[:3]
    actor = r';'.join(actor)
    type = selector.xpath('//span[@property="v:genre"]/text()')
    type = r';'.join(type)
    runtime = selector.xpath('//span[@property="v:runtime"]/text()')[0]
    #制片国家使用re
    country = re.findall('制片国家/地区:(.+)
', response.text, re.S)
    level = selector.xpath('//strong[@class="ll rating_num"]/text()')[0]

    info = {
        '电影':name,
        '导演':director,
        '主演':actor,
        '类型':type,
        '国家':country,
        '时长':runtime,
        '评价':level }

    table.insert_one(info)

if __name__=='__main__':
    start = time.time()
    url_lists = []  #全部链接的列表
    for i in url:
        url_lists.extend(download_urls(i))
    for url in url_lists:
    #有几个链接是无效的 ，使用try...except...
        try:
            get_content(url)
        except Exception as e:
            continue

    stop = time.time()
    print('run time:', (stop - start))


# Libraries
import streamlit as st
from PIL import Image
import pandas as pd
import plotly.express as px
# Layout

st.set_page_config(page_title='Developer Activity in 2022 on NEAR Protocol', page_icon=':bar_chart:', layout='wide')
st.title('Developer activity')

#CSS
with open('style.css')as f:
    st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html = True)

@st.cache(ttl=1)
def get_data(query):
    if query == 'Top Open PR':
        return pd.read_json('https://node-api.flipsidecrypto.com/api/v2/queries/4877ed5c-f392-4c30-b987-6bd0e3f9d783/data/latest')
    elif query == 'Top Close PR':
        return pd.read_json('https://node-api.flipsidecrypto.com/api/v2/queries/76e9f3ce-8ffc-4f63-b752-ab341e14b1e9/data/latest')
    # elif query == 'test':
    #     return pd.read_json('https://node-api.flipsidecrypto.com/api/v2/queries/4b9fa244-4679-4fe7-9fda-55cb3fedb140/data/latest')
    # elif query == 'Transactions Fee Payers':
    #     return pd.read_json('https://node-api.flipsidecrypto.com/api/v2/queries/7eae69ea-2387-420d-b4b9-6eceeb5ef22d/data/latest')
    return None

#Get Data
Top_Open_PR = get_data('Top Open PR')
Top_Close_PR = get_data('Top Close PR')

theme_plotly = None # None or streamlit

# st.table(Top_Open_PR)
c1,c2 = st.columns(2)
with c1:
    st.info('Developer Activity - Top Open PR')
    st.table(Top_Open_PR)
with c2:
    st.info('Developer Activity - Top Close PR')
    st.table(Top_Close_PR)


# هنا هعمل هثبت المكتبات ال محتاجينها وهعمل استدعاء ليها
#from unittest import result
from sys import excepthook

import requests
import bs4
import csv
from itertools import zip_longest
job_title=[]
company_name=[]
location_name=[]
job_skill=[]
date=[]
links=[]
#هنا بعمل طلب على شان ادخل على اللنك واحفظ الصفحة
page_num=0
while True:
    url = f"https://wuzzuf.net/search/jobs/?a=hpb&q=e-marketer&start={page_num}"
    result = requests.get(url)
    src = result.content

    #print(src)

    #هنا هنعمل اوبجت جديد  على شان نقدر نفصل فى المعلومات ال طالعة (parse content)

    soup = bs4.BeautifulSoup(src, "lxml")
    #print(soup)
    page_limit=int(soup.find("strong").text)
    if(page_num>page_limit//15):
        print("page ended, terminate")
        break
    # هنطلع الحاجات ال محتاجنها من الصفة مثل اسم الوظيفة ,اسم الشركة , الموقع , المهارات المطلوبة , وقت الطرح

    job_titles=soup.find_all("h2",{"class":"css-m604qf"})
    #print(job_title)

    company_names=soup.find_all("a",{"class":"css-17s97q8"})
    #print(campany_name)

    locations_names=soup.find_all("span",{"class":"css-5wys0k"})
    #print(locations_name)

    job_skills=soup.find_all("div",{"class":"css-y4udm8"})
    #print(job_skills)

    posteds_old=soup.find_all("div",{"class":"css-do6t5g"})
    posteds_new=soup.find_all("div",{"class":"css-4c4ojb"})
    posted=[*posteds_new,*posteds_old]
    #هنا هنعمل لوب على شان نلف فى الكونتت كلها ونستخرج ال احنا محتاجينه منها

    for i in range(len(job_titles)):
        job_title.append(job_titles[i].text)
        links.append("https://wuzzuf.net"+job_titles[i].find("a").attrs['href'])
        company_name.append(company_names[i].text)
        location_name.append(locations_names[i].text)
        job_skill.append(job_skills[i].text)
        date.append(posted[i].text)
    f_list = [job_title, company_name, date, location_name, job_skill, links]  # """هنا هنعمل حاجة اسمعها ان باكنج"""

    for all in zip(*f_list):
        print(all)
    page_num+=1
    print("\npage switched\n")

#print(posted)

# print(job_title)
# print("\n")
# print(company_name)
# print("\n")
# print(locations_name)
# print("\n")
# print(job_skil ls)

#هنا هنخزن المعلومات ال اتستخرجنها ونملى بيها الفايل الخاص بنا

exp=zip_longest(*f_list)
with open("D:\تالتة حاسبات\python\python3.csv","w") as thefile:
    wrr=csv.writer(thefile)
    wrr.writerow(["job title","company name", "date","location","skills","links"])
    wrr.writerows(exp)


from tqdm import tqdm
import pandas as pd
import numpy as np
import json
import argparse

import bokeh.models as bm
import bokeh.plotting as pl
from bokeh.io import output_notebook

from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler

import fasttext.util

class word_embeddings:
    def __init__(self, model, df, text_column, batch_size=5):
        self.model = model
        self.df = df
        self.text_column = text_column
        self.batch_size = batch_size
        self.num_batches = int(np.ceil(len(df) / batch_size))

    def batch_generator(self):
        while True:
            for i in range(self.num_batches):
                batch = self.df[i*self.batch_size : (i+1)*self.batch_size]
                X_batch = batch[self.text_column].values
                yield X_batch

    def predict(self, X):
        return [self.model.get_sentence_vector(x) for x in X]

    def draw_vectors(
        self, x, y, radius=10, alpha=0.25, color="blue", width=600, height=400, show=True, **kwargs
        ):
        """draws an interactive plot for data points with auxilirary info on hover"""

        output_notebook()
        if isinstance(color, str):
            color = [color] * len(x)
        data_source = bm.ColumnDataSource({"x": x, "y": y, "color": color, **kwargs})

        fig = pl.figure(active_scroll="wheel_zoom", width=width, height=height)
        fig.scatter("x", "y", size=radius, color="color", alpha=alpha, source=data_source)

        fig.add_tools(bm.HoverTool(tooltips=[(key, "@" + key) for key in kwargs.keys()]))
        if show:
            pl.show(fig)
        return fig

def main(config_file: str) -> None:
    """
    The main function for data preprocessing.

    Args:
        config_file (str): Path to the JSON configuration file.
    """

    # Load the JSON configuration file
    with open(config_file, "r") as f:
        config = json.load(f)

    # Extract values from the configuration file
    texts_data_path = config["data_paths"]["texts"]
    embeddings_data_path = config["data_paths"]["embeddings"]

    ft = fasttext.load_model('cc.en.300.bin')
    df = pd.read_csv(texts_data_path, index_col=0)

    to_vec = word_embeddings(ft, df, 'title')
    num_batches = to_vec.num_batches

    data_generator = to_vec.batch_generator()

    X = []
    for i in tqdm(range(num_batches)):
        X_batch = next(data_generator)
        X.extend(to_vec.predict(X_batch))
    X = np.asarray(X)

    np.save(embeddings_data_path, X)

    """
    pca = PCA(2)
    scaler = StandardScaler()

    phrase_vectors_2d = pca.fit_transform(X)
    phrase_vectors_2d = scaler.fit_transform(phrase_vectors_2d)

    to_vec.draw_vectors(
        phrase_vectors_2d[:, 0],
        phrase_vectors_2d[:, 1],
        phrase=[phrase[:50] for phrase in df.title.to_list()],
        radius=20,
    )"""

if __name__ == "__main__":
    # Parse the arguments from the command line
    parser = argparse.ArgumentParser()
    parser.add_argument("-config_path", type=str, help="Path to JSON configuration file")
    args = parser.parse_args()

    # Call the main function with the parsed arguments
    main(args.config_path)


#!/usr/bin/python3
""" Send request """

if __name__ == "__main__":
    import requests
    import sys

    response = requests.get(sys.argv[1])
    statusCode = response.status_code

    if statusCode >= 400:
        print("Error code:", statusCode)
    else:
        print(response.text)


import torch
from torch import nn
import numpy as np
class DNN_layer():
    def __init__(self, in_, out_, next = None, pre = None):
        self.w = torch.autograd.Variable(torch.randn(in_, out_).float(), requires_grad = True)
        self.b = torch.autograd.Variable(torch.ones(out_).float(), requires_grad = True)
        self.next = next
        self.pre = pre
    def forward(self, input):
        self.input = input
        hidden = torch.mm(input, self.w) + self.b #+ self.b1
        if self.next != None:
            self.output = torch.sigmoid(hidden)
        else:
            self.output = hidden
        return self.output

    def cal_grad(self, input, dz_next, w):
        m1 = torch.mm(dz_next, w.T)
        m2 = (1 - self.output)* self.output
        dz = m1 * m2
        self.dz = dz
        self.w.grad = torch.mm(input.T, dz)
        self.b.grad = dz.sum(dim = 0)

    def cal_grad_last_layer(self, input, y_tre):
        self.dz = (self.output - y_tre)
        self.w.grad = torch.mm(input.T, self.dz)
        self.b.grad = (self.output - y_tre).sum(dim=0)

    def backward(self, y_tre = None):
        if self.next != None:
            self.cal_grad(self.input, self.next.dz, self.next.w)
        else: ###最后一层
            self.cal_grad_last_layer(self.input, y_tre)

class DNN():
    def __init__(self, layer_nodes, y_tre):
        self.head_layer = DNN_layer(layer_nodes[0][0], layer_nodes[0][1])
        head = self.head_layer
        for i in range(1, len(layer_nodes)):
            layer = DNN_layer(layer_nodes[i][0], layer_nodes[i][1])
            layer.pre = head
            head.next = layer
            head = layer
        self.last_layer = head
        self.y_tre = y_tre
    def forward(self, input):
        layer = self.head_layer
        output = layer.forward(input)
        while layer.next != None:
            layer = layer.next
            output = layer.forward(output)
        self.last_layer = layer
        return output
    def backward(self):
        layer = self.last_layer
        layer.backward(self.y_tre)
        while layer.pre != None:
            layer = layer.pre
            layer.backward()

def loss_function(y_tre, y_pre):
    return 1/2*torch.sum((y_tre-y_pre)**2)

if __name__ == "__main__":
    input = torch.randn(20, 10)
    y = torch.from_numpy(np.array([0] *10 + [1]*10)).unsqueeze(1)
    nodes_list = [(10, 5), (5, 2), (2, 1)]
    model = DNN(nodes_list, y)
    out = model.forward(input)
    L = loss_function(y, out)
    L.backward()
    print(model.head_layer.next.w.grad[0,:5])
    print('-------------my backward-------------')
    model.backward()
    print(model.head_layer.next.w.grad[0, :5])


pcode, pamount, punit = input().split()  # split to separate the inputs after whitespace
pcode2, pamount2, punit2 = input().split()

pcode = int(pcode)
pamount = int(pamount)
pcode2 = int(pcode2)
pamount2 = int(pamount2)

punit = float(punit)
punit2 = float(punit2)

total = (pamount * punit) + (pamount2 * punit2)

print('VALOR A PAGAR: R$ {:.2f}'.format(total))


"""

49. Group Anagrams

Given an array of strings, group anagrams together.

Example:

Input: ["eat", "tea", "tan", "ate", "nat", "bat"],
Output:
[
  ["ate","eat","tea"],
  ["nat","tan"],
  ["bat"]
]
Note:

All inputs will be in lowercase.
The order of your output does not matter.

"""

import collections

def group_anagrams(strs):
    dic = collections.defaultdict(list)
    ans = []
    for each_word in strs:
        dic[tuple(sorted(each_word))].append(each_word)
    for key, values in dic.items():
        ans.append(values)
    return ans

group_anagrams(["eat", "tea", "tan", "ate", "nat", "bat"])