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

from __future__ import print_function, division

import argparse

import matplotlib
import torch
import torch.nn as nn
import torch.optim as optim
from torch.autograd import Variable
from torch.optim import lr_scheduler
from torchvision import datasets, transforms

matplotlib.use('agg')
import time
import os
from model import ft_net, ft_net_dense
from random_erasing import RandomErasing
import json

######################################################################
# Options
# --------
parser = argparse.ArgumentParser(description='Training')
parser.add_argument('--gpu_ids', default='0', type=str, help='gpu_ids: e.g. 0  0,1,2  0,2')
parser.add_argument('--name', default='ft_ResNet50', type=str, help='output model name')
parser.add_argument('--data_dir', default='/home/paul/datasets/viper/pytorch', type=str, help='training dir path')
parser.add_argument('--train_all', action='store_true', help='use all training data')
parser.add_argument('--color_jitter', action='store_true', help='use color jitter in training')
parser.add_argument('--batchsize', default=32, type=int, help='batchsize')
parser.add_argument('--erasing_p', default=0, type=float, help='Random Erasing probability, in [0,1]')
parser.add_argument('--use_dense', action='store_true', help='use densenet')
opt = parser.parse_args()

data_dir = opt.data_dir
name = opt.name
str_ids = opt.gpu_ids.split(',')
gpu_ids = []
for str_id in str_ids:
    gid = int(str_id)
    if gid >= 0:
        gpu_ids.append(gid)

# set gpu ids
if len(gpu_ids) > 0:
    torch.cuda.set_device(gpu_ids[0])

'''width = 256
random_width_crop = 256
height = 256
random_height_crop = 256
if opt.use_dense:
    width = 288
    height = 144
    random_width_crop = 256
    random_height_crop = 128
'''

transform_train_list = [

    transforms.Resize((288, 144), interpolation=3),
    transforms.RandomCrop((256, 128)),
    transforms.RandomHorizontalFlip(),
    transforms.ToTensor(),
    transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
]

transform_val_list = [
    transforms.Resize(size=(256, 128), interpolation=3),  # Image.BICUBIC
    transforms.ToTensor(),
    transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
]

if opt.PCB:
    transform_train_list = [
        transforms.Resize((384, 192), interpolation=3),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
    ]
    transform_val_list = [
        transforms.Resize(size=(384, 192), interpolation=3),  # Image.BICUBIC
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
    ]

if opt.erasing_p > 0:
    transform_train_list = transform_train_list + [RandomErasing(probability=opt.erasing_p, mean=[0.0, 0.0, 0.0])]

if opt.color_jitter:
    transform_train_list = [transforms.ColorJitter(brightness=0.1, contrast=0.1, saturation=0.1,
                                                   hue=0)] + transform_train_list

print(transform_train_list)
data_transforms = {
    'train': transforms.Compose(transform_train_list),
    'val': transforms.Compose(transform_val_list),
}

train_all = ''
if opt.train_all:
    train_all = '_all'

image_datasets = {}
image_datasets['train'] = datasets.ImageFolder(os.path.join(data_dir, 'train' + train_all),
                                               data_transforms['train'])
image_datasets['val'] = datasets.ImageFolder(os.path.join(data_dir, 'val'),
                                             data_transforms['val'])

dataloaders = {x: torch.utils.data.DataLoader(image_datasets[x], batch_size=opt.batchsize,
                                              shuffle=True, num_workers=16)
               for x in ['train', 'val']}
dataset_sizes = {x: len(image_datasets[x]) for x in ['train', 'val']}
class_names = image_datasets['train'].classes

use_gpu = torch.cuda.is_available()

inputs, classes = next(iter(dataloaders['train']))

y_loss = {}  # loss history
y_loss['train'] = []
y_loss['val'] = []
y_err = {}
y_err['train'] = []
y_err['val'] = []

def train_model(model, criterion, optimizer, scheduler, num_epochs=25):
    since = time.time()

    best_model_wts = model.state_dict()
    best_acc = 0.0

    for epoch in range(num_epochs):
        print('Epoch {}/{}'.format(epoch, num_epochs - 1))
        print('-' * 10)

        # Each epoch has a training and validation phase
        for phase in ['train', 'val']:
            if phase == 'train':
                scheduler.step()
                model.train(True)  # Set model to training mode
            else:
                model.train(False)  # Set model to evaluate mode

            running_loss = 0.0
            running_corrects = 0
            # Iterate over data.
            for data in dataloaders[phase]:
                # get the inputs
                inputs, labels = data
                # print(inputs.shape)
                # wrap them in Variable
                if use_gpu:
                    inputs = Variable(inputs.cuda())
                    labels = Variable(labels.cuda())
                else:
                    inputs, labels = Variable(inputs), Variable(labels)

                # zero the parameter gradients
                optimizer.zero_grad()

                # forward
                outputs = model(inputs)
                _, preds = torch.max(outputs.data, 1)
                loss = criterion(outputs, labels)

                # backward + optimize only if in training phase
                if phase == 'train':
                    loss.backward()
                    optimizer.step()

                # statistics
                running_loss += loss.item()
                print("Current Loss {}".format(loss.item()))
                running_corrects += torch.sum(preds == labels.data)

            epoch_loss = running_loss / dataset_sizes[phase]
            epoch_acc = running_corrects / dataset_sizes[phase]

            print('{} Loss: {:.4f} Acc: {:.4f}'.format(
                phase, epoch_loss, epoch_acc))

            y_loss[phase].append(epoch_loss)
            y_err[phase].append(1.0 - epoch_acc)
            # deep copy the model
            if phase == 'val':
                last_model_wts = model.state_dict()
                #if epoch % 10 == 9:
                save_network(model, epoch)
                #draw_curve(epoch)

        print()

    time_elapsed = time.time() - since
    print('Training complete in {:.0f}m {:.0f}s'.format(
        time_elapsed // 60, time_elapsed % 60))
    # print('Best val Acc: {:4f}'.format(best_acc))

    # load best model weights
    model.load_state_dict(last_model_wts)
    save_network(model, 'last')
    return model

x_epoch = []

def save_network(network, epoch_label):
    save_filename = 'net_%s.pth' % epoch_label
    save_path = os.path.join('./model', name, save_filename)
    torch.save(network.cpu().state_dict(), save_path)
    if torch.cuda.is_available:
        network.cuda(gpu_ids[0])

if opt.use_dense:
    model = ft_net_dense(len(class_names))
else:
    model = ft_net(len(class_names))

#print(model)

if use_gpu:
    model = model.cuda()

criterion = nn.CrossEntropyLoss()

ignored_params = list(map(id, model.model.fc.parameters())) + list(map(id, model.classifier.parameters()))
base_params = filter(lambda p: id(p) not in ignored_params, model.parameters())
optimizer_ft = optim.SGD([
    {'params': base_params, 'lr': 0.01},
    {'params': model.model.fc.parameters(), 'lr': 0.1},
    {'params': model.classifier.parameters(), 'lr': 0.1}
], weight_decay=5e-4, momentum=0.9, nesterov=True)

# Decay LR by a factor of 0.1 every 40 epochs
exp_lr_scheduler = lr_scheduler.StepLR(optimizer_ft, step_size=40, gamma=0.1)

dir_name = os.path.join('./model', name)
if not os.path.isdir(dir_name):
    os.mkdir(dir_name)

# save opts
with open('%s/opts.json' % dir_name, 'w') as fp:
    json.dump(vars(opt), fp, indent=1)

if __name__ == "__main__":

    model = train_model(model, criterion, optimizer_ft, exp_lr_scheduler,
                    num_epochs=60)


#coding=utf-8
__author__='silver'
import os
from os.path import splitext
from os.path import isfile
from os import listdir
from os import stat
from os import stat_result
from os import walk
from os import stat_float_times
#from re import re
from datetime import datetime
from dateutil import tz

"""

Function: util.filemanager.getdocumentlist(
                                            dir, # path to the directory
                                            recursive=True, # if it should recursive at the dir
                                            detail=True, # if need time and other detailed infos(in unix time format)
                                            )
Return Value: [{path, filename, [t_create, t_modify]},..]

"""

def getdocumentlist(path, recursive=True, detail=True):
    path=os.path.normpath(path)
    path=os.path.relpath(path)  #convert to rel path
    document_list=[]
    # have the list
    if recursive:
        for curdir, curdirlist, curfilelist in walk(path):
            for filename in curfilelist:
                    document_list.append({
                            "filename":splitext(filename)[0],
                            "path":curdir+"/"+filename
                        })
    else:
        for filename in listdir(path):  #have a loop test
            if os.path.isfile(path+'/'+filename):
                document_list.append({
                        "filename":splitext(filename)[0],
                        "path":path+'/'+filename          # just use relative path
                    })
    # find the detailed time if need
    if detail:
        # state save
        _stat=os.stat_float_times()
        os.stat_float_times(False)
        for x in range(len(document_list)):
            document_list[x].update(getdocumentdetail(document_list[x].get('path')))
        #state restore
        os.stat_float_times(_stat)
    return document_list

def getdocumentdetail(path):
    # os.stat_float_times(False) should be executed before all calls
    m=stat(path)
    strt=datetime.fromtimestamp(m.st_mtime, tz=tz.gettz('Asia/Shanghai')).strftime(r"%b.%d, %Y %Z %H:%M:%S")
    return {"path":path, "t_create":m.st_ctime, "t_modify":m.st_mtime, 'last_update':strt}
    # os.stat_float_times(True)  should be executed after all calls

if __name__=="__main__":
    print(getdocumentlist('.', detail=False))
    print('---')
    print(getdocumentlist('../controller'))


import time
startTime = time.time()
import warnings
warnings.filterwarnings('ignore')
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
sns.set()
import spacy
# import pyLDAvis.gensim_models
# pyLDAvis.enable_notebook()# Visualise inside a notebook
from gensim.corpora.dictionary import Dictionary
from gensim.models import LdaMulticore
from gensim.models import LdaModel
from gensim.models import CoherenceModel

import sys

try:
    lang = sys.argv[1]
    print("Running topic modelling in " + str(lang) + " language")
except:
    print("Please input a language parameter [en, ru, it] in order to run the script")
    exit()

print("Reading in input data now.")
df_comb = pd.read_excel("./files/input/combined_data.xlsx")

from langdetect import detect, DetectorFactory
DetectorFactory.seed = 0

df_comb["actual_lang"] = df_comb["content"].apply(detect)
print("Filtering data to account for content with language detected as " + str(lang))
df = df_comb[df_comb["actual_lang"] == str(lang)].reset_index(drop=True)

lang_models = {"en": spacy.load("en_core_web_md"), "it": spacy.load("it_core_news_md"), "ru": spacy.load("ru_core_news_md")}

# Our spaCy model:
nlp = lang_models[str(lang)]

print("Loading " + str(lang) + " model")

# Tags I want to remove from the text
print("Tokenizing, lemmatizing and removing stopwords.")
removal= ['ADV','PRON','CCONJ','PUNCT','PART','DET','ADP','SPACE', 'NUM', 'SYM']
tokens = []
for summary in nlp.pipe(df['content']):
    proj_tok = [token.lemma_.lower() for token in summary if token.pos_ not in removal and not token.is_stop and token.is_alpha]
    tokens.append(proj_tok)

df['tokens'] = tokens

print("Creating dictionary and corpus")
dictionary = Dictionary(df['tokens'])

dictionary.filter_extremes(no_below=5, no_above=0.5, keep_n=1000)

corpus = [dictionary.doc2bow(doc) for doc in df['tokens']]

## Code to plot coherence plot for each topic number (present in topic_modelling_full.ipynb file as well)

# topics = []
# score = []
# for i in range(1,10,1):
#     print("Running iteration number " + str(i))
#     lda_model = LdaMulticore(corpus=corpus, id2word=dictionary, iterations=10, num_topics=i, workers = 4, passes=10, random_state=100)
#     cm = CoherenceModel(model=lda_model, texts = df['tokens'], corpus=corpus, dictionary=dictionary, coherence='c_v')
#     topics.append(i)
#     score.append(cm.get_coherence())
# num_topics = topics[score.index(max(score))]

## best parameters for each language model (pre-run)
topic_lang = {"en": 7, "it": 6, "ru": 8}
topic_alpha = {"en": "symmetric", "it": "asymmetric", "ru": "asymmetric"}
topic_beta = {"en": 0.01, "it": 0.91, "ru": 0.01}

num_topics = topic_lang[lang]
print("Number of topics with highest coherence score for " + str(lang) + " model is: " + str(num_topics))

alpha = topic_alpha[lang]
beta = topic_beta[lang]

print("Optimal Parameters for " + str(lang) + " have been obtained. Running model now.")

# lda_model = LdaMulticore(corpus=corpus, id2word=dictionary, iterations=100, num_topics=num_topics, workers = 4, passes=100)
lda_model = LdaModel(corpus=corpus, id2word=dictionary, iterations=100, num_topics=num_topics, passes=100, alpha = alpha, eta = beta, random_state=100)

# lda_display = pyLDAvis.gensim_models.prepare(lda_model, corpus, dictionary, sort_topics=False)
# pyLDAvis.save_html(lda_display, "./files/output/" + str(lang) + "/lda_" + str(lang) + ".html")

def format_topics_sentences(ldamodel=None, corpus=corpus, texts=df['tokens']):
    # Init output
    sent_topics_df = pd.DataFrame()

    # Get main topic in each document
    for i, row_list in enumerate(ldamodel[corpus]):
        row = row_list[0] if ldamodel.per_word_topics else row_list
        # print(row)
        row = sorted(row, key=lambda x: (x[1]), reverse=True)
        # Get the Dominant topic, Perc Contribution and Keywords for each document
        for j, (topic_num, prop_topic) in enumerate(row):
            if j == 0:  # => dominant topic
                wp = ldamodel.show_topic(topic_num)
                topic_keywords = ", ".join([word for word, prop in wp])
                sent_topics_df = sent_topics_df.append(pd.Series([int(topic_num), round(prop_topic,4), topic_keywords]), ignore_index=True)
            else:
                break
    sent_topics_df.columns = ['Dominant_Topic', 'Perc_Contribution', 'Topic_Keywords']
    return(sent_topics_df)

df_topic_sents_keywords = format_topics_sentences(ldamodel=lda_model, corpus=corpus, texts=df['tokens'])

# Format
df_dominant_topic = df_topic_sents_keywords.reset_index(drop=True)
df_dominant_topic.columns = ['Dominant_Topic', 'Topic_Perc_Contrib', 'Keywords']
df_dominant_topic = pd.concat([df,df_dominant_topic], axis=1)
df_dominant_topic = df_dominant_topic.sort_values(by='Topic_Perc_Contrib',ascending=False).reset_index(drop=True)

writer = pd.ExcelWriter('./files/output/' + str(lang) + '/topic_modelling_output_' + str(lang) + '.xlsx' , engine='xlsxwriter')
workbook = writer.book
workbook.formats[0].set_font_size(12)
df_dominant_topic.to_excel(writer, sheet_name = 'Output', index=False)

for idx, col in enumerate(df_dominant_topic):  # loop through all columns
    series = df_dominant_topic[col]
    max_len = 20
    writer.sheets['Output'].set_column(idx, idx, max_len)  # set column width

writer.close()

print("Outputted excel file has been saved in ./files/output/"+str(lang)+" folder")
print("Visualisation data has been saved in ./files/output/"+str(lang)+" folder")
print("Coherence plot has been saved in ./files/output/"+str(lang)+" folder")

executionTime = (time.time() - startTime)
print('Execution time in seconds: ' + str(executionTime))


import random
import typing as t

INITIAL_CAPACITY = 10 ** 2
MULTIPLIER = 4
EXPANSION_ON = 0.5

class Node:

    def __init__(self, value):
        self.value = value
        self.next = None

class HashTable:

    def __init__(self):
        self.capacity = INITIAL_CAPACITY
        self.size = 0
        self.storage = [Node(None)] * self.capacity
        self.expand_times = 0

    def hash(self, value) -> int:
        return hash(value) % self.capacity

    def add(self, value) -> t.Optional[int]:
        self.size += 1
        index = self.hash(value)
        node = self.storage[index]

        if node.value is None:
            self.storage[index] = Node(value)
            return index

        parent = node
        while node is not None:
            parent = node
            node = node.next
        parent.next = Node(value)
        return index

    def find(self, value) -> t.Optional[int]:
        index = self.hash(value) % self.capacity
        node = self.storage[index]
        while node.value != value:
            node = node.next
            if node.value is None:
                return None
        return index

    def delete(self, value) -> bool:
        index = self.hash(value) % self.capacity
        node = self.storage[index]
        parent = None

        while node.value != value:
            node = node.next
            if node.value is None:
                return False

        self.size -= 1
        if parent is None:
            self.storage[index] = node.next
        else:
            parent.next = node.next
        return True

    def print_table(self):
        for index in range(self.capacity):
            print('['+str(index)+']:', end=' ')
            node = self.storage[index]
            while node is not None:
                if node.value is not None:
                    print(str(node.value), end=' ')
                node = node.next
            print()

    def check(self) -> bool:
        # вернет True, если надо расширяться, False - если нет
        return True if self.size / self.capacity > EXPANSION_ON else False

    def expand(self):
        self.storage += [Node(None)] * self.capacity
        self.capacity *= 2

if __name__ == '__main__':
    hash_table = HashTable()
    hash_table.print_table()
    for _ in range(INITIAL_CAPACITY // MULTIPLIER):
        hash_table.add(random.choice(range(INITIAL_CAPACITY * MULTIPLIER)))

    hash_table.print_table()


"""
Turning radar PPIs into Cartesian grids.

@title: radar_grids
@name: radar_grids
@author: Valentin Louf <valentin.louf@bom.gov.au>
@institution: Monash University and the Australian Bureau of Meteorology
@date: 12/03/2021

.. autosummary::
    :toctree: generated/

    chunks
    buffer
    main
"""
# Python Standard Library
import os
import sys
import glob
import time
import argparse
import warnings
import traceback

from concurrent.futures import TimeoutError

import pandas as pd
from pebble import ProcessPool, ProcessExpired

import radar_grids

def chunks(l, n):
    """
    Yield successive n-sized chunks from l.
    From http://stackoverflow.com/a/312464
    """
    for i in range(0, len(l), n):
        yield l[i : i + n]

def buffer(infile: str) -> None:
    """
    It calls the production line and manages it. Buffer function that is used
    to catch any problem with the processing line without screwing the whole
    multiprocessing stuff.

    Parameters:
    ===========
    infile: str
        Name of the input radar file.
    """
    radar_grids.标准映射(infile, OUTPATH, prefix="502", na_standard=True)

    return None

def main(start: pd.Timestamp, end: pd.Timestamp) -> None:
    """
    Look for files in the subdirectory structure and spawn the multiprocessing
    pools to process these files.

    Parameters:
    ===========
    start: pd.Timestamp
        Start date for processing.
    end: pd.Timestamp
        End date for processing.
    """
    drange = pd.date_range(start, end)
    for date in drange:
        st = time.time()
        datestr = date.strftime("%Y%m%d")
        input_dir = os.path.join(INPATH, str(date.year), datestr, "*.nc")
        flist = sorted(glob.glob(input_dir))
        if len(flist) == 0:
            print(f"No file found for {input_dir}.")
            continue
        print(f"Found {len(flist)} files for {datestr}.")

        for flist_chunk in chunks(flist, 32):
            with ProcessPool() as pool:
                future = pool.map(buffer, flist_chunk, timeout=120)
                iterator = future.result()

                while True:
                    try:
                        _ = next(iterator)
                    except StopIteration:
                        break
                    except TimeoutError as error:
                        print("function took longer than %d seconds" % error.args[1])
                    except ProcessExpired as error:
                        print("%s. Exit code: %d" % (error, error.exitcode))
                    except Exception:
                        traceback.print_exc()

        ed = time.time()
        print(f"Processed in {ed - st:0.1f}s.")

    return None

if __name__ == "__main__":
    """
    Global variables definition.
    """
    # Parse arguments
    parser_description = "Processing of radar data from level 1a to level 1b."
    parser = argparse.ArgumentParser(description=parser_description)

    parser.add_argument(
        "-i",
        "--input-dir",
        dest="indir",
        default="/scratch/kl02/vhl548/opol/v2020/ppi/",
        type=str,
        help="Input directory.",
    )
    parser.add_argument(
        "-o",
        "--output-dir",
        dest="outdir",
        default="/scratch/kl02/vhl548/opol/v2021/gridded/",
        type=str,
        help="Output directory.",
    )
    parser.add_argument(
        "-s",
        "--start-date",
        dest="start_date",
        default=None,
        type=str,
        help="Start date for radar processing.",
        required=True,
    )
    parser.add_argument(
        "-e",
        "--end-date",
        dest="end_date",
        default=None,
        type=str,
        help="End date for radar processing.",
        required=True,
    )

    args = parser.parse_args()
    INPATH = args.indir
    OUTPATH = args.outdir
    START_DATE = args.start_date
    END_DATE = args.end_date
    print(f"The input directory is {INPATH}\nThe output directory is {OUTPATH}.")

    try:
        start = pd.Timestamp(START_DATE)
        end = pd.Timestamp(END_DATE)
        if start > end:
            parser.error("End date older than start date.")
    except ValueError:
        parser.error("Invalid dates.")
        sys.exit()

    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        main(start, end)


import streamlit as st
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib

matplotlib.style.use('ggplot')

from scipy.stats import multivariate_normal

st.set_page_config(layout="wide")

class BayesianRegression:
    def __init__(self, prior_mean, precision):
        self.prior_mean = prior_mean
        prior_cov = 1/precision * np.eye(prior_mean.shape[0])
        self.prior_cov = prior_cov
        self.X = None
        self.y = None

    def fit(self, X, y):
        if self.X is None:
            self.X = X
        else:
            self.X = np.vstack((self.X, X))
        if self.y is None:
            self.y = y
        else:
            self.y = np.hstack((self.y, y))
        # Do a MLE estimate of the coefficients first
        w_mle = np.linalg.solve(self.X.T.dot(self.X), self.X.T.dot(self.y))
        # Estimate beta from the residuals
        self.beta = 1/((self.y - self.X.dot(w_mle)).var())
        # Update the posterior mean and covariance
        posterior_cov_inv = np.linalg.inv(self.prior_cov) + self.beta * X.T.dot(X)
        posterior_cov = np.linalg.inv(posterior_cov_inv)
        posterior_mean = posterior_cov.dot(np.linalg.inv(self.prior_cov).dot(self.prior_mean) + self.beta * X.T.dot(y))
        self.prior_mean = posterior_mean
        self.prior_cov = posterior_cov

    def predict(self, X, pi=False):
        predictions = X.dot(self.prior_mean)
        if not pi:
            return predictions

        predictions_stds = np.zeros(X.shape[0])
        for i, x in enumerate(X):
            predictions_stds[i] = np.sqrt(1/self.beta + x.T.dot(self.prior_cov).dot(x))

        return predictions, predictions_stds

    def plot_param_distributions(self):
        if len(self.prior_mean) > 2:
            print('Distribution plot for more than 2 parameters is not implemented yet.')
            return
        x, y = np.mgrid[-1:1:.01, -1:1:.01]
        pos = np.dstack((x, y))
        rv = multivariate_normal(self.prior_mean, self.prior_cov)

        fig, ax = plt.subplots(figsize=(10, 10))
        ax.contourf(x, y, rv.pdf(pos), cmap='plasma')
        ax.set_xlabel('$w_0$')
        ax.set_ylabel('$w_1$')
        plt.show()

def main():

    st.title("Interactive Bayesian Linear Regression Demo")

    # Empty dataframe to store data points
    # We need to add it to the website's state so that it doesn't get reset on each rerun
    if 'dots_df' not in st.session_state:
        st.session_state.dots_df = pd.DataFrame(columns=['x', 'y'])

    # New points that we use to update the model
    if 'new_dots_df' not in st.session_state:
        st.session_state.new_dots_df = pd.DataFrame(columns=['x', 'y'])

    # Empty model
    if 'model' not in st.session_state:
        st.session_state.model = BayesianRegression(np.zeros(2), 1)

    # Model fit flag
    if 'model_fit' not in st.session_state:
        st.session_state.model_fit = False

    st.markdown(
        """
        Manually place data points on the graph and see how the model changes.
        - Click on the "Place Dot" button to place a new data point.
        - Click on the "Remove Last Dot" button to remove the last data point.
        - Click on the "Clear Dots" button to remove all data points.
        - Click on the "Update Model" button to update the model with the new data points.
        """
    )

    # Sidebar controls
    x = st.slider('x', min_value=-1.0, max_value=1.0, value=0.0, step=0.01)
    y = st.slider('y', min_value=-1.0, max_value=1.0, value=0.0, step=0.01)

    def add_dot(x, y):
        new_dot = {'x': x, 'y': y}
        st.session_state.new_dots_df = pd.concat([st.session_state.new_dots_df, pd.DataFrame(new_dot, index=[0])], ignore_index=True)

    def remove_last_dot():
        st.session_state.new_dots_df = st.session_state.new_dots_df.iloc[:-1]

    def clear_dots():
        # Reset the old points
        st.session_state.dots_df = pd.DataFrame(columns=['x', 'y'])
        # Reset the new points
        st.session_state.new_dots_df = pd.DataFrame(columns=['x', 'y'])
        # Reset the model
        st.session_state.model = BayesianRegression(np.zeros(2), 1)

    def update_model():
        X = np.vstack((np.ones(st.session_state.new_dots_df.shape[0]), st.session_state.new_dots_df['x'])).T
        y = st.session_state.new_dots_df['y']
        st.session_state.model.fit(X, y)

        # Add the new points to the old ones
        st.session_state.dots_df = pd.concat([st.session_state.dots_df, st.session_state.new_dots_df], ignore_index=True)
        # Reset the new points
        st.session_state.new_dots_df = pd.DataFrame(columns=['x', 'y'])

        # Update the model fit flag
        st.session_state.model_fit = True

    def plot_points_predictions_param_distribution():
        fig, ax = plt.subplots(1, 2, figsize=(16, 8))
        ax[0].scatter(st.session_state.dots_df['x'], st.session_state.dots_df['y'],
                   label='Fitted Points', color='blue')
        ax[0].scatter(st.session_state.new_dots_df['x'], st.session_state.new_dots_df['y'],
                   label='New Points', color='green')
        ax[0].set_xlim(-1.0, 1.0)
        ax[0].set_ylim(-1.0, 1.0)
        ax[0].set_title('Data and the predictive interval')
        # Only plot the prediction interval if there are at least 3 points
        # and the model has been updated at least once
        if len(st.session_state.dots_df) > 2 and st.session_state.model_fit:
            x = np.linspace(-1.0, 1.0, 100)
            X = np.vstack((np.ones(x.shape[0]), x)).T
            predictions, predictions_stds = st.session_state.model.predict(X, pi=True)
            ax[0].plot(x, predictions, color='red', label='Predictions')
            ax[0].fill_between(x, predictions - predictions_stds,
                            predictions + predictions_stds, color='red',
                            alpha=0.2, label=r'$1\sigma$ Prediction Interval')
            ax[0].legend()

        crt_mean_estimate = st.session_state.model.prior_mean
        # Draw the distribution in a 2x2 square around the current mean estimate
        x, y = np.mgrid[crt_mean_estimate[0]-1:crt_mean_estimate[0]+1:.01,
                        crt_mean_estimate[1]-1:crt_mean_estimate[1]+1:.01]
        pos = np.dstack((x, y))
        rv = multivariate_normal(st.session_state.model.prior_mean, st.session_state.model.prior_cov)

        ax[1].set_title('Parameter distribution')
        ax[1].contourf(x, y, rv.pdf(pos), cmap='coolwarm')
        ax[1].set_xlabel('$w_0$ - intercept')
        ax[1].set_ylabel('$w_1$ - slope')

        st.pyplot(fig)

    # Create three buttons side by side
    col1, col2, col3, col4 = st.columns(4)

    with col1:
        # Place Dot button
        st.button('Place Dot', on_click=add_dot, args=(x, y))

    with col2:
        # Remove Dot button
        st.button('Remove Last Dot', on_click=remove_last_dot)

    with col3:
        # Clear Dots button
        st.button('Clear Dots', on_click=clear_dots)

    with col4:
        st.button('Update Model', on_click=update_model)

    plot_points_predictions_param_distribution()

main()


from flask import Flask, render_template
#from conexion import añadiralist
import sys
import pyodbc

#abrir la conexion con la base de datos
conn= pyodbc.connect(r"Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ= c:\Users\cuc\Documents\base\TallerDB.accdb;")
#abrir el cursor
cursor=conn.cursor()
app = Flask(__name__)
@app.route('/')
def index():
    cursor.execute("SELECT * FROM Estudiantes")
    Estudiantes=cursor.fetchall()
    cursor.execute("SELECT * FROM cursos")
    cursos=cursor.fetchall()
    return f"esta es la tabla de estudiantes
 {Estudiantes}
 esta es la tabla de cursos
{cursos}"
if __name__=='__main__':
    app.run()
cursor.close
#cerrar la conexion
conn.close


from typing import Tuple, Union
import datetime
from .spyglass_helpers import fetch_sessions, fetch_subjects, fetch_electrode_groups, fetch_interval_lists
import threading
import numpy as np

class RtcsharePlugin:
    def initialize(context):
        context.register_service('spyglass-explorer', SpyglassExplorerService)

# The lock is needed because datajoint database access is not thread-safe
lock = threading.Lock()

class SpyglassExplorerService:
    def handle_query(query: dict, *, dir: str, user_id: Union[str, None]=None) -> Tuple[dict, bytes]:
        # todo: authenticate user
        type0 = query['type']
        if type0 == 'get_subjects':
            with lock:
                a = fetch_subjects(
                    limit = query['limit'],
                    subject_id=query.get('subject_id', None),
                    age=query.get('age', None),
                    description=query.get('description', None),
                    genotype=query.get('genotype', None),
                    sex=query.get('sex', None),
                    species=query.get('species', None)
                )
                subjects = []
                for s in a:
                    subjects.append(s.key)
                return convert_to_json_serializable({'subjects': subjects}), b''
        elif type0 == 'get_sessions':
            with lock:
                a = fetch_sessions(
                    limit=query['limit'] + 1, # include one more so we can tell if there are additional sessions
                    session_id=query.get('session_id', None),
                    subject_id=query.get('subject_id', None),
                    experiment_description=query.get('experiment_description', None),
                    institution_name=query.get('institution_name', None),
                    lab_name=query.get('lab_name', None),
                    nwb_file_name=query.get('nwb_file_name', None),
                    session_description=query.get('session_description', None),
                    # session_start_time_min=query.get('session_start_time_min', None),
                    # session_start_time_max=query.get('session_start_time_max', None),
                    # timestamps_reference_time_min=query.get('timestamps_reference_time_min', None),
                    # timestamps_reference_time_max=query.get('timestamps_reference_time_max', None)
                )
                sessions = []
                has_more = False
                for s in a:
                    if len(sessions) >= query['limit']:
                        has_more = True
                        break
                    sessions.append(s.key)
                return convert_to_json_serializable({'sessions': sessions, 'has_more': has_more}), b''
        elif type0 == 'get_electrode_groups':
            with lock:
                a = fetch_electrode_groups(
                    limit=query['limit'] + 1, # include one more so we can tell if there are additional electrode groups
                    nwb_file_name=query['nwb_file_name']
                )
                electrode_groups = []
                has_more = False
                for eg in a:
                    if len(electrode_groups) >= query['limit']:
                        has_more = True
                        break
                    electrode_groups.append(eg.key)
                return convert_to_json_serializable({'electrode_groups': electrode_groups, 'has_more': has_more}), b''
        elif type0 == 'get_interval_lists':
            with lock:
                a = fetch_interval_lists(
                    limit=query['limit'] + 1, # include one more so we can tell if there are additional interval lists
                    nwb_file_name=query['nwb_file_name']
                )
                interval_lists = []
                has_more = False
                for il in a:
                    if len(interval_lists) >= query['limit']:
                        has_more = True
                        break
                    interval_lists.append(il.key)
                return convert_to_json_serializable({'interval_lists': interval_lists, 'has_more': has_more}), b''
        else:
            raise Exception(f'Unexpected query type: {type0}')

def convert_to_json_serializable(obj):
    if isinstance(obj, (datetime.date, datetime.datetime)):
        return obj.timestamp()
    elif isinstance(obj, np.ndarray):
        return obj.tolist()
    elif isinstance(obj, dict):
        ret = {}
        for key, val in obj.items():
            ret[key] = convert_to_json_serializable(val)
        return ret
    elif isinstance(obj, list):
        ret = []
        for val in obj:
            ret.append(convert_to_json_serializable(val))
        return ret
    else:
        return obj


from PySide6.QtWidgets import QPushButton, QLabel, \
    QLineEdit, QComboBox, QFrame, QVBoxLayout, QHBoxLayout, \
    QHeaderView, QTableWidget, QTableWidgetItem, QMessageBox, QTabWidget, QWidget, QDialog

from PySide6.QtGui import Qt, QIcon, QPixmap
from PySide6 import QtCore, QtGui

# user import
from draw_line import QHSeparationLine, QVSeparationLine
from front_end.result_preview import PreviewResult
from front_end.result_print import PrintResult
from new_student import RegisterUI
from result_single import ResultSingle

from back_end.database import DatabaseOps
from back_end.middleware import prepare_scores

class Result(QFrame):
    """UI page for result section"""

    def __init__(self, database_handle):
        super(Result, self).__init__()
        self.database_handle = database_handle
        self.result_tabs = QTabWidget()
        self.result_tabs.setStyleSheet("""
                                        QTabBar::tab:selected {
                                            background: white;
                                            padding:8px;
                                        }
                                        QTabWidget::pane { background:white;padding:10px;border-radius:0px}"""
                                       )

        self.result_tabs.addTab(PreviewResult(database_handle),
                                QPixmap("../images/view.png"),
                                "Preview Result",
                                )
        self.result_tabs.addTab(AddResult(),
                                QPixmap("../images/add.png"),
                                "New Entry")
        self.result_tabs.addTab(PrintResult(database_handle),
                                QPixmap("../images/print.png"),
                                "Print Result")

        main_layout = QVBoxLayout()
        main_layout.addWidget(self.result_tabs)
        self.setLayout(main_layout)

class AddResult(QWidget):
    """Tab view for adding of result"""

    def __init__(self):
        super().__init__()

        page_layout = QVBoxLayout()
        menu_layout = QHBoxLayout()
        class_filter = QLabel("Select Class")
        class_filter.setStyleSheet("padding:4px;font-size:15px;")
        self.class_filter_input = QComboBox()
        self.database_handle = DatabaseOps()
        classes = self.database_handle.fetch_class().fetchall()
        for item in classes:
            self.class_filter_input.addItem(item[0])
        self.class_filter_input.setStyleSheet("padding:4px;font-size:15px;")
        self.class_filter_input.setMinimumWidth(200)
        self.class_filter_input.currentTextChanged.connect(lambda text: self.populate_subject(text))

        subject_filter = QLabel("Select Subject")
        subject_filter.setStyleSheet("padding:4px;font-size:15px;")
        self.subject_filter_input = QComboBox()
        # subject_filter_input.currentTextChanged.connect(lambda text: self.)
        self.subject_filter_input.setStyleSheet("padding:4px;font-size:15px;")
        self.subject_filter_input.setMinimumWidth(200)
        # filter_input.currentTextChanged.connect("me")
        self.search_input = QLineEdit()
        self.search_input.setPlaceholderText("Find A Student")
        self.search_input.setStyleSheet("padding:4px;font-size:15px;")
        search_button = QPushButton("Search")
        search_button.clicked.connect(self.search_student_callback)
        search_button.setStyleSheet("padding:8px;border-radius:0px;background:rgb(14, 180, 166);")
        # add_button = QPushButton("New Student")
        # add_button.clicked.connect(self.add_student_callback)
        # add_button.setStyleSheet("padding:8px;border-radius:0px;"
        #                          "background:rgb(14, 180, 166);color:white;font-weight:bold;")

        menu_layout.addWidget(class_filter)
        menu_layout.addWidget(self.class_filter_input)
        menu_layout.addWidget(subject_filter)
        menu_layout.addWidget(self.subject_filter_input)
        menu_layout.addWidget(QVSeparationLine())
        menu_layout.addWidget(self.search_input)
        menu_layout.addWidget(search_button)
        menu_layout.addWidget(QVSeparationLine())

        self.table = QTableWidget()
        self.table.setColumnCount(5)
        # table.setColumnWidth(0)
        self.table.setHorizontalHeaderLabels(["Name", "Class", "First CA", "Second CA", "Exams"])
        self.table.setStyleSheet(
            "QTableWidget::item {font-size:18px;selection-background-color:#f5f5f5;selection-color:black;}"
            "QHeaderView {font-size:18px;}")
        self.table.horizontalHeader().setDefaultAlignment(QtCore.Qt.AlignLeft)
        # self.student = self.database_handle.fetch_record("all").fetchall()
        # self.table.setRowCount(len(self.student))
        self.table.horizontalHeader().setSectionResizeMode(0, QHeaderView.Stretch)
        # load student into table
        action_layout = QHBoxLayout()
        clear_btn = QPushButton("Clear All")
        clear_btn.clicked.connect(self.clear_btn_callback)
        finish_btn = QPushButton("Save Record")
        finish_btn.clicked.connect(self.finish_btn_callback)
        action_layout.addWidget(clear_btn)
        action_layout.addWidget(finish_btn)
        # arrange widget to page layout
        page_layout.addLayout(menu_layout)
        page_layout.addWidget(QHSeparationLine())
        page_layout.addWidget(self.table)
        page_layout.addLayout(action_layout)
        # page_layout.addWidget(self.result_tabs)

        self.setLayout(page_layout)

    def populate_table(self):
        """function to populated table based on request"""
        if not self.student:
            QMessageBox.information(self, 'No Record!', "Record not found for entry made!")
            self.table.clearContents()
            self.table.setRowCount(0)
            self.subject_filter_input.clear()
        else:
            self.table.setRowCount(len(self.student))
            for index, value in enumerate(self.student):
                name = QTableWidgetItem(self.student[index][0])
                name.setFlags(Qt.ItemIsEnabled)
                class_ = QTableWidgetItem(self.student[index][2])
                class_.setFlags(Qt.ItemIsEnabled)
                self.table.setItem(index, 0, name)
                self.table.setItem(index, 1, class_)
                # self.table.setItem(index, 2, QTableWidgetItem(self.student[index][2]))
                # self.table.setItem(index, 3, QTableWidgetItem(str(self.student[index][3])))
                # self.table.setItem(index, 4, QTableWidgetItem(self.student[index][4]))
                # self.table.setItem(index, 5, QTableWidgetItem(self.student[index][5]))

    def populate_subject(self, key):
        classes = self.database_handle.fetch_subject_per_class(key).fetchall()
        self.student = self.database_handle.fetch_record(key).fetchall()
        for item in classes:
            self.subject_filter_input.addItem(item[0])
        self.populate_table()

    def finish_btn_callback(self):
        """function collect data into table widget after entry.
            also prepare the data for further processing. set zero in field with not scores."""
        subject = self.subject_filter_input.currentText()
        session = self.database_handle.fetch_current_session().fetchone()[0]
        term = self.database_handle.fetch_current_term().fetchone()[0]
        class_ = self.class_filter_input.currentText()
        if subject:
            score_dict = {}  # create empty dict for later use
            for row in range(self.table.rowCount()):  # loop through table rows.
                score_list = []  # create a temporal list for storage
                for column in range(self.table.columnCount()):  # loop through the columns
                    value = self.table.item(row, column)
                    if value:
                        if value.text():
                            score_list.append(value.text())
                        else:
                            score_list.append('0')
                    else:
                        score_list.append('0')

                score_dict[row] = score_list

            # pass data for further processing
            scores = prepare_scores(score_dict.values(), self.subject_filter_input.currentText())
            # loop through students list and added score.
            return_value = None
            for student in scores[1]:
                return_value = self.database_handle.insert_score(
                    student, class_, subject, session, term,
                    scores[1][student][subject]["first_ca"],
                    scores[1][student][subject]["second_ca"],
                    scores[1][student][subject]["exam"],
                    scores[1][student][subject]["total"],
                )

                if return_value == "error":
                    QMessageBox.warning(self, "Error", "An unexpected error occurred. ")

                else:
                    QMessageBox.information(self, "Success", "Score add successfully")

        else:
            QMessageBox.warning(self, "Error", "No Subject selected! Select A Subject And Try Again. ")

    def validate_data(self):
        stored_score = self.database_handle.fetch_result(self.class_filter_input.currentText(),
                                                         self.subject_filter_input, "2023/2024")
        # for values in scores:
        #     result = self.database_handle.insert_scores(scores)
        pass

    def clear_btn_callback(self):
        """call back function to clear the entries """
        accept = QMessageBox.question(self, "Warning", "Are you sure you want to clear? "
                                                       "Scores will be gone")
        if accept == 16384:
            for row in range(self.table.rowCount()):
                for column in range(2, self.table.columnCount()):
                    value = self.table.item(row, column)
                    if value:
                        value.setText("")
                    else:
                        continue

    def add_student_callback(self):
        print("hello")
        app = RegisterUI(self)
        app.show()

    def search_student_callback(self):
        keyword = self.search_input.text()
        if not keyword:
            QMessageBox.information(self, "Empty!", "No Search Word Entered")
        else:
            self.student = self.database_handle.search_record(keyword).fetchall()
            # self.populate_table()


import collections
import random

from xls.common import test_base
from xls.dslx.concrete_type import ArrayType
from xls.dslx.concrete_type import BitsType
from xls.dslx.concrete_type import TupleType
from xls.dslx.fuzzer import ast_generator
from xls.dslx.fuzzer import sample_generator
from xls.dslx.fuzzer.sample import SampleOptions

class SampleGeneratorTest(test_base.TestCase):

  def test_randrange_biased_towards_zero(self):
    rng = random.Random(0)
    counter = collections.Counter()
    for _ in range(1024):
      counter[sample_generator.randrange_biased_towards_zero(16, rng)] += 1
    print(sorted(counter.items()))
    self.assertTrue(all(k < 16 for k in counter.keys()))
    self.assertTrue(all(counter[0] > counter[i] for i in range(1, 16)))

  def test_generate_empty_arguments(self):
    rng = random.Random(0)
    self.assertEqual(sample_generator.generate_arguments((), rng), ())

  def test_generate_single_bits_arguments(self):
    rng = random.Random(0)
    args = sample_generator.generate_arguments(
        (BitsType(signed=False, size=42),), rng)
    self.assertLen(args, 1)
    self.assertTrue(args[0].is_ubits())
    self.assertEqual(args[0].get_bit_count(), 42)

  def test_generate_mixed_bits_arguments(self):
    rng = random.Random(0)
    args = sample_generator.generate_arguments(
        (BitsType(signed=False, size=123), BitsType(signed=True, size=22)), rng)
    self.assertLen(args, 2)
    self.assertTrue(args[0].is_ubits())
    self.assertEqual(args[0].get_bit_count(), 123)
    self.assertTrue(args[1].is_sbits())
    self.assertEqual(args[1].get_bit_count(), 22)

  def test_generate_tuple_argument(self):
    rng = random.Random(0)
    args = sample_generator.generate_arguments((TupleType(
        (BitsType(signed=False, size=123),
         BitsType(signed=True, size=22))),), rng)
    self.assertLen(args, 1)
    self.assertTrue(args[0].is_tuple())
    self.assertEqual(args[0].tuple_members[0].get_bit_count(), 123)
    self.assertEqual(args[0].tuple_members[1].get_bit_count(), 22)

  def test_generate_array_argument(self):
    rng = random.Random(0)
    args = sample_generator.generate_arguments(
        (ArrayType(BitsType(signed=True, size=4), 24),), rng)
    self.assertLen(args, 1)
    self.assertTrue(args[0].is_array())
    self.assertLen(args[0].array_payload.elements, 24)
    self.assertTrue(args[0].array_payload.index(0).is_sbits())
    self.assertTrue(args[0].array_payload.index(0).get_bit_count(), 4)

  def test_generate_basic_sample(self):
    rng = random.Random(0)
    sample = sample_generator.generate_sample(
        rng,
        ast_generator.AstGeneratorOptions(),
        calls_per_sample=3,
        default_options=SampleOptions(
            convert_to_ir=True, optimize_ir=True, codegen=False,
            simulate=False))
    self.assertTrue(sample.options.input_is_dslx)
    self.assertTrue(sample.options.convert_to_ir)
    self.assertTrue(sample.options.optimize_ir)
    self.assertFalse(sample.options.codegen)
    self.assertFalse(sample.options.simulate)
    self.assertLen(sample.args_batch, 3)
    self.assertIn('fn main', sample.input_text)

  def test_generate_codegen_sample(self):
    rng = random.Random(0)
    sample = sample_generator.generate_sample(
        rng,
        ast_generator.AstGeneratorOptions(),
        calls_per_sample=0,
        default_options=SampleOptions(
            convert_to_ir=True, optimize_ir=True, codegen=True, simulate=True))
    self.assertTrue(sample.options.input_is_dslx)
    self.assertTrue(sample.options.convert_to_ir)
    self.assertTrue(sample.options.optimize_ir)
    self.assertTrue(sample.options.codegen)
    self.assertTrue(sample.options.simulate)
    self.assertNotEmpty(sample.options.codegen_args)
    self.assertEmpty(sample.args_batch)

if __name__ == '__main__':
  test_base.main()


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

from setuptools import setup, find_packages
import glob
import ntpath

def get_module_name(module_path):
    """
    Return the module name of the module path
    """
    return ntpath.split(module_path)[1].split(".")[0]

def snake_to_camel(word):
    """
    Convert a word from snake_case to CamelCase
    """
    return ''.join(x.capitalize() or '_' for x in word.split('_'))

setup(
    display_name='Scheduler',
    name='fn_scheduler',
    version='2.1.1',
    license='MIT',
    author='IBM SOAR',
    url='https://ibm.com/mysupport',
    description="Functions to allow Rules/Playbooks to be scheduled",
    long_description="""This package of functions allows an enterprise to schedule a rule or playbook to run in the future associated with a incident, task, artifact, and datatable.

        Functions available include:

            1) Scheduling a rule/playbook

            2) Listing scheduled jobs

            3) Pause and resume scheduled job

            4) Removing a scheduled job""",
    install_requires=[
        'resilient_circuits>=46.0.0',
        'pytz',
        "APScheduler < 3.9;python_version < '3.6'",
        "APScheduler >= 3.9;python_version >= '3.6'",
        'SQLAlchemy[postgresql]>=1.3.8',
        'python-dateutil>=2.8.1'
    ],
    extras_require={
        'apphost': ['py-postgresql']
    },
    packages=find_packages(),
    include_package_data=True,
    platforms='any',
    classifiers=[
        'Programming Language :: Python',
    ],
    entry_points={
        "resilient.circuits.components": [
            # When setup.py is executed, loop through the .py files in the components directory and create the entry points.
            "{}FunctionComponent = fn_scheduler.components.{}:FunctionComponent".format(snake_to_camel(get_module_name(filename)), get_module_name(filename)) for filename in glob.glob("./fn_scheduler/components/[a-zA-Z]*.py")
        ],
        "resilient.circuits.configsection": ["gen_config = fn_scheduler.util.config:config_section_data"],
        "resilient.circuits.customize": ["customize = fn_scheduler.util.customize:customization_data"],
        "resilient.circuits.selftest": ["selftest = fn_scheduler.util.selftest:selftest_function"]
    }
)


from custom_code.models import GladeCatalog
from django.core.management.base import BaseCommand

class Command(BaseCommand):

    help = 'Ingests the GLADE+ catalog into the database'

    def add_arguments(self, parser):
        parser.add_argument('--filename', help='Ingest catalog information from this file')

    def handle(self, *args, **options):

        filename = options['filename']

        with open(filename, 'r') as f:

            lines = f.readlines()
            column_names = ['pgc_no', 'gwgc_name', 'hyperleda_name', 'twomass_name',
                            'wisexscos_name', 'sdss_dr16q_name', 'object_type_flag',
                            'ra', 'dec']

            column_types = ['int', 'str', 'str', 'str', 'str', 'str', 'str', 'float', 'float']

            mag_dict_keys = ['B', 'B_err', 'B_flag', 'B_abs', 'J', 'J_err', 'H', 'H_err',
                             'K', 'K_err', 'W1', 'W1_err', 'W2', 'W2_err', 'W1_flag', 'B_J',
                             'B_J_err']

            column_names_two = ['z_helio', 'z_cmb', 'z_flag', 'v_err', 'z_err', 'd_l', 'd_l_err',
                                'dist_flag', 'm_star', 'm_star_err', 'm_star_flag', 'merger_rate',
                                'merger_rate_err']

            column_types_two = ['float', 'float', 'int', 'float', 'float', 'float', 'float', 'int',
                                'float', 'float', 'int', 'float', 'float']

            for line in lines:
                ### Insert into database
                x = line.split()
                param_dict = {}

                for i in range(len(column_names)):
                    if x[i+1] != 'null':

                        if column_types[i] == 'int':
                            param_dict[column_names[i]] = int(x[i+1])

                        elif column_types[i] == 'float':
                            param_dict[column_names[i]] = float(x[i+1])

                        else:
                            param_dict[column_names[i]] = x[i+1]

                mag_dict = {}

                for i in range(len(mag_dict_keys)):
                    if x[i+len(column_names)+1] != 'null' and 'flag' not in mag_dict_keys[i]:

                        mag_dict[mag_dict_keys[i]] = float(x[i+len(column_names)+1])

                    elif x[i+len(column_names)+1] != 'null':

                        mag_dict[mag_dict_keys[i]] = int(x[i+len(column_names)+1])

                param_dict['mag'] = mag_dict

                for i in range(len(column_names_two)):
                    if x[i+len(column_names)+len(mag_dict_keys)+1] != 'null':

                        if column_types_two[i] == 'int':
                            param_dict[column_names_two[i]] = int(x[i+len(column_names)+len(mag_dict_keys)+1])

                        elif column_types_two[i] == 'float':
                            param_dict[column_names_two[i]] = float(x[i+len(column_names)+len(mag_dict_keys)+1])

                        else:
                            param_dict[column_names_two[i]] = x[i+len(column_names)+len(mag_dict_keys)+1]

                g = GladeCatalog(**param_dict)
                g.save()


import click

from utils.raster import GeoTiffConverter
from utils.data import RasterData

@click.command()
@click.argument('datasets', type=lambda s: s.split(','))
def convert_to_zarr(datasets):
    """
    Convert GeoTIFFs to Zarr.
    """
    groups = {'global': ['recent'],#['historic', 'recent'],
              'scenarios': ['crop_I', 'crop_MG', 'crop_MGI', 'grass_part', 'grass_full', 'rewilding',
                            'degradation_ForestToGrass', 'degradation_ForestToCrop', 'degradation_NoDeforestation'],
              'experimental': ['stocks', 'concentration']}

    for dataset in datasets:
        print(dataset)
        for group in groups[dataset]:
            print(group)
            # Create an instance of a GeoTiffData Data Class with all data information
            geotiff_data = RasterData(dataset, group)
            # Save GeoTIFFs as Zarr
            geotiff_converter = GeoTiffConverter(geotiff_obj=geotiff_data)
            geotiff_converter.convert_to_zarr()

if __name__ == '__main__':
    convert_to_zarr()


import os
from setuptools import find_packages, setup

here = os.path.abspath(os.path.dirname(__file__))

with open(os.path.join("pycharge", "version.txt")) as file_handler:
    __version__ = file_handler.read().strip()

with open(os.path.join(here, "README.md"), encoding="utf-8") as f:
    long_description = f.read()

DESCRIPTION = 'ML Library'

# Setting up
setup(
    name="python-charge",
    version=__version__,
    author="Ashutosh Mishra",
    url="https://github.com/ASH1998/pYcharge",
    author_email="pythonicdevil@gmail.com",
    description=DESCRIPTION,
    license="MIT",
    long_description_content_type="text/markdown",
    long_description=long_description,
    packages=[package for package in find_packages() if package.startswith("pycharge")],
    install_requires=[],
    python_requires=">=3.8",
    keywords=['python', 'machine learning'],
    classifiers=[
        "Development Status :: 1 - Planning",
        "Intended Audience :: Developers",
        "Programming Language :: Python :: 3",
        "Operating System :: Unix",
        "Operating System :: MacOS :: MacOS X",
        "Operating System :: Microsoft :: Windows",
        "Programming Language :: Python :: 3",
        "Programming Language :: Python :: 3.7",
        "Programming Language :: Python :: 3.8",
        "Programming Language :: Python :: 3.9",
    ]
)


#!/usr/bin/env python3

import argparse
import json
import os
import subprocess
import glob

# Parse Arguements and print
parser = argparse.ArgumentParser()

parser.add_argument('--json', type=str, required=True)
parser.add_argument('--dir', type=str, required=True)
parser.add_argument('--mixtape', default=False, type=bool, required=False)

args = vars(parser.parse_args())

json_file = args['json']
flac_dir =  args['dir']
mixtape_bool = args['mixtape']
print('\n')
print(flac_dir)
print('\t' + json_file.split('/')[-1])
print('\t' + str(mixtape_bool))
print('\n')

with open(json_file) as f:
   data = json.load(f)

# Core fuction
def mod_song_meta_data(data: dict, file: str, mixtape_bool: bool):
    file_name = file.split('/')[-1]
    track_number = file_name.split(' - ')[0]
    print('\t' + data['Songs'][track_number]['ARTIST'])

    #Album
    tag_album = data['ALBUM']
    cmd_album = [
                'metaflac',
                '--remove-tag=ALBUM',
                '--set-tag=ALBUM=' + tag_album,
                file
                ]

    #AlbumArtist
    tag_alb_art = data['ALBUMARTIST']
    cmd_alb_art = [
                'metaflac',
                '--remove-tag=ALBUMARTIST',
                '--set-tag=ALBUMARTIST=' + tag_alb_art,
                file
                ]

    #MUSICBRAINZ_ALBUMARTIST
    tag_muscbrnz_alb_art = data['MUSICBRAINZ_ALBUMARTIST']
    cmd_muscbrnz_alb_art = [
                'metaflac',
                '--remove-tag=MUSICBRAINZ_ALBUMARTIST',
                '--set-tag=MUSICBRAINZ_ALBUMARTIST=' + tag_muscbrnz_alb_art,
                file
                ]

    #ARTIST
    if mixtape_bool:
        tag_art = data['Songs'][track_number]['ARTIST']
    else:
        tag_art = data['ARTIST']
    cmd_art = [
                'metaflac',
                '--remove-tag=ARTIST',
                '--set-tag=ARTIST=' + tag_art,
                file
                ]

    #TRACKNUMBER
    tag_trknum = str(data['Songs'][track_number]['TRACKNUMBER'])
    cmd_trknum = [
                'metaflac',
                '--remove-tag=TRACKNUMBER',
                '--set-tag=TRACKNUMBER=' + tag_trknum,
                file
                ]

    #TITLE
    tag_title = data['Songs'][track_number]['TITLE']
    cmd_title = [
                'metaflac',
                '--remove-tag=TITLE',
                '--set-tag=TITLE=' + tag_title,
                file
                ]

    #GENRE
    tag_genre = data['GENRE']
    cmd_genre = [
                'metaflac',
                '--remove-tag=GENRE',
                '--set-tag=GENRE=' + tag_genre,
                file
                ]

    # Run the commands to process the files
    out_album = subprocess.run(cmd_album,
                            capture_output=True)
    out_alb_art = subprocess.run(cmd_alb_art,
                                capture_output=True)
    out_muscbrnz_alb_art = subprocess.run(cmd_muscbrnz_alb_art,
                                        capture_output=True)
    out_art = subprocess.run(cmd_art,
                            capture_output=True)
    out_trknum = subprocess.run(cmd_trknum,
                                capture_output=True)
    out_title = subprocess.run(cmd_title,
                            capture_output=True)
    out_genre = subprocess.run(cmd_genre,
                            capture_output=True)

    #Change file Name
    file_path = file.split('/')
    file_path.pop()

    file_name_new = track_number + ' - ' + tag_title + '.flac'
    file_new = '/'.join(file_path) + '/' + file_name_new

    os.rename(file, file_new)

    return file_new #Could add a dictionary of subprocess outs.

# Loop through files
new_files = []
for i in glob.glob(flac_dir + '*.flac'):
    file_name = i.split('/')[-1]
    print(file_name)
    new_files.append(mod_song_meta_data(data, i, mixtape_bool))
    print('\t' + new_files[-1].split('/')[-1])

# Command line example check that the renameing worked. Could fold into a check.
#ls /Users/jcorwin/Desktop/MusicTemp/MixTapes/Lisa\ MixTape\ 01/

#metaflac --list /Users/jcorwin/Desktop/MusicTemp/MixTapes/Lisa\ MixTape\ 01/15\ -\ Uncle\ John\'s\ Band.flac | grep comment


#!/usr/bin/python
import ecto #ecto core library
from ecto.opts import scheduler_options, run_plasm, cell_options
from srv_ecto_vision import features3d
import ecto_ros
from ecto_ros import ecto_sensor_msgs
import ecto_opencv
from ecto_opencv.highgui import imshow, FPSDrawer
from ecto_opencv.features2d import DrawKeypoints
import argparse

#short names
ImageSub = ecto_sensor_msgs.Subscriber_Image
InfoSub = ecto_sensor_msgs.Subscriber_CameraInfo

debug = True

def do_ecto():
  parser = argparse.ArgumentParser(description="Stereo Feature Extractor")

  # add cell options
  feature_extractor_factory = cell_options(parser, features3d.FeatureExtractor, prefix="fe")

  # add scheduler options
  group = parser.add_argument_group("ecto scheduler options")
  scheduler_options(group)

  options = parser.parse_args()

  # setup ros subscribers
  subscriptions = dict(image_left  = ImageSub(topic_name="image_left",  queue_size=0),
                       #info_left   = InfoSub (topic_name="camera_info_left", queue_size=0),
                       image_right = ImageSub(topic_name="image_right", queue_size=0),
                       #info_right  = InfoSub (topic_name="camera_info_right", queue_size=0)
                       )
  sync = ecto_ros.Synchronizer("Synchronizer", subs=subscriptions)

  # setup cells
  image2mat_left = ecto_ros.Image2Mat()
  image2mat_right = ecto_ros.Image2Mat()
  feature_extractor_left  = feature_extractor_factory(options)
  feature_extractor_right = feature_extractor_factory(options)

  match_mask_creator = features3d.StereoMatchMaskCreator()
  descriptor_matcher = features3d.DescriptorMatcherKnn()
  matches_filter = features3d.MatchesFilter(verbose=True)

  #setup the processing graph
  graph = [
      sync["image_left"] >> image2mat_left["image"],
      image2mat_left["image"] >> feature_extractor_left["image"],
      sync["image_right"] >> image2mat_right["image"],
      image2mat_right["image"] >> feature_extractor_right["image"],

      feature_extractor_left["key_points"] >> match_mask_creator["key_points_left"],
      feature_extractor_right["key_points"] >> match_mask_creator["key_points_right"],

      feature_extractor_left["descriptors"] >> descriptor_matcher["train_descriptors"],
      feature_extractor_right["descriptors"] >> descriptor_matcher["test_descriptors"],

      descriptor_matcher["knn_matches"] >> matches_filter["knn_matches"],
      match_mask_creator["match_mask"] >> matches_filter["match_mask"],

      match_mask_creator["match_mask"] >> imshow(name="match mask")["image"],
      ]

  #instantiate a plasm
  plasm = ecto.Plasm()
  plasm.connect(graph)

  sched = ecto.schedulers.Singlethreaded(plasm)

  run_plasm(options, plasm, locals=vars())

if __name__ == '__main__':
  import sys
  # init ros (does topic remapping)
  ecto_ros.init(sys.argv, "stereo_feature_extraction")
  do_ecto()


from typing import Sequence
from Bio import SeqIO
from Bio.Seq import Seq,reverse_complement, transcribe, back_transcribe, translate
from Bio.SeqRecord import SeqRecord
import numpy as np
import itertools

def findORF(seq,x):
     ORF = []
     for letter in range(x, len(seq), 3):
          endCodon = seq[letter:letter+3]
          if(endCodon == "TAA" or endCodon == "TAG" or endCodon == "TGA" ):
                    for letter1 in range(letter + 3, len(seq),3):
                         StopCodon = seq[letter1:letter1+3]
                         if(StopCodon == "TAA" or StopCodon == "TAG" or StopCodon == "TGA"):
                              coding = seq[letter+3:letter1+3]
                              letter = letter1
                              if(len(coding)> 100):
                                   ORF.append(coding)
                              break
     return ORF

def findSeq(ORFS,x):
     CodingList = []
     for ORF in ORFS:
          for StartCodon in range(x, len(ORF), 3):
               cod = ORF[StartCodon:StartCodon+3]
               if(cod == "ATG"):
                    coding = ORF[StartCodon:len(ORF)]
                    #print(coding)
                    if(len(coding) > 100):
                         CodingList.append(coding)
                    break
     return CodingList

def CountCodonFreq(seq):
     totalCodons = len(seq)/3
     Letters = ['A','T','C','G']
     letterProducts = itertools.product(Letters, repeat=3)
     CodonList = []
     for prod in letterProducts:
          CodonList.append(''.join(prod))

     CodonDict = dict.fromkeys(CodonList, 0.0)
     for letter in range(0, len(seq), 3):
          Codon = seq[letter: letter + 3]
          CodonDict[Codon] += 1
     for Codon in CodonDict:
          CodonDict[Codon] = CodonDict[Codon] / totalCodons * 100
     return CodonDict

def CountDicodonFreq(seq):
     if(len(seq) % 6 != 0):
          seq = seq[:-3]
     totalDicodons = len(seq)/6
     Letters = ['A','T','C','G']
     letterProducts = itertools.product(Letters, repeat=6)
     DicodonList = []
     for prod in letterProducts:
          DicodonList.append(''.join(prod))

     DicodonDict = dict.fromkeys(DicodonList, 0.0)
     for letter in range(0, len(seq), 6):
          Dicodon = seq[letter: letter + 6]
          DicodonDict[Dicodon] += 1
     for Dicodon in DicodonDict:
          DicodonDict[Dicodon] = DicodonDict[Dicodon] / totalDicodons * 100
     return DicodonDict

def start_stop(seq,x):
     CodingList = []
     for letter in range(x, len(seq),3):
          startCodon = seq[letter:letter+3]
          if(startCodon == "ATG"):
               for letter1 in range(letter, len(seq),3):
                    endCodon = seq[letter1:letter1+3]
                    if(endCodon == "TAA" or endCodon == "TAG" or endCodon == "TGA"):
                         coding = seq[letter:letter1+3]
                         letter = letter1 + 3
                         if(len(coding) > 100):
                              CodingList.append(coding)
                         break
     return CodingList

def findCoding(Fasta):
     CodingList1 = findSeq(findORF(Fasta.seq,0), 0)
     CodingList2 = findSeq(findORF(Fasta.seq,1), 0)
     CodingList3 = findSeq(findORF(Fasta.seq,2), 0)
     reverse_fasta = Seq(Fasta.seq.reverse_complement())
     CodingList4 = findSeq(findORF(reverse_fasta,0), 0)
     CodingList5 = findSeq(findORF(reverse_fasta,1), 0)
     CodingList6 = findSeq(findORF(reverse_fasta,2), 0)

     Coding1 = Seq('').join(CodingList1)
     Coding2 = Seq('').join(CodingList2)
     Coding3 = Seq('').join(CodingList3)
     Coding4 = Seq('').join(CodingList4)
     Coding5 = Seq('').join(CodingList5)
     Coding6 = Seq('').join(CodingList6)
     AllCoding = Coding1 + Coding2 + Coding3 + Coding4 + Coding5 + Coding6
     return AllCoding

Lactococcus_phage = SeqIO.read("C:\\Users\\Paulius\\Documents\\Bioinformatics\\viruses\\data\\bacterial1.fasta","fasta")
Escherichia_phage = SeqIO.read("C:\\Users\\Paulius\\Documents\\Bioinformatics\\viruses\\data\\bacterial2.fasta","fasta")
Streptococcus_phage = SeqIO.read("C:\\Users\\Paulius\\Documents\\Bioinformatics\\viruses\\data\\bacterial3.fasta","fasta")
Cellulophaga_phage = SeqIO.read("C:\\Users\\Paulius\\Documents\\Bioinformatics\\viruses\\data\\bacterial4.fasta","fasta")
coronavirus = SeqIO.read("C:\\Users\\Paulius\\Documents\\Bioinformatics\\viruses\\data\\mamalian1.fasta","fasta")
adenovirus = SeqIO.read("C:\\Users\\Paulius\\Documents\\Bioinformatics\\viruses\\data\\mamalian2.fasta","fasta")
Variola_virus = SeqIO.read("C:\\Users\\Paulius\\Documents\\Bioinformatics\\viruses\\data\\mamalian3.fasta","fasta")
herpesvirus = SeqIO.read("C:\\Users\\Paulius\\Documents\\Bioinformatics\\viruses\\data\\mamalian4.fasta","fasta")

Codon = CountCodonFreq(findCoding(Lactococcus_phage))

CodonFreq = []
CodonFreq.append(CountCodonFreq(findCoding(Lactococcus_phage)))
CodonFreq.append(CountCodonFreq(findCoding(Escherichia_phage)))
CodonFreq.append(CountCodonFreq(findCoding(Streptococcus_phage)))
CodonFreq.append(CountCodonFreq(findCoding(Cellulophaga_phage)))
CodonFreq.append(CountCodonFreq(findCoding(coronavirus)))
CodonFreq.append(CountCodonFreq(findCoding(adenovirus)))
CodonFreq.append(CountCodonFreq(findCoding(Variola_virus)))
CodonFreq.append(CountCodonFreq(findCoding(herpesvirus)))

Freq = []

CodonMatrix = [[0 for i in range(8)] for j in range (8)]
for i in range(0,8):
     for j in range(0,8):
          for key in Codon:
               Freq.append((CodonFreq[i][key] - CodonFreq[j][key])**2)
          CodonMatrix[i][j] = sum(Freq)
          Freq = []

np.savetxt("CodonFreq.txt", CodonMatrix)

Dicodon = CountDicodonFreq(findCoding(Lactococcus_phage))
DicodonFreq = []
DicodonFreq.append(CountDicodonFreq(findCoding(Lactococcus_phage)))
DicodonFreq.append(CountDicodonFreq(findCoding(Escherichia_phage)))
DicodonFreq.append(CountDicodonFreq(findCoding(Streptococcus_phage)))
DicodonFreq.append(CountDicodonFreq(findCoding(Cellulophaga_phage)))
DicodonFreq.append(CountDicodonFreq(findCoding(coronavirus)))
DicodonFreq.append(CountDicodonFreq(findCoding(adenovirus)))
DicodonFreq.append(CountDicodonFreq(findCoding(Variola_virus)))
DicodonFreq.append(CountDicodonFreq(findCoding(herpesvirus)))

Freq = []

DicodonMatrix = [[0 for i in range(8)] for j in range (8)]
for i in range(0,8):
     for j in range(0,8):
          for key in Dicodon:
               Freq.append((DicodonFreq[i][key] - DicodonFreq[j][key])**2)
          DicodonMatrix[i][j] = sum(Freq)
          Freq = []

np.savetxt("DicodonFreq.txt", DicodonMatrix)


# -*- coding: utf-8 -*-
from PyQt5 import QtGui,QtCore,QtWidgets
import math
import codescene
import time
import ui.CodeUIItem as CodeUIItem

class CodeView(QtWidgets.QGraphicsView):
	def __init__(self, *args):
		super(CodeView, self).__init__(*args)
		from UIManager import UIManager
		self.setScene(UIManager.instance().getScene())
		self.setViewportUpdateMode(QtWidgets.QGraphicsView.FullViewportUpdate)
		self.setCacheMode(QtWidgets.QGraphicsView.CacheNone)
		#self.setDragMode(QtWidgets.QGraphicsView.RubberBandDrag)
		self.setTransformationAnchor(QtWidgets.QGraphicsView.AnchorUnderMouse)
		self.setMouseTracking(True)
		self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
		self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
		self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
		self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
		self.setAcceptDrops(True)

		self.mousePressPnt = None
		self.mouseCurPnt = None
		self.isFrameSelectMode = False
		self.isMousePressed = False

		self.updateTimer = QtCore.QTimer()
		self.updateTimer.setInterval(70)
		# self.connect(self.updateTimer, QtCore.SIGNAL('timeout()'), self, QtCore.SLOT('updateView()'))
		self.updateTimer.timeout.connect(self.updateView)
		self.centerPnt = QtCore.QPointF()
		self.scale(0.6,0.6)
		self.brushRadius = 8
		self.setBackgroundBrush(QtGui.QBrush(QtGui.QColor(50,50,50)))
		self.hudFont = QtGui.QFont('tahoma', 8)
		self.hudFontMetric = QtGui.QFontMetrics(self.hudFont)

	@QtCore.pyqtSlot()
	def updateView(self):
		scene = self.scene()
		if scene:
			scene.acquireLock()
			pos = scene.getSelectedCenter()
			self.centerPnt = self.centerPnt * 0.97 + pos * 0.03
			self.centerOn(self.centerPnt)
			scene.releaseLock()

	def keyPressEvent(self, event):
		from UIManager import UIManager
		mainUI = UIManager.instance().getMainUI()
		self.setCursor(QtCore.Qt.ArrowCursor)
		# if event.modifiers() == QtCore.Qt.AltModifier:
		if event.key() == QtCore.Qt.Key_Up:
			mainUI.goToUp()
		elif event.key() == QtCore.Qt.Key_Down:
			mainUI.goToDown()
		elif event.key() == QtCore.Qt.Key_Left:
			mainUI.goToLeft()
		elif event.key() == QtCore.Qt.Key_Right:
			mainUI.goToRight()
		elif event.modifiers() == QtCore.Qt.AltModifier:
			if event.key() == QtCore.Qt.Key_1:
				mainUI.showScheme([1, True])
			elif event.key() == QtCore.Qt.Key_2:
				mainUI.showScheme([2, True])
			elif event.key() == QtCore.Qt.Key_3:
				mainUI.showScheme([3, True])
			elif event.key() == QtCore.Qt.Key_4:
				mainUI.showScheme([4, True])
			elif event.key() == QtCore.Qt.Key_5:
				mainUI.showScheme([5, True])
			elif event.key() == QtCore.Qt.Key_6:
				mainUI.showScheme([6, True])
			elif event.key() == QtCore.Qt.Key_7:
				mainUI.showScheme([7, True])
			elif event.key() == QtCore.Qt.Key_8:
				mainUI.showScheme([8, True])
			elif event.key() == QtCore.Qt.Key_9:
				mainUI.showScheme([9, True])
		elif event.modifiers() == QtCore.Qt.ControlModifier:
			if event.key() == QtCore.Qt.Key_1:
				mainUI.toggleSelectedEdgeToScheme([1, True])
			elif event.key() == QtCore.Qt.Key_2:
				mainUI.toggleSelectedEdgeToScheme([2, True])
			elif event.key() == QtCore.Qt.Key_3:
				mainUI.toggleSelectedEdgeToScheme([3, True])
			elif event.key() == QtCore.Qt.Key_4:
				mainUI.toggleSelectedEdgeToScheme([4, True])
			elif event.key() == QtCore.Qt.Key_5:
				mainUI.toggleSelectedEdgeToScheme([5, True])
			elif event.key() == QtCore.Qt.Key_6:
				mainUI.toggleSelectedEdgeToScheme([6, True])
			elif event.key() == QtCore.Qt.Key_7:
				mainUI.toggleSelectedEdgeToScheme([7, True])
			elif event.key() == QtCore.Qt.Key_8:
				mainUI.toggleSelectedEdgeToScheme([8, True])
			elif event.key() == QtCore.Qt.Key_9:
				mainUI.toggleSelectedEdgeToScheme([9, True])
		else:
			super(CodeView, self).keyPressEvent(event)
		self.viewport().update()

	def keyReleaseEvent(self, event):
		self.setCursor(QtCore.Qt.ArrowCursor)
		if event.key() == QtCore.Qt.Key_Control or event.key() == QtCore.Qt.Key_Shift:
			pass
		else:
			super(CodeView, self).keyReleaseEvent(event)

	def mousePressEvent(self, event):
		self.mouseCurPnt = self.mousePressPnt = event.pos()
		self.isMousePressed = True
		item = self.itemAt(self.mousePressPnt)
		self.isFrameSelectMode = (not item)

		modifiers = QtWidgets.QApplication.keyboardModifiers()
		if modifiers == QtCore.Qt.ControlModifier or modifiers == QtCore.Qt.ShiftModifier:
			if item:
				item.setSelected(not item.isSelected())
		else:
			super(CodeView, self).mousePressEvent(event)

	def mouseMoveEvent(self, event):
		self.mouseCurPnt = event.pos()
		if self.isFrameSelectMode:
			pass
		super(CodeView,self).mouseMoveEvent(event)
		#self.invalidateScene(self.scene().sceneRect())
		#self.update()
		self.viewport().update()

	def mouseReleaseEvent(self, event):
		self.mouseCurPnt = event.pos()
		self.isMousePressed = False
		if self.isFrameSelectMode:
			topLeftX = min(self.mousePressPnt.x(), self.mouseCurPnt.x())
			topLeftY = min(self.mousePressPnt.y(), self.mouseCurPnt.y())
			width = abs(self.mousePressPnt.x()-self.mouseCurPnt.x())
			height= abs(self.mousePressPnt.y()-self.mouseCurPnt.y())
			self.isFrameSelectMode = False
			itemList = self.items(topLeftX, topLeftY, width, height)

			modifiers = QtWidgets.QApplication.keyboardModifiers()
			if modifiers == QtCore.Qt.ShiftModifier:
				for item in itemList:
					item.setSelected(False)
			else:
				if modifiers != QtCore.Qt.ControlModifier:
					self.scene().clearSelection()
				for item in itemList:
					item.setSelected(True)

		super(CodeView, self).mouseReleaseEvent(event)

	def wheelEvent(self, event):
		posScene = self.mapToScene(event.pos())
		factor = 1.001 ** event.delta()
		self.scale(factor, factor)

		posMouse = self.mapFromScene(posScene)
		mov = posMouse - event.pos()
		self.horizontalScrollBar().setValue(mov.x() + self.horizontalScrollBar().value())
		self.verticalScrollBar().setValue(mov.x() + self.verticalScrollBar().value())

		self.centerPnt = self.mapToScene(self.viewport().rect().center())

	def drawForeground(self, painter, rectF):
		super(CodeView, self).drawForeground(painter, rectF)

		if self.isFrameSelectMode:
			if self.mousePressPnt and self.mouseCurPnt:
				topLeftX = min(self.mousePressPnt.x(), self.mouseCurPnt.x())
				topLeftY = min(self.mousePressPnt.y(), self.mouseCurPnt.y())
				width = abs(self.mousePressPnt.x()-self.mouseCurPnt.x())
				height= abs(self.mousePressPnt.y()-self.mouseCurPnt.y())

				painter.setPen(QtGui.QPen(QtGui.QColor(100,164,230),1.0))
				painter.setBrush(QtGui.QBrush(QtGui.QColor(100,164,230,100)))
				painter.setTransform(QtGui.QTransform())
				painter.drawRect(topLeftX, topLeftY, width, height)
		self.drawScheme(painter, rectF)
		self.drawLegend(painter, rectF)

	def drawScheme(self, painter, rectF):
		from UIManager import UIManager
		scene = UIManager.instance().getScene()
		schemeList = scene.getCurrentSchemeList()
		nScheme = len(schemeList)
		if not nScheme:
			return
		painter.setTransform(QtGui.QTransform())
		painter.setFont(self.hudFont)
		colorList = scene.getCurrentSchemeColorList()
		cw = 10
		y  = 10

		maxWidth = 0
		for ithScheme, schemeName in enumerate(schemeList):
			schemeSize = self.hudFontMetric.size(QtCore.Qt.TextSingleLine, schemeName)
			maxWidth = max(maxWidth, schemeSize.width())

		painter.setCompositionMode(QtGui.QPainter.CompositionMode_Multiply)
		painter.setPen(QtCore.Qt.NoPen)
		painter.setBrush(QtGui.QColor(0,0,0,150))
		painter.drawRect(5,5, 80 + maxWidth, nScheme * cw + (nScheme-1)*2 + 10)

		painter.setCompositionMode(QtGui.QPainter.CompositionMode_Source)
		for ithScheme, schemeName in enumerate(schemeList):
			painter.setPen(QtCore.Qt.NoPen)
			painter.setBrush(QtGui.QBrush(colorList[ithScheme]))
			painter.drawRect(QtCore.QRect(10,y+5,20,2))

			painter.setPen(QtGui.QPen(QtGui.QColor(255,157,38,255),1))
			painter.drawText(39, y+cw, 'Alt + %s' % (ithScheme+1,))
			painter.setPen(QtGui.QPen(QtGui.QColor(255,255,255,255),1))
			painter.drawText(QtCore.QRect(80,y-1,maxWidth,cw+3), QtCore.Qt.AlignRight | QtCore.Qt.AlignTop, schemeName)
			y += cw + 2

	def drawLegend(self, painter, rectF):
		painter.setTransform(QtGui.QTransform())
		painter.setFont(self.hudFont)

		from UIManager import UIManager
		scene = UIManager.instance().getScene()
		itemDict = scene.getItemDict()
		classNameDict = {}
		for uname, item in itemDict.items():
			if item.isSelected() or item.isConnectedToFocusNode:
				cname = item.getClassName()
				if not cname:
					cname = '[global function]'
				classNameDict[cname] = item.getColor()

		maxWidth = 0
		cw = 10
		nClasses = len(classNameDict)
		if not nClasses:
			return

		for className in classNameDict.keys():
			classSize = self.hudFontMetric.size(QtCore.Qt.TextSingleLine, className)
			maxWidth = max(maxWidth, classSize.width())

		painter.setCompositionMode(QtGui.QPainter.CompositionMode_Multiply)
		painter.setPen(QtCore.Qt.NoPen)
		painter.setBrush(QtGui.QColor(0,0,0,150))
		interiorHeight = nClasses * cw + (nClasses-1)*2
		painter.drawRect(5,self.height()-15-interiorHeight, 22 + maxWidth, interiorHeight + 10)

		painter.setCompositionMode(QtGui.QPainter.CompositionMode_Source)
		y = self.height() - 20
		for cname, clr in classNameDict.items():
			painter.setPen(QtCore.Qt.NoPen)
			painter.setBrush(QtGui.QBrush(clr))
			painter.drawRect(QtCore.QRect(10,y,cw,cw))

			painter.setPen(QtGui.QPen(QtGui.QColor(255,255,255,255),1))
			painter.drawText(cw+12, y+cw, cname)
			y -= cw + 2

	def drawComment(self, painter, rectF):
		from UIManager import UIManager
		scene = UIManager.instance().getScene()

		for uname, itemData in scene.itemDataDict.items():
			item = scene.itemDict.get(uname, None)
			if not item:
				continue

	def paintEvent(self, QPaintEvent):
		scene = self.scene()
		scene.acquireLock()
		t0 = time.time()
		QtWidgets.QGraphicsView.paintEvent(self, QPaintEvent)
		scene.releaseLock()


import glob
from setuptools import setup, find_packages
from setuptools.extension import Extension
from setupext import casacorebuild_ext

PKGNAME = "pyrap.quanta"
EXTNAME = "_quanta"
casalibs = [] # casa_casa is added by default

casaextension = Extension(name="%s.%s" % (PKGNAME, EXTNAME),
                          sources = glob.glob('src/*.cc'),
                          depends = glob.glob('src/*.h'),
                          libraries = casalibs)
setup(name = PKGNAME,
      version = 'trunk',
      description = 'Python bindings to casacore Quanta',
      author = 'Malte Marquarding',
      author_email = 'Malte.Marquarding@csiro.au',
      url = 'http://code.google.com/p/pyrap',
      keywords = ['quanta','casacore', 'unit conversion'],
      long_description = '''
This is a python module to do unit conversion using the casacore Quanta c++ library.
''',
      packages = find_packages(),
      namespace_packages = ["pyrap"],
      license = 'GPL',
      zip_safe = 0,
      ext_modules =[ casaextension ],
      cmdclass={'build_ext': casacorebuild_ext})


import json
from flask import Blueprint, render_template, redirect, request
from ym_backend import controller

product = Blueprint('product',__name__)
ProductCtrl = controller.Product(__name__)

@product.route('/')
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
def index():
  return render_template('./index.html')

# 注册
@product.route('/addProduct', methods=['POST'])
def addProduct():
  name = request.form['name']
  pic = request.form['pic']
  params = {
    'name': name,
    'pic': pic
  }
  return ProductCtrl.addProduct(params)

# 查询用户信息
@product.route('/queryProduct', methods=['POST', 'GET'])
def queryProduct():
  if request.method == 'POST':
    name = request.form['name']
    params = {
      'name': name
    }
    return ProductCtrl.queryProduct(params)
  elif request.method == 'GET':
    return ProductCtrl.queryProductAll()

# 修改用户信息
@product.route('/modifyProduct', methods=['POST'])
def modifyProduct():
  name = request.form['name']
  pic = request.form['pic']
  params = {
    'name': name,
    'pic': pic
  }
  return ProductCtrl.modifyProduct(params)

# 删除用户信息
@product.route('/delProduct', methods=['POST'])
def delProduct():
  id = request.form['id']
  return ProductCtrl.delProduct({'id': id})


# -*- coding:utf-8 -*-
import cv2
import numpy as np
import sys

def videoBlueColorCapture():

    cap = cv2.VideoCapture(0)

    while (1):

        # 读取视频的每一帧
        _, frame = cap.read()

        # 将图片从 BGR 空间转换到 HSV 空间
        hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

        # 定义在HSV空间中蓝色的范围
        lower_blue = np.array([110, 50, 50])
        upper_blue = np.array([130, 255, 255])

        # 根据以上定义的蓝色的阈值得到蓝色的部分
        mask = cv2.inRange(hsv, lower_blue, upper_blue)

        res = cv2.bitwise_and(frame, frame, mask=mask)

        cv2.imshow('frame', frame)
        cv2.imshow('mask', mask)
        cv2.imshow('res', res)
        k = cv2.waitKey(5) & 0xFF
        if k == 27:
            break

    cv2.destroyAllWindows()

def videoCapture():
    cap = cv2.VideoCapture(0)
    # cap = cv2.VideoCapture('test.avi') #use local file to create capture
    # Define the codec and create VideoWriter object
    fourcc = cv2.VideoWriter_fourcc(*'X264')
    out = cv2.VideoWriter('output.avi', fourcc, 25.0, (640, 480))

    while (cap.isOpened()):
        ret, frame = cap.read()
        if ret == True:
            frame = cv2.flip(frame, 0)

            # write the flipped frame
            out.write(frame)

            cv2.imshow('frame', frame)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
        else:
            break

    # Release everything if job is finished
    cap.release()
    out.release()
    cv2.destroyAllWindows()

def CatchUsbVideo(window_name, camera_idx):
    cv2.namedWindow(window_name)

    # 视频来源，可以来自一段已存好的视频，也可以直接来自USB摄像头
    cap = cv2.VideoCapture(camera_idx)

    while cap.isOpened():
        ok, frame = cap.read()  # 读取一帧数据
        if not ok:
            break

            # 显示图像并等待10毫秒按键输入，输入‘q’退出程序
        cv2.imshow(window_name, frame)
        c = cv2.waitKey(10)
        if c & 0xFF == ord('q'):
            break

            # 释放摄像头并销毁所有窗口
    cap.release()
    cv2.destroyAllWindows()

if __name__ == '__main__':
    if len(sys.argv) != 2:
        CatchUsbVideo("截取视频流", int(0))
        #print("Usage:%s camera_id\r\n" % (sys.argv[0]))
    else:
        CatchUsbVideo("截取视频流", int(sys.argv[1]))


from collections import defaultdict
from aocd.models import Puzzle

class Intcomp():
    def __init__(self, code: list):
        self.code = None
        self.orig_code = None
        self.load(code)
        self.ip = 0
        self.rel_base = 0
        self.num_of_operands = [0, 3, 3, 1, 1, 2, 2, 3, 3, 1]
        self.out = []

    def load(self, code: list):
        self.orig_code = code
        self.code = defaultdict(int, enumerate(code))

    def set(self, code: list = None, pointer: int = None, base: int = None):
        if code is not None:
            self.load(code)
        self.ip = pointer if pointer is not None else self.ip
        self.rel_base = base if base is not None else self.rel_base

    def reset(self):
        self.set(code=self.orig_code, pointer=0, base=0)
        self.out = []

    def run(self, inp: tuple = None, halt_after=-1):
        self.out = []
        if inp is None:
            inp = (0,)
        inp_i = 0
        while self.code[self.ip] != 99:
            if 0 < halt_after <= len(self.out):
                return self.out
            modes = [int(x) for x in f"{self.code[self.ip]:0>5}"[:3]][::-1]
            instruction = int(f"{self.code[self.ip]:0>5}"[3:])
            operands = [0 for _ in range(self.num_of_operands[instruction])]
            adjust = [0 for _ in range(self.num_of_operands[instruction])]
            for m in range(self.num_of_operands[instruction]):
                if modes[m] == 0:
                    operands[m] = self.code[self.code[self.ip + m + 1]]
                elif modes[m] == 1:
                    operands[m] = self.code[self.ip + m + 1]
                elif modes[m] == 2:
                    adjust[m] = self.rel_base
                    operands[m] = self.code[self.rel_base + self.code[self.ip + m + 1]]

            if instruction == 1:  # ADD
                self.code[adjust[2] + self.code[self.ip + 3]] = operands[0] + operands[1]
            elif instruction == 2:  # MULTIPLY
                self.code[adjust[2] + self.code[self.ip + 3]] = operands[0] * operands[1]
            elif instruction == 3:  # INPUT
                self.code[adjust[0] + self.code[self.ip + 1]] = inp[inp_i]
                inp_i = (inp_i + 1) % len(inp)
            elif instruction == 4:  # OUTPUT
                self.out.append(operands[0])
            elif instruction == 5:  # JUMP IF TRUE
                self.ip = (operands[1] - 3) if operands[0] != 0 else self.ip
            elif instruction == 6:  # JUMP IF FALSE
                self.ip = (operands[1] - 3) if operands[0] == 0 else self.ip
            elif instruction == 7:  # TRUE IS LESS THAN
                self.code[adjust[2] + self.code[self.ip + 3]] = int(operands[0] < operands[1])
            elif instruction == 8:  # TRUE IF EQUAL
                self.code[adjust[2] + self.code[self.ip + 3]] = int(operands[0] == operands[1])
            elif instruction == 9:  # INCREASE RELATIVE BASE
                self.rel_base += operands[0]
            self.ip += self.num_of_operands[instruction] + 1

if __name__ == "__main__":
    puzzle = Puzzle(year=2019, day=9)
    in9 = [int(x) for x in puzzle.input_data.split(',')]

    prog = Intcomp(code=in9)
    prog.run(1)
    print(prog.out, prog.out[-1])
    # puzzle.answer_a = proga.out[-1]

    prog.reset()
    prog.run(2)
    print(prog.out, prog.out[-1])
    # puzzle.answer_b = prog.out[-1]


import logging
import typing

import scenario
from scenario.reflex import qualname
import scenario.test
import scenario.text

class LoadConfigFile(scenario.test.Step):

    def __init__(
            self,
            input_path,  # type: scenario.Path
            root_key="",  # type: str
    ):  # type: (...) -> None
        scenario.test.Step.__init__(self)

        self.input_path = input_path  # type: scenario.Path
        self.root_key = root_key  # type: str

    def step(self):  # type: (...) -> None
        self.STEP(f"Load '{self.input_path}'")

        _root_key_spec = f" from key {self.root_key!r}" if self.root_key else " at the root level"  # type:str
        if self.ACTION(f"Load the '{self.input_path}' configuration file{_root_key_spec}."):
            scenario.conf.loadfile(self.input_path, root=self.root_key)

class SaveConfigFile(scenario.test.Step):

    def __init__(
            self,
            output_path,  # type: scenario.Path
            root_key="",  # type: str
    ):  # type: (...) -> None
        scenario.test.Step.__init__(self)

        self.output_path = output_path  # type: scenario.Path
        self.root_key = root_key  # type: str

    def step(self):  # type: (...) -> None
        self.STEP(f"Save '{self.output_path}'")

        _root_key_spec = f" from key {self.root_key!r}" if self.root_key else " from the root level"  # type:str
        if self.ACTION(f"Save the configuration database{_root_key_spec} to file '{self.output_path}'"):
            scenario.conf.savefile(self.output_path, root=self.root_key)

class ShowConfigValue(scenario.test.Step):

    def __init__(
            self,
            key,  # type: str
    ):  # type: (...) -> None
        scenario.test.Step.__init__(self)

        self.key = key  # type: str

    def step(self):  # type: (...) -> None
        self.STEP(f"Show {self.key}")

        if self.ACTION(f"Show configuration key {self.key!r}."):
            scenario.conf.show(logging.INFO)

class StoreConfigValue(scenario.test.Step):

    def __init__(
            self,
            key,  # type: str
            value,  # type: typing.Any
    ):  # type: (...) -> None
        scenario.test.Step.__init__(self)

        self.key = key  # type: str
        self.value = value  # type: typing.Any

    def step(self):  # type: (...) -> None
        self.STEP(f"Set {self.key}={self.value!r}")

        if self.ACTION(f"Store {self.key}={self.value!r} in the configuration database."):
            scenario.conf.set(self.key, self.value)

class RemoveConfigValue(scenario.test.Step):

    def __init__(
            self,
            key,  # type: str
    ):  # type: (...) -> None
        scenario.test.Step.__init__(self)

        self.key = key  # type: str

    def step(self):  # type: (...) -> None
        self.STEP(f"Unset {self.key}")

        if self.ACTION(f"Remove {self.key!r} from the configuration database."):
            scenario.conf.remove(self.key)

class CheckDictConfigNode(scenario.test.Step):

    def __init__(
            self,
            key,  # type: str
            field_names,  # type: typing.List[str]
    ):  # type: (...) -> None
        scenario.test.Step.__init__(self)

        self.key = key  # type: str
        self.field_names = field_names  # type: typing.List[str]

    def step(self):  # type: (...) -> None
        self.STEP(f"Check {self.key!r} section node")

        _data = None  # type: typing.Any
        if self.ACTION(f"Retrieve configuration data for key {self.key!r}."):
            _data = scenario.conf.get(self.key)
            self.evidence(f"Data: {scenario.debug.saferepr(_data)}")
        if self.RESULT("The data retrieved is a dictionary."):
            self.assertisinstance(
                _data, dict,
                evidence=f"{self.key!r} data type",
            )
        _fields_txt = scenario.text.Countable("field", self.field_names)  # type: scenario.text.Countable
        if self.RESULT(f"It contains {len(_fields_txt)} {_fields_txt}{_fields_txt.ifany(':', '.')}"):
            self.assertlen(
                _data, len(self.field_names),
                evidence=f"{self.key!r} number of fields",
            )
        for _field_name in self.field_names:  # type: str
            if self.RESULT(f"- {_field_name!r}"):
                self.assertin(
                    _field_name, _data,
                    evidence=f"{self.key!r} field {_field_name!r}",
                )

class CheckListConfigNode(scenario.test.Step):

    def __init__(
            self,
            key,  # type: str
            length,  # type: int
    ):  # type: (...) -> None
        scenario.test.Step.__init__(self)

        self.key = key  # type: str
        self.length = length  # type: int

    def step(self):  # type: (...) -> None
        self.STEP(f"Check {self.key!r} list node")

        _data = None  # type: typing.Any
        if self.ACTION(f"Retrieve configuration data for key {self.key!r}."):
            _data = scenario.conf.get(self.key)
            self.evidence(f"Data: {scenario.debug.saferepr(_data)}")
        if self.RESULT("The data retrieved is a list."):
            self.assertisinstance(
                _data, list,
                evidence=f"{self.key!r} data type",
            )
        _items_txt = scenario.text.Countable("item", self.length)  # type: scenario.text.Countable
        if self.RESULT(f"It contains {len(_items_txt)} {_items_txt}."):
            self.assertlen(
                _data, self.length,
                evidence=f"{self.key!r} number of items",
            )

class CheckConfigValue(scenario.test.Step):

    def __init__(
            self,
            key,  # type: str
            read_as,  # type: typing.Optional[type]
            expected_type,  # type: type
            expected_value=None,  # type: typing.Any
            origin=None,  # type: str
    ):  # type: (...) -> None
        scenario.test.Step.__init__(self)

        self.key = key  # type: str
        self.read_as = read_as  # type: typing.Optional[type]
        self.expected_type = expected_type  # type: type
        self.expected_value = expected_value  # type: typing.Optional[typing.Any]
        self.origin = origin  # type: typing.Optional[str]

        self.value_read = None  # type: typing.Any

    def step(self):  # type: (...) -> None
        self.STEP(f"Read {self.key!r}{f' as `{qualname(self.read_as)}`' if self.read_as is not None else ''}")

        if self.read_as is None:
            self.ACTION(f"Read {self.key!r} without specifying the type.")
        else:
            self.ACTION(f"Read {self.key!r} as a `{qualname(self.read_as)}` value.")

        _value_error = None  # type: typing.Optional[ValueError]
        if self.doexecute():
            try:
                if self.read_as is None:
                    self.value_read = scenario.conf.get(self.key)
                else:
                    self.value_read = scenario.conf.get(self.key, type=self.read_as)
                self.evidence(f"Value read: {scenario.debug.saferepr(self.value_read)}")
            except ValueError as _err:
                _value_error = _err

        if self.expected_type is not ValueError:
            if self.RESULT(f"The type of the value read is `{qualname(self.expected_type)}`."):
                self.assertisinstance(
                    self.value_read, self.expected_type,
                    evidence=f"Value type{f' read as `{qualname(self.read_as)}`' if self.read_as is not None else ''}",
                )
            if self.expected_value is not None:
                if self.RESULT(f"The value read is {self.expected_value!r}."):
                    self.assertequal(
                        self.value_read, self.expected_value,
                        evidence="Value",
                    )
        else:
            if self.RESULT("A `ValueError` exception is raised."):
                self.assertisnotnone(
                    _value_error,
                    evidence="Value error",
                )
            if self.origin is not None:
                if self.RESULT("The exception gives the origin of the bad configuration value."):
                    self.assertin(
                        self.origin, str(_value_error),
                        evidence="Configuration value origin",
                    )


from itertools import islice

def extract_mongodb(client, dbs, coll, initial_id=None, extract_by_batch=None):
    """
    export data from mongodb to json.

    Arg:
        client = ``MongoClient()``.

        dbs = name of database.

        coll = name of collection.

        initial_id = document id in ``objectID`` or any unique keys, default ``None``

        extract_by_batch = ``int`` batch of rows , default ``None``
    Return:
        list_of_docs
    """
    with client:
        db=client[dbs]
        fetch_before=db[coll].find()
        fetch=db[coll].find()
        list_of_docs=[]
        count=0
        if initial_id is not None:                                          # determine which row to start
            for doc in fetch_before:
                count+=1
                if initial_id == None:
                    count=0
                    break
                if initial_id == doc['_id']:
                    break

        if extract_by_batch is None and initial_id is None:
            for docs in fetch:
                docs['_id']=str(docs['_id'])
                list_of_docs.append(docs)
            print('extract all')
        elif extract_by_batch is None and initial_id is not None:
            for docs in islice(fetch, count):
                docs['_id']=str(docs['_id'])
                list_of_docs.append(docs)
            print('extract all start at {}'.format(count))
        elif extract_by_batch is not None and initial_id is None:
            for docs in islice(fetch, 0, count+extract_by_batch):
                docs['_id']=str(docs['_id'])
                list_of_docs.append(docs)
            print('extract_by_batch {} at {}'.format(extract_by_batch, count))
        elif extract_by_batch is not None and initial_id is not None:
            for docs in islice(fetch, count, count+extract_by_batch):
                docs['_id']=str(docs['_id'])
                list_of_docs.append(docs)
            print('extract_by_batch {} at {}'.format(extract_by_batch, count))
        print(len(list_of_docs),"'s rows from {} is being extract'".format(coll))
        del fetch_before, fetch
    return list_of_docs