import RPi.GPIO as GPIO
import time
from configparser import ConfigParser
from helpers import init_all_loggers, get_main_bot_logger

logger = get_main_bot_logger()

def toggle_relay(relay_pin, duration):
    GPIO.output(relay_pin, GPIO.HIGH)
    time.sleep(duration)
    GPIO.output(relay_pin, GPIO.LOW)
    logger.info(f"Relay {relay_pin} activated")

def main(config):
    config_settings = config['SETTINGS']
    config_gpio = config['GPIO']
    init_all_loggers(log_level=config_settings.get("LOG_LEVEL"))

    TRIG = int(config_gpio.get("ULTRASONIC_TRIG"))
    ECHO = int(config_gpio.get("ULTRASONIC_ECHO"))
    RELAY = int(config_gpio.get("RELAY"))
    DURATION = int(config_settings.get("RELAY_DURATION"))

    logger.info(f"Starting up main....")
    try:
        GPIO.setmode(GPIO.BCM)
        GPIO.setwarnings(False)

        max_time = 0.04
        while True:
            GPIO.setup(TRIG,GPIO.OUT)
            GPIO.setup(ECHO,GPIO.IN)
            GPIO.setup(RELAY, GPIO.OUT)

            GPIO.output(TRIG,False)
            time.sleep(0.01)
            GPIO.output(TRIG,True)
            time.sleep(0.00001)
            GPIO.output(TRIG,False)

            pulse_start = time.time()
            timeout = pulse_start + max_time
            while GPIO.input(ECHO) == 0 and pulse_start < timeout:
                pulse_start = time.time()

            pulse_end = time.time()
            timeout = pulse_end + max_time
            while GPIO.input(ECHO) == 1 and pulse_end < timeout:
                pulse_end = time.time()

            pulse_duration = pulse_end - pulse_start
            distance = pulse_duration * 17000
            distance = round(distance, 2)

            if distance < 20:
                toggle_relay(relay_pin=RELAY, duration=DURATION)

    except Exception as e:
        logger.info(f"__Exception:Main__: {e}")
        GPIO.cleanup()

if __name__ == "__main__":
    config = ConfigParser()
    config.read("config.ini")

    main(config)


import numpy as np

height = float(input("Enter the pentagon height: "))

side = (2 * height) / (np.sqrt(5 + 2 * np.sqrt(5)))
area = (side ** 2) * np.sqrt(25 + 10 * np.sqrt(5)) / 4
perimeter = side * 5

print("The length for one side of the pentagon is %.4f." % (side))
print("The pentagon area and perimeter are %0.4f and %0.4f." % (area, perimeter))


"""
Interval scheduling maximization

- sort interval by end time, greedily take next interval with smallest ending, if its start does not overlap with previous end
https://leetcode.com/problems/maximum-number-of-non-overlapping-substrings/
"""

def interval_schedule(intervals):
    intervals.sort(key=lambda x: (x[1], x[0]))  # sort by end first, then start

    # maximum interval scheduling, greedily take the next one with smallest ending time,
    # if its start does not overlap with previous ending
    res, prev_end = [], -1
    for start, end in intervals:
        if start > prev_end:  # next smallest ending interval's start does not overlap with previous ending
            res.append((start, end))  # add to result
            prev_end = end  # update previous ending

    return res


class Solution:
    def maxProbability(self, n: int, edges: List[List[int]], succProb: List[float], start: int, end: int) -> float:

        adjList =collections.defaultdict(list)

        for i in range(len(edges)):
            s,d = edges[i]
            p=succProb[i]

            adjList[s].append((d,p))
            adjList[d].append((s,p))

        res=[0.0]*n
        q= collections.deque()

        q.append(start)
        res[start]=1.0
        while q:

            pop = q.popleft()

            for nei,prob in adjList[pop]:
                new = res[pop]*prob

                if new>res[nei]:
                    res[nei]=new
                    q.append(nei)

        return res[end]


import os

ROS_PATH = '/opt/ros/kinetic/lib/python2.7/dist-packages'
CURR_PATH = os.path.dirname(__file__)

SOURCE = {'Auto':1, 'Track':2, 'Manual':3}

DETECT_MODELS = ('Mask RCNN', "YOLO")

DET_CONFIG = dict()
DET_CONFIG['DO_LAMP_DETECTION'] = False

DET_CONFIG['MASK_RCNN_LOG_DIR'] = os.path.join(CURR_PATH, 'data', 'mask_rcnn', 'logs')
DET_CONFIG['CLASSES_PATH'] = os.path.join(CURR_PATH, 'data', 'coco_classes.txt')
DET_CONFIG['MASK_RCNN_MODEL_PATH'] = os.path.join(CURR_PATH, 'data', 'mask_rcnn', 'mask_rcnn_coco.h5')
DET_CONFIG['MASK_RCNN_EGO_MODEL_PATH'] = os.path.join(CURR_PATH, 'data', 'mask_rcnn_ego', 'mask_rcnn_apollo.h5')
DET_CONFIG['CLASSES_PATH_EGO'] = os.path.join(CURR_PATH, 'data', 'apollo_classes.txt')

DET_CONFIG['MASK_RCNN_SCORE_THRESHOLD'] = 0.7

DET_CONFIG['YOLO_MODEL_PATH'] = os.path.join(CURR_PATH, 'data','yolo', 'yolo4_weights.h5')
DET_CONFIG['YOLO_ANCHORS_PATH'] = os.path.join(CURR_PATH, 'data','yolo', 'yolo4_anchors.txt')
DET_CONFIG['YOLO_CLASSES_PATH'] = os.path.join(CURR_PATH, 'data', 'coco_classes.txt')
DET_CONFIG['YOLO_SCORE_THRESHOLD'] = 0.6

DET_CONFIG['DEVICE'] = '/GPU:0'#/CPU:0'
DET_CONFIG['NMS_RATIO'] = 0.8

#Track
TRACK_CONFIG = dict()
TRACK_CONFIG['TRACK_MODE'] = 'DEEP_SORT'
TRACK_CONFIG['REID_MODEL'] = os.path.join(CURR_PATH, 'data', 'vehicle_reid', 'vehicle_reid_model.pb')#used by deep_sort
TRACK_CONFIG['DEVICE'] = '/CPU:0'

#Lane
LANE_CONFIG = dict()
LANE_CONFIG['MODEL'] = os.path.join(CURR_PATH, 'data', 'lane', 'model_culane.pkl')
LANE_CONFIG['DEVICE'] = 'cuda:0'

#depth
DEPTH_CONFIG = dict()
DEPTH_CONFIG['MODEL'] = os.path.join(CURR_PATH, 'data', 'depth')
DEPTH_CONFIG['DEVICE'] = 'cuda:0'


import json

import requests
import tornado.web

from service.db import User, Comp, CompStatus
from service.redis import new_login_session, get_login_openid
from service.utils import WeChat

class Result:
    @staticmethod
    def Success():
        return json.dumps({"result": "success"})

    @staticmethod
    def Failed(code):
        return json.dumps({"result": "failed",
                           "reason": code})

    @staticmethod
    def Redirect(route):
        return json.dumps({"result": "redirect",
                           "router": route})

class BaseHandler(tornado.web.RequestHandler):
    def get_current_user(self) -> User:
        openid = None
        if 'Authorization' in self.request.headers:
            openid = get_login_openid(self.request.headers['Authorization'])
        return None if openid is None else User.get_user(openid)

# Tencent framework is piece of shit so that maintenance a session myself
def authenticated(func):
    def inner(self, *args, **kwargs):
        if self.current_user:
            print(f"Access: {self.current_user.open_id}")
            func(self, *args, **kwargs)
        else:
            self.set_status(403)
            self.write("Illegal access")

    return inner

class TestHandler(BaseHandler):
    def get(self):
        self.write(Comp.get_comp(1))

    def post(self):
        pass

    def put(self):
        comp = Comp(status=CompStatus.open)
        comp.new_comp()
        self.set_status(204)

class AuthHandler(BaseHandler):
    def get(self):
        if self.current_user is not None:
            # Already logged && session still alive
            self.set_status(204)
            return
        user_info = WeChat.get_user_info(js_code=self.request.arguments['code'][0].decode())
        user_uuid = new_login_session(openid=user_info['openid'], session_key=user_info['session_key'])
        self.set_header('Authorization', user_uuid)
        print(f"Login: {user_uuid} {user_info['openid']}")
        if User.get_user(user_info['openid']):
            pass
        else:
            User.new_user(user_info['openid'])
            self.write(Result.Redirect(""))
        self.set_status(200)

class UserHandler(BaseHandler):
    @authenticated
    def get(self):
        user: User = self.current_user
        self.write(json.dumps(user.info_dict))

    @authenticated
    def post(self):
        user: User = self.current_user
        try:
            update_data = json.loads(self.request.body)
            user.update_user_info(update_data)
            self.set_status(200)
            self.write(Result.Success())
        except Exception:
            self.set_status(400)
            self.write(Result.Failed(400))

class CompetitionsHandler(BaseHandler):
    def get(self):
        args = self.request.arguments
        filters = json.loads(self.request.body) if self.request.body else None
        tags = args['tags'] if 'tags' in args else []

        def date_filter():
            return lambda: filters['date_to'] > Comp.time_begin > filters['date_from']

        require_page = args['page'][0].decode() if 'page' in args else 0
        self.write(json.dumps(Comp.get_comps(comp_filters=[], tags=tags, page=require_page)))

class CompetitionHandler(BaseHandler):
    def get(self):
        args = self.request.arguments
        if "id" in args:
            res = Comp.get_comp(comp_id=args["id"])
            self.write(json.dumps(res))
            self.set_status(200)
        else:
            self.write(Result.Failed("Argument Illegal."))
            self.set_status(400)

    def put(self):
        pass

    def post(self):
        pass

class ProxyHandler(BaseHandler):
    def get(self):
        link = self.request.arguments['link'][0].decode()
        res = requests.get(link)
        self.write(res.content)


import os
import warnings
import tempfile

import pytest

from lightkurve.utils import LightkurveDeprecationWarning, LightkurveError
from lightkurve import (
    PACKAGEDIR,
    KeplerTargetPixelFile,
    TessTargetPixelFile,
    LightCurve,
)
from lightkurve.io import read

from .. import TESTDATA

def test_read():
    # define paths to k2 and tess data
    k2_path = os.path.join(TESTDATA, "test-tpf-star.fits")
    tess_path = os.path.join(TESTDATA, "tess25155310-s01-first-cadences.fits.gz")
    # Ensure files are read in as the correct object
    k2tpf = read(k2_path)
    assert isinstance(k2tpf, KeplerTargetPixelFile)
    tesstpf = read(tess_path)
    assert isinstance(tesstpf, TessTargetPixelFile)
    # Open should fail if the filetype is not recognized
    try:
        read(os.path.join(PACKAGEDIR, "data", "lightkurve.mplstyle"))
    except LightkurveError:
        pass
    # Can you instantiate with a path?
    assert isinstance(KeplerTargetPixelFile(k2_path), KeplerTargetPixelFile)
    assert isinstance(TessTargetPixelFile(tess_path), TessTargetPixelFile)
    # Can open take a quality_bitmask argument?
    assert read(k2_path, quality_bitmask="hard").quality_bitmask == "hard"

def test_open():
    """Does the deprecated `open` function still work?"""
    from lightkurve.io import open

    with warnings.catch_warnings():  # lk.open is deprecated
        warnings.simplefilter("ignore", LightkurveDeprecationWarning)
        # define paths to k2 and tess data
        k2_path = os.path.join(TESTDATA, "test-tpf-star.fits")
        tess_path = os.path.join(TESTDATA, "tess25155310-s01-first-cadences.fits.gz")
        # Ensure files are read in as the correct object
        k2tpf = open(k2_path)
        assert isinstance(k2tpf, KeplerTargetPixelFile)
        tesstpf = open(tess_path)
        assert isinstance(tesstpf, TessTargetPixelFile)
        # Open should fail if the filetype is not recognized
        try:
            open(os.path.join(PACKAGEDIR, "data", "lightkurve.mplstyle"))
        except LightkurveError:
            pass
        # Can you instantiate with a path?
        assert isinstance(KeplerTargetPixelFile(k2_path), KeplerTargetPixelFile)
        assert isinstance(TessTargetPixelFile(tess_path), TessTargetPixelFile)
        # Can open take a quality_bitmask argument?
        assert open(k2_path, quality_bitmask="hard").quality_bitmask == "hard"

def test_filenotfound():
    """Regression test for #540; ensure lk.read() yields `FileNotFoundError`."""
    filename = "some/path/DOESNOTEXIST"
    with pytest.raises(FileNotFoundError) as excinfo:
        read(filename)
    # ensure the filepath is in the exception
    assert filename in str(excinfo.value)

@pytest.mark.filterwarnings("ignore:.*been truncated.*")  # ignore AstropyUserWarning: File may have been truncated
def test_file_corrupted():
    """Regression test for #1184; ensure lk.read() yields an error that includes the filename."""
    # fits source: mast:TESS/product/tess2018206045859-s0001-0000000261136679-0120-s_lc.fits
    filename_lc_pimen_corrupted = os.path.join(TESTDATA, "test-lc-tess-pimen-100-cadences-corrupted.fits")
    with pytest.raises(BaseException) as excinfo:
        read(filename_lc_pimen_corrupted)
    # ensure the filepath is in the exception
    assert filename_lc_pimen_corrupted in str(excinfo.value)

def test_basic_ascii_io():
    """Verify we do not break the basic ascii i/o functionality provided by AstroPy Table."""
    # Part I: Can we read a LightCurve from a CSV file?
    csvfile = tempfile.NamedTemporaryFile(
        delete=False
    )  # using delete=False to make tests pass on Windows
    try:
        csvfile.write(b"time,flux,flux_err,color\n1,2,3,red\n4,5,6,green\n7,8,9,blue")
        csvfile.flush()
        lc_csv = LightCurve.read(csvfile.name, format="ascii.csv")
        assert lc_csv.time[0].value == 1
        assert lc_csv.flux[1] == 5
        assert lc_csv.color[2] == "blue"
    finally:
        csvfile.close()
        os.remove(csvfile.name)

    # Part II: can we write the light curve to a tab-separated ascii file, and read it back in?
    tabfile = tempfile.NamedTemporaryFile(delete=False)
    try:
        lc_csv.write(tabfile.name, format="ascii.tab", overwrite=True)
        lc_rst = LightCurve.read(tabfile.name, format="ascii.tab")
        assert lc_rst.color[2] == "blue"
        assert (lc_csv == lc_rst).all()
    finally:
        tabfile.close()
        os.remove(tabfile.name)


import numpy as np
import tensorflow as tf

# this file defines classes for data io
class TrainingDataIO:
    def __init__(self, feature_filename, label_filename, total_trainig_samples, feature_length, label_length):
        print("Construct the data IO class for training!\n")
        self.fin_label = open(label_filename, "rb")
        self.fin_feature = open(feature_filename, "rb")
        self.total_trainig_samples = total_trainig_samples
        self.feature_length = feature_length
        self.label_length = label_length

    def __del__(self):
        print("Delete the data IO class!\n")
        self.fin_feature.close()
        self.fin_label.close()

    def load_next_mini_batch(self, mini_batch_size, factor_of_start_pos=1):
        # the function is to load the next batch where the datas in the batch are from a continuous memory block
        # the start position for reading data must be a multiple of factor_of_start_pos
        remain_samples = mini_batch_size
        sample_id = np.random.randint(self.total_trainig_samples)   # output a single value which is less than total_trainig_samples
        features = np.zeros((0))
        labels = np.zeros((0))
        if mini_batch_size > self.total_trainig_samples:
            print("Mini batch size should not be larger than total sample size!\n")
        self.fin_feature.seek((self.feature_length * 4) * (sample_id//factor_of_start_pos*factor_of_start_pos), 0)  # float32 = 4 bytes = 32 bits
        self.fin_label.seek((self.label_length * 4) * (sample_id//factor_of_start_pos*factor_of_start_pos), 0)

        while 1:
            new_feature = np.fromfile(self.fin_feature, np.float32, self.feature_length * remain_samples)
            new_label = np.fromfile(self.fin_label, np.float32, self.label_length * remain_samples)
            features = np.concatenate((features, new_feature))
            labels = np.concatenate((labels, new_label))
            remain_samples -= len(new_feature) // self.feature_length
            if remain_samples == 0:
                break
            self.fin_feature.seek(0, 0)
            self.fin_label.seek(0, 0)
        features = features.reshape((mini_batch_size, self.feature_length))
        labels = labels.reshape((mini_batch_size, self.label_length))
        return features, labels

class TestDataIO:
    def __init__(self, feature_filename, label_filename, test_sample_num, feature_length, label_length):
        self.fin_label = open(label_filename, "rb")
        self.fin_feature = open(feature_filename, "rb")
        self.test_sample_num = test_sample_num
        self.feature_length = feature_length
        self.label_length = label_length
        self.all_features = np.zeros(0)
        self.all_labels = np.zeros(0)
        self.data_position = 0

    def __del__(self):
        self.fin_feature.close()
        self.fin_label.close()

    def seek_file_to_zero(self):  # reset the file pointer to the start of the file
        self.fin_feature.seek(0, 0)
        self.fin_label.seek(0, 0)

    def load_batch_for_test(self, batch_size):
        if batch_size > self.test_sample_num:
            print("Batch size should not be larger than total sample size!\n")
        if np.size(self.all_features) == 0:
            self.all_features = np.fromfile(self.fin_feature, np.float32, self.feature_length * self.test_sample_num)
            self.all_labels = np.fromfile(self.fin_label, np.float32, self.label_length * self.test_sample_num)
            self.all_features = np.reshape(self.all_features, [self.test_sample_num, self.feature_length])
            self.all_labels = np.reshape(self.all_labels, [self.test_sample_num, self.label_length])

        features = self.all_features[self.data_position:(self.data_position + batch_size), :]
        labels = self.all_labels[self.data_position:(self.data_position + batch_size), :]
        self.data_position += batch_size
        if self.data_position >= self.test_sample_num:
            self.data_position = 0
        return features, labels

class NoiseIO:
    def __init__(self, blk_len, read_from_file, noise_file, cov_1_2_mat_file_gen_noise, rng_seed=None):
        self.read_from_file = read_from_file
        self.blk_len = blk_len
        self.rng_seed = rng_seed
        if read_from_file:
            self.fin_noise = open(noise_file, 'rb')
        else:
            self.rng = np.random.RandomState(rng_seed)
            fin_cov_file = open(cov_1_2_mat_file_gen_noise, 'rb')
            cov_1_2_mat = np.fromfile(fin_cov_file, np.float32, blk_len*blk_len)
            cov_1_2_mat = np.reshape(cov_1_2_mat, [blk_len, blk_len])
            fin_cov_file.close()
            ## output parts of the correlation function for check
            cov_func = np.matmul(cov_1_2_mat, cov_1_2_mat)
            print('Correlation function of channel noise: ')
            print(cov_func[0,0:10])
            self.awgn_noise = tf.placeholder(dtype=tf.float32, shape=[None, blk_len])
            self.noise_tf = tf.matmul(self.awgn_noise, cov_1_2_mat)
            self.sess = tf.Session()

    def __del__(self):
        if self.read_from_file:
            self.fin_noise.close()
        else:
            self.sess.close()

    def reset_noise_generator(self): # this function resets the file pointer or the rng generator to generate the same noise data
        if self.read_from_file:
            self.fin_noise.seek(0, 0)
        else:
            self.rng = np.random.RandomState(self.rng_seed)

    def generate_noise(self, batch_size):
        if self.read_from_file:
            noise = np.fromfile(self.fin_noise, np.float32, batch_size * self.blk_len)
            noise = np.reshape(noise, [batch_size, self.blk_len])
        else:
            noise_awgn = self.rng.randn(batch_size, self.blk_len)
            noise_awgn = noise_awgn.astype(np.float32)
            noise = self.sess.run(self.noise_tf, feed_dict={self.awgn_noise: noise_awgn})

        return noise


import torch
from .mylib import *
from dataset import *
import numpy as np
import torch.nn as nn
import word2vec
SEED = 1111
random.seed(SEED)
np.random.seed(SEED)
torch.manual_seed(SEED)
torch.cuda.manual_seed(SEED)
torch.backends.cudnn.deterministic = True
device ='cuda'
class Font_Generator:
    def __init__(self, G_model, ID, device, data_pararell=True, imp2font=False):
        super().__init__()
        self.ID = ID
        self.G_model = G_model.to(device)
        if data_pararell:
            self.G_model = nn.DataParallel(self.G_model)
        self.device = device
        self.word2vec = word2vec.word2vec()
        self.alpha2num = lambda c: ord(c) -ord('A')
        self.imp2font = imp2font
        if self.imp2font:
            self.z_img = torch.normal(mean=0.5, std=0.2, size=(1000, 300))
        else:
            self.z_img = torch.randn(1000, 256 * 4 * 4)
        self.z_cond = torch.randn(1000, 100)
    def generate_from_impression(self, generate_num, impression_word, alphabet="ABCHERONS", shuffle=False):
        alphabet = list(alphabet)
        alpha_num = list(map(self.alpha2num, alphabet))
        char_num = len(alpha_num)
        char_class = torch.eye(26)[torch.tensor(alpha_num)].repeat(generate_num, 1)
        if shuffle:
            idx = torch.randperm(len(self.z_img))[:generate_num]
        else:
            idx = torch.tensor(list(range(len(self.z_img))))[:generate_num]
        try:
            label = [[self.ID[token] for token in impression_word]]
            label = Multilabel_OneHot(label, len(self.ID), normalize=True)
            emb=True
        except:
            label = [torch.tensor(self.word2vec[ii]) for ii in impression_word]
            label = torch.stack(label)
            label = label.sum(0)
            label = label/(torch.sqrt((label ** 2).sum(0))+ 1e-7)
            label = label.unsqueeze(0)
            emb = False
        label = torch.tensor(label).repeat(char_num * generate_num, 1).to(self.device)
        print(label.shape)
        if self.imp2font:
            noise = self.z_img[idx]
            noise = tile(noise, 0, char_num).to(self.device)
        else:
            z_img = self.z_img[idx]
            z_cond = self.z_cond[idx]
            z_img = tile(z_img, 0, char_num).to(self.device)
            z_cond = tile(z_cond, 0, char_num).to(self.device)
            noise = (z_img, z_cond)
        with torch.no_grad():
            samples = self.G_model(noise, char_class, label, 5, emb=emb)[0]
            samples = samples.data.cpu()
            samples = samples.reshape(-1, char_num, samples.size(2), samples.size(3))
        return samples
    def interpolation_noise(self, word, c=5, alphabet="ABCHERONS"):
        alphabet = list(alphabet)
        alpha_num = list(map(self.alpha2num, alphabet))
        char_num = len(alpha_num)
        char_class = torch.eye(26)[torch.tensor(alpha_num)].repeat(c, 1)
        z_img1 = torch.randn(1, 256 * 4 * 4)
        z_img2= torch.randn(1, 256 * 4 * 4)
        alpha_list = np.linspace(0, 1, c)
        z_img = torch.cat([(1 - alpha) * z_img1 + alpha * z_img2 for alpha in alpha_list])
        z_cond1 = torch.randn(1, 100)
        z_cond2 = torch.randn(1, 100)
        z_cond = torch.cat([(1 - alpha) * z_cond1 + alpha * z_cond2 for alpha in alpha_list])
        z_img = tile(z_img, 0, char_num).to(self.device)
        z_cond = tile(z_cond, 0, char_num).to(self.device)
        label = [[self.ID[token] for token in word]]
        label = Multilabel_OneHot(label, len(self.ID), normalize=True)
        label = torch.tensor(label)
        condition = tile(label, 0, char_num * c).to(self.device)
        noise = (z_img, z_cond)
        print(z_img.shape, z_cond.shape)
        with torch.no_grad():
            samples = self.G_model(noise, char_class, condition, 5)[0]
            samples = samples.data.cpu()
            samples = samples.reshape(-1, char_num, samples.size(2), samples.size(3))
        return samples

    def interpolation_impression(self, word1, word2, c=6, alphabet="ABCHERONS"):
        alphabet = list(alphabet)
        alpha_num = list(map(self.alpha2num, alphabet))
        char_num = len(alpha_num)
        char_class = torch.eye(26)[torch.tensor(alpha_num)].repeat(c, 1)
        z_img = torch.randn(1, 256 * 4 * 4)
        z_cond = torch.randn(1, 100)
        z_img = tile(z_img, 0, char_num * c).to(self.device)
        z_cond = tile(z_cond, 0, char_num * c).to(self.device)
        label1 = [[self.ID[token] for token in word1]]
        label1 = Multilabel_OneHot(label1, len(self.ID), normalize=True)
        label1 = torch.tensor(label1)
        label2 = [[self.ID[token] for token in word2]]
        label2 = Multilabel_OneHot(label2, len(self.ID), normalize=True)
        label2 = torch.tensor(label2)
        alpha_list = np.linspace(0, 1, c)
        condition = torch.cat([(1 - alpha) * label1 + alpha * label2 for alpha in alpha_list])
        condition = tile(condition, 0, char_num).to(self.device)
        noise = (z_img, z_cond)
        with torch.no_grad():
            samples = self.G_model(noise, char_class, condition, 5)[0]
            samples = samples.data.cpu()
            samples = samples.reshape(-1, char_num, samples.size(2), samples.size(3))
        return samples

    def generate_random(self, c=100, alphabet="A"):
        alphabet = list(alphabet)
        alpha_num = list(map(self.alpha2num, alphabet))
        char_num = len(alpha_num)
        char_class = torch.eye(26)[torch.tensor(alpha_num)].repeat(c, 1)
        z_img = self.z_img[:c]
        z_cond = self.z_cond[:c]
        label = torch.zeros(c, 1430)
        idx = torch.randint(0, 1430, (c,))
        for i1, i2 in zip(range(len(z_img)), idx):
            label[i1, i2] = 1
        print(label)
        label = label.to(self.device)
        char_class = char_class.to(self.device)
        z_img = z_img.to(self.device)
        z_cond = z_cond.to(self.device)
        noise = (z_img, z_cond)
        print(z_img.shape, z_cond.shape, char_class.shape, label.shape)
        with torch.no_grad():
            samples = self.G_model(noise, char_class, label, 5)[0]
            samples = samples.data.cpu()
            samples = samples.reshape(-1, char_num, samples.size(2), samples.size(3))
        return samples

    def generate_from_changed_ratio(self, label, c=100, alphabet="A", ratio=[1, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1], shuffle=False, co_matrix=None):
        alphabet = list(alphabet)
        alpha_num = list(map(self.alpha2num, alphabet))
        char_num = len(alpha_num)
        char_class = torch.eye(26)[torch.tensor(alpha_num)].repeat(c, 1)
        char_class = char_class.to(self.device)
        if shuffle:
            idx = torch.randperm(len(self.z_img))[:c]
        else:
            idx = torch.tensor(range(len(self.z_img)))[:c]
        if self.imp2font:
            noise = self.z_img[idx]
            noise = tile(noise, 0, char_num).to(self.device)
        else:
            z_img = self.z_img[idx]
            z_cond = self.z_cond[idx]
            z_img = tile(z_img, 0, char_num).to(self.device)
            z_cond = tile(z_cond, 0, char_num).to(self.device)
            noise = (z_img, z_cond)
        embed_label = torch.tensor([self.ID[token] for token in label])-1
        if co_matrix!=None:
            imp_num = co_matrix[embed_label, embed_label]
            max_idx = np.argsort(-imp_num)[0]
            co_occurence_score = co_matrix[embed_label[max_idx], embed_label]
            idx = np.argsort(co_occurence_score)
        else:
            idx = torch.randperm(len(embed_label))
        samples_img = []
        samples_label = []
        for r in ratio:
            length = int(len(label)*r)
            label_ = [label[i] for i in idx[:length]]
            samples_label.append(label_)
            label_ = [[self.ID[token] for token in label_]]
            label_ = Multilabel_OneHot(label_, len(self.ID), normalize=True)
            label_ = torch.tensor(label_).repeat(char_num * c, 1).to(self.device)
            label_ = label_.to(self.device)
            with torch.no_grad():
                samples = self.G_model(noise, char_class, label_, 5)[0]
                samples = samples.data.cpu()
                samples = samples.reshape(-1, char_num, samples.size(2), samples.size(3))
            samples_img.append(samples)

        return samples_img, samples_label


# PyQT é um toolkit desenvolvido em C++ utilizando vários programas para
# criação de apicativos GUI (Interface Gráfica). Também inclui diversas
# funcionalidade, como: acesso a base de dados, threads, comunicação de rede,
# etc...

# pip install PyQt5

import sys
from PyQt5.QtWidgets import QMainWindow, QApplication, QPushButton
from PyQt5.QtWidgets import QWidget, QGridLayout

class App(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.cw = QWidget()
        self.grid = QGridLayout(self.cw)

        self.btn = QPushButton('Texto botão')
        self.btn.setStyleSheet('font-size: 40px')
        self.grid.addWidget(self.btn, 0, 0, 1, 1)
        # grid = linha, coluna, qtd linhas, qtd colunas
        self.btn.clicked.connect(lambda: print('Ola mundo!'))
        self.setCentralWidget(self.cw)

if __name__ == '__main__':
    qt = QApplication(sys.argv)
    app = App()
    app.show()
    qt.exec()


import datetime
import time

from selenium.common.exceptions import NoSuchElementException, InvalidElementStateException
from selenium.webdriver.common.by import By

from DataBase.data import (add_new_profile, get_column,
                           check_ok, subscribe_ok,
                           wait_list_unsub, get_to_unsubscribe,
                           delete_subscript)
from global_set import settings
from global_set.global_set import get_browser
from delay_time import daley_press

browser = get_browser()

class ObjectMixin:
    @staticmethod
    def get_xpath_object(path_html):
        """
        Принимает путь XPath элемента HTML
        Возвращает объект(ссылку)
        :param path_html:
        """
        button_or_form = browser.find_element(By.XPATH, path_html)

        if button_or_form:
            return button_or_form

        else:
            return 'Ошибка Xpath'

class LoginUser(ObjectMixin):
    def log_in(self):
        """
        Функция авторизации
        :param:
        :return:    1 if crash
                    0 if success connect
        """
        form_login = '//*[@id="loginForm"]/div/div[1]/div/label/input'
        form_pass = '//*[@id="loginForm"]/div/div[2]/div/label/input'
        button_login = '//*[@id="loginForm"]/div/div[3]'

        next_page_url = 'https://www.instagram.com/accounts/onetap/?next=%2F'

        count_auth = 1
        time.sleep(2)

        while browser.current_url != next_page_url:
            print('Попытка авторизации: ', count_auth)
            count_auth += 1
            try:
                time.sleep(settings.wait_open_new_page)

                self.get_xpath_object(form_login).clear()
                self.get_xpath_object(form_pass).clear()

                time.sleep(2)

                self.get_xpath_object(form_login).send_keys(settings.user_name)
                self.get_xpath_object(form_pass).send_keys(settings.user_pass)
                self.get_xpath_object(button_login).click()

                time.sleep(settings.wait_open_new_page)

            except NoSuchElementException:
                pass

        print('Авторизация прошла успешно!')
        return 0

class Scrolling(ObjectMixin):
    def __scroll(self):
        """
        Функция пролистывания списка подписчиков
        :return:
        """

        block_to_scroll = browser.find_element(By.CLASS_NAME, 'isgrP')
        script = "arguments[0].scrollTop = arguments[0].scrollHeight"

        end_scroll = True
        try:
            time.sleep(2)

            browser.execute_script(script, block_to_scroll)
            return 0

        except NoSuchElementException:
            if not end_scroll:
                return 1
            else:
                time.sleep(5)
                self.__scroll()
            return 0.5

    def wait_scroll(self, number_to_scroll, button):
        """
        Нажимаем на кнопку подписчиков и листаем на установленное количесво раз
        :return:
        """
        try:
            time.sleep(2)
            self.get_xpath_object(button).click()
            time.sleep(2)

            number_scroll_local = 0
            count = 0
            time_wait = 0
            end = True
            while end:
                number_scroll_local += 1
                count += int(self.__scroll())

                if time_wait == 10:
                    time.sleep(3)

                if count == 3 or number_scroll_local == number_to_scroll:
                    break

                time_wait += 1
        except:
            return 0

class UserInfo(ObjectMixin):
    def __init__(self, url_user):
        self.url_user = url_user

    def get_info_user(self):
        """
        Функция возвращает информацию со страницы профиля
        :return data_return = [posts_user, name_user, num_followers[0], number_sub, number_posts]
                            [Список постов, ник, кол-во подписчиков, кол-во подписок, кол-во постов]
        """

        browser.get(self.url_user)
        time.sleep(4)
        try:
            block_name = '//*[@id="react-root"]/section/main/div/header/section/div[1]/h2'
            block_followers = '//*[@id="react-root"]/section/main/div/header/section/ul/li[2]/a/span'

            name_user = self.get_xpath_object(block_name).text
            no_filter_followers = self.get_xpath_object(block_followers).text

            # Проверка на наличя слова тыс в кол-во подписчиков
            num_followers = []
            if 'т' in no_filter_followers:
                num_followers.append(no_filter_followers[0:3] + '000')

            elif ' ' in no_filter_followers:
                x = no_filter_followers.split()
                res = x[0] + x[1]
                num_followers.append(res)

            else:
                num_followers.append(no_filter_followers)

            try:
                number_sub = self.get_xpath_object('//*[@id="react-root"]/section/main/'
                                                   'div/header/section/ul/li[3]/span/span').text
            except:
                number_sub = self.get_xpath_object('//*[@id="react-root"]/section/main/'
                                                   'div/header/section/ul/li[3]/a/span').text
            try:
                number_posts = self.get_xpath_object('//*[@id="react-root"]/section/main/'
                                                     'div/header/section/ul/li[1]/span/span').text
            except:
                number_posts = self.get_xpath_object('//*[@id="react-root"]/section/main/'
                                                     'div/header/section/ul/li[2]/a/span').text

            # Фильтрация кол-во подписок
            if ' ' in number_sub:
                number = number_sub.split()
                number_subscript = number[0] + number[1]
            else:
                number_subscript = number_sub

            posts_user = self.__get_user_posts()
            data_return = {'posts_user': str(posts_user),
                           'name_user': str(name_user),
                           'num_followers': str(num_followers[0]),
                           'number_subscript': str(number_subscript),
                           'number_posts': str(number_posts),
                           'url_user': str(self.url_user)}

            return data_return
        except:
            return 404

    @staticmethod
    def __get_user_posts():
        # Получение списка постов(ссылки)
        links = browser.find_elements(By.CLASS_NAME, 'v1Nh3.kIKUG._bz0w')
        posts_user = []
        for el in links:
            post = el.find_element(By.TAG_NAME, 'a').get_attribute('href')
            posts_user.append(post)
        return posts_user

    def __get_followers_links(self):
        """
        Получение списка ссылок на подписчиков пользователя
        :return: all_followers
        """

        # Находим поле с подписчиками
        followers = browser.find_elements(By.CSS_SELECTOR, 'body>div.RnEpo.Yx5HN>div>div>div.isgrP>ul>div>li')

        all_followers = []
        for follower in followers:
            post = follower.find_element(By.TAG_NAME, 'a').get_attribute('href')
            all_followers.append(post)
        self.get_xpath_object('/html/body/div[6]/div/div/div[1]/div/div[2]/button').click()
        return str(all_followers)

    def __get_subscript_links(self):
        """
        Получение списка ссылок на подписки пользователя
        :return: all_sub
        """
        try:
            number_sub = self.get_xpath_object('//*[@id="react-root"]/section/main/div/header/'
                                               'section/ul/li[3]/span/span').text

        except:
            number_sub = self.get_xpath_object('//*[@id="react-root"]/section/main/div/header/'
                                               'section/ul/li[3]/a/span').text

        if number_sub != '0':
            # Находим поле с подписками
            subscript = browser.find_elements(By.CSS_SELECTOR, 'body>div.RnEpo.Yx5HN>div>div>div.isgrP>ul>div')

            all_subscript = []
            for sub in subscript:
                post = sub.find_element(By.TAG_NAME, 'a').get_attribute('href')
                all_subscript.append(post)

            self.get_xpath_object('/html/body/div[6]/div/div/div[1]/div/div[2]/button').click()

            return str(all_subscript)
        else:
            pass

    def get_dict_profile(self):
        scroll = Scrolling()
        xpath_follower_window = '//*[@id="react-root"]/section/main/div/header/section/ul/li[2]/a'
        xpath_subscript_window = '//*[@id="react-root"]/section/main/div/header/section/ul/li[3]/a'

        try:
            profile_info = self.get_info_user()

            scroll.wait_scroll(settings.number_scroll_followers, xpath_follower_window)
            all_followers_user = self.__get_followers_links()

            scroll.wait_scroll(settings.number_scroll_subscripts, xpath_subscript_window)
            all_subscripts_user = self.__get_subscript_links()

            # Чистка 1 000 от пробела (подписчики)
            if profile_info == 404:
                return 404
            clear_number = profile_info['number_posts']
            if ' ' in clear_number:
                clear_number = profile_info['number_posts'].split()
                clear_number = int(clear_number[0] + clear_number[1])

            data = {'user_name': profile_info['name_user'],
                    'user_link': profile_info['url_user'],
                    'user_sub': all_subscripts_user,
                    'user_followers': all_followers_user,
                    'user_posts': profile_info['posts_user'],
                    'number_sub': profile_info['number_subscript'],
                    'number_followers': profile_info['num_followers'],
                    'number_posts': clear_number,
                    }

            return data
        except InvalidElementStateException:
            return 404

class NewSubscript(ObjectMixin):
    """
    Удаление/Подписка на пользователей
    """

    def new_subscript(self):
        """
        Подписка на пользователей
        :return:
        """
        data = get_column(name_column='user_link', sub_max=1_000, follower_max=1_000)
        for user_link in data:
            print(user_link)

            try:
                browser.get(user_link[1])
                self.__subscript_button()
                subscribe_ok(user_link[0])
                today = datetime.date.today()
                tomorrow = today + datetime.timedelta(days=1)
                data = {'name_user': user_link[1],
                        'date_sub': datetime.date.today(),
                        'date_unsub': tomorrow}

                wait_list_unsub(data)
                daley_press('ожидание после подписки')
            except:
                pass

    def __subscript_button(self):
        try:
            button_subscript = '//*[@id="react-root"]/section/main' \
                               '/div/header/section/div[1]/div[1]/div/div/div/span/span[1]/button'
            self.get_xpath_object(button_subscript).click()
        except NoSuchElementException:
            pass

    @staticmethod
    def delete_sub():
        response_db = get_to_unsubscribe()
        time.sleep(2)

        # link = ['id', 'url-профиля']
        for link in response_db:
            link = list(link)
            print(link)
            try:
                browser.get(link[1])
                time.sleep(settings.wait_open_new_page)

                button_unsubscribe = '_5f5mN.-fzfL._6VtSN.yZn4P'
                browser.find_element(By.CLASS_NAME, button_unsubscribe).click()
                time.sleep(1)

                button_unsubscribe_ok = 'aOOlW.-Cab_'
                browser.find_element(By.CLASS_NAME, button_unsubscribe_ok).click()

                delete_subscript(link[0])
                wait_list_unsub('Ожидание после отписки')

            except:
                print(link[0], 'не удалось')

class InitProgram:
    list_check_profile = []
    list_links = get_column()

    def __init__(self):
        self.login = LoginUser()
        self.login.log_in()
        self.sub = NewSubscript()

    def check_profiles(self):
        for link in self.list_links:
            for profile in link[1]:
                try:
                    profile_user = UserInfo(profile)
                    profile_user = profile_user.get_dict_profile()
                except:
                    profile_user = 404

                if profile_user == 404:
                    pass
                else:
                    add_new_profile(profile_user)
            self.list_check_profile.append(link[0])
            check_ok(link[0])

    def new_subs(self):
        self.sub.new_subscript()

init = InitProgram()
init.sub.new_subscript()
time.sleep(500)


'''
2. Написать два алгоритма нахождения i-го по счёту простого числа. Функция нахождения простого числа должна принимать на вход натуральное и возвращать соответствующее простое число. Проанализировать скорость и сложность алгоритмов.
Первый — с помощью алгоритма «Решето Эратосфена».
Примечание. Алгоритм «Решето Эратосфена» разбирался на одном из прошлых уроков. Используйте этот код и попробуйте его улучшить/оптимизировать под задачу.
Второй — без использования «Решета Эратосфена».
'''
import cProfile

def sieve(num,pos):
    prime = [True for i in range(num + 1)]
    p = 2
    num_list = []
    while (p * p <= num):
        if (prime[p] == True):
            for i in range(p * 2, num + 1, p):
                prime[i] = False
        p += 1
    prime[0] = False
    prime[1] = False
    for p in range(num + 1):
        if prime[p]:
            num_list.append(p)
    return num_list[pos-1]

# 100 loops, best of 5: 4.56 usec per loop "les_4_task_2.sieve(10,3)"
# 100 loops, best of 5: 18.9 usec per loop "les_4_task_2.sieve(100,10)"
# 100 loops, best of 5: 17.4 usec per loop "les_4_task_2.sieve(100,25)"
# 100 loops, best of 5: 231 usec per loop "les_4_task_2.sieve(1000,100)"
# 100 loops, best of 5: 1.91 msec per loop "les_4_task_2.sieve(10000,200)"

# cProfile.run('sieve(10,3)')
# 1    0.000    0.000    0.000    0.000 les_4_task_2.py:10(sieve)
# cProfile.run('sieve(100,10)')
# 1    0.000    0.000    0.000    0.000 les_4_task_2.py:10(sieve)
# cProfile.run('sieve(100,25)')
# 1    0.000    0.000    0.000    0.000 les_4_task_2.py:10(sieve)
# cProfile.run('sieve(1000,100)')
# 1    0.000    0.000    0.000    0.000 les_4_task_2.py:10(sieve)
# cProfile.run('sieve(10000,200)')
# 1    0.005    0.005    0.007    0.007 les_4_task_2.py:10(sieve)
# cProfile.run('sieve(120000,10000)')
# 1    0.053    0.053    0.066    0.066 les_4_task_2.py:10(sieve)

def prime(num):
    count = 1
    number = 1
    prime = [2]

    if num == 1:
        return 2

    while count != num:
        number += 2
        for i in prime:
            if number % i == 0:
                break
        else:
            count += 1
            prime.append(number)

    return number

# 100 loops, best of 5: 811 nsec per loop "les_4_task_2.sieve(3)"
# 100 loops, best of 5: 14 usec per loop "les_4_task_2.sieve(10)"
# 100 loops, best of 5: 26.8 usec per loop "les_4_task_2.sieve(25)"
# 100 loops, best of 5: 549 usec per loop "les_4_task_2.sieve(100)"
# 100 loops, best of 5: 1.23 msec per loop "les_4_task_2.sieve(200)"

# cProfile.run('prime(3)')
# 1    0.000    0.000    0.000    0.000 les_4_task_2.py:44(prime)
# cProfile.run('prime(10)')
# 1    0.000    0.000    0.000    0.000 les_4_task_2.py:44(prime)
# cProfile.run('prime(25)')
# 1    0.000    0.000    0.000    0.000 les_4_task_2.py:44(prime)
# cProfile.run('prime(100)')
# 1    0.001    0.001    0.001    0.001 les_4_task_2.py:44(prime)
# cProfile.run('prime(200)')
# 1    0.003    0.003    0.003    0.003 les_4_task_2.py:44(prime)
# cProfile.run('prime(10000)')
# 1    4.966    4.966    4.970    4.970 les_4_task_2.py:44(prime)

# Вывод: сложность обоих алгоритмов O(n**2)
# Для решета Эратосфена требуется первоначальная генерация чисел, что усложняет работу с функцией.
# На значительно больших значениях решето Эратосфена может выигрывать по времени


''' Merger of five PDF's '''

from PyPDF2 import PdfFileMerger

pdfs = ['1.pdf','2.pdf', '3.pdf', '4.pdf', '5.pdf']

merger = PdfFileMerger(strict=False)

for pdf in pdfs:
    merger.append(pdf)

merger.write("OneFile.pdf")
merger.close()


from client import client
from threading import Lock

class clientlist:

	def __init__(self):
		self.list = {}
		self.lock = Lock()

	def get(self, cn):
		with self.lock:
			if self.list.has_key(cn):
				return self.list[cn]
			else:
				c = client()
				self.list[cn] = c
				return c

	def keep(self, cn=None):
		if cn != None:
			self.get(cn).keep = True
		else:
			with self.lock:
				dellist = []
				for i in self.list:
					c = self.list[i]
					if not c.keep:
						dellist.append(i)
					else:
						c.keep = False
				for i in dellist:
					del self.list[i]

	def len(self):
		with self.lock:
			return len(self.list)

	def iterator(self):
		with self.lock:
			for cn in self.list:
				yield (cn, self.list[cn])


from __future__ import print_function
import json
import re
import string
import numpy as np

from pyspark import SparkContext, SparkConf
from pyspark import SQLContext
from pyspark.mllib.feature import Normalizer
from pyspark.mllib.regression import LabeledPoint
from pyspark.mllib.tree import RandomForest, RandomForestModel
from pyspark.mllib.util import MLUtils

def tokenize(text):
    tokens = []
    text = text.encode('ascii', 'ignore')  # to decode
    text = re.sub('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*(),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', '',
                  text)  # to replace url with ''
    text = remove_spl_char_regex.sub(" ", text)  # Remove special characters
    text = text.lower()

    for word in text.split():
       if word not in stopwords \
          and word not in string.punctuation \
          and len(word) > 1 \
          and word != '``':
             tokens.append(word)
    return tokens

def doc2vec(document):
    #100D word vector
    doc_vec = np.zeros(100)
    tot_words = 0

    for word in document:
        try:
            # get word's features
            vec = np.array(lookup_bd.value.get(word)) + 1
            # print(vec)
            # if features exists in pretrained models, add it to vector
            if vec != None:
                doc_vec += vec
                tot_words += 1
        except:
            continue

    vec = doc_vec / float(tot_words)
    return vec

conf = SparkConf().setAppName("sentiment_analysis")
sc = SparkContext(conf=conf)
sc.setLogLevel("WARN")
sqlContext = SQLContext(sc)

#remove stop words, signs and urls
#regex to remove special characters
remove_spl_char_regex = re.compile('[%s]' % re.escape(string.punctuation))
stopwords = [u'rt', u're', u'i', u'me', u'my', u'myself', u'we', u'our', u'ours', u'ourselves', u'you', u'your',
    u'yours', u'yourself', u'yourselves', u'he', u'him', u'his', u'himself', u'she', u'her', u'hers',
    u'herself', u'it', u'its', u'itself', u'they', u'them', u'their', u'theirs', u'themselves', u'what',
    u'which', u'who', u'whom', u'this', u'that', u'these', u'those', u'am', u'is', u'are', u'was', u'were',
    u'be', u'been', u'being', u'have', u'has', u'had', u'having', u'do', u'does', u'did', u'doing', u'a',
    u'an', u'the', u'and', u'but', u'if', u'or', u'because', u'as', u'until', u'while', u'of', u'at', u'by',
    u'for', u'with', u'about', u'against', u'between', u'into', u'through', u'during', u'before', u'after',
    u'above', u'below', u'to', u'from', u'up', u'down', u'in', u'out', u'on', u'off', u'over', u'under',
    u'again', u'further', u'then', u'once', u'here', u'there', u'when', u'where', u'why', u'how', u'all',
    u'any', u'both', u'each', u'few', u'more', u'most', u'other', u'some', u'such', u'no', u'nor', u'not',
    u'only', u'own', u'same', u'so', u'than', u'too', u'very', u's', u't', u'can', u'will', u'just', u'don',
    u'should', u'now']
#import word2VecM model for feature extracting
lookup = sqlContext.read.parquet("/home/word2vecM_simple/data").alias("lookup")
lookup.printSchema()
lookup_bd = sc.broadcast(lookup.rdd.collectAsMap())

#load labeled data for sentiment analysis
with open('tweets.json', 'r') as f:
    rawTrn_data = json.load(f)
    f.close()

trn_data = []
for obj in rawTrn_data['results']:
    token_text = tokenize(obj['text']) #nomalized the tweets text
    tweet_text = doc2vec(token_text) #convert text to vector
    #use LabeledPoint to give each tweets a sentiment label
    trn_data.append(LabeledPoint(obj['polarity'], tweet_text))

trnData = sc.parallelize(trn_data)
#print(trnData)

##################  Here to load test data from Cassandra #########

with open(sys.argv[1]) as f:
    rawTst_data = json.load(f)
    f.close()

tst_data = []
for obj in rawTst_data['text']:
    token_text = tokenize(obj)
    tweet_text = doc2vec(token_text)

tst_dataRDD = sc.parallelize(tst_data)

#train classificatin model
model = RandomForest.trainClassifier(trnData, numClasses=3, categoricalFeaturesInfo = {},numTrees = 3,featureSubsetStrategy = 'auto',
impurity = 'gini', maxDepth = 4, maxBins = 32)

#get prediction of each tweets
predictions = model.predict(tst_dataRDD.map(lambda x: x.features))


#MEGA CASE STUDY:
""" in the CC app case, identify the frauds using an SOM, and predict the
probability of being a true positive using an ANN"""

#code from SOM section:
#Self Organizing Maps:
import os
os.chdir('C:\\Users\\Ralph\\Desktop\\Courses\\DeepLearning\\Deep_Learning_A_Z\\Deep_Learning_A_Z\\Volume 2 - Unsupervised Deep Learning\\Part 4 - Self Organizing Maps (SOM)')
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
dataset = pd.read_csv('Credit_Card_Applications.csv')
X = dataset.iloc[:, :-1].values
y = dataset.iloc[:,-1].values

#Feature Scaling:
#we are scaling to y (class). no need to scale this as well, just X.
from sklearn.preprocessing import MinMaxScaler
sc = MinMaxScaler(feature_range = (0,1))
X = sc.fit_transform(X)

#Train the SOM:
#import minisom
from minisom import MiniSom
som = MiniSom(x = 10,
              y = 10,
              input_len = 15,
              sigma = 1.0,
              learning_rate = 0.5,
              random_seed= 1)

#initialize weights:
som.random_weights_init(X)
som.train_random(data=X, num_iteration=100)

#VIZ:
from pylab import bone, pcolor, colorbar, plot, show
bone()
pcolor(som.distance_map().T)
colorbar()
markers = ['o', 's'] #circle, square
colors = ['r', 'g'] # red, green

#loop on each row of the dataset:
for i, j in enumerate(X):
    w = som.winner(j)
    plot(w[0] + 0.5,
         w[1] + 0.5,
         markers[y[i]],
         markeredgecolor = colors[y[i]],
         markerfacecolor = 'None',
         markersize = 10,
         markeredgewidth = 2)
show()
#Obtain explicit list of 'cheaters':

mappings = som.win_map(X)
frauds = np.concatenate((mappings[7,1], mappings[8,4]),axis = 0)
frauds = sc.inverse_transform(frauds)

#PART 2:
#Supervised deep learning
#Creating the new MOF:

customers = dataset.iloc[:, 1:].values

#creating the target variable:
#initialize an array of zeros:
is_fraud = np.zeros(len(dataset))
for i in range(len(dataset)):
    if dataset.iloc[i,0] in frauds:
        is_fraud[i] = 1

#Scale the data:
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
customers = sc.fit_transform(customers)

#Make the ANN:

# Importing the Keras libraries and packages
import keras
from keras.models import Sequential
from keras.layers import Dense

# Initialising the ANN
classifier = Sequential()

# Adding the input layer and the first hidden layer
classifier.add(Dense(units = 2, kernel_initializer = 'uniform',
                     activation = 'relu', input_dim = 15))

# Adding the output layer
classifier.add(Dense(units = 1, kernel_initializer = 'uniform',
                     activation = 'sigmoid'))

# Compiling the ANN
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy',
                   metrics = ['accuracy'])

# Fitting the ANN to the Training set
classifier.fit(customers, is_fraud, batch_size = 1, epochs = 4)

# Predicting the probabilities of fraud:
y_pred = classifier.predict(customers)

#Making the data presentable:
#let's return the y_pred probs with their corresponding cust. IDs:
y_pred = np.concatenate((dataset.iloc[:,0:1],y_pred), axis = 1)
#TRICK!: .iloc array trick [:,0:1]

#TRICK!: sorting with proper association:
y_pred = y_pred[y_pred[:,1].argsort()]


'''
Created on Nov 16, 2021

@author: Faizan3800X-Uni
'''

class GTGPrepareRltznGnrc:

    def __init__(self):

        # Add var labs to _getdata in save.py if they need to be there.

        self.label = None  # Label of the simulation. Set automatically.
        self.probs = None  # Non-exceedence probabilities of data points.
        self.ft = None  # FT coefficients of the transformed series.
        self.phs_spec = None  # Of the transformed series.
        self.mag_spec = None  # Of the transformed series.
        self.scorrs = None  # Lumped bivariate.
        self.asymms_1 = None  # Lumped bivariate.
        self.asymms_2 = None  # Lumped bivariate.
        self.ecop_dens = None  # 2D grids (bivariate).
        self.ecop_etpy = None  # Lumped bivariate.
        self.data = None  # Original (reshuffled for sims) data.
        self.pcorrs = None  # Lumped bivariate.
        self.nths = None  # Lumped nth order sums.
        self.data_ft = None  # Actually, cumm_corr_spec.
        self.probs_ft = None  # Actually, cumm_corr_spec.
        self.scorrs_ms = None  # Multivariate Spearman corr.
        self.ecop_etpy_ms = None  # Lumped, on a multivariate E. copula.
        self.data_ms_ft = None  # Actually, cumm_max_corr_spec.
        self.probs_ms_ft = None  # Actually, cumm_max_corr_spec.
        self.data_ms_pair_ft = None  # Multisite pairwise data cumm_corr_spec.
        self.probs_ms_pair_ft = None  # Multisite pairwise data cumm_corr_spec.
        self.data_ft_coeffs = None  # FT of the data.
        self.data_ft_coeffs_mags = None  # Magnitudes of data_ft_coeffs.
        self.data_ft_coeffs_phss = None  # Phases of data_ft_coeffs.
        self.probs_ft_coeffs = None  # FT of the probs.
        self.probs_ft_coeffs_mags = None  # Magnitudes of probs_ft_coeffs.
        self.probs_ft_coeffs_phss = None  # Phases of probs_ft_coeffs.

        self.skip_io_vars = []  # Not to write in the main save function.
        return


"""Creator information."""
from typing import Optional

class Creator:
    """Creator in ModelHistory and other COMBINE formats."""

    def __init__(
        self,
        familyName: str,
        givenName: str,
        email: str,
        organization: str,
        site: Optional[str] = None,
        orcid: Optional[str] = None,
    ):
        """Initialize Creator."""
        self.familyName = familyName
        self.givenName = givenName
        self.email = email
        self.organization = organization
        self.site = site
        self.orcid = orcid

    def __str__(self) -> str:
        """Get string representation."""
        return f"{self.familyName} {self.givenName} ({self.email}, {self.organization}, {self.site}, {self.orcid})"

    def __hash__(self) -> int:
        """Get hash."""
        return hash(str(self))

    def __eq__(self, other: object) -> bool:
        """Check for equality."""
        if not isinstance(other, Creator):
            return NotImplemented

        return (
            self.familyName == other.familyName
            and self.givenName == other.givenName
            and self.email == other.email
            and self.organization == other.organization
            and self.site == other.site
            and self.orcid == other.orcid
        )


# -*- coding: UTF-8 -*-
## Copyright 2011-2013 Luc Saffre
## This file is part of the Lino project.
## Lino is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or
## (at your option) any later version.
## Lino is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
## You should have received a copy of the GNU General Public License
## along with Lino; if not, see <http://www.gnu.org/licenses/>.

"""
Some calendar utilities

"""

from __future__ import unicode_literals

import datetime

from dateutil.tz import tzlocal

from django.utils.translation import ugettext_lazy as _
from django.utils.translation import pgettext_lazy
from django.utils import translation

from babel.dates import format_datetime, format_date
from north.utils import to_locale

from lino.core import actions
from lino import dd

def aware(d):
    return datetime.datetime(d.year,d.month,d.day,tzinfo=tzlocal())

def dt2kw(dt,name,**d):
    """
    Store given timestamp `dt` in a field dict.
    `name` is the base name of the fields.
    Examples:

    >>> dt = datetime.datetime(2013,12,25,17,15,00)
    >>> dt2kw(dt,'foo')
    {u'foo_date': datetime.date(2013, 12, 25), u'foo_time': datetime.time(17, 15)}

    """
    if dt:
        if isinstance(dt,datetime.datetime):
            d[name+'_date'] = dt.date()
            if dt.time():
                d[name+'_time'] = dt.time()
            else:
                d[name+'_time'] = None
        elif isinstance(dt,datetime.date):
            d[name+'_date'] = dt
            d[name+'_time'] = None
        else:
            raise Exception("Invalid datetime value %r" % dt)
    else:
        d[name+'_date'] = None
        d[name+'_time'] = None
    return d

def setkw(obj,**kw):
    for k,v in kw.items():
        setattr(obj,k,v)

def when_text(d,t=None):
    """
    Return a string with a concise representation of the given
    date and time combination.
    Examples:

    >>> when_text(datetime.date(2013,12,25))
    u'2013 Dec 25 (Wed)'

    >>> when_text(datetime.date(2013,12,25),datetime.time(17,15,00))
    u'2013 Dec 25 (Wed) 17:15'

    >>> when_text(None)
    u''

    """
    if d is None: return ''
    fmt = 'yyyy MMM dd (EE)'
    if t is None:
        return format_date(d,fmt,locale=to_locale(translation.get_language()))
    #~ if d.year == datetime.date.today().year:
        #~ fmt = "%a" + settings.SITE.time_format_strftime
    #~ else:
        #~ fmt = "%a %y %b %d" + settings.SITE.time_format_strftime
    #~ fmt = "%a %Y %b %d " + settings.SITE.time_format_strftime
    #~ return datetime.datetime.combine(d,t).strftime(fmt)
    fmt += " HH:mm"
    return format_datetime(datetime.datetime.combine(d,t),fmt,locale=to_locale(translation.get_language()))

class CalendarAction(actions.Action):
    """
    The default action for :class:`lino.modlib.cal.models.CalendarPanel`,
    only used there.
    """
    opens_a_window = True
    action_name = 'grid' # because...
    default_format = 'html'
    #~ icon_name = 'x-tbar-calendar'
    icon_name = 'calendar'

class Weekdays(dd.ChoiceList):
    verbose_name = _("Weekday")
add = Weekdays.add_item
add('1', _('Monday'),'monday')
add('2', _('Tuesday'),'tuesday')
add('3', _('Wednesday'),'wednesday')
add('4', _('Thursday'),'thursday')
add('5', _('Friday'),'friday')
add('6', _('Saturday'),'saturday')
add('7', _('Sunday'),'sunday')

class DurationUnit(dd.Choice):

    def add_duration(unit,orig,value):
        """
        Return a date or datetime obtained by adding `value`
        times this `unit` to the specified value `orig`.
        Returns None is `orig` is empty.

        This is intended for use as a
        `curried magic method` of a specified list item:

        >>> start_date = datetime.date(2011,10,26)
        >>> DurationUnits.months.add_duration(start_date,2)
        datetime.date(2011, 12, 26)

        See more usage examples in :func:`lino.modlib.cal.tests.cal_test.test01`.
        """
        if orig is None:
            return None
        if unit.value == 's' :
            return orig + datetime.timedelta(seconds=value)
        if unit.value == 'm' :
            return orig + datetime.timedelta(minutes=value)
        if unit.value == 'h' :
            return orig + datetime.timedelta(hours=value)
        if unit.value == 'D' :
            return orig + datetime.timedelta(days=value)
        if unit.value == 'W' :
            return orig + datetime.timedelta(days=value*7)
        day = orig.day
        while True:
            year = orig.year
            try:
                if unit.value == 'M' :
                    m = orig.month + value
                    while m > 12:
                        m -= 12
                        year += 1
                    while m < 1:
                        m += 12
                        year -= 1
                    return orig.replace(month=m,day=day,year=year)
                if unit.value == 'Y' :
                    return orig.replace(year=orig.year+value,day=day)
                raise Exception("Invalid DurationUnit %s" % unit)
            except ValueError:
                if day > 28:
                    day -= 1
                else:
                    raise

class DurationUnits(dd.ChoiceList):
    """A list of possible values for the `duration_unit` field of an :class:`Event`.
    """
    verbose_name = _("Duration Unit")
    item_class = DurationUnit

add = DurationUnits.add_item
add('s', _('seconds'),'seconds')
add('m', _('minutes'),'minutes')
add('h', _('hours')  ,'hours'  )
add('D', _('days')   ,'days'   )
add('W', _('weeks')  ,'weeks'  )
add('M', _('months') ,'months' )
add('Y', _('years')  ,'years'  )

class Recurrencies(dd.ChoiceList):
    """
    List of possible choices for a 'recurrency' field.
    """
    verbose_name = _("Recurrency")
    item_class = DurationUnit

add = Recurrencies.add_item
add('O', _('once')    ,'once'   )
add('D', _('daily')   ,'daily'   )
add('W', _('weekly')  ,'weekly'  )
add('M', _('monthly') ,'monthly' )
add('Y', _('yearly')  ,'yearly'  )
add('P', _('per weekday')  ,'per_weekday'  )

def amonthago():
    return DurationUnits.months.add_duration(datetime.date.today(),-1)

class AccessClasses(dd.ChoiceList):
    verbose_name = _("Access Class")
add = AccessClasses.add_item
add('10', _('Private'),'private')
add('20', _('Show busy'),'show_busy')
add('30', _('Public'),'public')


import ujson as json
from redis.exceptions import ResponseError
from rediscluster import RedisCluster
from redisbloom.client import Client

import config
rc_list=json.loads(config.config(section='rediscluster')['rediscluster'])
redisbloomclient = Client(host=config.config()['host'],port=config.config()['port'])

rediscluster_client = RedisCluster(startup_nodes=rc_list, decode_responses=True)

import os
from concurrent.futures import ThreadPoolExecutor, as_completed
n_cpus = os.cpu_count()
print(f'Number of CPUs: {n_cpus}')
executor = ThreadPoolExecutor(max_workers=n_cpus)

from pathlib import Path

datapath = Path('../input')

def parse_json_body_text(json_filename):
    print("Processing ..", json_filename.stem)
    with open(json_filename) as json_data:
        data = json.load(json_data)
        paper_id=data['paper_id']
        for body_text in data['body_text']:
            para = body_text['text']
            yield para

try:
    redisbloomclient.cfCreate('processed_documents', 40000)
except ResponseError as e:
    print("Error:", repr(e))

#process document return sentences and entities
def process_file(f,redisbloomclient=redisbloomclient, rediscluster_client=rediscluster_client):
    pid = 0
    article_id=f.stem
    print("Processing article_id ", article_id)
    if redisbloomclient.cfExists('processed_documents', article_id):
        print("already processed ", article_id)
        return article_id
    for para in parse_json_body_text(f):
        rediscluster_client.setnx(f"paragraphs:{article_id}:pid:{pid}",para)
        pid+= 1
    redisbloomclient.cfAdd('processed_documents', article_id)
    return article_id

# main submission loop
processed=[]
json_filenames = datapath.glob('**/*.json')
for each_file in json_filenames:
    print("Submitting task")
    task=executor.submit(process_file,each_file,redisbloomclient,rediscluster_client)
    processed.append(task)

print("Waiting for tasks to complete")
for each_task in as_completed(processed):
    print(task.result())
print("Completed")


#!/usr/bin/env python

from typing import List
import aoc
from collections import defaultdict

@aoc.timing
def solve(inp: str, part2=False):
    def find_path(current: str, path: List[str] = []):
        if current == 'end':
            yield path
            return

        for nxt in caves[current]:
            if nxt == 'start':
                continue
            if nxt.islower() and nxt in path:
                if not part2:
                    continue
                elif any(path.count(c) > 1 for c in path if c.islower()):
                    continue

            yield from find_path(nxt, path + [nxt])

    caves = defaultdict(list)
    for line in inp.splitlines():
        parts = line.split('-')
        caves[parts[0]].append(parts[1])
        caves[parts[1]].append(parts[0])

    return len(list(find_path('start')))

@aoc.timing
def part2(inp: str):
    return inp

with open('test2.txt', 'r') as f:
    inp = f.read()
    print("Part 1:", solve(inp))
    print("Part 2:", solve(inp, True))

with open('input.txt', 'r') as f:
    inp = f.read()
    print("Part 1:", solve(inp))
    print("Part 2:", solve(inp, True))


import base64
import cv2
import numpy as np
from tensorflow.keras.models import model_from_json
from flask import Flask, jsonify, make_response, request
from flask_cors import CORS

app = Flask(__name__)
cors = CORS(app, resources={r"/*": {"origins": "*"}})

# Load Face Detection Model
face_cascade = cv2.CascadeClassifier(
    "./models/haarcascade_frontalface_default.xml")
# Load Anti-Spoofing Model graph
json_file = open('antispoofing_models/antispoofing_model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
model = model_from_json(loaded_model_json)
# load antispoofing model weights
model.load_weights('antispoofing_models/antispoofing_model.h5')
print("Model loaded from disk")

def detect_liveness(frame):
    frame = cv2.resize(frame, (600, 400))
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
    for (x, y, w, h) in faces:
        face = frame[y-5:y+h+5, x-5:x+w+5]
        resized_face = cv2.resize(face, (160, 160))
        resized_face = resized_face.astype("float") / 255.0
        resized_face = np.expand_dims(resized_face, axis=0)
        preds = model.predict(resized_face)[0]
        print(preds)
        if preds > 0.5:
            label = 'spoof'
            print(label)
            cv2.putText(frame, label, (x, y - 10),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
            cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 0, 255), 2)
        else:
            label = 'real'
            print(label)
            cv2.putText(frame, label, (x, y - 10),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
            cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
    return frame

@app.route('/process_video', methods=['POST'])
def check_liveness():
    try:
        base64_image = request.data
        image_array = np.frombuffer(base64_image, dtype=np.uint8)
        image = cv2.imdecode(image_array, cv2.IMREAD_COLOR)
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        faces = face_cascade.detectMultiScale(gray, 1.3, 5)
        for (x, y, w, h) in faces:
            face = image[y-5:y+h+5, x-5:x+w+5]
            resized_face = cv2.resize(face, (160, 160))
            resized_face = resized_face.astype("float") / 255.0
            resized_face = np.expand_dims(resized_face, axis=0)
            preds = model.predict(resized_face)[0]
            print(preds)
            if preds > 0.5:
                print('spoof')
                result = {'label': 'spoof'}
                status_code = 200
            else:
                print('real')
                result = {'label': 'real'}
                status_code = 200
            return make_response(jsonify(result), status_code)
        return make_response(jsonify({'label': 'no_face_detected'}), 400)
    except Exception as e:
        print('error')
        return make_response(jsonify({'error': str(e)}), 500)

# def process_video():
#     stop_stream = False  # flag variable for stopping the stream

#     def generate():
#         with app.test_request_context():
#             nonlocal stop_stream  # use the flag variable from the outer function

#             while not stop_stream:
#                 # Read video frame
#                 data = request.data

#                 if not data:
#                     continue

#                 # Decode base64 data
#                 img_bytes = base64.b64decode(data)

#                 # Convert bytes to numpy array
#                 nparr = np.frombuffer(img_bytes, np.uint8)

#                 # Decode image
#                 img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)

#                 # Process frame for liveness detection
#                 processed_frame = detect_liveness(img)

#                 # Encode image as JPEG string
#                 ret, jpeg = cv2.imencode('.jpg', processed_frame)
#                 frame = jpeg.tobytes()

#                 # Yield the frame in byte format
#                 yield (b'--frame\r\n'
#                        b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')

#     return Response(generate(), mimetype='multipart/x-mixed-replace; boundary=frame')

@app.route('/stop_stream', methods=['POST'])
def stop_stream():
    data = request.get_json()
    stop = data.get('stop')

    if stop:
        global stop_stream
        stop_stream = True

    return 'OK', 200

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000, debug=True)


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

import utils

from scrapy import Selector
from .basespider import BaseSpider
from proxy import Proxy

class HidemySpider(BaseSpider):
    name = 'hidemy'

    def __init__(self, *a, **kw):
        super(HidemySpider, self).__init__(*a, **kw)

        self.urls = ['https://hidemy.name/en/proxy-list/?start=%s' % n for n in range(0, 5 * 64, 64)]
        self.headers = {
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
            'Accept-Encoding': 'gzip, deflate, br',
            'Accept-Language': 'en-US,en;q=0.5',
            'Connection': 'keep-alive',
            'Host': 'hidemy.name',
            'Referer': 'https://hidemy.name/en/proxy-list/?start=0',
            'Upgrade-Insecure-Requests': '1',
            'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:51.0) Gecko/20100101 Firefox/51.0',
        }

        self.init()

    def parse_page(self, response):
        self.write(response.body)

        sel = Selector(response)
        infos = sel.xpath('//tbody/tr').extract()
        for i, info in enumerate(infos):
            if i == 0:
                continue

            val = Selector(text = info)
            ip = val.xpath('//td[1]/text()').extract_first()
            port = val.xpath('//td[2]/text()').extract_first()
            country = val.xpath('//td[3]/div/text()').extract_first()
            anonymity = val.xpath('//td[6]/text()').extract_first()

            proxy = Proxy()
            proxy.set_value(
                    ip = ip,
                    port = port,
                    country = country,
                    anonymity = anonymity,
                    source = self.name,
            )

            self.add_proxy(proxy = proxy)


from ... import utils as _utils
from ... import preprocessing as _preprocessing

from ... import model as _model
from ... import edgetpu as _edgetpu

class DeepLabV3(_model.Model):
    '''
    DeepLabV3 instance segmentation model trained in Pascal VOC dataset.

    Model is taken from the `google-coral repo <https://github.com/google-coral/edgetpu/tree/master/test_data>`_.
    '''

    URL_CPU = 'https://github.com/google-coral/edgetpu/raw/master/test_data/deeplabv3_mnv2_dm05_pascal_quant.tflite'
    URL_TPU = 'https://github.com/google-coral/edgetpu/raw/master/test_data/deeplabv3_mnv2_dm05_pascal_quant_edgetpu.tflite'
    URL_LABELS = 'https://github.com/google-coral/edgetpu/raw/master/test_data/pascal_voc_segmentation_labels.txt'

    def __init__(self, device='CPU'):
        '''
        :parameter device: str.
            ``CPU`` by default.
            Set ``TPU`` or ``TPU:0`` to use the first EdgeTPU device.
            Set ``TPU:1`` to use the second EdgeTPU device etc.
        '''
        super().__init__()

        # Load model
        if device == 'CPU':
            model_path = self.URL_CPU
        else:
            model_path = self.URL_TPU
        self.model = _edgetpu.EdgeTPUModel(model_path, device)

        # Load labels from text file
        labels_path = _utils.file_from_url(self.URL_LABELS, 'labels')
        self._labels = []
        for line in open(labels_path):
            if line.strip() != '':
                self.labels.append(line.strip())
            else:
                break

    def forward(self, image):
        '''
        :parameter image: np array.
            Input image
        :return: numpy array.
            Segmentation map of the same size as the input image: ``shape=[batch, 513, 513]``.
            For each pixel gives an integer denoting class.
            Class labels are available through ``.labels`` attribute of this object.
        '''
        segmentation = self.model(image)[0]
        return segmentation

    def get_preprocessing(self):
        return _preprocessing.Preprocessing(
            resize=(513, 513),
            dtype='uint8',
            padding=False,
            batch_dimension=True,
        )

    @property
    def labels(self):
        '''
        :return: list of Pascal VOC labels
        '''
        return self._labels


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 22 14:33:59 2019

@author: SpaceMeerkat

A script for testing the trained CAE with synthetic moment maps
"""

# =============================================================================
# Import relevant packages
# =============================================================================

import matplotlib.pyplot as plt
import multiprocessing as mp
import numpy as np
import torch
from tqdm import tqdm
import sys
sys.path.append("../utils/")

from model_int import CAE
from functions import return_cube, synthetic_plotter
from rcparams import rcparams
rcparams()

# =============================================================================
# Setup paths
# =============================================================================

model_path = '/path/to/saves/models/'
image_path = '/path/to/image/save/directory/'

# =============================================================================
# Setup the model
# =============================================================================

def apply_dropout(m):
    if type(m) == torch.nn.Dropout:
        m.train(True)

model = CAE()
model.load_state_dict(torch.load(model_path))
model = model.cpu()
model.train(False)
model.apply(apply_dropout)
print("Model cast to CPU")

# =============================================================================
# Open the testing data
# =============================================================================

index = np.array([100])
num_cores = 14
pool = mp.Pool(num_cores,maxtasksperchild=100)
results = list(pool.imap(return_cube,index))
pool.close()

batch = np.array([r[0] for r in results])
target = np.array([r[1:] for r in results])

batch = torch.tensor(batch).to(torch.float)
batch[batch!=batch]=0
target = torch.tensor(target).to(torch.float)

pos = target[:,0]
ah = target[:,-2]

mom0s, mom1s = batch[:,0,:,:].unsqueeze(1), batch[:,1,:,:].unsqueeze(1)

print("Test data created")

# =============================================================================
# Begin the testing procedure
# =============================================================================

predictions = []
errors = []
temp_pred = []
for _ in tqdm(range(1000)): #10000
    prediction1 = model.test_encode(mom0s,mom1s,pos)
    prediction1 = prediction1.detach().numpy()
    temp_pred.append(prediction1)
temp_pred = np.vstack(temp_pred)
mean_pred = np.mean(temp_pred,0)
predictions.append(mean_pred)
errors.append(np.sum(np.abs(temp_pred-mean_pred[None,:]),0)/len(temp_pred))

errors = np.vstack(errors)

fig, axs = plt.subplots(1, 4, figsize=(30,7.5))

axs[0].hist(np.rad2deg(temp_pred[:,1]),bins=50,density=True,color='b')
axs[0].set_xlim(10,90)
axs[1].hist(temp_pred[:,2],bins=50,density=True,color='b')
axs[1].set_xlim(0.1,0.35)
axs[2].hist(temp_pred[:,4],bins=50,density=True,color='b')
axs[2].set_xlim(50,500)
axs[3].hist(temp_pred[:,3],bins=50,density=True,color='b')
axs[3].set_xlim(0.01,0.8)

axs[0].set(ylabel= 'Probability density')
axs[0].set(xlabel= r'i $(^{\circ})$')
axs[1].set(xlabel= r'$a_{I}$')
axs[2].set(xlabel= r'$V_{max} \, sin(i) \, (km\,s^{-1})$')
axs[3].set(xlabel= r'$V_{scale}$')

plt.tight_layout()
plt.savefig(image_path + 'param_dists.png')

# =============================================================================
# Put the medians back into the network to get "out" images
# =============================================================================

predictions = torch.tensor(mean_pred).to(torch.float).unsqueeze(0).unsqueeze(0).unsqueeze(0)

batch_size = predictions.shape[0]

### Create the auxiliary arrays
l = torch.arange(0 - 63/2., (63/2.)+1)
yyy, xxx, zzz = torch.meshgrid(l,l,l)

xxx, yyy, zzz = xxx.repeat(batch_size,1,1,1), yyy.repeat(batch_size,1,1,1), zzz.repeat(batch_size,1,1,1)
xxx = xxx.to(torch.float)
yyy = yyy.to(torch.float)
zzz = zzz.to(torch.float)

BRIGHTNESS, VELOCITY, vmax = CAE(xxx,yyy,zzz).test_images(mom0s, mom1s, predictions[:,:,:,0],
                                                          predictions[:,:,:,1], predictions[:,:,:,2],
                                                          predictions[:,:,:,3], predictions[:,:,:,4],
                                                          predictions[:,:,:,0]*0 + 1, shape=64)

predictions = predictions.squeeze(0).squeeze(0).numpy()
mom0s, mom1s = mom0s.squeeze(1).numpy(), mom1s.squeeze(1).numpy()
BRIGHTNESS, VELOCITY = BRIGHTNESS.squeeze(1).numpy(), VELOCITY.squeeze(1).numpy()

synthetic_plotter(np.array([32]), mom0s, mom1s, BRIGHTNESS,VELOCITY,predictions,errors,0,image_path+'synthetic.pdf')

# =============================================================================
# End of script
# =============================================================================


import os
import csv
# build path
csvpath=os.path.join('budget_data.csv')
with open(csvpath) as csvfile:
    csvreader=csv.reader(csvfile)
    header=next(csvreader)
    print(f"this is header prcossed: {header}")
    L=list(csvreader)
    changetracker=[]
    #print(L)
    mounthcount=1
    # now switch to index loop
    profitloss=0
    culmulativechange=0
    for i in range(0,len(L)-1):
        row=L[i]
        #print(row)
        profitloss+=int(row[1])
        #print(f"{row[1]}")

        timeaggregagte=row[0].split('-')
        month=timeaggregagte[0]
        #find the month
        #print(month)
        rownext=L[i+1]
        timeaggregagtenext=rownext[0].split('-')
        monthnext=timeaggregagtenext[0]
        #print(monthnext)
        if month !=monthnext:
        #count the different months
            mounthcount+=1
        thismonthProfitloss=int(row[1])
        nextmontProfitloss=int(rownext[1])
        change=nextmontProfitloss-thismonthProfitloss
        culmulativechange=culmulativechange+change
        changetracker.append(change)
Lastrow=L[len(L)-1]
lastprofitloss=Lastrow[1]
#print(type(lastprofitloss))
finalprofitloss=profitloss+int(lastprofitloss)
#print(mounthcount)
#print(finalprofitloss)
avgChange=culmulativechange/(int(mounthcount)-1)
#print(avgChange)
#print(changetracker)
largest=max(changetracker)
minvalue=min(changetracker)
#print(largest)
largestincreasechangeset=L[changetracker.index(largest)+1]
yearLargestincreasehappened=largestincreasechangeset[0]
#print(yearLargestincreasehappened)
#print(minvalue)
largestDecreaseChangeset=L[changetracker.index(minvalue)+1]
yearLargestDecreasehappend=largestDecreaseChangeset[0]
#print(yearLargestDecreasehappend)

print("Financial Analysis")
print("-----------------------------------")
print(f"Total Months:{mounthcount}")
print(f'Total: ${finalprofitloss}')
print(f"Average Change: ${round((avgChange),2)}")
print(f"Greatest Increasse in Profits: {yearLargestincreasehappened} (${largest})")
print(f"Greatest Decreasse in Profits: {yearLargestDecreasehappend} (${minvalue})")
#export as excel
outputpath=os.path.join("..","analysis","analysisPybank.txt")
#print(outputpath)
with open(outputpath,'w') as txt:
    txt.write("Financial Analysis\n")
    txt.write("-----------------------------------\n")
    txt.write(f"Total Months:{mounthcount}\n")
    txt.write(f'Total: ${finalprofitloss}\n')
    txt.write(f"Average Change: ${round((avgChange),2)}\n")
    txt.write(f"Greatest Increasse in Profits: {yearLargestincreasehappened} (${largest})\n")
    txt.write(f'Greatest Decreasse in Profits: {yearLargestDecreasehappend} (${minvalue})')