text:
import time
import importlib

import dash
import dash_core_components as dcc
import dash_html_components as html
import numpy as np
from dash.dependencies import Input, Output, State
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn import datasets
from sklearn.svm import SVC
from sklearn.linear_model import LogisticRegression
from sklearn.discriminant_analysis import (
    LinearDiscriminantAnalysis,
    QuadraticDiscriminantAnalysis,
)
from sklearn.neural_network import MLPClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import (
    RandomForestClassifier,
    AdaBoostClassifier,
    GradientBoostingClassifier,
)
from sklearn.neighbors import KNeighborsClassifier

import utils.dash_reusable_components as drc
import utils.figures as figs

app = dash.Dash(
    __name__,
    meta_tags=[
        {"name": "viewport", "content": "width=device-width, initial-scale=1.0"}
    ],
)
app.config["suppress_callback_exceptions"] = True
server = app.server

def generate_data(n_samples, dataset, noise):
    if dataset == "moons":
        return datasets.make_moons(n_samples=n_samples, noise=noise, random_state=0)

    elif dataset == "circles":
        return datasets.make_circles(
            n_samples=n_samples, noise=noise, factor=0.5, random_state=1
        )

    elif dataset == "linear":
        n_pos = n_samples // 2
        n_neg = n_samples - n_pos

        X = np.vstack((np.linspace(-0.8, 1.2, n_pos), np.linspace(-2.4, 0.0, n_pos))).T
        X = np.vstack(
            (X, np.vstack((np.linspace(-0.5, 1, n_neg), np.linspace(-1, 1, n_neg))).T)
        )
        y = np.hstack((np.ones(n_pos), np.zeros(n_neg)))

        # rng = np.random.RandomState(2)
        X += noise * np.random.uniform(size=X.shape)
        linearly_separable = (X, y)

        return linearly_separable

    elif dataset == "gaussians":
        X, y = datasets.make_classification(
            n_samples=n_samples,
            n_features=2,
            n_redundant=0,
            n_informative=2,
            random_state=2,
            n_clusters_per_class=1,
        )

        # rng = np.random.RandomState(9)
        X += noise * np.random.uniform(size=X.shape)
        two_gaussians = (X, y)

        return two_gaussians

    else:
        raise ValueError(
            "Data type incorrectly specified. Please choose an existing dataset."
        )

app.layout = html.Div(
    children=[
        # .container class is fixed, .container.scalable is scalable
        html.Div(
            className="banner",
            children=[
                # Change App Name here
                html.Div(
                    className="container scalable",
                    children=[
                        # Change App Name here
                        html.Img(
                            src=app.get_asset_url("visualizerlogo.png"),
                            style={
                                "width": "auto",
                                "height": "auto",
                                "max-width": "60px",
                                "max-height": "60px",
                                "float": "left",
                                "margin-top": "17px",
                                "padding-bottom": "10px",
                                "padding-right": "20px",
                            },
                        ),
                        html.H2(
                            id="banner-title",
                            children=[
                                html.A(
                                    "Decision Boundary Visualizer",
                                    href="https://github.com/sagnibak/ml-visualizer",
                                    style={
                                        "text-decoration": "none",
                                        "color": "inherit",
                                    },
                                ),
                            ],
                        ),
                        html.A(
                            id="attribution-logo",
                            children=[
                                html.Img(src=app.get_asset_url("dash-logo-new.png")),
                                html.H3("Made with"),
                            ],
                            href="https://plot.ly/products/dash/",
                        ),
                    ],
                )
            ],
        ),
        html.Div(
            id="body",
            className="container scalable",
            children=[
                html.Div(
                    id="app-container",
                    # className="row",
                    children=[
                        html.Div(
                            # className="three columns",
                            id="left-column",
                            children=[
                                drc.Card(
                                    id="first-card",
                                    children=[
                                        drc.NamedDropdown(
                                            name="Select Model",
                                            id="dropdown-select-model",
                                            options=[
                                                {
                                                    "label": "Support Vector Machine",
                                                    "value": "SVM",
                                                },
                                                {
                                                    "label": "Logistic Regression",
                                                    "value": "LogReg",
                                                },
                                                {
                                                    "label": "Linear Discriminant Analysis",
                                                    "value": "LDA",
                                                },
                                                {
                                                    "label": "Quadratic Discriminant Analysis",
                                                    "value": "QDA",
                                                },
                                                {
                                                    "label": "Multilayer Perceptron",
                                                    "value": "MLP",
                                                },
                                                {
                                                    "label": "Decision Tree",
                                                    "value": "DTree",
                                                },
                                                {
                                                    "label": "Random Forest",
                                                    "value": "RForest",
                                                },
                                                {
                                                    "label": "AdaBoost",
                                                    "value": "ABoost",
                                                },
                                                {
                                                    "label": "XGBoost",
                                                    "value": "XGBoost",
                                                },
                                                {
                                                    "label": "k Nearest Neighbors",
                                                    "value": "kNN",
                                                },
                                            ],
                                            clearable=False,
                                            searchable=True,
                                            value="SVM",
                                        ),
                                        drc.NamedDropdown(
                                            name="Select Dataset",
                                            id="dropdown-select-dataset",
                                            options=[
                                                {"label": "Moons", "value": "moons"},
                                                {
                                                    "label": "Linearly Separable",
                                                    "value": "linear",
                                                },
                                                {
                                                    "label": "Circles",
                                                    "value": "circles",
                                                },
                                                {
                                                    "label": "Two Gaussians",
                                                    "value": "gaussians",
                                                },
                                            ],
                                            clearable=False,
                                            searchable=False,
                                            value="moons",
                                        ),
                                        drc.NamedSlider(
                                            name="Sample Size",
                                            id="slider-dataset-sample-size",
                                            min=100,
                                            max=500,
                                            step=100,
                                            marks={
                                                str(i): str(i)
                                                for i in [100, 200, 300, 400, 500]
                                            },
                                            value=300,
                                        ),
                                        drc.NamedSlider(
                                            name="Noise Level",
                                            id="slider-dataset-noise-level",
                                            min=0,
                                            max=1,
                                            marks={
                                                i / 10: str(i / 10)
                                                for i in range(0, 11, 2)
                                            },
                                            step=0.1,
                                            value=0.2,
                                        ),
                                    ],
                                ),
                                drc.Card(
                                    id="button-card",
                                    children=[
                                        drc.NamedSlider(
                                            name="Threshold",
                                            id="slider-threshold",
                                            min=0,
                                            max=1,
                                            value=0.5,
                                            step=0.01,
                                        ),
                                        html.Button(
                                            "Reset Threshold",
                                            id="button-zero-threshold",
                                        ),
                                    ],
                                ),
                                drc.Card(
                                    id="last-card",
                                    children=[
                                        html.Div(
                                            id="svm-params",
                                            children=[
                                                drc.NamedDropdown(
                                                    name="Kernel",
                                                    id="dropdown-svm-parameter-kernel",
                                                    options=[
                                                        {
                                                            "label": "Radial basis function (RBF)",
                                                            "value": "rbf",
                                                        },
                                                        {
                                                            "label": "Linear",
                                                            "value": "linear",
                                                        },
                                                        {
                                                            "label": "Polynomial",
                                                            "value": "poly",
                                                        },
                                                        {
                                                            "label": "Sigmoid",
                                                            "value": "sigmoid",
                                                        },
                                                    ],
                                                    value="rbf",
                                                    clearable=False,
                                                    searchable=False,
                                                ),
                                                drc.NamedSlider(
                                                    name="Cost (C) for Slack",
                                                    id="slider-svm-parameter-C-power",
                                                    min=-2,
                                                    max=4,
                                                    value=0,
                                                    marks={
                                                        i: "{}".format(10 ** i)
                                                        for i in range(-2, 5)
                                                    },
                                                ),
                                                drc.FormattedSlider(
                                                    id="slider-svm-parameter-C-coef",
                                                    min=1,
                                                    max=9,
                                                    value=1,
                                                ),
                                                drc.NamedSlider(
                                                    name="Degree",
                                                    id="slider-svm-parameter-degree",
                                                    min=2,
                                                    max=10,
                                                    value=3,
                                                    step=1,
                                                    marks={
                                                        i: str(i)
                                                        for i in range(2, 11, 2)
                                                    },
                                                ),
                                                drc.NamedSlider(
                                                    name="Zero-order Kernel Term",
                                                    id="slider-svm-coef0",
                                                    min=0,
                                                    max=2,
                                                    value=1,
                                                    step=0.01,
                                                    marks={
                                                        i: str(i)
                                                        for i in [0, 0.5, 1, 1.5, 2]
                                                    },
                                                ),
                                                drc.NamedSlider(
                                                    name="Gamma",
                                                    id="slider-svm-parameter-gamma-power",
                                                    min=-5,
                                                    max=0,
                                                    value=-1,
                                                    marks={
                                                        i: "{}".format(10 ** i)
                                                        for i in range(-5, 1)
                                                    },
                                                ),
                                                drc.FormattedSlider(
                                                    id="slider-svm-parameter-gamma-coef",
                                                    min=1,
                                                    max=9,
                                                    value=5,
                                                ),
                                                html.Div(
                                                    id="shrinking-container",
                                                    children=[
                                                        html.P(children="Shrinking"),
                                                        dcc.RadioItems(
                                                            id="radio-svm-parameter-shrinking",
                                                            labelStyle={
                                                                "margin-right": "7px",
                                                                "display": "inline-block",
                                                            },
                                                            options=[
                                                                {
                                                                    "label": " Enabled",
                                                                    "value": "True",
                                                                },
                                                                {
                                                                    "label": " Disabled",
                                                                    "value": "False",
                                                                },
                                                            ],
                                                            value="True",
                                                        ),
                                                    ],
                                                ),
                                            ],
                                        ),
                                        html.Div(
                                            id="logreg-params",
                                            children=[
                                                drc.NamedDropdown(
                                                    name="Regularization Type",
                                                    id="dropdown-logreg-regtype",
                                                    options=[
                                                        {"label": "L1", "value": "l1",},
                                                        {"label": "L2", "value": "l2",},
                                                        {
                                                            "label": "ElasticNet",
                                                            "value": "elasticnet",
                                                        },
                                                        {
                                                            "label": "None",
                                                            "value": "none",
                                                        },
                                                    ],
                                                    value="none",
                                                    clearable=False,
                                                    searchable=True,
                                                ),
                                                drc.NamedSlider(
                                                    name="Cost (C) for Regularization",
                                                    id="slider-logreg-C-power",
                                                    min=-2,
                                                    max=5,
                                                    value=0,
                                                    marks={
                                                        i: "{}".format(10 ** i)
                                                        for i in range(-2, 5)
                                                    },
                                                ),
                                                drc.FormattedSlider(
                                                    id="slider-logreg-C-coef",
                                                    min=1,
                                                    max=9,
                                                    value=1,
                                                ),
                                                drc.NamedSlider(
                                                    name="L1 Contribution (ElasticNet)",
                                                    id="slider-logreg-l1-ratio",
                                                    min=0,
                                                    max=1,
                                                    step=0.01,
                                                    value=0.5,
                                                    marks={
                                                        0: "0.0",
                                                        0.5: "0.5",
                                                        1: "1.0",
                                                    },
                                                ),
                                            ],
                                        ),
                                        html.Div(
                                            id="mlp-params",
                                            children=[
                                                drc.NamedInput(
                                                    name="Hidden layer sizes",
                                                    id="input-mlp-layers",
                                                    type="text",
                                                    value="10, 10",
                                                    placeholder="layer 1, layer 2, ...",
                                                    debounce=True,
                                                    style={"color": "inherit"},
                                                ),
                                                drc.NamedDropdown(
                                                    name="Activation Function",
                                                    id="dropdown-mlp-activation",
                                                    options=[
                                                        {"label": act, "value": act}
                                                        for act in (
                                                            "identity",
                                                            "logistic",
                                                            "tanh",
                                                            "relu",
                                                        )
                                                    ],
                                                    clearable=False,
                                                    searchable=True,
                                                    value="relu",
                                                ),
                                                drc.NamedSlider(
                                                    name="Max Number of Epochs",
                                                    id="slider-mlp-max-iter",
                                                    min=1,
                                                    max=2000,
                                                    value=200,
                                                    marks={
                                                        i: str(i) for i in [1] +
                                                            list(range(0, 2001, 400))
                                                    },
                                                ),
                                                drc.NamedSlider(
                                                    name="Batch Size",
                                                    id="slider-mlp-batch-size",
                                                    min=1,
                                                    included=False,
                                                ),
                                                drc.NamedSlider(
                                                    name="L2 penalty",
                                                    id="slider-mlp-penalty-power",
                                                    min=-2,
                                                    max=5,
                                                    value=0,
                                                    marks={
                                                        i: "{}".format(10 ** i)
                                                        for i in range(-2, 5)
                                                    },
                                                ),
                                                drc.FormattedSlider(
                                                    id="slider-mlp-penalty-coef",
                                                    min=1,
                                                    max=9,
                                                    value=1,
                                                ),
                                            ],
                                        ),
                                        html.Div(
                                            id="dtree-params",
                                            children=[
                                                drc.NamedSlider(
                                                    name="Minimum Leaf Size",
                                                    id="slider-dt-min-leaf",
                                                    min=1,
                                                    max=50,
                                                    value=1,
                                                    marks={
                                                        i: str(i)
                                                        for i in [1]
                                                        + list(range(0, 51, 10))
                                                    },
                                                ),
                                                drc.NamedSlider(
                                                    name="Maximum Depth",
                                                    id="slider-dt-max-depth",
                                                    min=1,
                                                    max=50,
                                                    value=3,
                                                    marks={
                                                        i: str(i)
                                                        for i in [1]
                                                        + list(range(0, 51, 10))
                                                    },
                                                ),
                                            ],
                                        ),
                                        html.Div(
                                            id="rforest-params",
                                            children=[
                                                drc.NamedSlider(
                                                    name="Number of Trees",
                                                    id="slider-rf-n-estim",
                                                    min=1,
                                                    max=300,
                                                    value=50,
                                                    marks={
                                                        i: str(i)
                                                        for i in [1]
                                                        + list(range(0, 301, 50))
                                                    },
                                                ),
                                                drc.NamedSlider(
                                                    name="Minimum Leaf Size",
                                                    id="slider-rf-min-leaf",
                                                    min=1,
                                                    max=50,
                                                    value=1,
                                                    marks={
                                                        i: str(i)
                                                        for i in [1]
                                                        + list(range(0, 51, 10))
                                                    },
                                                ),
                                                drc.NamedSlider(
                                                    name="Maximum Depth of Trees",
                                                    id="slider-rf-max-depth",
                                                    min=1,
                                                    max=50,
                                                    value=3,
                                                    marks={
                                                        i: str(i)
                                                        for i in [1]
                                                        + list(range(0, 51, 10))
                                                    },
                                                ),
                                            ],
                                        ),
                                        html.Div(
                                            id="adaboost-params",
                                            children=[
                                                drc.NamedSlider(
                                                    name="Number of Stumps",
                                                    id="slider-ab-n-estim",
                                                    min=1,
                                                    max=300,
                                                    value=50,
                                                    marks={
                                                        i: str(i)
                                                        for i in [1]
                                                        + list(range(0, 301, 50))
                                                    },
                                                )
                                            ],
                                        ),
                                        html.Div(
                                            id="xgboost-params",
                                            children=[
                                                drc.NamedSlider(
                                                    name="Number of Weak Learners",
                                                    id="slider-xg-n-estim",
                                                    min=1,
                                                    max=300,
                                                    value=50,
                                                    marks={
                                                        i: str(i)
                                                        for i in [1]
                                                        + list(range(0, 301, 50))
                                                    },
                                                ),
                                                drc.NamedSlider(
                                                    name="Minimum Leaf Size",
                                                    id="slider-xg-min-leaf",
                                                    min=1,
                                                    max=50,
                                                    value=1,
                                                    marks={
                                                        i: str(i)
                                                        for i in [1]
                                                        + list(range(0, 51, 10))
                                                    },
                                                ),
                                                drc.NamedSlider(
                                                    name="Maximum Depth of Learners",
                                                    id="slider-xg-max-depth",
                                                    min=1,
                                                    max=50,
                                                    value=3,
                                                    marks={
                                                        i: str(i)
                                                        for i in [1]
                                                        + list(range(0, 51, 10))
                                                    },
                                                ),
                                            ],
                                        ),
                                        html.Div(
                                            id="knn-params",
                                            children=[
                                                drc.NamedSlider(
                                                    name="k",
                                                    id="slider-knn-k",
                                                    min=1,
                                                    max=50,
                                                    value=5,
                                                    marks={
                                                        i: str(i)
                                                        for i in [1]
                                                        + list(range(10, 51, 10))
                                                    },
                                                )
                                            ],
                                        ),
                                    ],
                                ),
                            ],
                        ),
                        html.Div(
                            id="div-graphs",
                            children=dcc.Graph(
                                id="graph-sklearn-svm",
                                figure=dict(
                                    layout=dict(
                                        plot_bgcolor="#282b38", paper_bgcolor="#282b38"
                                    )
                                ),
                            ),
                        ),
                    ],
                )
            ],
        ),
    ]
)

@app.callback(Output("svm-params", "style"), [Input("dropdown-select-model", "value")])
def show_svm_params(model):
    if model == "SVM":
        return {"visibility": "visible"}
    else:
        return {"display": "none"}

@app.callback(
    Output("logreg-params", "style"), [Input("dropdown-select-model", "value")]
)
def show_logreg_params(model):
    if model == "LogReg":
        return {"visibility": "visible"}
    else:
        return {"display": "none"}

@app.callback(Output("mlp-params", "style"), [Input("dropdown-select-model", "value")])
def show_mlp_params(model):
    if model == "MLP":
        return {"visibility": "visible"}
    else:
        return {"display": "none"}

@app.callback(
    Output("dtree-params", "style"), [Input("dropdown-select-model", "value")]
)
def show_dtree_params(model):
    if model == "DTree":
        return {"visibility": "visible"}
    else:
        return {"display": "none"}

@app.callback(
    Output("rforest-params", "style"), [Input("dropdown-select-model", "value")]
)
def show_rforest_params(model):
    if model == "RForest":
        return {"visibility": "visible"}
    else:
        return {"display": "none"}

@app.callback(
    Output("adaboost-params", "style"), [Input("dropdown-select-model", "value")]
)
def show_adaboost_params(model):
    if model == "ABoost":
        return {"visibility": "visible"}
    else:
        return {"display": "none"}

@app.callback(
    Output("xgboost-params", "style"), [Input("dropdown-select-model", "value")]
)
def show_xgboost_params(model):
    if model == "XGBoost":
        return {"visibility": "visible"}
    else:
        return {"display": "none"}

@app.callback(Output("knn-params", "style"), [Input("dropdown-select-model", "value")])
def show_knn_params(model):
    if model == "kNN":
        return {"visibility": "visible"}
    else:
        return {"display": "none"}

@app.callback(
    Output("slider-svm-parameter-gamma-coef", "marks"),
    [Input("slider-svm-parameter-gamma-power", "value")],
)
def update_slider_svm_parameter_gamma_coef(power):
    scale = 10 ** power
    return {i: str(round(i * scale, 8)) for i in range(1, 10, 2)}

@app.callback(
    Output("slider-svm-parameter-C-coef", "marks"),
    [Input("slider-svm-parameter-C-power", "value")],
)
def update_slider_svm_parameter_C_coef(power):
    scale = 10 ** power
    return {i: str(round(i * scale, 8)) for i in range(1, 10, 2)}

@app.callback(
    Output("slider-threshold", "value"), [Input("button-zero-threshold", "n_clicks")],
)
def reset_threshold_center(n_clicks):
    return 0.499

# Disable Sliders if kernel not in the given list
@app.callback(
    Output("slider-svm-parameter-degree", "disabled"),
    [Input("dropdown-svm-parameter-kernel", "value")],
)
def disable_slider_param_degree(kernel):
    return kernel != "poly"

@app.callback(
    Output("slider-svm-coef0", "disabled"),
    [Input("dropdown-svm-parameter-kernel", "value")],
)
def disable_slider_param_degree(kernel):
    return kernel != "poly" and kernel != "sigmoid"

@app.callback(
    Output("slider-svm-parameter-gamma-coef", "disabled"),
    [Input("dropdown-svm-parameter-kernel", "value")],
)
def disable_slider_param_gamma_coef(kernel):
    return kernel not in ["rbf", "poly", "sigmoid"]

@app.callback(
    Output("slider-svm-parameter-gamma-power", "disabled"),
    [Input("dropdown-svm-parameter-kernel", "value")],
)
def disable_slider_param_gamma_power(kernel):
    return kernel not in ["rbf", "poly", "sigmoid"]

@app.callback(
    Output("slider-logreg-C-coef", "marks"), [Input("slider-logreg-C-power", "value")],
)
def update_slider_logreg_C_coef(power):
    scale = 10 ** power
    return {i: str(round(i * scale, 8)) for i in range(1, 10, 2)}

# disable elasticNet slider if other regularization
@app.callback(
    Output("slider-logreg-l1-ratio", "disabled"),
    [Input("dropdown-logreg-regtype", "value")],
)
def disable_slider_logreg_l1_ratio(reg_type):
    return reg_type != "elasticnet"

# set max, value, and marks for batch size slider for MLP
@app.callback(
    Output("slider-mlp-batch-size", "max"),
    [Input("slider-dataset-sample-size", "value")],
)
def update_mlp_batch_size_max(sample_size):
    return sample_size

@app.callback(
    Output("slider-mlp-batch-size", "value"),
    [Input("slider-dataset-sample-size", "value")],
)
def update_mlp_batch_size_value(sample_size):
    return sample_size // 2

@app.callback(
    Output("slider-mlp-batch-size", "marks"),
    [Input("slider-dataset-sample-size", "value")],
)
def update_mlp_batch_size_marks(sample_size):
    return {1: "1", sample_size: str(sample_size)}

@app.callback(
    Output("slider-mlp-penalty-coef", "marks"),
    [Input("slider-mlp-penalty-power", "value")],
)
def update_slider_mlp_penalty_coef(power):
    scale = 10 ** power
    return {i: str(round(i * scale, 8)) for i in range(1, 10, 2)}

@app.callback(
    Output("div-graphs", "children"),
    [
        Input("dropdown-select-model", "value"),
        Input("dropdown-svm-parameter-kernel", "value"),
        Input("slider-svm-parameter-degree", "value"),
        Input("slider-svm-parameter-C-coef", "value"),
        Input("slider-svm-parameter-C-power", "value"),
        Input("slider-svm-parameter-gamma-coef", "value"),
        Input("slider-svm-parameter-gamma-power", "value"),
        Input("dropdown-select-dataset", "value"),
        Input("slider-dataset-noise-level", "value"),
        Input("radio-svm-parameter-shrinking", "value"),
        Input("slider-threshold", "value"),
        Input("slider-dataset-sample-size", "value"),
        Input("slider-svm-coef0", "value"),
        Input("dropdown-logreg-regtype", "value"),
        Input("slider-logreg-C-coef", "value"),
        Input("slider-logreg-C-power", "value"),
        Input("slider-logreg-l1-ratio", "value"),
        Input("input-mlp-layers", "value"),
        Input("dropdown-mlp-activation", "value"),
        Input("slider-mlp-max-iter", "value"),
        Input("slider-mlp-batch-size", "value"),
        Input("slider-mlp-penalty-coef", "value"),
        Input("slider-mlp-penalty-power", "value"),
        Input("slider-dt-min-leaf", "value"),
        Input("slider-dt-max-depth", "value"),
        Input("slider-rf-n-estim", "value"),
        Input("slider-rf-min-leaf", "value"),
        Input("slider-rf-max-depth", "value"),
        Input("slider-ab-n-estim", "value"),
        Input("slider-xg-n-estim", "value"),
        Input("slider-xg-min-leaf", "value"),
        Input("slider-xg-max-depth", "value"),
        Input("slider-knn-k", "value"),
    ],
)
def update_svm_graph(
    model,
    kernel,
    degree,
    C_coef,
    C_power,
    gamma_coef,
    gamma_power,
    dataset,
    noise,
    shrinking,
    threshold,
    sample_size,
    svm_coef0,
    logreg_reg_type,
    logreg_C_coef,
    logreg_C_power,
    logreg_l1_ratio,
    mlp_layers,
    mlp_activation,
    mlp_max_iter,
    mlp_batch_size,
    mlp_l2_coef,
    mlp_l2_pow,
    dt_min_leaf_size,
    dt_max_depth,
    rf_n_estimators,
    rf_min_leaf_size,
    rf_max_depth,
    aboost_n_estimators,
    xg_n_estimators,
    xg_min_leaf_size,
    xg_max_depth,
    knn_k,
):
    h = 0.3  # step size in the mesh

    # Data Pre-processing
    X, y = generate_data(n_samples=sample_size, dataset=dataset, noise=noise)
    X = StandardScaler().fit_transform(X)
    X_train, X_test, y_train, y_test = train_test_split(
        X, y, test_size=0.4, random_state=42
    )

    x_min = X[:, 0].min() - 1.5
    x_max = X[:, 0].max() + 1.5
    y_min = X[:, 1].min() - 1.5
    y_max = X[:, 1].max() + 1.5
    xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))

    if model == "SVM":
        C = C_coef * 10 ** C_power
        gamma = gamma_coef * 10 ** gamma_power

        if shrinking == "True":
            flag = True
        else:
            flag = False

        clf = SVC(
            C=C,
            kernel=kernel,
            degree=degree,
            gamma=gamma,
            shrinking=flag,
            probability=True,
            coef0=svm_coef0
        )

    elif model == "LogReg":
        C = logreg_C_coef * 10 ** logreg_C_power

        if logreg_reg_type == "none" or logreg_reg_type == "elasticnet":
            solver = "saga"
        else:
            solver = "liblinear"

        clf = LogisticRegression(
            penalty=logreg_reg_type, C=C, l1_ratio=logreg_l1_ratio, solver=solver
        )

    elif model == "LDA":
        clf = LinearDiscriminantAnalysis()

    elif model == "QDA":
        clf = QuadraticDiscriminantAnalysis()

    elif model == "MLP":
        hidden_layers = tuple(map(int, mlp_layers.split(", ")))
        l2_penalty = mlp_l2_coef * 10 ** mlp_l2_pow

        clf = MLPClassifier(
            hidden_layer_sizes=hidden_layers,
            activation=mlp_activation,
            batch_size=mlp_batch_size,
            alpha=l2_penalty,
            max_iter=mlp_max_iter
        )

    elif model == "DTree":
        clf = DecisionTreeClassifier(
            min_samples_leaf=dt_min_leaf_size, max_depth=dt_max_depth
        )

    elif model == "RForest":
        clf = RandomForestClassifier(
            n_estimators=rf_n_estimators,
            min_samples_leaf=rf_min_leaf_size,
            max_depth=rf_max_depth,
        )

    elif model == "ABoost":
        clf = AdaBoostClassifier(n_estimators=aboost_n_estimators)

    elif model == "XGBoost":
        clf = GradientBoostingClassifier(
            n_estimators=xg_n_estimators,
            min_samples_leaf=xg_min_leaf_size,
            max_depth=xg_max_depth,
        )

    elif model == "kNN":
        clf = KNeighborsClassifier(n_neighbors=knn_k)

    else:
        raise ValueError(f"Unsupported model: {model}")
    clf.fit(X_train, y_train)

    # Plot the decision boundary. For that, we will assign a color to each
    # point in the mesh [x_min, x_max]x[y_min, y_max].
    Z = clf.predict_proba(np.c_[xx.ravel(), yy.ravel()])[:, 1]

    prediction_figure = figs.serve_prediction_plot(
        model=clf,
        X_train=X_train,
        X_test=X_test,
        y_train=y_train,
        y_test=y_test,
        Z=Z,
        xx=xx,
        yy=yy,
        mesh_step=h,
        threshold=threshold,
    )

    roc_figure = figs.serve_roc_curve(model=clf, X_test=X_test, y_test=y_test)

    confusion_figure = figs.serve_pie_confusion_matrix(
        model=clf, X_test=X_test, y_test=y_test, Z=Z, threshold=threshold
    )

    return [
        html.Div(
            id="svm-graph-container",
            children=dcc.Loading(
                className="graph-wrapper",
                children=dcc.Graph(id="graph-sklearn-svm", figure=prediction_figure),
                style={"display": "none"},
            ),
        ),
        html.Div(
            id="graphs-container",
            children=[
                dcc.Loading(
                    className="graph-wrapper",
                    children=dcc.Graph(id="graph-line-roc-curve", figure=roc_figure),
                ),
                dcc.Loading(
                    className="graph-wrapper",
                    children=dcc.Graph(
                        id="graph-pie-confusion-matrix", figure=confusion_figure
                    ),
                ),
            ],
        ),
    ]

# Running the server
if __name__ == "__main__":
    app.run_server(debug=True)

ext: py

max_stars_repo_name: haoruilee/ml-visualizer

max_issues_repo_name: haoruilee/ml-visualizer

max_forks_repo_name: haoruilee/ml-visualizer

reason: import numpy


text:
import numpy as np
import itertools
from math import sqrt

def read_vectors(vecfile):
    vectors = {}
    with open(vecfile) as f:
        veclines=f.read().splitlines()

    #Make dictionary with key=row, value=vector
    for l in veclines:
        items=l.split()
        row=items[0]
        vec=[float(i) for i in items[1:]]
        vec=np.array(vec)
        vectors[row]=vec
    return vectors

def read_dimensions(dimfile):
    with open(dimfile) as f:
        dimensions=f.read().splitlines()
    return dimensions[0].split()

def cosine_similarity(v1, v2):
    if len(v1) != len(v2):
        raise ValueError("Vectors must be of same length")
    num = np.dot(v1, v2)
    den_a = np.dot(v1, v1)
    den_b = np.dot(v2, v2)
    return num / (sqrt(den_a) * sqrt(den_b))

def coherence(sentence, vectors):
    cosines = []
    words = sentence.split()
    if len(words) > 2:
        for w1,w2 in itertools.combinations(words,2):
            if w1 != w2 and w1 in vectors and w2 in vectors:
                cos = cosine_similarity(vectors[w1],vectors[w2])
                #print(w1,w2,cos)
                cosines.append(cos)
    if len(cosines) > 0:
        return sum(cosines) / len(cosines)
    else:
        return 0

max_stars_repo_name: minimalparts/PoetrySemantics

max_issues_repo_name: minimalparts/PoetrySemantics

max_forks_repo_name: minimalparts/PoetrySemantics


text:
'''
Created on Jan 16, 2017

@author: Alexandre Day
'''

import numpy as np
from matplotlib import pyplot as plt
import matplotlib.patheffects as PathEffects
from .mycolors import COLOR_PALETTE
from .fdc import FDC
import math

def set_nice_font(size = 18, usetex=False):
    font = {'family' : 'serif', 'size'   : size}
    plt.rc('font', **font)
    if usetex is True:
        plt.rc('text', usetex=True)

def density_map(X, z,
                xlabel=None, ylabel=None, zlabel=None, label=None,
                centers=None,
                psize=20,
                out_file=None, title=None, show=True, cmap='coolwarm',
                remove_tick=False,
                use_perc=False,
                rasterized = True,
                fontsize = 15,
                vmax = None,
                vmin = None
                ):
    """Plots a 2D density map given x,y coordinates and an intensity z for
    every data point

    Parameters
    ----------
    X : array-like, shape=[n_samples,2]
        Input points.
    z : array-like, shape=[n_samples]
        Density at every point

    Returns
    -------
    None
    """
    x, y = X[:,0], X[:,1]

    fontsize = fontsize

    if use_perc :
        n_sample = len(x)
        outlier_window = int(0.05 * n_sample)

        argz = np.argsort(z)
        bot_outliers = argz[:outlier_window]
        top_outliers = argz[-outlier_window:]
        typical = argz[outlier_window:-outlier_window]

        # plot typical
        plt.scatter(x[typical],y[typical],c=z[typical],cmap=cmap,s=psize, alpha=1.0,rasterized=rasterized)
        cb=plt.colorbar()
        # plot bot outliers (black !)
        plt.scatter(x[bot_outliers],y[bot_outliers],c='black',s=psize,alpha=1.0,rasterized=rasterized)
        # plot top outliers (green !)
        plt.scatter(x[top_outliers],y[top_outliers],c='#36DA36',s=psize,alpha=1.0,rasterized=rasterized)

    else:
        if label is not None:
            plt.scatter(x,y,c=z,cmap=cmap,s=psize,alpha=1.0,rasterized=rasterized,label=label,vmax=vmax,vmin=vmin)
        else:
            plt.scatter(x,y,c=z,cmap=cmap,s=psize,alpha=1.0,rasterized=rasterized, vmax=vmax,vmin=vmin)

        cb=plt.colorbar()

    if remove_tick:
        plt.tick_params(labelbottom='off',labelleft='off')

    if xlabel is not None:
        plt.xlabel(xlabel,fontsize=fontsize)
    if ylabel is not None:
        plt.ylabel(ylabel,fontsize=fontsize)
    if zlabel is not None:
        cb.set_label(label=zlabel,labelpad=10)
    if title is not None:
        plt.title(title,fontsize=fontsize)
    if label is not None:
        plt.legend(loc='best')

    if centers is not None:
        plt.scatter(centers[:,0],centers[:,1], c='lightgreen', marker='*',s=200, edgecolor='black',linewidths=0.5)

    if out_file is not None:
        plt.savefig(out_file)
    if show:
        plt.show()

def scatter_w_label(x, y, z, psize=20, label = None):

    unique_z=np.sort(np.unique(z.flatten()))
    mycol = COLOR_PALETTE()

    plt.subplots(figsize=(8,6))

    for i, zval in enumerate(unique_z):
        pos=(z.flatten()==zval)
        if label is not None:
            plt.scatter(x[pos],y[pos],s=psize,c=mycol[i], label=label[i], rasterized=True)
        else:
            plt.scatter(x[pos],y[pos],s=psize,c=mycol[i], rasterized=True)

    if label is not None:
        plt.legend(loc='best',fontsize=12)

    plt.tight_layout()
    plt.show()

def plot_true_label(X, palette, y=None, fontsize = 15, psize = 20):
    plt.title('True labels', fontsize=fontsize)
    print("--> Plotting summary: True clustered labels, inferred labels and density map ")
    if y is None:
        plt.scatter(X[:,0],X[:,1],c=palette[0],rasterized=True)
    else:
        y_unique = np.unique(y)
        for i, yu in enumerate(y_unique):
            pos=(y==yu)
            plt.scatter(X[pos,0],X[pos,1], s=psize, c=palette[i],rasterized=True)

def plot_inferred_label(ax, X, idx_centers, cluster_label, palette, psize = 20, eta = None, eta_show = True, fontsize=15):

    n_center = len(idx_centers)

    for i in range(n_center):
        pos=(cluster_label==i)
        plt.scatter(X[pos,0],X[pos,1],c=palette[i], s=psize, rasterized=True)

    centers = X[idx_centers]
    for xy, i in zip(centers, range(n_center)) :
        # Position of each label.
        txt = ax.annotate(str(i),xy,
        xytext=(0,0), textcoords='offset points',
        fontsize=20,horizontalalignment='center', verticalalignment='center'
        )
        txt.set_path_effects([
            PathEffects.Stroke(linewidth=5, foreground="w"),
            PathEffects.Normal()])

    xmin,xmax = plt.xlim()
    ymin,ymax = plt.ylim()
    dx = xmax - xmin
    dy = ymax - ymin

    if eta is not None: # displaying eta parameter
        if eta_show:
            txt = ax.annotate("$\eta=%.2f$"%eta,[xmin+0.15*dx,ymin+0.05*dy], xytext=(0,0), textcoords='offset points',
            fontsize=20,horizontalalignment='center', verticalalignment='center')
            txt.set_path_effects([
                PathEffects.Stroke(linewidth=5, foreground="w"),
                PathEffects.Normal()])

    plt.title('Inferred labels',fontsize=fontsize)
    plt.tight_layout()

def summary(idx_centers, cluster_label, rho, X, eta = None, eta_show = True, y=None, psize=20, savefile=None, show=False,
plot_to_show = None
):
    """ Summary plots : original labels (if available), inferred labels and density map used for clustering """

    fontsize=15
    n_sample=X.shape[0]
    n_center=idx_centers.shape[0]
    palette=COLOR_PALETTE()

    plt.figure(1,figsize=(22,10))

    plt.subplot(131)
    plot_true_label(X, palette, y=y,fontsize=fontsize, psize = psize)

    ax = plt.subplot(132)
    plot_inferred_label(ax, X, idx_centers, cluster_label, palette, psize =psize, eta = eta, eta_show = eta_show, fontsize=fontsize)

    plt.subplot(133)
    density_map(X,rho,centers=X[idx_centers],title='Density map', psize=psize, show=False)

    plt.tight_layout()
    if savefile:
        plt.savefig(savefile, dpi=300)
    if show is True:
        plt.show()
        plt.clf()

def summary_model(model, eta=None, ytrue = None, show=True, savefile = None, eta_show = True):
    """ Summary figure passing in only an FDC object (model), noise can be specified via the eta parameter """

    if eta is None:
        eta_ = model.eta
        idx_centers = model.idx_centers
        cluster_label = model.cluster_label
    else:
        pos = np.argmin(np.abs(np.array(model.noise_range)-eta))
        eta_ = model.noise_range[pos]
        idx_centers = model.hierarchy[pos]['idx_centers']
        cluster_label = model.hierarchy[pos]['cluster_labels']

    rho = model.rho
    X = model.X
    summary(idx_centers, cluster_label, rho, X, y=ytrue, eta = eta_, show=show, savefile=savefile, eta_show=eta_show)

def inferred_label(model, eta=None, show=True, savefile = None, eta_show = True, fontsize =15, psize = 20):

    if eta is None:
        eta_ = model.noise_range[-1]
        idx_centers = model.idx_centers
        cluster_label = model.cluster_label
    else:
        pos = np.argmin(np.abs(np.array(model.noise_range)-eta))
        eta_ = model.noise_range[pos]
        idx_centers = model.hierarchy[pos]['idx_centers']
        cluster_label = model.hierarchy[pos]['cluster_labels']

    rho = model.rho
    X = model.X

    n_sample=X.shape[0]
    n_center=idx_centers.shape[0]
    palette=COLOR_PALETTE()

    plt.figure(1,figsize=(10,10))
    ax = plt.subplot(111)
    plot_inferred_label(ax, X, idx_centers, cluster_label, palette, psize = psize, eta = eta_, eta_show = eta_show, fontsize=fontsize)

    if savefile is not None:
        plt.savefig(savefile)

    if show is True:
        plt.show()

    plt.clf()

def cluster_w_label(X, y, Xcluster=None, show=True, savefile = None, fontsize =15, psize = 20, title=None, w_label = True, figsize=None,
     dpi=200, alpha=0.7, edgecolors=None, cp_style=1, w_legend=False, outlier=True):

    if figsize is not None:
        plt.figure(figsize=figsize)
    y_unique_ = np.unique(y)

    palette = COLOR_PALETTE(style=cp_style)
    idx_centers = []
    ax = plt.subplot(111)
    all_idx = np.arange(len(X))

    if outlier is True:
        y_unique = y_unique_[y_unique_ > -1]
    else:
        y_unique = y_unique_
    n_center = len(y_unique)

    for i, yu in enumerate(y_unique):
        pos=(y==yu)
        Xsub = X[pos]
        plt.scatter(Xsub[:,0],Xsub[:,1],c=palette[i], s=psize, rasterized=True, alpha=alpha, edgecolors=edgecolors, label = yu)

        if Xcluster is not None:
            Xmean = Xcluster[i]
        else:
            Xmean = np.mean(Xsub, axis=0)
        #Xmean = np.mean(Xsub,axis=0)
        idx_centers.append(all_idx[pos][np.argmin(np.linalg.norm(Xsub - Xmean, axis=1))])

    if outlier is True:
        color_out = {-3 : '#ff0050', -2 : '#9eff49', -1 : '#89f9ff'}
        for yi in [-3, -2, -1]:
            pos = (y == yi)
            if np.count_nonzero(pos) > 0:
                Xsub = X[pos]
                plt.scatter(Xsub[:,0],Xsub[:,1],c=color_out[yi], s=psize, rasterized=True, alpha=alpha, marker="2",edgecolors=edgecolors, label = yi)

    if w_label is True:
        centers = X[idx_centers]
        for xy, i in zip(centers, y_unique) :
            # Position of each label.
            txt = ax.annotate(str(i),xy,
            xytext=(0,0), textcoords='offset points',
            fontsize=fontsize,horizontalalignment='center', verticalalignment='center'
            )
            txt.set_path_effects([
                PathEffects.Stroke(linewidth=5, foreground="w"),
                PathEffects.Normal()])

    xmin,xmax = plt.xlim()
    ymin,ymax = plt.ylim()
    dx = xmax - xmin
    dy = ymax - ymin
    plt.xticks([])
    plt.yticks([])

    if title is not None:
        plt.title(title,fontsize=fontsize)
    if w_legend is True:
        plt.legend(loc='best')

    plt.tight_layout()
    if savefile is not None:
        if dpi is None:
            plt.savefig(savefile)
        else:
            plt.savefig(savefile,dpi=dpi)

    if show is True:
        plt.show()
        plt.clf()
        plt.close()

    return ax

def summary_v2(idx_centers, cluster_label, rho, X, n_true_center=1, y=None, psize=20, savefile=None, show=False):
    """ Summary plots w/o density map """

    fontsize=15
    n_sample=X.shape[0]
    n_center=idx_centers.shape[0]
    palette=COLOR_PALETTE()

    '''plt.figure(1,figsize=(10,10))

    plt.subplot(131)
    plt.title('True labels',fontsize=fontsize)
    print("--> Plotting summary: True clustered labels, inferred labels and density map ")
    if y is None:
        plt.scatter(X[:,0],X[:,1],c=palette[0],rasterized=True)
    else:
        for i in range(n_true_center):
            pos=(y==i)
            plt.scatter(X[pos,0],X[pos,1], s=psize,c=palette[i],rasterized=True)
    '''
    ax = plt.subplot(111)
    for i in range(n_center):
        pos=(cluster_label==i)
        plt.scatter(X[pos,0],X[pos,1],c=palette[i], s=psize, rasterized=True)

    centers = X[idx_centers]
    for xy, i in zip(centers, range(n_center)) :
        # Position of each label.
        txt = ax.annotate(str(i),xy,
        xytext=(0,0), textcoords='offset points',
        fontsize=20,horizontalalignment='center', verticalalignment='center'
        )
        txt.set_path_effects([
            PathEffects.Stroke(linewidth=5, foreground="w"),
            PathEffects.Normal()])

    #plt.title('Inferred labels',fontsize=fontsize)
    #plt.tight_layout()
    #plt.subplot(133)

    #density_map(X,rho,centers=X[idx_centers],title='Density map', psize=psize, show=False)

    if savefile:
        plt.savefig(savefile)
    if show is True:
        plt.show()

    plt.clf()

def dendrogram(model, show=True, savefile=None):
    from scipy.cluster.hierarchy import dendrogram as scipydendro
    from .hierarchy import compute_linkage_matrix

    fontsize=15

    Z = compute_linkage_matrix(model)
    scipydendro(Z)

    plt.ylim(0, 1.2 * model.max_noise)

    plt.xlabel('cluster $\#$',fontsize=fontsize)
    plt.ylabel('$\eta$',fontsize=fontsize)
    plt.title('Clustering hierarchy',fontsize=fontsize)
    plt.tight_layout()

    if show is True:
        plt.show()
    if savefile is not None:
        plt.savefig(savefile)

    plt.clf()

def viSNE(X_2D, X_original, markers, show=True, savefig=None, col_index = None, col_wrap = 4, downsample = None):
    import pandas as pd
    """Plots intensity on top of data.

    Parameters
    ------------

    X_2D : coordinates of the data points (2d points)

    X_original : original marker intensity

    makers : list of str, list of names of the markers (for showing as titles)

    savefig : str, if u want to save figure, should be the name of the output file

    downsample : int, number of data points in a random sample of the original data

    show : bool, if u want to see the plots

    """

    X = X_2D
    if col_index is not None:
        z_df = pd.DataFrame(X_original[:,col_index], columns=[markers[i] for i in col_index])
    else:
        z_df = pd.DataFrame(X_original, columns=markers)

    facegrid(X[:,0], X[:,1], z_df, show=show, savefig=savefig, downsample = downsample, col_wrap=col_wrap)

def facegrid(x, y, z_df, col_wrap = 4, downsample = None, show=True, savefig = None):

    n_sample = x.shape[0]
    if downsample is not None:
        random_sub = np.random.choice(np.arange(0, n_sample, dtype=int), downsample, replace=False)
        xnew = x[random_sub]
        ynew = y[random_sub]
        znew = z_df.iloc[random_sub]
    else:
        xnew = x
        ynew = y
        znew = z_df

    n_plot = z_df.shape[1]

    assert len(x) == len(y) and len(x) == len(z_df)

    n_row = math.ceil(n_plot / col_wrap)

    xfig = 12
    yfig = 8
    xper_graph = xfig/col_wrap
    yper_graph = yfig/n_row

    if n_row >= col_wrap:
        xper_graph = yper_graph
    else:
        yper_graph = xper_graph

    plt.figure(figsize=(xper_graph*col_wrap,yper_graph*n_row))

    col_names = z_df.columns.values

    for i in range(n_plot):
        ax = plt.subplot(n_row, col_wrap, i+1)
        my_scatter(xnew, ynew, znew.iloc[:,i].as_matrix(), ax)
        ax.set_title(col_names[i])
        ax.set_xticks([])
        ax.set_yticks([])

    plt.tight_layout()

    if show is True:
        plt.show()

    if savefig is True:
        plt.savefig(savefig)

def my_scatter(x, y, z, ax):

    cmap = plt.get_cmap('coolwarm')

    argz = np.argsort(z)

    #zmin, zmax = z[argz[0]], z[argz[-1]]
    #ztmp = (z+zmin)*(1./zmax)

    n_sample = len(x)
    bot_5 = round(n_sample*0.05)
    top_5 = round(n_sample*0.95)
    mid = argz[bot_5:top_5]
    bot = argz[:bot_5]
    top = argz[top_5:]

    x_mid = x[mid]
    y_mid = y[mid]
    z_mid = z[mid]

    x_bot = x[bot]
    y_bot = y[bot]
    z_bot = z[bot]

    x_top = x[top]
    y_top = y[top]
    z_top = z[top]

    ax.scatter(x_mid, y_mid, c = z_mid, cmap = cmap, s=6)
    ax.scatter(x_bot, y_bot, c = "purple", s=4)
    ax.scatter(x_top, y_top, c = "#00FF00",s=4)

def select_data(X, y, X_original = None, option = None, loop=False, kwargs=None):
    from .widget import Highlighter
    # Taking selection from the user, will plot an histogram of the underlying data (default)
    # Other options are {mnist, etc. etc.}

    if loop is True:
        n_repeat = 10

    if option == 'mnist':

        for _ in range(n_repeat):
            if kwargs is not None:
                ax = cluster_w_label(X, y, show=False, **kwargs)
            else:
                ax = cluster_w_label(X, y, show=False)

            highlighter = Highlighter(ax, X[:,0], X[:,1])
            selected_regions = highlighter.mask
            plt.close()
            X_sub = X_original[selected_regions]
            n_plot = min([len(X_sub), 16])
            #print(xcluster.shape)

            rpos = np.random.choice(np.arange(len(X_sub)), size=n_plot)
            #print(rpos)
            fig, ax = plt.subplots(4,4,figsize=(8,8))
            count = 0
            for i in range(4):
                for j in range(4):
                    count+=1
                    if count > n_plot:
                        break
                    ax[i,j].imshow(X_sub[rpos[4*i+j]].reshape(28,28),cmap="Greys")
                    ax[i,j].set_xticks([])
                    ax[i,j].set_yticks([])

            plt.tight_layout()
            plt.show()
            plt.clf()

reason: import numpy,from scipy


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

"""
====================================
Find the edges in non-dominated sets
====================================

:Author:
   Richard Everson   <R.M.Everson@exeter.ac.uk>
:Date:
   7 September 2012
:Copyright:
   Copyright (c)  Richard Everson, University of Exeter, 2012
:File:
   edge.py
"""

from __future__ import division

import numpy as np
import matplotlib.pyplot as plt
from itertools import combinations
import numpy.random.mtrand
import scipy.misc
import scipy.stats.mstats
import sys

def rankCoords(X):
    """
    Return X in rank coordinates
    """
    Z = np.zeros(X.shape)
    for c in range(X.shape[1]):
        Z[:,c] = scipy.stats.mstats.rankdata(X[:,c])
    return Z

def nondom(Z):
    """
    Return a list of the indices of the non-dominated rows of Z.
    """
    N, D = Z.shape

    nd = []
    for n in range(N):
        # First term finds rows Z[j,:] which are smaller than or equal to
        # Z[n,:]; second term checks that there is at least one element of
        # Z[n] strictly smaller.
        dominators = np.sum( (np.sum(Z <= Z[n], 1) == D) * (np.sum(Z < Z[n], 1) > 0) )
        if dominators == 0:
            nd.append(n)
    return nd

def nondom_max(Z):
    """
    Return a list of the indices of the rows of Z that are non-dominated under maximimisation.
    """
    N, D = Z.shape

    nd = []
    for n in range(N):
        # First term finds rows Z[j,:] which are greater than or equal to
        # Z[n,:]; second term checks that there is at least one element of
        # Z[n] strictly greater.
        dominators = np.sum( (np.sum(Z >= Z[n], 1) == D) * (np.sum(Z > Z[n], 1) > 0) )
        if dominators == 0:
            nd.append(n)
    return nd

def edges(Y, k=None, solo=False, max=False):
    """
    Find the edges in Y by projection onto k-dimensional subsets. If k is None
    corners of all orders are found; if k is specified only edges of the given
    order k are located.

    Returns a list of the indices of Y that are edge points

    If max is True the points found are non-dominated under maximisation
    rather than minimisation.

    If solo is True, a point is an edge point if it is the sole dominator in a
    particular projection; this is similar to Singh et al's "corners". If
    False all the non-dominated points in that projection are edges.
    """
    ndfunc = nondom_max if max else nondom

    N, M = Y.shape
    if k is None:
        K = range(1, M)
    else:
        K = [k]

    corners = {}
    for k in K:
        print k
        for criteria in combinations(range(M), k):
            sys.stdout.flush()
            Yk = Y[:, criteria]
            nd = ndfunc(Yk)
            if solo and len(nd) == 1:
                if nd[0] not in corners:
                    print k, nd[0]
                corners[nd[0]] = 1
            elif not solo:
                for n in nd:
                    corners[n] = 1

    return corners.keys()

def weak_dominates(Y, x):
    """
    c = weak_dominates(Y, x)
    Return a vector signifying whether each row of Y weakly dominates x
    """
    return (Y <= x).sum(axis=1) == Y.shape[1]

def attain_edge(Y, detailed=False):
    """
    Return indices of rows of Y that are edge points of Y in the sense that
    they extend the range of the attainment surface.

    If detailed is True a list of tuples is returned, the first element of
    the tuple is the index i of the edge point and the second is the
    dimension in which extending the point to rplus produces a new point
    that is dominated only by Y[i]
    """

    rplus = Y.max(axis=0) + 1;

    edge = []
    for i, y in enumerate(Y):

        for m in range(Y.shape[1]):
            x = y.copy()
            x[m] = rplus[m]

            doms = weak_dominates(Y, x).sum()
            assert doms > 0

            if  doms == 1:          # Exactly one dominator, therefore edge
                if detailed:
                    edge.append((i, m))
                else:
                    edge.append(i)
                    break            # Don't bother testing this y any more
        else:
            pass
            #print 'Not weakly dominated!', y, x
    return edge

def dominance_distance(X, w=None):
    """
    Find the dominance distance between pairs of rows of X. Objectives
    (columns) are weighted by w. If w is None (the default) then the weights
    are all unity.

    Returns a matrix of the dominance distances.
    """
    N, M = X.shape
    if w is None:
        w = np.ones(M)

    R = np.zeros((N,M))
    for m in range(M):
        R[:,m] = scipy.stats.mstats.rankdata(X[:,m])
    D = np.zeros((N,N))
    for m in range(M):
        r = np.tile(R[:,m], (N,1))
        D += w[m]*np.abs(r - r.T)
    D /= M
    return D

def edge_frequency(Y, k=2):
    """
    Find the fraction of k-dimensional subset projections for which each point in Y is
    non-dominated under maximisation or minimisation.

    Returns an array of the fractions.
    """
    N, M = Y.shape
    f = np.zeros(N)

    for criteria in combinations(range(M), k):
        Yk = Y[:, criteria]
        nd = []
        for ndfunc in (nondom, nondom_max):
            nd += ndfunc(Yk)
        nd = list(set(nd))                          # Unique elements
        f[nd] += 1
    return f/(scipy.misc.comb(M, k, exact=1))

def edge_frequency_detailed(Y, k=2):
    """
    Find the fraction of k-dimensional subset projections for which each point in Y is
    non-dominated under minimisation, maximisation or both

    Returns arrays of the fractions: min, max, both.
    """
    N, M = Y.shape
    f = np.zeros(N)
    fmin = np.zeros(N)
    fmax = np.zeros(N)

    for criteria in combinations(range(M), k):
        Yk = Y[:, criteria]
        ndmin = nondom(Yk)
        fmin[ndmin] += 1
        ndmax = nondom_max(Yk)
        fmax[ndmax] += 1
        nd = list(set(ndmin+ndmax))                 # Unique elements
        f[nd] += 1

    MCk = scipy.misc.comb(M, k, exact=1)
    return fmin/MCk, fmax/MCk, f/MCk

def freq_hist(E, F, bounds, Nx = 30):
    """
    Histogram the frequencies
    """
    d = (bounds[1] - bounds[0])/Nx
    Ny = int(np.ceil((bounds[3]-bounds[2])/d))

    x = F[:,0]
    y = F[:,1]

    xloc = bounds[0] + np.arange(Nx+1)*d
    yloc = bounds[2] + np.arange(Ny+1)*d
    H = np.zeros((Nx, Ny))

    for i in range(Nx):
        for j in range(Ny):
            Ix = np.logical_and(xloc[i] < x, x <= xloc[i+1])
            Iy = np.logical_and(yloc[j] < y, y <= yloc[j+1])
            I = np.logical_and(Ix, Iy)
            if any(I):
                H[i,j] = np.median(E[I])
            else:
                H[i,j] = -1

    xloc[-1] -= 0.002
    yloc[-1] -= 0.002
    return H, xloc, yloc

max_stars_repo_name: richardeverson/life-on-the-edge

max_issues_repo_name: richardeverson/life-on-the-edge

max_forks_repo_name: richardeverson/life-on-the-edge

reason: import numpy,import scipy


text:
import tensorflow as tf
from tensorflow.python.keras._impl.keras.datasets.cifar10 import load_data
import numpy as np
import matplotlib.pyplot as plt
plt.switch_backend('agg')
import matplotlib.gridspec as gridspec
import os
import math
import cv2
from scipy.misc import toimage
from glob import glob
from random import shuffle
from download import download_celeb_a
from utils import *
from utils import add_noise_to_gradients
import time
initializer = tf.contrib.layers.xavier_initializer()
rand_uniform = tf.random_uniform_initializer(-1,1,seed=2)

mb_size = 128
X_dim = 128*128

def next_batch(num, data, shuffle=True):
    '''
    Return a total of `num` random samples and labels.
    '''
    idx = np.arange(0 , len(data))
    if shuffle == True:
        np.random.shuffle(idx)
    idx = idx[:num]
    data_shuffle = [data[ i] for i in idx]

    return np.asarray(data_shuffle)

def normalize(x):
    """
        argument
            - x: input image data in numpy array [32, 32, 3]
        return
            - normalized x
    """
    min_val = np.min(x)
    max_val = np.max(x)
    x = (x-min_val) / (max_val-min_val)
    return x

def plot(samples):
    fig = plt.figure(figsize=(4, 4))
    gs = gridspec.GridSpec(8, 8)
    gs.update(wspace=0.05, hspace=0.05)

    for i, sample in enumerate(samples):
        ax = plt.subplot(gs[i])
        plt.axis('off')
        ax.set_xticklabels([])
        ax.set_yticklabels([])
        ax.set_aspect('equal')
        img = sample.reshape(128, 128,3)
        plt.imshow(toimage(img),interpolation='nearest')

    return fig

initializer = tf.contrib.layers.xavier_initializer()
rand_uniform = tf.random_uniform_initializer(-1,1,seed=2)

X = tf.placeholder(tf.float32, shape=[None, 128, 128, 3])

download_celeb_a("../data")
data_files = glob(os.path.join("../data/celebA/*.jpg"))
len_x_train = len(data_files)
sample = [get_image(sample_file, 108, True, 128, is_grayscale = 0) for sample_file in data_files]
sample_images = np.array(sample).astype(np.float32)
x_train = sample_images

x_train = normalize(x_train)

theta_A = []
theta_G = []
def xavier_init(size):
    in_dim = size[0]
    xavier_stddev = 1. / tf.sqrt(in_dim / 2.)
    return tf.random_normal(shape=size, stddev=xavier_stddev)

def autoencoder(x):
    input_shape=[None, 128, 128, 3]
    n_filters=[3, 128, 256, 512, 1024]
    filter_sizes=[5, 5, 5, 5, 5]

    if len(x.get_shape()) == 3:
        x_dim = np.sqrt(x.get_shape().as_list()[1])
        if x_dim != int(x_dim):
            raise ValueError('Unsupported input dimensions')
        x_dim = int(x_dim)
        x_tensor = tf.reshape(
            x, [-1, x_dim, x_dim, 3])
    elif len(x.get_shape()) == 4:
        x_tensor = x
    else:
        raise ValueError('Unsupported input dimensions')
    current_input = x_tensor

    encoder = []
    decoder = []
    shapes_enc = []
    shapes_dec = []
    with tf.name_scope("Encoder"):
        for layer_i, n_output in enumerate(n_filters[1:]):
            n_input = current_input.get_shape().as_list()[3]
            shapes_enc.append(current_input.get_shape().as_list())
            W = tf.Variable(xavier_init([filter_sizes[layer_i],filter_sizes[layer_i],n_input, n_output]))
            theta_G.append(W)
            encoder.append(W)
            conv = tf.nn.conv2d(current_input, W, strides=[1, 2, 2, 1], padding='SAME')
            conv = tf.contrib.layers.batch_norm(conv,updates_collections=None,decay=0.9, zero_debias_moving_mean=True,is_training=True)
            output = tf.nn.leaky_relu(conv)
            current_input = output
        encoder.reverse()
        shapes_enc.reverse()
        W_fc1 = tf.Variable(tf.random_normal([8*8*1024, 128]))
        theta_G.append(W_fc1)
        z = tf.matmul(tf.layers.flatten(current_input),W_fc1)
        z =  tf.contrib.layers.batch_norm(z,updates_collections=None,decay=0.9, zero_debias_moving_mean=True,is_training=True)
        z = tf.nn.tanh(z)
        z_value = z
        W_fc2 = tf.Variable(tf.random_normal([128, 8*8*1024]))
        theta_G.append(W_fc2)
        z_ = tf.matmul(z,W_fc2)
        z_ = tf.contrib.layers.batch_norm(z_,updates_collections=None,decay=0.9, zero_debias_moving_mean=True,is_training=True)
        z_ = tf.nn.relu(z_)
        current_input = tf.reshape(z_, [-1, 8, 8, 1024])
        for layer_i, shape in enumerate(shapes_enc):
            W_enc = encoder[layer_i]
            W = tf.Variable(xavier_init(W_enc.get_shape().as_list()))
            theta_G.append(W)
            decoder.append(W)
            shapes_dec.append(current_input.get_shape().as_list())
            deconv = tf.nn.conv2d_transpose(current_input, W,
                                     tf.stack([tf.shape(x)[0], shape[1], shape[2], shape[3]]),
                                     strides=[1, 2, 2, 1], padding='SAME')
            if layer_i ==3:
                output = tf.nn.sigmoid(deconv)
            else:
                deconv = tf.contrib.layers.batch_norm(deconv,updates_collections=None,decay=0.9, zero_debias_moving_mean=True,is_training=True)
                output = tf.nn.relu(deconv)
            current_input = output
        g = current_input
        g_logits = deconv

        encoder.reverse()
        shapes_enc.reverse()
        decoder.reverse()
        shapes_dec.reverse()

    with tf.name_scope("Decoder"):
        for layer_i, shape in enumerate(shapes_dec):
            W_dec = decoder[layer_i]
            conv = tf.nn.conv2d(current_input, W_dec, strides=[1, 2, 2, 1], padding='SAME')
            conv = tf.contrib.layers.batch_norm(conv,updates_collections=None,decay=0.9, zero_debias_moving_mean=True,is_training=True)
            output = tf.nn.leaky_relu(conv)
            current_input = output
        encoder.reverse()
        shapes_enc.reverse()
        z = tf.matmul(tf.layers.flatten(current_input), tf.transpose(W_fc2))
        z = tf.contrib.layers.batch_norm(z,updates_collections=None,decay=0.9, zero_debias_moving_mean=True,is_training=True)
        z = tf.nn.tanh(z)
        z_transpose = z
        z_ = tf.matmul(z, tf.transpose(W_fc1))
        z_ =  tf.contrib.layers.batch_norm(z_,updates_collections=None,decay=0.9, zero_debias_moving_mean=True,is_training=True)
        z_ = tf.nn.relu(z_)
        current_input = tf.reshape(z_, [-1, 8, 8,1024])
        for layer_i, shape in enumerate(shapes_enc):
            W_enc = encoder[layer_i]
            deconv = tf.nn.conv2d_transpose(current_input, W_enc,
                                     tf.stack([tf.shape(x)[0], shape[1], shape[2], shape[3]]),
                                     strides=[1, 2, 2, 1], padding='SAME')
            if layer_i ==3:
                output = tf.nn.sigmoid(deconv)
            else:
                deconv = tf.contrib.layers.batch_norm(deconv,updates_collections=None,decay=0.9, zero_debias_moving_mean=True,is_training=True)
                output = tf.nn.relu(deconv)
            current_input = output
        a = current_input
        a_logits = deconv

    return g_logits, g, a_logits, a, z_value, z_transpose

W1 = tf.Variable(xavier_init([5,5,3,64]))
W2 = tf.Variable(xavier_init([5,5,64,128]))
W3 = tf.Variable(xavier_init([5,5,128,256]))
W4 = tf.Variable(xavier_init([5,5,256,512]))
W5 = tf.Variable(xavier_init([8*8*512, 1]))
b5 = tf.Variable(tf.zeros(shape=[1]))

theta_D = [W1,W2,W3,W4,W5,b5]

def discriminator(x):
    if len(x.get_shape()) == 2:
        x_dim = np.sqrt(x.get_shape().as_list()[1])
        if x_dim != int(x_dim):
            raise ValueError('Unsupported input dimensions')
        x_dim = int(x_dim)
        x_tensor = tf.reshape(x, [-1, 128, 128, 3])
    elif len(x.get_shape()) == 4:
        x_tensor = x
    else:
        raise ValueError('Unsupported input dimensions')
    with tf.name_scope("Discriminator"):
        conv1 = tf.nn.conv2d(x_tensor, W1, strides=[1,2,2,1],padding='SAME')
        conv1 = tf.contrib.layers.layer_norm(conv1)
        h1 = tf.nn.leaky_relu(conv1)

        conv2 = tf.nn.conv2d(h1, W2, strides=[1,2,2,1],padding='SAME')
        conv2 = tf.contrib.layers.layer_norm(conv2)
        h2 = tf.nn.leaky_relu(conv2)

        conv3 = tf.nn.conv2d(h2, W3, strides=[1,2,2,1],padding='SAME')
        conv3 = tf.contrib.layers.layer_norm(conv3)
        h3 = tf.nn.leaky_relu(conv3)

        conv4 = tf.nn.conv2d(h3, W4, strides=[1,2,2,1],padding='SAME')
        conv4 = tf.contrib.layers.layer_norm(conv4)
        h4 = tf.nn.leaky_relu(conv4)

        h5 = tf.layers.flatten(h4)

        d = tf.nn.xw_plus_b(h5, W5, b5)

    return d

W1_H = tf.Variable(xavier_init([5,5,3,128]))
W2_H = tf.Variable(xavier_init([5,5,128,256]))
W3_H = tf.Variable(xavier_init([5,5,256,512]))
W4_H = tf.Variable(xavier_init([5,5,512,1024]))
W_fc = tf.Variable(xavier_init([8*8*1024, 128]))
theta_H = [W1_H,W2_H,W3_H,W4_H,W_fc]

def hacker(x):
    if len(x.get_shape()) == 2:
        x_dim = np.sqrt(x.get_shape().as_list()[1])
        if x_dim != int(x_dim):
            raise ValueError('Unsupported input dimensions')
        x_dim = int(x_dim)
        x_tensor = tf.reshape(x, [-1, 128, 128, 3])
    elif len(x.get_shape()) == 4:
        x_tensor = x
    else:
        raise ValueError('Unsupported input dimensions')
    with tf.name_scope("Hacker"):
        conv1 = tf.nn.conv2d(x_tensor, W1_H, strides=[1,2,2,1],padding='SAME')
        conv1 = tf.contrib.layers.batch_norm(conv1,updates_collections=None,decay=0.9, zero_debias_moving_mean=True,is_training=True)
        h1 = tf.nn.leaky_relu(conv1)

        conv2 = tf.nn.conv2d(h1, W2_H, strides=[1,2,2,1],padding='SAME')
        conv2 = tf.contrib.layers.batch_norm(conv2,updates_collections=None,decay=0.9, zero_debias_moving_mean=True,is_training=True)
        h2 = tf.nn.leaky_relu(conv2)

        conv3 = tf.nn.conv2d(h2, W3_H, strides=[1,2,2,1],padding='SAME')
        conv3 = tf.contrib.layers.batch_norm(conv3,updates_collections=None,decay=0.9, zero_debias_moving_mean=True,is_training=True)
        h3 = tf.nn.leaky_relu(conv3)

        conv4 = tf.nn.conv2d(h3, W4_H, strides=[1,2,2,1],padding='SAME')
        conv4 = tf.contrib.layers.batch_norm(conv4,updates_collections=None,decay=0.9, zero_debias_moving_mean=True,is_training=True)
        h4 = tf.nn.leaky_relu(conv4)

        z_value = tf.matmul(tf.layers.flatten(h4),W_fc)
        z_value = tf.nn.tanh(z_value)
    return z_value

G_logits,G_sample,A_logits,A_sample, gen_real_z, gen_trans_z = autoencoder(X)
D_real_logits = discriminator(X)
D_fake_logits = discriminator(G_sample)
disc_fake_z = hacker(G_sample)
A_true_flat = tf.reshape(X, [-1,128,128,3])

global_step = tf.Variable(0, name="global_step", trainable=False)
A_loss = tf.reduce_mean(tf.pow(A_true_flat - A_sample, 2))
G_z_loss = tf.reduce_mean(tf.pow(gen_trans_z - gen_real_z, 2))
D_z_loss =tf.reduce_mean(tf.pow(disc_fake_z - gen_real_z, 2))
G_loss = -tf.reduce_mean(D_fake_logits) - 10.0*D_z_loss + 10.0*G_z_loss + 10.0*A_loss
H_loss = 10.0*D_z_loss

D_loss = tf.reduce_mean(D_fake_logits)-tf.reduce_mean(D_real_logits)

epsilon = tf.random_uniform(shape=[mb_size, 1, 1, 1], minval=0.,maxval=1.)
X_hat = A_true_flat + epsilon * (G_sample - A_true_flat)
D_X_hat = discriminator(X_hat)
grad_D_X_hat = tf.gradients(D_X_hat, [X_hat])[0]
red_idx = list(range(1, X_hat.shape.ndims))
slopes = tf.sqrt(tf.reduce_sum(tf.square(grad_D_X_hat), reduction_indices=red_idx))
gradient_penalty = tf.reduce_mean(tf.square(slopes - 1.))
D_loss = D_loss + 10.0 * gradient_penalty

tf.summary.image('Original',A_true_flat)
tf.summary.image('G_sample',G_sample)
tf.summary.image('A_sample',A_sample)
tf.summary.scalar('D_loss', -D_loss)
tf.summary.scalar('G_loss',-tf.reduce_mean(D_fake_logits))
tf.summary.scalar('A_loss',A_loss)
tf.summary.scalar('G_z_loss',G_z_loss)
tf.summary.scalar('D_z_loss',D_z_loss)
merged = tf.summary.merge_all()

num_batches_per_epoch = int((len_x_train-1)/mb_size) + 1

D_solver = tf.train.AdamOptimizer(learning_rate=1e-4,beta1=0.5, beta2=0.9).minimize(D_loss,var_list=theta_D, global_step=global_step)
G_solver = tf.train.AdamOptimizer(learning_rate=1e-4,beta1=0.5, beta2=0.9).minimize(G_loss,var_list=theta_G, global_step=global_step)
H_solver = tf.train.AdamOptimizer(learning_rate=1e-4,beta1=0.5, beta2=0.9).minimize(H_loss,var_list=theta_H, global_step=global_step)

timestamp = str(int(time.time()))
out_dir = os.path.abspath(os.path.join(os.path.curdir, "models/celebA128_" + timestamp))
checkpoint_dir = os.path.abspath(os.path.join(out_dir, "checkpoints"))
checkpoint_prefix = os.path.join(checkpoint_dir, "model")
if not os.path.exists('models/'):
    os.makedirs('models/')
if not os.path.exists(checkpoint_dir):
    os.makedirs(checkpoint_dir)
    saver = tf.train.Saver(tf.global_variables())
if not os.path.exists('dc_out_celebA128/'):
    os.makedirs('dc_out_celebA128/')
if not os.path.exists('generated_celebA128/'):
    os.makedirs('generated_celebA128/')
with tf.Session() as sess:
    train_writer = tf.summary.FileWriter('graphs/'+'celebA128',sess.graph)
    sess.run(tf.global_variables_initializer())
    i=0

    for it in range(1000000000):
        for _ in range(5):
            X_mb = next_batch(mb_size, x_train)
            _, D_loss_curr = sess.run([D_solver, D_loss],feed_dict={X: X_mb})
            _, H_loss_curr = sess.run([H_solver, H_loss],feed_dict={X: X_mb})
        summary,_, G_loss_curr,A_loss_curr = sess.run([merged,G_solver, G_loss, A_loss],feed_dict={X: X_mb})
        current_step = tf.train.global_step(sess, global_step)
        train_writer.add_summary(summary,current_step)

        if it % 100 == 0 and it != 0:
            print('Iter: {}; D_loss: {:.4}; G_loss: {:.4};  A_loss: {:.4};'.format(it,D_loss_curr, G_loss_curr, A_loss_curr))

        if it % 1000 == 0 and it != 0:
            samples = sess.run(G_sample, feed_dict={X: X_mb})
            samples_flat = tf.reshape(samples,[-1,128,128,3]).eval()
            fig = plot(np.append(X_mb[:32], samples_flat[:32], axis=0))
            plt.savefig('dc_out_celebA128/{}_G.png'.format(str(i).zfill(3)), bbox_inches='tight')
            plt.close(fig)
            samples = sess.run(A_sample, feed_dict={X: X_mb})
            samples_flat = tf.reshape(samples,[-1,128,128,3]).eval()
            fig = plot(np.append(X_mb[:32], samples_flat[:32], axis=0))
            plt.savefig('dc_out_celebA128/{}_A.png'.format(str(i).zfill(3)), bbox_inches='tight')
            i += 1
            plt.close(fig)
            path = saver.save(sess, checkpoint_prefix, global_step=current_step)
            print('Saved model at {} at step {}'.format(path, current_step))

        if it% 100000 == 0 and it != 0:
            for ii in range(len_x_train//100):
                xt_mb = next_batch(mb_size, x_train, shuffle=False)
                samples = sess.run(G_sample, feed_dict={X: xt_mb})
                if ii == 0:
                    generated = samples
                else:
                    np.append(generated,samples,axis=0)
            np.save('./generated_celebA128/generated_{}_image.npy'.format(str(it)), generated)

for iii in range(len_x_train//100):
    xt_mb = next_batch(mb_size, x_train, shuffle=False)
    samples = sess.run(G_sample, feed_dict={X: xt_mb})
    if iii == 0:
        generated = samples
    else:
        np.append(generated,samples,axis=0)
np.save('./generated_celebA128/generated_{}_image.npy'.format(str(it)), generated)


text:
import cv2

import numpy as np

from libs.utils import *

class Visualize(object):

    def __init__(self, cfg):

        self.cfg = cfg

        self.mean = np.array([cfg.mean], dtype=np.float32)
        self.std = np.array([cfg.std], dtype=np.float32)

        self.line = np.zeros((cfg.height, 3, 3), dtype=np.uint8)
        self.line[:, :, :] = 255

        self.show = {}

    # def overlap_2_on_1(self, ref_name1, ref_name2, name='overlap'):
    #     img1 = np.copy(self.show[ref_name1])
    #     img2 = self.show[ref_name2]
    #
    #     img1[img2 == 255] = 255
    #
    #     self.show[name] = img1

    def update_image(self, img, name='img'):
        img = to_np(img.permute(1, 2, 0))
        img = np.uint8((img * self.std + self.mean) * 255)[:, :, [2, 1, 0]]
        self.show[name] = img

    def update_label(self, label, name='label'):
        label = to_np(label)
        label = np.repeat(np.expand_dims(np.uint8(label != 0) * 255, axis=2), 3, 2)
        self.show[name] = label

    def update_image_name(self, img_name):
        self.show['img_name'] = img_name

    def draw_lines_cv(self, data, name, ref_name='img', color=(255, 0, 0), s=2):
        img = np.ascontiguousarray(np.copy(self.show[ref_name]))
        for i in range(data.shape[0]):
            pts = data[i]
            pt_1 = (pts[0], pts[1])
            pt_2 = (pts[2], pts[3])
            img = cv2.line(img, pt_1, pt_2, color, s)

        self.show[name] = img

    def display_saveimg(self, dir_name, list):
        disp = self.line
        for i in range(len(list)):
            disp = np.concatenate((disp, self.show[list[i]], self.line), axis=1)

        mkdir(dir_name + '/'.join(self.show['img_name'].split('/')[:-1]))
        cv2.imwrite(dir_name + self.show['img_name'], disp)

    def display_gtlines(self, data, mode='pri'):

        self.draw_lines_cv(data=data, name='gtlines')

        self.show['overlap'] = np.copy(self.show['label'])
        if mode == 'mul' and self.cfg.datalist_mode != 'test':
            self.draw_lines_cv(data=data, name='overlap', ref_name='overlap')
        if mode == 'pri':
            self.draw_lines_cv(data=data, name='overlap', ref_name='overlap',
                               color=(0, 255, 0))

        self.display_saveimg(dir_name=self.cfg.dir['out'] + 'display/',
                             list=['img', 'gtlines', 'overlap'])

max_stars_repo_name: dongkwonjin/Semantic-Line-MWCS

max_issues_repo_name: dongkwonjin/Semantic-Line-MWCS

max_forks_repo_name: dongkwonjin/Semantic-Line-MWCS


text:
import numpy as np
from hypothesis import given

from crowddynamics.core.motion.adjusting import force_adjust, torque_adjust
from crowddynamics.core.motion.contact import force_contact
from crowddynamics.core.motion.fluctuation import force_fluctuation, \
    torque_fluctuation
from crowddynamics.core.motion.helbing import \
    force_social_helbing
from crowddynamics.testing import reals

SIZE = 10

@given(mass=reals(min_value=0, shape=SIZE),
       scale=reals(min_value=0, shape=SIZE))
def test_force_fluctuation(mass, scale):
    ans = force_fluctuation(mass, scale)
    assert isinstance(ans, np.ndarray)
    assert ans.dtype.type is np.float64
    assert ans.shape == (SIZE, 2)

@given(mass=reals(min_value=0),
       tau_adj=reals(min_value=0, exclude_zero='near'),
       v0=reals(min_value=0),
       e0=reals(shape=2),
       v=reals(shape=2))
def test_force_adjust(mass, tau_adj, v0, e0, v):
    ans = force_adjust(mass, tau_adj, v0, e0, v)
    assert isinstance(ans, np.ndarray)
    assert ans.dtype.type is np.float64
    assert ans.shape == (2,)

@given(h=reals(),
       n=reals(shape=2),
       a=reals(min_value=0),
       b=reals(min_value=0, exclude_zero='near'))
def test_force_social_helbing(h, n, a, b):
    ans = force_social_helbing(h, n, a, b)
    assert isinstance(ans, np.ndarray)
    assert ans.dtype.type is np.float64
    assert ans.shape == (2,)

@given(h=reals(),
       n=reals(shape=2),
       v=reals(shape=2),
       t=reals(shape=2),
       mu=reals(min_value=0),
       kappa=reals(min_value=0),
       damping=reals(min_value=0))
def test_force_contact(h, n, v, t, mu, kappa, damping):
    ans = force_contact(h, n, v, t, mu, kappa, damping)
    assert isinstance(ans, np.ndarray)
    assert ans.dtype.type is np.float64
    assert ans.shape == (2,)

@given(inertia_rot=reals(0, shape=SIZE), scale=reals(0, shape=SIZE))
def test_torque_fluctuation(inertia_rot, scale):
    ans = torque_fluctuation(inertia_rot, scale)
    assert isinstance(ans, np.ndarray)
    assert ans.dtype.type is np.float64
    assert ans.shape == (SIZE,)

@given(inertia_rot=reals(0),
       tau_rot=reals(0, exclude_zero='near'),
       phi_0=reals(),
       phi=reals(),
       omega_0=reals(),
       omega=reals())
def test_torque_adjust(inertia_rot, tau_rot, phi_0, phi, omega_0, omega):
    ans = torque_adjust(inertia_rot, tau_rot, phi_0, phi, omega_0,
                        omega)
    assert isinstance(ans, float)


text:
#   Copyright 2020 Oinam Romesh Meitei
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

import numpy,sys
from copy import deepcopy

def twinamp(x, y, list1):
    cout_ = 0

    for i in list1:
        if x<i<=y:
            cout_ += 1

    return cout_

def anatwin(self, list1, pobj, hobj, nstep,normalize):

    from ctrlq.lib.solve import pulsec
    from ctrlq.lib.agradc import grad_trotter
    from ctrlq.lib.agradc import grad_trotter_normalized

    ini_state = hobj.initial_state
    tlist = numpy.linspace(0, pobj.duration, nstep)
    dsham = numpy.diagonal(-1j * hobj.dsham.toarray())

    ampcout = [[] for i in range(pobj.nqubit)]
    for i in range(pobj.nqubit):
        if pobj.nwindow==1:
            ampcout[i].append(twinamp(0.0,pobj.duration, tlist))
            continue
        for j in range(pobj.nwindow-1):

            if j == 0:
                ampcout[i].append(twinamp(0.0,pobj.tseq[i][j], tlist))
            else:
                ampcout[i].append(twinamp(pobj.tseq[i][j-1], pobj.tseq[i][j], tlist))
        ampcout[i].append(twinamp(pobj.tseq[i][j], pobj.duration, tlist))

    amp = [[] for j in range(pobj.nqubit)]
    samp = [[] for j in range(pobj.nqubit)]
    cout = 0
    for i in range(pobj.nqubit):
        for j in range(pobj.nwindow):

            if j == 0:
                amp[i].append(0.0) #amp=0.0 at t=0.0
            for k in range(ampcout[i][j]):
                amp[i].append(list1[cout])
            samp[i].append(list1[cout])
            cout += 1

    self.square_amp = samp
    pobj.amp = amp

    for i in range(pobj.nqubit):
        pobj.freq[i] = list1[cout]
        cout += 1
    sfreq = numpy.array(pobj.freq) / pobj.fscale
    pobjc = pulsec(amp, pobj.tseq, sfreq, pobj.duration,
                   pobj.nqubit, pobj.nwindow)

    if normalize:
        aobj = grad_trotter_normalized(tlist, ini_state, pobjc, numpy.array(hobj.hdrive),
                                       dsham, hobj.states, hobj.mham)
    else:
        aobj = grad_trotter(tlist, ini_state, pobjc, numpy.array(hobj.hdrive),
                                       dsham, hobj.states, hobj.mham)

    self.square_gamp = aobj.gradient

    agradient = []
    for i in range(pobj.nqubit):
        cout_ = 0
        for j in range(pobj.nwindow):
            agrad__ = 0.0
            if j==0:
                #agrad__ += aobj.gradient[i][0]
                cout_ += 1
            for k in range(ampcout[i][j]):
                agrad__ += aobj.gradient[i][cout_]
                cout_ += 1
            agradient.append(agrad__)

    for i in range(pobj.nqubit):
        ng1 = self.numgradfreq_2_(i, aobj.energy, ini_state, pobj, hobj, nstep,normalize=normalize)
        agradient.append(ng1)
    self.energy_ = aobj.energy
    self.leak = 1.0 - aobj.norm
    self.gradient_ = agradient

    return(aobj.energy, agradient)

max_stars_repo_name: asthanaa/ctrlq

max_issues_repo_name: asthanaa/ctrlq

max_forks_repo_name: asthanaa/ctrlq