"""Figure 1 plot - paradigm figure and behavior."""

# %%
# Imports
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
from matplotlib.lines import Line2D
from mpl_toolkits.axes_grid1.inset_locator import inset_axes

from config import ANALYSIS_DIR_LOCAL, STREAMS
from utils import eq1, find_dot_idxs

# %%
# Settings
analysis_dir = ANALYSIS_DIR_LOCAL

ci = 68

subj_line_settings = dict(color="black", alpha=0.1, linewidth=0.75)
axhline_args = dict(color="black", linestyle="--", linewidth=1)
pointscale = 1
pointmarkers = "."  # for pointplot which marker style
pointerrwidth = 3
pointlinewidth = axhline_args["linewidth"]
pointcapwidth = 0.1
swarmsize = 2

minimize_method = "Nelder-Mead"
x0_type = "specific"

# Whether or not to draw lines through the extreme and mid points
# to better show the slopes in the data and compare them without a model
draw_model_free_lines = False

# Whether or not to additionally draw "single" in "dual" inset and vice versa
insets_plot_other = True

# %%
# File paths
fname_accs = analysis_dir / "derived_data" / "accuracies.tsv"

fname_weights = analysis_dir / "derived_data" / "weights.tsv"
fname_weights_k_is_1 = analysis_dir / "derived_data" / "weights_k_is_1.tsv"

fname_estimates = analysis_dir / "derived_data" / f"estim_params_{minimize_method}.tsv"

fname_fig1 = analysis_dir / "figures" / "fig1b+.pdf"

# %%
# Load param estimates
df_estims = pd.read_csv(fname_estimates, sep="\t")
df_estims = df_estims[df_estims["x0_type"] == x0_type]

# %%
# Figure 1b+
# Figure 1a is created in LibreOffice Draw
# Fig1a and Fig1b+ are then stitched together in Latex

# figure layout
with sns.plotting_context("talk"):
    fig, axs = plt.subplots(2, 3, figsize=(12, 8))
    fig.tight_layout(h_pad=2.25)
# %%
# panel b - accuracies
df_accs = pd.read_csv(fname_accs, sep="\t")
with sns.plotting_context("talk"):

    x = "stream"
    order = STREAMS
    colname = "accuracy"
    ax = axs[0, 0]
    data = df_accs

    with plt.rc_context({"lines.linewidth": pointlinewidth}):
        sns.pointplot(
            x=x,
            order=order,
            y=colname,
            data=data,
            ci=ci,
            ax=ax,
            markers=pointmarkers,
            scale=pointscale,
            errwidth=pointerrwidth,
            capsize=pointcapwidth,
            color="black",
        )

    sns.swarmplot(
        x=x,
        order=order,
        y=colname,
        data=data,
        ax=ax,
        size=swarmsize,
    )

    # ax.set_ylim(0.5, 1)
    ax.set_xlabel("")
    ax.set_ylabel("Accuracy")
    ax.set_xlim(-0.75, 1.75)

    # connect subj dots with lines
    # https://stackoverflow.com/a/51157346/5201771
    _idx1, _idx2 = find_dot_idxs(ax, int(df_accs.shape[0] / 2))
    locs1 = ax.get_children()[_idx1].get_offsets()
    locs2 = ax.get_children()[_idx2].get_offsets()

    # before plotting, we need to sort so that the data points correspond
    sort_idxs1 = np.argsort(data[data["stream"] == STREAMS[0]]["accuracy"].to_numpy())
    sort_idxs2 = np.argsort(data[data["stream"] == STREAMS[1]]["accuracy"].to_numpy())
    locs2_sorted = locs2[sort_idxs2.argsort()][sort_idxs1]

    for i in range(locs1.shape[0]):
        _x = [locs1[i, 0], locs2_sorted[i, 0]]
        _y = [locs1[i, 1], locs2_sorted[i, 1]]
        ax.plot(_x, _y, **subj_line_settings)

    ax.yaxis.set_major_locator(plt.MaxNLocator(5))
    ax.set_xticklabels([i.capitalize() for i in STREAMS])

    sns.despine(ax=ax)

# %%
# panels c and d - weightings
df_ws = pd.read_csv(fname_weights, sep="\t")
df_ws = df_ws[df_ws["weight_type"].isin(["data", "model", "model_k1"])]

df_ws_k1 = pd.read_csv(fname_weights_k_is_1, sep="\t")
df_ws_k1 = df_ws_k1[df_ws_k1["weight_type"].isin(["data", "model", "model_k1"])]

with sns.plotting_context("talk"):

    for istream, stream in enumerate(STREAMS):
        data = df_ws[df_ws["stream"] == stream]

        x = "number"
        colname = "weight"
        ax = axs[0, 1 + istream]

        sns.pointplot(
            data=data[data["weight_type"] == "data"],
            x=x,
            y=colname,
            ax=ax,
            ci=68,
            color=f"C{istream}",
        )

        sns.pointplot(
            data=data[data["weight_type"] == "model"],
            x=x,
            y=colname,
            ax=ax,
            ci=None,
            color="black",
            join=False,
            scale=0.5,
        )
        plt.setp(ax.collections, zorder=100, label="")

        plot_k1_fitted = True  # whether to plot "fitted" k1, or by just setting k=1
        if plot_k1_fitted:
            ____ = df_ws_k1[df_ws_k1["stream"] == stream]
            k1_true = ax.plot(
                np.arange(9),
                ____[____["weight_type"] == "model_k1"]
                .groupby("number")["weight"]
                .mean(),
                color="black",
                lw=0.5,
            )
        else:
            k1 = ax.plot(
                np.arange(9),
                data[data["weight_type"] == "model_k1"]
                .groupby("number")["weight"]
                .mean(),
                color="black",
                lw=0.5,
            )

        ax.axhline(0.5, xmax=0.95, **axhline_args)

        # plot insets
        # bias = df_estims[df_estims["stream"] == stream]["bias"].mean()
        kappa = df_estims[df_estims["stream"] == stream]["kappa"].mean()
        xs = np.linspace(-1, 1, 9)  # "numbers_rescaled"
        ys = eq1(X=xs, bias=0, kappa=kappa)
        size = 1.0
        axins = inset_axes(
            ax,
            width=size,
            height=size,
            loc="upper left",
            bbox_to_anchor=(0.02, 1),
            bbox_transform=ax.transAxes,
        )
        axins.plot(xs, ys, color=f"C{STREAMS.index(stream)}")

        if insets_plot_other:
            _kappa = df_estims[
                df_estims["stream"]
                == f"{STREAMS[dict(zip((0, 1), (1, 0)))[STREAMS.index(stream)]]}"
            ]["kappa"].mean()
            _ys = eq1(X=xs, bias=0, kappa=_kappa)
            axins.plot(
                xs,
                _ys,
                color=f"C{dict(zip((0, 1), (1, 0)))[STREAMS.index(stream)]}",
                zorder=-1,
                linestyle="--",
                linewidth=0.75,
            )

        if draw_model_free_lines:
            y1 = data[(data["weight_type"] == "data") & (data["number"] == 1)][
                "weight"
            ].mean()
            y2 = data[(data["weight_type"] == "data") & (data["number"] == 2)][
                "weight"
            ].mean()
            y4 = data[(data["weight_type"] == "data") & (data["number"] == 4)][
                "weight"
            ].mean()
            y6 = data[(data["weight_type"] == "data") & (data["number"] == 6)][
                "weight"
            ].mean()
            y8 = data[(data["weight_type"] == "data") & (data["number"] == 8)][
                "weight"
            ].mean()
            y9 = data[(data["weight_type"] == "data") & (data["number"] == 9)][
                "weight"
            ].mean()

            _kwargs = dict(color="red", lw=0.75, zorder=100)

            _coefs = np.polyfit((0, 1), (y1, y2), 1)
            polynomial = np.poly1d(_coefs)
            ax.plot(np.linspace(-3, 4), polynomial(np.linspace(-3, 4)), **_kwargs)

            _coefs = np.polyfit((3, 5), (y4, y6), 1)
            polynomial = np.poly1d(_coefs)
            ax.plot(np.linspace(0, 8), polynomial(np.linspace(0, 8)), **_kwargs)

            _coefs = np.polyfit((7, 8), (y8, y9), 1)
            polynomial = np.poly1d(_coefs)
            ax.plot(np.linspace(4, 11), polynomial(np.linspace(4, 11)), **_kwargs)

        axins.axhline(0, lw=0.1, color="black")
        sns.despine(ax=axins)
        axins.set_xticks([])
        axins.set_yticks([])

        # inset label
        axins.text(
            x=0.175,
            y=0.675,
            s=r"$\widehat{k}$" + f"$ = {kappa:.2f}$",
            ha="left",
            va="center",
            transform=ax.transAxes,
            fontsize=12,
            zorder=100,
            color=f"C{istream}",
        )

        # make a legend
        if istream in [0, 1]:
            handles = [
                Line2D([0], [0], color=f"C{istream}", marker="o", lw=4),
                Line2D(
                    [0],
                    [0],
                    marker="o",
                    color="white",
                    markerfacecolor="black",
                    markersize=8,
                ),
                Line2D([0], [0], color="black"),
            ]

            ax.legend(
                handles=handles,
                labels=["Data", f"Model (k={kappa:.2f})", "Model (k=1.00)"],
                loc="lower right",
                ncol=1,
                frameon=False,
                fontsize=10,
            )

        # other settings
        sns.despine(ax=ax)
        ax.set(xlabel="", ylabel="Decision weight")
        ax.set_title(stream.capitalize())

for ax in axs[0, 1:]:
    ax.set_ylim(
        (
            min(axs[0, 1].get_ylim()[0], axs[0, 2].get_ylim()[0]),
            max(axs[0, 1].get_ylim()[1], axs[0, 2].get_ylim()[1]),
        )
    )

# %%
# panels e, f, g - kappa, bias, noise
param_names = ["kappa", "bias", "noise"]
df_estims = df_estims[["subject", "stream"] + param_names].melt(
    id_vars=["subject", "stream"], var_name="parameter"
)

order = STREAMS
x = "stream"
colname = "value"
hlines = dict(kappa=1, bias=0, noise=0)
with sns.plotting_context("talk"):

    for iparam, param in enumerate(param_names):
        ax = axs[1, iparam]

        data = df_estims[df_estims["parameter"] == param]

        sns.pointplot(
            data=data,
            x=x,
            order=order,
            y=colname,
            ci=68,
            ax=ax,
            markers=pointmarkers,
            scale=pointscale,
            errwidth=pointerrwidth,
            capsize=pointcapwidth,
            color="black",
        )

        sns.swarmplot(
            x=x,
            order=order,
            y=colname,
            data=data,
            ax=ax,
            size=swarmsize,
        )

        # connect subj dots with lines
        # https://stackoverflow.com/a/51157346/5201771
        _idx1, _idx2 = find_dot_idxs(ax, int(df_accs.shape[0] / 2))
        locs1 = ax.get_children()[_idx1].get_offsets()
        locs2 = ax.get_children()[_idx2].get_offsets()

        # before plotting, we need to sort so that the data points correspond
        sort_idxs1 = np.argsort(data[data["stream"] == STREAMS[0]][colname].to_numpy())
        sort_idxs2 = np.argsort(data[data["stream"] == STREAMS[1]][colname].to_numpy())
        locs2_sorted = locs2[sort_idxs2.argsort()][sort_idxs1]

        for i in range(locs1.shape[0]):
            _x = [locs1[i, 0], locs2_sorted[i, 0]]
            _y = [locs1[i, 1], locs2_sorted[i, 1]]
            ax.plot(_x, _y, **subj_line_settings)

        ax.axhline(hlines[param], xmax=0.95, **axhline_args)
        sns.despine(ax=ax)

        ax.text(
            x=0.5,
            y=0.9,
            s=param,
            ha="center",
            va="center",
            transform=ax.transAxes,
        )

        ylabel = dict(kappa="$k$", bias="$b$", noise="$s$")[param]
        ax.set_xlabel("")
        ax.set_ylabel(ylabel)
        ax.set_xticklabels([i.capitalize() for i in STREAMS])

# %%
# Final settings and save
# axs[1, 0].set_yscale("log")
# axs[1, 0].set_ylim(0.2, None)
# Save the figure
fig.savefig(fname_fig1, bbox_inches="tight")

# %%


import datetime
import logging
import os

from dotenv import load_dotenv
from pymongo import MongoClient

from aiogram.utils.exceptions import BotBlocked, BotKicked, UserDeactivated

load_dotenv()

BOT_TOKEN = os.getenv("BOT_TOKEN")
MONGO_URL = os.getenv("MONGO_URL")
ADMIN_IDS = tuple(os.getenv("ADMIN_IDS").split(","))
GROUP_ID = int(os.getenv("GROUP_ID"))

cluster = MongoClient(MONGO_URL)
collusers = cluster.bot.users
collreports = cluster.bot.reports
collquestions = cluster.bot.questions
collanswers = cluster.bot.answers

all_content_types = ["text", "sticker", "photo",
                     "voice", "document", "video", "video_note"]

if not os.getenv("DEBUG"):
    formatter = '[%(asctime)s] %(levelname)8s --- %(message)s (%(filename)s:%(lineno)s)'
    logging.basicConfig(
        filename=f'logs/bot-from-{datetime.datetime.now().date()}.log',
        filemode='w',
        format=formatter,
        datefmt='%Y-%m-%d %H:%M:%S',
        level=logging.WARNING
    )

async def on_startup(dp):
    print("Bot are started!")
    for i in ADMIN_IDS:
        try:
            await dp.bot.send_message(i, "Bot are start!")
        except (BotKicked, BotBlocked, UserDeactivated):
            pass

async def on_shutdown(dp):
    logging.warning("Shutting down..")
    for i in ADMIN_IDS:
        try:
            await dp.bot.send_message(i, "Bot are shutting down!")
        except (BotKicked, BotBlocked, UserDeactivated):
            pass
    await dp.storage.close()
    await dp.storage.wait_closed()
    logging.warning("Bye!")


import datetime
from unittest import mock

import pytest
import staticconf
import staticconf.testing
from kubernetes.client import V1Container
from kubernetes.client import V1ObjectMeta
from kubernetes.client import V1Pod
from kubernetes.client import V1PodCondition
from kubernetes.client import V1PodSpec
from kubernetes.client import V1PodStatus
from kubernetes.client import V1ResourceRequirements

from clusterman.signals.pending_pods_signal import PendingPodsSignal
from clusterman.util import ClustermanResources
from clusterman.util import SignalResourceRequest

@pytest.fixture
def pending_pods_signal():
    with staticconf.testing.PatchConfiguration({"autoscale_signal.period_minutes": 5}, namespace="bar.kube_config"):
        return PendingPodsSignal(
            "foo",
            "bar",
            "kube",
            "app1",
            "bar.kube_config",
            mock.Mock(),
            mock.Mock(get_unschedulable_pods=mock.Mock(return_value=[])),
        )

@pytest.fixture
def allocated_resources():
    return ClustermanResources(cpus=150, mem=300000, disk=500000, gpus=0)

@pytest.fixture
def total_resources():
    return ClustermanResources(cpus=200, mem=350000, disk=750000, gpus=0)

@pytest.fixture
def target_capacity_margin():
    return 0.02

@pytest.fixture
def pending_pods():
    return [
        V1Pod(
            metadata=V1ObjectMeta(
                name="pod1",
                creation_timestamp=datetime.datetime(2023, 1, 1, 12, 0, 0, tzinfo=datetime.timezone.utc),
            ),
            status=V1PodStatus(
                phase="Pending",
                conditions=[V1PodCondition(status="False", type="PodScheduled", reason="Unschedulable")],
            ),
            spec=V1PodSpec(
                containers=[
                    V1Container(
                        name="container1",
                        resources=V1ResourceRequirements(requests={"cpu": "1.5", "memory": "1500MB"}),
                    ),
                    V1Container(
                        name="container1",
                        resources=V1ResourceRequirements(requests={"cpu": "1.5", "memory": "3500MB"}),
                    ),
                ]
            ),
        ),
    ]

# Just the existing resources, no pending pods
def test_get_resource_request_no_pending_pods(allocated_resources, pending_pods_signal):
    assert pending_pods_signal._get_resource_request(allocated_resources) == SignalResourceRequest(
        cpus=150,
        mem=300000,
        disk=500000,
        gpus=0,
    )

# Just the increase from pending pods (2 pods with 1.5 CPU = 3 x default multiplier 2), with no existing resources
def test_get_resource_request_only_pending_pods(pending_pods, pending_pods_signal):
    assert pending_pods_signal._get_resource_request(ClustermanResources(), pending_pods) == SignalResourceRequest(
        cpus=6,
        mem=10000,
        disk=0,
        gpus=0,
    )

# Existing resources AND pending pods, so sum of both
def test_get_resource_request_pending_pods_and_metrics(allocated_resources, pending_pods, pending_pods_signal):
    assert pending_pods_signal._get_resource_request(allocated_resources, pending_pods) == SignalResourceRequest(
        cpus=156,
        mem=310000,
        disk=500000,
        gpus=0,
    )

# Increase from pending pods but higher multipler
def test_get_resource_request_only_pending_pods_custom_multipler(pending_pods, pending_pods_signal):
    pending_pods_signal.parameters["pending_pods_multiplier"] = 20
    assert pending_pods_signal._get_resource_request(ClustermanResources(), pending_pods) == SignalResourceRequest(
        cpus=60,
        mem=100000,
        disk=0,
        gpus=0,
    )

def test_get_resource_request_v2_no_pending_pods(
    allocated_resources,
    total_resources,
    target_capacity_margin,
    pending_pods_signal,
):
    assert pending_pods_signal._get_resource_request_v2(
        allocated_resources,
        total_resources,
        target_capacity_margin,
    ) == SignalResourceRequest(cpus=150, mem=300000, disk=500000, gpus=0)

def test_get_resource_request_v2_pending_pods_and_metrics(
    allocated_resources,
    total_resources,
    target_capacity_margin,
    pending_pods,
    pending_pods_signal,
):
    assert pending_pods_signal._get_resource_request_v2(
        allocated_resources,
        total_resources,
        target_capacity_margin,
        pending_pods,
    ) == SignalResourceRequest(cpus=206, mem=360000, disk=750000, gpus=0)

def test_get_resource_request_v2_pending_pods_and_metrics_bigger_margin(
    allocated_resources,
    total_resources,
    pending_pods,
    pending_pods_signal,
):
    assert pending_pods_signal._get_resource_request_v2(
        allocated_resources,
        total_resources,
        0.05,
        pending_pods,
    ) == SignalResourceRequest(cpus=210, mem=367500, disk=787500, gpus=0)


#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function

import argparse
import os
import sys

py3 = sys.version_info.major == 3

def warn(msg):
    print('[powerline-bash] ', msg)

if py3:
    def unicode(x):
        return x

class Powerline:
    symbols = {
        'compatible': {
            'lock': 'RO',
            'network': 'SSH',
            'separator': u'\u25B6',
            'separator_thin': u'\u276F'
        },
        'patched': {
            'lock': u'\uE0A2',
            'network': u'\uE0A2',
            'separator': u'\uE0B0',
            'separator_thin': u'\uE0B1'
        },
        'flat': {
            'lock': '',
            'network': '',
            'separator': '',
            'separator_thin': ''
        },
    }

    color_templates = {
        'bash': '\\[\\e%s\\]',
        'zsh': '%%{%s%%}',
        'bare': '%s',
    }

    def __init__(self, args, cwd):
        self.args = args
        self.cwd = cwd
        mode, shell = args.mode, args.shell
        self.color_template = self.color_templates[shell]
        self.reset = self.color_template % '[0m'
        self.lock = Powerline.symbols[mode]['lock']
        self.network = Powerline.symbols[mode]['network']
        self.separator = Powerline.symbols[mode]['separator']
        self.separator_thin = Powerline.symbols[mode]['separator_thin']
        self.segments = []

    def color(self, prefix, code):
        if code is None:
            return ''
        elif code is -1:
            return self.color_template % ('[0m')
        else:
            return self.color_template % ('[%s;5;%sm' % (prefix, code))

    def fgcolor(self, code):
        return self.color('38', code)

    def bgcolor(self, code):
        return self.color('48', code)

    def append(self, content, fg, bg, separator=None, separator_fg=None):
        self.segments.append((content, fg, bg,
            separator if separator is not None else self.separator,
            separator_fg if separator_fg is not None else bg))

    def draw(self):
        text = (''.join(self.draw_segment(i) for i in range(len(self.segments)))
                + self.reset) + ' '
        if py3:
            return text
        else:
            return text.encode('utf-8')

    def draw_segment(self, idx):
        segment = self.segments[idx]
        next_segment = self.segments[idx + 1] if idx < len(self.segments)-1 else None

        return ''.join((
            self.fgcolor(segment[1]),
            self.bgcolor(segment[2]),
            segment[0],
            self.bgcolor(next_segment[2]) if next_segment else self.reset,
            self.fgcolor(segment[4]),
            segment[3]))

class RepoStats:
    symbols = {
        'detached': u'\u2693',
        'ahead': u'\u2B06',
        'behind': u'\u2B07',
        'staged': u'\u2714',
        'not_staged': u'\u270E',
        'untracked': u'\u2753',
        'conflicted': u'\u273C'
    }

    def __init__(self):
        self.ahead = 0
        self.behind = 0
        self.untracked = 0
        self.not_staged = 0
        self.staged = 0
        self.conflicted = 0

    @property
    def dirty(self):
        qualifiers = [
            self.untracked,
            self.not_staged,
            self.staged,
            self.conflicted,
        ]
        return sum(qualifiers) > 0

    def __getitem__(self, _key):
        return getattr(self, _key)

    def n_or_empty(self, _key):
        """Given a string name of one of the properties of this class, returns
        the value of the property as a string when the value is greater than
        1. When it is not greater than one, returns an empty string.

        As an example, if you want to show an icon for untracked files, but you
        only want a number to appear next to the icon when there are more than
        one untracked files, you can do:

            segment = repo_stats.n_or_empty("untracked") + icon_string
        """
        return unicode(self[_key]) if int(self[_key]) > 1 else u''

    def add_to_powerline(self, powerline, color):
        def add(_key, fg, bg):
            if self[_key]:
                s = u" {}{} ".format(self.n_or_empty(_key), self.symbols[_key])
                powerline.append(s, fg, bg)
        add('ahead', color.GIT_AHEAD_FG, color.GIT_AHEAD_BG)
        add('behind', color.GIT_BEHIND_FG, color.GIT_BEHIND_BG)
        add('staged', color.GIT_STAGED_FG, color.GIT_STAGED_BG)
        add('not_staged', color.GIT_NOTSTAGED_FG, color.GIT_NOTSTAGED_BG)
        add('untracked', color.GIT_UNTRACKED_FG, color.GIT_UNTRACKED_BG)
        add('conflicted', color.GIT_CONFLICTED_FG, color.GIT_CONFLICTED_BG)

def get_valid_cwd():
    """ We check if the current working directory is valid or not. Typically
        happens when you checkout a different branch on git that doesn't have
        this directory.
        We return the original cwd because the shell still considers that to be
        the working directory, so returning our guess will confuse people
    """
    # Prefer the PWD environment variable. Python's os.getcwd function follows
    # symbolic links, which is undesirable. But if PWD is not set then fall
    # back to this func
    try:
        cwd = os.getenv('PWD') or os.getcwd()
    except:
        warn("Your current directory is invalid. If you open a ticket at " +
            "https://github.com/milkbikis/powerline-shell/issues/new " +
            "we would love to help fix the issue.")
        sys.stdout.write("> ")
        sys.exit(1)

    parts = cwd.split(os.sep)
    up = cwd
    while parts and not os.path.exists(up):
        parts.pop()
        up = os.sep.join(parts)
    if cwd != up:
        warn("Your current directory is invalid. Lowest valid directory: "
            + up)
    return cwd

if __name__ == "__main__":
    arg_parser = argparse.ArgumentParser()
    arg_parser.add_argument('--cwd-mode', action='store',
            help='How to display the current directory', default='fancy',
            choices=['fancy', 'plain', 'dironly'])
    arg_parser.add_argument('--cwd-only', action='store_true',
            help='Deprecated. Use --cwd-mode=dironly')
    arg_parser.add_argument('--cwd-max-depth', action='store', type=int,
            default=5, help='Maximum number of directories to show in path')
    arg_parser.add_argument('--cwd-max-dir-size', action='store', type=int,
            help='Maximum number of letters displayed for each directory in the path')
    arg_parser.add_argument('--colorize-hostname', action='store_true',
            help='Colorize the hostname based on a hash of itself.')
    arg_parser.add_argument('--mode', action='store', default='patched',
            help='The characters used to make separators between segments',
            choices=['patched', 'compatible', 'flat'])
    arg_parser.add_argument('--shell', action='store', default='bash',
            help='Set this to your shell type', choices=['bash', 'zsh', 'bare'])
    arg_parser.add_argument('prev_error', nargs='?', type=int, default=0,
            help='Error code returned by the last command')
    args = arg_parser.parse_args()

    powerline = Powerline(args, get_valid_cwd())

class DefaultColor:
    """
    This class should have the default colors for every segment.
    Please test every new segment with this theme first.
    """
    USERNAME_FG = 250
    USERNAME_BG = 240
    USERNAME_ROOT_BG = 124

    HOSTNAME_FG = 250
    HOSTNAME_BG = 238

    HOME_SPECIAL_DISPLAY = True
    HOME_BG = 31  # blueish
    HOME_FG = 15  # white
    PATH_BG = 237  # dark grey
    PATH_FG = 250  # light grey
    CWD_FG = 254  # nearly-white grey
    SEPARATOR_FG = 244

    READONLY_BG = 124
    READONLY_FG = 254

    SSH_BG = 166 # medium orange
    SSH_FG = 254

    REPO_CLEAN_BG = 148  # a light green color
    REPO_CLEAN_FG = 0  # black
    REPO_DIRTY_BG = 161  # pink/red
    REPO_DIRTY_FG = 15  # white

    JOBS_FG = 39
    JOBS_BG = 238

    CMD_PASSED_BG = 236
    CMD_PASSED_FG = 15
    CMD_FAILED_BG = 161
    CMD_FAILED_FG = 15

    SVN_CHANGES_BG = 148
    SVN_CHANGES_FG = 22  # dark green

    GIT_AHEAD_BG = 240
    GIT_AHEAD_FG = 250
    GIT_BEHIND_BG = 240
    GIT_BEHIND_FG = 250
    GIT_STAGED_BG = 22
    GIT_STAGED_FG = 15
    GIT_NOTSTAGED_BG = 130
    GIT_NOTSTAGED_FG = 15
    GIT_UNTRACKED_BG = 52
    GIT_UNTRACKED_FG = 15
    GIT_CONFLICTED_BG = 9
    GIT_CONFLICTED_FG = 15

    VIRTUAL_ENV_BG = 35  # a mid-tone green
    VIRTUAL_ENV_FG = 00

class Color(DefaultColor):
    """
    This subclass is required when the user chooses to use 'default' theme.
    Because the segments require a 'Color' class for every theme.
    """
    pass

def add_set_term_title_segment(powerline):
    term = os.getenv('TERM')
    if not (('xterm' in term) or ('rxvt' in term)):
        return

    if powerline.args.shell == 'bash':
        set_title = '\\[\\e]0;\\u@\\h: \\w\\a\\]'
    elif powerline.args.shell == 'zsh':
        set_title = '\033]0;%n@%m: %~\007'
    else:
        import socket
        set_title = '\033]0;%s@%s: %s\007' % (os.getenv('USER'), socket.gethostname().split('.')[0], powerline.cwd or os.getenv('PWD'))

    powerline.append(set_title, None, None, '')

add_set_term_title_segment(powerline)

def add_username_segment(powerline):
    import os
    if powerline.args.shell == 'bash':
        user_prompt = ' \\u '
    elif powerline.args.shell == 'zsh':
        user_prompt = ' %n '
    else:
        user_prompt = ' %s ' % os.getenv('USER')

    if os.getenv('USER') == 'root':
        bgcolor = Color.USERNAME_ROOT_BG
    else:
        bgcolor = Color.USERNAME_BG

    powerline.append(user_prompt, Color.USERNAME_FG, bgcolor)

add_username_segment(powerline)
def add_hostname_segment(powerline):
    if powerline.args.colorize_hostname:
        from lib.color_compliment import stringToHashToColorAndOpposite
        from lib.colortrans import rgb2short
        from socket import gethostname
        hostname = gethostname()
        FG, BG = stringToHashToColorAndOpposite(hostname)
        FG, BG = (rgb2short(*color) for color in [FG, BG])
        host_prompt = ' %s ' % hostname.split('.')[0]

        powerline.append(host_prompt, FG, BG)
    else:
        if powerline.args.shell == 'bash':
            host_prompt = ' \\h '
        elif powerline.args.shell == 'zsh':
            host_prompt = ' %m '
        else:
            import socket
            host_prompt = ' %s ' % socket.gethostname().split('.')[0]

        powerline.append(host_prompt, Color.HOSTNAME_FG, Color.HOSTNAME_BG)

add_hostname_segment(powerline)
import os

def add_ssh_segment(powerline):

    if os.getenv('SSH_CLIENT'):
        powerline.append(' %s ' % powerline.network, Color.SSH_FG, Color.SSH_BG)

add_ssh_segment(powerline)
import os

ELLIPSIS = u'\u2026'

def replace_home_dir(cwd):
    home = os.getenv('HOME')
    if cwd.startswith(home):
        return '~' + cwd[len(home):]
    return cwd

def split_path_into_names(cwd):
    names = cwd.split(os.sep)

    if names[0] == '':
        names = names[1:]

    if not names[0]:
        return ['/']

    return names

def requires_special_home_display(name):
    """Returns true if the given directory name matches the home indicator and
    the chosen theme should use a special home indicator display."""
    return (name == '~' and Color.HOME_SPECIAL_DISPLAY)

def maybe_shorten_name(powerline, name):
    """If the user has asked for each directory name to be shortened, will
    return the name up to their specified length. Otherwise returns the full
    name."""
    if powerline.args.cwd_max_dir_size:
        return name[:powerline.args.cwd_max_dir_size]
    return name

def get_fg_bg(name):
    """Returns the foreground and background color to use for the given name.
    """
    if requires_special_home_display(name):
        return (Color.HOME_FG, Color.HOME_BG,)
    return (Color.PATH_FG, Color.PATH_BG,)

def add_cwd_segment(powerline):
    cwd = powerline.cwd or os.getenv('PWD')
    if not py3:
        cwd = cwd.decode("utf-8")
    cwd = replace_home_dir(cwd)

    if powerline.args.cwd_mode == 'plain':
        powerline.append(' %s ' % (cwd,), Color.CWD_FG, Color.PATH_BG)
        return

    names = split_path_into_names(cwd)

    max_depth = powerline.args.cwd_max_depth
    if max_depth <= 0:
        warn("Ignoring --cwd-max-depth argument since it's not greater than 0")
    elif len(names) > max_depth:
        # https://github.com/milkbikis/powerline-shell/issues/148
        # n_before is the number is the number of directories to put before the
        # ellipsis. So if you are at ~/a/b/c/d/e and max depth is 4, it will
        # show `~ a ... d e`.
        #
        # max_depth must be greater than n_before or else you end up repeating
        # parts of the path with the way the splicing is written below.
        n_before = 2 if max_depth > 2 else max_depth - 1
        names = names[:n_before] + [ELLIPSIS] + names[n_before - max_depth:]

    if (powerline.args.cwd_mode == 'dironly' or powerline.args.cwd_only):
        # The user has indicated they only want the current directory to be
        # displayed, so chop everything else off
        names = names[-1:]

    for i, name in enumerate(names):
        fg, bg = get_fg_bg(name)

        separator = powerline.separator_thin
        separator_fg = Color.SEPARATOR_FG
        is_last_dir = (i == len(names) - 1)
        if requires_special_home_display(name) or is_last_dir:
            separator = None
            separator_fg = None

        powerline.append(' %s ' % maybe_shorten_name(powerline, name), fg, bg,
                         separator, separator_fg)

add_cwd_segment(powerline)
import os

def add_read_only_segment(powerline):
    cwd = powerline.cwd or os.getenv('PWD')

    if not os.access(cwd, os.W_OK):
        powerline.append(' %s ' % powerline.lock, Color.READONLY_FG, Color.READONLY_BG)

add_read_only_segment(powerline)
import re
import subprocess
import os

def get_PATH():
    """Normally gets the PATH from the OS. This function exists to enable
    easily mocking the PATH in tests.
    """
    return os.getenv("PATH")

def git_subprocess_env():
    return {
        # LANG is specified to ensure git always uses a language we are expecting.
        # Otherwise we may be unable to parse the output.
        "LANG": "C",

        # https://github.com/milkbikis/powerline-shell/pull/126
        "HOME": os.getenv("HOME"),

        # https://github.com/milkbikis/powerline-shell/pull/153
        "PATH": get_PATH(),
    }

def parse_git_branch_info(status):
    info = re.search('^## (?P<local>\S+?)''(\.{3}(?P<remote>\S+?)( \[(ahead (?P<ahead>\d+)(, )?)?(behind (?P<behind>\d+))?\])?)?$', status[0])
    return info.groupdict() if info else None

def _get_git_detached_branch():
    p = subprocess.Popen(['git', 'describe', '--tags', '--always'],
                         stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                         env=git_subprocess_env())
    detached_ref = p.communicate()[0].decode("utf-8").rstrip('\n')
    if p.returncode == 0:
        branch = u'{} {}'.format(RepoStats.symbols['detached'], detached_ref)
    else:
        branch = 'Big Bang'
    return branch

def parse_git_stats(status):
    stats = RepoStats()
    for statusline in status[1:]:
        code = statusline[:2]
        if code == '??':
            stats.untracked += 1
        elif code in ('DD', 'AU', 'UD', 'UA', 'DU', 'AA', 'UU'):
            stats.conflicted += 1
        else:
            if code[1] != ' ':
                stats.not_staged += 1
            if code[0] != ' ':
                stats.staged += 1

    return stats

def add_git_segment(powerline):
    try:
        p = subprocess.Popen(['git', 'status', '--porcelain', '-b'],
                             stdout=subprocess.PIPE, stderr=subprocess.PIPE,
                             env=git_subprocess_env())
    except OSError:
        # Popen will throw an OSError if git is not found
        return

    pdata = p.communicate()
    if p.returncode != 0:
        return

    status = pdata[0].decode("utf-8").splitlines()
    stats = parse_git_stats(status)
    branch_info = parse_git_branch_info(status)

    if branch_info:
        stats.ahead = branch_info["ahead"]
        stats.behind = branch_info["behind"]
        branch = branch_info['local']
    else:
        branch = _get_git_detached_branch()

    bg = Color.REPO_CLEAN_BG
    fg = Color.REPO_CLEAN_FG
    if stats.dirty:
        bg = Color.REPO_DIRTY_BG
        fg = Color.REPO_DIRTY_FG

    powerline.append(' %s ' % branch, fg, bg)
    stats.add_to_powerline(powerline, Color)

add_git_segment(powerline)
import os
import re
import subprocess

def add_jobs_segment(powerline):
    pppid_proc = subprocess.Popen(['ps', '-p', str(os.getppid()), '-oppid='],
                                  stdout=subprocess.PIPE)
    pppid = pppid_proc.communicate()[0].decode("utf-8").strip()

    output_proc = subprocess.Popen(['ps', '-a', '-o', 'ppid'],
                                   stdout=subprocess.PIPE)
    output = output_proc.communicate()[0].decode("utf-8")

    num_jobs = len(re.findall(str(pppid), output)) - 1

    if num_jobs > 0:
        powerline.append(' %d ' % num_jobs, Color.JOBS_FG, Color.JOBS_BG)

add_jobs_segment(powerline)
def add_root_segment(powerline):
    root_indicators = {
        'bash': ' \\$ ',
        'zsh': ' %# ',
        'bare': ' $ ',
    }
    bg = Color.CMD_PASSED_BG
    fg = Color.CMD_PASSED_FG
    if powerline.args.prev_error != 0:
        fg = Color.CMD_FAILED_FG
        bg = Color.CMD_FAILED_BG
    powerline.append("\n", -1, -1, '')
    powerline.append(root_indicators[powerline.args.shell], fg, bg)

add_root_segment(powerline)
sys.stdout.write(powerline.draw())


"""
Decomposition Segregation based Metrics
"""

__author__ = "Renan X. Cortes <renanc@ucr.edu>, Elijah Knaap <elijah.knaap@ucr.edu>, and Sergio J. Rey <sergio.rey@ucr.edu>"

import warnings
from pysal.explore.segregation.util.util import _generate_counterfactual, _dep_message, DeprecationHelper

# Including old and new api in __all__ so users can use both

__all__ = ['Decompose_Segregation',
           'DecomposeSegregation']

# The Deprecation calls of the classes are located in the end of this script #

def _decompose_segregation(index1,
                           index2,
                           counterfactual_approach='composition'):
    """Decompose segregation differences into spatial and attribute components.

    Given two segregation indices of the same type, use Shapley decomposition
    to measure whether the differences between index measures arise from
    differences in spatial structure or population structure

    Parameters
    ----------
    index1 : segregation.SegIndex class
        First SegIndex class to compare.
    index2 : segregation.SegIndex class
        Second SegIndex class to compare.
    counterfactual_approach : str, one of
                              ["composition", "share", "dual_composition"]
        The technique used to generate the counterfactual population
        distributions.

    Returns
    -------
    tuple
        (shapley spatial component,
         shapley attribute component,
         core data of index1,
         core data of index2,
         data with counterfactual variables for index1,
         data with counterfactual variables for index2)

    """
    df1 = index1.core_data.copy()
    df2 = index2.core_data.copy()

    assert index1._function == index2._function, "Segregation indices must be of the same type"

    counterfac_df1, counterfac_df2 = _generate_counterfactual(
        df1,
        df2,
        'group_pop_var',
        'total_pop_var',
        counterfactual_approach=counterfactual_approach)

    seg_func = index1._function

    # index for spatial 1, attribute 1
    G_S1_A1 = index1.statistic

    # index for spatial 2, attribute 2
    G_S2_A2 = index2.statistic

    # index for spatial 1 attribute 2 (counterfactual population for structure 1)
    G_S1_A2 = seg_func(counterfac_df1, 'counterfactual_group_pop',
                       'counterfactual_total_pop')[0]

    # index for spatial 2 attribute 1 (counterfactual population for structure 2)
    G_S2_A1 = seg_func(counterfac_df2, 'counterfactual_group_pop',
                       'counterfactual_total_pop')[0]

    # take the average difference in spatial structure, holding attributes constant
    C_S = 1 / 2 * (G_S1_A1 - G_S2_A1 + G_S1_A2 - G_S2_A2)

    # take the average difference in attributes, holding spatial structure constant
    C_A = 1 / 2 * (G_S1_A1 - G_S1_A2 + G_S2_A1 - G_S2_A2)

    return C_S, C_A, df1, df2, counterfac_df1, counterfac_df2, counterfactual_approach

class DecomposeSegregation:
    """Decompose segregation differences into spatial and attribute components.

    Given two segregation indices of the same type, use Shapley decomposition
    to measure whether the differences between index measures arise from
    differences in spatial structure or population structure

    Parameters
    ----------
    index1 : segregation.SegIndex class
        First SegIndex class to compare.
    index2 : segregation.SegIndex class
        Second SegIndex class to compare.
    counterfactual_approach : str, one of
                              ["composition", "share", "dual_composition"]
        The technique used to generate the counterfactual population
        distributions.

    Attributes
    ----------

    c_s : float
        Shapley's Spatial Component of the decomposition

    c_a : float
        Shapley's Attribute Component of the decomposition

    Methods
    ----------

    plot : Visualize features of the Decomposition performed
        plot_type : str, one of ['cdfs', 'maps']

        'cdfs' : visualize the cumulative distribution functions of the compositions/shares
        'maps' : visualize the spatial distributions for original data and counterfactuals generated and Shapley's components (only available for GeoDataFrames)

    Examples
    --------
    Several examples can be found at https://github.com/pysal/segregation/blob/master/notebooks/decomposition_wrapper_example.ipynb.

    """

    def __init__(self, index1, index2, counterfactual_approach='composition'):

        aux = _decompose_segregation(index1, index2, counterfactual_approach)

        self.c_s = aux[0]
        self.c_a = aux[1]
        self._df1 = aux[2]
        self._df2 = aux[3]
        self._counterfac_df1 = aux[4]
        self._counterfac_df2 = aux[5]
        self._counterfactual_approach = aux[6]

    def plot(self, plot_type='cdfs'):
        """
        Plot the Segregation Decomposition Profile
        """
        try:
            import matplotlib.pyplot as plt
        except ImportError:
            warnings.warn('This method relies on importing `matplotlib`')

        if (plot_type == 'cdfs'):
            if (self._counterfactual_approach == 'composition'):
                plt.suptitle(
                    'Spatial Component = {}, Attribute Component: {}'.format(
                        round(self.c_s, 3), round(self.c_a, 3)),
                    size=20)
                plt.step(
                    self._counterfac_df1['group_composition'].sort_values(),
                    self._counterfac_df1['group_composition'].rank(
                        pct=True).sort_values(),
                    label='First Context Group Composition')

                plt.step(
                    self._counterfac_df2['group_composition'].sort_values(),
                    self._counterfac_df2['group_composition'].rank(
                        pct=True).sort_values(),
                    label='Second Context Group Composition')
                plt.legend()

            if (self._counterfactual_approach == 'share'):
                plt.suptitle(
                    'Spatial Component = {}, Attribute Component: {}'.format(
                        round(self.c_s, 3), round(self.c_a, 3)),
                    size=20)
                plt.step((self._df1['group_pop_var'] /
                          self._df1['group_pop_var'].sum()).sort_values(),
                         (self._df1['group_pop_var'] /
                          self._df1['group_pop_var'].sum()).rank(
                              pct=True).sort_values(),
                         label='First Context Group Share')

                plt.step((self._df2['group_pop_var'] /
                          self._df2['group_pop_var'].sum()).sort_values(),
                         (self._df2['group_pop_var'] /
                          self._df2['group_pop_var'].sum()).rank(
                              pct=True).sort_values(),
                         label='Second Context Group Share')

                plt.step(
                    ((self._df1['total_pop_var'] - self._df1['group_pop_var'])
                     / (self._df1['total_pop_var'] -
                        self._df1['group_pop_var']).sum()).sort_values(),
                    ((self._df1['total_pop_var'] - self._df1['group_pop_var'])
                     / (self._df1['total_pop_var'] -
                        self._df1['group_pop_var']).sum()).rank(
                            pct=True).sort_values(),
                    label='First Context Complementary Group Share')

                plt.step(
                    ((self._df2['total_pop_var'] - self._df2['group_pop_var'])
                     / (self._df2['total_pop_var'] -
                        self._df2['group_pop_var']).sum()).sort_values(),
                    ((self._df2['total_pop_var'] - self._df2['group_pop_var'])
                     / (self._df2['total_pop_var'] -
                        self._df2['group_pop_var']).sum()).rank(
                            pct=True).sort_values(),
                    label='Second Context Complementary Group Share')
                plt.legend()

            if (self._counterfactual_approach == 'dual_composition'):
                plt.suptitle(
                    'Spatial Component = {}, Attribute Component: {}'.format(
                        round(self.c_s, 3), round(self.c_a, 3)),
                    size=20)
                plt.step(
                    self._counterfac_df1['group_composition'].sort_values(),
                    self._counterfac_df1['group_composition'].rank(
                        pct=True).sort_values(),
                    label='First Context Group Composition')

                plt.step(
                    self._counterfac_df2['group_composition'].sort_values(),
                    self._counterfac_df2['group_composition'].rank(
                        pct=True).sort_values(),
                    label='Second Context Group Composition')

                plt.step(
                    (1 -
                     self._counterfac_df1['group_composition']).sort_values(),
                    (1 - self._counterfac_df1['group_composition']).rank(
                        pct=True).sort_values(),
                    label='First Context Complementary Group Composition')

                plt.step(
                    (1 -
                     self._counterfac_df2['group_composition']).sort_values(),
                    (1 - self._counterfac_df2['group_composition']).rank(
                        pct=True).sort_values(),
                    label='Second Context Complementary Group Composition')

                plt.legend()

        if (plot_type == 'maps'):
            if (str(type(self._df1)) !=
                    '<class \'geopandas.geodataframe.GeoDataFrame\'>'):
                raise TypeError(
                    'data is not a GeoDataFrame and, therefore, maps cannot be draw.'
                )

            # Subplots
            fig, axs = plt.subplots(2, 2, figsize=(10, 10))

            fig.suptitle(
                'Spatial Component = {}, Attribute Component: {}'.format(
                    round(self.c_s, 3), round(self.c_a, 3)),
                size=20)
            fig.subplots_adjust(hspace=1.25, wspace=0.2,
                                top=0.95)  # hspace space between lines
            fig.tight_layout(rect=[
                0, 0, 1, 0.925
            ])  # rect is to position the suptitle above the subplots

            # Original First Context (Upper Left)
            self._counterfac_df1.plot(column='group_composition',
                                      cmap='OrRd',
                                      legend=True,
                                      ax=axs[0, 0])
            axs[0, 0].title.set_text('Original First Context Composition')
            axs[0, 0].axis('off')

            # Counterfactual First Context (Bottom Left)
            self._counterfac_df1.plot(column='counterfactual_composition',
                                      cmap='OrRd',
                                      legend=True,
                                      ax=axs[1, 0])
            axs[1, 0].title.set_text(
                'Counterfactual First Context Composition')
            axs[1, 0].axis('off')

            # Counterfactual Second Context (Upper Right)
            self._counterfac_df2.plot(column='counterfactual_composition',
                                      cmap='OrRd',
                                      legend=True,
                                      ax=axs[0, 1])
            axs[0, 1].title.set_text(
                'Counterfactual Second Context Composition')
            axs[0, 1].axis('off')

            # Original Second Context (Bottom Right)
            self._counterfac_df2.plot(column='group_composition',
                                      cmap='OrRd',
                                      legend=True,
                                      ax=axs[1, 1])
            axs[1, 1].title.set_text('Original Second Context Composition')
            axs[1, 1].axis('off')

# Deprecation Calls

msg = _dep_message("Decompose_Segregation", "DecomposeSegregation")
Decompose_Segregation = DeprecationHelper(DecomposeSegregation, message=msg)


import argparse
import sys

import autocrit

import autocrit_tools.load as load
from autocrit_tools.path import ExperimentPaths
from autocrit_tools.util import random_string

DEFAULT_VERBOSITY = 0

DEFAULT_OPTIMIZER = "gd"
DEFAULT_OPTIMIZER_LR = 0.1
DEFAULT_OPTIMIZER_MOMENTUM = 0.1

DEFAULT_LOG_KWARGS = {"track_theta": True, "track_f": True, "track_g": False}

def main(args):

    ID = args.ID or random_string(6)
    if args.verbosity > 0:
        print("creating files for {}".format(ID))

    paths = ExperimentPaths.from_data_dir(args.data_dir,
                                          network_ID=args.network_ID, optimizer_ID=ID)
    data = load.fetch_data(paths.data)

    optimizer_kwargs = extract_optimizer_kwargs(args)
    log_kwargs = setup_log_kwargs(args)

    MLP = autocrit.nn.networks.Network.from_json(
        data, paths.network)

    if MLP.batch_size is not None:
        loss = MLP.loss_on_random_batch
    else:
        loss = MLP.loss

    optimization_experiment = autocrit.OptimizationExperiment(
        loss,
        optimizer_str=args.optimizer,
        optimizer_kwargs=optimizer_kwargs,
        log_kwargs=log_kwargs,
        seed=args.seed)

    optimization_experiment.to_json(paths.optimizer)

def extract_optimizer_kwargs(args):
    optimizer_kwargs = {"lr": args.optimizer_lr}
    if args.optimizer == "momentum":
        if args.optimizer_momentum is not None:
            optimizer_kwargs["momentum"] = args.optimizer_momentum
        else:
            optimizer_kwargs["momentum"] = DEFAULT_OPTIMIZER_MOMENTUM

    return optimizer_kwargs

def setup_log_kwargs(args):
    log_kwargs = DEFAULT_LOG_KWARGS.copy()
    if args.log_gradients:
        log_kwargs["g_theta"] = True

    return log_kwargs

def setup_parser():
    parser = argparse.ArgumentParser(
        description="Create necessary files for an optimization experiment.")

    # PROGRAM
    parser.add_argument("-v", dest="verbosity",
                        action="store_const", const=1, default=0,
                        help="verbosity flag")

    # PATHS
    parser.add_argument("--ID", type=str, default=None,
                        help="identifier for this optimization problem." +
                        "provide to over-ride default behavior, generating a random ID.")
    parser.add_argument("--data_dir", type=str,
                        help="path to directory containing dataset.")

    # NETWORK
    parser.add_argument("--network_ID", type=str,
                        help="identifier for network. must be located inside data_dir " +
                        "as subdirectory and contain network.json.")

    # OPTIMIZER
    parser.add_argument("--optimizer", type=str, default=DEFAULT_OPTIMIZER,
                        help="optimizer to apply to network loss. " +
                        "default is {}".format(DEFAULT_OPTIMIZER),
                        choices=["gd", "momentum"])
    parser.add_argument("--optimizer_lr", type=float, default=DEFAULT_OPTIMIZER_LR,
                        help="learning rate for optimizer. " +
                        "default is {}".format(DEFAULT_OPTIMIZER_LR))
    parser.add_argument("--optimizer_momentum", type=float, default=None,
                        help="momentum level for momentum optimizer. " +
                        "default is {}".format(DEFAULT_OPTIMIZER_MOMENTUM))
    parser.add_argument("--seed", type=int, default=None,
                        help="seed value for np.random and random. if not provided, " +
                        "defaults to a shared global value in autocrit.experiments.")
    parser.add_argument("--log_gradients",
                        dest="log_gradients", action="store_const", const=True, default=False,
                        help="flag to log gradients of loss with respect to theta" +
                        "during training.")

    return parser

if __name__ == "__main__":
    parser = setup_parser()
    args = parser.parse_args()
    main(args)
    sys.exit(0)


from django.shortcuts import render

from .models import OrderItem
from .forms import OrderCreateForm
from basket.basket import Basket
from .tasks import order_created, order_push

def order_create(request):
    basket = Basket(request)
    title = 'Оформление заказа'
    title1 = 'Спасибо за заказ!'
    if request.method == 'POST':
        form = OrderCreateForm(request.POST)
        if form.is_valid():
            order = form.save()
            for item in basket:
                OrderItem.objects.create(order=order,
                                         gabardin=item['gabardin'],
                                         price=item['price'],
                                         quantity=item['quantity'])

            # очистка корзины
            basket.clear()
            order_created.delay(order.id)
            order_push.delay(order.id)
            return render(request, 'orders/created.html', {'order': order, 'title': title1})
    else:
        form = OrderCreateForm
    return render(request, 'orders/create.html', {'basket': basket, 'form': form, 'title': title})


import pandas as pd
import numpy as np

from pyspark.sql import SparkSession
from pyspark.ml.linalg import Vectors as MLVectors

class PySparkTestData:

    def __init__(self, spark=None):
        if spark is None:

            spark = SparkSession.builder.appName("test").master("local").getOrCreate()

            spark.conf.set("spark.executor.memory", "120g")
            spark.conf.set("spark.driver.memory", "120g")
            spark.conf.set("spark.task.cpu", "1")
            spark.conf.set("spark.executor.extraJavaOptions", "-XX:+UseCompressedOops")
            spark.conf.set("spark.python.worker.memory", "2g")
            spark.conf.set("spark.executor.cores", "1")
            self.spark = spark

        else:
            self.spark = spark

    def get_weight_data(self):
        """

        :return:
        """
        num_samples = 100

        def tmp(x):
            _x = np.exp(x)

            _x = _x / (1 + _x)

            if _x < 0.5:
                return 0
            else:
                return 1

        df1 = pd.DataFrame(data=np.arange(num_samples), columns=["id1"])
        df1["v1"] = [MLVectors.dense([float(val) for val in np.random.random(5, ).tolist()]) for _ in range(num_samples)]
        df1["pred"] = df1["v1"].apply(lambda x: x.sum())
        df1["pred_clf"] = df1["pred"].apply(tmp)

        def tmp(x):
            _x = np.exp(x)

            _x = _x / (1 + _x)

            if _x < (1.0 / 3.0):
                return 1
            elif _x < (2.0 / 3.0):
                return 2
            else:
                return 3

        df1["pred_multi"] = df1["pred"].apply(tmp)

        sdf1 = self.spark.createDataFrame(df1)

        sdf2 = sdf1.withColumnRenamed("id1", "id2").withColumnRenamed("v1", "v2")\
            .drop("pred").drop("pred_clf").drop("pred_multi")

        return sdf1, sdf2

    def get_model_data(self):
        """

        :return:
        """
        df1 = pd.DataFrame(data=np.arange(100), columns=["id"])

        for i in range(1, 5):
            df1["col_{}".format(i)] = [float(val) for val in np.random.random(100, ).tolist()]

        df1["pred"] = df1[["col_{}".format(i) for i in range(1, 5)]].sum(axis=1)

        def tmp(x):
            _x = np.exp(x)

            _x = _x / (1 + _x)

            if _x < 0.5:
                return 0
            else:
                return 1

        df1["pred_clf"] = df1["pred"].apply(tmp)

        def tmp(x):
            _x = np.exp(x)

            _x = _x / (1 + _x)

            if _x < (1.0 / 3.0):
                return 1
            elif _x < (2.0 / 3.0):
                return 2
            else:
                return 3

        df1["pred_multi"] = df1["pred"].apply(tmp)

        sdf1 = self.spark.createDataFrame(df1)

        sdf2 = self.spark.createDataFrame(df1.drop(["pred", "pred_clf", "pred_multi"], axis=1))

        return sdf1, sdf2

    def get_nn_data(self):
        """

        :return:
        """

        df1 = pd.DataFrame(data=np.arange(5), columns=["id1"])
        df1["v1"] = [MLVectors.dense(np.random.random(5,).tolist()) for _ in range(5)]

        sdf1 = self.spark.createDataFrame(df1)

        sdf2 = sdf1.withColumnRenamed("id1", "id2").withColumnRenamed("v1", "v2")

        return sdf1, sdf2


from scipy.misc import imread,imresize,imsave
import numpy as np
import scipy
import matplotlib.pyplot as plt
from skimage.color import rgb2gray

def read(path):
    img = imread(path)
    return img

def save(path, img):
    img = imsave(path, img)

def show(img,title='', cmap=None):
    plt.figure()
    plt.title(title)
    plt.imshow(img, cmap=cmap)
    plt.show()

def to_binary(img):
    for i in range(img.shape[0]):
        for j in range(img.shape[1]):
            val = img[i][j]
            if val < 0.5:
                img[i][j] = 0
            else:
                img[i][j] = 1
    return img

if __name__ == "__main__":
    print("Don't run it directly")


# WRITE YOUR SOLUTION HERE:
import pygame
from random import *

pygame.init()
window = pygame.display.set_mode((640, 480))

robot = pygame.image.load("robot.png")

robot_x = 0
robot_y = 0
target_x = 0
target_y = 0

while True:
    for event in pygame.event.get():
        if event.type == pygame.MOUSEBUTTONDOWN:
            target_x = event.pos[0]
            target_y = event.pos[1]

            if target_x > robot_x and target_x < robot_x+robot.get_width() and target_y > robot_y and target_x < robot_x+robot.get_width():
                robot_x = randint(0,640-robot.get_width())
                robot_y = randint(0,480-robot.get_height())

        window.fill((0, 0, 0))
        window.blit(robot, (robot_x, robot_y))
        pygame.display.flip()

        if event.type == pygame.QUIT:
            exit()


import logging

def compare(expected, actual):
    """
    Checks each key from the expected json in the actual json.
    If all values checked match, returns True. Otherwise, returns False.
    """
    all_match = True
    for key, value in expected.iteritems():
        if not value==actual.get(key):
            logging.error("'%s' key did not match. Expected '%s', got '%s'" % (key, value, actual.get(key)))
            all_match = False
    return all_match


from numpy.typing import ArrayLike
from sklearn.utils.validation import check_is_fitted
from sklearn.exceptions import NotFittedError
from typing import Any
import warnings

from float.data.preprocessing import BaseScaler

class SklearnScaler(BaseScaler):
    """Wrapper for sklearn scaler functions.

    Attributes:
        scaler_obj (Any): A scikit-learn scaler object (e.g. MinMaxScaler)
    """
    def __init__(self, scaler_obj: Any, reset_after_drift: bool = False):
        """Inits the sklearn scaler.

        Args:
            scaler_obj: A scikit-learn scaler object (e.g. MinMaxScaler)
            reset_after_drift: A boolean indicating if the scaler will be reset after a drift was detected.
        """
        super().__init__(reset_after_drift=reset_after_drift)

        self.scaler_obj = scaler_obj
        self._has_partial_fit = True
        self._must_be_fitted = False
        self._validate()

    def partial_fit(self, X: ArrayLike):
        """Updates the scaler.

        Args:
            X: Array/matrix of observations.
        """
        if self._must_be_fitted:
            self.scaler_obj.fit(X)
            self._must_be_fitted = False
        elif self._has_partial_fit:
            self.scaler_obj.partial_fit(X)

    def transform(self, X: ArrayLike) -> ArrayLike:
        """Scales the given observations.

        Args:
            X: Array/matrix of observations.

        Returns:
            ArrayLike: The scaled observations.
        """
        return self.scaler_obj.transform(X)

    def reset(self):
        """Resets the scaler.

        We automatically re-fit the scaler upon the next call to partial_fit.
        """
        self._must_be_fitted = True

    def _validate(self):
        """Validates the provided scaler object.

        Raises:
            TypeError: If the sklearn scaler object does neither have a partial_fit nor a fit function.
            TypeError: If the sklearn scaler object does not have a transform function.
        """
        # Check if scaler object has a partial fit function
        partial_fit_func = getattr(self.scaler_obj, "partial_fit", None)
        if not callable(partial_fit_func):
            # Check if scaler object has a fit function
            fit_func = getattr(self.scaler_obj, "fit", None)
            if not callable(fit_func):
                raise TypeError("{} is not a valid sklearn scaler (missing 'fit' or 'partial_fit' function).".format(
                    type(self.scaler_obj).__name__))
            else:
                try:
                    self._has_partial_fit = False
                    warnings.warn(
                        "The {} scaler has no partial_fit function and will thus not be updated, which may mitigate "
                        "the overall performance.".format(type(self.scaler_obj).__name__))
                    check_is_fitted(self.scaler_obj)  # Check if scaler object has already been fitted
                except NotFittedError:
                    self._must_be_fitted = True
                    warnings.warn('Sklearn scaler object {} has not been fitted and will be fitted on the first batch '
                                  'of observations.'.format(type(self.scaler_obj).__name__))
                    pass

        # Check if scaler object has a transform function
        transform_func = getattr(self.scaler_obj, "transform", None)
        if not callable(transform_func):
            raise TypeError("{} is not a valid sklearn scaler (missing 'transform' function).".format(
                type(self.scaler_obj).__name__))


#!/usr/bin/python3
""" function that queries the Reddit API and returns the number of subscribers
    (not active users, total subscribers) for a given subreddit.
    If an invalid subreddit is given, the function should return 0
"""
import requests

def number_of_subscribers(subreddit):
    url = "http://www.reddit.com/r/{}/about.json".format(subreddit)
    headers = {
            "User-Agent": "Reddit Subscribers Checker (by your_username)"
    }

    response = requests.get(url, headers=headers)

    if response.status_code == 200:
        data = response.json()

        subscribers = data["data"]["subscribers"]
        return subscribers
    else:
        return 0

if __name__ == "__main__":
    number_of_subscribers()


import unittest
import tornado.web
import tornado.testing
from tornado.httputil import HTTPHeaders
from test.test_runner import BaseAsyncTest
import time
import rethinkdb as r
import logging
import random
import services.lorem_ipsum as lorem_ipsum
from models.basemodel import BaseModel
from models.user import User
from models.group import Group
from models.survey import Survey
from models.survey_response import SurveyResponse
from models.question_response import QuestionResponse
from models.question import Question
from unittest import mock
from handlers.base_handler import BaseHandler

class TestSurveyResults(BaseAsyncTest):
    responses_to_make = 5
    survey_response_ids = []
    question_response_ids = []
    survey_id = ""
    survey_data = {}
    user_id = ""
    user_data = {}
    group_id = ""
    question_data = []
    question_ids = []
    q1 = {
        'title': 'free response',
        "response_format": Question().RESPONSE_FREE,
        "options": []
    }
    q2 = {
        'title': 'multiple choice',
        "response_format": Question().RESPONSE_MULTIPLE_CHOICE,
        "options": ["alpha", "beta", "gamma", "delta"]
    }
    q3 = {
        'title': 'true or false',
        "response_format": Question().RESPONSE_TRUE_OR_FALSE,
        "options": [True, False]
    }
    q4 = {
        'title': 'rating',
        "response_format": Question().RESPONSE_RATING,
        "options": []
    }
    questions = []
    survey_data = {}

    def setUpClass():
        logging.disable(logging.CRITICAL)
        user_ids = [User().create_generic_item() for i in range(TestSurveyResults.responses_to_make)]
        TestSurveyResults.user_ids = user_ids
        TestSurveyResults.user_data = User().get_item(TestSurveyResults.user_id)
        TestSurveyResults.generic_group_id = Group().create_generic_item()
        for question in [TestSurveyResults.q1, TestSurveyResults.q2, TestSurveyResults.q3, TestSurveyResults.q4]:
            question_id = Question().create_item(question)
            TestSurveyResults.questions.append(question_id)
        TestSurveyResults.survey_data = {
            'id': 'test_survey',
            'item_type': 'Group',
            'item_id': TestSurveyResults.generic_group_id,
            'item_name': 'Generic Group',
            'creator_id': 'meta',
            'creator_name': 'meta',
            'responses': [],
            'created_timestamp': time.time(),
            'closed_timestamp': None,
            'deleted': False,
            'questions': TestSurveyResults.questions
        }
        Survey().create_item(TestSurveyResults.survey_data)
        return

    def test_survey_responses(self):
        self.respond_to_surveys()
        results = Survey().get_results('test_survey')
        formatted_results = Survey().get_formatted_results('test_survey')
        # logging.info(formatted_results)
        for question_response_id in self.question_response_ids:
            question_response = QuestionResponse().get_item(question_response_id)
            question_id = question_response['question_id']
            response_data = question_response['response_data']
            self.assertIn(response_data, results[question_id])
        for question_id, value in list(results.items()):
            for formatted_result_data in formatted_results:
                if formatted_result_data['id'] != question_id:
                    continue
                else:
                    self.assertEqual(len(value), len(formatted_result_data['results']))
                    self.assertEqual(sorted(value), sorted(formatted_result_data['results']))
                    response_format = formatted_result_data['response_format']
                    if response_format == Question().RESPONSE_FREE:
                        self._test_formatted_response_free(formatted_result_data)
                    elif response_format == Question().RESPONSE_RATING:
                        self._test_formatted_response_rating(formatted_result_data)
                    elif response_format == Question().RESPONSE_TRUE_OR_FALSE:
                        self._test_formatted_response_true_or_false(formatted_result_data)
                    elif response_format == Question().RESPONSE_MULTIPLE_CHOICE:
                        self._test_formatted_response_multiple_choice(formatted_result_data)
                    else:
                        self.fail("unknown response format: {0}".format(response_format))
                    break
        return

    def test_survey_results_api(self):
        formatted_results = Survey().get_formatted_results('test_survey')
        with mock.patch.object(BaseHandler, 'get_current_user') as m:
            m.return_value = self.user_ids[0]
            response = self.fetch('/api/results/test_survey', method="GET")
        result = tornado.escape.json_decode(response.body)
        self.assertEqual(response.code, 200)
        self.assertEqual(result, formatted_results)

    def _test_formatted_response_free(self, formatted_result_data):
        self.assertEqual(formatted_result_data['bar_data'], [])
        self.assertEqual(formatted_result_data['pie_data'], [])
        return

    def _test_formatted_response_rating(self, formatted_result_data):
        logging.info(formatted_result_data)
        bar_data = formatted_result_data['bar_data']
        series_data = bar_data['series'][0]
        self.assertNotEqual(bar_data, [])
        self.assertEqual(len(series_data), len(bar_data['labels']))
        self.assertEqual(sorted(bar_data['labels']), [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
        self.assertEqual(len(bar_data['labels']),
            len(series_data),
            "length of {0} does not match length of {1}".format(bar_data['labels'], series_data))
        self.assertEqual(formatted_result_data['pie_data'], [])
        return

    def _test_formatted_response_true_or_false(self, formatted_result_data):
        self.assertEqual(formatted_result_data['bar_data'], [])
        self.assertNotEqual(formatted_result_data['pie_data'], [])
        return

    def _test_formatted_response_multiple_choice(self, formatted_result_data):
        self.assertNotEqual(formatted_result_data['bar_data'], [])
        self.assertEqual(formatted_result_data['pie_data'], [])
        return

    def respond_to_surveys(self):
        survey_response_ids = []
        question_response_ids = []
        questions = self.questions

        def _respond_to_questions(questions):
            d1 = {}
            d2 = {}
            d3 = {}
            d4 = {}
            d1['question_id'] = questions[0]
            d2['question_id'] = questions[1]
            d3['question_id'] = questions[2]
            d4['question_id'] = questions[3]
            d1['response_format'] = Question().RESPONSE_FREE
            d2['response_format'] = Question().RESPONSE_MULTIPLE_CHOICE
            d3['response_format'] = Question().RESPONSE_TRUE_OR_FALSE
            d4['response_format'] = Question().RESPONSE_RATING
            d1['response_data'] = lorem_ipsum.lorem_ipsum()
            d2['response_data'] = random.choice(["alpha", "beta", "gamma", "delta"])
            d3['response_data'] = random.choice([True, False])
            d4['response_data'] = random.randint(0, 10)
            r1 = QuestionResponse().create_item(d1)
            r2 = QuestionResponse().create_item(d2)
            r3 = QuestionResponse().create_item(d3)
            r4 = QuestionResponse().create_item(d4)
            return [r1, r2, r3, r4]

        for responder_id in self.user_ids:
            question_responses = _respond_to_questions(questions)
            for response_id in question_responses:
                question_response_ids.append(response_id)
            response_data = {
                'question_responses': question_responses,
                'responder_id': responder_id,
                'survey_id': 'test_survey',
                'response_time': time.time()
            }
            survey_response_id = SurveyResponse().create_item(response_data)
            survey_response_ids.append(survey_response_id)
        self.survey_response_ids = survey_response_ids
        self.question_response_ids = question_response_ids
        return

    def tearDownClass():
        logging.disable(logging.NOTSET)
        # Drop the database
        for question_id in TestSurveyResults.questions:
            Question().delete_item(question_id)
        Survey().delete_item('test_survey')
        for survey_response in TestSurveyResults.survey_response_ids:
            SurveyResponse().delete_item(survey_response)
        for question_response in TestSurveyResults.question_response_ids:
            QuestionResponse().delete_item(question_response)
        for responder_id in TestSurveyResults.user_ids:
            User().delete_item(responder_id)


import json
import hashlib
from operator import itemgetter

from flask import Flask, render_template
from time import time
from uuid import uuid4

class Blockchain(object):
    difficulty_level = "0000"

    def __init__(self):
        self.chain = []
        self.current_transaction = []
        genesis_Hash = self.Block_Hash("genesis_block")
        self.append_block(
            Previous_block_hash=genesis_Hash,
            nonce=self.PoW(0, genesis_Hash, [])
        )

    # Hash the block
    def Block_Hash(self, block):
        # json.dumps covert the Python Object into JSON String
        blockEncoder = json.dumps(block, sort_keys=True).encode()
        return hashlib.sha256(blockEncoder).hexdigest()

    # Proof of Work
    def PoW(self, index, Previous_block_hash, transactions):
        nonce = 0
        time1 = time()
        while self.validate_proof(index, Previous_block_hash,
                                  transactions, nonce) is False:
            nonce += 1
            print(nonce)
        time_total = time() - time1
        print(time_total)
        print(nonce)
        return nonce

    # Makes sure the PoW is correct
    def validate_proof(self, index, Previous_block_hash, transactions, nonce):
        data = f'{index},{Previous_block_hash},{transactions},{nonce}'.encode()
        hash_data = hashlib.sha256(data).hexdigest()
        return hash_data[:len(self.difficulty_level)] == self.difficulty_level

    # add the block to the chain
    def append_block(self, nonce, Previous_block_hash):
        block = {
            'index': len(self.chain),
            'transactions': self.current_transaction,
            'timestamp': time(),
            'nonce': nonce,
            'Previous_block_hash': Previous_block_hash
        }
        self.current_transaction = []
        self.chain.append(block)
        return block

    # add the vote to the block
    def add_vote(self, election, voter_ID, candidate_vote):
        self.current_transaction.append({
            'candidate_vote': candidate_vote,
            'voter_ID': voter_ID,
            'election': election
        })
        return self.last_block['index'] + 1

    # return the last block in the block chain
    @property
    def last_block(self):
        return self.chain[-1]

    # Counts all the votes that have occurred for each candidate
    @property
    def all_blocks(self):
        temp = list(filter(lambda transactions: transactions['transactions'], self.chain))
        temp2 = list(map(itemgetter('transactions'), temp))
        vote1 = 0
        vote2 = 0
        for i in temp2:
            vote = list(map(itemgetter('candidate_vote'), i))
            vote = int(vote[0])
            if vote == 1:
                vote1 += 1
            elif vote == 2:
                vote2 += 1

        return vote1, vote2

    # Checks the voter ID to see if they already voted
    @property
    def check_vote_status(self):
        temp = list(filter(lambda transactions: transactions['transactions'], self.chain))
        temp2 = list(map(itemgetter('transactions'), temp))
        for i in temp2:
            voted = list(map(itemgetter('voter_ID'), i))
            if voted == list(map(itemgetter('voter_ID'), i)):
                print("you have already voted")
                return True

        return False

# Flask , #routes
app = Flask(__name__, template_folder="templates")
blockchain = Blockchain()
node_identifier = str(uuid4()).replace('-', "")

# routes ####################################################################################
@app.route('/blockchain', methods=['GET'])
def full_chain():
    response = {
        'chain': blockchain.chain,
        'length': len(blockchain.chain)
    }
    return render_template("blockchain.html", data=response)

@app.route('/', methods=['GET'])
def vote_page():
    return render_template("vote.html")

@app.route('/candidate1', methods=['GET'])
def add_vote_candidate1():
    if not blockchain.check_vote_status:
        blockchain.add_vote(
            election="0",
            voter_ID=node_identifier,
            candidate_vote=1
        )
        last_block_hash = blockchain.Block_Hash(blockchain.last_block)
        index = len(blockchain.chain)
        nonce = blockchain.PoW(index, last_block_hash, blockchain.current_transaction)
        block = blockchain.append_block(nonce, last_block_hash)
        response = {
            'message': "new block has been added",
            'index': block['index'],
            'hash_of_previous_block': block['Previous_block_hash'],
            'nonce': block['nonce'],
            'transaction': block['transactions']
        }
        return render_template("Candidate1vote.html"), response
    else:
        return render_template("AlreadyVoted.html")

@app.route('/candidate2', methods=['GET'])
def add_vote_candidate2():
    if not blockchain.check_vote_status:
        blockchain.add_vote(
            election="0",
            voter_ID=node_identifier,
            candidate_vote=2
        )
        last_block_hash = blockchain.Block_Hash(blockchain.last_block)
        index = len(blockchain.chain)
        nonce = blockchain.PoW(index, last_block_hash, blockchain.current_transaction)
        block = blockchain.append_block(nonce, last_block_hash)
        response = {
            'message': "new block has been added",
            'index': block['index'],
            'hash_of_previous_block': block['Previous_block_hash'],
            'nonce': block['nonce'],
            'transaction': block['transactions']
        }
        return render_template("Candidate2vote.html"), response
    else:
        return render_template("AlreadyVoted.html")

@app.route('/results', methods=['GET'])
def results():
    vote1, vote2 = blockchain.all_blocks
    response = {
        'vote1': vote1,
        'vote2': vote2
    }
    return render_template("results.html", data=response)

if __name__ == '__main__':
    app.run(debug=True, host='127.0.0.1', port=int(8000))


from .OptionInfo import OptionInfo
from .Image import Image
from typing import List

class CarouselItem(dict):
    """
    {
      'optionInfo': {
        object(OptionInfo)
      },
      'title': string,
      'description': string,
      'image': {
        object(Image)
      }
    }
    """

    def __init__(self, title: str = '', description: str = '', image: Image = None, option_info: OptionInfo = None):
        super(CarouselItem, self).__init__()

        if option_info is not None:
            self['optionInfo'] = option_info

        if image is not None:
            self['image'] = image

        if title is not None:
            self['title'] = title

        if description is not None:
            self['description'] = description

    def add_option_info(self, key: str, synonyms: List[str]) -> OptionInfo:
        self['optionInfo'] = OptionInfo(key=key, synonyms=synonyms)
        return self['optionInfo']

    def add_image(self, url: str, accessibility_text: str = '', height: int = 0, width: int = 0) -> Image:
        self['image'] = Image(url=url, accessibility_text=accessibility_text, height=height, width=width)
        return self['image']


from django.shortcuts import render
from django.http import HttpResponse
from webpage.models import restaurant, restaurant_menu
from django.contrib.auth.decorators import login_required
from myreview.models import Review
# Create your views here.
# @login_required(login_url='login')
def index(request):
    """ หน้าหลักแสดงร้านอาหารที่มีทั้งหมด """

    # เป็นตัวแปรเพื่อรับค่าค้นหานั้นมา (Frame)
    search_restaurant = request.GET.get('search_restaurant', '')
    # เป็นตัวแปรที่ดึงข้อมูลจาก models restaurant (Frame)
    restaurantfo = restaurant.objects.all()

    # ถ้ามีการค้นหาจะเรียกเงื่อนไขนี้มาทำแล้วแสดงข้อมูลตาม keyword ที่หาไป (Frame)
    if request.method == 'GET':
        if search_restaurant != '':
            restaurantfo = restaurant.objects.filter(restaurant_name__icontains=search_restaurant)

    # แปลงเพื่อไปใช้บนหน้า html (Frame)
    context = {
        'search_restaurant': search_restaurant,
        'resinfo': restaurantfo
    }
    return render(request, 'webpage/index.html', context=context)

def index_type(request, type_id):
    """ แสดงตามประเภท """
    # เป็นตัวแปรเพื่อรับค่าค้นหานั้นมา (Frame)
    search_restaurant = request.GET.get('search_restaurant', '')
    # เป็นตัวแปรที่ดึงข้อมูล models restaurant ที่เอาข้อมูลแค่ประเภทของร้านอาหารนั้นๆ (Frame)
    restaurantfo = restaurant.objects.filter(restaurant_type_id_id=type_id)

    if request.method == 'GET':
        if search_restaurant != '':
            restaurantfo = restaurant.objects.filter(restaurant_name__icontains=search_restaurant)
    # แปลงเพื่อไปใช้บนหน้า html (Frame)
    context = {
        'search_restaurant': search_restaurant,
        'resinfo': restaurantfo
    }
    return render(request, 'webpage/index.html', context=context)

# หน้ารายละเอียดนี่ยังไม่ได้ทำนะครับ (Frame)
def res_detail(request, restaurant_id):
    """ ดูรายละเอียดข้องร้านอาหาร และ มีปุ่มจองร้านอาหาร """
    if request.method == "POST":
        review = request.POST.get('review', '')
        print(review)
        if review != '':
            reviewjing = Review.objects.create(comment=review,user=request.user,restaurant_id=restaurant_id)

    review_list = Review.objects.filter(restaurant__id = restaurant_id)
    review_count = len(review_list)
    restaurantdt = restaurant.objects.get(pk=restaurant_id)
    menushow = restaurant_menu.objects.filter(restaurant_id_id=restaurant_id)
    context = {
        'restaurantdt': restaurantdt,
        'menushow': menushow,
        'review_list': review_list,
        'review_count': review_count,
    }
    return render(request, 'webpage/detail.html', context=context)


from typing import List

class NumArray:

    def __init__(self, nums: List[int]):
        res_count = []
        num = 0
        for i in range(len(nums)):
            num += nums[i]
            res_count.append(num)
        self.res_count = res_count

    def sumRange(self, left: int, right: int) -> int:
        if left == 0:
            return self.res_count[right]
        else:
            return self.res_count[right] - self.res_count[left - 1]

# *****
# class NumArray:
#
#     def __init__(self, nums):
#         self.sums = [0]
#         for i in nums:
#             self.sums.append(self.sums[-1] + i)
#
#     def sumRange(self, left: int, right: int) -> int:
#         _sums = self.sums
#         return _sums[right + 1] - _sums[left]

if __name__ == '__main__':
    s = NumArray([-2, 0, 3, -5, 2, -1])
    print(s.sumRange(2, 5))

303. 区域和检索 - 数组不可变.py

303. 区域和检索 - 数组不可变.py


"""Import required object classes needed for views to function as intended
i.e. render views, get objects,get model and get form"""
from datetime import date
from django.shortcuts import render, get_object_or_404
from django.views import View
from .forms import BookingForm
from .models import BookingInformation

class HomePage(View):
    """This will display the home view"""
    def get(self, request):
        """This function will handle get requests for this view"""
        return render(request, "online_booking/index.html")

class CreateBooking(View):
    """This will display the create booking view"""
    def get(self, request):
        """This function will handle get requests for this view
        and allocate the form to render"""
        booking_form = {
                "booking_form": BookingForm()
            }

        return render(
            request,
            "online_booking/create_booking.html",
            booking_form
        )

    def post(self, request):
        """This function will handle post requests for this view, allocate
        which form to render, specify what to do if the form is valid and
        what information to send to the template as its context"""
        booking_form = BookingForm(data=request.POST)

        if booking_form.is_valid():
            booking_form.instance.username = request.user.username
            booking_form.save()
        else:
            booking_form = BookingForm()

        bookings = BookingInformation.objects.all()
        context = {
            'bookings': bookings
        }

        return render(request,
                      "online_booking/view_booking.html", context
                      )

class ViewBooking(View):
    """This will display the view booking view"""

    def get(self, request):
        """This function will handle post requests for this view
        and what information to send to the template as its context"""
        bookings = BookingInformation.objects.all()
        context = {
            'bookings': bookings
        }
        return render(request, "online_booking/view_booking.html", context)

class EditBooking(View):
    """This will display the create booking view"""
    def get(self, request, booking_id):
        """This function will handle get requests for this view, allocate
        a booking entry identifier and allocate which form to render as
        well as allocate which entry to edit"""
        booking_identifier = get_object_or_404(BookingInformation,
                                               id=booking_id)
        edit_form = {
                "edit_form": BookingForm(instance=booking_identifier)
            }
        return render(request, "online_booking/edit_booking.html", edit_form)

    def post(self, request, booking_id):
        """This function will handle post requests for this view, allocate
        which form to render, specify what to do if the form is valid and
        what information to send to the template as its context"""
        edit_form = BookingForm(data=request.POST)

        if edit_form.is_valid():
            edit_form.instance.username = request.user.username
            edit_form.instance.pk = booking_id
            edit_form.save()
        else:
            edit_form = BookingForm()

        bookings = BookingInformation.objects.all()
        context = {
            'bookings': bookings
        }

        return render(
            request,
            "online_booking/view_booking.html", context
        )

class DeleteBooking(View):
    """This view will allow the user to delete his booking"""
    def get(self, request, booking_id):
        """This function will handle get requests for this view, allocate
        a booking entry identifier, allocate which entry to delete and
        what information to send to the template"""
        booking_identifier = get_object_or_404(BookingInformation,
                                               id=booking_id)
        booking_identifier.delete()

        bookings = BookingInformation.objects.all()
        context = {
            'bookings': bookings
        }

        return render(
            request,
            "online_booking/view_booking.html", context
        )

class ViewBookingEmployee(View):
    """This function will display the home page"""

    def get(self, request):
        """This function will display the home page"""
        today = date.today()
        bookings = BookingInformation.objects.filter(date=today)
        context = {
            'bookings': bookings,
        }
        return render(request, "online_booking/view_booking.html", context)


import argparse, os

def evaluate_data(fdir):

    f_list = [f for f in os.listdir(fdir) if os.path.isfile(f'{fdir}/{f}')]

    total = 0

    for i, file in enumerate(f_list):

        print(f'{file}: {i+1:03}')
        line_count = 0

        with open(f'{fdir}/{file}', 'r') as f:
            for l in f:
                line_count += 1
                if line_count%4 == 0:
                    total += len(l.rstrip())

    print(f'Total Bases\t{total}')

def main():

    # Configure Argument Parser
    parser = argparse.ArgumentParser(
        description=__doc__,
        formatter_class=argparse.RawDescriptionHelpFormatter
        )
    parser.add_argument(
        '-fdir', '--file_directory',
        help='Please specify the directory of fastq files!',
        metavar='',
        type=str,
        required=True
        )
    args=vars(parser.parse_args())

    fdir = args['file_directory']

    if fdir[-1] == '/': fdir = fdir[:-1]

    evaluate_data(fdir)

if __name__ == "__main__":
    main()


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('hpy01', '0008_auto_20160614_1227'),
    ]

    operations = [
        migrations.CreateModel(
            name='QQGrouphpy',
            fields=[
                ('id', models.AutoField(auto_created=True, verbose_name='ID', primary_key=True, serialize=False)),
                ('name', models.CharField(max_length=64)),
                ('brief', models.TextField(max_length=1024, default='nothing')),
                ('members_limit', models.IntegerField(default=200)),
                ('admin', models.ManyToManyField(to='hpy01.UserProfilehpy', related_name='qq_admimn')),
                ('founder', models.ForeignKey(to='hpy01.UserProfilehpy')),
                ('members', models.ManyToManyField(to='hpy01.UserProfilehpy', related_name='qqmembers')),
            ],
        ),
    ]


#!/usr/bin/python

# Refreshes security reviews from the authoritative source code repository.

import collections
import json
import logging
import os
import re
import subprocess

import dateutil
import requests
from dateutil.parser import parse
from packageurl.contrib import purl2url, url2purl

from .Base import BaseJob

class RefreshScorecard(BaseJob):
    """
    Refreshes data from the OpenSSF Scorecard project.
    """

    _payload = {}

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

    def execute_complete(self):
        """
        Loads data from the public data collected by the OpenSSF Scorecard project.
        """
        logging.info("Gathering all scorecard data.")
        try:
            payloads = []

            res = requests.get(
                "https://storage.googleapis.com/ossf-scorecards/latest.json", timeout=120
            )
            if res.status_code != 200:
                logging.warning("Failure fetching latest JSON: %s", res.status_code)
                return

            for line in res.text.splitlines():
                try:
                    data = json.loads(line)
                except Exception as msg:
                    logging.warning("Invalid JSON: [%s]", line)
                    continue

                package_url = url2purl.url2purl("https://" + data.get("Repo"))
                if not package_url:
                    logging.warning(
                        "Unable to identify Package URL from repository: [%s]", data.get("Repo")
                    )
                    continue

                date_ = parse(data.get("Date"))

                for check in data.get("Checks", []):
                    check_name = check.get("CheckName").lower().strip()
                    payload = {
                        "package_url": str(package_url),
                        "operation": "replace",
                        "key": f"openssf.scorecard.raw.{check_name}",
                        "values": [{"value": str(check.get("Pass")).lower(), "properties": check}],
                    }
                    payloads.append(payload)
            res = requests.post(self.METRIC_API_ENDPOINT, json=payloads, timeout=120)
            if res.status_code == 200:
                logging.info("Success: %s", res.text)
            else:
                logging.warning("Failure: status code: %s", res.status_code)

        except Exception as msg:
            logging.warn("Error: %s", msg)

    def execute(self):
        """
        Calculates the scorecard value from a Docker container.
        """
        logging.info("Gathering scorecard data for [%s]", str(self.package_url))

        source_repo = self.get_source_repository()
        if not source_repo:
            return

        token = self.get_api_token("github")
        if not token:
            logging.warning("Unable to retrieve Github token.")
            return

        try:
            result = subprocess.run(
                f'docker run --rm -it --env "GITHUB_AUTH_TOKEN={token}" docker.io/library/scorecard --repo={source_repo} --format json',
                shell=True,
                stdout=subprocess.PIPE,
            )
            scorecard_output = result.stdout.decode("utf-8")
            scorecard_output = scorecard_output[scorecard_output.find("{") :]
            js = json.loads(scorecard_output)

            payloads = []

            for check in js.get("Checks", []):
                check_name = check.get("CheckName", "").lower().strip()
                if not check_name:
                    continue
                pass_value = str(check.get("Pass", False)).lower()

                payload = {
                    "package_url": str(self.package_url),
                    "operation": "replace",
                    "key": f"openssf.scorecard.raw.{check_name}",
                    "values": [{"value": pass_value, "properties": check}],
                }
                payloads.append(payload)

            res = requests.post(self.METRIC_API_ENDPOINT, json=payloads, timeout=120)
            if res.status_code == 200:
                logging.info("Success: %s", res.text)
            else:
                logging.warning("Failure: status code: %s", res.status_code)

        except Exception as msg:
            logging.warn("Error processing Scorecard data: %s", msg)
            raise

if __name__ == "__main__":
    processor = RefreshScorecard(package_url=sys.argv[1])
    processor.execute()


"""empty message

Revision ID: afe50b8a7822
Revises: d516996dee73
Create Date: 2019-05-29 22:21:05.607261

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = 'afe50b8a7822'
down_revision = 'd516996dee73'
branch_labels = None
depends_on = None

def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint('m2m_authors_ibfk_2', 'm2m_authors', type_='foreignkey')
    op.create_foreign_key(None, 'm2m_authors', 'books', ['book_id'], ['id'])
    op.drop_constraint('m2m_categories_ibfk_1', 'm2m_categories', type_='foreignkey')
    op.create_foreign_key(None, 'm2m_categories', 'books', ['book_id'], ['id'])
    # ### end Alembic commands ###

def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(None, 'm2m_categories', type_='foreignkey')
    op.create_foreign_key('m2m_categories_ibfk_1', 'm2m_categories', 'users', ['book_id'], ['id'])
    op.drop_constraint(None, 'm2m_authors', type_='foreignkey')
    op.create_foreign_key('m2m_authors_ibfk_2', 'm2m_authors', 'users', ['book_id'], ['id'])
    # ### end Alembic commands ###


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Nov 17 10:06:42 2020

@author: elvinagovendasamy
"""

import numpy as np
from numpy.fft import fft,ifft
import matplotlib.pyplot as plt
from math import pi
import pandas as pd
from scipy.linalg import dft

# Exercice 1 -

# =============================================================================
# Question a
# =============================================================================

meteo1=pd.read_csv('meteo.csv',sep=';').loc[:,'Temperature']
temp=meteo1[0:25] # temp est une serie, on ne peut prend que les 25 premieres lignes
temp=temp.astype({'Temperature':float})

x = range(25)

plt.plot(x,temp)

# =============================================================================
# Question b)
# =============================================================================

#on a choisi N=25, et e= 1mesure/h donc f= 1.1e-5 Hz
#les fréquences complexes sont donc [-12f,...,-f,0,f,...,12f]
#on calcul les coefficients associés à ces fréquences

#Fonction renvoyant le matrice de fourier et la matrice inverse de fourier

def MatriceFourrier(d):
    M = dft(d, scale = 'n')     #matrice de Fourier du cours
    M_inv = np.conj(d*M)
    return(M,M_inv)

M = MatriceFourrier(25)[0]
M_inverse = MatriceFourrier(25)[1]

coefficients= np.dot(M,temp) #attention à l'ordre des coefs, le premier est c0 puis c1 jusqu'à c12 puis c-12 jusqu'à c-1

# =============================================================================
# Question c) A BIEN COMPRENDRE
# =============================================================================
# On veut la temperature à l'heure
# Les frequences sont toujours en secondes
# Alors on a 60*60 = 3600 secondes
# On a e=1/3600 = 2.78e-4
# Nf=e
#f=e/N =(1/3600)/25

f=(1/3600)/25
print(f)
liste_freq= np.array([k*f for k in range(13)]+[k*f for k in range(-12,0)])

def temperature(t,coeff):
    freq=2*np.pi*1j*3600*t*liste_freq
    vect_exp=np.exp(freq)
    valeurs=np.dot(coeff,vect_exp)
    signal_real=np.real(valeurs)
    return (signal_real)

# =============================================================================
# Question d)  A BIEN COMPRENDRE
# =============================================================================

time=np.linspace(0,25,100)

signal=[temperature(t,coefficients) for t in time]
values=[temperature(t_,coefficients) for t_ in x]

# for t in x:
#     values.append(temperature(t,coefficients))

plt.scatter(x,values,c='r',s=5)
plt.plot(time,signal,c='green')

# =============================================================================
# Question e)  A FAIRE
# =============================================================================


import re
from itertools import chain

def parse_dish(line):
    dish, allergens = re.match('(.+?) \(contains (.+?)\)', line).groups()
    return dish.split(), allergens.split(', ')

def find_allergens(dishes):
    allergens = dict()
    tmp = dict()
    for dish_ing, dish_allerg in dishes:
        for a in dish_allerg:
            tmp.setdefault(a, set(dish_ing)).intersection_update(dish_ing)
    while tmp:
        allergen, ingredient = next((a, ing) for a, ing in tmp.items() if len(ing) == 1)
        allergens[next(iter(ingredient))] = allergen
        del tmp[allergen]
        for v in tmp.values():
            v -= ingredient
    return allergens

def star1():
    dishes = [parse_dish(line.strip()) for line in open('input.txt')]
    allergens = find_allergens(dishes)
    print(sum(1 for ingridient in chain.from_iterable(d[0] for d in dishes) if ingridient not in allergens))

def star2():
    dishes = [parse_dish(line.strip()) for line in open('input.txt')]
    allergens = find_allergens(dishes)
    print(','.join(sorted(allergens.keys(), key=allergens.get)))

print('Star 1:')
star1()
print('Star 2:')
star2()