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

import logging
import os
import subprocess

from galaxy.datatypes.data import get_file_peek, Text
from galaxy.datatypes.metadata import MetadataElement

def count_special_lines(word, filename, invert=False):
    """
    searching for special 'words' using the grep tool
    grep is used to speed up the searching and counting
    The number of hits is returned.
    """
    try:
        cmd = ["grep", "-c"]
        if invert:
            cmd.append("-v")
        cmd.extend([word, filename])
        out = subprocess.Popen(cmd, stdout=subprocess.PIPE)
        return int(out.communicate()[0].split()[0])
    except Exception:
        pass
    return 0

def count_lines(filename, non_empty=False):
    """
    counting the number of lines from the 'filename' file
    """
    try:
        if non_empty:
            out = subprocess.Popen(
                ["grep", "-cve", "^\s*$", filename], stdout=subprocess.PIPE  # noqa W605
            )
        else:
            out = subprocess.Popen(["wc", "-l", filename], stdout=subprocess.PIPE)
        return int(out.communicate()[0].split()[0])
    except Exception:
        pass
    return 0

class Infernal_CM_1_1(Text):
    file_ext = "cm"

    MetadataElement(
        name="number_of_models",
        default=0,
        desc="Number of covariance models",
        readonly=True,
        visible=True,
        optional=True,
        no_value=0,
    )

    def set_peek(self, dataset, is_multi_byte=False):
        if not dataset.dataset.purged:
            dataset.peek = get_file_peek(dataset.file_name, is_multi_byte=is_multi_byte)
            if dataset.metadata.number_of_models == 1:
                dataset.blurb = "1 model"
            else:
                dataset.blurb = "%s models" % dataset.metadata.number_of_models
            dataset.peek = get_file_peek(dataset.file_name, is_multi_byte=is_multi_byte)
        else:
            dataset.peek = "file does not exist"
            dataset.blurb = "file purged from disc"

    def sniff(self, filename):
        if count_special_lines("^INFERNAL1/a", filename) > 0:
            return True
        else:
            return False

    def set_meta(self, dataset, **kwd):
        """
        Set the number of models in dataset.
        """
        dataset.metadata.number_of_models = count_special_lines(
            "^INFERNAL1/a", dataset.file_name
        )

    def split(cls, input_datasets, subdir_generator_function, split_params):
        """
        Split the input files by model records.
        """
        if split_params is None:
            return None

        if len(input_datasets) > 1:
            raise Exception("CM-file splitting does not support multiple files")
        input_files = [ds.file_name for ds in input_datasets]

        chunk_size = None
        if split_params["split_mode"] == "number_of_parts":
            raise Exception(
                'Split mode "%s" is currently not implemented for CM-files.'
                % split_params["split_mode"]
            )
        elif split_params["split_mode"] == "to_size":
            chunk_size = int(split_params["split_size"])
        else:
            raise Exception("Unsupported split mode %s" % split_params["split_mode"])

        def _read_cm_records(filename):
            lines = []
            with open(filename) as handle:
                for line in handle:
                    if line.startswith("INFERNAL1/a") and lines:
                        yield lines
                        lines = [line]
                    else:
                        lines.append(line)
            yield lines

        def _write_part_cm_file(accumulated_lines):
            part_dir = subdir_generator_function()
            part_path = os.path.join(part_dir, os.path.basename(input_files[0]))
            part_file = open(part_path, "w")
            part_file.writelines(accumulated_lines)
            part_file.close()

        try:
            cm_records = _read_cm_records(input_files[0])
            cm_lines_accumulated = []
            for counter, cm_record in enumerate(cm_records, start=1):
                cm_lines_accumulated.extend(cm_record)
                if counter % chunk_size == 0:
                    _write_part_cm_file(cm_lines_accumulated)
                    cm_lines_accumulated = []
            if cm_lines_accumulated:
                _write_part_cm_file(cm_lines_accumulated)
        except Exception as e:
            logging.error("Unable to split files: %s" % str(e))
            raise

    split = classmethod(split)

if __name__ == "__main__":
    Infernal_CM_1_1()
    # Stockholm_1_0()  # ???


#Code for datetime
# Python code to convert string to list
TimeDelta = []

#timeStart
def Convert(string):
    li = list(string.split("_"))
    return li

# Driver code
TStart = sheet_obj.cell(row=1, column=2)
t1 = TStart.value
#print(t1)

#print(Convert(t1[0:4]))
#print(t1)
#print("Total years in hrs:")
T1Yearsinhrs =365*24*int(t1[0:4])
#print(T1Yearsinhrs)
#print("Total months in hrs")
T1months = int(t1[5:7])
#print(T1months)
if  (T1months == 1) or (T1months == 3) or (T1months == 5) or (T1months == 7) or (T1months == 8) or  (T1months == 10)   or (T1months == 12):
    T1monthsinHrs = T1months*31*24
elif (T1months == 4) or (T1months == 6) or  (T1months == 9) or (T1months == 11):
    T1monthsinHrs = T1months * 30 * 24
elif (T1months == 2):
    T1monthsinHrs = T1months * 29 * 24
else:
    print("The month range is out of range")
#print(T1monthsinHrs)

T1Days = int(t1[8:10])
#print(T1Days)
T1DayinHrs = T1Days*24
#print(T1DayinHrs)
T1hrs = int(t1[11:13])
#print(T1hrs)
T1min = int(t1[14:16])
T1minInHrs = T1min/60

#print(T1minInHrs)
TotalStartTimeinHrs = T1Yearsinhrs+T1monthsinHrs+T1DayinHrs+T1hrs+T1minInHrs
#print(TotalStartTimeinHrs)

#TimeEnd
for i in range(0,count-1):
    TEndA = sheet_obj.cell(row=6+i, column=3)
    t2A = TEndA.value
    # print(t2)

    # print(Convert(t2[0:4]))
    # print(t2)
    # print("Total years in hrs:")
    T2YearsinhrsA = 365 * 24 * int(t2A[0:4])
    # print(T2Yearsinhrs)
    # print("Total months in hrs")
    T2monthsA = int(t2A[5:7])
    # print(T2months)
    if (T2monthsA == 1) or (T2monthsA == 3) or (T2monthsA == 5) or (T2monthsA == 7) or (T2monthsA == 8) or (
            T2monthsA == 10) or (T2monthsA == 12):
        T2monthsinHrsA = T2monthsA * 31 * 24
    elif (T2monthsA == 4) or (T2monthsA == 6) or (T2monthsA == 9) or (T2monthsA == 11):
        T2monthsinHrsA = T2monthsA * 30 * 24
    elif (T2monthsA == 2):
        T2monthsinHrsA = T2monthsA * 29 * 24
    else:
        print("The month range is out of range")
    # print(T2monthsinHrs)

    T2DaysA = int(t1[8:10])
    # print(T2Days)
    T2DayinHrsA = T2DaysA* 24
    # print("Total days in hrs:")
    # print(T2DayinHrs)
    T2hrsA = int(t2A[11:13])
    # print("Total hrs in hrs:")
    # print(T2hrs)
    T2minA = float(t2A[14:16])
    # print(T2min)
    T2minInHrsA = float(T2minA / 60)
    # print("Total mins in hrs:")
    # print(T2minInHrs)
    TotalEndTimeinHrsA = T2YearsinhrsA + T2monthsinHrsA + T2DayinHrsA + T2hrsA + T2minInHrsA
    # print(TotalEndTimeinHrs)
    TimeDifA = TotalEndTimeinHrsA - TotalStartTimeinHrs
    TimeDelta.append(TimeDifA)

   #saving The timediff
SplitTimeDelta =  [TimeDelta[i:i + 3] for i in range(0, len(TimeDelta), 3)]
k=0
for row in SplitTimeDelta:

    for i in range(0, 3):
        u20 = sheet_obj.cell(row=6 + k, column=20 + 3 * i)
        u20.value = row[i]
        print(u20.value
          )
    k = k + 1

print(TimeDelta)
wb_obj.save("OD.xlsx")
plt.plot(TimeDelta,avg,'.',)
plt.show()


from config import config
from processed_images.processed_images import LockingProcessedImages, ProcessedImage
from hdr_finder_app import HDRProcessedImages

from graphene import ObjectType, String, Schema, DateTime, List, Int, Field, Float, JSONString

from flask import Flask
from flask_graphql import GraphQLView
from flask_cors import CORS

import logging
import sys

photos = LockingProcessedImages(db_dir=config['photo_database']['database_dir'])
photos.load()

class Photo(ObjectType):
    filetype = String()
    filename = String(required=True)
    thumbnail = String()
    datetaken = DateTime()
    latitude = Float()
    longitude = Float()
    exifdata = JSONString()

class HDRGroup(ObjectType):
  group = List(String)

class Query(ObjectType):
    photolist = List(String, startdatetime=DateTime(required=True), enddatetime=DateTime(required=True))
    photo = Field(Photo, filename=String(required=True))
    hdrgroups = List(HDRGroup)

    @staticmethod
    def resolve_photolist(root, info, startdatetime, enddatetime):
        return photos.get_file_list_date_range(startdatetime, enddatetime)

    @staticmethod
    def resolve_photo(root, info, filename):
        p = photos.retrieve(filename)

        return Photo(filetype=p.filetype,
                     filename=p.filename,
                     thumbnail=p.thumbnail,
                     datetaken=p.date_taken,
                     latitude=p.latitude,
                     longitude=p.longitude,
                     exifdata=p.exif_data)

    @staticmethod
    def resolve_hdrgroups(root, info):
        return [HDRGroup(group=[
            '/work/stash/Photos/M3/2017/03/04/CR2/IMG_4719.CR2',
            '/work/stash/Photos/M3/2017/03/04/CR2/IMG_4720.CR2',
            '/work/stash/Photos/M3/2017/03/04/CR2/IMG_4721.CR2'
        ])]

# GraphQL schema created:
schema = Schema(query=Query)

# Flask init
app = Flask(__name__)
CORS(app)

app.add_url_rule('/graphql', view_func=GraphQLView.as_view(
    'graphql',
    schema=schema,
    graphiql=True
))

# Serve this out via flask to be picked up in React:
@app.route('/queries')
def queries():
  return """
query getPhotolist {
  photolist(startdatetime: "2020-03-01T00:00:00", enddatetime: "2020-03-02T00:00:00")
}

query getPhoto($filename: String!) {
  photo(filename: $filename) {
    filetype
    filename
    datetaken
    latitude
    longitude
    thumbnail
    exifdata
  }
}

query gethdrgroups {
  hdrgroups {
    group
  }
}
"""

if __name__ == '__main__':
    app.run(host='0.0.0.0')


import torch
from tokenizers import SentencePieceBPETokenizer
from tokenizers.processors import RobertaProcessing
from transformers import RobertaModel, AutoModel
import numpy as np

def load_embeddings(model_dir = "roberta_base_transformers"):
    """
    Function that loads a pre-trained polish BErt model used for embedding extraction
    :param model_dir: full path to the directory with model files
    :return: model, tokenizer
    """

    tokenizer = SentencePieceBPETokenizer(f"{model_dir}/vocab.json", f"{model_dir}/merges.txt")
    getattr(tokenizer, "_tokenizer").post_processor = RobertaProcessing(sep=("</s>", 2), cls=("<s>", 0))
    model: RobertaModel = AutoModel.from_pretrained(model_dir)
    return model, tokenizer

def feed_batch(x, batch_size):
    """
    Function that feeds batches of embeddings
    :param x: all indicies
    :param batch_size:
    :return: depends on the current index
    """
    i, batch = 0, 0
    for batch, i in enumerate(range(0, len(x) - batch_size, batch_size), 1):
        x_batch = x[i : i + batch_size]
        yield x_batch, batch
    if i + batch_size < len(x):
        yield x[i + batch_size :], batch + 1
    if batch == 0:
        yield x, 1

def extract_in_batches(indicies, embeddings, batch_size=30):
    """
    Function that extracts embeddings in batches due to OOM issues
    :param indicies: all indicies
    :param embeddings: embeddings
    :param batch_size:
    :return: new embeddings
    """
    new = torch.zeros((*indicies.shape, 768))
    previous = 0
    with torch.no_grad():
        for batch, num in feed_batch(indicies, batch_size):
            new[previous*batch_size:num*batch_size] = embeddings(batch)[0]
            previous = num
    return new

def tokenize_text(df, max_seq, tokenizer):
    return [
        tokenizer.encode(text, add_special_tokens=True).ids[:max_seq] for text in df.values]
def pad_text(tokenized_text, max_seq):
    return np.array([el + [0] * (max_seq - len(el)) for el in tokenized_text])
def tokenize_and_pad_text(df, max_seq, tokenizer):
    tokenized_text = tokenize_text(df, max_seq, tokenizer)
    padded_text = pad_text(tokenized_text, max_seq)
    return torch.tensor(padded_text)
def targets_to_tensor(df, target_columns):
    return torch.tensor(df[target_columns].values, dtype=torch.float32)


from typing import List

class Solution:
    def rob(self, nums: List[int]) -> int:
        prev_prev = prev = cur = 0
        for i in nums:
            prev_prev, prev, cur = prev, cur, max(i + prev, i + prev_prev)
        return max(prev, cur)

if __name__ == '__main__':
    s = Solution()
    assert s.rob([1, 2, 3, 1]) == 4
    assert s.rob([2, 7, 9, 3, 1]) == 12
    assert s.rob([10, 13, 7, 1, 9, 1, 1, 3, 1]) == 29
    assert s.rob([10, 18, 7, 1, 9, 1, 1, 3, 1]) == 30


n,k=map(int,input().split())

s=0

count=0

while 1:
    print(n,k)
    k+=k
    if(k>=n):
        k=k%n
    count+=1
    if n%k!=0:
        break
print(count)

第九届蓝桥杯大赛决赛/格雷码.py


import numpy as np
import pandas as pd

from matplotlib import pyplot as plt
#from matplotlib.collections import LineCollection

from sklearn import manifold
from sklearn.decomposition import PCA

dist_data_file = \
  "C:\\DePaulCoursework\\Research\\BryanExample\\2018715_21_44_53_Bicluster_Distance_Matrix.csv"
#dist_data_file = \
#  "C:\\DePaulCoursework\\Research\\BryanExample\\2018715_21_44_53_Bicluster_Adjacency_Low75.csv"
dist_df = pd.read_csv(dist_data_file, delimiter=",", index_col=0, header=0)
dist_np = np.array(dist_df)

#dist_np = dist_np.astype("float")
#ndx = dist_np == 0
#dist_np[ndx] = 0.000001
#dist_np = 1/dist_np

gene_list = list(dist_df.index)

seed = 20
mds = manifold.MDS(n_components=2, max_iter=3000, eps=1e-9, random_state=seed,
                   dissimilarity="precomputed", n_jobs=1)
pos = mds.fit(dist_np).embedding_

nmds = manifold.MDS(n_components=2, metric=False, max_iter=3000, eps=1e-12,
                    dissimilarity="precomputed", random_state=seed, n_jobs=1,
                    n_init=1)
npos = nmds.fit_transform(dist_np, init=pos)

# Rotate the data
clf = PCA(n_components=2)
pos = clf.fit_transform(pos)
npos = clf.fit_transform(npos)

fig = plt.figure(1)
ax = plt.axes([0., 0., 1., 1.])

s = 100
pos_x_data = pos[:, 0]
pos_y_data = pos[:, 1]
plt.scatter(pos_x_data, pos_y_data, color='turquoise', s=s, lw=0, label='MDS')
plt.legend(scatterpoints=1, loc='best', shadow=False)

for i, txt in enumerate(gene_list):
    ax.annotate(txt, (pos_x_data[i], pos_y_data[i]))

plt.show()

fig = plt.figure(1)
ax = plt.axes([0., 0., 1., 1.])

npos_x_data = npos[:, 0]
npos_y_data = npos[:, 1]
plt.scatter(npos_x_data, npos_y_data, color='darkorange', s=s, lw=0, label='NMDS')
plt.legend(scatterpoints=1, loc='best', shadow=False)

for i, txt in enumerate(gene_list):
    ax.annotate(txt, (npos_x_data[i], npos_y_data[i]))

plt.show()

#similarities = dist_np.max() / dist_np * 100
#similarities[np.isinf(similarities)] = 0

# Plot the edges
#start_idx, end_idx = np.where(pos)
# a sequence of (*line0*, *line1*, *line2*), where::
#            linen = (x0, y0), (x1, y1), ... (xm, ym)
#segments = [[X_true[i, :], X_true[j, :]]
#            for i in range(len(pos)) for j in range(len(pos))]
#values = np.abs(dist_np)
#lc = LineCollection(segments,
#                    zorder=0, cmap=plt.cm.Blues,
#                    norm=plt.Normalize(0, values.max()))
#lc.set_array(dist_np.flatten())
#lc.set_linewidths(np.full(len(segments), 0.5))
#ax.add_collection(lc)


MSG = ""
def print_wipe(msg1, msg0=False):
    backspace(len(MSG))
    global MSG
    if (msg0 == True):
        MSG=""
    else:
        MSG = msg1
    print(msg1, end="")
    return

def backspace(n):
    print('\r' * n, end="")
    print(" " * len(MSG), end="")
    print('\r' * n, end="")


#Create a class called Node.This will be used to create nodes to insert into the lists.

# The class takes in one value, the value that each node will contain. By default, the next property of every node will be none(null in JS).
class Node:
    def __init__(self,value):
        self.value = value
        self.next = None

# Create a class called SList. This will be used to create lists. Lists will contain nodes.

class SList:
    def __init__(self, value):
        node = Node(value)
        self.head = node

    def addNote(self, value):
        node = Node(value)
        runner = self.head
        while(runner.next != None):
            runner = runner.next
        runner.next = node

    def printAllValues(self, msg=""):
        runner = self.head
        print("Printing the values in the list ---", msg,"---")
        while(runner.next != None):
            print(runner.value)
            runner = runner.next
        print(runner.value)

print("==================START OF PROGRAM==============")
list = SList(5)
list.addNote(7)
list.addNote(9)
list.addNote(1)

list.printAllValues("Attempt 1")


# @Author : bamtercelboo
# @Datetime : 2018/1/30 15:58
# @File : DataConll2003_Loader.py
# @Last Modify Time : 2018/1/30 15:58
# @Contact : bamtercelboo@{gmail.com, 163.com}

"""
    FILE :
    FUNCTION :
"""
import sys
import os
import re
import random
import torch
import json
from Dataloader.Instance import Instance

from DataUtils.Common import *
torch.manual_seed(seed_num)
random.seed(seed_num)

class DataLoaderHelp(object):
    """
    DataLoaderHelp
    """

    @staticmethod
    def _clean_str(string):
        """
        Tokenization/string cleaning for all datasets except for SST.
        Original taken from https://github.com/yoonkim/CNN_sentence/blob/master/process_data.py
        """
        string = re.sub(r"[^A-Za-z0-9(),!?\'\`]", " ", string)
        string = re.sub(r"\'s", " \'s", string)
        string = re.sub(r"\'ve", " \'ve", string)
        string = re.sub(r"n\'t", " n\'t", string)
        string = re.sub(r"\'re", " \'re", string)
        string = re.sub(r"\'d", " \'d", string)
        string = re.sub(r"\'ll", " \'ll", string)
        string = re.sub(r",", " , ", string)
        string = re.sub(r"!", " ! ", string)
        string = re.sub(r"\(", " \( ", string)
        string = re.sub(r"\)", " \) ", string)
        string = re.sub(r"\?", " \? ", string)
        string = re.sub(r"\s{2,}", " ", string)
        return string.strip().lower()

    @staticmethod
    def _normalize_word(word):
        """
        :param word:
        :return:
        """
        new_word = ""
        for char in word:
            if char.isdigit():
                new_word += '0'
            else:
                new_word += char
        return new_word

    @staticmethod
    def _sort(insts):
        """
        :param insts:
        :return:
        """
        sorted_insts = []
        sorted_dict = {}
        for id_inst, inst in enumerate(insts):
            sorted_dict[id_inst] = inst.words_size
        dict = sorted(sorted_dict.items(), key=lambda d: d[1], reverse=True)
        for key, value in dict:
            sorted_insts.append(insts[key])
        print("Sort Finished.")
        return sorted_insts

class DataLoader(DataLoaderHelp):
    """
    DataLoader
    """
    def __init__(self, path, shuffle, config):
        """
        :param path: data path list
        :param shuffle:  shuffle bool
        :param config:  config
        """
        #
        print("Loading Data......")
        self.data_list = []
        self.max_count = config.max_count
        self.path = path
        self.shuffle = shuffle

        # BERT
        self.bert_path = [config.bert_train_file,
                          config.bert_dev_file,
                          config.bert_test_file]

        self.use_bert = config.use_bert

    def dataLoader(self):
        """
        :return:
        """
        path = self.path
        shuffle = self.shuffle
        assert isinstance(path, list), "Path Must Be In List"
        print("Data Path {}".format(path))
        for id_data in range(len(path)):
            print("Loading Data Form {}".format(path[id_data]))
            insts = self._Load_Each_Data(path=path[id_data], path_id=id_data)
            print("shuffle train data......")
            random.shuffle(insts)
            self.data_list.append(insts)
        # return train/dev/test data
        if len(self.data_list) == 3:
            return self.data_list[0], self.data_list[1], self.data_list[2]
        elif len(self.data_list) == 2:
            return self.data_list[0], self.data_list[1]

    def _Load_Each_Data(self, path=None, path_id=None):
        """
        :param path:
        :param shuffle:
        :return:
        """
        assert path is not None, "The Data Path Is Not Allow Empty."
        insts = []
        now_lines = 0
        with open(path, encoding="UTF-8") as f:
            inst = Instance()
            for line in f.readlines():
                line = line.strip()
                now_lines += 1
                if now_lines % 200 == 0:
                    sys.stdout.write("\rreading the {} line\t".format(now_lines))
                if line == "\n":
                    print("empty line")

                inst = Instance()
                line = line.split()
                label = line[0]
                word = " ".join(line[1:])
                if label not in ["0", "1"]:
                    print("Error line: ", " ".join(line))
                    continue
                inst.words = self._clean_str(word).split()
                inst.labels.append(label)
                inst.words_size = len(inst.words)
                insts.append(inst)

                if len(insts) == self.max_count:
                    break
            # print("\n")
        if self.use_bert:
            insts = self._read_bert_file(insts, path=self.bert_path[path_id])
        return insts

    def _read_bert_file(self, insts, path):
        """
        :param insts:
        :param path:
        :return:
        """
        print("\nRead BERT Features File From {}".format(path))
        now_lines = 0
        with open(path, encoding="utf-8") as f:
            for inst, bert_line in zip(insts, f.readlines()):
                now_lines += 1
                if now_lines % 2000 == 0:
                    sys.stdout.write("\rreading the {} line\t".format(now_lines))
                bert_fea = json.loads(bert_line)
                inst.bert_tokens = bert_fea["features"]["tokens"]
                inst.bert_feature = bert_fea["features"]["values"]
                # print(inst.bert_feature)
            sys.stdout.write("\rReading the {} line\t".format(now_lines))
        return insts


import json

import pendulum

from gtfs_realtime_translators.factories import TripUpdate, FeedMessage

class CtaBusGtfsRealtimeTranslator:
    TIMEZONE = 'America/Chicago'

    def __call__(self, data):
        json_data = json.loads(data)
        predictions = json_data['bustime-response']['prd']
        entities = [self.__make_trip_update(idx, arr) for idx, arr in enumerate(predictions)]

        return FeedMessage.create(entities=entities)

    @classmethod
    def __to_unix_time(cls, time):
        return pendulum.parse(time).in_tz(cls.TIMEZONE).int_timestamp

    @classmethod
    def __make_trip_update(cls, _id, prediction):
        entity_id = str(_id + 1)
        route_id = prediction['rt']
        stop_id = prediction['stpid']

        # Per the docs, the prediction is either for a scheduled departure, or realtime arrival
        departure_type = 'D'
        is_scheduled = prediction['typ'] == departure_type

        parsed_arrival_time = cls.__to_unix_time(prediction['prdtm'])
        arrival_time = None if is_scheduled else parsed_arrival_time

        ##### Intersection Extensions
        headsign = prediction['des']
        scheduled_arrival_time = parsed_arrival_time if is_scheduled else None

        return TripUpdate.create(entity_id=entity_id,
                                route_id=route_id,
                                stop_id=stop_id,
                                arrival_time=arrival_time,
                                headsign=headsign,
                                scheduled_arrival_time=scheduled_arrival_time)


import asyncio
from mavsdk import System

absolute_altitude = 10
async def run():
    drone = System(port=14030)
    await drone.connect(system_address="udp://:14030")

    print("Waiting for drone to connect...")
    async for state in drone.core.connection_state():
        if state.is_connected:
            print(f"-- Connected to drone!")
            break

    print("-- Arming")
    await drone.action.arm()
    await asyncio.sleep(3)
    print("-- Taking off")
    await drone.action.takeoff()

    await asyncio.sleep(3)
    # To fly drone 20m above the ground plane
    flying_alt = absolute_altitude + 20.0
    # goto_location() takes Absolute MSL altitude
    await drone.action.goto_location(47.397606, 8.543060, flying_alt, 0)

    while True:
        print("Staying connected, press Ctrl-C to exit")
        await asyncio.sleep(1)

if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.run_until_complete(run())


# Description: Fetches METAR data from Aviation Weather Center API and posts it to PocketBase API

# Import libraries
import requests
import csv
import json
from datetime import datetime

# Define constants
API_URL = "https://www.aviationweather.gov/adds/dataserver_current/httpparam" # Aviation Weather Center API URL
POCKETBASE_API_URL = "https://custom.domain/api/collections/collection_name/records" # PocketBase API URL

# Define variables
data_type = "metars"
airport_code = "XXXX" # Airport code
hours_before_now = "24" # Number of hours before now to fetch data for
output_format = "csv"

# Define function to get current time
def execution_time():
    return datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")

# Define function to fetch CSV data
def fetch_csv_data(api_url, parameters):
    print(execution_time() + " - Making request to Aviation Weather Center...")
    # Make the API request
    response = requests.get(api_url, params=parameters)
    # Check for successful request
    if response.status_code == 200:
        # Decode the CSV content
        content = response.content.decode("utf-8")
        # Parse the CSV data and return it
        csv_data = list(csv.reader(content.splitlines(), delimiter=","))
        return csv_data
    else:
        print(execution_time() + " - Request to Aviation Weather Center failed! with status code: " + str(response.status_code))

# Set up the API request parameters
parameters = {
    "dataSource": data_type,
    "requestType": "retrieve",
    "format": output_format,
    "stationString": airport_code,
    "hoursBeforeNow": hours_before_now,
}

# Print execution time
print(execution_time() + " - Executing script...")

# Call the function to fetch the CSV data
csv_data = fetch_csv_data(API_URL, parameters)

# Check if there are results in the CSV data
if len(csv_data) <= 6:
    print(execution_time() + " - No results found in CSV data received from Aviation Weather Center")
else:
    print(execution_time() + " - CSV data received from Aviation Weather Center successfully!")
    # Parse the remaining rows of the CSV data and convert to JSON
    print(execution_time() + " - Parsing CSV data and converting it to JSON...")
    for row in csv_data[6:]: # Skip the first 6 rows
        # Get the headers from the first row of the CSV data
        headers = csv_data[5]
        # Create a dictionary with the desired keys and values
        data = {
            "raw_text": row[headers.index("raw_text")],
            "station_id": airport_code,
            "observation_time": datetime.strptime(row[headers.index("observation_time")], "%Y-%m-%dT%H:%M:%SZ").strftime("%Y-%m-%d %H:%M:%S.000Z"),
            "temp_c": float(row[headers.index("temp_c")]),
            "dewpoint_c": float(row[headers.index("dewpoint_c")]) if row[headers.index("dewpoint_c")] else 99, # Set to 99 if dewpoint_c is empty
            "wind_dir_degrees": int(row[headers.index("wind_dir_degrees")]) if row[headers.index("wind_dir_degrees")] else 0,
            "wind_speed_kt": int(row[headers.index("wind_speed_kt")]) if row[headers.index("wind_speed_kt")] else 0,
            "altim_in_hg": float(row[headers.index("altim_in_hg")]),
            "corrected": bool(row[headers.index("corrected")]),
            "precip_in": float(row[headers.index("precip_in")]) if row[headers.index("precip_in")] else 0,
            "metar_type": row[headers.index("metar_type")],
        }

        # Set up the headers for the POST request
        headers = {'Content-Type': 'application/json'}

        # Make the POST request to PocketBase API
        print(execution_time() + " - Sending data to PocketBase with date and time: " + data["observation_time"])
        response = requests.post(POCKETBASE_API_URL, data=json.dumps(data), headers=headers)

        # Check for successful request
        if response.status_code == 200:
            print(execution_time() + " - Data sent to PocketBase successfully!")
        else:
            print(execution_time() + " - Request to PocketBase failed! with status code: " + str(response.status_code))


import MapReduce
import sys

"""
Inverted Index in Simple Python MapReduce Framework.
Functions to create an index of words and texts
they appear in.
"""

mr = MapReduce.MapReduce()

# =============================
# Do not modify above this line

def mapper(record):
    """
    Mapper should produce a set of tuples,
    each of which has the word as a key
    and text name as a value
    """
    # key: document identifier
    # value: document contents
    docid = record[0]
    value = record[1]
    words = value.split()
    for w in words:
      mr.emit_intermediate(w, docid)

def reducer(key, list_of_values):
    """
    Reducer should take in a bunch of tuples with
    words as keys and combine them so that each output
    tuple has a word as a key and a list of texts
    as value.
    """
    # key: word
    # value: docid containing word
    docs = set()
    for v in list_of_values:
      docs.add(v)
    docs_lst = list(docs)
    mr.emit((key, docs_lst))

# Do not modify below this line
# =============================
if __name__ == '__main__':
  inputdata = open(sys.argv[1])
  mr.execute(inputdata, mapper, reducer)


#!/usr/bin/env python
"""
Converts an image to fit a style given by a texture.

Usage:
  convert.py convert <texture> <source> <destination>
  convert.py build <source> <destination>
"""

from __future__ import print_function

import numpy as np
import scipy.ndimage as nd
import sys
import skimage.io
from PIL import Image
import os
import sys
import glob
import warnings
warnings.filterwarnings("ignore", category=UserWarning)

# insert pycaffe into pythonpath
caffe_path = os.path.abspath(os.getenv('CAFFE'))
pycaffe_path = caffe_path + '/python'
if pycaffe_path not in sys.path:
  sys.path.insert(0, pycaffe_path)
# suppress wall of text logging
os.environ['GLOG_minloglevel'] = '2'

import caffe
from caffe.proto import caffe_pb2

DEPLOY_PATH = './models/vgg/deploy.prototxt'
CAFFEMODEL_PATH = './snapshots/VGG_ILSVRC_19_layers.caffemodel'
DEFAULT_CONTENT_LAYERS = ['conv4_2']
DEFAULT_STYLE_LAYERS = ['conv1_1', 'conv2_1', 'conv3_1', 'conv4_1', 'conv5_1']

def build_net(deploy_path, caffemodel_path, mode=caffe.TRAIN):
  caffe.set_mode_cpu()

  return caffe.Classifier(deploy_path, caffemodel_path, mode,
      mean=np.array([103.939, 116.779, 123.680]), channel_swap=(2, 1, 0))

def preprocess(net, im):
  return np.float32(np.rollaxis(im, 2)[::-1]) - net.transformer.mean['data']

def deprocess(net, im):
  return np.dstack((im + net.transformer.mean['data'])[::-1])

def layer_texture(net, layer):
  # activations already exist in the net
  activations = net.blobs[layer].data[0]
  n_filters = activations.shape[0]
  activations = activations.reshape((n_filters, -1))
  return activations.dot(activations.T) #/ activations.shape[1]

def build_texture(net, im_files, layers=None):
  """
  Texture is G, where G[l][i, j] is the dot product of the activations of filters i and j of layer l.
  Each dot product is normalized by the dimensions of the activations.

  If multiple images are given, the textures for each image are averaged together.
  """
  layers = layers or DEFAULT_STYLE_LAYERS
  G = {}

  for layer in layers:
    n_filters = net.blobs[layer].data.shape[1]
    G[layer] = np.zeros((len(im_files), n_filters, n_filters))

  for n, im_file in enumerate(im_files):
    # feed forward
    im = np.float32(Image.open(im_file))
    im = preprocess(net, im)

    if im.shape[1] * im.shape[2] > 720 * 442:
      # XXX temporary resize
      print("Warning: image may be too big and cause caffe to segfault, cropping")
      im = im[:, :720, :442]
    blob = net.blobs['data']
    # resize net input
    blob.reshape(1, *im.shape)
    blob.data[0] = im
    net.forward(end=layers[-1])

    # compute dot products
    for layer in layers:
      activations = net.blobs[layer].data[0]
      n_filters = activations.shape[0]
      activations = activations.reshape((n_filters, -1))
      G[layer][n, :, :] = activations.dot(activations.T) #/ activations.shape[1]

  # average imagewise textures
  for layer in G:
    G[layer] = np.average(G[layer], axis=0)

  return G

def adadelta(data, diff, param):
  p = param.get('p', 0.95) # decay constant
  e = param.get('e', 1e-2) # epsilon

  if not 'g_t' in param:
    # initialize accumulation variables
    param['g_t'] = np.zeros_like(data)
    param['dx_t'] = np.zeros_like(data)

  g_t = param['g_t']
  dx_t = param['dx_t']

  # accumulate gradient
  g_t[:] = p * g_t + (1.0 - p) * diff * diff

  # compute update
  rms_g = np.sqrt(g_t + e)
  rms_dx = np.sqrt(dx_t + e)
  dx = -rms_dx / rms_g * diff

  # accumulate update
  dx_t[:] = p * dx_t + (1.0 - p) * dx * dx

  # apply update
  data += dx
  return dx

def irpropm(data, diff, param):
  n_p = param.get('n_p', 1.2) # increase factor
  n_m = param.get('n_m', 0.5) # decrease factor
  step_max = param.get('step_max', 5.0)
  step_min = param.get('step_min', 1e-6)

  if not 'g_t' in param:
    param['g_t'] = np.zeros_like(data)
    param['dx_t'] = np.ones_like(data) / 2.0

  g_t = param['g_t']   # prev gradient
  dx_t = param['dx_t'] # prev step

  d_p = (diff * g_t) >= 0
  d_m = (diff * g_t) < 0

  dx_t[:] = np.maximum(dx_t, step_min)
  d = (d_p * np.minimum(dx_t * n_p, step_max)) + (d_m * np.maximum(dx_t * n_m, step_min))
  diff[d_m] = 0.0

  step = -np.sign(diff) * d
  data += step

  g_t[:] = diff
  dx_t[:] = d
  return step

def train_content_layer(net, layer, reference_data, alpha, param):
  net.forward(end=layer)

  activation = net.blobs[layer].data[0]
  ref_activation = reference_data[layer]
  net.blobs[layer].diff[0] = alpha * (activation - ref_activation) * (activation > 0)

  net.backward(start=layer)

  blob = net.blobs['data']
  irpropm(blob.data[0], blob.diff[0], param)

def train_style_layer(net, layer, texture, beta, layer_weight, param):
  net.forward(end=layer)

  G_l = layer_texture(net, layer)

  activation = net.blobs[layer].data[0]
  n_filters = activation.shape[0]
  n_size = activation.shape[1] * activation.shape[2]
  net.blobs[layer].diff[0] = beta * layer_weight / ((n_filters * n_size) ** 2) * \
    (G_l - texture[layer]).T.dot(activation.transpose(1, 0, 2)) * (activation > 0)

  net.backward(start=layer)

  blob = net.blobs['data']
  irpropm(blob.data[0], blob.diff[0], param)

def convert(net, im, texture, n_iter=250, content_layers=None, style_layers=None, alpha=1.0, beta=1000.0):
  content_layers = content_layers or DEFAULT_CONTENT_LAYERS
  style_layers = style_layers or DEFAULT_STYLE_LAYERS
  layer_weight = 1.0 / len(style_layers)
  im = preprocess(net, im)

  # compute reference activations
  reference_data = {}
  blob = net.blobs['data']
  blob.reshape(1, *im.shape)
  blob.data[0] = im
  for layer in content_layers:
    net.forward(end=layer)
    reference_data[layer] = np.copy(net.blobs[layer].data[0])

  param = {}

  for i in range(n_iter):
    # backprop onto reference image according to loss from http://arxiv.org/abs/1508.06576
    for layer in content_layers:
      train_content_layer(net, layer, reference_data, alpha, param)
    for layer in style_layers:
      train_style_layer(net, layer, texture, beta, layer_weight, param)

    print(i, end=" ")
    sys.stdout.flush()

  im = blob.data[0]
  return deprocess(net, im)

def convert_cmd(target, source, destination):
  texture = np.load(target)
  im = np.float32(Image.open(source))
  net = build_net(DEPLOY_PATH, CAFFEMODEL_PATH)
  im_new = convert(net, im, texture)

  im_new = np.uint8(np.clip(im_new, 0, 255))
  skimage.io.imsave(destination, im_new)

def build_cmd(source, destination):
  if os.path.isfile(source):
    im_files = [source]
  elif os.path.isdir(source):
    im_files = glob.iglob("{}/*.*".format(source))
  else:
    raise ValueError("Source does not exist.")

  net = build_net(DEPLOY_PATH, CAFFEMODEL_PATH)
  G = build_texture(net, im_files)
  with open(destination, 'wb') as f:
    np.savez(f, **G)

if __name__ == '__main__':
  if len(sys.argv) == 5 and sys.argv[1] == 'convert':
    convert_cmd(sys.argv[2], sys.argv[3], sys.argv[4])

  elif len(sys.argv) == 4 and sys.argv[1] == 'build':
    build_cmd(sys.argv[2], sys.argv[3])

  else:
    print(__doc__)


import warnings

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import pytest
import scipy.signal

import neurokit2 as nk

# =============================================================================
# Signal
# =============================================================================

def test_signal_simulate():
    # Warning for nyquist criterion
    with pytest.warns(
        nk.misc.NeuroKitWarning, match=r"Skipping requested frequency.*cannot be resolved.*"
    ):
        nk.signal_simulate(sampling_rate=100, frequency=11, silent=False)

    # Warning for period duration
    with pytest.warns(
        nk.misc.NeuroKitWarning, match=r"Skipping requested frequency.*since its period of.*"
    ):
        nk.signal_simulate(duration=1, frequency=0.1, silent=False)

def test_signal_smooth():

    # TODO: test kernels other than "boxcar"
    signal = np.cos(np.linspace(start=0, stop=20, num=1000))
    smooth1 = nk.signal_smooth(signal, kernel="boxcar", size=100)
    smooth2 = nk.signal_smooth(signal, kernel="boxcar", size=500)
    # assert that the signal's amplitude is attenuated more with wider kernels
    assert np.allclose(np.std(smooth1), 0.6044, atol=0.00001)
    assert np.allclose(np.std(smooth2), 0.1771, atol=0.0001)

def test_signal_smooth_boxcar():
    signal = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], dtype=float)
    np.testing.assert_array_almost_equal(
        nk.signal_smooth(signal, kernel="boxcar", size=3),
        [(1 + 1 + 2) / 3, 2, 3, 4, 5, 6, 7, 8, 9, (9 + 10 + 10) / 3],
    )

def test_signal_binarize():

    signal = np.cos(np.linspace(start=0, stop=20, num=1000))
    binary = nk.signal_binarize(signal)
    assert len(binary) == 1000

    binary = nk.signal_binarize(list(signal))
    assert len(binary) == 1000

def test_signal_resample():

    signal = np.cos(np.linspace(start=0, stop=20, num=50))

    downsampled_interpolation = nk.signal_resample(
        signal, method="interpolation", sampling_rate=1000, desired_sampling_rate=500
    )
    downsampled_numpy = nk.signal_resample(
        signal, method="numpy", sampling_rate=1000, desired_sampling_rate=500
    )
    downsampled_pandas = nk.signal_resample(
        signal, method="pandas", sampling_rate=1000, desired_sampling_rate=500
    )
    downsampled_fft = nk.signal_resample(
        signal, method="FFT", sampling_rate=1000, desired_sampling_rate=500
    )
    downsampled_poly = nk.signal_resample(
        signal, method="poly", sampling_rate=1000, desired_sampling_rate=500
    )

    # Upsample
    upsampled_interpolation = nk.signal_resample(
        downsampled_interpolation,
        method="interpolation",
        sampling_rate=500,
        desired_sampling_rate=1000,
    )
    upsampled_numpy = nk.signal_resample(
        downsampled_numpy, method="numpy", sampling_rate=500, desired_sampling_rate=1000
    )
    upsampled_pandas = nk.signal_resample(
        downsampled_pandas, method="pandas", sampling_rate=500, desired_sampling_rate=1000
    )
    upsampled_fft = nk.signal_resample(
        downsampled_fft, method="FFT", sampling_rate=500, desired_sampling_rate=1000
    )
    upsampled_poly = nk.signal_resample(
        downsampled_poly, method="poly", sampling_rate=500, desired_sampling_rate=1000
    )

    # Check
    rez = pd.DataFrame(
        {
            "Interpolation": upsampled_interpolation - signal,
            "Numpy": upsampled_numpy - signal,
            "Pandas": upsampled_pandas - signal,
            "FFT": upsampled_fft - signal,
            "Poly": upsampled_poly - signal,
        }
    )
    assert np.allclose(np.mean(rez.mean()), 0.0001, atol=0.0001)

def test_signal_detrend():

    signal = np.cos(np.linspace(start=0, stop=10, num=1000))  # Low freq
    signal += np.cos(np.linspace(start=0, stop=100, num=1000))  # High freq
    signal += 3  # Add baseline

    rez_nk = nk.signal_detrend(signal, order=1)
    rez_scipy = scipy.signal.detrend(signal, type="linear")
    assert np.allclose(np.mean(rez_nk - rez_scipy), 0, atol=0.000001)

    rez_nk = nk.signal_detrend(signal, order=0)
    rez_scipy = scipy.signal.detrend(signal, type="constant")
    assert np.allclose(np.mean(rez_nk - rez_scipy), 0, atol=0.000001)

    # Tarvainen
    rez_nk = nk.signal_detrend(signal, method="tarvainen2002", regularization=500)
    assert np.allclose(np.mean(rez_nk - signal), -2.88438737697, atol=0.000001)

def test_signal_filter():

    signal = np.cos(np.linspace(start=0, stop=10, num=1000))  # Low freq
    signal += np.cos(np.linspace(start=0, stop=100, num=1000))  # High freq
    filtered = nk.signal_filter(signal, highcut=10)
    assert np.std(signal) > np.std(filtered)

    with pytest.warns(nk.misc.NeuroKitWarning, match=r"The sampling rate is too low.*"):
        with pytest.raises(ValueError):
            nk.signal_filter(signal, method="bessel", sampling_rate=100, highcut=50)

    # Generate 10 seconds of signal with 2 Hz oscillation and added 50Hz powerline-noise.
    sampling_rate = 250
    samples = np.arange(10 * sampling_rate)

    signal = np.sin(2 * np.pi * 2 * (samples / sampling_rate))
    powerline = np.sin(2 * np.pi * 50 * (samples / sampling_rate))

    signal_corrupted = signal + powerline
    signal_clean = nk.signal_filter(
        signal_corrupted, sampling_rate=sampling_rate, method="powerline"
    )

    # import matplotlib.pyplot as plt
    # figure, (ax0, ax1, ax2) = plt.subplots(nrows=3, ncols=1, sharex=True)
    # ax0.plot(signal_corrupted)
    # ax1.plot(signal)
    # ax2.plot(signal_clean * 100)
    # plt.suptitle("Powerline")
    # plt.show()

    assert np.allclose(sum(signal_clean - signal), -2, atol=0.2)

    lowcut = 60
    highcut = 40
    order = 2

    signal_bandstop = nk.signal_filter(
        signal_corrupted, sampling_rate=sampling_rate, lowcut=lowcut, highcut=highcut, method="butterworth",
        order=order
    )

    freqs = [highcut, lowcut]
    filter_type = "bandstop"
    sos = scipy.signal.butter(order, freqs, btype=filter_type, output="sos", fs=sampling_rate)
    signal_bandstop_scipy = scipy.signal.sosfiltfilt(sos, signal_corrupted)

    # figure, (ax0, ax1, ax2, ax3) = plt.subplots(nrows=4, ncols=1, sharex=True)
    # ax0.plot(signal_corrupted)
    # ax1.plot(signal)
    # ax2.plot(signal_bandstop * 100)
    # ax3.plot(signal_bandstop_scipy * 100)
    # plt.suptitle("Bandstop")
    # plt.show()

    assert np.allclose(signal_bandstop, signal_bandstop_scipy, atol=0.2)

def test_signal_filter_with_missing():
    sampling_rate = 100
    duration_not_missing = 10
    frequency = 2
    signal = np.concatenate(
    [
        nk.signal_simulate(duration=duration_not_missing, sampling_rate=sampling_rate, frequency=frequency, random_state=42),
        [np.nan] * 1000,
        nk.signal_simulate(duration=duration_not_missing, sampling_rate=sampling_rate, frequency=frequency, random_state=43),
    ]
    )
    samples = np.arange(len(signal))
    powerline = np.sin(2 * np.pi * 50 * (samples / sampling_rate))
    signal_corrupted = signal + powerline
    signal_clean = nk.signal_filter(
        signal_corrupted, sampling_rate=sampling_rate, method="powerline"
    )
    assert signal_clean.size == signal.size
    assert np.allclose(signal_clean, signal, atol=0.2, equal_nan=True)

def test_signal_interpolate():

    x_axis = np.linspace(start=10, stop=30, num=10)
    signal = np.cos(x_axis)

    interpolated = nk.signal_interpolate(x_axis, signal, x_new=np.arange(1000))
    assert len(interpolated) == 1000
    assert interpolated[0] == signal[0]
    assert interpolated[-1] == signal[-1]

def test_signal_findpeaks():

    signal1 = np.cos(np.linspace(start=0, stop=30, num=1000))
    info1 = nk.signal_findpeaks(signal1)

    signal2 = np.concatenate(
        [np.arange(0, 20, 0.1), np.arange(17, 30, 0.1), np.arange(30, 10, -0.1)]
    )
    info2 = nk.signal_findpeaks(signal2)
    assert len(info1["Peaks"]) > len(info2["Peaks"])

def test_signal_merge():

    signal1 = np.cos(np.linspace(start=0, stop=10, num=100))
    signal2 = np.cos(np.linspace(start=0, stop=20, num=100))

    signal = nk.signal_merge(signal1, signal2, time1=[0, 10], time2=[-5, 5])
    assert len(signal) == 150
    assert signal[0] == signal2[0] + signal2[0]

def test_signal_rate():  # since singal_rate wraps signal_period, the latter is tested as well

    # Test with array.
    duration = 10
    sampling_rate = 1000
    signal = nk.signal_simulate(duration=duration, sampling_rate=sampling_rate, frequency=1)
    info = nk.signal_findpeaks(signal)
    rate = nk.signal_rate(peaks=info["Peaks"], sampling_rate=1000, desired_length=len(signal))
    assert rate.shape[0] == duration * sampling_rate

    # Test with dictionary.produced from signal_findpeaks.
    assert info[list(info.keys())[0]].shape == (info["Peaks"].shape[0],)

    # Test with DataFrame.
    duration = 120
    sampling_rate = 1000
    rsp = nk.rsp_simulate(
        duration=duration,
        sampling_rate=sampling_rate,
        respiratory_rate=15,
        method="sinuosoidal",
        noise=0,
    )
    rsp_cleaned = nk.rsp_clean(rsp, sampling_rate=sampling_rate)
    signals, info = nk.rsp_peaks(rsp_cleaned)
    rate = nk.signal_rate(
        signals, sampling_rate=sampling_rate, desired_length=duration * sampling_rate
    )
    assert rate.shape == (signals.shape[0],)

    # Test with dictionary.produced from rsp_findpeaks.
    rate = nk.signal_rate(
        info, sampling_rate=sampling_rate, desired_length=duration * sampling_rate
    )
    assert rate.shape == (duration * sampling_rate,)

def test_signal_period():
    # Test warning path of no peaks
    with pytest.warns(nk.NeuroKitWarning, match=r"Too few peaks detected to compute the rate."):
        nk.signal_period(np.zeros)

def test_signal_plot():

    # Test with array
    signal = nk.signal_simulate(duration=10, sampling_rate=1000)
    nk.signal_plot(signal, sampling_rate=1000)
    fig = plt.gcf()
    axs = fig.get_axes()
    assert len(axs) == 1
    ax = axs[0]
    handles, labels = ax.get_legend_handles_labels()
    assert labels == ["Signal"]
    assert len(labels) == len(handles) == len([signal])
    assert ax.get_xlabel() == "Time (seconds)"
    plt.close(fig)

    # Test with dataframe
    data = pd.DataFrame(
        {
            "Signal2": np.cos(np.linspace(start=0, stop=20, num=1000)),
            "Signal3": np.sin(np.linspace(start=0, stop=20, num=1000)),
            "Signal4": nk.signal_binarize(np.cos(np.linspace(start=0, stop=40, num=1000))),
        }
    )
    nk.signal_plot(data, sampling_rate=None)
    fig = plt.gcf()
    for ax in fig.get_axes():
        handles, labels = ax.get_legend_handles_labels()
    assert labels == list(data.columns.values)
    assert len(labels) == len(handles) == len(data.columns)
    assert ax.get_xlabel() == "Samples"
    plt.close(fig)

    # Test with list
    signal = nk.signal_binarize(nk.signal_simulate(duration=10))
    phase = nk.signal_phase(signal, method="percents")
    nk.signal_plot([signal, phase])
    fig = plt.gcf()
    for ax in fig.get_axes():
        handles, labels = ax.get_legend_handles_labels()
    assert labels == ["Signal1", "Signal2"]
    assert len(labels) == len(handles) == len([signal, phase])
    assert ax.get_xlabel() == "Samples"
    plt.close(fig)

def test_signal_power():

    signal1 = nk.signal_simulate(duration=20, frequency=1, sampling_rate=500)
    pwr1 = nk.signal_power(signal1, [[0.9, 1.6], [1.4, 2.0]], sampling_rate=500)

    signal2 = nk.signal_simulate(duration=20, frequency=1, sampling_rate=100)
    pwr2 = nk.signal_power(signal2, [[0.9, 1.6], [1.4, 2.0]], sampling_rate=100)

    assert np.allclose(np.mean(pwr1.iloc[0] - pwr2.iloc[0]), 0, atol=0.01)

def test_signal_timefrequency():

    signal = nk.signal_simulate(duration=50, frequency=5) + 2 * nk.signal_simulate(
        duration=50, frequency=20
    )

    # short-time fourier transform
    frequency, time, stft = nk.signal_timefrequency(
        signal, method="stft", min_frequency=1, max_frequency=50, show=False
    )

    assert len(frequency) == stft.shape[0]
    assert len(time) == stft.shape[1]
    indices_freq5 = np.logical_and(frequency > 3, frequency < 7)
    indices_freq20 = np.logical_and(frequency > 18, frequency < 22)
    assert np.sum(stft[indices_freq5]) < np.sum(stft[indices_freq20])

    # wavelet transform
    frequency, time, cwtm = nk.signal_timefrequency(
        signal, method="cwt", max_frequency=50, show=False
    )

    assert len(frequency) == cwtm.shape[0]
    assert len(time) == cwtm.shape[1]
    indices_freq5 = np.logical_and(frequency > 3, frequency < 7)
    indices_freq20 = np.logical_and(frequency > 18, frequency < 22)
    assert np.sum(cwtm[indices_freq5]) < np.sum(cwtm[indices_freq20])

    # wvd
    frequency, time, wvd = nk.signal_timefrequency(
        signal, method="wvd", max_frequency=50, show=False
    )
    assert len(frequency) == wvd.shape[0]
    assert len(time) == wvd.shape[1]
    indices_freq5 = np.logical_and(frequency > 3, frequency < 7)
    indices_freq20 = np.logical_and(frequency > 18, frequency < 22)
    assert np.sum(wvd[indices_freq5]) < np.sum(wvd[indices_freq20])

    # pwvd
    frequency, time, pwvd = nk.signal_timefrequency(
        signal, method="pwvd", max_frequency=50, show=False
    )
    assert len(frequency) == pwvd.shape[0]
    assert len(time) == pwvd.shape[1]
    indices_freq5 = np.logical_and(frequency > 3, frequency < 7)
    indices_freq20 = np.logical_and(frequency > 18, frequency < 22)
    assert np.sum(pwvd[indices_freq5]) < np.sum(pwvd[indices_freq20])

def test_signal_psd(recwarn):
    warnings.simplefilter("always")

    data = nk.data("bio_eventrelated_100hz")
    out = nk.signal_psd(data["ECG"], sampling_rate=100)

    assert list(out.columns) == ["Frequency", "Power"]

def test_signal_distort():
    signal = nk.signal_simulate(duration=10, frequency=0.5, sampling_rate=10)

    # Warning for nyquist criterion
    with pytest.warns(
        nk.misc.NeuroKitWarning, match=r"Skipping requested noise frequency.*cannot be resolved.*"
    ):
        nk.signal_distort(signal, sampling_rate=10, noise_amplitude=1, silent=False)

    # Warning for period duration
    with pytest.warns(
        nk.misc.NeuroKitWarning, match=r"Skipping requested noise frequency.*since its period of.*"
    ):
        signal = nk.signal_simulate(duration=1, frequency=1, sampling_rate=10)
        nk.signal_distort(signal, noise_amplitude=1, noise_frequency=0.1, silent=False)

    signal2 = nk.signal_simulate(duration=10, frequency=0.5, sampling_rate=10)

def test_signal_surrogate():
    # Logistic map
    r = 3.95
    x = np.empty(450)
    x[0] = 0.5
    for i in range(1, len(x)):
        x[i] = r * x[i - 1] * (1 - x[i - 1])
    x = x[50:]
    # Create surrogate
    surrogate = nk.signal_surrogate(x, method="IAAFT", random_state=127)
    # Check mean and variance
    assert np.allclose(np.mean(x), np.mean(surrogate))
    assert np.allclose(np.var(x), np.var(surrogate))
    # Check distribution
    assert np.allclose(
        np.histogram(x, 10, (0, 1))[0],
        np.histogram(surrogate, 10, (0, 1))[0],
        atol=1
    )
    # Check spectrum
    assert (
        np.mean(np.abs(np.abs(np.fft.rfft(surrogate - np.mean(surrogate)))
                       - np.abs(np.fft.rfft(x - np.mean(x))))) < 0.1
    )


# This is a sample Python script.

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.

import turtle
window = turtle.Screen() # creating window
window.title("simple pong Game ->")
window.bgcolor("black")
window.setup(width=700, height=700)
window.tracer(0)   #  showing  screen while drawing

# Paddle 1
paddle_1 = turtle.Turtle()  # by defult 20 width and 20 px height
paddle_1.speed(0) #turtle's speed
paddle_1.shape("square")
paddle_1.color("red")
paddle_1.shapesize(stretch_wid=5,stretch_len=1)  # 5*20px width
paddle_1.penup() #--------
paddle_1.goto(-320,0) #--------

# Paddle 2
paddle_2 = turtle.Turtle()  # by defult 20 width and 20 px height
paddle_2.speed(0) #turtle's speed
paddle_2.shape("square")
paddle_2.color("red")
paddle_2.shapesize(stretch_wid=5,stretch_len=1)  # 5*20px width
paddle_2.penup()
paddle_2.goto(320,-0) #--------

# Ball
ball = turtle.Turtle()  # by defult 20 width and 20 px height
ball.speed(0) #turtle's speed
ball.shape("circle")
ball.color("white")
ball.pencolor('white')
ball.goto(0,0) #-------
ball.dx=0.4 # it moves every time by two pixels
ball.dy=0.4

def paddle_1_up():
    y = paddle_1.ycor() # return the y coordinate
    y+=40
    paddle_1.sety(y)

    if y > 300 :
       y = 300
    paddle_1.sety(y)

def paddle_1_down():
    y = paddle_1.ycor() # return the y coordinate
    y-=40
    if y < -300:
       y = -300
    paddle_1.sety(y)

def paddle_2_up():
    y = paddle_2.ycor() # return the y coordinate
    y+=40
    if y > 300:
        y= 300
    paddle_2.sety(y)

def paddle_2_down():
    y = paddle_2.ycor() # return the y coordinate
    y-=40
    if y < -300:
        y= -300
    paddle_2.sety(y)

# keyboard binding
window.listen() # listen to the keyboard input
window.onkeypress( paddle_1_up ,"w")
window.onkeypress( paddle_1_down ,"s")

window.onkeypress( paddle_2_up ,"Up")
window.onkeypress( paddle_2_down ,"Down")

while True:
    window.update() # updating the screen and keep it running
    # move the  ball
    ball.setx(ball.xcor() + ball.dx)
    ball.sety(ball.ycor() + ball.dy)
    # border checking
    if ball.ycor() > 340:
        ball.sety(340)
        ball.dy *=-1 # reverse the direction

    if ball.ycor() < -340:
        ball.sety(-340)
        ball.dy *= -1 # reverse the direction

    if ball.xcor() > 340:
        ball.goto(0,0)
        ball.dx *=-1

    if ball.xcor() < -340:
        ball.goto(0,0)
        ball.dx *=-1

# paddle and ball coalitions
    if (ball.xcor() > 300 and ball.xcor() < 320) and (ball.ycor() < paddle_2.ycor() +
                              40 and ball.ycor() > paddle_2.ycor() -40 ):
        ball.setx(300)
        ball.dx *= -1

    if (ball.xcor() < -300 and ball.xcor() > -320) and (ball.ycor() < paddle_1.ycor() +
                              40 and ball.ycor() > paddle_1.ycor() -40 ):
        ball.setx(-300)
        ball.dx *= -1


import subprocess
import re
import os
from SentenceTokenizer import strip_sec_headers_tokenized_text
from SystemUtilities.Configuration import STANFORD_NER_PATH, ATTRIB_EXTRACTION_DIR_HOME, STANFORD_NER_LIB_ALL
from SystemUtilities.Globals import entity_types, attrib_extraction_features

def train(patients, model_path=ATTRIB_EXTRACTION_DIR_HOME, script_path="Extraction\\AttributeExtraction\\",
          stanford_ner_path=STANFORD_NER_PATH,
          train_script_name="train_model.bat"):

    # if os.environ['OS'] == "Windows_NT":
    #     train_script_name = "train_model.bat"

    training_doc_objs = get_documents_from_patients(patients)

    for type in entity_types:
        train_file_name = model_path + r"Train-Files\\" + "train-" + type + ".tsv"
        prop_file_name = model_path + r"Prop-Files\\" + type + ".prop"
        model_name = model_path + r"Models\\" + "model-" + type + ".ser.gz"
        create_train_file(training_doc_objs, train_file_name, type)
        create_prop_file(prop_file_name, train_file_name, attrib_extraction_features, model_name)
        train_model(stanford_ner_path, prop_file_name, script_path+train_script_name)

def create_train_file(training_sent_objs, train_file_name, type):
    """
    Sorry about the crazy embedded FOR loops and indents.
    I will modularize better to make it prettier. - Martin
    """
    train_file = open(train_file_name, 'w')
    for sent_obj in training_sent_objs:
        if len(sent_obj.gold_events) > 0: # if the sentence is predicted to have an event
            sentence = sent_obj.text
            gold_event_set = sent_obj.gold_events
            sent_offset = sent_obj.span_in_doc_start

            for match in re.finditer("\S+", sentence):
                start = match.start()
                end = match.end()
                pointer = sent_offset + start
                word = match.group(0).rstrip(",.:;)")
                if word not in {"SOCIAL", "HISTORY", "SUBSTANCE",
                                    "ABUSE"}:  # see tokenizer in utils, they must both match
                    train_file.write(word)
                    # Debug line
                    # train_file.write("[" + str(pointer) + "," + str(sent_offset + match.end()) + "]")
                    train_file.write("\t")
                    answer = "0"
                    debug_str = ""
                    for entity in gold_event_set:
                        if answer != "0":
                            break
                        attr_dict = entity.attributes
                        for attr in attr_dict:
                            for span in attr_dict[attr].all_value_spans:
                                attr_start = int(span.start)
                                attr_end = int(span.stop)
                                if attr_dict[attr].type == type and \
                                                        attr_start <= pointer < attr_end:
                                    answer = type
                                    # Debug lines
                                    # answer += "\t" + attr_dict[attr].text +\
                                    #          "[" + str(attr_start) +\
                                    #          "," + str(attr_end) + "]"
                                    debug_str = "--- Sent obj start index: " + str(sent_offset) + "\n" + \
                                                "--- Match obj start index: " + str(start) + "\n" + \
                                                "--- Match obj end index: " + str(end) + "\n" + \
                                                "--- Pointer index: " + str(sent_offset) + " + " + \
                                                str(start) + " = " + str(pointer) + "\n" + \
                                                "--- Attr start index: " + str(attr_start) + "\n" + \
                                                "--- Attr end index: " + str(attr_end) + "\n"
                                    break
                    train_file.write(answer + "\n")
                    # Debug line
                    # train_file.write(debug_str)
                    # print(debug_str)
    train_file.close()

def create_prop_file(prop_file_name, train_file_name, features, model_name):
    prop_file = open(prop_file_name, 'w')
    prop_file.write("trainFile = " + train_file_name + "\n")
    prop_file.write("serializeTo = " + model_name + "\n")
    # prop_file.write("map = word=0,answer=1,temp=2,method=3,type=4,amount=5,freq=6,hist=7\n")
    prop_file.write("map = word=0,answer=1\n")
    for feat in features:
        prop_file.write(feat + "\n")
    prop_file.close()

def train_model(stanford_ner_path, prop_file_name, train_script_name):
    subprocess.call([train_script_name, stanford_ner_path, prop_file_name, STANFORD_NER_LIB_ALL], shell=True)

def get_documents_from_patients(patients):
    sentences = list()
    for patient in patients:
        for document in patient.doc_list:
            for sent_obj in document.sent_list:
                if (len(sent_obj.gold_events)) > 0:  # if the sentence has an event
                    sentences.append(sent_obj)
    return sentences


import logging
import os
import shutil
from pathlib import Path
import tempfile

import pandas as pd

from autumn.core import db, plots
from autumn.settings import REMOTE_BASE_DIR
from autumn.infrastructure.tasks.utils import get_project_from_run_id
from autumn.core.utils.s3 import download_from_run_s3, list_s3, upload_to_run_s3, get_s3_client
from autumn.core.utils.timer import Timer
from .storage import StorageMode, MockStorage, S3Storage, LocalStorage
from autumn.core.runs import ManagedRun

logger = logging.getLogger(__name__)

def powerbi_task(run_id: str, urunid: str, quiet: bool, store="s3"):

    REMOTE_BASE_DIR = Path(tempfile.mkdtemp())

    POWERBI_PLOT_DIR = os.path.join(REMOTE_BASE_DIR, "plots", "uncertainty")
    POWERBI_DATA_DIR = os.path.join(REMOTE_BASE_DIR, "data", "powerbi")
    POWERBI_DIRS = [POWERBI_DATA_DIR, POWERBI_PLOT_DIR]
    POWERBI_PRUNED_DIR = os.path.join(POWERBI_DATA_DIR, "pruned")
    POWERBI_COLLATED_PATH = os.path.join(POWERBI_DATA_DIR, "collated")
    POWERBI_COLLATED_PRUNED_PATH = os.path.join(POWERBI_DATA_DIR, "collated-pruned")

    s3_client = get_s3_client()
    project = get_project_from_run_id(run_id)

    # Set up directories for plots and output data.
    with Timer(f"Creating PowerBI directories"):
        for dirpath in POWERBI_DIRS:
            if os.path.exists(dirpath):
                shutil.rmtree(dirpath)

            os.makedirs(dirpath)

    mr = ManagedRun(run_id, s3_client=s3_client)

    if store == StorageMode.MOCK:
        storage = MockStorage()
    elif store == StorageMode.S3:
        s3_client = get_s3_client()
        storage = S3Storage(s3_client, run_id, REMOTE_BASE_DIR, not quiet)
    elif store == StorageMode.LOCAL:
        storage = LocalStorage(run_id, REMOTE_BASE_DIR)

    # Find the full model run databases in AWS S3.
    # key_prefix = os.path.join(run_id, os.path.relpath(FULL_RUN_DATA_DIR, REMOTE_BASE_DIR))
    # chain_db_keys = []
    # for filename_base in ["mcmc_run", "mcmc_params", "derived_outputs"]:
    #    chain_db_keys += list_s3(s3_client, key_prefix, key_suffix=f"{filename_base}.feather")

    # Download the full model run databases.
    # with Timer(f"Downloading full model run data"):
    #    for src_key in chain_db_keys:
    #        download_from_run_s3(s3_client, run_id, src_key, quiet)

    # No urunid supplied; get a single candidate dataframe (ie the MLE run)
    if urunid == "mle":
        all_mcmc_df = mr.full_run.get_mcmc_runs()
        candidates_df = db.process.select_pruning_candidates(all_mcmc_df, 1)
    else:
        c, r = (int(x) for x in urunid.split("_"))
        candidates_df = pd.DataFrame(columns=["chain", "run"])
        candidates_df.loc[0] = dict(chain=c, run=r)

    # Remove unnecessary data from each full model run database.
    full_db_paths = db.load.find_db_paths(mr.full_run.local_path)
    with Timer(f"Pruning chain databases"):
        get_dest_path = lambda p: os.path.join(POWERBI_PRUNED_DIR, os.path.basename(p))
        for full_db_path in full_db_paths:
            chain_id = int(full_db_path.split("-")[-1])
            chain_candidates = candidates_df[candidates_df["chain"] == chain_id]
            db.process.prune_chain(full_db_path, get_dest_path(full_db_path), chain_candidates)

    # Collate data from each pruned full model run database into a single database.
    pruned_db_paths = db.load.find_db_paths(POWERBI_PRUNED_DIR)
    with Timer(f"Collating pruned databases"):
        db.process.collate_databases(pruned_db_paths, POWERBI_COLLATED_PATH)

    # Calculate uncertainty for model outputs.
    with Timer(f"Calculating uncertainty quartiles"):
        db.uncertainty.add_uncertainty_quantiles(POWERBI_COLLATED_PATH, project.plots)

    # Remove unnecessary data from the database.
    with Timer(f"Pruning final database"):
        db.process.prune_final(POWERBI_COLLATED_PATH, POWERBI_COLLATED_PRUNED_PATH, candidates_df)

    # Unpivot database tables so that they're easier to process in PowerBI.
    run_slug = run_id.replace("/", "-")
    dest_db_path = os.path.join(POWERBI_DATA_DIR, f"powerbi-{run_slug}.db")
    with Timer(f"Applying PowerBI specific post-processing final database"):
        db.process.powerbi_postprocess(POWERBI_COLLATED_PRUNED_PATH, dest_db_path, run_id)

    # Upload final database to AWS S3
    with Timer(f"Uploading PowerBI data to AWS S3"):
        # upload_to_run_s3(s3_client, run_id, dest_db_path, quiet)
        storage.store(dest_db_path)

    # Create uncertainty plots
    with Timer(f"Creating uncertainty plots"):
        plots.uncertainty.plot_uncertainty(project.plots, dest_db_path, POWERBI_PLOT_DIR)

    # Upload the plots to AWS S3.
    with Timer(f"Uploading plots to AWS S3"):
        # upload_to_run_s3(s3_client, run_id, POWERBI_PLOT_DIR, quiet)
        storage.store(POWERBI_PLOT_DIR)

    shutil.rmtree(REMOTE_BASE_DIR)


"""
Utility functions for working with image data.
"""
from cStringIO import StringIO
from math import ceil, floor
import svgwrite

def normalize(imagedata):
    # No -= or /= to avoid accidentally modifying the input:
    imagedata = imagedata - imagedata.min()
    imagedata = imagedata / imagedata.max()
    return imagedata

def generate_svg_filter_map(num_filters, ksize, num_cols, scale=1, padding_px=1):
    """
    Returns an SVG that functions like an HTML image map.
    """
    num_rows = int(ceil((1.0 * num_filters / num_cols)))
    max_cols = int(floor(1.0 * num_filters / num_rows))
    img_width  = ((max_cols - 1) * padding_px) + (ksize * max_cols)
    img_height = ((num_rows - 1) * padding_px) + (ksize * num_rows)
    filter_img_size = (ksize * scale, ksize * scale)
    svg_size = (img_width * scale, img_height * scale)

    svg = svgwrite.Drawing(size=svg_size)
    svg.add_stylesheet("/static/svg-styles.css", "noname")
    for row in range(num_rows):
        for col in range(num_cols):
            x = col * (ksize + padding_px) * scale
            y = row * (ksize + padding_px) * scale
            rect = svg.rect(insert=(x, y), size=filter_img_size, fill="none",
                            id="filter%i;channel%i" % (row, col))
            # It would be nice to use custom 'data-' attributes here.
            # It looks like the SVG spec doesn't officially support them,
            # so `svgwrite` flags them as errors:
            # see http://stackoverflow.com/questions/15532371
            svg.add(rect)
    out = StringIO()
    svg.write(out)
    return out.getvalue()


import colorsys
import math
import pandas
import matplotlib

def color_list_redv(size, green=200, blue=200, curvature=0.3):
    """Get a color gradient as a list of hex values

    This helper returns a list of given length populated with hex values at a consistent
    blue and green level while varying the red level. This is useful for diverging sequences

    Args:
        size (int): Defined size of the list
        green(int): value between 0 and 255. Default to 200
        blue(int): value between 0 and 255. Default to 200
        curvature (float): a value defining the curvature of the color gradient.
                        A lower number pushes the colors at the low end of the gradient closer together and stretches
                        out the colors at the high end, and vice-versa

    Returns:
        A list of hex values
    """
    return ['#%02x%02x%02x' % (round(math.pow((x/size),curvature)*255), green, blue) for x in range(0, size, 1)]

def color_list_lightness(size, hue=0.25, saturation=1, low=0, high=360):
    """Get a color gradient as a list of hex values

    This helper returns a list of given length populated with hex values at a consistent
    hue and saturation level. This is useful for a one dimensional sequence

    Args:
        size (int): Defined size of the list
        lightness(float): value between 0 and 1. Default to 0.75 for a pastel look
        saturation(float): value between 0 and 1. Default to 1
        low (int): hue index value between 0 and 360
        high (int): hue index value between 0 and 360

    Returns:
        A list of hex values
    """

    assert low>=0, "Low range must be greater than or equal to 0"
    assert high<=360, "High range must be less than or equal to 360"
    assert low<high-size, "High must be greater than Low by at least the number of required intervals"

    color = [colorsys.hls_to_rgb(hue, math.pow(x/360,0.8), saturation) for x in range(low, high, round((high-low)/size))]
    return ['#%02x%02x%02x' % (round(x[0]*255), round(x[1]*255), round(x[2]*255)) for x in color]

def color_list(size, lightness=0.75, saturation=1, low=0, high=360):
    """Get a color gradient as a list of hex values

    This helper returns a list of given length populated with hex values at a consistent
    lightness and saturation level. This is useful for categorical values

    Args:
        size (int): Defined size of the list
        lightness(float): value between 0 and 1. Default to 0.75 for a pastel look
        saturation(float): value between 0 and 1. Default to 1
        low (int): hue index value between 0 and 360
        high (int): hue index value between 0 and 360

    Returns:
        A list of hex values
    """
    assert low>=0, "Low range must be greater than or equal to 0"
    assert high<=360, "High range must be less than or equal to 360"
    assert low<high-size, "High must be greater than Low by at least the number of required intervals"
    color = [colorsys.hls_to_rgb(x/360, lightness, saturation) for x in range(low, high, round((high-low)/size))]
    return ['#%02x%02x%02x' % (round(x[0]*255), round(x[1]*255), round(x[2]*255)) for x in color]

def get_plot_legend(df, category, aggs={}, color_type=color_list, **kwargs):
    """Get a color coded legend for plotting

    This helper returns a DataFrame of the levels to be plotted with associated colors

    Args:
        df (DataFrame): A pandas DataFrame
        category (str): The name of the column to groupby
        aggs (dict): A dictionary of strings where the key is the name of the column and the value is the aggregation function
        color_type (func): The color function to use

    Returns:
        A legend DataFrame containing aggregate statistics and a color coded column of hex values
    """
    keep_cols = [x for x in aggs.keys()] + [category]
    legend_sum = (df[keep_cols]
                      .groupby(category, as_index=False)
                      .agg(aggs)
                      .rename(columns={x:x+"_"+str(aggs[x]) for x in aggs})
                     )
    colors = color_type(size=len(legend_sum.index), **kwargs)
    legend_sum['color'] = colors

    return legend_sum, legend_sum.style.apply(lambda col: ['background-color: %s' % (colors[i]) for i in range(len(colors))],
                                  subset=['color']
                                 )

def plot_categoricals(df, ax, plot_legend, category):
    """Plot categoricals by color

    Intended to be used in conjunction with get_plot_legend to plot categoricals by color

    Args:
        df (DataFrame): A pandas DataFrame
        ax (matplotlib ax object): The axis to plot on
        plot_legend (DataFrame): A legend DataFrame output by get_plot_legend with the categorical variable and corresponding color
        category (str): The name of the column to groupby

    Returns:
        A matplotlib plot
    """

    for x in range(len(plot_legend.index)):
        df.loc[df[category] == plot_legend.iloc[x][category]].plot(ax=ax, color=plot_legend.iloc[x]["color"])


from vpp_papi import VppConnection, VppMsgProxy

conn = VppConnection()
conn.connect()

proxy = VppMsgProxy(conn)

# Get the current version of VPP
version = proxy.vpe_api_version()

print("VPP Version:", version.program, version.version)


"""
Code borrowed and adapted from
https://github.com/shubham-goel/ucmr/blob/master/src/nnutils/geom_utils.py
"""
from __future__ import absolute_import, division, print_function

import torch
import numpy as np

def get_viewpoint(azimouth):
    ''' Returns the viewpoint given azimouth angle '''
    return 'side' if (azimouth>=45 and azimouth<135) or (azimouth>=-135 and azimouth<-45) else 'front-back'

def quat_inverse(quat):
    """
    quat: B x 4: [quaternions]
    returns inverted quaternions
    """
    flip = torch.tensor([1,-1,-1,-1],dtype=quat.dtype,device=quat.device)
    quat_inv = quat * flip.view((1,)*(quat.dim()-1)+(4,))
    return quat_inv

def hamilton_product(qa, qb):
    """Multiply qa by qb.
    Args:
        qa: B X N X 4 quaternions
        qb: B X N X 4 quaternions
    Returns:
        q_mult: B X N X 4
    """
    qa_0 = qa[..., 0]
    qa_1 = qa[..., 1]
    qa_2 = qa[..., 2]
    qa_3 = qa[..., 3]

    qb_0 = qb[..., 0]
    qb_1 = qb[..., 1]
    qb_2 = qb[..., 2]
    qb_3 = qb[..., 3]

    # See https://en.wikipedia.org/wiki/Quaternion#Hamilton_product
    q_mult_0 = qa_0*qb_0 - qa_1*qb_1 - qa_2*qb_2 - qa_3*qb_3
    q_mult_1 = qa_0*qb_1 + qa_1*qb_0 + qa_2*qb_3 - qa_3*qb_2
    q_mult_2 = qa_0*qb_2 - qa_1*qb_3 + qa_2*qb_0 + qa_3*qb_1
    q_mult_3 = qa_0*qb_3 + qa_1*qb_2 - qa_2*qb_1 + qa_3*qb_0

    return torch.stack([q_mult_0, q_mult_1, q_mult_2, q_mult_3], dim=-1)

def quat_rotate(X, quat):
    """Rotate points by quaternions.

    Args:
        X: B X N X 3 points
        quat: B X 4 quaternions

    Returns:
        X_rot: B X N X 3 (rotated points)
    """
    # repeat q along 2nd dim
    # import ipdb; ipdb.set_trace()
    # ones_x = X[[0], :, :][:, :, [0]]*0 + 1
    # q = torch.unsqueeze(q, 1)*ones_x
    quat      = quat[:,None,:].expand(-1,X.shape[1],-1)
    quat_conj = torch.cat([ quat[:, :, 0:1] , -1*quat[:, :, 1:4] ], dim=-1)
    X         = torch.cat([ X[:, :, 0:1]*0, X ], dim=-1)
    X_rot     = hamilton_product(quat, hamilton_product(X, quat_conj))
    return X_rot[:, :, 1:4]

# ------------------------------

def axisangle2quat(axis, angle):
    """
    axis : Bx3: [axis]
    angle: B  : [angle]
    returns
        quaternion: Bx4
    """
    axis  = torch.nn.functional.normalize(axis,dim=-1)
    angle = angle.unsqueeze(-1)/2
    quat  = torch.cat([angle.cos(), angle.sin()*axis], dim=-1)
    return quat

def quat2axisangle(quat):
    """
    quat: B x 4: [quaternions]
    returns quaternion axis, angle
    """
    cos   = quat[..., 0]
    sin   = quat[..., 1:].norm(dim=-1)
    axis  = quat[..., 1:]/sin[..., None]
    angle = 2*cos.clamp(-1+1e-6,1-1e-6).acos()
    return axis, angle

def get_base_quaternions(num_pose_az=8, num_pose_el=5, initial_quat_bias_deg=90., elevation_bias=0, azimuth_bias=0):
    _axis = torch.eye(3).float()

    # Quaternion base bias
    xxx_base = [1.,0.,0.]
    aaa_base = initial_quat_bias_deg
    axis_base = torch.tensor(xxx_base).float()
    angle_base = torch.tensor(aaa_base).float() / 180. * np.pi
    qq_base = axisangle2quat(axis_base, angle_base) # 4

    # Quaternion multipose bias
    azz = torch.as_tensor(np.linspace(0,2*np.pi,num=num_pose_az,endpoint=False)).float() + azimuth_bias * np.pi/180
    ell = torch.as_tensor(np.linspace(-np.pi/2,np.pi/2,num=(num_pose_el+1),endpoint=False)[1:]).float() + elevation_bias * np.pi/180
    quat_azz = axisangle2quat(_axis[1], azz) # num_pose_az,4
    quat_ell = axisangle2quat(_axis[0], ell) # num_pose_el,4
    quat_el_az = hamilton_product(quat_ell[None,:,:], quat_azz[:,None,:]) # num_pose_az,num_pose_el,4
    quat_el_az = quat_el_az.view(-1,4)                  # num_pose_az*num_pose_el,4
    _quat = hamilton_product(quat_el_az, qq_base[None,...]).float()

    return _quat

def camera_quat_to_position_az_el(quat, initial_quat_bias_deg=0.):
    """Quat: N,4"""
    assert(quat.dim()==2)
    assert(quat.shape[1]==4)
    quat    = quat_to_camera_position(quat, initial_quat_bias_deg=initial_quat_bias_deg)
    quat_uv = convert_3d_to_uv_coordinates(quat)
    return quat_uv

def quat_to_camera_position(quat, initial_quat_bias_deg):
    """Quat: N,4"""
    X = torch.zeros((quat.shape[0],1,3),dtype=torch.float32,device=quat.device)
    X[:,:,2] = -1
    new_quat = quat_inverse(quat)

    xxx_base   = [1.,0.,0.]
    aaa_base   = initial_quat_bias_deg
    axis_base  = torch.tensor(xxx_base)
    angle_base = torch.tensor(aaa_base) / 180. * np.pi
    qq_base    = axisangle2quat(axis_base, angle_base) # 4
    new_quat   = hamilton_product(qq_base[None,:], new_quat)

    new_quat = hamilton_product(axisangle2quat( torch.eye(3, dtype=torch.float32)[0], torch.tensor(np.pi/2))[None,:],  new_quat ) # rotate 90deg about X
    new_quat = hamilton_product(axisangle2quat( torch.eye(3, dtype=torch.float32)[2], torch.tensor(-np.pi/2))[None,:], new_quat ) # rotate -90deg about Z
    rotX     = quat_rotate(X, new_quat).squeeze(1)   # ...,3
    return rotX

def convert_3d_to_uv_coordinates(X):
    """
    X : N,3
    Returns UV: N,2 normalized to [-1, 1]
    U: Azimuth: Angle with +X [-pi,pi]
    V: Inclination: Angle with +Z [0,pi]
    """
    if type(X) == torch.Tensor:
        eps=1e-4
        rad = torch.norm(X, dim=-1).clamp(min=eps)
        theta = torch.acos( (X[..., 2] / rad).clamp(min=-1+eps,max=1-eps) )  # Inclination: Angle with +Z [0,pi]
        phi = torch.atan2(X[..., 1], X[..., 0])                              # Azimuth    : Angle with +X [-pi,pi]
        vv = (theta / np.pi) * 2 - 1
        uu = ((phi + np.pi) / (2*np.pi)) * 2 - 1
        uv = torch.stack([uu, vv],dim=-1)
    else:
        rad = np.linalg.norm(X, axis=-1)
        rad = np.clip(rad, 1e-12, None)
        theta = np.arccos(X[..., 2] / rad)                                   # Inclination: Angle with +Z [0,pi]
        phi = np.arctan2(X[..., 1], X[..., 0])                               # Azimuth    : Angle with +X [-pi,pi]
        vv = (theta / np.pi) * 2 - 1
        uu = ((phi + np.pi) / (2*np.pi)) * 2 - 1
        uv = np.stack([uu, vv],-1)
    return uv


"""
Miscellaneous tools related to centrality.
"""
from overtime.components.digraphs import TemporalDiGraph
from operator import itemgetter

def order_centrality(centralities):
    """
        Converts a dictionary which relates nodes to centrality values, so that items are ordered by centrality
        value.
    """
    ordered_centralities = {k: v for k, v in sorted(centralities.items(), key=itemgetter(1), reverse=True)}
    return ordered_centralities

def convert_to_directed(graph):
    """
        Converts an undirected graph so each undirected edge is modelled by two bidirectional edges.

        Parameter(s):
        -------------
        graph : TemporalGraph
            An undirected temporal graph.

        Returns:
        --------
        converted_graph : TemporalDiGraph
            A digraph which has bidirected edges which represent the undirected edges of the input graph.
    """
    # Initialize
    converted_graph = TemporalDiGraph("undirected_graph")
    # Add nodes
    for node in graph.nodes.aslist():
        converted_graph.add_node(node.label)
    # Add edges in both directions for each undirected edge
    for edge in graph.edges.aslist():
        converted_graph.edges.add(edge.node1.label, edge.node2.label, graph.nodes, edge.start, edge.end)
        converted_graph.edges.add(edge.node2.label, edge.node1.label, graph.nodes, edge.start, edge.end)

    return converted_graph


from datetime import datetime
from core.validators import FieldValidator
from .constants import CREATE, UPDATE

class Field:
    """
    Base class of any field.
    """
    field_type = None
    _name = None
    _value = None
    is_sub_field = False
    _reserved_attributes = {
        'null': bool,
        'blank': bool,
        'min_length': int,
        'max_length': int,
        'unique': bool,
        'choices': (tuple, list),
        'on_delete': int
    }

    def __setattr__(self, key, value):
        if key in self._reserved_attributes:
            required_type = self._reserved_attributes.get(key)

            if value is not None and not isinstance(value, required_type):
                raise TypeError(
                    'Reserved attr `{attr_name}` has wrong type! '
                    'Expected `{attr_type}`'.format(
                        attr_name=key,
                        attr_type=required_type.__name__
                    )
                )

        super().__setattr__(key, value)

    def set_field_name(self, name):
        self._name = name

    def get_field_name(self):
        return self._name

    def set_field_value(self, value):
        self._value = value

    def get_field_value(self):
        return self._value

    def get_choice_key(self, value):
        choices = getattr(self, 'choices', None)

        if choices:
            choices = {key: value for key, value in choices}

            if value in choices.keys():
                value = choices.get(value)

            elif value not in choices.values():
                raise ValueError(
                    'The value \'{field_value}\' is not specified in the \'choices\' attribute.'.format(
                        field_value=value
                    )
                )

        return value

    def get_choice_value(self, key):
        choices = getattr(self, 'choices', None)
        value = key

        if choices:
            choices = {value: key for key, value in choices}

            if key not in choices.keys():
                raise ValueError(
                    'The value \'{field_value}\' is not specified in the \'choices\' attribute.'.format(
                        field_value=key
                    )
                )

            value = choices.get(key)

        return value

    def get_value(self, name, value, action):
        default = getattr(self, 'default', None)
        choices = getattr(self, 'choices', None)

        # Process the 'default' attribute
        if default is not None and not value:
            value = default() if callable(default) else default

        # Process the 'choices' attribute
        if value and choices:
            value = self.get_choice_key(value)

        return value

    def validate(self, name, value):
        field_validator = FieldValidator(self, name, value)
        field_validator.validate()

        return value

    def to_internal_value(self, value):
        """
        Define this method if you need to save specific data format.
        Here you must to serialize your data.
        """
        return value

    def to_external_value(self, value):
        """
        Define this method if you specified `to_internal_value`,
        to represent data from the storage.
        Here you must to deserialize your data.
        """
        return value

class BoolField(Field):
    field_type = bool

    def __init__(self, null=False, default=None, choices=None):
        self.null = null
        self.default = default
        self.choices = choices

class StringField(Field):
    field_type = str

    def __init__(self, null=False, blank=True, min_length=None, max_length=None,
                 unique=False, index=None, default=None, choices=None):
        self.null = null
        self.blank = blank
        self.min_length = min_length
        self.max_length = max_length
        self.unique = unique
        self.index = index
        self.default = default
        self.choices = choices

class IntegerField(Field):
    field_type = int

    def __init__(self, null=False, unique=False, default=None, choices=None):
        self.null = null
        self.unique = unique
        self.default = default
        self.choices = choices

class FloatField(Field):
    field_type = float

    def __init__(self, null=False, unique=False, default=None, choices=None):
        self.null = null
        self.unique = unique
        self.default = default
        self.choices = choices

class ListField(Field):
    field_type = list

    def __init__(self, child=None, null=False, length=None, unique=False, default=None):
        self.child = child
        self.null = null
        self.length = length
        self.unique = unique
        self.default = default

    # For IDE tips
    def __iter__(self):
        return self

    def validate(self, name, value):
        value = super().validate(name, value)

        # TODO: use multiprocessing pool of workers
        if value and self.child is not None:
            for list_item in value:
                self.child.is_sub_field = True
                self.child.validate(name, list_item)

        return value

class DictField(Field):
    field_type = dict

    def __init__(self, null=False, unique=False, min_length=None, max_length=None, default=None):
        self.null = null
        self.unique = unique
        self.min_length = min_length
        self.max_length = max_length
        self.default = default

    # For IDE tips
    def __iter__(self):
        return self

class DateTimeField(Field):
    field_type = datetime

    def __init__(self, null=False, auto_now_create=False, auto_now_update=False):
        self.null = null
        self.auto_now_create = auto_now_create
        self.auto_now_update = auto_now_update

    def get_value(self, name, value, action):
        if (action is CREATE and self.auto_now_create) or (action is UPDATE and self.auto_now_update) and not value:
            # MonogoDB rounds microseconds, and ODM does not request the created document,
            # for data consistency I reset them
            value = datetime.now().replace(microsecond=0)

        return value

class BaseRelationField(Field):
    backward_class = None
    _query = None

    def __init__(self, relation, related_name=None, null=False, on_delete=None):
        self.relation = relation
        self.related_name = related_name
        self.null = null
        self.on_delete = on_delete

    def __aiter__(self):
        return self

    # TODO: Test it!
    async def __anext__(self):
        async for item in self._get_query():
            return item
        raise StopAsyncIteration()

    def __await__(self):
        return self._get_query().__await__()

    def _get_query(self):
        raise NotImplementedError

class BaseBackwardRelationField(Field):
    _query = None

    def get_query(self):
        raise NotImplementedError

    def __init__(self, relation):
        self.relation = relation

    def __aiter__(self):
        return self

    async def __anext__(self):
        async for item in self.get_query():
            return item
        raise StopAsyncIteration()

    def __await__(self):
        return self.get_query().__await__()

class ForeignKeyBackward(BaseBackwardRelationField):
    def get_query(self):
        if not self._query:
            filter_kwargs = {self._name: self._value}
            self._query = self.relation.objects.filter(**filter_kwargs)

        return self._query

class OneToOneBackward(BaseBackwardRelationField):
    def get_query(self):
        if not self._query:
            filter_kwargs = {self._name: self._value}
            self._query = self.relation.objects.get(**filter_kwargs)

        return self._query

class ForeignKey(BaseRelationField):
    backward_class = ForeignKeyBackward

    def _get_query(self):
        if not self._query:
            get_kwargs = {'_id': self._value.id}
            self._query = self.relation.objects.get(**get_kwargs)

        return self._query

class OneToOne(BaseRelationField):
    backward_class = OneToOneBackward

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.unique = True

    def _get_query(self):
        get_kwargs = {'_id': self._value.id}
        return self.relation.objects.get(**get_kwargs)


import math

#inputs
#Use metric

root = 0.36
tip = 0.110
span = 0.125
thickness = 0.01
shearModulus = 4891784.65609
heightAtMaxVelocity = 1000

optimize = True

lowerLim = 480
upperLim = 500

iterations = 1000
#if no iteration put 0 for iteration factor, there should be one factor for each parameter
iterationFactors = [0, 0.001, 0, 0, 0, 0]

root *= 39.37
tip *= 39.37
span *= 39.37
thickness *= 39.37
shearModulus /= 6.895
heightAtMaxVelocity *= 3.281

def calculateFlutterSpeed(root, tip, span, thickness, shearModulus, heightAtMaxVelocity):
    s = 0.5*span*(root+tip)
    aspectRatio = (span**2)/s
    lamb = tip/root
    temp = 59-(0.00356*heightAtMaxVelocity)
    pressure = 2116*(((temp+459.7)/518.6)**5.256)/144
    a = (1.4*1716.59*(temp+460))**0.5

    bottomTop = (1.337*(aspectRatio**3)*pressure*(lamb+1))
    botomBottom = (2*(aspectRatio+2)*((thickness/root)**3))
    bottom = bottomTop/botomBottom

    flutterSpeed = a*((shearModulus/bottom)**0.5)

    flutterSpeedMetric = flutterSpeed/3.281
    return flutterSpeedMetric

#optimize
if optimize == True:
    parametersToIterate = [root, tip, span, thickness, shearModulus, heightAtMaxVelocity]
    '''
    for i in range(iterations):

        for i in range(6):
            parametersToIterate[i] += iterationFactors[i]

        print(parametersToIterate[0], parametersToIterate[1], parametersToIterate[2], parametersToIterate[3], parametersToIterate[4], parametersToIterate[5], calculate(parametersToIterate[0], parametersToIterate[1], parametersToIterate[2], parametersToIterate[3], parametersToIterate[4], parametersToIterate[5]))
    '''
    for i in range(6):

        for j in range(iterations):
            parametersToIterate[i] += iterationFactors[i]
            result = calculateFlutterSpeed(parametersToIterate[0], parametersToIterate[1], parametersToIterate[2], parametersToIterate[3], parametersToIterate[4], parametersToIterate[5])
            if lowerLim < result < upperLim:
                break

    print(parametersToIterate[0]/39.37, parametersToIterate[1]/39.37, parametersToIterate[2]/39.37, parametersToIterate[3]/39.37, parametersToIterate[4]*6.895, parametersToIterate[5]/3.281, calculateFlutterSpeed(parametersToIterate[0], parametersToIterate[1], parametersToIterate[2], parametersToIterate[3], parametersToIterate[4], parametersToIterate[5]))

else:
    print("Flutter speed: ", round(calculateFlutterSpeed(root, tip, span, thickness, shearModulus, heightAtMaxVelocity), 2), "m/s")


import discord
from discord.ext import commands

from bot import BaseBot
from core import error
from core.utils import read_config

config = read_config()

class Log(commands.Cog):
    def __init__(self, bot: BaseBot):
        self.bot = bot

    @commands.Cog.listener()
    async def on_command_error(self, ctx, err):
        err = getattr(err, "original", err)
        err = error.BotException(err, self.bot)
        return await err.propagate()

    @commands.Cog.listener()
    async def on_message_edit(
        self, message_before: discord.Message, message_after: discord.Message
    ):
        if (
            not message_after.author.bot
            and message_before.guild.id == config["server"]["guild_id"]
        ):
            message_log_channel_id = self.bot.config["server"]["message_log_channel"]
            message_log_channel = await self.bot.fetch_channel(message_log_channel_id)
            e = discord.Embed(
                title="Message Edit",
                color=0xF6F600,
                description=f"**Original**: {message_before.content}\n**New**: {message_after.content}\n[jump url]({message_after.jump_url})",
            )
            e.set_author(
                name=message_after.author.name,
                icon_url=message_after.author.display_avatar,
            )
            await message_log_channel.send(embed=e)

    @commands.Cog.listener()
    async def on_message_delete(self, message: discord.Message):
        if not message.author.bot and message.guild.id == config["server"]["guild_id"]:
            message_log_channel_id = self.bot.config["server"]["message_log_channel"]
            message_log_channel = await self.bot.fetch_channel(message_log_channel_id)
            e = discord.Embed(
                title="Message delete",
                color=0xF64800,
                description=f"{message.content}",
            )
            e.set_author(
                name=message.author.name, icon_url=message.author.display_avatar
            )
            await message_log_channel.send(embed=e)

async def setup(bot):
    await bot.add_cog(Log(bot))


def has_storage(self, address):
        """
        True if something has been written to the storage.
        Note that if a slot has been erased from the storage this function may
        lose any meaning.
        """
        storage = self._world_state[address]['storage']
        array = storage.array
        while not isinstance(array, ArrayVariable):
            if isinstance(array, ArrayStore):
                return True
            array = array.array
        return False