import numpy as np
import matplotlib.pyplot as plt
from math import sqrt, pi
# ~ import calcMiso
micdata = np.genfromtxt('MicrostructureText_Layer4.mic',skip_header=4)
grains = np.genfromtxt('GrainsLayer4.csv',skip_header=9)
nGrains,z = grains.shape
out=np.zeros((nGrains,8))
for grainNr in range(nGrains):
	grain = grains[grainNr,:]
	grCOM = grain[10:12]
	filtPts = micdata[micdata[:,0] == grainNr]
	nrPts,z = filtPts.shape
	avgPos = [np.mean(filtPts[:,3]),np.mean(filtPts[:,4])]
	out[grainNr,:] = np.array([grCOM[0],grCOM[1],avgPos[0],avgPos[1],sqrt((grCOM[0]-avgPos[0])**2+(grCOM[1]-avgPos[1])**2),grain[22],sqrt(nrPts*sqrt(3)/pi),grain[22]-sqrt(nrPts*sqrt(3)/pi)])

print(np.mean(out[:,4]))
plt.scatter(out[:,4],out[:,6])
plt.show()

fig,ax = plt.subplots(1,1)
ax.scatter(micdata[:,3],micdata[:,4],c=micdata[:,0],cmap=plt.get_cmap('gray'))
ax.scatter(grains[:,10],grains[:,11],s=grains[:,22],c=range(1,nGrains+1),cmap=plt.get_cmap('gray'))
ax.set_aspect('equal')
plt.show()


# -*- coding: utf-8 -*-
from testsIntegrationDB.context import instabotpatrik
from testsIntegrationDB.context import testcommon
import logging
import unittest
import pymongo
import instabotpatrik.repository
import instabotpatrik.model
import datetime
from testsIntegrationDB import common
import pytz
import freezegun

logging.getLogger().setLevel(30)
logging.basicConfig(format='[%(levelname)s] [%(asctime)s] [%(name)s:%(funcName)s] : %(message)s',
                    datefmt='%m/%d/%Y-%H:%M:%S')

class RepositoryTestCase(unittest.TestCase):
    def setUp(self):
        self.config = common.get_config()
        self.mongo_client = pymongo.MongoClient('localhost', 27017)
        self.mongo_client.drop_database(self.config.get_db_name())
        self.repository = common.create_repo(self.config, self.mongo_client)

    def tearDown(self):
        self.mongo_client.drop_database(self.config.get_db_name())

class ItShouldSaveAndLoadUpdateUser(RepositoryTestCase):

    @freezegun.freeze_time("2011-11-11 11:11:00", tz_offset=0)
    def test_run(self):
        instagram_id = "nn213b1jkbjk"
        user1 = instabotpatrik.model.InstagramUser(
            instagram_id=instagram_id,
            username="username1234xyz",
            user_detail=testcommon.factory.create_user_detail(count_followed_by=123)
        )
        self.repository.update_user(user1)
        user1_loaded = self.repository.find_user(instagram_id)

        self.assertEqual(user1.instagram_id, user1_loaded.instagram_id)
        self.assertEqual(user1.username, user1_loaded.username)

        self.assertEqual(user1.url, user1_loaded.url)
        self.assertEqual(user1.count_shared_media, user1_loaded.count_shared_media)
        self.assertEqual(user1.count_follows, user1_loaded.count_follows)
        self.assertEqual(user1.count_followed_by, user1_loaded.count_followed_by)
        self.assertEqual(user1.we_follow_user, user1_loaded.we_follow_user)
        self.assertEqual(user1.user_follows_us, user1_loaded.user_follows_us)

        self.assertEqual(None, user1_loaded.count_likes_we_gave)
        self.assertEqual(None, user1_loaded.dt_like)
        self.assertEqual(None, user1_loaded.dt_follow)
        self.assertEqual(None, user1_loaded.dt_unfollow)

        user1_loaded.register_like()
        user1_loaded.register_follow()
        self.repository.update_user(user1_loaded)

        user1_loaded2 = self.repository.find_user(instagram_id)

        self.assertEqual(user1_loaded2.instagram_id, user1.instagram_id)
        self.assertEqual(user1_loaded2.username, user1.username)

        self.assertEqual(user1.url, user1_loaded2.url)
        self.assertEqual(user1.count_shared_media, user1_loaded2.count_shared_media)
        self.assertEqual(user1.count_follows, user1_loaded2.count_follows)
        self.assertEqual(123, user1_loaded2.count_followed_by)
        self.assertEqual(user1.we_follow_user, user1_loaded2.we_follow_user)
        self.assertEqual(False, user1_loaded2.user_follows_us)

        self.assertEqual(1, user1_loaded2.count_likes_we_gave)
        self.assertEqual(datetime.datetime(2011, 11, 11, 11, 11, 0, tzinfo=pytz.UTC), user1_loaded2.dt_like)
        self.assertEqual(datetime.datetime(2011, 11, 11, 11, 11, 0, tzinfo=pytz.UTC), user1_loaded2.dt_follow)
        self.assertEqual(None, user1_loaded2.dt_unfollow)

class ItShouldSaveAndLoadUpdateMedia(RepositoryTestCase):

    @freezegun.freeze_time("2012-10-12 13:00:00", tz_offset=0)
    def test_run(self):
        instagram_id = "nn213b1jkbjk"
        media1 = instabotpatrik.model.InstagramMedia(
            instagram_id=instagram_id,
            shortcode="foobar42",
            owner_id="abcd1337",
            caption="awesome #cool",
            like_count=987,
            owner_username="user12",
            is_liked=False,
            time_liked=None
        )
        self.repository.update_media(media1)
        media1_loaded = self.repository.find_media_by_id(instagram_id)

        self.assertEqual(media1_loaded.instagram_id, media1.instagram_id)
        self.assertEqual(media1_loaded.shortcode, media1.shortcode)
        self.assertEqual(media1_loaded.owner_id, media1.owner_id)
        self.assertEqual(media1_loaded.caption, media1.caption)
        self.assertEqual(media1_loaded.is_liked, media1.is_liked)
        self.assertEqual(media1_loaded.like_count, media1.like_count)
        self.assertEqual(media1_loaded.time_liked, media1.time_liked)
        self.assertEqual(media1_loaded.owner_username, media1.owner_username)

        media1_loaded.add_like()
        self.repository.update_media(media1_loaded)
        media1_loaded2 = self.repository.find_media_by_id(instagram_id)

        self.assertEqual(media1_loaded2.instagram_id, media1.instagram_id)
        self.assertEqual(media1_loaded2.shortcode, media1.shortcode)
        self.assertEqual(media1_loaded2.owner_id, media1.owner_id)
        self.assertEqual(media1_loaded2.caption, media1.caption)
        self.assertEqual(media1_loaded2.is_liked, True)
        self.assertEqual(media1_loaded2.like_count, media1.like_count)
        self.assertEqual(media1_loaded2.time_liked, datetime.datetime(2012, 10, 12, 13, 0, 0, tzinfo=pytz.UTC))
        self.assertEqual(media1_loaded2.owner_username, media1.owner_username)

class ItShouldFindUserFollowedUsers(RepositoryTestCase):
    def runTest(self):
        user1 = instabotpatrik.model.InstagramUser(
            instagram_id="abc",
            username="abcuser",
            user_detail=testcommon.factory.create_user_detail(we_follow_user=False, user_follows_us=False)
        )
        user2 = instabotpatrik.model.InstagramUser(
            instagram_id="xyz",
            username="xyzuser",
            user_detail=testcommon.factory.create_user_detail(we_follow_user=True, user_follows_us=False)
        )
        user3 = instabotpatrik.model.InstagramUser(
            instagram_id="foo",
            username="foouser",
            user_detail=testcommon.factory.create_user_detail(we_follow_user=True, user_follows_us=False)
        )
        user4 = instabotpatrik.model.InstagramUser(
            instagram_id="bar",
            username="baruser",
        )
        self.repository.update_user(user1)
        self.repository.update_user(user2)
        self.repository.update_user(user3)
        self.repository.update_user(user4)
        followed = self.repository.find_followed_users()

        self.assertEqual(len(followed), 2)
        self.assertTrue("xyz" in [user.instagram_id for user in followed])
        self.assertTrue("foo" in [user.instagram_id for user in followed])

class ItShouldDeleteUser(RepositoryTestCase):
    def runTest(self):
        user1 = instabotpatrik.model.InstagramUser(
            instagram_id="abc",
            username="abcuser",
            user_detail=testcommon.factory.create_user_detail(we_follow_user=False, user_follows_us=False)
        )
        self.repository.update_user(user1)
        db_user = self.repository.find_user(instagram_id="abc")
        self.repository.delete_user(username=db_user.username)
        after_delete = self.repository.find_user(instagram_id="abc")

        self.assertIsNotNone(db_user)
        self.assertIsNone(after_delete)

class ItShouldDeleteMedia(RepositoryTestCase):
    def runTest(self):
        media1 = instabotpatrik.model.InstagramMedia(
            instagram_id="abc",
            shortcode="shortcodeABC",
        )
        self.repository.update_media(media1)
        db_media = self.repository.find_media_by_id(media_id=media1.instagram_id)
        self.repository.delete_media(shortcode=media1.shortcode)
        after_delete = self.repository.find_media_by_id(media_id=media1.instagram_id)

        self.assertIsNotNone(db_media)
        self.assertIsNone(after_delete)


import pygame
from pygame.math import Vector2
import random
import math
import sys

pygame.init()
screen_info = pygame.display.Info()

# Set the window size to match the screen size
screen_width, screen_height = screen_info.current_w, screen_info.current_h
window_size = (screen_width, screen_height)
WIN = pygame.display.set_mode(window_size)

#Establishes the paddles
paddle1_rect=pygame.Rect(screen_width//7,screen_height//7,screen_width//30,screen_height//3)
paddle2_rect=pygame.Rect(screen_width-paddle1_rect.x,screen_height//7,screen_width//30,screen_height//3)

paddle1_rect.center=(paddle1_rect.x,screen_height//2)
paddle2_rect.center=(paddle2_rect.x,screen_height//2)

#Establishes the ball and its movement properties
BALL_RECT=pygame.Rect(0,0,paddle1_rect.width//2,paddle1_rect.width//2)
BALL_RECT.center=(screen_width//2,screen_height//2)
#ball_vel=Vector2(random.randint(1,3),random.randint(1,3))
random_number = random.choice([random.randint(-5, -3), random.randint(3, 5)])
random_number2 = random.choice([random.randint(-5, -3), random.randint(3, 5)])
ball_vel=Vector2(random_number,random_number2)

#Establishes the score
player1_score=0
player2_score=0

#Establishes clock and FPS
FPS=60
clock=pygame.time.Clock()

run=True

#Function to handle paddle movement through keyboard input
def paddle_handle_movement(keys_pressed,left,right):
    if keys_pressed[pygame.K_w] and left.top>0:
        left.y-=5
    if keys_pressed[pygame.K_s] and left.bottom<screen_height:
        left.y+=5

    if keys_pressed[pygame.K_UP] and right.top>0:
        right.y-=5
    if keys_pressed[pygame.K_DOWN] and right.bottom<screen_height:
        right.y+=5

#Function to deal with a goal score scenario
def scored(ball,player1_score,player2_score,screen_width,screen_height):
    global ball_vel
    #New random velocity)
    random_number = random.choice([random.randint(-5, -3), random.randint(3, 5)])
    random_number2 = random.choice([random.randint(-5, -3), random.randint(3, 5)])

    #If player2 scores on player 1(Right to left)
    if ball.left<=0:
        ball.center=(screen_width//2,screen_height//2)
        player2_score+=1
        ball_vel=Vector2(random_number,random_number2)
    #If player1 scores on player2(left to right)
    if ball.right>=screen_width:
        ball.center=(screen_width//2,screen_height//2)
        player1_score+=1
        ball_vel=Vector2(random_number,random_number2)

    return player1_score,player2_score

def ball_handle_movement(ball,paddle1,paddle2):
    ball.move_ip(ball_vel)
    norm=Vector2(1,0)
    passed=(ball.left<paddle1.right-abs(ball_vel.x)) or (ball.right>paddle2.left+abs(ball_vel.x))
    if (ball.colliderect(paddle1) or ball.colliderect(paddle2)):
        paddle_rect = paddle1 if ball.colliderect(paddle1) else paddle2
        if not passed:

            # Compute the collision angle
            offset = ball.centery - paddle_rect.centery
            normalized_offset = offset / (paddle_rect.height / 2)
            collision_angle = normalized_offset * (math.pi / 3)

            # Reflect the velocity vector
            norm = Vector2(1, 0)
            ball_vel.reflect_ip(norm.rotate(collision_angle))
        else:
            #A real terrible solution but it works
            if paddle_rect==paddle1:
                if paddle_rect.bottom-6<=ball.top<=paddle_rect.bottom:
                    new_coords=list(paddle_rect.bottomright)
                    new_coords[0]+=1
                    ball.topleft=tuple(new_coords)
                elif paddle_rect.top<=ball.bottom<=paddle_rect.top+6:
                    new_coords=list(paddle_rect.topright)
                    new_coords[0]+=1
                    ball.bottomleft=tuple(new_coords)
            else:
                if paddle_rect.bottom-6<=ball.top<=paddle_rect.bottom:
                    new_coords=list(paddle_rect.bottomleft)
                    new_coords[0]-=1
                    ball.topright=tuple(new_coords)
                elif paddle_rect.top<=ball.bottom<=paddle_rect.top+6:
                    new_coords=list(paddle_rect.topleft)
                    new_coords[0]-=1
                    ball.bottomright=tuple(new_coords)

    if ball.top < 0:
        ball_vel.reflect_ip(Vector2(0, 1))
    if ball.bottom > screen_height:
        ball_vel.reflect_ip(Vector2(0, -1))

def draw_game_elements(win, paddle1_rect, paddle2_rect, ball_rect, score1, score2):
    font = pygame.font.SysFont("Impact", 50)
    score1_text = font.render(str(score1), True, (255, 255, 255))
    score2_text = font.render(str(score2), True, (255, 255, 255))

    score1_rect = score1_text.get_rect()
    score1_rect.center = (win.get_width()//4, 50)

    score2_rect = score2_text.get_rect()
    score2_rect.center = (win.get_width() - win.get_width()//4, 50)

    pygame.draw.rect(win, (255, 255, 255), paddle1_rect)
    pygame.draw.rect(win, (255, 255, 255), paddle2_rect)
    pygame.draw.rect(win, (255, 255, 255), ball_rect)
    win.blit(score1_text, score1_rect)
    win.blit(score2_text, score2_rect)

run=True
while run:
    clock.tick(FPS)
    black=pygame.Rect(0,0,screen_width,screen_height)
    pygame.draw.rect(WIN,(0,0,0),black)

    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            pygame.quit()

    keys_pressed=pygame.key.get_pressed()

    paddle_handle_movement(keys_pressed,paddle1_rect,paddle2_rect)
    ball_handle_movement(BALL_RECT,paddle1_rect,paddle2_rect)
    player1_score,player2_score=scored(BALL_RECT,player1_score,player2_score,screen_width,screen_height)

    draw_game_elements(WIN, paddle1_rect, paddle2_rect, BALL_RECT,player1_score,player2_score)
    pygame.display.flip()

    if player1_score==10 or player2_score==10:
        pygame.quit()
        sys.exit()


import linecache
import re
from spell_checker import *

def dict_all() -> dict:
    dic_data = {}
    count = 26
    count_2 = 1000
    letter = "A"
    for j in range(count):
        for i in range(count_2):
            k = "".join([letter, str(i).zfill(3)])
            dic_data[k] = []

        letter = chr(ord(letter) + 1)
    return dic_data

def dict_final(dic_data: dict) -> dict:
    with open("dictionary.txt", "r") as rf:
        for i in range(194433):
            word = rf.readline()
            word = word[:-1]
            key = soundex(word)
            if not bool(dic_data):
                dic_data[key] = [word]
            elif bool(dic_data):
                if key in dic_data.keys():
                    dic_data[key].append(word)
                else:
                    dic_data.update({key: [word]})
    return dic_data

def open_file_dict():
    with open("dict_data.txt", "w") as new:
        for k, v in dict_final(dict_all()).items():
            new.write(str(k) + "-" +  str(v) + "\n")

def choose_le_levenshtein(misspelled_word: str, word_list: list) -> list:
    dict_lev_count = {}
    for i in word_list:

        count_key = levenshtein(misspelled_word, i)
        if not bool(dict_lev_count):
            dict_lev_count[count_key] = [i]
        elif bool(dict_lev_count):
            if count_key in dict_lev_count.keys():
                dict_lev_count[count_key].append(i)
            else:
                dict_lev_count.update({count_key: [i]})

    choose_list_word = min(dict_lev_count.keys())
    return dict_lev_count[choose_list_word]

def spell_correction(misspelled_word: str) -> list:
    misspelled_word_soundex = soundex(misspelled_word)

    letter, number = misspelled_word_soundex[:1], misspelled_word_soundex[1:]

    with open("dict_data.txt", "r"):
        word_list_str = linecache.getline(
            "dict_data.txt",
            (ord(letter) - ord("A")) * 1000 + int(number)  + 1,
        )
        word_list = re.sub("[^\w]", " ", word_list_str).split()
        return choose_le_levenshtein(misspelled_word, word_list[1:])

if __name__ == "__main__":

    # open_file_dict() # call to create the data file
    print(",".join(spell_correction("name")))


import cv2

faceCascade = cv2.CascadeClassifier("Model/OpenCVTrainedPara.xml")

cap = cv2.VideoCapture(0) # open the camera
cap.set(3, 640)  # set Width
cap.set(4, 480)  # set Height

while True:
    success, img = cap.read() # read in the information of the camera now
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    faces = faceCascade.detectMultiScale(  # classifier function
        gray,  # the input gray scale image
        scaleFactor=1.2,  # specify how much the image size is reduced at each image scale
        minNeighbors=5,  # specify how many neighbors each candidate rectangle should have
        minSize=(100, 100)  # specify the minimum rectangle size to be detected as a face
    )

    for (x, y, w, h) in faces:  # for every face detected
        cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)  # draw a rectangle

        cv2.imshow('video', img)

    k = cv2.waitKey(30) & 0xff
    if k == 27:  # press 'ESC' to quit
        break

# cleanup
print("\n [INFO] Exiting Program and cleanup stuff")
cap.release()
cv2.destroyAllWindows()


from databaseWorker import *
from flask import Flask, render_template, redirect, request

app = Flask("Chat", template_folder = "template")
prepareDb("userLoginMessage.db")

login = ""

@app.route('/')
def index():
    rows = getLoginsAndMessages("userLoginMessage.db")
    saveLogin = "<textarea name=\"login\" id=\"textareaLoginAlign\" required>" + str(login) + "</textarea>"
    return render_template("index.html", message=generateUsersHTMLTable(rows), loginInput = saveLogin)

@app.route('/message')
def userMessage():
    global login
    login = request.args.get("login")
    message = request.args.get("message")
    registerUserMessage("userLoginMessage.db", login, message)
    return redirect('/')

@app.route('/refreshThePage')
def refreshThePage():
    return render_template("index.html")

app.run(host = "0.0.0.0", port = 8081)


"""
This program is free software: you can redistribute it and/or modify it under the terms of the GNU
General Public License as published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not,
see <https://www.gnu.org/licenses/>.
"""

import logging
from refine_contigs.utils import (
    do_parallel,
    get_components,
    fasta_to_dataframe,
    fast_flatten,
    df_to_seq,
    dereplicate_fragments,
    combine_fragment_files,
    get_components_large,
    get_graph,
    process_alns_reg,
    get_seqs,
    clean_up,
    concat_df,
)
import gzip
import networkx as nx
import pathlib, os, sys
from Bio import SeqIO
import uuid
import pandas as pd
import pyfaidx
import datatable as dt
import numpy as np

log = logging.getLogger("my_logger")

sys.setrecursionlimit(10 ** 6)

def split_contigs(args):
    dt.options.progress.clear_on_success = True
    dt.options.nthreads = args.threads

    prefix = args.prefix
    dname = str(uuid.uuid4())
    tmp_dir = pathlib.Path(args.tmp_dir, dname).absolute()
    min_id = float(100.0 * args.min_id)
    if not os.path.isdir(tmp_dir):
        os.makedirs(tmp_dir, exist_ok=True)

    # Read contigs file
    logging.info("Processing contig file")
    contigs = fasta_to_dataframe(args.contigs)
    contigs["name_original"] = contigs["name"]
    contigs["name"] = contigs.reset_index().index
    contigs["name"] = contigs["name"].apply(lambda x: f"{prefix}_{x:012d}", 1)
    contigs["length"] = contigs.sequence.map(len)

    logging.info(f"Read and processed {len(contigs.index):,} contigs")
    contigs_tmp = pathlib.Path(tmp_dir, dname).with_suffix(".fasta")
    seq_records = df_to_seq(contigs[["name", "sequence"]])
    with open(contigs_tmp, "w") as handle:
        SeqIO.write(seq_records, handle, "fasta")

    pyfaidx.Faidx(str(contigs_tmp))

    max_seq_len = contigs.length.max()
    assm_len = sum(contigs.length)
    contigs_len = contigs[["name", "length"]].set_index("name").T.to_dict()

    G, results = get_graph(
        contigs=str(contigs_tmp),
        tmp_dir=tmp_dir,
        max_seq_len=max_seq_len,
        threads=args.threads,
        min_id=min_id,
        min_cov=args.min_cov,
    )

    if nx.number_connected_components(G) > 0:
        # Get components
        G_components = get_components(G)
        if G_components.count(None) == len(G_components):
            logging.info("Couldn't find any component")
            exit(0)
        d = {
            name: f"comp-{k}"
            for k, comp in enumerate(list(G_components))
            for name in comp
        }
        comps = (
            pd.DataFrame.from_dict(d, orient="index", columns=["component"])
            .rename_axis("Chromosome")
            .reset_index()
        )

        ids_overlaps = fast_flatten([list(n.nodes()) for n in G_components])
        ids_overlaps = sorted(ids_overlaps, key=len, reverse=True)
        # component = G_components[0]
        # For each component extrac aligned and non-aligned regions
        logging.info("Finding overlaps between contigs")

        results_filt = results[
            (dt.f.qcov >= args.min_cov)
            & (dt.f.pident >= args.min_id)
            & (dt.f.source != dt.f.target),
            [
                "source",
                "target",
                "pident",
                "alnlen",
                "qstart",
                "qend",
                "tstart",
                "tend",
                "qcov",
                "qlen",
                "tlen",
            ],
        ].to_pandas()

        parms = {
            "contigs": str(contigs_tmp),
            "results": results_filt,
            "min_id": args.min_id,
            "min_cov": args.min_cov,
            "contigs_len": contigs_len,
            "threads": args.threads,
            "par": False,
        }

        # Split components list by length of the components, longer components than 1K will
        # be processed in parallel, while smaller ones while be sequential but many in parallel
        logging.info(f"Splitting by large and small components")
        min_size = 50
        G_components_small = sorted(
            [n for n in G_components if len(list(n.nodes())) < min_size],
            key=len,
            reverse=True,
        )
        G_components_large = sorted(
            [n for n in G_components if len(list(n.nodes())) >= min_size],
            key=len,
            reverse=True,
        )

        if len(G_components_small) > 0:
            logging.info(f"Processing {len(G_components_small):,} small components")
            aln_reg_small = do_parallel(
                parms=parms,
                lst=G_components_small,
                threads=args.threads,
                func=process_alns_reg,
            )

            chunks = np.array_split(aln_reg_small, args.threads)
            chunks = list(filter(lambda df: not df.empty, chunks))

            parms = {
                "contigs": str(contigs_tmp),
                "chunks": chunks,
            }
            aln_reg_small = do_parallel(
                parms=parms,
                lst=list(range(0, len(chunks))),
                threads=args.threads,
                func=get_seqs,
            )
        else:
            aln_reg_small = None

        parms = {
            "contigs": str(contigs_tmp),
            "results": results_filt,
            "min_id": min_id,
            "min_cov": args.min_cov,
            "contigs_len": contigs_len,
            "threads": args.threads,
            "par": True,
        }

        if len(G_components_large) > 0:
            logging.info(f"Processing {len(G_components_large):,} large components")
            aln_reg_large = get_components_large(
                parms=parms,
                components=G_components_large,
                threads=args.threads,
                func=process_alns_reg,
            )
            chunks = np.array_split(aln_reg_large, args.threads)
            parms = {
                "contigs": str(contigs_tmp),
                "chunks": chunks,
            }
            aln_reg_large = do_parallel(
                parms=parms,
                lst=list(range(0, args.threads)),
                threads=args.threads,
                func=get_seqs,
            )
            if aln_reg_small is not None:
                aln_reg = concat_df([aln_reg_small, aln_reg_large])
            else:
                aln_reg = aln_reg_large

        else:
            if aln_reg_small is not None:
                aln_reg = aln_reg_small
            else:
                logging.info("Couldn't process any component")
                exit(0)

        miss_contigs = aln_reg["Chromosome"].unique()
        miss_contigs_ovl = aln_reg["Class"].tolist().count("overlap")
        miss_contigs_novl = aln_reg["Class"].tolist().count("non-overlap")
        miss_contigs_ovl_nt = sum(
            aln_reg[aln_reg["Class"] == "overlap"]["length"].tolist()
        )
        miss_contigs_nt = sum(
            [contigs_len[k]["length"] for k in miss_contigs if k in contigs_len]
        )
        miss_contigs_ovl_nt_prop = 100 * (miss_contigs_ovl_nt / assm_len)
        logging.info(
            f"Found {miss_contigs_ovl:,} overlaps in {len(miss_contigs):,} contigs ({miss_contigs_ovl_nt_prop:.2f}% of the assembly)"
        )

        aln_reg_fname = f"{args.output}.split.overlaps.tsv.gz"
        logging.info(f"Saving overlaps to {aln_reg_fname}")

        contigs_names = contigs[["name", "name_original"]]
        contigs_names.columns = ["Chromosome", "name_original"]
        pd.merge(
            left=aln_reg[["Chromosome", "Start", "End", "Class", "length"]],
            right=contigs_names,
            how="inner",
        ).to_csv(aln_reg_fname, sep="\t", compression="gzip", index=False)

        logging.info(
            f"Clustering fragments longer than {args.frag_min_len} NTs [id:{args.frag_cls_id*100}%; cov:{args.frag_cls_cov*100}]"
        )

        aln_reg["frag"] = aln_reg.groupby(["Chromosome"]).cumcount()
        aln_reg["name"] = aln_reg["Chromosome"] + str("-") + aln_reg["frag"].astype(str)

        derep_frag, derep_tsv_frag = dereplicate_fragments(
            frags=aln_reg[(aln_reg["length"] > args.frag_min_len)],
            threads=args.threads,
            tmp_dir=tmp_dir,
            cls_id=args.frag_cls_id,
            cls_cov=args.frag_cls_cov,
            cls_step="fragment",
        )

        logging.info("Combining merged and non-merged contigs")
        dfs = combine_fragment_files(df1=derep_frag, df2=contigs, ids=ids_overlaps)

        logging.info(
            f"Global clustering [id:{args.global_cls_id*100}%; cov:{args.global_cls_cov*100}]"
        )

        derep_global, derep_tsv_global = dereplicate_fragments(
            frags=dfs,
            threads=args.threads,
            tmp_dir=tmp_dir,
            cls_id=args.global_cls_id,
            cls_cov=args.global_cls_cov,
            cls_step="global",
        )

        dfs = fasta_to_dataframe(derep_global)
        dfs["old_name"] = dfs["name"]
        dfs["name"] = dfs.index
        dfs["name"] = dfs["name"].apply(lambda x: f"{prefix}_sp_{x:012d}", 1)
        seq_records = df_to_seq(dfs)

        fname = f"{args.output}.split.fasta.gz"
        logging.info(f"Saving contigs to {fname} file")
        with gzip.open(fname, "wt") as handle:
            SeqIO.write(seq_records, handle, "fasta")
        if pathlib.Path(derep_tsv_frag).exists():
            cls_frag = pd.read_csv(
                derep_tsv_frag, sep="\t", names=["rep_fragment", "name"]
            )
        else:
            cls_frag = pd.DataFrame(columns=["rep_fragment", "name"])
        cls_global = pd.read_csv(
            derep_tsv_global, sep="\t", names=["rep_global", "name"]
        )
        mappings = (
            pd.merge(
                left=contigs_names,
                right=aln_reg[["Chromosome", "name", "length"]],
                how="left",
            )
            .merge(
                right=cls_frag,
                how="left",
            )
            .merge(
                right=cls_global,
                how="left",
            )
            .merge(right=comps, how="left")
        )
        mappings["old_name"] = mappings.apply(
            lambda x: x["Chromosome"]
            if pd.isnull(x["rep_global"])
            else x["rep_global"],
            axis=1,
        )

        dfs = dfs[["name", "old_name"]]
        dfs.columns = ["final_name", "old_name"]
        mappings = pd.merge(left=mappings, right=dfs, how="left",)[
            [
                "name_original",
                "Chromosome",
                "name",
                "component",
                "rep_fragment",
                "rep_global",
                "final_name",
            ]
        ]
        mappings.columns = [
            "contig_name_original",
            "contig_renamed",
            "contig_name_fragment",
            "component",
            "cls_rep_frag",
            "cls_rep_global",
            "contig_name_refined",
        ]

        mapping_fname = f"{args.output}.split.mapping.tsv.gz"
        logging.info(f"Saving name mappings to {mapping_fname} file")
        mappings.to_csv(mapping_fname, sep="\t", compression="gzip", index=False)

        comp_fname = f"{args.output}.split.all-vs-all.tsv.gz"
        logging.info(f"Saving all-vs-all comparison to {comp_fname} file")
        results.to_pandas().to_csv(
            comp_fname, sep="\t", compression="gzip", index=False
        )

        g_fname = f"{args.output}.split.graph-edgelist.tsv.gz"
        logging.info(f"Saving graph edgelist to {g_fname} file")
        nx.to_pandas_edgelist(G).to_csv(
            g_fname, sep="\t", compression="gzip", index=False
        )
        clean_up(keep=args.keep_files, temp_dir=str(tmp_dir))
    else:
        logging.info("Couldn't find any overlaps")


import socket
from datetime import datetime
from server import Server

class ServerLinear(Server):
    def __init__(self) -> None:
        super().__init__()

    def run_server(self):
        server_socket = socket.socket(
            socket.AF_INET, socket.SOCK_STREAM, proto=0)
        server_socket.bind(('', self.port))
        server_socket.listen()
        cid = 0
        while True:
            client_socket, client_addr = server_socket.accept()
            print(f'Client {cid} connected - {datetime.now().time()}')
            self.serve_client(client_socket, cid)
            cid += 1

    def serve_client(self, client_socket: socket.socket, cid: int):
        request = client_socket.recv(1024)
        response = self.handle_request(request)
        client_socket.sendall(response)
        client_socket.close()
        print(f'Client {cid} served - {datetime.now().time()}')

if __name__ == "__main__":
    server = ServerLinear()
    server.run_server()
    # print(server.load)


import os
import json
import boto3

def lambda_handler(event, context):
    TABLE_NAME = os.environ["TABLE_NAME"]

    dynamodb = boto3.resource('dynamodb')
    table = dynamodb.Table(TABLE_NAME)

    return {
        "statusCode": 200,
        "body": json.dumps({
            "success": True,
            "items": table.scan().get("Items",[])
        })
    }


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

# Imports
import torch
from torch.utils.data.dataset import Dataset
import torchlanguage.transforms
import urllib
import os
import zipfile
import json
import codecs
import random
import pickle
import numpy as np
from datetime import datetime

# Reuters C50 dataset
class ReutersC50Dataset(Dataset):
    """
    Reuters C50 dataset
    """

    # Constructor
    def __init__(self, root='./data', download=False, n_authors=50, dataset_size=100, dataset_start=0, authors=None,
                 transform=None, retain_transform=False, load_transform=False, load_features=u""):
        """
        Constructor
        :param root: Data root directory.
        :param download: Download the dataset?
        :param n_authors: How many authors from the dataset to load (2 to 50).
        :param dataset_size: How many samples from each author to load (1 to 100).
        :param authors: The list of authors name to load.
        :param transform: A TextTransformer object to apply.
        :param retain_transform:
        :param load_features: Load pre-computed features ?
        """
        # Properties
        self.root = root
        self.n_authors = n_authors if authors is None else len(authors)
        self.dataset_size = dataset_size
        self.dataset_start = dataset_start
        self.authors = authors
        self.transform = transform
        self.author2id = dict()
        self.id2author = dict()
        self.texts = list()
        self.retain_transform = retain_transform
        self.load_transform = load_transform
        self.last_tokens = None
        self.tokenizer = torchlanguage.transforms.Token()
        self.last_file = None
        self.load_features = load_features

        # Create directory if needed
        if not os.path.exists(self.root):
            self._create_root()
        # end if

        # Download the data set
        if download and not os.path.exists(os.path.join(self.root, "authors.json")):
            self._download()
        # end if

        # Generate data set
        self._load()
    # end __init__

    #############################################
    # PUBLIC
    #############################################

    # Set start
    def set_start(self, start):
        """
        Set start
        :param start:
        :return:
        """
        self.dataset_start = start
    # end set_start

    #############################################
    # OVERRIDE
    #############################################

    # Length
    def __len__(self):
        """
        Length
        :return:
        """
        return len(self.texts)
    # end __len__

    # Get item
    def __getitem__(self, idx):
        """
        Get item
        :param idx:
        :return:
        """
        # Current file
        text_path, author_name = self.texts[idx]

        # Last file
        self.last_file = (text_path, author_name)

        # Read text
        text_content = codecs.open(text_path, 'r', encoding='utf-8').read()

        # Last text
        self.last_tokens = self.tokenizer(text_content)

        # Transform
        if self.transform is not None:
            # Load transform
            transformed = None
            if self.load_transform:
                transformed = self._load_transform(text_path, type(self.transform).__name__)
            # end if

            # Transform if not found
            if transformed is None:
                transformed = self.transform(text_content)
                to_be_saved = True
            else:
                to_be_saved = False
            # end if

            # Load features
            if self.load_features != u"":
                # Root text path
                root_text_path = text_path[:-4]

                # Load features
                text_features = torch.from_numpy(np.load(root_text_path + u"." + self.load_features + u".npy"))
                text_features = text_features.type(torch.FloatTensor)

                # Concate
                transformed = torch.cat((transformed, text_features), dim=1)
            # end if

            # Transformed size
            if type(transformed) is list:
                transformed_size = len(transformed)
            elif type(transformed) is torch.LongTensor or type(transformed) is torch.FloatTensor \
                    or type(transformed) is torch.cuda.LongTensor or type(transformed) is torch.cuda.FloatTensor \
                    or type(transformed) is torch.Tensor:
                transformed_dim = transformed.dim()
                transformed_size = transformed.size(transformed_dim - 2)
            # end if

            # Save transform
            if to_be_saved and self.retain_transform:
                self._save_transform(transformed, text_path, type(self.transform).__name__)
            # end if

            return transformed, self.author2id[author_name], self._create_labels(author_name, transformed_size)
        else:
            return text_content, self.author2id[author_name]
        # end if
    # end __getitem__

    ##############################################
    # PRIVATE
    ##############################################

    # Save transform
    def _save_transform(self, transform, text_path, transform_name):
        """
        Save transform
        :param text_path:
        :param transform_name:
        :return:
        """
        print(u"load")
        return pickle.dump(transform, open(text_path + "." + transform_name, 'wb'))
    # end _save_transform

    # Load transform
    def _load_transform(self, text_path, transform_name):
        """
        Load transform
        :param text_path:
        :return:
        """
        print(u"load")
        try:
            return pickle.load(open(text_path + "." + transform_name, 'rb'))
        except IOError:
            return None
        # end try
    # end if

    # Create labels
    def _create_labels(self, author_name, transformed_length):
        """
        Create labels
        :param author_name:
        :param length:
        :return:
        """
        # Author id
        author_id = self.author2id[author_name]

        # Vector
        tag_vector = torch.zeros(transformed_length, self.n_authors)

        # Set
        tag_vector[:, author_id] = 1.0

        return tag_vector
    # end _create_labels

    # Create the root directory
    def _create_root(self):
        """
        Create the root directory
        :return:
        """
        os.mkdir(self.root)
    # end _create_root

    # Download the dataset
    def _download(self):
        """
        Downlaod the dataset
        :return:
        """
        # Path to zip file
        path_to_zip = os.path.join(self.root, "reutersc50.zip")

        # Download
        urllib.urlretrieve("http://www.nilsschaetti.com/datasets/reutersc50.zip", path_to_zip)

        # Unzip
        zip_ref = zipfile.ZipFile(path_to_zip, 'r')
        zip_ref.extractall(self.root)
        zip_ref.close()

        # Delete zip
        os.remove(path_to_zip)
    # end _download

    # Load dataset
    def _load(self):
        """
        Load the dataset
        :return:
        """
        # Authors info
        authors_info = json.load(open(os.path.join(self.root, "authors.json"), 'r'))

        # Author count
        author_count = 0

        # Given authors
        if self.authors is not None:
            given_authors = list(self.authors)
        else:
            given_authors = None
        # end if
        self.authors = list()

        # For each authors
        for index, author_name in enumerate(authors_info.keys()):
            # If in the set
            if author_count < self.n_authors and (given_authors is None or author_name in given_authors):
                # New author
                self.author2id[author_name] = author_count
                self.id2author[index] = author_name

                # Add each text
                for text_index, text_name in enumerate(authors_info[author_name]):
                    if text_index >= self.dataset_start and text_index < self.dataset_start + self.dataset_size:
                        # Add
                        self.texts.append((os.path.join(self.root, text_name + ".txt"), author_name))
                    # end if
                # end for

                # Count
                self.authors.append(author_name)
                author_count += 1
            # end if
        # end for

        # Shuffle but always the same
        random.seed(1985)
        random.shuffle(self.texts)
    # end _load

# end ReutersC50Dataset


import numpy as np
from matplotlib import pyplot as plt

nes = [10, 20, 50, 80, 100, 120, 200]

plt.rcParams.update({'font.size': 15})
fig = plt.figure()
ax = fig.subplots()

data = []
lines = []
names = []
for n in nes:
    data.append(np.load('data/energies-%d.npy' % n))
    lines.append(ax.plot(data[-1][0], data[-1][1]/n)[0])
    names.append('$n = %d$' % n)

print(data[-2][1][-1]/120)

ax.grid()
ax.set_xscale('log')
ax.set_ylabel('$E/n$')
ax.set_xlabel('$\\beta$')
ax.legend(lines, names)
plt.show()


#!/usr/bin/python3
"""Reformating a set of downloaded csvs."""

import pandas as pd
import numpy as np
import urllib
import string
import multiprocessing
import os
import argparse
import sys

# Base url and debugging stuff for pandas
pd.set_option('max_colwidth', 2000)
pd.set_option('display.width', 20000)
BASE_URL = 'https://s3-us-west-2.amazonaws.com/cauldron-workshop/data/'
OUTPUT_FILE = 'output.csv'

def generate_file_names():
    """Return a list of filenames."""
    return [letter + '.csv' for letter in list(string.ascii_lowercase)]

def download_file(url):
    """Return a downloaded file as a pandas dataframe."""
    return pd.read_csv(url)

def download_files(base_url=BASE_URL):
    """Return a list of pandas dataframes.

    Keyword arguments:
    base_url -- the base url for downloading a list of files
    """
    urls = [base_url + name for name in generate_file_names()]
    pool = multiprocessing.Pool(processes=len(urls))
    return pool.map(download_file, urls)

def concatinate_dataframes(df_list):
    """Concatinate a list of dataframes into one dataframe."""
    return pd.concat(df_list)

def transform_dataframe(df):
    """Return a transformed pandas dataframe."""
    df = df[['user_id', 'path', 'length']]
    return df.pivot(index="user_id", columns="path", values="length").fillna(0)

def write_csv(df_list, output_file="output.csv"):
    """Write a formated csv to disk.

    Keyword arguments:
    output_file -- the name of the csv file to be written to disk
    """
    df = concatinate_dataframes(df_list)
    df = transform_dataframe(df)
    df.to_csv(output_file)

def get_args():
    """Return argparse arguments."""
    parser = argparse.ArgumentParser()
    parser.add_argument("-o", "--output", dest="output_file",
                        default=OUTPUT_FILE, help="Name of the output file")
    parser.add_argument("-u", "--url", dest="base_url",
                        default=BASE_URL,
                        help="Base url for downloading files")
    return parser.parse_args()

def main():
    """The main function."""
    args = get_args()
    files = download_files(args.base_url)
    write_csv(files, args.output_file)

if __name__ == "__main__":
    main()


import uuid
from dataclasses import field
from datetime import timedelta
from typing import List, Optional, Union

from jose import jwt
from pydantic.dataclasses import dataclass

from kpm.settings import settings as s
from kpm.shared.domain.time_utils import now_utc_sec

def _get_expire_time(
    valid_from: int, expire_delta: Union[timedelta, int]
) -> Optional[int]:
    if not isinstance(valid_from, int):
        raise TypeError("Valid from must be an `int`.")
    if not expire_delta or expire_delta == 0:
        return None
    if isinstance(expire_delta, timedelta):
        return valid_from + int(expire_delta.total_seconds())
    elif isinstance(expire_delta, int):
        return valid_from + expire_delta
    else:
        raise TypeError(
            "Expire time has to be of type `Union[timedelta, int]` and "
            + f"it is if type `{type(expire_delta)}`"
        )

class JWTClaims:
    """
    Standard claim names from https://www.iana.org/assignments/jwt/jwt.xhtml
    """

    ISSUER: str = "iss"
    SUBJECT: str = "sub"
    AUDIENCE: str = "aud"
    JWT_ID: str = "jti"
    NOT_BEFORE: str = "nbf"
    EXPIRATION_TIME: str = "exp"
    ISSUED_AT: str = "iat"
    SCOPES: str = "scope"
    # Custom claims
    FRESH: str = "fsh"
    TYPE: str = "typ"

@dataclass
class JWTToken:
    """Data inside the token.

    It needs to be enough for all microservices so they do not need to ask
    for extra user information. Include auth groups, ids...
    """

    """Identifier for who this token is for example user_id"""
    subject: str
    """Indicate token is access_token or refresh_token"""
    type: str
    """Duration of the token"""
    exp_time_delta: Optional[Union[timedelta, int]] = None
    """Freshness of the access token for elevated privilege tasks"""
    fresh: bool = True
    """User access privileges this token has. For example `admin`"""
    scopes: List[str] = field(default_factory=list)
    """If values are obtained from another token, do not allow to transform
    back."""

    jwt_id: str = field(default_factory=lambda: str(uuid.uuid4()))
    exp_time: int = None
    not_before: int = field(default_factory=now_utc_sec)
    issued_at: int = field(default_factory=now_utc_sec)
    can_generate_str: bool = True

    def __post_init__(self):
        if not self.subject or not isinstance(self.subject, str):
            raise ValueError("Subject is needed.")
        if self.type not in ("access", "refresh"):
            raise ValueError("Token can only be of types access or refresh.")
        if not self.exp_time:
            self.exp_time = _get_expire_time(
                self.not_before, self.exp_time_delta
            )
        # We remove duplicate values
        self.scopes = list(set(self.scopes))

    def is_fresh(self) -> bool:
        return self.fresh

    def is_access(self) -> bool:
        return self.type == "access"

    def is_refresh(self) -> bool:
        return self.type == "refresh"

    def is_valid(self, scope: str = None) -> bool:
        """Checks if token validity conditions are met"""
        current_time = now_utc_sec()
        time_is_valid = self.not_before <= current_time < self.exp_time
        scope_is_valid = scope in self.scopes if scope else True

        return self.subject and time_is_valid and scope_is_valid

    def to_token(self) -> str:
        if not self.can_generate_str:
            raise ValueError("This token cannot be recreated")
        to_enc = {
            # Reserved claims
            JWTClaims.SUBJECT: self.subject,
            # "aud": None,
            JWTClaims.JWT_ID: self.jwt_id,
            JWTClaims.ISSUED_AT: self.issued_at,
            JWTClaims.NOT_BEFORE: self.not_before,
            # Custom claims
            JWTClaims.SCOPES: self.scopes,
            JWTClaims.TYPE: self.type,
        }

        if self.exp_time:
            to_enc[JWTClaims.EXPIRATION_TIME] = self.exp_time
        if isinstance(self.fresh, bool) and self.is_access():
            to_enc[JWTClaims.FRESH] = self.fresh

        return jwt.encode(
            claims=to_enc,
            key=s.JWT_SECRET_KEY,
            algorithm=s.JWT_ALGORITHM,
        )

@dataclass
class AccessToken(JWTToken):
    type: str = "access"
    exp_time_delta: Union[timedelta, int, None] = field(
        default=s.JWT_ACCESS_EXPIRE_TIME
    )

@dataclass
class RefreshToken(JWTToken):
    type: str = "refresh"
    exp_time_delta: Union[timedelta, int, None] = field(
        default=s.JWT_REFRESH_EXPIRE_TIME
    )

def from_token(encoded_token: str) -> Union[AccessToken, RefreshToken]:
    decoded = jwt.decode(
        token=encoded_token,
        key=s.JWT_SECRET_KEY,
        algorithms=s.JWT_DECODE_ALGORITHMS,
    )
    tok_cls = JWTToken
    if decoded.get("typ") == "access":
        tok_cls = AccessToken
    elif decoded.get("typ") == "refresh":
        tok_cls = RefreshToken
    else:
        raise TypeError("Token type not supported")

    return tok_cls(
        subject=decoded.get(JWTClaims.SUBJECT),
        fresh=decoded.get(JWTClaims.FRESH, False),
        scopes=decoded.get(JWTClaims.SCOPES),
        exp_time_delta=None,
        can_generate_str=False,
        exp_time=decoded.get(JWTClaims.EXPIRATION_TIME),
        issued_at=decoded.get(JWTClaims.ISSUED_AT),
        not_before=decoded.get(JWTClaims.NOT_BEFORE),
        jwt_id=decoded.get(JWTClaims.JWT_ID),
    )


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

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('geo', '0001_initial'),
        ('events', '0001_initial'),
    ]

    operations = [
        migrations.CreateModel(
            name='Grantee',
            fields=[
                ('grantee_id', models.CharField(max_length=30, serialize=False, primary_key=True)),
                ('name', models.CharField(max_length=255)),
                ('title', models.CharField(max_length=255, blank=True)),
                ('alt_location', models.ForeignKey(related_name='alt_location', blank=True, to='geo.Place', null=True)),
            ],
        ),
        migrations.CreateModel(
            name='GrantorName',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('name', models.CharField(max_length=255)),
                ('title', models.CharField(max_length=255, blank=True)),
                ('event', models.ForeignKey(to='events.Event')),
            ],
        ),
        migrations.CreateModel(
            name='Identifier',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('name', models.CharField(max_length=255)),
            ],
        ),
        migrations.AddField(
            model_name='grantee',
            name='identifiers',
            field=models.ManyToManyField(to='people.Identifier', null=True, blank=True),
        ),
        migrations.AddField(
            model_name='grantee',
            name='professional_location',
            field=models.ForeignKey(related_name='professional_location', blank=True, to='geo.Place', null=True),
        ),
        migrations.AddField(
            model_name='grantee',
            name='standard_location',
            field=models.ForeignKey(related_name='standard_location', blank=True, to='geo.Place', null=True),
        ),
    ]


from pathlib import Path
from shlib import Run
import nestedtext as nt
import arrow
import os
import re

now = arrow.now()

def create_logfile(age, extra=''):
    path = Path('test.log')
    path.write_text(f"entry written = {age} days ago.{extra}")
    ctime = now.shift(days=-age, seconds=-600)
    os.utime(str(path), (ctime.timestamp(), ctime.timestamp()))
    return ctime

def exercise_ntlog(
    keep_for = None,
    min_entries = None,
    max_entries = None,
    delete = None,
    delete_running_log = True
):
    cmd = ['ntlog']
    if keep_for:
        cmd.extend(['--keep-for', str(keep_for)])
    if min_entries:
        cmd.extend(['--min-entries', str(min_entries)])
    if max_entries:
        cmd.extend(['--max-entries', str(max_entries)])
    if delete:
        cmd.append('--delete')
    cmd.append('test.log')
    keep_for = keep_for if keep_for else 7
    min_entries = min_entries if min_entries else 1
    max_entries = max_entries if max_entries else 0
    upper_bound = max(keep_for, min_entries, max_entries)
    if not max_entries:
        max_entries = 1000
    if delete_running_log:
        Path('test.log.nt').unlink(missing_ok=True)

    ctimes = []
    for days in reversed(range(upper_bound + 7)):
        ctime = create_logfile(days)
        ctimes.append(ctime)
        ntlog = Run(cmd, 'sOEW')
        running_log = nt.load('test.log.nt')

        # running log must contain the given log entry
        assert str(ctime) in running_log

        # check to see if given logfile was deleted if requested
        assert os.path.isfile('test.log') != delete

        # check that the number of entries matches our expectations
        num_entries = len(running_log)
        runs = len(ctimes)
        assert num_entries <= min(runs, max_entries)
        assert num_entries >= min(runs, min_entries)

        ctimes_to_check = ctimes[-num_entries:]
        for ctime in ctimes_to_check:
            age = (now - ctime).days
            ctime = str(ctime)
            assert ctime in running_log, ctime
            assert running_log[ctime] == f"entry written = {age} days ago."

def test_defaults():
    exercise_ntlog()

    # now shrink keep_for and check that old entries are deleted
    running_log = nt.load('test.log.nt')
    ctimes = [arrow.get(k) for k in running_log]
    expected_ctimes = [ctime for ctime in ctimes if (now - ctime).days < 3]

    ntlog = Run(['ntlog', '--keep-for', '3', 'test.log'], 'sOEW')
    running_log = nt.load('test.log.nt')
    ctimes = [arrow.get(k) for k in running_log.keys()]
    assert len(ctimes) == len(expected_ctimes)
    assert ctimes == expected_ctimes

def test_delete():
    exercise_ntlog(delete=True)

def test_keep_for():
    exercise_ntlog(keep_for=3)

def test_min_entries():
    exercise_ntlog(min_entries=3)

def test_max_entries():
    exercise_ntlog(max_entries=3)

def test_retention():
    # checks that you can add a given log file that is beyond the keep_for date
    ctime = create_logfile(21)
    Path('test.log.nt').unlink(missing_ok=True)
    for i in range(5):
        ntlog = Run(['ntlog', 'test.log'], 'sOEW')
        running_log = nt.load('test.log.nt')
        ctimes = list(running_log.keys())
        assert len(ctimes) == 1
        assert ctimes[0] == str(ctime)

def test_exceptions():
    running_logfile = Path('test.log.nt')

    running_logfile.unlink(missing_ok=True)
    ntlog = Run(['ntlog', 'does-not-exist'], 'sOEW1')
    assert ntlog.status == 1
    assert ntlog.stderr == 'ntlog error: does-not-exist: no such file or directory.\n'

    running_logfile.unlink(missing_ok=True)
    ntlog = Run(['ntlog', '--max-entries', 'infinity', 'does-not-exist'], 'sOEW1')
    assert ntlog.status == 1
    assert ntlog.stderr == 'ntlog error: infinity: could not convert to number.\n'

    running_logfile.unlink(missing_ok=True)
    ntlog = Run(['ntlog', '--min-entries', '0', 'does-not-exist'], 'sOEW1')
    assert ntlog.status == 1
    assert ntlog.stderr == 'ntlog error: 0: expected strictly positive number.\n'

    # try to save a log file with same ctime but differing contents
    running_logfile.unlink(missing_ok=True)
    create_logfile(1)
    ntlog = Run(['ntlog', 'test.log'], 'sOEW1')
    assert ntlog.status == 0
    ctime = create_logfile(1, extra='\na difference')
    ntlog = Run(['ntlog', 'test.log'], 'sOEW1')
    assert ntlog.status == 1
    #assert re.match('ntlog error: [^ ]+: attempt to overwrite log entry.\n', ntlog.stderr)
    assert ntlog.stderr == f'ntlog error: {ctime!s}: attempt to overwrite log entry.\n'

    # attempt to read a running log file with a bogus datestamp
    path = running_logfile.write_text("not a date: contents")
    ntlog = Run(['ntlog', 'test.log'], 'sOEW1')
    assert ntlog.status == 1
    assert "Expected an ISO 8601-like string, but was given 'not a date'." in ntlog.stderr

    # attempt to read a bogus running log file
    path = running_logfile.write_text("not a valid NT file")
    ntlog = Run(['ntlog', 'test.log'], 'sOEW1')
    assert ntlog.status == 1
    assert 'unrecognized line.' in ntlog.stderr

    # attempt to read a bogus running log file
    ntlog = Run(['ntlog', '--keep-for', '2fortnight', 'test.log'], 'sOEW1')
    assert ntlog.status == 1
    assert 'unable to convert' in ntlog.stderr

if __name__ == '__main__':
    # As a debugging aid allow the tests to be run on their own, outside pytest.
    # This makes it easier to see and interpret and textual output.

    defined = dict(globals())
    for k, v in defined.items():
        if callable(v) and k.startswith('test_'):
            print()
            print('Calling:', k)
            print((len(k)+9)*'=')
            v()


import pygame as pygame

pygame.init()
fsize = 20
font = pygame.font.SysFont("comicsans", fsize)
left = 42
middle = 540
right = 1030
first_height = 100
second_height = 250
third_height = 400
middle_height = 300
small_radius = 35
big_radius = 145
din = 10
small_delta = 37
big_delta = 177
army_price = 200
it_price = 50
dollar_price = 100
democracy_price = 600
electro_price = 400
sanction_price = 300
maxscore = 10000000000000
maxlvl = 5
income1 = 10
income2 = 100
income3 = 1000
income4 = 10000
income5 = 100000
vicx = 400
vicy = 200
vicz = 50
# # #
monx = 400
mony = 600
monz = 30
# # #
incx = 800
incy = 600
incz = 30
# # #
worx = 800
wory = 670
worz = 30
# # #
capx = 400
capy = 670
capz = 30
# # #
cx = 500
cy = 500
cz = 30
# # #
px = 800
py = 20
pz = 15
# # #
width = 1080
height = 720
mainfont = 19
sleep = 0.1
light = (173, 216, 230)
red = (255, 0, 0)
white = (255, 255, 255)
black = (0, 0, 0)
blue = (0, 0, 255)
dark_blue = (11, 11, 69)
grey = (128, 128, 128)
green = (0, 255, 0)
olive = (128, 128, 0)
light_olive = (156, 175, 136)
orange = (255, 131, 0)
light_orange = (252, 210, 153)
lime = (0, 255, 0)
dark_green = (0, 100, 0)
light_red = (255, 204, 203)
# sourceFileDir = os.path.dirname(os.path.abspath(__file__))
army = pygame.transform.scale(pygame.image.load('images/army2.png'), (80, 80))
usa = pygame.transform.scale(pygame.image.load('images/usa.png'), (300, 300))
tech = pygame.transform.scale(pygame.image.load('images/tech.png'), (80, 80))
dollar = pygame.transform.scale(pygame.image.load('images/dollar.png'), (80, 80))
tesla = pygame.transform.scale(pygame.image.load('images/tesla.png'), (80, 80))
sanc = pygame.transform.scale(pygame.image.load('images/sanc.png'), (80, 80))
demo = pygame.transform.scale(pygame.image.load('images/nato.png'), (80, 80))
bg = pygame.transform.scale(pygame.image.load('images/usabg.png'), (1080, 720))
asound = pygame.mixer.Sound('sounds/army.wav')
csound = pygame.mixer.Sound('sounds/click.wav')
dsound = pygame.mixer.Sound('sounds/dollar.wav')
nsound = pygame.mixer.Sound('sounds/nato.wav')
jsound = pygame.mixer.Sound('sounds/sanction.wav')
tsound = pygame.mixer.Sound('sounds/tesla.wav')
autech = 0
autodollar = 0
autotesla = 0
capitalist = 0
'''
a = 0
b = 0
c = 0
d = 0
e = 0
'''
score = 0
income = 0
FPS = 30
# # #
b1 = 1.2
b2 = 2
b3 = 5
ob1 = 4
ob2 = 7  # overprice boost 2
ob3 = 10
caplvl1 = 1
caplvl2 = 2
caplvl3 = 3
caplvl4 = 4
caplvl5 = 5

i1 = 0.5
oi1 = 1.5
i2 = 2
oi2 = 2  # overprice income 2
i3 = 5
oi3 = 3
rect_delta_x1 = 35
rect_delta_x2 = 15
rect_delta_y1 = 20
rect_delta_x11 = 30
rect_delta_x22 = 7
rect_delta = 5
rect_delta_y2 = 10
rect_delta_y22 = 25
rect_corr = 1.3


import cv2
import pandas as pd
import matplotlib.pyplot as plt
import datetime
import numpy as np
import os
import progressbar

class Dataset_Class():

    def __init__(self, sequence, lidar=True, progress_bar=True, low_memory=True):

        self.lidar = lidar
        self.low_memory = low_memory
        # Load standard framees
        self.seq_dir = '/Smat/Nemo/dataset/kitti_odometry_dataset/original/sequences/{}/'.format(sequence)
        self.poses_dir = '/Smat/Nemo/dataset/kitti_odometry_dataset/original/poses/{}.txt'.format(sequence)

        self.left_image_files = os.listdir(self.seq_dir + 'image_0/')
        self.left_image_files.sort()
        self.right_image_files = os.listdir(self.seq_dir + 'image_1/')
        self.right_image_files.sort()
        self.velodyne_files = os.listdir(self.seq_dir + 'velodyne/')
        self.velodyne_files.sort()
        self.num_frames = len(self.left_image_files)
        self.lidar_path = self.seq_dir + 'velodyne/'

        # Load event frames
        # Load event frames for 10fps
        self.event_frame_dir_10 = '/Smat/Nemo/dataset/kitti_odometry_dataset/reconstruction_10/sequences/{}/'.format(sequence)
        self.left_event_frame_image_files_10 = os.listdir(self.event_frame_dir_10 + 'image_0/')
        self.left_event_frame_image_files_10.sort()
        self.right_event_frame_image_files_10 = os.listdir(self.event_frame_dir_10 + 'image_1/')
        self.right_event_frame_image_files_10.sort()

        # Load event frames for 30fps
        self.event_frame_dir_30 = '/Smat/Nemo/dataset/kitti_odometry_dataset/reconstruction_30/sequences/{}/'.format(sequence)
        self.left_event_frame_image_files_30 = os.listdir(self.event_frame_dir_30 + 'image_0/')
        self.left_event_frame_image_files_30.sort()
        self.right_event_frame_image_files_30 = os.listdir(self.event_frame_dir_30 + 'image_1/')
        self.right_event_frame_image_files_30.sort()

        poses = pd.read_csv(self.poses_dir, delimiter = ' ', header=None)

        self.gt = np.zeros((self.num_frames, 3, 4))
        for i in range(len(poses)):
            self.gt[i] = np.array(poses.iloc[i]).reshape(3,4)

        calib = pd.read_csv(self.seq_dir + 'calib.txt', delimiter = ' ', header = None, index_col = 0)

        self.P0 =np.array(calib.loc['P0:']).reshape((3, 4))
        self.P1 =np.array(calib.loc['P1:']).reshape((3, 4))
        self.P2 =np.array(calib.loc['P2:']).reshape((3, 4))
        self.P3 =np.array(calib.loc['P3:']).reshape((3, 4))
        self.Tr =np.array(calib.loc['Tr:']).reshape((3, 4))

        if low_memory:
            self.reset_frames()
            self.first_left_image = cv2.imread(self.seq_dir + 'image_0/'
                                              + self.left_image_files[0], 0)
            self.first_right_image = cv2.imread(self. seq_dir + 'image_1/'
                                               + self.right_image_files[0], 0)
            self.second_left_image = cv2.imread(self.seq_dir + 'image_0/'
                                               + self.left_image_files[1], 0)
            # Load event frames 10fps
            self.first_left_event_image_10 = cv2.imread(self.event_frame_dir_10 + 'image_0/'
                                               + self.left_event_frame_image_files_10[0], 0)
            self.first_right_event_image_10 = cv2.imread(self.event_frame_dir_10 + 'image_1/'
                                               + self.right_event_frame_image_files_10[0], 0)
            self.second_left_event_image_10 = cv2.imread(self.event_frame_dir_10 + 'image_0/'
                                              + self.left_event_frame_image_files_10[1], 0)

            # Load event frames 30fps
            self.first_left_event_image_30 = cv2.imread(self.event_frame_dir_30 + 'image_0/'
                                               + self.left_event_frame_image_files_30[0], 0)
            self.first_right_event_image_30 = cv2.imread(self.event_frame_dir_30 + 'image_1/'
                                               + self.right_event_frame_image_files_30[0], 0)
            self.second_left_event_image_30 = cv2.imread(self.event_frame_dir_30 + 'image_0/'
                                               + self.left_event_frame_image_files_30[1], 0)

            if lidar:
                self.first_pointcloud = np.fromfile(self.lidar_path+self.velodyne_files[0],
                                                   dtype=np.float32, count=-1).reshape((-1, 4))
            self.imheight = self.first_left_image.shape[0]
            self.imwidth = self.first_left_image.shape[1]
        else:
            self.left_images = []
            self.right_images = []
            if progress_bar:
                bar = progressbar.ProgressBar(max_value=self.num_frames)
            for i, name_left in enumerate(self.left_image_files):
                name_right = self.right_image_files[i]
                self.left_images.append(cv2.imread(self.seq_dir + 'image_0/' + name_left))
                self.right_images.append(cv2.imread(self.seq_dir + 'image_1/' + name_right))
                if lidar:
                    pointcloud = np.fromfile(self.lidar_path + velodyne_file, dtype=np.float32).reshape((-1, 4))
                    self.pointclouds.append(pointcloud)
                if progress_bar:
                    bar.update(i+i)
            self.imheight = self.left_images[0].shape[0]
            self.imwidth = self.right_images[0].shape[1]
            self.first_left_image = self.left_images[0]
            self.first_right_image = self.right_images[0]
            self.second_left_image = self.left_images[1]
            if self.lidar:
                self.first_pointcloud = self.pointclouds[0]

    def reset_frames(self):
        self.left_images = (cv2.imread(self.seq_dir + 'image_0/' + name_left, 0) for name_left in self.left_image_files)
        self.right_images = (cv2.imread(self.seq_dir + 'image_1/' + name_right, 0) for name_right in self.right_image_files)
        if self.lidar:
            self.pointclouds = (np.fromfile(self.lidar_path + velodyne_file, dtype=np.float32, count=-1).reshape((-1, 4)) for velodyne_file in self.velodyne_files)

        pass


import pandas as pd
import statsmodels.api as sm
import matplotlib.pyplot as plt

def change_to_celsius(x):
    '''change temperature from Fahr to C'''
    degree = (x-32)*5/9
    return degree

def analyze(data, figure_filename):
    '''perform analysis on mosquito data

       data is a DataFrame with columns 'temperature', 'rainfall' and 'mosquitos'.
       Performs a least squares regression, plots the result and returns the fit parameters

       Figure_filename is the name of output plot'''

    assert data['temperature'].max() < 70, 'check the input temp is less than 70'
    regr_results = sm.OLS.from_formula('mosquitos ~ temperature + rainfall', data).fit()
    parameters = regr_results.params
    rsquared = regr_results.rsquared
    predicted = parameters[0] + parameters[1] * data['temperature'] + parameters[2] * data['rainfall']
    plt.plot(predicted, data['mosquitos'], 'ro')
    min_mosquitos, max_mosquitos = min(data['mosquitos']), max(data['mosquitos'])
    plt.plot([min_mosquitos, max_mosquitos], [min_mosquitos, max_mosquitos], 'k-')
    #plt.show() # use this to show multi figures # stop the script here and show the plot, when the user close the plot, go forward
    plt.savefig(figure_filename)

    return parameters