import string
from random import choice
from typing import Optional, TYPE_CHECKING

from quart import Blueprint, jsonify

from litecord.auth import admin_check
from ...errors import InternalServerError, NotFound
from litecord.types import timestamp_
from litecord.schemas import validate
from litecord.admin_schemas import INSTANCE_INVITE

if TYPE_CHECKING:
    from litecord.typing_hax import app, request
else:
    from quart import current_app as app, request

bp = Blueprint("instance_invites", __name__)
ALPHABET = string.ascii_lowercase + string.ascii_uppercase + string.digits

def _gen_inv() -> str:
    """Generate an invite code"""
    return "".join(choice(ALPHABET) for _ in range(6))

async def gen_inv(ctx) -> Optional[str]:
    """Generate an invite."""
    for _ in range(10):
        possible_inv = _gen_inv()

        created_at = await ctx.db.fetchval(
            """
        SELECT created_at
        FROM instance_invites
        WHERE code = $1
        """,
            possible_inv,
        )

        if created_at is None:
            return possible_inv

    return None

@bp.route("", methods=["GET"], strict_slashes=False)
async def _all_instance_invites():
    await admin_check()

    rows = await app.db.fetch(
        """
    SELECT code, created_at, uses, max_uses
    FROM instance_invites
    """
    )

    rows = [dict(row) for row in rows]

    for row in rows:
        row["created_at"] = timestamp_(row["created_at"])

    return jsonify(rows)

@bp.route("", methods=["POST"], strict_slashes=False)
async def _create_invite():
    await admin_check()

    j = validate(await request.get_json(), INSTANCE_INVITE)

    code = await gen_inv(app)
    if code is None:
        raise InternalServerError()

    await app.db.execute(
        """
    INSERT INTO instance_invites (code, max_uses)
    VALUES ($1, $2)
    """,
        code,
        j["max_uses"],
    )

    inv = await app.db.fetchrow(
        """
        SELECT code, created_at, uses, max_uses
        FROM instance_invites
        WHERE code = $1
        """,
        code,
    )
    dinv = dict(inv)
    dinv["created_at"] = timestamp_(dinv["created_at"])

    return jsonify(dinv)

@bp.route("/<invite>", methods=["GET"])
async def _get_invite(invite):
    inv = await app.db.fetchrow(
        """
        SELECT code, created_at, uses, max_uses
        FROM instance_invites
        WHERE code = $1
        """,
        invite,
    )

    if not inv:
        raise NotFound(10006)
    dinv = dict(inv)
    dinv["created_at"] = timestamp_(dinv["created_at"])
    return jsonify(dinv)

@bp.route("/<invite>", methods=["DELETE"])
async def _del_invite(invite: str):
    await admin_check()

    res = await app.db.execute(
        """
    DELETE FROM instance_invites
    WHERE code = $1
    """,
        invite,
    )
    if res == "DELETE 0":
        raise NotFound(10006)

    return "", 204


import sys
import speechbrain as sb
from hyperpyyaml import load_hyperpyyaml

from a3d import KWSBrain
from a3d.dataset import prep_speechcommands
from a3d.dataset import create_datasets_speechcommands

def main(hparams, hparams_file, run_opts, overrides):
    # Create experiment directory
    sb.create_experiment_directory(
        experiment_directory=hparams["output_folder"],
        hyperparams_to_save=hparams_file,
        overrides=overrides,
    )

    # Data prep to run on the main thread
    sb.utils.distributed.run_on_main(
        prep_speechcommands,
        kwargs={
            "data_folder": hparams["data_folder"],
            "save_folder": hparams["annotation_folder"]
        },
    )

    # Create dataset objects
    datasets, label_encoder = create_datasets_speechcommands(hparams)

    # Initialize the Trainer
    brain = KWSBrain(
        modules=hparams["modules"],
        hparams=hparams,
        run_opts=run_opts
    )

    # Evaluate the best checkpoint on the test set
    brain.evaluate_robustness(
        test_set=datasets["test"],
        test_loader_kwargs=hparams["dataloader_options"]
    )

if __name__ == "__main__":
    # Reading command line arguments
    hparams_file, run_opts, overrides = sb.parse_arguments(sys.argv[1:])

    # Load hyperparameters file with command-line overrides
    with open(hparams_file) as f:
        hparams = load_hyperpyyaml(f, overrides)

    # Initialize ddp (useful only for multi-GPU DDP training)
    sb.utils.distributed.ddp_init_group(run_opts)

    main(hparams, hparams_file, run_opts, overrides)


import pytest

import abjad

def test_LilyPondParser__spanners__HorizontalBracket_01():

    maker = abjad.NoteMaker()
    target = abjad.Container(maker([0] * 4, [(1, 4)]))
    abjad.horizontal_bracket(target[:])
    abjad.horizontal_bracket(target[:2])
    abjad.horizontal_bracket(target[2:])

    assert abjad.lilypond(target) == abjad.string.normalize(
        r"""
        {
            c'4
            \startGroup
            \startGroup
            c'4
            \stopGroup
            c'4
            \startGroup
            c'4
            \stopGroup
            \stopGroup
        }
        """
    )

    parser = abjad.parser.LilyPondParser()
    result = parser(abjad.lilypond(target))
    assert abjad.lilypond(target) == abjad.lilypond(result) and target is not result

def test_LilyPondParser__spanners__HorizontalBracket_02():
    """
    Starting and stopping on the same leaf.
    """

    string = r"""{ c \startGroup \stopGroup c c c }"""
    with pytest.raises(Exception):
        abjad.LilyPondParser()(string)

def test_LilyPondParser__spanners__HorizontalBracket_03():
    """
    One group stopping on a leaf, while another begins on the same leaf.
    """

    string = r"""{ c \startGroup c \stopGroup \startGroup c c \stopGroup }"""
    with pytest.raises(Exception):
        abjad.LilyPondParser()(string)

def test_LilyPondParser__spanners__HorizontalBracket_04():
    """
    Unterminated.
    """

    string = r"""{ c \startGroup c c c }"""
    with pytest.raises(Exception):
        abjad.LilyPondParser()(string)

def test_LilyPondParser__spanners__HorizontalBracket_05():
    """
    Unstarted.
    """

    string = r"""{ c c c c \stopGroup }"""
    with pytest.raises(Exception):
        abjad.LilyPondParser()(string)


"""
Really fast, no bugs!

good work @ me

idea was pretty simple, ie. reuse the current space

what tripped me up was thinking about worst case runtime

turns out still is O(n)
"""

from typing import List

class Solution:
    def firstMissingPositive(self, nums: List[int]) -> int:
        for index in range(len(nums)):
            done = False

            while not done:
                val = nums[index]
                if 1 <= val and val <= len(nums):
                    if nums[val - 1] != val:
                        next_num = nums[val - 1]
                        nums[val - 1] = val
                        nums[index] = next_num
                        continue
                done = True
        for index in range(len(nums)):
            if nums[index] != index + 1:
                return index + 1
        return len(nums) + 1


import os
import rnmd.extract_code
import rnmd.compile_markdown

default_compile_dir="/tmp/rnmd"
os.makedirs(default_compile_dir, exist_ok=True)

def run_markdown(source, args):

    temp_compile_path = os.path.join(default_compile_dir, os.path.basename(source))

    rnmd.compile_markdown.compile_markdown(source, temp_compile_path)

    command = ""
    if(args is None):
        command = temp_compile_path
    else:
        all_args = [temp_compile_path] + args
        command = (" ").join(all_args)

    os.system(command)

if __name__ == "__main__":
    import argparse
    parser = argparse.ArgumentParser(
        description="Run markdown contained code"
    )
    parser.add_argument('source', help="Source of the documentation file")
    arguments = parser.parse_args()
    source = arguments.source

    run_markdown(source)


import datetime
from individual import Individual
from family import Family

individuals = {}
families = {}

supported_tags: list = [
    "INDI",
    "NAME",
    "SEX",
    "BIRT",
    "DEAT",
    "FAMC",
    "FAMS",
    "FAM",
    "MARR",
    "HUSB",
    "WIFE",
    "CHIL",
    "DIV",
    "DATE",
    "HEAD",
    "TRLR",
    "NOTE",
]

def parse_date(date: str) -> datetime.date:
    """Helps to parse date"""

    month_dict = {
        "JAN": 1,
        "FEB": 2,
        "MAR": 3,
        "APR": 4,
        "MAY": 5,
        "JUN": 6,
        "JUL": 7,
        "AUG": 8,
        "SEP": 9,
        "OCT": 10,
        "NOV": 11,
        "DEC": 12,
    }
    date = date.split()
    return datetime.datetime(int(date[2]), month_dict[date[1]], int(date[0])).date()

def is_supported_tag(tag: str) -> bool:
    """check weather in"""
    return tag in supported_tags

def process_family(rows) -> Family:
    """read"""
    family = Family()

    # by default will not process marriage and divorce assuming level will be 0
    process_marriage = False
    process_divorce = False

    for row in rows:

        # Reads each row and sets the corresponding field in the family object
        if row[1] == "FAM":
            family.identifier = row[2].strip()
        elif row[1] == "HUSB":
            family.husband_id = row[2].strip()
        elif row[1] == "WIFE":
            family.wife_id = row[2].strip()
        elif row[1] == "CHIL":
            family.children.append(row[2].strip())
        elif row[1] == "DIV":
            # Begin reading in the divorce date
            process_divorce = True
            family.is_divorced = True
        elif row[1] == "MARR":
            # Begin reading in the marriage date
            process_marriage = True
        elif int(row[0]) == 2 and row[1] == "DATE":
            if process_marriage:
                family.married = parse_date(row[2].strip())
            elif process_divorce:
                family.is_divorced = True
                family.divorced = parse_date(row[2].strip())

    return family

def process_individual(rows: list) -> Individual:
    """Helps to read individual"""

    # create a new individual.
    individual = Individual()

    # by default will not process birth and death assuming level will be 0
    process_birth = False
    process_death = False

    for row in rows:

        if int(row[0]) == 1:
            process_birth = False
            process_death = False

        if row[1] == "INDI":
            individual.identifier = row[2].strip()
        elif row[1] == "BIRT":
            process_birth = True
        elif int(row[0]) == 2 and row[1] == "DATE":
            if process_birth:
                individual.birthday = parse_date(row[2].strip())
            elif process_death:
                individual.death_day = parse_date(row[2].strip())
        elif row[1] == "FAMS":
            individual.spouse_family.append(row[2].strip())
        elif row[1] == "FAMC":
            individual.children_family.append(row[2].strip())
        elif row[1] == "NAME":
            individual.name = row[2].strip()
        elif row[1] == "SEX":
            individual.gender = row[2].strip()
        elif row[1] == "DEAT":
            if "Y" == row[2].strip():
                process_death = True
                individual.alive = False
            else:
                individual.alive = True

    # process age
    individual.process_age()

    return individual

def parser_file(file_path: str):
    """"""

    is_individual = False
    is_family = False

    lines_list = []

    # default  tags and args to str

    tag = ""
    level = 0

    with open(file_path, "r") as lines:

        for line in lines:

            list_of_words = line.split()
            level = int(list_of_words.pop(0))

            if len(list_of_words) == 1:
                tag = list_of_words[0]
                args = ""
            else:
                tag = (
                    list_of_words.pop(1)
                    if list_of_words[1] == "INDI" or list_of_words[1] == "FAM"
                    else list_of_words.pop(0)
                )
                args=""
                for word in list_of_words:
                    args = args + word + " "

            if level == 0:
                if is_individual:
                    new_individual = process_individual(lines_list)
                    individuals[new_individual.identifier] = new_individual
                if is_family:
                    new_family = process_family(lines_list)
                    families[new_family.identifier] = new_family
                if tag == "INDI":
                    is_individual = True
                    is_family = False
                    lines_list = []
                elif tag == "FAM":
                    is_individual = False
                    is_family = True
                    lines_list = []
                else:
                    is_individual = False
                    is_family = False

            if tag in supported_tags:
                lines_list.append([level, tag, args])

            for family in families.keys():
                families[family].husband_name = individuals[families[family].husband_id].name
                families[family].wife_name = individuals[families[family].wife_id].name
        individual_list = []
        family_list = []
        individual_list = []
        for individual in sorted(individuals.keys()):
            ind = individuals[individual]
            individual_list.append(
                [
                    ind.identifier,
                    ind.name,
                    ind.gender,
                    ind.birthday,
                    ind.age,
                    ind.alive,
                    ind.death_day,
                    ind.get_children_family() ,
                    ind.get_spouse_family(),
                ]
            )
        for family in sorted(families.keys()):
            fam = families[family]
            family_list.append(
                [
                    fam.identifier,
                    fam.married,
                    fam.get_is_divorced(),
                    fam.husband_id,
                    fam.husband_name,
                    fam.wife_id,
                    fam.wife_name,
                    fam.get_children(),
                ]
            )

        return individual_list ,family_list


# key builder test
from flexmock import flexmock
from keyBuilder import buildKey
from assertpy import assert_that

def test_buildKey():
	config = flexmock(keyName="test",
		keyPattern=".*",
		hasGroup=False,
		groupNum=None)

	assert_that(buildKey("alan", [config])).is_equal_to({"test":"alan"})

def test_buildKey_two_elements():
	config = flexmock(keyName="test",
		keyPattern=".*",
		hasGroup=False,
		groupNum=None)

	config1 = flexmock(keyName="test1",
		keyPattern=".*",
		hasGroup=False,
		groupNum=None)

	assert_that(buildKey("alan", [config, config1])).is_equal_to({"test":"alan", "test1":"alan"})

def test_buildKey_has_group():
	config = flexmock(keyName="test",
		keyPattern="\w+:(\w+)",
		hasGroup=True,
		groupNum=0)

	assert_that(buildKey("name:string", [config])).is_equal_to({"test":"string"})


from nptdms import TdmsFile

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

input_file="M9_Default_200921-123303_Courants50Hz.tdms"
with TdmsFile.open(input_file) as tdms_file:
    # Iterate over all items in the file properties and print them
    for name, value in tdms_file.properties.items():
        print("{0}: {1}".format(name, value))

    # List groups
    all_groups = tdms_file.groups()
    print("all_groups=", all_groups)

    # for a given group
    group = tdms_file['Courants et Ref. Alimentations']

    # List Groups and Channels per Group
    all_group_channels = group.channels()
    print(all_group_channels)

    for group in all_groups:
        print("group=", group.name)
        all_group_channels = group.channels()
        print(all_group_channels)

        for channel in all_group_channels:
            print("channel=", channel.name)

            # Get a channel property
            property_list = tdms_file[group.name][channel.name].properties
            print("property_list=", property_list, "type(property_list)=", type(property_list))

            all_channel_data = channel[:]
            # print("data=", all_channel_data)

    group = tdms_file['Courants et Ref. Alimentations']
    channel = group['Courant_A1']

    # 'wf_start_time'
    start_timestamp = channel.properties['wf_start_time']
    print("start_time=", start_timestamp)

    # 'wf_start_offset'
    start_offset = channel.properties['wf_start_offset']
    print("start_offset=", start_offset)

    # 'wf_increment'
    increment = channel.properties['wf_increment']
    print("increment=", increment)

    # 'wf_samples'
    samples = channel.properties['wf_samples']
    # 'unit_string'
    time_steps = np.array([i*increment for i in range(0,samples)])

    ax = plt.gca()
    plt.plot(time_steps, channel[:], label="A1")
    plt.plot(time_steps, group['Courant_A2'][:], label="A2")
    plt.ylabel(channel.name + " [" + channel.properties['unit_string'] + "]")
    plt.xlabel("t [s]")
    plt.grid(b=True)
    ax.legend()
    plt.show()


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
data = pd.read_csv('Data.csv')
x = data.iloc[:,[0]].values
y = data.iloc[:,[1]].values

x_t = np.transpose(x)
first_inv = np.matmul(x_t, x)
second = np.matmul(x_t, y)
first = np.linalg.inv(first_inv)
theta = np.matmul(first, second)
y_prad = theta*x

regressor = LinearRegression()
regressor.fit(x, y)
y_prad2 = regressor.predict(x)
plt.scatter(x, y)
plt.plot(x, y_prad, 'red')
plt.plot(x, y_prad2, 'green')


# **hdfs操作
import os
from hdfs import client

#**url：ip：端口
client = client.InsecureClient("http://yh001:50070", user="hadoop" ,root="/")

def upload(remote_dir="/pings/wordcount/", local_dir="D:/data/1/"):
    """上传本地文件到hdfs"""
    client.delete(remote_dir, True)
    client.makedirs(remote_dir)
    for file in os.listdir(local_dir):
        client.upload(remote_dir, local_dir + file)

if __name__ == "__main__":
    upload()
    print(client.list("/"))


from sklearn.preprocessing import StandardScaler
from sklearn.cluster import KMeans
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.metrics import silhouette_score, davies_bouldin_score

def clustering_k_means(df, columns_clustering, n_clusters=3, label_column_name='label',
                       post_analysis=True, figure_file_path=None, colors_plot=None, metrics=True):
    """
    Perform k-means clustering on a DataFrame using specified columns and optionally conduct post-analysis.

    Args:
        df (pandas.DataFrame): The input DataFrame containing the data to be clustered.
        columns_clustering (list of str): A list of column names in the DataFrame to be used for clustering.
        n_clusters (int, optional): The number of clusters to create. Default is 3.
        label_column_name (str, optional): The name of the column where cluster labels will be added to the DataFrame.
            Default is 'label'.
        post_analysis (bool, optional): Whether to conduct post-analysis and generate density plots. Default is True.
        figure_file_path (str, optional): The file path to save the generated density plot figure.
        colors_plot (list of str, optional): A list of colors for plotting each cluster's density distribution.
        metrics (bool, optional): Whether to compute and return internal evaluation metrics. Default is True.

    Returns:
        pandas.DataFrame: A DataFrame with an additional column containing cluster labels.
        dict or None: A dictionary containing internal evaluation metrics if metrics=True, otherwise None.

    Example:
        >>> data = {
        ...     'feature1': [1.2, 2.5, 3.1, 4.8, 5.2],
        ...     'feature2': [0.9, 1.8, 2.5, 3.7, 4.0]
        ... }
        >>> df = pd.DataFrame(data)
        >>> columns_to_cluster = ['feature1', 'feature2']
        >>> clustered_df, metrics_dict = clustering_k_means(df, columns_to_cluster, n_clusters=2, post_analysis=False)
        >>> print(clustered_df)
           feature1  feature2  label
        0       1.2       0.9      0
        1       2.5       1.8      0
        2       3.1       2.5      0
        3       4.8       3.7      1
        4       5.2       4.0      1
        >>> print(metrics_dict)
        {
            'Silhouette Score': 0.654,
            'Davies-Bouldin Index': 0.529,
            'Inertia': 2.371
        }
    """
    # Obtain dataframe with the variables for clustering
    df_clustering = df[columns_clustering]

    # Standardize data before clustering
    scaler = StandardScaler()
    df_clustering = scaler.fit_transform(df_clustering)

    # Perform k-means clustering for the specified number of clusters
    clustering_kmeans = KMeans(n_clusters=n_clusters, random_state=0, n_init="auto").fit(df_clustering)

    # Add cluster labels to the original dataframe
    df[label_column_name] = list(clustering_kmeans.labels_)

    if post_analysis:
        # Plot density distribution for each variable by clustering label

        # Set up the figure and subplots
        # Calculate the number of rows and columns based on the length of columns_clustering
        num_plots = len(columns_clustering)
        num_cols = min(3, num_plots)
        num_rows = -(-num_plots // num_cols)  # Ceiling division to ensure enough rows

        fig, axe = plt.subplots(nrows=num_rows, ncols=num_cols, figsize=(15, 5))
        # Unpack all the axes subplots
        axes = axe.ravel()

        # Generate colors for plotting if not provided
        if colors_plot is None:
            colors_plot = [plt.cm.jet(i / n_clusters) for i in range(n_clusters)]
        else:
            # Check if the length of colors_plot matches the number of clusters
            if len(colors_plot) != n_clusters:
                raise ValueError("The length of colors_plot must be the same as the number of clusters.")

        # Loop through the continuous variables to create plots
        for i, var in enumerate(columns_clustering):
            ax = axes[i]
            for group_label in df[label_column_name].unique():
                group_data = df[df[label_column_name] == group_label]
                group_color = colors_plot[group_label]
                group_data[var].plot(kind='kde', legend=True, ax=ax, label=group_label, color=group_color)

            # Set title and legend for each subplot
            ax.set_title(f'Density Plot of {var}')
            ax.legend()

        # Add overall title
        title_plot = "Clustering post-analysis"
        fig.suptitle(title_plot, fontsize=16)
        # Adjust layout and save the figure if a file path is provided
        if figure_file_path:
            plt.tight_layout()
            plt.savefig(figure_file_path)
        plt.show()

    if metrics:
        # Compute Silhouette Score (a higher score is better)
        silhouette_avg = silhouette_score(df_clustering, df[label_column_name])

        # Compute Davies-Bouldin Index (lower values are better)
        db_index = davies_bouldin_score(df_clustering, df[label_column_name])

        # Compute Inertia (within-cluster sum of squares) (lower values are better)
        inertia = clustering_kmeans.inertia_

        # Create a dictionary with score information
        score = {
            'Type of Score': ['Silhouette Score', 'Davies-Bouldin Index', 'Inertia'],
            'Range of Values': ['[-1, 1]', '[0, Inf)', '[0, Inf)'],
            'Best Value': ['Higher', 'Lower', 'Lower'],
            'Value for the Score': [silhouette_avg, db_index, inertia]
        }

    return df, score


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Nov 29 17:49:23 2018
@author: kzeman2

NitroShed Model

"""

from mesa import Agent, Model
import random
from mesa.time import RandomActivation
from mesa.space import MultiGrid
from schedule import model_schedule
#import matplotlib.pyplot as plt

#Agent Classes used in model
from farmer_agent import *
from bmp_agent import *
from community_agent import *
from farmland_agent import *
from environment_agent import *

######## Model Control ##########
"""
A model with some number of farmer agents. Farmers are influence based on
the other agents within the model.These are the CommunityAgent, the
FarmLandAgent, the EnvironmentAgent, and the BMPAgent.

"""
class NitroShedModel(Model):
    """
    Creates the NitroShed Super Class which creates all of the model Agents

    classes used in this model are:
        farmer_agent: creates all of the farmers with typology parameters and
        places them at a location of farmland

        farmland_agent: creates the farmland from information givent from the
        environement agent

        bmp_agent: creates the bmps that can be used by the farmer

        community_agent: creates the community which acts as all other
        stakeholders in the model
        environment_agent: creates the environment HRU's and watershed where
        the model takes place

    """

    print('initalizing Nitroshed Model')

    def __init__(self, Num_Farmers, width, height, bmps_available):
        r"""
        Initializes the model and creates the agents

        Takes in parameters designated from runModel.py to create the farmer
        agents, BMP agents, Farmland agents, community agent and environment
        agent.

        Parameters
        ----------

        Num_Farmers: int
            Number of farmer agents which will be created and run in the model.
            Each farmer has a typology and other inherint traits which can be
            found in farmer_agent.py

        width: int
            designates how wide the model space is

        height: int
            designates how tall the model space is

        bmps_available: int
            gives the number of BMP's that are created in the model. Used in
            the creation of bmp_agent.py

        %%%%%%%%%%%%%

        num_agents: int
        number of farmers created in the model

        grid: matrix

        schedule: class
        function schedule.py that creates the order that the agents in the model are
        called to take a step

        bmps_available:
        int
            gives the number of BMP's that are created in the model. Used in
            the creation of bmp_agent.py

        returns
        -------
        completed model

        """
        self.num_agents = Num_Farmers
        #self.schedule = RandomActivation(self)
        self.grid = MultiGrid(width, height, True)
        self.schedule = model_schedule(self)
        self.bmps_available=bmps_available

        #Create BMP's
        print('Creating BMPs')
        for i in range(self.bmps_available):
            bmp= BMPAgent(i, self)
            self.schedule.add(bmp)

        #Create Environment
        print('Creating EnvironmentAgent')
        env=EnvironmentAgent(self)

        #Creat Community
        print('Creating Community Agent')
        community= CommunityAgent(self)

        # Create agents
        print('Creating Agents')
        for i in range(self.num_agents):
            a = FarmerAgent(i, self)
            self.schedule.add(a)
            b= FarmLandAgent(i, self)
            self.schedule.add(b)

            #Add the agent to a random grid cell
            x = random.randrange(self.grid.width)
            y = random.randrange(self.grid.height)
            self.grid.place_agent(a, (x, y))
            self.grid.place_agent(b, (x, y))
            print("Farmer:",a.farmer_id,"Capital",a.capital, "location:", a.pos, "Area:", b.area,"Farm Location", b.pos)

    def step(self,i):
        '''Advance the model by one step.'''
        print("model step", i)
        self.schedule.step()


"""
Module for processing aspect data
"""
from __future__ import division, absolute_import, print_function

from sys import stdout
from operator import attrgetter, itemgetter
import warnings
import inspect

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
import pandas as pd
from scipy.optimize import curve_fit
from skimage.feature import match_template, peak_local_max
from skimage.transform import warp
from astropy.io import fits

from ..util.time import oeb2utc
from ..util.fitting import parapeak

__all__ = ['PYFrame', 'PYSequence', 'RFrame', 'RSequence']

NUM_ROWS = 960
NUM_COLS = 1280

def _kernel(radius):
    """The empirically chosen kernel to represent both the Sun and the fiducials."""
    axis = np.arange(-(radius * 2), radius * 2 + 1)
    x, y = np.meshgrid(axis, axis)
    template = 1 - np.cosh(np.sqrt(x**2 + y**2) * 5. / radius) / 200.
    template[template < 0] = 0
    return template

template_sun = _kernel(50)
template_fiducial = 1 - _kernel(4)

class Frame(object):
    """Base class for a camera frame

    Parameters
    ----------
    filename : str
        The name of the FITS file of the camera frame
    uid : int
        The UID of the camera to check for consistency with the camera frame.
        The default of None means that consistency is not checked.
    decimation_range : tuple of int
        The allowable range of decimation levels.
    """
    def __init__(self, filename, uid=None, decimation_range=(2, 10)):
        with fits.open(filename) as f:
            if uid != None and f[0].header['CAMERA'] != uid:
                raise RuntimeError
            self.filename = filename
            self.header = f[0].header
            self.data = f[1].data.astype(np.uint8)
            self.trigger_time = f[0].header['GT_TRIG']
        self.decimation = self._detect_decimation(decimation_range)

    def _detect_decimation(self, decimation_range):
        """Detects the decimation level of the camera frame.

        Parameters
        ----------
        decimation_range : tuple of int
            The inclusive range of decimation levels to check against the camera frame

        Returns
        -------
        decimation_level : int
            For a decimation_level of N, N-1 rows out of every N rows is empty
        decimation_index : int
            The index of the non-empty row for every decimation block
            (i.e. strictly less than decimation_level)
        """
        dead1 = (self.data == 0).sum(1) == NUM_COLS
        for step in range(decimation_range[0], decimation_range[1] + 1):
            sub_rows = NUM_ROWS // step
            dead2 = dead1[:sub_rows * step].reshape(sub_rows, step).max(0)
            idx = np.flatnonzero(~dead2)
            if len(idx) == 1:
                return (step, idx[0])
        return (1, 0)

    def plot_image(self, **imshow_kwargs):
        """Plots the camera frame, defaulting to no interpolation."""
        args = {'cmap'   : 'gray',
                'extent' : [-0.5, NUM_COLS - 0.5, NUM_ROWS - 0.5, -0.5],
                'interpolation' : 'nearest'}
        args.update(imshow_kwargs)
        return plt.imshow(self.image, **args)

    @property
    def image(self):
        if self.decimation == (1, 0):
            return self.data
        else:
            transformation = np.matrix([[1, 0, 0],
                                        [0, 1. / self.decimation[0], -self.decimation[1] / self.decimation[0]],
                                        [0, 0, 1]])
            return warp(self.data[self.decimation[1]::self.decimation[0], :], transformation,
                        output_shape=(NUM_ROWS, NUM_COLS), mode='edge', preserve_range=True)

class PYFrame(Frame):
    """Class for a pitch-yaw image

    Parameters
    ----------
    filename : str
        The name of the FITS file of the camera frame
    uid : int
        Defaults to the UID of the pitch-yaw camera, but can be set to a different camera's UID or to None
    """
    def __init__(self, filename, uid=158434):
        try:
            Frame.__init__(self, filename, uid=uid)
        except RuntimeError:
            raise RuntimeError("This file does not appear to be a valid frame from the pitch-yaw camera")

        self.peaks, self.fiducials = self._process()

    def _process(self):
        """Finds the Suns and the fiducials."""
        # Perform a coarse search for Suns
        coarse_image = self.image[::10, ::10]
        coarse_match = match_template(coarse_image, template_sun[::10, ::10], pad_input=True)
        coarse_peaks = peak_local_max(coarse_match, threshold_abs=0.9, num_peaks=3)

        fine_peaks = []
        strength = []
        fiducials = []

        for coarse_peak in coarse_peaks:
            # For each coarse detection, do a detection at the full resolution
            if coarse_peak[0] < 11 or coarse_peak[0] > 84 or coarse_peak[1] < 11 or coarse_peak[1] > 116:
                break
            sub_image = self.image[coarse_peak[0] * 10 - 110:coarse_peak[0] * 10 + 111,
                                   coarse_peak[1] * 10 - 110:coarse_peak[1] * 10 + 111]
            match = match_template(sub_image, template_sun, pad_input=True)
            peak = peak_local_max(match, threshold_abs=0.9, num_peaks=1)
            if len(peak) > 0:
                peak = peak[0]
                peak_r, peak_c = parapeak(match[peak[0] - 1:peak[0] + 2, peak[1] - 1:peak[1] + 2])
                peak += coarse_peak * 10 - 110

                fine_peaks.append((peak[0] + peak_r, peak[1] + peak_c))

                #FIXME: need a more robust estimate of the strength of each peak
                strength.append(self.image[peak[0], peak[1]])

                # Find fiducials near the center of the Sun
                match = match_template(self.image[peak[0]-60:peak[0]+61, peak[1]-60:peak[1]+61],
                                       template_fiducial, pad_input=True)
                fids = peak_local_max(match, threshold_abs=0.8)
                for fid in fids:
                    fid_r, fid_c = parapeak(match[fid[0] - 1:fid[0] + 2, fid[1] - 1:fid[1] + 2])
                    fid += peak - 60

                    fiducials.append((fid[0] + fid_r, fid[1] + fid_c))

        # Sort the peaks in order of decreasing strength
        fine_peaks = [peak for (strength, peak) in sorted(zip(strength, fine_peaks), reverse=True)]

        return fine_peaks, fiducials

    def plot_image(self, **imshow_kwargs):
        """Plots the pitch-yaw image, including Sun/fiducial detections."""
        if self.peaks:
            # Draw an X at the center of each Sun
            arr_peaks = np.array(self.peaks)
            plt.plot(arr_peaks[:, 1], arr_peaks[:, 0], 'rx')

            # Draw circles around each Sun
            radius = 60
            angle = 2 * np.pi * np.arange(0, 101, 0.01)
            for peak in self.peaks:
                plt.plot(peak[1] + radius * np.cos(angle), peak[0] + radius * np.sin(angle), 'r')

        if self.fiducials:
            # Draw a cross at each fiducial
            arr_fiducials = np.array(self.fiducials)
            plt.plot(arr_fiducials[:, 1], arr_fiducials[:, 0], 'b+')

        return Frame.plot_image(self, **imshow_kwargs)

class RFrame(Frame):
    """Class for a roll image

    Parameters
    ----------
    filename : str
        The name of the FITS file of roll image
    uid : int
        Defaults to the UID of the roll camera, but can be set to a different camera's UID or to None
    """
    def __init__(self, filename, uid=142974):
        try:
            Frame.__init__(self, filename, uid=uid, decimation_range=(10, 10))
        except RuntimeError:
            raise RuntimeError("This file does not appear to be a valid frame from the roll camera")

        self.midpoint = self._process()

    def _process(self, plot=False):
        """Fit the horizons"""
        # For now, fit the whole thing
        data = np.mean(self.data[self.decimation[1]::self.decimation[0], :], 0)
        left = np.min(np.flatnonzero(data[0:640] < np.mean(data[0:640])))
        right = np.max(np.flatnonzero(data[640:] < np.mean(data[640:]))) + 640

        subx = np.arange(left, right + 1)
        guess = (639.5, 0.02, 0.04, 10)
        try:
            popt, pcov = curve_fit(RFrame.atmosphere_sym, subx, data[left:right+1], guess)
        except RuntimeError:
            warnings.warn_explicit("Could not automatically fit the horizon",
                                   RuntimeWarning, __file__, inspect.currentframe().f_lineno)
            return None

        if plot:
            print(popt)

            plt.plot(data)
            plt.plot(subx, RFrame.atmosphere_sym(subx, *popt))

        return popt[0]

    def plot_image(self, **imshow_kwargs):
        """Plots the roll image, including annotations."""
        return Frame.plot_image(self, **imshow_kwargs)

    @staticmethod
    def atmosphere_sym(x, midpoint, scale, amp, dc):
        """Exponential atmospheric model with symmetry (i.e., hyperbolic cosine)"""
        return amp * np.cosh(scale * (x - midpoint)) + dc

    @staticmethod
    def atmosphere_asym(x, midpoint, scale, amp1, amp2, dc):
        """Exponential atmospheric model with asymmetry in normalization"""
        return amp1 * np.exp(-scale * (x - midpoint)) + amp2 * np.exp(scale * (x - midpoint)) + dc

class FrameSequence(list):
    """Base class for a sequence of camera frames"""
    def __init__(self, list_of_files, frame_class=Frame):
        for entry in list_of_files:
            self.append(frame_class(entry))
            stdout.write(".")

        stdout.write("\n")

    def animate(self, fps=4, fig=None, **imshow_kwargs):
        """Return a matplotlib animation of the frame sequence.  Extra keyword arguments are passed on to
        `matplotlib.pyplot.imshow`.

        Parameters
        ----------
        fps : int
            Number of frames per second for interactive display

        fig : `matplotlib.figure.Figure`
            If None, the current figure is used

        Returns
        -------
        `matplotlib.animation.FuncAnimation`

        Examples
        --------
        To save a matplotlib animation as an animated GIF:

        >>> anim = framesequence.animate()
        >>> anim.save('animation.gif', writer='imagemagick', fps=4)

        On Windows, you may need to configure matplotlib to find `convert.exe`, e.g.:

        >>> import matplotlib as mpl
        >>> mpl.rcParams['animation.convert_path'] = 'C:\\Program Files\\ImageMagick-6.9.2-Q16\\convert.exe'

        """
        if fig is None:
            fig = plt.gcf()

        args = {'cmap'   : 'gray',
                'extent' : [-0.5, NUM_COLS - 0.5, NUM_ROWS - 0.5, -0.5],
                'clim' : (0, 256),
                'interpolation' : 'nearest'}
        args.update(imshow_kwargs)
        im = plt.imshow(np.zeros_like(self[0].image), **args)

        iterator = self.__iter__()

        # Although this function does nothing, otherwise matplotlib "burns" the first frame for initialization
        def init():
            pass

        # Update with the next frame's data
        def update_im(frame, im):
            im.set_data(frame.image)
            plt.gca().set_title(frame.filename)
            return im,

        ani = animation.FuncAnimation(fig, update_im, iterator, fargs=(im,), init_func=init,
                                      repeat=False, interval=1000 / fps)

        return ani

    def compact(self):
        """Crude compaction of a decimated sequence"""
        for i in range(len(self) - 1, 0, -1):
            if (self[i].decimation[0] == self[i-1].decimation[0]) and\
               (self[i].decimation[1] > self[i-1].decimation[1]):
                self[i-1].data += self[i].data
                self.pop(i)
            else:
                self[i].decimation = (1, 0)
        self[0].decimation = (1, 0)

class PYSequence(FrameSequence):
    """Class for a sequence of pitch-yaw images

    Parameters
    ----------
    list_of_files : list of str
        List of FITS files with pitch-yaw images
    """
    def __init__(self, list_of_files):
        FrameSequence.__init__(self, list_of_files, frame_class=PYFrame)

    @property
    def dataframe(self):
        """Obtain a pandas DataFrame"""
        center = [x.peaks[0] for x in self]
        return pd.DataFrame({'center_x' : [z[1] for z in center],
                             'center_y' : [z[0] for z in center]},
                            index=oeb2utc([x.trigger_time for x in self]))

    def plot_centers(self, **dataframe_plot_kwargs):
        """Plot the center of the brightest Sun across the entire image sequence"""
        return self.dataframe.plot(**dataframe_plot_kwargs)

class RSequence(FrameSequence):
    """Class for a sequence of roll images

    Parameters
    ----------
    list_of_files : list of str
        List of FITS files with roll images
    """
    def __init__(self, list_of_files):
        FrameSequence.__init__(self, list_of_files, frame_class=RFrame)

    @property
    def dataframe(self):
        """Obtain a pandas DataFrame"""
        return pd.DataFrame({'midpoint' : [x.midpoint for x in self]},
                            index=oeb2utc([x.trigger_time for x in self]))

    def plot_midpoint(self, **dataframe_plot_kwargs):
        """Plot the midpoints"""
        return self.dataframe.plot(**dataframe_plot_kwargs)


#!/usr/bin/python
import cv2
import sys
import os
if __name__ == "__main__" :

            imagename="contrast.jpg"
            img=cv2.imread(imagename)
            # if (img==None):
            # print("File not found") # Or not readable?
            # sys.exit()

        #create haar cascade classifier
            path="haarcascade_frontalface_alt.xml"
        #path = "/usr/share/opencv/haarcascades/haarcascade_frontalface_alt2.xml"
            hc=cv2.CascadeClassifier(path)
            # if (hc.empty()):
            #   print("CascadeClassifer could not be loaded")
            #   sys.exit()

        #detect faces
            faces=hc.detectMultiScale(img)
            i=1
            for face in faces:
              imgROI = img[face[1]:face[1]+face[3],face[0]:face[0]+face[2]]
            #imgROI = img[face[1]-25:face[1]+face[3]+25,
            #             face[0]-20:face[0]+face[2]+20]
              s=imagename
              cv2.imwrite(s,imgROI)
              i=i+1

            print(str(i-1)+" face(s) extracted")


from front_end_gui_script import print_console_gui
import os
import calendar
import pandas as pd
from datetime import datetime
from datetime import timedelta
from dateutil import relativedelta
import snscrape.modules.twitter as sntwitter

tweet_path = 'tweets'

def initiate_path():
    if not os.path.exists(tweet_path):
        os.mkdir(tweet_path)

def calculate_days_in_month(month, year):
    return calendar.monthrange(year, month)[1]

def calculate_number_of_months(start_date, end_date, date_format):
    start_date = datetime.strptime(str(start_date), date_format)
    end_date = datetime.strptime(str(end_date), date_format)
    r = relativedelta.relativedelta(end_date, start_date)
    return (r.years * 12) + r.months

def add_date_by_days(date, days, date_format):
    date = datetime.strptime(date, date_format)
    date = date + timedelta(days)
    return date.strftime(date_format)

def subtract_date_by_days(date, days, date_format):
    date = datetime.strptime(date, date_format)
    date = date - timedelta(days)
    return date.strftime(date_format)

def calculate_end_date(start_date, date_format):
    if date_format == '%m-%d-%Y':
        return str(start_date[0:2] + '-' + str(calculate_days_in_month(int(start_date[0:2]),\
                                                int(start_date[-4:]))) + '-' + start_date[-4:])
    elif date_format == '%d-%m-%Y':
        return str(str(calculate_days_in_month(int(start_date[3:5]), int(start_date[-4:])))\
                                               + '-' + start_date[3:5] + '-' + start_date[-4:])

def get_twitter_search_string(keyword, start_date, end_date):
     return str(keyword + ' lang:en since:' + str(start_date[-4:]) + '-' + str(start_date[-10:-8])\
                + '-' + str(start_date[-7:-5]) + ' until:' + str(end_date[-4:]) + '-'\
                + str(end_date[-10:-8]) + '-' + str(end_date[-7:-5]))

def get_file_name_string(keyword, end_date):
    end_date = subtract_date_by_days(end_date, 1, '%m-%d-%Y')
    return str(keyword + '_' + str(end_date[-10:-8]) + '-' + str(end_date[-7:-5]) +\
               '-' + str(end_date[-2:]) + '.csv')

def check_tweet_availability(keyword, start_date, end_date):
    try:
        for tweet in enumerate(sntwitter.TwitterSearchScraper\
                        (get_twitter_search_string(keyword, start_date, end_date)).get_items()):
            if tweet:
                return 1
            else:
                return 0
    except:
        print('\nSearch keyword has unsearchable characters, please replace them!\n', flush = True)
        return 0

def tweet_scraper(number_of_tweets_per_month, start_date, end_date, keyword):
    # Using TwitterSearchScraper to scrape data and append tweets to list
    if check_tweet_availability(keyword, start_date, end_date):
        initiate_path()
        final_date = end_date
        months = calculate_number_of_months(start_date, end_date, '%m-%d-%Y')
        for month_count in range(0, months + 1):
            # Creating list to append tweet data to
            tweets_list2 = []
            if not months == month_count:
                end_date = calculate_end_date(start_date, '%m-%d-%Y')
            else:
                end_date = final_date
            for i, tweet in enumerate(sntwitter.TwitterSearchScraper\
                        (get_twitter_search_string(keyword, start_date, end_date)).get_items()):
                if i > number_of_tweets_per_month:
                    break
                tweets_list2.append([tweet.date, tweet.id, tweet.content])
            # Creating a dataframe from the tweets list above
            tweets_df2 = pd.DataFrame(tweets_list2, columns=['Datetime', 'Tweet Id', 'Text'])
            file_name = os.path.join(tweet_path, get_file_name_string(keyword, end_date))
            tweets_df2.to_csv(file_name)
            start_date = add_date_by_days(end_date, 1, '%m-%d-%Y')
        return 0
    else:
        return -1

def main():
    # mm-dd-YYYY format
    start_date = '01-01-2020'
    end_date = '05-24-2021'
    keyword = 'coronavirus'
    number_of_tweets_per_month = 1000
    tweet_scraper(number_of_tweets_per_month, start_date, end_date, keyword)

if __name__ == '__main__':
    main()


from pprint import pprint
from time import sleep
from selenium.webdriver.chrome.service import Service
from selenium import webdriver
from selenium.webdriver.common.by import By
from bs4 import BeautifulSoup
import requests

class DataEntryJobAutomation():
    def __init__(self) -> None:
        self.captura_completa = False

    def get_research_houses(self, url_zillow):
        self.url_zillow = url_zillow
        headers_list = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"
            "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 "
            "Safari/537.36 OPR/83.0.4254.27",
            "Accept-Language": "pt-BR,pt;q=0.9,en-US;q=0.8,en;q=0.7"
        }

        response = requests.get(self.url_zillow, headers=headers_list)
        response.raise_for_status()

        data = response.text
        soup = BeautifulSoup(data, "html.parser")

        print()
        # Getting all the links elements
        links_element = soup.select(".list-card-info a")

        all_links = []
        for link in links_element:
            href = link["href"]
            if "http" not in href:
                href = f"https://www.zillow.com{href}"
            print(href)
            all_links.append(href)
        print(f"Tamanho de links: {len(all_links)}\n")

        # Getting all the rent values
        rent_values_elements = soup.select("div .list-card-price")

        all_rent_values = []
        for rent_value in rent_values_elements:
            value = rent_value.getText().split("/")[0].split("+")[0]
            print(value)
            all_rent_values.append(value)
        print(f"Tamanho de alugueis: {len(all_rent_values)}\n")

        # Getting all the address
        all_address_elements = soup.select(".list-card-addr")

        all_address = []
        for address in all_address_elements:
            address_text = address.getText()
            print(address_text)
            all_address.append(address_text)
        print(f"Tamanho de Enderecos: {len(all_address)}\n")

        if len(all_address) == len(all_links) and \
                len(all_links) == len(all_rent_values):
            self.tamanho = len(all_links)
            self.lista = [
                {
                    "address": all_address[i],
                    "rent-value": all_rent_values[i],
                    "link": all_links[i],
                }
                for i in range(self.tamanho)
            ]
            pprint(self.lista)
            self.captura_completa = True
        else:
            print("Houve problemas na captura dos dados.")
            self.captura_completa = False

    def inicializar_driver(self):
        chrome_driver_path =\
            "C:\\github\\100_days_of_code_python\\chromedriver\\chromedriver.exe"

        ser = Service(chrome_driver_path)
        op = webdriver.ChromeOptions()
        self.driver = webdriver.Chrome(service=ser, options=op)

    def send_forms(self, url_form):
        self.url_form = url_form
        if self.captura_completa:
            self.inicializar_driver()
            self.driver.get(self.url_form)

            for indice in range(self.tamanho):
                sleep(2)

                input_address = self.driver.find_element(
                    By.CSS_SELECTOR,
                    "#mG61Hd > div.freebirdFormviewerViewFormCard.exportFormCard > div > div.freebirdFormviewerViewItemList > div:nth-child(1) > div > div > div.freebirdFormviewerComponentsQuestionTextRoot > div > div.quantumWizTextinputPaperinputMainContent.exportContent > div > div.quantumWizTextinputPaperinputInputArea > input"
                )
                input_address.send_keys(self.lista[indice]["address"])

                input_rent = self.driver.find_element(
                    By.CSS_SELECTOR,
                    "#mG61Hd > div.freebirdFormviewerViewFormCard.exportFormCard > div > div.freebirdFormviewerViewItemList > div:nth-child(2) > div > div > div.freebirdFormviewerComponentsQuestionTextRoot > div > div.quantumWizTextinputPaperinputMainContent.exportContent > div > div.quantumWizTextinputPaperinputInputArea > input"
                )
                input_rent.send_keys(self.lista[indice]["rent-value"])

                input_link = self.driver.find_element(
                    By.CSS_SELECTOR,
                    "#mG61Hd > div.freebirdFormviewerViewFormCard.exportFormCard > div > div.freebirdFormviewerViewItemList > div:nth-child(3) > div > div > div.freebirdFormviewerComponentsQuestionTextRoot > div > div.quantumWizTextinputPaperinputMainContent.exportContent > div > div.quantumWizTextinputPaperinputInputArea > input"
                )
                input_link.send_keys(self.lista[indice]["link"])

                button_send = self.driver.find_element(
                    By.XPATH,
                    '//*[@id="mG61Hd"]/div[2]/div/div[3]/div[1]/div[1]/div/span'
                )
                button_send.click()

                button_resend = self.driver.find_element(
                    By.LINK_TEXT,
                    "Enviar outra resposta"
                )
                button_resend.click()

            self.driver.close()
        else:
            print("Não foi possivel enviar os dados para o formulario.")


from __future__ import absolute_import
from __future__ import division
from __future__ import unicode_literals

import enum
from googlecloudsdk.api_lib.notebooks import util
from googlecloudsdk.command_lib.util.apis import arg_utils
from googlecloudsdk.core import log
from googlecloudsdk.core import resources

def CreateRuntime(args, messages):
  """Creates the Runtime message for the create request.

  Args:
    args: Argparse object from Command.Run
    messages: Module containing messages definition for the specified API.

  Returns:
    Runtime of the Runtime message.
  """

  def GetRuntimeVirtualMachineFromArgs():
    machine_type = 'n1-standard-4'
    if args.IsSpecified('machine_type'):
      machine_type = args.machine_type
    virtual_machine_config = messages.VirtualMachineConfig(
        machineType=machine_type, dataDisk=messages.LocalDisk())
    return messages.VirtualMachine(virtualMachineConfig=virtual_machine_config)

  def GetRuntimeAccessConfigFromArgs():
    runtime_access_config = messages.RuntimeAccessConfig
    type_enum = None
    if args.IsSpecified('runtime_access_type'):
      type_enum = arg_utils.ChoiceEnumMapper(
          arg_name='runtime-access-type',
          message_enum=runtime_access_config.AccessTypeValueValuesEnum,
          include_filter=lambda x: 'UNSPECIFIED' not in x).GetEnumForChoice(
              arg_utils.EnumNameToChoice(args.runtime_access_type))
    return runtime_access_config(
        accessType=type_enum, runtimeOwner=args.runtime_owner)

  def GetPostStartupScriptBehavior():
    type_enum = None
    if args.IsSpecified('post_startup_script_behavior'):
      runtime_software_config_message = messages.RuntimeSoftwareConfig
      type_enum = arg_utils.ChoiceEnumMapper(
          arg_name='post-startup-script-behavior',
          message_enum=(runtime_software_config_message
                        .PostStartupScriptBehaviorTypeValueValuesEnum),
          include_filter=lambda x: 'UNSPECIFIED' not in x).GetEnumForChoice(
              arg_utils.EnumNameToChoice(args.post_startup_script_behavior))
    return type_enum

  def GetRuntimeSoftwareConfigFromArgs():
    runtime_software_config = messages.RuntimeSoftwareConfig()
    if args.IsSpecified('idle_shutdown_timeout'):
      runtime_software_config.idleShutdownTimeout = args.idle_shutdown_timeout
    if args.IsSpecified('install_gpu_driver'):
      runtime_software_config.installGpuDriver = args.install_gpu_driver
    if args.IsSpecified('custom_gpu_driver_path'):
      runtime_software_config.customGpuDriverPath = args.custom_gpu_driver_path
    if args.IsSpecified('post_startup_script'):
      runtime_software_config.postStartupScript = args.post_startup_script
    if args.IsSpecified('post_startup_script_behavior'):
      runtime_software_config.postStartupScriptBehavior = (
          GetPostStartupScriptBehavior())
    return runtime_software_config

  runtime = messages.Runtime(
      name=args.runtime,
      virtualMachine=GetRuntimeVirtualMachineFromArgs(),
      accessConfig=GetRuntimeAccessConfigFromArgs(),
      softwareConfig=GetRuntimeSoftwareConfigFromArgs(),
  )
  return runtime

def CreateRuntimeCreateRequest(args, messages):
  parent = util.GetParentForRuntime(args)
  runtime = CreateRuntime(args, messages)
  return messages.NotebooksProjectsLocationsRuntimesCreateRequest(
      parent=parent, runtime=runtime, runtimeId=args.runtime)

def CreateRuntimeListRequest(args, messages):
  parent = util.GetParentFromArgs(args)
  return messages.NotebooksProjectsLocationsRuntimesListRequest(parent=parent)

def GetRuntimeResource(args):
  return args.CONCEPTS.runtime.Parse()

def GetSwitchRuntimeRequest(args, messages):
  """Create and return switch runtime request."""
  machine_type = 'n1-standard-4'
  if args.IsSpecified('machine_type'):
    machine_type = args.machine_type
  runtime_accelerator_config = messages.RuntimeAcceleratorConfig()
  if args.IsSpecified('accelerator_core_count'):
    runtime_accelerator_config.coreCount = args.accelerator_core_count
  if args.IsSpecified('accelerator_type'):
    type_enum = arg_utils.ChoiceEnumMapper(
        arg_name='accelerator-type',
        message_enum=runtime_accelerator_config.TypeValueValuesEnum,
        include_filter=lambda x: 'UNSPECIFIED' not in x).GetEnumForChoice(
            arg_utils.EnumNameToChoice(args.accelerator_type))
    runtime_accelerator_config.type = type_enum
  return messages.SwitchRuntimeRequest(
      machineType=machine_type, acceleratorConfig=runtime_accelerator_config)

def CreateRuntimeDeleteRequest(args, messages):
  runtime = GetRuntimeResource(args).RelativeName()
  return messages.NotebooksProjectsLocationsRuntimesDeleteRequest(
      name=runtime)

def CreateRuntimeResetRequest(args, messages):
  runtime = GetRuntimeResource(args).RelativeName()
  reset_request = messages.ResetRuntimeRequest()
  return messages.NotebooksProjectsLocationsRuntimesResetRequest(
      name=runtime, resetRuntimeRequest=reset_request)

def CreateRuntimeStartRequest(args, messages):
  runtime = GetRuntimeResource(args).RelativeName()
  start_request = messages.StartRuntimeRequest()
  return messages.NotebooksProjectsLocationsRuntimesStartRequest(
      name=runtime, startRuntimeRequest=start_request)

def CreateRuntimeStopRequest(args, messages):
  runtime = GetRuntimeResource(args).RelativeName()
  stop_request = messages.StopRuntimeRequest()
  return messages.NotebooksProjectsLocationsRuntimesStopRequest(
      name=runtime, stopRuntimeRequest=stop_request)

def CreateRuntimeSwitchRequest(args, messages):
  runtime = GetRuntimeResource(args).RelativeName()
  switch_request = GetSwitchRuntimeRequest(args, messages)
  return messages.NotebooksProjectsLocationsRuntimesSwitchRequest(
      name=runtime, switchRuntimeRequest=switch_request)

def CreateRuntimeDescribeRequest(args, messages):
  runtime = GetRuntimeResource(args).RelativeName()
  return messages.NotebooksProjectsLocationsRuntimesGetRequest(name=runtime)

def CreateRuntimeDiagnoseRequest(args, messages):
  """"Create and return Diagnose request."""
  runtime = GetRuntimeResource(args).RelativeName()
  diagnostic_config = messages.DiagnosticConfig(
      gcsBucket=args.gcs_bucket,
  )
  if args.IsSpecified('relative_path'):
    diagnostic_config.relativePath = args.relative_path
  if args.IsSpecified('enable-repair'):
    diagnostic_config.repairFlagEnabled = True
  if args.IsSpecified('enable-packet-capture'):
    diagnostic_config.packetCaptureFlagEnabled = True
  if args.IsSpecified('enable-copy-home-files'):
    diagnostic_config.copyHomeFilesFlagEnabled = True

  timeout_minutes = None
  if args.IsSpecified('timeout_minutes'):
    timeout_minutes = int(args.timeout_minutes)

  return messages.NotebooksProjectsLocationsRuntimesDiagnoseRequest(
      name=runtime, diagnoseRuntimeRequest=messages.DiagnoseRuntimeRequest(
          diagnosticConfig=diagnostic_config, timeoutMinutes=timeout_minutes))

def GetRuntimeURI(resource):
  instance = resources.REGISTRY.ParseRelativeName(
      resource.name, collection='notebooks.projects.locations.runtimes')
  return instance.SelfLink()

class OperationType(enum.Enum):
  CREATE = (log.CreatedResource, 'created')
  DELETE = (log.DeletedResource, 'deleted')
  UPDATE = (log.UpdatedResource, 'updated')
  RESET = (log.ResetResource, 'reset')

def HandleLRO(operation,
              args,
              runtime_service,
              release_track,
              operation_type=OperationType.CREATE):
  """Handles Long Running Operations for both cases of async.

  Args:
    operation: The operation to poll.
    args: ArgParse instance containing user entered arguments.
    runtime_service: The service to get the resource after the long running
      operation completes.
    release_track: base.ReleaseTrack object.
    operation_type: Enum value of type OperationType indicating the kind of
      operation to wait for.

  Raises:
    apitools.base.py.HttpError: if the request returns an HTTP error

  Returns:
    The Runtime resource if synchronous, else the Operation Resource.
  """
  logging_method = operation_type.value[0]
  if args.async_:
    logging_method(
        util.GetOperationResource(operation.name, release_track),
        kind='notebooks runtime {0}'.format(args.runtime),
        is_async=True)
    return operation
  else:
    response = util.WaitForOperation(
        operation,
        'Waiting for operation on Runtime [{}] to be {} with [{}]'.format(
            args.runtime, operation_type.value[1], operation.name),
        service=runtime_service,
        release_track=release_track,
        is_delete=(operation_type.value[1] == 'deleted'))
    logging_method(
        util.GetOperationResource(operation.name, release_track),
        kind='notebooks runtime {0}'.format(args.runtime),
        is_async=False)
    return response


import os

from celery import Celery

from configs.default.cron import task_schedule

# set the default Django settings module for the 'celery' program.
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "configs")

app = Celery("sspanel")

# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
#   should have a `CELERY_` prefix.
app.config_from_object("django.conf:settings", namespace="CELERY")

# Load task modules from all registered Django app configs.
app.autodiscover_tasks()

app.conf.beat_schedule = {
    task: dict(task=task, schedule=schedule) for task, schedule in task_schedule.items()
}


import argparse

parser = argparse.ArgumentParser(description='read in message')
parser.add_argument('n', help='public key n', type=str)
parser.add_argument('d', help='private key d', type=str)
parser.add_argument('message', help='message', nargs=argparse.REMAINDER, default=[])
args = parser.parse_args()

def decrypt(encr_m, d, n):
    message = [chr(pow(int(t), d, n)) for t in encr_m]
    return ''.join(message)

if __name__ == '__main__':
    n = int(args.n)
    d = int(args.d)
    message = args.message

    print('*Decrypted Message*')
    print(decrypt(message, d, n))


from django.shortcuts import render
from rest_framework import status
from rest_framework.decorators import api_view
from rest_framework.response import Response
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from .models import Branch_Office
from .serializers import *

@api_view(['GET'])
def branch_office_list(request):
    """
    List  products, or create a new product.
    """
    if request.method == 'GET':
        data = []
        nextPage = 1
        previousPage = 1
        products = Branch_Office.objects.all()
        page = request.GET.get('page', 1)
        paginator = Paginator(products, 10)
        try:
            data = paginator.page(page)
        except PageNotAnInteger:
            data = paginator.page(1)
        except EmptyPage:
            data = paginator.page(paginator.num_pages)

        serializer = Branch_OfficeSerializer(data,context={'request': request} ,many=True)
        if data.has_next():
            nextPage = data.next_page_number()
        if data.has_previous():
            previousPage = data.previous_page_number()

        return Response({'data': serializer.data , 'count': paginator.count, 'numpages' : paginator.num_pages, 'nextlink': '/api/branch_office/?page=' + str(nextPage), 'prevlink': '/api/branch_office/?page=' + str(previousPage)})


import numpy as np
import torch
import torch.nn.functional as f

class Conv:
    def __init__(self, c_in, c_out, kernel, stride=1, padding=0, dilation=1):
        self.kernel = kernel
        self.stride = stride
        self.padding = padding
        self.dilation = dilation
        self.c_in = c_in
        self.c_out = c_out

        self.weight = np.random.rand(self.c_out, self.c_in, self.kernel, self.kernel)
        self.bias = np.random.rand(self.c_out)

    def forward(self, X):
        """
        X is of shape BxC_inxHxW
        """

        B, cx_in, H, W = X.shape
        assert cx_in == self.c_in

        H_out = (
            H + 2 * self.padding - ((self.kernel - 1) * self.dilation + 1)
        ) // self.stride + 1
        W_out = (
            W + 2 * self.padding - ((self.kernel - 1) * self.dilation + 1)
        ) // self.stride + 1

        X = np.pad(
            X,
            (
                (0, 0),
                (0, 0),
                (self.padding, self.padding),
                (self.padding, self.padding),
            ),
        )
        out = np.zeros((B, self.c_out, H_out, W_out))

        for i in range(H_out):
            for j in range(W_out):
                w2 = (self.kernel // 2) * self.dilation
                in_i = w2 + i * self.stride
                in_j = w2 + j * self.stride
                patch = X[
                    :,
                    :,
                    in_i - w2 : in_i + w2 + 1 : self.dilation,
                    in_j - w2 : in_j + w2 + 1 : self.dilation,
                ]
                out[:, :, i, j] = (patch[:, None] * self.weight[None]).sum(
                    (2, 3, 4)
                ) + self.bias[None]

        return out

    def forward_torch(self, X):
        out = f.conv2d(
            torch.from_numpy(X),
            torch.from_numpy(self.weight),
            torch.from_numpy(self.bias),
            self.stride,
            self.padding,
            self.dilation,
        )
        return out.numpy()

def main():
    img = np.random.randn(1, 3, 10, 10)
    conv = Conv(3, 2, 3, 2, 1, 2)
    out2 = conv.forward_torch(img)
    out1 = conv.forward(img)
    assert (out1 - out2).sum() < 1e-10

if __name__ == "__main__":
    main()


# from pudb import set_trace; set_trace()
from typing import List
from collections import Counter, deque
import heapq

class Solution1:
    def maxResult(self, nums: List[int], k: int) -> int:
        """LeetCode 1696

        The DP idea is fairly straightforward. In fact it is exactly the same
        as the one we handled on the past Monday. The tricky part is that k is
        also a variable, which means if we iterate through all k values in the
        DP table for each new n, the runtime would be O(NK). This will time out
        given the size of N and K. Therefore, the problem to solve is how to
        acquire the max value in a sliding window of size k in less than linear
        time.

        My solution is to use a heap, which tracks the max value in logarithm
        time. The trick is to prune the heap when the heap size grows larger
        than the allowed k. Since we can only prune the max value in the heap,
        we need to also record the indices of each value pushed into the heap.
        Once the heap size is larger than k, AND the top of the heap has index
        outside the current sliding window range, we pop it. This way, it is
        guaranteed that each value in the heap is only popped once.

        O(Nlog(K)), 1652 ms, very bad runtime.
        """
        heap = []
        N = len(nums)
        for i in range(N - 1):
            # remove the top of heap if its index suggests that it is no longer
            # in range
            while len(heap) > k and i - heap[0][1] > k:
                heapq.heappop(heap)
            cur = nums[i] + (-heap[0][0] if heap else 0)
            heapq.heappush(heap, (-cur, i))
        # Must prune the heap one more time
        while len(heap) > k and N - 1 - heap[0][1] > k:
            heapq.heappop(heap)
        return nums[-1] + (-heap[0][0] if heap else 0)

class Solution2:
    def maxResult(self, nums: List[int], k: int) -> int:
        """O(N) solution, 1676 ms. Although the runtime seems bad, but this is
        a brilliant solution. While I used a heap to solve the problem of
        maintaining the max value in a sliding window, this solution uses a
        non increasing (monotonic) deque to solve the same problem. The idea is to always
        keep the max value on the left of the deque. This requires that each
        time a new value gets pushed into the deque, we pop out, from the right
        side, all the values that are smaller than the new one. Also, we check
        the left side to see if the index is out of range. If it is, we pop from
        the left.

        The runtime is bad probably because I am using a tuple for the deque.
        Or it could be because LeetCode has increased test cases.
        """
        scores = deque([(nums[0], 0)])
        for i in range(1, len(nums)):
            while i - scores[0][1] > k:
                scores.popleft()
            cur = nums[i] + scores[0][0]
            while scores and scores[-1][0] < cur:
                scores.pop()
            scores.append((cur, i))
        return scores[-1][0]

class Solution3:
    def maxResult(self, nums: List[int], k: int) -> int:
        """Speed up version of solution2

        Courtesy: https://leetcode.com/problems/jump-game-vi/discuss/978462/C%2B%2B-DP-%2B-Monoqueue-O(n)

        1228 ms.
        """
        indices = deque([0])
        scores = [0] * len(nums)
        scores[0] = nums[0]
        for i in range(1, len(nums)):
            while i - indices[0] > k:
                indices.popleft()
            cur = nums[i] + scores[indices[0]]
            while indices and scores[indices[-1]] < cur:
                indices.pop()
            indices.append(i)
            scores[i] = cur
        return scores[-1]

sol = Solution3()
tests = [
    ([1, -1, -2, 4, -7, 3], 2, 7),
    ([10, -5, -2, 4, 0, 3], 3, 17),
    ([1, -5, -20, 4, -1, 3, -6, -3], 2, 0),
    ([1], 1, 1),
    ([100, -1, -100, -1, 100], 2, 198),
]

for i, (nums, k, ans) in enumerate(tests):
    res = sol.maxResult(nums, k)
    if res == ans:
        print(f'Test {i}: PASS')
    else:
        print(f'Test {i}; Fail. Ans: {ans}, Res: {res}')


import sys
import os
from PyQt5.QtWidgets import QMainWindow, QPushButton,\
    QApplication, QInputDialog, QLineEdit, QLabel, \
    QFileDialog, QComboBox, QAction, QMessageBox
from PyQt5.QtCore import pyqtSlot, QByteArray, Qt
from PyQt5.QtGui import QIcon, QPixmap, QColor
import client
from final import image_parser, make_hist, decode
import skimage
from skimage.viewer import ImageViewer
from PIL import Image
from matplotlib import pyplot as plt
import io

global_user_email = ""
global_image_name = []
global_selected_name = ""
global_process_type = ""
global_image_dict = {}
global_process_image = ""

class App(QMainWindow):

    def __init__(self):
        super().__init__()
        self.title = 'BME 590 Image Processor'
        self.left = 10
        self.top = 10
        self.width = 720
        self.height = 480
        self.init_gui()
        self.next = None

    def init_gui(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)
        self.setAutoFillBackground(True)
        p = self.palette()
        p.setColor(self.backgroundRole(), QColor(204, 204, 255))
        self.setPalette(p)
        self.statusBar().showMessage('Welcome!')
        self.display_text()
        self.button_new_user()
        self.button_existing_user()
        self.show()

    def display_text(self):
        label = QLabel('Authors: Sarah Putney, '
                       'Erica Skerrett, Roujia Wang', self)
        label.setAlignment(Qt.AlignCenter)
        label.move(120, 300)
        label.setMinimumSize(480, 40)

    def button_new_user(self):
        button = QPushButton('Create New User', self)
        button.setMinimumSize(200, 40)
        button.setToolTip('This is an example button')
        button.move(150, 200)
        button.clicked.connect(self.create_user)

    def button_existing_user(self):
        button = QPushButton('Existing User', self)
        button.setMinimumSize(200, 40)
        button.setToolTip('This is an example button')
        button.move(400, 200)
        button.clicked.connect(self.get_user)

    @pyqtSlot()
    def create_user(self):
        user_email, ok_pressed = QInputDialog.getText(
            self, "Account Information", "User Email:", QLineEdit.Normal, "")
        if ok_pressed and user_email != '':
            global global_user_email
            global_user_email = user_email
            client.post_create_user(global_user_email)
            self.next = App2()
            self.close()

    @pyqtSlot()
    def get_user(self):
        user_email, ok_pressed = QInputDialog.getText(
            self, "Account Information", "User Email:", QLineEdit.Normal, "")
        if ok_pressed and user_email != '':
            global global_user_email
            global_user_email = user_email
            email = client.get_returning_user(global_user_email)
            if email.get("error_message") == 'None':
                self.next = App2()
                self.close()
            else:
                print(email)
                button_reply = QMessageBox.question(
                    self, 'Error Message', email.get("error_message"),
                    QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
                if button_reply == QMessageBox.Yes:
                    self.next = App()
                    self.close()
                else:
                    self.close()

class App2(QMainWindow):

    def __init__(self):
        super().__init__()
        self.title = 'Image Processor'
        self.left = 10
        self.top = 10
        self.width = 720
        self.height = 480
        self.label = QLabel()
        self.next = None
        self.init_gui()

    def init_gui(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)
        self.setAutoFillBackground(True)
        p = self.palette()
        p.setColor(self.backgroundRole(), QColor(204, 204, 255))
        self.setPalette(p)
        self.statusBar().showMessage('Step 1: Choose Images!')
        self.display_image()
        self.button_choose()
        self.show()

    def display_image(self):
        label = QLabel(self)
        pixmap = QPixmap('christmas.jpg')
        pixmap2 = pixmap.scaledToWidth(400)
        label.setPixmap(pixmap2)
        label.setGeometry(160, 50, 640, 280)

    def button_choose(self):
        button = QPushButton('Choose Image', self)
        button.setMinimumSize(200, 40)
        button.setToolTip('This is an example button')
        button.move(260, 340)
        button.clicked.connect(self.image_dialog)

    def image_dialog(self):
        options = QFileDialog.Options()
        options |= QFileDialog.DontUseNativeDialog
        file_name, _ = QFileDialog.getOpenFileNames(
            self, "QFileDialog.getOpenFileNames()", "",
            "All Files (*)", options=options)
        if file_name:
            global global_image_name
            global global_user_email
            global global_image_dict
            global_image_name = file_name
            global_image_dict = image_parser(global_image_name)
            client.post_uploaded_images(global_user_email, global_image_dict)
            self.close()
            self.next = App3()
        else:
            button_reply = QMessageBox.question(
                self, 'Error Message', 'Please Choose Image(s) to Proceed',
                QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
            if button_reply == QMessageBox.Yes:
                self.next = App2()
                self.close()
            else:
                self.close()

class App3(QMainWindow):

    def __init__(self):
        super().__init__()
        self.title = 'Image Processor'
        self.left = 10
        self.top = 10
        self.width = 720
        self.height = 480
        self.next = None
        self.init_gui()

    def init_gui(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)
        self.setAutoFillBackground(True)
        p = self.palette()
        p.setColor(self.backgroundRole(), QColor(204, 204, 255))
        self.setPalette(p)
        self.statusBar().showMessage('Step 2: Upload Image(s)!')
        self.display_text()
        self.scroll_down_menu()
        self.button_upload()
        self.show()

    def display_text(self):
        label = QLabel('Please Select From Following Options'
                       ' by Clicking the Image Name:', self)
        label.setAlignment(Qt.AlignCenter)
        label.move(40, 150)
        label.setMinimumSize(640, 40)

    def scroll_down_menu(self):
        global global_user_email
        global global_image_name
        uploaded_images = global_image_name
        combo = QComboBox(self)
        for i in uploaded_images:
            combo.addItem(i)
        combo.move(140, 200)
        combo.setMinimumSize(440, 40)
        combo.activated[str].connect(self.on_activated)

    def on_activated(self, name):
        global global_image_name
        global global_selected_name
        for i in global_image_name:
            if name in i:
                global_selected_name = name
        print(global_selected_name)

    def button_upload(self):
        button = QPushButton('Upload Image', self)
        button.setMinimumSize(200, 40)
        button.setToolTip('This is an example button')
        button.move(260, 320)
        button.clicked.connect(self.next_window)

    def next_window(self):
        global global_selected_name
        if global_selected_name == '':
            button_reply = QMessageBox.question(
                self, 'Error Message', 'You must click on the image to'
                                       ' select it!',
                QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
            if button_reply == QMessageBox.Yes:
                self.next = App3()
                self.close()
            else:
                self.close()
        else:
            self.close()
            self.next = App4()

class App4(QMainWindow):

    def __init__(self):
        super().__init__()
        global global_selected_name
        self.title = 'Image Processor'
        self.left = 10
        self.top = 10
        self.width = 720
        self.height = 480
        self.next = None
        self.path, self.filename = os.path.split(global_selected_name)
        self.init_gui()
        print(global_selected_name)

    def init_gui(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)
        self.setAutoFillBackground(True)
        p = self.palette()
        p.setColor(self.backgroundRole(), QColor(204, 204, 255))
        self.setPalette(p)
        self.statusBar().showMessage('Step 3: Process Image!')
        self.display_image()
        self.button1()
        self.button2()
        self.button3()
        self.button4()
        self.show()

    def display_image(self):
        label = QLabel(self)
        pixmap = QPixmap(os.path.join(self.path, self.filename))
        pixmap2 = pixmap.scaledToWidth(400)
        label.setPixmap(pixmap2)
        label.setGeometry(50, 20, 640, 400)

    def button1(self):
        button = QPushButton('Histogram Equalization', self)
        button.setMinimumSize(200, 40)
        button.setToolTip('This is an example button')
        button.move(470, 120)
        button.clicked.connect(self.histogram)

    def button2(self):
        button = QPushButton('Contrast Stretching', self)
        button.setMinimumSize(200, 40)
        button.setToolTip('This is an example button')
        button.move(470, 170)
        button.clicked.connect(self.contrast)

    def button3(self):
        button = QPushButton('Log Compression', self)
        button.setMinimumSize(200, 40)
        button.setToolTip('This is an example button')
        button.move(470, 220)
        button.clicked.connect(self.compression)

    def button4(self):
        button = QPushButton('Reverse Video', self)
        button.setMinimumSize(200, 40)
        button.setToolTip('This is an example button')
        button.move(470, 270)
        button.clicked.connect(self.reverse)

    @pyqtSlot()
    def histogram(self):
        global global_user_email
        global global_process_type
        global global_selected_name
        global_process_type = "HistogramEqualization"
        print(global_user_email)
        print(global_selected_name)
        print(global_process_type)
        client.post_processed_image(global_user_email,
                                    global_selected_name, global_process_type)
        print('posted')
        print(global_selected_name)
        self.close()
        self.next = App5()

    @pyqtSlot()
    def contrast(self):
        global global_user_email
        global global_process_type
        global global_selected_name
        global_process_type = "ContrastStretching"
        print(global_user_email)
        print(global_selected_name)
        print(global_process_type)
        client.post_processed_image(global_user_email,
                                    global_selected_name, global_process_type)
        self.close()
        self.next = App5()

    @pyqtSlot()
    def compression(self):
        global global_user_email
        global global_process_type
        global global_selected_name
        global_process_type = "LogCompression"
        print(global_user_email)
        print(global_process_type)
        client.post_processed_image(global_user_email,
                                    global_selected_name, global_process_type)
        self.close()
        self.next = App5()

    @pyqtSlot()
    def reverse(self):
        global global_user_email
        global global_process_type
        global global_selected_name
        global_process_type = "ReverseVideo"
        print(global_user_email)
        print(global_process_type)
        client.post_processed_image(global_user_email,
                                    global_selected_name, global_process_type)
        self.close()
        self.next = App5()

class App5(QMainWindow):

    def __init__(self):
        super().__init__()
        self.title = 'Image Processor'
        self.left = 10
        self.top = 10
        self.width = 720
        self.height = 480
        self.next = None
        self.init_gui()

    def init_gui(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)
        self.setAutoFillBackground(True)
        p = self.palette()
        p.setColor(self.backgroundRole(), QColor(204, 204, 255))
        self.setPalette(p)
        self.statusBar().showMessage('Step 4: Download Processed Image!')
        self.display_images()
        self.display_images_info()
        self.button_download()
        self.upload_new_images()
        self.button_histogram()
        self.show()

    def display_images(self):
        global global_user_email
        global global_process_type
        global global_selected_name
        global global_process_image
        image_strip = global_selected_name.split('/')[-1]
        image_name = image_strip.split('.')[0]
        processed_images = client.get_processed_image(
            global_user_email, image_name, global_process_type)
        print(processed_images.keys())
        label = QLabel(self)
        s = processed_images[image_name]
        global_process_image = s
        data = QByteArray.fromBase64(s.encode())
        image_type = image_strip.split('.')[1]
        upload_sizes = client.get_upload_sizes(global_user_email)
        upload_size = upload_sizes[image_name]
        size_string = "Upload Size: " + str(upload_size)
        upload_times = client.get_upload_time(global_user_email)
        upload_time = upload_times[image_name]
        print(upload_time)
        time_string = "Upload Time: " + upload_time
        pixmap = QPixmap()
        if pixmap.loadFromData(data, image_type):
            pixmap2 = pixmap.scaledToWidth(250)
            label.setPixmap(pixmap2)
            label.setGeometry(460, 100, 640, 250)
        label1 = QLabel(size_string, self)
        label1.move(455, 30)
        label1.setMinimumSize(220, 40)
        label2 = QLabel(time_string, self)
        label2.move(410, 50)
        label2.setMinimumSize(290, 40)

    def display_images_info(self):
        global global_user_email
        info = client.get_user_metrics(global_user_email)
        user_metrics = list(info.keys())
        user_metrics_info = list(info.values())
        for i, n in enumerate(user_metrics):
            label1 = QLabel(str(n), self)
            label1.move(30, 100 + i * 20)
            label1.setMinimumSize(220, 40)
        for i, n in enumerate(user_metrics_info):
            label2 = QLabel(str(n), self)
            label2.move(260, 100 + i * 20)
            label2.setMinimumSize(200, 40)

    def button_download(self):
        button = QPushButton('Download Image', self)
        button.setMinimumSize(200, 40)
        button.setToolTip('This is an example button')
        button.move(130, 370)
        button.clicked.connect(self.download)

    def upload_new_images(self):
        button = QPushButton('Upload New Image(s)', self)
        button.setMinimumSize(200, 40)
        button.setToolTip('This is an example button')
        button.move(130, 410)
        button.clicked.connect(self.new_upload)

    def button_histogram(self):
        button = QPushButton('View Histograms', self)
        button.setMinimumSize(200, 40)
        button.setToolTip('This is an example button')
        button.move(130, 330)
        button.clicked.connect(self.histogram_window)

    @pyqtSlot()
    def download(self):
        global global_process_image
        options = QFileDialog.Options()
        options |= QFileDialog.DontUseNativeDialog
        file_name, options = QFileDialog.getSaveFileName(
            self, "QFileDialog.getSaveFileName()",
            "", "JPEG Files(*.jpeg);; "
                "TIFF Files(*.tiff);; "
                "PNG Files(*.png)", options=options)
        if file_name:
            options = options.split('.')[-1]
            options = options.strip(')')
            img_buf = decode(global_process_image)
            img_array = skimage.io.imread(img_buf)
            plt.imsave(file_name + '.' + options,
                       img_array)

    @pyqtSlot()
    def new_upload(self):
        print('Upload New Images')
        self.close()
        self.next = App2()

    @pyqtSlot()
    def histogram_window(self):
        global global_selected_name
        global global_image_dict
        image_strip = global_selected_name.split('/')[-1]
        image_key = image_strip.split('.')[0]
        image_data = global_image_dict[image_key]
        make_hist(image_data)
        self.close()
        self.next = App6()

class App6(QMainWindow):

    def __init__(self):
        super().__init__()
        self.title = 'Image Processor'
        self.left = 10
        self.top = 10
        self.width = 720
        self.height = 480
        self.next = None
        self.init_gui()

    def init_gui(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)
        self.setAutoFillBackground(True)
        p = self.palette()
        p.setColor(self.backgroundRole(), QColor(204, 204, 255))
        self.setPalette(p)
        self.statusBar().showMessage('Step 5: View Histograms!')
        self.upload_new_images()
        self.histogram_original()
        self.histogram_processed()
        self.show()

    def upload_new_images(self):
        button = QPushButton('Upload New Image(s)', self)
        button.setMinimumSize(200, 30)
        button.setToolTip('This is an example button')
        button.move(260, 400)
        button.clicked.connect(self.new_upload)

    def histogram_original(self):
        label_text = QLabel('Original Image', self)
        label_text.move(105, 80)
        label_text.setMinimumSize(150, 40)
        label = QLabel('Original Image', self)
        pixmap = QPixmap('hist.jpeg')
        pixmap2 = pixmap.scaledToWidth(300)
        label.setPixmap(pixmap2)
        label.setGeometry(30, 120, 640, 200)

    def histogram_processed(self):
        global global_process_image
        vals = make_hist(global_process_image)
        label_text = QLabel('Processed Image', self)
        label_text.move(465, 80)
        label_text.setMinimumSize(150, 40)
        label = QLabel('Processed Image', self)
        pixmap = QPixmap('hist.jpeg')
        pixmap2 = pixmap.scaledToWidth(300)
        label.setPixmap(pixmap2)
        label.setGeometry(390, 120, 640, 200)

    @pyqtSlot()
    def new_upload(self):
        self.close()
        self.next = App2()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())


from __future__ import print_function
from keras.datasets import cifar10
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.convolutional import Convolution2D, MaxPooling2D
from keras.optimizers import SGD
from keras.utils import np_utils
from keras.layers.normalization import BatchNormalization
import numpy
import random
from keras.callbacks import ModelCheckpoint
from keras.callbacks import EarlyStopping
from sklearn.metrics import accuracy_score
import theano

from keras import backend as K

import numpy as np
import scipy as sp
from scipy import signal
import os
from PIL import Image

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import time

from scipy.misc import imread

def get_kernel(shape=(3,3),sigma=0.5):
    """
    2D gaussian mask - should give the same result as MATLAB's
    fspecial('gaussian',[shape],[sigma])
    """
    m,n = [(ss-1.)/2. for ss in shape]
    y,x = np.ogrid[-m:m+1,-n:n+1]
    h = np.exp( -(x*x + y*y) / (2.*sigma*sigma) )
    h[ h < np.finfo(h.dtype).eps*h.max() ] = 0
    sumh = h.sum()
    if sumh != 0:
        h /= sumh
    return h

def add_gnoise_util(image):
    kernel = get_kernel(shape=(10,10),sigma=3)
    image   = signal.convolve2d(image, kernel, boundary='fill', fillvalue=0,mode='same')
    #print(image.shape)
    return image

def convert_to_image(image, name):

	#plt.imshow(image, interpolation='none')
	#plt.savefig(name)
    a = (image*255).astype('uint8')
    im = Image.fromarray(a)
    im.save(name)

im = imread("boat.jpg")

im = numpy.swapaxes(im,1,2)
im = numpy.swapaxes(im,0,1)

im = im[numpy.newaxis,:,:,:]
print(im.shape)

im = im.astype('float32')
#im /= 255

batch_size = 256
nb_classes = 10
nb_epoch = 200
data_augmentation = True

# input image dimensions
img_rows, img_cols = 32, 32
# the CIFAR10 images are RGB
img_channels = 3

# the data, shuffled and split between train and test sets
(X_train, y_train), (X_test, y_test) = cifar10.load_data()

model = Sequential()

model.add(Convolution2D(64, 3, 3, border_mode='same',
                        input_shape=(img_channels, img_rows, img_cols)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))

model.add(Convolution2D(128, 3, 3, border_mode='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Convolution2D(256, 3, 3, border_mode='same'))
model.add(Activation('relu'))
model.add(Convolution2D(256, 3, 3, border_mode='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Convolution2D(1024, 4, 4, border_mode='valid'))
model.add(Activation('relu'))
model.add(Convolution2D(1024, 1, 1, border_mode='valid'))
model.add(Activation('relu'))

model.add(Convolution2D(nb_classes, 1, 1, border_mode='valid'))
vc = BatchNormalization()
model.add(vc)
model.add(Flatten())
#model.add(Dense(nb_classes))
model.add(Activation('softmax'))

model.load_weights("/data/lisatmp4/sarath/data/output/conv/1/weights.hdf5")#/data/lisatmp4/chinna/data/ift6268/temp/1/weights.hdf5")

# let's train the model using SGD + momentum (how original).
sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy', optimizer=sgd)

convout = theano.function([model.get_input(train=False)], vc.get_output(train=False))
t0 = time.clock()
[layer_output] = convout(im)
print(layer_output.shape)

dpath = "/data/lisatmp4/chinna/data/ift6268/temp/1/"

for i in range(0,10):
	convert_to_image(layer_output[i],dpath+str(i)+"old.jpg")
	layer_output[i] = add_gnoise_util(layer_output[i])
	print(max(layer_output[i].flatten()))
	convert_to_image(layer_output[i],dpath+str(i)+".jpg")
print ("Time")
print (time.clock() - t0)


# Kernel Support Vector Machine for Non-linearly Separable Data
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv('Datasets/Social_Network_Ads.csv')
X = df.iloc[:, 2:4].values
y = df.iloc[:, 4].values

# Split the Dataset into Train and Test Data
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, random_state = 0)

# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

# Now Its time to impliment the SVM Classifier
from sklearn.svm import SVC
classifier_rbf = SVC(kernel = 'rbf', random_state = 0)  # Changing the kernel value fron linear to rbf (Gaussion)
classifier_rbf.fit(X_train, y_train)

# Predict the Resultant Value
y_pred_rbf = classifier_rbf.predict(X_test)

# Classifier for Linear kernel
classifier_linear = SVC(kernel = 'linear', random_state = 0)  # Changing the kernel value fron linear to rbf (Gaussion)
classifier_linear.fit(X_train, y_train)

# Predict the Resultant Value
y_pred_linear = classifier_linear.predict(X_test)

# Classifier for SIGMOID kernel
classifier_sigmoid = SVC(kernel = 'sigmoid', random_state = 0)  # Changing the kernel value fron linear to rbf (Gaussion)
classifier_sigmoid.fit(X_train, y_train)

# Predict the Resultant Value
y_pred_sigmoid = classifier_sigmoid.predict(X_test)

# Classifier for SIGMOID kernel
classifier_poly = SVC(kernel = 'poly', random_state = 0)  # Changing the kernel value fron linear to rbf (Gaussion)
classifier_poly.fit(X_train, y_train)

# Predict the Resultant Value
y_pred_sigmoid = classifier_sigmoid.predict(X_test)

# Checking the accuracy of the Traning Set
print('RBF Train : ', classifier_rbf.score(X_train, y_train))
print('RBF Test : ', classifier_rbf.score(X_test, y_test))
print('Linear Train : ', classifier_linear.score(X_train, y_train))
print('Linear Test : ', classifier_linear.score(X_test, y_test))
print('Sigmoid Train : ', classifier_sigmoid.score(X_train, y_train))
print('Sigmoid Test : ', classifier_sigmoid.score(X_test, y_test))
print('Poly Train : ', classifier_poly.score(X_train, y_train))
print('Poly Test : ', classifier_poly.score(X_test, y_test))

# Lest Understand the confusion Matrix
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred_rbf)
print(cm)

# Visualising the Training Set Result
from matplotlib.colors import ListedColormap
X_set, y_set = X_train, y_train
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01),
                     np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01))
plt.contourf(X1, X2, classifier_rbf.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape), alpha = 0.75, cmap = ListedColormap(('red', 'green')))
plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())
for i, j in enumerate(np.unique(y_set)):
    plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],
                c = ListedColormap(('red', 'green'))(i), label = j)
plt.title('Support Vector Machine (Traning Set)')
plt.xlabel('Age')
plt.ylabel('Estimated Salary')
plt.legend()
plt.show()

# Visualising the Test Set Result
from matplotlib.colors import ListedColormap
X_set, y_set = X_test, y_test
X1, X2 = np.meshgrid(np.arange(start = X_set[:, 0].min() - 1, stop = X_set[:, 0].max() + 1, step = 0.01),
                     np.arange(start = X_set[:, 1].min() - 1, stop = X_set[:, 1].max() + 1, step = 0.01))
plt.contourf(X1, X2, classifier_rbf.predict(np.array([X1.ravel(), X2.ravel()]).T).reshape(X1.shape), alpha = 0.75, cmap = ListedColormap(('red', 'green')))
plt.xlim(X1.min(), X1.max())
plt.ylim(X2.min(), X2.max())
for i, j in enumerate(np.unique(y_set)):
    plt.scatter(X_set[y_set == j, 0], X_set[y_set == j, 1],
                c = ListedColormap(('red', 'green'))(i), label = j)
plt.title('Support Vector Machine (Test Set)')
plt.xlabel('Age')
plt.ylabel('Estimated Salary')
plt.legend()
plt.show()


from django import forms

from .models import PostModel

class PostForm(forms.ModelForm):

    class Meta:
        model = PostModel
        fields = ("post",)
        labels = {
            'post': 'Напишите пост',
        }
        widgets = {
            'post': forms.Textarea(attrs={'class': 'post-input'})
        }


import requests
from io import StringIO
import pandas as pd
import matplotlib.pyplot as plt

url = "https://www.twse.com.tw/exchangeReport/STOCK_DAY?response=csv&date=20210301&stockNo=2330"
resp = requests.get(url)
raw_data = resp.text
clean_data = raw_data.split('\r\n')[1:-6]
clean_data[0] = '"date","deal_amount","deal_price","open_price","highest_price","lowest_price","end_price","diff_pirce","deal_num",'

sum_str = ""
for data in clean_data:
    sum_str += data + '\r\n'

df = pd.read_csv(StringIO(sum_str), index_col=0)
df.to_csv("stock.csv")
df['end_price'].plot()
plt.show()


import sqlite3

class DBHelper:

    def __init__(self, dbname="julio_bot.sqlite"):
        self.dbname = dbname
        self.conn = sqlite3.connect(dbname)

    def setup(self):
        tblstmt = "CREATE TABLE IF NOT EXISTS items (trigger, response text)"
        trig_idx = "CREATE INDEX IF NOT EXISTS triggerIndex ON items (trigger ASC)"
        resp_idx = "CREATE INDEX IF NOT EXISTS responseIndex ON items (response ASC)"
        self.conn.execute(tblstmt)
        self.conn.execute(trig_idx)
        self.conn.execute(resp_idx)
        self.conn.commit()

    def add_item(self, trigger, response):
        stmt = "INSERT INTO items (trigger, response) VALUES (?, ?)"
        args = (trigger, response)
        self.conn.execute(stmt, args)
        self.conn.commit()

    def delete_item(self, trigger, response):
        stmt = "DELETE FROM items WHERE trigger = (?) AND response = (?)"
        args = (trigger, response)
        self.conn.execute(stmt, args)
        self.conn.commit()

    def get_items(self, trigger):
        stmt = "SELECT trigger FROM items"
        # args = (trigger)
        return [x[0] for x in self.conn.execute(stmt)]

    def get_response(self, trigger):
        stmt = "SELECT * FROM items WHERE trigger = (?)"
        args = (trigger, )
        return [x for x in self.conn.execute(stmt, args)]