import os
import torch
import torchvision
import torchvision.transforms as transforms
import random
import pathlib
import utils
import numpy as np
from PIL import Image
from torch.utils import data

tensor = torchvision.transforms.Compose([torchvision.transforms.ToTensor()])
NUM_CLASS = 19

# class CityscapesLoader(data.Dataset):
#     """
#     Load Cityscapes dataset for training and testing
#
#     Dataset augmentations performed in a synchronised manner here.
#     """
#     def __init__(self,
#                  root,
#                  input_res,
#                  split='train',
#                  mode='train',
#                  encoder_only=False,
#                  transform=None,
#                  target_transform=None):
#         """
#         :param root: path to cityscapes folder
#         :param input_res: desired output res of the images and targets
#         :param split: dataset split
#         :param mode: mode of the transformation setup
#         :param transform: final transformation applications
#         """
#         super(CityscapesLoader, self).__init__()
#
#         self.root = pathlib.Path(root)
#         self.split = split
#         self.mode = mode
#         self.transform = transform
#         self.target_transform = target_transform
#         self.input_res = input_res
#         self.encoder_only = encoder_only
#
#         self.images_base = self.root / "leftImg8bit" / self.split
#         self.annotations_base = self.root / "gtFine" / self.split
#         self.files = {}
#         self.files[self.split] = utils.recursive_glob(rootdir=self.images_base, suffix="*.png")
#
#         self.void_classes = [0, 1, 2, 3, 4, 5, 6, 9, 10, 14, 15, 16, 18, 29, 30, -1]
#         self.valid_classes = [
#             7,
#             8,
#             11,
#             12,
#             13,
#             17,
#             19,
#             20,
#             21,
#             22,
#             23,
#             24,
#             25,
#             26,
#             27,
#             28,
#             31,
#             32,
#             33,
#         ]
#         self.class_map = dict(zip(self.valid_classes, range(19)))
#         self.ignore_index = 250
#
#     def __getitem__(self, index):
#         img_path = self.files[self.split][index]
#         img = Image.open(img_path).convert('RGB')
#
#         lbl_basename = pathlib.Path(self.annotations_base) / os.path.basename(os.path.dirname(img_path))
#         lbl_path = lbl_basename / (os.path.basename(img_path)[:-15] + "gtFine_labelIds.png")
#         lbl_gt = lbl_basename / (os.path.basename(img_path)[:-15] + "gtFine_color.png")
#         mask = Image.open(lbl_path)
#         gt = Image.open(lbl_gt).convert('RGB')
#
#         if self.split == 'test' or (self.split == 'val' and self.mode == 'semseg'):
#             gt = transforms.Resize(size=self.input_res, interpolation=Image.NEAREST)(gt)
#             gt = self._img_transform(gt)
#             gt = transforms.ToTensor()(gt)
#
#             img, mask = self.resize_transform(img, mask)
#             img, mask = self._img_transform(img), self._mask_transform(mask)
#             img = self.transform(img) if self.transform is not None else img
#             # img = self.target_transform(img) if self.target_transform is not None else img
#
#         elif self.split == 'train' or self.split == 'val':
#             gt = transforms.ToTensor()(gt)
#             img, mask = self.sync_transform(img, mask)
#             img = self.transform(img) if self.transform is not None else img
#
#         return img, mask, gt
#
#     def sync_transform(self,
#                        img,
#                        mask):
#         """
#         Augmentations to perform simultaneously to the images and its corresponding label
#         """
#         # Resize
#         img, mask = self.resize_transform(img, mask)
#
#         # random crop
#         # if not self.encoder_only:
#         #     rcrop = transforms.RandomCrop(self.input_res)
#         #
#         #     img = rcrop(img)
#         #     mask = rcrop(mask)
#
#         # Random horizontal flip
#         hflip = transforms.RandomHorizontalFlip()
#         img = hflip(img)
#         mask = hflip(mask)
#
#         # final transform
#         img, mask = self._img_transform(img), self._mask_transform(mask)
#
#         return img, mask
#
#     def resize_transform(self, img, mask):
#         """
#         Resize image to desired input res for the model
#         """
#         resize_img = transforms.Resize(self.input_res)
#         if self.encoder_only:
#             input_res = self.train_encoder()
#             resize_mask = transforms.Resize(input_res, interpolation=transforms.InterpolationMode.NEAREST)
#         else:
#             resize_mask = transforms.Resize(self.input_res, interpolation=Image.NEAREST)
#
#         img = resize_img(img)
#         mask = resize_mask(mask)
#
#         return img, mask
#
#     def train_encoder(self):
#         input_width = int(self.input_res[0] / 8)
#         input_height = int(self.input_res[1] / 8)
#
#         return [input_width, input_height]
#
#     def _img_transform(self, img):
#         return np.array(img)
#
#     def _mask_transform(self, mask):
#         target = self.encode_segmap(np.array(mask).astype('int32'))
#
#         return torch.LongTensor(np.array(target).astype('int32'))
#
#     def __len__(self):
#         return len(self.files[self.split])
#
    # @property
    # def num_class(self):
    #     """Number of categories."""
    #     return self.NUM_CLASS
#
#     # @property
#     # def pred_offset(self):
#     #     return 0
#
#     def encode_segmap(self, mask):
#         # Put all void classes to zero
#         for _voidc in self.void_classes:
#             mask[mask == _voidc] = self.ignore_index
#         for _validc in self.valid_classes:
#             mask[mask == _validc] = self.class_map[_validc]
#         return mask
#
    # def load_data(self,
    #               batch_size=1,
    #               num_workers=0,
    #               shuffle=False,
    #               sampler=None):
    #     """
    #     Load the dataset to be ready for use
    #     """
    #     return torch.utils.data.DataLoader(self,
    #                                        batch_size=batch_size,
    #                                        num_workers=num_workers,
    #                                        shuffle=shuffle,
    #                                        sampler=sampler)

class CityscapesLoader(data.Dataset):
    """
    Load Cityscapes dataset for training and testing

    Dataset augmentations performed in a synchronised manner here.
    """
    def __init__(self,
                 root,
                 split="train",
                 is_transform=True,
                 input_res=(1024, 2048),
                 augments=None,
                 test_mode=False,
                 **kwargs):
        """
        :param root: path to cityscapes folder
        :param input_res: desired output res of the images and targets
        :param split: dataset split
        :param is_transform: tag whether or not to apply transformations (normalisation and resizing)
        :param augments: dictionary containing the desired augmentations
        """
        self.root = pathlib.Path(root)
        self.split = split
        self.is_transform = is_transform
        self.augments = augments
        self.n_classes = 19
        self.input_res = tuple(input_res)
        self.test_mode = test_mode

        self.images_base = self.root / "leftImg8bit" / self.split
        self.annotations_base = self.root / "gtFine" / self.split
        self.files = {}
        self.files[self.split] = utils.recursive_glob(rootdir=self.images_base, suffix="*.png")

        self.void_classes = [0, 1, 2, 3, 4, 5, 6, 9, 10, 14, 15, 16, 18, 29, 30, -1]
        self.valid_classes = [
            7,
            8,
            11,
            12,
            13,
            17,
            19,
            20,
            21,
            22,
            23,
            24,
            25,
            26,
            27,
            28,
            31,
            32,
            33,
        ]
        self.class_names = [
            "unlabelled",
            "road",
            "sidewalk",
            "building",
            "wall",
            "fence",
            "pole",
            "traffic_light",
            "traffic_sign",
            "vegetation",
            "terrain",
            "sky",
            "person",
            "rider",
            "car",
            "truck",
            "bus",
            "train",
            "motorcycle",
            "bicycle",
        ]

        self.ignore_index = 250
        self.class_map = dict(zip(self.valid_classes, range(19)))

    def __len__(self):
        """
        __len__
        """
        return len(self.files[self.split])

    def __getitem__(self,
                    index):
        """
        __getitem__
        """
        # Load image
        img_path = self.files[self.split][index]
        img = Image.open(img_path).convert('RGB')
        img = np.array(img, dtype=np.uint8)

        # Load label
        lbl_basename = pathlib.Path(self.annotations_base) / os.path.basename(os.path.dirname(img_path))
        lbl_path = lbl_basename / (os.path.basename(img_path)[:-15] + "gtFine_labelIds.png")
        mask = Image.open(lbl_path)
        mask = self.encode_segmap(np.array(mask, dtype=np.uint8))

        # Load label (colour map)
        lbl_gt = lbl_basename / (os.path.basename(img_path)[:-15] + "gtFine_color.png")
        gt = Image.open(lbl_gt).convert('RGB')
        if not self.test_mode:
           gt = transforms.Resize(size=self.input_res, interpolation=transforms.InterpolationMode.NEAREST)(gt)
        gt = transforms.ToTensor()(np.array(gt))

        # Conduct augmentations on the image and label (hflip and rcrop), usually None for the validation and test set
        if self.augments is not None:
            img, mask = self.augments(img, mask)

        # Conduct transformations on the image and label (resizing, normalisation and toTensor)
        if self.is_transform:
            img, mask = self.transform(img, mask)

        return img, mask, gt

    def transform(self,
                  img,
                  lbl):
        """
        Conduct the transformation on the image and labels
        """
        # img = Image.fromarray(img).resize((self.input_res[1], self.input_res[0]))  # uint8 with RGB mode

        mean = [0.5, 0.5, 0.5]
        std = [0.5, 0.5, 0.5]
        norm = transforms.Normalize(mean=mean,
                                    std=std)
        to_tensor = transforms.ToTensor()

        classes = np.unique(lbl)
        lbl = lbl.astype(float)

        if not self.test_mode:
            lbl = np.array(Image.fromarray(lbl).resize((self.input_res[1], self.input_res[0]), resample=Image.NEAREST))
            img = Image.fromarray(img).resize((self.input_res[1], self.input_res[0]))  # uint8 with RGB mode
        # else:
        #     lbl = np.array(Image.fromarray(lbl))

        lbl = lbl.astype(int)

        # if not np.all(classes == np.unique(lbl)):
        #     print("number of unique classes before resize", classes)
        #     print("number of unique classes after resize", np.unique(lbl))
        #     print("WARN: resizing labels yielded fewer classes")
        #
        # if not np.all(np.unique(lbl[lbl != self.ignore_index]) < self.n_classes):
        #     print("after det", classes, np.unique(lbl))
        #     raise ValueError("Segmentation map contained invalid class values")

        img = to_tensor(img)
        img = norm(img)
        lbl = torch.from_numpy(lbl).long()

        return img, lbl

    def encode_segmap(self,
                      mask):
        """
        Put all void classes to zero (or 250)
        """
        for _voidc in self.void_classes:
            mask[mask == _voidc] = self.ignore_index

        for _validc in self.valid_classes:
            mask[mask == _validc] = self.class_map[_validc]

        return mask

    @property
    def num_class(self):
        """
        Number of categories
        """
        return self.NUM_CLASS

    def load_data(self,
                  batch_size=1,
                  num_workers=0,
                  shuffle=False,
                  sampler=None):
        """
        Load the dataset to be ready for use
        """
        return torch.utils.data.DataLoader(self,
                                           batch_size=batch_size,
                                           num_workers=num_workers,
                                           shuffle=shuffle,
                                           sampler=sampler)


import urllib.request
import datetime
import argparse
from bs4 import BeautifulSoup
from ics import Calendar, Event

WEBSITE = "https://spaceflightnow.com/launch-schedule/"
MONTHES = {"Jan": 1, "Feb": 2, "March": 3, "April": 4, "May": 5, "June": 6,
           "July": 7, "Aug": 8, "Sept": 9, "Oct": 10, "Nov": 11, "Dec": 12}

CURRENT_YEAR = datetime.datetime.today().year
CURRENT_MONTH = datetime.datetime.today().month

def get_year(datename):
    year = CURRENT_YEAR
    month = get_month(datename)
    if not month:
        return None
    if month < CURRENT_MONTH:
        year += 1

    return year

def get_month(datename):
    date = datename.find('span', attrs={'class': 'launchdate'}).text
    for possible_month in MONTHES:
        if possible_month in date:
            return MONTHES[possible_month]

    #print(date, "has no valid month")
    return None

def get_day(datename, missiondata):
    date = datename.find('span', attrs={'class': 'launchdate'}).text
    day = "".join([letter for letter in date if (letter.isdigit() or letter == "/")])
    if not day:
        return None

    # Now we get the time of the launch
    timetype, _ = missiondata.findAll("span")
    launchtime = timetype.next_sibling

    # If the launch is on different days in different time zones find the day that is on GMT
    if "/" in day:
        day = launchtime.split("GMT on")[1].split("(")[0]
        day = "".join([l for l in day if l.isdigit()])

    day = int(day)
    if day > 31:
        return None

    return day

def get_mission(datename):
    mission = datename.find('span', attrs={'class': 'mission'})
    mission = mission.text.replace(" • ", ": ")
    return mission

def get_location(missiondata):
    _, location = missiondata.findAll("span")
    location = location.next_sibling
    while location[0] == " ":
        location = location[1::]
    return location

def get_launchtime(missiondata):
    # If the time is not yet set, make the event all day
    timetype, _ = missiondata.findAll("span")
    launchtime = timetype.next_sibling

    if "TBD" in launchtime or "GMT" not in launchtime:
        #print(launchtime, "is not a valid time stamp")
        return 0, 0, 0, True

    launchtime = launchtime.split("GMT")[0].replace(" ", "").replace(":", "")

    launchstart = launchtime.split("-")[0]

    if len(launchstart) == 4:
        launchstart += "00"
    if not launchstart.isdigit():
        #print(launchstart, "is not a valid time stamp")
        return 0, 0, 0, True

    assert len(launchstart) == 6

    hour = launchstart[0:2]
    minute = launchstart[2:4]
    second = launchstart[4:6]

    return int(hour), int(minute), int(second), False

def make_ics_timestamp(year, month, day, hour, minute):
    timestamp = str(year)
    timestamp += str(month).zfill(2)
    timestamp += str(day).zfill(2)
    timestamp += "T"
    timestamp += str(hour).zfill(2)
    #timestamp += ":"
    timestamp += str(minute).zfill(2)
    #timestamp += ":"
    #timestamp += str(second).zfill(2)
    return timestamp

def parse_website():
    page = urllib.request.urlopen(WEBSITE)
    soup = BeautifulSoup(page, 'html5lib')

    datename = soup.findAll('div', attrs={'class': 'datename'})
    missiondata = soup.findAll('div', attrs={'class': 'missiondata'})
    missisondescription = soup.findAll('div', attrs={'class': 'missdescrip'})

    return datename, missiondata, missisondescription

def get_full_launchtime(datename, missiondata):
    year = get_year(datename)
    month = get_month(datename)
    day = get_day(datename, missiondata)
    hour, minute, _, is_all_day = get_launchtime(missiondata)

    if not None in [year, month, day]:
        begin = make_ics_timestamp(year, month, day, hour, minute)
    else:
        begin = None

    return begin, is_all_day

def main():
    parser = argparse.Argparser = argparse.ArgumentParser(
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument("-o", "--output-filename", dest="outfile",
                        required=True, type=str)
    args = parser.parse_args()
    run(args.outfile)

def run(output_file):
    calendar = Calendar()
    datenames, missiondatas, descriptions = parse_website()

    for datename, data, desc in zip(datenames, missiondatas, descriptions):
        mission = get_mission(datename)
        location = get_location(data)
        begin, is_all_day = get_full_launchtime(datename, data)

        if begin:
            event = Event()
            event.begin = begin
            event.description = desc.text
            event.name = mission
            if is_all_day:
                event.make_all_day()
            event.location = location
            event.uid = mission.replace(" ", "")

            calendar.events.add(event)

    with open(output_file, "w") as outfile:
        outfile.writelines(calendar)

    print(calendar.events)
    print(len(calendar.events))

if __name__ == "__main__":
    main()


from pyfiglet import Figlet
import sys

figlet = Figlet()

fonts = figlet.getFonts()
#check for 0 command line arguments
if len(sys.argv) == 1:
   print('Invalid usage')

#check for only 2 command line arguments
elif len(sys.argv) == 3:

   #check for '-f' or '-font' as first command line argument
   if sys.argv[1] == '-f' or sys.argv[1] == '-font':
      #check for a font as second command line argument
      if sys.argv[2] in fonts:
         figlet.setFont(font = sys.argv[2])
         s = input('Input: ')
         print('Output: ' + figlet.renderText(s))

      #if second command line argument is not a font
      else:
         sys.exit('Invalid usage')

   else:
      sys.exit('Invalid usage')

else:
   sys.exit('Invalid usage')


# Uses Selenium to start the Chrome browser in the background,
# displays the last 56 arrests in Sampson county,
# searches for the perp's name, then displays a dialog if found.

import sys, os
import selenium as se
from selenium 			    import webdriver
from selenium.common.exceptions     import TimeoutException
from selenium.webdriver.common.keys import Keys

# Case Sensitive
criminal_name = 'Firstname Lastname'
arrests_records_url = 'https://northcarolina.arrests.org/index.php?county=862&page=1&results=56'

# Run via the Chrome browser in background
# Running headless also prevents from being blacklisted
options = se.webdriver.ChromeOptions()
options.add_argument('headless')
driver = se.webdriver.Chrome(chrome_options=options)
driver.get(arrests_records_url)

# Get the html source substring of just the arrests
try:
	html_source = driver.page_source
	html_source = html_source.encode('utf-8')
	html_source = html_source[html_source.index('search-results'):html_source.index('arrests-ribbon')]
except:
	print('Error: Couldn\'t get the HTML source')
	quit()

# Search for the name of our perp
if criminal_name in html_source:
    print('Found, displaying dialog')
    os.system('open ./ALERT.app')
else:
    print('No results for ' + criminal_name)


import xlrd, json

with open('alumnosIntercambio.json', 'w') as outfile:
    book = xlrd.open_workbook("VacantesIntercambioFeb-16.xls")
    sh = book.sheet_by_index(0)
    carreras = {"Industrial":"ing-industrial", u"Electr\xf3nica":"ing-electronica", u"Inform\u00e1tica":"ing-informatica", u"An\u00e1lisis de Sistemas": "lic-sistemas", "Naval":"ing-naval", u"Qu\u00edmica":"ing-quimica", u"Mec\u00e1nica":"ing-mecanica",  u"Industrial - Mec\xe1nica":"ing-industrial", "Civil":"ing-civil"}
    outfile.write("[")
    for rx in range(1, sh.nrows):
        name = sh.cell_value(rowx=rx, colx=3).encode('utf-8')
        lastName = sh.cell_value(rowx=rx, colx=2).encode('utf-8')
        title = name + " " + lastName
        universidad = sh.cell_value(rowx=rx, colx=0)
        carrera = carreras[sh.cell_value(rowx=rx, colx=1)]
        mail = sh.cell_value(rowx=rx, colx=5)
        data = json.dumps({"title": title, "universidad": universidad, "description": carrera + "; " + mail, "carrera": carrera}, separators=(',',':'))
        outfile.write(data)
        if(rx != sh.nrows-1):
            outfile.write(",")

    outfile.write("]")


import pandas as pd
from PIL import Image
from io import BytesIO
import numpy as np
from abc import ABC, abstractmethod
from datetime import (datetime,
                    timedelta)
from typing import (List,
                    Tuple,
                    Union)

class DataOperations(ABC):
    _heart_rate: Union[List, None] = None
    _data: Union[List, None] = None

    @property
    @abstractmethod
    def data(self): ...

    @data.setter
    @abstractmethod
    def data(self): ...

    def is_data_none_wrapper(func):
        def __is_data_none(self, *args, **kwargs) -> Union[None, NameError]:
            if self.data is None:
                raise NameError(f'data not found. You must set the data before calling the {func.__name__} function')
            return func(self, *args, **kwargs)
        return __is_data_none

    def is_heart_rate_none_wrapper(func):
        def __is_heart_rate_none(self, *args, **kwargs) -> Union[None, NameError]:
            if self.heart_rate is None:
                raise NameError(f'heart_rate data not found. You must call the get_heart_rate function before calling the {func.__name__} function')
            return func(self, *args, **kwargs)
        return __is_heart_rate_none

    @property
    @is_data_none_wrapper
    def heart_rate(self) -> List:
        # sourcery skip: inline-immediately-returned-variable, list-comprehension
        self._heart_rate = []
        for d in self.data:
            if d['type'] == 7:
                self._heart_rate.append(
                    {
                        'rate': float(d['samplePoints'][0]['value']),
                        'time': (datetime.fromtimestamp(
                                    int(str(d['samplePoints'][0]['endTime'])[:10])
                            ) + timedelta(
                                    hours = int(self.data[0]['timeZone'][1:].replace('0', ''))
                                )
                        )
                    }
                )
        return self._heart_rate

    @is_heart_rate_none_wrapper
    def get_heart_rate_for_all_days_as_axis(self) -> Tuple:
        heart_rate = pd.DataFrame(self._heart_rate)
        return (
            list (
                map(
                    lambda t: t.strftime("%d-%m-%Y %X"),
                    heart_rate['time']
                )
            ),
            heart_rate['rate']
        )

    @is_heart_rate_none_wrapper
    def get_average_heart_rate_for_days_as_axis(self) -> Tuple:
        heart_rate_grouped = pd.DataFrame(
            list(
                map(
                    lambda t: {'rate': t['rate'], 'date': t['time'].strftime("%d-%m-%Y")}, self._heart_rate
                )
            )
        ).groupby('date', sort=False).mean()['rate']

        return (heart_rate_grouped.keys(), heart_rate_grouped.values)

    @is_heart_rate_none_wrapper
    def get_heart_rate_for_one_day(self, day) -> Tuple:
        heart_rate = pd.DataFrame(
            list(
                filter(
                    lambda t: t['time'].strftime("%d-%m-%Y") == day, self._heart_rate
                )
            )
        )

        return (
            list (
                map(
                    lambda t: t.strftime("%d-%m-%Y %X"),
                    heart_rate['time']
                )
            ),
            heart_rate['rate']
        )

    def get_averages_of_heart_rates(self, y: pd.Series, average_number: int) -> Tuple:
        heart_rate2 = []
        rates = list(y)

        for i in range(0, len(rates), average_number):
            t = rates[i:i + average_number]
            heart_rate2.append(sum(t) / len(t))

        return (range(len(heart_rate2)), heart_rate2)

    @classmethod
    def __array_to_bytes_image(cls, img_array: np.array) -> bytes:
        buf = BytesIO()
        Image.fromarray(img_array, 'RGB').save(buf, format="PNG")
        return buf.getvalue()

    @staticmethod
    def get_heart_rate_as_img(rates: List) -> bytes:
        rates_len = len(rates)
        old_max = max(rates)
        old_min = min(rates)
        new_max = 255
        new_min = 0

        old_range = (old_max - old_min)
        new_range = (new_max - new_min)

        for max_n in range(rates_len):
            if max_n * max_n > rates_len: break

        img_array = np.full((max_n, max_n, 3), [0, 255, 0], dtype=np.uint8)

        x, y = 0, 0
        for n in rates:
            color = (255 - (((n - old_min) * new_range) / old_range) + new_min)
            img_array[x][y] = [color, color, color]
            if y < max_n-1: y += 1
            else:
                x += 1
                y = 0

        return DataOperations.__array_to_bytes_image(img_array)


import subprocess as sp
import os
import numpy as np
from pathlib import Path
import xml.etree.ElementTree as ET
import sys
import tempfile
import shutil
import time
import glob
from tempfile import TemporaryDirectory
from distutils.dir_util import copy_tree
import pytest
import re

driver_models = [
    "lj",
    "sg",
    "harm",
    "harm3d",
    "morse",
    "zundel",
    "qtip4pf",
    "pswater",
    "eckart",
    "ch4hcbe",
    "ljpolymer",
    "doublewell",
    "doublewell_1D",
    "MB",
]

def get_driver_info(
    example_folder,
    driver_info_file="driver.txt",
    driver="gas",
    socket_mode="unix",
    port_number=33333,
    address_name="localhost",
    flags=[],
):
    """This function looks for the existence of .driver_info file
    to run the example with a meaningfull driver. If the file doesn't
    exist, the driver gas is assigned."""
    try:
        with open(Path(example_folder) / driver_info_file) as f:
            flags = list()
            ncount = 0
            while True:
                line = f.readline()
                if not line:
                    break
                elif "driver" in line:
                    driver = line.split()[1]
                elif "socket_mode" in line:
                    socket_mode = line.split()[1]
                elif "port" in line:
                    port_number = line.split()[1]
                elif "address" in line:
                    address_name = line.split()[1]
                elif "flags" in line:
                    flags.append({line.split()[1]: line.split()[2:]})
                ncount += 1
            if ncount == 0:
                raise ValueError("driver.txt is empty")
    except FileNotFoundError:
        raise FileNotFoundError(
            "({}) An input.xml file was found but a driver.txt not".format(
                example_folder
            )
        )
    except:
        pass

    if driver not in driver_models:
        driver = "gas"

    driver_info = {
        "model": driver,
        "socket_mode": socket_mode,
        "port_number": port_number,
        "address_name": address_name,
        "flag": flags,
    }

    return driver_info

def get_info_test(parent):
    """This function recursively searches for examples
    and checks for the presence of the required additional info
    required (i.e. driver.txt)
    """
    folders = [x[0] for x in os.walk(parent)]
    reg_tests = list()

    for ff in folders:
        if os.path.isfile(Path(ff) / "input.xml"):
            try:
                driver_info = get_driver_info(ff)
                reg_tests.append([ff, driver_info])
            except FileNotFoundError:
                raise FileNotFoundError(
                    "({}) An input.xml file was found but a driver.txt not".format(ff)
                )
            except ValueError:
                raise ValueError(
                    'Please specify "model address      port mode" inside driver.txt'
                )

    return reg_tests

class Runner(object):
    """This class handles the creation of tmp directories,
    the i-pi call, the driver call, and finally
    it checks that the generated outputs are the expected ones.
    """

    def __init__(
        self,
        parent,
        call_ipi="i-pi input.xml",
        call_driver="i-pi-driver",
        check_errors=True,
        check_numpy_output=True,
        check_xyz_output=True,
    ):
        """Store parent directory and commands to call i-pi and driver
        call_ipi: command to call i-pi
        call_driver: list of commands to call drivers
        """

        self.parent = parent
        self.call_ipi = call_ipi
        self.call_driver = call_driver
        self.check_error = check_errors
        self.check_numpy_output = check_numpy_output
        self.check_xyz_output = check_xyz_output

    def _run(self, info, nid):
        """This function tries to run the example in a tmp folder and
        checks if ipi has ended without error.
        After that the output is checked against a reference
        arguments:
            cwd: folder where all the original regression tests are stored
        """

        cwd = info[0]
        self.files = []
        self.forms = []

        try:
            # Create temp file and copy files
            self.tmp_dir = Path(tempfile.mkdtemp())
            print("temp folder: {}".format(self.tmp_dir))

            files = os.listdir(self.parent / cwd)
            for f in files:
                shutil.copy(self.parent / cwd / f, self.tmp_dir)
            driver_info = get_driver_info(self.tmp_dir)

            # Creating the clients list
            input_name = self.tmp_dir / "input.xml"
            output_name = self.tmp_dir / "input.xml"
            try:
                tree = ET.parse(input_name)
            except:
                print("The error is in the format or the tags of the xml!")
            root = tree.getroot()

            clients = list()

            for s, ffsocket in enumerate(root.findall("ffsocket")):
                name = ffsocket.attrib["name"]
                mode = driver_info["socket_mode"]
                ffsocket.attrib["mode"] = mode

                for element in ffsocket:
                    port = driver_info["port_number"]
                    if element.tag == "port":
                        element.text = str(port)
                    elif element.tag == "address":
                        dd = driver_info["address_name"] + "_" + str(nid) + "_" + str(s)
                        element.text = dd
                        address = dd

                model = driver_info["model"]
                clients.append([model, address, port, mode])

                for flag in driver_info["flag"]:
                    for k, v in flag.items():
                        clients[s].append(k)
                        clients[s].extend(v)

            tree.write(open(output_name, "wb"))

            # Run i-pi
            ipi = sp.Popen(
                self.call_ipi,
                cwd=(self.tmp_dir),
                shell=True,
                stdout=sp.PIPE,
                stderr=sp.PIPE,
            )
            time.sleep(2)

            with open(self.tmp_dir / "files_to_check.txt") as f:
                lines = f.readlines()
            for nl, line in enumerate(lines):
                if nl > 1:
                    self.files.append(line.split()[0])
                    self.forms.append(line.split()[1])

            # Run drivers by defining cmd2 which will be called, eventually
            flag_indeces = list()

            driver = list()
            cmd2 = list()
            for client in clients:
                if client[3] == "unix":
                    clientcall = self.call_driver + " -m {} -h {} -u ".format(
                        client[0], client[1]
                    )
                    cmd2.append(clientcall)
                elif client[3] == "inet":
                    cmd2.append(
                        self.call_driver
                        + " -m {} -h {} -p {}".format(client[0], client[1], client[2])
                    )
                else:
                    raise ValueError("Driver mode has to be either unix or inet")

                cmd = cmd2[0]
                if any("-" in str(s) for s in client):
                    flag_indeces = [
                        i for i, elem in enumerate(client) if "-" in str(elem)
                    ]
                    for i, ll in enumerate(flag_indeces):
                        if i < len(flag_indeces) - 1:
                            cmd2 += "{} {}".format(
                                client[ll],
                                ",".join(client[ll + 1 : flag_indeces[i + 1]][:]),
                            )
                        else:
                            cmd2.append(
                                " {} {}".format(
                                    client[ll], ",".join(client[ll + 1 :][:])
                                )
                            )
                    cmd += cmd2[1]
                driver.append(
                    sp.call(cmd, cwd=(cwd), shell=True, stdout=sp.PIPE, stderr=sp.PIPE)
                )

        except sp.TimeoutExpired:
            raise RuntimeError(
                "Time is out. Aborted during {} test. \
              Error {}".format(
                    str(cwd), ipi.communicate(timeout=2)[0]
                )
            )

        except FileNotFoundError:
            raise ("{}".format(str(cwd)))

        except ValueError:
            raise ("{}".format(str(cwd)))
        try:
            for filename in glob.glob("/tmp/ipi_*"):
                os.remove(filename)
        except:
            pass

        if self.check_error:
            self._check_error(ipi)
        if self.check_numpy_output:
            self._check_numpy_output(cwd)
        if self.check_xyz_output:
            self._check_xyz_output(cwd)

    def _check_error(self, ipi):
        """ This function checks if ipi has exited with errors"""

        ipi_error = ipi.communicate(timeout=120)[1].decode("ascii")
        if ipi_error != "":
            print("IPI ERROR OCCURED: {}".format(ipi_error))
        assert "" == ipi_error

    def _check_numpy_output(self, cwd):
        """This function checks if the numpy-accessible datafiles are 'all_close' to the
        reference file provided

        The checked 'numpy-accessible' files are collected in the files_to_check.txt
        where there filetype is specified, too.
        """

        for ii, refname in enumerate(self.files):
            if self.forms[ii] == "numpy":
                try:
                    ref_output = np.loadtxt(Path(cwd) / refname)
                except IOError:
                    raise IOError(
                        'Please provide a reference properties output named "{}"'.format(
                            refname
                        )
                    )
                except ValueError:
                    raise ValueError(
                        "Please check ref_simulation.out in {}".format(str(self.parent))
                    )

                fname = refname[4:]
                test_output = np.loadtxt(self.tmp_dir / fname)

                try:
                    np.testing.assert_allclose(
                        test_output, ref_output, rtol=1.0e-7, atol=1.0e-15
                    )
                    print("No anomaly during the regtest for {}".format(refname))
                except AssertionError:
                    raise AssertionError(
                        "ANOMALY: Disagreement between reference and {} in {}".format(
                            fname, str(self.parent)
                        )
                    )

    def _check_xyz_output(self, cwd):
        """This function checks if the ref_simulation.XXXXX.xyz files are 'all_close'
        to the reference file provided.

        The checked .xyz files are collected in the files_to_check.txt where there filetype is
        also specified.

        Common options for XXXXX and the tags they can be generated with:
            pos_c = <trajectory filename='pos_c' stride='1' > x_centroid </trajectory>
            frc_c = <trajectory filename='frc_c' stride='1' > f_centroid </trajectory>
            vel_c = <trajectory filename='vel_c' stride='1' > v_centroid </trajectory>
            mom_c = <trajectory filename='mom_c' stride='1' > p_centroid </trajectory>

        In case of a multi-bead simulations, please check of the positions and
        forces on the first bead, too.
            pos_0 = <trajectory filename='pos' stride='1' bead='0'> positions </trajectory>
            frc_0 = <trajectory filename='frc' stride='1' bead='0'> forces </trajectory>
            vel_0 = <trajectory filename='vel' stride='1' bead='0'> velocities </trajectory>
            mom_0 = <trajectory filename='mom' stride='1' bead='0'> momenta </trajectory>

        """

        for ii, refname in enumerate(self.files):
            if self.forms[ii] == "xyz":
                ref_structs = []
                try:
                    with open(Path(cwd) / refname) as ref:
                        ref_natoms = int(ref.readline())
                        for s, ll in enumerate(ref.readlines()):
                            if (s + 1) % (ref_natoms + 2) != 0 and (s + 1) % (
                                ref_natoms + 2
                            ) != 1:
                                ref_structs.append(ll.split()[1:])
                    reff = [[float(v) for v in r] for r in ref_structs]
                    ref_xyz = np.array(reff)
                except IOError:
                    raise IOError(
                        "Please provide a reference file named {} in {}".format(
                            refname, str(self.parent / cwd)
                        )
                    )

                except ValueError:
                    raise ValueError(
                        "Please check the values for the file named {} in {}".format(
                            refname, str(self.parent / cwd)
                        )
                    )

                fname = refname[4:]
                structs = []
                with open(self.tmp_dir / fname) as f:
                    natoms = int(f.readline())
                    for s, ll in enumerate(f.readlines()):
                        if (s + 1) % (natoms + 2) != 0 and (s + 1) % (natoms + 2) != 1:
                            structs.append(ll.split()[1:])
                    testt = [[float(v) for v in r] for r in structs]
                    test_xyz = np.array(testt)

                try:
                    np.testing.assert_allclose(
                        test_xyz, ref_xyz, rtol=1.0e-7, atol=1.0e-8
                    )
                    print("No anomaly during the regtest for {}".format(refname))
                except AssertionError:
                    raise AssertionError(
                        "ANOMALY: Disagreement between reference and {} in {}".format(
                            fname, str(self.parent / cwd)
                        )
                    )


import cv2, os
import numpy as np
from numpy.lib.function_base import unwrap

def drawCube(img_rgb, rvecs, tvecs, mtx, dist, cube_color = (255, 0, 0), tag_size = 1 ,is_centered = True):
	# Define the ar cube

	if is_centered == True:
		# It is important to note that the center of the marker corresponds to the origin and we must therefore move 0.5 away from the origin
		axis = np.float32([[-0.5, -0.5, 0], [-0.5, 0.5, 0], [0.5, 0.5, 0], [0.5, -0.5, 0],
							[-0.5, -0.5, 1], [-0.5, 0.5, 1], [0.5, 0.5, 1],[0.5, -0.5, 1]])
	else:
		axis = np.float32([[0, 0, 0], [0, 1, 0], [1, 1, 0], [1, 0, 0],
						[0, 0, 1], [0, 1, 1], [1, 1, 1],[1, 0, 1]])

	axis = axis * tag_size # scale the axis to match the tag size

	# Now we transform the cube to the marker position and project the resulting points into 2d
	imgpts, jac = cv2.projectPoints(axis, rvecs, tvecs, mtx, dist)
	imgpts = np.int32(imgpts).reshape(-1, 2)

	# Now comes the drawing.
	# In this example, I would like to draw the cube so that the walls also get a painted
	# First create six copies of the original picture (for each side of the cube one)
	side1 = img_rgb.copy()
	side2 = img_rgb.copy()
	side3 = img_rgb.copy()
	side4 = img_rgb.copy()
	side5 = img_rgb.copy()
	side6 = img_rgb.copy()

	# Draw the bottom side (over the marker)
	side1 = cv2.drawContours(side1, [imgpts[:4]], -1, cube_color, -2)
	# Draw the top side (opposite of the marker)
	side2 = cv2.drawContours(side2, [imgpts[4:]], -1, cube_color, -2)
	# Draw the right side vertical to the marker
	side3 = cv2.drawContours(side3, [np.array(
		[imgpts[0], imgpts[1], imgpts[5],
			imgpts[4]])], -1, cube_color, -2)
	# Draw the left side vertical to the marker
	side4 = cv2.drawContours(side4, [np.array(
		[imgpts[2], imgpts[3], imgpts[7],
			imgpts[6]])], -1, cube_color, -2)
	# Draw the front side vertical to the marker
	side5 = cv2.drawContours(side5, [np.array(
		[imgpts[1], imgpts[2], imgpts[6],
			imgpts[5]])], -1, cube_color, -2)
	# Draw the back side vertical to the marker
	side6 = cv2.drawContours(side6, [np.array(
		[imgpts[0], imgpts[3], imgpts[7],
			imgpts[4]])], -1, cube_color, -2)

	# Until here the walls of the cube are drawn in and can be merged
	img_rgb = cv2.addWeighted(side1, 0.1, img_rgb, 0.9, 0)
	img_rgb = cv2.addWeighted(side2, 0.1, img_rgb, 0.9, 0)
	img_rgb = cv2.addWeighted(side3, 0.1, img_rgb, 0.9, 0)
	img_rgb = cv2.addWeighted(side4, 0.1, img_rgb, 0.9, 0)
	img_rgb = cv2.addWeighted(side5, 0.1, img_rgb, 0.9, 0)
	img_rgb = cv2.addWeighted(side6, 0.1, img_rgb, 0.9, 0)

	# Now the edges of the cube are drawn thicker and stronger
	img_rgb = cv2.drawContours(img_rgb, [imgpts[:4]], -1, cube_color, 2)
	for i, j in zip(range(4), range(4, 8)):
		img_rgb = cv2.line(img_rgb, tuple(imgpts[i]), tuple(imgpts[j]), cube_color, 2)
	img_rgb = cv2.drawContours(img_rgb, [imgpts[4:]], -1, cube_color, 2)

	return img_rgb

def draw_cube_list(img_rgb, detection_list, calib_mtx, dist_coef, tag_size =1, cube_color = (125,125,125), is_centered = True):

	for detection in detection_list:
		img_rgb = drawCube(img_rgb, detection['rvec'], detection['tvec'], calib_mtx, dist_coef, cube_color, tag_size = tag_size, is_centered = True)

	return img_rgb

def calibrator():
	import cv2
	import numpy as np
	import pyrealsense2 as rs

	# Defines the path to save the calibration file and the dictonary used
	device_name = input("Please enter the device name :")
	name = device_name + "_" + get_time() + ".npz"
	dictionary = cv2.aruco.DICT_APRILTAG_36h10

	# Initialize communication with intel realsense
	pipeline = rs.pipeline()
	realsense_cfg = rs.config()
	realsense_cfg.enable_stream(rs.stream.color, 1920, 1080, rs.format.rgb8, 30)
	pipeline.start(realsense_cfg)

	# Check communication
	print("Test data source...")
	try:
		np.asanyarray(pipeline.wait_for_frames().get_color_frame().get_data())
	except:
		raise Exception("Can't get rgb frame from data source")

	# Define what the calibration board looks like (same as the pdf)
	board = cv2.aruco.CharucoBoard_create(6, 6, 0.16/6, 0.16/6*0.8, cv2.aruco.Dictionary_get(dictionary))
	record_count = 0
	# Create two arrays to store the recorded corners and ids
	all_corners = []
	all_ids = []

	print("Start recording [1/4]")
	print("1. Move the grid from calibration directory a little bit in front of the camera and press [r] to make a record (if auto record is not set to True)")
	print("2. Finish this task and start calculation press [c]")
	print("3. Interrupt application [ESC]")
	while True:
		# Get frame from realsense and convert to grayscale image
		frames = pipeline.wait_for_frames()
		img_rgb = np.asanyarray(frames.get_color_frame().get_data())
		img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_RGB2GRAY)

		# Detect markers on the gray image
		res = cv2.aruco.detectMarkers(img_gray, cv2.aruco.getPredefinedDictionary(dictionary))
		# Draw the detected markers
		cv2.aruco.drawDetectedMarkers(img_rgb, res[0], res[1])
		# Display the result
		display_image = cv2.resize(img_rgb, (960, 540))
		cv2.imshow("AR-Example", cv2.cvtColor(display_image, cv2.COLOR_RGB2BGR))

		key = cv2.waitKey(10)
		# If 18 markers have been found, allow recording
		if len(res[0]) == 18:
			# Interpolate the corners of the markers
			res2 = cv2.aruco.interpolateCornersCharuco(res[0], res[1], img_gray, board)
			# Add the detected interpolated corners and the marker ids to the arrays if the user press [r] and the interpolation is valid
			if key == ord('r') and res2[1] is not None and res2[2] is not None and len(res2[1]) > 18:
				all_corners.append(res2[1])
				all_ids.append(res2[2])
				record_count += 1
				print("Record: " + str(record_count))
		# If [c] pressed, start the calculation
		if key == ord('c'):
			# Close all cv2 windows
			cv2.destroyAllWindows()
			# Check if recordings have been made
			if(record_count != 0):
				print("Calculate calibration [2/4] --> Use "+str(record_count)+" records"),
				# Calculate the camera calibration
				ret, mtx, dist, rvecs, tvecs = cv2.aruco.calibrateCameraCharuco(all_corners, all_ids, board, img_gray.shape, None, None)
				print("Save calibration [3/4]")
				# Save the calibration information into a file
				np.savez_compressed(getCalibPath(name), ret=ret, mtx=mtx, dist=dist, rvecs=rvecs, tvecs=tvecs)
				print("Done [4/4]")
			else:
				print("Interrupted since there are no records...")
			break
		# If [ESC] pressed, close the application
		if key == 27:
			print("Application closed without calculation")
			# Close all cv2 windows
			cv2.destroyAllWindows()
			break

def getCalibPath(filename):
	# Defines the path of the calibration file and the dictonary used
	return os.path.join(os.path.split(os.path.dirname(__file__))[0], 'calibration','calibfiles', filename)

def get_test_path(filename):
	# Defines the path of the calibration file and the dictonary used
	return os.path.join(os.path.split(os.path.dirname(__file__))[0], 'testresults', filename)

def displayCalibMat(filename, showmtx= True, showDist=True):
	# Defines the path of the calibration file and the dictonary used
	calibration_path = os.path.join(os.path.split(os.path.dirname(__file__))[0], 'calibration','calibfiles', filename)

	# Load calibration from file
	mtx = None
	dist = None
	with np.load(calibration_path) as X:
		mtx, dist, _, _ = [X[i] for i in ('mtx', 'dist', 'rvecs', 'tvecs')]

	if showmtx==True:print('Calibration Matrix : \n' , mtx,'\n')
	if showDist==True:print('Distortion Parameters : \n' ,dist,'\n')

def getCalibData(filename):
	# Defines the path of the calibration file and the dictonary used
	calibration_path = os.path.join(os.path.split(os.path.dirname(__file__))[0], 'calibration','calibfiles', filename)

	# Load calibration from file
	mtx = None
	dist = None
	with np.load(calibration_path) as X:
		mtx, dist = [X[i] for i in ('mtx', 'dist')]

	print("\nThe file used for the camera calibration :", calibration_path)

	return mtx, dist

def angle_error(v1, v2):
	# v1 is your firsr vector
	# v2 is your second vector
    return np.arccos(np.dot(v1, v2) / (np.linalg.norm(v1) * np.linalg.norm(v2)))

def angle_error_rowwise(A, B):
	A, B = unwrap(A), unwrap(B)
	p1 = np.einsum('ij,ij->i',A,B)
	p2 = np.einsum('ij,ij->i',A,A)
	p3 = np.einsum('ij,ij->i',B,B)
	p4 = p1 / np.sqrt(p2*p3)
	return np.arccos(np.clip(p4,-1.0,1.0))

def distance_matrix(A, B, squared=False):
    """
    Compute all pairwise distances between vectors in A and B.

    Parameters
    ----------
    A : np.array
        shape should be (M, K)
    B : np.array
        shape should be (N, K)

    Returns
    -------
    D : np.array
        A matrix D of shape (M, N).  Each entry in D i,j represnets the
        distance between row i in A and row j in B.

    See also
    --------
    A more generalized version of the distance matrix is available from
    scipy (https://www.scipy.org) using scipy.spatial.distance_matrix,
    which also gives a choice for p-norm.
    """
    M = A.shape[0]
    N = B.shape[0]

    assert A.shape[1] == B.shape[1], f"The number of components for vectors in A \
        {A.shape[1]} does not match that of B {B.shape[1]}!"

    A_dots = (A*A).sum(axis=1).reshape((M,1))*np.ones(shape=(1,N))
    B_dots = (B*B).sum(axis=1)*np.ones(shape=(M,1))
    D_squared =  A_dots + B_dots -2*A.dot(B.T)

    if squared == False:
        zero_mask = np.less(D_squared, 0.0)
        D_squared[zero_mask] = 0.0
        return np.sqrt(D_squared)

    return D_squared

def elementwise_distance(A, B, squared = False):
	"""
	Compute all elementwise distances between vectors in A and B.

	Parameters
	----------
	A : np.array
		shape should be (M, K)
	B : np.array
		shape should be (M, K)

	Returns
	-------
	D : np.array
		A matrix D of shape (M, 1).  Each entry in D i represnets the
		distance between row i in A and row i in B.

	"""

	M = A.shape[0]
	N = B.shape[0]

	assert A.shape[0] == B.shape[0], f"The number of components for vectors in A \
		{A.shape[1]} does not match that of B {B.shape[1]}!"

	try:
		D_squared = np.sum(np.square(A - B), axis = 1)
	except:
		D_squared = np.sum(np.square(A - B), axis = 0)

	if squared == False:
		return np.sqrt(D_squared)

	return D_squared

def moving_average(old_mean, new_data, n_of_frames):

	return old_mean + 1 / n_of_frames * (new_data - old_mean)

def get_time():
	import time
	return time.strftime("%d%m%Y_%H%M%S")

def get_process_memory():
	import psutil, os

	process = psutil.Process(os.getpid())
	mem_info = process.memory_info()
	return mem_info.rss + mem_info.vms + mem_info.shared  # in bytes

def user_prompt(question: str) -> bool:
    """ Prompt the yes/no-*question* to the user. """
    from distutils.util import strtobool

    while True:
        user_input = input(question + " [y/n]: ")
        try:
            return bool(strtobool(user_input))
        except ValueError:
            print("Please use y/n or yes/no.\n")

class GetImages():
	def __init__(self, is_camera = True, dataset_name = None):
		if is_camera:
			import pyrealsense2 as rs

			self.is_camera = True

			# Initialize communication with intel realsense
			self.pipeline = rs.pipeline()
			realsense_cfg = rs.config()
			realsense_cfg.enable_stream(rs.stream.color, 1920, 1080, rs.format.rgb8, 30)
			self.pipeline.start(realsense_cfg)

			# Check communication
			print("Testing the connection with the camera...")
			try:
				np.asanyarray(self.pipeline.wait_for_frames().get_color_frame().get_data())
			except:
				raise Exception("Can't get rgb frame from camera")

			print("Connection with the camera is succesful!")

		else:
			self.is_camera = False   #use dataset
			self.dataset_folder_path = os.path.join(os.path.split(os.path.dirname(__file__))[0], 'datasets', dataset_name)
			file_list = os.listdir(self.dataset_folder_path)
			print("Path of the dataset to be used :", self.dataset_folder_path)
			self.filtered_file_list = [k for k in file_list if ".png" in k]
			self.i = 0

	def get_image(self):
		if self.is_camera:
			frames = self.pipeline.wait_for_frames()
			img_rgb = np.asanyarray(frames.get_color_frame().get_data())

			return img_rgb

		else:
			if self.i >= len(self.filtered_file_list):
				raise Exception("No more image left in the dataset!")
			else:
				img_rgb = cv2.imread(os.path.join(self.dataset_folder_path, self.filtered_file_list[self.i]))
				self.i += 1
				return img_rgb

def record_dataset(folder_name, n_of_image = 100, file_name = None):
	import pathlib

	if file_name == None : file_name = folder_name
	file_id_padding = 5

	dataset_folder_path = os.path.join(os.path.split(os.path.dirname(__file__))[0], 'datasets', folder_name)
	pathlib.Path(dataset_folder_path).mkdir(parents=True, exist_ok=True)

	get_image = GetImages(is_camera = True)

	for i in range(n_of_image):
		img_rgb = get_image.get_image()
		cv2.imwrite(os.path.join(dataset_folder_path, file_name + "_{:>0{}}.png".format(i, file_id_padding)), img_rgb)

def display_dataset(folder_name, n):
	get_image = GetImages(False, folder_name)

	for i in range(n):
		cv2.imshow(folder_name, cv2.cvtColor(get_image.get_image(), cv2.COLOR_RGB2BGR))
		cv2.waitKey(0)
	cv2.destroyAllWindows()

def image_saver(folder_name = None, wait_time = 5):
	import time, pathlib

	if folder_name == None: folder_name = input("What should the folder name for the images to be saved ? : ")
	get_image = GetImages(is_camera=True)

	image_folder_path = os.path.join(os.path.split(os.path.dirname(__file__))[0], 'calibration', 'calibration_images', folder_name)
	pathlib.Path(image_folder_path).mkdir(parents=True, exist_ok=True)
	print("Images will be saved to :", image_folder_path)

	img_counter = 0
	start_time = time.time()

	print("Press [ESC] to close the application")

	while True:
		img_rgb = get_image.get_image()
		display_image = cv2.resize(img_rgb, (960, 540))

		remaining_time = str(wait_time - (time.time() - start_time))[0] + ' second(s) left'
		info = str(img_counter) + ' image(s) captured'

		#--- Position the time at (10, 70) coordinate with certain font style, size and color ---
		cv2.putText(display_image, remaining_time + " | " + info , (10,70), cv2.FONT_HERSHEY_SIMPLEX, 1.5,(15,200,15),2,cv2.LINE_AA)
		cv2.imshow("Image Saver", cv2.cvtColor(display_image, cv2.COLOR_RGB2BGR))

		if time.time() - start_time >= wait_time: # Check if 5 wait time is passed
			img_name = "image_{:>0{}}.png".format(img_counter, 3)

			cv2.imwrite(os.path.join(image_folder_path, img_name), cv2.cvtColor(img_rgb, cv2.COLOR_RGB2BGR))
			print("{} written!".format(img_name))
			start_time = time.time()
			img_counter += 1

		# If [ESC] pressed, close the application
		if cv2.waitKey(100) == 27:
			print("Application closed")
			break
	cv2.destroyAllWindows()

def unwrap(rvecs):
	"""Make sures that angels are in range of -pi and pi"""
	return (rvecs + np.pi) % (2 * np.pi) - np.pi

def find_ambiguity(ids, rvecs, tvecs):

	if ids is None: return []

	n = ids.size
	flipped_ids = []

	for i in range(n):
		R = cv2.Rodrigues(rvecs[i])[0]

		if R[1,2] > 0:
			flipped_ids.append(int(ids[i]))

	return flipped_ids

def boxplot_data(data):
	if data.size == 0 : return None
	data_dict = {}
	data_dict['median'] = np.median(data)
	data_dict['average'] = np.average(data)
	data_dict['upper_quartile'] = np.percentile(data, 75)
	data_dict['lower_quartile'] = np.percentile(data, 25)
	data_dict['max'] = np.max(data)
	data_dict['min'] = np.min(data)

	return data_dict


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('sfotipy', '0004_auto_20151118_1318'),
    ]

    operations = [
        migrations.AlterField(
            model_name='album',
            name='cover',
            field=models.ImageField(default=b'coverdefault.png', upload_to=b'sfotipy'),
        ),
    ]


import PySimpleGUI as sg
import coordinates as co

def open_gui(input_val: str = "", scale: float = 8.0, message: str = "") -> list:
    layout = [
        [sg.Text("Coordinates (DMS): \t"), sg.In(size=(25, 1), enable_events=True, key="-COORDINATES-", default_text=input_val)],
        [sg.Text("Size (m): \t\t"), sg.In(size=(25, 1), enable_events=True, key="-SCALE-", default_text=scale)],
        [sg.Text(message)],
        [sg.Button("Enter"), sg.Button("Exit")]
    ]
    window = sg.Window("3D Map Viewer", layout)
    while True:
        event, values = window.read()
        if event == "Enter" or event == "Exit":
            last_state = event
            break
        if event == sg.WIN_CLOSED:
            last_state = "Exit"
            break
        if event == "-COORDINATES-":
            input_val = values["-COORDINATES-"]
        if event == "-SCALE-":
            scale_input = values["-SCALE-"]
            if co.is_decimal(scale_input):
                scale = float(scale_input)
    window.close()
    if last_state == "Exit":
        return None
    x, y = co.convert_coords_to_decimals(input_val.split())
    return [x, y, scale, input_val, last_state]


from keras import backend as K

def earth_movers_distance(y_true, y_pred):
    cdf_true = K.cumsum(y_true, axis=-1)
    cdf_pred = K.cumsum(y_pred, axis=-1)
    emd = K.sqrt(K.mean(K.square(cdf_true - cdf_pred), axis=-1))
    return K.mean(emd)

def age_mae(y_true, y_pred):
    true_age = K.sum(y_true * K.arange(0, 100, dtype="float32"), axis=-1)
    pred_age = K.sum(y_pred * K.arange(0, 100, dtype="float32"), axis=-1)
    mae = K.mean(K.abs(true_age - pred_age))
    return mae


# Jason Sanders & Bradley White
# CSCI 466: Networks
# Battleship HTTP: Client
# September 19, 2016
#
# This python module sends fire messages as POST requests to play Battleship over HTTP
# The program takes four arguments and should be invoked every time a fire message needs to be sent:
# server IP address, server port, x_coordinate, y_coordinate

import argparse
import urllib.parse
import http.client
import Board

# Parse command line arguments for server IP, port and fire coordinates
def parseArgs():
    # Start a new argument parser
    parser = argparse.ArgumentParser()

    # Add the four arguments
    parser.add_argument("ip")
    parser.add_argument("port", type=int)
    parser.add_argument("x_coordinate", type=int)
    parser.add_argument("y_coordinate", type=int)
    args = parser.parse_args()

    # Save the arguments into new variables
    server_address = args.ip
    port = args.port
    coordinates = (args.x_coordinate, args.y_coordinate)
    return (server_address, port, coordinates)

# Opens connection with the server and sends a fire message as a POST request
def openConnection(arguments):
    # Connect to the server at the IP and the port specified
    server_address = (arguments[0], arguments[1])
    connection = http.client.HTTPConnection(arguments[0], arguments[1])
    print('Connecting to IP: {} on port: {}'.format(*server_address))

    # Parse the coordinates into a proper POST request
    params = urllib.parse.urlencode({'x': arguments[2][0], 'y': arguments[2][1]})
    # Create the header
    headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
    # Send the POST request with parameters and header to the server
    connection.request("POST", "", params, headers)
    # Wait for the response
    r2 = connection.getresponse()
    # Print the status code and message or type of error to terminal
    print(r2.status, r2.reason)
    # Close the connection
    connection.close()
    # If the fire message was legitimate, updated the opponent_board with the results
    if r2.status is 200:
        coord = (arguments[2][0], arguments[2][1])
        # Split the message into a list
        msg2 = r2.reason.split('=')
        # Send the coordinates and a 1 or 0 for hit or miss to Board for updates
        Board.update_eboard(coord, msg2[1])

# Invokes the other functions
def main():
    # Pass terminal arguments for opening the connection
    arguments = parseArgs()
    openConnection(arguments)

# Start!
main()


# coding: utf-8
# creates table-like custom objects from the items in CSV files
# and connects path for CSV to be read or written to/from
import csv
from pathlib import Path

"""The purpose of this program is to analyze loan data and
determine the fair price of the loans to ensure loans are bought
at the best possible value.

This program is organized into five parts;
Part 1: Automation of Calculations
Part 2: Analysis of Loan Data
Part 3: Financial Calculations
Part 4: Conditionally filters lists of loans
Part 5: Save results to CSV file

"""

"""Part 1 - Automation of Calculations"""

# list of loan prices
loan_costs = [500, 600, 200, 1000, 450]

# tells us how many loan prices are inside of the loan_costs list, then includes them in a statement
print("The number of loans is:", len(loan_costs))

# adds the values in the loan_costs list then includes them in a statement
print("The total value of the loans:$",sum(loan_costs))

# averages the loan cost values from previous "loan_cost" list data then includes in a stament
print("The average loan amount is:$", (sum(loan_costs) / len(loan_costs)))

""" Part 2: Analysis of Loan Data

*** Using a dictionary of additional loan information, we can determine fair price using the following
formula;

Present Value = Future Value / (1 + Discount_Rate/12) ** remaining_months

    @NOTE
        ***Future value:  the amount of money the borrower must pay back upon maturity of the
        loan (also known as the face value).

        ***Remaining months: the remaining maturity (in months) before the loan needs to be fully repaid.

        ***Fair Value: the loan is worth at least the cost to buy it.

        *** Discount Rate- 20%

"""

# dictionary of loan data

loan = {
    "loan_price": 500,
    "remaining_months": 9,
    "repayment_interval": "bullet",
    "future_value": 1000,
}

# uses get() function to extract the Future Value on the loan

print("Future Value:$", loan.get("future_value"))

# uses get() function to extract the remaining amount of months on the loan

print("Remaining Months:", loan.get("remaining_months"))

# Calculates fair value using **monthly** present value formula with 20% discount rate value

discount_rate = 0.2

present_value = loan.get("future_value") / (1 + discount_rate/12) ** loan.get("remaining_months")

# print(present_value)- **kept as test tool

# determines present value using .get function which pulls data from loan_price list
# if the loan data is less than or equal to present value it prints a statement confirming
# its a good buy, otherwise it will say the loan is not a good buy

if present_value >= loan.get("loan_price"):
    print("The loan is worth at least the cost to buy it.")

else :
    print("The loan is too expensive and not worth the price")

""" Part 3: Financial Calculations - This function can be used with any newly uploaded loan data """

# new list of loan data
new_loan = {
    "loan_price": 800,
    "remaining_months": 12,
    "repayment_interval": "bullet",
    "future_value": 1000,
}

#   Using calculate_present_value as a calculating function we give paramaters of `future_value`, `remaining_months`,
#   and the `annual_discount_rate`

#   This function gives us the `present_value` for the loan.

def calculate_present_value(future_value, remaining_months, annual_discount_rate):
    discount_rate = annual_discount_rate
    present_value = loan.get("future_value") / (1 + discount_rate/12) ** loan.get("remaining_months")
    return present_value

# This function recalculates the present value with new data using an annual discount rate of 20%

calculate_present_value(new_loan.get("future_value"),new_loan.get("remaining_months"),0.2)

# displays present value loan amount with rounding for two decimal places

print(f"The present value of the loan is:$ {round(present_value, 2)}")

"""Part 4: Conditionally filters lists of loans.

This is will filter your lists of loan prices/values based on your specifications and
take the desired data and put it into a new list for clean reading.

"""
# list of dictionaries containing loan data
loans = [
    {
        "loan_price": 700,
        "remaining_months": 9,
        "repayment_interval": "monthly",
        "future_value": 1000,
    },
    {
        "loan_price": 500,
        "remaining_months": 13,
        "repayment_interval": "bullet",
        "future_value": 1000,
    },
    {
        "loan_price": 200,
        "remaining_months": 16,
        "repayment_interval": "bullet",
        "future_value": 1000,
    },
    {
        "loan_price": 900,
        "remaining_months": 16,
        "repayment_interval": "bullet",
        "future_value": 1000,
    },
]

# empty list created to receive data that will be filtered
inexpensive_loans = []

# Loops through loan data in 'loans' list with condition of $500 or less, the
# values which meet that condition will then be uploaded into a new list called 'inexpensive_loans'

for loan in loans:
    loanPrice = loan.get("loan_price")
    if loanPrice <= 500:
        inexpensive_loans.append(loan)

# shows newly generated list with filtered results

print("The following loans are considered inexpensive:",[inexpensive_loans])

"""Part 5: Save the results.

Saves(writes) newly generated data to a CSV file"""

# Sets the output header
header = ["loan_price", "remaining_months", "repayment_interval", "future_value"]

# Sets the output file path, added r for absolute file path raw, otherwise unicode error appears

output_path = Path(r"C:\Users\brien\Desktop\FinTech_Workspace\Challenges\loan_analyzer.csv")

# loop for CSV files to be written, using 'header' variable that was created and uploading
# it to out_path (where you would like the CSV saved)

with open(output_path,'w', newline='') as output_path:
    csvwriter = csv.writer(output_path)
    csvwriter.writerow(header)
    for row in inexpensive_loans:
      csvwriter.writerow(row.values())

# You should now have a new CSV file saved in your specified folder (output_path)


import sqlite3
from typing import List
from audio_converter import AudioFile

class Database:
    def __init__(self, db_path: str = "audio_conversions.db"):
        self.db_path = db_path
        self.connection = sqlite3.connect(self.db_path)
        self.cursor = self.connection.cursor()
        self.create_table()

    def create_table(self):
        self.cursor.execute("""
            CREATE TABLE IF NOT EXISTS conversions (
                file_path TEXT,
                format TEXT,
                text TEXT
            )
        """)

    def save_conversion(self, audio_file: AudioFile):
        self.cursor.execute("""
            INSERT INTO conversions (file_path, format, text) VALUES (?, ?, ?)
        """, (audio_file.file_path, audio_file.format, audio_file.text))
        self.connection.commit()

    def get_previous_conversions(self) -> List[AudioFile]:
        self.cursor.execute("""
            SELECT * FROM conversions
        """)
        rows = self.cursor.fetchall()
        audio_files = [AudioFile(row[0], row[1], row[2]) for row in rows]
        return audio_files

    def close(self):
        self.connection.close()


from functools import lru_cache
from glob import glob
from pathlib import Path
from typing import Dict, List, Text, Tuple

import aep.tools.libs.data

def get_bundles(args) -> List[Text]:
    bundle_list = []
    for bundle_type in ("incident", "threatactors"):
        for bundle in glob(f"{args.data_dir}/{bundle_type}/*"):
            bundle_list.append(Path(bundle_type) / Path(bundle).name)

    return sorted(bundle_list)

@lru_cache()
def read_promise_description_file(filename: Path) -> List:
    return aep.tools.libs.data.read_promise_description_file(open(Path(filename)))

@lru_cache()
def read_technique_promises(
    tech_promises_file: Path, promise_descriptions_file: Path, conditions_file: Path
) -> Tuple[Dict, Dict, bool]:

    return aep.tools.libs.data.read_technique_promises(
        tech_promises_file, promise_descriptions_file, conditions_file
    )


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

import json
import argparse
from raw_data_preprocess_transfertransfo import *
from pathlib import Path

# set default path for data and test data
project_dir = Path(__file__).resolve().parent
datasets_dir = project_dir.joinpath('./data/')
images_test_dir = project_dir.joinpath('./online_test_data/images_test/')

def preprocessing_ctx(ctx_list):
    """预处理上下文text, 删除空行"""
    clean_ctx_list = []
    flag = False  # 标志上一轮是否为空(Q或者A)
    for single_ctx in ctx_list:
        if not single_ctx[2:]:
            # 当前轮为空
            flag = True
            continue
        if flag:
            # 直接将当前轮数据与clean_ctx_list[-1]拼接(他们是同一个人说的)
            clean_ctx_list[-1] = clean_ctx_list[-1] + "|||" + single_ctx[2:]
            flag = False
        else:
            # 原始数据不为空，直接不做任何处理
            clean_ctx_list.append(single_ctx)

    if len(clean_ctx_list) == 1 and clean_ctx_list[0][0] == 'Q':
        clean_ctx_list.append("A:~")  # 人为的多＋一个波浪线answer(主要是为了不丢弃这条Q)

    return clean_ctx_list

def gen_test_set(args):
    sess_length = args.sess_turns * 2
    with open(os.path.join(args.directory + 'test_questions.json'), mode='r', encoding='utf-8') as f:
        items = json.load(f)

    if args.kb_f:
        product_info = ProductInfo(args.kb_f)

    datasets = {}
    # all conversations
    conversations = []
    for item in items:
        # single conversation 所有数据
        conversation = {}
        utterances = []
        single_data = {}

        img_list = list()
        src_str = ''
        # ctx_str = ''
        # qus_str = ''

        ques_id = item['Id']
        session_id = item['SessionId']
        p_id = item["ProductId"]
        # ctx_list = item['Context']
        origin_ctx_list = item['Context']
        ques = item['Question']

        ctx_list = preprocessing_ctx(origin_ctx_list)
        ctx_str_list = []
        for idx, sent_i in enumerate(ctx_list):
            if sent_i == '':
                continue
            sent_i_type = sent_i[0]  # 说话人角色
            if (len(ctx_list) % 2 == 1
                    and sent_i_type == "A"
                    and idx == 0):
                # 历史轮数为奇数, 且第一轮为bot表达
                continue
            sent_i = sent_i[2:].strip()
            sent_i_list = sent_i.split('|||')

            single_ctx_str = str()
            for sent_j in sent_i_list:
                # 清理过长空格
                sent_j = ' '.join(sent_j.strip().split())
                # fir_ans = (idx <= 1 and sent_i_type == "A" and len(item['Context']) == len(ctx_list))
                # if match_robot(sent_j, fir_ans):
                #     continue
                if len(sent_j) > MAX_LEN:
                    # 超长截取
                    sent_j = sent_j[:MAX_LEN]

                if sent_j.endswith('.jpg'):
                    img_list.append(sent_j)
                    sent_j = '<img>'
                else:
                    img_list.append('NULL')
                    sent_j = clean_text(sent_j)
                if args.seg_voc:
                    sent_seg = ' '.join(tokenize_spt(sent_j.strip()))
                else:
                    # bert预训练模型不需要分词
                    sent_seg = sent_j.strip()

                if sent_seg:
                    src_str = src_str + sent_seg + '</s>'
                    single_ctx_str = single_ctx_str + sent_seg + ' '
                else:
                    img_list.pop(-1)
            ctx_str_list.append(single_ctx_str)

        assert len(ctx_str_list) % 2 == 0, "非2的倍数"
        ques_list = ques.split('|||')
        qus_str = str()
        for sent in ques_list:
            # 清理过长空格
            sent = ' '.join(sent.strip().split())
            if len(sent) > MAX_LEN:
                # 超长截取
                sent = sent[:MAX_LEN]

            if sent.endswith('.jpg'):
                img_list.append(sent)
                sent = '<img>'
            else:
                img_list.append('NULL')
                sent = clean_text(sent)

            if sent:
                if args.seg_voc:
                    sent_seg = ' '.join(tokenize_spt(sent.strip()))
                else:
                    sent_seg = sent.strip()
                src_str = src_str + sent_seg + '</s>'
                qus_str = qus_str + sent_seg + ' '  # 行与行之间使用空格隔开
            else:
                img_list.pop(-1)
        ctx_str_list.append(qus_str)

        if src_str.endswith("</s>"):
            src_str = src_str[:-4]
        img_str = ' '.join(img_list)
        src_list = src_str.split('</s>')
        assert len(src_list) == len(img_list)
        # lazy to reuse train dataloader code
        candidates = []
        trg_str = 'NULL'
        candidates.append(trg_str)
        # 收集单条数据
        single_data['candidates'] = candidates
        single_data['history'] = ctx_str_list
        single_data['img_list'] = img_list
        single_data['sid'] = session_id
        # 单条数据的集合
        utterances.append(single_data)

        pid_str = None
        if args.kb_f:
            # 获取PID info
            pid_str = product_info.get_info_by_pid(p_id)
            if args.seg_voc:
                # 分词
                seg_pid_str = pid_str
                single_seg_pid = tokenize_spt(seg_pid_str.strip())
                pid_str = str()  # 置空重新赋值
                pid_str = ' '.join(single_seg_pid)

        conversation['personality'] = pid_str
        conversation['utterances'] = utterances
        # 将conversation数据存入列表
        conversations.append(conversation)
    # 将测试数据加入datasets
    datasets['test'] = conversations

    save_info_to_json(os.path.join(datasets_dir, args.out_json_name), datasets)

if __name__ == '__main__':
    parser = argparse.ArgumentParser(description="tool to process online test data")

    parser.add_argument('-d', '--directory', default='online_test_data/')
    parser.add_argument('-t', '--sess_turns', default=2)
    parser.add_argument('-out', '--out_json_name', default='test_datasets.json')
    parser.add_argument('-f', '--kb_f', default='./data/kb_info.json')
    parser.add_argument('--seg_voc', action='store_true', default=True, help='是否分词')

    args = parser.parse_args()

    # get the online test file to generate internal data format
    gen_test_set(args)


from typing import Iterator

import requests
from base import LotSpaces, Scraper

class AshevilleScraper(Scraper):
    API_URLS = [
        "https://s3.amazonaws.com/asheville-parking-decks/spaces.json",
        "https://s3.amazonaws.com/bc-parking-decks/164College",
        "https://s3.amazonaws.com/bc-parking-decks/40Coxe",
    ]
    TIMEOUT = 5

    name = "asheville"

    def fetch_spaces(self) -> Iterator[LotSpaces]:
        for api_url in self.API_URLS:
            response = requests.get(api_url, timeout=self.TIMEOUT)
            response.raise_for_status()
            data = response.json()
            for deck in data["decks"]:
                yield LotSpaces(
                    lot=deck["name"],
                    spaces=int(deck["available"]),
                )


from celery.result import AsyncResult
from sqlalchemy.orm import Session

from danswer.background.celery.celery import sync_document_set_task
from danswer.background.utils import interval_run_job
from danswer.db.document_set import (
    fetch_document_sets,
)
from danswer.db.engine import get_sqlalchemy_engine
from danswer.utils.logger import setup_logger

logger = setup_logger()

_ExistingTaskCache: dict[int, AsyncResult] = {}

def _document_sync_loop() -> None:
    # cleanup tasks
    existing_tasks = list(_ExistingTaskCache.items())
    for document_set_id, task in existing_tasks:
        if task.ready():
            logger.info(
                f"Document set '{document_set_id}' is complete with status "
                f"{task.status}. Cleaning up."
            )
            del _ExistingTaskCache[document_set_id]

    # kick off new tasks
    with Session(get_sqlalchemy_engine()) as db_session:
        # check if any document sets are not synced
        document_set_info = fetch_document_sets(db_session=db_session)
        for document_set, _ in document_set_info:
            if not document_set.is_up_to_date:
                if document_set.id in _ExistingTaskCache:
                    logger.info(
                        f"Document set '{document_set.id}' is already syncing. Skipping."
                    )
                    continue

                logger.info(
                    f"Document set {document_set.id} is not synced. Syncing now!"
                )
                task = sync_document_set_task.apply_async(
                    kwargs=dict(document_set_id=document_set.id),
                )
                _ExistingTaskCache[document_set.id] = task

if __name__ == "__main__":
    interval_run_job(
        job=_document_sync_loop, delay=5, emit_job_start_log=False
    )  # run every 5 seconds


import pygame
import constants
from spritesheet_functions import SpriteSheet

class Player(pygame.sprite.Sprite):
        """ This class represents the player. """

        # Direction of the player
        direction = "R"
        speed_x = 0
        speed_y = 0
        walking_frames_l = []
        walking_frames_r = []
        walking_frames_u = []
        walking_frames_d = []

        def __init__(self):

                pygame.sprite.Sprite.__init__(self)
                sprite_sheet = SpriteSheet("Ninja.png")

                #This holds all the images for the animated walk

                #Load all the right facing images into a list
                image = sprite_sheet.get_image(14, 54, 24, 24)
                self.walking_frames_r.append(image)
                image = sprite_sheet.get_image(51, 56, 24, 24)
                self.walking_frames_r.append(image)
                image = sprite_sheet.get_image(86, 54, 24, 24)
                self.walking_frames_r.append(image)
                image = sprite_sheet.get_image(114, 57, 24, 24)
                self.walking_frames_r.append(image)

                #Load all the right facing images, tehn flip them(left)
                image = sprite_sheet.get_image(14, 54, 24, 24)
                image = pygame.transform.flip(image, True, False)
                self.walking_frames_l.append(image)
                image = sprite_sheet.get_image(51, 56, 24, 24)
                image = pygame.transform.flip(image, True, False)
                self.walking_frames_l.append(image)
                image = sprite_sheet.get_image(86, 54, 24, 24)
                image = pygame.transform.flip(image, True, False)
                self.walking_frames_l.append(image)
                image = sprite_sheet.get_image(114, 57, 24, 24)
                image = pygame.transform.flip(image, True, False)
                self.walking_frames_l.append(image)

                #Load UP facing images
                image = sprite_sheet.get_image(96, 118, 24, 24)
                self.walking_frames_u.append(image)
                image = sprite_sheet.get_image(123, 118, 24, 24)
                self.walking_frames_u.append(image)

                #Load Down facing images
                image = sprite_sheet.get_image(99, 90, 24, 24)
                self.walking_frames_d.append(image)

                #Set the image the player starts with
                self.image = self.walking_frames_r[0]

                #Set a referance to the image rect
                self.rect = self.image.get_rect()

        def update(self):
                self.rect.x += self.speed_x
                self.rect.y += self.speed_y
                posx = self.rect.x
                posu = self.rect.y
                if self.speed_x == 0 and self.speed_y == 0:
                        self.direction = "S"
                if self.direction == "R":
                        # Number 30 is the speed to change animations
                        frame = (posx // 30) % len(self.walking_frames_r)
                        self.image = self.walking_frames_r[frame]
                elif self.direction == "L":
                        frame = (posx // 30) % len(self.walking_frames_l)
                        self.image = self.walking_frames_l[frame]
                elif self.direction == "U":
                        frame = (posu // 30) % len(self.walking_frames_u)
                        self.image = self.walking_frames_u[frame]
                elif self.direction == "D":
                        frame = (posu // 30) % len(self.walking_frames_d)
                        self.image = self.walking_frames_d[frame]
                else:
                        self.image = self.walking_frames_r[0]

        #Player-controlled movement:
        def go_left(self):
                self.speed_x = -6
                self.direction = "L"

        def go_right(self):
                self.speed_x = 6
                self.direction = "R"

        def go_up(self):
                self.speed_y = -6
                self.direction = "U"

        def go_down(self):
                self.speed_y = 6
                self.direction = "D"

        def stopx(self):
                self.speed_x = 0

        def stopy(self):
                self.speed_y = 0


import pandas as pd
import requests
import json
from datetime import datetime
import os

def get_job_posted(headers):
    """

    """

    url = "https://jsearch.p.rapidapi.com/search"

    querystring = {"query":"Data Engineer and Data Analyst in UK and USA","page":"1","num_pages":"1"}

    response = requests.get(url, headers=headers, params=querystring)

    response_data = response.json()

    with open ('raw/raw_job_posted_data.json', 'w') as jobfile:
        json.dump(response_data, jobfile)
        print('data successfully written to a raw database file')

def transformed_raw_data():

    # the raw csv is read by a json load function
    with open('raw/raw_job_posted_data.json', 'r') as job_file:
        reader = json.load(job_file)
        data = reader['data']

    # reading the data to pandas dataframe, and the selected columns required are listed in the column_name
    column_names = ['employer_website', 'job_id', 'job_employment_type', 'job_title', 'job_apply_link', 'job_description',
                    'job_city', 'job_country', 'job_posted_at_timestamp','employer_company_type']

    job_df = pd.DataFrame(data, columns=column_names)

    #converting the timestamp vale to date
    job_date=pd.to_datetime(job_df['job_posted_at_timestamp'], unit='s')
    job_df['job_posted_at_timestamp'] = job_date
    job_df = job_df.rename(columns={"job_employment_type": "employment_type", "job_city": "City",
                            "job_country": "Country", "job_posted_at_timestamp": "job_posted_date", "employer_company_type": "Sector"})

    # write the transformed data to a csv file
    filepath = 'Transformed/cleaned_job_posted.csv'
    job_df.to_csv(filepath, index=False)
    print('clean date succesffuly write to the transformed staging area')

def load(engine):
    # read the file from the filepath to pandas dataframe
    filepath = 'Transformed/cleaned_job_posted.csv'
    df = pd.read_csv(filepath)
    df.to_sql('talent', con=engine)


# pylint: disable=line-too-long
"""Base class for subscribable objects"""
from abc import ABC, abstractmethod
import logging

_LOGGER = logging.getLogger(__name__)

class SubscriberBase(ABC):
    """Base class for subscribable objects"""

    def __init__(self):
        self._dirty_list: list[str] = []
        self._callbacks = []
        self._dirty = False

    @abstractmethod
    def debug_string(self) -> str:
        """Returns string to be logged in messages"""

    def attr_updater(self, data_set, attr: str, property_name: str = None) -> bool:
        """Updates an attribue"""
        if attr in data_set:
            attr_val = data_set[attr]
            if property_name is None:
                property_name = attr
            if getattr(self, property_name) != attr_val:
                setattr(self, property_name, attr_val)
                self._dirty = True
                if property_name not in self._dirty_list:
                    self._dirty_list.append(property_name)
                    _LOGGER.debug(
                        "update_attr: [%s] attr [%s] value [%s]", self.debug_string(), property_name, attr_val
                    )
                return True
        return False

    def register_on_update_callback(self, callbackfunc, match=None):
        """Register a callback for changes"""
        self._callbacks.append({"func": callbackfunc, "match": match})

    def execute_on_update_callbacks(self):
        """Execute callbacks"""
        if self._dirty:
            for callback in self._callbacks:
                callbackfunc = callback["func"]
                match = callback["match"]
                matches = False
                if match is None:
                    matches = True
                else:
                    for match_item in match:
                        if match_item in self._dirty_list:
                            matches = True
                            break
                try:
                    if matches:
                        callbackfunc()
                except Exception:  # pylint: disable=broad-exception-caught
                    # Log and eat this exception so we can process other callbacks
                    _LOGGER.exception("executeOnUpdateCallback - [%s] - failed", self.debug_string())
        self._dirty = False
        self._dirty_list = []


##########################################################
################### simulating NUV-DoA ###################
##########################################################

import torch
import math
import time

from NUV import NUV_DoA

from simulations import utils
from simulations import config
from data.Data_generation import DataGenerator

#### initialization ####
args = config.general_settings()
args.use_cuda = False
# GPU or CPU
if args.use_cuda:
   if torch.cuda.is_available():
      device = torch.device('cuda')
      print("Using GPU")
   else:
      raise Exception("No GPU found, please set args.use_cuda = False")
else:
    device = torch.device('cpu')
    print("Using CPU")
# number of samples
args.sample = 5
samples_run = args.sample

args.resol = 0.05 # resolution of spatial filter
args.q_init = 0.1 # initial guess of q
centers = torch.linspace(args.B1, args.B2, int((args.B2 - args.B1) / args.resol + 1)).to(device) # centers of all windows
center_num = len(centers) # number of centers
half_window_size = args.resol * 50 # each window covers the interval [center - half_window_size, center + half_window_size]
args.m_SF = int((half_window_size * 2)/args.resol)+1  # number of steering vectors of each SF window, here m_SF=101

#### Generate data ####
generator = DataGenerator(args)
# x_dire, x_true, y_train = generator.generate_experiment_data()
# torch.save([x_dire, x_true, y_train], 'data/DoA_m=360_k=3_sample=5.pt')
[x_dire, x_true, y_train] = torch.load('data/DoA_m=360_k=3_sample=5.pt', map_location=device)

y_mean = y_train.mean(dim=1) # generate y_mean by averaging l snapshots for each sample

A, A_H = generator.ULA_refine(centers, half_window_size) # generate dictionary matrices for each little window
A = A.to(device)
A_H = A_H.to(device)

# #### estimation ####
import gc
x_pred = torch.zeros(samples_run, center_num, dtype=torch.cfloat, device=device)
theta_tuning = torch.zeros(samples_run, args.k, dtype=torch.float, device=device)
MSE = torch.zeros(samples_run, dtype=torch.float, device=device)

for i in range(samples_run): #r=r_2 * 6000 when k=1
  print(i)
  print('true doa is {}'.format(x_dire[i]))
  r_tuning =  [20000] # [20000, 15000, 10000, 300, 100, 90]
  MSE_tuning = torch.zeros(len(r_tuning), dtype=torch.float, device=device)
  for r in range(len(r_tuning)):
    print('r_tuning = {}'.format(r_tuning[r]))
    # NUV-SSR + SF
    x_pred[i], iterations = NUV_DoA(args, center_num, A, A_H, y_mean[i, :, 0], r_tuning[r])
    # peak finding and convert to DoA
    peak_indices = utils.peak_finding(x_pred[i], args.k)
    theta_tuning[i] = utils.convert_to_doa(peak_indices, centers)
    print('predict doa = {}'.format(theta_tuning[i]))
    # permuted MSE
    MSE_tuning[r] = utils.permuted_mse(theta_tuning[i], x_dire[i])
    print('MSE_tuning = {}'.format(MSE_tuning[r]))
    gc.collect()
    if args.use_cuda:
        torch.cuda.empty_cache()

  MSE[i] = torch.min(MSE_tuning)
  print('MSE[i] = {}'.format(MSE[i]))
  print('-----------------------------------------')
  gc.collect()
  if args.use_cuda:
    torch.cuda.empty_cache()

mean_MSE = torch.mean(MSE)
MSE_dB = 10 * (torch.log10(mean_MSE))
print('averaged MSE in dB = {}'.format(MSE_dB))
MSE_linear = torch.sqrt(mean_MSE)
print('averaged RMSE in linear = {}'.format(MSE_linear))
print('--------------------------------------------')
SNR = 10*math.log10((args.x_var + args.mean_c) / args.r2)
print('SNR = {}'.format(SNR))

#### plotting ####
# import matplotlib.pyplot as plt
# import numpy as np
# p = 0

# fig,ax = plt.subplots(figsize=(6, 3))

#   # x_c = xx_pred[p]
#   # axs = axs.ravel()
# x_c = x_pred[p].cpu()
# y_c = np.linspace(args.B1, args.B2, len(x_c), endpoint=False)
#   # y_c = np.linspace(-0.5, 0.5, len(x_c), endpoint=False)
# ax.plot(y_c, x_c, marker = '*')
# # plt.plot(0, x_c[50], "o")
# # ax.set_title('true doa = {}'.format(x_dire[p] ))
#   # argrelextrema(np.array(x_c), np.greater)
# print('true doa = {}'.format(x_dire[p]))
# print('predicted doa = {}'.format(theta[p]))
# print('σ = 3r2 = 30')


#coding: utf-8
from django.core.management.base import BaseCommand
from django.db.models import get_models

class Command(BaseCommand):
    help = u"""
      ticket:9 Command that prints all project models and the
      count of objects in every model
    """
    args = ''
    requires_model_validation = True

    def handle(self, *args, **options):
        for m in get_models():
            data = "%s.%s:\t%i\n" % (m._meta.app_label,
                                                m._meta.module_name.capitalize(),
                                                m.objects.count())
            self.stdout.write(data)
            self.stderr.write("error: " + data)


from vedis import Vedis
import config

def get_current_price(user_id_price):
    with Vedis(config.db_file) as db:
        try:
            return db[user_id_price].decode()
        except KeyError:
            return config.States.User_Cost.value

def set_user_price(user_id_price, value):
    with Vedis(config.db_file) as db:
        try:
            db[user_id_price] = value
            return True
        except:
            # тут желательно как-то обработать ситуацию
            return False


# ch10/example6.py

from math import sqrt
import asyncio
from concurrent.futures import ProcessPoolExecutor
from timeit import default_timer as timer

#async def is_prime(x):
def is_prime(x):
    print('Processing %i...' % x)

    if x < 2:
        print('%i is not a prime number.' % x)

    elif x == 2:
        print('%i is a prime number.' % x)

    elif x % 2 == 0:
        print('%i is not a prime number.' % x)

    else:
        limit = int(sqrt(x)) + 1
        for i in range(3, limit, 2):
            if x % i == 0:
                print('%i is not a prime number.' % x)
                return

        print('%i is a prime number.' % x)

async def main():

    task1 = loop.run_in_executor(executor, is_prime, 9637529763296797)
    task2 = loop.run_in_executor(executor, is_prime, 427920331)
    task3 = loop.run_in_executor(executor, is_prime, 157)

    await asyncio.gather(*[task1, task2, task3])

if __name__ == '__main__':
    try:
        start = timer()

        executor = ProcessPoolExecutor(max_workers=3)
        loop = asyncio.get_event_loop()
        loop.run_until_complete(main())

        print('Took %.2f seconds.' % (timer() - start))

    except Exception as e:
        print('There was a problem:')
        print(str(e))

    finally:
        loop.close()