import concurrent
import concurrent.futures
import contextlib
import itertools
import json
import logging
import time
from dataclasses import dataclass
from typing import Dict, List, Mapping, Optional, Tuple, Union

from paho.mqtt.client import MQTTMessage

from tavern._core import exceptions
from tavern._core.dict_util import check_keys_match_recursive
from tavern._core.loader import ANYTHING
from tavern._core.pytest.newhooks import call_hook
from tavern._core.report import attach_yaml
from tavern._core.strict_util import StrictSetting
from tavern.response import BaseResponse

from .client import MQTTClient

logger = logging.getLogger(__name__)

_default_timeout = 1

class MQTTResponse(BaseResponse):
    def __init__(self, client: MQTTClient, name, expected, test_block_config) -> None:
        super().__init__(name, expected, test_block_config)

        self._client = client

        self.received_messages = []  # type: ignore

    def __str__(self):
        if self.response:
            return self.response.payload
        else:
            return "<Not run yet>"

    def verify(self, response) -> dict:
        """Ensure mqtt message has arrived

        Args:
            response: not used except for debug printing
        """

        self.response = response

        try:
            return self._await_response()
        finally:
            self._client.unsubscribe_all()

    def _await_response(self) -> dict:
        """Actually wait for response

        Returns:
            dict: things to save to variables for the rest of this test
        """

        # Get into class with metadata attached
        expected = self.expected["mqtt_responses"]

        by_topic = {
            m: list(v) for m, v in itertools.groupby(expected, lambda x: x["topic"])
        }

        correct_messages: List["_ReturnedMessage"] = []
        warnings: List[str] = []

        with concurrent.futures.ThreadPoolExecutor() as executor:
            futures = []

            for topic, expected_for_topic in by_topic.items():
                logger.debug("Starting thread for messages on topic '%s'", topic)
                futures.append(
                    executor.submit(
                        self._await_messages_on_topic, topic, expected_for_topic
                    )
                )

            for future in concurrent.futures.as_completed(futures):
                # for future in futures:
                try:
                    messages, warnings = future.result()
                except Exception as e:
                    raise exceptions.ConcurrentError(
                        "unexpected error getting result from future"
                    ) from e
                else:
                    warnings.extend(warnings)
                    correct_messages.extend(messages)

        if self.errors:
            if warnings:
                self._adderr("\n".join(warnings))

            raise exceptions.TestFailError(
                "Test '{:s}' failed:\n{:s}".format(self.name, self._str_errors()),
                failures=self.errors,
            )

        saved = {}

        for msg in correct_messages:
            # Check saving things from the payload and from json
            saved.update(
                self.maybe_get_save_values_from_save_block(
                    "payload",
                    msg.msg.payload,
                    outer_save_block=msg.expected,
                )
            )
            saved.update(
                self.maybe_get_save_values_from_save_block(
                    "json",
                    msg.msg.payload,
                    outer_save_block=msg.expected,
                )
            )

            saved.update(self.maybe_get_save_values_from_ext(msg.msg, msg.expected))

        # Trying to save might have introduced errors, so check again
        if self.errors:
            raise exceptions.TestFailError(
                "Saving results from test '{:s}' failed:\n{:s}".format(
                    self.name, self._str_errors()
                ),
                failures=self.errors,
            )

        return saved

    def _await_messages_on_topic(
        self, topic: str, expected: List[Dict]
    ) -> Tuple[List["_ReturnedMessage"], List[str]]:
        """
        Waits for the specific message

        Args:
            topic: topic to listen on
            expected: expected response for this block

        Returns:
            tuple(msg, list): The correct message (if any) and warnings from processing the message
        """

        timeout = max(m.get("timeout", _default_timeout) for m in expected)

        # A list of verifiers that can be used to validate messages for this topic
        verifiers = [_MessageVerifier(self.test_block_config, v) for v in expected]

        correct_messages = []
        warnings = []

        time_spent = 0.0
        while (time_spent < timeout) and verifiers:
            t0 = time.time()

            msg = self._client.message_received(topic, timeout - time_spent)

            if not msg:
                # timed out
                break

            logger.debug("Seeing if message '%s' matched expected", msg)

            call_hook(
                self.test_block_config,
                "pytest_tavern_beta_after_every_response",
                expected=expected,
                response=msg,
            )

            self.received_messages.append(msg)

            with contextlib.suppress(AttributeError):
                msg.payload = msg.payload.decode("utf8")

            attach_yaml(
                {
                    "topic": msg.topic,
                    "payload": msg.payload,
                    "timestamp": msg.timestamp,
                },
                name="rest_response",
            )

            found: List[int] = []
            for i, v in enumerate(verifiers):
                if v.is_valid(msg):
                    correct_messages.append(_ReturnedMessage(v.expected, msg))
                    if found:
                        logger.warning(
                            "Message was matched by multiple mqtt_response blocks"
                        )
                    found.append(i)
                warnings.extend(v.popwarnings())
            verifiers = [v for (i, v) in enumerate(verifiers) if i not in found]

            time_spent += time.time() - t0

        if verifiers:
            for v in verifiers:
                self._adderr(
                    "Expected '%s' on topic '%s' but no such message received",
                    v.expected_payload,
                    topic,
                )

        for msg in correct_messages:
            if msg.expected.get("unexpected"):
                self._adderr(
                    "Got '%s' on topic '%s' marked as unexpected",
                    msg.expected["payload"],
                    topic,
                )

            self._maybe_run_validate_functions(msg)

        return correct_messages, warnings

@dataclass
class _ReturnedMessage:
    """An actual message returned from the API and it's matching 'expected' block."""

    expected: dict
    msg: MQTTMessage

class _MessageVerifier:
    def __init__(self, test_block_config, expected) -> None:
        self.expires = time.time() + expected.get("timeout", _default_timeout)

        self.expected = expected
        self.expected_payload, self.expect_json_payload = self._get_payload_vals(
            expected
        )

        test_strictness = test_block_config.strict
        self.block_strictness: StrictSetting = test_strictness.option_for("json")

        # Any warnings to do with the request
        # eg, if a message was received but it didn't match, message had payload, etc.
        self.warnings: List[str] = []

    def is_valid(self, msg: MQTTMessage) -> bool:
        if time.time() > self.expires:
            return False

        topic = self.expected["topic"]

        def addwarning(w, *args, **kwargs):
            logger.warning(w, *args, **kwargs)
            self.warnings.append(w % args)

        if self.expect_json_payload:
            try:
                msg.payload = json.loads(msg.payload)
            except json.decoder.JSONDecodeError:
                addwarning(
                    "Expected a json payload but got '%s'",
                    msg.payload,
                    exc_info=True,
                )
                return False

        if self.expected_payload is None:
            if msg.payload is None or msg.payload == "":
                logger.info("Got message with no payload (as expected) on '%s'", topic)
                return True
            else:
                addwarning(
                    "Message had payload '%s' but we expected no payload",
                    msg.payload,
                )
        elif self.expected_payload is ANYTHING:
            logger.info("Got message on %s matching !anything token", topic)
            return True
        elif msg.payload != self.expected_payload:
            if self.expect_json_payload:
                try:
                    check_keys_match_recursive(
                        self.expected_payload,
                        msg.payload,
                        [],
                        strict=self.block_strictness,
                    )
                except exceptions.KeyMismatchError:
                    # Just want to log the mismatch
                    pass
                else:
                    logger.info(
                        "Got expected message in '%s' with expected payload",
                        msg.topic,
                    )
                    logger.debug("Matched payload was '%s", msg.payload)
                    return True

            addwarning(
                "Got unexpected payload on topic '%s': '%s' (expected '%s')",
                msg.topic,
                msg.payload,
                self.expected_payload,
            )
        else:
            logger.info(
                "Got expected message in '%s' with expected payload",
                msg.topic,
            )
            logger.debug("Matched payload was '%s", msg.payload)
            return True

        return False

    @staticmethod
    def _get_payload_vals(expected: Mapping) -> Tuple[Optional[Union[str, dict]], bool]:
        """Gets the payload from the 'expected' block

        Returns:
            tuple: First element is the expected payload, second element is whether it's
                expected to be json or not
        """
        # TODO move this check to initialisation/schema checking
        if "json" in expected:
            if "payload" in expected:
                raise exceptions.BadSchemaError(
                    "Can only specify one of 'payload' or 'json' in MQTT response"
                )

            payload = expected["json"]
            json_payload = True

            if payload.pop("$ext", None):
                raise exceptions.MisplacedExtBlockException(
                    "json",
                )
        elif "payload" in expected:
            payload = expected["payload"]
            json_payload = False
        else:
            payload = None
            json_payload = False

        return payload, json_payload

    def popwarnings(self) -> List[str]:
        popped = []
        while self.warnings:
            popped.append(self.warnings.pop(0))
        return popped


import unittest
import re
import sys

class Feature:
    def __init__(self):
        self.name = 'target'
        self.type = ''
        self.values = []

    def dump(self, out_stream=sys.stdout):
        print(self.name + ': ', end='', file=out_stream)
        if self.type == 'categorical':
            print(', '.join(self.values), end='', file=out_stream)
            print('.', file=out_stream)
        else:
            print(self.type + '.', file=out_stream)

class Names:
    def __init__(self):
        self.feature = {}
        self.feature_list = []
        self.target_feature = 'target'

    def size(self):
        return len(self.feature_list)

    def target_index(self):
        return self.feature_list.index(self.target_feature)

    def dump(self, out_stream=sys.stdout):
        print(self.target_feature + '. | the target attribute', file=out_stream)
        for feature_name in self.feature_list:
            self.feature[feature_name].dump(out_stream)

    @staticmethod
    def process_line(line):
        empty = True
        feature = Feature()
        line = re.sub(r"\n", "", line)
        line = re.sub(r"[ ]*\|.*", "", line)
        line = re.sub(r"[\. ]*$", "", line)
        line = re.sub(r"^[ ]*", "", line)
        if line == '':
            return empty, feature
        empty = False
        data = re.split(":", line, 1)
        data[0] = re.sub("[ ]*$", "", data[0])
        if re.search(",", data[0]):
            data.append(data[0])
        else:
            feature.name = data[0]
            if len(data) < 2:
                return empty, feature
        data[1] = re.sub("^[ ]*", "", data[1])
        if data[1] == '':
            return empty, feature
        if data[1] in ['continuous', 'ignore', 'label']:
            feature.type = data[1]
            return empty, feature
        feature.type = 'categorical'
        for value in re.split(",", data[1]):
            value = re.sub("[ ]*$", "", value)
            value = re.sub("^[ ]*", "", value)
            feature.values.append(value)
        return empty, feature

    def from_file(self, file):
        fp = open(file, 'r')
        empty, target_feature = Names.process_line(fp.readline())
        while empty:
            empty, target_feature = Names.process_line(fp.readline())
            self.target_feature = target_feature.name
        line = fp.readline()
        while line:
            empty, feature = Names.process_line(line)
            if not empty:
                self.feature[feature.name] = feature
                self.feature_list.append(feature.name)
            line = fp.readline()
        fp.close()
        if self.target_feature not in self.feature_list:
            self.feature[self.target_feature] = target_feature
            self.feature_list.append(self.target_feature)
        return self

class TestNames(unittest.TestCase):

    def test_feature(self):
        f = Feature()
        f.name = 'testName'
        self.assertEqual(f.name, 'testName')

    def test_names_basic(self):
        N = Names()
        self.assertTrue(N.target_feature == 'target')
        self.assertFalse(N.size() < 0)

    def test_names_real_file(self):
        N = Names().from_file('adult.names')
        self.assertEqual(N.size(), 15)
        out_stream = open('adult1.names', 'w')
        N.dump(out_stream)
        out_stream.close()
        self.assertFalse(0 > 0)

if __name__ == "__main__":
    unittest.main()


from django.urls import path

from . import views
from rest_framework.authtoken import views as rest_framework_views

urlpatterns = [
    #list all luggages
    path('lugagges/', views.LuggageTypeList.as_view()),

    #edit update existing luggage
    path('lugagges/<int:pk>/', views.LuggageTypeDetail.as_view()),
    #create new lugagge item, only is staff users can perform action
    path('lugagges/new-item/', views.LuggageTypeCreate.as_view()),

    #see all bookings
    path('booking/', views.BookingTypeList.as_view()),
    # path('booking/<int:pk>/', views.BookingTypeDetail.as_view()),
    #new booking all users can do booking
    path('booking/new-booking/', views.BookingTypeCreate.as_view()),

    # user signup
    path("signup/",views.CreateUserProfile.as_view(), name="signup"),
    # retrieves profile details of user
    path("profiles/<int:pk>",views.userProfileDetailView.as_view(), name="profile"),
    #see all user profiles
    path("profiles/",views.ProfilesTypeList.as_view(), name="profiles"),

]


from enum import Enum
from pathlib import Path
import struct

def u(f, format):
    bytes = f.read(struct.calcsize(format))
    return struct.unpack(format, bytes)

class ChunkType(Enum):
    VERTEX = 0
    EDGE = 1
    FACE_EDGE = 2
    PLANE = 3
    TEXINFO = 4
    LIGHTMAP = 5
    FACE = 6
    OBJECT = 12

    LIGHTMAP_DATA = 26
    NUM = 27

def load(f, dump_geometry_to=None, verbose=False):
    magic = f.read(8)
    assert magic == b"S3D\0\x33\x33\xF3\x40"

    unk_0 = u(f, "<ffffff")
    print("HEADER", unk_0)

    chunks = []
    for i in range(ChunkType.NUM.value):
        start, length = u(f, "<II")
        try:
            t = ChunkType(i)
            print(t, f"{start:06X}h {length:06X}h")

            if t == ChunkType.VERTEX: print(f"\t{length // 12} vertices")
            if t == ChunkType.EDGE: print(f"\t{length // 4} edges")
            if t == ChunkType.FACE_EDGE: print(f"\t{length // 4} face edge indices")
            if t == ChunkType.PLANE: print(f"\t{length // 20} planes")
            if t == ChunkType.TEXINFO: print(f"\t{length // 48} texinfo entries")
            if t == ChunkType.LIGHTMAP: print(f"\t{length // 24} lightmap entries")
        except ValueError:
            print(f"BLOCK_{i:02X}h {start:06X}h {length:06X}h")

        chunks.append((start, length))

    assert f.tell() == chunks[0][0]

    if dump_geometry_to is not None:
        f.seek(chunks[ChunkType.VERTEX.value][0])

        for i in range(chunks[ChunkType.VERTEX.value][1] // 12):
            vtx = u(f, "<fff")
            dump_geometry_to.write(f"v {vtx[0]} {vtx[1]} {vtx[2]}\n")

        f.seek(chunks[ChunkType.EDGE.value][0])

        linesegs = []
        for i in range(chunks[ChunkType.EDGE.value][1] // 4):
            a, b = u(f, "<HH")
            linesegs.append((a, b))

        f.seek(chunks[ChunkType.FACE_EDGE.value][0])

        while f.tell() < chunks[ChunkType.FACE_EDGE.value][0] + chunks[ChunkType.FACE_EDGE.value][1]:
            start = None
            indexes = []

            while True:
                idx, = u(f, "<i")

                a, b = linesegs[abs(idx)]

                if idx < 0:
                    a, b = b, a

                if start is None:
                    start = a
                else:
                    pass

                indexes.append(a)

                if b == start:
                    break

            verts = " ".join([str(1 + x) for x in indexes])
            dump_geometry_to.write(f"f {verts}\n")

if __name__ == "__main__":
    import argparse
    parser = argparse.ArgumentParser(description="Parse SBP format")
    parser.add_argument("input", type=Path)
    args = parser.parse_args()

    with open(args.input, "rb") as f:
        # For now, dump scene geometry
        dump_path = Path(args.input.name).with_suffix(".obj")
        with open(dump_path, "wt") as fobj:
            load(f, dump_geometry_to=fobj, verbose=True)
        print("Written", dump_path)


# Here import your model
from sklearn.svm import SVR

from sklearn.model_selection import validation_curve
from sklearn.utils import resample
from sklearn import preprocessing
import pandas as pd
import numpy as np
from helpers import root_mean_squared_log_error, rmsle_scorer

print("load")
dataset = pd.read_csv('data/train_merged.csv')

# Extract the objective values
y = dataset['trip_duration_in_minutes'].values
# Delete irrelevant columns in training set
del dataset['trip_duration'], dataset["id"], dataset["trip_duration_in_minutes"]
X = dataset.values

means = np.nanmean(X, axis=0)
nan_locations = np.where(np.isnan(X))
X[nan_locations] = np.take(means, nan_locations[1])

print("preprocess")
# Normalize X
X = preprocessing.scale(X)

X_train, y_train = resample(X, y, n_samples=5000)

print("fit")

regressor = SVR(verbose=10)

gamma_range =  np.logspace(-5, 2, 10)
train_scores, valid_scores = validation_curve(regressor, X_train, y_train, "gamma", gamma_range, n_jobs=-1, scoring=rmsle_scorer)
valid_scores = [np.mean(s) for s in valid_scores]

# Take the alpha giving the highest validation score, and test it on test set
best_gamma = gamma_range[np.nanargmax(valid_scores)]
print("best gamma:", best_gamma)
regressor.set_params(gamma=best_gamma)

X_train, y_train = resample(X, y, n_samples=20000)
regressor.fit(X_train, y_train)

print("test")
# Since we can't load the whole dataset, do batch testing
batch_size = 5000
X_test, y_test = resample(X, y, n_samples=100000)
y_pred = np.ndarray((0,))
for i in range(0, X_test.shape[0], batch_size):
    print(i)
    y_pred = np.hstack((y_pred, regressor.predict(X_test[i: i + batch_size])))
print("RMSLE =", root_mean_squared_log_error(y_test, y_pred))


# pytesseract is for OCR from images
# Check PyPi Project - https://pypi.org/project/pytesseract/
import pytesseract

# OpenCV for pre-processing images before feeding to the tesseract OCR.
# Check PyPi Project - https://pypi.org/project/opencv-python/
import cv2

import numpy as np

from yolo_object import detect_license_plate

import numpy as np
from skimage import io
from skimage.transform import rotate
from skimage.color import rgb2gray
from deskew import determine_skew

# Including the path to the tesseract.exe for PyTesseract to work
pytesseract.pytesseract.tesseract_cmd = 'Tesseract-OCR/tesseract.exe'
# PyTesseract Config Options
# language = English
# psm or Page segmentation modes = 7 represents treat the image as a single line text
# oem or Engine Mode - Nueral Nets LSTM engine only
config = ('-l eng --oem 1 --psm 7')

def crop_image_only_outside(img,tol=0):
    # img is 2D image data
    # tol  is tolerance
    mask = img>tol
    m,n = img.shape
    mask0,mask1 = mask.any(0),mask.any(1)
    col_start,col_end = mask0.argmax(),n-mask0[::-1].argmax()
    row_start,row_end = mask1.argmax(),m-mask1[::-1].argmax()
    return img[row_start:row_end,col_start:col_end]

def secondCrop(img):
    gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    ret,thresh = cv2.threshold(gray,127,255,0)
    contours,_ = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
    areas = [cv2.contourArea(c) for c in contours]
    if(len(areas)!=0):
        max_index = np.argmax(areas)
        cnt=contours[max_index]
        x,y,w,h = cv2.boundingRect(cnt)
        bounds = cv2.boundingRect(cnt)
        cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
        secondCrop = img[y:y+h,x:x+w]
    else:
        secondCrop = img
    return secondCrop

def read_text(img):
    # img = cv2.imread('images/img7.jpg')
    img = detect_license_plate(img)
    second_img = secondCrop(img)
    img = cv2.resize(second_img, None, fx=7, fy=7, interpolation = cv2.INTER_AREA)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    blur = cv2.GaussianBlur(img,(5,5),0)
    r, threshold = cv2.threshold(blur, 150, 255, cv2.THRESH_BINARY)
    cv2.imwrite('test.png', threshold)
    image = io.imread('test.png')
    grayscale = rgb2gray(image)
    angle = determine_skew(grayscale)
    rotated = rotate(image, angle, resize=True) * 255
    text = pytesseract.image_to_string(rotated, config=config)
    print(text)
    return text
    # cv2.imshow("Image", rotated)
    # cv2.waitKey(0)
    # text = read_text(img)

if __name__ == '__main__':
    read_text()


"""
587 Erect the Fence

There are some trees, where each tree is represented by (x,y) coordinate in a two-dimensional garden.
Your job is to fence the entire garden using the minimum length of rope as it is expensive.
The garden is well fenced only if all the trees are enclosed. Your task is to help find the coordinates of trees which are exactly located on the fence perimeter.

Example 1:
Input: [[1,1],[2,2],[2,0],[2,4],[3,3],[4,2]]
Output: [[1,1],[2,0],[4,2],[3,3],[2,4]]
Explanation:

Example 2:
Input: [[1,2],[2,2],[4,2]]
Output: [[1,2],[2,2],[4,2]]
Explanation:

Even you only have trees in a line, you need to use rope to enclose them.
Note:

All trees should be enclosed together. You cannot cut the rope to enclose trees that will separate them in more than one group.
All input integers will range from 0 to 100.
The garden has at least one tree.
All coordinates are distinct.
Input points have NO order. No order required for output.
"""

class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def __repr__(self):
        return '['+str(self.x)+','+str(self.y)+']'

from functools import reduce
class Solution:
    # help from https://www.cnblogs.com/weedboy/p/6896478.html
    def outerTrees(self, points):
        """
        :type points: List[Point]
        :rtype: List[Point]
        """
        def sign(p, q, r):
            # < 0: r is on the right side of vector p->q (or external)
            # == 0: r is on the line of vector p->q
            # > 0: r is on the left side of vector p->q (or inner)
            return (p.x-r.x)*(q.y-r.y) - (p.y-r.y)*(q.x-r.x)

        def drive(hull, r): # reduce inner call: accum_value = func(accum_value, p)
            hull.append(r)
            while len(hull) > 2 and sign(*hull[-3:]) < 0:
                hull.pop(-2)

            return hull

        # main
        points.sort(key = lambda p: (p.x, p.y))
        # get lower board
        lower = reduce(drive, points, [])
        # get upper board
        upper = reduce(drive, points[::-1], [])

        return list(set(lower + upper))

nums = [[1,1],[2,2],[2,0],[2,4],[3,3],[4,2]]
points = []
for x, y in nums:
    points.append(Point(x, y))

print(Solution().outerTrees(points))


'''
    Written by Debojit Kaushik (25th April 2018)
'''

import pandas as pd
import numpy as np
import traceback, json
from q10_clean import MakeFile

if __name__ == '__main__':
    try:
        f = pd.read_csv('../csv_results/plot_data.csv', encoding = 'utf-8')

        data = {
            'positive': {
                '1': 0,
                '2': 0,
                '3': 0,
                '4': 0,
                '5': 0
            },
            'negative': {
                '1': 0,
                '2': 0,
                '3': 0,
                '4': 0,
                '5': 0
            }
        }
        for it, item in enumerate(f['SentimentLabels']):
            if item == 1:
                data['positive'][str(f['ReviewRating'][it])] += 1
            elif item == 0:
                data['negative'][str(f['ReviewRating'][it])] += 1

        MakeFile('../src/q6-d.json', data)
    except Exception:
        print(traceback.format_exc())


# open and read the file to a string
input = open("input.in", "r")
input = input.read()
input = input.split("\n")

# Solution Time: 4:46
def process_input(input):
    screen = [['_' for x in range(50)] for y in range(6)]
    for line in input:
        if line.startswith("rect"):
            line = line.replace("rect ", "")
            line = line.split("x")
            for x in range(int(line[0])):
                for y in range(int(line[1])):
                    screen[y][x] = "#"
        elif line.startswith("rotate row"):
            line = line.replace("rotate row y=", "")
            line = line.split(" by ")
            row = int(line[0])
            amount = int(line[1])
            for x in range(amount):
                screen[row] = [screen[row][-1]] + screen[row][:-1]
        elif line.startswith("rotate column"):
            line = line.replace("rotate column x=", "")
            line = line.split(" by ")
            column = int(line[0])
            amount = int(line[1])
            for x in range(amount):
                column_values = [screen[y][column] for y in range(len(screen))]
                column_values = column_values[-1:] + column_values[:-1]
                for y in range(len(screen)):
                    screen[y][column] = column_values[y]
    return screen

def print_screen(screen):
    for row in range(0, len(screen), 6):
        for col in range(0, len(screen[0]), 5):
            for r in range(row, row+6):
                print(''.join(screen[r][col:col+5]))
            print()

print_screen(process_input(input))


import numpy as np
import os

data_dir = '\\..\\data\\mnist\\'

def one_hot(x,n):
	if type(x) == list:
		x = np.array(x)
	x = x.flatten()
	o_h = np.zeros((len(x),n))
	o_h[np.arange(len(x)),x] = 1
	return o_h

def mnist(ntrain=60000,ntest=10000,onehot=True):
	fd = open(os.path.dirname(__file__) + data_dir + 'train-images-idx3-ubyte')
	loaded = np.fromfile(file=fd,dtype=np.uint8)
	trX = loaded[16:].reshape((60000,28*28)).astype(float)

	fd = open(os.path.dirname(__file__) + data_dir + 'train-labels-idx1-ubyte')
	loaded = np.fromfile(file=fd,dtype=np.uint8)
	trY = loaded[8:].reshape((60000))

	fd = open(os.path.dirname(__file__) + data_dir + 't10k-images-idx3-ubyte')
	loaded = np.fromfile(file=fd,dtype=np.uint8)
	teX = loaded[16:].reshape((10000,28*28)).astype(float)

	fd = open(os.path.dirname(__file__) + data_dir + 't10k-labels-idx1-ubyte')
	loaded = np.fromfile(file=fd,dtype=np.uint8)
	teY = loaded[8:].reshape((10000))

	trX = trX/255.
	teX = teX/255.

	trX = trX[:ntrain]
	trY = trY[:ntrain]

	teX = teX[:ntest]
	teY = teY[:ntest]

	if onehot:
		trY = one_hot(trY, 10)
		teY = one_hot(teY, 10)
	else:
		trY = np.asarray(trY)
		teY = np.asarray(teY)

	return trX,teX,trY,teY

def geotutor():
    f=open(os.path.dirname(__file__)+'\\..\\data\\geo tutor\\all_data.txt')
    firstline=True
    colnames=list()
    data=list()
    y=list()
    del_index=list()
    for line in f:
        if(firstline):
            y_index=index=0
            for col in line.split('	'):
                colnames.append(col)
                if(col=='Outcome'):
                    y_index=index
                    del_index.append(index)
                if(col in ['Row', 'Transaction Id', 'Session Id', 'Time', 'Problem Start Time','Duration (sec)', 'Sample Name']):
                    del_index.append(index)
                if(col in ['KC (Geometry)', 'KC Category (Geometry)', 'KC (Area)', 'KC Category (Area)','KC (Textbook)', 'KC Category (Textbook)','KC (Textbook New)', 'KC Category (Textbook New)', 'KC (Decompose)', 'KC Category (Decompose)',
                'KC (Textbook_New_Decompose)', 'KC Category (Textbook_New_Decompose)', 'KC (DecomposeArith)', 'KC Category (DecomposeArith)', 'KC (DecompArithDiam)', 'KC Category (DecompArithDiam)', 'KC (xDecmpTrapCheat)', 'KC Category (xDecmpTrapCheat)',
                'KC (LFASearchAICWholeModel2)', 'KC Category (LFASearchAICWholeModel2)', 'KC (textbook2)', 'KC Category (textbook2)', 'KC (LFASearchAICWholeModel3)', 'KC Category (LFASearchAICWholeModel3)', 'KC (LFASearchModel1-renamed&chgd)',
                'KC Category (LFASearchModel1-renamed&chgd)', 'KC (LFASearchModel1-backward)', 'KC Category (LFASearchModel1-backward)', 'KC (LFASearchModel1-backward)','KC Category (LFASearchModel1-backward)', 'KC (LFASearchModel1-renamed&chgd.2)',
                'KC Category (LFASearchModel1-renamed&chgd.2)',	'KC (LFASearchModel1-renamed&chgd.3)','KC Category (LFASearchModel1-renamed&chgd.3)','KC (LFASearchModel1.context-single)','KC Category (LFASearchModel1.context-single)',
                'KC (LFASearchModel1-context)','KC Category (LFASearchModel1-context)',	'KC (LFASearchModel1-context)',	'KC Category (LFASearchModel1-context)','KC (LFASearchModel1-back-context)','KC Category (LFASearchModel1-back-context)',
                'KC (LFASearchModel1-back-context)','KC Category (LFASearchModel1-back-context)','KC (LFASearchModel1-back-context)','KC Category (LFASearchModel1-back-context)','KC (LFASearchModel1-renamed)','KC Category (LFASearchModel1-renamed)',
                'KC (Single-KC)','KC Category (Single-KC)','KC (Unique-step)','KC Category (Unique-step)','KC (Decompose_height)','KC Category (Decompose_height)','KC (Circle-Collapse)','KC Category (Circle-Collapse)','KC (Orig-Trap-Merge)',
                'KC Category (Orig-Trap-Merge)','KC (Orig-trap-merge)','KC Category (Orig-trap-merge)','KC (Concepts)','KC Category (Concepts)','KC (new trap merge)','KC Category (new trap merge)','KC (Merge-Trap)','KC Category (Merge-Trap)',
                'KC (DecompArithDiam2)','KC Category (DecompArithDiam2)','KC (LFASearchAICWholeModel3arith)','KC Category (LFASearchAICWholeModel3arith)','KC (LFASearchAIC1_no_textbook_new_decompose)','KC Category (LFASearchAIC1_no_textbook_new_decompose)',
                'KC (LFASearchAIC2_no_textbook_new_decompose)',	'KC Category (LFASearchAIC2_no_textbook_new_decompose)','KC (LFASearchAIC1_with_texkbook_new_decompose)','KC Category (LFASearchAIC1_with_texkbook_new_decompose)',
                'KC (LFASearchAIC2_with_texkbook_new_decompose)','KC Category (LFASearchAIC2_with_texkbook_new_decompose)','KC (Item)','KC Category (Item)']):
                 #keep KC (Original)	KC Category (Original)
                    del_index.append(index)
                index+=1
            count=0
            for i in del_index:
                del colnames[i-count]
                count+=1
            firstline=False
        else:
            row=line.split('	')
            y.append(row[y_index])
            count=0
            for i in del_index:
                del row[i-count]
                count+=1
            data.append(row)
    #colnames=np.array(colnames)
    #data=np.array(data)
    #y=np.array(y)
    return colnames, data, y

def simple_process(cols, data, targets): #transform each feature as true/false
    features=list()
    for i in range(len(cols)):
        s=set(row[i] for row in data)
        while(len(s)>0):
            feature=cols[i]+':'+s.pop()
            features.append(feature)

    newdata=list()
    for row in data:
        newrow=[0]*len(features)
        for i in range(len(row)):
            index=features.index(cols[i]+':'+row[i])
            newrow[index]=1
        newdata.append(newrow)

    newy=list()
    y_cat=list(set(targets))
    for y in targets:
        newy.append(y_cat.index(y))

    features=np.array(features)
    newdata=np.array(newdata)
    newy=np.array(newy)
    newy = one_hot(newy, 2)
    return features,newdata,newy

class question:
    correct_attempts=0.
    incorrect_attempts=0.
    upper_correct=0.
    lower_correct=0.
    def __init__(self,qid):
        self.qid=qid
    def difficulty(self):
        return self.incorrect_attempts/(self.incorrect_attempts+self.correct_attempts)
    def discrimination(self):
        return (self.upper_correct-self.lower_correct)/(self.upper_correct+self.lower_correct)

class student:
    kc_prac_num=dict()
    def __init__(self,sid,all_kcs):
        self.sid=sid
        for kc in all_kcs:
            self.kc_prac_num[kc]=0
    def inc_exec(self,kc_name):
        self.kc_prac_num[kc_name]+=1
    def get_prac_num(self,kc_name):
        return self.kc_prac_num[kc_name]

def items_features(cols,data,targets):
    q_dict=dict()
    prob_index=0
    index=0
    for col in cols:
        if(col=='Problem Name'):
            prob_index=index
        index+=1
    rownum=0
    for row in data:
        if(not q_dict.has_key(row[prob_index])):
            q=question(row[prob_index])
            q_dict[row[prob_index]]=q
        q=q_dict[row[prob_index]]
        if(targets[rownum]=='CORRECT'):
            q.correct_attempts+=1
        else:
            q.incorrect_attempts+=1
        rownum+=1
    '''for q in q_dict.values():
        print q.qid, q.correct_attempts, q.incorrect_attempts'''
    return q_dict

def process(cols,data,targets):
    features=list()
    newlogs=list()
    s_dict=dict()
    index=0
    index_kc=index_stu=0
    all_kcs=list()
    for col in cols:
        if(col=='KC (Original)'):
            index_kc=index
        if(col=='Anon Student Id'):
            index_stu=index
        index+=1

    features=list()
    for i in range(len(cols)):
        s=set(row[i] for row in data)
        while(len(s)>0):
            feature=cols[i]+':'+s.pop()
            features.append(feature)
    for kc in set(row[index_kc] for row in data):
        features.append("prac_num_"+kc)
        all_kcs.append(kc)

    for record in data:
        newrecord=list()
        newrecord=[0]*len(features)
        for i in range(len(record)):
            index=features.index(cols[i]+':'+record[i])
            newrecord[index]=1

        stuid=record[index_stu] #student id
        kcid=record[index_kc]
        if(not s_dict.has_key(stuid)):
            s_dict[stuid]=student(stuid,all_kcs)
        stu=s_dict[stuid]
        stu.inc_exec(kcid)
        for kc_prac_num in stu.kc_prac_num.values():
            newrecord.append(kc_prac_num)
        newlogs.append(newrecord)
    newlogs=normalize(newlogs)

    newy=list()
    y_cat=list(set(targets))
    for y in targets:
        newy.append(y_cat.index(y))
    newy=np.array(newy)
    newy = one_hot(newy, 2)
    return features,newlogs,newy

def normalize(data):
    max_index=np.argmax(data,axis=0)
    max_values=list(data[max_index[col]][col] for col in range(len(max_index)))
    for i in range(len(data)):
        for j in range(len(max_index)):
            if(data[max_index[j]][j] >0):
                data[i][j]/=(max_values[j]*1.)
    return data

col, trX, trY=geotutor()
items_features(col, trX, trY)
col, trX, trY=process(col, trX, trY)


import sys
input = sys.stdin.readline

N, M = map(int, input().split())
A, B, d = map(int, input().split())
D = [[0] * M for _ in range(N)]
D[A][B] = 1

graph = []
for _ in range(N):
    graph.append(list(map(int, input().split())))

dir = [(-1, 0), (0, -1), (1, 0), (0, 1)]
cnt = 1

while True:
    for _ in range(4):
        d += 1
        if d == 4:
            d = 0

        nA = A + dir[d][0]
        nB = B + dir[d][1]
        try:
            if D[nA][nB] == 0 and graph[nA][nB] == 0 and nA >= 0 and nA < N and nB >= 0 and nB < M:
                A, B = nA, nB
                D[nA][nB] = 1
                cnt += 1
                break
        except IndexError:
            continue
    else:
        nA = A - dir[d][0]
        nB = B - dir[d][1]

        try:
            if D[nA][nB] == 0 and nA >= 0 and nA < N and nB >= 0 and nB < M:
                A, B = nA, nB
        except IndexError:
            continue

        else:
            break

print(cnt)


#! /usr/bin/env python
# -*- coding: utf-8 -*-
import json
import time
import pymongo
from main.config import *

class GetSysData(object):

    def __init__(self, machine_id, monitor_item, timing, no=0):
        self.machine_id = machine_id
        self.monitor_item = monitor_item
        self.timing = timing
        self.no = no

    def get_data(self):
        client = pymongo.MongoClient(MONGO_URL,MONGO_PORT)
        db = client[MONGO_DB]
        collection = db[self.machine_id]
        now_time = int(time.time())
        find_time = now_time-self.timing
        cursor = collection.find({'timestamp': {'$gte': find_time}}, {self.monitor_item: 1, "timestamp": 1}).limit(self.no)
        return cursor


import numpy as np

from HPOlibConfigSpace.configuration_space import ConfigurationSpace
from HPOlibConfigSpace.hyperparameters import UniformFloatHyperparameter, \
    UniformIntegerHyperparameter, CategoricalHyperparameter, \
    UnParametrizedHyperparameter, Constant

from ..regression_base import AutoSklearnRegressionAlgorithm
# get our own forests to replace the sklearn ones
from ...implementations import forest

class RandomForest(AutoSklearnRegressionAlgorithm):
    def __init__(self, n_estimators, criterion, max_features,
                 max_depth, min_samples_split, min_samples_leaf,
                 bootstrap,
                 max_leaf_nodes_or_max_depth="max_depth",
                 max_leaf_nodes=None, random_state=None,
                 n_jobs=1):
        self.n_estimators = int(n_estimators)
        self.estimator_increment = 10
        if criterion in ("mse",):
            self.criterion = criterion
        else:
            raise ValueError("criterion should be in (mse,) but is: %s" %
                             str(criterion))

        if max_features in ("sqrt", "log2", "auto"):
            raise ValueError("'max_features' should be a float: %s" %
                             str(max_features))
        self.max_features = float(max_features)

        self.max_leaf_nodes_or_max_depth = str(max_leaf_nodes_or_max_depth)
        if self.max_leaf_nodes_or_max_depth == "max_depth":
            if max_depth == 'None':
                self.max_depth = None
            else:
                self.max_depth = int(max_depth)
            self.max_leaf_nodes = None
        elif self.max_leaf_nodes_or_max_depth == "max_leaf_nodes":
            self.max_depth = None
            if max_leaf_nodes == 'None':
                self.max_leaf_nodes = None
            else:
                self.max_leaf_nodes = int(max_leaf_nodes)
        else:
            raise ValueError("max_leaf_nodes_or_max_depth sould be in "
                             "('max_leaf_nodes', 'max_depth'): %s" %
                             self.max_leaf_nodes_or_max_depth)
        self.min_samples_split = int(min_samples_split)
        self.min_samples_leaf = int(min_samples_leaf)

        if bootstrap == "True":
            self.bootstrap = True
        else:
            self.bootstrap = False

        self.random_state = random_state
        self.n_jobs = n_jobs
        self.estimator = None

    def fit(self, X, Y):
        num_features = X.shape[1]
        max_features = int(float(self.max_features) * (np.log(num_features) + 1))
        # Use at most half of the features
        max_features = max(1, min(int(X.shape[1] / 2), max_features))
        self.estimator = forest.RandomForestRegressor(
            n_estimators=0,
            criterion=self.criterion,
            max_features=max_features,
            max_depth=self.max_depth,
            min_samples_split=self.min_samples_split,
            min_samples_leaf=self.min_samples_leaf,
            bootstrap=self.bootstrap,
            max_leaf_nodes=self.max_leaf_nodes,
            random_state=self.random_state,
            n_jobs=self.n_jobs,
            warm_start=True)
        # JTS TODO: I think we might have to copy here if we want self.estimator
        #           to always be consistent on sigabort
        while len(self.estimator.estimators_) < self.n_estimators:
            tmp = self.estimator # TODO copy ?
            tmp.n_estimators += self.estimator_increment
            tmp.fit(X, Y)
            self.estimator = tmp
        return self

    def predict(self, X):
        if self.estimator is None:
            raise NotImplementedError
        return self.estimator.predict(X)

    @staticmethod
    def get_properties():
        return {'shortname': 'RF',
                'name': 'Random Forest Regressor',
                'handles_missing_values': False,
                'handles_nominal_values': False,
                'handles_numerical_features': True,
                'prefers_data_scaled': False,
                # TODO find out if this is good because of sparcity...
                'prefers_data_normalized': False,
                'is_deterministic': True,
                'handles_sparse': False,
                # TODO find out what is best used here!
                # But rather fortran or C-contiguous?
                'preferred_dtype': np.float32}

    @staticmethod
    def get_hyperparameter_search_space(dataset_properties=None):
        criterion = Constant(name="criterion", value="mse")
        # Copied from classification/random_forest.py
        #n_estimators = UniformIntegerHyperparameter(
        #    name="n_estimators", lower=10, upper=100, default=10, log=False)
        n_estimators = Constant("n_estimators", 100)
        max_features = UniformFloatHyperparameter(
            "max_features", 0.5, 5, default=1)
        max_depth = UnParametrizedHyperparameter("max_depth", "None")
        min_samples_split = UniformIntegerHyperparameter(
            name="min_samples_split", lower=2, upper=20, default=2, log=False)
        min_samples_leaf = UniformIntegerHyperparameter(
            name="min_samples_leaf", lower=1, upper=20, default=1, log=False)
        bootstrap = CategoricalHyperparameter(
            name="bootstrap", choices=["True", "False"], default="True")

        cs = ConfigurationSpace()
        cs.add_hyperparameter(n_estimators)
        cs.add_hyperparameter(max_features)
        cs.add_hyperparameter(max_depth)
        cs.add_hyperparameter(min_samples_split)
        cs.add_hyperparameter(min_samples_leaf)
        cs.add_hyperparameter(bootstrap)
        cs.add_hyperparameter(criterion)

        return cs


from utils.util import Util
from db.database import Database
from datetime import date
from models.cliente import Cliente
from models.produto import Produto

class Pedido():
    def __init__(self):
        self.id_pedido = 0
        self.cliente = None
        self.itens = []
        self.total = 0

    def __to_object(itens):

        lista_produto = []
        lista_pedido = []
        lista_pedido_item_somado = []
        soma_total_pedido = 0

        for index, elem in enumerate(itens):
            curr_item = itens[index]
            next_item = itens[(index + 1) % len(itens)]

            id_pedido_atual = curr_item[3]
            id_pedido_proximo = next_item[3]

            if id_pedido_atual == id_pedido_proximo:

                id_item = curr_item[5]
                resultado = any(
                    elem == id_item for elem in lista_pedido_item_somado)
                if resultado == False:
                    produto = Produto(
                        curr_item[7], curr_item[8], curr_item[9], curr_item[10])
                    lista_produto.append(produto)
                    lista_pedido_item_somado.append(id_item)
                    soma_total_pedido += float(curr_item[10])

                id_item = next_item[5]
                resultado = any(
                    elem == id_item for elem in lista_pedido_item_somado)
                if resultado == False:
                    produto = Produto(
                        next_item[7], next_item[8], next_item[9], next_item[10])
                    lista_produto.append(produto)
                    lista_pedido_item_somado.append(id_item)
                    soma_total_pedido += float(next_item[10])

            else:
                cliente = Cliente(curr_item[0], curr_item[1], curr_item[2])

                pedido = Pedido()
                pedido.id_pedido = curr_item[3]
                pedido.cliente = cliente
                pedido.itens = lista_produto

                pedido.total = soma_total_pedido
                lista_pedido.append(pedido)

                lista_produto = []
                lista_pedido_item_somado = []
                soma_total_pedido = 0

        return lista_pedido

    @ staticmethod
    def obtem_todos():

        try:
            query_sql = " select cli.id_cliente, "
            query_sql += " cli.nome, "
            query_sql += " cli.email, "
            query_sql += " ped.id_pedido, "
            query_sql += " ped.data_pedido, "
            query_sql += " pit.id_item, "
            query_sql += " pit.data_pedido_item, "
            query_sql += " pro.id_produto, "
            query_sql += " pro.nome_produto, "
            query_sql += " pro.descricao, "
            query_sql += " pro.preco "
            query_sql += " from clientes as cli "
            query_sql += " inner join pedidos as ped on (cli.id_cliente = ped.id_cliente) "
            query_sql += " inner join pedido_itens as pit on (ped.id_pedido = pit.id_pedido) "
            query_sql += " inner join produtos as pro on(pit.id_produto = pro.id_produto) "
            query_sql += " order by ped.data_pedido"

            resultado = Database.consultar(query_sql)
            return Pedido.__to_object(resultado)

        except:
            return []

    @ staticmethod
    def obtem_por_id_pedido(id_pedido):
        query_sql = " select cli.id_cliente, "
        query_sql += " cli.nome, "
        query_sql += " cli.email, "
        query_sql += " ped.id_pedido, "
        query_sql += " ped.data_pedido, "
        query_sql += " pit.id_item, "
        query_sql += " pit.data_pedido_item, "
        query_sql += " pro.id_produto, "
        query_sql += " pro.nome_produto, "
        query_sql += " pro.descricao, "
        query_sql += " pro.preco "
        query_sql += " from clientes as cli "
        query_sql += " inner join pedidos as ped on (cli.id_cliente = ped.id_cliente) "
        query_sql += " inner join pedido_itens as pit on (ped.id_pedido = pit.id_pedido) "
        query_sql += " inner join produtos as pro on(pit.id_produto = pro.id_produto) "
        query_sql += f" where ped.id_pedido = {id_pedido} "
        query_sql += " order by ped.data_pedido "
        resultado = Database.consultar(query_sql)
        return Pedido.__to_object(resultado)

    def salvar_arquivo(self):
        try:
            data_pedido = date.today()
            query_insert_pedido = f"insert into pedidos (id_cliente, data_pedido) values ({self.cliente.id_cliente}, {data_pedido})"
            resultado_id_pedido = Database.executar(query_insert_pedido)

            for item in self.itens:
                data_pedido_item = date.today()
                query_insert_itens_pedido = f"insert into pedido_itens(id_pedido, id_produto, data_pedido_item) values ({str(resultado_id_pedido)}, {str(item.id_produto)}, {data_pedido_item})"
                Database.executar(query_insert_itens_pedido)

        except Exception as e:
            print(f"{Util.FAIL}Erro: {e}{Util.ENDC}")
            return False
        finally:
            return True


import matplotlib.pyplot as plt
from model_config import vgg16_net, model, batch_size
from tensorflow.keras.optimizers import Adam
from model_compile import nb_train_samples, train_generator, val_generator, nb_validation_samples, test_generator

vgg16_net.trainable = True
trainable = False
for layer in vgg16_net.layers:
    if layer.name == 'block5_conv3':
        trainable = True
    layer.trainable = trainable

# model.summary()  # Comment if you don`t need the summary

if __name__ == "__main__":
    model.compile(loss='categorical_crossentropy',
                  optimizer=Adam(lr=1e-5),
                  metrics=['accuracy'])

    history = model.fit(
                train_generator,
                steps_per_epoch=nb_train_samples // batch_size,
                epochs=5,
                validation_data=val_generator,
                validation_steps=nb_validation_samples // batch_size)

    scores = model.evaluate(test_generator)
    print(f"Accuracy on test data: {(scores[1]*100):.2f}")
    model.save("tuned_model.keras")

    plt.plot(history.history['accuracy'])
    plt.plot(history.history['val_accuracy'])
    plt.title('Model accuracy')
    plt.ylabel('accuracy')
    plt.xlabel('Epoch')
    plt.legend(['Train', 'Val'], loc='upper left')
    plt.savefig('tuned_model_training.png')
    plt.show()


from __future__ import division, print_function, absolute_import
import argparse

parser = argparse.ArgumentParser(description='Example with non-optional arguments')

parser.add_argument('method', action="store")
parser.add_argument('dataDir', action="store")
parser.add_argument('baseImage', action="store")
parser.add_argument('entryPoint', action="store")
args = parser.parse_args()
method = args.method
dataDir = args.dataDir
baseImage = args.baseImage
entryPoint = args.entryPoint
print (parser.parse_args())
import os
import sys
import tensorflow as tf
import numpy as np

def nkTrain():
    # Genrating random linear data
    # There will be 50 data points ranging from 0 to 50
    x = np.linspace(0, 50, 50)
    y = np.linspace(0, 50, 50)

    # Adding noise to the random linear data
    x += np.random.uniform(-4, 4, 50)
    y += np.random.uniform(-4, 4, 50)

    n = len(x) # Number of data points

    X = tf.placeholder("float")
    Y = tf.placeholder("float")
    W = tf.Variable(np.random.randn(), name = "W")
    b = tf.Variable(np.random.randn(), name = "b")
    learning_rate = 0.01
    training_epochs = 1000

    # Hypothesis
    y_pred = tf.add(tf.multiply(X, W), b)

    # Mean Squared Error Cost Function
    cost = tf.reduce_sum(tf.pow(y_pred-Y, 2)) / (2 * n)

    # Gradient Descent Optimizer
    optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

    # Global Variables Initializer
    init = tf.global_variables_initializer()

    sess = tf.Session()
    sess.run(init)

    # Iterating through all the epochs
    for epoch in range(training_epochs):

        # Feeding each data point into the optimizer using Feed Dictionary
        for (_x, _y) in zip(x, y):
            sess.run(optimizer, feed_dict = {X : _x, Y : _y})

        # Displaying the result after every 50 epochs
        if (epoch + 1) % 50 == 0:
            # Calculating the cost a every epoch
            c = sess.run(cost, feed_dict = {X : x, Y : y})
            print("Epoch", (epoch + 1), ": cost =", c, "W =", sess.run(W), "b =", sess.run(b))

    # Storing necessary values to be used outside the Session
    training_cost = sess.run(cost, feed_dict ={X: x, Y: y})
    weight = sess.run(W)
    bias = sess.run(b)

    print('Weight: ', weight, 'Bias: ', bias)
import random, string
import os
import logging
import subprocess
import importlib
from kubeflow import fairing
from kubeflow.fairing import TrainJob
from kubeflow.fairing.backends import KubeflowAWSBackend

AWS_REGION = 'us-west-2'
FAIRING_BACKEND = 'KubeflowAWSBackend'
AWS_ACCOUNT_ID = fairing.cloud.aws.guess_account_id()
BASE_DOCKER_IMAGE = baseImage
DOCKER_REGISTRY = '{}.dkr.ecr.{}.amazonaws.com'.format(AWS_ACCOUNT_ID, AWS_REGION)
#S3_BUCKET = f'{HASH}-kubeflow-pipeline-data'
S3_BUCKET = 'pjz16s-eks-ml-data'
ENTRY_POINT = entryPoint

#NOTEBOOK_BASE_DIR = fairing.notebook.notebook_util.get_notebook_name()

DATASET = dataDir
REQUIREMENTS = 'requirements.txt'

if FAIRING_BACKEND == 'KubeflowAWSBackend':
    from kubeflow.fairing.builders.cluster.s3_context import S3ContextSource
    BuildContext = S3ContextSource(
        aws_account=AWS_ACCOUNT_ID, region=AWS_REGION,
        bucket_name=S3_BUCKET
    )

BackendClass = getattr(importlib.import_module('kubeflow.fairing.backends'), FAIRING_BACKEND)

print("About to train job setup...")

from kubeflow.fairing import TrainJob
#train_job = TrainJob(str(ENTRY_POINT), input_files=[DATASET,REQUIREMENTS],
#                     base_docker_image=BASE_DOCKER_IMAGE,
#                     docker_registry=DOCKER_REGISTRY,
#                     backend=BackendClass(build_context_source=BuildContext))

print('ENTRY POINT for building image is :'+ ENTRY_POINT)

if ENTRY_POINT == "nkTrain" :
    print('Now in FUNCTION  or CLASS entrypoint. Entry point is set as '+ENTRY_POINT)
    train_job = TrainJob(nkTrain, input_files=[DATASET,REQUIREMENTS],
                     base_docker_image=BASE_DOCKER_IMAGE,
                     docker_registry=DOCKER_REGISTRY,
                     backend=BackendClass(build_context_source=BuildContext))
else :
    print('Now in NON function or class entrypoint. Entry point is set as '+ENTRY_POINT)
    train_job = TrainJob(str(ENTRY_POINT), input_files=[DATASET,REQUIREMENTS],
                     base_docker_image=BASE_DOCKER_IMAGE,
                     docker_registry=DOCKER_REGISTRY,
                     backend=BackendClass(build_context_source=BuildContext))

print("about to submit job")

outJob = train_job.submit()

print ("Job to create image  is :  " + str(outJob))
print("Successfully created Docker Image in the configured registry "+ DOCKER_REGISTRY)


#상하좌우
#for문 안에서 continue와 break의 차이 : continue는 반복을 끝내지 않고 i만 증가 시키지만 break문을 사용하면 반복을 끝내 버린다.
# 내가 푼 방식 장단점 : 단점을 살펴 보면 책에서 분 방식은 R, L, U, D 방향 별로 이동하는 값을 하나의 리스트에 x,y로 분리하여 정의해서 나의 코드보다 연산 횟수를 줄 일 수 있었고 범위또한 마지막 if 문에서 걸러서 연산 횟수를 한번으로 줄일 수 있다.
# 장점은 L, R, U, D에 대한 연산을 각각 조건문에 처리했기 때문에 코드의 가독성이 좋은 것 같다.

n=int(input())
data=[]

data.append(1)
data.append(1)

dir=input().split()

for i in dir:
  if i=='R':
    if data[1]==5:
      continue
    data[1]+=1

  elif i=='L':
    if data[1]==1:
      continue
    data[1]-=1

  elif i=='U':
    if data[0]<=1:
      continue
    data[0]-=1

  elif i=='D':
    if data[0]>=5:
      continue
    data[0]+=1

print(data[0],data[1])

# 책에서 푼 방식
n=int(input())
x,y=1,1
plans=input().split()

# L, R, U, D에 따른 이동 방향
dx = [0,0,-1,1]
dy=[-1,1,0,0]
move_types=['L','R','U','D']

# 입력값을 하나씩 끄집어 내서
for plan in plans:
  # L, R, U, D 인지 체크
  for i in range(len(move_types)):
    # 해당하는 방향에서의 값 더하기
    if plan==move_types[i]:
      nx=x+dx[i]
      ny=y+dy[i]
  # 만약에 범위를 초과할 시, 다음 순서로 이동
  if nx<1 or ny<1 or nx>n or ny>n:
    continue
    # x,y 결과 좌표에 연산한 좌표 할당
  x,y=nx,ny

print(x,y)


"""Call summarization routines."""

import click

from kmtools.action import summarize
from kmtools.action.kagi import kagi_action
from kmtools.source import WebResource

@click.command(name="summarize")
@click.option(
    "-q", "--quiet", is_flag=True, default=False, help="Output just the summary"
)
@click.option("-k", "--kagi", is_flag=True, default=False, help="Get summary from Kagi")
@click.argument("url")
@click.pass_obj
def summarize_command(details, url=None, quiet=False, kagi=False) -> None:
    """Output a summarization of the specified URL

    :param details: Context object
    :param url: URL to summarize
    """
    if url:
        source = WebResource(url)
        derived_date, summarization = summarize.summarize(source)
        if kagi:
            summarization = kagi_action.retrieve_summary(url)
        if not quiet:
            click.echo(
                f"The webpage at {url} was published on {derived_date}. It can be summarized as follows\n"
            )
        click.echo(summarization)
    else:
        click.echo("No URL submitted.")


import fileinput

score=0
for line in fileinput.input():
    line = line.strip()

    myChoice = line[2]
    theirChoice = line[0]

    if(myChoice=="X"):
        score+=1
    if(myChoice=="Y"):
        score+=2
    if(myChoice=="Z"):
        score+=3

    if(line in ["A Y","B Z","C X"]):
        score+=6 #win
    if(line in ["A X","B Y","C Z"]):
        score+=3 #draw

print(score)

    # A,X = Rock
    # B,Y = Paper
    # C,Z = Scissors


# Documentação:
# https://pythonhosted.org/PyPDF2/
# Mais exemplos de uso:
# http://www.blog.pythonlibrary.org/2018/06/07/an-intro-to-pypdf2/

# pip install pypdf2 # virtualenv
# pipenv install pypdf2 # pipenv
import PyPDF2
from pathlib import Path

CAMINHO_ROOT = Path(__file__).parent
CAMINHO_PDF = CAMINHO_ROOT / 'pdf'
CAMINHO_NOVO_PDF = CAMINHO_PDF / 'novo_arquivo.pdf'
CAMINHO_PDF_SEPARADO = CAMINHO_ROOT / 'pdf_separado'

# Junta os arquivos
novo_pdf = PyPDF2.PdfMerger()
for arquivo_pdf in CAMINHO_PDF.iterdir():
    arquivo = open(arquivo_pdf, 'rb')
    novo_pdf.append(arquivo)

with open(CAMINHO_NOVO_PDF, 'wb') as meu_novo_pdf:
    novo_pdf.write(meu_novo_pdf)

# # Separa o arquivo
# with open(CAMINHO_NOVO_PDF, 'rb') as arquivo_pdf:
#     leitor = PyPDF2.PdfReader(arquivo_pdf)
#     num_paginas = len(leitor.pages)

#     for num_pagina in range(num_paginas):
#         escritor = PyPDF2.PdfWriter()
#         pagina_atual = leitor.pages[num_pagina]
#         escritor.add_page(pagina_atual)

#         with open(CAMINHO_PDF_SEPARADO / f'{num_pagina}.pdf', 'wb') as novo_pdf:
#             escritor.write(novo_pdf)


import sys
import os
import random
from PIL import Image
import subprocess
import shutil
import random
from fuzzywuzzy import fuzz, process

def openImage(path):
    imageViewerFromCommandLine = {'linux':'xdg-open',
                                  'win32':'explorer',
                                  'darwin':'open'}[sys.platform]
    subprocess.run([imageViewerFromCommandLine, path])

# PureRef LOCATION
user_path = os.environ['USERPROFILE']

image_quantity = input("How many images will you open?? 🔢:  ")

if not len(sys.argv) > 1:
    basedir = os.getcwd()
    print("🤷🏽 We DON'T have basedir, using current dir ➡", basedir)
elif sys.argv[1] == ".":
    basedir = os.getcwd()
    print("👇🏽 Got it! Using current dir ➡", basedir)
else:
    basedir = sys.argv[1]
    print("🙌 We have path! Added to var! ➡", basedir)

files_list = []
for root, dirs, files in os.walk(basedir):
    for file in files:
        if file.endswith(".jpeg") or file.endswith(".png") or file.endswith(".mp4") or file.endswith(".jfif") or file.endswith(".jpg") or file.endswith(".gif"):
            files_list.append(os.path.join(root, file))

# print 20 random images
files_chosen = random.sample(files_list, int(image_quantity))
print("file LIST 📃 ", files_chosen)  # prints the random filename
#image_var = files_chosen[0]
print("type of FILES_CHOSEN OUTSIDE LOOP:",type(files_chosen))
for item in files_chosen:
    print("type of FILES_CHOSEN IIIIINSIDE LOOP:",type(files_chosen))
    #item = "\"" + item + "\""
    print("grabbing img...",item)
    """ command = 'i_view64.exe '+item
    print("command!!💻", command) """
    #subprocess.Popen(item)
    openImage(item)

print("finished!💥")
raise SystemExit


# Преобразование строки чисел в массив чисел
# 1 2 3 4 5 6 7 8 9
# split() - функция, разделяющая строку на части по пробелам
# map(func, array) - функция, применяющая к каждому элементу массива array функцию func
# list() - преобразует в список
arr = list(map(int, input().split()))
print(arr)

# Вариант с помощью цикла
arr = input().split()
for i in range(len(arr)):
    arr[i] = int(arr[i])
print(arr)


import ast
import pdb
import pprint
import logging
logger = logging.getLogger(__name__)

SETUP_FUNCTION_NAMES = [
    'setup',
    # if setuptools.setup() is the function call. occurs in
    # https://github.com/jackmaney/python-stdlib-list
    'setuptools',
]

def parse(path_to_setuppy):
    """Helper function to gather information from setup.py

    Parameters
    ----------
    path_to_setuppy : list
        Path to setup.py file.
        e.g. '/home/edill/dev/conda/conda-skeletor/setup.py'

    Returns
    -------
    dict
        Dictionary keyed on the kwargs that get passed to the setup() function
        inside of setup.py
    """
    with open(path_to_setuppy, 'r') as f:
        code = f.read()
    # pdb.set_trace()
    tree = ast.parse(code)
    scraper = SetupScraper()
    logger.debug("\nScraping setup.py\n"
                   "-----------------")
    # try:
    scraper.visit(tree)
    # except IndexError as ie:
    #     pdb.set_trace()
    #     logger.debug('setup.py scaping failed -- %s' % ie)
    #     raise
    # info = {v[0]: v[1:] if len(v) == 2 else v[1]
    #         for v in scraper.setup_info}
    info = {}
    for line in scraper.setup_info:
        value = line[1:]
        if len(line) == 2:
            value = line[1]
        info[line[0]] = value

    # TODO correctly parse the entry_points argument
    # this will break as soon as the setup.py does not look like:
    # entry_points={
    #     'console_scripts': [
    #         'replay = replay.replay:main']},
    entry_points = info.get('entry_points')
    if entry_points:
        info['entry_points'] = {entry_points[0]: entry_points[1]}
    return info

class SetupScraper(ast.NodeVisitor):
    def __init__(self):
        self.in_setup = False
        self.setup_info = []

    def visit_keyword(self, node):
        if self.in_setup:
            logger.debug('Adding kwarg=%s to setup info' % node.arg)
            self.setup_info.append([node.arg])
        self.visit(node.value)

    def visit_Call(self, node):
        if self.in_setup:
            self.setup_info[-1].append(node)
        if ((isinstance(node.func, ast.Name) and node.func.id == 'setup') or
            (isinstance(node.func, ast.Attribute) and node.func.attr == 'setup')):
            logger.debug('entering setup function')
            self.in_setup = True
        logger.debug(pprint.pformat(vars(node)))
        self.generic_visit(node)
        if ((isinstance(node.func, ast.Name) and node.func.id == 'setup') or
            (isinstance(node.func, ast.Attribute) and node.func.attr == 'setup')):
            logger.debug('leaving setup function')
            self.in_setup = False

    def visit_Name(self, node):
        logger.debug('node info = %s' % vars(node))
        if node.id in SETUP_FUNCTION_NAMES:
            logger.debug('in setup function, skipping the name visitor for '
                         'node.id=%s' % node.id)
            return
        if self.in_setup:
            self.setup_info[-1].append(node.id)

    def visit_Str(self, node):
        if self.in_setup:
            self.setup_info[-1].append(node.s)

    def generic_visit(self, node):
        """Called if no explicit visitor function exists for a node.

        Overridden from the ast.NodeVisitor base class so that I can add some
        local state to keep track of whether or not my node visitor is inside
        a try/except block.  When a try block is encountered, the node is added
        to the `trys` instance attribute and then the try block is recursed in
        to.  Once the recursion has exited, the node is removed from the `trys`
        instance attribute
        """
        for field, value in ast.iter_fields(node):
            if isinstance(value, list):
                for item in value:
                    if isinstance(item, ast.AST):
                        self.visit(item)
            elif isinstance(value, ast.AST):
                self.visit(value)


# 4) Leia 2 valores de ponto flutuante A e B, que correspondem a 2 notas de um aluno. A seguir, calcule a média do aluno, sabendo que a nota A tem peso 3.5 e a nota B tem peso 7.5.

nota1 =float(input("Insira a primeira nota do aluno: "))
nota2 =float(input("Insira a segunda nota do aluno: "))

peso1 = 3.5
peso2 = 7.5

mediap = ( (nota1 * peso1) + (nota2 * peso2)  ) / (peso1 + peso2 )

print("O aluno possui a média ",mediap)


# -*- coding: utf-8 -*-
"""
Created on Thu Aug 16 23:58:16 2018

@author: Elliott
"""
import numpy as np
import cv2 as cv
class Feature:
    def __init__(self,pic_info_path,pic_path,feature_path):
        self.pic_info_path =pic_info_path
        self.pic_path =pic_path
        self.feature_path=feature_path

    #每行 列白点数
    def Feature1Extraction1(self):
        data =np.loadtxt(self.pic_info_path,dtype =str,skiprows=1,delimiter='\t')
        index =data[:,0]
        datacategory =data[:,1]
        pic =data[:,2]
        length = pic.shape[0]
        filepath1 = self.feature_path+'feature1.txt'
        file =open(filepath1,'w+')
        for i in range(length):
            image =cv.imread(self.pic_path+pic[i],0)
            pic_length ,pic_width=image.shape  #92 47
            featurelist1=[]
            for lines in range(pic_length):
                count =0
                for columns in range(pic_width):
                    value  =image[lines,columns]
                    if (value==0):
                            count +=1
                file.writelines(str(count/pic_width))
                file.writelines('\t')
            for lines in range(pic_width):
                count = 0
                for columns in range(pic_width):
                    value =image[lines,columns]
                    if (value==0):
                            count +=1
                file.writelines(str(count/pic_width))
                file.writelines('\t')
            file.writelines('\n')
        file.close()

    #区域密度
    def Feature1Extraction2(self,kernel_size):
        data =np.loadtxt(self.pic_info_path,dtype=str,skiprows=1,delimiter='\t')
        index  =data[:,0]
        datacategory =data[:,1]
        pic =data[:,2]
        legth =pic.shape[0]
        #image test
        #filepath_test =self.feature_path+'featuretest.txt'
        #filetest =open(filepath_test,'w+')
        #image =cv.imread(self.pic_path +pic[0],0)
        #pic_length ,pic_width=image.shape  #92 47
        #for i in range(pic_length):
        #    for j in range(pic_width):
        #        filetest.writelines(str(image[i,j]))
        #        filetest.writelines('\t')
        #    filetest.writelines('\n')
        #filetest.close()

        filepath2 = self.feature_path+'feature2.txt'
        file =open(filepath2,'w+')
        for i in range(legth):
            image =cv.imread(self.pic_path+pic[i],0)
            pic_length ,pic_width=image.shape  #92 47
            kernel_line_size = int(pic_length/kernel_size)-1
            kernel_col_size = int(pic_width/kernel_size)-1
            for lines in range(kernel_line_size):
                for columns in range(kernel_col_size):
                    total =0
                    for parameter_line in range(kernel_size):
                        for parameter_column in range(kernel_size):
                            total += image[lines*kernel_size+parameter_line,columns*kernel_size+parameter_column]
                    file.writelines(str(total))
                    file.writelines('\t')
            file.writelines('\n')
        file.close()

    #图像区域取最大值代替原区域，模糊处理
    def Feature1Extraction3(self,kernel_size):
        data =np.loadtxt(self.pic_info_path,dtype=str,skiprows=1,delimiter='\t')
        index  =data[:,0]
        datacategory =data[:,1]
        pic =data[:,2]
        legth =pic.shape[0]

        filepath3 = self.feature_path+'feature3.txt'
        file =open(filepath3,'w+')
        for i in range(legth):
            image =cv.imread(self.pic_path+pic[i],0)
            pic_length ,pic_width=image.shape  #92 47
            kernel_line_size = int(pic_length/kernel_size)-1
            kernel_col_size = int(pic_width/kernel_size)-1
            for lines in range(kernel_line_size):
                for columns in range(kernel_col_size):
                    maxnum =0
                    for parameter_line in range(kernel_size):
                        for parameter_column in range(kernel_size):
                            if maxnum<image[lines*kernel_size+parameter_line,columns*kernel_size+parameter_column]:
                                maxnum =image[lines*kernel_size+parameter_line,columns*kernel_size+parameter_column]
                    file.writelines(str(maxnum))
                    file.writelines('\t')
            file.writelines('\n')
        file.close()

    def Feature1Extraction4(self):
        pass

    def Feature1Extraction5(self):
        pass

    def Feature1Extraction6(self):
        pass


import numpy as np
from numpy.fft import rfftn, irfftn
from numpy.fft import fftn, ifftn

def fftconvolve(in1, in2, mode="full", fft_in1=None, fft_in2=None):
    """Convolve two N-dimensional arrays using FFT.

    Convolve `in1` and `in2` using the fast Fourier transform method, with
    the output size determined by the `mode` argument.

    This is generally much faster than `convolve` for large arrays (n > ~500),
    but can be slower when only a few output values are needed, and can only
    output float arrays (int or object array inputs will be cast to float).

    Parameters
    ----------
    in1 : array_like
        First input.
    in2 : array_like
        Second input. Should have the same number of dimensions as `in1`;
        if sizes of `in1` and `in2` are not equal then `in1` has to be the
        larger array.
    mode : str {'full', 'valid', 'same'}, optional
        A string indicating the size of the output:

        ``full``
           The output is the full discrete linear convolution
           of the inputs. (Default)
        ``valid``
           The output consists only of those elements that do not
           rely on the zero-padding.
        ``same``
           The output is the same size as `in1`, centered
           with respect to the 'full' output.

    Returns
    -------
    out : array
        An N-dimensional array containing a subset of the discrete linear
        convolution of `in1` with `in2`.

    This code has been modified from scipy.signal.filter.py under the following
    license:
    Copyright (c) 2001, 2002 Enthought, Inc.
    All rights reserved.

    Copyright (c) 2003-2012 SciPy Developers.
    All rights reserved.

    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are met:

      a. Redistributions of source code must retain the above copyright notice,
         this list of conditions and the following disclaimer.
      b. Redistributions in binary form must reproduce the above copyright
         notice, this list of conditions and the following disclaimer in the
         documentation and/or other materials provided with the distribution.
      c. Neither the name of Enthought nor the names of the SciPy Developers
         may be used to endorse or promote products derived from this software
         without specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
    BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
    OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
    THE POSSIBILITY OF SUCH DAMAGE.
    """
    in1 = np.asarray(in1)
    in2 = np.asarray(in2)

    if np.rank(in1) == np.rank(in2) == 0:  # scalar inputs
        return in1 * in2
    elif not in1.ndim == in2.ndim:
        raise ValueError("in1 and in2 should have the same rank")
    elif in1.size == 0 or in2.size == 0:  # empty arrays
        return np.array([])

    s1 = np.array(in1.shape)
    s2 = np.array(in2.shape)
    complex_result = (np.issubdtype(in1.dtype, np.complex) or
                      np.issubdtype(in2.dtype, np.complex))
    size = s1 + s2 - 1

    # Always use 2**n-sized FFT
    fsize = 2 ** np.ceil(np.log2(size)).astype(int)
    fslice = tuple([slice(0, int(sz)) for sz in size])
    fft1_given = not (fft_in1 is None)
    fft2_given = not (fft_in2 is None)
    if not complex_result:
        if not fft1_given:
            fft_in1 = rfftn(in1, fsize)
        if not fft2_given:
            fft_in2 = rfftn(in2, fsize)
        ret = irfftn(fft_in1 * fft_in2, fsize)[fslice].copy()
        ret = ret.real
    else:
        if not fft1_given:
            fft_in1 = fftn(in1, fsize)
        if not fft2_given:
            fft_in2 = fftn(in2, fsize)
        ret = ifftn(fft_in1 * fft_in2)[fslice].copy()

    if mode == "full":
        pass
    elif mode == "same":
        ret = _centered(ret, s1)
    elif mode == "valid":
        ret = _centered(ret, s1 - s2 + 1)
    else:
        raise ValueError("Acceptable mode flags are 'valid',"
                         " 'same', or 'full'.")

    ret = [ret]
    if not fft1_given:
        ret.append(fft_in1)
    if not fft2_given:
        ret.append(fft_in2)
    ret = tuple(ret)

    if len(ret) == 1:
        ret = ret[0]
    return ret

def _centered(arr, newsize):
    # Return the center newsize portion of the array.
    newsize = np.asarray(newsize)
    currsize = np.array(arr.shape)
    startind = (currsize - newsize) // 2
    endind = startind + newsize
    myslice = [slice(startind[k], endind[k]) for k in range(len(endind))]
    return arr[tuple(myslice)]


import numpy as np
import matplotlib.pyplot as plt

from activelabeling.auxillary import target_func
from activelabeling.kernel import Kernel

plt.rcParams['text.usetex'] = True
plt.rcParams["font.family"] = "serif"
plt.rcParams["font.serif"] = "Times"

np.random.seed(100)

def solution(x_train, kernel, gamma=.3, verbose=False):

    n_train = x_train.shape[0]
    kernel.set_support(x_train)
    K = kernel(x_train)
    alpha = np.zeros(n_train, float)

    if isinstance(gamma, float):
        gamma = np.full(n_train, gamma, float)

    if verbose:
        history = np.zeros((n_train, 2), float)

    for i in range(n_train):
        z = K[i] @ alpha
        epsilon = np.sign(z - target_func(x_train[i,0]))
        alpha[i] -= epsilon * gamma[i]
        if verbose:
            history[i, 0] = epsilon
            history[i, 1] = z

    return alpha, history

n_train = 30
x_train = np.random.rand(n_train)[:, np.newaxis]

sigma = .2
kernel = Kernel('gaussian', sigma=sigma)

x_test = np.linspace(0, 1, 50)
y_test = target_func(x_test)

alpha, history = solution(x_train, kernel, verbose=True)
k_test = kernel(x_test[:, np.newaxis]).T

fig, ax = plt.subplots(1,1, figsize=(2.5, 1.75))
ind = history[:, 0] == 1
a = ax.scatter(x_train[ind, 0], history[ind, 1], s=10)
ind = history[:, 0] == -1
b = ax.scatter(x_train[ind, 0], history[ind, 1], s=10)
c, = ax.plot(x_test, y_test, linestyle='--')
y_pred = k_test @ alpha
d, = ax.plot(x_test, y_pred, alpha=.7)
ax.legend([c,d], ['Signal', 'Reconstruction'], prop={"size": 8})
ax.set_ylim(-2.5, 2.5)
ax.set_xticks([])
ax.set_yticks([])
ax.set_title(r'Active strategy', size=10)
ax.set_xlabel(r'Inputs',size=8)
ax.set_ylabel(r'Outputs', size=8)
fig.tight_layout()
fig.savefig('one_d_active.pdf')

epsilon_bis = np.zeros((n_train, 2), float)
epsilon_bis[:, 1] = np.random.randn(n_train)

alpha_bis = np.zeros(n_train, float)
gamma_0 = .7
gamma = np.full(n_train, gamma_0, float)
K = kernel(x_train)
for i in range(n_train):
    epsilon_bis[i, 0] = np.sign(epsilon_bis[i, 1] - target_func(x_train[i,0]))
    err = np.sign(epsilon_bis[i, 1] - K[i] @ alpha_bis)
    if err != epsilon_bis[i, 0]:
        alpha_bis[i] -= epsilon_bis[i, 0] * gamma[i]

fig, ax = plt.subplots(1,1, figsize=(2.5, 1.75))
ind = epsilon_bis[:, 0] == 1
a = ax.scatter(x_train[ind, 0], epsilon_bis[ind, 1], s=10)
ind = epsilon_bis[:, 0] == -1
b = ax.scatter(x_train[ind, 0], epsilon_bis[ind, 1], s=10)
c, = ax.plot(x_test, y_test, linestyle='--')
y_pred_bis = k_test @ alpha_bis
d, = ax.plot(x_test, y_pred_bis, alpha=.7)
ax.legend([a,b], ['Above', 'Below'], prop={"size": 8})
ax.set_ylim(-2.5, 2.5)
ax.set_xticks([])
ax.set_yticks([])
ax.set_title('Passive strategy', size=10)
ax.set_xlabel(r'Inputs',size=8)
ax.set_ylabel(r'Outputs', size=8)
fig.tight_layout()
fig.savefig('one_d_passive.pdf')


import random
import time
import warnings
import sys
import argparse
import shutil

import torch
import torch.backends.cudnn as cudnn
from torch.optim import SGD
from torch.utils.data import DataLoader
import torchvision.transforms as T
import torch.nn.functional as F
import os

from common.modules.classifier import Classifier
import common.vision.datasets as datasets
import common.vision.models as models
from common.vision.transforms import ResizeImage
from common.utils.metric import accuracy
from common.utils.meter import AverageMeter, ProgressMeter
from common.utils.data import ForeverDataIterator
from common.utils.logger import CompleteLogger

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

def main(args: argparse.Namespace):
    logger = CompleteLogger(args.log, args.phase)
    print(args)

    if args.seed is not None:
        random.seed(args.seed)
        torch.manual_seed(args.seed)
        cudnn.deterministic = True
        warnings.warn('You have chosen to seed training. '
                      'This will turn on the CUDNN deterministic setting, '
                      'which can slow down your training considerably! '
                      'You may see unexpected behavior when restarting '
                      'from checkpoints.')

    cudnn.benchmark = True

    # Data loading code
    normalize = T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
    train_transform = T.Compose([
        ResizeImage(256),
        T.RandomResizedCrop(224),
        T.RandomHorizontalFlip(),
        T.ToTensor(),
        normalize
    ])
    val_transform = T.Compose([
        ResizeImage(256),
        T.CenterCrop(224),
        T.ToTensor(),
        normalize
    ])

    dataset = datasets.__dict__[args.data]
    train_dataset = dataset(root=args.root, split='train', sample_rate=args.sample_rate,
                            download=True, transform=train_transform)
    train_loader = DataLoader(train_dataset, batch_size=args.batch_size, shuffle=True,
                              num_workers=args.workers, drop_last=True)
    train_iter = ForeverDataIterator(train_loader)
    val_dataset = dataset(root=args.root, split='test', sample_rate=100, download=True, transform=val_transform)
    val_loader = DataLoader(val_dataset, batch_size=args.batch_size, shuffle=False, num_workers=args.workers)

    # create model
    print("=> using pre-trained model '{}'".format(args.arch))
    backbone = models.__dict__[args.arch](pretrained=True)
    if args.pretrained:
        if os.path.isfile(args.pretrained):
            print("=> loading checkpoint '{}'".format(args.pretrained))
            model_dir = args.pretrained
            checkpoint = torch.load(model_dir)
            model_state = checkpoint['state_dict_best'] if not model_dir.endswith('.pth.tar') else checkpoint['state_dict']
            print("=> loaded checkpoint '{}' "
                  .format(args.pretrained))
            for k in list(model_state.keys()):
                # retain only encoder_q up to before the embedding layer
                if k.startswith('module.encoder_q') and not k.startswith('module.encoder_q.fc'):
                    model_state[f"{k[len('module.encoder_q.'):]}"] = model_state[k]
                # delete renamed or unused k
                del model_state[k]
            weights = model_state
            print(list(backbone.state_dict().keys()))
            print(f"TOTAL: {len(list(backbone.state_dict().keys()))}\n======")
            weights = {k: weights[k] for k in weights if k in backbone.state_dict()}
            print(f"Pretrained weights found (TOTAL: {len(list(weights.keys()))}):\n{weights.keys()}\n")
            x = backbone.state_dict()
            x.update(weights)
            backbone.load_state_dict(x)
    num_classes = train_dataset.num_classes
    classifier = Classifier(backbone, num_classes).to(device)

    # define optimizer and lr scheduler
    optimizer = SGD(classifier.get_parameters(args.lr), lr=args.lr, momentum=args.momentum, weight_decay=args.wd, nesterov=True)
    lr_scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, args.lr_decay_epochs, gamma=args.lr_gamma)

    # resume from the best checkpoint
    if args.phase == 'test':
        checkpoint = torch.load(logger.get_checkpoint_path('best'), map_location='cpu')
        classifier.load_state_dict(checkpoint)
        acc1 = validate(val_loader, classifier, args)
        print(acc1)
        return

    # start training
    best_acc1 = 0.0
    for epoch in range(args.epochs):
        print(lr_scheduler.get_lr())
        # train for one epoch
        train(train_iter, classifier, optimizer, epoch, args)
        lr_scheduler.step()
        # evaluate on validation set
        acc1 = validate(val_loader, classifier, args)

        # remember best acc@1 and save checkpoint
        torch.save(classifier.state_dict(), logger.get_checkpoint_path('latest'))
        if acc1 > best_acc1:
            shutil.copy(logger.get_checkpoint_path('latest'), logger.get_checkpoint_path('best'))
        best_acc1 = max(acc1, best_acc1)

    print("best_acc1 = {:3.1f}".format(best_acc1))
    logger.close()

def train(train_iter: ForeverDataIterator, model: Classifier, optimizer: SGD,
          epoch: int, args: argparse.Namespace):
    batch_time = AverageMeter('Time', ':4.2f')
    data_time = AverageMeter('Data', ':3.1f')
    losses = AverageMeter('Loss', ':3.2f')
    cls_accs = AverageMeter('Cls Acc', ':3.1f')

    progress = ProgressMeter(
        args.iters_per_epoch,
        [batch_time, data_time, losses, cls_accs],
        prefix="Epoch: [{}]".format(epoch))

    # switch to train mode
    model.train()

    end = time.time()
    for i in range(args.iters_per_epoch):
        x, labels = next(train_iter)

        x = x.to(device)
        label = labels.to(device)

        # measure data loading time
        data_time.update(time.time() - end)

        # compute output
        y, f = model(x)
        cls_loss = F.cross_entropy(y, label)
        loss = cls_loss

        # measure accuracy and record loss
        losses.update(loss.item(), x.size(0))
        cls_acc = accuracy(y, label)[0]
        cls_accs.update(cls_acc.item(), x.size(0))

        # compute gradient and do SGD step
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()

        # measure elapsed time
        batch_time.update(time.time() - end)
        end = time.time()

        if i % args.print_freq == 0:
            progress.display(i)

def validate(val_loader: DataLoader, model: Classifier, args: argparse.Namespace) -> float:
    batch_time = AverageMeter('Time', ':6.3f')
    losses = AverageMeter('Loss', ':.4e')
    top1 = AverageMeter('Acc@1', ':6.2f')
    top5 = AverageMeter('Acc@5', ':6.2f')
    progress = ProgressMeter(
        len(val_loader),
        [batch_time, losses, top1, top5],
        prefix='Test: ')

    # switch to evaluate mode
    model.eval()

    with torch.no_grad():
        end = time.time()
        for i, (images, target) in enumerate(val_loader):
            images = images.to(device)
            target = target.to(device)

            # compute output
            output, _ = model(images)
            loss = F.cross_entropy(output, target)

            # measure accuracy and record loss
            acc1, acc5 = accuracy(output, target, topk=(1, 5))
            losses.update(loss.item(), images.size(0))
            top1.update(acc1.item(), images.size(0))
            top5.update(acc5.item(), images.size(0))

            # measure elapsed time
            batch_time.update(time.time() - end)
            end = time.time()

            if i % args.print_freq == 0:
                progress.display(i)

        print(' * Acc@1 {top1.avg:.3f} Acc@5 {top5.avg:.3f}'
              .format(top1=top1, top5=top5))

    return top1.avg

if __name__ == '__main__':
    architecture_names = sorted(
        name for name in models.__dict__
        if name.islower() and not name.startswith("__")
        and callable(models.__dict__[name])
    )
    dataset_names = sorted(
        name for name in datasets.__dict__
        if not name.startswith("__") and callable(datasets.__dict__[name])
    )
    parser = argparse.ArgumentParser(description='Baseline for Finetuning')
    # dataset parameters
    parser.add_argument('root', metavar='DIR',
                        help='root path of dataset')
    parser.add_argument('-d', '--data', metavar='DATA',
                        help='dataset: ' + ' | '.join(dataset_names))
    parser.add_argument('-sr', '--sample-rate', default=100, type=int,
                        metavar='N',
                        help='sample rate of training dataset (default: 100)')
    # model parameters
    parser.add_argument('-a', '--arch', metavar='ARCH', default='resnet50',
                        choices=architecture_names,
                        help='backbone architecture: ' +
                             ' | '.join(architecture_names) +
                             ' (default: resnet50)')
    parser.add_argument('--pretrained', default=None,
                        help="pretrained checkpoint of the backbone. "
                             "(default: None, use the ImageNet supervised pretrained backbone)")
    # training parameters
    parser.add_argument('-b', '--batch-size', default=48, type=int,
                        metavar='N',
                        help='mini-batch size (default: 48)')
    parser.add_argument('--lr', '--learning-rate', default=0.01, type=float,
                        metavar='LR', help='initial learning rate', dest='lr')
    parser.add_argument('--lr-gamma', default=0.1, type=float, help='parameter for lr scheduler')
    parser.add_argument('--lr-decay-epochs', type=int, default=(12, ), nargs='+', help='epochs to decay lr')
    parser.add_argument('--momentum', default=0.9, type=float, metavar='M',
                        help='momentum')
    parser.add_argument('--wd', '--weight-decay', default=0.0005, type=float,
                        metavar='W', help='weight decay (default: 5e-4)')
    parser.add_argument('-j', '--workers', default=2, type=int, metavar='N',
                        help='number of data loading workers (default: 4)')
    parser.add_argument('--epochs', default=20, type=int, metavar='N',
                        help='number of total epochs to run')
    parser.add_argument('-i', '--iters-per-epoch', default=500, type=int,
                        help='Number of iterations per epoch')
    parser.add_argument('-p', '--print-freq', default=100, type=int,
                        metavar='N', help='print frequency (default: 100)')
    parser.add_argument('--seed', default=None, type=int,
                        help='seed for initializing training. ')
    parser.add_argument("--log", type=str, default='baseline',
                        help="Where to save logs, checkpoints and debugging images.")
    parser.add_argument("--phase", type=str, default='train', choices=['train', 'test'],
                        help="When phase is 'test', only test the model.")
    args = parser.parse_args()
    main(args)


#Esse script ultiliza a versão 3.6 do python e todas as bibliotecas em suas ultimas versões
import tkinter as tk
from tkinter import *
from tkinter import filedialog
from PIL import Image,ImageTk
import tabula
import PyPDF2
import pandas as pd
import openpyxl
from manipulacao_csv import manipulacao
from tkinter import messagebox

def selecionar_arquivo():
    arquivo_pdf = filedialog.askopenfilename(filetypes=[("Arquivos PDF","*.pdf")])
    tabela = tabula.read_pdf(arquivo_pdf)
    pdf = open(arquivo_pdf,"rb")
    conteudo_pdf = PyPDF2.PdfReader(pdf)
    n_pagina = len(conteudo_pdf.pages)

    if tabela: #essa parte do programa ta ok, façta adicionar para a função ler como texto
        listas_tabela= tabula.read_pdf(arquivo_pdf,pages="all",multiple_tables=True)
        for tabela in listas_tabela:
            tabela=tabela.drop("Unnamed: 0",axis=1)
            tabela=tabela.drop("Unnamed: 1",axis=1)
            tabela=tabela.drop("Unnamed: 2",axis=1)
            tabela=tabela.drop("Unnamed: 3",axis=1)
        tabela.to_excel("extractionPDF.xlsx")
        messagebox.showinfo('[AVISO]','Já terminei de extrair os dados e salvei o arquivo extractionPDF na pasta do programa.')

    elif n_pagina == 1:
        pageObj = conteudo_pdf.pages[0]
        pageObj.extract_text()
        texto_pagina=pageObj.extract_text()
        lista_trans=texto_pagina.split("\n")
        df=pd.DataFrame(zip(lista_trans))
        df.to_csv("extractionPDF.csv")
        manipulacao()

    elif n_pagina > 1:
        texto_pagina = []
        for i in range(n_pagina):
            pageObj = conteudo_pdf.pages[i]
            pageObj.extract_text()
            texto_pagina.append(pageObj.extract_text())
        result_3=[item.split("\n") for item in texto_pagina]
        result_3_flat=[item for l in result_3 for item in l]
        df=pd.DataFrame(zip(result_3_flat))
        df.to_csv("extractionPDF.csv")
        manipulacao()

vermelho_flamengo = "#bf2512"
janela_1 = tk.Tk()
janela_1.geometry("700x500")
janela_1.config(bg="black")
janela_1.iconphoto(False, PhotoImage(file="flamengo.png"))
janela_1.title("Extrator de pdf")
h1 = tk.Label (width=50,height=2,text="Olá Daniel Araya, selecione o pdf que deseja extrair os dados",font="arial 15 bold",fg=vermelho_flamengo, bg="black")
h1.grid(row=1,column=0)
h1.pack()
btn_selecionar_arquivo = tk.Button (text="Selecionar Arquivo pdf", bg=vermelho_flamengo, fg="black",font="arial",command=selecionar_arquivo)
btn_selecionar_arquivo.pack()
imagem_escudo = "escudo_flamengo.jpg"
image = Image.open(imagem_escudo) #abrindo a imagem
photo = ImageTk.PhotoImage(image)
escudo = tk.Label (janela_1,image=photo)
escudo.pack()
janela_1.resizable(width=False, height=False)
janela_1.mainloop()


from hyperspyui.plugins.plugin import Plugin
import numpy as np
import hyperspy.api as hs
from hyperspy.drawing import utils
from functools import partial

class VirtualBfDf(Plugin):
    name = "Virtual BF/DF"

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._rois = []

    def create_actions(self):
        self.add_action(self.name + '.virtual_navigator',
                        "Virtual navigator",
                        self.virtual_navigator,
                        tip="Set the navigator inesity by a virtual aperture")
        self.add_action(self.name + '.virtual_aperture',
                        "Virtual aperture",
                        self.virtual_aperture,
                        tip="Add a virtual aperture to the diffraction image")
        self.add_action(self.name + '.virtual_annulus',
                        "Virtual annulus",
                        self.virtual_annulus,
                        tip="Add a virtual annulus to the diffraction image")

    def create_menu(self):
        self.add_menuitem(
            'Diffraction', self.ui.actions[self.name + '.virtual_navigator'])
        self.add_menuitem(
            'Diffraction', self.ui.actions[self.name + '.virtual_aperture'])
        self.add_menuitem(
            'Diffraction', self.ui.actions[self.name + '.virtual_annulus'])

    def _on_close(self, roi):
        for w in roi.widgets:
            w.close()
        roi.signal_map.clear()
        if roi in self._rois:
            self._rois.remove(roi)

    def virtual_navigator(self, signal=None):
        return self.virtual_aperture(signal=signal, navigate=True)

    def virtual_annulus(self, signal=None):
        return self.virtual_aperture(signal=signal, annulus=True)

    def virtual_aperture(self, signal=None, annulus=False, navigate=False):
        ui = self.ui
        if signal is None:
            signal = ui.get_selected_signal()
        dd = np.array([a.high_value + a.low_value for a in
                       signal.axes_manager.signal_axes]) / 2.0
        r = hs.roi.CircleROI(dd[0], dd[1],
                             signal.axes_manager.signal_axes[0].scale*3)
        if annulus:
            r.r_inner = signal.axes_manager.signal_axes[0].scale*2
        s_virtual = r.interactive(signal, None,
                                  axes=signal.axes_manager.signal_axes)
        s_nav = hs.interactive(
            s_virtual.mean,
            s_virtual.events.data_changed,
            axis=s_virtual.axes_manager.signal_axes)
        s_nav.axes_manager.set_signal_dimension(2)
        if navigate:
            signal.plot(navigator=s_nav)
            signal._plot.navigator_plot.update()
            s_nav.events.data_changed.connect(
                signal._plot.navigator_plot.update, [])
            utils.on_figure_window_close(
                signal._plot.navigator_plot.figure,
                partial(self._on_close, r))
        else:
            s_nav.plot()
            utils.on_figure_window_close(
                s_nav._plot.signal_plot.figure,
                partial(self._on_close, r))

        if navigate:
            r.add_widget(signal, axes=signal.axes_manager.signal_axes,
                         color='darkorange')
        else:
            r.add_widget(signal, axes=signal.axes_manager.signal_axes)
        self._rois.append(r)
        self.record_code("<p>.virtual_aperture(navigate=%s)" % navigate)


from operator import truediv
from tkinter import FIRST
from selenium import webdriver

import time

class CookieClicker:

    def __init__(self):
       self.SITE_LINK = "https://orteil.dashnet.org/cookieclicker/"
       self.SITE_MAP = {
           "buttons": {
               "biscoito":{
                   "xpath": "/html/body/div/div[2]/div[15]/div[8]/button"
               },
               "upgrade":{
                   "xpath": "/html/body/div/div[2]/div[19]/div[3]/div[6]/div[$$number$$]"
               },
               "pt":{
                   "xpath": "/html/body/div[2]/div[2]/div[2]/div/div[11]"
               },
               "clique_em_go_it":{
                   "xpath": "/html/body/div[1]/div/a[1]"
               },
               "label_qtd_cookies":{
                   "xpath": "/html/body/div/div[2]/div[15]/div[4]"
               }
           }
       }

       self.driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
       self.driver.maximize_window()

    def abrir_site(self):
           time.sleep(2)
           self.driver.get(self.SITE_LINK)
           time.sleep(10)

    def clicar_em_pt(self):
           self.driver.find_element_by_xpath(self.SITE_MAP["buttons"]["pt"]["xpath"]).click()

    def clique_em_go_it(self):
           self.driver.find_element_by_xpath(self.SITE_MAP["buttons"]["clique_em_go_it"]["xpath"]).click()

    def clicar_no_cookei(self):
        self.driver.find_element_by_xpath(self.SITE_MAP["buttons"]["biscoito"]["xpath"]).click()

    def pega_melhor_upgrade(self):
            encontrei = False

            elemento_atual = 2

            while not encontrei:
                objeto = self.SITE_MAP["buttons"]["upgrade"]["xpath"].replace("$$number$$" , str(elemento_atual))
                classes_objeto = self.driver.find_element_by_xpath(objeto).get_attribute("class")

                if not "enabled" in classes_objeto:
                    encontrei: True
                    break
                else:
                    elemento_atual += 1

            return  elemento_atual - 1

    def comprar_upgrade(self):
           objeto = self.SITE_MAP["buttons"]["upgrade"]["xpath"].replace("$$number$$", str(self.pega_melhor_upgrade()))
           self.driver.find_element_by_xpath(objeto).click()

    def recuperarValorDeCookies(self):
            valor_do_campo_cookie = self.driver.find_element_by_xpath(self.SITE_MAP["buttons"]["label_qtd_cookies"]["xpath"]).get_attribute("innerHTML")
            return valor_do_campo_cookie

biscoito = CookieClicker()
biscoito.abrir_site()
biscoito.clicar_em_pt()
biscoito.clique_em_go_it()

i = 0
time.sleep(3)
while True:
    if i % 100 == 0 and i != 0:
        time.sleep(1)
        qtd_cookies = biscoito.recuperarValorDeCookies()
        print("o valor de I é ", i)
        print("quantidade de cookies quando cliquei é ", qtd_cookies)
        biscoito.comprar_upgrade()
        time.sleep(1)
    biscoito.clicar_no_cookei()
    i += 1