import os
import uuid

from pytest import fixture
from .helpers import setup_vfile
from ..api import VersionedHDF5File

# Run tests marked with @pytest.mark.slow last. See
# https://stackoverflow.com/questions/61533694/run-slow-pytest-commands-at-the-end-of-the-test-suite
def by_slow_marker(item):
    return bool(item.get_closest_marker('slow'))

def pytest_collection_modifyitems(items):
    items.sort(key=by_slow_marker)

@fixture
def filepath(tmp_path, request):
    file_name = os.path.join(tmp_path, 'file.hdf5')
    m = request.node.get_closest_marker('setup_args')
    if m is not None:
        if 'file_name' in m.kwargs.keys():
            file_name = m.kwargs['file_name']
    yield file_name

@fixture
def h5file(tmp_path, request):
    file_name = os.path.join(tmp_path, 'file.hdf5')
    version_name = None
    m = request.node.get_closest_marker('setup_args')
    if m is not None:
        if 'file_name' in m.kwargs.keys():
            file_name = m.kwargs['file_name']
        if 'name' in m.kwargs.keys():
            raise ValueError("The name argument is no longer used")
        if 'version_name' in m.kwargs.keys():
            version_name = m.kwargs['version_name']

    f = setup_vfile(file_name=file_name, version_name=version_name)
    yield f
    try:
        f.close()
    # Workaround upstream h5py bug. https://github.com/deshaw/versioned-hdf5/issues/162
    except ValueError as e:
        if e.args[0] == "Unrecognized type code -1":
            return
        raise
    except RuntimeError as e:
        if e.args[0] in ["Can't increment id ref count (can't locate ID)",
                         "Unspecified error in H5Iget_type (return value <0)",
                         "Can't retrieve file id (invalid data ID)"]:
            return
        raise

@fixture
def vfile(tmp_path, h5file):
    file = VersionedHDF5File(h5file)
    yield file
    file.close()

def generate_bad_data():
    """Generate versioned-hdf5 files with bad object dtype hash tables in them.

    See https://github.com/deshaw/versioned-hdf5/issues/256 for more information.

    Raises:
        ImportError: Raised if the user tries to generate bad data with newer versions
        of the library; you need an old version to replicate the hash table issue.
    """
    import numpy as np
    import h5py
    from versioned_hdf5 import VersionedHDF5File, __version__

    try:
        from versioned_hdf5.backend import DATA_VERSION  # noqa: F401
    except ImportError:
        DATA_VERSION = None

    if DATA_VERSION is not None:
        raise ImportError(
            f"versioned_hdf5=={__version__} installed; "
            "this file only generates bad data on versioned_hdf5 <= 1.3.14."
        )

    filename = "object_dtype_bad_hashtable_data.h5"
    with h5py.File(filename, mode="w") as f:
        vf = VersionedHDF5File(f)
        arr = np.array(
            [
                "abcd",
                "def",
                "ghi",
                "jkl",
            ],
            dtype=object
        )

        with vf.stage_version("r0") as group:
            group.create_dataset(
                "data_with_bad_hashtable",
                dtype=h5py.string_dtype(length=None),
                data=arr
            )

    filename = "object_dtype_bad_hashtable_data2.h5"
    with h5py.File(filename, mode="w") as f:
        vf = VersionedHDF5File(f)

        arr = np.array(
            [
                "abcd",
                "def",
                "ghi",
                "jkl",
            ],
            dtype=object
        )
        arr2 = np.array(
            [
                "abcd",
                "pqrs",
                "df",
                "tuvw",
                "xyz",
            ],
            dtype=object
        )
        arr3 = np.linspace(1, 100, 1000)

        with vf.stage_version("r0") as group:
            group.create_dataset(
                "data_with_bad_hashtable",
                dtype=h5py.string_dtype(length=None),
                data=arr
            )
            group.create_dataset(
                "data_with_bad_hashtable2",
                dtype=h5py.string_dtype(length=None),
                data=arr2
            )
            group.create_dataset("linspace", data=arr3)

        with vf.stage_version("r1") as group:
            group['data_with_bad_hashtable'][1] = "foo"
            group['linspace'][3] = 8

        with vf.stage_version("r2") as group:
            group['data_with_bad_hashtable'][2] = "bar"
            group['data_with_bad_hashtable2'][2] = "what"

    filename = "nested_data_old_data_version.h5"
    with h5py.File(filename, mode="w") as f:
        vf = VersionedHDF5File(f)

        with vf.stage_version('r0') as sv:
            data_group = sv.create_group('data')
            data_group.create_dataset('values',
                                      data=np.array(['1', '2', '3']),
                                      dtype=h5py.string_dtype(length=None))

    filename = "multiple_nested_data_old_data_version.h5"
    with h5py.File(filename, mode="w") as f:
        vf = VersionedHDF5File(f)

        arr = np.array(
            [
                "abcd",
                "def",
                "ghi",
                "jkl",
            ],
            dtype=object
        )

        with vf.stage_version('r0') as sv:
            g = sv.create_group('foo/bar/baz')
            g.create_dataset(
                "foo/bar/baz/values",
                dtype=h5py.string_dtype(length=None),
                data=arr
            )

    filename = "object_dtype_bad_hashtable_chunk_reuse.h5"
    with h5py.File(filename, mode="w") as f:
        vf = VersionedHDF5File(f)
        with vf.stage_version(str(0)) as sv:
            sv.create_dataset('values',
                              data=np.arange(0).astype(str).astype('O'),
                              dtype=h5py.string_dtype(length=None),
                              chunks=(10,))

    for i in range(1, 11):
        with h5py.File(filename, 'r+') as f:
            vf = VersionedHDF5File(f)
            with vf.stage_version(str(i)) as sv:
                sv['values'] = np.arange(i).astype(str).astype('O')

    filename = "object_dtype_bad_hashtable_chunk_reuse_unicode.h5"
    numbers = ['れい', 'いち', 'に', 'さん', 'し', 'ご', 'ろく', 'しち', 'はち', 'きゅう', 'じゅう']
    with h5py.File(filename, mode="w") as f:
        vf = VersionedHDF5File(f)
        with vf.stage_version(str(0)) as sv:
            sv.create_dataset('values',
                              data=np.array(numbers[:1], dtype='O'),
                              dtype=h5py.string_dtype(length=None),
                              chunks=(10,))

    for i in range(1, 11):
        with h5py.File(filename, 'r+') as f:
            vf = VersionedHDF5File(f)
            with vf.stage_version(str(i)) as sv:
                sv['values'] = np.array(numbers[:i + 1], dtype='O')

    filename = "object_dtype_bad_hashtable_chunk_reuse_multi_dim.h5"
    with h5py.File(filename, mode="w") as f:
        vf = VersionedHDF5File(f)
        with vf.stage_version(str(uuid.uuid4())) as sv:
            sv.create_dataset('values',
                              data=np.array([[chr(ord('a') + ((j + k) % 10)) * 3 for j in range(4)]
                                             for k in range(4)], dtype='O'),
                              dtype=h5py.string_dtype(length=None),
                              chunks=(2, 2))

    for i in range(1, 11):
        with h5py.File(filename, 'r+') as f:
            vf = VersionedHDF5File(f)
            with vf.stage_version(str(uuid.uuid4())) as sv:
                sv['values'] = np.array([[chr(ord('a') + ((i + j + k) % 10)) * 3 for j in range(4)]
                                         for k in range(4)], dtype='O')


import math

import mesa

from .model import State, VirusOnNetwork, number_infected

def network_portrayal(G):
    # The model ensures there is always 1 agent per node

    def node_color(agent):
        return {State.INFECTED: "#FF0000", State.SUSCEPTIBLE: "#008000"}.get(
            agent.state, "#808080"
        )

    def edge_color(agent1, agent2):
        if State.RESISTANT in (agent1.state, agent2.state):
            return "#000000"
        return "#e8e8e8"

    def edge_width(agent1, agent2):
        if State.RESISTANT in (agent1.state, agent2.state):
            return 3
        return 2

    def get_agents(source, target):
        return G.nodes[source]["agent"][0], G.nodes[target]["agent"][0]

    portrayal = {}
    portrayal["nodes"] = [
        {
            "size": 6,
            "color": node_color(agents[0]),
            "tooltip": f"id: {agents[0].unique_id}
state: {agents[0].state.name}",
        }
        for (_, agents) in G.nodes.data("agent")
    ]

    portrayal["edges"] = [
        {
            "source": source,
            "target": target,
            "color": edge_color(*get_agents(source, target)),
            "width": edge_width(*get_agents(source, target)),
        }
        for (source, target) in G.edges
    ]

    return portrayal

network = mesa.visualization.NetworkModule(network_portrayal, 500, 500)
chart = mesa.visualization.ChartModule(
    [
        {"Label": "Infected", "Color": "#FF0000"},
        {"Label": "Susceptible", "Color": "#008000"},
        {"Label": "Resistant", "Color": "#808080"},
    ]
)

def get_resistant_susceptible_ratio(model):
    ratio = model.resistant_susceptible_ratio()
    ratio_text = "∞" if ratio is math.inf else f"{ratio:.2f}"
    infected_text = str(number_infected(model))

    return "Resistant/Susceptible Ratio: {}
Infected Remaining: {}".format(
        ratio_text, infected_text
    )

model_params = {
    "num_nodes": mesa.visualization.Slider(
        "Number of agents",
        10,
        10,
        100,
        1,
        description="Choose how many agents to include in the model",
    ),
    "avg_node_degree": mesa.visualization.Slider(
        "Avg Node Degree", 3, 3, 8, 1, description="Avg Node Degree"
    ),
    "initial_outbreak_size": mesa.visualization.Slider(
        "Initial Outbreak Size",
        1,
        1,
        10,
        1,
        description="Initial Outbreak Size",
    ),
    "virus_spread_chance": mesa.visualization.Slider(
        "Virus Spread Chance",
        0.4,
        0.0,
        1.0,
        0.1,
        description="Probability that susceptible neighbor will be infected",
    ),
    "virus_check_frequency": mesa.visualization.Slider(
        "Virus Check Frequency",
        0.4,
        0.0,
        1.0,
        0.1,
        description="Frequency the nodes check whether they are infected by a virus",
    ),
    "recovery_chance": mesa.visualization.Slider(
        "Recovery Chance",
        0.3,
        0.0,
        1.0,
        0.1,
        description="Probability that the virus will be removed",
    ),
    "gain_resistance_chance": mesa.visualization.Slider(
        "Gain Resistance Chance",
        0.5,
        0.0,
        1.0,
        0.1,
        description="Probability that a recovered agent will become "
        "resistant to this virus in the future",
    ),
}

server = mesa.visualization.ModularServer(
    VirusOnNetwork,
    [network, get_resistant_susceptible_ratio, chart],
    "Virus Model",
    model_params,
)
server.port = 8521


import sys
import os

from PyQt4.QtGui import *
from PyQt4.QtCore import *
from DMA_DEFINES import *

sys.path.append(os.path.join(os.path.dirname(__file__),
                             os.pardir,
                             os.pardir,
                             os.pardir,
                             os.pardir))

from NysaGui.common.hex_validator import HexValidator

class InstructionWidget(QWidget):

    def __init__(self, status, actions, index, instruction_count):
        super (InstructionWidget, self).__init__()
        self.status = status
        self.actions = actions
        self.instruction_count = instruction_count
        self.index = index
        self.label = QLabel("Instruction %d" % index)
        form_layout = QFormLayout()

        self.source_address = QLineEdit("0x00000000")
        hv = HexValidator()
        hv.setRange(0, 0xFFFFFFFFFFFFFFFF)
        self.source_address.setValidator(hv)
        self.source_address.textChanged.connect(self.source_addr_changed)
        self.sink_address = QLineEdit("0x00000000")
        hv = HexValidator()
        hv.setRange(0, 0xFFFFFFFFFFFFFFFF)
        self.sink_address.setValidator(hv)
        self.sink_address.textChanged.connect(self.sink_addr_changed)
        v = QDoubleValidator()
        v.setRange(0, 0xFFFFFFFF)
        self.data_count = QLineEdit("1")
        self.data_count.setValidator(v)
        self.data_count.textChanged.connect(self.data_count_changed)
        self.data_count_changed(self.data_count.text())

        #Populate with all the instruction indexes
        self.next_instruction = QComboBox()
        self.next_instruction.currentIndexChanged.connect(self.next_inst_changed)
        self.bond_ingress_addr = QComboBox()
        self.bond_ingress_addr.currentIndexChanged.connect(self.ingress_addr_changed)
        self.bond_egress_addr = QComboBox()
        self.bond_egress_addr.currentIndexChanged.connect(self.egress_addr_changed)
        for i in range (self.instruction_count):
            self.next_instruction.addItem(str(i))
            self.bond_ingress_addr.addItem(str(i))
            self.bond_egress_addr.addItem(str(i))

        self.next_instruction.setCurrentIndex(0)
        self.bond_ingress_addr.setCurrentIndex(0)
        self.bond_egress_addr.setCurrentIndex(0)

        self.reset_src_addr_on_command = QCheckBox()
        self.reset_dest_addr_on_command = QCheckBox()
        self.enable_bond_ingress = QCheckBox()
        self.enable_bond_egress = QCheckBox()
        self.cmd_continue = QCheckBox()
        self.commit_button = QPushButton("Commit")

        self.reset_src_addr_on_command.stateChanged.connect(self.reset_src_addr_on_command_changed)
        self.reset_dest_addr_on_command.stateChanged.connect(self.reset_dest_addr_on_command_changed)
        self.enable_bond_ingress.stateChanged.connect(self.enable_ingress_changed)
        self.enable_bond_egress.stateChanged.connect(self.enable_egress_changed)
        self.cmd_continue.stateChanged.connect(self.cmd_continue_changed)
        self.commit_button.clicked.connect(self.commit)

        form_layout.addRow(self.label)
        form_layout.addRow(QString("Reset Source Address on New Instruction"), self.reset_src_addr_on_command)
        form_layout.addRow(QString("Reset Dest Address on New Instruction"), self.reset_dest_addr_on_command)
        form_layout.addRow(QString("Source Address"), self.source_address)
        form_layout.addRow(QString("Sink Address"), self.sink_address)
        form_layout.addRow(QString("Continue Command"), self.cmd_continue)
        form_layout.addRow(QString("Data Count"), self.data_count)
        form_layout.addRow(QString("Next Instruction"), self.next_instruction)
        form_layout.addRow(QString("Enable Bond Ingress"), self.enable_bond_ingress)
        form_layout.addRow(QString("Bond Ingress Instruction addr"), self.bond_ingress_addr)
        form_layout.addRow(QString("Enable Bond Egress"), self.enable_bond_egress)
        form_layout.addRow(QString("Bond Egress Instruction addr"), self.bond_egress_addr)
        form_layout.addRow(self.commit_button)

        self.setLayout(form_layout)
        self.label.setStyleSheet("background-color: %s" % DMA_GOOD)

    def next_inst_changed(self, index):
        self.label.setStyleSheet("background-color: %s" % DMA_WARNING)

    def egress_addr_changed(self, index):
        self.label.setStyleSheet("background-color: %s" % DMA_WARNING)

    def ingress_addr_changed(self, index):
        self.label.setStyleSheet("background-color: %s" % DMA_WARNING)

    def reset_src_addr_on_command_changed(self):
        value = self.reset_src_addr_on_command.isChecked()
        self.label.setStyleSheet("background-color: %s" % DMA_WARNING)

    def reset_dest_addr_on_command_changed(self):
        value = self.reset_dest_addr_on_command.isChecked()
        self.label.setStyleSheet("background-color: %s" % DMA_WARNING)

    def enable_ingress_changed(self):
        self.label.setStyleSheet("background-color: %s" % DMA_WARNING)

    def enable_egress_changed(self):
        self.label.setStyleSheet("background-color: %s" % DMA_WARNING)

    def cmd_continue_changed(self):
        self.label.setStyleSheet("background-color: %s" % DMA_WARNING)

    def source_addr_changed(self, data):
        self.label.setStyleSheet("background-color: %s" % DMA_WARNING)

    def sink_addr_changed(self, data):
        self.label.setStyleSheet("background-color: %s" % DMA_WARNING)

    def data_count_changed(self, data):
        self.label.setStyleSheet("background-color: %s" % DMA_WARNING)
        v = self.data_count.validator()
        state = v.validate(data, 0)[0]
        color = DMA_ERROR
        if state == QValidator.Acceptable:
            color = DMA_GOOD
        elif state == QValidator.Intermediate:
            color = DMA_WARNING
        else:
            color = DMA_ERROR
        self.data_count.setStyleSheet('QLineEdit { background-color: %s }' % color)

    def commit(self):
        self.label.setStyleSheet("background-color: %s" % DMA_GOOD)
        inst_dict = {}
        inst_dict["SRC_RST_ON_CMD"] = self.reset_src_addr_on_command.isChecked()
        inst_dict["DEST_RST_ON_CMD"] = self.reset_dest_addr_on_command.isChecked()
        inst_dict["CMD_CONTINUE"] = self.cmd_continue.isChecked()
        inst_dict["EGRESS_ADDR"] = int(str(self.bond_egress_addr.currentText()), 0)
        inst_dict["EGRESS_ENABLE"] = self.enable_bond_egress.isChecked()
        inst_dict["INGRESS_ADDR"] = int(str(self.bond_ingress_addr.currentText()), 0)
        inst_dict["INGRESS_ENABLE"] = self.enable_bond_ingress.isChecked()
        inst_dict["SRC_ADDR"] = long(str(self.source_address.text()), 0)
        inst_dict["DEST_ADDR"] = long(str(self.sink_address.text()), 0)
        inst_dict["COUNT"] = int(str(self.data_count.text()), 0)
        self.actions.instruction_commit.emit(self.index, inst_dict)

    def update_settings(self, inst_dict):
        self.reset_src_addr_on_command.setChecked(inst_dict["SRC_RST_ON_CMD"])
        self.reset_dest_addr_on_command.setChecked(inst_dict["DEST_RST_ON_CMD"])
        self.cmd_continue.setChecked(inst_dict["CMD_CONTINUE"])
        self.enable_bond_egress.setChecked(inst_dict["EGRESS_ENABLE"])
        self.bond_egress_addr.setCurrentIndex(inst_dict["EGRESS_ADDR"])
        self.enable_bond_ingress.setChecked(inst_dict["INGRESS_ENABLE"])
        self.bond_ingress_addr.setCurrentIndex(inst_dict["INGRESS_ADDR"])
        self.source_address.setText("0x%016X" % inst_dict["SRC_ADDR"])
        self.sink_address.setText("0x%016X" % inst_dict["DEST_ADDR"])
        self.data_count.setText(str(inst_dict["COUNT"]))
        self.label.setStyleSheet("background-color: %s" % DMA_GOOD)


import numpy as np
from nltk.stem import WordNetLemmatizer
from nltk import word_tokenize
from nltk.corpus import stopwords
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import OneHotEncoder
from utils.hw2 import load_data, save_prediction, read_vocab
from utils.general import sigmoid, tanh, show_keras_model

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Embedding, Conv1D, MaxPooling1D, Dense, GlobalMaxPooling1D

from sklearn.model_selection import train_test_split

data = load_data("train.txt")

X, y = data.text, data.target
X_train, X_dev, y_train, y_dev = train_test_split(X[:2000], y[:2000], test_size=0.3)

MAX_LENGTH = 100
wnet = WordNetLemmatizer()
vocab = read_vocab("vocab.txt")

unknown = vocab['__unknown__']
X_train2 = [[vocab.get(wnet.lemmatize(w), unknown) for w in word_tokenize(sent)] for sent in X_train]
X_dev2 = [[vocab.get(wnet.lemmatize(w), unknown) for w in word_tokenize(sent)] for sent in X_dev]

def trim_X(X, max_length=100, default=vocab['.']):
    for i in range(len(X)):
        if len(X[i]) > max_length:
            X[i] = X[i][:max_length]
        elif len(X[i]) < max_length:
            X[i] = X[i] + [default] * (max_length - len(X[i]))
    return np.array(X)

X_train2 = trim_X(X_train2, MAX_LENGTH)
X_dev2 = trim_X(X_dev2, MAX_LENGTH)

model = Sequential()
model.add(Embedding(input_dim=len(vocab), input_length=MAX_LENGTH, output_dim=1024, name="Embedding-1"))
model.add(Conv1D(filters=100, kernel_size=2, activation="tanh", name="Conv1D-1"))
model.add(GlobalMaxPooling1D(name="MaxPooling1D-1"))
model.add(Dense(1, activation="sigmoid", name="Dense-1"))

print(model.summary())
# show_keras_model(model)

model.compile(loss="binary_crossentropy", optimizer='adam', metrics=['accuracy'])
model.fit(X_train2, y_train, epochs=10, validation_data=[X_dev2, y_dev])

data = load_data("test.txt")

X, y = data.text, data.target
X = [[vocab.get(wnet.lemmatize(w), unknown) for w in word_tokenize(sent)] for sent in X]

X = trim_X(X, MAX_LENGTH)
y_predict = model.predict(X)

save_prediction(y_predict)


import tkinter as tk
import random

class TicTacToe:
    def __init__(self):
        # Create the game board
        self.board = [[None for _ in range(3)] for _ in range(3)]

        # Initialize the game variables
        self.current_player = random.randint(0, 1)
        self.player_wins = [0, 0]
        self.num_moves = 0

    def check_win(self):
        # Check rows
        for i in range(3):
            if self.board[i][0] == self.board[i][1] == self.board[i][2] is not None:
                return True

        # Check columns
        for i in range(3):
            if self.board[0][i] == self.board[1][i] == self.board[2][i] is not None:
                return True

        # Check diagonals
        if self.board[0][0] == self.board[1][1] == self.board[2][2] is not None:
            return True

        if self.board[0][2] == self.board[1][1] == self.board[2][0] is not None:
            return True

        # No winner yet
        return False

    def reset_game(self):
        # Reset the game board
        for i in range(3):
            for j in range(3):
                self.buttons[i][j]["text"] = ""
                self.buttons[i][j]["state"] = "normal"
        # Reset game variables
        self.current_player = "X"
        self.moves = 0
        self.game_over = False
        self.player_X_wins = 0
        self.player_O_wins = 0
        self.draws = 0
        # Update stats display
        self.update_stats_display()

class TicTacToeUI:
    def __init__(self, master):
        self.master = master
        master.title("Tic Tac Toe")

        # Create the game board
        self.board = [[None for _ in range(3)] for _ in range(3)]

        # Create the player labels and winning percentage labels
        self.player_labels = [tk.Label(master, text="Player 1"), tk.Label(master, text="Player 2")]
        self.win_percent_labels = [tk.Label(master, text="Wins: 0 / 0"), tk.Label(master, text="Wins: 0 / 0")]

        # Create the buttons for the game board
        self.buttons = [[tk.Button(master, width=5, height=2, command=lambda row=i, col=j: self.make_move(row, col)) for j in range(3)] for i in range(3)]

        # Place the player labels and winning percentage labels
        for i in range(2):
            self.player_labels[i].grid(row=i, column=3, padx=10, pady=10)
            self.win_percent_labels[i].grid(row=i+1, column=3, padx=10, pady=10)

        # Place the buttons for the game board
        for i in range(3):
            for j in range(3):
                self.buttons[i][j].grid(row=i, column=j)

        # Initialize the game variables
        self.current_player = 0
        self.player_wins = [0, 0]
        self.num_moves = 0

        # Initialize the winning percentages
        self.win_percentages = [None, None]

    def check_win(self):
        # Check rows
        for i in range(3):
            if self.board[i][0] == self.board[i][1] == self.board[i][2] is not None:
                return True

        # Check columns
        for i in range(3):
            if self.board[0][i] == self.board[1][i] == self.board[2][i] is not None:
                return True

        # Check diagonals
        if self.board[0][0] == self.board[1][1] == self.board[2][2] is not None:
            return True

        if self.board[0][2] == self.board[1][1] == self.board[2][0] is not None:
            return True

        # No winner yet
        return False

    def update_stats_display(self):
        self.stats_label.config(text=f"Total games: {self.total_games}\nX wins: {self.x_wins}\nO wins: {self.o_wins}\nDraws: {self.draws}")

    def reset_game(self):
        # Reset the game board
        for i in range(3):
            for j in range(3):
                self.buttons[i][j]["text"] = ""
                self.buttons[i][j]["state"] = "normal"
        # Reset game variables
        self.current_player = "X"
        self.moves = 0
        self.game_over = False
        self.player_X_wins = 0
        self.player_O_wins = 0
        self.draws = 0
        # Update stats display
        self.update_stats_display()

    def make_move(self, clicked_row, clicked_col):
        # Check if the button has already been clicked
        if self.buttons[clicked_row][clicked_col]["text"] != "":
            return

        # Update the game board and the button text
        self.board[clicked_row][clicked_col] = self.current_player
        self.buttons[clicked_row][clicked_col]["text"] = "X" if self.current_player == 0 else "O"

        # Check if the current player has won
        if self.check_win():
            self.player_wins[self.current_player] += 1
            self.num_moves += 1
            self.win_percentages[self.current_player] = float(self.player_wins[self.current_player]) / self.num_moves
            self.win_percent_labels[self.current_player]["text"] = "Wins: {} / {} ({:.1f}%)".format(self.player_wins[self.current_player], self.num_moves, self.win_percentages[self.current_player] * 100)
            self.reset_game()
            return

        # Check if the game is a tie
        if self.num_moves == 9:
            self.win_percentages[0] = float(self.player_wins[0]) / self.num_moves
            self.win_percentages[1] = float(self.player_wins[1]) / self.num_moves
            self.win_percent_labels[0]["text"] = "Wins: {} / {} ({:.1f}%)".format(self.player_wins[0], self.num_moves, self.win_percentages[0] * 100)
            self.win_percent_labels[1]["text"] = "Wins: {} / {} ({:.1f}%)".format(self.player_wins[1], self.num_moves, self.win_percentages[1] * 100)
            self.reset_game()
            return

        # Switch to the other player
        self.current_player = 1 if self.current_player == 0 else 0

# Create the main window
root = tk.Tk()

# Create the TicTacToeUI object and start the game
tictactoe = TicTacToeUI(root)

# Run the main event loop
root.mainloop()


import pytest
import dash
import dash_html_components as html
import dash_extendable_graph as deg
from dash.dependencies import Input, Output, State
from dash.exceptions import PreventUpdate
import time

TIMEOUT = 20

# component responsive flag behaves as expected. This test is pulled from dash-core-components:
# https://github.com/plotly/dash-core-components/blob/dev/tests/integration/graph/test_graph_responsive.py

class WaitForTimeout(Exception):
    """This should only be raised inside the `wait_for` function."""

    pass

def wait_for(condition_function, get_message=lambda: "", *args, **kwargs):
    """
    Waits for condition_function to return True or raises WaitForTimeout.
    :param (function) condition_function: Should return True on success.
    :param args: Optional args to pass to condition_function.
    :param kwargs: Optional kwargs to pass to condition_function.
        if `timeout` is in kwargs, it will be used to override TIMEOUT
    :raises: WaitForTimeout If condition_function doesn't return True in time.
    Usage:
        def get_element(selector):
            # some code to get some element or return a `False`-y value.
        selector = '.js-plotly-plot'
        try:
            wait_for(get_element, selector)
        except WaitForTimeout:
            self.fail('element never appeared...')
        plot = get_element(selector)  # we know it exists.
    """

    def wrapped_condition_function():
        """We wrap this to alter the call base on the closure."""
        if args and kwargs:
            return condition_function(*args, **kwargs)
        if args:
            return condition_function(*args)
        if kwargs:
            return condition_function(**kwargs)
        return condition_function()

    if "timeout" in kwargs:
        timeout = kwargs["timeout"]
        del kwargs["timeout"]
    else:
        timeout = TIMEOUT

    start_time = time.time()
    while time.time() < start_time + timeout:
        if wrapped_condition_function():
            return True
        time.sleep(0.5)

    raise WaitForTimeout(get_message())

@pytest.mark.parametrize("responsive", [True, False])  # reduce iterations: None = False
@pytest.mark.parametrize("autosize", [True, False])  # reduce iterations -- None = True
@pytest.mark.parametrize("height", [600, None])
@pytest.mark.parametrize("is_responsive", [True, False, "auto"])
def test_extg006_responsive(dash_duo, responsive, autosize, height, is_responsive):
    width = 600  # reduce test iterations, just check vs height (ignore width)
    app = dash.Dash(__name__)

    header_style = dict(padding="10px", backgroundColor="yellow", flex="0 0 100px")

    graph_style = dict(padding="10px", backgroundColor="red", flex="1 0 0")

    card_style = dict(
        display="flex",
        flexFlow="column",
        backgroundColor="green",
        padding="10px",
        height="500px",
        width="1000px",
    )

    header = html.Div(
        id="header",
        style=header_style,
        children=[html.Button(id="resize", children=["Resize"])],
    )

    graph = html.Div(
        style=graph_style,
        children=[
            deg.ExtendableGraph(
                id="graph",
                responsive=is_responsive,
                style=dict(height="100%", width="100%"),
                config=dict(responsive=responsive),
                figure=dict(
                    layout=dict(autosize=autosize, height=height, width=width),
                    data=[
                        dict(x=[1, 2, 3, 4], y=[5, 4, 3, 6], line=dict(shape="spline"),)
                    ],
                ),
            )
        ],
    )

    app.layout = html.Div(
        [
            html.Div(
                [
                    "responsive: {}, ".format(responsive),
                    "autosize: {}, ".format(autosize),
                    "height: {}, ".format(height),
                    "width: {}, ".format(width),
                    "is_responsive: {}".format(is_responsive),
                ]
            ),
            html.Div(id="card", style=card_style, children=[header, graph]),
        ]
    )

    @app.callback(
        Output("header", "style"),
        [Input("resize", "n_clicks")],
        [State("header", "style")],
    )
    def resize(n_clicks, style):
        if n_clicks is None:
            raise PreventUpdate

        return dict(style, **dict(flex="0 0 200px"))

    dash_duo.start_server(app)

    # autosize=true|udefined will make the graph fit its parent on first render, responsive has no impact on that behavior
    #
    # responsive=true will make the graph resize only if autosize=true|undefined, interestingly enough, responsive=undefined
    # behaves the same as responsive=false (https://github.com/plotly/plotly.js/blob/master/src/plot_api/plot_config.js#L122)

    initial_responsive = is_responsive is True or (
        is_responsive == "auto"
        and autosize is not False
        and (height is None or width is None)
    )

    resize_responsive = is_responsive is True or (
        is_responsive == "auto"
        and responsive is True
        and autosize is not False
        and (height is None or width is None)
    )

    initial_height = (
        360
        if (initial_responsive and (height is None or is_responsive is True))
        else 450
        if height is None
        else height
    )

    resize_height = (
        260
        if (resize_responsive and (height is None or is_responsive is True))
        else initial_height
        if height is None
        else height
    )

    wait_for(
        # 500px card minus (100px header + 20px padding) minus (20px padding on container) -> 360px left
        lambda: dash_duo.wait_for_element("#graph svg.main-svg").size.get("height", -1)
        == initial_height,
        lambda: "initial graph height {}, expected {}".format(
            dash_duo.wait_for_element("#graph svg.main-svg").size.get("height", -1),
            initial_height,
        ),
    )
    dash_duo.wait_for_element("#resize").click()

    wait_for(
        # 500px card minus (200px header + 20px padding) minus (20px padding on container) -> 260px left
        lambda: dash_duo.wait_for_element("#graph svg.main-svg").size.get("height", -1)
        == resize_height,
        lambda: "resized graph height {}, expected {}".format(
            dash_duo.wait_for_element("#graph svg.main-svg").size.get("height", -1),
            resize_height,
        ),
    )


import pygame
import time

from audiovisual import theme

"""
	stores every information about the game, as the achievements, time played, achievements
	and rank information of suvival mode.
"""

new_score, new_player = 0, '-'

startTime = 0
already = False
def getActualTime():
	return int(time.mktime(time.localtime( )) - startTime)

i = {'timePlayed': 0, 'blocksConquered': 0, 'deaths': 0, 'ballsDestructed': 0,
		'lastUnlockedStages': 1, 'theme':0,
		#achievements
		'doubleKill': 0, 'worldEmperor': 0, 'yogaMaster': 0, 'immortal': 0, 'jedi':0,
	 	'pilgrim': 0, 'hacker': 0, 'pacifist': 0, 'serialKiller':0,
		#settings
		'musicVolume': 100, 'effectsVolume':100, 'rank': [('-', 0) for k in range(10)]}

leftWingSprite = [theme.sprite('wing1Mini.png'), theme.sprite('wing2Mini.png'),
				  	theme.sprite('wing3Mini.png'), theme.sprite('fullWingMini.png')]

def insert_rank():
	i['rank'].append((new_player, new_score))
	i['rank'] = sorted(i['rank'], key = lambda x: x[1])[::-1]
	i['rank'].pop(-1)

menu_vol, game_vol = 1.0, 1.0
old_music, old_sfx = 1.0, 1.0
vol_max, sfx_max = 1.0, 1.0


import numpy as np
import matplotlib
matplotlib.rcParams['text.usetex'] = True
import matplotlib.pyplot as plt
import pickle

#from mpl_toolkits.axes_grid1.inset_locator import inset_axes

outfile = "plot_corr_triple_ce.pdf"; fbase =  "../cdataD3_4-10_" ;
ymax=(0.10, 0.10, 0.05); ylabel="$\langle a_1 a_2 a_3^* \\rangle $"

#-- canvas options --

fig, ax = plt.subplots(ncols=3, nrows=2, figsize=(10,7))

runs = ("e1", "c1", "c5")

for i in (0,1,2):

    run=runs[i]

    fname = fbase + run +".txt"

    print(fname)
    iax = ax[0][i]
    dat = np.loadtxt(fname, usecols=range(1,12))

    w    = dat[:,0]**2 + dat[:,1]**2
    corr = dat[:,2]
    bg   = corr/dat[:,3]
    phi   = dat[:,4]
    w2    = dat[:,5]**2 + dat[:,6]**2
    corr2 = dat[:,7]
    phi2  = dat[:,9]
    group = dat[:,10]

    ind = (group == 0 )

    iax.plot(w[ind], corr[ind],  'or',  mfc='none', ms=5,  mew=1, label="resonant")
    iax.plot(w[ind], bg[ind],    'og',  mfc='none', ms=5,  mew=0.5, label="background")
    iax.plot(w[ind], corr2[ind], 'db',  mfc='none', ms=5,  mew=0.5, label="non resonant")

    ind = (group == 1 )

    iax.plot(w[ind], corr[ind],  '+r',  mfc='none', ms=4,  mew=0.5)
    iax.plot(w[ind], corr2[ind], '+b',  mfc='none', ms=4,  mew=0.2)

    ind = (group == 2 )

    iax.plot(w[ind], corr[ind],  'xr',  mfc='none', ms=4,  mew=0.5)
    iax.plot(w[ind], corr2[ind], 'xb',  mfc='none', ms=4,  mew=0.2)

    ind = (group == 3 )

    iax.plot(w[ind], corr[ind],  '+r',  mfc='none', ms=4,  mew=0.5)
    iax.plot(w[ind], corr2[ind], '+b',  mfc='none', ms=4,  mew=0.2)

    ind = (group > 3 )

    iax.plot(w[ind], corr[ind],  'xr',  mfc='none', ms=4,  mew=0.5)
    iax.plot(w[ind], corr2[ind], 'xb',  mfc='none', ms=4,  mew=0.2)

    iax.set_title(run)
    iax.set(xlabel="$|k_1|^2 + |k_2|^2$", ylabel=ylabel)
    iax.legend() #frameon=False)

    iax.set_xlim(25, 175)
    iax.set_ylim(0, ymax[i])

    iax = ax[1][i]

    ind = (group == 0 )
    iax.plot(w[ind], phi[ind],  'or',  mfc='none', ms=5,  mew=1)
    iax.plot(w[ind], phi2[ind],  'db',  mfc='none', ms=5,  mew=0.5)

    ind = (group == 1 )
    iax.plot(w[ind], phi[ind],  '+r',  mfc='none', ms=4,  mew=0.5)
    iax.plot(w[ind], phi2[ind], '+b',  mfc='none', ms=4,  mew=0.2)

    ind = (group == 2 )
    iax.plot(w[ind], phi[ind],  'xr',  mfc='none', ms=4,  mew=0.5)
    iax.plot(w[ind], phi2[ind], 'xb',  mfc='none', ms=4,  mew=0.2)

    ind = (group == 3 )
    iax.plot(w[ind], phi[ind],  '+r',  mfc='none', ms=4,  mew=0.5)
    iax.plot(w[ind], phi2[ind], '+b',  mfc='none', ms=4,  mew=0.2)

    ind = (group > 3 )
    iax.plot(w[ind], phi[ind],  'xr',  mfc='none', ms=4,  mew=0.5)
    iax.plot(w[ind], phi2[ind], 'xb',  mfc='none', ms=4,  mew=0.2)

    iax.set(xlabel="$|k_1|^2 + |k_2|^2$", ylabel="$\\theta / \pi$")
    iax.set_xlim(25, 175)
    iax.set_ylim(-1, 1)

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

plt.subplots_adjust(left=0.08, right=0.98, top=0.96, bottom=0.12, hspace=0.25, wspace=0.28)

plt.savefig(outfile, pad_inches=0)

plt.close()

#===========================================

#=========================================================


import pandas as pd
import csv
import requests
import time
import os
os.chdir(os.path.dirname(os.path.abspath(__file__)))

folders = [name for name in os.listdir(".") if os.path.isdir(name) and name!= 'preprocessed_dir']
print(folders)

current_dir = os.getcwd()
preprocessed_dir = os.path.join(current_dir, 'preprocessed_dir')
if not os.path.exists(preprocessed_dir):
    os.mkdir(preprocessed_dir)

ref_folder_name = 'DOW'
ref_folder = os.path.join(current_dir, ref_folder_name)

dir_list = []
years = [1]
months = [1,2,3,4,5,6,7,8,9,10,11,12]
for year in years:
    for month in months:
        element = ref_folder_name + 'year' + str(year) + 'month' + str(month) + '.csv'
        dir_list.append(element)

csv_path = os.path.join(ref_folder, dir_list[0])
df_csv = pd.read_csv(csv_path, delimiter=',')

for i in range(1, len(dir_list)):
    csv_path = os.path.join(ref_folder, dir_list[i])
    #print(csv_path)
    df_csv2 = pd.read_csv(csv_path, delimiter=',')
    #print(df_csv2)
    df_csv = pd.concat([df_csv, df_csv2],ignore_index=True)

output_file = ref_folder_name + '.csv'
output_dir = os.path.join(preprocessed_dir, output_file)
df_csv.to_csv(output_dir, index = False, header = True)


import csv
import os
import shutil

def anonymize_group(filename, num_assignment):
    with open(f"data/{filename}", "r") as file:
        content = file.read()
        lines = content.splitlines()
        if lines[-1] == "":
            lines.pop()
        new_file_content = ""
        for line in lines:
            new_file_content += num_assignment[line] + "\n"
        with open(f"anon-data/{filename}", "w") as new_file:
            new_file.write(new_file_content)

def anonymize_questionnaire(filename, num_assignment):
    with open(f"data/{filename}", "r") as file:
        reader = csv.DictReader(file)
        with open(f"anon-data/{filename}", "w") as new_file:
            writer = csv.writer(new_file)
            wrote_header = False
            for row in reader:
                for key in row:
                    value = row[key]
                    if "\n" in value:
                        row[key] = value.replace("\n",";")

                row.pop("Carimbo de data/hora")
                if "Are you willing and interested to participate in this study?" in row:
                    row.pop("Are you willing and interested to participate in this study?")
                row.pop("What is your name?")
                email = row["What is your email address?"]
                row.pop("What is your email address?")
                row["id"] = num_assignment[email]

                if not wrote_header:
                    writer.writerow(row)
                    wrote_header = True
                writer.writerow(row.values())

def anonymize_dockerfiles(num_assignment):
    for email, id in num_assignment.items():
        old_path = f"data/Dockerfiles/{email}"
        if os.path.exists(old_path):
            new_path = f"anon-data/Dockerfiles/{id}"
            os.makedirs(new_path, exist_ok=True)

            tasks = [1, 2, 3]
            for task in tasks:
                old_filename = f"{old_path}/Dockerfile.{email}.task-{task}"
                new_filename = f"{new_path}/Dockerfile.task-{task}"
                shutil.copy(old_filename, new_filename)
        else:
            continue

num_assignment = {}

with open("data/participant_id.csv", "r") as assignment_file:
    reader = csv.DictReader(assignment_file, fieldnames=["email", "num"])
    for row in reader:
        num_assignment[row["email"]] = row["num"]

anonymize_group("control.txt", num_assignment)
anonymize_group("experimental.txt", num_assignment)
anonymize_questionnaire("background.csv", num_assignment)
anonymize_questionnaire("control_questionnaire.csv", num_assignment)
anonymize_questionnaire("experimental_questionnaire.csv", num_assignment)
anonymize_dockerfiles(num_assignment)


from flask import Flask
from googleplex_assistant.config import token
from logging.config import dictConfig

def create_app():
    dictConfig({
        'version': 1,
        'formatters': {'default': {
            'format': '[%(asctime)s] %(levelname)s in %(module)s: %(message)s',
        }},
        'handlers': {'wsgi': {
            'class': 'logging.StreamHandler',
            'stream': 'ext://flask.logging.wsgi_errors_stream',
            'formatter': 'default'
        }},
        'root': {
            'level': 'DEBUG',
            'handlers': ['wsgi']
        }
    })
    app = Flask(__name__, instance_relative_config=True)
    app.logger.debug("Parsing configuration...")

    app.logger.info("The token to include is")
    app.logger.info(token)
    from googleplex_assistant.controllers import hello, config, ifttt, dialogflow
    app.register_blueprint(hello.bp)
    app.register_blueprint(config.bp)
    app.register_blueprint(ifttt.bp)
    app.register_blueprint(dialogflow.bp)

    from googleplex_assistant.helpers import upnpclient
    upnpclient.init_app()
    upnpclient.map_port()

    return app

application = create_app()


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

# 3rd party imports
import logging

import numpy as np

from .e_vxb import e_vxb

# Local imports
from .resample import resample

__author__ = "Louis Richard"
__email__ = "louisr@irfu.se"
__copyright__ = "Copyright 2020-2023"
__license__ = "MIT"
__version__ = "2.4.2"
__status__ = "Prototype"

def vht(e, b, no_ez: bool = False):
    r"""Estimate velocity of the De Hoffman-Teller frame from the velocity
    estimate the electric field eht=-vht x b

    Parameters
    ----------
    e : xarray.DataArray
        Time series of the electric field.
    b : xarray.DataArray
        Time series of the magnetic field.
    no_ez : boolean, Optional
        If True assumed no Ez. Default is False.

    Returns
    -------
    vht : numpy.ndarray
        De Hoffman Teller frame velocity [km/s].
    vht : xarray.DataArray
        Time series of the electric field in the De Hoffman frame.
    dv_ht : numpy.ndarray
        Error of De Hoffman Teller frame.

    """

    n_samples = len(e)

    # Resample magnetic field to electric field sampling (usually higher)

    if n_samples != len(b):
        b = resample(b, e)

    p = np.zeros(6)

    p[0] = np.sum(b[:, 0].data * b[:, 0].data) / n_samples  # Bx*Bx
    p[1] = np.sum(b[:, 0].data * b[:, 1].data) / n_samples  # Bx*By
    p[2] = np.sum(b[:, 0].data * b[:, 2].data) / n_samples  # Bx*Bz
    p[3] = np.sum(b[:, 1].data * b[:, 1].data) / n_samples  # By*By
    p[4] = np.sum(b[:, 1].data * b[:, 2].data) / n_samples  # By*Bz
    p[5] = np.sum(b[:, 2].data * b[:, 2].data) / n_samples  # Bz*Bz

    # assume only Ex and Ey
    if no_ez:
        e[:, 2] *= 0  # put z component to 0 when using only Ex and Ey

        k_mat = np.array(
            [[p[5], 0, -p[2]], [0, p[5], -p[4]], [-p[2], -p[4], p[0] + p[3]]],
        )
    else:
        k_mat = np.array(
            [
                [p[3] + p[5], -p[1], -p[2]],
                [-p[1], p[0] + p[5], -p[4]],
                [-p[2], -p[4], p[0] + p[3]],
            ],
        )

    exb = np.cross(e, b)

    ind_data = np.where(~np.isnan(exb[:, 0].data))[0]

    # revised by Wenya LI; 2015-11-21, wyli @ irfu
    exb_avg = np.sum(exb[ind_data, :], axis=0) / n_samples

    # averExB=sum(ExB(indData).data,1)/nSamples;
    # end revise.
    v_ht = np.linalg.solve(k_mat, exb_avg.T) * 1e3  # 9.12 in ISSI book

    v_ht_hat = v_ht / np.linalg.norm(v_ht, keepdims=True)

    logging.info(
        "v_ht =%(v_mag)7.4f * %(v_vec)s km/s",
        {"v_mag": np.linalg.norm(v_ht), "v_vec": np.array_str(v_ht_hat)},
    )

    # Calculate the goodness of the Hoffman Teller frame
    e_ht = e_vxb(v_ht, b)

    if no_ez:
        e_p, e_ht_p = [e[ind_data], e_ht[ind_data]]
        e_p.data[:, 2], e_ht_p.data[:, 2] = [0, 0]
    else:
        e_p, e_ht_p = [e[ind_data], e_ht[ind_data]]

    delta_e = e_p.data - e_ht_p.data

    poly_fit = np.polyfit(
        e_ht_p.data.reshape([len(e_ht_p) * 3]),
        e_p.data.reshape([len(e_p) * 3]),
        1,
    )
    corr_coeff = np.corrcoef(
        e_ht_p.data.reshape([len(e_ht_p) * 3]),
        e_p.data.reshape([len(e_p) * 3]),
    )

    logging.info(
        "slope = %(slope)6.4f, offs = %(offset)6.4f, cc = %(cc)6.4f",
        {"slope": poly_fit[0], "offset": poly_fit[1], "cc": corr_coeff[0, 1]},
    )

    dv_ht = np.sum(np.sum(delta_e**2)) / len(ind_data)
    s_mat = (dv_ht / (2 * len(ind_data) - 3)) / k_mat
    dv_ht = np.sqrt(np.diag(s_mat)) * 1e3

    dv_ht_hat = dv_ht / np.linalg.norm(dv_ht)

    logging.info(
        "dv_ht =%(dv_mag)7.4f * %(dv_vec)s km/s",
        {"dv_mag": np.linalg.norm(dv_ht), "dv_vec": np.array_str(dv_ht_hat)},
    )

    return v_ht, e_ht, dv_ht


#This script is similar to context_retrieval and context_retrieval_probout.
#In addition, it facilitates two things
# 1) It randomly picks num_examples contexts out of topk most similar docs.
# 2) It calculates the average similarity/distance of context examples to the test doc.

from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM

import torch
from util.util_eval_probout import evaluate_rel, get_logger
from util.util_diverse_context_retrieval import PromptDatasetDiverseContext
import argparse

import pandas as pd
import os
import json
from pathlib import Path

def get_model_and_tokenizer(args, device):

    #Load and set the tokenizer
    tokenizer = AutoTokenizer.from_pretrained(args.model_name, padding_side="left")
    tokenizer.pad_token = tokenizer.eos_token

    #Load and send model to gpu
    #model = AutoModelForCausalLM.from_pretrained(args.model_name)
    model = AutoModelForCausalLM.from_pretrained(args.model_name, low_cpu_mem_usage=True, torch_dtype=torch.bfloat16)
    model.to(device)

    return model, tokenizer

def main(args):
    #First configure our logger
    log = get_logger(os.path.join(args.experiments_main_folder, args.experiment_folder, args.log))

    device = "cuda:"+args.device_cuda if torch.cuda.is_available() else "cpu"

    model,tokenizer = get_model_and_tokenizer(args,device)

    #queries, subjects, labels, wiki_dict = get_raw_data(args=args)
    #template = "The Wall (French: Le Mur) by Jean-Paul Sartre, a collection of short stories published in 1939 containing the eponymous story \"The Wall\", is considered one of the author's greatest existentialist works of fiction. Sartre dedicated the book to his companion Olga Kosakiewicz, a former student of Simone de Beauvoir.\nThe author of The Wall is Jean-Paul Sartre\n\nDragonsbane is a fantasy novel written by author Barbara Hambly and published by Del Rey Books in 1985.\nThe author of Dragonsbane is Barbara Hambly\n\n"
    #if args.out_triplet:
    #    template = "The Wall (French: Le Mur) by Jean-Paul Sartre, a collection of short stories published in 1939 containing the eponymous story \"The Wall\", is considered one of the author's greatest existentialist works of fiction. Sartre dedicated the book to his companion Olga Kosakiewicz, a former student of Simone de Beauvoir.\n(author of, The Wall, Jean-Paul Sartre)\n\nDragonsbane is a fantasy novel written by author Barbara Hambly and published by Del Rey Books in 1985.\n(author of, Dragonsbane, Barbara Hambly)\n\n"

    dataset = PromptDatasetDiverseContext(args=args)
    log("dataset is ready with size " + str(len(dataset)))

    log("Start predictions for the dataset")

    results_df = evaluate_rel(model, tokenizer, dataset, args, device=device, log=log)

    log("Done all the prediction! Now saving the prediction results")

    write_file = args.prediction_path
    write_file = os.path.join(args.experiments_main_folder, args.experiment_folder, write_file)
    results_df.to_csv(write_file, index=False)

if __name__ == "__main__":

    parser = argparse.ArgumentParser()

    parser.add_argument('--model_name', type=str, default="togethercomputer/GPT-JT-6B-v1", help='Currently supported model is togethercomputer/GPT-JT-6B-v1. It should work with other auto-regressive models with minor modifications.')
    parser.add_argument('-tmp', '--temperature', type=float, default=0.0001, help='The temperature of the model sampling')

    parser.add_argument('--path_data', type=str, default="DocRED/data/relation_docs_dev", help='Path to the DOCRED relation documents of evaluation set')
    parser.add_argument('--path_embeddings', type=str, default="DocRED/data/embeddings_dev.pkl", help='Path to the DOCRED document embeddings of evaluation set')

    parser.add_argument('--path_context', type=str, default="DocRED/data/relation_docs_distant", help='Path to the DOCRED relation documents to be used for in-context examples')
    parser.add_argument('--path_context_embeddings', type=str, default="DocRED/data/embeddings_train_distant.pkl", help='Path to the DOCRED in-context document embeddings')

    #Subset the documents that are extremely long. (to enable fitting into the memory)
    parser.add_argument('--doc_max_chars', type=int, default=1000, help='Remove paragraphs longer than doc_max_chars chars (for context)')

    #Below 2 lines are to subset the documents having relations between 'min_num_rels' and 'max_num_rels'. Default is no subsetting.
    parser.add_argument('--min_num_rels', type=int, default=0, help='Remove paragraphs having less relations than min_num_rels (for context)')
    parser.add_argument('--max_num_rels', type=int, default=99, help='Remove paragraphs having more relations than max_num_rels (for context)')

    #To control which relation to evaluate
    parser.add_argument('-rel', '--relation', type=str, default="P1001", help='enter the id of the relation to evaluate')

    #To control which relation to evaluate
    parser.add_argument('-dp', '--doc_prefix', type=str, default="Context: ", help='Enter the prefix to identify the context paragraph')
    #To control which relation to evaluate
    parser.add_argument('-rp', '--relation_prefix', type=str, default="Relation: ", help='Enter the prefix to identify the relation extracted')

    #To control the number of few-shot examples. NOTE: Refers to 'top-K' in paper
    parser.add_argument('-n_ex', '--num_examples', type=int, default=5, help='number of example contexts to extract triplets')
    #Total number of most-similar context examples, before random selection of num_examples. NOTE: Refers to 'top-N' in paper
    parser.add_argument('-t_k', '--topk', type=int, default=20, help='Total number of most-similar context examples considered to extract triplets')
    #In case of multiple relations exist (for the same context) use below argument to extract all relations
    parser.add_argument('-om', '--output_mode', type=str, default="multi", help='whether or not to output multiple relatons. Choose from {single, multi}')

    parser.add_argument('-bs', '--batch_size', type=int, default=4, help='Batch size')
    parser.add_argument('-mnt', '--max_new_tokens', type=int, default=200, help='max number of new tokens to be generated')
    parser.add_argument('--device_cuda', type=str, default="0", help='the cuda device to be used')

    #For adversarial experiments. NOT NEEDED FOR THE MAIN RESULTS
    parser.add_argument('--subset_context', type=float, default=1.0, help='What percentage of subset to keep. Used for noise experiments')

    parser.add_argument('-emf', '--experiments_main_folder', type=str, default="./experiments_gpt", help='path to the main experiment folder')
    parser.add_argument('-ef', '--experiment_folder', type=str, default="default_exp", help='name of subdirectory for the specific experiment')
    parser.add_argument('--prediction_path', type=str, default="predictions.csv", help='name of the prediction file')
    parser.add_argument('-l', '--log', type=str, default='train.log')

    #To control what is the separator
    parser.add_argument('-sep', '--separator', type=str, default=" <==>", help='special string to separate triplet elements')

    parser.add_argument('--seed', type=int, default=0, help='Randomness')

    args = parser.parse_args()

    full_experiment_path = os.path.join(args.experiments_main_folder, args.experiment_folder)
    full_experiment_path = Path(full_experiment_path)
    full_experiment_path.mkdir(parents=True, exist_ok=True)

    with open(os.path.join(args.experiments_main_folder, args.experiment_folder, 'commandline_args.txt'), 'w') as f:
        json.dump(args.__dict__, f, indent=2)

    main(args)


#!/usr/bin/python
import os
import sys

def print_zone(zone_name, master_ip):
    return """
zone "{}" {{
  type slave;
  file "/var/cache/bind/db.{}";
  masters {{ {}; }};
}};

""".format(zone_name, zone_name, master_ip)

if __name__ == '__main__':
    if len(sys.argv) < 2:
        raise Exception("usage: {} command-to-run args".format(sys.argv[0]))

    zone_names = os.environ['ZONE_NAMES'].split()
    master_ip = os.environ['MASTER_IP']

    with open('/etc/bind/named.conf.local', 'w') as conf_fh:
        for zone_name in zone_names:
            conf_fh.write(print_zone(zone_name, master_ip))

    os.execvp(sys.argv[1], sys.argv[1:])


#######################################
# Importing required librarie Pipeline#
#######################################

print("Step 1: Required libraries imported successfully")

import time
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

from sklearn.model_selection import train_test_split
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import GridSearchCV
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import chi2
from imblearn.over_sampling import SMOTE
from imblearn.pipeline import Pipeline as imbpipeline
from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer
from sklearn.impute import SimpleImputer
from sklearn.preprocessing import OneHotEncoder
from sklearn.preprocessing import OrdinalEncoder
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import accuracy_score
from sklearn.metrics import roc_auc_score
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix
from sklearn.metrics import f1_score
from sklearn.svm import SVC
from xgboost import XGBClassifier
from lightgbm import LGBMClassifier
from sklearn.dummy import DummyClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.tree import ExtraTreeClassifier
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.linear_model import RidgeClassifier
from sklearn.linear_model import SGDClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import AdaBoostClassifier
from sklearn.ensemble import BaggingClassifier
from sklearn.naive_bayes import BernoulliNB
from sklearn.neural_network import MLPClassifier

####################
# To ignore warning#
####################

from warnings import simplefilter
from sklearn.exceptions import ConvergenceWarning
simplefilter("ignore", category=ConvergenceWarning)

################################################
# Loading online_shoppers_intention.csv dataset#
################################################

print("Step 2: Created DataFrame successfully")

df = pd.read_csv("online_shoppers_intention.csv")

######################
# Feature Engineering#
######################

print("Step 3: Feature Engineering Done successfully on Weekend, Revenue")

df['Weekend'] = df['Weekend'].replace((True, False), (1, 0))
df['Revenue'] = df['Revenue'].replace((True, False), (1, 0))

condition = df['VisitorType']=='Returning_Visitor'

#################################
# Added Returning_Visitor column#
#################################

print("Step 4: Added Returning_Visitor column successfully")

df['Returning_Visitor'] = np.where(condition, 1, 0)

df = df.drop(columns=['VisitorType'])

############################################
# Applying One Hot Encoding on Month column#
############################################

print("Step 5: Applied one hot encoding successfully on Month column")

ordinal_encoder = OrdinalEncoder()
df['Month'] = ordinal_encoder.fit_transform(df[['Month']])

#########################################
# Checking correlation on Revenue column#
#########################################

print("Step 6: Checking correlation done successfully")

result = df[df.columns[1:]].corr()['Revenue']
result1 = result.sort_values(ascending=False)

###########################################
# Prepairing Features as X and target as y#
###########################################

print("Step 7: Prepairing features as X and target as y done successfully")

X = df.drop(['Revenue'], axis=1)
y = df['Revenue']

####################################
# Prepairing Train and Test Dataset#
####################################

print("Step 8: Splitting data X_train, X_test, y_train & y_test done successfully")

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state = 0)

#################
# Model Pipeline#
#################

print("Step 9: model_pipeline fcuntion created done successfully")

def model_pipeline(X, model):
    n_c = X.select_dtypes(exclude=['object']).columns.values.tolist()
    c_c = X.select_dtypes(include=['object']).columns.values.tolist()

    numeric_columns = n_c
    categorical_columns = c_c

    numeric_pipeline = SimpleImputer(strategy = 'constant')

    categorical_pipeline = OneHotEncoder(handle_unknown = 'ignore')

    a = ('numeric', numeric_pipeline, numeric_columns)
    b = ('categorical', categorical_pipeline, categorical_columns)

    preprocessor = ColumnTransformer(

    transformers = [a, b],
    remainder = 'passthrough'

    )

    c = ('preprocessor', preprocessor)
    d = ('smote', SMOTE(random_state = 1))
    e = ('scaler', MinMaxScaler())
    f = ('feature_selection', SelectKBest(score_func = chi2, k = 6))
    g = ('model', model)

    bundled_pipeline = imbpipeline(steps = [c, d, e, f, g])

    return bundled_pipeline

##################
# Model Selection#
##################

print("Step 10: select_model fcuntion created done successfully")

def select_model(X, y, pipeline=None):

    classifiers = {}

    c_d1 = {"DummyClassifier": DummyClassifier(strategy='most_frequent')}
    classifiers.update(c_d1)

    xgb = XGBClassifier(
    verbosity=0,
    use_label_encoder=False,
    eval_metric='logloss',
    objective='binary:logistic',
    )

    c_d2 = {"XGBClassifier": xgb}
    classifiers.update(c_d2)

    c_d3 = {"LGBMClassifier": LGBMClassifier()}
    classifiers.update(c_d3)

    c_d4 = {"RandomForestClassifier": RandomForestClassifier()}
    classifiers.update(c_d4)

    c_d5 = {"DecisionTreeClassifier": DecisionTreeClassifier()}
    classifiers.update(c_d5)

    c_d6 = {"ExtraTreeClassifier": ExtraTreeClassifier()}
    classifiers.update(c_d6)

    c_d7 = {"ExtraTreesClassifier": ExtraTreeClassifier()}
    classifiers.update(c_d7)

    c_d8 = {"AdaBoostClassifier": AdaBoostClassifier()}
    classifiers.update(c_d8)

    c_d9 = {"KNeighborsClassifier": KNeighborsClassifier()}
    classifiers.update(c_d9)

    c_d10 = {"RidgeClassifier": RidgeClassifier()}
    classifiers.update(c_d10)

    c_d11 = {"SGDClassifier": SGDClassifier()}
    classifiers.update(c_d11)

    c_d12 = {"BaggingClassifier": BaggingClassifier()}
    classifiers.update(c_d12)

    c_d13 = {"BernoulliNB": BernoulliNB()}
    classifiers.update(c_d13)

    c_d14 = {"SVC": SVC()}
    classifiers.update(c_d14)

    c_d15 = {"MLPClassifier":MLPClassifier()}
    classifiers.update(c_d15)

    mlpc = {
    "MLPClassifier (paper)": MLPClassifier(hidden_layer_sizes=(27, 50),
    max_iter=300,
    activation='relu',
    solver='adam',
    random_state=1)
    }
    c_d16 = mlpc
    classifiers.update(c_d16)

    cols = ['model', 'run_time', 'roc_auc']
    df_models = pd.DataFrame(columns = cols)

    for key in classifiers:

        start_time = time.time()

        print()
        print("Step 12: model_pipeline run successfully on", key)

        pipeline = model_pipeline(X_train, classifiers[key])

        cv = cross_val_score(pipeline, X, y, cv=10, scoring='roc_auc')

        row = {'model': key,
               'run_time': format(round((time.time() - start_time)/60,2)),
               'roc_auc': cv.mean(),
        }

        df_models = pd.concat([df_models, pd.DataFrame([row])], ignore_index=True)

    df_models = df_models.sort_values(by='roc_auc', ascending=False)

    return df_models

#####################################
# Access Model select_model function#
#####################################

print("Step 11: Accessing select_model function done successfully")

models = select_model(X_train, y_train)

###################################
# Lets see total model with score #
###################################

print("Step 13: Accessing select_model function done successfully")

print(models)

#####################################
# Accessing best model and training #
#####################################

print("Step 14: Accessing select_model function done successfully")

selected_model = MLPClassifier()
bundled_pipeline = model_pipeline(X_train, selected_model)
bundled_pipeline.fit(X_train, y_train)

#####################################
# Accessing best model and training #
#####################################

print("Step 15: Results predicted successfully")
y_pred = bundled_pipeline.predict(X_test)

print(y_pred)

#####################
# ROC and AOC score #
#####################

print("Step 16: ROC and AOC scores")

roc_auc = roc_auc_score(y_test, y_pred)
accuracy = accuracy_score(y_test, y_pred)
f1_score = f1_score(y_test, y_pred)

print('ROC/AUC:', roc_auc)
print('Accuracy:', accuracy)
print('F1 score:', f1_score)

#########################
# Classification report #
#########################

print("Step 17: classification report generated successfully")

classif_report = classification_report(y_test, y_pred)

print(classif_report)

########################################
# BOSS its a right time to celebrate :)#
########################################


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import scipy.io.wavfile as wavfile
import mne
from scipy import signal as sgn
import Processing
from numpy.fft import fft, fftfreq
import parselmouth
import opensmile
from parselmouth.praat import call
import textgrids
import os
import Funciones
import seaborn as sn

## PARAMETROS
s = 21
trial = 1
channel = 1

valores_faltantes_pitch = np.nan
audio_sr = 16000
sampleStep = 0.01

sr = 128
audio_sr = 16000
tmin, tmax = -0.6, -0.003
delays = - np.arange(np.floor(tmin * sr), np.ceil(tmax * sr), dtype=int)

## PHN features

sesiones = [21, 22, 23, 24, 25, 26, 27, 29, 30]

all_labels = []

for sesion in sesiones:

    phn_path = "Datos/phonemes/S" + str(sesion) + "/"
    trials = list(set([int(fname.split('.')[2]) for fname in os.listdir(phn_path) if
                       os.path.isfile(phn_path + f'/{fname}')]))
    for trial in trials:
        for channel in [1, 2]:
            try:
                phn_fname = "Datos/phonemes/S" + str(sesion) + "/s" + str(sesion) + ".objects." + "{:02d}".format(trial) + ".channel" + \
                            str(channel) + ".aligned_fa.TextGrid"
                # phn_fname = "Datos/phonemes/S" + str(sesion) + "/manual/s" + str(sesion) + "_objects_" + "{:02d}".format(
                #     trial) + "_channel" + str(channel) + "_aligned_faTAMARA.TextGrid"

                phrases_fname = "Datos/phrases/S" + str(sesion) + "/s" + str(sesion) + ".objects." + "{:02d}".format(trial) + ".channel" + str(
                        channel) + ".phrases"

                # Get trial total length
                phrases = pd.read_table(phrases_fname, header=None, sep="\t")
                trial_tmax = phrases[1].iloc[-1]

                # Phonemes
                grid = textgrids.TextGrid(phn_fname)

                phonemes = grid['transcription : phones']
                phonemes[0].xmin = 0.

                # Parse
                labels = []
                times = []
                samples = []

                for ph in phonemes:
                    label = ph.text.transcode()
                    label = label.replace(' ', '')
                    label = label.replace('º', '')
                    label = label.replace('-', '')
                    # Rename silences
                    if label == 'sil' or label == 'sp' or label == 'sile' or label == 'silsil'\
                            or label == 'SP' or label == 's¡p' or label == 'sils':
                        label = ""
                    labels.append(label)
                    times.append((ph.xmin, ph.xmax))
                    samples.append(np.round((ph.xmax - ph.xmin) * sr).astype("int"))

                labels.append("")
                times.append((ph.xmin, trial_tmax))
                samples.append(np.round((trial_tmax - ph.xmax) * sr).astype("int"))

                # Get unique phonemes
                labels_set = set(labels)
                unique_labels = (list(labels_set))

                all_labels.append(unique_labels)
            except:
                print(f'Could not upload Sesion: {sesion} - Trial {trial} - Channel {channel}')

dfvowels = pd.read_csv('vowels.csv', header=None, index_col=0)
dfconsonants = pd.read_csv('consonants.csv', header=None, index_col=0)
dfarpabet = pd.concat([pd.get_dummies(dfconsonants), pd.get_dummies(dfvowels)]).fillna(0)
dfarpabet.index.name = 'arpabet'
dfarpabet = dfarpabet.sort_values('arpabet')
plt.figure(figsize=(12, 12))
sn.heatmap(dfarpabet)

## Phonemes

sesiones = [21, 22, 23, 24, 25, 26, 27, 29, 30]

all_labels = []

for sesion in sesiones:

    phn_path = "Datos/phonemes/S" + str(sesion) + "/"
    trials = list(set([int(fname.split('.')[2]) for fname in os.listdir(phn_path) if
                       os.path.isfile(phn_path + f'/{fname}')]))
    for trial in trials:
        for channel in [1, 2]:
            try:
                phn_fname = "Datos/phonemes/S" + str(sesion) + "/s" + str(sesion) + ".objects." + "{:02d}".format(trial) + ".channel" + \
                            str(channel) + ".aligned_fa.TextGrid"
                # phn_fname = "Datos/phonemes/S" + str(sesion) + "/manual/s" + str(sesion) + "_objects_" + "{:02d}".format(
                #     trial) + "_channel" + str(channel) + "_aligned_faTAMARA.TextGrid"

                phrases_fname = "Datos/phrases/S" + str(sesion) + "/s" + str(sesion) + ".objects." + "{:02d}".format(trial) + ".channel" + str(
                        channel) + ".phrases"

                # Get trial total length
                phrases = pd.read_table(phrases_fname, header=None, sep="\t")
                trial_tmax = phrases[1].iloc[-1]

                # Phonemes
                grid = textgrids.TextGrid(phn_fname)

                phonemes = grid['transcription : phones']
                phonemes[0].xmin = 0.

                # Parse
                labels = []
                times = []
                samples = []

                for ph in phonemes:
                    label = ph.text.transcode()
                    label = label.replace(' ', '')
                    label = label.replace('º', '')
                    label = label.replace('-', '')
                    # Rename silences
                    if label == 'sil' or label == 'sp' or label == 'sile' or label == 'silsil'\
                            or label == 'SP' or label == 's¡p' or label == 'sils':
                        label = ""
                    labels.append(label)
                    times.append((ph.xmin, ph.xmax))
                    samples.append(np.round((ph.xmax - ph.xmin) * sr).astype("int"))

                labels.append("")
                times.append((ph.xmin, trial_tmax))
                samples.append(np.round((trial_tmax - ph.xmax) * sr).astype("int"))

                # Get unique phonemes
                labels_set = set(labels)
                unique_labels = (list(labels_set))

                all_labels.append(unique_labels)
            except:
                print(f'Could not upload Sesion: {sesion} - Trial {trial} - Channel {channel}')

all_labels = Funciones.flatten_list(all_labels)

labels_set = set(all_labels)
unique_labels = (list(labels_set))

unique_labels.sort()

ph_labels = ['', 'CH', 'NY', 'R', 'a', 'b', 'd', 'e', 'f', 'g', 'i', 'k', 'l', 'm', 'n', 'o', 'p', 'r', 's', 't', 'u', 'x', 'y']
ph_labels_man = ['', '(d)o', 'CH', 'NY', 'R', 'a', 'ap', 'b', 'chas', 'd', 'e', 'es', 'f', 'g', 'i', 'k', 'l', 'lg',
                 'm', 'n', 'ns', 'o', 'p', 'r', 's', 't', 'u', 'x', 'y']

ph_labels_man = ['', '(d)o', 'A', 'AH', 'CH', 'F', 'NY', 'R', 'SP', 'Y', 'a', 'ap', 'b', 'br', 'c', 'chas', 'd', 'de',
                 'e', 'es', 'f', 'g', 'h', 'i', 'k', 'l', 'l-', 'lg', 'm', 'n', 'ns', 'o', 'p', 'r', 's', 'si', 'sils',
                 's¡p', 'sº', 't', 'u', 'v', 'x', 'y']

# WAV
wav_fname = "Datos/wavs/S" + str(s) + "/s" + str(s) + ".objects." + "{:02d}".format(trial) + ".channel" + str(
    channel) + ".wav"
wav = wavfile.read(wav_fname)[1]
wav = wav.astype("float")

# ENVELOPE
envelope = np.abs(sgn.hilbert(wav))
window_size = 125
stride = 125
envelope = np.array(
    [np.mean(envelope[i:i + window_size]) for i in range(0, len(envelope), stride) if i + window_size <= len(envelope)])
envelope = envelope.ravel().flatten()

diferencia = np.sum(samples) - len(envelope)
if diferencia > 0:
    samples[-1] -= diferencia
elif diferencia < 0:
    samples[-1] += diferencia

# Make empty df of phonemes
df = pd.DataFrame(0, index=np.arange(np.sum(samples)), columns=unique_labels)

#
phonemes_tgrid = np.repeat(labels, samples)

for i, phoneme in enumerate(phonemes_tgrid):
    df.loc[i, phoneme] = envelope[i]
    # df.iloc[i][phoneme] = 1

## EEG
Band = None
l_freq_eeg, h_freq_eeg = Processing.band_freq(Band)

eeg_fname = "Datos/EEG/S" + str(s) + "/s" + str(s) + "-" + str(channel) + "-Trial" + str(trial) + "-Deci-Filter-Trim-ICA-Pruned.set"
eeg = mne.io.read_raw_eeglab(eeg_fname)
eeg_freq = eeg.info.get("sfreq")
info = eeg.info
eeg.load_data()
if Band: eeg = eeg.filter(l_freq=l_freq_eeg, h_freq=h_freq_eeg, phase='minimum')
# eeg.plot()

## Source estimation
from mne.datasets import fetch_fsaverage
import os

montage = mne.channels.make_standard_montage('biosemi128')
eeg.set_montage(montage)
eeg.set_eeg_reference(projection=True)  # needed for inverse modeling

# Download fsaverage files
fs_dir = r'C:\Users\joaco\OneDrive - The University of Nottingham\MEGEYEHS\DATA\MRI_DATA\FreeSurfer_out\fsaverage'
subjects_dir = os.path.dirname(fs_dir)
os.environ["SUBJECTS_DIR"] = subjects_dir

# The files live in:
subject = 'fsaverage'
trans = 'trans.fif'  # MNE has a built-in fsaverage transformation
src = os.path.join(fs_dir, 'bem', 'fsaverage-ico-5-src.fif')
bem = os.path.join(fs_dir, 'bem', 'fsaverage-5120-5120-5120-bem-sol.fif')
# Check that the locations of EEG electrodes is correct with respect to MRI

eeg_info_path = 'eeg_info.fif'
eeg.save(eeg_info_path)

mne.gui.coregistration(subject='fsaverage', subjects_dir=subjects_dir, inst=eeg_info_path)

mne.viz.plot_alignment(eeg.info, src=src, eeg=['original', 'projected'], trans=trans,
                       show_axes=False, mri_fiducials=False)

# eeg = eeg.to_data_frame()
# eeg = np.array(eeg)[:, 1:129]  # paso a array y tomo tiro la primer columna de tiempos

# eeg = np.array(eeg._data * 1e6).transpose()  # 1e6 es por el factor de scaling del eeg.to_data_frame()
#
# # Downsample
# eeg = Processing.subsamplear(eeg, int(eeg_freq / sr))

# plt.savefig('gráficos/Raw_EEG.png')
# plt.savefig('{}Theta.png'.format(s))

## PSD
psds_welch_mean, freqs_mean = mne.time_frequency.psd_array_welch(eeg._data, sfreq=eeg_freq, fmin=1, fmax=60)

fig, ax = plt.subplots()
evoked = mne.EvokedArray(psds_welch_mean, info)
evoked.times = freqs_mean
evoked.plot(scalings=dict(eeg=1, grad=1, mag=1), zorder='std', time_unit='s',
            show=False, spatial_colors=True, unit=False, units='w', axes=ax)
ax.set_xlabel('Frequency [Hz]')
ax.grid()
# plt.savefig('NO_CAUSAL.png')

## WAV
wav_fname = "Datos/wavs/S" + str(s) + "/s" + str(s) + ".objects." + "{:02d}".format(trial) + ".channel" + str(
    channel) + ".wav"
wav = wavfile.read(wav_fname)[1]
wav = wav.astype("float")

## ENVELOPE

envelope = np.abs(sgn.hilbert(wav))

window_size = 125
stride = 125
envelope = np.array(
    [np.mean(envelope[i:i + window_size]) for i in range(0, len(envelope), stride) if i + window_size <= len(envelope)])
envelope = envelope.ravel().flatten()

envelope_x = np.linspace(0, len(wav)/audio_sr, len(envelope))
wav_x = np.linspace(0, len(wav)/audio_sr, len(wav))
fig, ax = plt.subplots()
plt.plot(wav_x, wav, label='Audio signal')
plt.plot(envelope_x, envelope, label='Envelope')
ax.set_xlabel('Time [s]')
ax.set_ylabel('Amplitude')
plt.grid()
plt.legend()

## PLOT spectre
sp = fft(envelope)
freq = fftfreq(envelope.shape[-1], d=1 / 128)
plt.figure()
plt.plot(freq, np.abs(sp))
plt.xlim([0, 30])
plt.ylim([0, 3e4])
plt.grid()
plt.title('Hilbert + Butter + Prom')
plt.xlabel('Frecuency [Hz]')
plt.ylabel('Amplitud')

## PITCH Pablo
path = r"C:\Users\joaco\Desktop\Joac\Facultad\Tesis\Código\Datos\Pitch\Pablo\tracks_generales"
subject_letter = 'A' if channel else 'B'
pitch_name = path + '\{}_{}.csv'.format(s,subject_letter)

df = pd.read_csv(pitch_name)
trial_changes = [i for i, value in enumerate(np.diff(df['time'].values)<0) if value]
pitch = df['pitch'].values

pitch_trial = pitch[:trial_changes[0]]
plt.plot(pitch_trial, label='Pablo')

## PITCH
pitch_fname = "Datos/Pitch_threshold_0.03/S" + str(s) + "/s" + str(s) + ".objects." + "{:02d}".format(trial) + ".channel" + str(
    channel) + ".txt"

read_file = pd.read_csv(pitch_fname)

time = np.array(read_file['time'])
pitch = np.array(read_file['pitch'])
intensity = np.array(read_file['intensity'])

pitch[pitch == '--undefined--'] = np.nan
pitch = np.array(pitch, dtype=float)

pitch_der = Funciones.sliding_window(df=pd.DataFrame(pitch))

if not valores_faltantes_pitch:
    pitch[np.isnan(pitch)] = valores_faltantes_pitch
    pitch_der[np.isnan(pitch_der)] = valores_faltantes_pitch
else:
    pitch[np.isnan(pitch)] = float(valores_faltantes_pitch)
    pitch_der[np.isnan(pitch_der)] = float(valores_faltantes_pitch)
    pitch[np.isnan(pitch)] = float(valores_faltantes_pitch)
    pitch_der[np.isnan(pitch_der)] = float(valores_faltantes_pitch)
# else:
#     print('Invalid missing value for pitch {}'.format(valores_faltantes_pitch) + '\nMust be finite.')

pitch = np.array(np.repeat(pitch, audio_sr * sampleStep), dtype=float)
pitch = Processing.subsamplear(pitch, 125)

pitch_der = np.array(np.repeat(pitch_der, audio_sr * sampleStep), dtype=float)
pitch_der = Processing.subsamplear(pitch_der, 125)

fig, ax = plt.subplots()
plt.plot(np.linspace(0, len(wav)/audio_sr, len(wav)), wav, label='Audio signal [Amplitude]')
plt.plot(np.linspace(0,len(wav)/audio_sr, len(pitch)), pitch, label='Pitch [Hz]')
ax.set_xlabel('Time [s]')
ax.set_ylabel('Magnitude')
plt.grid()
plt.legend()

## Normalice features Leave nans for plot

no_nan_pitch = pitch[~np.isnan(pitch)]
no_nan_pitch_der = pitch_der[~np.isnan(pitch_der)]

norm = Processing.normalizar()
norm.normalize_01(envelope)
norm.normalize_01(no_nan_pitch)
norm.normalize_11(no_nan_pitch_der)
no_nan_pitch_der -= no_nan_pitch_der.mean()

pitch[~np.isnan(pitch)] = no_nan_pitch
pitch_der[~np.isnan(pitch_der)] = no_nan_pitch_der

pitch = np.array(np.repeat(pitch, audio_sr * sampleStep), dtype=float)
pitch = Processing.subsamplear(pitch, 125)

pitch_der = np.array(np.repeat(pitch_der, audio_sr * sampleStep), dtype=float)
pitch_der = Processing.subsamplear(pitch_der, 125)

norm.normalize_11(wav)
wav -= wav.mean()

## Spectrogram
import librosa
import librosa.display

n_fft = 125
hop_length = 125
n_mels = 16

S = librosa.feature.melspectrogram(wav, sr=audio_sr, n_fft=n_fft, hop_length=hop_length, n_mels=n_mels)
S_DB = librosa.power_to_db(S, ref=np.max)

plt.figure()
librosa.display.specshow(S_DB, sr=audio_sr, hop_length=hop_length, x_axis='time', y_axis='mel', )
plt.colorbar(format='%+2.0f dB')

# Shifted matrix row by row
spec_shift = Processing.matriz_shifteada(S_DB[0], delays)
for i in np.arange(1, len(S_DB)):
    spec_shift = np.hstack((spec_shift, Processing.matriz_shifteada(S_DB[i], delays)))

fmin = n_fft/2
mel_f = librosa.mel_frequencies(n_mels+2, fmin=fmin, fmax=8000)
low = mel_f[:-2]
center = mel_f[1:-1]
high = mel_f[2:]

## PLOT
time = np.arange(len(wav)) / 16000

# plt.ion()
# plt.figure()
# plt.plot(time, wav, label='Audio signal')
plt.plot(np.linspace(0, time[-1], len(envelope)), envelope, label='Envelope')
plt.plot(np.linspace(0, time[-1], len(S_DB[-1])), S_DB[-1], label='Spectrogram')
# plt.plot(np.linspace(0, time[-1], len(pitch)), pitch, label='Pitch [Hz]')
# plt.plot(np.linspace(0, time[-1], len(pitch_der)), pitch_der, label='Pitch derivate [Hz/s]')
plt.ylabel('Magnitude')
plt.xlabel('Time [s]')
# plt.xlim([41,43])
plt.title('Scaled audio features')
plt.legend()

## SHIMMER
smile = opensmile.Smile(
    feature_set=opensmile.FeatureSet.eGeMAPSv02,
    feature_level=opensmile.FeatureLevel.LowLevelDescriptors,
    sampling_rate=16000)

y = smile.process_file(wav_fname)
y.index = y.index.droplevel(0)
y.index = y.index.map(lambda x: x[0].total_seconds())

shimmer = y['shimmerLocaldB_sma3nz']

mcm = Funciones.minimo_comun_multiplo(len(shimmer), len(envelope))
shimmer = np.repeat(shimmer, mcm/len(shimmer))
shimmer = Processing.subsamplear(shimmer, mcm/len(envelope))

norm.normalize_11(wav)
wav -= wav.mean()

fig, ax = plt.subplots()
plt.plot(np.linspace(0, len(wav)/audio_sr, len(wav)), wav*5, label='Audio signal')
plt.plot(np.linspace(0,len(wav)/audio_sr, len(shimmer)), shimmer, label='Shimmer')
ax.set_xlabel('Time [s]')
ax.set_ylabel('Amplitude')
plt.grid()
plt.legend()

## JITTER

smile = opensmile.Smile(
    feature_set=opensmile.FeatureSet.eGeMAPSv02,
    feature_level=opensmile.FeatureLevel.LowLevelDescriptors)

y = smile.process_file(wav_fname)
y.index = y.index.droplevel(0)
y.index = y.index.map(lambda x: x[0].total_seconds())
jitter = y['jitterLocal_sma3nz']

mcm = Funciones.minimo_comun_multiplo(len(jitter), len(envelope))
jitter = np.repeat(jitter, mcm/len(jitter))
jitter = Processing.subsamplear(jitter, mcm/len(envelope))

plt.figure(figsize=(16, 4))
plt.plot(jitter)
plt.title('Jitter')

## CSSP

snd = parselmouth.Sound(wav_fname)

data = []
frame_length = 0.2
hop_length = 1 / 128
t1s = np.arange(0, snd.duration - frame_length, hop_length)
times = zip(t1s, t1s + frame_length)

for t1, t2 in times:
    powercepstrogram = call(snd.extract_part(t1, t2), "To PowerCepstrogram", 60, 0.0020001, 5000, 50)
    cpps = call(powercepstrogram, "Get CPPS", "yes", 0.02, 0.0005, 60, 330, 0.05, "Parabolic", 0.001, 0,
                "Exponential decay", "Robust")
    data.append(cpps)

data = np.array(np.repeat(data, audio_sr * sampleStep))
data = Processing.subsamplear(data, 125)
data = Processing.matriz_shifteada(data, delays)

cssp = np.array(data)
print(cssp.shape)

plt.figure(figsize=(16, 4))
plt.plot(cssp[:,0])
plt.title('CPPS')


a = [2, 4, 7, 9, 11, 19, 23]
#7을 찾는 경우!
'''
def binarySearch(a, key):
    key = 7
    for i in range(0, len(a)-1):
        if key < a[(len(a)//2 + 1)]:
'''

arr1 = [2, 4, 7, 9, 11, 19, 23]  #리스트 안의 숫자는 int형?
key = int(input())
#      0  1  2  3   4   5   6
# 중간값이 끝값보다 작거나 같으면
def binarySearch(arr):
    start = 0
    end = len(arr)-1 #arr의 마지막 값

    while (start <= end): #처음 값
        mid = (start+end)//2  #4
        if arr[mid] == key:
            return True
        elif arr[mid] > key:
            end = mid - 1
        else:
            start = mid + 1
    return False

print(binarySearch(arr1, key))


import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import json

def seirm_ode(model_params, initial_conditions, max_time):
    s0, e0, i0, r0, m0 = initial_conditions
    N = s0 + e0 + i0 + r0 + m0

    if type(model_params["beta"]) != list:
        model_params["beta"] = [model_params["beta"] for t in range(max_time)]
    if type(model_params["alpha"]) != list:
        model_params["alpha"] = [model_params["alpha"] for t in range(max_time)]
    if type(model_params["gamma"]) != list:
        model_params["gamma"] = [model_params["gamma"] for t in range(max_time)]
    if type(model_params["mu"]) != list:
        model_params["mu"] = [model_params["mu"] for t in range(max_time)]

    ts = {}
    ts["S"], ts["E"], ts["I"], ts["R"], ts["M"] = [s0], [e0], [i0], [r0], [m0]

    for t in range(max_time):
        dS = -model_params["beta"][t] * ts["S"][-1] * ts["I"][-1] / N
        dE = model_params["beta"][t] * ts["S"][-1] * ts["I"][-1] / N - model_params["alpha"][t] * ts["E"][-1]
        dI = model_params["alpha"][t] * ts["E"][-1] - (model_params["gamma"][t] + model_params["mu"][t]) * ts["I"][-1]
        dR = model_params["gamma"][t] * ts["I"][-1]
        dM = model_params["mu"][t] * ts["I"][-1]

        ts["S"].append(ts["S"][-1] + dS)
        ts["E"].append(ts["E"][-1] + dE)
        ts["I"].append(ts["I"][-1] + dI)
        ts["R"].append(ts["R"][-1] + dR)
        ts["M"].append(ts["M"][-1] + dM)

    return ts, N

def linear_parameter_time_series(init_val, final_val, max_time):
    return list(np.arange(init_val, final_val, (final_val - init_val) / max_time))

def sigmoid_parameter_time_series(init_val, final_val, max_time, const):
    return list(init_val + (final_val - init_val) * 1 / (1 + np.exp(-np.arange(-const, const, 2 * const / max_time))))

def get_epiweek_data(max_time):
    epiweeks = []
    year = 2020
    epiweek_num = 10

    for i in range(max_time):
        if epiweek_num >= 52:
            epiweek_num = 0
            year += 1
        epiweeks.append(str(year) + str(int(epiweek_num + i // 7)))
    return epiweeks

def model_params_to_json(model_params, max_time):
    data = {}
    for i in range(max_time):
        data["ode_" + str(i)] = {}
        for param in ["alpha", "beta", "gamma", "mu"]:
            if type(model_params[param]) == float:
                data["ode_" + str(i)][param] = model_params[param]
            else:
                data["ode_" + str(i)][param] = model_params[param][i]
    return json.dumps(data)

def plot_time_series(ts, N, title=None):
    for label, time_series in ts.items():
        plt.plot(np.array(time_series) / N, label=label + "(t)")

    plt.ylabel("Proportion of Population")
    plt.xlabel("Time Step")
    plt.legend()

    if title is not None:
        plt.title(title)
    plt.show()

def plot_time_series_with_params(ts, N, model_params, title=None):
    fig, ax = plt.subplots(2)

    for label, time_series in ts.items():
        ax[0].plot(np.array(time_series) / N, label=label + "(t)")

    ax[0].set_ylabel("Proportion of Population")
    ax[0].legend()

    for label, time_series in model_params.items():
        if label == "beta":
            letter = "β"
        if label == "alpha":
            letter = "α"
        if label == "gamma":
            letter = "γ"
        if label == "mu":
            letter = "µ"
        ax[1].plot(time_series, label=letter + "(t)")

    ax[1].set_ylabel("Parameter Value")
    ax[1].set_ylim(0, 0.45)
    ax[1].set_xlabel("Time Step")
    ax[1].legend()

    if title is not None:
        ax[0].set_title(title)

    plt.show()

if __name__ == "__main__":
    max_time = 200

    model_params = {
        "beta": 0.3,
        "alpha": linear_parameter_time_series(0.1, 0.3, max_time),
        "gamma": 0.03,
        "mu": 0.01,
    }
    file_name = "dataset.csv"

    initial_conditions = (900, 0, 100, 0, 0)
    ts, N = seirm_ode(model_params, initial_conditions, max_time)

    # plot_time_series(ts, N, title="SEIRM (β=0.3, α=0.2, γ=0.05, µ=0.02)")
    # plot_time_series_with_params(ts, N, model_params, title="SEIRM (β varies, α=0.2, γ varies, µ=0.02)")

    data = pd.DataFrame(ts)
    data.to_csv(file_name, index=False)


import pytest
from rest_framework.request import Request
from rest_framework.test import APIRequestFactory

from epic_app.models.epic_questions import (
    EvolutionQuestion,
    LinkagesQuestion,
    NationalFrameworkQuestion,
)
from epic_app.serializers.question_serializer import (
    EvolutionQuestionSerializer,
    LinkagesQuestionSerializer,
    NationalFrameworkQuestionSerializer,
)
from epic_app.tests.epic_db_fixture import epic_test_db

@pytest.fixture(autouse=True)
def question_serializer_fixture(
    epic_test_db: pytest.fixture,
):
    """
    Dummy fixture just to load a default db from dummy_db.

    Args:
        epic_test_db (pytest.fixture): Fixture to load for the whole file tests.
    """
    pass

def get_serializer():
    factory = APIRequestFactory()
    request = factory.get("/")

    return {
        "request": Request(request),
    }

serializer_context = get_serializer()

@pytest.mark.django_db
class TestNationalFrameworkSerializer:
    def test_given_valid_instances_when_to_representation_returns_expected_data(self):
        def validate_fields(dict_item: dict) -> bool:
            return "description" in list(dict_item.keys())

        serialized_data = list(
            NationalFrameworkQuestionSerializer(
                NationalFrameworkQuestion.objects.all(),
                many=True,
                context=serializer_context,
            ).data
        )

        assert len(serialized_data) == 2
        assert all(map(validate_fields, serialized_data))

@pytest.mark.django_db
class TestEvolutionQuestionSerializer:
    def test_given_valid_instances_when_to_representation_returns_expected_data(self):
        def validate_fields(dict_item: dict) -> bool:
            valid_fields = [
                "nascent_description",
                "engaged_description",
                "capable_description",
                "effective_description",
            ]
            return all(v_field in list(dict_item.keys()) for v_field in valid_fields)

        serialized_data = list(
            EvolutionQuestionSerializer(
                EvolutionQuestion.objects.all(), many=True, context=serializer_context
            ).data
        )

        assert len(serialized_data) == 2
        assert all(map(validate_fields, serialized_data))

@pytest.mark.django_db
class TestLinkagesQuestionSerializer:
    def test_given_valid_instances_when_to_representation_returns_expected_data(self):
        serialized_data = list(
            LinkagesQuestionSerializer(
                LinkagesQuestion.objects.all(), many=True, context=serializer_context
            ).data
        )

        assert len(serialized_data) == 1


import torch
import torch.nn as nn
import enum
import torch.nn.functional as F

class UpsampleType(enum.Enum):
    Bilinear = 0
    Deconv = 1

class FlexDecoder(nn.Module):
    def __init__(self,
                 num_classes,
                 in_max_channel=1024,
                 block_dims=(512, 256, 128, 64, 32, 16, 8),
                 upsample_type=UpsampleType.Deconv,
                 use_batchnorm=False,
                 use_softmax=False):
        super(FlexDecoder, self).__init__()
        self.upsample2d_type = upsample_type
        self.use_softmax = use_softmax

        if upsample_type == UpsampleType.Bilinear:
            self.upsample2d = nn.UpsamplingBilinear2d(scale_factor=2.)
        elif upsample_type == UpsampleType.Deconv:
            self.upsample2d = nn.ModuleList()

        in_channel = in_max_channel
        self.block_list = nn.ModuleList()

        for idx, out_dim in enumerate(block_dims):
            if use_batchnorm:
                block = nn.Sequential(
                    nn.Conv2d(in_channel, out_dim, 3, 1, 1, bias=False),
                    nn.BatchNorm2d(out_dim),
                    nn.ReLU(inplace=True),
                    nn.Conv2d(out_dim, out_dim, 3, 1, 1, bias=False),
                    nn.BatchNorm2d(out_dim),
                    nn.ReLU(inplace=True),
                )
            else:
                block = nn.Sequential(
                    nn.Conv2d(in_channel, out_dim, 3, 1, 1, bias=False),
                    nn.ReLU(inplace=True),
                    nn.Conv2d(out_dim, out_dim, 3, 1, 1, bias=False),
                    nn.ReLU(inplace=True),
                )
            if upsample_type == UpsampleType.Deconv:
                upsample_op = nn.Sequential(
                    nn.ConvTranspose2d(in_channel, out_dim, 4, 2, 1, bias=False),
                    nn.ReLU(inplace=True)
                )
                self.upsample2d.append(upsample_op)

            in_channel = out_dim
            self.block_list.append(block)

        self.cls_pred_conv = nn.Conv2d(in_channel, num_classes, 1, bias=True)

    def forward(self, inputs):
        feat_list = inputs

        feat_list.reverse()
        x_i = feat_list[0]
        for idx, feat in enumerate(feat_list[:-1]):
            x_i_before = feat_list[idx + 1]
            if self.upsample2d_type == UpsampleType.Deconv:
                p_i = self.upsample2d[idx](x_i)
            elif self.upsample2d_type == UpsampleType.Bilinear:
                p_i = self.upsample2d(x_i)
            else:
                raise NotImplementedError()

            concat_i = torch.cat([p_i, x_i_before], dim=1)

            out_i = self.block_list[idx](concat_i)

            x_i = out_i

        logit = self.cls_pred_conv(x_i)
        if self.use_softmax:
            prob = F.softmax(logit, dim=1)
        else:
            prob = F.sigmoid(logit)
        ret = {
            'logit': logit,
            'prob': prob
        }
        return ret