#! /usr/bin/env python3

import argparse
import logging

import smbus

from slcc.Si534xdriver import Si534xdriver
from slcc.SiLabsTxtParser import SiLabsTxtParser
from slcc.extra_logging import LEVEL_TRACE

from slcc.BoardConfig import CLK_BUS

def main():
    """ Process input args, parse text file, program the device over I2C """

    parser = argparse.ArgumentParser(
        description='Get info about Si Labs device on I2C bus')
    parser.add_argument('--debug', action='store_true',
                        help='print debug information')
    parser.add_argument('--trace', action='store_true',
                        help='print trace information (very verbose)')
    parser.add_argument('--i2c_bus', default=CLK_BUS,
                        help='select I2C bus')
    parser.add_argument('i2c_addr', type=lambda x: int(x, 0),
                        help='address of the device on I2C bus')
    parser.add_argument('--get_rev', action='store_true',
                        help='print chip revision')

    args = parser.parse_args()

    if args.trace:
        logging.basicConfig(level=LEVEL_TRACE)
        logging.log(LEVEL_TRACE, "logging: enabled TRACE level")
    elif args.debug:
        logging.basicConfig(level=logging.DEBUG)
        logging.log(logging.DEBUG, "logging: enabled DEBUG level")
    else:
        # for get_rev command, be quiet if log level is not given explicitly
        logging.basicConfig(
            level=logging.WARNING if args.get_rev else logging.INFO)

    logging.debug("arguments:")
    logging.debug("  i2c bus: %d", args.i2c_bus)
    logging.debug("  i2c addr: 0x%02x", args.i2c_addr)

    clk_bus = smbus.SMBus(args.i2c_bus)
    si5341 = Si534xdriver(clk_bus, args.i2c_addr)

    if args.get_rev:
        print(si5341.get_rev())

if __name__ == "__main__":
    main()


import scrapy
from scrapy_splash import SplashRequest

import numpy as np

import json

from ..items import WallmartItem

import logging

class GetVariationsSpider(scrapy.Spider):
    name:str = 'GetVariationsSpider'
    allowed_domains:list = ['www.walmart.com']

    script1:str = '''
        function main(splash, args)
            splash.private_mode_enabled = false
            url = args.url
            assert(splash:go(url))
            assert(splash:wait(1))
            splash:set_viewport_full()
            return splash:html()
        end
    '''

    url:str = ''

    def start_requests(self, request):
        self.url = request.url
        return SplashRequest(
                url=str(self.url),
                callback=self.parse_item,
                endpoint='execute',
                args = {
                    'lua_source': self.script1
            }
        )

    def parse_item(self, response):
        item = WallmartItem()
        #specs = response.css

        item['title'] = response.xpath('//*[@id="product-overview"]/div/div[3]/div/h1//text()').get()
        item['price'] = response.css('span.price-characteristic::attr(content)').getall()
        item['deliveryDate'] = response.css('p.no-margin::text').get()
        item['pictures'] = response.css('img.prod-alt-image-carousel-image--left::attr(src)').getall()
        item['description'] = response.css('div.about-desc ::text').getall()
        item['variations'] = []

        return response.follow(self.url, self.variations, cb_kwargs=dict(item=item))

    def variations(self, response, item):
        variations:list = []

        #variations = response.css('div.variants__contain').getall()

        colorAvailability = response.css('div.variants__contain')[0]
        ids = colorAvailability.css('input.var__radio::attr(id)').getall()
        imgs = colorAvailability.css('img.tile__img::attr(src)').getall()
        variationAvailability = colorAvailability.css('label.var__contain::attr(availabilitystatus)').getall()

        variations = [
            ids,
            imgs,
            variationAvailability
        ]

        multiDimentionalArray = np.array(variations)

        testArray:list = []

        for i in range(0,len(ids)):
            testArray.append(multiDimentionalArray[0:, i])

        variationsList:list = []

        for j in testArray:
            placeholder = j.tolist()
            variationDict:dict = {
                'iD' : placeholder[0],
                'img' : placeholder[1],
                'availability' : placeholder[2]
            }
            variationsList.append(variationDict)

        item['variations'] = variationsList

        yield item


# -*- coding: utf-8 -*-
# coding: utf-8
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.chrome.options import Options

class chromeDriver:
    def __init__(self):
        chrome_options = Options()
        chrome_options.add_argument("--headless")
        chrome_options.add_argument('--no-sandbox')
        chrome_options.add_argument('--disable-dev-shm-usage')
        # 한국어 변환 추가
        chrome_options.add_argument("lang=ko_KR")
        chrome_options.add_argument("user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36")
        time.sleep(2)
        driver = webdriver.Chrome( '/data16/appscrap/chromedriver', options=chrome_options)
        time.sleep(2)
        self.driver = driver

    def get(self, url):
        self.driver.get(url)
        time.sleep(5)

    def exeScroll(self):
        no_of_pagedowns = 30
        body = self.driver.find_element_by_tag_name("body")
        while no_of_pagedowns:
            body.send_keys(Keys.PAGE_DOWN)
            time.sleep(0.2)
            no_of_pagedowns-=1

    def getHtml(self):
        return self.driver.page_source

    def close(self):
        self.driver.quit()


from typing import Union, Tuple

import numpy as np

from dispim import Volume

def extract_3d(data: np.ndarray, center: np.ndarray, half_size: int):
    """
    Extract an area around a point in a 3d numpy array, zero padded as necessary such that the specified point is at the
    center

    :param data: The numpy array to extract from
    :param center: The point around which to extract
    :param half_size: The half-size of the extracted area (full size is half_size*2+1, where the th center point is
                        center)
    :return: The extracted area
    """
    # FIXME: Doesn't always return the expected shape
    imax = np.clip(center + half_size + 1, 0, data.shape).astype(np.int)
    imin = np.clip(center - half_size, 0, data.shape).astype(np.int)

    subvol = data[imin[0]:imax[0], imin[1]:imax[1], imin[2]:imax[2]]

    max_missing = ((center + half_size + 1) - imax).astype(np.int)
    min_missing = (imin - (center - half_size)).astype(np.int)

    return np.pad(subvol, [(min_missing[i], max_missing[i]) for i in range(3)], mode='constant')

def crop_view(data: np.ndarray, crop: Union[float, Tuple[float, float, float]], center_crop: bool = True):
    """
    Get a cropped view of a 3d numpy array (does not modify the input)

    :param data: The numpy array to crop
    :param crop: The percentage to crop in each dimension
    :param center_crop: If True, the crop is centered around the middle of the volume, otherwise, the crop expands from
                                    (0, 0, 0)
    :return: The cropped view
    """
    if type(crop) == float or type(crop) == int:
        if crop > 0.99999:
            return data
        icropx = 1 - crop
        icropy = 1 - crop
        icropz = 1 - crop
    else:
        icropx = 1 - crop[0]
        icropy = 1 - crop[1]
        icropz = 1 - crop[2]

    w, h, l = data.shape

    if center_crop:
        view = data[int(w / 2 * icropx):int(-w / 2 * icropx),
               int(h / 2 * icropy):int(-h / 2 * icropy),
               int(l / 2 * icropz):int(-l / 2 * icropz)]
    else:
        view = data[:int(w * (1 - icropx)), :int(h * (1 - icropy)), :int(l * (1 - icropz))]

    return view

def plot_ortho_overlayed(vol_a: Volume, vol_b: Volume, axis=2, pixel_size: float = 1.0) -> None:
    """
    Plot two axis-reduced volumes overlayed as two channels (red and green), taking into account the spacing of both volumes

    :param vol_a: The first volume to plot (red)
    :param vol_b: The second volume to plot (green)
    :param axis: The axis along which both volumes will be reduced
    :param pixel_size: The size of a pixel, relative to the spacing of the the volumes
    """
    from scipy.ndimage.interpolation import zoom
    import matplotlib.pyplot as plt

    vol_a_zoomed = np.mean(zoom(vol_a, np.array(vol_a.spacing) * pixel_size), axis=axis)
    vol_b_zoomed = np.mean(zoom(vol_b, np.array(vol_b.spacing) * pixel_size), axis=axis)
    b_channel = np.zeros_like(vol_a_zoomed)

    max_val = max(vol_a_zoomed.max(), vol_b_zoomed.max())
    min_val = min(vol_a_zoomed.min(), vol_b_zoomed.min())

    vol_a_zoomed = (vol_a_zoomed - min_val) / (max_val - min_val)
    vol_b_zoomed = (vol_b_zoomed - min_val) / (max_val - min_val)

    plt.imshow(np.stack([vol_a_zoomed, vol_b_zoomed, b_channel], axis=2))
    plt.show()

def show_ipv(data: np.ndarray):
    """
    Show a 3d visualization of 3d numpy array
    :param data: The numpy array to show
    :return: The ipyvolume figure
    """
    import ipyvolume as ipv
    return ipv.quickvolshow(data)

def threshold_otsu(image: np.ndarray, nbins: int = 256, ignore: int = 0) -> float:
    """
    Compute the Otsu threshold for a numpy array, without taking into account empty areas

    :param image: The volume to compute the threshold for
    :param nbins: The number of bins used
    :param ignore: The value to ignore
    :return: The Otsu threshold
    """
    from skimage.filters.thresholding import histogram
    # Check if the image is multi-colored or not
    if image.min() == image.max():
        raise ValueError("threshold_otsu is expected to work with images "
                         "having more than one color. The input image seems "
                         "to have just one color {0}.".format(image.min()))

    img_flat = image.ravel()
    img_flat = img_flat[img_flat != ignore]
    hist, bin_centers = histogram(img_flat, nbins)
    hist = hist.astype(float)

    # class probabilities for all possible thresholds
    weight1 = np.cumsum(hist)
    weight2 = np.cumsum(hist[::-1])[::-1]
    # class means for all possible thresholds
    mean1 = np.cumsum(hist * bin_centers) / weight1
    mean2 = (np.cumsum((hist * bin_centers)[::-1]) / weight2[::-1])[::-1]

    # Clip ends to align class 1 and class 2 variables:
    # The last value of `weight1`/`mean1` should pair with zero values in
    # `weight2`/`mean2`, which do not exist.
    variance12 = weight1[:-1] * weight2[1:] * (mean1[:-1] - mean2[1:]) ** 2

    idx = np.argmax(variance12)
    threshold = bin_centers[:-1][idx]
    return threshold


import torch
import pandas as pd

def get_prompt_length(tokenizer, prompt):
    return len(tokenizer.encode(prompt))

def tokenize_multipart_input(
    tokenizer,
    input_text_list: list,
    max_seq_len: int,
    template=None,
    prompt=None,
):
    """This function is an adaptation of the `tokenize_multipart_input` found in princeton-nlp's repository
    at https://github.com/princeton-nlp/LM-BFF/blob/main/src/dataset.py.

    Modifications include:
    - Extension of automatic prompt generation for multi-label classification.
    - Removal of parameters like `first_sent_limit`, `other_sent_limit`, `gpt3`, `truncate_head`, and `support_labels`.
    - Optimization of the code flow.

    Args:
        tokenizer: a pre-trained tokenizer from Hugging Face Transformers
        input_text_list (list): documents ready for tokenization.
        max_seq_len (int): max sequence length after adding the prompt along with special tokens from BERT.
        template (str, optional): placeholder for the prompt.
        prompt (str, optional): the prompt we use for input text.
    """
    def enc(text):
        return tokenizer.encode(text, add_special_tokens=False)

    input_ids = []
    attention_mask = []
    token_type_ids = []  # Only for BERT
    mask_pos = None  # Position of the mask token

    if prompt:
        special_token_mapping = {
            "cls": tokenizer.cls_token_id,
            "mask": tokenizer.mask_token_id,
            "sep": tokenizer.sep_token_id,
            "sep+": tokenizer.sep_token_id,
        }
        # Get variable list in the template
        if prompt != "auto":
            template = template.replace("[PROMPT]", prompt)
        template_list = template.split("*")
        if prompt == "auto":
            # find cls place
            cls_pos = template_list.index("cls")
            if template_list[cls_pos + 1] == "":
                # For these kinds of cases: *cls**sent_0*_Liver*mask*.*sep+*
                # Prompt is next to sent_0.
                prompt = template_list[cls_pos + 3]
            elif template_list[cls_pos + 1] != "" and (
                template_list[cls_pos + 1].startswith("_")
            ):
                # For these kinds of cases: *cls*_Liver*mask*.*+sent_0**sep+*
                # Prompt is next to cls.
                prompt = template_list[cls_pos + 1]
            if prompt.startswith("_"):
                prompt = prompt[1:]
        segment_id = 0

        for part in template_list:
            new_tokens = []
            segment_plus_1_flag = False
            if part in special_token_mapping:
                new_tokens.append(special_token_mapping[part])
                if part == "sep+":
                    segment_plus_1_flag = True
            elif part[:5] == "sent_" or part[:6] == "+sent_":
                sent_id = int(part.split("_")[1])
                max_len = max_seq_len - 3 - get_prompt_length(tokenizer, prompt)
                # Tokenize and truncate to max_seq_len
                tokens = enc(input_text_list[sent_id])[-max_len:]
                new_tokens += tokens
            else:
                # Just natural language prompt
                part = part.replace("_", " ")
                # handle special case when T5 tokenizer might add an extra space
                if len(part) == 1:
                    new_tokens.append(tokenizer.convert_tokens_to_ids(part))
                else:
                    new_tokens += enc(part)

            input_ids += new_tokens
            attention_mask += [1 for i in range(len(new_tokens))]
            token_type_ids += [segment_id for i in range(len(new_tokens))]

            if segment_plus_1_flag:
                segment_id += 1

        mask_pos = [input_ids.index(tokenizer.mask_token_id)]
        # Make sure that the masked position is inside the max_length
        assert mask_pos[0] < max_seq_len

    else:
        input_ids = [tokenizer.cls_token_id]
        attention_mask = [1]
        token_type_ids = [0]
        max_len = max_seq_len - 2

        for sent_id, input_text in enumerate(input_text_list):
            if input_text is None:
                # Do not have text_b
                continue
            if pd.isna(input_text) or input_text is None:
                # Empty input
                input_text = ""
            input_tokens = enc(input_text)[:max_len] + [tokenizer.sep_token_id]
            input_ids += input_tokens
            attention_mask += [1 for i in range(len(input_tokens))]
            token_type_ids += [sent_id for i in range(len(input_tokens))]

    return input_ids, attention_mask, token_type_ids, mask_pos

class FewShotDataset(torch.utils.data.Dataset):
    """
    A class for creating the CGMH dataset in PyTorch.
    Currently, this class supports:
    (1) Few-shot data (e.g., train_size=16)
    (2) Small-size data (e.g., train_size>100)
    ---
    Attributes
        data (pd.DataFrame): the CGMH dataset
        tokenizer: a pre-trained HuggingFace tokenizer
        max_seq_len (int): maximum length for a sequence
        template (_type_, optional): template for the model. Defaults to None.
        prompt (_type_, optional): prompt for the model. Defaults to None.
    """

    def __init__(
        self,
        data: pd.DataFrame,
        tokenizer,
        max_seq_len: int,
        template=None,
        prompt=None,
    ):
        self.template = template
        self.prompt = prompt
        self.docs = data.X.tolist()
        self.labels = data.y_true.tolist()
        self.tokenizer = tokenizer
        self.max_seq_len = max_seq_len

    def __getitem__(self, idx):
        doc = self.docs[idx]
        input_ids, attn_mask, segs, mask_pos = tokenize_multipart_input(
            tokenizer=self.tokenizer,
            input_text_list=[doc],
            template=self.template,
            prompt=self.prompt,
            max_seq_len=self.max_seq_len,
        )
        item = {
            "input_ids": input_ids,
            "token_type_ids": segs,
            "attention_mask": attn_mask,
            "labels": [float(y) for y in self.labels[idx]],
        }
        if self.prompt:
            item["mask_pos"] = mask_pos
        return item

    def __len__(self):
        return len(self.docs)


"""
The module `model_runner.cross_validation_runner` includes functions to run k-fold cross validation using specified models

"""

import numpy as np
import pandas as pd

from sklearn.model_selection import KFold
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier
from sklearn.neural_network import MLPClassifier

_builtin_classifier = {
    "lr": LogisticRegression,
    "knn": KNeighborsClassifier,
    "svm": SVC,
    "rf": RandomForestClassifier,
    "mlp": MLPClassifier
}

def run_classifier_kfold_cv(X_train, y_train, X_test=None, classifier="rf", classifier_params=None, **kwargs):
    """

    Run classification model with K folds cross validation
    If test data is given, the final prediction (probabilities) for test data are averaged over all folds

    Parameters
    ----------
    X_train          : np.array, training data
    y_train          : np.array, training label
    X_test           : np.array, test data
    classifier       : str, classifier name
    classifier_params: dict, classifier training parameters
    kwargs           : other parameters needed

    Returns
    -------

    """

    folds = kwargs.get("n_folds", 5)
    shuffle = kwargs.get("shuffle", True)
    random_state = kwargs.get("random_state", 2019)
    kfold = KFold(n_splits=folds, shuffle=shuffle, random_state=random_state)
    oof_preds = np.zeros(y_train.shape[0])
    oof_preds_proba = np.zeros(y_train.shape[0])
    output = {}
    clf_list = []

    if classifier_params is None:
        classifier_params = {}
    if not isinstance(classifier_params, dict):
        raise ValueError("Argument `classifier_params` has to be dictionary or None by default.")

    for n_fold, (trn_idx, val_idx) in enumerate(kfold.split(X_train, y_train)):
        X_train_, X_val_ = X_train[trn_idx], X_train[val_idx]
        y_train_, y_val_ = y_train[trn_idx], y_train[val_idx]
        clf = _builtin_classifier[classifier](**classifier_params)
        clf.fit(X_train_, y_train_)
        oof_preds[val_idx] = clf.predict(X_val_)
        oof_preds_proba[val_idx] = clf.predict_proba(X_val_)[:, 1]
        clf_list.append(clf)

    # save out-of-fold predictions
    output["oof_preds"] = oof_preds
    output["oof_preds_proba"] = oof_preds_proba

    # run prediction on test data
    if X_test is not None:
        test_preds_proba = np.zeros(X_test.shape[0])
        for clf_ in clf_list:
            test_preds_proba += clf_.predict_proba(X_test)[:, 1] / folds
        output["test_preds_proba"] = test_preds_proba

    return output


import matplotlib.pyplot as plt
import matplotlib.dates as mdates

from display import formattingFigures as format

def display_data(dates, data, title, savefig=False, savePath=None):
    """
    Display daily new infections data and associated dates and given title.
    :param dates: ndarray of shape (days, ) for str in format 'YYYY-MM-DD'
    :param data: ndarray of shape (days, ) float of daily new infections
    :param title: str title
    :param savefig: (optional) bool for saving figure or not
    :param savePath: (optional) if savefig is True, precise the path where the figure is saved.
    :return: fig, ax and dates (in matplotlib.dates format)
    """
    formattedDates = [mdates.datestr2num(t) for t in dates]
    fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(19.5, 9.5))
    fig.tight_layout(pad=5.0)
    plt.ticklabel_format(axis="both", style="sci", scilimits=(0, 0))

    ax.plot(formattedDates, data, label="$\mathsf{Z}_t$", color='black')
    ax.set(title=title, ylabel='New cases $\mathsf{Z}_t$')

    # Formatting the grid
    ax.grid(which="major", linestyle='-', alpha=0.6)
    ax.grid(which="minor", linestyle='--', alpha=0.3)
    ax.legend()

    # Formatting xticks
    locator, dateFormatter = format.adaptiveDaysLocator(formattedDates)
    ax.xaxis.set_major_locator(locator)
    ax.xaxis.set_major_formatter(dateFormatter)

    # Formatting yticks
    ax.set_ylim(0, format.adaptiveYLimit(data))
    yScalarFormatter = format.ScalarFormatterClass(useMathText=True)
    yScalarFormatter.set_powerlimits((0, 0))
    ax.yaxis.set_major_formatter(yScalarFormatter)

    if savefig:
        fig.savefig(savePath)
    fig.show()
    return fig, ax, formattedDates

def display_REstim(dates, data, REstimate, method, OEstimate=None, comparison=False, RTrue=None,
                   dataUnder=False, savefig=False, savePath=None):
    """
    Display an estimation of R and Outliers estimations.
    :param dates: ndarray of shape (days, ) for str in format 'YYYY-MM-DD'
    :param data: ndarray of shape (days, ) float
    :param REstimate: ndarray of shape (days, ) float
    :param method: str between 'MLE', 'Cori', 'PL' and 'Joint' method to estimate R.
    :param OEstimate: (optional) ndarray of shape (days, )
    :param RTrue: (optional) ndarray of shape (days, )
    :param dataUnder: (optional) bool to display used data below on another axis. Can't be used if displaying RTrue
    :param comparison: (optional) bool to display multiple R estimations. method shoud be a list indexing REstimate.
    :param savefig: (optional) bool for saving figure or not
    :param savePath: (optional) if savefig is True, precise the path where the figure is saved.
    :return: fig, ax and dates (in matplotlib.dates format)
    """
    formattedDates = [mdates.datestr2num(t) for t in dates]
    if dataUnder:
        fig, axes = plt.subplots(nrows=2, ncols=1, figsize=(19.5, 12))
        fig.tight_layout(pad=6.5)
        fig.subplots_adjust(hspace=0.3, right=0.99, bottom=0.05, top=0.95)
        axR = axes[1]
        ax = axes[0]
    else:
        fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(19.5, 9.5))
        fig.tight_layout(pad=5)
        axR = ax

    plt.ticklabel_format(axis="both", style="sci", scilimits=(0, 0))

    # Displaying (or not) data used for following estimation ---
    if dataUnder:
        ax.plot(formattedDates, data, label="$\mathsf{Z}$", color='black')
        if comparison:
            for m in ['PL', 'Joint']:
                ax.plot(formattedDates, data - OEstimate[m], label='$\mathsf{Z}^{\mathsf{denoised}}$ ($\mathsf{%s}$)' % m,
                        color=format.colors['denoised%s' % m])
        elif OEstimate is not None and method in ['PL', 'Joint']:
            ax.plot(formattedDates, data - OEstimate, label='$\mathsf{Z}^{\mathsf{denoised}}$ ',
                    color=format.colors['denoised%s' % method])
        ax.legend(loc='upper left')
        ax.set(ylabel='New cases $\mathsf{Z}_t$')
    else:
        if OEstimate is not None:
            NoDisplayData = ValueError("Cannot display estimated outliers if no data displayed." +
                                       " Please set dataUnder = True")
            raise NoDisplayData

    # Displaying R estimation(s) ---
    if comparison:
        for m in method:
            axR.plot(formattedDates, REstimate[m], label='$\mathsf{R}^{\mathsf{%s}}$' % m, color=format.colors[m])
        axR.legend(loc='lower left')
    else:
        axR.plot(formattedDates, REstimate, label='$\mathsf{R}^{\mathsf{%s}}$' % method, color=format.colors[method])
        axR.legend(loc='lower left')

    # Displaying ground truth RTrue (if available) ---
    if RTrue is not None:
        if len(RTrue) != len(data):
            assert (len(RTrue) > len(data))
            RTrueCropped = RTrue[len(RTrue) - len(data):]
        else:
            RTrueCropped = RTrue
        axR.plot(formattedDates, RTrueCropped, color='black', label='$\mathsf{R}^{\mathrm{true}}$')

    axR.set(ylabel='$\mathsf{R}_t$')

    # Formatting xticks ---
    locator, dateFormatter = format.adaptiveDaysLocator(formattedDates)

    axR.xaxis.set_major_locator(locator)
    axR.xaxis.set_major_formatter(dateFormatter)

    # Formatting yticks ---
    yScalarFormatter = format.ScalarFormatterClass(useMathText=True)
    yScalarFormatter.set_powerlimits((0, 0))
    axR.set_ylim(0, 2)
    axR.set_yticks([0, 0.5, 1, 1.5, 2])

    # Formatting the grid ---
    axR.grid(which="major", linestyle='-', alpha=0.6)
    axR.grid(which="minor", linestyle='--', alpha=0.3)

    if dataUnder:
        # Formatting xticks ---
        ax.xaxis.set_major_locator(locator)
        ax.xaxis.set_major_formatter(dateFormatter)
        # Formatting yticks ---
        ax.set_ylim(0, format.adaptiveYLimit(data))
        ax.yaxis.set_major_formatter(yScalarFormatter)
        # Formatting the grid ---
        ax.grid(which="major", linestyle='-', alpha=0.6)
        ax.grid(which="minor", linestyle='--', alpha=0.3)
        ax.set_xticklabels([])

    # Saving figure (or not) ---
    if savefig:
        fig.savefig(savePath)
    fig.show()
    return fig, ax, formattedDates

def display_dataBuilt(datesBuilt, dataBuilt, RTrue, OTrue, displayO=False,
                      savefig=False, savePath=''):
    """
    Display the generated daily new cases (dataBuilt) compared to the ground truth (RTrue, OTrue) used to do so.
    :param datesBuilt: ndarray of shape (days, ) for str in format 'YYYY-MM-DD'
    :param dataBuilt: ndarray of shape (days, )
    :param RTrue: ndarray of shape (days, )
    :param OTrue: ndarray of shape (days, )
    :param displayO: (optional) bool for displaying OTrue or not
    :param savefig: (optional) bool for saving figure or not
    :param savePath: (optional) if savefig is True, precise the path where the figure is saved.
    :return: fig, ax and dates (in matplotlib.dates format)
    """
    formattedDates = [mdates.datestr2num(t) for t in datesBuilt]
    fig, axes = plt.subplots(nrows=2, ncols=1, figsize=(19.5, 12))
    fig.tight_layout(pad=7.5)
    fig.subplots_adjust(hspace=0.3, right=0.99, bottom=0.05, top=0.95)
    plt.ticklabel_format(axis="both", style="sci", scilimits=(0, 0))

    axR = axes[0]

    axR.plot(formattedDates, RTrue, color='black', label='$\mathsf{R}^{\mathrm{true}}$')
    axR.set(ylabel='$\mathsf{R}_t$')
    axR.legend(loc='lower left')

    ax = axes[1]
    ax.plot(formattedDates, dataBuilt, label="$\mathsf{Z}$", color=format.colors['synthData'])
    if displayO:
        ax.plot(formattedDates, dataBuilt - OTrue, label="$\mathsf{Z} - \mathsf{O}$", color='black')
    ax.legend(loc='upper left')
    ax.set(ylabel='New cases $\mathsf{Z}_t$')

    # Formatting xticks
    locator, dateFormatter = format.adaptiveDaysLocator(formattedDates)
    axR.xaxis.set_major_locator(locator)
    axR.xaxis.set_major_formatter(dateFormatter)

    ax.xaxis.set_major_locator(locator)
    ax.xaxis.set_major_formatter(dateFormatter)

    # Formatting yticks
    yScalarFormatter = format.ScalarFormatterClass(useMathText=True)
    yScalarFormatter.set_powerlimits((0, 0))

    axR.set_ylim(0, 2)
    axR.set_yticks([0, 0.5, 1, 1.5, 2])

    ax.set_ylim(0, format.adaptiveYLimit(dataBuilt))
    ax.yaxis.set_major_formatter(yScalarFormatter)

    # Formatting the grid
    axR.grid(which="major", linestyle='-', alpha=0.6)
    axR.grid(which="minor", linestyle='--', alpha=0.3)
    # Formatting the grid 2nd figure
    ax.grid(which="major", linestyle='-', alpha=0.6)
    ax.grid(which="minor", linestyle='--', alpha=0.3)
    ax.set_xticklabels([])

    # ax.set_title(method)
    if savefig:
        fig.savefig(savePath)
    fig.show()
    return fig, ax, formattedDates


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

from django.conf.urls import patterns, url

from ticket import views

urlpatterns = patterns('ticket.views',
    url(r'^$', views.index, name='index'),
    url(r'^new/$', views.new, name='new'),
    url(r'^(?P<ticket_id>\d+)/$', views.detail, name='detail'),
    url(r'^(?P<ticket_id>\d+)/comment/$', views.comment, name='comment'),
    url(r'^(?P<ticket_id>\d+)/visit/$', views.visit, name='visit'),
    url(r'^customer/(?P<phone>\d+)/$', views.customer, name='customer'),
    url(r'^customer_area/(?P<area_id>-?\d+)/$', views.customer_area, name='customer_area'),
)

# Local Variables: **
# comment-column: 56 **
# indent-tabs-mode: nil **
# python-indent: 4 **
# End: **


from pathlib import Path

import pytest
from invoke import Context

from erudition.__main__ import main
from erudition.data_challenge import constants, tasks
from erudition.util import cd_into, git_commit

def test_full(tmp_path):
    c = Context()
    origin_path = tmp_path / "origin"
    origin_path.mkdir(exist_ok=True)
    with cd_into(origin_path):
        c.run("git init -b main")
        c.run("git checkout -b tmp")

    ch_name = "test-challenge"
    main([None, "challenge", ch_name, tmp_path])

    with cd_into(tmp_path / f"{ch_name}-challenge"):
        c.run(f"git remote add origin {origin_path}")
        c.run("git push -u origin main")
        with cd_into(origin_path):
            c.run("git checkout main")
            with pytest.raises(EnvironmentError):
                tasks.test_modified_solutions(c, "P-1")
            c.run("git checkout -b tmp")
        c.run("git push --tags origin")
        _dummy_solution("s1", "{1: 2}")
        git_commit(c, "s1", "add s1")
        tasks.test_modified_solutions(c, "P-1")
        tasks.retag(c)
        _dummy_solution("s2", '{"X": 0}')
        git_commit(c, "s2", "add s2")
        tasks.test_modified_solutions(c, "P-1")
        tasks.retag(c)
        tasks.test_modified_solutions(c, "P-1")
        _dummy_solution("s2", '{"A": 10}')
        tasks.test_solution(c, "s2", "P-1")
        tasks.get_test_pack(c)

def _dummy_solution(name, todump):
    Path(name).mkdir(exist_ok=True)
    Path(name, constants.CONF_PATH).write_text("process-command: python m.py")
    py_str = f"""open("output.json", "w").write('{todump}')"""
    Path(name, "m.py").write_text(py_str)


#!/usr/bin/env python3
"""Set up pygleif."""

from pathlib import Path

from setuptools import find_packages, setup

PROJECT_DIR = Path(__file__).parent.resolve()
README_FILE = PROJECT_DIR / "README.md"
LONG_DESCRIPTION = README_FILE.read_text(encoding="utf-8")

VERSION = (PROJECT_DIR / "pygleif" / "VERSION").read_text().strip()

GITHUB_URL = "https://github.com/ggravlingen/pygleif"
DOWNLOAD_URL = f"{GITHUB_URL}/archive/{VERSION}.zip"

PACKAGES = find_packages(exclude=["tests", "tests.*"])

setup(
    name="pygleif",
    packages=PACKAGES,
    python_requires=">=3.10",
    version=VERSION,
    long_description=LONG_DESCRIPTION,
    author='ggravlingen',
    author_email="no@email.com",
    long_description_content_type="text/markdown",
    url=GITHUB_URL,
    include_package_data=True,
    license="MIT",
    keywords='lei-code lei api gleif leicode',
    download_url=DOWNLOAD_URL,
    classifiers=[
        "Development Status :: 5 - Production/Stable",
        "Natural Language :: English",
        "Programming Language :: Python :: 3",
        "Programming Language :: Python :: 3.10",
        "Programming Language :: Python :: 3.11",
        "Intended Audience :: Financial and Insurance Industry",
    ],
)


import serial
import time

class Manipulator():
    def __init__(self):
        '''Initializes Manipulator in ARPES Lab via RS232'''
        self.ser = serial.Serial(port='COM2', #Remember to check if the instrument is on COM2 or not
                    baudrate=9600,
                    timeout=3,
                    bytesize=serial.EIGHTBITS,
                    xonxoff=False)

    def write(self, input):
        writestr = "\x02" + str(input) + "\x03\r\n"
        self.ser.write(writestr.encode())

    def ask(self, instr):
        time.sleep(0.05)
        self.ser.flushInput()
        self.ser.flushOutput()
        self.write(instr)
        value = self.ser.readline().decode('utf-8')
        value = value[2:-3].strip()

        return value

    def go_to_absolute(self):
        pass

    def close(self):
        self.ser.close()

    def readVal(self, ax):
        ''' Reads Value of ax
        Returns
        -------
        str value
        '''
        self.ser.flushInput()
        self.ser.flushOutput()
        self.write(f'{ax}P21R')
        value = self.ser.readline().decode('utf-8')
        value = value[2:-3].strip()

        return value

    def getPossiblePositions(self, position):

        possible_coords = {
            'Gamma': {
                'X': -4,
                'Y': 0.0,
                # 'Z': -14.3,
            },
            'Insert': {
                'X': 34.8,
                'Y': -19.0,
                'Z': 26,
            }
        }

        possible_coords_old = {
            'Gamma': {
                'X': -414.0,
                'Y': 17.675,
                'Z': -25.5,
            },
            'Insert': {
                'X': -387.1,
                'Y': 0.7,
                'Z': -11.2,
            }
        }

        return possible_coords[position]


import numpy as np
import matplotlib.pyplot as plt
import matplotlib.lines as mlines
import torch
import sys
sys.path.append('../')
from dataloader import megadepth
import torch.utils.data
from CAPS.caps_model import CAPSModel
import cv2

class Visualization(object):
    def __init__(self, args):
        dataset = megadepth.MegaDepth(args)
        self.dataloader = torch.utils.data.DataLoader(dataset, batch_size=1, shuffle=True, num_workers=1)
        self.model = CAPSModel(args)
        self.loader_iter = iter(self.dataloader)

    def random_sample(self):
        self.sample = next(self.loader_iter)

    def plot_img_pair(self, with_std=False, with_epipline=False):
        self.coords = []
        self.colors = []
        self.with_std = with_std
        self.with_epipline = with_epipline
        im1 = self.sample['im1_ori']
        im2 = self.sample['im2_ori']
        self.h, self.w = im1.shape[1], im1.shape[2]
        im1 = im1.squeeze().cpu().numpy()
        im2 = im2.squeeze().cpu().numpy()
        blank = np.ones((self.h, 5, 3)) * 255
        out = np.concatenate((im1, blank, im2), 1).astype(np.uint8)
        self.fig = plt.figure(figsize=(12, 5))
        self.ax = self.fig.add_subplot(111)
        self.ax.imshow(out)
        self.ax.axis('off')
        plt.tight_layout()
        cid = self.fig.canvas.mpl_connect('button_press_event', self.onclick)

    def onclick(self, event):
        color = tuple(np.random.rand(3).tolist())
        coord = [event.xdata, event.ydata]
        self.coord = coord
        self.color = color
        self.coords.append(coord)
        self.colors.append(color)
        self.ax.scatter(event.xdata, event.ydata, c=color)
        self.find_correspondence()
        self.plot_correspondence()

    def find_correspondence(self):
        data_in = self.sample
        data_in['coord1'] = torch.from_numpy(np.array(self.coord)).float().cuda().unsqueeze(0).unsqueeze(0)
        data_in['coord2'] = data_in['coord1']
        self.model.set_input(data_in)
        coord2_e, std = self.model.test()
        self.correspondence = coord2_e.squeeze().cpu().numpy()
        self.std = std.squeeze().cpu().numpy()

    def plot_correspondence(self):
        point1 = self.coord
        point2 = self.correspondence
        point2[0] += self.w + 5
        self.ax.scatter(point2[0], point2[1], color=self.color)
        if self.with_std:
            circle = plt.Circle((point2[0], point2[1]), radius=100 * self.std, fill=False, color=self.color)
            self.ax.add_patch(circle)

        if self.with_epipline:
            line2 = cv2.computeCorrespondEpilines(np.array(point1).reshape(-1, 1, 2), 1,
                                                   self.sample['F'].squeeze().cpu().numpy())
            line2 = np.array(line2).squeeze()
            intersection = np.array([[0, -line2[2]/line2[1]],
                                     [-line2[2]/line2[0], 0],
                                     [self.w-1, -(line2[2]+line2[0]*(self.w-1))/line2[1]],
                                     [-(line2[1]*(self.h-1)+line2[2])/line2[0], self.h-1]])
            valid = (intersection[:, 0] >= 0) & (intersection[:, 0] <= self.w-1) & \
                    (intersection[:, 1] >= 0) & (intersection[:, 1] <= self.h-1)
            if np.sum(valid) == 2:
                intersection = intersection[valid].astype(int)
                x0, y0 = intersection[0]
                x1, y1 = intersection[1]
                l = mlines.Line2D([x0+self.w + 5, x1+self.w + 5], [y0, y1], color=self.color)
                self.ax.add_line(l)

        plt.show()


from collections import defaultdict,OrderedDict

#print the data
def printDict(d):
    count = d[0][1]
    for i in d:
        if i[1]==count:
            print(i[0],i[1])

#a functiona to sort alphabetically and value of dictionary
def sortDict(d):
    sorted_list=sorted(d.items(), key=lambda x: x[0])
    myOrdDic = OrderedDict(sorted_list)
    copyD = sorted(myOrdDic.items(), key=lambda x: x[1], reverse=True)
    printDict(copyD)

# a function to store the data into dictionary
def findUtil(d, n):
    for i in range(n):
        user_name, tweet_id = map(str,input().split())
        d[user_name]+=1
    sortDict(d)

# main or driven function
if __name__=='__main__':
    t=int(input())
    for test in range(t):
        n=int(input())
        d=defaultdict(int)
        findUtil(d, n)


#! coding: utf-8

import scrapy
import httplib2
from scrapy import Request
from djspider.items import GearBestItem

class ScrapyTestSpider(scrapy.Spider):
    name = "gearbest"
    allowed_domains = ["www.gearbest.com"]
    start_urls = ["http://www.gearbest.com/car-electronics-c_11247/",
    ]

    def parse(self, response):

        for href in response.xpath("//a[@class='proImg_a']/@href"):
            url = response.urljoin(href.extract())
            yield Request(url, callback=self.parse_product)

        url = response.urljoin(response.xpath("//a[@class='next']/@href")[0].extract())
        yield Request(url, callback=self.parse)

    def parse_product(self, response):
        item=GearBestItem()
        item['url']=str(response.url)
        item['name']=response.xpath('//*[@id="mainWrap"]/div[2]/div/h1/text()').extract()[0]
        item['code']=response.xpath('//*[@id="mainWrap"]/div[2]/div/span[2]/text()').extract()[0]
        preg=response.xpath('//*[@id="market_price"]/text()').extract()
        pdis=response.xpath('//*[@id="unit_price"]/text()').extract()

        item['price_reg']=[]
        item['price_discount']=[]

        if(len(preg)>0):
            item['price_reg'].append(str(preg[0]))
            item['price_discount'].append(str(pdis[0]))
        else:
            item['price_reg'].append(str(pdis[0]))

        img_url=response.xpath('//*[@id="js_jqzoom"]/img/@src').extract()[0]
        item['img']=img_url

        return item


import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QLabel, QVBoxLayout, QHBoxLayout, QFileDialog , QMessageBox
from PIL import Image
import torchvision.transforms as transforms
import torch
import torchvision
import torchvision.models as models
import torchvision.datasets as datasets
import torch.nn as nn
from PyQt5.QtCore import *
from PyQt5.QtGui import *

# dữ liệu
data_dir = '../DoAnML/CNN/data'
train_dir = data_dir + '/train'
val_dir = data_dir + '/validation'
test_dir = data_dir + '/test'
# định hình
transform = transforms.Compose([
    transforms.Resize((224,224)),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
                                ])
# Tạo dataset
train_data = datasets.ImageFolder(train_dir, transform=transform)
val_data = datasets.ImageFolder(val_dir, transform=transform)
test_data = datasets.ImageFolder(test_dir, transform=transform)
# Tạo dataloader
train_loader = torch.utils.data.DataLoader(train_data, batch_size=32, shuffle=True)
val_loader = torch.utils.data.DataLoader(val_data, batch_size=32, shuffle=True)
test_loader = torch.utils.data.DataLoader(test_data, batch_size=32, shuffle=True)

# Xây dựng mô hình
class CNN(nn.Module):
    def __init__(self):
        super(CNN, self).__init__()
        self.conv1 = nn.Conv2d(3, 32, 3, padding=1)
        self.pool = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(32, 64, 3, padding=1)
        self.fc1 = nn.Linear(64 * 56 * 56, 512)
        self.fc2 = nn.Linear(512, len(train_data.classes))

    def forward(self, x):
        x = self.pool(nn.functional.relu(self.conv1(x)))
        x = self.pool(nn.functional.relu(self.conv2(x)))
        x = torch.flatten(x, 1)
        x = nn.functional.relu(self.fc1(x))
        x = self.fc2(x)
        return x
model = CNN()

### tải lại mô hình đã lưu
# Tạo mô hình giống với mô hình đã được huấn luyện trước đó
model = torchvision.models.resnet18()
# Tải trọng số mô hình đã lưu vào mô hình mới
PATH = '../DoAnML/CNN/model.pth'
model.load_state_dict(torch.load(PATH))

# Sử dụng mô hình để dự đoán
input_data = torch.randn(1, 3, 224, 224)  # ví dụ input data ngẫu nhiên
output = model(input_data)

# tạo giao diện
app = QApplication(sys.argv)
window = QWidget()
window.setWindowTitle('Dự đoán trái cây và rau củ quả  🍅')
window.setGeometry(100, 100, 400, 200)

# Tạo các thành phần giao diện
image_path = None
image_label = QLabel('KHÔNG CÓ BỨC ẢNH NÀO ĐƯỢC CHỌN. 🍅 ')
image_label.setStyleSheet("background-color: #F0F0F0;font-weight:bold; font-size: 24px; color: red;")
image_label.setFont(QFont('Arial', 12))

image_display = QLabel()
image_display.setFixedSize(224, 224)

browse_button = QPushButton('Tải ảnh lên...')
browse_button.setStyleSheet("background-color: 'black'; font-size: 12pt; font-weight: bold; color:white")

predict_button = QPushButton('Dự Đoán')
predict_button.setStyleSheet("background-color: 'green' ; font-size: 12pt; font-weight: bold; color:white")

recovery_button = QPushButton('Khôi Phục Ảnh Gốc')
recovery_button.setStyleSheet("background-color: 'Blue'; font-size: 12pt; font-weight: bold; color:white")

result_label = QLabel('')
result_label.setFont(QFont('Arial', 12))

# Xử lý sự kiện khi nút "Browse" được nhấn
def browse_image():
    global image_path
    options = QFileDialog.Options()
    options |= QFileDialog.DontUseNativeDialog
    file_path, _ = QFileDialog.getOpenFileName(window, "Chọn ảnh", "", "Image Files (*.jpg *.jpeg *.png *.bmp)", options=options)
    if file_path:
        image_path = file_path
        image_label.setText('Ảnh đầu vào : ' + image_path)
        # Chuyển đổi hình ảnh PIL thành QImage
        img = Image.open(image_path)
        img = Image.open(image_path)
        img = img.resize((224, 224))
        gray_img = img.convert('L')  # Chuyển đổi sang ảnh màu xám
        qimage = QImage(gray_img.tobytes(), gray_img.size[0], gray_img.size[1], QImage.Format_Grayscale8)
        image_display.setPixmap(QPixmap.fromImage(qimage))

# Xử lý sự kiện khi nút "Predict" được nhấn
def predict_image():

    if image_path:
        # Đọc và tiền xử lý hình ảnh đầu vào
        img = Image.open(image_path)
        transform = transforms.Compose([
            transforms.Resize((224,224)),
            transforms.ToTensor(),
            transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])])
        img_tensor = transform(img)
        img_tensor = torch.unsqueeze(img_tensor, 0)
     # Dùng mô hình để dự đoán
        model.eval()
        with torch.no_grad():
            output = model(img_tensor)
            _, predicted = torch.max(output.data, 1)
        # Chuyển đổi kết quả dự đoán sang loại rau củ hay trái cây và tên loại
        class_names = train_data.classes
        if predicted.item() == 0:
            result_label.setText("Ảnh này được phân loại là một loại hoa quả  .\nTên loại là: " + class_names[predicted.item()])
        else:
            result_label.setText("Ảnh này được phân loại là một loại rau củ   .\nTên loại là: " + class_names[predicted.item()])

# Recovery image

    else:
        message_box = QMessageBox()
        message_box.setWindowTitle("Thông báo")
        message_box.setText("Hãy chọn vào 1 bức ảnh.")
        message_box.exec_()

def recovery_image():
     img_pixmap = QPixmap(image_path).scaled(image_display.size(), Qt.KeepAspectRatio)
     image_display.setPixmap(img_pixmap)

# Kết nối các thành phần với sự kiện
browse_button.clicked.connect(browse_image)
predict_button.clicked.connect(predict_image)
recovery_button.clicked.connect(recovery_image)

# Bố trí các thành phần trên cửa sổ giao diện
image_layout = QVBoxLayout()
image_layout.addWidget(image_label)
image_layout.addWidget(image_display)

button_layout = QHBoxLayout()
button_layout.addWidget(browse_button)

button_layout = QHBoxLayout()
button_layout.addWidget(browse_button)
button_layout.addWidget(predict_button)
button_layout.addWidget(recovery_button)

main_layout = QVBoxLayout()
main_layout.addLayout(image_layout)
main_layout.addLayout(button_layout)
main_layout.addWidget(result_label)

window.setLayout(main_layout)

window.show()
sys.exit(app.exec_())


#!/usr/bin/env python3
import picamera
import datetime
import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
SHUTTER_SWITCH = 4  # Physical pin 26, switch to GND
MODE_SWITCH = 17
GPIO.setup(SHUTTER_SWITCH,GPIO.IN,pull_up_down = GPIO.PUD_UP)
GPIO.setup(MODE_SWITCH,GPIO.IN,pull_up_down = GPIO.PUD_UP)

print ("Press Ctrl & C to Quit")

mode = "None"

while True:
    try:
        while mode == "None":
            if GPIO.input(MODE_SWITCH) == 0:
                print("Switched to Picture")
                mode = "Picture"
                time.sleep(0.5)

    except KeyboardInterrupt:
        print("Quit")
        GPIO.cleanup()

    try:
        while mode == "Picture":
            with picamera.PiCamera() as camera:
                camera.resolution = (1280, 720)
                now = datetime.datetime.now()
                timestamp = now.strftime("%y-%m-%d_%H-%M-%S")
                camera.start_preview()
                if GPIO.input(SHUTTER_SWITCH) == 0:
                    print("Snap")
                    camera.capture("/mnt/extra/" + str(timestamp) + '.jpeg')
                    time.sleep(.5)
                if GPIO.input(MODE_SWITCH) == 0:
                    print("Switched to Video")
                    mode = "Video"
                    time.sleep(0.5)

    except KeyboardInterrupt:
        print ("Quit")
        GPIO.cleanup()

    Record = 0

    try:
        while mode == "Video":
            with picamera.PiCamera() as camera:
                camera.resolution = (1280, 720)
                camera.framerate = 60
                if GPIO.input(MODE_SWITCH) == 0:
                   print("Switched to None")
                   mode = "None"
                   time.sleep(0.5)
                elif GPIO.input(SHUTTER_SWITCH) == 0:
                    Record = 1
                    print ("Recording")
                    now = datetime.datetime.now()
                    timestamp = now.strftime("%y-%m-%d_%H-%M-%S")
                    camera.start_preview()
                    camera.start_recording("/mnt/extra/" + str(timestamp) + '.h264')
                    time.sleep(.5)
                    while Record == 1:
                        if GPIO.input(SHUTTER_SWITCH) == 0:
                            print ("Stopped")
                            camera.stop_recording()
                            Record = 0
                            while GPIO.input(SHUTTER_SWITCH) == 0:
                                time.sleep(0.1)
                            time.sleep(.5)
                        elif GPIO.input(MODE_SWITCH) == 0 and Record == 1:
                            camera.stop_recording()
                            Record = 0
                            print("Switched to None")
                            mode = "None"
                            time.sleep(0.5)

    except KeyboardInterrupt:
        print ("Quit")
        GPIO.cleanup()


"""
Backend agnostic linear regression with gradient descent optimization:
a demonstration on most of core features and paradigm of tensorcircuit
"""

# this script shows how backend agnostic magic works, no code change is required to switch backend
# we also include jit, vmap and AD features in this pure classical example
# this demonstrates that tensorcircuit can serve as a solid unified ML library without any "quantumness"

import sys

sys.path.insert(0, "../")
import numpy as np
import tensorcircuit as tc

# (x, y) data preparation

nsamples = 100
k0 = 1.0
b0 = 0.0

xs0 = np.random.uniform(low=-1, high=1, size=[nsamples])
ys0 = k0 * xs0 + b0 + np.random.normal(scale=0.1, size=[nsamples])

def lr(xs, ys):
    """
    fully ML backend agnostic linear regression implementation
    """

    # construct the loss
    def loss_pointwise(x, y, param):
        k, b = param["k"], param["b"]
        yp = k * x + b
        return (yp - y) ** 2

    # we suppose this loss function only works for scalar, so that we can show the usage of ``vmap``

    loss_vmap = tc.backend.vmap(loss_pointwise, vectorized_argnums=(0, 1))

    # now we define the total loss for all data

    def loss(xs, ys, param):
        losses = loss_vmap(xs, ys, param)
        return tc.backend.sum(losses)

    # we get the jitted function to evaluate loss and its derivatives wrt. param

    loss_and_grad = tc.backend.jit(tc.backend.value_and_grad(loss, argnums=2))

    # setup initial values and optimizers

    weight = {"k": tc.backend.implicit_randn(), "b": tc.backend.implicit_randn()}

    if tc.backend.name == "tensorflow":
        import tensorflow as tf

        opt = tc.backend.optimizer(tf.keras.optimizers.Adam(1e-2))
    elif tc.backend.name == "jax":
        import optax

        opt = tc.backend.optimizer(optax.adam(1e-2))
    else:
        raise ValueError("Unsupported backend")

    # gradient descent optimization loop
    maxstep = 500
    for i in range(maxstep):
        loss, grad = loss_and_grad(xs, ys, weight)
        weight = opt.update(grad, weight)
        if i % 100 == 0 or i == maxstep - 1:
            print("optimized MSE loss after %s round: " % i, tc.backend.numpy(loss))

    return tc.backend.numpy(weight["k"]), tc.backend.numpy(weight["b"])

if __name__ == "__main__":
    for n in ["tensorflow", "jax"]:
        with tc.runtime_backend(n):  # runtime backend switch with context manager
            print("~~~~~~~~ using %s backend ~~~~~~~~" % n)
            xs_tensor, ys_tensor = tc.array_to_tensor(xs0, ys0, dtype="float32")
            print("predicted coefficient", lr(xs_tensor, ys_tensor))


from pyrogram import Client, filters
from pyrogram.types import Message
from .utils.utils import modules_help, prefix
from .utils.scripts import restart
import requests
import os
import hashlib

@Client.on_message(filters.command(['modhash', 'mh'], prefix) & filters.me)
async def get_mod_hash(client: Client, message: Message):
    if len(message.command) == 1:
        return
    url = message.command[1]
    resp = requests.get(url)
    if not resp.ok:
        await message.edit(f'Troubleshooting with downloading module {url}')
        return
    await message.edit(f'Module hash: {hashlib.sha256(resp.content).hexdigest()}')

@Client.on_message(filters.command(['loadmod', 'lm'], prefix) & filters.me)
async def load_mods(client: Client, message: Message):
    if len(message.command) == 1:
        return
    url = message.command[1]

    async def download_mod(content=None):
        if not os.path.exists(f'{os.path.abspath(os.getcwd())}/plugins/custom_modules'):
            os.mkdir(f'{os.path.abspath(os.getcwd())}/plugins/custom_modules')
        code = requests.get(url) if content is None else content
        if not code.ok:
            await message.edit(f'Can\'t find module {url.split("/")[-1].split(".")[0]}')
            return
        with open(f'./plugins/custom_modules/{url.split("/")[-1]}', 'wb') as mod:
            mod.write(code.content)
        await message.edit(f'The module {url.split("/")[-1].split(".")[0]} is loaded!')
        await restart()

    if '/'.join(url.split('/')[:6]) == 'https://raw.githubusercontent.com/Dragon-Userbot/custom_modules/main':
        await download_mod()
    elif '/' not in url and '.' not in url:
        url = f'https://raw.githubusercontent.com/Dragon-Userbot/custom_modules/main/{url}.py'
        await download_mod()
    else:
        resp = requests.get(url)
        if not resp.ok:
            await message.edit(f'Troubleshooting with downloading module {url}')
            return
        hashes = requests.get('https://raw.githubusercontent.com/Dragon-Userbot/custom_modules/main/modules_hashes.txt').text
        if hashlib.sha256(resp.content).hexdigest() in hashes:
            await download_mod(resp)
        else:
            await message.edit(
                'Only verified'
                ' modules or from the official '
                'custom_modules repository are supported!',
                disable_web_page_preview=True)

@Client.on_message(filters.command(['unloadmod', 'ulm'], prefix) & filters.me)
async def unload_mods(client: Client, message: Message):
    if len(message.command) > 1:
        mod = message.command[1]
        if '/'.join(mod.split('/')[:6]) == 'https://raw.githubusercontent.com/Dragon-Userbot/custom_modules/main':
            mod = '/'.join(mod.split('/')[6:]).split('.')[0]

        if os.path.exists(f'{os.path.abspath(os.getcwd())}/plugins/custom_modules/{mod}.py'):
            os.remove(
                f'{os.path.abspath(os.getcwd())}/plugins/custom_modules/{mod}.py')
            await message.edit(f'The module {mod} removed!')
            await restart()

        elif os.path.exists(f'{os.path.abspath(os.getcwd())}/plugins/{mod}.py'):
            await message.edit(f'It is forbidden to remove built-in modules, it will disrupt the updater')

        else:
            await message.edit(f'Module {mod} not found')

modules_help.update(
    {'loader': '''loadmod [link] - Download module]\n[Only modules from the official custom_modules repository and proven modules whose hashes are in modules_hashes.txt are supported,
                  unloadmod [module_name] - Delete module,
                  modhash [link] - Get module hash by link''',
    'loader module': 'Loader: loadmod, unloadmod, modhash'})


import pygame

GREEN = (0, 255, 0)

WIDTH = 800
HEIGHT = 600

screen = pygame.display.set_mode((WIDTH, HEIGHT))

running = True

while running:
    for evt in pygame.event.get():
        if evt.type == pygame.QUIT:
            running = False
        mx, my = pygame.mouse.get_pos()
        mb = pygame.mouse.get_pressed()

    for i in range(-900, 900, 10):
        pygame.draw.line(screen, GREEN, (i, 0), (i+750, 800), 1)
        pygame.draw.line(screen, GREEN, (900 - i, 0), (900 - i-750, 800), 1)
    pygame.display.flip()

pygame.quit()


from __future__ import annotations

from typing import Any, Iterable, Sequence, Literal

import ibis.expr.types as ir

import ibisml as ml
from ibisml.core import Metadata, Step, Transform
from ibisml.select import SelectionType, selector

class ExpandDate(Step):
    """A step for expanding date columns into one or more features.

    New features will be named ``{input_column}_{component}``. For example, if
    expanding a ``"year"`` component from column ``"x"``, the feature column
    would be named ``"x_year"``.

    Parameters
    ----------
    inputs
        A selection of date columns to expand into new features.
    components
        A sequence of components to expand. Options include

        - ``day``: the day of the month as a numeric value
        - ``week``: the week of the year as a numeric value
        - ``month``: the month of the year as a categorical value
        - ``year``: the year as a numeric value
        - ``dow``: the day of the week as a categorical value
        - ``doy``: the day of the year as a numeric value

        Defaults to ``["dow", "month", "year"]``.

    Examples
    --------
    >>> import ibisml as ml

    Expand date columns using the default components

    >>> step = ml.ExpandDate(ml.date())

    Expand specific columns using specific components

    >>> step = ml.ExpandDate(["x", "y"], ["day", "year"])
    """

    def __init__(
        self,
        inputs: SelectionType,
        components: Sequence[Literal["day", "week", "month", "year", "dow", "doy"]] = (
            "dow",
            "month",
            "year",
        ),
    ):
        self.inputs = selector(inputs)
        self.components = list(components)

    def _repr(self) -> Iterable[tuple[str, Any]]:
        yield ("", self.inputs)
        yield ("components", self.components)

    def fit(self, table: ir.Table, metadata: Metadata) -> Transform:
        columns = self.inputs.select_columns(table, metadata)
        if "month" in self.components:
            for col in columns:
                metadata.set_categories(
                    f"{col}_month",
                    [
                        "January",
                        "February",
                        "March",
                        "April",
                        "May",
                        "June",
                        "July",
                        "August",
                        "September",
                        "October",
                        "November",
                        "December",
                    ],
                )
        if "dow" in self.components:
            for col in columns:
                metadata.set_categories(
                    f"{col}_dow",
                    [
                        "Monday",
                        "Tuesday",
                        "Wednesday",
                        "Thurday",
                        "Friday",
                        "Saturday",
                        "Sunday",
                    ],
                )
        return ml.transforms.ExpandDate(columns, self.components)

class ExpandTime(Step):
    """A step for expanding time columns into one or more features.

    New features will be named ``{input_column}_{component}``. For example, if
    expanding an ``"hour"`` component from column ``"x"``, the feature column
    would be named ``"x_hour"``.

    Parameters
    ----------
    inputs
        A selection of time columns to expand into new features.
    components
        A sequence of components to expand. Options include ``hour``,
        ``minute``, ``second``, and ``millisecond``.

        Defaults to ``["hour", "minute", "second"]``.

    Examples
    --------
    >>> import ibisml as ml

    Expand time columns using the default components

    >>> step = ml.ExpandTime(ml.time())

    Expand specific columns using specific components

    >>> step = ml.ExpandTime(["x", "y"], ["hour", "minute"])
    """

    def __init__(
        self,
        inputs: SelectionType,
        components: Sequence[Literal["hour", "minute", "second", "millisecond"]] = (
            "hour",
            "minute",
            "second",
        ),
    ):
        self.inputs = selector(inputs)
        self.components = list(components)

    def _repr(self) -> Iterable[tuple[str, Any]]:
        yield ("", self.inputs)
        yield ("components", self.components)

    def fit(self, table: ir.Table, metadata: Metadata) -> Transform:
        columns = self.inputs.select_columns(table, metadata)
        return ml.transforms.ExpandTime(columns, self.components)


import pygame as pg
from gtts import gTTS

counter = 1

class Tts:

	tts_file = ""
	text = ""
	lang = ""

	def __init__(self, tts_file, text, lang):
		self.tts_file = tts_file
		self.text = text
		self.lang = lang

		freq = 24000
		bitsize = -16
		channels = 2
		buffer = 4096
		pg.mixer.init(freq, bitsize, channels, buffer)

	def createAndPlay(self, volume):
		tts = gTTS(self.text, self.lang)
		global counter
		tts.save("testSpeech/sounds/" + self.tts_file+str(counter)+str(".mp3"))

		pg.mixer.music.set_volume(volume)
		clock = pg.time.Clock()
		try:
			pg.mixer.music.load("testSpeech/sounds/" + self.tts_file + str(counter) + str(".mp3"))
			print("Music file {} loaded!".format(self.tts_file + str(counter) + str(".mp3")))
		except pg.error:
			print("File {} not found! ({})".format(self.tts_file + str(counter) + str(".mp3"), pg.get_error()))
			return
		pg.mixer.music.play()
		while pg.mixer.music.get_busy():
			clock.tick(100)
		pg.mixer.music.stop()
		pg.mixer.quit()

		counter += 1

if __name__ == "__main__":
	tts = Tts("test", "salut petit coquin", "fr")
	tts.createAndPlay(100)


import json
import logging

logger = logging.getLogger()
logger.setLevel(logging.INFO)

def http_response(body, status_code=200):
    if not isinstance(body, dict):
        body = dict(body)
    return build_response(status_code, body)

def build_response(status_code, body):
    logger.info(body)
    return {
        "statusCode": status_code,
        "headers": {
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Credentials": True,
        },
        "body": json.dumps(body),
    }


#pip3 install opencv-python
#Run from the terminal in Mac

import sys
import os
import cv2
import time

if len(sys.argv) != 3:
    print("Usage: <video_capture_source_number> <sleep_sec>")
    sys.exit(1)

video_source = int(sys.argv[1])
sleep_sec = int(sys.argv[2])

outdir = "./data/images/"

if not os.path.exists(outdir):
    os.makedirs(outdir)

# video capture source.  video_source=0 is usually the main webcam.
webcam = cv2.VideoCapture(video_source)
print(f"WEBCAM: {webcam}")

try:
    while(True):
        # Get an image from the webcam
        ret, frame = webcam.read()

        if ret:
            time_str = time.strftime("%Y-%m-%dT%H:%M:%S", time.gmtime())
            f = f"{outdir}/image-{time_str}.png"
            print(f"WRITING IMAGE: {f}")
            cv2.imwrite(f, frame)
        else:
            print("NO FRAME RETURNED")
        time.sleep(sleep_sec)
except KeyboardInterrupt:
    print("CAMERA: SHUTDOWN")
    cv2.destroyAllWindows()
    webcam.release()


from math import log
import maxflow
import sys
from itertools import combinations
from random import choice
from math import log
from random import shuffle
import itertools

class Denoiser:

    def __init__(self, param, beta_param, labels):
        self.param = param
        self.beta_param = beta_param
        self.labels = labels

    def D(self, evaluation, label):
        if evaluation == label:
            return -log(1 - self.param)
        else:
            return -log(self.param)

    def V(self, evaluation, label):
        if evaluation == label:
            return 0
        else:
            return self.beta_param

    def E(self, original_eval, new_eval):
        d_cost = sum(map(self.D,original_eval, new_eval))
        v_cost = sum(map(self.V,new_eval[0:-1],new_eval[1:]))
        energy = d_cost + v_cost
        return energy

    def neighbors_map(self, evals, alpha, beta):
        neighbors = {}
        for i, ev in enumerate(evals):
            if i == 0 and len(evals) != 1:
                if evals[i+1] not in [alpha, beta]:
                    neighbors[i] = [evals[i+1]]
                else:
                    neighbors[i] = []
            elif i == len(evals)-1:
                if evals[i-1] not in [alpha, beta]:
                    neighbors[i] = [evals[i-1]]
                else:
                    neighbors[i] = []
            else:
                evs = [evals[i-1],evals[i+1]]
                neighbors[i] = [x for x in evs if x not in [alpha, beta]]
        return neighbors

    def make_graph_swap(self, new_evals, original_eval, alpha, beta):
        n_link = self.V(alpha, beta)
        indices = []
        g = maxflow.Graph[float](200, 200)
        neighbors = self.neighbors_map(new_evals, alpha, beta)
        for i, ev in enumerate(new_evals):
            if ev in [alpha, beta]:
                if len(neighbors[i]) != 0:
                    t_alpha = self.D(original_eval[i], alpha) + sum(map(self.V, neighbors[i], [alpha]*len(neighbors[i])))
                    t_beta = self.D(original_eval[i], beta) + sum(map(self.V, neighbors[i], [beta]*len(neighbors[i])))
                else:
                    t_alpha = self.D(original_eval[i], alpha) + 0
                    t_beta = self.D(original_eval[i], beta) + 0
                indices.append(i)
                node = g.add_nodes(1)
                g.add_tedge(node[0], t_alpha, t_beta)
                if i != 0:
                    if new_evals[i-1] in [alpha, beta]:
                        g.add_edge(node[0], node[0]-1, n_link, n_link)
        return g, indices

    def make_graph_expand(self, new_evals, original_eval, alpha):
        indices = []
        g = maxflow.Graph[float](200, 200)
        node_next = False
        for i, ev in enumerate(new_evals):
            t_alpha = self.D(original_eval[i], alpha) #+ 1     # adding this term ( + 1) makes a difference
            if node_next == False:
                node = g.add_nodes(1)

            else:
                node = node_next
                node_next = False

            indices.append(node[0])

            if ev == alpha:
                t_not_alpha = 100000000
            else:
                t_not_alpha = self.D(new_evals[i], original_eval[i])

            g.add_tedge(node[0], t_alpha, t_not_alpha)

            if i != len(new_evals) - 1:
                if ev != new_evals[i+1]:
                    node_a = g.add_nodes(1)
                    node_next = g.add_nodes(1)
                    t_a_not_alpha = self.V(ev, new_evals[i+1])
                    g.add_tedge(node_a[0], 0, t_a_not_alpha)
                    e_p_a = self.V(ev, alpha)
                    g.add_edge(node[0], node_a[0], e_p_a, e_p_a)
                    e_a_q = self.V(new_evals[i+1], alpha)
                    g.add_edge(node_next[0], node_a[0], e_a_q, e_a_q)
                else:
                    node_next = g.add_nodes(1)
                    e_p_q = self.V(ev, alpha)
                    g.add_edge(node_next[0], node[0], e_p_q, e_p_q)

        return g, indices

    def construct_evals_swap(self, f, graph, indices, alpha, beta):
        for i, index in enumerate(indices):
            if graph.get_segment(i) == 1:
                f[index] = alpha
            else:
                f[index] = beta
        return f

    def construct_evals_expand(self, f, graph, indices, label):
        for i, index in enumerate(indices):
            if graph.get_segment(index) == 1:
                f[i] = label
        return f

    def swap(self, evals, pairs = None):
        f = evals
        e_value = self.E(evals, evals)
        if pairs == None:
            pairs = list(combinations(self.labels, 2))
        success = True
        while success:
            success = False
            for pair in pairs:
                graph, indices = self.make_graph_swap(f, evals, pair[0], pair[1])
                graph.maxflow()
                new_evals = self.construct_evals_swap(f,graph, indices, pair[0], pair[1])
                new_e_value = self.E(evals, new_evals)
                if new_e_value < e_value:
                    f = new_evals
                    e_value = new_e_value
                    success = True
        return f

    def expand(self, evals):
        f = evals
        e_value = self.E(evals, evals)
        success = True
        while success:
            success = False
            for label in self.labels:
                graph, indices = self.make_graph_expand(f, evals, label)
#                 plot_graph_3d(graph, (10,10,10))
                graph.maxflow()
                new_evals = self.construct_evals_expand(f,graph, indices, label)
                new_e_value = self.E(evals, new_evals)
                if new_e_value < e_value:
                    f = new_evals
                    e_value = new_e_value
                    success = True
        return f


#coding:utf8
# __author__ = 'fenton-fd.zhu'

from django.shortcuts import render, render_to_response, HttpResponse
from django.http import HttpRequest, HttpResponseRedirect
from django.template import loader, RequestContext, Context
from website.python.common.response import ResponsesSingleton
from website.python.userBase.userBase import UserBase
from website.python.organizationInfo.organizationDatabase import OrganizationInfoManager, JobsManager
from website.python.jobsInfo.jobsDatabase import JobsInfoManager
from SchoolJob import settings

'''
主页
'''
class Main(object):

    def __init__(self, request=HttpRequest()):
        super(Main, self).__init__();
        self.request = request;
        self.jobInfoManager = JobsInfoManager();
        self.jobManager = JobsManager();
        self.organizationInfoManager = OrganizationInfoManager();

    #获取当前选定学校的所有组织
    def getAllOrganization(self):
        uid = self.request.session.get('universityId', None);  #获取学校id
        #获取所有
        organization = OrganizationInfoManager();
        organizationList = organization.getData(universityId=uid);
        return organizationList;

    #根据搜索条件获取job
    def getJobsByKeyword(self):

        organizationAccount = self.request.GET.get('organizationAccount', None);
        jobName = self.request.GET.get('jobName', None);
        jobsList = [];
        if not organizationAccount:
            organizationList = self.getAllOrganization();

            for item in organizationList:
                if not jobName:
                    #遍历改学校的所有组织, 没有jobname，只有组织，则获取所有
                    jobsList += self.jobManager.getSearchData(account=item['account']);
                else:
                    jobsList += self.jobManager.getSearchData(account=item['account'], name=jobName);
        else:
            #有组织，则直接搜索
            jobsList = self.jobManager.getSearchData(account=organizationAccount, name=jobName); #搜索jobname

        newJobsList = [];
        if jobsList:
            for item in jobsList:  #添加url
                item['url'] = settings.BASE_URL+'jobInfo/'+item['jobId'];
                newJobsList.append(item);
        return newJobsList;

    #ajax搜索
    def searchInfoRequest(self):
        #获取职位
        jobsList = self.getJobsByKeyword();
        if jobsList:
            return ResponsesSingleton.getInstance().responseJsonArray('success', '获取成功', jobsList);
        else:
            return ResponsesSingleton.getInstance().responseJsonArray('success', '获取失败', jobsList);

    def pageMain(self):
        #取出数据库d大banner推广的组织
        organizationList = self.getAllOrganization();
        organizationAccount = self.request.GET.get('organizationAccount', None);
        #获取职位
        jobsList = self.getJobsByKeyword();
        data = {
            'data' : {'popLoginModal' : False,
                      'popAddressModal' : False,
                      'popRegisterModal' : False,
                      'organizationList' : organizationList,
                      'jobsList' : jobsList,
                      'currentOrganization' :  organizationAccount
                      },
        }
        return render_to_response('main/main.html', context_instance=RequestContext(self.request, data));

    #返回职位详情
    def pageInfo(self, jobId):
        data = self.jobInfoManager.getData(jobId=jobId);
        if data:
            data = data[0];
            return render_to_response('main/jobInfo.html', context_instance=RequestContext(self.request, data));
        else:
            return HttpResponseRedirect('/noresult');


import cv2 as cv
import numpy as np

def canny(image):
    """canny边缘提取"""
    blurred = cv.GaussianBlur(image, (3, 3), 0)
    gray = cv.cvtColor(blurred, cv.COLOR_BGR2GRAY)

    grad_x = cv.Sobel(gray, cv.CV_16SC1, 1, 0)
    grad_y = cv.Sobel(gray, cv.CV_16SC1, 0, 1)

    # image：要检测的图像,threshold1：阈值1（最小值）,threshold2：阈值2（最大值），使用此参数进行明显的边缘检测,
    # canny_output2 = cv.Canny(grad_x, grad_y, 30, 150)
    canny_output1 = cv.Canny(gray, 50, 150)  # 也可以直接传入gray

    return canny_output1

def contours(image):
    """轮廓查找"""
    binary = canny(image)
    contours, hierarchy = cv.findContours(binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)

    for i, contour in enumerate(contours):
        # 函数 cv2.drawContours() 可以被用来绘制轮廓。它可以根据你提供的边界点绘制任何形状。
        # 它的第一个参数是原始图像，第二个参数是轮廓，一个 Python 列表。
        # 第三个参数是轮廓的索引（在绘制独立轮廓是很有用，当设 置为 -1 时绘制所有轮廓）。
        # 接下来的参数是轮廓的颜色和厚度等。
        cv.drawContours(image, contours, i, (0, 0, 255), 1)  # 2为像素大小，-1时填充轮廓
        print(i)
    cv.imshow("detect contours", image)

def image_contour(image):
    """轮廓查找并描点"""
    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU)  # 图像二值化
    print("threshold value: %s" % ret)  # 输出阈值
    # cv.imshow("binary image", binary)

    contours, hierarchy = cv.findContours(binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
    for i, contour in enumerate(contours):
        cv.drawContours(image, contours, i, (0, 0, 255), 2)  # 用红色线条画出轮廓

        epsilon = 0.01 * cv.arcLength(contour, True)
        approx = cv.approxPolyDP(contour, epsilon, True)
        cv.drawContours(image, approx, -1, (255, 0, 0), 10)
    cv.imshow("contour_approx", image)

def image_rectangle(image):

    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
    # gray = cv.Canny(gray, 50, 150)
    # cv.imshow('test', gray)
    # cv.waitKey(0)
    ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU)  # 图像二值化
    print("threshold value: %s" % ret)  # 输出阈值
    # cv.imshow("binary image", binary)

    contours, hierarchy = cv.findContours(binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
    for i, contour in enumerate(contours):
        # print(cv.contourArea(contour))
        if cv.contourArea(contour) < 25 : continue
        x, y, w, h = cv.boundingRect(contour)
        cv.rectangle(image, (x,y), (x+w, y+w), (255, 0, 0), 1)
    cv.imshow("contour_jx", image)

if __name__ == "__main__":
    img2 = cv.imread('1080/zhulou.png')
    cv.imshow("img2", img2)
    gray = cv.cvtColor(img2, cv.COLOR_BGR2GRAY)
    ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU)

    kernel = np.ones((1, 1), np.uint8)
    img_t = cv.erode(binary, kernel, iterations=1)

    cv.imshow("img_t",img_t)
    cv.imshow("binary",binary)
    cv.waitKey(0)
    image_rectangle(img2[620:720])
    cv.waitKey(0)


"""
Вам дана область карты https://stepik.org/media/attachments/lesson/266078/mapcity.osm

Пройдите по первым ста node в этой области и выведите для каждого три числа:
id, широту (атрибут lat) и долготу (атрибут lon).

Числа для каждого node выводите в отдельной строке, разделяя одним пробелом.
Обрабатывать node нужно в том же порядке, в котором они встречаются во входном файле.

Если вы все делаете правильно, то ваш ответ должен начинаться со строк:

60276311 55.5695795 37.5764692
60276312 55.5682232 37.5754573
"""
from urllib.request import urlopen, urlretrieve
from bs4 import BeautifulSoup

resp = urlopen('https://stepik.org/media/attachments/lesson/266078/mapcity.osm')
xml = resp.read().decode('utf8')
soup = BeautifulSoup(xml, 'lxml')

for node in soup.find_all('node', limit=100):
    print(node['id'], node['lat'], node['lon'])


from flask import Blueprint, request
from app.models import Tag, db, Note
from app.forms.tag_form import TagForm
from flask_login import login_required

tag_routes = Blueprint('tag', __name__)

@tag_routes.route('/', methods=['POST'])
@login_required
def add_tag_route():
    form = TagForm()
    form['csrf_token'].data = request.cookies['csrf_token']

    if form.validate_on_submit():
        new_tag = Tag()
        form.populate_obj(new_tag)

        db.session.add(new_tag)
        db.session.commit()
        return new_tag.to_dict()
    else:
        return form.errors

@tag_routes.route('/<int:tag_id>', methods=['GET', 'PUT', 'DELETE'])
@login_required
def tag_by_id(tag_id):
    tag = Tag.query.get(tag_id)

    if tag:
        if request.method == 'GET':
            return tag.to_dict()

        if request.method == 'PUT':
            form = TagForm()
            form['csrf_token'].data = request.cookies['csrf_token']
            if form.validate_on_submit():
                tag.name = form.data['name']
                db.session.commit()
                return tag.to_dict()
            else:
                return form.errors

        if request.method == 'DELETE':
            db.session.delete(tag)
            db.session.commit()
            return {'message': 'Successfully Deleted'}

    else:
        return { "error": "Tag not found", "errorCode" : 404 }, 404

@tag_routes.route('/<int:tag_id>/alter-notes/<int:note_id>', methods=['POST', 'DELETE'])
@login_required
def alter_tag_by_note_id(tag_id, note_id):
    tag = Tag.query.get(tag_id)

    if tag:
        if request.method == 'POST':
            note = Note.query.get(note_id)
            tag.notes.append(note)
            db.session.commit()
            return { "message": "successfully appended note" }, 201

        if request.method == 'DELETE':
            note = Note.query.get(note_id)
            tag.notes.remove(note)
            db.session.commit()
            return { 'message': 'successfully removed note'}

    else:
        return { "error": "Note not found", "errorCode" : 404 }, 404


from torch import nn
import numpy as np
import torch
import torch.nn.functional as F

def norm_grad(input, max_norm):
    if input.requires_grad:
        def norm_hook(grad):
            N = grad.size(0) # batch number
            norm = grad.contiguous().view(N, -1).norm(p=2, dim=1) + 1e-6
            scale = (norm / max_norm).clamp(min=1).view([N]+[1]*(grad.dim()-1))
            return grad / scale

            # clip_coef = float(max_norm) / (grad.norm(2).data[0] + 1e-6)
            # return grad.mul(clip_coef) if clip_coef < 1 else grad
        input.register_hook(norm_hook)

class GaussianBlurConv(nn.Module):
    def __init__(self, channels=3):
        super(GaussianBlurConv, self).__init__()
        self.channels = channels
        kernel = [[0.00078633, 0.00655965, 0.01330373, 0.00655965, 0.00078633],
                  [0.00655965, 0.05472157, 0.11098164, 0.05472157, 0.00655965],
                  [0.01330373, 0.11098164, 0.22508352, 0.11098164, 0.01330373],
                  [0.00655965, 0.05472157, 0.11098164, 0.05472157, 0.00655965],
                  [0.00078633, 0.00655965, 0.01330373, 0.00655965, 0.00078633]]
        kernel = torch.tensor(kernel, dtype=torch.float).unsqueeze(0).unsqueeze(0)
        kernel = kernel.expand((channels, 1, 5, 5))
        self.weight = torch.tensor(data=kernel, requires_grad=False).cuda()

    def forward(self, x):
        y = F.conv2d(x, self.weight, padding=2, groups=self.channels)
        return y

class IdentityFun(torch.autograd.Function):

    @staticmethod
    def forward(ctx, input):
        return input

    @staticmethod
    def backward(ctx, grad_output):
        return grad_output

class Identity(nn.Module):

    def __init__(self):
        super(Identity, self).__init__()

    def forward(self, input):
        return IdentityFun.apply(input)

def func(func_name):
    if func_name is None:
        return None
    elif func_name == 'tanh':
        return nn.Tanh()
    elif func_name == 'relu':
        return nn.ReLU()
    elif func_name == 'sigmoid':
        return nn.Sigmoid()
    elif func_name == 'softmax':
        return nn.Softmax(dim=1)
    else:
        assert False, 'Invalid func_name.'

class FCN(nn.Module):

    def __init__(self, features, hid_trans='tanh', out_trans=None, hid_bn=0, out_bn=0):
        super(FCN, self).__init__()
        self.layer_num = len(features) - 1
        assert self.layer_num > 0, 'Invalid fc parameters'
        self.hid_bn = hid_bn
        self.out_bn = out_bn
        # Linear layers 快速构建多层FC
        for i in range(0, self.layer_num):
            setattr(self, 'fc'+str(i), nn.Linear(features[i], features[i+1]))
            if hid_bn == 1:
                setattr(self, 'hid_bn_func'+str(i), nn.BatchNorm1d(features[i+1]))
        if out_bn == 1:
            self.out_bn_func = nn.BatchNorm1d(features[-1])
        # Transformations
        self.hid_trans_func = func(hid_trans)
        self.out_trans_func = func(out_trans)

    def forward(self, X):
        H = X
        # Hidden layers
        for i in range(0, self.layer_num):
            H = getattr(self, 'fc'+str(i))(H)
            if i < self.layer_num - 1:
                if self.hid_bn == 1:
                    H = getattr(self, 'hid_bn_func'+str(i))(H)
                H = self.hid_trans_func(H)
        # Output layer
        if self.out_bn == 1:
            H = self.out_bn_func(H)
        if self.out_trans_func is not None:
            H = self.out_trans_func(H)
        return H


from django.shortcuts import render,HttpResponse,redirect
from django.utils.safestring import mark_safe
from django.core.paginator import Paginator

from app01.models import Person
# Create your views here.

def index(request):
    # return HttpResponse("Welcome to mysite")
    return render(request,'index.html')
def test(request):
    # data=['hello','world']
    # return render(request,'test.html',{'data':data})

    data={'k1':'hello','k2':'world'}
    return render(request,'test.html',{'data':data})
def ormtest(request):
    '''
    :param request:
    :return:

    新增用户
    Person.objects.create(name='scott',age=10)
    Person.objects.create(name='jack', age=20)
    <QuerySet [<Person: Person object (1)>, <Person: Person object (2)>, <Person: Person object (3)>, <Person: Person object (4)>]>
    '''
    data=Person.objects.all()
    return render(request, 'test.html', {'data': data})
def list_user(request):

    if request.method == 'GET':
        # 获取所有数据
        objects = Person.objects.all()
        # 每页10条数据
        paginator = Paginator(objects, 5)
        # 获取第一页的数据
        page_number = request.GET.get('page')
        page_obj = paginator.get_page(page_number)
        return render(request, 'list_user.html', {'page_obj': page_obj})

    print(request.POST)
    data=request.POST.get('jump-page')
    print(data)
    print('*******************')
    # 获取所有数据
    objects = Person.objects.all()
    # 每页10条数据
    paginator = Paginator(objects, 5)
    # 获取第一页的数据
    page_number = request.GET.get('page')
    page_obj = paginator.get_page(page_number)

    return render(request, 'list_user.html', {'page_obj': page_obj})

    # return render(request, 'list_user.html', {'data': data,'page_string':page_string,'total_page':total_page})

#####添加用户，这里基于modelform来处理
# Django ModelForm修改默认的控件属性
# https://blog.csdn.net/weixin_30254435/article/details/99050157
from django.forms import ModelForm
class Add_user_modelfrom(ModelForm):
    class Meta:
        model=Person
        # fields = ['name', 'age', ]
        fields='__all__'

#      <input type="email" class="form-control" id="exampleInputEmail1" placeholder="Email">
    def __init__(self, *args, **kwargs):
        super(Add_user_modelfrom, self).__init__(*args, **kwargs)
        for field_name in self.base_fields:
            field = self.base_fields[field_name]
            field.widget.attrs.update({'class': "form-control", 'id': 'exampleInput'})

def add_user(request):
    print(request.method)
    if request.method == 'GET':
        form=Add_user_modelfrom()
        return render(request,'add_user.html',{'form':form})
    form=Add_user_modelfrom(request.POST)
    form.save()
    return redirect('/list/user/')

def delete_user(request):
    '''
    :param request:
    :return:
     借助 ?id= 获取 key来删除
     https://blog.csdn.net/MaxineZhou/article/details/104971307
    '''
    id=request.GET.get('id')
    data=Person.objects.get(id=id).delete()
    print(data)
    return redirect('/list/user')

from django.forms import ModelForm
class Modify_user_modelfrom(ModelForm):
    class Meta:
        model=Person
        # fields = ['name', 'age', ]
        fields='__all__'

#      <input type="email" class="form-control" id="exampleInputEmail1" placeholder="Email">
    def __init__(self, *args, **kwargs):
        super(Modify_user_modelfrom, self).__init__(*args, **kwargs)
        for field_name in self.base_fields:
            field = self.base_fields[field_name]
            field.widget.attrs.update({'class': "form-control", 'id': 'exampleInput'})

def modify_user(request,uid):
    '''
    :param request:
    :return:
    '''
    data = Person.objects.filter(id=uid).first()
    print(data)
    if request.method == 'GET':
        form=Modify_user_modelfrom(instance=data)
        return render(request,'modify.html',{'form':form})

    else:
        form= Modify_user_modelfrom(request.POST,instance=data)
        if form.is_valid():
            form.save()
            return redirect('/list/user')


import connexion
from connexion import NoContent
from datetime import datetime
import logging.config
import yaml
import datetime
import json
from pykafka import KafkaClient
from pykafka.common import OffsetType
from threading import Thread
from flask_cors import CORS, cross_origin
import os

if "TARGET_ENV" in os.environ and os.environ["TARGET_ENV"] == "test":
    print("In Test Environment")
    app_conf_file = "/config/app_conf.yml"
    log_conf_file = "/config/log_conf.yml"
else:
    print("In Dev Environment")
    app_conf_file = "app_conf.yml"
    log_conf_file = "log_conf.yml"

with open(app_conf_file, 'r') as f:
    app_config = yaml.safe_load(f.read())

with open(log_conf_file, 'r') as f:
    log_config = yaml.safe_load(f.read())
    logging.config.dictConfig(log_config)

logger = logging.getLogger('basicLogger')

logger.info("App Conf File: %s" % app_conf_file)
logger.info("Log Conf File: %s" % log_conf_file)

hostname = app_config['events']['hostname']

def get_bullet_efficiency_stats(index):
    hostname = "%s:%d" % (app_config["events"]["hostname"],
                          app_config["events"]["port"])
    client = KafkaClient(hosts=hostname)
    topic = client.topics[str.encode(app_config['events']['topic'])]

    consumer = topic.get_simple_consumer(reset_offset_on_start=True,
                                         consumer_timeout_ms=1000)

    logger.info("Retrieving bullet efficiency at index %d" % index)

    message_count = 0

    try:
        for msg in consumer:
            msg_str = msg.value.decode('utf-8')
            msg = json.loads(msg_str)

            if msg['type'] == 'bullet_efficiency':
                if message_count == index:
                    return { "message": msg }, 200
                else:
                    message_count += 1

    except:
        logger.error("No more messages found")

    logger.error("Could not find bullet efficiency at index %d" % index)
    return { "message": "Not found"}, 404

def get_ability_efficiency_stats(index):
    hostname = "%s:%d" % (app_config["events"]["hostname"],
                          app_config["events"]["port"])
    client = KafkaClient(hosts=hostname)
    topic = client.topics[str.encode(app_config['events']['topic'])]

    consumer = topic.get_simple_consumer(reset_offset_on_start=True,
                                         consumer_timeout_ms=1000)

    logger.info("Retrieving ability efficiency at index %d" % index)

    message_count = 0

    try:
        for msg in consumer:
            msg_str = msg.value.decode('utf-8')
            msg = json.loads(msg_str)
            if msg['type'] == 'ability_efficiency':
                if message_count == index:
                    return { "message": msg }, 200
                else:
                    message_count += 1

    except:
        logger.error("No more messages found")

    logger.error("Could not find ability efficiency at index %d" % index)
    return { "message": "Not found"}, 404

def health_check():
    return 200

app = connexion.FlaskApp(__name__, specification_dir='')
if "TARGET_ENV" not in os.environ or os.environ["TARGET_ENV"] != "test":
    CORS(app.app)
    app.app.config['CORS_HEADERS'] = 'Content-Type'
app.add_api("openapi.yaml", base_path="/audit_log", strict_validation=True,
validate_responses=True)

if __name__ == "__main__":
    app.run(port=8110, use_reloader=False)


# References: https://mccormickml.com/2019/07/22/BERT-fine-tuning/
# for Bert in Italian language: http://ceur-ws.org/Vol-2481/paper57.pdf

import os

import transformers
import json
import common.bert.models.bert_for_sentence_classification as bert_for_sentence_classification
import common.bert.models.bert_for_next_sentence_prediction as bert_for_next_sentence_prediction
import common.bert.constants as constants
import common.bert.models_utilities as models_utilities

from services import Service, MissingModel, MissingParameter, MissingResource

class SentenceClassification(Service):

    def __init__(self, model_dir=os.path.join('models', 'bert', 'classification')):

        Service.__init__(self, 'classification', 'bert', ['parse'])
        self.models = {}
        self.results = {}
        self.id2label = {}
        langs = set()
        for name in os.listdir(model_dir):
            if not os.path.isdir(os.path.join(model_dir, name)):
                continue

            with open(os.path.join(model_dir, name, transformers.CONFIG_NAME), 'r') as f:
                configs = json.load(f)

            num_labels = configs['_num_labels']
            language = configs[constants.MODEL_INFO][constants.LANGUAGE]
            langs.add(language)

            pretrained_model_name_or_path = os.path.join(model_dir, name)
            self.models[name] = bert_for_sentence_classification.BertForSentenceClassification(language, num_labels,
                                                                                pretrained_model_name_or_path)
            self.results[name] = configs[constants.MODEL_INFO]
            self.id2label[name] = models_utilities.load_labels(pretrained_model_name_or_path)

        self.langs = list(langs)

    def _predict_sentence(self, model, sentence, text, id2label, debug):

        start = sentence[0]['start']
        end = sentence[-1]['end']

        predicted_label, probabilities = model.predict_example(text[start:end])

        item = {
            'start': start,
            'end': end,
            'label': predicted_label if id2label is None else id2label[str(predicted_label)]
        }
        if debug:
            item['text'] = text[start:end]
            item['probabilities'] = probabilities

        return item

    def run(self, request, response):
        if 'bert-model-classification' in request:
            model_name = request['bert-model-classification']
        else:
            raise MissingParameter(self.task, self.name, 'bert-model-classification')

        if model_name in self.models:
            model = self.models[model_name]
            id2label = self.id2label[model_name]
        else:
            raise MissingModel(self.task, self.name, model_name, list(self.models))

        lang = request['lang']
        if lang not in self.langs:
            raise MissingResource(self.task, self.name, f'{lang}.txt')

        text = request['text']
        parsing = response['parse']
        debug = request.get('debug', False)
        result = []
        for sentence in parsing:
            prediction = self._predict_sentence(model, sentence, text, id2label, debug)
            result.append(prediction)

        return result

    def describe(self):
        result = super().describe()
        result['langs'] = self.langs
        result['extra-params'] = [{
            'name': 'bert-model-classification',
            'type': 'string',
            'choices': list(self.models.keys()),
            'required': True
        }]
        result['models'] = self.results
        return result

class NextSentencePrediction(Service):

    def __init__(self, model_dir=os.path.join('models', 'bert', 'next_sentence_prediction')):

        Service.__init__(self, 'next-sentence-prediction', 'bert', ['parse'])
        self.models = {}
        self.results = {}
        langs = set()
        for name in os.listdir(model_dir):
            if not os.path.isdir(os.path.join(model_dir, name)):
                continue

            with open(os.path.join(model_dir, name, transformers.CONFIG_NAME), 'r') as f:
                configs = json.load(f)

            language = configs[constants.MODEL_INFO][constants.LANGUAGE]
            langs.add(language)

            pretrained_model_name_or_path = os.path.join(model_dir, name)
            self.models[name] = bert_for_next_sentence_prediction.BertForNextSentencePrediction(language, pretrained_model_name_or_path)
            self.results[name] = configs[constants.MODEL_INFO]

        self.langs = list(langs)

    def _predict_sentences_pair(self, model, sentence1, sentence2, text, debug):

        start1 = sentence1[0]['start']
        end1 = sentence1[-1]['end']

        start2 = sentence2[0]['start']
        end2 = sentence2[-1]['end']

        predicted_label, probabilities = model.predict_example(text[start1:end1], text[start2:end2])

        item = {
            'start1': start1,
            'end1': end1,
            'start2': start2,
            'end2': end2,
            'is_next': predicted_label
        }
        if debug:
            item['sentence1'] = text[start1:end1]
            item['sentence2'] = text[start2:end2]
            item['probabilities'] = probabilities

        return item

    def run(self, request, response):
        if 'bert-model-nsp' in request:
            model_name = request['bert-model-nsp']
        else:
            raise MissingParameter(self.task, self.name, 'bert-model-nsp')

        if model_name in self.models:
            model = self.models[model_name]
        else:
            raise MissingModel(self.task, self.name, model_name, list(self.models))

        lang = request['lang']
        if lang not in self.langs:
            raise MissingResource(self.task, self.name, f'{lang}.txt')

        text = request['text']
        parsing = response['parse']
        debug = request.get('debug', False)
        result = []
        if len(parsing) < 2:
            return result

        sentence1 = parsing[0]
        for sentence2 in parsing[1:]:
            prediction = self._predict_sentences_pair(model, sentence1, sentence2, text, debug)
            result.append(prediction)

            sentence1 = sentence2

        return result

    def describe(self):
        result = super().describe()
        result['langs'] = self.langs
        result['extra-params'] = [
            {
                'name': 'bert-model-nsp',
                'type': 'string',
                'choices': list(self.models.keys()),
                'required': True
            }]
        result['models'] = self.results
        return result


from configparser import ConfigParser
import sys
import random
import pickle
import os
from time import time
import pygmo as pg
import numpy as np
from tengp_eval.utils import get_keijzer_data
from tengp_eval.utils.function_sets import keijzer_fixed_funset

def run(algos, output_dir, configs_dir, default_ini=None):
    random.seed(42)
    pg.set_global_rng_seed(42)

    np.warnings.filterwarnings('ignore')

    if not os.path.exists(output_dir):
        os.makedirs(output_dir)

    start = time()

    for bench_id in  range(1,16):

        x_train, y_train, x_test, y_test = get_keijzer_data(random, bench_id)
        print(bench_id, x_test.shape)

        chosen_run = algos[0]

#        if x_test.shape[0] > 10000:
#            print('volim si coevoluciu')
#            chosen_run = algos[1]

        cp = ConfigParser()

        if default_ini:
            cp.read(os.path.join(configs_dir, default_ini))
        else:
            cp.read(os.path.join(configs_dir, f'hp-keijzer{bench_id}.ini'))

        max_trials = 50

        all_logs = []

        for i in range(max_trials):
            print(f'keijzer{bench_id}-{i}')
            log = chosen_run(cp, x_test, y_test, keijzer_fixed_funset)
            all_logs.append(log)

        with open(os.path.join(output_dir, f'keijzer{bench_id}.log'), 'wb') as f:
            pickle.dump(all_logs, f)

    print(f'finised, wall time: {time() - start}')

if __name__ == '__main__':

    output_dir = sys.argv[2]
    configs_dir = sys.argv[3]
    default_ini = None
    if len(sys.argv) == 5:
        default_ini = sys.argv[4]

    if sys.argv[1] == 'pso':
        from .pso import RUNNERS
    elif sys.argv[1] == 'sa':
        from .sa import RUNNERS

    algos = RUNNERS

    run(algos, output_dir, configs_dir, default_ini)


import pandas as pd
import nltk
import spacy
from nltk.stem import PorterStemmer

# Load NLTK and spaCy models
nltk.download('punkt')
nlp = spacy.load('en_core_web_sm')

# Load the CSV dataset
df = pd.read_csv('storeTransactions.csv')  # Replace with your dataset file path

# Function for stemming using NLTK
def stem_text(text):
    stemmer = PorterStemmer()
    words = nltk.word_tokenize(text)
    stemmed_words = [stemmer.stem(word) for word in words]
    return ' '.join(stemmed_words)

# Function for lemmatization using spaCy
def lemmatize_text(text):
    doc = nlp(text)
    lemmatized_words = [token.lemma_ for token in doc]
    return ' '.join(lemmatized_words)

# Apply stemming and lemmatization to all columns
for column in df.columns:
    if df[column].dtype == 'O':  # Check if the column contains text data
        df[f'{column}_stemmed'] = df[column].apply(stem_text)
        df[f'{column}_lemmatized'] = df[column].apply(lemmatize_text)

# Save the processed dataset to a new CSV file
df.to_csv('sample_superstore_processed.csv', index=False)  # Change the output file path as needed