import numpy as np
import matplotlib.pyplot as plt
from testCases import *
import sklearn
import sklearn.datasets
from sklearn.linear_model import LogisticRegressionCV
from planar_utils import plot_decision_boundary, sigmoid, load_planar_dataset, load_extra_datasets
from sklearn.metrics import accuracy_score

np.random.seed(1)
X, Y = load_planar_dataset()

# plt.scatter(X[0, :], X[1, :], c=Y.ravel(), s=40, cmap=plt.cm.Spectral)
# plt.show()

### START CODE HERE ### (≈ 3 lines of code)
shape_X = X.shape
shape_Y = Y.shape
m = shape_X[1]  # training set size
### END CODE HERE ###

print('The shape of X is: ' + str(shape_X))
print('The shape of Y is: ' + str(shape_Y))
print('I have m = %d training examples!' % (m))

clf = LogisticRegressionCV()
clf.fit(X.T, Y.T)
# Plot the decision boundary for logistic regression
# plt.title("Logistic Regression")
# plot_decision_boundary(lambda x: clf.predict(x), X, Y)

# Print accuracy
LR_predictions = clf.predict(X.T)
print()
print('Accuracy of logistic regression: %d ' % float(
    (np.dot(Y, LR_predictions) + np.dot(1 - Y, 1 - LR_predictions)) / float(Y.size) * 100) +
      '% ' + "(percentage of correctly labelled datapoints)")

# GRADED FUNCTION: layer_sizes

def layer_sizes(X, Y):
    """
    Arguments:
    X -- input dataset of shape (input size, number of examples)
    Y -- labels of shape (output size, number of examples)

    Returns:
    n_x -- the size of the input layer
    n_h -- the size of the hidden layer
    n_y -- the size of the output layer
    """
    ### START CODE HERE ### (≈ 3 lines of code)
    n_x = X.shape[0]  # size of input layer
    n_h = 4
    n_y = Y.shape[0]  # size of output layer
    ### END CODE HERE ###
    return (n_x, n_h, n_y)

X_assess, Y_assess = layer_sizes_test_case()
(n_x, n_h, n_y) = layer_sizes(X_assess, Y_assess)
print()
print("The size of the input layer is: n_x = " + str(n_x))
print("The size of the hidden layer is: n_h = " + str(n_h))
print("The size of the output layer is: n_y = " + str(n_y))

# GRADED FUNCTION: initialize_parameters

def initialize_parameters(n_x, n_h, n_y):
    """
    Argument:
    n_x -- size of the input layer
    n_h -- size of the hidden layer
    n_y -- size of the output layer

    Returns:
    params -- python dictionary containing your parameters:
                    W1 -- weight matrix of shape (n_h, n_x)
                    b1 -- bias vector of shape (n_h, 1)
                    W2 -- weight matrix of shape (n_y, n_h)
                    b2 -- bias vector of shape (n_y, 1)
    """

    np.random.seed(2)  # we set up a seed so that your output matches ours although the initialization is random.

    ### START CODE HERE ### (≈ 4 lines of code)
    W1 = np.random.randn(n_h, n_x) * 0.01
    b1 = np.zeros((n_h, 1))
    W2 = np.random.randn(n_y, n_h) * 0.01
    b2 = np.zeros((n_y, 1))
    ### END CODE HERE ###

    assert (W1.shape == (n_h, n_x))
    assert (b1.shape == (n_h, 1))
    assert (W2.shape == (n_y, n_h))
    assert (b2.shape == (n_y, 1))

    parameters = {"W1": W1,
                  "b1": b1,
                  "W2": W2,
                  "b2": b2}

    return parameters

n_x, n_h, n_y = initialize_parameters_test_case()

parameters = initialize_parameters(n_x, n_h, n_y)
print()
print("W1 = " + str(parameters["W1"]))
print("b1 = " + str(parameters["b1"]))
print("W2 = " + str(parameters["W2"]))
print("b2 = " + str(parameters["b2"]))

# GRADED FUNCTION: forward_propagation

def forward_propagation(X, parameters):
    """
    Argument:
    X -- input data of size (n_x, m)
    parameters -- python dictionary containing your parameters (output of initialization function)

    Returns:
    A2 -- The sigmoid output of the second activation
    cache -- a dictionary containing "Z1", "A1", "Z2" and "A2"
    """
    # Retrieve each parameter from the dictionary "parameters"
    ### START CODE HERE ### (≈ 4 lines of code)
    W1 = parameters["W1"]
    b1 = parameters["b1"]
    W2 = parameters["W2"]
    b2 = parameters["b2"]
    ### END CODE HERE ###

    # Implement Forward Propagation to calculate A2 (probabilities)
    ### START CODE HERE ### (≈ 4 lines of code)
    Z1 = np.dot(W1, X) + b1
    A1 = np.tanh(Z1)
    Z2 = np.dot(W2, A1) + b2
    A2 = sigmoid(Z2)
    ### END CODE HERE ###

    assert (A2.shape == (1, X.shape[1]))

    cache = {"Z1": Z1,
             "A1": A1,
             "Z2": Z2,
             "A2": A2}

    return A2, cache

X_assess, parameters = forward_propagation_test_case()

A2, cache = forward_propagation(X_assess, parameters)

# Note: we use the mean here just to make sure that your output matches ours.
print()
print(np.mean(cache['Z1']), np.mean(cache['A1']), np.mean(cache['Z2']), np.mean(cache['A2']))

# GRADED FUNCTION: compute_cost

def compute_cost(A2, Y, parameters):
    """
    Computes the cross-entropy cost given in equation (13)

    Arguments:
    A2 -- The sigmoid output of the second activation, of shape (1, number of examples)
    Y -- "true" labels vector of shape (1, number of examples)
    parameters -- python dictionary containing your parameters W1, b1, W2 and b2

    Returns:
    cost -- cross-entropy cost given equation (13)
    """

    m = Y.shape[1]  # number of example

    # Compute the cross-entropy cost
    ### START CODE HERE ### (≈ 2 lines of code)
    logprobs = -1 / m * np.nan_to_num(np.multiply(Y, np.log(A2)) + np.multiply(1 - Y, np.log(1 - A2)))
    cost = np.sum(logprobs)
    ### END CODE HERE ###

    cost = np.squeeze(cost)  # makes sure cost is the dimension we expect.
    # E.g., turns [[17]] into 17
    assert (isinstance(cost, float))

    return cost

A2, Y_assess, parameters = compute_cost_test_case()

print()
print("cost = " + str(compute_cost(A2, Y_assess, parameters)))

# GRADED FUNCTION: backward_propagation

def backward_propagation(parameters, cache, X, Y):
    """
    Implement the backward propagation using the instructions above.

    Arguments:
    parameters -- python dictionary containing our parameters
    cache -- a dictionary containing "Z1", "A1", "Z2" and "A2".
    X -- input data of shape (2, number of examples)
    Y -- "true" labels vector of shape (1, number of examples)

    Returns:
    grads -- python dictionary containing your gradients with respect to different parameters
    """
    m = X.shape[1]

    # First, retrieve W1 and W2 from the dictionary "parameters".
    ### START CODE HERE ### (≈ 2 lines of code)
    W1 = parameters["W1"]
    W2 = parameters["W2"]
    ### END CODE HERE ###

    # Retrieve also A1 and A2 from dictionary "cache".
    ### START CODE HERE ### (≈ 2 lines of code)
    A1 = cache["A1"]
    A2 = cache["A2"]
    ### END CODE HERE ###

    # Backward propagation: calculate dW1, db1, dW2, db2.
    ### START CODE HERE ### (≈ 6 lines of code, corresponding to 6 equations on slide above)
    dZ2 = A2 - Y
    dW2 = np.dot(dZ2, A1.T) / m
    db2 = 1 / m * np.sum(dZ2, axis=1, keepdims=True)
    dZ1 = np.multiply(np.dot(W2.T, dZ2), 1 - np.power(A1, 2))
    dW1 = 1 / m * np.dot(dZ1, X.T)
    db1 = 1 / m * np.sum(dZ1, axis=1, keepdims=True)
    ### END CODE HERE ###

    grads = {"dW1": dW1,
             "db1": db1,
             "dW2": dW2,
             "db2": db2}

    return grads

parameters, cache, X_assess, Y_assess = backward_propagation_test_case()

grads = backward_propagation(parameters, cache, X_assess, Y_assess)
print()
print("dW1 = " + str(grads["dW1"]))
print("db1 = " + str(grads["db1"]))
print("dW2 = " + str(grads["dW2"]))
print("db2 = " + str(grads["db2"]))

# GRADED FUNCTION: update_parameters

def update_parameters(parameters, grads, learning_rate=1.2):
    """
    Updates parameters using the gradient descent update rule given above

    Arguments:
    parameters -- python dictionary containing your parameters
    grads -- python dictionary containing your gradients

    Returns:
    parameters -- python dictionary containing your updated parameters
    """
    # Retrieve each parameter from the dictionary "parameters"
    ### START CODE HERE ### (≈ 4 lines of code)
    W1 = parameters["W1"]
    b1 = parameters["b1"]
    W2 = parameters["W2"]
    b2 = parameters["b2"]
    ### END CODE HERE ###

    # Retrieve each gradient from the dictionary "grads"
    ### START CODE HERE ### (≈ 4 lines of code)
    dW1 = grads["dW1"]
    db1 = grads["db1"]
    dW2 = grads["dW2"]
    db2 = grads["db2"]
    ## END CODE HERE ###

    # Update rule for each parameter
    ### START CODE HERE ### (≈ 4 lines of code)
    W1 -= learning_rate * dW1
    b1 -= learning_rate * db1
    W2 -= learning_rate * dW2
    b2 -= learning_rate * db2
    ### END CODE HERE ###

    parameters = {"W1": W1,
                  "b1": b1,
                  "W2": W2,
                  "b2": b2}

    return parameters

parameters, grads = update_parameters_test_case()
parameters = update_parameters(parameters, grads)

print()
print("W1 = " + str(parameters["W1"]))
print("b1 = " + str(parameters["b1"]))
print("W2 = " + str(parameters["W2"]))
print("b2 = " + str(parameters["b2"]))

# GRADED FUNCTION: nn_model

def nn_model(X, Y, n_h, num_iterations=10000, print_cost=False):
    """
    Arguments:
    X -- dataset of shape (2, number of examples)
    Y -- labels of shape (1, number of examples)
    n_h -- size of the hidden layer
    num_iterations -- Number of iterations in gradient descent loop
    print_cost -- if True, print the cost every 1000 iterations

    Returns:
    parameters -- parameters learnt by the model. They can then be used to predict.
    """

    np.random.seed(3)
    n_x = layer_sizes(X, Y)[0]
    n_y = layer_sizes(X, Y)[2]

    # Initialize parameters, then retrieve W1, b1, W2, b2. Inputs: "n_x, n_h, n_y". Outputs = "W1, b1, W2, b2, parameters".
    ### START CODE HERE ### (≈ 5 lines of code)
    parameters = initialize_parameters(n_x, n_h, n_y)
    W1 = parameters["W1"]
    b1 = parameters["b1"]
    W2 = parameters["W2"]
    b2 = parameters["b2"]
    ### END CODE HERE ###

    # Loop (gradient descent)

    for i in range(0, num_iterations):

        ### START CODE HERE ### (≈ 4 lines of code)
        # Forward propagation. Inputs: "X, parameters". Outputs: "A2, cache".
        A2, cache = forward_propagation(X, parameters)

        # Cost function. Inputs: "A2, Y, parameters". Outputs: "cost".
        cost = compute_cost(A2, Y, parameters)

        # Backpropagation. Inputs: "parameters, cache, X, Y". Outputs: "grads".
        grads = backward_propagation(parameters, cache, X, Y)

        # Gradient descent parameter update. Inputs: "parameters, grads". Outputs: "parameters".
        parameters = update_parameters(parameters, grads)

        ### END CODE HERE ###

        # Print the cost every 1000 iterations
        if print_cost and i % 1000 == 0:
            print("Cost after iteration %i: %f" % (i, cost))

    return parameters

X_assess, Y_assess = nn_model_test_case()

parameters = nn_model(X_assess, Y_assess, 4, num_iterations=10000, print_cost=False)

print()
print("W1 = " + str(parameters["W1"]))
print("b1 = " + str(parameters["b1"]))
print("W2 = " + str(parameters["W2"]))
print("b2 = " + str(parameters["b2"]))

# GRADED FUNCTION: predict

def predict(parameters, X):
    """
    Using the learned parameters, predicts a class for each example in X

    Arguments:
    parameters -- python dictionary containing your parameters
    X -- input data of size (n_x, m)

    Returns
    predictions -- vector of predictions of our model (red: 0 / blue: 1)
    """

    # Computes probabilities using forward propagation, and classifies to 0/1 using 0.5 as the threshold.
    ### START CODE HERE ### (≈ 2 lines of code)
    A2, cache = forward_propagation(X, parameters)
    predictions = np.where(A2 > 0.5, 1, 0)
    ### END CODE HERE ###

    return predictions

parameters, X_assess = predict_test_case()

predictions = predict(parameters, X_assess)
print()
print("predictions mean = " + str(np.mean(predictions)))

# Build a model with a n_h-dimensional hidden layer
parameters = nn_model(X, Y, n_h=4, num_iterations=10000, print_cost=True)

# Plot the decision boundary
# plt.title("Decision Boundary for hidden layer size " + str(4))
# plot_decision_boundary(lambda x: predict(parameters, x.T), X, Y)

# Print accuracy
predictions = predict(parameters, X)
print('Accuracy: %d' % float((np.dot(Y, predictions.T) + np.dot(1 - Y, 1 - predictions.T)) / float(Y.size) * 100) + "%")
print('Accuracy: %d' % float(accuracy_score(Y.ravel(), predictions.ravel())) + "%")

# This may take about 2 minutes to run

# plt.figure(figsize=(16, 32))
# hidden_layer_sizes = [1, 2, 3, 4, 5, 10, 20]
# for i, n_h in enumerate(hidden_layer_sizes):
#     plt.subplot(5, 2, i + 1)
#     plt.title('Hidden Layer of size %d' % n_h)
#     parameters = nn_model(X, Y, n_h, num_iterations=5000)
#     plot_decision_boundary(lambda x: predict(parameters, x.T), X, Y, False)
#     predictions = predict(parameters, X)
#     accuracy = float((np.dot(Y, predictions.T) + np.dot(1 - Y, 1 - predictions.T)) / float(Y.size) * 100)
#     print("Accuracy for {} hidden units: {} %".format(n_h, accuracy))
# plt.show()

# Datasets
noisy_circles, noisy_moons, blobs, gaussian_quantiles, no_structure = load_extra_datasets()

datasets = {"noisy_circles": noisy_circles,
            "noisy_moons": noisy_moons,
            "blobs": blobs,
            "gaussian_quantiles": gaussian_quantiles}

### START CODE HERE ### (choose your dataset)
dataset = "noisy_moons"
### END CODE HERE ###

X, Y = datasets[dataset]
X, Y = X.T, Y.reshape(1, Y.shape[0])

# make blobs binary
if dataset == "blobs":
    Y = Y % 2

# Visualize the data
# plt.scatter(X[0, :], X[1, :], c=Y.ravel(), s=40, cmap=plt.cm.Spectral)

plt.figure(figsize=(16, 16))
hidden_layer_sizes = [1, 2, 3, 4]
for i, n_h in enumerate(hidden_layer_sizes):
    plt.subplot(5, 2, i + 1)
    parameters = nn_model(X, Y, n_h, num_iterations=5000)
    plot_decision_boundary(lambda x: predict(parameters, x.T), X, Y, False)
    predictions = predict(parameters, X)
    accuracy = float((np.dot(Y, predictions.T) + np.dot(1 - Y, 1 - predictions.T)) / float(Y.size) * 100)
    print("Accuracy for {} hidden units: {} %".format(n_h, accuracy))
    plt.xlabel("Accuracy: {} Hidden Layer of size {}".format(accuracy, n_h))

plt.show()


import os
import sys
import boto3
import argparse
import json
from collections import defaultdict
from botocore import exceptions as botoexcept

"""
A tool for retrieving basic information from the running EC2 instances.
Largely taken from https://gist.github.com/dastergon/b4994c605f76d528d0c4
but modified to allow argument-based specification of tag name and value,
query region and to display brief or extended details in both text or
json. Text output will be in columns for density reasons.
"""

def confirm_settings(TagName, TagValue, Region, Profile, Output):
    message = """
AWS Profile: {}
AWS Region:  {}
Tag Name:    {}
Tag Value:   {}
Output Mode: {}
""".format(
        Profile, Region, TagName, TagValue, Output
    )

    print(message)

    try:
        input("Press Enter to continue or CTRL+C to exit.")
    except KeyboardInterrupt:
        sys.exit("")

def get_ec2_instances(TagName, TagValue, Region, Profile, OutputMode, Extended):

    try:
        # Connect to EC2
        session = boto3.Session(profile_name=Profile)
        ec2 = session.resource("ec2", Region)

        # Get information for all running instances
        running_instances = ec2.instances.filter(
            Filters=[{"Name": "tag:" + TagName, "Values": [TagValue]}]
        )

        ec2info = defaultdict()
        for instance in running_instances:
            for tag in instance.tags:
                if tag["Key"] == "Name":
                    name = tag["Value"]

                hostname = "Undefined"
                if "HostName" in tag["Key"]:
                    hostname = tag["Value"]

                privateip = "Undefined"
                if instance.private_ip_address != None:
                    privateip = instance.private_ip_address

                publicip = "Undefined"
                if instance.public_ip_address != None:
                    publicip = instance.public_ip_address

                # Render datetime object
                launchtime = "{}".format(instance.launch_time)

            # Add instance info to a dictionary
            if Extended:
                ec2info[instance.id] = {
                    "Instance Id": instance.id,
                    "Name": name,
                    "HostName": hostname,
                    "Private IP": privateip,
                    "Public IP": publicip,
                    "Type": instance.instance_type,
                    "State": instance.state["Name"],
                    "Launch Time": launchtime,
                }
            else:
                ec2info[instance.id] = {
                    "Instance Id": instance.id,
                    "Name": name,
                    "Private IP": privateip,
                    "State": instance.state["Name"],
                }

    except botoexcept.NoCredentialsError as nocrederr:
        sys.exit(nocrederr)

    except botoexcept.UnauthorizedSSOTokenError as ssotokenerr:
        sys.exit(ssotokenerr)

    except botoexcept.ProfileNotFound as profileerr:
        sys.exit(profileerr)

    if OutputMode == "json":
        print(json.dumps(ec2info, indent=4))
    else:
        # Determine column width by checking string lengths + 2 characters for padding
        if len(ec2info.items()) > 0:
            col_width = (
                max(
                    len(data_value)
                    for instance_id, instance_data in ec2info.items()
                    for data_key, data_value in instance_data.items()
                )
                + 2
            )

            # Set up column headers
            if Extended:
                col_headers = [
                    "Instance Id",
                    "Name",
                    "HostName",
                    "Private IP",
                    "Public IP",
                    "Type",
                    "State",
                    "Launch Time",
                ]
            else:
                col_headers = ["Instance Id", "Name", "Private IP", "State"]

            # Setup output
            print("")
            print("".join(col_header.ljust(col_width) for col_header in col_headers))
            print("=" * (col_width * len(col_headers)))

            for instance_id, instance_data in ec2info.items():
                print(
                    "".join(
                        data_value.ljust(col_width)
                        for data_key, data_value in instance_data.items()
                    )
                )

        else:
            # No results returned
            print("")
            print("No results found.")

    print("")

def main():
    # Double-check we're running with at least Python 3.9
    if sys.version_info < (3,9):
        sys.exit("Error: Python 3.9 or higher is required.")

    # Parse command line arguments/options
    parser = argparse.ArgumentParser(description="Program arguments and options")
    parser.add_argument("tag", help="Tag to use to filter instances.")
    parser.add_argument("value", help="Value of the supplied tag.")
    parser.add_argument(
        "--profile",
        metavar="profile",
        nargs="?",
        help="AWS profile to use for authentication. Default: AWS_PROFILE var",
        default="",
    )
    parser.add_argument(
        "--region",
        metavar="region",
        nargs="?",
        help="AWS region to query. Default: eu-west-2",
        default="eu-west-2",
    )
    parser.add_argument(
        "--extended",
        metavar="extended",
        help="Display extended instance detail.",
        action=argparse.BooleanOptionalAction,
        default=False,
    )
    parser.add_argument(
        "--output",
        default="text",
        type=str,
        help="Output format.",
        choices=["text", "json"],
    )
    parser.add_argument(
        "--confirm",
        metavar="confirm",
        help="Verify settings before executing query.",
        action=argparse.BooleanOptionalAction,
        default=False,
    )
    args = parser.parse_args()

    # Check to ensure user's AWS_PROFILE env var is set or we've been
    # given a value via the --profile argument. Values provided via
    # command line argument take precedence.
    if not os.environ.get("AWS_PROFILE") and args.profile == "":
        sys.exit("Error: AWS_PROFILE is not set and no profile specified.")
    elif os.environ.get("AWS_PROFILE") and args.profile == "":
        awsprofile = os.environ.get("AWS_PROFILE")
    elif not os.environ.get("AWS_PROFILE") and args.profile != "":
        awsprofile = args.profile
    elif os.environ.get("AWS_PROFILE") and args.profile != "":
        awsprofile = args.profile
    else:
        sys.exit("Unexpected error determining AWS Profile.")

    # Confirm script settings before continuing
    if args.confirm:
        confirm_settings(args.tag, args.value, args.region, awsprofile, args.output)

    # Connect to API and run the query
    try:
        get_ec2_instances(
            args.tag, args.value, args.region, awsprofile, args.output, args.extended
        )
    except KeyboardInterrupt:
        sys.exit("\nOperation cancelled by user.\n")

if __name__ == "__main__":
    main()


import logging
import os.path
from typing import List, Optional

from pipenv.patched.pip._internal.cli.spinners import open_spinner
from pipenv.patched.pip._internal.utils.setuptools_build import make_setuptools_bdist_wheel_args
from pipenv.patched.pip._internal.utils.subprocess import call_subprocess, format_command_args

logger = logging.getLogger(__name__)

def format_command_result(
    command_args: List[str],
    command_output: str,
) -> str:
    """Format command information for logging."""
    command_desc = format_command_args(command_args)
    text = f"Command arguments: {command_desc}\n"

    if not command_output:
        text += "Command output: None"
    elif logger.getEffectiveLevel() > logging.DEBUG:
        text += "Command output: [use --verbose to show]"
    else:
        if not command_output.endswith("\n"):
            command_output += "\n"
        text += f"Command output:\n{command_output}"

    return text

def get_legacy_build_wheel_path(
    names: List[str],
    temp_dir: str,
    name: str,
    command_args: List[str],
    command_output: str,
) -> Optional[str]:
    """Return the path to the wheel in the temporary build directory."""
    # Sort for determinism.
    names = sorted(names)
    if not names:
        msg = ("Legacy build of wheel for {!r} created no files.\n").format(name)
        msg += format_command_result(command_args, command_output)
        logger.warning(msg)
        return None

    if len(names) > 1:
        msg = (
            "Legacy build of wheel for {!r} created more than one file.\n"
            "Filenames (choosing first): {}\n"
        ).format(name, names)
        msg += format_command_result(command_args, command_output)
        logger.warning(msg)

    return os.path.join(temp_dir, names[0])

def build_wheel_legacy(
    name: str,
    setup_py_path: str,
    source_dir: str,
    global_options: List[str],
    build_options: List[str],
    tempd: str,
) -> Optional[str]:
    """Build one unpacked package using the "legacy" build process.

    Returns path to wheel if successfully built. Otherwise, returns None.
    """
    wheel_args = make_setuptools_bdist_wheel_args(
        setup_py_path,
        global_options=global_options,
        build_options=build_options,
        destination_dir=tempd,
    )

    spin_message = f"Building wheel for {name} (setup.py)"
    with open_spinner(spin_message) as spinner:
        logger.debug("Destination directory: %s", tempd)

        try:
            output = call_subprocess(
                wheel_args,
                command_desc="python setup.py bdist_wheel",
                cwd=source_dir,
                spinner=spinner,
            )
        except Exception:
            spinner.finish("error")
            logger.error("Failed building wheel for %s", name)
            return None

        names = os.listdir(tempd)
        wheel_path = get_legacy_build_wheel_path(
            names=names,
            temp_dir=tempd,
            name=name,
            command_args=wheel_args,
            command_output=output,
        )
        return wheel_path


# -*- coding: utf-8 -*-
import os, sys
import time, datetime
import argparse
import configparser
import wx
import wx.lib.agw.aui as aui
from pubsub import pub

from panel_one import PanelOne

class TaskConfig():
    """
    Read config settings
    """
    def __init__(self):
        self.config = configparser.ConfigParser(allow_no_value=True)
        self.criteria = configparser.RawConfigParser()
        self.criteria.optionxform = str
        self.config_file = "config/settings.ini"

        # read the file
        self.read_config_file()

    def read_config_file(self):
        # print("Read config file {}".format(self.config_file))
        if not os.path.exists(self.config_file):
            print("Config file ({}) does not exist.".format(self.config_file))
            sys.exit(999)
        else:
            self.config.read(self.config_file, encoding="utf-8")

    def get_config_item(self, category, item):
        return self.config.get(category, item).strip()

    def get_criteria_item(self, category, item):
        return self.criteria.get(category, item).strip()

class TaskFrame(wx.Frame):
    """
    The main frame of the application
    """
    def __init__(self, title, icon):
        # ensure the parent's __init__ is called
        super(TaskFrame, self).__init__(None, title=title, style=wx.DEFAULT_FRAME_STYLE)

        # get screen size
        self.screenSize = wx.DisplaySize()
        #self.width, self.high = self.screenSize
        #print(self.width, self.high)

        # set icon
        self.SetIcon(wx.Icon(icon))
        # ----------
        # get current work path
        self.cwd = os.getcwd()

        # read config files
        self.config = TaskConfig()

        # set font size for all control, 9 is default
        font = self.GetFont()
        font.SetPointSize(15)
        #self.SetFont(font)
        font.SetWeight(wx.FONTWEIGHT_BOLD)

        # create menu bar
        self.create_menu()

        # create status bar
        self.create_statusBar()
        self.statusBar.SetStatusText("Program Idle...")

        # create panel
        self.panel = wx.Panel(self)
        # self.notebook = wx.Notebook(self.panel) # "wx.Notebook" cannot disable tabs... Use "wx.lib.agw.aui.AuiNotebook" instead
        self.notebook = aui.AuiNotebook(self.panel, agwStyle=aui.AUI_NB_DEFAULT_STYLE^aui.AUI_NB_CLOSE_ON_ACTIVE_TAB)
        self.notebook.SetFont(font)

        self.notebook.pageChanged_count = 0 # !!! when page changed, we need a count to initial the pages....
        self.notebook.Bind(aui.EVT_AUINOTEBOOK_PAGE_CHANGED, self.PageChanged)

        self.page1 = PanelOne(self.notebook, self.screenSize, self.menu_bar, self.config)
        self.notebook.AddPage(self.page1, "Calibration")

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.notebook, border=5, proportion=1, flag=wx.ALL|wx.EXPAND)
        self.panel.SetSizer(sizer)

        # create pubsub receiver
        pub.subscribe(self.updateStatusBar, "updateStatusBar")

        # remove black rectangular issue !!!
        self.Layout()

    def delNotebookPage(self, pageTitle):
        for index in range(self.notebook.GetPageCount()):
            page = self.notebook.GetPageText(index)
            if page == pageTitle:
                self.notebook.DeletePage(index)
                self.notebook.SendSizeEvent()
                break

    # ----------Menu Related Start ---------- #
    def create_menu(self):
        """
        Create the menu
        """
        self.menu_bar = wx.MenuBar()

        menu_file = wx.Menu()

        m_reload = menu_file.Append(-1, "Reload Initial Setting Files\tCtrl-R", "Reload Initial Setting Files")
        self.Bind(wx.EVT_MENU, self.on_reload, m_reload)
        menu_file.AppendSeparator() # adds a separator to the end of the menu

        m_exit = menu_file.Append(-1, "Exit\tCtrl-X", "Exit")
        self.Bind(wx.EVT_MENU, self.on_exit, m_exit)

        self.menu_bar.Append(menu_file, "&File")

        self.SetMenuBar(self.menu_bar)

    def on_reload(self, event):
        # reload config files
        self.config = TaskConfig()

        self.notebook.Unbind(aui.EVT_AUINOTEBOOK_PAGE_CHANGED) # because the page has not been created yet

        # destory pages
        self.delNotebookPage("Calibration")

        # create new pages
        self.page1 = PanelOne(self.notebook, self.screenSize, self.menu_bar, self.config)
        self.notebook.AddPage(self.page1, "Calibration")

        self.notebook.Bind(aui.EVT_AUINOTEBOOK_PAGE_CHANGED, self.PageChanged)

        self.Layout()

    def on_exit(self, event):
        self.Destroy()
    # ---------- Menu Related End ---------- #

    # ---------- Status Bar Start ---------- #
    def create_statusBar(self):
        """
        Create the status bar
        """
        self.statusBar = self.CreateStatusBar()

    def updateStatusBar(self, msg):
        self.statusBar.SetStatusText(msg)
    # ---------- Status Bar End ---------- #

    # ---------- Tab Related Start ---------- #
    def PageChanged(self, event=None):
        if self.notebook.pageChanged_count > 0:
            #print(self.notebook.GetChildren())
            children = self.notebook.GetChildren()
            for child in children:
                if isinstance(child, wx.Panel):
                    child.init_state()

        #print(self.notebook.pageChanged_count)
        self.notebook.pageChanged_count+=1
    # ---------- Tab Related End ---------- #

def create_output_folders(paths):
    for path in paths:
        os.makedirs(path, exist_ok=True)

def main():
    # # create output folder
    # create_output_folders(["output"])

    # init settings
    tool_title = "Coordinate Transfer Tool"
    tool_version = "v1.4.0"
    tool_icon = "icon/logo.png"

    # create application
    app = wx.App()
    app.frame = TaskFrame(title="%s_%s"%(tool_title, tool_version), icon=tool_icon)
    app.frame.Show()
    app.frame.Maximize(True)
    app.MainLoop()

if __name__ == "__main__":
    main()


from Crypto.Cipher import AES
from Crypto.Util.Padding import pad
from hashlib import sha256

xor = lambda a,b : bytes(i^j for i,j in zip(a,b))

def decrypt(ciphertext, key, iv):
    cipher = AES.new(key, AES.MODE_ECB)
    blocks = [ciphertext[i:i+16] for i in range(0, len(ciphertext), 16)]
    pt = b''
    for block in blocks:
        dec_block = xor(cipher.decrypt(block), iv)
        iv = sha256(iv).digest()[:16]
        pt += dec_block
    return pt

ct = open('file.enc', 'rb').read()
pt = decrypt(ct, b'PERFECTLENGTHKEY', b'Oh no you got me')
with open('flag.wav', 'wb') as f:
    f.write(pt)


from collections import namedtuple
import torch as t
import torch.nn as nn
from const import DIM_POSITION_2x2, DIM_POSITION_3x3, DIM_NUMBER_2x2, DIM_NUMBER_3x3, DIM_TYPE, DIM_SIZE, DIM_COLOR
from nvsa.reasoning.vsa_block_utils import binding_circular, block_discrete_codebook
from models import MLP
from VSAConverter import VSAConverter

Scene = namedtuple("Scene", ["position", "number", "type", "size", "color"])

class AttributeLevelReasoner(nn.Module):

    def __init__(self, device, constellation, model, hidden_layers, dictionary, vsa_conversion=False, vsa_selection=False, context_superposition=False):
        super(AttributeLevelReasoner, self).__init__()
        self.device = device
        self.constellation = constellation
        self.compute_attribute_dims()
        self.d = dictionary.shape[1]*dictionary.shape[2]
        self.k = dictionary.shape[1]
        self.vsa_conversion = vsa_conversion
        self.vsa_selection = vsa_selection
        self.context_superposition = context_superposition
        self.vsa_converter = VSAConverter(device, self.constellation, dictionary)
        self.compute_attribute_models(model, hidden_layers)
        if self.context_superposition:
            context_keys, _ = block_discrete_codebook(device=device, d=self.d, k=self.k, scene_dim=8)
            self.context_keys = nn.Parameter(context_keys)
            position_number_keys, _ = block_discrete_codebook(device=device, d=self.d, k=self.k, scene_dim=2)
            self.position_key, self.number_key = nn.Parameter(position_number_keys[0]), nn.Parameter(position_number_keys[1])

    def compute_attribute_dims(self):
        if "distribute" in self.constellation:
            if "four" in self.constellation:
                DIM_POSITION = DIM_POSITION_2x2
                DIM_NUMBER = DIM_NUMBER_2x2
            else:
                DIM_POSITION = DIM_POSITION_3x3
                DIM_NUMBER = DIM_NUMBER_3x3
            self.position_dim = DIM_POSITION
            self.number_dim = DIM_NUMBER
        self.type_dim = DIM_TYPE + 1
        self.size_dim = DIM_SIZE + 1
        self.color_dim = DIM_COLOR + 1

    def compute_attribute_models(self, model, hidden_layers):
        if self.vsa_conversion:
            if "distribute" in self.constellation:
                self.pos_num_model = MLP(2*self.d, self.position_dim, self.d, hidden_layers=hidden_layers, softmax=True)
            self.type_model = MLP(self.d, self.type_dim, self.d, softmax=True)
            self.size_model = MLP(self.d, self.size_dim, self.d, softmax=True)
            self.color_model = MLP(self.d, self.color_dim, self.d, softmax=True)
        else:
            if "distribute" in self.constellation:
                self.pos_num_model = MLP(self.position_dim + self.number_dim, self.position_dim, self.d, hidden_layers=hidden_layers, softmax=True)
            self.type_model = MLP(self.type_dim, self.type_dim, self.d, softmax=True)
            self.size_model = MLP(self.size_dim, self.size_dim, self.d, softmax=True)
            self.color_model = MLP(self.color_dim, self.color_dim, self.d, softmax=True)

    def compute_vsa_outputs(self, outputs):
        if "distribute" in self.constellation:
            position_dist = outputs.position.unsqueeze(-1)
            position_dict = self.vsa_converter.position_dictionary.reshape((self.vsa_converter.position_dictionary.shape[0], -1)).unsqueeze(0)
            position = t.sum(position_dist*position_dict, dim=1)
        else:
            position = None

        type_dist = outputs.type.unsqueeze(-1)
        type_dict = self.vsa_converter.type_dictionary.reshape((self.vsa_converter.type_dictionary.shape[0], -1)).unsqueeze(0)
        type = t.sum(type_dist*type_dict, dim=1)

        size_dist = outputs.size.unsqueeze(-1)
        size_dict = self.vsa_converter.size_dictionary.reshape((self.vsa_converter.size_dictionary.shape[0], -1)).unsqueeze(0)
        size = t.sum(size_dist*size_dict, dim=1)

        color_dist = outputs.color.unsqueeze(-1)
        color_dict = self.vsa_converter.color_dictionary.reshape((self.vsa_converter.color_dictionary.shape[0], -1)).unsqueeze(0)
        color = t.sum(color_dist*color_dict, dim=1)

        return Scene(position, None, type, size, color)

    def compute_context_superposition(self, x):
        if self.context_superposition:
            x_with_superposition = binding_circular(x.reshape((-1, self.k, self.d//self.k)),
                                    self.context_keys.repeat(x.shape[0], 1, 1))
            x_with_superposition = x_with_superposition.reshape((x.shape[0], x.shape[1], -1))
            x_with_superposition = x_with_superposition.sum(dim=1)/x.shape[1]
        else:
            x_with_superposition = x
        return x_with_superposition

    def position_number_superposition(self, position, number):
        position_binded = binding_circular(position.reshape((-1, self.k, self.d//self.k)),
                         self.position_key.unsqueeze(0).repeat(position.shape[0]*position.shape[1], 1, 1))
        number_binded = binding_circular(number.reshape((-1, self.k, self.d//self.k)),
                         self.number_key.unsqueeze(0).repeat(number.shape[0]*number.shape[1], 1, 1))
        pos_num = (position_binded + number_binded)/2
        pos_num = pos_num.reshape((position.shape[0], position.shape[1], -1))
        return pos_num

    def forward(self, scene_prob):
        scene_vsa = self.vsa_converter(scene_prob)
        if "distribute" in self.constellation:
            scene_vsa = Scene(t.flatten(scene_vsa.position, start_dim=len(scene_vsa.position.shape)-2),
                              t.flatten(scene_vsa.number, start_dim=len(scene_vsa.number.shape)-2),
                              t.flatten(scene_vsa.type, start_dim=len(scene_vsa.type.shape)-2),
                              t.flatten(scene_vsa.size, start_dim=len(scene_vsa.size.shape)-2),
                              t.flatten(scene_vsa.color, start_dim=len(scene_vsa.color.shape)-2))
        else:
            scene_vsa = Scene(None,
                              None,
                              t.flatten(scene_vsa.type, start_dim=len(scene_vsa.type.shape)-2),
                              t.flatten(scene_vsa.size, start_dim=len(scene_vsa.size.shape)-2),
                              t.flatten(scene_vsa.color, start_dim=len(scene_vsa.color.shape)-2))

        scene_prob = Scene(scene_prob.position_prob, scene_prob.number_prob, scene_prob.type_prob, scene_prob.size_prob, scene_prob.color_prob)

        if self.vsa_conversion:
            scene = scene_vsa
        else:
            scene = scene_prob

        if "distribute" in self.constellation:
            if self.context_superposition:
                pos_num = self.position_number_superposition(scene.position[:, :8], scene.number[:, :8])
                pos_num_output = self.pos_num_model(self.compute_context_superposition(pos_num))
            else:
                pos_num_output = self.pos_num_model(t.cat((self.compute_context_superposition(scene.position[:, :8]),
                                                        self.compute_context_superposition(scene.number[:, :8])), dim=-1))
        else:
            pos_num_output = None
        type_output = self.type_model(self.compute_context_superposition(scene.type[:, :8]))
        size_output = self.size_model(self.compute_context_superposition(scene.size[:, :8]))
        color_output = self.color_model(self.compute_context_superposition(scene.color[:, :8]))
        outputs = Scene(pos_num_output, None, type_output, size_output, color_output)
        if self.vsa_selection:
            outputs = self.compute_vsa_outputs(outputs)
            scene = scene_vsa
        else:
            scene = scene_prob

        if "distribute" in self.constellation:
            candidates = Scene(scene.position[:, 8:], scene.number[:, 8:], scene.type[:, 8:], scene.size[:, 8:], scene.color[:, 8:])
        else:
            candidates = Scene(None, None, scene.type[:, 8:], scene.size[:, 8:], scene.color[:, 8:])

        return outputs, candidates


"""
Testing aefoilnaca

"""

import aefoilnaca
import matplotlib.pyplot as plt

c = 10.0 # Chord length
t=0.15   # the maximum relative thickness
n=80     # number of point
m=0.02   # the maximum camber
p=0.4    # the location of maximum camber

y = aefoilnaca.naca4_sym(c,t,n)
yy = aefoilnaca.naca4_cam(0.02,0.4,t,c,80)

plt.plot(y[0,:],y[1,:])
plt.plot(yy[0,:],yy[1,:])
plt.show()


from PyQt5.QtCore import QThread
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

from src.db.NNstorage import available_networks, save_new_network
from src.game.GameEngine import GameEngine
from src.gui.WindowUtils import WindowUtils

class MenuTab(WindowUtils):
    """ Defines UI elements for configuration menu """

    def __init__(self, centralWidget, baseStack, ownStack, wrap):
        """ Widgets declaration and connection to methods """

        super(MenuTab, self).__init__(wrap)
        self.baseStack = baseStack  # Allows menu navigation

        # Main title {{
        title = QLabel("Othello")
        title.setFont(QFont('Courier', 18))

        # laying out
        title_layout = QHBoxLayout()
        title_layout.addStretch()
        title_layout.addWidget(title)
        title_layout.addStretch()
        # }}

        # Train ML agent #

        # select file {{
        train_file_lb = QLabel("Filename", centralWidget)
        train_file_lb.setMinimumWidth(100)

        self.train_file_cb = self.init_combobox(list(), centralWidget)

        # layout
        train_file_lt = self.horizontal_menu_widget_layout(train_file_lb, self.train_file_cb)
        # }}

        # move selection {{
        move_selec_lb = QLabel("Move selection", centralWidget)
        move_selec_lb.setMinimumWidth(100)

        self.move_selec_cb = self.init_combobox(["eps-greedy", "softmax exponential"], centralWidget)

        self.move_selec_lt = self.horizontal_menu_widget_layout(move_selec_lb, self.move_selec_cb)
        # }}

        # learning rate {{
        learn_rate_lb = QLabel("Learning rate", centralWidget)
        learn_rate_lb.setMinimumWidth(100)

        self.learn_rate_sb = QDoubleSpinBox(centralWidget)
        self.learn_rate_sb.setMinimumWidth(125)
        self.learn_rate_sb.setRange(0, 1)
        self.learn_rate_sb.setSingleStep(0.05)

        learn_rate_lt = self.horizontal_menu_widget_layout(learn_rate_lb, self.learn_rate_sb)
        # }}

        # random factor (epsilon) {{
        train_eps_lb = QLabel("Random factor", centralWidget)
        train_eps_lb.setMinimumWidth(100)

        self.train_eps_sb = QDoubleSpinBox(centralWidget)
        self.train_eps_sb.setMinimumWidth(125)
        self.train_eps_sb.setRange(0, 1)
        self.train_eps_sb.setSingleStep(0.05)

        train_eps_lt = self.horizontal_menu_widget_layout(train_eps_lb, self.train_eps_sb)
        # }}

        # number of games {{
        nb_trains_lb = QLabel("Number of games", centralWidget)
        nb_trains_lb.setMinimumWidth(100)

        self.nb_trains_sb = QSpinBox(centralWidget)
        self.nb_trains_sb.setMinimumWidth(125)
        self.nb_trains_sb.setRange(1000, 1000000)
        self.nb_trains_sb.setSingleStep(1000)

        nb_trains_lt = self.horizontal_menu_widget_layout(nb_trains_lb, self.nb_trains_sb)
        # }}

        # progress bar {{
        train_prog_lb = QLabel("Training", centralWidget)
        train_prog_lb.setMinimumWidth(100)

        self.train_prog_pb = QProgressBar()
        self.train_prog_pb.setMinimumWidth(125)

        train_prog_lt = self.horizontal_menu_widget_layout(train_prog_lb, self.train_prog_pb)
        # }}

        # launch button {{
        self.launch_train_btn = QPushButton("Train", centralWidget)
        self.launch_train_btn.setMinimumWidth(250)

        # connect
        self.launch_train_btn.clicked.connect(lambda: self.launch_train_compare("train"))

        launch_train_lt = self.horizontal_menu_widget_layout(self.launch_train_btn)
        # }}
        # end Train ML agent #

        # Game against program #

        # player 1 type {{
        player_1_lb = QLabel("Player 1", centralWidget)
        player_1_lb.setMinimumWidth(100)

        self.player1_type_cb = self.init_combobox(["human", "random", "ML agent", "minimax"], centralWidget)

        # layout
        player1_lt = self.horizontal_menu_widget_layout(player_1_lb, self.player1_type_cb)
        # }}

        # player 2 type {{
        player_2_lb = QLabel("Player 2", centralWidget)
        player_2_lb.setMinimumWidth(100)

        self.player2_type_cb = self.init_combobox(["human", "random", "ML agent", "minimax"], centralWidget)

        # layout
        player2_lt = self.horizontal_menu_widget_layout(player_2_lb, self.player2_type_cb)
        # }}

        # pawns style {{
        pawns_style_lb = QLabel("Pawns style", centralWidget)
        pawns_style_lb.setMinimumWidth(100)

        self.pawns_style_cb = self.init_combobox(["classic", "undertale"], centralWidget)

        pawns_style_lt = self.horizontal_menu_widget_layout(pawns_style_lb, self.pawns_style_cb)
        # }}

        # random factor (epsilon) {{
        match_eps_lb = QLabel("Random factor", centralWidget)
        self.match_eps_sb = QDoubleSpinBox(centralWidget)
        self.match_eps_sb.setMinimumWidth(125)
        self.match_eps_sb.setRange(0, 1)
        self.match_eps_sb.setSingleStep(0.05)

        # layout
        match_eps_lt = self.horizontal_menu_widget_layout(match_eps_lb, self.match_eps_sb)
        # }}

        # ML agent subtitle {{
        ml_subtitle = QLabel("ML agent", centralWidget)
        ml_subtitle.setFont(QFont('Courier', 12))

        # laying out
        ml_subtitle_lt = self.horizontal_menu_widget_layout(ml_subtitle)
        # }}

        # ML match file (player 1) {{
        match_file1_lb = QLabel("Filename (agent 1)", centralWidget)
        match_file1_lb.setMinimumWidth(100)

        self.match_file1_cb = self.init_combobox(list(), centralWidget)

        # layout
        match_file1_lt = self.horizontal_menu_widget_layout(match_file1_lb, self.match_file1_cb)
        # }}

        # ML match file (player 2) {{
        match_file2_lb = QLabel("Filename (agent 2)", centralWidget)
        match_file2_lb.setMinimumWidth(100)

        self.match_file2_cb = self.init_combobox(list(), centralWidget)

        # layout
        match_file2_lt = self.horizontal_menu_widget_layout(match_file2_lb, self.match_file2_cb)
        # }}

        # minimax subtitle {{
        mnx_subtitle = QLabel("Minimax agent", centralWidget)
        mnx_subtitle.setFont(QFont('Courier', 12))

        # laying out
        mnx_subtitle_lt = self.horizontal_menu_widget_layout(mnx_subtitle)
        # }}

        # minimax early hits (player 1) {{
        play_ehits1_lb = QLabel("Depth (agent 1)")
        play_ehits1_lb.setMinimumWidth(100)

        self.play_ehits1_sb = QSpinBox(centralWidget)
        self.play_ehits1_sb.setMinimumWidth(125)
        self.play_ehits1_sb.setRange(0, 60)
        self.play_ehits1_sb.setSingleStep(1)

        # layout
        play_ehits1_lt = self.horizontal_menu_widget_layout(play_ehits1_lb, self.play_ehits1_sb)
        # }}

        # minimax early hits (player 2) {{
        play_ehits2_lb = QLabel("Depth (agent 2)")
        play_ehits2_lb.setMinimumWidth(100)

        self.play_ehits2_sb = QSpinBox(centralWidget)
        self.play_ehits2_sb.setMinimumWidth(125)
        self.play_ehits2_sb.setRange(0, 60)
        self.play_ehits2_sb.setSingleStep(1)

        # layout
        play_ehits2_lt = self.horizontal_menu_widget_layout(play_ehits2_lb, self.play_ehits2_sb)
        # }}

        # launch button {{
        self.launch_match_btn = QPushButton("Play", centralWidget)
        self.launch_match_btn.setMinimumWidth(250)

        # connect
        self.launch_match_btn.clicked.connect(lambda: self.launch_match(baseStack, 1))

        launch_match_lt = self.horizontal_menu_widget_layout(self.launch_match_btn)
        # }}

        # End of game against program #

        # New ML agent #

        # filename {{
        new_agent_fname_lb, self.new_agent_fname = self.label_and_input("Filename", centralWidget)

        new_agent_fname_lt = self.horizontal_menu_widget_layout(new_agent_fname_lb, self.new_agent_fname)
        # }}

        # NN size {{
        nn_size_lb = QLabel("NN size")
        nn_size_lb.setMinimumWidth(100)

        self.nn_size_sb = QSpinBox(centralWidget)
        self.nn_size_sb.setMinimumWidth(125)
        self.nn_size_sb.setRange(1, 100)
        self.nn_size_sb.setSingleStep(1)

        # layout
        nn_size_lt = self.horizontal_menu_widget_layout(nn_size_lb, self.nn_size_sb)
        # }}

        # learning strategy {{
        learn_strat_lb = QLabel("Learning strategy", centralWidget)
        learn_strat_lb.setMinimumWidth(100)

        self.learn_strat_cb = self.init_combobox(["Q-learning", "SARSA"], centralWidget)

        learn_strat_lt = self.horizontal_menu_widget_layout(learn_strat_lb, self.learn_strat_cb)
        # }}

        # activation function {{
        act_fun_lb = QLabel("Activation function", centralWidget)
        act_fun_lb.setMinimumWidth(100)

        self.act_fun_cb = self.init_combobox(["sigmoïd", "ReLU", "hyperbolic tangent"], centralWidget)

        act_fun_lt = self.horizontal_menu_widget_layout(act_fun_lb, self.act_fun_cb)
        # }}

        # create button {{
        self.create_agent_btn = QPushButton("Create", centralWidget)
        self.create_agent_btn.setMinimumWidth(250)

        # connect
        self.create_agent_btn.clicked.connect(lambda: self.create_new_agent())

        create_agent_lt = self.horizontal_menu_widget_layout(self.create_agent_btn)
        # }}
        # End new ML agent #

        # Comparison #

        # agent 1 type {{
        agent1_type_lb = QLabel("Agent 1", centralWidget)
        agent1_type_lb.setMinimumWidth(100)

        self.agent1_type_cb = self.init_combobox(["random", "ML agent", "minimax"], centralWidget)

        agent1_type_lt = self.horizontal_menu_widget_layout(agent1_type_lb, self.agent1_type_cb)
        # }}

        # agent 2 type {{
        agent2_type_lb = QLabel("Agent 2", centralWidget)
        agent2_type_lb.setMinimumWidth(100)

        self.agent2_type_cb = self.init_combobox(["random", "ML agent", "minimax"], centralWidget)

        agent2_type_lt = self.horizontal_menu_widget_layout(agent2_type_lb, self.agent2_type_cb)
        # }}

        # number of games {{
        nb_comps_lb = QLabel("Number of games", centralWidget)
        nb_comps_lb.setMinimumWidth(100)

        self.nb_comps_sb = QSpinBox(centralWidget)
        self.nb_comps_sb.setMinimumWidth(125)
        self.nb_comps_sb.setRange(1, 1000000)
        self.nb_comps_sb.setSingleStep(1000)

        nb_comps_lt = self.horizontal_menu_widget_layout(nb_comps_lb, self.nb_comps_sb)
        # }}

        # random factor (epsilon) {{
        cmp_eps_lb = QLabel("Random factor", centralWidget)
        cmp_eps_lb.setMinimumWidth(100)
        self.cmp_eps_sb = QDoubleSpinBox(centralWidget)
        self.cmp_eps_sb.setMinimumWidth(125)
        self.cmp_eps_sb.setRange(0, 1)
        self.cmp_eps_sb.setSingleStep(0.05)

        cmp_eps_lt = self.horizontal_menu_widget_layout(cmp_eps_lb, self.cmp_eps_sb)
        # }}

        # ML agent subtitle {{
        cmp_ml_agent_subtitle = QLabel("ML agent")
        cmp_ml_agent_subtitle.setFont(QFont('Courier', 12))

        # laying out
        cmp_ml_agent_subtitle_lt = self.horizontal_menu_widget_layout(cmp_ml_agent_subtitle)
        # }}

        # agent 1 file (ML) {{
        agent1_file_lb = QLabel("Filename (ML)", centralWidget)
        agent1_file_lb.setMinimumWidth(100)

        self.agent1_file_cb = self.init_combobox(list(), centralWidget)

        # layout
        agent1_file_lt = self.horizontal_menu_widget_layout(agent1_file_lb, self.agent1_file_cb)
        # }}

        # agent 2 file (ML) {{
        agent2_file_lb = QLabel("Filename (ML)", centralWidget)
        agent2_file_lb.setMinimumWidth(100)

        self.agent2_file_cb = self.init_combobox(list(), centralWidget)

        # layout
        agent2_file_lt = self.horizontal_menu_widget_layout(agent2_file_lb, self.agent2_file_cb)
        # }}

        # minimax agent subtitle {{
        cmp_minimax_agent_subtitle = QLabel("Minimax agent")
        cmp_minimax_agent_subtitle.setFont(QFont('Courier', 12))

        # laying out
        cmp_minimax_agent_subtitle_lt = self.horizontal_menu_widget_layout(cmp_minimax_agent_subtitle)
        # }}

        # minimax early hits (agent 1) {{
        cmp_ehits1_lb = QLabel("Depth (agent 1)")
        cmp_ehits1_lb.setMinimumWidth(100)

        self.cmp_ehits1_sb = QSpinBox(centralWidget)
        self.cmp_ehits1_sb.setMinimumWidth(125)
        self.cmp_ehits1_sb.setRange(0, 60)
        self.cmp_ehits1_sb.setSingleStep(1)

        # layout
        cmp_ehits1_lt = self.horizontal_menu_widget_layout(cmp_ehits1_lb, self.cmp_ehits1_sb)
        # }}

        # minimax early hits (agent 2) {{
        cmp_ehits2_lb = QLabel("Depth (agent 2)")
        cmp_ehits2_lb.setMinimumWidth(100)

        self.cmp_ehits2_sb = QSpinBox(centralWidget)
        self.cmp_ehits2_sb.setMinimumWidth(125)
        self.cmp_ehits2_sb.setRange(0, 60)
        self.cmp_ehits2_sb.setSingleStep(1)

        # layout
        cmp_ehits2_lt = self.horizontal_menu_widget_layout(cmp_ehits2_lb, self.cmp_ehits2_sb)
        # }}

        # progress bar {{
        cmp_prog_lb = QLabel("Training", centralWidget)
        cmp_prog_lb.setMinimumWidth(100)

        self.cmp_prog_pb = QProgressBar()
        self.cmp_prog_pb.setMinimumWidth(125)

        cmp_prog_lt = self.horizontal_menu_widget_layout(cmp_prog_lb, self.cmp_prog_pb)
        # }}

        # results {{
        results_w_lb = QLabel("Results:", centralWidget)
        results_w_lb.setMinimumWidth(100)

        self.results_w_dsp = QLabel(str(), centralWidget)
        self.results_w_dsp.setMinimumWidth(100)

        results_b_lb = QLabel(str(), centralWidget)
        results_b_lb.setMinimumWidth(100)

        self.results_b_dsp = QLabel(str(), centralWidget)
        self.results_b_dsp.setMinimumWidth(100)

        results_t_lb = QLabel(str(), centralWidget)
        results_t_lb.setMinimumWidth(100)

        self.results_t_dsp = QLabel(str(), centralWidget)
        self.results_t_dsp.setMinimumWidth(100)

        cmp_results_lt = QVBoxLayout()
        cmp_results_lt.addLayout(self.horizontal_menu_widget_layout(results_b_lb, self.results_b_dsp))
        cmp_results_lt.addLayout(self.horizontal_menu_widget_layout(results_w_lb, self.results_w_dsp))
        cmp_results_lt.addLayout(self.horizontal_menu_widget_layout(results_t_lb, self.results_t_dsp))
        # }}

        # launch comparison button {{
        self.launch_comp_btn = QPushButton("Compare", centralWidget)
        self.launch_comp_btn.setMinimumWidth(250)

        # connect
        self.launch_comp_btn.clicked.connect(lambda: self.launch_train_compare("compare"))

        launch_comp_lt = self.horizontal_menu_widget_layout(self.launch_comp_btn)
        # }}

        # End comparison #

        self.update_comboboxes()

        # Groupboxes
        train_ml_gb = self.groupboxer("Train ML agent", train_file_lt, self.move_selec_lt, learn_rate_lt, train_eps_lt,
                                      nb_trains_lt, train_prog_lt, launch_train_lt)

        match_gb = self.groupboxer("Match", player1_lt, player2_lt, pawns_style_lt, match_eps_lt, ml_subtitle_lt,
                                   match_file1_lt, match_file2_lt, mnx_subtitle_lt, play_ehits1_lt, play_ehits2_lt,
                                   launch_match_lt)

        new_ml_gb = self.groupboxer("New ML agent", new_agent_fname_lt, nn_size_lt, learn_strat_lt, act_fun_lt,
                                    create_agent_lt)

        cmp_gb = self.groupboxer("Compare AIs", agent1_type_lt, agent2_type_lt, nb_comps_lt, cmp_eps_lt,
                                 cmp_ml_agent_subtitle_lt, agent1_file_lt, agent2_file_lt,
                                 cmp_minimax_agent_subtitle_lt, cmp_ehits1_lt, cmp_ehits2_lt, cmp_prog_lt,
                                 cmp_results_lt, launch_comp_lt)

        # finalize (layout the boxes)
        left_col = QVBoxLayout()
        left_col.addWidget(match_gb)

        middle_col = QVBoxLayout()
        middle_col.addStretch()
        middle_col.addLayout(title_layout)
        middle_col.addStretch()
        middle_col.addWidget(train_ml_gb)
        middle_col.addWidget(new_ml_gb)

        right_col = QVBoxLayout()
        right_col.addWidget(cmp_gb)

        mainmenu_layout = QHBoxLayout()
        mainmenu_layout.addLayout(left_col)
        mainmenu_layout.addLayout(middle_col)
        mainmenu_layout.addLayout(right_col)

        ownStack.setLayout(mainmenu_layout)

        # {{ end of __init__ }}

    # Utility functions #
    def fetch_parameters(self, game_mode):
        """ Read parameters from the widgets. All cases (match, train, comapre) are handled independently since
        parameters are provided by different widgets for those cases. """

        game_parameters = dict()

        if game_mode == "match":
            self.wrap.pawns_style = self.pawns_style_cb.currentText()
            game_parameters = {"mode": "match",
                               "player1": {"type": self.player1_type_cb.currentText().lower()},
                               "player2": {"type": self.player2_type_cb.currentText().lower()},
                               "nb_games": None}
            pars = None
            if game_parameters["player1"]["type"] == "ml agent":
                pars = {"network_name": self.match_file1_cb.currentText(), "eps": self.match_eps_sb.value()}
            elif game_parameters["player1"]["type"] == "minimax":
                pars = {"ehits": self.play_ehits1_sb.value(), "eps": self.match_eps_sb.value()}
            game_parameters["player1"]["pars"] = pars

            pars = None
            if game_parameters["player2"]["type"] == "ml agent":
                pars = {"network_name": self.match_file2_cb.currentText(), "eps": self.cmp_eps_sb.value()}
            elif game_parameters["player2"]["type"] == "minimax":
                pars = {"ehits": self.play_ehits2_sb.value(), "eps": self.cmp_eps_sb.value()}
            game_parameters["player2"]["pars"] = pars

        elif game_mode == "train":
            game_parameters = {"mode": "train",
                               "player1": {"type": "ml agent",
                                           "pars": {"network_name": self.train_file_cb.currentText(),
                                                    "eps": self.train_eps_sb.value(), "ls": None,
                                                    "mv_selec": self.move_selec_cb.currentText().split(" ")[0],
                                                    "lr": self.learn_rate_sb.value(), "act_f": None}},
                               "player2": None,
                               "nb_games": self.nb_trains_sb.value()}

        elif game_mode == "compare":
            game_parameters = {"mode": "compare",
                               "player1": {"type": self.agent1_type_cb.currentText().lower()},
                               "player2": {"type": self.agent2_type_cb.currentText().lower()},
                               "nb_games": self.nb_comps_sb.value()}
            pars = None
            if game_parameters["player1"]["type"] == "ml agent":
                pars = {"network_name": self.agent1_file_cb.currentText(), "eps": self.cmp_eps_sb.value()}
            elif game_parameters["player1"]["type"] == "minimax":
                pars = {"ehits": self.cmp_ehits1_sb.value(), "eps": self.cmp_eps_sb.value()}
            game_parameters["player1"]["pars"] = pars

            pars = None
            if game_parameters["player2"]["type"] == "ml agent":
                pars = {"network_name": self.agent2_file_cb.currentText(), "eps": self.cmp_eps_sb.value()}
            elif game_parameters["player2"]["type"] == "minimax":
                pars = {"ehits": self.cmp_ehits2_sb.value(), "eps": self.cmp_eps_sb.value()}
            game_parameters["player2"]["pars"] = pars

        return game_parameters

    def toggle_buttons_activation(self):
        disabling = self.launch_match_btn.isEnabled()
        self.launch_match_btn.setDisabled(disabling)
        self.create_agent_btn.setDisabled(disabling)
        self.launch_comp_btn.setDisabled(disabling)
        self.launch_train_btn.setDisabled(disabling)

    def update_comboboxes(self):
        """ Update contents of comboboxes that may be influenced by UI """
        for cb in [self.match_file1_cb, self.match_file2_cb, self.agent1_file_cb, self.agent2_file_cb, self.train_file_cb]:
            cb.clear()
            for f in available_networks():
                cb.addItem(f)
            if cb.count():
                cb.setCurrentIndex(0)

    # Button triggered operations #

    def create_new_agent(self):
        network_name = self.new_agent_fname.text()
        if network_name in available_networks():
            self.informative_popup("Warning", "Archived ai with this filename already exists.", "dismiss")

        elif network_name == str():
            self.informative_popup("Warning", "Filename must not be empty.", "dismiss")

        else:
            save_new_network(network_name, self.learn_strat_cb.currentText(), self.act_fun_cb.currentText().lower(), 128, self.nn_size_sb.value())
            self.update_comboboxes()
        self.new_agent_fname.setText(str())

    def launch_match(self, stack, menu_index):
        self.wrap.game_parameters = self.fetch_parameters("match")
        stack.setCurrentIndex(menu_index)

    def display_results(self, results):
        """ Display results after comparison session """

        self.results_w_dsp.setText(results[0])
        self.results_b_dsp.setText(results[1])
        self.results_t_dsp.setText(results[2])

    def launch_train_compare(self, mode):
        """ Launch train or compare loop in QThread. Same function is used for both purposes since GameEngine is passed
        parameter indicating if it's dealing with training or comparison. """

        self.toggle_buttons_activation()

        # Initialization of the QThread object
        self.thread = QThread()
        self.worker = GameEngine(self, self.fetch_parameters(mode))
        self.worker.moveToThread(self.thread)

        # Connect signals and slots
        self.thread.started.connect(self.worker.run)
        self.worker.finished.connect(self.thread.quit)
        self.worker.finished.connect(self.worker.deleteLater)
        self.thread.finished.connect(self.thread.deleteLater)
        self.thread.finished.connect(self.toggle_buttons_activation)

        # Other signals & slots
        self.worker.train_progress.connect(self.train_prog_pb.setValue)
        self.worker.compare_progress.connect(self.cmp_prog_pb.setValue)
        self.worker.compare_result.connect(self.display_results)

        # Start the thread
        self.thread.start()


import requests
import json
import datetime as dt
import pandas as pd
import sqlite3
import time

# connect to sql database that stores all the values
conn = sqlite3.connect('./reddit_data.db')

cursor = conn.cursor()

# check to make sure it connects to the database
print(cursor.execute("SELECT * FROM submissions LIMIT 10").fetchall())

# function to create a string type output from a list of keywords we want
def keywords_filter_str(keywords_filter):
    str_kf = ""
    for k in keywords_filter:
        str_kf = str_kf + k + ","
    str_kf = str_kf[:-1]
    return str_kf

# get data

def get_data(conn):

    query = "SELECT * FROM submissions WHERE num_comments < 250 ORDER BY RANDOM() LIMIT 30;"

    df = pd.read_sql_query(query, conn)

    if 'comment_avail' in df.columns:
        pass
    else:
        conn.execute("ALTER TABLE submissions ADD comment_avail INTEGER;")
        conn.commit()

    return df

def get_batchids (df):

    batch = []
    total_comments = 0
    i = 0

    while total_comments < 230:

        if df.iloc[i]['num_comments'] < 230:

            batch.append(df.iloc[i]['id'])
            total_comments += df.iloc[i]['num_comments']

            i += 1

        else:

            i += 1
    total_comments -= int(df[df['id'] == batch[-1]]['num_comments'])
    return batch[:-1]

def extract_comments (comments_endpoint, size, comments_keywords_filter, batch, conn):

    time.sleep(2)

    try:
        comments = requests.get(f"{comments_endpoint}?q=*&fields={keywords_filter_str(comments_keywords_filter)}&sort=asc&link_id={keywords_filter_str(batch)}&size={size}")

        if comments.status_code == 200:

            comments_df = pd.DataFrame(json.loads(comments.text)['data'])
            comments_df = comments_df.set_index('id')
            comments_df.to_sql('comments', conn, if_exists='append') # push the data to sql database

            for id in batch:
                    query = f'''UPDATE submissions SET comment_avail = 1 WHERE id = "{id}"; '''
                    conn.execute(query)
                    conn.commit()

        else:

            error_df = pd.DataFrame([{'code': comments.status_code, 'batch': str(batch)}])
            error_df.to_sql('errors_comments', conn, if_exists='append')

    except:

        error_df = pd.DataFrame([{'code': -999, 'batch': str(batch)}])
        error_df.to_sql('errors_comments', conn, if_exists='append')

def main():

    comments_endpoint = "https://api.pushshift.io/reddit/comment/search/"
    size = 250
    comments_keywords_filter = ['id', 'author', 'body', 'created_utc', 'link_id', 'is_submitter', 'score', 'subreddit', 'subreddit_id']

    for _ in range(90000):

        reddit_df = get_data(conn)
        batch = get_batchids(reddit_df)
        extract_comments(comments_endpoint, size, comments_keywords_filter, batch, conn)

if __name__ == "__main__":
    main()


from .state import State, File
from .requests import FileWatchUpdate

import asyncio
from pathlib import Path

async def watchFileSystem(state: State, config):
    directory = Path(config.folder)
    while True:
        if state.phase == "running":
            computer = state.find_computer(state.name)
            files = set(f for f in directory.rglob("*") if f.is_file())
            current_files = (
                {i.path for i in computer.fileSystem.files.values()}
                if computer.fileSystem
                else set()
            )
            missing_files = files - current_files

            if missing_files:
                yield FileWatchUpdate(
                    {
                        f.relative_to(config.folder): File(
                            f, "ready", set(), set(), f.stat().st_size
                        )
                        for f in missing_files
                    }
                )

        await asyncio.sleep(config.fileWatchInterval)


import os, pygame

class Drawer:
    def __init__(self, main):
        self.main = main

    def init(self):
        self.center = (int(round(self.main.width/2.0)), int(round(self.main.height/2.0)))
        self.radius = int(round(min(self.main.width, self.main.height)/2.0)*0.8)
        self.rect = (self.center[0]-self.radius, self.center[1]-self.radius, 2*self.radius, 2*self.radius)
        self.graphic = self.load_image("graphic.png")
        self.font = pygame.font.Font(None, 12)

    def draw(self, transform):
        self.clear()

        # scale graphic to circle size
        graphic = pygame.transform.smoothscale(self.graphic, (2*self.radius, 2*self.radius))

        if transform['flip_horizontally'] or transform['flip_vertically']:
            graphic = pygame.transform.flip(graphic, transform['flip_horizontally'], transform['flip_vertically'])

        if transform['rotate']:
            graphic = pygame.transform.rotate(graphic, transform['rotate'])

        self.main.screen.blit(graphic, self.rect)

    def clear(self):
        self.main.screen.fill((48, 48, 64))
        pygame.draw.circle(self.main.screen, (128, 128, 152), self.center, self.radius, 0)

    def render_text(self, text, bottom):
        text = self.font.render(text, True, (255, 255, 255))

        if bottom:
            self.main.screen.blit(text, (10, self.main.height-10-text.get_height()))
        else:
            self.main.screen.blit(text, (10, 10))

    def load_image(self, filename):
        image_path = os.path.join(os.path.dirname(__file__), 'images')
        path = os.path.join(image_path, filename)
        return pygame.image.load(path)


from datetime import datetime
import os
import platform
import subprocess
import sys

from gonotego.common import internet
from gonotego.common import status
from gonotego.command_center import note_commands
from gonotego.command_center import registry
from gonotego.settings import settings

register_command = registry.register_command

Status = status.Status

SAY_COMMAND = 'say' if platform.system() == 'Darwin' else 'espeak'

@register_command('whoami')
@register_command('who am i')
def whoami():
  note_taking_system = settings.get('NOTE_TAKING_SYSTEM')
  if note_taking_system == 'email':
    user = settings.get('EMAIL')
  elif note_taking_system == 'ideaflow':
    user = settings.get('IDEAFLOW_USER')
  elif note_taking_system == 'remnote':
    user = settings.get('REMNOTE_USER_ID')[:6]
  elif note_taking_system == 'roam':
    user = f'{settings.get("ROAM_GRAPH")} {settings.get("ROAM_USER")}'
  elif note_taking_system == 'mem':
    user = settings.get('MEM_API_KEY')[:6]
  elif note_taking_system == 'notion':
    user = settings.get('NOTION_DATABASE_ID')[:6]
  elif note_taking_system == 'twitter':
    user = settings.get('twitter.screen_name')
  say(f'uploader {note_taking_system} ; user {user}')

@register_command('t')
@register_command('time')
def time():
  shell(f'date "+%A, %B%e %l:%M%p" | {SAY_COMMAND} &')

@register_command('at {}:{}', requirements=('scheduler',))
def schedule(at, what, scheduler):
  scheduler.schedule(at, what)

@register_command('status')
@register_command('ok')
def status_command():
  say('ok')

@register_command('say {}')
def say(text):
  dt = datetime.now().strftime('%k:%M:%S')
  with open('tmp-say', 'w') as tmp:
    print(f'[{dt}] Writing "{text}" to tmp-say')
    tmp.write(text)
  cmd = f'cat tmp-say | {SAY_COMMAND} &'
  shell(cmd)

@register_command('silence')
@register_command('silencio')
def silence():
  shell(f'pkill {SAY_COMMAND}')

@register_command('shell {}')
def shell(cmd):
  dt = datetime.now().strftime('%k:%M:%S')
  print(f"[{dt}] Executing command: '{cmd}'")
  os.system(cmd)

@register_command('flush')
def flush():
  sys.stdout.flush()
  sys.stderr.flush()

@register_command('update')
def update():
  shell('git pull')

@register_command('restart')
def restart():
  shell('./env/bin/supervisorctl -u go -p notego restart all')

@register_command('reboot')
def reboot():
  shell('sudo reboot')

@register_command('env')
def env():
  shell('env | sort')

@register_command('ip')
def ip_address():
  hostname_output = subprocess.check_output(['hostname', '-I']).decode('utf-8').strip()
  hostname = hostname_output.split(' ')[0]
  say(hostname)
  note_commands.add_note(hostname)

@register_command('i')
@register_command('internet')
def check_internet():
  say('yes' if internet.is_internet_available() else 'no')

@register_command('wifi {} {}')
@register_command('wpa {} {}')
def add_wpa_wifi(ssid, psk):
  if '"' in ssid or '"' in psk:
    say('WiFi not set.')
    return
  network_string = f"""
network={{
        ssid="{ssid}"
        psk="{psk}"
        key_mgmt=WPA-PSK
}}
"""
  filepath = '/etc/wpa_supplicant/wpa_supplicant.conf'
  shell(f"echo '{network_string}' | sudo tee -a {filepath}")
  reconfigure_wifi()

@register_command('wifi {}')
def add_wifi_no_psk(ssid):
  if '"' in ssid:
    say('WiFi not set.')
    return
  network_string = f"""
network={{
        ssid="{ssid}"
        key_mgmt=NONE
}}
"""
  filepath = '/etc/wpa_supplicant/wpa_supplicant.conf'
  shell(f"echo '{network_string}' | sudo tee -a {filepath}")
  reconfigure_wifi()

@register_command('reconnect')
@register_command('wifi refresh')
@register_command('wifi reconfigure')
def reconfigure_wifi():
  shell('wpa_cli -i wlan0 reconfigure')


from flask import Flask, render_template
from database import Database

app = Flask(__name__)
db = Database()

# fetch all the listings from the db
db.get_all_search_listings()

@app.route('/')
def home():
    db.get_all_search_listings()
    return render_template("index.html")

@app.route('/listings/<string:city>')
def show_listing(city):
    listings = db.get_all_search_listings_by_city(city)
    # for listing in data:
    #     if listing.city == city:
    #         print(f"address: {listing.address} price: {listing.price} url {listing.url} date: {listing.date}"
    #                f"city: {listing.city}")
    return render_template("rentals.html", listings=listings)

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


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

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('pybb', '0001_initial'),
    ]

    operations = [
        migrations.AddField(
            model_name='post',
            name='hidden',
            field=models.BooleanField(default=False, verbose_name='Hidden'),
        ),
    ]


import os
import math
import random
import numpy as np
import torch
import cv2
from torchvision.utils import make_grid
from datetime import datetime

import matplotlib.pyplot as plt
import os

def gen_masked_data_less(x,type='s',ratio=0.1):
	bs,ch,W,H = x.shape
	zero = 1e-6

	if type=='s': # spatial
		# mask1 = torch.ones(W,H)
		for i in range(int(W*H*ratio)):
			indx = np.random.randint(0,W)
			indy = np.random.randint(0,H)
			x[:,9:12,indx,indy] = zero
		# masked_x = torch.mul(mask1,x)
		return x
	elif type=='t': # temperol
		x[:,9:12,:,:] = zero
		return x
		# n_frame = ch//3*ratio
		# if n_frame<1:
		# 	n_frame=1
		# for i in range(int(n_frame)):
		# 	indc = np.random.randint(0,ch//3)
		# 	x[:,3*indc:3*indc+3,:,:] = zero

	elif type=='b': # block
		# mask2 = torch.ones(ch,W,H)
		for i in range(W*H//10):
			indx = np.random.randint(0,W)
			indy = np.random.randint(0,H)
			# indz = np.random.randint(9,12)
			indz = np.random.randint(0,8)
			x[indz,:,indx,indy] = zero
		# masked_x = torch.mul(mask1,x)
		return x
def gen_masked_data(x,type='s',ratio=0.1):
	bs,ch,W,H = x.shape
	zero = 1e-6

	if type=='s': # spatial
		# mask1 = torch.ones(W,H)
		for i in range(int(W*H*ratio)):
			indx = np.random.randint(0,W)
			indy = np.random.randint(0,H)
			x[:,9:12,indx,indy] = zero
		# masked_x = torch.mul(mask1,x)
		return x
	elif type=='t': # temperol
		x[:,9:12,:,:] = zero
		return x
		# n_frame = ch//3*ratio
		# if n_frame<1:
		# 	n_frame=1
		# for i in range(int(n_frame)):
		# 	indc = np.random.randint(0,ch//3)
		# 	x[:,3*indc:3*indc+3,:,:] = zero

	elif type=='b': # block
		# mask2 = torch.ones(ch,W,H)
		for i in range(W*H//10):
			indx = np.random.randint(0,W)
			indy = np.random.randint(0,H)
			indz = np.random.randint(9,12)
			x[:,indz,indx,indy] = zero
		# masked_x = torch.mul(mask1,x)
		return x

def mask_sequence(x,type_list=['s'],ratio=0.1):
    masked_seq = ()
    for i in type_list:
        masked = gen_masked_data(x,i)
        masked_seq = masked_seq+(masked,)
    return masked_seq

bayer = [[0,0], [0,1], [1,0], [1,1]]

def np2tch_cuda(a):
    return torch.from_numpy(a).cuda()
def cuda2np(a):
    return a.cpu().detach().numpy()

def masks_CFA_Bayer_cuda(shape):
    pattern = 'RGGB'
    channels = dict((channel, torch.zeros(shape)) for channel in 'RGB')
    for channel, (y, x) in zip(pattern, [(0, 0), (0, 1), (1, 0), (1, 1)]):
        channels[channel][y::2, x::2] = 1

    return tuple(channels[c].bool() for c in 'RGB')

def masks_CFA_Bayer_tensor(shape, pattern='RGGB'):
    pattern = pattern.upper()
    channels = dict((channel, torch.zeros(shape)) for channel in 'RGB')
    for channel, (y, x) in zip(pattern, [(0, 0), (0, 1), (1, 0), (1, 1)]):
        channels[channel][y::2, x::2] = 1

    return tuple(channels[c].bool().cuda() for c in 'RGB')

def gen_bayer_mask(R_m, G_m, B_m):
    # torch.unsqueeze
    mask = torch.cat([R_m.unsqueeze(2), G_m.unsqueeze(2), B_m.unsqueeze(2)],dim=2)
    return mask

def gen_bayer_img(RGB,output_ch=1):# M N 3 B
    mask = gen_bayer_mask(masks_CFA_Bayer_tensor(RGB.shape[0],RGB.shape[1]))
    torch.repeat_interleave(mask.unsqueeze(3),RGB.shape[2],dim=3)
    bayer_img = RGB*mask
    bayer_img = torch.sum(bayer_img,dim=3).squeeze(3)
    if output_ch==1:
        return bayer_img # M N B
    elif output_ch==4:
        return oneCh2FourCh(bayer_img) # M/2 N/2 B 4

def fourCh2OneCh(RGGB):
    if len(RGGB.shape)==3:
        M ,N = RGGB.shape[0],RGGB.shape[1]
        oneCh = torch.zeros(M*2,N*2).cuda()
        for ib in range(len(bayer)):
            b = bayer[ib]
            oneCh[b[0]::2, b[1]::2]=RGGB[...,ib]
    elif len(RGGB.shape)==4:
        M ,N,B = RGGB.shape[0],RGGB.shape[1],RGGB.shape[2]
        oneCh = torch.zeros(M*2,N*2,B).cuda()
        for ib in range(len(bayer)):
            b = bayer[ib]
            oneCh[b[0]::2, b[1]::2]=RGGB[...,ib]
    return oneCh

def oneCh2FourCh(oneCh):
    M ,N,B = oneCh.shape[0],oneCh.shape[1],oneCh.shape[2]
    RGGB = torch.zeros(M//2,N//2,B,4).cuda()
    for ib in range(len(bayer)):
        b = bayer[ib]
        RGGB[...,ib] = oneCh[b[0]::2,b[1]::2]
    return RGGB

def oneCh2ThreeCh(oneCh):
    M ,N,B = oneCh.shape[0],oneCh.shape[1],oneCh.shape[2]
    RGB = torch.zeros(M,N,3,B).cuda()

    RGB[0::2,0::2,0,:] = oneCh[0::2,0::2,:]
    RGB[0::2,1::2,1,:] = oneCh[0::2,1::2,:]
    RGB[1::2,0::2,1,:] = oneCh[1::2,0::2,:]
    RGB[1::2,1::2,2,:] = oneCh[1::2,1::2,:]
    return RGB
def fourCh2ThreeCh(RGGB):
    M ,N,B = RGGB.shape[0]*2,RGGB.shape[1]*2,RGGB.shape[2]
    RGB = torch.zeros(M,N,3,B).cuda()

    RGB[0::2,0::2,0,:] = RGGB[:,:,:,0]
    RGB[0::2,1::2,1,:] = RGGB[:,:,:,1]
    RGB[1::2,0::2,1,:] = RGGB[:,:,:,2]
    RGB[1::2,1::2,2,:] = RGGB[:,:,:,3]

    return RGB

def add_gasuss_noise_meas(meas, std=0.01):
    mean=0
    noise = np.random.normal(mean, std, meas.shape)

    out = meas + noise

    # out = np.clip(out, 0.0, 1.0)

    return out

def add_gaussian_noise_meas_cuda(meas, std=0.01):
    meas = meas.detach().cpu().numpy()
    mean=0
    noise = np.random.normal(mean, std, meas.shape)

    out = meas + noise

    # out = np.clip(out, 0.0, 1.0)

    return torch.from_numpy(out).cuda()

def mosaic_CFA_Bayer_cuda(RGB):
    R_m, G_m, B_m = masks_CFA_Bayer_cuda(RGB.shape[0:2])
    mask = torch.cat((torch.unsqueeze(R_m,dim=-1), torch.unsqueeze(G_m,dim=-1), torch.unsqueeze(B_m,dim=-1)), dim=-1)
    # mask = tstack((R_m, G_m, B_m))
    mosaic = torch.multiply(mask.cuda(), RGB)  # mask*RGB
    CFA = mosaic.sum(2)

    CFA4 = torch.zeros((RGB.shape[0] // 2, RGB.shape[1] // 2, 4))
    CFA4[:, :, 0] = CFA[0::2, 0::2]
    CFA4[:, :, 1] = CFA[0::2, 1::2]
    CFA4[:, :, 2] = CFA[1::2, 0::2]
    CFA4[:, :, 3] = CFA[1::2, 1::2]

    return CFA, CFA4, mosaic, mask


"""
Command-line script for generating the demo table for README files.
"""

import seqsim
import tabulate

def main():
    demo1 = ["kitten", "sitting"]
    demo2 = [(1, 2, 3, 4), (3, 4, 2, 1)]

    # Collect results for all methods
    ret = []
    for method in sorted(seqsim.METHODS):
        dist1 = seqsim.distance(demo1, method=method)
        dist2 = seqsim.distance(demo2, method=method)
        ret.append([method, dist1, dist2])

        dist1_norm = seqsim.distance(demo1, method=method, normal=True)
        dist2_norm = seqsim.distance(demo2, method=method, normal=True)
        ret.append([method + "_normal", dist1_norm, dist2_norm])

    print(
        tabulate.tabulate(
            ret,
            headers=["Method", '"kitten" / "sitting"', "(1, 2, 3, 4) / (3, 4, 2, 1)"],
            tablefmt="github",
        )
    )

if __name__ == "__main__":
    main()


#courseapp ana uygulama
#courses , pages diğer uygulamalar
from django.contrib import admin
from django.urls import include, path

urlpatterns = [

    path('', include('pages.urls')),
    path('kurslar/' , include('courses.urls')),
    path('admin/', admin.site.urls),
]


"""Celery tasks for running MATL programs."""
from __future__ import annotations

import logging
import pathlib
import tempfile
from functools import cached_property
from logging import LogRecord, StreamHandler
from typing import Any, Dict, List, Optional

from celery import Task
from celery.exceptions import SoftTimeLimitExceeded
from celery.signals import task_failure, worker_process_init
from flask_socketio import SocketIO  # type: ignore

from matl_online.extensions import celery, rollbar
from matl_online.matl.core import matl
from matl_online.matl.io import parse_matl_results
from matl_online.octave import OctaveSession
from matl_online.settings import config
from matl_online.types import MATLTaskParameters

octave = None

socket = SocketIO(message_queue=config.SOCKETIO_MESSAGE_QUEUE)

Task.__class_getitem__ = classmethod(lambda cls, *args, **kwargs: cls)  # type: ignore[attr-defined]

class OutputHandler(StreamHandler):  # type: ignore
    """Custom handler for converting logged data to socket events."""

    contents: List[str]
    task: "OctaveTask"

    def __init__(
        self,
        task: "OctaveTask",
        *args: Any,
        **kwargs: Any,
    ) -> None:
        """Initialize the handler with the task we are handling."""
        StreamHandler.__init__(self, *args, **kwargs)
        self.task = task
        self.contents = []

    def clear(self) -> None:
        """Clear all messages that have been logged so far."""
        self.contents = []

    def messages(self) -> str:
        """Concatenate all messages into a long stream."""
        return "\n".join([x for x in self.contents])

    def send(self) -> Dict[str, Any]:
        """Send a message out to the specified rooms."""
        output = parse_matl_results(self.messages())
        result = {"data": output, "session": self.task.session_id}
        socket.emit("status", result, room=self.task.session_id)
        return result

    def emit(self, record: LogRecord) -> None:
        """Overloaded emit method to receive LogRecord instances."""
        # Look to see if there are any special commands in here. These
        # commands will clear the output:
        #
        #   1. [PAUSE]  Send everything that we have so far
        #   2. [CLC]    Send an empty message and clear contents
        #   3. [IMAGE]  FUTURE ENCODING TO BASE64

        if record.levelno == logging.INFO:
            self.process_message(record.msg)

    def process_message(self, message: str) -> None:
        """Append a message to be sent back to the user."""
        print(message)

        if message == "[PAUSE]":
            # For now, we send the entire message again. Consider a better
            # approach (i.e. adding a field to the result that says to
            # flush prior to display)
            self.send()
            return

        if message == "[CLC]":
            self.send()
            self.clear()
            return

        if message.startswith("warning:"):
            return

        if message.startswith("MATL run-time error:"):
            for item in message.split("\n"):
                self.contents.append("[STDERR]" + item)

            return

        self.contents.append(message)

class OctaveTask(Task[[MATLTaskParameters], None]):
    """Custom Task type for interacting with octave."""

    abstract: bool = True
    session_id: Optional[str] = None

    throws = (SoftTimeLimitExceeded,)

    @property
    def octave(self) -> Optional[OctaveSession]:
        """Dependent property which automatically spawns octave if needed."""
        global octave
        return octave

    @cached_property
    def handler(self) -> OutputHandler:
        """Dependent property that creates an OutputHandler instance."""
        return OutputHandler(self)

    def emit(self, *args: Any, **kwargs: Any) -> None:
        """Send an event to any listening clients."""
        socket.emit(*args, room=self.session_id, **kwargs)

    def on_term(self) -> None:
        """Clean up after termination event."""
        # Restart octave, so we're ready to go with future calls
        if self.octave:
            self.octave.restart()

    def on_success(self, *args: Any, **kwargs: Any) -> None:
        """Send a completion messages upon successful completion."""
        self.emit("complete", {"success": True, "message": ""})

    def on_kill(self) -> None:
        """Clean up after a task is killed.

        This is a hard kill by celery so this worker will be destroyed. No
        need to restart octave
        """
        if self.octave:
            self.octave.terminate()

        self.on_failure()

    def send_results(self) -> Dict[str, Any]:
        """Local forwarder for all send events."""
        return self.handler.send()

    def on_failure(self, *args: Any, **kwargs: Any) -> None:
        """Send a message if the task failed for any reason."""
        self.send_results()
        self.emit("complete", {"success": False})

@celery.task(base=OctaveTask, bind=True)
def matl_task(
    task: OctaveTask,
    params: MATLTaskParameters,
) -> Dict[str, Any]:
    """Celery task for processing a MATL command and returning the result."""
    task.session_id = params.session_id
    task.handler.clear()

    assert task.octave, "Octave is not configured properly"

    with tempfile.TemporaryDirectory() as folder:
        try:
            matl(
                task.octave,
                params,
                directory=pathlib.Path(folder),
                line_handler=task.handler.process_message,
            )

            result = task.send_results()

        # In the case of an interrupt (either through a time limit or a
        # revoke() event, we will still clean things up
        except (KeyboardInterrupt, SystemExit):
            task.handler.process_message("[STDERR]Job cancelled")
            task.on_kill()
            raise
        except SoftTimeLimitExceeded:
            # Propagate the term event up the chain to actually kill the worker
            task.handler.process_message("[STDERR]Operation timed out")
            task.on_term()
            raise
        except Exception:
            task.handler.process_message("[STDERR]Unknown error")
            task.on_term()
            raise

    return result

def _initialize_process(**kwargs: Any) -> None:
    """Initialize the octave instance.

    Function to be called when a worker process is spawned. We use this to
    opportunity to actually launch octave and execute a quick MATL program
    """

    # Create a global reference to octave that is unique to this worker process
    global octave

    octave = OctaveSession(
        octaverc=config.OCTAVERC,
        default_paths=[
            config.MATL_WRAP_DIR,
        ],
        logger=logging.Logger(__name__),
    )

@task_failure.connect
def handle_task_failure(**kwargs: Any) -> None:
    rollbar.report_exc_info(extra_data=kwargs)

# When a worker process is spawned, initialize octave
worker_process_init.connect(_initialize_process)


from django import forms
from .models import *
from django.contrib.auth import get_user_model
#from blog.models import User

User = get_user_model()

class ContactForm(forms.ModelForm):

    class Meta():
        model=Contact
        fields= '__all__'

        widgets={
            'Name': forms.TextInput(attrs={'placeholder':'Enter your name'}),
            'Email': forms.EmailInput(attrs={'placeholder':'Enter your email'}),
            'Message':forms.TextInput(attrs={'placeholder':'Enter you message to be sent'}),
            'ContactNo':forms.NumberInput(attrs={'placeholder':'Enter your contact no'})
        }

from django.contrib.auth.forms import UserCreationForm, UserChangeForm
class UserForm(UserCreationForm):
    class Meta:
        model = User
        fields = ('email', 'username','full_name', 'password1', 'password2')

class CustomUserChangeForm(UserChangeForm):
    class Meta:
        model = get_user_model()
        fields = ('full_name', 'email', 'username',)

#user edit form
class UserEditForm(forms.ModelForm):
    class Meta:
        model = User
        fields = (
            'picture_url',
            'full_name',
            'username',
            'email',
            'status',
            'college',
            'country',
            'website',
            'phone_number',
            'gender',

            )
        labels = {

            'phone_number': 'Phone'
        }

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

        for field in self.fields:
            self.fields[field].widget.attrs.update({'class': 'form-control form-control-sm'})


from django.contrib.auth.decorators import login_required
from django.forms import ValidationError
from django.http import JsonResponse, HttpResponse
from django.shortcuts import redirect, render, get_object_or_404
from country_list import countries_for_language
import json
from item.models import Offer, SellerRating
from transaction.forms.checkout_form import PaymentForm
from notifications.interfaces import NotificationInterface

def has_accepted_offers(request) -> bool:
    return request.user.offer_set.filter(status='Accepted').exists()

@login_required
def index(request):
    if hasattr(request.user, 'person'):
        accepted_offers = request.user.offer_set.filter(status='Accepted')
        return render(
            request,
            'checkout/index.html',
            {'basket': accepted_offers}
        )
    return redirect("/profile/create-person")

@login_required
def contact_information(request):
    if hasattr(request.user, 'person'):
        # NOTE: User has to have some accepted offers to access this page
        if has_accepted_offers(request):
            countries = dict(countries_for_language('en'))
            return render(
                request,
                'checkout/contact_information.html',
                {'countries': countries.values()}
            )
        return render(request, '403_site.html')
    return redirect("/profile/create-person")

@login_required
def payment_information(request):
    if hasattr(request.user, 'person'):
        if has_accepted_offers(request):
            form = PaymentForm()
            return render(
                request, 'checkout/payment_information.html', {'form': form})
        return render(request, '403_site.html')
    return redirect("/profile/create-person")

@login_required
def review(request):
    if hasattr(request.user, 'person'):
        if has_accepted_offers(request):
            accepted_offers = request.user.offer_set.filter(status='Accepted')
            total = 0
            for item in accepted_offers:
                total += item.amount
            if request.method == 'POST':
                json_content = json.loads(request.body)
                if 'offerId' in json_content.keys():
                    try:
                        offerId = json_content['offerId']
                        offer = get_object_or_404(Offer, pk=offerId)
                        offer.status = 'Confirmed'
                        offer.full_clean()
                        offer.save()
                        item = offer.item
                        item.status = 'Sold'
                        item.full_clean()
                        item.save()
                        NotificationInterface().payment_received(
                            offer.buyer, item.seller.user, offer.amount, item)
                        return JsonResponse(
                            status=200, data={"message": "Item Sold"})
                    except ValidationError as error:
                        return JsonResponse(
                            status=400, data={"message": str(error)})
                else:
                    return JsonResponse(
                        status=400,
                        data={"message": "OfferId must be supplied"}
                    )
            return render(
                request,
                'checkout/review.html',
                {'basket': accepted_offers, 'total': total}
            )
        return render(request, '403_site.html')
    return redirect("/profile/create-person")

@login_required
def thank_you(request):
    if hasattr(request.user, 'person'):
        if request.method == 'POST':
            json_content = json.loads(request.body)
            if 'offerId' in json_content.keys():
                try:
                    offerId = json_content['offerId']
                    offer = get_object_or_404(Offer, pk=offerId)
                    seller = offer.item.seller
                    buyer = offer.buyer
                    SellerRating.objects.create(rating=json_content['rating'],
                                                seller=seller, buyer=buyer)
                    allratings = SellerRating.objects.filter(
                        seller=seller.id)
                    sum = 0
                    count = 0
                    for rating in allratings:
                        sum += rating.rating
                        count += 1
                    seller.rating = sum / count
                    seller.full_clean()
                    seller.save()
                    return JsonResponse(
                        status=200, data={"message": "Rating updated"})
                except ValidationError as error:
                    return JsonResponse(
                        status=400, data={"message": str(error)})
            else:
                return JsonResponse(
                    status=400, data={"message": "OfferId must be supplied"})
        return render(request, 'checkout/thank_you.html')
    return redirect("/profile/create-person")


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

from app import app
from flask import g, request, jsonify
import logging
from app.config.Config import Config as config_app
logger = logging.getLogger(config_app().data['import_name'])
from app.models.DefaultModel import Default as model_default

@app.before_request
def before_request():
    try:
        secret = request.path.split('/')[2]
    except:
        return stop(412, 'The parameter secret is empty or missing')
    model = model_default.get_by_secret(secret)
    if model is not None:
        if config_app().data['limit'].get(model.limit, None) is None:
            return stop(501, 'The requested functionality is not supported')
    if model is not None:
        counter = model_default.get_usage(model)
        if counter >= config_app().data['limit'][model.limit]:
            return stop(429, 'The usage limit has been reached')
    else:
        return stop(401, 'The secret used is not authorized')

@app.after_request
def after_request(f):
    if not hasattr(g, 'after_request_callbacks'):
        g.after_request_callbacks = []
    g.after_request_callbacks.append(f)
    try:
        secret = request.path.split('/')[2]
        model = model_default.get_by_secret(secret)
        model_default.update_usage(model)
    except:
        pass
    return f

def stop(status, msg, logger=True):
    response = {
        'status': status,
        'message': msg
    }
    if logger:
        log(response)
    response = jsonify(response)
    response.status_code = status
    return response

def err(status, msg=None, logger=False):
    response = {
        'status': status
    }
    if msg is not None:
        response['message'] = msg
    if logger:
        log(response)
    return response

def log(response):
    import time, traceback
    data = {
        'time': time.strftime('%m%d%y'),
        'status': response['status'],
        'remote_addr': request.remote_addr,
        'method': request.method,
        'full_path': request.full_path,
        'message': response['message']
    }
    try:
        data['traceback'] = traceback.format_exc()
    except:
        pass
    logger.error(data)


#!/usr/bin/env python
from setuptools import find_packages, setup

project = "microcosm-fastapi"
version = "0.1.11"

setup(
    name=project,
    version=version,
    description="Opinionated microservice API with FastAPI",
    long_description=open("README.md").read(),
    long_description_content_type="text/markdown",
    author="Globality Engineering",
    author_email="engineering@globality.com",
    url="https://github.com/globality-corp//microcosm-fastapi",
    packages=find_packages(exclude=["*.tests", "*.tests.*", "tests.*", "tests"]),
    include_package_data=True,
    zip_safe=False,
    python_requires=">=3.6",
    keywords="microcosm",
    install_requires=[
        "microcosm>=3.0.0",
        "fastapi",
        "uvicorn",
        "aiofiles",
        "SQLAlchemy[asyncio]>=1.4.0",
        "httpx",
        "h11<0.13", # @pierce 01-24-2022 pin because of httpx conflict
        "click",
        "jinja2",
        "sqlalchemy-utils",
        # @piercefreeman 02/16/2021 - required until we refactor async code
        # into microcosm-postgres and microcosm-pubsub
        "microcosm-pubsub",
        "microcosm-postgres[encryption]>=2.0.0",
        "asyncpg",
        "psycopg2-binary>=2.7.5",
        "makefun",
    ],
    setup_requires=[
        "nose>=1.3.7",
    ],
    dependency_links=[
    ],
    entry_points={
        "microcosm.factories": [
            "app = microcosm_fastapi.factories.fastapi:configure_fastapi",
            "postgres_async = microcosm_fastapi.database.postgres:configure_postgres",
            "session_maker_async = microcosm_fastapi.database.session:configure_session_maker",
            "sqs_message_dispatcher_async = microcosm_fastapi.pubsub.dispatcher:SQSMessageDispatcherAsync",
            # Conventions
            "documentation_convention = microcosm_fastapi.factories.docs:configure_docs",
            "build_info_convention = microcosm_fastapi.conventions.build_info.route:configure_build_info",
            "health_convention = microcosm_fastapi.conventions.health.route:configure_health",
            "config_convention = microcosm_fastapi.conventions.config.route:configure_config",
            "landing_convention = microcosm_fastapi.conventions.landing.route:configure_landing",
            "audit_middleware = microcosm_fastapi.audit:configure_audit_middleware",
            "request_context = microcosm_fastapi.context:configure_request_context",
            "global_exception_handler = microcosm_fastapi.exception_handler:configure_global_exception_handler",
            "logging_data_map = microcosm_fastapi.logging_data_map:configure_logging_data_map",
            "session_injection = microcosm_fastapi.session:configure_session_injection",
            "route_metrics = microcosm_fastapi.metrics:configure_route_metrics"
        ],
    },
    extras_require={
        "metrics": "microcosm-metrics>=2.5.1",
        "test": [
            "coverage>=3.7.1",
            "PyHamcrest>=1.9.0",
            "pytest",
            "pytest-cov",
            "pytest-asyncio",
            "microcosm-metrics"
        ],
        "typehinting": [
            "types-pkg-resources",
            "types-requests",
            "types-setuptools"
        ],
        "lint": [
            "flake8",
            "flake8-print",
            "flake8-logging-format",
            "flake8-isort",
        ]
    }
)