import pandas as pd
import category_encoders as ce
from sklearn.preprocessing import LabelEncoder

def sk_label_encoder_2df(train_df, test_df, cols):
    # 0~割り振られる
    tmp_df = pd.concat([train_df, test_df], axis=0, sort=False).reset_index(drop=True)
    le = LabelEncoder().fit(tmp_df[cols])
    return pd.DataFrame(le.transform(train_df[cols]), columns=[cols]), pd.DataFrame(le.transform(test_df[cols]),
                                                                                columns=[cols])

def sk_label_encoder_1df(df, cols):
    """カテゴリ変換
    sklearnのLabelEncoderでEncodingを行う

    Args:
        df: カテゴリ変換する対象のデータフレーム
        cols (list of str): カテゴリ変換する対象のカラムリスト

    Returns:
        pd.Dataframe: dfにカテゴリ変換したカラムを追加したデータフレーム
    """
    # 0~割り振られる
    for col in cols:
        le = LabelEncoder()
        not_null = df[col][df[col].notnull()]  # nullのデータは変換対象外
        df[col] = pd.Series(le.fit_transform(not_null), index=not_null.index)
        # df[col + '_sk_lbl'] = pd.Series(le.fit_transform(not_null), index=not_null.index)
    return df

def target_encoder_mean(train_df, test_df, cols, target):
    target_mean = train_df.groupby(cols)[target].mean()
    return pd.DataFrame(train_df[cols].map(target_mean)), pd.DataFrame(test_df[cols].map(target_mean))

def target_encoder_std(train_df, test_df, cols, target):
    target_mean = train_df.groupby(cols)[target].std()
    return pd.DataFrame(train_df[cols].map(target_mean)), pd.DataFrame(test_df[cols].map(target_mean))

def ce_ordinal_encoder_tt(train_df, test_df, cols):
    # 1~割り振られる
    tmp_df = pd.concat([train_df, test_df], axis=0, sort=False).reset_index(drop=True)
    ce_oe = ce.OrdinalEncoder(cols=cols, handle_unknown='impute')
    tmp_df = ce_oe.fit_transform(tmp_df[cols])
    tmp_train = tmp_df.iloc[:len(train_df), :]
    tmp_test = tmp_df.iloc[len(train_df):, :]
    return tmp_train, tmp_test

def ce_ordinal_encoder(df, cols):
    # 1~割り振られる
    tmp_df = df.copy()
    ce_oe = ce.OrdinalEncoder(cols=cols, handle_unknown='impute')
    tmp_df[cols] = ce_oe.fit_transform(tmp_df[cols])
    return tmp_df

def ce_one_hot_encoder(train_df, test_df, cols):
    tmp_df = pd.concat([train_df, test_df], axis=0, sort=False).reset_index(drop=True)
    ce_ohe = ce.OneHotEncoder(cols=cols, handle_unknown='impute')
    tmp_df = ce_ohe.fit_transform(tmp_df[cols])
    tmp_train = tmp_df.iloc[:len(train_df), :]
    tmp_test = tmp_df.iloc[len(train_df):, :]
    return tmp_train, tmp_test

def ce_binary_encoder(train_df, test_df, cols):
    tmp_df = pd.concat([train_df, test_df], axis=0, sort=False).reset_index(drop=True)
    ce_bine = ce.BinaryEncoder(cols=cols, handle_unknown='impute')
    tmp_df = ce_bine.fit_transform(tmp_df[cols])
    tmp_train = tmp_df.iloc[:len(train_df), :]
    tmp_test = tmp_df.iloc[len(train_df):, :]
    return tmp_train, tmp_test

def ce_leave_one_out_encoder_v1(train_df, test_df, cols, target):
    # こちらは正真正銘looエンコードする
    ce_loo = ce.LeaveOneOutEncoder(cols=cols)
    tmp_train = ce_loo.fit_transform(X=train_df[cols], y=train_df[target])
    if test_df is not None:
        tmp_test = ce_loo.transform(test_df[cols])
        return tmp_train, tmp_test
    return tmp_train, None

def ce_leave_one_out_encoder_v2(train_df, test_df, cols, target):
    # こちらは自分も含めてtargetの平均でエンコードする
    ce_loo = ce.LeaveOneOutEncoder(cols=cols).fit(X=train_df[cols], y=train_df[target])
    tmp_train = ce_loo.transform(train_df[cols])
    if test_df is not None:
        tmp_test = ce_loo.transform(test_df[cols])
        return tmp_train, tmp_test
    return tmp_train, None

def ce_target_encoder(train_df, test_df, cols, target):
    ce_tgt = ce.TargetEncoder(cols=cols)
    tmp_train = ce_tgt.fit_transform(X=train_df[cols], y=train_df[target])
    if test_df is not None:
        tmp_test = ce_tgt.transform(test_df[cols])
        return tmp_train, tmp_test
    return tmp_train, None

def ce_catboost_encoder(train_df, test_df, cols, target):
    ce_cbe = ce.CatBoostEncoder(cols=cols, random_state=42)
    tmp_train = ce_cbe.fit_transform(X=train_df[cols], y=train_df[target])
    if test_df is not None:
        tmp_test = ce_cbe.transform(test_df[cols])
        return tmp_train, tmp_test
    return tmp_train, None

def ce_jamesstein_encoder(train_df, test_df, cols, target):
    ce_jse = ce.JamesSteinEncoder(cols=cols, drop_invariant=True)
    tmp_train = ce_jse.fit_transform(X=train_df[cols], y=train_df[target])
    if test_df is not None:
        tmp_test = ce_jse.transform(test_df[cols])
        return tmp_train, tmp_test
    return tmp_train, None


import cv2
import numpy as np

#Pick an image to analyze
img = cv2.imread('cube.jpg')

#Coordinates we need to extract [y,x], y increasing downwards
coordinates = 84,141

#color outputs as [B G R]
pxl = img[coordinates]

#Print pxl, RGB values
print(pxl)

#returns a List of coordinates, read from a txt file
def getCoordinatesArray(filename):
    file = open(filename, "r")
    upList, rightList, fwdList,downList, leftList, backList = [],[],[],[],[],[]

    list = []
    for line in file:
        list.append(line.strip().split('\n'))

    for x in range(0,6):
        upList.append(tuple(list[x]))
    for x in range(6,12):
        rightList.append(tuple(list[x]))
    for x in range(12,18):
        fwdList.append(tuple(list[x]))
    for x in range(18,24):
        downList.append(tuple(list[x]))
    for x in range(24,30):
        leftList.append(tuple(list[x]))
    for x in range(30,36):
        backList.append(tuple(list[x]))

    file.close()
    colorList = []
    colorList.append(upList)
    colorList.append(rightList)
    colorList.append(fwdList)
    colorList.append(downList)
    colorList.append(leftList)
    colorList.append(backList)
    return colorList

#print("R: " + str(pxl[2]) + "\tG: " + str(pxl[1]) + "\tB: " + str(pxl[0]))


""" explainer_main.py

     Main user interface for the explainer module.
"""
import argparse
import os

import sklearn.metrics as metrics

from tensorboardX import SummaryWriter

import pickle
import shutil
import torch

import models
import utils.io_utils as io_utils
import utils.parser_utils as parser_utils
from explainer import explain

def arg_parse():
    parser = argparse.ArgumentParser(description="GNN Explainer arguments.")
    io_parser = parser.add_mutually_exclusive_group(required=False)
    io_parser.add_argument("--dataset", dest="dataset", help="Input dataset.")
    benchmark_parser = io_parser.add_argument_group()
    benchmark_parser.add_argument(
        "--bmname", dest="bmname", help="Name of the benchmark dataset"
    )
    io_parser.add_argument("--pkl", dest="pkl_fname", help="Name of the pkl data file")

    parser_utils.parse_optimizer(parser)

    parser.add_argument("--clean-log", action="store_true", help="If true, cleans the specified log directory before running.")
    parser.add_argument("--logdir", dest="logdir", help="Tensorboard log directory")
    parser.add_argument("--ckptdir", dest="ckptdir", help="Model checkpoint directory")
    parser.add_argument("--cuda", dest="cuda", help="CUDA.")
    parser.add_argument(
        "--gpu",
        dest="gpu",
        action="store_const",
        const=True,
        default=False,
        help="whether to use GPU.",
    )
    parser.add_argument(
        "--epochs", dest="num_epochs", type=int, help="Number of epochs to train."
    )
    parser.add_argument(
        "--hidden-dim", dest="hidden_dim", type=int, help="Hidden dimension"
    )
    parser.add_argument(
        "--output-dim", dest="output_dim", type=int, help="Output dimension"
    )
    parser.add_argument(
        "--num-gc-layers",
        dest="num_gc_layers",
        type=int,
        help="Number of graph convolution layers before each pooling",
    )
    parser.add_argument(
        "--bn",
        dest="bn",
        action="store_const",
        const=True,
        default=False,
        help="Whether batch normalization is used",
    )
    parser.add_argument("--dropout", dest="dropout", type=float, help="Dropout rate.")
    parser.add_argument(
        "--nobias",
        dest="bias",
        action="store_const",
        const=False,
        default=True,
        help="Whether to add bias. Default to True.",
    )
    parser.add_argument(
        "--no-writer",
        dest="writer",
        action="store_const",
        const=False,
        default=True,
        help="Whether to add bias. Default to True.",
    )
    # Explainer
    parser.add_argument("--mask-act", dest="mask_act", type=str, help="sigmoid, ReLU.")
    parser.add_argument(
        "--mask-bias",
        dest="mask_bias",
        action="store_const",
        const=True,
        default=False,
        help="Whether to add bias. Default to True.",
    )
    parser.add_argument(
        "--explain-node", dest="explain_node", type=int, help="Node to explain."
    )
    parser.add_argument(
        "--graph-idx", dest="graph_idx", type=int, help="Graph to explain."
    )
    parser.add_argument(
        "--graph-mode",
        dest="graph_mode",
        action="store_const",
        const=True,
        default=False,
        help="whether to run Explainer on Graph Classification task.",
    )
    parser.add_argument(
        "--multigraph-class",
        dest="multigraph_class",
        type=int,
        help="whether to run Explainer on multiple Graphs from the Classification task for examples in the same class.",
    )
    parser.add_argument(
        "--multinode-class",
        dest="multinode_class",
        type=int,
        help="whether to run Explainer on multiple nodes from the Classification task for examples in the same class.",
    )
    parser.add_argument(
        "--align-steps",
        dest="align_steps",
        type=int,
        help="Number of iterations to find P, the alignment matrix.",
    )

    parser.add_argument(
        "--method", dest="method", type=str, help="Method. Possible values: base, att."
    )
    parser.add_argument(
        "--name-suffix", dest="name_suffix", help="suffix added to the output filename"
    )
    parser.add_argument(
        "--explainer-suffix",
        dest="explainer_suffix",
        help="suffix added to the explainer log",
    )

    # TODO: Check argument usage
    parser.set_defaults(
        logdir="log",
        ckptdir="ckpt",
        dataset="syn1",
        opt="adam",
        opt_scheduler="none",
        cuda="0",
        lr=0.1,
        clip=2.0,
        batch_size=20,
        num_epochs=100,
        hidden_dim=20,
        output_dim=20,
        num_gc_layers=3,
        dropout=0.0,
        method="base",
        name_suffix="",
        explainer_suffix="",
        align_steps=1000,
        explain_node=None,
        graph_idx=-1,
        mask_act="sigmoid",
        multigraph_class=-1,
        multinode_class=-1,
    )
    return parser.parse_args()

def main():
    # Load a configuration
    prog_args = arg_parse()

    if prog_args.gpu:
        os.environ["CUDA_VISIBLE_DEVICES"] = prog_args.cuda
        print("CUDA", prog_args.cuda)
    else:
        print("Using CPU")

    # Configure the logging directory
    if prog_args.writer:
        path = os.path.join(prog_args.logdir, io_utils.gen_explainer_prefix(prog_args))
        if os.path.isdir(path) and prog_args.clean_log:
           print('Removing existing log dir: ', path)
           if not input("Are you sure you want to remove this directory? (y/n): ").lower().strip()[:1] == "y": sys.exit(1)
           shutil.rmtree(path)
        writer = SummaryWriter(path)
    else:
        writer = None

    # Load a model checkpoint
    ckpt = io_utils.load_ckpt(prog_args)
    cg_dict = ckpt["cg"] # get computation graph
    input_dim = cg_dict["feat"].shape[2]
    num_classes = cg_dict["pred"].shape[2]
    print("Loaded model from {}".format(prog_args.ckptdir))
    print("input dim: ", input_dim, "; num classes: ", num_classes)

    # Determine explainer mode
    graph_mode = (
        prog_args.graph_mode
        or prog_args.multigraph_class >= 0
        or prog_args.graph_idx >= 0
    )

    # build model
    print("Method: ", prog_args.method)
    if graph_mode:
        # Explain Graph prediction
        model = models.GcnEncoderGraph(
            input_dim=input_dim,
            hidden_dim=prog_args.hidden_dim,
            embedding_dim=prog_args.output_dim,
            label_dim=num_classes,
            num_layers=prog_args.num_gc_layers,
            bn=prog_args.bn,
            args=prog_args,
        )
    else:
        if prog_args.dataset == "ppi_essential":
            # class weight in CE loss for handling imbalanced label classes
            prog_args.loss_weight = torch.tensor([1.0, 5.0], dtype=torch.float).cuda()
        # Explain Node prediction
        model = models.GcnEncoderNode(
            input_dim=input_dim,
            hidden_dim=prog_args.hidden_dim,
            embedding_dim=prog_args.output_dim,
            label_dim=num_classes,
            num_layers=prog_args.num_gc_layers,
            bn=prog_args.bn,
            args=prog_args,
        )
    if prog_args.gpu:
        model = model.cuda()
    # load state_dict (obtained by model.state_dict() when saving checkpoint)
    model.load_state_dict(ckpt["model_state"])

    # Create explainer
    explainer = explain.Explainer(
        model=model,
        adj=cg_dict["adj"],
        feat=cg_dict["feat"],
        label=cg_dict["label"],
        pred=cg_dict["pred"],
        train_idx=cg_dict["train_idx"],
        args=prog_args,
        writer=writer,
        print_training=True,
        graph_mode=graph_mode,
        graph_idx=prog_args.graph_idx,
    )

    # TODO: API should definitely be cleaner
    # Let's define exactly which modes we support
    # We could even move each mode to a different method (even file)
    if prog_args.explain_node is not None:
        explainer.explain(prog_args.explain_node, unconstrained=False)
    elif graph_mode:
        if prog_args.multigraph_class >= 0:
            print(cg_dict["label"])
            # only run for graphs with label specified by multigraph_class
            labels = cg_dict["label"].numpy()
            graph_indices = []
            for i, l in enumerate(labels):
                if l == prog_args.multigraph_class:
                    graph_indices.append(i)
                if len(graph_indices) > 30:
                    break
            print(
                "Graph indices for label ",
                prog_args.multigraph_class,
                " : ",
                graph_indices,
            )
            explainer.explain_graphs(graph_indices=graph_indices)

        elif prog_args.graph_idx == -1:
            # just run for a customized set of indices
            explainer.explain_graphs(graph_indices=[1, 2, 3, 4])
        else:
            explainer.explain(
                node_idx=0,
                graph_idx=prog_args.graph_idx,
                graph_mode=True,
                unconstrained=False,
            )
            io_utils.plot_cmap_tb(writer, "tab20", 20, "tab20_cmap")
    else:
        if prog_args.multinode_class >= 0:
            print(cg_dict["label"])
            # only run for nodes with label specified by multinode_class
            labels = cg_dict["label"][0]  # already numpy matrix

            node_indices = []
            for i, l in enumerate(labels):
                if len(node_indices) > 4:
                    break
                if l == prog_args.multinode_class:
                    node_indices.append(i)
            print(
                "Node indices for label ",
                prog_args.multinode_class,
                " : ",
                node_indices,
            )
            explainer.explain_nodes(node_indices, prog_args)

        else:
            # explain a set of nodes
            masked_adj = explainer.explain_nodes_gnn_stats(
                range(400, 700, 5), prog_args
            )

if __name__ == "__main__":
    main()


from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from os import path
from flask_login import LoginManager
from .logger_config import setup_logger
db = SQLAlchemy()
DB_NAME = "schmeisinger.db"

logger = setup_logger()

def create_app():
    app = Flask(__name__) # initialize flask
    app.config['SECRET_KEY'] = 'eih7theicieFeipap5heechaethaeT' # remove in production
    app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{DB_NAME}'
    db.init_app(app)

    from .views import views # register our blueprint
    from .auth import auth
    from .configure import configure
    app.register_blueprint(views,url_prefix='/')
    app.register_blueprint(auth, url_prefi='/') # '/auth' > means you need to navigate /auth/<name of page>
    app.register_blueprint(configure, url_prefix='/')

    from .models import UserEntity, GameEntity, PenaltyEntity, PenaltyRecordEntity, ParticipantEntity
    create_database(app)

    login_manager = LoginManager()
    login_manager.init_app(app)
    login_manager.login_view = 'auth.login'
    @login_manager.user_loader
    def load_user(id):
        return UserEntity.query.get(int(id)) # query.get is like query_id but looks for primary key by default so no id=id required

    def load_game(id):
        return GameEntity.query.get(int(id))
    return app

def create_database(app):
    # check if db exists if not create
    if not path.exists('website/' + DB_NAME):
        with app.app_context():
            db.create_all()
        logger.info("Created Database!")


from pygments.style import Style
from pygments.token import (
    Keyword,
    Name,
    Comment,
    String,
    Error,
    Number,
    Operator,
    Generic,
)

class AnsiStyle(Style):
    """
    Pygments version of the ansi colorscheme.
    """

    background_color = "ansiblack"
    highlight_color = "ansibrightblack"

    styles = {
        Comment:            "ansigreen",
        Comment.PreProc:    "ansicyan",
        Comment.Special:    "bold ansiyellow",
        Keyword:            "bold ansiblue",
        Keyword.Pseudo:     "nobold ansiblue",
        Keyword.Type:       "nobold ansiblue",
        Operator:           "ansiblue",
        Operator.Word:      "bold ansiblue",
        String:             "ansigreen",
        String.Escape:      "ansibrightyellow",
        String.Doc:         "ansigreen",
        String.Interpol:    "ansigreen",
        String.Escape:      "ansiyellow",
        String.Regex:       "ansiyellow",
        String.Symbol:      "ansigreen",
        String.Other:       "ansigreen",
        Number:             "ansimagenta",
        Name.Builtin:       "ansiblue",
        Name.Variable:      "ansiblue",
        Name.Constant:      "ansimagenta",
        Name.Class:         "ansicyan",
        Name.Function:      "ansicyan",
        Name.Namespace:     "ansicyan",
        Name.Exception:     "ansired",
        Name.Tag:           "ansicyan",
        Name.Attribute:     "ansiyellow",
        Name.Decorator:     "ansibrightyellow",
        Generic.Heading:    "bold ansigray",
        Generic.Subheading: "underline ansigray",
        Generic.Deleted:    "bg:ansired ansiblack",
        Generic.Inserted:   "bg:ansigreen ansiblack",
        Generic.Error:      "ansired",
        Generic.Emph:       "italic",
        Generic.Strong:     "bold",
        Generic.Prompt:     "ansibrightblack",
        Generic.Output:     "ansiwhite",
        Generic.Traceback:  "ansired",
        Error:              "bg:ansired ansiblack",
    }


from globals import *
from tank import *

def setup():
    global AFV1
    size(HEIGHT, WIDTH)
    AFV1 = Tank(150, 150, 100, 100, 150, 10, 10, 0, color = color(255,0,0))

def drawGrid():

    # Oh hey I found the text options
    textAlign(LEFT, TOP) # Bottom left corner of text is the specified x,y co ordinate
    textSize(10)

    stroke(0,0,0,30) # Black with transparency (30/255)
    fill(0,0,0,100) # Black with less transparency (100/255)

    # draw vertical lines
    x = 100
    while x < width:
        line(x,0,x,height)
        text(str(x), x + 2, 2) # Leave a border of 2 pixels
        x = x + 100 # Increment x by 100

    y = 100
    while y < height:
        line(0,y,width,y)
        text(str(y), 2, y + 2) # Leave a border of 2 pixels
        y = y + 100 # Increment y by 100

def draw():
    global AFV1
    #background(100)

    AFV1.draw()
    drawGrid()

'''def keyPressed():
    global bPosX, bVelX, bPosY, bVelY, AFV1.elAngle
    if keyCode == 37:
        AFV1.elAngle -= 1

    if keyCode == 39:
        AFV1.elAngle += 1

    if keyCode == 32:

        bPosX.append(width/2)
        bVelX.append(30*cos(elAngle*deg))
        bPosY.append(height/2)
        bVelY.append(30*sin(elAngle*deg))'''


import argparse
import os

from chatllama.rlhf.dataset import AnthropicRLHF, StanfordNLPSHPDataset

if __name__ == "__main__":

    # Setup argument parser
    parser = argparse.ArgumentParser(
        prog="generate_rewards.py",
        description="Generate rewards using LangChain and LLMs",
    )

    parser.add_argument(
        "dataset_name",
        help="dataset name it can be. SSHP: stanfordnlp/SHP or ",
        choices=["SHP", "ARLHF"],
    )
    parser.add_argument(
        "-p",
        "--path",
        help="Specify the path for the dataset",
        default="./datasets",
    )
    parser.add_argument(
        "-n",
        "--number_of_samples",
        help="Specify the number of samples for the reward dataset",
        default=200,
    )

    args = parser.parse_args()
    if os.path.exists(args.path) is False:
        os.mkdir(args.path)

    try:
        n_samples = int(args.number_of_samples)
    except ValueError:
        raise ValueError("Number of samples should be an integer")

    if args.dataset_name == "SHP":
        dataset = StanfordNLPSHPDataset()
        dataset.save_dataset(args.path, n_samples)

    elif args.dataset_name == "ARLHF":
        dataset = AnthropicRLHF()
        dataset.save_dataset(
            args.path,
            n_samples,
        )


#!/bin/python

#################################################
#   Name: Neil Johnson                          #
#   Due: 10/28/18                               #
#   Assignment: Project 1                       #
#   Details: Create a simple chat client        #
#################################################

from socket import *
import sys
import os
import time

def clear_terminal():
    os.system('cls' if os.name == 'nt' else 'clear')

def animate_connect(port_number):
    print("Trying to connect on port {}".format(port_number), end="")

    for i in range(0, 5):
        time.sleep(1)
        sys.stdout.write(".")

    print("Connected!")
    time.sleep(1)
    print("\nThe server is ready to receive.\n")
    time.sleep(2)

def get_username():
    # Make sure that the username is less than or equal to 10 digits
    user = input("Type in your user (less than or equal to 10 characters. > ")
    while 0 >= len(user) or len(user) > 10:
        if len(user) < 1:
            print("Too short of a user, try again.")
            user = input("> ")
        elif len(user) > 10:
                print("Too long of a user, try again.")
                user = input("> ")
    print("Name approved, hello {}, waiting for other user to connect...".format(user))
    time.sleep(1)

    # Return the appropriate name for the user
    return user

def build_message(user, msg_size):
    # Build the message and make sure it can fit in the allotted size that was asked (500) for our case.
    message = ""
    while 1 > len(message) or len(message) > msg_size:
        message = input("{}> ".format(user))

        if len(message) < 1:
            print("Too short of a message, try again.")
        elif len(message) > msg_size:
            print("Too long of a message, try again.")

    # Return the message to the main statement
    return message

def name_exchange(connection_socket, user):
    # Receive the name from the client
    client_name = connectionSocket.recv(1024).decode()

    # Send the username and encode it for shipping out.
    connection_socket.send(user.encode())

    return client_name

# Create the continuous chatting system between server and client.
def chat_feature(connectionSocket, msg_size, client, user):
    # Clear out terminal to make it look fresh
    clear_terminal()

    # Print out a basic welcome screen once the server is set up
    print("##########################################################################")
    print("#    Welcome to Neil's Project 1 Chat Client!                            ")
    print("#    In here you can chat with other users connected to the same system. ")
    print("#    Current Connected User:                                             ")
    print("#    Server: {}                                                          ".format(user))
    print("#    Client: {}                                                          ".format(client))
    print("##########################################################################")

    while True:
        # Get the message from the client up to a specific amount specified earlier.
        client_msg = connectionSocket.recv(500)[0:-1].decode()

        if client_msg == "":
            print("Terminating connection.")
            break

        print("{}> {}".format(client, client_msg))

        server_msg = build_message(user, msg_size)

        if server_msg == "\quit":
            print("Terminating connection.")
            break

        connectionSocket.send(server_msg.encode())

if __name__ == "__main__":
    # Verify user has given the port as an argument
    if len(sys.argv) != 2:
        print("Incorrect amount of arguments supplied. Re-run and supply the port number you'd like to use.")
        exit(1)
    elif not sys.argv[1].isdigit():
        print("Supplied value is not a number.  Re-run and supply the port number you'd like to use.")
        exit(1)

    # Mimicked this portion from the lecture files to build the connection
    serverPort = sys.argv[1]
    serverSocket = socket(AF_INET, SOCK_STREAM)
    serverSocket.bind(('', int(serverPort)))
    serverSocket.listen(1)

    # Create animation to make connection look fancy on terminal
    animate_connect(serverPort)

    # Get a valid username for the server to use
    username = get_username()

    connectionSocket, addr = serverSocket.accept()

    # Create the chat feature between the user and the server
    chat_feature(connectionSocket, 500, name_exchange(connectionSocket, username), username)

    # Close the connection once done with the client
    connectionSocket.close()

    exit(1)


import math

class Annealer:
    """
    This class is used to anneal the KL divergence loss over the course of training VAEs.
    After each call, the step() function should be called to update the current epoch.
    """

    def __init__(self, total_steps, shape, baseline=0.0, cyclical=False, disable=False):
        """
        Parameters:
            total_steps (int): Number of epochs to reach full KL divergence weight.
            shape (str): Shape of the annealing function. Can be 'linear', 'cosine', or 'logistic'.
            baseline (float): Starting value for the annealing function [0-1]. Default is 0.0.
            cyclical (bool): Whether to repeat the annealing cycle after total_steps is reached.
            disable (bool): If true, the __call__ method returns unchanged input (no annealing).
        """
        self.total_steps = total_steps
        self.current_step = 0
        self.cyclical = cyclical
        self.shape = shape
        self.baseline = baseline
        if disable:
            self.shape = 'none'
            self.baseline = 0.0

    def __call__(self, kld):
        """
        Args:
            kld (torch.tensor): KL divergence loss
        Returns:
            out (torch.tensor): KL divergence loss multiplied by the slope of the annealing function.
        """
        out = kld * self.slope()
        return out

    def slope(self):
        if self.shape == 'linear':
            y = (self.current_step / self.total_steps)
        elif self.shape == 'cosine':
            y = (math.cos(math.pi * (self.current_step / self.total_steps - 1)) + 1) / 2
        elif self.shape == 'logistic':
            exponent = ((self.total_steps / 2) - self.current_step)
            y = 1 / (1 + math.exp(exponent))
        elif self.shape == 'none':
            y = 1.0
        else:
            raise ValueError('Invalid shape for annealing function. Must be linear, cosine, or logistic.')
        y = self.add_baseline(y)
        return y

    def step(self):
        if self.current_step < self.total_steps:
            self.current_step += 1
        if self.cyclical and self.current_step >= self.total_steps:
            self.current_step = 0
        return

    def add_baseline(self, y):
        y_out = y * (1 - self.baseline) + self.baseline
        return y_out

    def cyclical_setter(self, value):
        if value is not bool:
            raise ValueError('Cyclical_setter method requires boolean argument (True/False)')
        else:
            self.cyclical = value
        return


import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from scipy.spatial.qhull import ConvexHull

# These functions graphically plot the polyhedron and the randomly distributed source points

def plot_poly_with_sources(poly, points):
    points_by_innermost_poly = {poly: [] for poly in poly.all_nested_polygons()}
    for point in points:
        innermost_poly = poly.innermost_poly_at(point)
        points_by_innermost_poly[innermost_poly].append(point)
    _plot_graph(points_by_innermost_poly)

def _plot_graph(points_by_innermost_poly):
    fig = plt.figure()
    ax = fig.add_subplot(111, projection="3d")

    for poly, poly_points in points_by_innermost_poly.items():
        xs, ys, zs = zip(*poly_points)
        ax.plot(xs, ys, zs, "o", markersize=0.3)

        ax.plot(poly.points.T[0], poly.points.T[1], poly.points.T[2], "ko")

        for s in ConvexHull(poly.points).simplices:
            s = np.append(s, s[0])  # Here we cycle back to the first coordinate
            ax.plot(poly.points[s, 0], poly.points[s, 1], poly.points[s, 2], "r-")

    # Make axis label
    for i in ["x", "y", "z"]:
        eval("ax.set_{:s}label('{:s}')".format(i, i))

    plt.show()


from django.conf.urls import url

from . import views

app_name = 'movies'
urlpatterns = [
	url(r'^$', views.listing, name='index'),
	url(r'^wishlist/(?P<user_id>[0-9]+)/$', views.show_wish_list, name='wishlist'),
	url(r'^remove-wishlist/$', views.remove_from_wish_list, name='remove-wishlist'),
	url(r'^add-wishlist/$', views.add_to_wish_list, name='add-wishlist'),
	url(r'^rate/$', views.rate, name='rate'),
    url(r'^profile/(?P<user_id>[0-9]+)/$', views.profile, name='profile'),
    url(r'^update/(?P<user_id>[0-9]+)/$', views.update, name='update'),
    url(r'^(?P<movie_id>[0-9]+)/$', views.detail, name='detail'),
]

Software Process and Project Management/movies/urls.py


"""
module_template.py - A simple Module

Author: Levi Nachamni (levi0x0)
Date: 20/05/2014
Version: 0.1
License: GPL 3

Description:
	A Simple Hello World Module

"""

MODULE_NAME = "Hello World"
MODULE_AUTHOR = "Your Name"
MODULE_LICENSE = "GPL 3"
MODULE_VERSION = "0.1"
MODULE_DESC = "A Simple Hello World Module"

"""
	Your Code HERE!
"""

def main():
	"""
		initialize your code here
	"""

"""
END OF THE CODE!
"""


from setuptools import setup, find_packages

with open("./req.txt", "r") as fi:
    req = fi.read().split("\n")

print(find_packages())

setup(
    name="my_package",
    version="0.1",
    packages=find_packages(),
    license="No License",
    author="suraj.mittal",
    install_requires=req,
    python_requires=">=3.7",
#     package_data={
#         "sample": ["package_data.dat"],
#     },
)

# setup(
#     name="my_packages_2",
#     packages=find_packages(where="src"),
#     version="0.1",
#     description="pacakging inside src folder",
#     author="suraj.mittal",
#     package_dir={"": "src"},
# )


import time, sys, os, random
import numpy as np
import tensorflow as tf

def load_weights(sess,path,scope=None):
    if os.path.exists(path):
        # Restore saved weights
        print('Restoring saved model ... ')
        # Find out from which epoch we are restoring weights
        epoch = int(path.split('/')[-1].replace('epoch=',''))
        # Create model saver
        if scope is None:
            saver = tf.train.Saver()
        else:
            saver = tf.train.Saver(var_list=tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope=scope))
        #end
        saver.restore(sess, "%s/model.ckpt" % path)
        return epoch
    else:
        raise Exception('Path does not exist!')
    #end if
#end

def save_weights(sess,path,scope=None):
    # Create /tmp/ directory to save weights
    if not os.path.exists(path):
        os.makedirs(path)
    #end if
    # Create model saver
    if scope is None:
        saver = tf.train.Saver()
    else:
        saver = tf.train.Saver(var_list=tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope=scope))
    #end
    saver.save(sess, "%s/model.ckpt" % path)
    print('Model saved in path: {path}\n'.format(path=path))
#end


# -*- coding:utf-8 -*-
#####################
# BACKEND FUNCTIONS #
#####################
# FORKED FROM CMD ATE DATA READER
from abc import ABC
import numpy as np
import matplotlib
from mpl_toolkits.mplot3d import Axes3D

matplotlib.use('Agg')
import matplotlib.pyplot as plt
import pandas as pd
from decimal import Decimal
import os

# IMPORTANT DOCUMENTATION I NEED TO FILL OUT TO MAKE SURE PEOPLE KNOW WHAT THE HELL IS GOING ON

# ~~~~~~~~~~ Data definition explanations (in functions) ~~~~~~~~~~ #
#                                                                   #
# test_tuple --> ['test_number', 'test_name']                       #
#   Structure for associating a test's name with its test number    #
#                                                                   #
# test_list --> List of test_tuple                                  #
#   find_test_of_number() returns this                              #
#   list_of_test_numbers in main() is the complete list of this     #
#                                                                   #
# num_of_sites --> number of testing sites for each test run        #
#   number_of_sites = int(sdr_parse[3]) in main()                   #
#                                                                   #
# test_info_list --> list of sets of site_data                      #
#   (sorted in the same order as corresponding tests in test_list)  #
#                                                                   #
# site_data --> array of float data points number_of_sites long     #
#   raw data for a single corresponding test_tuple                  #
#                                                                   #
# test_list and test_info_list are the same length                  #
#                                                                   #
# minimum, maximum --> floats                                       #
#   lower and upper extremes for a site_data from a corresponding   #
#       test_tuple. These are found in the ptr_data (data), which   #
#       has the values located in one of the columns for the first  #
#       test site in the first data point in a test.                #
#   returned by get_plot_extremes() abstraction                     #
# units --> string                                                  #
#   Gathered virtually identically to minimum and maximum.          #
#       Represents units for plotting and post calculations on      #
#       data sets.                                                  #
#                                                                   #
# ~~~~~~~~~~~~ Parsed text file structure explanation ~~~~~~~~~~~~~ #
# The PySTDF library parses the data really non-intuitively,        #
#   although it can be viewed somewhat more clearly if you use the  #
#   toExcel() function (I recommend doing this for figuring out the #
#   way the file is formatted). Basically, each '|' separates the   #
#   information in columns, and the first column determines the     #
#   "page" you are dealing with. The only ones I found particularly #
#   useful were SDR (for the number of sites) and PTR (where all    #
#   the data is actually contained).                                #
# The way the PTR data is parsed is very non-intuitive still, with  #
#   the data broken into chunks num_of_sites lines long, meaning    #
#   each chunk of num_of_sites lines contain a corresponding        #
#   test_tuple that can be extracted, as well as a data point       #
#   result for each test site. This is then done for every single   #
#   test_tuple combination. After all that is done, the process is  #
#   repeated for every single run of the test, creating a new data  #
#   point for each site in each test tuple for however many numbers #
#   of tests there are.                                             #
# It's very not obvious at first, so I strongly recommend creating  #
#   an excel file first to look at it yourself and reverse-engineer #
#   it like I did if you really want to try and figure out the file #
#   format yourself. I'm sorry the library sucks but I didn't       #
#   design it :/ . Good luck!                                       #
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #

# This is horrible design and I'm so sorry, but here's a huge library full of static methods for processing the data
# These were all taken virtually verbatim from the previous program so have mercy on me
class Backend(ABC):

    # Plots the results of everything from one test
    @staticmethod
    def plot_everything_from_one_test(test_data, sdr_parse, data, num_of_sites, test_tuple, fail_limit, group_by_file):

        # Find the limits
        low_lim = Backend.get_plot_min(data, test_tuple, num_of_sites)
        hi_lim = Backend.get_plot_max(data, test_tuple, num_of_sites)
        units = Backend.get_units(data, test_tuple, num_of_sites)

        print(test_tuple)

        # if low_lim == float('-inf'):
        #
        #     if min(np.concatenate(test_data)) < 0:
        #         low_lim = min(np.concatenate(test_data, axis=0))
        #
        #     else:
        #         low_lim = 0
        #
        # if hi_lim == float('inf') or low_lim > hi_lim:
        #     hi_lim = max(np.concatenate(test_data, axis=0))

        # Title for everything
        plt.suptitle(
            str("Test: " + test_tuple[0] + " - " + test_tuple[1] + " - Units: " + units))

        # Plots the table of results, showing a max of 16 sites at once, plus all the collective data
        table = Backend.table_of_results(test_data, sdr_parse, low_lim, hi_lim, units)
        # cell_text = table.copy()
        plt.subplot2grid((2, 3), (1, 2))
        cell_text = []
        for row in range(len(table)):
            cell_text.append([table.index[row]] + table.iloc[row].to_list())

        plt.table(cellText=cell_text, loc='center')
        plt.axis('off')
        # else:
        # Plots the trendline
        plt.subplot2grid((2, 3), (0, 0), colspan=3)
        Backend.plot_full_test_trend(test_data, low_lim, hi_lim, fail_limit, sdr_parse, group_by_file)
        plt.xlabel("Trials")
        plt.ylabel(units)
        plt.title("Trendline")
        plt.grid(color='0.9', linestyle='--', linewidth=1)

        # Plots the histogram
        ax = plt.subplot2grid((2, 3), (1, 0), colspan=2, projection='3d')
        Backend.plot_full_test_hist(test_data, low_lim, hi_lim, fail_limit, ax)
        ax.set_xlabel(units)
        # ax.set_xticklabels(units, rotation=45)
        ax.set_ylabel('Site')
        ax.set_zlabel("Trials")
        # ax.legend(loc='best')
        # plt.xlabel(units)
        # plt.xticks(rotation=45)  # Tilted 45 degree angle
        # plt.ylabel("Trials")
        # plt.title("Histogram")
        # plt.grid(color='0.9', linestyle='--', linewidth=1, axis='y')
        # plt.legend(loc='best')

    # TestNumber (string) + ListOfTests (list of tuples) -> ListOfTests with TestNumber as the 0th index (list of tuples)
    # Takes a string representing a test number and returns any test names associated with that test number
    #   e.g. one test number may be 1234 and might have 40 tests run on it, but it may be 20 tests under
    #   the name "device_test_20kHz" and then another 20 tests under the name "device_test_100kHz", meaning
    #   there were two unique tests run under the same test number.
    @staticmethod
    def find_tests_of_number(test_number, test_list):
        tests_of_number = []
        for i in range(0, len(test_list)):
            if test_list[i][0] == test_number:
                tests_of_number.append(test_list[i])

        return tests_of_number

    # Returns the lower allowed limit of a set of data
    @staticmethod
    def get_plot_min(data, test_tuple, num_of_sites):
        minimum = Backend.get_plot_extremes(data, test_tuple, num_of_sites)[0]
        try:

            smallboi = float(minimum)

        except ValueError:

            smallboi = 'n/a'

        return smallboi

    # Returns the upper allowed limit of a set of data
    @staticmethod
    def get_plot_max(data, test_tuple, num_of_sites):

        maximum = Backend.get_plot_extremes(data, test_tuple, num_of_sites)[1]

        try:

            bigboi = float(maximum)

        except ValueError:

            bigboi = 'n/a'

        return bigboi

    # Returns the units for a set of data
    @staticmethod
    def get_units(data, test_tuple, num_of_sites):
        return Backend.get_plot_extremes(data, test_tuple, num_of_sites)[2]

    # Abstraction of above 3 functions, returns tuple with min and max
    @staticmethod
    def get_plot_extremes(data, test_tuple, num_of_sites):
        minimum_test = 0
        maximum_test = 1
        units = ''
        temp = 0
        not_found = True
        while not_found:
            # try:
            # if data[temp].split("|")[1] == test_tuple[0]:
            #     minimum_test = (data[temp].split("|")[13])
            #     maximum_test = (data[temp].split("|")[14])
            #     units = (data[temp].split("|")[15])
            #     not_found = False

            # Check data out with TNUM&TNAME
            if data[temp][4] == test_tuple[0] and data[temp][0] == test_tuple[1]:
                minimum_test = data[temp][2]
                maximum_test = data[temp][1]
                units = data[temp][3]
                not_found = False
            temp += 1  # num_of_sites
            # except IndexError:
            #     os.system('pause')
        return [minimum_test, maximum_test, units]

    # Plots the results of all sites from one test
    @staticmethod
    def plot_full_test_trend(test_data, minimum, maximum, fail_limit, label_list, group_by_file):

        data_min = min(np.concatenate(test_data, axis=0))
        data_max = max(np.concatenate(test_data, axis=0))

        # in case of (-inf,inf)
        if (minimum == float('-inf')) or (maximum == float('inf')):
            minimum = data_min
            maximum = data_max

        expand = max([abs(minimum), abs(maximum)])

        # Plots each site one at a time
        for i in range(0, len(test_data)):
            Backend.plot_single_site_trend(test_data[i], group_by_file, label_list, i)
        plt.legend()

        # Plots the minimum and maximum barriers
        if fail_limit:
            if minimum == float('-inf'):
                plt.plot(range(0, len(test_data[0])), [
                    0] * len(test_data[0]), color="red", linewidth=3.0)
                plt.plot(range(0, len(test_data[0])), [
                    maximum] * len(test_data[0]), color="red", linewidth=3.0)

            elif maximum == float('inf'):
                plt.plot(range(0, len(test_data[0])), [
                    minimum] * len(test_data[0]), color="red", linewidth=3.0)
                plt.plot(range(0, len(test_data[0])), [max(np.concatenate(
                    test_data, axis=0))] * len(test_data[0]), color="red", linewidth=3.0)

            else:
                plt.plot(range(0, len(test_data[0])), [
                    minimum] * len(test_data[0]), color="red", linewidth=3.0)
                plt.plot(range(0, len(test_data[0])), [
                    maximum] * len(test_data[0]), color="red", linewidth=3.0)

        if fail_limit:

            # My feeble attempt to get pretty dynamic limits
            if minimum == maximum:
                plt.ylim(ymin=minimum - abs(0.05 * expand))
                # try:
                plt.ylim(ymax=maximum + abs(0.05 * expand))
                # except ValueError:
                #     # print(type(maximum))
                #     print(max(maximum + abs(0.05 * expand), 1.05))
                #     os.system('pause')
            else:
                # try:
                plt.ylim(ymin=minimum - abs(0.05 * expand))
                # except ValueError:
                #     print(type(minimum))
                #     print(minimum)
                plt.ylim(ymax=maximum + abs(0.05 * expand))
        else:
            if minimum == maximum:
                plt.ylim(ymin=minimum - abs(0.05 * expand))
                plt.ylim(ymax=maximum + abs(0.05 * expand))
            else:
                plt.ylim(ymin=(min(data_min, minimum - abs(0.05 * expand))))
                # try:
                plt.ylim(ymax=(max(data_max, maximum + abs(0.05 * expand))))
                # except BaseException:
                #     print(min(data_min, minimum - abs(0.05 * expand)))
                #     print(max(data_max, maximum + abs(0.05 * expand)))
                #     os.system('pause')

    # Returns the table of the results of all the tests to visualize the data
    @staticmethod
    def table_of_results(test_data, sdr_parse, minimum, maximum, units):
        parameters = ['Site', 'Units', 'Runs', 'Fails', 'LowLimit', 'HiLimit',
                      'Min', 'Mean', 'Max', 'Range', 'STD', 'Cp', 'Cpl', 'Cpu', 'Cpk']

        # Clarification
        if 'db' in units.lower():
            parameters[7] = 'STD (%)'

        all_data = np.concatenate(test_data, axis=0)

        test_results = [Backend.site_array(
            all_data, minimum, maximum, 'ALL', units)]

        for i in range(0, len(sdr_parse)):
            test_results.append(Backend.site_array(
                test_data[i], minimum, maximum, sdr_parse[i], units))

        table = pd.DataFrame(test_results, columns=parameters).T

        return table

    # Returns an array a site's final test results
    @staticmethod
    def site_array(site_data, minimum, maximum, site_number, units):

        high_limit = maximum
        low_limit = minimum
        if units.startswith('Unnamed'):
            units = ''
        # if minimum == 'n/a' and maximum == 'n/a':
        #     minimum = 0
        #     maximum = 0

        # if (minimum == float('-inf')) or (maximum == float('inf')):
        #     minimum = 0
        #     maximum = 0

        if (minimum is None) and (maximum is None):
            minimum = 0
            maximum = 0
        # Big boi initialization
        site_results = []

        if len(site_data) == 0:
            return site_results

        # Not actually volts, it's actually % if it's db technically but who cares
        volt_data = []

        # Pass/fail data is stupid
        if minimum == maximum or min(site_data) == max(site_data) or \
                ((minimum == float('-inf')) or (maximum == float('inf'))):
            mean_result = np.mean(site_data)
            std_string = str(np.std(site_data))
            cp_result = 'n/a'
            cpl_result = 'n/a'
            cpu_result = 'n/a'
            cpk_result = 'n/a'

        # The struggles of logarithmic data
        elif 'db' in units.lower():

            for i in range(0, len(site_data)):
                volt_data.append(Backend.db2v(site_data[i]))

            mean_result = Backend.v2db(np.mean(volt_data))
            standard_deviation = np.std(
                volt_data) * 100  # *100 for converting to %
            std_string = str('%.3E' % (Decimal(standard_deviation)))

            cp_result = float(Decimal(Backend.cp(volt_data, Backend.db2v(
                minimum), Backend.db2v(maximum))).quantize(Decimal('0.001')))
            cpl_result = str(Decimal(Backend.cpl(
                volt_data, Backend.db2v(minimum))).quantize(Decimal('0.001')))
            cpu_result = str(Decimal(Backend.cpu(
                volt_data, Backend.db2v(maximum))).quantize(Decimal('0.001')))
            cpk_result = float(Decimal(Backend.cpk(volt_data, Backend.db2v(
                minimum), Backend.db2v(maximum))).quantize(Decimal('0.001')))

        # Yummy linear data instead
        else:
            mean_result = np.mean(site_data)
            # try:
            std_string = str(
                Decimal(np.std(site_data)).quantize(Decimal('0.001')))
            cp_result = float(
                Decimal(Backend.cp(site_data, minimum, maximum)).quantize(Decimal('0.001')))
            cpl_result = str(
                Decimal(Backend.cpl(site_data, minimum)).quantize(Decimal('0.001')))
            cpu_result = str(
                Decimal(Backend.cpu(site_data, maximum)).quantize(Decimal('0.001')))
            cpk_result = float(
                Decimal(Backend.cpk(site_data, minimum, maximum)).quantize(Decimal('0.001')))
            # except decimal.InvalidOperation:
            #     print(type(minimum))
            #     print(minimum)
            #     print(maximum)
            #     os.system("pause")
            # raise UnexpectedSymbol(minimum, maximum)

        # Appending all the important results weow!
        site_results.append(str(site_number))
        site_results.append(units)
        site_results.append(str(len(site_data)))
        site_results.append(
            str(Backend.calculate_fails(site_data, minimum, maximum)))
        # try:
        if low_limit == None or low_limit == float('inf') or low_limit == float('-inf') or low_limit == 'n/a':
            site_results.append(str(low_limit))
        else:
            site_results.append(str(Decimal(low_limit).quantize(Decimal('0.000001'))))
        if high_limit == None or high_limit == float('inf') or high_limit == float('-inf') or low_limit == 'n/a':
            site_results.append(str(high_limit))
        else:
            site_results.append(str(Decimal(high_limit).quantize(Decimal('0.000001'))))

        try:
            site_results.append(
                str(Decimal(float(min(site_data))).quantize(Decimal('0.000001'))))
        except Exception as e:
            print(e)
            os.system('pause')
        site_results.append(
            str(Decimal(mean_result).quantize(Decimal('0.000001'))))
        site_results.append(
            str(Decimal(float(max(site_data))).quantize(Decimal('0.000001'))))
        site_results.append(
            str(Decimal(float(max(site_data) - min(site_data))).quantize(Decimal('0.000001'))))

        site_results.append(std_string)
        site_results.append(cp_result)
        site_results.append(cpl_result)
        site_results.append(cpu_result)
        site_results.append(cpk_result)

        return site_results

    # Converts to decibels
    @staticmethod
    def v2db(v):
        return 20 * np.log10(abs(v))

    # Converts from decibels
    @staticmethod
    def db2v(db):
        return 10 ** (db / 20)

    # Function to convert from mW to dBm
    @staticmethod
    def mW2dBm(mW):
        return 10. * np.log10(mW)

    # Function to convert from dBm to mW
    @staticmethod
    def dBm2mW(dBm):
        return 10 ** ((dBm) / 10.)

    # Counts the number of fails in a data set
    @staticmethod
    def calculate_fails(site_data, minimum, maximum):
        fails_count = 0
        if (minimum != float('-inf') or maximum != float('inf')) and (minimum != 'n/a' and maximum != 'n/a'):
            # Increase a fails counter for every data point that exceeds an extreme
            for i in range(0, len(site_data)):
                if site_data[i] > float(maximum) or site_data[i] < float(minimum):
                    fails_count += 1

        return fails_count

    # Plots the historgram results of all sites from one test
    @staticmethod
    def plot_full_test_hist(test_data, minimum, maximum, fail_limit, ax):

        # in case of inf
        if minimum == float('-inf'):
            new_minimum = min(np.concatenate(test_data, axis=0))
        else:
            new_minimum = min(min(np.concatenate(test_data, axis=0)), minimum)
        if maximum == float('inf'):
            new_maximum = max(np.concatenate(test_data, axis=0))
        else:
            new_maximum = max(max(np.concatenate(test_data, axis=0)), maximum)

        # if (minimum == float('-inf')) or (maximum == float('inf')):
        #     minimum = 0
        #     maximum = 0

        # Plots each site one at a time
        for i in range(0, len(test_data)):
            Backend.plot_single_site_hist(
                test_data[i], new_minimum, new_maximum, test_data, i, ax)

        if fail_limit == False:

            # My attempt to get pretty dynamic limits
            if minimum == maximum:
                expand = abs(20.0 * minimum)
                # plt.xlim(xmin=minimum - abs(0.25 * minimum))
                # plt.xlim(xmax=minimum + abs(0.25 * minimum))

            elif minimum == float('-inf') and maximum != float('inf'):
                expand = abs(maximum)
                # plt.xlim(xmin=new_minimum - abs(0.05 * expand))
                # plt.xlim(xmax=new_maximum + abs(0.05 * expand))

            elif maximum == float('inf') and minimum != float('-inf'):
                expand = abs(minimum)
                # plt.xlim(xmin=new_minimum - abs(0.05 * expand))
                # plt.xlim(xmax=new_maximum + abs(0.05 * expand))

            elif maximum == float('inf') and minimum == float('-inf'):
                expand = max([abs(new_minimum), abs(new_maximum)])
                # plt.xlim(xmin=new_minimum - abs(0.05 * expand))
                # plt.xlim(xmax=new_maximum + abs(0.05 * expand))
            else:
                expand = max([abs(minimum), abs(maximum)])
                # plt.xlim(xmin=new_minimum - abs(0.05 * expand))
                # plt.xlim(xmax=new_maximum + abs(0.05 * expand))
            ax.set_xlim(new_minimum - abs(0.05 * expand), new_maximum + abs(0.05 * expand))

        else:

            if minimum == maximum:
                # plt.axvline(x=minimum, linestyle="--")
                # plt.axvline(x=minimum, linestyle="--")
                # plt.xlim(xmin=minimum - 1)
                # plt.xlim(xmax=minimum + 1)
                expand = abs(20.0 * minimum)

            elif minimum == float('-inf') and maximum != float('inf'):
                expand = abs(maximum)
                # plt.axvline(x=maximum, linestyle="--")
                # plt.xlim(xmin=new_minimum - abs(0.1 * expand))
                # plt.xlim(xmax=new_maximum + abs(0.1 * expand))

            elif maximum == float('inf') and minimum != float('-inf'):
                expand = abs(minimum)
                # plt.axvline(x=minimum, linestyle="--")
                # plt.xlim(xmin=new_minimum - abs(0.1 * expand))
                # plt.xlim(xmax=new_maximum + abs(0.1 * expand))

            elif maximum == float('inf') and minimum == float('-inf'):
                expand = 20 # max([abs(new_minimum), abs(new_maximum)])
                # plt.xlim(xmin=new_minimum - 1)
                # plt.xlim(xmax=new_maximum + 1)
            else:
                expand = max([abs(minimum), abs(maximum)])
                # ax.axvline(x=minimum, linestyle="--")
                # ax.axvline(x=maximum, linestyle="--")
            ax.set_xlim(new_minimum - abs(0.05 * expand), new_maximum + abs(0.05 * expand))
        # Nop this command for the figure may shift out of sight when the values too close
        #ax.get_proj = lambda: np.dot(Axes3D.get_proj(ax), np.diag([1.2, 1, 1, 1]))

        # ax.set_zlim(0,len(test_data[0]))

    # Plots a single site's results
    @staticmethod
    def plot_single_site_trend(site_data, group_by_file, label_list, i):
        marker_list = ['o','+','x','*','.','v','s','p','D','d','^','1']
        if group_by_file:
            plt.scatter(range(0, len(site_data)), site_data, marker=marker_list[i], alpha=0.6, label=label_list[i])
        else:
            plt.plot(range(0, len(site_data)), site_data, marker=marker_list[i % 12], alpha=0.6, label='site ' + str(label_list[i]))

    # Plots a single site's results as a histogram
    @staticmethod
    def plot_single_site_hist(site_data, minimum, maximum, test_data, site_num, ax):

        # if (minimum == float('-inf')) or (maximum == float('inf')):
        #     minimum = 0
        #     maximum = 0

        # At the moment the bins are the same as they are in the previous program's results. Will add fail bin later.

        # Damn pass/fail data exceptions everywhere
        if minimum == maximum:
            binboi = np.linspace(minimum - 1, maximum + 1, 51)

        elif minimum > maximum:
            binboi = np.linspace(minimum, max(
                np.concatenate(test_data, axis=0)), 51)

        elif minimum == float('-inf'):
            binboi = np.linspace(0, maximum, 51)

        elif maximum == float('inf'):
            binboi = np.linspace(minimum, max(
                np.concatenate(test_data, axis=0)), 51)

        else:
            binboi = np.linspace(minimum, maximum, 51)
        # try:
        # plt.hist(site_data, bins=binboi, edgecolor='white', linewidth=0.5, label='site ' + str(site_num))
        hist, bins = np.histogram(site_data, bins=binboi)
        xs = (bins[:-1] + bins[1:]) / 2
        X, Y = np.meshgrid(minimum, np.linspace(0, np.max(hist), 10))
        ax.bar(xs, hist, zs=site_num, zdir='y', alpha=0.8,
               width=np.diff(binboi).mean())  # , label='site ' + str(site_num))
        ax.plot(xs=X.reshape(10), ys=Y.reshape(10), zs=site_num, zdir='y', color='red', linestyle='--')
        X, Y = np.meshgrid(maximum, np.linspace(0, np.max(hist), 10))
        ax.plot(xs=X.reshape(10), ys=Y.reshape(10), zs=site_num, zdir='y', color='red', linestyle='--')
        # except ValueError:
        #     print(binboi)
        #     print(type(minimum))
        #     print(minimum)
        # np.clip(site_data, binboi[0], binboi[-1])

    # Creates an array of arrays that has the raw data for each test site in one particular test
    # expect a 2D array with each row being the reran test results for each of the sites in a particular test
    @staticmethod
    def single_test_data(num_of_sites, extracted_ptr):

        # Me being bad at initializing arrays again, hush
        single_test = []

        # Runs through once for each of the sites in the test, incrementing by 1
        for i in range(0, num_of_sites):

            single_site = []

            # Runs through once for each of the loops of the test, incrementing by the number of test sites until all test
            # loops are covered for the individual testing site. The incremented i offsets it so that it moves on to the
            # next testing site
            for j in range(i, len(extracted_ptr), num_of_sites):
                single_site.append(float(extracted_ptr[j][6]))

            single_test.append(single_site)

        return single_test

    # For the four following functions, site_data is a list of raw floating point data, minimum is the lower limit and
    # maximum is the upper limit

    # CP AND CPK FUNCTIONS
    # Credit to: countrymarmot on github gist:  https://gist.github.com/countrymarmot/8413981
    @staticmethod
    def cp(site_data, minimum, maximum):
        if minimum == float('-inf') or maximum == float('inf'):
            return 'n/a'
        else:
            sigma = np.std(site_data)
            cp_value = float(maximum - minimum) / (6 * sigma)
            return cp_value

    @staticmethod
    def cpk(site_data, minimum, maximum):
        if minimum == float('-inf') or maximum == float('inf'):
            return 'n/a'
        else:
            sigma = np.std(site_data)
            m = np.mean(site_data)
            cpu_value = float(maximum - m) / (3 * sigma)
            cpl_value = float(m - minimum) / (3 * sigma)
            cpk_value = np.min([cpu_value, cpl_value])
            return cpk_value

    # One sided calculations (cpl/cpu)
    @staticmethod
    def cpl(site_data, minimum):
        if minimum == float('-inf'):
            return 'n/a'
        else:
            sigma = np.std(site_data)
            m = np.mean(site_data)
            cpl_value = float(m - minimum) / (3 * sigma)
            return cpl_value

    @staticmethod
    def cpu(site_data, maximum):
        if maximum == float('inf'):
            return 'n/a'
        else:
            sigma = np.std(site_data)
            m = np.mean(site_data)
            cpu_value = float(maximum - m) / (3 * sigma)
            return cpu_value

###################################################


from . import yolo
from . import yolov2
from . import vanilla
from os.path import basename

class framework(object):
    constructor = vanilla.constructor
    loss = vanilla.train.loss

    def __init__(self, meta, FLAGS):
        model = basename(meta['model'])
        model = '.'.join(model.split('.')[:-1])
        meta['name'] = model

        self.constructor(meta, FLAGS)

    def is_inp(self, file_name):
        return True

class YOLO(framework):
    constructor = yolo.constructor
    parse = yolo.data.parse
    shuffle = yolo.data.shuffle
    preprocess = yolo.predict.preprocess
    postprocess = yolo.predict.postprocess
    loss = yolo.train.loss
    is_inp = yolo.misc.is_inp
    profile = yolo.misc.profile
    _batch = yolo.data._batch
    resize_input = yolo.predict.resize_input
    findboxes = yolo.predict.findboxes
    process_box = yolo.predict.process_box

class YOLOv2(framework):
    # 在createFramework的时候，父类会调用此构造器，
    # 进行labels读取，colors分配以及Flags.threshold的覆盖
    constructor = yolo.constructor

    # 以下方法只有被调用时才会执行
    parse = yolo.data.parse
    shuffle = yolov2.data.shuffle
    preprocess = yolo.predict.preprocess
    loss = yolov2.train.loss
    is_inp = yolo.misc.is_inp
    postprocess = yolov2.predict.postprocess
    _batch = yolov2.data._batch
    resize_input = yolo.predict.resize_input
    findboxes = yolov2.predict.findboxes
    process_box = yolo.predict.process_box

"""
framework factory
"""

types = {
    '[detection]': YOLO,
    '[region]': YOLOv2
}

def create_framework(meta, FLAGS):
    '''
    if model is yolov1,then net_type is '[detection]'
    if model is yolov2,then net_type is '[region]'
    :param meta:
    :param FLAGS:
    :return:
    '''
    net_type = meta['type']
    this = types.get(net_type, framework)
    return this(meta, FLAGS)


special_characters1 = '"!#$%&*()-+,/"|" "./\''
special_characters2 = ':;<>=?@'
special_characters3 = '[]\\^_`'
special_characters4 = '{}|~'
numeric_characters = '0123456789'

class CipherCeasarSalad:

    def cipher(text, shiftBy):
        text1 = text.split("\n")
        final_result = []
        for text in text1:
            result = ""
            for i in range(len(text)):
                char = text[i]
                if char in special_characters1:
                    result += chr((ord(char) + shiftBy - 32) % 16 + 32)
                elif char in special_characters2:
                    result += chr((ord(char) + shiftBy - 58) % 7 + 58)
                elif char in special_characters3:
                    result += chr((ord(char) + shiftBy - 91) % 6 + 91)
                elif char in special_characters4:
                    result += chr((ord(char) + shiftBy - 123) % 4 + 123)
                elif char in numeric_characters:
                    result += chr((ord(char) + shiftBy - 48) % 10 + 48)
                elif char.isupper():
                    result += chr((ord(char) + shiftBy - 65) % 26 + 65)
                else:
                    result += chr((ord(char) + shiftBy - 97) % 26 + 97)
            final_result.append(result)
        final_ciper_text = "\n".join(final_result)
        return final_ciper_text

    def decipher(text, shiftBy):
        text1 = text.split("\n")
        final_result = []
        for text in text1:
            result = ""
            for i in range(len(text)):
                char = text[i]
                if char in special_characters1:
                    result += chr((ord(char) - shiftBy - 32) % 16 + 32)
                elif char in special_characters2:
                    result += chr((ord(char) - shiftBy - 58) % 7 + 58)
                elif char in special_characters3:
                    result += chr((ord(char) - shiftBy - 91) % 6 + 91)
                elif char in special_characters4:
                    result += chr((ord(char) - shiftBy - 123) % 4 + 123)
                elif char in numeric_characters:
                    result += chr((ord(char) - shiftBy - 48) % 10 + 48)
                elif char.isupper():
                    result += chr((ord(char) - shiftBy - 65) % 26 + 65)
                else:
                    result += chr((ord(char) - shiftBy - 97) % 26 + 97)
            final_result.append(result)
        final_deciper_text = "\n".join(final_result)
        return final_deciper_text

if __name__ == '__main__':
    print("Enter/Paste your text (can be multiline)..")
    text = []
    while True:
        line = input()
        if not line:
            con = str(input("Do you want to continue writing (Enter yes, if want to continue): "))
            y = {"y", "yes", "Yes", "YES", "Y"}
            if con in y:
                continue
            else:
                break
        else:
            text.append(line)
    text = '\n'.join(text)
    shift = int(input("shift by.."))
    multilineCiperText = CipherCeasarSalad.cipher(text, shift)
    cip = multilineCiperText.split("\n")
    ciperText = "%$#".join(cip)
    print("Ciper Text: %s" % ciperText)
    deciperTest = CipherCeasarSalad.decipher(multilineCiperText, shift)
    print("DeCiper Text: %s" % deciperTest)


import SFRSeg
import FANet
import DFANet
import FastScnn
import ICNet

from tensorflow import keras
import tensorflow as tf
import cityscapesscripts.helpers.labels as labels
import numpy as np
import PIL
import argparse
import sys
import os
import glob
import time

gpus = tf.config.list_physical_devices('GPU')
# Set Memory Growth to alleviate memory issues
for gpu in gpus:
    tf.config.experimental.set_memory_growth(gpu, True)
    tf.config.set_visible_devices(gpus, 'GPU')

#----------IMPORT MODELS AND UTILITIES----------#

#----------CONSTANTS----------#

CITYSCAPES_LABELS = [
    label for label in labels.labels if -1 < label.trainId < 255]
# Add unlabeled
#CITYSCAPES_LABELS.append(labels.labels[0])
# Get the IDS
CITYSCAPES_IDS = [label.id for label in CITYSCAPES_LABELS]

# Get the TRAINIDS
CITYSCAPES_TRAINIDS = [label.trainId for label in CITYSCAPES_LABELS]

# Get all the colors
CITYSCAPES_COLORS = [label.color for label in CITYSCAPES_LABELS]
CLASSES = 19

#----------ARGUMENTS----------#
parser = argparse.ArgumentParser(
    prog='predict', description="Generate predictions from a batch of images from cityscapes")
parser.add_argument(
    "-m", "--model",
    help="Specify the model you wish to use: OPTIONS: SFRSeg, FANet, DFAnet, ICNet",
    choices=['SFRSeg', 'FANet', 'DFANet',
             'fastscnn', 'ICNet', 'ContextNet', 'STDC1_Seg', 'STDC2_Seg', 'BiseNetV2'],
    required=True)
parser.add_argument(
    "-w", "--weights",
    help="Specify the weights path",
    type=str)
parser.add_argument(
    '-p', "--path",
    help="Specify the root folder for cityscapes dataset, if not used looks for CITYSCAPES_DATASET environment variable",
    type=str)
parser.add_argument(
    '-r', "--results-path",
    help="Specify the path for results",
    type=str)
parser.add_argument(
    '-c', "--coarse",
    help="Use the coarse images", action="store_true")
parser.add_argument(
    '-t', "--target-size",
    help="Set the image size for training, should be a elements of a tuple x,y,c",
    default=(1024, 2048, 3),
    type=tuple)
parser.add_argument(
    '--backbone',
    help="The backbone for the deeplabv3+ model",
    choices=['mobilenetv2', 'xception']
)

args = parser.parse_args()
# Get model_name
model_name = args.model
# Check the CITYSCAPES_ROOT path
if os.path.isdir(args.path):
    CITYSCAPES_ROOT = args.path
elif 'CITYSCAPES_DATASET' in os.environ:
    CITYSCAPES_ROOT = os.environ.get('CITYSCAPES_DATASET')
else:
    parser.error("ERROR: No valid path for Cityscapes Dataset given")
# Now do the target size
if args.target_size is None:
    target_size = (1024, 2048, 3)
else:
    target_size = args.target_size
# Check the path of weights
if not os.path.isfile(args.weights):
    parser.error("ERROR: Weights File not found.")
# Ensure results dir is made
if args.results_path is not None:
    if not os.path.isdir(args.results_path):
        os.makedirs(args.results_path)
else:
    parser.error("No results path specified")

files = datasets.get_cityscapes_files(CITYSCAPES_ROOT, 'leftImg8bit', 'test', 'leftImg8bit')

# Get the model
if model_name == 'SFRSeg':
    model = SFRSeg.model(input_size=target_size, num_classes=CLASSES)
if model_name == 'FANet':
    model = FANet.model(input_size=target_size, num_classes=CLASSES)
elif model_name == 'DFANet':
    model = DFANet.model(num_classes=CLASSES, input_size=target_size)
elif model_name == 'ICNet':
    model = ICNet.model(input_size=target_size, num_classes=CLASSES)
elif model_name == 'ContextNet':
    model = ContextNet.model(input_size=target_size, num_classes=CLASSES)
elif model_name == 'FastScnn':
    model = FastScnn.model(input_size=target_size, num_classes=CLASSES)
elif model_name == 'STDC1_Seg':
    model = STDC1_Seg.model(input_size=target_size, num_classes=CLASSES)
elif model_name == 'STDC2_Seg':
    model = STDC2_Seg.model(input_size=target_size, num_classes=CLASSES)
elif model_name == 'BiseNetV2':
    model = BiseNetV2.model(input_size=target_size, num_classes=CLASSES)

# Load the weights
model.load_weights(args.weights)
total = 0
elapsed_times = []
for image_path in files:
    image_string = tf.io.read_file(image_path)
    image = tf.image.decode_png(image_string, channels=3)
    image = tf.image.convert_image_dtype(image, tf.float64)
    image = tf.image.resize(image, (args.target_size[0], args.target_size[1]), method='nearest')
    image = tf.expand_dims(image, 0)

    prediction = model.predict(image)

    prediction = tf.reshape(tf.argmax(prediction, axis=-1),
                            (target_size[0], target_size[1], 1))
    prediction = np.matmul(tf.one_hot(prediction, CLASSES), CITYSCAPES_IDS)
    #if model is trained with different size and want to genarate 1024x2048 size output
    #prediction = tf.image.resize(prediction, (1024, 2048), method='nearest')

    semantic_map = []
    for id in CITYSCAPES_TRAINIDS:
      class_map = tf.reduce_all(tf.equal(prediction, id), axis=-1)
      semantic_map.append(class_map)
    # Save the Image
    semantic_map = tf.stack(semantic_map, axis=-1)
    semantic_map = tf.cast(semantic_map, tf.float32)
    prediction = semantic_map.numpy()
    prediction = np.matmul(prediction, CITYSCAPES_COLORS)

    pil = tf.keras.preprocessing.image.array_to_img(
        prediction, data_format="channels_last", scale=False)
    pil.save(os.path.join(args.results_path, os.path.basename(image_path)))


from tkinter import Canvas, Tk
#from tkinter import * # For Python 3.2.3 and higher.
root = Tk()
root.title('Une courbe')

cw = 250 # canvas width
ch = 200 # canvas height
canvas_1 = Canvas(root, width=cw, height=ch, background="pink")
canvas_1.grid(row=0, column=1)

x1 = 50
y1 = 10
x2 = 50
y2 = 180
x3 = 180
y3 = 180

canvas_1.create_line(x1,y1, x2,y2, x3,y3, smooth="true", width= 2)

root.mainloop()

Tkinter/IV - Les canevas/17 - Dessiner une Courbe/index.py


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Feb 20 21:19:54 2020

@author: franklinvelasquezfuentes

Herarchical Clustering
"""

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

# Importing the mall dataset with pandas

dataset = pd.read_csv("Mall_Customers.csv")

X = dataset.iloc[:,[3,4]].values

# Using the dendogram to find the optimal number of clusters

import scipy.cluster.hierarchy as sch

# linkage is the algorithm of hierarchy, 'ward' is a method that try to minimize the variance between clusters
dendogram = sch.dendrogram(sch.linkage(X, method='ward'))

plt.title("Dendogram")
plt.xlabel("Customers")
plt.ylabel("Euclidian distances")
plt.show()

"""
    Based on the picture we can see that the optimal number of clusters is 5
"""

# Fitting hierarchical clustering to the mall dataset. We will use agglomerative clustering

from sklearn.cluster import AgglomerativeClustering

ch = AgglomerativeClustering(n_clusters=5,affinity='euclidean',linkage='ward')

# this array show to wich number of cluster belongs each row in X
y_ch = ch.fit_predict(X)

# Visualising the clusters

# we want first and second column of X where the cluster is equal to number 0
plt.scatter(X[y_ch==0,0],X[y_ch==0,1], s = 100 , c = 'red' , label = 'Cluster 1 -> Careful Clients')
# we do the same for the rest of clusters
plt.scatter(X[y_ch==1,0],X[y_ch==1,1], s = 100 , c = 'blue' , label = 'Cluster 2 -> Standard')
plt.scatter(X[y_ch==2,0],X[y_ch==2,1], s = 100 , c = 'green' , label = 'Cluster 3 -> Target')
plt.scatter(X[y_ch==3,0],X[y_ch==3,1], s = 100 , c = 'cyan' , label = 'Cluster 4  -> Careless')
plt.scatter(X[y_ch==4,0],X[y_ch==4,1], s = 100 , c = 'magenta' , label = 'Cluster 5 -> Sensible')

plt.title("Clusters de Clientes - (Herarchical Clustering)")
plt.xlabel("Ingreso Anual (k$) ")
plt.ylabel("Spending Score (1-100)")
plt.legend()

plt.show()

Part 4 - Clustering/Section 25 - Hierarchical Clustering/herarchical_clustering.py


#!/usr/bin/env python3

import rospy
from marine_acoustic_msgs.msg import RawSonarImage, SonarImageData
import sys
import math
import kongsberg_watercolumn
import time
import struct

rospy.init_node('kongsberg_watercolumn', sys.argv)

publisher = rospy.Publisher('watercolumn', RawSonarImage, queue_size=10)

data_directory = '/home/field/data/mnt/surveypc/sisdata/raw/NA155_DX082303_NautilusTechChallenge/'
data_directory = rospy.get_param('~data_directory', data_directory)

def processWatercolumn(wc):
  print('ping!', wc['header']['dgdatetime'])
  message = RawSonarImage()
  message.header.frame_id = 'project11/drix_8/em712'
  message.header.stamp = rospy.Time.from_seconds(wc['header']['dgdatetime'].timestamp())

  i = 0
  nadir_beam_angle = None
  nadir_beam_index = None
  for beam in wc['beamData']:
    beam_angle = beam['beamPointAngReVertical_deg']
    if nadir_beam_angle is None or abs(beam_angle) < abs(nadir_beam_angle):
      nadir_beam_angle = beam_angle
      nadir_beam_index = i
    i += 1
  print(nadir_beam_index, nadir_beam_angle)
  beam = wc['beamData'][nadir_beam_index]
  sector_number = beam['beamTxSectorNum']
  sector = None
  for sector_data in wc['sectorData']:
    if sector_data['txSectorNum'] == sector_number:
      sector = sector_data
      break
  message.ping_info.frequency = sector['centreFreq_Hz']
  message.ping_info.tx_beamwidths.append(math.radians(sector['txBeamWidthAlong_deg']))
  message.ping_info.sound_speed = wc['rxInfo']['soundVelocity_mPerSec']
  message.sample_rate = wc['rxInfo']['sampleFreq_Hz']
  message.samples_per_beam = beam['numSampleData']
  message.sample0 = beam['startRangeSampleNum']
  message.image.beam_count = 1
  message.image.dtype = SonarImageData.DTYPE_FLOAT32

  samples = []
  for sample in beam['sampleAmplitude05dB_p']:
    samples.append(sample*0.5)

  message.image.data = struct.pack('<'+str(len(samples))+'f', *samples)

  publisher.publish(message)
  time.sleep(0.1)

watcher = kongsberg_watercolumn.kongsberg_watercolumn.FilesystemWatcher(data_directory)

def checkForNewPingsCallback(event):
  pings = watcher.getNewPings()
  for ping in pings:
    processWatercolumn(ping)

rospy.Timer( rospy.Duration(1.0), checkForNewPingsCallback)

rospy.spin()


import sys
input = sys.stdin.readline

def check_available(dist):
    last = arr[0]
    count = 1
    for i in range(1, N):
        if arr[i] >= last+dist:
            count += 1
            last = arr[i]
    if count < C:
        return False
    else :
        return True

def upper_bound():
    left = 1
    right = arr[-1] - arr[0]
    result = 0
    while left <= right:
        mid = (left + right) // 2
        if check_available(mid):
            left = mid + 1
            result = mid
        else :
            right = mid - 1
    return result

N, C = map(int,input().split())
arr = [int(input()) for _ in range(N)]
arr.sort()
print(upper_bound())


import numpy as np
import pandas as pd
from scipy import stats
from sklearn.model_selection import train_test_split

class NB_Gaussian():
    def __init__(self, training_df, test_df, p_class):
        self.training_df = training_df
        self.test_df = test_df
        self.p_class = p_class
        # training df grouped by classes, with calculated means and standard deviation
        self.aggs = self.training_df.groupby(self.p_class).agg(['mean', 'std'])

    # P(A|C) - for single example (with distinct feature from distinct group)
    # from gauss formula
    def cond_prob(self, row, feature, group):
        mu = self.aggs[feature]['mean'][group]  # mean
        std = self.aggs[feature]['std'][group]  # standard deviation

        # one selected observation (sigle value)
        selected_observ = self.test_df.loc[self.test_df['id'] == row][feature].values[0]

        prob = stats.norm.pdf(selected_observ, loc=mu, scale=std)

        return prob

    def predict_class(self, row):
        classes = []

        min_class = np.amin(self.training_df[self.p_class].values)
        max_class = np.amax(self.training_df[self.p_class].values)

        for group in range(min_class, max_class):
            # P(C)
            class_amount = len(self.training_df[self.training_df[self.p_class] == group])

            prob = class_amount/len(self.training_df.index)
            for feature in self.training_df.columns:
                if feature == self.p_class:
                    break
                # P(C) * P(A|C)
                prob *= self.cond_prob(row, feature, group)
            classes.append(prob)

        return np.argmax(classes) + min_class  # from what number classes starts

    def predict(self):
        values = dict()
        for index, row in self.test_df.iterrows():
            values[index] = self.predict_class(index)
        return values

    def check_result(self):
        res = {'True': 0, 'False': 0}
        predicted_val = self.predict()
        for index, row in self.test_df.iterrows():
            if self.test_df.at[index, self.p_class] == predicted_val[index]:
                res['True'] += 1
            else:
                res['False'] += 1
        return res

def split_set(df, ratio):
    train_df, test_df = train_test_split(df, train_size=ratio)
    return train_df, test_df

def cross_validation(df, k, p_class):
    shuffled = df.sample(frac=1)
    parts = np.array_split(shuffled, k)
    results = []

    for n in range(0, k):
        test_set = parts[n]
        rest = parts.copy()
        rest.pop(n)
        train_set = pd.concat(parts, ignore_index=True)
        gauss_classifier = NB_Gaussian(train_set, test_set, p_class)
        res = gauss_classifier.check_result()
        results.append(res)

    return results

def count_efficiency(results):
    good = 0
    all = 0
    for res in results:
        good += res['True']
        all += res['True'] + res['False']

    return f'well predicted: {good}\nfalsely predicted: {all-good}\nefficiency: {(good/all)*100}%'

if __name__ == "__main__":
    df = pd.read_csv('winequality-red.csv', sep=";")
    i = 0
    for index, row in df.iterrows():
        df.at[index, 'id'] = i
        i += 1
    p_class = 'quality'
    results = []

    print('Use cross validation? [y]/[n]')
    isCrossVal = input()
    if isCrossVal == 'y':
        print('put parameter k: ')
        k = int(input())
        results = cross_validation(df, k, p_class)
    else:
        print('put fraction of training set: ')
        ratio = float(input())
        if ratio >= 1:
            raise ValueError('Ratio should be less than 1')
        train_df, test_df = split_set(df, ratio)
        gauss_classifier = NB_Gaussian(train_df, test_df, p_class)
        results = [gauss_classifier.check_result()]

    print(results)
    print(count_efficiency(results))