""" Training augmented model """
import os
from math import sqrt

import numpy as np
import torch
import torch.nn as nn

import utils
from bert_fineturn.data_processor.glue import glue_compute_metrics as compute_metrics
from config import AugmentConfig
from dist_util_torch import init_gpu_params, FileLogger
from kdTool import Emd_Evaluator, distillation_loss
from modeling import TinyBertForSequenceClassification
from models.augment_cnn import AugmentCNN

acc_tasks = ["mnli", "mrpc", "sst-2", "qqp", "qnli", "rte", "books"]
corr_tasks = ["sts-b"]
mcc_tasks = ["cola"]
os.environ['CUDA_VISIBLE_DEVICES'] = '7'
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
from transformers import RobertaForSequenceClassification, GPT2ForSequenceClassification

def convert_to_attn(hidns, mask):
    if type(hidns[0]) is not tuple:
        hdim = hidns[0].shape[-1]
        attns = [torch.matmul(x, x.transpose(2, 1)) / sqrt(hdim) for x in hidns]
        mask = mask.unsqueeze(1)
        mask = (1.0 - mask) * -10000.0
        attns = [torch.nn.functional.softmax(x + mask, dim=-1) for x in attns]
    else:
        hidns = [torch.stack(x, dim=1) for x in hidns]
        hdim = hidns[0][0].shape[-1]
        attns = [torch.matmul(x, x.transpose(-1, -2)) / sqrt(hdim) for x in hidns]
        mask = mask.unsqueeze(1).unsqueeze(2)
        mask = (1.0 - mask) * -10000.0
        attns = [torch.nn.functional.softmax(x + mask, dim=-1) for x in attns]
    return attns

def main():
    config = AugmentConfig()

    init_gpu_params(config)
    logger = FileLogger('./log', config.is_master, config.is_master)

    use_emd = config.use_emd

    # set seed
    np.random.seed(config.seed)
    torch.manual_seed(config.seed)
    torch.cuda.manual_seed_all(config.seed)

    torch.backends.cudnn.benchmark = True

    ############# LOADING DATA /START ###############
    task_name = config.datasets
    train_dataloader, _, eval_dataloader, output_mode, n_classes, config = utils.load_glue_dataset(config)
    logger.info(f"train_loader length {len(train_dataloader)}")

    ############# LOADING DATA /END ###############

    ############### BUILDING MODEL /START ###############

    model = AugmentCNN(config, n_classes, output_mode, auxiliary=False)
    pre_d, new_d = {}, {}
    for k, v in model.named_parameters():
        pre_d[k] = torch.sum(v)
    if config.teacher_type == 'gpt2':
        utils.load_gpt2_embedding_weight(model,
                                    config.teacher_model)
    elif config.teacher_type == 'bert':
        utils.load_bert_embedding_weight(model,
                                    config.teacher_model)
    elif config.teacher_type == 'roberta':
        utils.load_roberta_embedding_weight(model,
                                         config.teacher_model)
    for k, v in model.named_parameters():
        new_d[k] = torch.sum(v)

    logger.info("=" * 10 + "alter" + "=" * 10)
    for k in pre_d.keys():
        if pre_d[k] != new_d[k]:
            logger.info(k)
    del pre_d, new_d

    model = model.to(device)
    emd_tool = None
    if config.use_emd and config.use_kd:
        emd_tool = Emd_Evaluator(config.layers, 12, device)

    if not config.use_kd:
        teacher_model = None
    else:

        if config.teacher_type == 'gpt2':
            teacher_model = GPT2ForSequenceClassification.from_pretrained(config.teacher_model, num_labels=n_classes)
        elif config.teacher_type == 'bert':
            teacher_model = TinyBertForSequenceClassification.from_pretrained(config.teacher_model,num_labels=n_classes)
        elif config.teacher_type == 'roberta':
            teacher_model = RobertaForSequenceClassification.from_pretrained(config.teacher_model)
        teacher_model = teacher_model.to(device)
        teacher_model.eval()

    # model size
    mb_params = utils.param_size(model)
    if config.is_master:
        logger.info("Model size = {:.3f} MB".format(mb_params))

    optimizer = torch.optim.SGD(
        model.parameters(), config.lr, momentum=config.momentum, weight_decay=config.weight_decay)

    lr_scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, config.epochs/2, eta_min=config.lr_min)
    torch.autograd.set_detect_anomaly(True)
    best_top1 = 0

    ############### BUILDING MODEL /END ###############

    ############### TRAIN /START ###############
    # training loop
    for epoch in range(config.epochs):
        drop_prob = config.drop_path_prob * epoch / config.epochs
        model.drop_path_prob(drop_prob)

        # training
        train(logger, config, train_dataloader, model, teacher_model, optimizer, epoch, task_name.lower(), emd_tool=emd_tool)

        lr_scheduler.step()
        # validation
        cur_step = (epoch + 1) * len(train_dataloader)
        top1 = validate(logger, config, eval_dataloader, model, epoch, cur_step, task_name.lower(), "val")
        # top1 = validate(test_loader, model, criterion, epoch, cur_step, "test", len(task_types))
        # save
        if best_top1 < top1:
            best_top1 = top1
            is_best = True
        else:
            is_best = False
        utils.save_checkpoint(model, config.path, is_best)
        logger.info("Present best Prec@1 = {:.4%}".format(best_top1))
        print("")

    logger.info("Final best Prec@1 = {:.4%}".format(best_top1))
    ############### TRAIN /END ###############

def train(logger, config, train_loader, model, teacher_model, optimizer, epoch, task_name, emd_tool=None):
    top1 = utils.AverageMeter()
    losses = utils.AverageMeter()

    total_num_step = len(train_loader)
    cur_step = epoch * len(train_loader)
    cur_lr = optimizer.param_groups[0]['lr']

    logger.info("Epoch {} LR {}".format(epoch, cur_lr))
    # writer.add_scalar('train/lr', cur_lr, cur_step)
    model.train()

    if model.output_mode == "classification":
        criterion = nn.CrossEntropyLoss()
    elif model.output_mode == "regression":
        criterion = nn.MSELoss()

    for step, data in enumerate(train_loader):
        data = [x.to("cuda", non_blocking=True) for x in data]
        input_ids, input_mask, segment_ids, label_ids, seq_lengths = data
#       [32,128]    [32,128]   [32,128]      [32]         [32]
        X = [input_ids, input_mask, segment_ids, seq_lengths]
        y = label_ids

        N = X[0].size(0)

        optimizer.zero_grad()
        logits = model(X)  #[32,2] , [[32,128,768],[32,128,768],[32,128,768],[32,128,768]]
        if config.use_emd:
            logits, s_layer_out = logits
        if config.use_kd:
            # with torch.no_grad():
            #     check_ids = input_ids.cpu()  #[32,128]
            #     check_seg = segment_ids.cpu() #[32,128]
            #     mask_check = input_mask.cpu() #[32,128]

            if config.teacher_type == 'roberta' or config.teacher_type == 'gpt2':
                output_dict = teacher_model(input_ids, attention_mask=input_mask, output_hidden_states=True,return_dict=True)

                teacher_logits, teacher_reps = output_dict.logits,output_dict.hidden_states
            elif config.teacher_type == 'bert':
                teacher_logits, teacher_reps = teacher_model(input_ids, segment_ids, input_mask)
            # print(np.argmax(teacher_logits.detach().cpu().numpy(),axis=1))
            # print(label_ids.cpu().numpy())
            # print("#####################################################")
            kd_loss, _, _ = distillation_loss(logits, y, teacher_logits, model.output_mode, alpha=config.kd_alpha)
            rep_loss = 0
            if config.use_emd:
                if config.hidn2attn:
                    s_layer_out = convert_to_attn(s_layer_out, input_mask)
                    teacher_reps = convert_to_attn(teacher_reps, input_mask)
                rep_loss, flow, distance = emd_tool.loss(s_layer_out, teacher_reps, return_distance=True)
                if config.update_emd:
                    emd_tool.update_weight(flow, distance)
            # loss = kd_loss * config.emd_only + rep_loss * config.emd_rate
            loss = kd_loss
        else:
            loss = criterion(logits, y)
        loss.backward()

        nn.utils.clip_grad_norm_(model.parameters(), config.grad_clip)
        optimizer.step()

        preds = logits.detach().cpu().numpy()
        if model.output_mode == "classification":
            preds = np.argmax(preds, axis=1)
        elif model.output_mode == "regression":
            preds = np.squeeze(preds)        # top5.update(prec5.item(), N)
        result = compute_metrics(task_name, preds, y.detach().cpu().numpy())

        if task_name == "cola":
            train_acc = result['mcc']
        elif task_name in ["sst-2", "mnli", "mnli-mm", "qnli", "rte", 'books']:
            train_acc = result['acc']
        elif task_name in ["mrpc", "qqp"]:
            train_acc = result['f1']
        elif task_name == "sts-b":
            train_acc = result['corr']
        losses.update(loss.item(), N)
        top1.update(train_acc, N)

        if step % config.print_freq == 0 or step == total_num_step - 1:
            logger.info(
                "Train: , [{:2d}/{}] Step {:03d}/{:03d} Loss {:.3f} Prec {top1:.1%}"
                .format(epoch + 1, config.epochs, step, total_num_step - 1, losses.avg, top1=train_acc))
        cur_step += 1

def validate(logger, config, data_loader, model, epoch, cur_step, task_name, mode="dev"):
    top1 = utils.AverageMeter()
    losses = utils.AverageMeter()
    if model.output_mode == "classification":
        criterion = nn.CrossEntropyLoss()
    elif model.output_mode == "regression":
        criterion = nn.MSELoss()
    eval_labels = []
    model.eval()
    preds = []
    with torch.no_grad():
        for step, data in enumerate(data_loader):
            data = [x.to(device, non_blocking=True) for x in data]
            # input_ids, input_mask, segment_ids, label_ids, seq_lengths = data
            input_ids, input_mask, segment_ids, label_ids, seq_lengths = data

            X = [input_ids, input_mask, segment_ids, seq_lengths]
            y = label_ids
            N = X[0].size(0)
            logits = model(X)
            if config.use_emd:
                logits, _ = logits
            loss = criterion(logits, y)
            correct = torch.sum(torch.argmax(logits, axis=1) == y)

            if len(preds) == 0:
                preds.append(logits.detach().cpu().numpy())
            else:
                preds[0] = np.append(preds[0], logits.detach().cpu().numpy(), axis=0)
            eval_labels.extend(y.detach().cpu().numpy())

        preds = preds[0]
        if model.output_mode == "classification":
            preds = np.argmax(preds, axis=1)
        elif model.output_mode == "regression":
            preds = np.squeeze(preds)
        result = compute_metrics(task_name, preds, eval_labels)
        print(np.sum(preds == eval_labels), len(eval_labels), result)
        if task_name == "cola":
            acc = result['mcc']
        elif task_name in ["sst-2", "mnli", "mnli-mm", "qnli", "rte", 'books']:
            acc = result['acc']
        elif task_name in ["mrpc", "qqp"]:
            acc = result['f1']
        elif task_name == "sts-b":
            acc = result['corr']

    logger.info(mode + ": [{:2d}/{}] Final Prec@1 {:.4%}".format(epoch + 1, config.epochs, acc))
    return acc

if __name__ == "__main__":
    main()


import pytest
from time import sleep
from src.collect_functions import collect

# Testing the greetings

@pytest.fixture(name='greeting',scope='module') # Desse modo podemos usar a função greeting mais de uma vez guardando o resultado dela em cache
def greeting():
    greeting = 'hello'
    sleep(3)
    return greeting

class TestCollectInfo(object):
    def test_enter_params(self, greeting):
        actual = collect.collect_info('jon',60,1.70,greeting) # usando função greeting
        expected = 'hello Name: jon, Weight: 60.0, Height: 1.7, IMC: 21'
        assert actual == expected, f'actual:{actual}, expected:{expected}'

    def test_outer(self, greeting):
        actual = collect.collect_info('jon',60,2.0,greeting) # usando dnv
        expected = 'hello Name: jon, Weight: 60.0, Height: 2.0, IMC: 15'
        assert actual == expected, f'actual:{actual}, expected:{expected}'


#!/usr/bin/env fslpython

import sys
import subprocess
import vtk
import vtk.util.numpy_support as vtknp
import nibabel.nifti1 as nii
import scipy.ndimage.morphology

print('NB: make_mesh.py assumes radiological orientation!')

infile = sys.argv[1]
outroot = sys.argv[2]
threshold = float(sys.argv[3])
distweight = float(sys.argv[4])
iterations = int(sys.argv[5])

img = nii.Nifti1Image.load(infile)
zooms = img.get_header().get_zooms()
vol = img.get_data()

if distweight == 0.0:
    print('Not using distance weighting')
else:
    print('Using distance weighting')

distmap = scipy.ndimage.morphology.distance_transform_edt(vol < 95)
vol *= distweight * distmap ** 2 / 100 + 1

imd = vtk.vtkImageData()
imd.SetDimensions(vol.shape[0], vol.shape[1], vol.shape[2])
imd.SetSpacing(zooms[0], zooms[1], zooms[2])
imd.GetPointData().SetScalars(vtknp.numpy_to_vtk(vol.flatten('F'), deep = 1))

mcf = vtk.vtkImageMarchingCubes()
mcf.SetInputData(imd)
mcf.SetValue(0, threshold)
mcf.Update()

writer = vtk.vtkPolyDataWriter()
writer.SetInputData(mcf.GetOutput())
writer.SetFileName(outroot + '_orig_mesh.vtk')
writer.SetFileTypeToASCII()
writer.Write()

ndt = scipy.ndimage.morphology.distance_transform_edt(vol > threshold)
ndtimg = nii.Nifti1Image(ndt, img.get_affine())
ndtimg.to_filename(outroot + '_distmap')

subprocess.call("run_mesh_utils --doDeformSurface -i {0} -m {1} -o {2} --w_im=-0.2 --w_tan=1.0 --w_tri=1.0 --w_norm=0.0 -d {3} -t 0.2".format(
        outroot + '_distmap', outroot + '_orig_mesh.vtk', outroot + '_deformed_mesh.vtk', iterations), shell = True)


from typing import Tuple, List
import numpy as np
from sklearn.ensemble import RandomForestClassifier
import optuna
from sklearn.metrics import roc_auc_score
from sklearn.model_selection import StratifiedKFold, cross_val_score

def hyper_opt_random_forest(
    Model: RandomForestClassifier,
    X: np.ndarray,
    y: np.ndarray,
    timeout: int,
    seed: int,
    **kwargs,
) -> dict:
    skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=seed)

    def Objective(trial: optuna.Trial):
        param = {
            "n_estimators": trial.suggest_int("n_estimators", 50, 1000),
            "max_depth": trial.suggest_int("max_depth", 4, 50),
            "min_samples_split": trial.suggest_int("min_samples_split", 1, 150),
            "min_samples_leaf": trial.suggest_int("min_samples_leaf", 1, 60),
            "random_state": trial.suggest_categorical("random_state", [seed]),
        }

        model = Model(**param)
        score = cross_val_score(model, X, y, scoring="roc_auc", cv=skf).mean()

        return score

    study = optuna.create_study(
        direction="maximize",
        study_name="RandomForest optimization",
    )
    study.optimize(Objective, gc_after_trial=True, timeout=timeout)

    model = Model()
    base_score = cross_val_score(model, X, y, scoring="roc_auc", cv=skf).mean()

    hyperopt_score = study.best_value
    best_params = study.best_params

    if base_score > hyperopt_score:
        best_params = {}

    best_params["random_state"] = seed

    model = Model(**best_params)
    model.fit(X, y)

    return best_params


import os, time
from colorama import Fore

try:
    from selenium import webdriver
except ImportError:
    print("• Installing selenium...")
    os.system("pip install --upgrade selenium >nul")
    print("• Selenium installed.")
    from selenium import webdriver

cwd = os.getcwd() # Gets current working directory path
browser = ""

while True:
    browser = input(f"{Fore.LIGHTYELLOW_EX}Enter browser (chrome / edge): {Fore.RESET}").lower()
    if browser == "edge" or browser == "chrome":
        print(f"Browser set to {Fore.LIGHTGREEN_EX}%s{Fore.RESET}" % browser.capitalize())
        break

if browser == 'chrome':
    driver_path = cwd + "\\chromedriver.exe"
else:
    driver_path = cwd + "\\edgedriver.exe"

if os.path.exists(driver_path): # Checks if a chromedriver is installed
    print(f"{Fore.LIGHTMAGENTA_EX}Created by Neek8044{Fore.RESET}\n")
    print("Make sure Chrome / Edge is installed with the latest update\n")
    video_url = input("Video URL: ")

    Loop = False
    while Loop:
        try:
            duration = int(input("Video Duration (seconds): ")) - 3
            Loop = True
        except:
            continue

    if browser.lower() == 'chrome':
        # Configuring drivers to work with C:\chromedriver.exe
        driver_1 = webdriver.Chrome(executable_path = cwd + "\\chromedriver.exe")
        driver_2 = webdriver.Chrome(executable_path = cwd + "\\chromedriver.exe")
        driver_3 = webdriver.Chrome(executable_path = cwd + "\\chromedriver.exe")
        driver_4 = webdriver.Chrome(executable_path = cwd + "\\chromedriver.exe")
    else:
        # Configuring drivers to work with C:\chromedriver.exe
        driver_1 = webdriver.Edge(executable_path = cwd + "\\edgedriver.exe")
        driver_2 = webdriver.Edge(executable_path = cwd + "\\edgedriver.exe")
        driver_3 = webdriver.Edge(executable_path = cwd + "\\edgedriver.exe")
        driver_4 = webdriver.Edge(executable_path = cwd + "\\edgedriver.exe")

    # Starts drivers
    driver_1.get(video_url)
    driver_2.get(video_url)
    driver_3.get(video_url)
    driver_4.get(video_url)

    while True:
        time.sleep(duration)
        driver_1.refresh()
        driver_2.refresh()
        driver_3.refresh()
        driver_4.refresh()

else: # If no driver can be found, show error.
    print(f"{Fore.LIGHTRED_EX}ERROR: No driver could be found in the current working directory.\nLaunch installer and restart the program.")

input(f"\n\n{Fore.LIGHTBLUE_EX}Press enter key to exit...{Fore.RESET}") # Keep the screen alive by waiting for any input


#/usr/bin/env python

from itertools import product
import sys
import yaml

clangVersions = (
    '3.8',
    '3.7',
    '3.6',
)

perlVersions = (
    '5.24.0',
    '5.22.0',
    '5.20.0',
)

qtVersions = (
    '5.7.0',
    '5.6.0',
    '5.5.0',
    '5.4.0',
    '5.3.0',
    '5.2.0',
)

config = {
    'matrix': {
        'include': []
    }
}

for (clangVersion, qtVersion, perlVersion) in product(clangVersions, qtVersions, perlVersions):
    d = {
        'env': 'CLANG_VERSION={0} QT_VERSION={1} PERL_VERSION={2}'.format(
            clangVersion, qtVersion, perlVersion
        ),
        'addons': {
            'apt': {
                'sources': [
                    'ubuntu-toolchain-r-test',
                    'llvm-toolchain-precise-{0}'.format(clangVersion),
                    'george-edison55-precise-backports',
                ],
                'packages': [
                    'clang-{0}'.format(clangVersion),
                    'libclang-{0}-dev'.format(clangVersion),
                    'llvm-{0}-dev'.format(clangVersion),
                    'cmake',
                    'cmake-data',
                    'libgtest-dev',
                    'libedit-dev',
                ]
            }
        }
    }
    config['matrix']['include'].append(d)

yaml.dump(config, sys.stdout, default_flow_style=False)
print(len(config['matrix']['include']))


f1=open("rt.tab","r")#human mouse rat
file=iter(f1)
next(file)

f=open("rt.in","w")

s=''
num_line=0
num_node=set()
for line in file:
    p1,p2=line.rstrip().split("\t")
    num_line+=1
    p1=str(int(p1[2:])-1)
    p2=str(int(p2[2:])-1)
    num_node.add(p1)
    num_node.add(p2)
    s+=f'{p1} {p2}\n'

f.write(f"{str(len(num_node))} {str(num_line)}\n"+s)


import numpy as np
import copy

from statestream.visualization.base_widgets import Widget
from statestream.utils.pygame_import import pg, pggfx

# =============================================================================
# =============================================================================
# =============================================================================

class ListMSelWindow(Widget):
    def __init__(self,
                 parent,
                 wcollector,
                 font="small",
                 pos=np.zeros([2,], dtype=np.float32),
                 parameter=[],
                 value=[],
                 dvalue=None,
                 ptype=None,
                 modus=None,
                 cb_LMB_clicked=None):
        """Class for multiple item selection window widget.

            rect: Rectangle in global coordinates.
        """
        # Init parent class.
        Widget.__init__(self, parent=parent, pos=pos)

        # Copy initial data.
        self.wcollector = wcollector
        self.parameter = parameter
        self.value = value
        self.ptype = ptype
        self.modus = modus
        self.cb_LMB_clicked = cb_LMB_clicked
        self.entry_height = wcollector.vparam["font " + font]
        self.type = "ListMSelWindow"

        # Determine maximum entry length over list.
        self.max_len = 0
        for i in self.parameter:
            if self.max_len < len(i):
                self.max_len = len(i)

        # Initialize all values with their default.
        self.current_value = [dvalue for v in range(len(parameter))]

        # Create a frame widget as direct child.
        self.frame = wcollector.add_frame(parent=self,
                                          pos=np.array([0, 0], dtype=np.float32))
        # Go through list of parameters and add widgets.
        self.param_entries = []
        for p in range(len(self.parameter)):
            text = " " + str(self.parameter[p]).ljust(self.max_len + 2) + ": " \
                   + str(self.current_value[p])
            self.param_entries.append(wcollector.add_button(parent=self.frame,
                                     pos=np.array([0, p * self.entry_height],
                                                  dtype=np.float32),
                                     text=text,
                                     font="small",
                                     cb_LMB_clicked=lambda x=p: self.cb_item_clicked(x)))
        # Add the "Done" button.
        self.done = wcollector.add_button(parent=self.frame,
                                          pos=np.array([0, (1 + len(self.parameter)) \
                                                            * self.entry_height],
                                                        dtype=np.float32),
                                          text=" done ",
                                          cb_LMB_clicked=self.cb_done_clicked)

        # Determine and save size of frame.
        self.frame.get_size(self.wcollector.screen,
                            self.wcollector.vparam,
                            self.wcollector.sprites,
                            self.wcollector.fonts)

        # Rectangle for this widget (same as the frame).
        self.rect = pg.Rect(self.pos[0],
                            self.pos[1],
                            self.frame.width,
                            self.frame.height)

        # Finally add this to the collectors widget list.
        wcollector.widgets.append(self)
        wcollector.on_top(self)

    def cb_done_clicked(self):
        """This is the widget internal callback for a button press.
        """
        # Destroy the list selection window and all its children.
        self.wcollector.destroy(self)
        # Execute the widgets LMB click callback.
        self.cb_LMB_clicked(modus=self.modus,
                            ptype=self.ptype,
                            param=self.parameter,
                            value=self.current_value)

    def cb_item_clicked(self, p):
        """This handles the click on an list item.
        """
        # Count current value up by 1.
        next_idx = (self.value.index(self.current_value[p]) + 1) % len(self.value)
        self.current_value[p] = self.value[next_idx]
        # Create new text.
        text = " " + str(self.parameter[p]).ljust(self.max_len + 2) + ": " \
               + str(self.current_value[p])
        # Update text for drawing.
        self.param_entries[p].text = copy.copy(text)

    def draw(self, screen, vparam, sprites, fonts, ccol):
        """Method to draw this widget onto screen.
        """
        # Blit frame widget (buttons are children of frame and will be drawn there).
        self.frame.draw(screen, vparam, sprites, fonts, ccol)

# =============================================================================
# =============================================================================
# =============================================================================


import unittest
from KnowledgeBase import KnowledgeBase

class TestKnowledgeBase(unittest.TestCase):
    def test_SetFilterFeatures(self):
        kb = KnowledgeBase()
        ff = {
            'id': set(),
            'name': set({'app_name'}),
            'features': set({'feature1', 'feature2'}),
        }

        kb.setFilterFeatures(ff)
        self.assertEqual(kb.getFilterFeatures(), ff)

    def test_UpdateFilterFeatures(self):
        kb = KnowledgeBase()
        ff = {
            'id': set(),
            'name': set({'app_name'}),
            'features': set({'feature1', 'feature2'}),
        }
        for header in ff:
            for value in ff[header]:
                kb.updateFilterFeatures(header, [value])
        self.assertEqual(kb.getFilterFeatures(), ff)


from BO_interfaces.BO import *
import os
import argparse

# Command to run VASP executable.
VASP_RUN_COMMAND = 'mpirun -np 56 vasp_ncl'
# Define the name for output file.
OUTFILENAME = 'vasp.out'
# Define the path direct to the VASP pesudopotential.
VASP_PP_PATH = '/home/smoayedp/pseudopotential'

def parse_argument():
    """
    bounds: The parameter that determines the range of shift in
            Cartetian coordinates in (x,y,z) format

    kappa: The parameter to control exploration and exploitation.
           exploitation 0 <-- kappa --> 10 exploration

    threshold: Convergence threshold of Bayesian optimization process.
    """
    parser = argparse.ArgumentParser(description='params')
    parser.add_argument('--bounds', dest='bounds',
                        type=tuple, default=[2, 2, 2])
    parser.add_argument('--kappa', dest='kappa', type=float, default=4)
    parser.add_argument('--threshold', dest='threshold',
                        type=float, default=0.05)
    parser.add_argument('--import_kpath', dest='import_kpath',
                        type=bool, default=False)

    return parser.parse_args()

def main():
    args = parse_argument()
    k = args.kappa
    br = args.br
    import_kpath = args.import_kpath
    os.environ['VASP_PP_PATH'] = VASP_PP_PATH
    if os.path.exists('POSCAR_shifted'):
        os.remove('POSCAR_shifted')

    calculate(command=VASP_RUN_COMMAND, outfilename=OUTFILENAME,
              method='dftu', import_kpath=import_kpath)
    outcar = open("dftu/scf/OUTCAR")
    for l in outcar.readlines():
        if "energy without entropy" in l:
            energy = float(l.split()[7])
    with open("BO_result.txt", "w") as res_file:
        print(0, 0, 0, energy, file=res_file)

    threshold = args.threshold
    for i in range(100):
        if os.path.exists('POSCAR_shifted'):
            os.rename('POSCAR_shifted', 'POSCAR_shifted_' + str(i))

        bayesianOpt = bayesOpt_DFTU(path='./')
        obj_next = bayesianOpt.bo()
        if i == 0:
            obj_ref = obj_next
        else:
            if (np.abs(obj_next[0] - obj_ref[0]) < threshold) and (np.abs(obj_next[1] - obj_ref[1]) < threshold) and \
                    (np.abs(obj_next[2] - obj_ref[2]) < threshold):
                break
            obj_ref = obj_next

        calculate(command=VASP_RUN_COMMAND, outfilename=OUTFILENAME,
                  method='dftu', import_kpath=import_kpath)
        outcar = open("dftu/scf/OUTCAR")
        for l in outcar.readlines():
            if "energy without entropy" in l:
                energy = float(l.split()[7])
        with open("BO.txt", "a") as res_file:
            print(energy, file=res_file)

if __name__ == "__main__":
    main()


import torch

def gram_schmidt(input_mat, reverse=False, modified=False):
    """ Carries out the Gram-Schmidt orthogonalization of a matrix.

    Arguments:
        input_mat (torch.Tensor): A quadratic matrix that will be turned into an
            orthogonal matrix.
        reverse (bool): Starts gram Schmidt method beginning from the last
            column if set to True.
        modified (bool): Uses modified Gram-Schmidt as described.
    """
    mat_size = input_mat.shape[0]
    Q = torch.zeros(mat_size, mat_size,
                    device=input_mat.device, dtype=input_mat.dtype)

    if modified:
        if reverse:
            outer_iterator = range(mat_size - 1, -1, -1)
            def inner_iterator(k): return range(k, -1, -1)
        else:
            outer_iterator = range(mat_size)
            def inner_iterator(k): return range(k+1, mat_size)

        # This implementation mostly follows the description from
        # https://www.math.uci.edu/~ttrogdon/105A/html/Lecture23.html
        # The more complex form is due to pytorch not allowing for inplace
        # operations of variables needed for gradient computation.
        v = input_mat
        for j in outer_iterator:
            Q[:, j] = v[:, j] / torch.norm(v[:, j])

            v_old = v
            v = torch.zeros(mat_size, mat_size,
                            device=input_mat.device, dtype=input_mat.dtype)

            for i in inner_iterator(j):
                v[:, i] = v_old[:, i] \
                          - (torch.dot(Q[:, j].clone(), v_old[:, i])
                             * Q[:, j].clone())

    elif not modified:
        if reverse:
            outer_iterator = range(mat_size - 1, -1, -1)
            def inner_iterator(k): return range(mat_size - 1, k, -1)
        else:
            outer_iterator = range(mat_size)
            def inner_iterator(k): return range(k)

        for j in outer_iterator:
            v = input_mat[:, j]
            for i in inner_iterator(j):
                p = torch.dot(Q[:, i].clone(), v) * Q[:, i].clone()
                v = v - p

            Q[:, j] = v / torch.norm(v)

    return Q

def gram_schmidt_batched(input_mat, reverse=False, modified=False):
    """ Carries out the Gram-Schmidt orthogonalization of a matrix on an
        entire batch.

    Arguments:
        input_mat (torch.Tensor): A tensor containing quadratic matrices each of
            which will be orthogonalized of shape (batch_size, m, m).
        reverse (bool): Starts gram Schmidt method beginning from the last
            column if set to True.
        modified (bool): Uses modified Gram-Schmidt as described.

    Returns:
        Q (torch.Tensor): A batch of orthogonal matrices of same shape as
            input_mat.
    """
    batch_size = input_mat.shape[0]
    mat_size = input_mat.shape[1]
    Q = torch.zeros(batch_size, mat_size, mat_size,
                    device=input_mat.device, dtype=input_mat.dtype)

    if modified:
    #TODO implement batched version
        for i in range(input_mat.shape[0]):
            q = gram_schmidt(input_mat[i], reverse, modified)
            Q[i] = q
    elif not modified:
        if reverse:
            raise NotImplementedError
        else:
            outer_iterator = range(mat_size)
            def inner_iterator(k): return range(k)

        for j in outer_iterator:
            v = input_mat[:, :, j].view(batch_size, mat_size, 1)
            for i in inner_iterator(j):
                q_squeezed = Q[:, :, i].view(batch_size, 1, mat_size).clone()
                dot_products = torch.bmm(q_squeezed, v)
                p = dot_products.repeat((1, mat_size, 1)) \
                    * Q[:, :, i].unsqueeze(2).clone()
                v = v - p

            Q[:, :, j] = v.squeeze() / torch.norm(v, dim=1).repeat(1, mat_size)

    return Q


import json
#coding=utf-8
import time
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
import logging
import os
# Import WebClient from Python SDK (github.com/slackapi/python-slack-sdk)
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError

class WebDriver(object):

    def __init__(self):
        self.options = Options()

        self.options.binary_location = '/opt/headless-chromium'
        self.options.add_argument('--headless')
        self.options.add_argument('--no-sandbox')
        self.options.add_argument('--start-maximized')
        self.options.add_argument('--start-fullscreen')
        self.options.add_argument('--single-process')
        self.options.add_argument('--disable-dev-shm-usage')

    def get(self):
        driver = Chrome('/opt/chromedriver', options=self.options)
        return driver

def lambda_handler(event, context):
	instance_ = WebDriver()
	driver = instance_.get()
	options = Options()
	a = os.listdir('/tmp')
	for x in a:
		print(x)
	URL = os.environ.get("URL")

	driver.get(URL)

	S = lambda X: driver.execute_script('return document.body.parentNode.scroll'+X)
	driver.set_window_size(S('Width'),S('Height'))
	#driver.set_window_size(1920, 1080)
	# May need manual adjustment
	driver.find_element_by_tag_name('body').screenshot('/tmp/web_screenshot.png')

	driver.quit()
	# WebClient instantiates a client that can call API methods
	# When using Bolt, you can use either `app.client` or the `client` passed to listeners.
	client = WebClient(token=os.environ.get("SLACK_BOT_TOKEN"))
	logger = logging.getLogger(__name__)
	# The name of the file you're going to upload
	file_name = "/tmp/web_screenshot.png"
	# ID of channel that you want to upload file to
	channel_id= os.environ.get("channel_id")

	#client.chat_postMessage(channel=channel_id, text="hello")
	try:
		# Call the files.upload method using the WebClient
		# Uploading files requires the `files:write` scope
		result = client.files_upload(
			channels=channel_id,
			initial_comment="Ops-Analytics Job Status: <"+ URL +"|Ops360 link>",
			file=file_name,
		)
		# Log the result
		logger.info(result)

	except SlackApiError as e:
		logger.error("Error uploading file: {}".format(e))

		return {
			'statusCode': 200,
			'body': json.dumps('Hello from Lambda!')
		}


# -*- coding: UTF-8 -*-
"""
title: 编辑距离
Given two strings word1 and word2, return the minimum number of operations required to convert word1 to word2.
You have the following three operations permitted on a word:
    Insert a character
    Delete a character
    Replace a character

Example 1:
Input: word1 = "horse", word2 = "ros"
Output: 3
Explanation:
horse -> rorse (replace 'h' with 'r')
rorse -> rose (remove 'r')
rose -> ros (remove 'e')

Example 2:
Input: word1 = "intention", word2 = "execution"
Output: 5
Explanation:
intention -> inention (remove 't')
inention -> enention (replace 'i' with 'e')
enention -> exention (replace 'n' with 'x')
exention -> exection (replace 'n' with 'c')
exection -> execution (insert 'u')

Constraints:
0 <= word1.length, word2.length <= 500
word1 and word2 consist of lowercase English letters.
"""

class Solution:
    def minDistance(self, word1: str, word2: str) -> int:
        """动态规划。要将word1 转换为 word2，可理解为将word1和word2变为一样。假设dp[i][j]表示word1的前i个字符与word2的前j个字符之间的编辑距离(操作次数)。
        因为只有3种操作：一、在word1中插入一个字符(等同于在word2中删除一个字符)；二、在word1中删除一个字符(等同于在word2中插入一个字符)；
        三、在word1中替换一个字符。
        dp[i-1][j] ——> dp[i][j] 表示在word2的第j个字符后面插入一个与word1的第i个字符相同的字符，等同于在word1中删除一个字符；
        dp[i][j-1] ——> dp[i][j] 表示在word1的第i个字符后面插入一个与word2的第j个字符相同的字符；
        dp[i-1][j-1] ——> dp[i][j] 表示将word1的第i个字符替换为word2的第j个字符，注意：若word1的第i个字符与word2的第j个字符原本就相同，则无需替换。
        dp[i][j] = min(dp[i-1][j] + 1, dp[i][j-1] + 1, dp[i-1][j-1] + 1)
        因为状态转移方程只涉及到 i-1、i 这两行，所以dp数组可以只保存两行。
        又因为最终结果是要让word1和word2变为一样，所以交换这两个word的位置，并不影响最终结果，可以选择将长度更小的那个作为word2，从而减小列数，进一步降低空间复杂度。
        """
        if len(word2) > len(word1):
            return self.minDistance(word2, word1)
        m, n = len(word1), len(word2)
        # 一个空字符串 与 一个非空字符串，它们之间的编辑距离为非空字符串的长度。所以row=0与col=0时，dp值无需计算。每次都只需更新dp数组第2行的值。
        # 因为m>=n，所以m=0时，n必为0，结果为0 = dp[0][0] = dp[-1][-1]。
        # m > 0时，会进入下面的for循环，结果为dp[-1][-1]。所以最后直接返回dp[-1][-1]即可
        dp = [list(range(n + 1)), [0] * (n + 1)]
        for row in range(1, m + 1):
            for col in range(n + 1):
                if col == 0:
                    dp[1][col] = row
                else:
                    dp[1][col] = min(dp[0][col] + 1, dp[1][col - 1] + 1,
                                     dp[0][col - 1] + int(word1[row - 1] != word2[col - 1]))
            dp[0] = dp[1][:]
        return dp[-1][-1]

if __name__ == '__main__':
    print(Solution().minDistance(word1="intention", word2="execution"))


from datetime import datetime
from unittest import TestCase
from django.http import HttpRequest
from .models import Article, add_article

class TestArticleModel(TestCase):
    def setUp(self):
        self.data = (
            ('hello', ['ni hao me']),
            ('hello2', ['ni hao me']),
        )
        self.ids = [
            'nyduwGYBt6UyLsIJHvAf',
            'iidtwGYBt6UyLsIJq_CJ',
            'hydxwGYBt6UyLsIJz_GU',
        ]

    def init_test_data(self):
        pass

    def test_add(self):
        id = 0
        for title, tags in self.data:
            article = Article(meta={'id': self.ids[id]}, title=title, tags=tags)
            article.body = ''' looong text xxxxxx xxxx '''
            article.published_from = datetime.now()
            article.save()
            id += 1

        for i in range(2):
            article = Article.get(id=self.ids[i])
            assert article.title == self.data[i][0]
            assert article.tags == self.data[i][1]

    def test_mget(self):
        ret = Article.mget(self.ids)
        assert len(ret) == len(self.ids)

    def test_search_body(self):
        assert 1 == 1

    def tearDown(self):
        self.data = None


from django.urls import path, include
from apps.viajeros.views import index,viajeroCreate,viajeroEdit,viajeroDelete
app_name = "viajeros";

urlpatterns = [
    path('', index, name='index'),
    path('nuevo/', viajeroCreate, name='viajeroCreate'),
    path('actualizar/<int:id_viajero>/', viajeroEdit, name='viajeroEdit'),
    path('eliminar/<int:id_viajero>/', viajeroDelete, name='viajeroDelete'),
]


"""second

Revision ID: df10cad9abbb
Revises: 662beefb328e
Create Date: 2023-03-07 00:28:39.558977

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = 'df10cad9abbb'
down_revision = '662beefb328e'
branch_labels = None
depends_on = None

def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('payment_types',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('name_ru', sa.String(length=255), nullable=False),
    sa.Column('name_en', sa.String(length=255), nullable=True),
    sa.Column('name_uz', sa.String(length=255), nullable=True),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_payment_types_id'), 'payment_types', ['id'], unique=True)
    op.add_column('refills', sa.Column('payment_type_id', sa.Integer(), nullable=True))
    op.create_foreign_key(None, 'refills', 'payment_types', ['payment_type_id'], ['id'])
    # ### end Alembic commands ###

def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(None, 'refills', type_='foreignkey')
    op.drop_column('refills', 'payment_type_id')
    op.drop_index(op.f('ix_payment_types_id'), table_name='payment_types')
    op.drop_table('payment_types')
    # ### end Alembic commands ###


from models import *
import config, peewee

def how_long_month(whois, number=None):
    try:
        query2 = Names.select().where(Names.whois == whois).get()
        name = query2.name
    except peewee.ProgrammingError as px:
        if px.args[0] == 1146:
            print('Добавьте данные о номере в файл "Номера телефонов - фамилии агентов.txt"')
        name = whois

    if number:
        query = User.select().where(((User.number == number) | (User.number == f'<--{number}')) & (User.whois == whois))
    elif not number:
        query = User.select().where(User.whois == whois)

    time_result = 0
    internet = 0
    for i in query:
        long = i.how_long.split(':')
        t = i.time.split(':')

        if config.at < int(t[0]) < config.to:  # время рабочее с 7 до 18
            try:
                time_result += (int(long[0]) * 60) + int(long[1])
            except IndexError:
                pass
            except ValueError:
                internet += int(i.how_long.split('Kb')[0])

    return name, round(time_result/3600, 2), round(internet/1024, 2)

def sort_dictionary_by_value(dictionary):
    list_of_sorted_pairs = [(k, dictionary[k]) for k in sorted(dictionary.keys(), key=dictionary.get, reverse=False)]

    return list_of_sorted_pairs

def write_result(dict1, dict2):
    dict1 = sort_dictionary_by_value(dict1)
    dict2 = sort_dictionary_by_value(dict2)

    with open('Результаты.txt', 'w') as f:
        f.write('Разговоры: \n')
        for i in reversed(dict1):
            f.write(f'{i[0]} {i[1]} часа телефонных разговоров в рабочее время\n')

        f.write('Интернет: \n')
        for i in reversed(dict2):
            f.write(f'{i[0]} {i[1]}Mb траффика в рабочее время\n')

def lider():
    query = User.select().group_by(User.whois)
    phone = {}
    internet = {}
    for i in query:
        num = how_long_month(i.whois)
        phone[num[0]] = num[1]
        internet[num[0]] = num[2]

    write_result(phone, internet)

def rating_numbers():
    query = User.select().group_by(User.number)
    query2 = User.select().group_by(User.whois).get()
    phone = {}
    internet = {}

    for i in query:
        if i.number[0:3] == '<--':
            continue
        elif i.number.isdigit():
            num = how_long_month(query2.whois, i.number)
            phone[i.number] = num[1]
        else:
            num = how_long_month(query2.whois, i.number)
            internet[i.number] = num[2]

    write_result(phone, internet)


largest = None
smallest = None
while True:
    num = input("Enter a number: ")
    if num == "done":
            break
    try:
        x = float(num)
    except:
        print('Invalid input')
        continue
    if largest>x:
        largest = x
    if smallest<x:
        smallest=x
print("Maximum is", largest)
print("Minimum is", smallest)


# https://www.acmicpc.net/problem/2490

# 윷짝을 3번 던진다.
for i in range(0, 3):
    # 한 줄에 입력한 윷짝의 결과를 yuut 리스트에 저장한다.
    yuut = list(map(int, input().split(' ')))

    # 윷을 던져서 나온 배와 개수를 세고 변수에 저장한다.
    # 던져서 나온 것이 도개걸윷모 중 어떤 것인지 알기 위해서는 배나 등의 개수 중 하나만 알고 있어도 된다.
    bae_count = yuut.count(0)

    # 배가 1개면
    if bae_count == 1:
        # 도 출력
        print('A')
    # 배가 2개면
    elif bae_count == 2:
        # 개 출력
        print('B')
    # 배가 3개면
    elif bae_count == 3:
        # 걸 출력
        print('C')
    # 배가 4개면
    elif bae_count == 4:
        # 윷 출력
        print('D')
    # 그 외의 경우 : 배가 0개인 경우뿐이다.
    else:
        # 모 출력
        print('E')


import os
import os.path
import pickle
from copy import copy

os.environ["CUDA_VISIBLE_DEVICES"] = "-1"

import numpy as np
import matplotlib.pyplot as plt
import rosbag
from glob import glob

import pinocchio as pino
from scipy.signal import butter, filtfilt
from scipy.fft import fft
from scipy.interpolate import interp1d
from scipy.spatial.transform import Rotation as Rot

from manifold_planning.utils.manipulator import Iiwa
from manifold_planning.utils.feasibility import check_if_plan_valid, compute_cartesian_losses

from manifold_planning.utils.constants import Table1, Table2, Cup, Limits

root_dir = os.path.dirname(__file__)
package_dir = os.path.dirname(root_dir)

robot_file = os.path.join(root_dir, "manifold_planning", "iiwa_cup.urdf")
pino_model = pino.buildModelFromUrdf(robot_file)
pino_data = pino_model.createData()
joint_id = pino_model.getFrameId("F_link_cup")

package_path = os.path.join(os.path.dirname(__file__), "..")
file_name = os.path.join(package_path, "data/kinodynamic7fixed_12kg_validated/test/data.tsv")
data = np.loadtxt(file_name, delimiter="\t").astype(np.float32)#[:3]

def compute_object_coordinates(xyz, R):
    h = Cup.height
    w = Cup.width
    xyz_cuboid = np.array([
        # corners
        [w, w, h], [w, w, -h], [w, -w, h], [w, -w, -h],
        [-w, w, h], [-w, w, -h], [-w, -w, h], [-w, -w, -h],
        ## middle points on the edges
        # [w, w, 0], [w, -w, 0], [-w, w, 0], [-w, -w, 0],
        # [w, 0, h], [w, 0, -h], [-w, 0, h], [-w, 0, -h],
        # [0, w, h], [0, w, -h], [0, -w, h], [0, -w, -h],
        ## middle points on the faces
        # [w, 0, 0], [-w, 0, 0],
        # [0, w, 0], [0, -w, 0],
        # [0, 0, h], [0, 0, -h],
    ])[np.newaxis, :, :, np.newaxis]
    xyz_object = xyz[:, np.newaxis] + (R[:, np.newaxis] @ xyz_cuboid)[..., 0]
    return xyz_object

def cal_d(fs, dts):
    dfs = np.zeros_like(fs)
    dfs[1: -1] = (fs[2:, :] - fs[:-2, :]) / (dts[2:] - dts[:-2])
    dfs[0] = dfs[1]
    dfs[-1] = dfs[-2]
    return dfs

def rnea(q, dq, ddq):
    taus = []
    for i in range(len(q)):
        taus.append(pino.rnea(pino_model, pino_data, q[i], dq[i], ddq[i]))
    return np.array(taus)

def get_vel_acc(t, q_m):
    # Position
    bv, av = butter(6, 40, fs=1000)
    b, a = butter(6, 40, fs=1000)
    bt, at = butter(6, 30, fs=1000)
    # bv, av = butter(6, 4, fs=1000)
    # b, a = butter(6, 4, fs=1000)
    q_m_filter = q_m

    # Velocity
    dq_m = cal_d(q_m, t)
    dq_m_filter = filtfilt(bv, av, dq_m.copy(), axis=0)

    # Acceleration
    ddq_m = cal_d(dq_m_filter, t)
    ddq_m_filter = filtfilt(b, a, ddq_m.copy(), axis=0)

    torque = rnea(q_m.astype(np.float32), dq_m.astype(np.float32), ddq_m.astype(np.float32))
    # Torque
    tau_m = torque
    tau_m_filter = filtfilt(bt, at, tau_m.copy(), axis=0)

    return q_m_filter, dq_m_filter, ddq_m_filter, tau_m_filter, q_m, dq_m, ddq_m, tau_m

def compute_vel_acc_tau(t, q, qd, qd_dot, qd_ddot):
    desired_torque = rnea(qd, qd_dot, qd_ddot)
    q, dq, ddq, torque, q_, dq_, ddq_, torque_ = get_vel_acc(t[:, np.newaxis], q)
    return q, dq, ddq, torque, qd, qd_dot, qd_ddot, desired_torque

def forwardKinematics(q):
    xyz = []
    R = []
    for i in range(len(q)):
        pino.forwardKinematics(pino_model, pino_data, q[i])
        pino.updateFramePlacements(pino_model, pino_data)
        xyz_pino = pino_data.oMf[joint_id].translation
        R_pino = pino_data.oMf[joint_id].rotation
        xyz.append(copy(xyz_pino))
        R.append(copy(R_pino))
    xyz = np.stack(xyz)
    R = np.stack(R)
    return xyz, R

def compute_vertical_constraints(q):
    xyz, R = forwardKinematics(q)
    vertical_loss = 1. - R[:, 2, 2]
    angles = []
    for i in range(len(R)):
        angles.append(Rot.from_matrix(R[i]).as_euler("xyz"))
    return vertical_loss, np.array(angles)

def if_valid_box_constraints(q, zh1, zh2):
    allowed_violation = 0.02
    xyz, R = forwardKinematics(q)

    def dist2box(xyz, xl, yl, zl, xh, yh, zh):
        l = np.stack([xl, yl, zl], axis=-1)
        h = np.stack([xh, yh, zh], axis=-1)
        xyz_dist = np.max(np.stack([l - xyz, np.zeros_like(xyz), xyz - h], axis=-1), axis=-1)
        dist = np.sqrt(np.sum(np.square(xyz_dist), axis=-1) + 1e-8)
        return dist

    def inside_box(xyz, xl, yl, zl, xh, yh, zh):
        pxl = xyz[..., 0] > xl + allowed_violation
        pxh = xyz[..., 0] < xh - allowed_violation
        pyl = xyz[..., 1] > yl + allowed_violation
        pyh = xyz[..., 1] < yh - allowed_violation
        pzl = xyz[..., 2] > zl + allowed_violation
        pzh = xyz[..., 2] < zh - allowed_violation
        return np.all(np.stack([pxl, pxh, pyl, pyh, pzl, pzh], axis=-1), axis=-1)

    robot_dist2box_1 = dist2box(xyz, Table1.xl, Table1.yl, -1e10, Table1.xh, Table1.yh, zh1 - Cup.height)
    robot_dist2box_2 = dist2box(xyz, Table2.xl, Table2.yl, -1e10, Table2.xh, Table2.yh, zh2 - Cup.height)
    inside_box_1 = inside_box(xyz, Table1.xl, Table1.yl, -1e10, Table1.xh, Table1.yh, zh1 - Cup.height)
    inside_box_2 = inside_box(xyz, Table2.xl, Table2.yl, -1e10, Table2.xh, Table2.yh, zh2 - Cup.height)

    xyz_object = compute_object_coordinates(xyz, R)

    object_inside_box_1 = inside_box(xyz_object, Table1.xl, Table1.yl, -1e10, Table1.xh, Table1.yh,
                                     zh1 - Cup.height)
    object_inside_box_2 = inside_box(xyz_object, Table2.xl, Table2.yl, -1e10, Table2.xh, Table2.yh,
                                     zh2 - Cup.height)

    return not np.any(inside_box_1) and not np.any(inside_box_2) and \
           np.all(robot_dist2box_1 > 0.15 - allowed_violation) and \
           np.all(robot_dist2box_2 > 0.15 - allowed_violation) and \
           not np.any(object_inside_box_1) and not np.any(object_inside_box_2)

def read_bag(bag):
    time = []
    joint_state_desired = []
    joint_state_actual = []
    joint_state_error = []
    planning_time = None
    success = None
    planned_motion_time = None

    for topic, msg, t_bag in bag.read_messages():
        if topic in ["/bspline_ff_kino_joint_trajectory_controller/state"]:
            n_joints = len(msg.joint_names)
            time.append(msg.header.stamp.to_sec())
            joint_state_desired.append(np.concatenate(
                [msg.desired.positions, msg.desired.velocities, msg.desired.accelerations, msg.desired.effort]))
            joint_state_actual.append(np.concatenate(
                [msg.actual.positions, msg.actual.velocities, msg.actual.accelerations, msg.actual.effort]))
            joint_state_error.append(np.concatenate(
                [msg.error.positions, msg.error.velocities, msg.error.accelerations, msg.error.effort]))
        elif topic == "/neural_planner/status":
            planning_time = msg.planning_time
            planned_motion_time = msg.planned_motion_time
            success = msg.success
    desired = np.array(joint_state_desired).astype(np.float32)
    actual = np.array(joint_state_actual).astype(np.float32)
    error = np.array(joint_state_error).astype(np.float32)
    t = np.array(time)
    t -= t[0]
    t = t.astype(np.float32)
    return dict(t=t, actual=actual, desired=desired, error=error, planning_time=planning_time, success=success,
                planned_motion_time=planned_motion_time)

def compute_metrics(bag_path):
    # bag_path = os.path.join(package_dir, "bags", bag_path)
    # bag_path = os.path.join(package_dir, "bags/ours_nn/K9.bag")
    bag_file = rosbag.Bag(bag_path)
    i = int(bag_path.split("/")[-1][:-4])
    data_i = data[i]
    zh1 = data_i[16]
    zh2 = data_i[19]

    result = {}

    bag_dict = read_bag(bag_file)
    q, dq, ddq, torque, qd, qd_dot, qd_ddot, torqued = compute_vel_acc_tau(bag_dict["t"],
                                                                           bag_dict["actual"][:, :7],
                                                                           bag_dict["desired"][:, :7],
                                                                           bag_dict["desired"][:, 7:14],
                                                                           bag_dict["desired"][:, 14:21])
    #plt.plot(bag_dict["t"], qd_dot[..., :2], 'r')
    # plt.plot(bag_dict["t"], dq[..., :2], 'm')
    moving = np.linalg.norm(qd_dot, axis=-1) > 5e-2
    if not np.any(moving):
        result["valid"] = 0
        result["finished"] = 0
        result["planning_time"] = bag_dict["planning_time"]
        return result
    q = q[moving]
    dq = dq[moving]
    ddq = ddq[moving]
    torque = torque[moving]
    qd = qd[moving]
    qd_dot = qd_dot[moving]
    qd_ddot = qd_ddot[moving]
    torqued = torqued[moving]
    t = bag_dict["t"][moving]

    #plt.plot(t, qd_dot[..., :2], 'b')
    #plt.show()

    vertical_loss, angles = compute_vertical_constraints(q)

    box_constraints = if_valid_box_constraints(q, zh1, zh2)
    vertical_constraints = np.all(vertical_loss < 0.05)
    alpha_beta = np.sum(np.abs(angles[:, :2]), axis=-1)

    torque_limits = 1.0 * np.array([320, 320, 176, 176, 110, 40, 40], dtype=np.float32)[np.newaxis]
    q_dot_limits = 1.0 * np.array([1.4835, 1.4835, 1.7453, 1.3090, 2.2689, 2.3562, 2.3562], dtype=np.float32)[np.newaxis]
    q_ddot_limits = 10. * q_dot_limits
    torque_constraints = np.all(np.abs(torque) < torque_limits)
    dq_constraints = np.all(np.abs(dq) < q_dot_limits)
    ddq_constraints = np.all(np.abs(ddq) < q_ddot_limits)
    #for i in range(7):
    #    plt.subplot(331 + i)
    #    plt.plot(np.abs(dq[:, i]))
    #    plt.plot([0, dq.shape[0]], [q_dot_limits[0, i], q_dot_limits[0, i]])
    #plt.show()
    #for i in range(7):
    #    plt.subplot(331 + i)
    #    plt.plot(np.abs(ddq[:, i]))
    #    plt.plot([0, ddq.shape[0]], [q_ddot_limits[0, i], q_ddot_limits[0, i]])
    #plt.show()
    #for i in range(7):
    #    plt.subplot(331 + i)
    #    plt.plot(np.abs(torque[:, i]))
    #    plt.plot([0, torque.shape[0]], [torque_limits[0, i], torque_limits[0, i]])
    #plt.show()

    #dist_2_goal = np.linalg.norm(data_i[7:14] - q[-1])
    #finished = dist_2_goal < 0.2
    xyz_final, _ = forwardKinematics([q[-1]])
    xyz_desired, _ = forwardKinematics([data_i[7:14]])
    dist_2_goal = np.linalg.norm(xyz_final[0] - xyz_desired[0])
    finished = dist_2_goal < 0.2
    #valid = box_constraints and vertical_constraints and torque_constraints and dq_constraints and ddq_constraints and finished
    valid = box_constraints and vertical_constraints and torque_constraints and dq_constraints and finished
    #valid = box_constraints and vertical_constraints and finished

    ee, _ = forwardKinematics(q)
    ee_d, _ = forwardKinematics(qd)

    # plt.plot(ee[..., 0], ee[..., 1], 'r')
    # plt.plot(ee_d[..., 0], ee_d[..., 1], 'g')
    # plt.plot(bag_dict['puck_pose'][:, 0], bag_dict['puck_pose'][:, 1], 'b')
    # plt.show()

    #for i in range(6):
    #   plt.subplot(321 + i)
    #   plt.plot(t, q[..., i], 'r')
    #   plt.plot(t, qd[..., i], 'g')
    #plt.show()

    # for i in range(6):
    #    plt.subplot(321 + i)
    #    plt.plot(t, np.abs(q[..., i] - qd[..., i]))
    #    plt.plot(t, np.linalg.norm(ee_d[..., :2] - ee[..., :2], axis=-1))
    # plt.show()

    movement_time = t[-1] - t[0]

    dt = np.diff(t)
    integral = lambda x: np.sum(np.abs(x)[1:] * dt)
    # reduce_integral = lambda x: integral(np.sum(np.abs(x), axis=-1))
    reduce_integral = lambda x: integral(np.linalg.norm(x, axis=-1))

    result["valid"] = int(valid)
    result["finished"] = int(finished)
    result["vertical_loss"] = integral(vertical_loss)
    result["planning_time"] = bag_dict["planning_time"]
    result["motion_time"] = movement_time

    result["alpha_beta"] = integral(alpha_beta)
    result["joint_trajectory_error"] = reduce_integral(qd - q)
    result["cartesian_trajectory_error"] = reduce_integral(ee_d - ee)

    print(result)
    return result

#planners = ["ours", "nlopt", "sst", "cbirrt", "mpcmpnet"]
#planners = ["nlopt", "sst", "cbirrt", "mpcmpnet"]
#planners = ["ours"]
#planners = ["ours_long"]
#planners = ["cbirrt"]
#planners = ["nlopt"]
#planners = ["mpcmpnet"]
#planners = ["sst"]
#planners = ["ours_n10"]
#planners = ["ours_n20"]
#planners = ["ours_l256", "ours_l512", "ours_l1024"]
#planners = ["ours_l64"]
#planners = ["ours_l128"]
#planners = ["ours_l128_long", "ours_l256_long", "ours_l3072_long"]
#planners = ["ours_l1024_long"]
#planners = ["ours_l512_long"]
#planners = ["ours_l64_long"]
#planners = ["ours_l2048s"]
#planners = ["ours_10kg"]
#planners = ["ours_12kg"]
planners = ["ours_13kg"]
#planners = ["ours_16kg"]
#planners = ["ours_18kg"]
#planners = ["ours_l2048_long"]
package_dir = "/home/piotr/b8/ah_ws/data"
for planner in planners:
    print(planner)
    dir_path = os.path.join(package_dir, "kino_exp", planner)
    sp = dir_path.replace("data", "results")
    os.makedirs(sp, exist_ok=True)
    for i, p in enumerate(glob(os.path.join(dir_path, "*.bag"))):
    #for i, p in enumerate([os.path.join(dir_path, f"{str(x).zfill(3)}.bag") for x in [16, 23, 24, 42, 44, 67, 72]]):
    #for i, p in enumerate([os.path.join(dir_path, f"{str(x).zfill(3)}.bag") for x in [1, 12, 16, 24, 39, 42, 44, 65, 74, 91]]):
    #for i, p in enumerate([os.path.join(dir_path, f"{str(x).zfill(3)}.bag") for x in [0, 58]]):
    #for i, p in enumerate([os.path.join(dir_path, f"{str(x).zfill(3)}.bag") for x in [24, 44, 53, 80, 93, 94]]):
    #for i, p in enumerate(glob(os.path.join(dir_path, "00*.bag"))):
    #for i, p in enumerate(glob(os.path.join(dir_path, "*42.bag"))):
    #for i, p in enumerate([os.path.join(dir_path, f"{str(x).zfill(3)}.bag") for x in [0, 6, 16, 21, 23, 24, 42, 44, 65, 66, 67, 72, 92]]):
        print(i)
        d = compute_metrics(p)
        save_path = p[:-3] + "res"
        save_path = save_path.replace("data", "results")
        with open(save_path, 'wb') as fh:
            pickle.dump(d, fh)


import tweepy
import time
from tweepy import OAuthHandler

import twitter_credentials

auth = OAuthHandler(twitter_credentials.CONSUMER_KEY, twitter_credentials.CONSUMER_SECRET)
auth.set_access_token(twitter_credentials.ACCESS_TOKEN, twitter_credentials.ACCESS_TOKEN_SECRET)
api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)

user = api.me()
search = 'Javascript'
numberOfTweets = 500

for tweet in tweepy.Cursor(api.search, search).items(numberOfTweets):
    try:
        print('Tweet liked')
        tweet.favorite()
        time.sleep(10)
    except tweepy.TweepError as e:
        print(e.reason)
    except StopIteration:
        break


from django.urls import path
from .views import (
    ClientTableView, ClientView, ContractingView, EstablishmentView, GetClientsView, GetCompaniesToCreateClient, GetCompaniesToCreateEstablishment, GetEstablishmentsToCreateClient, GetPriceTablesToCreateClient, SpecificClient, SpecificClientTable, SpecificContracting, SpecificEstablishment, CompanyView, SpecificCompany
)

urlpatterns = [
    path('contracting', ContractingView.as_view()),
    path('contracting/<contracting_code>', SpecificContracting.as_view()),
    #Company
    path('company', CompanyView.as_view()),
    path('company/<company_compound_id>', SpecificCompany.as_view()),
    # Establishment
    path('establishment', EstablishmentView.as_view()),
    path('establishment/<establishment_compound_id>', SpecificEstablishment.as_view()),
    # Client table
    path('client_table', ClientTableView.as_view()),
    path('client_table/<client_table_compound_id>', SpecificClientTable.as_view()),
    # Client
    #  path('client_tables_to_create_client', GetClientTablesToCreateClient.as_view()),
    path('companies_to_create_client', GetCompaniesToCreateClient.as_view()),
    path('establishments_to_create_client/<client_table_compound_id>', GetEstablishmentsToCreateClient.as_view()),
    path('price_tables_to_create_client/<company_compound_id>', GetPriceTablesToCreateClient.as_view()),
    path('get_clients/<client_table_compound_id>', GetClientsView.as_view()),
    path('client', ClientView.as_view()),
    path('client/<client_compound_id>', SpecificClient.as_view()),
    path('companies_to_create_establishment', GetCompaniesToCreateEstablishment.as_view()),

]


import re
with open("files/regex-test.txt") as names:
    for line in names.readlines():
        valid = re.match("([A-Z][a-z]+)( [A-Z][a-z]*){1,2}", line)
        if valid:
            print(valid.group())
        else:
            print(valid)

names.close()


#--------------------------------
# Name:         et_demands_zonal_stats.py
# Purpose:      Calculate zonal stats for ET zones
#--------------------------------

import argparse
from collections import defaultdict
import datetime as dt
import logging
import os
import pprint
import sys

from osgeo import ogr, osr
import pandas as pd
import rtree
from shapely.ops import unary_union
from shapely.wkt import loads

import _arcpy
import _gdal_common as gdc
import _util as util

def main(ini_path, overwrite_flag=False):
    """Calculate zonal statistics needed to run ET-Demands model

    Parameters
    ----------
    ini_path : str
        File path of the parameter INI file.
    overwrite_flag : boolean
        True : overwrite existing shapefile
        False : default

    Returns
    -------
    None

    """
    logging.info('\nComputing ET-Demands Zonal Stats')

    logging.debug('INI: {}'.format(ini_path))
    config = util.read_ini(ini_path, section='CROP_ET')

    gis_ws = config.get('CROP_ET', 'gis_folder')
    zone_path = config.get('CROP_ET', 'cells_path')
    crop_path = config.get('CROP_ET', 'crop_path')
    awc_path = config.get('CROP_ET', 'awc_path')
    clay_path = config.get('CROP_ET', 'clay_path')
    sand_path = config.get('CROP_ET', 'sand_path')

    crop_field = config.get('CROP_ET', 'crop_field')

    crosswalk_path = config.get('CROP_ET', 'crosswalk_path')

    soil_crop_mask_flag = config.getboolean('CROP_ET', 'soil_crop_mask_flag')
    save_crop_mask_flag = config.getboolean('CROP_ET', 'save_crop_mask_flag')

    # TODO: Read field names from INI
    cell_lat_field = 'LAT'
    cell_lon_field = 'LON'
    # cell_id_field = 'CELL_ID'
    # cell_name_field = 'CELL_NAME'
    # cell_station_id_field = 'STATION_ID'
    acreage_field = 'AG_ACRES'
    awc_field = 'AWC'
    clay_field = 'CLAY'
    sand_field = 'SAND'
    awc_in_ft_field = 'AWC_IN_FT'
    hydgrp_num_field = 'HYDGRP_NUM'
    hydgrp_field = 'HYDGRP'

    # +/- buffer distance (in zone units)
    simplify_threshold = 0.01

    sqm_2_acres = 0.000247105381
    sqft_2_acres = 0.0000229568

    # Check if crosswalk file exists
    if not os.path.isfile(zone_path):
        logging.error(
            '\nERROR: The ET zone shapefile doesn\'t exist, exiting\n'
            '  {}'.format(zone_path))
        sys.exit()
    elif not os.path.isfile(crop_path):
        logging.error(
            '\nERROR: The crop shapefile doesn\'t exist, exiting\n'
            '  {}'.format(crop_path))
        sys.exit()
    elif not os.path.isfile(crosswalk_path):
        logging.error(
            '\nERROR: The CDL Crosswalk file does not exist, exiting\n'
            ' Check the filename:  {}'.format(crosswalk_path))
        sys.exit()

    # Scratch files
    scratch_ws = os.path.join(gis_ws, 'scratch')
    if not os.path.isdir(scratch_ws):
        os.makedirs(scratch_ws)
    zone_crop_path = os.path.join(scratch_ws, 'zone_crop.shp')

    # if os.name == 'posix':
    #     shell_flag = False
    # else:
    #     shell_flag = True

    shp_driver = ogr.GetDriverByName('ESRI Shapefile')

    # Master zonal stats dictionary
    crop_stats = defaultdict(dict)
    zone_stats = defaultdict(dict)

    # Link zones to crops (and crops to zones, but this isn't being used)
    zone_crops = defaultdict(list)
    # crop_zones = defaultdict(list)

    # # Copy the zone_path
    # if overwrite_flag and _arcpy.exists(et_cells_path):
    #     _arcpy.delete(et_cells_path)
    # # Just copy the input shapefile
    # if not _arcpy.exists(et_cells_path):
    #     _arcpy.copy(zone_path, et_cells_path)

    # Add lat/lon fields
    logging.info('\nAdding Fields')
    zone_field_list = _arcpy.list_fields(zone_path)
    if cell_lat_field not in zone_field_list:
        logging.debug('  {}'.format(cell_lat_field))
        _arcpy.add_field(zone_path, cell_lat_field, ogr.OFTReal)
    if cell_lon_field not in zone_field_list:
        logging.debug('  {}'.format(cell_lon_field))
        _arcpy.add_field(zone_path, cell_lon_field, ogr.OFTReal)

    # # Cell ID/name
    # if cell_id_field not in zone_field_list:
    #     logging.debug('  {}'.format(cell_id_field))
    #     _arcpy.add_field(zone_path, cell_id_field, ogr.OFTString, width=24)
    # if cell_name_field not in zone_field_list:
    #     logging.debug('  {}'.format(cell_name_field))
    #     _arcpy.add_field(zone_path, cell_name_field, ogr.OFTString,
    #                      width=48)

    # Add soil fields
    if awc_field not in zone_field_list:
        logging.debug('  {}'.format(awc_field))
        _arcpy.add_field(zone_path, awc_field, ogr.OFTReal)
    if clay_field not in zone_field_list:
        logging.debug('  {}'.format(clay_field))
        _arcpy.add_field(zone_path, clay_field, ogr.OFTReal)
    if sand_field not in zone_field_list:
        logging.debug('  {}'.format(sand_field))
        _arcpy.add_field(zone_path, sand_field, ogr.OFTReal)
    if awc_in_ft_field not in zone_field_list:
        logging.debug('  {}'.format(awc_in_ft_field))
        _arcpy.add_field(zone_path, awc_in_ft_field, ogr.OFTReal,
                         width=8, precision=4)
    if hydgrp_num_field not in zone_field_list:
        logging.debug('  {}'.format(hydgrp_num_field))
        _arcpy.add_field(zone_path, hydgrp_num_field, ogr.OFTInteger)
    if hydgrp_field not in zone_field_list:
        logging.debug('  {}'.format(hydgrp_field))
        _arcpy.add_field(zone_path, hydgrp_field, ogr.OFTString, width=1)

    if acreage_field not in zone_field_list:
        logging.debug('  {}'.format(acreage_field))
        _arcpy.add_field(zone_path, acreage_field, ogr.OFTReal)

    # Crop fields are only added for needed crops below
    # for crop_num in crop_num_list:
    #     field_name = 'CROP_{0:02d}'.format(crop_num)
    #     if field_name not in zone_field_list:
    #         logging.debug('  {}'.format(field_name))
    #         _arcpy.add_field(zone_path, field_name, ogr.OFTInteger)

    # Rebuild the field list
    zone_field_list = _arcpy.list_fields(zone_path)

    # Update field width/precision
    logging.debug('\nUpdating ET zone field width and precision')
    shp_driver = ogr.GetDriverByName('ESRI Shapefile')
    input_ds = shp_driver.Open(zone_path, 1)
    input_lyr = input_ds.GetLayer()
    input_defn = input_lyr.GetLayerDefn()
    logging.debug('  {:<10} {:<10} {:<5} {:<9}'.format(
        'Name', 'Type', 'Width', 'Precision'))
    for i in range(input_defn.GetFieldCount()):
        fieldDefn = input_defn.GetFieldDefn(i)
        copyDefn = ogr.FieldDefn(fieldDefn.GetName(), fieldDefn.GetType())
        logging.debug('  {:<10s} {:<10s} {:>5d} {:>9d}'.format(
            fieldDefn.GetName(),
            fieldDefn.GetFieldTypeName(fieldDefn.GetType()),
            fieldDefn.GetWidth(), fieldDefn.GetPrecision()
        ))
        if (fieldDefn.GetFieldTypeName(fieldDefn.GetType()) == 'Real' and
                fieldDefn.GetWidth() < 24 and
                fieldDefn.GetPrecision() > 0):
            copyDefn.SetWidth(24)
            copyDefn.SetPrecision(15)
        else:
            continue
        input_lyr.AlterFieldDefn(i, copyDefn, (ogr.ALTER_WIDTH_PRECISION_FLAG))
    input_ds = None

    # Calculate lat/lon
    logging.info('\nCalculating ET zone lat/lon')
    cell_lat_lon_func(zone_path, cell_lat_field, cell_lon_field)

    # Load the ET zones shapefile geometries into memory
    # Build the spatial index in the zone spatial reference
    logging.debug('\nReading ET zone shapefile features')
    zone_full_rtree = rtree.index.Index()
    zone_full_wkt_dict = dict()
    zone_full_ds = shp_driver.Open(zone_path, 0)
    zone_full_lyr = zone_full_ds.GetLayer()
    zone_full_osr = zone_full_lyr.GetSpatialRef()

    # Check that the ET zones shapefile is in a projected coordinate system
    if zone_full_osr.IsGeographic():
        logging.error('\nERROR: The ET zones shapefile must be in a '
                      'projected coordinate system, exiting')
        sys.exit()
    zone_full_unit = zone_full_osr.GetLinearUnitsName()
    if zone_full_unit.upper() not in ['METER', 'METERS','METRE']:
        logging.error('\nERROR: Unsupported unit type: {}'.format(
            zone_full_unit))
        sys.exit()

    for zone_ftr in zone_full_lyr:
        zone_fid = zone_ftr.GetFID()
        zone_geom = zone_ftr.GetGeometryRef()
        zone_geom = zone_geom.Buffer(0)
        zone_extent = gdc.Extent(zone_geom.GetEnvelope())
        zone_extent = zone_extent.ogrenv_swap()
        zone_full_rtree.insert(zone_fid, list(zone_extent))
        zone_full_wkt_dict[zone_fid] = zone_geom.ExportToWkt()
    zone_full_ds = None

    # DEADBEEF - Commented out for testing
    # Read the crop shapefile and identify intersecting features
    logging.debug('\nReading crop shapefile features')
    crop_dict = defaultdict(dict)
    crop_ds = shp_driver.Open(crop_path, 0)
    crop_lyr = crop_ds.GetLayer()
    crop_osr = crop_lyr.GetSpatialRef()
    crop_tx = osr.CoordinateTransformation(crop_osr, zone_full_osr)
    # crop_lyr_name = zones_lyr.GetName()
    for crop_ftr in crop_lyr:
        crop_fid = crop_ftr.GetFID()
        if crop_fid % 100000 == 0 and crop_fid != 0:
            print('test')
            logging.info('FID: {}'.format(crop_fid))
        crop_geom = crop_ftr.GetGeometryRef()
        proj_geom = crop_geom.Clone()
        proj_geom.Transform(crop_tx)
        proj_geom = proj_geom.Buffer(0)
        proj_extent = gdc.Extent(proj_geom.GetEnvelope())
        proj_extent = proj_extent.ogrenv_swap()
        zone_fid_list = list(zone_full_rtree.intersection(list(proj_extent)))
        if not zone_fid_list:
            continue

        # Link zones to crops and crops to zones
        for zone_fid in zone_fid_list:
            zone_crops[zone_fid].append(crop_fid)
        # crop_zones[crop_fid] = zone_fid_list

        crop_dict[crop_fid] = {
            'fid': crop_fid,
            'wkt' :proj_geom.ExportToWkt(),
            'value': crop_ftr.GetField(crop_field),
        }
    crop_ds = None

    # Read ET demands crop number crosswalk
    # Link ET demands crop number (1-84) with input crop values (i.e. CDL)
    # Key is input crop number, value is crop number, ignore comment
    # note that crosswalk list cannot contain NaN or empty entries
    # NaN in etd_no forces data to type float and breaks str/int logic below (line 284)
    logging.info('\nReading Crop Crosswalk File\n  {}'.format(crosswalk_path))
    cross_df = pd.read_csv(crosswalk_path)
    cross_dict = dict()
    for index, row in cross_df.iterrows():
        # cross_dict[int(row.cdl_no)] = list(map(int, str(row.etd_no).split(',')))
        cross_dict[row.cdl_no] = list(map(int, str(row.etd_no).split(',')))
    # logging.debug(crop_num_dict)

    # Build the crop list
    # Because the spatial index is extent based,
    #   this may include crops that don't intersect the zones.
    input_crops = sorted(list(set(c['value'] for c in crop_dict.values())))
    try:
        etd_crops = sorted(list(set(
            x for c in crop_dict.values() for x in cross_dict[c['value']])))
    except KeyError as e:
        logging.error('\nError: Input crop not found in crosswalk file. '
                      'Missing Crop: {}\n Exiting.'.format(e))
        sys.exit()

    logging.info('\nInput Crops: {}'.format(', '.join(map(str, input_crops))))
    logging.info('Demands Crops: {}'.format(', '.join(map(str, etd_crops))))

    # Build the crop clipped ET zones shapefile
    # The shapefile only needs to be saved if the soils are being masked to
    #   the agricultural areas.  It would probably be possibly to avoid saving
    #   and keep the geometries in memory instead.
    if save_crop_mask_flag:
        logging.info('\nBuilding crop clipped zone shapefile')
        if os.path.exists(zone_crop_path):
            shp_driver.DeleteDataSource(zone_crop_path)
        zone_crop_ds = shp_driver.CreateDataSource(zone_crop_path)
        zone_crop_lyr_name = os.path.splitext(
            os.path.basename(zone_crop_path))[0]
        zone_crop_lyr = zone_crop_ds.CreateLayer(
            zone_crop_lyr_name, geom_type=ogr.wkbPolygon)
        zone_crop_lyr.CreateField(ogr.FieldDefn('ZONE_FID', ogr.OFTInteger))

    if soil_crop_mask_flag:
        zone_crop_rtree = rtree.index.Index()
        zone_crop_wkt_dict = dict()

    # Process crops (by zone) and compute area weighted stats
    # Write clipped zones (if necessary)
    logging.info('\nComputing crop area/type zonal stats')
    for zone_fid, crop_fid_list in sorted(zone_crops.items()):
        # if zone_fid % 1000 == 0 and zone_fid != 0:
        # logging.info('ZONE FID: {}'.format(zone_fid))
        logging.info('ZONE FID: {}'.format(zone_fid))
        logging.debug('CROP FID: {}'.format(crop_fid_list))
        if not crop_fid_list:
            logging.debug('  No crop FIDs, skipping zone')
            continue

        zone_poly = loads(zone_full_wkt_dict[zone_fid])
        zone_crop_polys = []
        zone_crop_area = 0

        # Initialize zonal stats crop acreages
        for etd_crop in etd_crops:
            field = 'CROP_{:02d}'.format(etd_crop)
            crop_stats[zone_fid][field] = 0
        crop_stats[zone_fid][acreage_field] = 0

        # Process all intersecting/neighboring crops
        for crop_fid in crop_fid_list:
            input_crop_dict = crop_dict[crop_fid]
            crop_value = input_crop_dict['value']
            crop_poly = loads(input_crop_dict['wkt'])

            clip_poly = zone_poly.intersection(crop_poly)

            if not clip_poly or clip_poly.is_empty:
                continue
            elif not clip_poly.is_valid:
                logging.error('\nERROR: Invalid clip geometry')
                input('ENTER')

            clip_area = clip_poly.area
            if not clip_area or clip_area <= 0:
                continue

            zone_crop_area += clip_area
            zone_crop_polys.append(clip_poly)

            for etd_crop in cross_dict[crop_value]:
                field = 'CROP_{:02d}'.format(etd_crop)
                crop_stats[zone_fid][field] += clip_area

        if soil_crop_mask_flag or save_crop_mask_flag:
            # Combine all polygons/multipolygons into a single multipolygon
            zone_crop_poly = unary_union(zone_crop_polys)\
                .buffer(simplify_threshold).buffer(-simplify_threshold)
            # .simplify(simplify_threshold, preserve_topology=False)\
            # zone_crop_poly = cascaded_union(zone_crop_polys)

            if zone_crop_poly.is_empty:
                logging.debug('  ZONE FID: {} - empty polygon, skipping'.format(
                    zone_fid))
                continue

            if soil_crop_mask_flag:
                # Save the crop masked zone to memory
                zone_crop_rtree.insert(zone_fid, list(zone_crop_poly.bounds))
                zone_crop_wkt_dict[zone_fid] = zone_crop_poly.wkt

            if save_crop_mask_flag:
                # Write the crop masked zone to shapefile
                zone_ftr = ogr.Feature(zone_crop_lyr.GetLayerDefn())
                zone_ftr.SetField('ZONE_FID', zone_fid)
                zone_ftr.SetGeometry(ogr.CreateGeometryFromWkt(zone_crop_poly.wkt))
                zone_crop_lyr.CreateFeature(zone_ftr)
                zone_ftr = None

    if save_crop_mask_flag:
        zone_crop_ds.ExecuteSQL("RECOMPUTE EXTENT ON {}".format(
            zone_crop_lyr_name))
        zone_crop_ds = None

        # Write projection/spatial reference to prj file
        # Format OSR as ESRI WKT
        prj_osr = zone_full_osr.Clone()
        prj_osr.MorphToESRI()
        with open(zone_crop_path.replace('.shp', '.prj'), 'w') as prj_f:
            prj_f.write(prj_osr.ExportToWkt())

    # Rebuild the crop list from the stats
    crop_field_list = sorted(list(set([
        crop_field for zone_crop_dict in crop_stats.values()
        for crop_field in zone_crop_dict.keys()])))
    logging.info('\nCrop Fields: {}'.format(', '.join(map(str, etd_crops))))

    logging.debug('\nAdding crop fields to zones shapefile')
    for crop_field in crop_field_list:
        if crop_field not in zone_field_list:
            logging.debug('  Field: {}'.format(crop_field))
            _arcpy.add_field(zone_path, crop_field, ogr.OFTReal)

    logging.debug('\nConverting crop areas to acres (if needed)')
    zone_ds = shp_driver.Open(zone_path, 0)
    zone_lyr = zone_ds.GetLayer()
    zone_osr = zone_lyr.GetSpatialRef()
    zone_unit = zone_osr.GetLinearUnitsName()
    if zone_unit.upper() not in ['METER', 'METERS', 'METRE']:
        raise ValueError('Unsupported unit type: {}'.format(zone_unit))
    for zone_fid, crop_stat_dict in crop_stats.items():
        for crop_field, crop_area in crop_stat_dict.items():
            if crop_area < 0:
                continue
            elif crop_field not in crop_field_list:
                continue
            elif zone_unit.upper() in ['METER', 'METERS', 'METRE']:
                crop_stats[zone_fid][crop_field] = crop_area * sqm_2_acres
            # elif zone_unit in ['Feet']:
            #     crop_stats[zone_fid][crop_field] = crop_area * sqft_2_acres

        # Compute total crop acreage per zone
        crop_stats[zone_fid][acreage_field] = sum(crop_stats[zone_fid].values())

    logging.debug('\nWriting crop zonal stats to zones shapefile')
    _arcpy.update_cursor(zone_path, crop_stats)

    # NOTE - Defining here to avoid passing zone_stats as an input
    def zonal_stats(input_path, input_field, zone_wkt_dict, zone_rtree):
        logging.debug('\nComputing {} zonal stats'.format(input_field))
        total_dict = dict()
        area_dict = dict()

        # Read the soil shapefile and identify intersecting features
        input_ds = shp_driver.Open(input_path, 0)
        input_lyr = input_ds.GetLayer()
        input_osr = input_lyr.GetSpatialRef()
        input_tx = osr.CoordinateTransformation(input_osr, zone_osr)
        for input_ftr in input_lyr:
            # input_fid = input_ftr.GetFID()
            input_value = input_ftr.GetField(input_field)
            #  added .Buffer(0) to clean up soil .shp intersecting geom 8/19/20
            input_geom = input_ftr.GetGeometryRef().Buffer(0)
            # input_geom = input_ftr.GetGeometryRef()
            proj_geom = input_geom.Clone()
            proj_geom.Transform(input_tx)
            input_poly = loads(proj_geom.ExportToWkt())

            proj_extent = gdc.Extent(proj_geom.GetEnvelope())
            proj_extent = proj_extent.ogrenv_swap()
            zone_fid_list = list(zone_rtree.intersection(list(proj_extent)))
            if not zone_fid_list:
                continue

            # Process all intersecting/neighboring features
            for zone_fid in zone_fid_list:
                try:
                    zone_poly = loads(zone_wkt_dict[zone_fid])
                except KeyError:
                    continue
                clip_poly = zone_poly.intersection(input_poly)
                if not clip_poly or clip_poly.is_empty:
                    continue
                elif not clip_poly.is_valid:
                    logging.error('\nERROR: Invalid clip geometry')
                    input('ENTER')
                elif not clip_poly.area or clip_poly.area <= 0:
                    continue

                if zone_fid not in total_dict.keys():
                    total_dict[zone_fid] = 0
                    area_dict[zone_fid] = 0

                if clip_poly.area > 0:
                    total_dict[zone_fid] += input_value * clip_poly.area
                    area_dict[zone_fid] += clip_poly.area
        input_ds = None

        # Compute area weighted values and save to master zonal stats dict
        for zone_fid, zone_total in total_dict.items():
            zone_stats[zone_fid][input_field] = zone_total / area_dict[zone_fid]

    if soil_crop_mask_flag:
        # # Load the crop masked zone shapefile
        # logging.debug('\nReading zone crop mask shapefile features into
        #  memory')
        # zone_crop_rtree = rtree.index.Index()
        # zone_crop_wkt_dict = dict()
        # zone_crop_ds = shp_driver.Open(zone_crop_path, 0)
        # zone_crop_lyr = zone_crop_ds.GetLayer()
        # for zone_crop_ftr in zone_crop_lyr:
        #     zone_crop_fid = zone_crop_ftr.GetFID()
        #     zone_fid = zone_crop_ftr.GetField('ZONE_FID')
        #     zone_crop_geom = zone_crop_ftr.GetGeometryRef()
        #     if not zone_crop_geom:
        #         continue
        #     zone_crop_geom = zone_crop_geom.Buffer(0)
        #     zone_crop_extent = gdc.Extent(zone_crop_geom.GetEnvelope())
        #     zone_crop_extent = zone_crop_extent.ogrenv_swap()
        #     zone_crop_rtree.insert(zone_crop_fid, list(zone_crop_extent))
        #     zone_crop_wkt_dict[zone_fid] = zone_crop_geom.ExportToWkt()
        # zone_crop_ds = None

        # Compute soil zonal stats for the crop masked ET zones
        # Process files separately even though geometries are probably the same
        zonal_stats(awc_path, awc_field, zone_crop_wkt_dict, zone_crop_rtree)
        zonal_stats(clay_path, clay_field, zone_crop_wkt_dict, zone_crop_rtree)
        zonal_stats(sand_path, sand_field, zone_crop_wkt_dict, zone_crop_rtree)

    else:
        # Compute soil zonal stats for the full ET zones
        # Process files separately even though geometries are probably the same
        zonal_stats(awc_path, awc_field, zone_full_wkt_dict, zone_full_rtree)
        zonal_stats(clay_path, clay_field, zone_full_wkt_dict, zone_full_rtree)
        zonal_stats(sand_path, sand_field, zone_full_wkt_dict, zone_full_rtree)

    logging.debug('\nWriting soil zonal stats to zones shapefile')
    _arcpy.update_cursor(zone_path, zone_stats)

    # Calculate AWC in in/feet
    logging.info('Calculating AWC in in/ft')
    _arcpy.calculate_field(
        zone_path, awc_in_ft_field, '!{}! * 12'.format(awc_field))

    # Calculate hydrologic group
    logging.info('Calculating hydrologic group')
    fields = (clay_field, sand_field, hydgrp_num_field, hydgrp_field)
    values = _arcpy.search_cursor(zone_path, fields)
    for fid, row in values.items():
        if row[sand_field] > 50:
            values[fid][hydgrp_num_field], values[fid][hydgrp_field] = 1, 'A'
        elif row[clay_field] > 40:
            values[fid][hydgrp_num_field], values[fid][hydgrp_field] = 3, 'C'
        else:
            values[fid][hydgrp_num_field], values[fid][hydgrp_field] = 2, 'B'
    _arcpy.update_cursor(zone_path, values)

#     # Save by FID/feature for easier writing to shapefile
#     logging.debug('\nParsing crop zonal stats')
#     zone_crop_dict = {}
#     crop_field_fmt = 'CROP_{:02d}'
#     for fid, ftr in enumerate(zs):
#         # logging.debug('FID: {}'.format(i))
#         zone_crop_dict[fid] = {}
#         for cdl_str, cdl_pixels in ftr.items():
#             # logging.debug('  {} {}'.format(cdl_str, cdl_pixels))
#             cdl_number = int(cdl_str)
#             # Only 'crops' have a crop number (no shrub, water, urban, etc.)
#             if cdl_number not in crop_num_dict.keys():
#                 if cdl_number != 0:
#                     logging.debug('  Skipping CDL {}'.format(cdl_number))
#                 continue
#
#             # Crop number can be an integer or list of integers (double crops)
#             crop_number = crop_num_dict[cdl_number]
#
#             # Crop numbers of -1 are for crops that haven't been linked
#             #   to a CDL number
#             if not crop_number or crop_number == -1:
#                 logging.warning('  Missing CDL {}'.format(cdl_number))
#                 continue
#
#             crop_acreage = cdl_pixels * sqm_2_acres * snap_cs ** 2
#
#             # Save acreage for both double crops
#             for c in crop_number:
#                 crop_field = crop_field_fmt.format(c)
#                 if crop_field not in zone_crop_dict[fid].keys():
#                     zone_crop_dict[fid][crop_field] = crop_acreage
#                 else:
#                     zone_crop_dict[fid][crop_field] += crop_acreage

def cell_lat_lon_func(input_path, lat_field, lon_field):
    """"""

    shp_driver = ogr.GetDriverByName('ESRI Shapefile')
    input_ds = shp_driver.Open(input_path, 1)
    input_lyr = input_ds.GetLayer()
    input_osr = input_lyr.GetSpatialRef()
    # input_osr.MorphToESRI()
    output_osr = input_osr.CloneGeogCS()
    # output_osr = osr.SpatialReference()
    # output_osr.ImportFromEPSG(4326)

    for input_ftr in input_lyr:
        input_fid = input_ftr.GetFID()
        # logging.debug('  FID: {}'.format(input_fid))
        input_geom = input_ftr.GetGeometryRef()
        # Project geometry to 4326 then extract geometry centroid
        centroid_geom = input_geom.Clone()
        centroid_geom.Transform(
            osr.CoordinateTransformation(input_osr, output_osr))
        centroid_geom = centroid_geom.Centroid()
        input_ftr.SetField(lat_field, centroid_geom.GetY())
        input_ftr.SetField(lon_field, centroid_geom.GetX())
        input_lyr.SetFeature(input_ftr)
    input_ds = None

def arg_parse():
    """"""
    parser = argparse.ArgumentParser(
        description='ET-Demands Zonal Stats',
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument(
        '-i', '--ini', required=True, metavar='INI',
        type=lambda x: util.is_valid_file(parser, x),
        help='Input INI File')
    parser.add_argument(
        '-o', '--overwrite', default=None, action='store_true',
        help='Overwrite existing file')
    parser.add_argument(
        '-d', '--debug', default=logging.INFO, const=logging.DEBUG,
        help='Debug level logging', action="store_const", dest="loglevel")
    args = parser.parse_args()

    # Convert input file to an absolute path
    if args.ini and os.path.isdir(os.path.abspath(args.ini)):
        args.ini = os.path.abspath(args.ini)

    return args

if __name__ == '__main__':
    args = arg_parse()

    logging.basicConfig(level=args.loglevel, format='%(message)s')
    logging.info('\n{}'.format('#' * 80))
    logging.info('{0:<20s} {1}'.format(
        'Run Time Stamp:', dt.datetime.now().isoformat(' ')))
    logging.info('{0:<20s} {1}'.format('Current Directory:', os.getcwd()))
    logging.info('{0:<20s} {1}'.format(
        'Script:', os.path.basename(sys.argv[0])))

    main(ini_path=args.ini, overwrite_flag=args.overwrite)


# File opening
with open("C.txt") as courses_file:
    for line in courses_file:
        print(line)

courses_file.close()

######################################################################
colors = "red green blue yellow"
# String splitting
color_parts = colors.split()
# ['red', 'green', 'blue', 'yellow']

print(colors)
print(color_parts)

for color in color_parts: # Good to use
    print(color)

second = color_parts[1] # prints the second second color in the color variable

print(second)

######################################################################
# Getting rid of whitespace from strings

name = "Brother Barnie      "

# clean_name = name.strip()   # Used to get rid of whitespace but doesn't change the elements of the variable
name = name.strip()           # Assigns the variable to itself so a new variable name isn't needed
print(f"---{name}---")
# print(f"---{clean_name}---")
######################################################################
# Reading files and parsing strings

with open("C.txt") as courses_file:
    for line in courses_file:
        # Line = "CSE 110,98.5"
        line.strip()
        parts = line.split(",")
        # Parts = ["CSE 110", "98.5"]
        name = parts[0]
        grade = float(parts[1])

        bonus_grade = grade + 3

        print(f"{name} - Grade: {grade}, after bonus: {bonus_grade}")


import cv2
import numpy as np
import copy
import math

cap_region_x_begin=0.5
cap_region_y_end=0.8
threshold = 60  # 默认阈值
blurValue = 41  # 高斯模糊参数
bgSubThreshold = 50
learningRate = 0

isBgCaptured = 0   # 布尔值, 是否捕获背景
triggerSwitch = False  # 如果是true, 键盘仿真器工作

def printThreshold(thr):
    print("阈值改变为："+str(thr))

def removeBG(frame):
    fgmask = bgModel.apply(frame,learningRate=learningRate)

    kernel = np.ones((3, 3), np.uint8)
    fgmask = cv2.erode(fgmask, kernel, iterations=1)
    res = cv2.bitwise_and(frame, frame, mask=fgmask)
    return res

def calculateFingers(res,drawing):
    hull = cv2.convexHull(res, returnPoints=False)
    if len(hull) > 3:
        defects = cv2.convexityDefects(res, hull)
        if type(defects) != type(None):

            cnt = 0
            for i in range(defects.shape[0]):  # calculate the angle
                s, e, f, d = defects[i][0]
                start = tuple(res[s][0])
                end = tuple(res[e][0])
                far = tuple(res[f][0])
                a = math.sqrt((end[0] - start[0]) ** 2 + (end[1] - start[1]) ** 2)
                b = math.sqrt((far[0] - start[0]) ** 2 + (far[1] - start[1]) ** 2)
                c = math.sqrt((end[0] - far[0]) ** 2 + (end[1] - far[1]) ** 2)
                angle = math.acos((b ** 2 + c ** 2 - a ** 2) / (2 * b * c))  # cosine theorem
                if angle <= math.pi / 2:  # angle less than 90 degree, treat as fingers
                    cnt += 1
                    cv2.circle(drawing, far, 8, [211, 84, 0], -1)
            return True, cnt
    return False, 0

camera = cv2.VideoCapture(0)
camera.set(10,200)
cv2.namedWindow('trackbar')
cv2.createTrackbar('trh1', 'trackbar', threshold, 100, printThreshold)

while camera.isOpened():
    ret, frame = camera.read()
    threshold = cv2.getTrackbarPos('trh1', 'trackbar')
    frame = cv2.bilateralFilter(frame, 5, 50, 100)
    frame = cv2.flip(frame, 1)
    cv2.rectangle(frame, (int(cap_region_x_begin * frame.shape[1]), 0),
                 (frame.shape[1], int(cap_region_y_end * frame.shape[0])), (255, 0, 0), 2)
    cv2.imshow('original', frame)

    if isBgCaptured == 1:
        img = removeBG(frame)
        img = img[0:int(cap_region_y_end * frame.shape[0]),
                    int(cap_region_x_begin * frame.shape[1]):frame.shape[1]]
        cv2.imshow('mask', img)

        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        blur = cv2.GaussianBlur(gray, (blurValue, blurValue), 0)
        cv2.imshow('blur', blur)
        ret, thresh = cv2.threshold(blur, threshold, 255, cv2.THRESH_BINARY)
        cv2.imshow('ori', thresh)

        thresh1 = copy.deepcopy(thresh)
        contours, hierarchy = cv2.findContours(thresh1, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
        length = len(contours)
        maxArea = -1
        if length > 0:
            for i in range(length):
                temp = contours[i]
                area = cv2.contourArea(temp)
                if area > maxArea:
                    maxArea = area
                    ci = i

            res = contours[ci]
            hull = cv2.convexHull(res)
            drawing = np.zeros(img.shape, np.uint8)
            cv2.drawContours(drawing, [res], 0, (0, 255, 0), 2)
            cv2.drawContours(drawing, [hull], 0, (0, 0, 255), 3)

            isFinishCal, cnt = calculateFingers(res,drawing)
            if triggerSwitch is True:
                if isFinishCal is True and cnt <= 2:
                    print (cnt)

        cv2.imshow('output', drawing)

    k = cv2.waitKey(10)
    if k == 27:  # 按ESC退出
        camera.release()
        cv2.destroyAllWindows()
        break
    elif k == ord('b'):  #按b捕捉背景
        bgModel = cv2.createBackgroundSubtractorMOG2(0, bgSubThreshold)
        isBgCaptured = 1
        print( '背景捕捉到啦')
    elif k == ord('r'):  #按r重置背景
        bgModel = None
        triggerSwitch = False
        isBgCaptured = 0
        print ('重置背景')
    elif k == ord('n'):
        triggerSwitch = True
        print ('开始啦')


from django.urls import path
from . import views

#Template Tagging
app_name="MyApp"

urlpatterns=[
#path('mane/',views.mane_view,name="mane"),
path('other/',views.other_view,name="other"),
path('relative_templates/',views.relative_templates_view,name="relative"),
]


import numpy as np
import matplotlib.pyplot as plt

# Load and display an image from CSV file
digits_file = 'digits.csv'
digits = np.loadtxt(digits_file, delimiter=',')
print("Datatype of digits:", type(digits))

# Select and reshape a row from digits array
im = digits[21, 1:]
im_sq = np.reshape(im, (28, 28))

# Display the reshaped image using matplotlib
plt.imshow(im_sq, cmap='Greys', interpolation='nearest')
plt.show()

# Load data from a file with a header and specific columns
header_data_file = 'digits_header.txt'
data = np.loadtxt(header_data_file, delimiter='\t', skiprows=1, usecols=(0, 2))
print("Loaded data:", data)

# Load and process string data from a file
seaslug_file = 'seaslug.txt'
data_str = np.loadtxt(seaslug_file, delimiter='\t', dtype=str)
print("First element of data_str:", data_str[0])

# Load and process numeric data from the same file
data_float = np.loadtxt(seaslug_file, delimiter='\t', dtype='float', skiprows=1)
print("10th element of data_float:", data_float[9])

# Create a scatterplot using loaded data
plt.scatter(data_float[:, 0], data_float[:, 1])
plt.xlabel('time (min.)')
plt.ylabel('percentage of larvae')
plt.show()

# Load and display structured data from a CSV file
titanic_file = 'titanic.csv'
d = np.recfromcsv(titanic_file)
print("First three entries of d:", d[:3])

14 - Introduction to Importing Data in Python/01 - Introduction and flat files/02 - Using Numpy to Import flat files.py


"""Invoke tasks for managing the repository."""
from pathlib import Path
import re
import sys
from typing import TYPE_CHECKING

try:
    from invoke import task
except ImportError:
    sys.exit("'invoke' MUST be installed to run these tasks.")

if TYPE_CHECKING:
    from typing import Optional, Tuple, Union

TOP_DIR = Path(__file__).parent.resolve()

def update_file(
    filename: "Union[Path, str]",
    sub_line: "Tuple[str, str]",
    strip: "Optional[str]" = None,
) -> None:
    """Utility function for tasks to read, update, and write files."""
    if isinstance(filename, str):
        filename = Path(filename)

    if not filename.exists():
        raise FileNotFoundError(f"Could not find file to be updated: {filename}")

    lines = [
        re.sub(sub_line[0], sub_line[1], line.rstrip(strip))
        for line in filename.read_text(encoding="utf8").splitlines()
    ]

    filename.write_text("\n".join(lines) + "\n", encoding="utf8")

@task(help={"ver": "OPTIMADE Validator Action version to set"})
def setver(_, ver=""):
    """Sets the OPTIMADE Validator Action version."""
    match = re.fullmatch(r"v?([0-9]+\.[0-9]+\.[0-9]+)", ver)
    if not match or (match and len(match.groups()) != 1):
        sys.exit(
            "Error: Please specify version as 'Major.Minor.Patch' or "
            "'vMajor.Minor.Patch'"
        )
    ver = match.group(1)

    major_version = ver.split(".")[0]
    tag_url = (
        "https://github.com/Materials-Consortia/optimade-validator-action/releases/tag/"
    )
    update_file(
        TOP_DIR / "README.md",
        (
            (
                rf"`v{major_version}` \| \[`v{major_version}(\.[0-9]+){{2}}.*"
                rf"v{major_version}(\.[0-9]+){{2}}"
            ),
            f"`v{major_version}` | [`v{ver}`]({tag_url}v{ver}",
        ),
        strip="\n",
    )

    print(f"Bumped version to {ver}")

@task
def check_dockerfile_python_version(_):
    """Check CI and config files are using the same Python version as in the
    Dockerfile."""
    with (TOP_DIR / "Dockerfile").open() as handle:
        for line in handle:
            match = re.match(
                r".*python:(?P<version>.*)-slim-buster.*",
                line,
            )
            if match:
                dockerfile_python_version: str = match.group("version")
                break
        else:
            sys.exit("Couldn't determine the Python version used in Dockerfile.")

    update_file(
        TOP_DIR / ".github" / "workflows" / "ci_tests.yml",
        (
            r"PYTHON_VERSION: 3\.[0-9]+.*",
            f"PYTHON_VERSION: {dockerfile_python_version}",
        ),
    )
    update_file(
        TOP_DIR / "pyproject.toml",
        (
            r"python_version = \".*\"",
            f"python_version = \"{'.'.join(dockerfile_python_version.split('.')[:2])}\"",
        ),
    )

    print(
        "Successfully updated CI and config files to use Python "
        f"{dockerfile_python_version}"
    )


def insertionSort(nList):
    for right in range(1, len(nList)):
        target = nList[right]
        for left in range(0, right):
            if target <= nList[left] :
                nList[left+1:right+1] = nList[left:right]
                print(nList)
                nList[left] = target
                break
            print(nList)

    return nList
numbers = [3,1,2,5,9,0]

print(insertionSort(numbers))


from example_code.groups import Group
import numpy as np

class SymmetricGroup(Group):
    notation = "S"
    symbol = "S"

    def _validate(self, value):
        """Ensure that value is a legitimate element value in this group."""
        value = np.asarray(list(value))
        if not (
            isinstance(value, np.ndarray)
            and np.shape(value)[0] == self.n
            and np.amax(value) == self.n - 1
            and np.amin(value) == 0
        ):
            raise ValueError(
                "Element value must be an numpy array"
                f" with elements in the range [0, {self.n})"
            )

    def operation(self, a, b):
        result = np.copy(a)
        for i in range(np.size(a)):
            result[i] = a[b[i]]
        return result


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Sep  7 13:16:56 2019

@author: willwen
"""

import pandas as pd
from nltk.stem import WordNetLemmatizer
from nltk.corpus import stopwords
from nltk import pos_tag, word_tokenize
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn import svm
from sklearn.metrics import confusion_matrix

data = pd.read_csv("spam.csv", encoding = "latin-1")
data = data[['v1', 'v2']]
data = data.rename(columns = {'v1': 'label', 'v2': 'text'})

lemmatizer = WordNetLemmatizer()
stopwords = set(stopwords.words('english'))

def review_messages(msg):
    # converting messages to lowercase
    msg = msg.lower()
    return msg

def alternative_review_messages(msg):
    # converting messages to lowercase
    msg = msg.lower()

    # uses a lemmatizer (wnpos is the parts of speech tag)
    # unfortunately wordnet and nltk uses a different set of terminology for pos tags
    # first, we must translate the nltk pos to wordnet
    nltk_pos = [tag[1] for tag in pos_tag(word_tokenize(msg))]
    msg = [tag[0] for tag in pos_tag(word_tokenize(msg))]
    wnpos = ['a' if tag[0] == 'J' else tag[0].lower() if tag[0] in ['N', 'R', 'V'] else 'n' for tag in nltk_pos]
    msg = " ".join([lemmatizer.lemmatize(word, wnpos[i]) for i, word in enumerate(msg)])

    # removing stopwords
    msg = [word for word in msg.split() if word not in stopwords]

    return msg

# Processing text messages
data['text'] = data['text'].apply(review_messages)

# train test split
X_train, X_test, y_train, y_test = train_test_split(data['text'], data['label'], test_size = 0.1, random_state = 1)

print(type(y_train))
# training vectorizer
vectorizer = TfidfVectorizer()
X_train = vectorizer.fit_transform(X_train)

# print(X_train)

# training the classifier
svm = svm.SVC(C=1000)
svm.fit(X_train, y_train)

# testing against testing set
X_test = vectorizer.transform(X_test)
y_pred = svm.predict(X_test)
# print(confusion_matrix(y_test, y_pred))

# test against new messages
def pred(msg):
    msg = vectorizer.transform([msg])
    prediction = svm.predict(msg)
    return prediction[0]