import tkinter as tk
from os import listdir
from os.path import isfile, join
import vlc
import time
import threading
import random
import os
from tkinter import *
from tkinter.ttk import *

# create basic tkinter mp3player window
root = tk.Tk()

root.geometry("700x500")
root.resizable(width=0, height=0)
root.title("pyplayer")
root.configure(background="black")

# field to put audiofiles-directory in
dir_name = tk.Entry(root, width=30)
dir_name.pack()

# showing song that is currently playing
current_song = tk.Label(root, text="", height=1, width=100, bg="black", fg="white")
current_song.pack(pady=3)

# text to show if shuffling audiofiles is on or off
shuffle_on = tk.Label(root, text="SHUFFLE OFF", height=1, width=15, bg="black", fg="white")
shuffle_on.place(relx=0.1, rely=0.0)

# text to show if repeating audiofile is on or off
repeat_on = tk.Label(root, text="REPEAT OFF", height=1, width=15, bg="black", fg="white")
repeat_on.place(relx=0.75, rely=0.0)

files_list = []
new_file_list = []

# list that is showing all audiofiles in chosen directory
audiofiles = tk.Listbox(root, width=87, height=20, bg="black", fg="white")
audiofiles.place(relx=0.0, rely="0.1")

# bool to control thread-flow
dead = False

# bool to control if shuffle is on or off
default_shuffle = False

# bool to control if repeating audio is on or off
default_repeat = False

def list_audio():
    """
    list_audio description
    gets all files in chosen directory
    inserts them into audiofiles-list-window
    """

    audiofiles.delete(0, tk.END)
    files_list.clear()
    for i in [f for f in listdir(dir_name.get()) if isfile(join(dir_name.get(), f))]:
        files_list.append(i)
    for i in files_list:
        audiofiles.insert(tk.END, i)

# creating mediaplayer object
media_object = vlc.MediaPlayer()

# setting default value of mediaplayer object
media_object.audio_set_volume(100)

def wait_test():
    """
    wait_test description
    thread that controls when next audiofiles is going to be played
    """
    while True:
        if (str(media_object.get_state()) == "State.Ended"):
            set_play_media()
            return
        time.sleep(1)

countdown_thread = threading.Thread(target=wait_test)

def set_play_media():
    """
    set_play_media description
    checks if repeat is on -> plays same audiofile again
    checks if shuffle is on -> plays random audio file using random-module

    plays next audio file normally
    """

    global default_shuffle
    countdown_thread = threading.Thread(target=wait_test)
    media_object.stop()

    if (default_repeat == False):
        if (default_shuffle == False):
            try:
                media = vlc.Media(
                    dir_name.get() + "/" + new_file_list[new_file_list.index(current_song.cget("text")) + 1])
                current_song.configure(text=new_file_list[new_file_list.index(current_song.cget("text")) + 1])
                media_object.set_media(media)
                media_object.play()
            except:
                media = vlc.Media(dir_name.get() + "/" + new_file_list[0])
                current_song.configure(text=new_file_list[0])
                media_object.set_media(media)
                media_object.play()
        else:
            random_audio = random.choice(new_file_list)
            media = vlc.Media(dir_name.get() + "/" + random_audio)
            current_song.configure(text=random_audio)
            media_object.set_media(media)
            media_object.play()
    else:
        media = vlc.Media(dir_name.get() + "/" + current_song.cget("text"))
        media_object.set_media(media)
        media_object.play()
    countdown_thread.start()

def play_audio():
    """
    play_audio description
    plays initial audiofile and sets new-audiofile-list
    """
    global new_file_list
    media_object.stop()
    current_song.configure(text=audiofiles.get(tk.ANCHOR))

    new_file_list = files_list[files_list.index(current_song.cget("text")):] + files_list[:files_list.index(
        current_song.cget("text"))]

    media = vlc.Media(dir_name.get() + "/" + audiofiles.get(tk.ANCHOR))
    media_object.set_media(media)
    media_object.play()
    countdown_thread.start()

def increase_audio():
    media_object.audio_set_volume(media_object.audio_get_volume() + 10)

def decrease_audio():
    media_object.audio_set_volume(media_object.audio_get_volume() - 10)

def next_audio():
    """
    next_audio description
    checks if shuffle is on -> skips to  random audio file as next audio

    skips to next audio file normally
    """
    global default_shuffle
    media_object.stop()
    if (default_shuffle == False):
        try:
            media = vlc.Media(dir_name.get() + "/" + files_list[files_list.index(current_song.cget("text")) + 1])
            media_object.set_media(media)
            current_song.configure(text=files_list[files_list.index(current_song.cget("text")) + 1])
            media_object.play()
        except:
            media = vlc.Media(dir_name.get() + "/" + files_list[0])
            media_object.set_media(media)
            current_song.configure(text=files_list[0])
            media_object.play()
    else:
        random_audio = random.choice(new_file_list)
        media = vlc.Media(dir_name.get() + "/" + random_audio)
        current_song.configure(text=random_audio)
        media_object.set_media(media)
        media_object.play()

def previous_audio():
    """
    previous_audio description
    goes back to previous audiofile in list
    """
    media_object.stop()
    try:
        media = vlc.Media(dir_name.get() + "/" + files_list[files_list.index(current_song.cget("text")) - 1])
        media_object.set_media(media)
        current_song.configure(text=files_list[files_list.index(current_song.cget("text")) - 1])
        media_object.play()
    except:
        media = vlc.Media(dir_name.get() + "/" + files_list[0])
        media_object.set_media(media)
        current_song.configure(text=files_list[files_list.index(current_song.cget("text")) - 1])
        media_object.play()

# create gui-audio-controls
control_audio = tk.Canvas(root, width=697.5, height=75, bg="black")
control_audio.place(rely=0.845, relx=0.0)

go_back_img = PhotoImage(file = r"go_back.png")

go_back = tk.Button(root, text="", image = go_back_img, command=previous_audio)
go_back.place(rely=0.863, relx=0.02, height=60, width=60)

def pause_audio():
    media_object.pause()

pause_play_img = PhotoImage(file = r"pause_play.png")
pause_play = tk.Button(root, text="", image = pause_play_img, command=pause_audio)
pause_play.place(rely=0.863, relx=0.12, height=60, width=60)

go_next_img = PhotoImage(file = r"go_next.png")
go_next = tk.Button(root, text="", image = go_next_img, command=next_audio)
go_next.place(rely=0.863, relx=0.22, height=60, width=60)

volume_down_img = PhotoImage(file = r"volume_down.png")
volume_down = tk.Button(root, text="", image = volume_down_img, command=decrease_audio)
volume_down.place(rely=0.863, relx=0.32, height=60, width=60)

volume_up_img = PhotoImage(file = r"volume_up.png")
volume_up = tk.Button(root, text="", image = volume_up_img,command=increase_audio)
volume_up.place(rely=0.863, relx=0.42, height=60, width=60)

def activate_repeat():
    """
    activate_repeat description
    changes repeat text and bool
    """
    global default_repeat
    if (default_repeat == False):
        default_repeat = True
        repeat_on.configure(text="REPEAT ON")
    else:
        default_repeat = False
        repeat_on.configure(text="REPEAT OFF")

repeat_img = PhotoImage(file = r"repeat_audio.png")
repeat_option = tk.Button(root, text="", image = repeat_img, command=activate_repeat)
repeat_option.place(rely=0.863, relx=0.56, height=60, width=60)

play_audio_img = PhotoImage(file = r"play_song.png")
play_audio = tk.Button(root, text="", image = play_audio_img, command=play_audio)
play_audio.place(rely=0.863, relx=0.70, height=60, width=60)

choose_dir_img = PhotoImage(file = r"choose_dir.png")
choose_dir = tk.Button(root, text="", image = choose_dir_img, command=list_audio)
choose_dir.place(rely=0.863, relx=0.80, height=60, width=60)

def activate_shuffle():
    """
    activate_shuffle description
    changes shuffle text and bool
    """
    global default_shuffle
    if (default_shuffle == False):
        default_shuffle = True
        shuffle_on.configure(text="SHUFFLE ON")
    else:
        shuffle_on.configure(text="SHUFFLE OFF")
        default_shuffle = False

shuffle_audio_img = PhotoImage(file = r"shuffle_audio.png")
shuffle_audio = tk.Button(root, text="", image = shuffle_audio_img, command=activate_shuffle)
shuffle_audio.place(rely=0.863, relx=0.90, height=60, width=60)

root.mainloop()


from typing import List, TypeVar, Tuple

from common import sender_obs
from simulator.network_simulator import network, packet
from simulator.network_simulator.constants import BITS_PER_BYTE, BYTES_PER_PACKET, EVENT_TYPE_SEND

class Sender:
    """Base class of senders.

    srtt and retransmission reference: https://datatracker.ietf.org/doc/html/rfc6298
    """

    SRTT_ALPHA = 1 / 8
    SRTT_BETA = 1 / 4
    RTO_K = 4

    def __init__(self, sender_id: int, dest: int):
        """Create a sender object.

        Args
            sender_id: id of sender device.
            dest: id of destination device.
        """
        self.sender_id = sender_id
        # variables to track in a MonitorInterval. Units: packet
        self.sent = 0  # no. of packets
        self.acked = 0  # no. of packets
        self.lost = 0  # no. of packets
        self.rtt_samples = []
        self.queue_delay_samples = []

        # variables to track accross the connection session
        self.tot_sent = 0 # no. of packets
        self.tot_acked = 0 # no. of packets
        self.tot_lost = 0 # no. of packets
        self.cur_avg_latency = 0.0
        self.first_ack_ts = None
        self.last_ack_ts = None
        self.first_sent_ts = None
        self.last_sent_ts = None

        self.pacing_rate = 0  # bytes/s
        self.bytes_in_flight = 0  # bytes
        self.net = None
        self.dest = dest
        self.ssthresh = 80

        self.srtt = None
        self.rttvar = None
        # initialize rto to 3s for waiting SYC packets of TCP handshake
        self.rto = 3  # retransmission timeout (seconds)

        self.event_count = 0
        self.got_data = True

        # variables to track binwise measurements
        self.bin_bytes_sent = {}
        self.bin_bytes_acked = {}
        self.lat_ts = []
        self.lats = []
        self.bin_size = 500 # ms

    def can_send_packet(self) -> bool:
        return True

    def register_network(self, net: "network.Network") -> None:
        self.net = net

    def on_packet_sent(self, pkt: "packet.Packet") -> bool:
        pkt.pkt_id = self.event_count
        self.event_count += 1
        self.sent += 1
        self.bytes_in_flight += pkt.pkt_size
        self.tot_sent += 1
        if self.first_sent_ts is None:
            self.first_sent_ts = pkt.ts
        self.last_sent_ts = pkt.ts

        bin_id = int((pkt.ts - self.first_sent_ts) * 1000 / self.bin_size)
        self.bin_bytes_sent[bin_id] = self.bin_bytes_sent.get(bin_id, 0) + pkt.pkt_size
        return True

    def on_packet_acked(self, pkt: "packet.Packet") -> None:
        self.acked += 1
        self.cur_avg_latency = (self.cur_avg_latency * self.tot_acked + pkt.rtt) / (self.tot_acked + 1)
        self.tot_acked += 1
        if self.first_ack_ts is None:
            self.first_ack_ts = pkt.ts
        self.last_ack_ts = pkt.ts
        assert self.bytes_in_flight >= pkt.pkt_size
        self.bytes_in_flight -= pkt.pkt_size
        if self.srtt is None and self.rttvar is None:
            self.srtt = pkt.rtt
            self.rttvar = pkt.rtt / 2
            # RTO <- SRTT + max (G, K*RTTVAR) ignore G because clock granularity of modern os is tiny
            self.rto = max(1, min(self.srtt + self.RTO_K * self.rttvar, 60))
        elif self.srtt and self.rttvar:
            self.rttvar = (1 - self.SRTT_BETA) * self.rttvar + \
                self.SRTT_BETA * abs(self.srtt - pkt.rtt)
            self.srtt = (1 - self.SRTT_ALPHA) * self.srtt + \
                self.SRTT_ALPHA * pkt.rtt
            self.rto = max(1, min(self.srtt + self.RTO_K * self.rttvar, 60))
        else:
            raise ValueError("srtt and rttvar shouldn't be None.")

        self.rtt_samples.append(pkt.rtt)
        self.queue_delay_samples.append(pkt.queue_delay)

        bin_id = int((pkt.ts - self.first_ack_ts) * 1000 / self.bin_size)
        self.bin_bytes_acked[bin_id] = self.bin_bytes_acked.get(bin_id, 0) + pkt.pkt_size
        self.lat_ts.append(pkt.ts)
        self.lats.append(pkt.rtt * 1000)

    def on_packet_lost(self, pkt: "packet.Packet") -> None:
        self.lost += 1
        self.tot_lost += 1
        assert self.bytes_in_flight >= pkt.pkt_size
        self.bytes_in_flight -= pkt.pkt_size

    def get_cur_time(self) -> float:
        assert self.net, "network is not registered in sender."
        return self.net.get_cur_time()

    def schedule_send(self, _) -> None:
        return

    def get_run_data(self):
        obs_end_time = self.get_cur_time()

        #obs_dur = obs_end_time - self.obs_start_time
        #print("Got %d acks in %f seconds" % (self.acked, obs_dur))
        #print("Sent %d packets in %f seconds" % (self.sent, obs_dur))
        #print("self.rate = %f" % self.rate)
        # print(self.acked, self.sent)
        # rtt_samples = self.rtt_samples # if self.rtt_samples else self.prev_rtt_samples

        return sender_obs.SenderMonitorInterval(
            self.sender_id,
            bytes_sent=self.sent * BYTES_PER_PACKET,
            bytes_acked=self.acked * BYTES_PER_PACKET,
            bytes_lost=self.lost * BYTES_PER_PACKET,
            send_start=self.obs_start_time,
            send_end=obs_end_time,
            recv_start=self.obs_start_time,
            recv_end=obs_end_time,
            rtt_samples=self.rtt_samples,
            queue_delay_samples=self.queue_delay_samples,
            packet_size=BYTES_PER_PACKET
        )

    def stop_run(self, pkt: "packet.Packet", end_time: float)->bool:
        return self.got_data and pkt.ts >= end_time and pkt.event_type == EVENT_TYPE_SEND

    def reset_obs(self):
        self.sent = 0
        self.acked = 0
        self.lost = 0
        self.rtt_samples = []
        self.queue_delay_samples = []
        self.obs_start_time = self.get_cur_time()

    def reset(self):
        self.ssthresh = 80
        self.event_count = 0
        self.pacing_rate = 0
        self.bytes_in_flight = 0
        self.srtt = None
        self.rttvar = None
        self.rto = 3  # retransmission timeout (seconds)
        self.tot_sent = 0 # no. of packets
        self.tot_acked = 0 # no. of packets
        self.tot_lost = 0 # no. of packets
        self.cur_avg_latency = 0.0
        self.first_ack_ts = None
        self.last_ack_ts = None
        self.reset_obs()

        self.bin_bytes_sent = {}
        self.bin_bytes_acked = {}
        self.lat_ts = []
        self.lats = []

    def timeout(self):
        # placeholder
        raise NotImplementedError

    def debug_print(self):
        pass

    @property
    def avg_sending_rate(self):
        """Average sending rate in packets/second."""
        assert self.last_ack_ts is not None and self.first_ack_ts is not None
        assert self.last_sent_ts is not None and self.first_sent_ts is not None
        return self.tot_sent / (self.last_sent_ts - self.first_sent_ts)

    @property
    def avg_throughput(self):
        """Average throughput in packets/second."""
        assert self.last_ack_ts is not None and self.first_ack_ts is not None
        assert self.last_sent_ts is not None and self.first_sent_ts is not None
        return self.tot_acked / (self.last_ack_ts - self.first_ack_ts)

    @property
    def avg_latency(self):
        """Average latency in second."""
        return self.cur_avg_latency

    @property
    def pkt_loss_rate(self):
        """Packet loss rate in one connection session."""
        return 1 - self.tot_acked / self.tot_sent

    @property
    def bin_tput(self) -> Tuple[List[float], List[float]]:
        tput_ts = []
        tput = []
        for bin_id in sorted(self.bin_bytes_acked):
            tput_ts.append(bin_id * self.bin_size / 1000)
            tput.append(
                self.bin_bytes_acked[bin_id] * BITS_PER_BYTE / self.bin_size * 1000 / 1e6)
        return tput_ts, tput

    @property
    def bin_sending_rate(self) -> Tuple[List[float], List[float]]:
        sending_rate_ts = []
        sending_rate = []
        for bin_id in sorted(self.bin_bytes_sent):
            sending_rate_ts.append(bin_id * self.bin_size / 1000)
            sending_rate.append(
                self.bin_bytes_sent[bin_id] * BITS_PER_BYTE / self.bin_size * 1000 / 1e6)
        return sending_rate_ts, sending_rate

    @property
    def latencies(self) -> Tuple[List[float], List[float]]:
        return self.lat_ts, self.lats

SenderType = TypeVar('SenderType', bound=Sender)


import os
from flask import Flask, request
from utils import check_count_args, foo

app = Flask(__name__)

BASE_DIR = os.path.dirname(os.path.abspath(__file__))
DATA_DIR = os.path.join(BASE_DIR, "data")

@app.route("/perform_query")
def perform_query():
    kwargs = {
    "cmd1": request.args.get('cmd1'),
    "cmd2": request.args.get('cmd2'),
    "val1": request.args.get('val1'),
    "val2": request.args.get('val2'),
    "file_name": request.args.get('file_name')
}
    kwargs["file_path"] = os.path.join(DATA_DIR, str(kwargs["file_name"]))

    check_count_args(**kwargs)

    f = open(kwargs["file_path"])
    res = foo(f, kwargs["cmd1"], kwargs["val1"])

    if kwargs["cmd2"] and kwargs["val2"]:
        res = foo(res, kwargs["cmd2"], kwargs["val2"])
    res = res = "\n".join(res)
    f.close()

    return app.response_class(res, content_type="text/plain"), 200

if __name__ == "__main__":
    app.run()


import math
import numpy as np
import pandas as pd
import seaborn as sns
import scipy.stats as ss
import matplotlib.pyplot as plt
from collections import Counter
from dython._private import convert, remove_incomplete_samples, replace_nan_with_value

REPLACE = 'replace'
DROP = 'drop'
DROP_SAMPLES = 'drop_samples'
DROP_FEATURES = 'drop_features'
SKIP = 'skip'
DEFAULT_REPLACE_VALUE = 0.0

def conditional_entropy(x, y, nan_strategy=REPLACE, nan_replace_value=DEFAULT_REPLACE_VALUE):
    """
    Calculates the conditional entropy of x given y: S(x|y)

    Wikipedia: https://en.wikipedia.org/wiki/Conditional_entropy

    **Returns:** float

    Parameters
    ----------
    x : list / NumPy ndarray / Pandas Series
        A sequence of measurements
    y : list / NumPy ndarray / Pandas Series
        A sequence of measurements
    nan_strategy : string, default = 'replace'
        How to handle missing values: can be either 'drop' to remove samples with missing values, or 'replace'
        to replace all missing values with the nan_replace_value. Missing values are None and np.nan.
    nan_replace_value : any, default = 0.0
        The value used to replace missing values with. Only applicable when nan_strategy is set to 'replace'.
    """
    if nan_strategy == REPLACE:
        x, y = replace_nan_with_value(x, y, nan_replace_value)
    elif nan_strategy == DROP:
        x, y = remove_incomplete_samples(x, y)
    y_counter = Counter(y)
    xy_counter = Counter(list(zip(x,y)))
    total_occurrences = sum(y_counter.values())
    entropy = 0.0
    for xy in xy_counter.keys():
        p_xy = xy_counter[xy] / total_occurrences
        p_y = y_counter[xy[1]] / total_occurrences
        entropy += p_xy * math.log(p_y/p_xy)
    return entropy

def cramers_v(x, y, nan_strategy=REPLACE, nan_replace_value=DEFAULT_REPLACE_VALUE):
    """
    Calculates Cramer's V statistic for categorical-categorical association.
    Uses correction from Bergsma and Wicher, Journal of the Korean Statistical Society 42 (2013): 323-328.
    This is a symmetric coefficient: V(x,y) = V(y,x)

    Original function taken from: https://stackoverflow.com/a/46498792/5863503
    Wikipedia: https://en.wikipedia.org/wiki/Cram%C3%A9r%27s_V

    **Returns:** float in the range of [0,1]

    Parameters
    ----------
    x : list / NumPy ndarray / Pandas Series
        A sequence of categorical measurements
    y : list / NumPy ndarray / Pandas Series
        A sequence of categorical measurements
    nan_strategy : string, default = 'replace'
        How to handle missing values: can be either 'drop' to remove samples with missing values, or 'replace'
        to replace all missing values with the nan_replace_value. Missing values are None and np.nan.
    nan_replace_value : any, default = 0.0
        The value used to replace missing values with. Only applicable when nan_strategy is set to 'replace'.
    """
    if nan_strategy == REPLACE:
        x, y = replace_nan_with_value(x, y, nan_replace_value)
    elif nan_strategy == DROP:
        x, y = remove_incomplete_samples(x, y)
    confusion_matrix = pd.crosstab(x,y)
    chi2 = ss.chi2_contingency(confusion_matrix)[0]
    n = confusion_matrix.sum().sum()
    phi2 = chi2/n
    r,k = confusion_matrix.shape
    phi2corr = max(0, phi2-((k-1)*(r-1))/(n-1))
    rcorr = r-((r-1)**2)/(n-1)
    kcorr = k-((k-1)**2)/(n-1)
    return np.sqrt(phi2corr/min((kcorr-1),(rcorr-1)))

def theils_u(x, y, nan_strategy=REPLACE, nan_replace_value=DEFAULT_REPLACE_VALUE):
    """
    Calculates Theil's U statistic (Uncertainty coefficient) for categorical-categorical association.
    This is the uncertainty of x given y: value is on the range of [0,1] - where 0 means y provides no information about
    x, and 1 means y provides full information about x.
    This is an asymmetric coefficient: U(x,y) != U(y,x)

    Wikipedia: https://en.wikipedia.org/wiki/Uncertainty_coefficient

    **Returns:** float in the range of [0,1]

    Parameters
    ----------
    x : list / NumPy ndarray / Pandas Series
        A sequence of categorical measurements
    y : list / NumPy ndarray / Pandas Series
        A sequence of categorical measurements
    nan_strategy : string, default = 'replace'
        How to handle missing values: can be either 'drop' to remove samples with missing values, or 'replace'
        to replace all missing values with the nan_replace_value. Missing values are None and np.nan.
    nan_replace_value : any, default = 0.0
        The value used to replace missing values with. Only applicable when nan_strategy is set to 'replace'.
    """
    if nan_strategy == REPLACE:
        x, y = replace_nan_with_value(x, y, nan_replace_value)
    elif nan_strategy == DROP:
        x, y = remove_incomplete_samples(x, y)
    s_xy = conditional_entropy(x,y)
    x_counter = Counter(x)
    total_occurrences = sum(x_counter.values())
    p_x = list(map(lambda n: n/total_occurrences, x_counter.values()))
    s_x = ss.entropy(p_x)
    if s_x == 0:
        return 1
    else:
        return (s_x - s_xy) / s_x

def correlation_ratio(categories, measurements, nan_strategy=REPLACE, nan_replace_value=DEFAULT_REPLACE_VALUE):
    """
    Calculates the Correlation Ratio (sometimes marked by the greek letter Eta) for categorical-continuous association.
    Answers the question - given a continuous value of a measurement, is it possible to know which category is it
    associated with?
    Value is in the range [0,1], where 0 means a category cannot be determined by a continuous measurement, and 1 means
    a category can be determined with absolute certainty.

    Wikipedia: https://en.wikipedia.org/wiki/Correlation_ratio

    **Returns:** float in the range of [0,1]

    Parameters
    ----------
    categories : list / NumPy ndarray / Pandas Series
        A sequence of categorical measurements
    measurements : list / NumPy ndarray / Pandas Series
        A sequence of continuous measurements
    nan_strategy : string, default = 'replace'
        How to handle missing values: can be either 'drop' to remove samples with missing values, or 'replace'
        to replace all missing values with the nan_replace_value. Missing values are None and np.nan.
    nan_replace_value : any, default = 0.0
        The value used to replace missing values with. Only applicable when nan_strategy is set to 'replace'.
    """
    if nan_strategy == REPLACE:
        categories, measurements = replace_nan_with_value(categories, measurements, nan_replace_value)
    elif nan_strategy == DROP:
        categories, measurements = remove_incomplete_samples(categories, measurements)
    categories = convert(categories, 'array')
    measurements = convert(measurements, 'array')
    fcat, _ = pd.factorize(categories)
    cat_num = np.max(fcat)+1
    y_avg_array = np.zeros(cat_num)
    n_array = np.zeros(cat_num)
    for i in range(0,cat_num):
        cat_measures = measurements[np.argwhere(fcat == i).flatten()]
        n_array[i] = len(cat_measures)
        y_avg_array[i] = np.average(cat_measures)
    y_total_avg = np.sum(np.multiply(y_avg_array,n_array))/np.sum(n_array)
    numerator = np.sum(np.multiply(n_array,np.power(np.subtract(y_avg_array,y_total_avg),2)))
    denominator = np.sum(np.power(np.subtract(measurements,y_total_avg),2))
    if numerator == 0:
        eta = 0.0
    else:
        eta = np.sqrt(numerator/denominator)
    return eta

def associations(dataset, nominal_columns=None, mark_columns=False, theil_u=False, plot=True, return_results=False, nan_strategy=REPLACE, nan_replace_value=DEFAULT_REPLACE_VALUE, **kwargs):
    """
    Calculate the correlation/strength-of-association of features in data-set with both categorical (eda_tools) and
    continuous features using:
     * Pearson's R for continuous-continuous cases
     * Correlation Ratio for categorical-continuous cases
     * Cramer's V or Theil's U for categorical-categorical cases

    **Returns:** a DataFrame of the correlation/strength-of-association between all features

    **Example:** see `associations_example` under `dython.examples`

    Parameters
    ----------
    dataset : NumPy ndarray / Pandas DataFrame
        The data-set for which the features' correlation is computed
    nominal_columns : string / list / NumPy ndarray
        Names of columns of the data-set which hold categorical values. Can also be the string 'all' to state that all
        columns are categorical, or None (default) to state none are categorical
    mark_columns : Boolean, default = False
        if True, output's columns' names will have a suffix of '(nom)' or '(con)' based on there type (eda_tools or
        continuous), as provided by nominal_columns
    theil_u : Boolean, default = False
        In the case of categorical-categorical feaures, use Theil's U instead of Cramer's V
    plot : Boolean, default = True
        If True, plot a heat-map of the correlation matrix
    return_results : Boolean, default = False
        If True, the function will return a Pandas DataFrame of the computed associations
    nan_strategy : string, default = 'replace'
        How to handle missing values: can be either 'drop_samples' to remove samples with missing values,
        'drop_features' to remove features (columns) with missing values, or 'replace' to replace all missing
        values with the nan_replace_value. Missing values are None and np.nan.
    nan_replace_value : any, default = 0.0
        The value used to replace missing values with. Only applicable when nan_strategy is set to 'replace'
    kwargs : any key-value pairs
        Arguments to be passed to used function and methods
    """
    dataset = convert(dataset, 'dataframe')
    if nan_strategy == REPLACE:
        dataset.fillna(nan_replace_value, inplace=True)
    elif nan_strategy == DROP_SAMPLES:
        dataset.dropna(axis=0, inplace=True)
    elif nan_strategy == DROP_FEATURES:
        dataset.dropna(axis=1, inplace=True)
    columns = dataset.columns
    if nominal_columns is None:
        nominal_columns = list()
    elif nominal_columns == 'all':
        nominal_columns = columns
    corr = pd.DataFrame(index=columns, columns=columns)
    for i in range(0,len(columns)):
        for j in range(i,len(columns)):
            if i == j:
                corr[columns[i]][columns[j]] = 1.0
            else:
                if columns[i] in nominal_columns:
                    if columns[j] in nominal_columns:
                        if theil_u:
                            corr[columns[j]][columns[i]] = theils_u(dataset[columns[i]], dataset[columns[j]], nan_strategy=SKIP)
                            corr[columns[i]][columns[j]] = theils_u(dataset[columns[j]], dataset[columns[i]], nan_strategy=SKIP)
                        else:
                            cell = cramers_v(dataset[columns[i]], dataset[columns[j]], nan_strategy=SKIP)
                            corr[columns[i]][columns[j]] = cell
                            corr[columns[j]][columns[i]] = cell
                    else:
                        cell = correlation_ratio(dataset[columns[i]], dataset[columns[j]], nan_strategy=SKIP)
                        corr[columns[i]][columns[j]] = cell
                        corr[columns[j]][columns[i]] = cell
                else:
                    if columns[j] in nominal_columns:
                        cell = correlation_ratio(dataset[columns[j]], dataset[columns[i]], nan_strategy=SKIP)
                        corr[columns[i]][columns[j]] = cell
                        corr[columns[j]][columns[i]] = cell
                    else:
                        cell, _ = ss.pearsonr(dataset[columns[i]], dataset[columns[j]])
                        corr[columns[i]][columns[j]] = cell
                        corr[columns[j]][columns[i]] = cell
    corr.fillna(value=np.nan, inplace=True)
    if mark_columns:
        marked_columns = ['{} (nom)'.format(col) if col in nominal_columns else '{} (con)'.format(col) for col in columns]
        corr.columns = marked_columns
        corr.index = marked_columns
    if plot:
        plt.figure(figsize=(kwargs.get('figsize',None)))

        sns.heatmap(corr, annot=kwargs.get('annot',True), fmt=kwargs.get('fmt','.2f'))
        plt.show()
    if return_results:
        return corr

def numerical_encoding(dataset, nominal_columns='all', drop_single_label=False, drop_fact_dict=True, nan_strategy=REPLACE, nan_replace_value=DEFAULT_REPLACE_VALUE):
    """
    Encoding a data-set with mixed data (numerical and categorical) to a numerical-only data-set,
    using the following logic:
    * categorical with only a single value will be marked as zero (or dropped, if requested)
    * categorical with two values will be replaced with the result of Pandas `factorize`
    * categorical with more than two values will be replaced with the result of Pandas `get_dummies`
    * numerical columns will not be modified

    **Returns:** DataFrame or (DataFrame, dict). If `drop_fact_dict` is True, returns the encoded DataFrame.
    else, returns a tuple of the encoded DataFrame and dictionary, where each key is a two-value column, and the
    value is the original labels, as supplied by Pandas `factorize`. Will be empty if no two-value columns are
    present in the data-set

    Parameters
    ----------
    dataset : NumPy ndarray / Pandas DataFrame
        The data-set to encode
    nominal_columns : sequence / string. default = 'all'
        A sequence of the nominal (categorical) columns in the dataset. If string, must be 'all' to state that
        all columns are nominal. If None, nothing happens.
    drop_single_label : Boolean, default = False
        If True, nominal columns with a only a single value will be dropped.
    drop_fact_dict : Boolean, default = True
        If True, the return value will be the encoded DataFrame alone. If False, it will be a tuple of
        the DataFrame and the dictionary of the binary factorization (originating from pd.factorize)
    nan_strategy : string, default = 'replace'
        How to handle missing values: can be either 'drop_samples' to remove samples with missing values,
        'drop_features' to remove features (columns) with missing values, or 'replace' to replace all missing
        values with the nan_replace_value. Missing values are None and np.nan.
    nan_replace_value : any, default = 0.0
        The value used to replace missing values with. Only applicable when nan_strategy is set to 'replace'
    """
    dataset = convert(dataset, 'dataframe')
    if nan_strategy == REPLACE:
        dataset.fillna(nan_replace_value, inplace=True)
    elif nan_strategy == DROP_SAMPLES:
        dataset.dropna(axis=0, inplace=True)
    elif nan_strategy == DROP_FEATURES:
        dataset.dropna(axis=1, inplace=True)
    if nominal_columns is None:
        return dataset
    elif nominal_columns == 'all':
        nominal_columns = dataset.columns
    converted_dataset = pd.DataFrame()
    binary_columns_dict = dict()
    for col in dataset.columns:
        if col not in nominal_columns:
            converted_dataset.loc[:,col] = dataset[col]
        else:
            unique_values = pd.unique(dataset[col])
            if len(unique_values) == 1 and not drop_single_label:
                converted_dataset.loc[:,col] = 0
            elif len(unique_values) == 2:
                converted_dataset.loc[:,col], binary_columns_dict[col] = pd.factorize(dataset[col])
            else:
                dummies = pd.get_dummies(dataset[col],prefix=col)
                converted_dataset = pd.concat([converted_dataset,dummies],axis=1)
    if drop_fact_dict:
        return converted_dataset
    else:
        return converted_dataset, binary_columns_dict

annos_path='C:\\Users\\P70065719\\Desktop\\Thesis\\code_snippets\\mycode\\new_annotations_LUNA1.csv'
df=pd.read_csv(annos_path)
df.drop('seriesuid',axis=1,inplace=True)
corr=associations(df,theil_u=True,nominal_columns=['subtlety','margin','lobulation','spiculation','calcification_new','sphericity_new','texture_new','malignancy'])


#Gabriel Martinez
#PHY 409 final project. Jun 2016
#NEEDS A REVISION FORSURE. First project written in Python.
#Fixes: Alot of parsing methods im sure can be rewritten using regex.
# I was working in 2D space only. Need to project 2d corrdinates to 3d.
# This project will simulate a biological system using a mathematical framework called D0LSystem.
#A recursive mathematical model that is given a Axiom and productions that will produce derivations until 'n' generations.
#More info on L-Systems see: https://en.wikipedia.org/wiki/L-system
import turtle as turtle
from tkinter import *
from math import *
from numpy import *
from queue import *
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from builtins import int, str
########################################Mathematical Framework to model biological system##########################################################
class LSystem:
    ##Input is in form of: (arg,arg2)
    #method extracts both arguments 1 and 2
    def toNumber(self,paran,i,chlist):#"("
            string=""
            for x in range(i,len(chlist)):
                    if chlist[x]==')':
                            i = x+1
                            a=string.split(',')
                            return float()
                    else:
                            string+=chlist[x]
            return float(string)
    def rotate(self,axis,theta):#3D functions dont work
            if axis=="Y":
                    m=self.currentPos
                    mr = m.dot(array([[cos(theta),0,sin(theta)],[0,1,0],[-sin(theta),0,cos(theta)]]))
                    self.currentPos=mr
            elif axis=="Z":
                    m=self.currentPos
                    mr = m.dot(array([[cos(theta),-sin(theta),0],[sin(theta),cos(theta),0],[0,0,1]]))
                    self.currentPos=mr
            return
        ######Meat of the class. This method draws from parsing the string or axioms. Uses Turtle language to draw to the screen.
    def drawLsystem(self,instructions):#, angle, distance):
            cmd=list(instructions)
            stack = []
            #for cmd in instructions:
            for i in range(len(cmd)):
                    if cmd[i] == 'F':#move forawrd Arg times
                            turtle.forward(self.getArg(i,cmd))
                    elif cmd[i]=="!":#change line width Arg size
                            turtle.width(self.getArg(i,cmd))
                    elif cmd[i]=="+":#turn right about v-axis
                            theta=self.getArg(i,cmd)
                            turtle.right(theta)
                    elif cmd[i]=='-':# turn left about v-axis
                            theta=self.getArg(i,cmd)
                            turtle.left(theta)
                    #elif cmd[i]=="/":# Roll left by Arg about L axis
                           # theta=self.getArg(i,cmd)
                            #self.rotate("Z", theta)
                            #covert 2D coordinated to 3d vector
                            #xpos=
                            #ypos=
                    #elif cmd[i]=="\\":# Roll left by Arg about L axis
                            #theta=self.getArg(i,cmd)
                            #xpos=
                            #ypos=
                    elif cmd[i]=='[':
                            stack.append((turtle.position(), turtle.heading()))
                    elif cmd[i]==']':
                            position, heading = stack.pop()
                            turtle.penup()
                            turtle.setposition(position)
                            turtle.setheading(heading)
                            turtle.pendown()
                    #else:
                    #	print("Exceptions:: ",cmd[i])#raise ValueError('Unknown Cmd: {}'.format(ord(cmd[i])))
                    turtle.update()

# creates a new string. Or Derives a new string until base case for 'n' generations.
    def createLSystem(self,numIters,axiom,var,meso=True):
            self.meso = meso
            startString = axiom
            endString = ""
            for i in range(numIters):
                    endString = self.processString(startString,var)
                    startString = endString

            return endString
    def getParan(self,ch,i,chlist):
            for x in range(i,len(chlist)):
                    if chlist[x]==')':
                            return x

    def processString(self,oldStr,var):
            newstr = ""
            #for ch in oldStr:
            ch=list(oldStr)
            for i in range(len(ch)):
                    newstr = newstr + self.applyRules(ch[i],i,ch,var)

            return newstr
    def getList(self,ch,i,chlist,var):#ch is A and i is the index of ch in chlist
            string=""
            for x in range(i+1,len(chlist)):
                    if chlist[x]==')':
                            list_a= string.split(',')
                            list_a.append(x)
                            return list_a
                    elif not chlist[x] == "(":
                            string+=chlist[x]
            return string.split(',')
    def getArg(self,i,chlist):#get x from A(x) or B(x) etc...
            arg=""
            x=i+1
            while x <= len(chlist)-1:
                    if chlist[x]==')':
                            return float(arg)
                    if not chlist[x] =='(' and not chlist[x]==')':
                            arg+=chlist[x]
                    x+=1
            return float(arg)
        ##Derives a new string.
    def applyRules(self,ch,i,chlist,var):
            newstr = ""
            if self.meso:
                    if ch=='A':
                            arg = self.getArg(i,chlist)
                            newstr= '[-(80)F(10)B('+str(arg)+')][+(80)F(10)B('+str(arg)+')]F(10)A('+str(float(arg+1))+')'
                    elif ch=='B':
                            arg = self.getArg(i,chlist)
                            if arg>0:
                                    newstr='F(10)B('+str(float(arg-1))+')'
                    else:
                            newstr=ch
                    return newstr
            if ch=='A':
                    a=self.getList(ch,i,chlist,var)

                    if float(a[0])>=self.min_constant:#a[1]=w0 and a[0]=s
                            w=float(a[1])
                            s=float(a[0])
                            a1=self.alpha1
                            a2=self.alpha2
                            A1=s*self.r1
                            A2=s*self.r2
                            A12=w*(self.qq**self.e)
                            A22=w*((1-self.qq)**self.e)
                            y1=self.phi1
                            y2=self.phi2
                            newstr="!("+str(w)+")F("+str(s)+")[+("+str(a1)+")/("+str(y1)+")A("+str(A1)+","+str(A12)+")][+("+str(a2)+")/("+str(y2)+")A("+str(A2)+","+str(A22)+")]" #'!('+str(a[1])+')F('+str(a[0])+')'+'[+('+str(self.alpha1)+')/('+str(self.phi1)+')A('+str(float(a[0])*self.r1)+','+str(float(a[1])*(self.qq**self.e))+')]'+'[+('+str(self.alpha2)+')/('+str(self.phi2)+')A('+str(float(a[0])*self.r2)+','+str(float(a[1])*((1-self.qq)**self.e))+')]'

                    return newstr
            else:
                    newstr = ch    # no rules apply so keep the character
            return newstr
        ##Main method used to start the program from gui inputs.
    def main(self,r1=.75,r2=.77,alpha1=35,alpha2=-35,phi1=0,phi2=0,w0=30,qq=.50,e=.40,min_const=0.0, n=10  ):
        turtle.title("Final Project: Computational Biology: D0L-System")
        turtle.setup(1000,600,0,0)
        turtle.pendown()
        #turtle.left(90)#point straight up
        self.min_constant=min_const
        self.alpha1= alpha1
        self.phi1= phi1
        self.alpha2= alpha2
        self.phi2=phi2;

        self.r1=r1
        self.r2=r2

        self.e=e
        self.qq=qq
        self.w0=w0
        turtle.speed(0)

        turtle.mode("logo")

        self.currentPos= array([0,0,0])
        self.Location=list()
        self.Location.append(self.currentPos)

        string = "A(100,"+str(w0)+")"
        instructions=self.createLSystem(int(n),string,[1,1,1,1,1],False)#takes in a meso value
        print (instructions)
        self.drawLsystem(instructions)
        print("done")
        print ("Now trying to create a new window for mesotonic")
        opt = input("PRESS any button to continue. Note the screen will be overwritten")

        m = Mesotonic()
        m.main(r1,r2,alpha1,alpha2,phi1,phi2,w0,qq,e,min_const,int(n))

        #print("Coord: ",self.Location[:])
        fig = plt.figure()
        ax = fig.add_subplot(111, projection='3d')
        ax.plot(self.Location[:],self.Location[:])
        plt.show()
class Mesotonic:#Class not implemented
    def main(self,r1=.75,r2=.77,alpha1=35,alpha2=-35,phi1=0,phi2=0,w0=30,qq=.50,e=.40,min_const=0.0, n=10  ):
        turtle.title("Final Project: Computational Biology: Mesotonic")
        turtle.setup(1000,600,0,0)
        turtle.pendown()
        #turtle.left(90)#point straight up
        self.min_constant=min_const
        self.alpha1= alpha1
        self.phi1= phi1
        self.alpha2= alpha2
        self.phi2=phi2;

        self.r1=r1
        self.r2=r2

        self.e=e
        self.qq=qq
        self.w0=w0
        turtle.speed(0)

        turtle.mode("logo")
        string = "A(100,"+str(self.w0)+")" #"F(10)A(0)"
        l = LSystem()
        instructions=l.createLSystem(10,string,[1,1,1,1,1], meso=True)
        print (instructions)
        l.drawLsystem(instructions)
        return
###################Display Window############################################
class Gui:
    # Begin to display a Frame with the options to vary the graphs
    def prob1(self):
        self.window_prob1=Toplevel()#the new GUI window for prob#1 HW 3
        self.window_prob1.protocol("WM_DELETE_WINDOW",self.close)
        Frame(self.window_prob1,width=600, height=0,takefocus=True).pack()
        #end default Values

        Label(self.window_prob1,text="r1").pack()
        self.r1=Spinbox(self.window_prob1, from_=.0001,to=1)
        self.r1.pack()

        Label(self.window_prob1,text="r2").pack()
        self.r2=Spinbox(self.window_prob1, from_=.0001,to=1)
        self.r2.pack()

        Label(self.window_prob1,text="alpha1").pack()
        self.alpha1=Spinbox(self.window_prob1, from_=-100,to=1000)
        self.alpha1.pack()

        Label(self.window_prob1,text="alpha2").pack()
        self.alpha2=Spinbox(self.window_prob1, from_=-100,to=1000)
        self.alpha2.pack()

        Label(self.window_prob1,text="phi1").pack()
        self.phi1=Spinbox(self.window_prob1, from_=-100,to=1000)
        self.phi1.pack()
        Label(self.window_prob1,text="phi2").pack()
        self.phi2=Spinbox(self.window_prob1, from_=-100,to=1000)
        self.phi2.pack()
        Label(self.window_prob1,text="w0").pack()
        self.w0=Spinbox(self.window_prob1, from_=-100,to=1000)
        self.w0.pack()
        Label(self.window_prob1,text="q").pack()
        self.q=Spinbox(self.window_prob1, from_=.0001,to=1000)
        self.q.pack()
        Label(self.window_prob1,text="e").pack()
        self.e=Spinbox(self.window_prob1, from_=.0001,to=1000)
        self.e.pack()
        Label(self.window_prob1,text="min").pack()
        self.min_constant=Spinbox(self.window_prob1, from_=.001,to=1000)
        self.min_constant.pack()
        Label(self.window_prob1,text="n").pack()
        self.n =Spinbox(self.window_prob1, from_=0,to=100)
        self.n.pack()
        self.graph=Button(self.window_prob1,text="Simulate plant growth",command=self.plot)
        self.graph.pack()
        self.window_prob1.focus_set()#need this to make modal window
        self.window_prob1.grab_set()
        self.window_prob1.transient(self.window)
        self.window_prob1.wait_window(self.window_prob1)#end modal
        return
	##########END OF PROB PYTHON FRAMES
	###########BEGIN OF FUNCTIONS TO GRAPH each PROBLEMS
    def plot(self):
        lsys = LSystem()
        #lsys.main()
        lsys.main(float(self.r1.get()),float(self.r2.get()),float(self.alpha1.get()),float(self.alpha2.get()),float(self.phi1.get()),float(self.phi2.get()),int(self.w0.get()),float(self.q.get()),float(self.e.get()),float(self.min_constant.get()),int(self.n.get()))
        return

############################CONSTRUCTOR##############################
    def __init__(self,window):#MAIN WINDOW
        self.window = window
        window.title("Final Project: D0L-System")
        window.geometry("500x200")
        window.eval('tk::PlaceWindow %s center' % window.winfo_toplevel())
        label = Label( window, text="Press one of the following options.", relief=RAISED ).pack()
        Button(window,text="Final Project: D0L-System",command=self.prob1).pack()
        return
    def on_closing(self):
        print("Program terminated.")
        root.destroy()
        return
    def close(self):
        self.window_prob1.grab_release()
        self.window_prob1.withdraw()
        return
    ##############################################################################
    ######Program runs here#######################################################
root = Tk()
window = Gui(root)
root.protocol("WM_DELETE_WINDOW",window.on_closing)#close all graphs
root.mainloop()


#!/usr/bin/python3

"""Script that prints the first State object from the database hbtn_0e_6_usa
"""

if __name__ == "__main__":
    import sys
    from sqlalchemy.orm import sessionmaker, Session
    from sqlalchemy import (create_engine)
    from model_state import Base, State

    engine = create_engine(f"mysql+mysqldb://{sys.argv[1]}\
:{sys.argv[2]}@localhost/{sys.argv[3]}")

    Session = sessionmaker(bind=engine)
    session = Session()

    q = session.query(State).first()

    if q:
        print(f"{q.id}: {q.name}")
    else:
        print("Nothing")


import src.lookml.lookml as lookml
import src.lookml.lkml as lkml
import unittest, copy
from pprint import pprint
import configparser, json
# from looker_sdk import client, models, methods
from looker_sdk import models, methods, init31
config = configparser.ConfigParser()
config.read('settings.ini')

class testScenariosGenerateFromSchema(unittest.TestCase):
    '''
        Objective: test end to end common use case scenarios
    '''
    def setUp(self):
        pass

    def test_create_view_from_info_schema(self):
        def column_to_dimension(col):
            if col['LOOKER_TYPE'] == 'time':
                tmpDim = lookml.DimensionGroup(
                        lookml.lookCase(col['COLUMN_NAME'])
                        )
            else:
                tmpDim = lookml.Dimension(lookml.lookCase(col['COLUMN_NAME']))
            tmpDim.setType(col['LOOKER_TYPE'])
            tmpDim.sql = "${TABLE}." + col['COLUMN_NAME']
            return tmpDim

        sdk = init31("tests/api.ini")
        sql = """
                SELECT
                    t.TABLE_NAME
                    ,t.TABLE_SCHEMA
                    ,t.COLUMN_NAME
                    ,t.DATA_TYPE
                    , CASE
                        WHEN t.DATA_TYPE IN ('TIMESTAMP_LTZ') THEN 'time'
                        WHEN t.DATA_TYPE IN ('FLOAT','NUMBER') THEN 'number'
                        ELSE 'string' END as "LOOKER_TYPE"
                FROM
                    information_schema.COLUMNS as t
                WHERE
                    1=1
                    AND t.table_name = 'ORDER_ITEMS'
                    AND t.table_schema = 'PUBLIC'
                LIMIT 100
        """
        query_config = models.WriteSqlQueryCreate(sql=sql, connection_id="snowlooker")
        query = sdk.create_sql_query(query_config)
        response = sdk.run_sql_query(slug=query.slug, result_format="json")
        response_json = json.loads(response)
        order_items = lookml.View('order_items_3')
        order_items.sql_table_name = 'PUBLIC.ORDER_ITEMS'

        for column in response_json:
            order_items + column_to_dimension(column)

        order_items.sumAllNumDimensions()
        order_items.addCount()

        proj = lookml.Project(
                 repo= config['github']['repo']
                ,access_token=config['github']['access_token']
                ,looker_host="https://profservices.dev.looker.com/"
                ,looker_project_name="russ_sanbox"
        )
        myNewFile = lookml.File(order_items)
        proj.put(myNewFile)
        proj.deploy()

    def tearDown(self):
        pass


import pytest
import requests

from main import send_requests

def get_cookie(x):
    resp = requests.get(x)
    print(resp.cookies)
    for k, v in resp.cookies.items():
        print(k + "=" + v)
    return x + 1

def test_answer():
    curPage = 0
    list_post = []
    result = send_requests(curPage, list_post)
    assert len(result) > 0

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


import os
import textwrap
import csv
import matplotlib.pyplot as plt
import numpy as np
from PIL.Image import Image as PilImage

def get_path(req_path):
    return os.path.realpath(os.path.join(os.path.dirname(__file__), req_path))

def createLabelsCsv(ds_path, csv_name):
    path_set = get_path(ds_path)
    folder_walk = os.walk(path_set)
    labels = next(folder_walk)[1]

    slashes = "/"

    if(os.name == "nt"):
        # i am on windows
        slashes = "\\"

    with open(os.path.realpath(os.path.join(path_set, csv_name)), "w") as f:
        csvwriter = csv.writer(f)
        for root, dirs, files in os.walk(path_set, topdown=True):
            for name in files:
                csvwriter.writerow(
                    [root.split(slashes)[-1]+slashes+name, root.split(slashes)[-1]])

    return labels

def get_latest_model(model_name):
    model_path = get_path(f'../../models/{model_name}')
    folder_walk = os.walk(model_path)
    return get_path(model_path + "/" + next(folder_walk)[2][-1])

def display_images(
        images: [PilImage],
        columns=4, width=20, height=8, max_images=11,
        label_wrap_length=50, label_font_size=8):

    if not images:
        print("No images to display.")
        return

    if len(images) > max_images:
        print(f"Showing {max_images} images of {len(images)}:")
        images = images[0:max_images]

    height = max(height, int(len(images)/columns) * height)
    plt.figure(figsize=(width, height))
    for i, image in enumerate(images):

        plt.subplot(int(len(images) / columns + 1), columns, i + 1)
        plt.imshow(image)

        if hasattr(image, 'filename'):
            title = image.filename
            if title.endswith("/"):
                title = title[0:-1]
            title = os.path.basename(title)
            title = textwrap.wrap(title, label_wrap_length)
            title = "\n".join(title)
            plt.title(title, fontsize=label_font_size)

    plt.show()

def imshow(img):
    npimg = img.numpy()[0]
    plt.imshow(np.transpose(npimg, (1, 2, 0)))
    plt.show()


"""Cache for SSH tunnels."""
import collections
import getpass
import logging
import os
import subprocess
import threading
import time
import weakref
from contextlib import suppress

import psutil

Context = collections.namedtuple('Context', (
    'uri',
    'interface',
    'local_socket',
    'remote_socket',
    'username',
    'hostname',
    'port',
    'identity_file',
    'ignore_hosts',
    'known_hosts',
))
Context.__new__.__defaults__ = (None, ) * len(Context._fields)

class Portal(collections.MutableMapping):
    """Expiring container for tunnels."""

    def __init__(self, sweap=25):
        """Construct portal, reap tunnels every sweap seconds."""
        self.data = collections.OrderedDict()
        self.sweap = sweap
        self.ttl = sweap * 2
        self.lock = threading.RLock()
        self._schedule_reaper()

    def __getitem__(self, key):
        """Given uri return tunnel and update TTL."""
        with self.lock:
            value, _ = self.data[key]
            self.data[key] = (value, time.time() + self.ttl)
            self.data.move_to_end(key)
            return value

    def __setitem__(self, key, value):
        """Store given tunnel keyed with uri."""
        if not isinstance(value, Tunnel):
            raise ValueError('Portals only support Tunnels.')

        with self.lock:
            self.data[key] = (value, time.time() + self.ttl)
            self.data.move_to_end(key)

    def __delitem__(self, key):
        """Remove and close tunnel from portal."""
        with self.lock:
            value, _ = self.data[key]
            del self.data[key]
            value.close()
            del value

    def __iter__(self):
        """Iterate tunnels."""
        with self.lock:
            values = self.data.values()

        for tunnel, _ in values:
            yield tunnel

    def __len__(self):
        """Return number of tunnels in portal."""
        with self.lock:
            return len(self.data)

    def _schedule_reaper(self):
        timer = threading.Timer(interval=self.sweap, function=self.reap)
        timer.setName('PortalReaper')
        timer.setDaemon(True)
        timer.start()

    def reap(self):
        """Remove tunnels who's TTL has expired."""
        now = time.time()
        with self.lock:
            reaped_data = self.data.copy()
            for entry in reaped_data.items():
                if entry[1][1] < now:
                    del self.data[entry[0]]
                else:
                    # StopIteration as soon as possible
                    break
            self._schedule_reaper()

class Tunnel():
    """SSH tunnel."""

    def __init__(self, context):
        """Construct Tunnel."""
        self.context = context
        self._closed = True

    @property
    def closed(self):
        """Is tunnel closed."""
        return self._closed

    def bore(self):
        """Create SSH tunnel from given context."""
        cmd = ['ssh', '-fNT']

        if logging.getLogger().getEffectiveLevel() == logging.DEBUG:
            cmd.append('-v')
        else:
            cmd.append('-q')

        if self.context.port:
            cmd.extend(('-p', str(self.context.port)))

        cmd.extend(('-L', '{}:{}'.format(self.context.local_socket,
                                         self.context.remote_socket)))

        if self.context.ignore_hosts:
            cmd.extend(('-o', 'StrictHostKeyChecking=no',
                        '-o', 'UserKnownHostsFile=/dev/null'))
        elif self.context.known_hosts:
            cmd.extend(('-o', 'UserKnownHostsFile={known_hosts}'.format(
                known_hosts=self.context.known_hosts)))

        if self.context.identity_file:
            cmd.extend(('-i', self.context.identity_file))

        cmd.append('{}@{}'.format(self.context.username,
                                  self.context.hostname))

        logging.debug('Opening tunnel "%s", cmd "%s"', self.context.uri,
                      ' '.join(cmd))

        tunnel = subprocess.Popen(cmd, close_fds=True)
        # The return value of Popen() has no long term value as that process
        # has already exited by the time control is returned here. This is a
        # side effect of the -f option. wait() will be called to clean up
        # resources.
        for _ in range(300):
            # TODO: Make timeout configurable
            if os.path.exists(self.context.local_socket) \
                    or tunnel.returncode is not None:
                break
            with suppress(subprocess.TimeoutExpired):
                # waiting for either socket to be created
                # or first child to exit
                tunnel.wait(0.5)
        else:
            raise TimeoutError(
                'Failed to create tunnel "{}", using: "{}"'.format(
                    self.context.uri, ' '.join(cmd)))
        if tunnel.returncode is not None and tunnel.returncode != 0:
            raise subprocess.CalledProcessError(tunnel.returncode,
                                                ' '.join(cmd))
        tunnel.wait()

        self._closed = False
        weakref.finalize(self, self.close)
        return self

    def close(self):
        """Close SSH tunnel."""
        logging.debug('Closing tunnel "%s"', self.context.uri)

        if self._closed:
            return

        # Find all ssh instances for user with uri tunnel the hard way...
        targets = [
            p
            for p in psutil.process_iter(attrs=['name', 'username', 'cmdline'])
            if p.info['username'] == getpass.getuser()
            and p.info['name'] == 'ssh'
            and self.context.local_socket in ' '.join(p.info['cmdline'])
        ]  # yapf: disable

        # ask nicely for ssh to quit, reap results
        for proc in targets:
            proc.terminate()
        _, alive = psutil.wait_procs(targets, timeout=300)

        # kill off the uncooperative, then report any stragglers
        for proc in alive:
            proc.kill()
        _, alive = psutil.wait_procs(targets, timeout=300)

        for proc in alive:
            logging.info('process %d survived SIGKILL, giving up.', proc.pid)

        with suppress(OSError):
            os.remove(self.context.local_socket)
        self._closed = True


import sys
from os.path import abspath, join, dirname

sys.path.insert(0, join(abspath( dirname(__file__)), '../'))
from model.dataModel import CASSI, HSISingleData, X0FromLS, get_rand_mask, normalizeByMax, repeatDim

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Button

DATASETNAME="Harvard"
# DATASETNAME="ICVL"

import h5py

class showHSI:
    def __init__(self, Xdata, Ydata=None):
        self.Xdata = Xdata
        self.Ydata = Ydata
        if self.Ydata is not None:
            assert Xdata.shape == Ydata.shape
            self.vmin = min(np.min(self.Xdata), np.min(self.Ydata))
            self.vmax = min(np.max(self.Xdata), np.max(self.Ydata))
            self.fig, self.ax = plt.subplots(1, 2)
            self.axX = self.ax[0]
            self.axY = self.ax[1]
        else:
            self.vmin = np.min(self.Xdata)
            self.vmax = np.max(self.Xdata)
            self.fig, self.ax = plt.subplots(1, 1)
            self.axX = self.ax

        print(self.vmin,self.vmax)

        self.currentDataIndex = 0
        if len(Xdata.shape) == 2:
            self.dataLen = 1
            self.currentXData = Xdata
        elif len(Xdata.shape) == 3:
            self.dataLen = Xdata.shape[2]
            self.currentXData = Xdata[:,:,self.currentDataIndex]
        else:
            print(f"UnExpect Shape: {Xdata.shape}")

        self.Xplt = self.axX.imshow(self.currentXData, cmap="gray", vmin=self.vmin, vmax=self.vmax)
        self.axX.set_title(f"X: {self.currentDataIndex}/{self.dataLen-1}")
        self.fig.colorbar(self.Xplt, ax=self.axX)

        if self.Ydata is not None:
            self.currentYData = Ydata[:,:,self.currentDataIndex]
            self.Yplt = self.axY.imshow(self.currentYData, cmap="gray", vmin=self.vmin, vmax=self.vmax)
            self.axY.set_title(f"Y: {self.currentDataIndex}/{self.dataLen-1}")
            self.fig.colorbar(self.Yplt, ax=self.axY)
        if len(Xdata.shape) == 3:
            plt.subplots_adjust(bottom=0.2)
            self.setButtons()

    def show(self):
        plt.show()

    def _next(self, event):
        if self.currentDataIndex < self.dataLen-1:
            self.currentDataIndex += 1
            self.setData()

    def _prev(self, event):
        if self.currentDataIndex > 0:
            self.currentDataIndex -= 1
            self.setData()

    def setData(self):
        self.currentXData = self.Xdata[:,:,self.currentDataIndex]
        self.Xplt.set_data(self.currentXData)
        self.axX.set_title(f"X: {self.currentDataIndex}/{self.dataLen-1}")
        if self.Ydata is not None:
            self.currentYData = self.Ydata[:,:,self.currentDataIndex]
            self.Yplt.set_data(self.currentYData)
            self.axY.set_title(f"Y: {self.currentDataIndex}/{self.dataLen-1}")
        plt.draw()

    def setButtons(self):
        ax_prev = plt.axes([0.41, 0.05, 0.07, 0.075])
        ax_next = plt.axes([0.51, 0.05, 0.07, 0.075])
        # btn_prev = Button(ax_prev,"<")
        self.prev_button = Button(ax_prev,"<")
        self.prev_button.on_clicked(self._prev)
        self.next_button = Button(ax_next,">")
        self.next_button.on_clicked(self._next)

def cutAndSave(file, num = 4):
    """ 从大的训练数据上切下来一部分以进行观察
    """
    dataset = HSISingleData(file, (3, 0, 1, 2), lambda x:X0FromLS(*CASSI(x), labelName="label"))
    cal_x, orl_y = np.zeros((num, 48, 48, 31)), np.zeros((num, 48, 48, 31))
    for i in range(4):
        cal_x[i], orl_y[i] = dataset[i]
        # print("X:\n", cal_x[i])
        # print("Y:\n", orl_y[i])
        # print("\n")
    np.save(f"save/cutData/cal_x_{num}", cal_x)
    np.save(f"save/cutData/orl_y_{num}", orl_y)

def getCut(num = 4):
    """ 读取切下来的数据
    """
    return np.load(f"./save/cutData/cal_x_{num}.npy"), np.load(f"save/cutData/orl_y_{num}.npy")

if __name__ == "__main__":
    # 测试showHSI的可用性
    # arr3 = np.ones((10,10,3))
    # arr = np.arange(300.)
    # arr3 = arr.reshape((10,10,3),order='F')/450.0
    # arr3y = np.ones((10,10,3))
    # arr = np.arange(150,450.)
    # arr3y = arr.reshape((10,10,3),order='F')/450/0
    # showHSI(arr3,arr3y).show()

    # 测试直接读取原始48*48*31的数据
    # cutAndSave(f'./data/Train/Training_Data_{DATASETNAME}_48.mat')
    # data   = h5py.File(file, 'r')
    # labels = data['label']                        # 获取其中数据标签
    # del data
    # new_labels = np.transpose(labels, (0, 3, 2, 1))   # 调整维度的顺序（batch_size，h,w,c）
    # one_label = new_labels[0]
    # showHSI(one_label).show()

    # 测试生成的mask
    # mask = get_rand_mask(48, 48, 31)
    # showHSI(mask).show()

    # 测试使用最小二乘后的 X0
    # file = f'./data/Train/Training_Data_{DATASETNAME}_48.mat'
    # x,y = getCut()
    # showHSI(x[0], y[0]).show()

    # file = f'./data/Train/Training_Data_{DATASETNAME}_48.mat'
    # data = HSISingleData(file, (3, 0, 1, 2), labelName="label")
    # 测试集中512*512的原始数据
    file = f'./data/Test/{DATASETNAME}48/44.mat'
    data = HSISingleData(file, None, lambda x:X0FromLS(*CASSI(x)), labelName="hyper_image")
    showHSI(data[0][0], data[0][1]).show()


import sqlite3

#Creamos la base de datos que contendrá los menús y sus precios

conexion = sqlite3.connect("Pizzeria/DataBase/menus.db")

cursor = conexion.cursor()

cursor.execute(
    """CREATE TABLE IF NOT EXISTS menus (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        nombre TEXT NOT NULL,
        precio INTEGER NOT NULL,
        elementos TEXT NOT NULL
    )"""
)

conexion.commit()
conexion.close()


#!/usr/bin/python3
# -*- coding:utf-8 -*-

import os
import sys
import configparser
import fasteners
import traceback

from rpi_epd2in7.epd2in7 import EPD
from PIL import Image,ImageDraw,ImageFont

LOCKFILE = '/tmp/lockepaper'

# 176x264
FONT_FILE = '/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf'
TITLE_Y = 0
TITLE_SZ = 18

def disp_qrcode(png_file, title_str, clear_frame):
    lock = fasteners.InterProcessLock(LOCKFILE)
    if not lock.acquire():
        print('now using')
        return

    try:
        print('init')
        epd = EPD()
        epd.init()

        if clear_frame:
            print('clear')
            epd.Clear(0xFF)

        else:
            print('display')
            image = Image.new('1', (epd.width, epd.height), 255)
            image = _disp_image(epd, image, png_file)
            image = _disp_title(epd, image, title_str)
            epd.display(epd.getbuffer(image))

        epd.sleep()
    except:
        print('exception')
        print('traceback.format_exc():\n%s' % traceback.format_exc())

    lock.release()

def _disp_image(epd, image, png_file):
    if os.path.isfile(png_file):
        print('PNG file' + png_file)
        img = Image.open(png_file);
        px = int((epd.width - img.size[0]) / 2)
        py = int((epd.height - img.size[1]) / 2)
        image.paste(img, (px, py))
    return image

def _disp_title(epd, image, title_str):
    if len(title_str) > 0:
        print('title')
        print(title_str)
        draw = ImageDraw.Draw(image)
        imgfont = ImageFont.truetype(FONT_FILE, TITLE_SZ)
        height = TITLE_Y
        for title in title_str:
            if title.isdecimal():
                title = '{:,}'.format(int(title))
            draw_sz = draw.textsize(title, font = imgfont)
            draw_pos = ((epd.width - draw_sz[0]) / 2, height)
            draw.text(draw_pos, title, font = imgfont, fill = 0)
            height += TITLE_SZ
    return image

# arg none: clear
# arg[1]: PNG filename
# arg[2..]: title
def main():
    print('start')
    clear_frame = False
    png_file = ''
    title_str = ''
    if len(sys.argv) < 3:
        clear_frame = True
    else:
        png_file = sys.argv[1]
        title_str = sys.argv[2:]

    disp_qrcode(png_file, title_str, clear_frame)

if __name__ == '__main__':
    main()


#!/usr/bin/env python3

from pathlib import Path
from vunit import VUnit

VU = VUnit.from_argv()
VU.add_osvvm()
VU.add_verification_components()

SRC_PATH = Path(__file__).parent / "src"

VU.add_library("lib").add_source_files(
    [SRC_PATH / "*.vhd", SRC_PATH / "test" / "*.vhd"]
)
print([SRC_PATH / "*.vhd", SRC_PATH / "test" / "*.vhd"])

VU.main()


import argparse
import os.path as osp
import mrcfile
import numpy as np

def getMaskCorrectedFSC(array1, array2, mask, samplingRate,
                        nIter=1, rndThr=0.8, rndOffset=4):

    unmaskedArray1 = np.fft.fftshift(np.fft.fftn(array1))
    unmaskedArray2 = np.fft.fftshift(np.fft.fftn(array2))

    if mask is not None:
        maskedArray1 = np.fft.fftshift(np.fft.fftn(array1*mask))
        maskedArray2 = np.fft.fftshift(np.fft.fftn(array2*mask))

    iSize = array1.shape[0]
    iSize2 = int(iSize/2)

    xV = np.arange(iSize) - iSize2
    XXmesh = np.meshgrid(xV, xV, xV, copy=False, indexing='ij')

    radii = np.zeros((iSize,)*3)

    for k in range(3):
        radii += XXmesh[k]**2
    radii = np.sqrt(radii)

    nShells = iSize2 + 1
    shellList = [None]*nShells

    for k in range(nShells):
        shellList[k] = np.where((radii >= k) * (radii < (k+1)))

    unmaskedCCF = np.real(unmaskedArray1 * unmaskedArray2.conj())
    unmaskedInt1 = np.real(unmaskedArray1 * unmaskedArray1.conj())
    unmaskedInt2 = np.real(unmaskedArray2 * unmaskedArray2.conj())

    if mask is not None:
        maskedCCF = np.real(maskedArray1 * maskedArray2.conj())
        maskedInt1 = np.real(maskedArray1 * maskedArray1.conj())
        maskedInt2 = np.real(maskedArray2 * maskedArray2.conj())

    unmaskedCCF_1D = np.zeros(nShells)
    unmaskedInt1_1D = np.zeros(nShells)
    unmaskedInt2_1D = np.zeros(nShells)

    if mask is not None:
        maskedCCF_1D = np.zeros(nShells)
        maskedInt1_1D = np.zeros(nShells)
        maskedInt2_1D = np.zeros(nShells)

    for k in range(nShells):
        unmaskedCCF_1D[k] = unmaskedCCF[shellList[k]].sum()
        unmaskedInt1_1D[k] = unmaskedInt1[shellList[k]].sum()
        unmaskedInt2_1D[k] = unmaskedInt2[shellList[k]].sum()
        if mask is not None:
            maskedCCF_1D[k] = maskedCCF[shellList[k]].sum()
            maskedInt1_1D[k] = maskedInt1[shellList[k]].sum()
            maskedInt2_1D[k] = maskedInt2[shellList[k]].sum()

    # FSC profiles
    unmaskedFSC = unmaskedCCF_1D/np.sqrt(unmaskedInt1_1D * unmaskedInt2_1D)
    maskedFSC = None
    correctedFSC = None
    phaseRndFSC = None
    if mask is not None:
        maskedFSC = maskedCCF_1D/np.sqrt(maskedInt1_1D * maskedInt2_1D)

        # Index for phase rondimization
        rndIdx = np.where(unmaskedFSC < rndThr)[0]

        if len(rndIdx) == 0:
            rndIdx = nShells - 1
        elif rndIdx[0] > 0:
            rndIdx = rndIdx[0]
        else:
            rndIdx = rndIdx[1]

        print(f'Phase randomization at pixel {rndIdx} =\
              {(iSize*samplingRate)/rndIdx:.2g} Å')

        rndMask = radii >= rndIdx
        nRnd = rndMask.sum()
        angleArray1 = np.angle(unmaskedArray1)
        angleArray2 = np.angle(unmaskedArray2)
        absArray1 = np.abs(unmaskedArray1)
        absArray2 = np.abs(unmaskedArray2)

    # We repeat the phase randomization to average

        correctedFSCum = np.zeros(nShells)
        phaseRndFSCum = np.zeros(nShells)

        for k in range(nIter):
            rndAngleArray1 = angleArray1.copy()
            rndAngleArray2 = angleArray2.copy()

            rndAngleArray1[rndMask] = np.random.rand(nRnd)*2*np.pi
            rndAngleArray2[rndMask] = np.random.rand(nRnd)*2*np.pi

            rndArray1FT = absArray1 * np.exp(1j*rndAngleArray1)
            rndArray2FT = absArray2 * np.exp(1j*rndAngleArray2)

            rndArray1 = np.fft.ifftn(np.fft.ifftshift(rndArray1FT))
            rndArray2 = np.fft.ifftn(np.fft.ifftshift(rndArray2FT))

            maskedRndArray1FT = np.fft.fftshift(np.fft.fftn(rndArray1*mask))
            maskedRndArray2FT = np.fft.fftshift(np.fft.fftn(rndArray2*mask))

            maskedRndCCF = np.real(maskedRndArray1FT * maskedRndArray2FT.conj())
            maskedRndInt1 = np.real(maskedRndArray1FT * maskedRndArray1FT.conj())
            maskedRndInt2 = np.real(maskedRndArray2FT * maskedRndArray2FT.conj())

            maskedRndCCF_1D = np.zeros(nShells)
            maskedRndInt1_1D = np.zeros(nShells)
            maskedRndInt2_1D = np.zeros(nShells)
            for k in range(nShells):
                maskedRndCCF_1D[k] = maskedRndCCF[shellList[k]].sum()
                maskedRndInt1_1D[k] = maskedRndInt1[shellList[k]].sum()
                maskedRndInt2_1D[k] = maskedRndInt2[shellList[k]].sum()

            maskedRndFSC = maskedRndCCF_1D / \
                np.sqrt(maskedRndInt1_1D * maskedRndInt2_1D)

            # Mask-corrected FSC
            correctedFSCTmp = maskedFSC.copy()
            correctedFSCTmp[rndIdx+rndOffset:] = \
                (maskedFSC[rndIdx+rndOffset:] - maskedRndFSC[rndIdx+rndOffset:]) /\
                (1 - maskedRndFSC[rndIdx+rndOffset:])
            correctedFSCum += correctedFSCTmp
            phaseRndFSCum += maskedRndFSC
            correctedFSC = correctedFSCum/nIter
            phaseRndFSC = phaseRndFSCum/nIter

    inv_resolution = np.arange(nShells) / (iSize*samplingRate)
    return inv_resolution, unmaskedFSC, maskedFSC, phaseRndFSC, correctedFSC

def determine_fsc_resolution(inv_resolution, fsc_values, threshold=0.143):

    index = np.where(fsc_values < threshold)[0][0]  #TODO: add try except for IndexError, probably caused by too tight mask
    inv_res_at_threshold = inv_resolution[index - 1] + (inv_resolution[index] - inv_resolution[index - 1]) * (
                threshold - fsc_values[index - 1]) / (fsc_values[index] - fsc_values[index - 1])
    res_at_threshold = 1 / inv_res_at_threshold
    return inv_res_at_threshold, res_at_threshold

def plot_fsc(ax, inv_resolution, fsc_values, inv_res_at_threshold, label):
    ax.plot(inv_resolution, fsc_values, label=label)
    ax.axvline(x=inv_res_at_threshold, color='r', linestyle='-.')
    ticks = inv_resolution[::10]
    ticks[0]=1/999
    ax.set_xticks(ticks)
    ax.set_xticklabels(np.round(1 / ticks, decimals=2))
    ax.set_xlabel('Resolution (Å)')
    ax.set_ylabel('FSC')

def getFSCResolution(array1, array2, mask, samplingRate, resolution_threshold=0.143):
    inv_resolution, unmaskedFSC, maskedFSC, phaseRndFSC, correctedFSC = \
        getMaskCorrectedFSC(array1, array2, mask, samplingRate)
    _, res_unmasked = determine_fsc_resolution(inv_resolution, unmaskedFSC, threshold=resolution_threshold)
    if maskedFSC is not None:
        _, res_masked = determine_fsc_resolution(inv_resolution, maskedFSC, threshold=resolution_threshold)
        _, res_phaseRnd = determine_fsc_resolution(inv_resolution, phaseRndFSC, threshold=resolution_threshold)
        _, res_corrected = determine_fsc_resolution(inv_resolution, correctedFSC, threshold=resolution_threshold)
    else:
        res_masked, res_phaseRnd, res_corrected = [None]*3
    return res_unmasked, res_masked, res_phaseRnd, res_corrected

if __name__ == "__main__":

    parser = argparse.ArgumentParser("FSC calculation")
    parser.add_argument("-i", "--inputMap", required=True, type=str, help="The input map or half map 1")
    parser.add_argument("-r", "--referenceMap", required=True, type=str, help="The reference map or half map 2")
    parser.add_argument("-m", "--mask", required=False, type=str, help="A mask to limit computations. It shoud have soft edges")
    parser.add_argument("-p", "--show_plot", action="store_true",
                        help="Show an interactive FSC resolution plot ")
    parser.add_argument("-o", "--plot_fname", required=False, type=str,
                        help="The filename for the plot to be saved")
    parser.add_argument("-t", "--resolution_threshold", required=False, type=float, default=0.143,
                        help="The thresold for the FSC resolution determination")
    args = parser.parse_args()

    threshold = args.resolution_threshold

    with mrcfile.open(osp.expanduser(args.inputMap)) as f:
        array1 = f.data.copy()
        samplingRate = float(f.voxel_size.x)
    with mrcfile.open(osp.expanduser(args.referenceMap)) as f:
        array2 = f.data.copy()
        assert round(samplingRate,3) == round(float(f.voxel_size.x),3), ("Error, input and reference map have different"
                                                                 " sampling rate")
    if args.mask is not None:
        with mrcfile.open(osp.expanduser(args.mask)) as f:
            mask = f.data.copy()
            assert round(samplingRate,3) == round(float(f.voxel_size.x),3), (f"Error, input map and mask have "
                                                                             f"different sampling rate "
                                                                             f"{samplingRate, f.voxel_size.x}")
    else:
        mask=None
    (inv_resolution, unmaskedFSC, maskedFSC,
     phaseRndFSC, correctedFSC) = getMaskCorrectedFSC(array1, array2, mask, samplingRate)
    inv_res_at_threshold, resolution = determine_fsc_resolution(inv_resolution, unmaskedFSC, threshold=threshold)
    print(f"Resolution {resolution} Å  (Sampling rate {samplingRate})")

    from matplotlib import pyplot as plt
    fig, ax = plt.subplots()
    ax.axhline(y=threshold, color='r', linestyle='--')
    plot_fsc(ax, inv_resolution, unmaskedFSC, inv_res_at_threshold,
             label=f'Unmasked FSC ({np.round(1/inv_res_at_threshold, 2)})')

    if maskedFSC is not None:
        inv_res_at_threshold, maskedResolution = determine_fsc_resolution(inv_resolution, maskedFSC)
        print(f"Masked Resolution {maskedResolution}")
        plot_fsc(ax, inv_resolution, maskedFSC, inv_res_at_threshold,
                 label=f'Masked FSC ({np.round(1/inv_res_at_threshold, 2)})')

    ax.legend()
    plt.show()

"""
-i ~/tmp/ConorData/bound/halfs/docks_simulted_micSim0_half1.mrc -r ~/tmp/ConorData/bound/halfs/docks_simulted_micSim0_half2.mrc
"""


from nltk.stem.snowball import SnowballStemmer
import pandas
import re

# genres.txt - файл с жанрами фильмов, release_origin_director_cast.csv - с годом, страной, режиссером и актерами
# (эти файлы были заранее избавлены от лишних слов, примечаний, переносов строки и т. д. с использованием регулярных
#  выражений).
# wikiplots.txt - файл со всеми данными по фильмам.
# В результате работы программы получаются файлы X.txt и Y.txt, которые используются в main.py.

def clean(s):  # заменить написание через дефис на слитное, оставить только буквы и заменить их на строчные
    s = s.lower()
    s = s.replace('-', '')
    s = re.sub('\W', ' ', s)
    s = re.sub('\s{2,}', ' ', s)
    s = s.strip(' ')
    return s

stemmer = SnowballStemmer("english")  # для нахождения основы слов
data = pandas.read_csv('wikiplots.txt')
data['Plot'] = data['Plot'].map(lambda pl: clean(pl))
data['Title'] = data['Title'].map(lambda pl: clean(pl))
data['Plot'] = data.Plot.map(lambda x: ' '.join([stemmer.stem(y) for y in x.split(' ')]))
data['Title'] = data.Title.map(lambda x: ' '.join([stemmer.stem(y) for y in x.split(' ')]))
rel_orig_dir_cast = pandas.read_csv('release_origin_director_cast.csv')
hdrs = ['Origin/Ethnicity', 'Director', 'Cast']
for hdr in hdrs:  # например, "James Smith, Jane Smith" -> "jamessmith janesmith"
    rel_orig_dir_cast[hdr] = rel_orig_dir_cast[hdr].map(lambda pl: str(pl))
    rel_orig_dir_cast[hdr] = rel_orig_dir_cast[hdr].str.lower()
    rel_orig_dir_cast[hdr] = rel_orig_dir_cast[hdr].str.replace(' ', '', regex=False)
    rel_orig_dir_cast[hdr] = rel_orig_dir_cast[hdr].str.replace(',', ' ', regex=False)
    rel_orig_dir_cast[hdr] = rel_orig_dir_cast[hdr].str.replace('^nan$', '')
gen = pandas.read_csv('genres.txt')
gen['Genre'] = gen['Genre'].map(lambda gg: set(gg.split()))
genres = {'kids/family', 'war', 'thriller', 'western', 'mystery', 'action/adventure', 'fantasy/folklore',
          'romance', 'unknown', 'crime', 'history', 'drama', 'comedy', 'horror', 'short',
          'animation/anime', 'musical/dance', 'sport', 'sci-fi', 'spy'}  # список жанров
d = dict()
for g in genres:
    d[g] = []
for g in genres:
    for v in gen['Genre'].values:
        if g in v:
            d[g].append(1)
        else:
            d[g].append(0)
y = pandas.DataFrame.from_dict(d)  # 1 - если является фильмом данного жанра, 0 - в противном случае
result = pandas.concat([data[['Title']], rel_orig_dir_cast, data[['Plot']]], axis=1)
for i in range(len(y.index)):  # исключить фильмы неизвестного жанра
    if y['unknown'][i] == 1:
        y = y.drop(i)
        result = result.drop(i)
y = y.drop(columns='unknown')
result.to_csv('X.txt', index=False)
y.to_csv('Y.txt', index=False)


# -*- coding: UTF-8 -*-
import datetime
import traceback

from pandas import DataFrame

from calculations.common.constants import constants
from calculations.common.exceptions.core_exception import CoreException
from calculations.core import LOG
from calculations.core.interceptor import interceptor
from calculations.repository.dailystock_repo import DailyStockRepo
from projects.common.utils.date_utils import DateUtils

# # BrokerID="capital" 、 "yuanta" 、 "kgi"
# @interceptor
# def getSIDMatch(Date, sid, BrokerID="capital", DataPath="C:/Data/"):
#     """
#     # 取得當月的日K
#     # 持續取得指定股票代碼的成交資訊
#     # 設定券商名稱
#     """
#     for i in tailer.follow(open(DataPath + BrokerID + "/" + Date + "/" + sid + "_Match.txt"), 0):
#         j = i.strip("\n").split(",")
#         yield j

# @interceptor
# def getLastSIDMatch(Date, sid, BrokerID="capital", DataPath="C:/Data/"):
#     """ 取得指定股票代碼的最新一筆成交資訊 """
#     tmpfiledata = open(DataPath + BrokerID + "/" + Date + "/" + sid + "_Match.txt").readlines()
#     tmpfiledata.reverse()
#     for i in tmpfiledata:
#         j = i.strip("\n").split(",")
#         return j

@interceptor
def getDayKBar(sid, yearmonth) -> DataFrame:
    """ 取得股票日Ｋ  """
    data = DailyStockRepo.find_month_by_symbol_and_yearmonth(sid, yearmonth)
    return data

    # # 透過API取得資料
    # html = urlopen("http://www.twse.com.tw/exchangeReport/STOCK_DAY?response=json&date=" + date + "&stockNo=" + sid)
    # content = html.read().decode("utf-8")
    # jcontent = json.loads(content)
    # data = jcontent["data"]
    # # 將資料整理成可用的資訊
    # data = [[i[0], float(i[3].replace(",", "")), float(i[4].replace(",", "")), float(i[5].replace(",", "")),
    #          float(i[6].replace(",", "")), float(i[8].replace(",", ""))] for i in data]
    # return data

@interceptor
def getDayKBarbyNum(sid, daynum):
    """ 取得往前推算n日的日K """
    tmpday = DateUtils.today(constants.YYYYMM)

    # 首先取得當月資料
    dayK = getDayKBar(sid, tmpday)

    # 若資料數量不足，則更往前補足資料
    while len(dayK) < daynum:
        tmpday = (datetime.datetime.strptime((tmpday[:6] + "01"), constants.YYYYMMDD) - datetime.timedelta(
            days=1)).strftime(constants.YYYYMMDD)
        tmpdata = getDayKBar(sid, tmpday)
        dayK = tmpdata + dayK
        # time.sleep(3)

    # 回傳指定天數的日K
    return dayK[-daynum:]

if __name__ == '__main__':
    """ ------------------- App Test ------------------- """
    try:
        symbol = '2330'
        year_month = '202102'
        num = 5

        df = getDayKBar(symbol, year_month)
        # df = getDayKBarbyNum(symbol, num)
        LOG.debug(df)
    except Exception as e:
        CoreException.show_error(e, traceback.format_exc())


import json
import uuid
import datetime
import xlsxwriter
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
from io import BytesIO
import mysql.connector

# import xlsx2pdf.transformators

def generate_report(cursor, id, workbook, need_attendance=False, need_remarks='', need_pdf=False):
    def with_permission_fun(from_prefix, to_prefix, from_suffix, to_suffix):
        prefix = []
        if from_prefix and to_prefix:
            prefix.append(f"{from_prefix.strftime('%d-%m-%Y')} to {to_prefix.strftime('%d-%m-%Y')}")
        elif not from_prefix and to_prefix:
            prefix.append(f"{to_prefix.strftime('%d-%m-%Y')}")
        elif from_prefix and not to_prefix:
            prefix.append(f"{from_prefix.strftime('%d-%m-%Y')}")

        suffix = []
        if from_suffix and to_suffix:
            suffix.append(f"{from_suffix.strftime('%d-%m-%Y')} to {to_suffix.strftime('%d-%m-%Y')}")
        elif not from_suffix and to_suffix:
            suffix.append(f"{to_suffix.strftime('%d-%m-%Y')}")
        elif from_suffix and not to_suffix:
            suffix.append(f"{from_suffix.strftime('%d-%m-%Y')}")

        on_permission = []
        if prefix and suffix:
            on_permission.append(f"{prefix[0]} & {suffix[0]}")
        elif not prefix and suffix:
            on_permission.append(suffix[0])
        elif prefix and not suffix:
            on_permission.append(prefix[0])

        return on_permission

    def attendence_register(dep, year):
        query = "SELECT register_name FROM `attendance_register` WHERE department = %s AND year = %s;"
        cursor.execute(query, (dep, year))
        data = cursor.fetchall()

        return data

    def getdata(type, id):
        if type == 'el':
            cursor.reset()
            query = "SELECT `from`, `to`, `from_prefix`, `to_prefix`, `from_suffix`, `to_suffix`, `date_of_rejoin`, `total` FROM " + type + " WHERE id=" + id + "; "
            cursor.execute(query)
            data_final = cursor.fetchall()

            on_permission = []
            for i in data_final:
                on_permission.append(with_permission_fun(i[2], i[3], i[4], i[5]))
            remark = []
            for i in data_final:
                remark.append(attendence_register(data[0][2], i[0].year))
            remark = [item[0] for sublist in remark for item in sublist]

            data_final_updated = []
            for i, sublist in enumerate(data_final):
                columns_to_keep = sublist[:2] + ((on_permission[i],) if on_permission else ()) + sublist[6:] + ((remark[i],) if remark else ())
                data_final_updated.append(columns_to_keep)
            data_final_updated = [sublist + (type,) for sublist in data_final_updated]
            return data_final_updated
        else:
            cursor.reset()
            query = "SELECT `from`,`to`,`total`, `medical_fittness_on`, `from_prefix`, `to_prefix`, `from_suffix`, `to_suffix`, `doj` FROM " + type + " WHERE id=" + id + ";; "
            cursor.execute(query)
            data_final = cursor.fetchall()
            on_permission = []
            for i in data_final:
                on_permission.append(with_permission_fun(i[4], i[5], i[6], i[7]))
            remark = []
            for i in data_final:
                remark.append(attendence_register(data[0][2], i[0].year))
            remark = [item[0] for sublist in remark for item in sublist]
            data_final_updated = []
            for i, sublist in enumerate(data_final):
                columns_to_keep = sublist[:4] + ((on_permission[i],) if on_permission else ()) + sublist[8:] + ((remark[i],) if remark else ())
                data_final_updated.append(columns_to_keep)
            data_final_updated = [sublist + (type,) for sublist in data_final_updated]
            return data_final_updated

    def generate_random_name():
        random_uuid = str(uuid.uuid4())

        random_name = random_uuid[:8]
        return random_name

    query = f"SELECT * FROM `staff` WHERE id={id};"
    cursor.execute(query)
    data = cursor.fetchall()
    data_el = getdata('el', id)
    data_ml = getdata('ml', id)
    data_lop = getdata('lop', id)
    data_mtl = getdata('mtl', id)
    sorted_data = sorted(data_lop + data_el + data_ml + data_mtl, key=lambda x: x[0])
    xlname = generate_random_name()
    worksheet = workbook.add_worksheet("EL")
    worksheet.set_column('A:L', 12)

    merge_ranges = [
        (2, 0, 2, 3),
        (2, 4, 2, 11),
        (3, 0, 3, 3),
        (3, 4, 3, 11),
        (4, 0, 4, 3),
        (4, 4, 4, 11),
        (5, 0, 5, 3),
        (5, 4, 5, 11),
        (6, 0, 6, 3),
        (6, 4, 6, 11),
        (7, 0, 7, 11),
        (8, 0, 8, 4),
        (8, 5, 8, 10)

    ]
    cell_format_center = workbook.add_format({
        'align': 'center',  # Center alignment
        'text_wrap': True,  # Wrap text
        'valign': 'vcenter',  # Center vertically
        'border': 1
    })
    cell_format_left = workbook.add_format({
        'align': 'center',  # Center alignment
        'text_wrap': True,  # Wrap text
        'valign': 'vcenter',  # Center vertically
        'border': 1
    })

    # Perform the defined merges
    worksheet.merge_range(*(0, 0, 0, 11), 'Annexure -II', cell_format_center)
    worksheet.merge_range(*(1, 0, 1, 11), 'Anna univeristy chennai', cell_format_center)
    worksheet.merge_range(*merge_ranges[0], 'Name', cell_format_center)
    worksheet.merge_range(*merge_ranges[1], data[0][1], cell_format_left)
    worksheet.merge_range(*merge_ranges[2], 'Designation', cell_format_left)
    worksheet.merge_range(*merge_ranges[3], '', cell_format_left)
    worksheet.merge_range(*merge_ranges[4], 'Department', cell_format_left)
    worksheet.merge_range(*merge_ranges[5], "MBA" if data[0][2] == "MS" else data[0][2], cell_format_left)
    worksheet.merge_range(*merge_ranges[6], 'Date of joining in service', cell_format_left)
    worksheet.merge_range(*merge_ranges[7], data[0][3].strftime('%d-%m-%Y'), cell_format_left)
    worksheet.merge_range(*merge_ranges[8], 'Campus', cell_format_left)
    worksheet.merge_range(*merge_ranges[9], 'ANNA UNIVERSITY REGIONAL CAMPUS   -   TIRUNELVELI', cell_format_center)
    worksheet.merge_range(*merge_ranges[10], 'DETAILS OF LEAVE', cell_format_left)
    worksheet.merge_range(*merge_ranges[11], 'Earned Leave  (EL)', cell_format_left)
    worksheet.merge_range(*merge_ranges[12], 'Medical Leave  (ML)  /  Maternity Leave  (MTL) / Loss of pay (LOP)',
                          cell_format_left)
    worksheet.write('L9', 'Type of Leave', cell_format_left)
    if need_attendance:
        worksheet.write('M10', 'Attendance Register', cell_format_left)

    head_list = [['From', 'To', 'with permission on', 'Date of Joining on Duty after EL', 'Total No. of Days',
                  'From', 'To', 'Total No. of Days', 'Medical Fitness on', 'with permission on',
                  'Date of Joining on Duty after leave', '(ML/ EL/LOP/MTL)']
                 ]

    # Define the starting row
    start_row = 9

    # Iterate through each sublist and its values
    for sublist in head_list:
        column_index = 0  # Start writing from the first column
        for value in sublist:
            worksheet.write(start_row, column_index, value, cell_format_left)
            column_index += 1  # Move to the next column for the next value
        start_row += 1

    start_row = 10
    for sublist in sorted_data:
        # Start writing from the first column
        if sublist[-1] == 'el':
            column_index = 0
            for value in sublist:
                if value == 'el':
                    worksheet.write(start_row, 11, value.upper(), cell_format_left)
                elif isinstance(value, datetime.date):
                    worksheet.write(start_row, column_index, value.strftime('%d-%m-%Y'), cell_format_left)
                    column_index += 1
                elif isinstance(value, str) and value.startswith("AURCT"):
                    if need_attendance:
                        worksheet.write(start_row, 12, value, cell_format_left)
                elif isinstance(value, list):

                    if value:
                        worksheet.write(start_row, column_index, value[0], cell_format_left)
                    else:
                        worksheet.write(start_row, column_index, "-", cell_format_left)

                    column_index += 1

                elif isinstance(value, bytes):
                    byte_data = value
                    byte_data_str = byte_data.decode('utf-8')
                    str_data = json.loads(byte_data_str)
                    worksheet.write(start_row, column_index, str_data[0], cell_format_left)
                    column_index += 1
                else:
                    worksheet.write(start_row, column_index, value, cell_format_left)
                    column_index += 1  # Move to the next column for the next value
            for i in range(column_index, 11):
                worksheet.write(start_row, i, "-", cell_format_center)

            start_row += 1
        else:
            column_index = 5
            for value in sublist:
                if value == 'lop' or value == 'mtl' or value == 'ml':
                    worksheet.write(start_row, 11, value.upper(), cell_format_left)
                elif isinstance(value, datetime.date):
                    worksheet.write(start_row, column_index, value.strftime('%d-%m-%Y'), cell_format_left)
                    column_index += 1

                elif isinstance(value, str) and value.startswith("AURCT"):
                    if need_attendance:
                        worksheet.write(start_row, 12, value, cell_format_left)

                elif isinstance(value, bytes):
                    byte_data = value
                    byte_data_str = byte_data.decode('utf-8')
                    str_data = json.loads(byte_data_str)
                    print(str_data)
                    formatted_date = '-' if str_data[0] == '-' else datetime.datetime.strptime(str_data[0], "%Y-%m-%d")
                    worksheet.write(start_row, column_index,
                                    formatted_date.strftime('%d-%m-%Y') if isinstance(formatted_date,
                                                                                datetime.date) else formatted_date,
                                    cell_format_left)
                    column_index += 1

                elif isinstance(value, list):
                    if value:
                        worksheet.write(start_row, column_index, value[0], cell_format_left)
                    else:
                        worksheet.write(start_row, column_index, "-", cell_format_left)
                    column_index += 1

                else:
                    worksheet.write(start_row, column_index, value, cell_format_left)
                    column_index += 1  # Move to the next column for the next value
            for i in range(0, 5):
                worksheet.write(start_row, i, "-", cell_format_center)

            start_row += 1
    if need_remarks:
        worksheet.merge_range(*(start_row, 0, start_row, 12), need_remarks, cell_format_center)
    if need_pdf:
        return {"name": xlname, "type": "pdf"}
    else:
        return {"name": xlname, "type": "xlsx"}

if __name__ == "__main__":
    mydb = mysql.connector.connect(host="localhost", port="3306", user="root",
                                   database="facultyleavedb")

    cursor = mydb.cursor()
    workbook = xlsxwriter.Workbook("test.xlsx")
    generate_report(cursor, '21002', workbook)
    workbook.close()


import numpy as np
from matplotlib.pyplot import figure

def polarplot(az, el, minel=0.0, delv=10.0, fig=None):
    """
    az: azimuths of points to plot (degrees)
    el: elevation (above horizon) of points to plot (degrees)

    minel: minimum elevation angle (degrees) to plot (axes limit)
    delv: increment to label elevation angles (degrees)
    """

    # astype(float) because some programs give off lists or object arrays that radians() doesn't like
    az = np.radians(np.asarray(az).astype(float))
    el = 90.0 - np.asarray(el).astype(float)

    try:
        ax = fig.gca(polar=True)
    except AttributeError:
        ax = figure().gca(polar=True)  # Note the polar=True keyword

    # ax.plot(az,el, marker='o',linestyle='none') # you can use other markers or linestyles too
    # %% boilerplate
    #     We reverse direction of "elevation" to make center of plot 90 deg.
    ax.set_theta_zero_location("N")
    ax.set_theta_direction(-1)

    yl = np.arange(0.0, 90.0 - minel + delv, delv)
    ylbl = (yl[::-1] + minel).astype(int).astype(str)
    ax.set_yticks(yl)
    ax.set_yticklabels(ylbl)

    return ax


def closest_k(location):
    import json
    content = None
    with open("dmatrixca.json") as file:
        content = json.load(file)

    matrix = content['matrix']
    mapping = content['mapping']
    reverse_mapping = {v: k for k, v in mapping.items()}
    ids = [id for id, map in mapping.items() if "CA" in map and map[0] != ',']

    # given county, find index of county in matrix
    # go through all it's neighbors and find the one with min distance
    index = reverse_mapping[location]
    state = location[-2:]
    neighbors = [(matrix[int(index)][int(id)], id) for id, map in mapping.items() if state in map and map[0] != ',']
    import heapq
    heapq.heapify(neighbors)
    closest_neighbors = heapq.nsmallest(11, neighbors)[1:]
    return [mapping[neighbor[1]] for neighbor in closest_neighbors]

if __name__ == '__main__':
    print(closest_k('Alpine , CA'))