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

import os
import shutil
import copy
import re

PROTOTYPE_PATH = "Prototype"
SITE_PATH = "Site"

class TemplateMaker:
    def __init__(self, templates_path):
        self.templates_path = templates_path
        self.templates = {}
        self.get_templates()

    def get_templates(self):
        for root, _, filenames in os.walk(self.templates_path):
            for filename in filenames:
                file_path = os.path.join(root, filename)
                with open(file_path, "r", encoding="utf-8") as template_file:
                    template = template_file.read()

                template_name = filename.replace(".html", "")
                self.templates[template_name] = template

    def template_keyword(self, template_name):
        """ Return the identifier that should be replaced """
        return '$' + template_name

    def fill_file_templates(self, file_path):
        """ Fill templates in the given file """
        with open(file_path, "r", encoding="utf-8") as page_file:
            page_content = page_file.read()

        for template_name, template_content in self.templates.items():
            keyword = self.template_keyword(template_name)
            page_content = page_content.replace(keyword, template_content)

        with open(file_path, "w", encoding="utf-8") as new_page_file:
            new_page_file.write(page_content)

    def fill_path_templates(self, path):
        """ Fill all templates found in the files under path """
        for root, dirnames, filenames in os.walk(path):
            for filename in filenames:
                extension = filename.split(".")[-1]
                if extension != "html":
                    continue
                else:
                    file_path = os.path.join(root, filename)
                    self.fill_file_templates(file_path)

class Tag:
    def __init__(self, name, properties={}, children=[], class_list=[]):
        self.class_list = copy.copy(class_list)
        self.name = name
        self.children = copy.copy(children)
        self.properties = properties

    def open_tag(self):
        properties = ""
        for name, value in self.properties.items():
            properties += " " + name + "=" + value

        result = "<" + self.name + properties + " class = ' "
        for class_name in self.class_list:
            result += class_name + " "
        result += "'>"
        return result

    def close_tag(self):
        result = "</" + self.name + ">"
        return result

    def append(self, *args):
        for child in args:
            self.children.append(child)

    def __str__(self):
        result = ""
        result += self.open_tag()
        for child in self.children:
            result += str(child) + " "

        result += self.close_tag()
        return result

    def __repr__(self):
        return self.name + " tag"

class ImgTag:
    def __init__(self, src):
        self.src = src

    def __str__(self):
        return f''

class PageBuilder:
    SECTION_SEPARATOR = '---'
    CODE_SEPARATOR = '```'
    SECTION_TITLE_MARKER = '#'
    NOTE_MARKER = '|'
    MONOSPACE_SEPARATOR = '`'

    def __init__(self):
        self.table_of_contents = []

    def build_pages(self, pages_path, target_path):
        """ Build all pages in the directory and put them to the target """
        for root, dirnames, filenames in os.walk(pages_path):
            for filename in filenames:
                file_path = os.path.join(root, filename)
                target_filename = filename.split('.')[0] + '.html'
                target_path = os.path.join(target_path, target_filename)
                self.build_page(file_path, target_path)

    def build_page(self, page_path, target_path):
        """ Create a HTML file in the target path
            basing on the given file path
        """
        with open(page_path, "r", encoding="utf-8") as page_file:
            page_content = page_file.read()

        new_page_content = self.build_html(page_content, page_path)

        with open(target_path, "w", encoding="utf-8") as new_page_file:
            new_page_file.write(new_page_content)

    def build_html(self, page_content, page_path):
        """ Return the HTML string made of given text """

        sections = page_content.split(PageBuilder.SECTION_SEPARATOR)

        page_data = self.extract_page_data(sections[0])
        page_data['path'] = page_path.split('\\')[-1].replace('txt', 'html')
        self.table_of_contents.append(page_data)

        html = Tag('html')
        head = Tag('head', children=["$head"])

        head.append(Tag('title', children=[page_data['title']]))
        body = Tag('body', children=["$menu"])
        container = Tag('div', class_list=['container'])
        body.append(container)

        header = True
        for section_content in sections:
            container.append(self.build_section(
                section_content, header=header))
            header = False

        html.append(head, body)
        return '<!DOCTYPE html>\n' + str(html)

    def extract_page_data(self, header):
        page_data = {}
        data_names = ['category', 'category_id', 'title']
        for line in header.split('\n'):
            for data_name in data_names:
                if data_name + ':' in line:
                    value = line.split(data_name + ':')[1]
                    page_data[data_name] = value.strip()

        return page_data

    def build_section(self, section_content, header=False):
        """ Creates a Tag object representing section """
        print("Building section")

        section = Tag('section')
        if header:
            section.class_list.append('header')

        lines = section_content.split('\n')

        note = ''
        quote = ''
        code = ''
        code_open = False
        ignore_if_find = ['category:', 'category_id:']

        for line in lines:
            if code_open and line != PageBuilder.CODE_SEPARATOR:
                code += line + '\n'

            ignore_line = False
            for ignored in ignore_if_find:
                if ignored in line:
                    ignore_line = True

            if ignore_line:
                continue

            if len(line) == 0:
                if note != "":
                    note_tag = Tag('div', children=[note], class_list=['note'])
                    section.append(note_tag)
                    note = ''

            elif "title:" in line:
                title = line.split("title:")[1]
                section.append(Tag('h2', children=[title],
                                   class_list=['title']))

            elif line[0] == PageBuilder.SECTION_TITLE_MARKER:
                section.append(Tag('h6', children=[line[1:]],
                                   class_list=['section-title']))

            elif line[0] == PageBuilder.NOTE_MARKER:
                note += line[1:] + '
'

            elif PageBuilder.CODE_SEPARATOR in line:
                if code != "":
                    pre_tag = Tag("pre", class_list=['code-example'])
                    code_tag = Tag("code", children=[code],
                                   class_list=['code-example-body', 'cpp'])
                    pre_tag.append(code_tag)
                    section.append(pre_tag)
                    code_open = False
                else:
                    code_open = True

            elif line[:3] == 'img':
                pattern = re.compile(r'img\((.*)\)')
                src = pattern.match(line).group(1)
                img_wrapper = Tag('div',
                                  children=[ImgTag(src)],
                                  class_list=['img-wrapper'])
                section.append(img_wrapper)

            elif line[:4] == 'href':
                pattern = re.compile(r'href\((.*)\)\[(.*)\]')
                link_text = pattern.match(line).group(1)
                link_href = pattern.match(line).group(2)
                link = Tag('a',
                    properties={'href': link_href},
                    children=[link_text])
                section.append(link)

            else:
                if '`' in line:
                    pattern = re.compile(r'.*`(.*)`.*')
                    code_content = pattern.match(line).group(1)
                    code = Tag('code', children=[code_content])

                    line = line.split('`' + code_content + '`')
                    section.append(line[0], code, line[1])

                else:
                    section.append(line)

        return section

    def build_table_of_contents(self, path):
        categories = {}
        category_ids = {}

        for data in self.table_of_contents:
            category = data['category']
            if category not in categories:
                categories[category] = []
            category_ids[category] = data['category_id']
            categories[category].append(data)

        section = Tag('section')
        ul = Tag('ul', class_list=['table-of-contents'])
        section.append(ul)

        for category in categories:
            category_item = Tag('li')
            category_header = Tag('span',
                children=[category],
                properties={'id': category_ids[category]},
                class_list=['list-header'])
            category_item.append(category_header)

            category_list = Tag('ul')
            for page_data in categories[category]:
                page_item = Tag('li')
                page_link = Tag('a',
                    properties={'href': page_data['path']},
                    children=[page_data['title']])
                page_item.append(page_link)
                category_list.append(page_item)

            category_item.append(category_list)
            ul.append(category_item)

        table_of_contents = str(section)
        with open(path, 'w') as table_of_contents_file:
            table_of_contents_file.write(table_of_contents)

def prepare_directory(path):
    try:
        shutil.rmtree(path)
    except Exception as e:
        print(e)

    os.makedirs(path)

def copy_prototype(path_from, path_to):
    ignore = ["_prototype"]
    for tree in os.listdir(path_from):
        if tree in ignore:
            continue
        try:
            shutil.copytree(os.path.join(path_from, tree),
                            os.path.join(path_to, tree))
        except:
            shutil.copy(os.path.join(path_from, tree),
                        os.path.join(path_to, tree))

        print("Copying tree:", tree)

def build_prototype():
    prepare_directory("Site")

    page_builder = PageBuilder()
    page_builder.build_pages("Prototype\\_prototype\\pages", "Site")
    page_builder.build_table_of_contents("Prototype\\_prototype\\templates\\table_of_contents.html")

    copy_prototype("Prototype", "Site")

    template_maker = TemplateMaker("Prototype\\_prototype\\templates")
    template_maker.fill_path_templates("Site")

build_prototype()


# -*- encoding: utf-8 -*-

import numpy
import pandas

def compute_vdu(quote, n, percent_shrink):
    vol = quote.volume
    vol_list = [vol]
    for i in range(1, n, 1):
        vol_list.append(vol.shift(periods=i))

    # 3天缩量
    cond1 = pandas.Series(True, index=vol.index)
    for i in range(n - 1):
        cond1 &= (vol_list[i] < vol_list[i + 1])

    # 10天内成交量萎缩 50% 以上
    vol_min = vol_list[0]
    ma_max = vol.rolling(5).max()
    cond2 = 100 * (vol_min / ma_max) < percent_shrink

    quote = quote.assign(vdu=(cond1 & cond2))

    return quote

def volume_dry_up(quote, period, back_days=10):
    # 信号
    percent_shrink = 30
    quote = compute_vdu(quote, 4, percent_shrink)

    return numpy.any(quote.vdu[-back_days:])

def volume_shrink(quote, period, back_days=10):
    # 信号
    percent_shrink = 50
    quote = compute_vdu(quote, 4, percent_shrink)

    return numpy.any(quote.vdu[-back_days:])

def volume_dry_up_ing(quote, period, back_days=10):
    # 候选
    percent_shrink = 30
    quote = compute_vdu(quote, 3, percent_shrink)

    return numpy.any(quote.vdu[-back_days:])


import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from typing import List

import numpy as np
from tqdm import tqdm

###### Lorentz ######

def lorenz_runge_kutta_4(dt: float, step: int, y0s: List[float], params: list) -> List[list]:
    X_0, Y_0, Z_0 = y0s
    res = [[], [], []]

    xyz = [X_0, Y_0, Z_0]
    for _ in range(step):
        k_0 = lorentz_equation(xyz, *params)
        k_1 = lorentz_equation([x + k * dt/2 for x, k in zip(xyz, k_0)], *params)
        k_2 = lorentz_equation([x + k * dt/2 for x, k in zip(xyz, k_1)], *params)
        k_3 = lorentz_equation([x + k * dt for x, k in zip(xyz, k_2)], *params)

        for i in range(3):
            xyz[i] += (k_0[i] + 2*k_1[i] + 2*k_2[i] + k_3[i]) * dt/6.0
            res[i].append(xyz[i])

    return res

def lorentz_equation(xyz: List[list], p: float, r: float, b: float) -> List[list]:
    return [
        -p * xyz[0] + p * xyz[1],
        -xyz[0] * xyz[2] + r * xyz[0] - xyz[1],
        xyz[0] * xyz[1] - b * xyz[2]
    ]

def plot_lorentz(res: List[list], params: List[float]) -> None:
    p, r, b = params
    fig = plt.figure()
    ax = Axes3D(fig)
    ax.set_xlabel("x")
    ax.set_ylabel("y")
    ax.set_zlabel("z")
    ax.set_title(f"Lorenz attractor rho={r}, sigma={p}, beta={round(b, 2)}")

    n = len(res[0])
    c = np.linspace(0, 1, n)
    pas = int(n/10)

    x, y, z = res
    for i in range(0, n-pas, pas):
        ax.plot(x[i:i+pas+1], y[i:i+pas+1], z[i:i+pas+1], color=(1, c[i],0), lw=1)
    plt.show()

###### Henon ######

def henon_rec(x: float, y: float, a: float, b: float) -> list:
    return 1 - a*x*x + y, b*x

def henon(x0: float, y0: float, a: float, b: float, N: int) -> List[list]:
    x, y = [x0], [y0]
    for i in range(N-1):
        xn, yn = henon_rec(x[i], y[i], a, b)
        x.append(xn)
        y.append(yn)

    return x, y

def plot_henon(x: list, y: list, params: List[float]) -> None:
    a, b, N = params
    fig, ax = plt.subplots()
    ax.set_xlabel("x")
    ax.set_ylabel("y")
    ax.set_title(f"Henon attractor a={a}, b={b}, N={N}")

    ax.scatter(x, y, color='red', s=0.2)
    plt.show()

###### Baier-Klein ######

def relation_baier_klein(x: list, y: list, z: list) -> List[list]:
    return 1.76 - y*y - 0.1*z, x, y

def baier_klein(x0: float, y0: float, z0: float, N: int) -> List[list]:
    x, y, z = [x0], [y0], [z0]

    for i in range(N - 1):
        xn, yn, zn = relation_baier_klein(x[i], y[i], z[i])
        x.append(xn)
        y.append(yn)
        z.append(zn)

    return x, y, z

def plot_baier_klein(res: list) -> None:
    fig = plt.figure()
    ax = Axes3D(fig)
    ax.set_xlabel("x")
    ax.set_ylabel("y")
    ax.set_zlabel("z")
    ax.set_title("Baier Klein System")
    ax.scatter(res[0], res[1], res[2], color="red", s=0.4)
    plt.show()

###### Ikeda ######

def ikeda_relation(z: float) -> complex:
    complex_z = 0.4j - 6.j/(1 + abs(z)*abs(z))
    return 1 + 0.9*z*np.exp(complex_z)

def ikeda(z0: float, N: int) -> complex:
    z = [z0]

    for i in range(N-1):
        z.append(ikeda_relation(z[i]))

    return z


import numpy as np
import pylab as plt

#Real part of smith transformation
def smith_trans_re(re, im):
    return (re**2 + im**2 -1) / ((re +1 )**2 + im**2)

#Imaginary part of smith transformation
def smith_trans_im(re, im):
    return (2*im)/((re+1)**2 + im**2)

#Plot full circles of constant real part
full_real_circles = [0,0.2,0.5,1,2,5,10]
im_range = np.arange(-100,100,0.05)
for re_circ in full_real_circles:
    plt.plot(smith_trans_re(re_circ, im_range),
             smith_trans_im(re_circ, im_range), 'k')

    #Plot real ticks

    #Only plot some of the ticks aligned to the imaginary line to avoid chaos
    #near Re(z) = Infinity
    if re_circ <= 2:
        tick_x = smith_trans_re(re_circ, -0.7) - 0.04
        tick_y = smith_trans_im(re_circ, -0.7) - 0.04

        plt.text(tick_x, tick_y, str(re_circ), horizontalalignment='center',
                 verticalalignment='center', color='black')

#Plot rest of the real ticks
plt.text(0.73, -0.19, 5, horizontalalignment='center', verticalalignment='center',
         color='black')
plt.text(0.86, -0.13, 10, horizontalalignment='center', verticalalignment='center',
         color='black')

#Plot full lines of constant imaginary part with ticks
full_im_lines = [-10,-5,-2,-1,-0.5,-0.2,0,0.2,0.5,1,2,5,10]
re_range = np.arange(0,10,0.05)
for im_line in full_im_lines:
    plt.plot(smith_trans_re(re_range, im_line),
             smith_trans_im(re_range, im_line), 'b')

    #Plot the ticks

    #Calculate intersection of imaginaryy line with Re=0
    outer_circle_x = smith_trans_re(0, im_line)
    outer_circle_y = smith_trans_im(0, im_line)

    #Calculate polar angle of this intersection
    phi = np.arccos(outer_circle_x)
    if outer_circle_y < 0:
        phi = -phi

    #Draw ticks in distance 1.1 of origin with calculated angle
    plt.text(1.1*np.cos(phi), 1.1*np.sin(phi), str(im_line),
             horizontalalignment='center', verticalalignment='center',
             color='blue')

plt.xlim(-1.05,1.05)
plt.ylim(-1.05,1.05)
plt.axis('off')
plt.show()


import os,sys
import h5py, time, itertools, datetime
import numpy as np

import torch

from torch_connectomics.model.loss import *
from torch_connectomics.utils.net import *
from torch_connectomics.utils.vis import visualize, visualize_aff
import torch.multiprocessing as t_mp

def train(args, train_loader, val_loader, model, model_cpu, device, criterion,
          optimizer, scheduler, logger, writer, regularization=None, model_lstm=None):
    record = AverageMeter()
    val_record = AverageMeter()

    model.train()

    if val_loader is not None:
        val_loader_itr = iter(val_loader)

    start = time.time()
    for iteration, data in enumerate(train_loader):
        print('time taken for itr: ', time.time() - start)
        start = time.time()
        sys.stdout.flush()

        if args.in_channel == 2:
            (_, volume, input_label, label, class_weight, _) = data
            volume, input_label, label = volume.to(device), input_label.to(device), label.to(device)
            output = model(torch.cat((volume, input_label), 1))
        else:
            (_, volume, label, class_weight, _) = data

            volume, label = volume.to(device), label.to(device)
            output = model(volume)
            if model_lstm is not None:
                output_pre_lstm = output
                output = model_lstm(output.clone())

        class_weight = class_weight.to(device)
        if regularization is not None:
            loss_unet = criterion(output_pre_lstm, label, class_weight) + regularization(output_pre_lstm)
            loss = loss_unet + criterion(output, label, torch.ones_like(class_weight)) + regularization(output)
        else:
            loss_unet = criterion(output_pre_lstm, label, class_weight)
            loss = loss_unet + criterion(output, label, torch.ones_like(class_weight))

        record.update(loss, args.batch_size)

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

        logger.write("[Volume %d] train_loss=%0.4f lr=%.5f\n" % (iteration,
                                                                 loss.item(), optimizer.param_groups[0]['lr']))
        print('[Iteration %d] train_loss=%0.4f lr=%.6f' % (iteration,
                                                           loss.item(), optimizer.param_groups[0]['lr']))
        writer.add_scalars('Loss', {'Train': loss.item()}, iteration)

        if iteration % 50 == 0 and iteration >= 1:

            params_gpu = model.named_parameters()
            params_cpu = model_cpu.named_parameters()
            dict_p = dict(params_gpu)
            dict_params_cpu = dict(params_cpu)
            for name, param in dict_p.items():
                dict_params_cpu[name].data.copy_(param.data)

            if args.task == 0:
                visualize_aff(volume, label, output, iteration, writer, mode='Train')
            elif args.task == 1 or args.task == 3:
                if args.in_channel == 2:
                    visualize(volume, label, output, iteration, writer, input_label=input_label)
                else:
                    visualize(volume, label, output, iteration, writer, input_label=output_pre_lstm)  #TODO used input label to show the pre LSTM output, change this later into more elegant format

            scheduler.step(record.avg)
            record.reset()

            if val_loader is not None:
                model.eval()
                val_record.reset()

                #for better coverage of validation dataset running multiple batches of val
                for _ in range(5):
                    try:
                        (_, volume, label, class_weight, _) = next(val_loader_itr)
                    except StopIteration:
                        val_loader_itr = iter(val_loader)
                        (_, volume, label, class_weight, _) = next(val_loader_itr)

                    with torch.no_grad():
                        volume, label = volume.to(device), label.to(device)
                        class_weight = class_weight.to(device)
                        output = model(volume)

                        if regularization is not None:
                            val_loss = criterion(output, label, class_weight) + regularization(output)
                        else:
                            val_loss = criterion(output, label, class_weight)

                        val_record.update(val_loss, args.batch_size)

                writer.add_scalars('Loss', {'Val': val_loss.item()}, iteration)
                print('[Iteration %d] val_loss=%0.4f lr=%.6f' % (iteration,
                      val_loss.item(), optimizer.param_groups[0]['lr']))

                if args.task == 0:
                    visualize_aff(volume, label, output, iteration, writer, mode='Validation')
                elif args.task == 1 or args.task == 3:
                    visualize(volume, label, output, iteration, writer)

                model.train()

            #print('weight factor: ', weight_factor) # debug
            # debug
            # if iteration < 50:
            #     fl = h5py.File('debug_%d_h5' % (iteration), 'w')
            #     output = label[0].cpu().detach().numpy().astype(np.uint8)
            #     print(output.shape)
            #     fl.create_dataset('main', data=output)
            #     fl.close()

        #Save model
        if iteration % args.iteration_save == 0 or iteration >= args.iteration_total:
            torch.save(model.state_dict(), args.output+(args.exp_name + '_%d.pth' % iteration))
            if model_lstm is not None:
                torch.save(model_lstm.state_dict(), args.output + (args.exp_name + 'head_%d.pth' % iteration))

        # Terminate
        if iteration >= args.iteration_total:
            break

def main():
    args = get_args(mode='train')

    print('Initial setup')
    model_io_size, device = init(args)

    if args.enable_logging:
        logger, writer = get_logger(args)
    else:
        print('No log file would be created.')

    print('Setup model')
    model, model_cpu = setup_model(args, device, model_io_size)

    model_lstm = setup_lstm_model(args, device, model_io_size)

    print('Setup data')
    train_loader = get_input(args, model_io_size, 'train', model=model_cpu)

    # val_loader = get_input(args, model_io_size, 'validation')

    print('Setup loss function')
    criterion = WeightedBCE()
    regularization = BinaryReg(alpha=10.0)

    print('Setup optimizer')
    torch.autograd.set_detect_anomaly(True)
    # list(model.parameters()) +
    optimizer = torch.optim.Adam(list(model_lstm.parameters()), lr=args.lr, betas=(0.9, 0.999),
                                 eps=1e-08, weight_decay=1e-6, amsgrad=True)
    scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='min', factor=0.1,
                patience=1000, verbose=False, threshold=0.0001, threshold_mode='rel', cooldown=0,
                min_lr=1e-7, eps=1e-08)

    print('4. start training')
    train(args, train_loader, None, model, model_cpu, device, criterion, optimizer, scheduler, logger, writer, model_lstm=model_lstm)

    print('5. finish training')
    logger.close()
    writer.close()

if __name__ == "__main__":
    main()


from flask import render_template,url_for,request,flash,redirect,abort
from app.main  import main
from app.models import User,Recommedation
from .. import db, photos
from flask_login import login_required,current_user
import secrets
import os
from .forms import UpdateProfile,CreateBlog,Recommendation

# from ..email import mail_message

@main.route('/')
def index():

    return render_template('index.html')

@main.route('/profile',methods=['GET','POST'])
def profile():
    form = UpdateProfile()
    if form.validate_on_submit():
        if form.profile_pic.data:
            picture_file= save_pic(form.profile_pic.data)
            current_user.profile_pic_path = picture_file
        current_user.username = form.username.data
        current_user.email = form.email.data
        current_user.bio = form.bio.data
        db.session.commit()
        flash('Profile Updated Successfully')
        return redirect(url_for('main.profile'))
    elif request.method =='GET':
        form.bio.data = current_user.bio
    profile_pic_path = url_for('static',filename='photos' + current_user.profile_pic_path)
    return render_template('profile/profile.html',form=form)

# @main.route('/commic/recommed/<int:id>')
# @loginrequired
# def recommed(id):
#     commic=Commic.query.get(id)
#     form=RecommedationForm()

#     if form.validate_on_submit():
#         new_recommedtaion=Recommedation(heading=form.heading.data,content=form.content.data)
#         new_recommedataion.recommedation_saves()

#         return redirect('index.html',commic=commic)
#         title=f'make a new recommedation to |commic.id '

#     return ('recommend.html',form=form)


# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Docker (http://docker.io) driver.
Created by Markos Gogoulos (mgogoulos@mist.io)
"""

import base64
import datetime
import shlex
import re
import socket

try:
    import simplejson as json
except:
    import json

from libcloud.utils.py3 import httplib
from libcloud.utils.py3 import b

from libcloud.compute.providers import Provider
from libcloud.common.base import JsonResponse, ConnectionUserAndKey
from libcloud.compute.types import (NodeState, InvalidCredsError,
                                    MalformedResponseError, LibcloudError)
from libcloud.compute.base import (Node, NodeDriver, NodeImage,
                                   NodeSize, NodeLocation)

VALID_RESPONSE_CODES = [httplib.OK, httplib.ACCEPTED, httplib.CREATED,
                        httplib.NO_CONTENT]

class DockerResponse(JsonResponse):

    valid_response_codes = [httplib.OK, httplib.ACCEPTED, httplib.CREATED,
                            httplib.NO_CONTENT]

    def parse_body(self):
        if len(self.body) == 0 and not self.parse_zero_length_body:
            return self.body

        try:
            # error responses are tricky in Docker. Eg response could be
            # an error, but response status could still be 200
            content_type = self.headers.get('content-type', 'application/json')
            if content_type == 'application/json' or content_type == '':
                body = json.loads(self.body)
            else:
                body = self.body
        except ValueError:
            m = re.search('Error: (.+?)"', self.body)
            if m:
                error_msg = m.group(1)
                raise Exception(error_msg)
            else:
                raise Exception('ConnectionError: Failed to parse JSON response')
        return body

    def parse_error(self):
        if self.status == 401:
            raise InvalidCredsError('Invalid credentials')
        return self.body

    def success(self):
        return self.status in self.valid_response_codes

class DockerConnection(ConnectionUserAndKey):

    responseCls = DockerResponse
    timeout = 60

    def add_default_headers(self, headers):
        """
        Add parameters that are necessary for every request
        If user and password are specified, include a base http auth
        header
        """
        if not headers.get('Content-Type'):
            headers['Content-Type'] = 'application/json'
        if self.user_id and self.key:
            user_b64 = base64.b64encode(b('%s:%s' % (self.user_id, self.key)))
            headers['Authorization'] = 'Basic %s' % (user_b64.decode('utf-8'))
        return headers

class DockerNodeDriver(NodeDriver):
    """
    Docker node driver class.

    >>> from libcloud.compute.providers import get_driver
    >>> driver = get_driver('docker')
    >>> conn = driver(host='198.61.239.128', port=4243)
    >>> conn.list_nodes()
    or connecting to http basic auth protected https host:
    >>> conn = driver('user', 'pass', host='https://198.61.239.128', port=443)

    connect with tls authentication, by providing a hostname, port, a private
    key file (.pem) and certificate (.pem) file
    >>> conn = driver(host='https://198.61.239.128', port=4243, key_file='key.pem', cert_file='cert.pem')
    """

    type = Provider.DOCKER
    name = 'Docker'
    website = 'http://docker.io'
    connectionCls = DockerConnection
    features = {'create_node': ['password']}

    def __init__(self, key=None, secret=None, host='localhost',
                 port=4243, secure=False, key_file=None, cert_file=None):

        super(DockerNodeDriver, self).__init__(key=key, secret=secret,
                                               host=host, port=port, secure=secure, key_file=key_file, cert_file=cert_file)
        if host.startswith('https://'):
            secure = True

        # strip the prefix
        prefixes = ['http://', 'https://']
        for prefix in prefixes:
            if host.startswith(prefix):
                host = host.strip(prefix)

        if key_file or cert_file:
            # docker tls authentication - https://docs.docker.com/articles/https/
            # We pass two files, a key_file with the private key and cert_file with the certificate
            # libcloud will handle them through LibcloudHTTPSConnection
            if not (key_file and cert_file):
                    raise Exception('Needs both private key file and certificate file for tls authentication')
            self.connection.key_file = key_file
            self.connection.cert_file = cert_file
            self.connection.secure = True
        else:
            self.connection.secure = secure

        self.connection.host = host
        self.connection.port = port

        try:
            socket.setdefaulttimeout(15)
            so = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            so.connect((host, int(port)))
            so.close()
        except:
            raise Exception("Make sure host is accessible and docker port %s is open" % port)

    def _get_api_version(self):
        """
        Get the docker API version information
        """

        result = self.connection.request('/version').object
        api_version = result.get('ApiVersion')

        return api_version

    def list_sizes(self):
        return (
            [NodeSize(
                id='default',
                name='default',
                ram='unlimited',
                disk='unlimited',
                bandwidth='unlimited',
                price=0,
                driver=self)]
        )

    def list_nodes(self, show_all=True):
        """
        List running and stopped containers
        show_all=False will show only running containers
        """
        try:
            result = self.connection.request("/containers/json?all=%s" %
                                         str(show_all)).object
        except Exception as exc:
            if hasattr(exc,'errno') and exc.errno == 111:
                raise Exception('Make sure docker host is accessible and the API port is correct')
            raise

        nodes = [self._to_node(value) for value in result]
        return nodes

    def inspect_node(self, node):
        """
        Inspect a container
        """
        result = self.connection.request("/containers/%s/json" %
                                         node.id).object

        name = result.get('Name').strip('/')
        if result['State']['Running']:
            state = NodeState.RUNNING
        else:
            state = NodeState.STOPPED

        extra = {
            'image': result.get('Image'),
            'volumes': result.get('Volumes'),
            'env': result.get('Config', {}).get('Env'),
            'ports': result.get('ExposedPorts'),
            'network_settings': result.get('NetworkSettings', {}),
            'exit_code': result['State'].get("ExitCode")
        }
        node_id = result.get('Id')
        if not node_id:
            node_id = result.get('ID', '')
        node = (Node(id=node_id ,
                     name=name,
                     state=state,
                     public_ips=[self.connection.host],
                     private_ips=[],
                     driver=self.connection.driver,
                     extra=extra))
        return node

    def list_processes(self, node):
        """
        List processes running inside a container
        """
        result = self.connection.request("/containers/%s/top" % node.id).object

        return result

    def reboot_node(self, node):
        """
        Restart a container
        """
        data = json.dumps({'t': 10})
        #number of seconds to wait before killing the container
        result = self.connection.request('/containers/%s/restart' % (node.id),
                                         data=data, method='POST')
        return result.status in VALID_RESPONSE_CODES

    def destroy_node(self, node):
        """
        Remove a container
        """

        result = self.connection.request('/containers/%s' % (node.id),
                                         method='DELETE')
        return result.status in VALID_RESPONSE_CODES

    def ex_start_node(self, node):
        """
        Start a container
        """

        payload = {
            'Binds': [],
            'PublishAllPorts': True,
        }
        data = json.dumps(payload)
        result = self.connection.request('/containers/%s/start' % (node.id),
                                         method='POST', data=data)
        return result.status in VALID_RESPONSE_CODES

    def ex_stop_node(self, node):
        """
        Stop a container
        """
        result = self.connection.request('/containers/%s/stop' % (node.id),
                                         method='POST')
        return result.status in VALID_RESPONSE_CODES

    def ex_rename_node(self, node, name):
        """
        rename a container
        """
        result = self.connection.request('/containers/%s/rename?name=%s' % (node.id, name),
                                         method='POST')
        return result.status in VALID_RESPONSE_CODES

    def get_logs(self, node, stream=False):
        """
        Get container logs

        If stream == True, logs will be yielded as a stream
        From Api Version 1.11 and above we need a GET request to get the logs
        Logs are in different format of those of Version 1.10 and below

        """
        payload = {}
        data = json.dumps(payload)
        if float(self._get_api_version()) > 1.10:
            logs = self.connection.request("/containers/%s/logs?follow=%s&stdout=1&stderr=1" %(node.id, int(stream)),headers={"Content-Type": "application/vnd.docker.raw-stream"}).object
        else:
            result = self.connection.request("/containers/%s/attach?logs=1&stream=%s&stdout=1&stderr=1" %
                                             (node.id, str(stream)), method='POST', data=data,
                                             headers={
                                                 "Content-Type": "application/vnd.docker.raw-stream"
                                             })
            logs = result.body

        return logs

    def create_node(self, name, image, command=None, hostname=None, user='',
                    detach=False, stdin_open=True, tty=True,
                    mem_limit=0, ports=None, environment=None, dns=None,
                    volumes=None, volumes_from=None,
                    network_disabled=False, entrypoint=None,
                    cpu_shares=None, working_dir='', domainname=None,
                    memswap_limit=0, port_bindings={}):
        """
        Create a container

        Create a container, based on an image and optionally specify command
        and other settings. If image is not found, try to pull it
        After the container is created, start it
        """
        command = shlex.split(str(command))

        params = {
            'name': name
        }

        payload = {
            'Hostname': hostname,
            'Domainname': domainname,
            'ExposedPorts': ports,
            'User': user,
            'Tty': tty,
            'OpenStdin': stdin_open,
            'StdinOnce': False,
            'Memory': mem_limit,
            'AttachStdin': True,
            'AttachStdout': True,
            'AttachStderr': True,
            'Env': environment,
            'Cmd': command,
            'Dns': dns,
            'Image': image,
            'Volumes': volumes,
            'VolumesFrom': volumes_from,
            'NetworkDisabled': network_disabled,
            'Entrypoint': entrypoint,
            'CpuShares': cpu_shares,
            'WorkingDir': working_dir,
            'MemorySwap': memswap_limit,
            'PublishAllPorts': True,
            'PortBindings': port_bindings,
        }

        data = json.dumps(payload)
        try:
            result = self.connection.request('/containers/create', data=data,
                                             params=params, method='POST')
        except Exception as e:
            #if image not found, try to pull it
            if e.message.startswith('No such image:'):
                try:
                    self.pull_image(image=image)
                    result = self.connection.request('/containers/create',
                                                     data=data, params=params,
                                                     method='POST')
                except:
                    raise Exception('No such image: %s' % image)
            else:
                raise Exception(e)

        id_ = result.object['Id']

        payload = {
            'Binds': [],
            'PublishAllPorts': True,
            'PortBindings': port_bindings,
        }

        data = json.dumps(payload)
        result = self.connection.request(
            '/containers/%s/start' % id_, data=data,
            method='POST')

        return Node(id=id_, name=id_, state=NodeState.RUNNING,
                    public_ips=[], private_ips=[],
                    driver=self.connection.driver, extra={})

    def list_images(self):
        "Return list of images as NodeImage objects"

        result = self.connection.request('/images/json').object
        images = []
        for image in result:
            try:
                name = image.get('RepoTags')[0]
            except:
                name = image.get('Id')
            images.append(NodeImage(
                id=image.get('Id'),
                name=name,
                driver=self.connection.driver,
                extra={
                    "created": image.get('Created'),
                    "size": image.get('Size'),
                    "virtual_size": image.get('VirtualSize'),
                },
            ))

        return images

    def search_images(self, term):
        """Search for an image on Docker.io.
           Returns a list of NodeImage objects

           >>> images = conn.search_images(term='mistio')
           >>> images
           [<NodeImage: id=rolikeusch/docker-mistio...>,
            <NodeImage: id=mist/mistio, name=mist/mistio, driver=Docker  ...>]
        """

        term = term.replace(' ', '+')
        result = self.connection.request('/images/search?term=%s' %
                                         term).object
        images = []
        for image in result:
            name = image.get('name')
            images.append(NodeImage(
                id=name,
                name=name,
                driver=self.connection.driver,
                extra={
                    "description": image.get('description'),
                    "is_official": image.get('is_official'),
                    "is_trusted": image.get('is_trusted'),
                    "star_count": image.get('star_count'),
                },
            ))

        return images

    def pull_image(self, image):
        """Create an image,
        Create an image either by pull it from the registry or by
        importing it
        >>> image = conn.pull_image(image='mist/mistio')
        >>> image
        <NodeImage: id=0ec05daec99f, name=mist/mistio, driver=Docker  ...>

        """

        payload = {
        }
        data = json.dumps(payload)

        result = self.connection.request('/images/create?fromImage=%s' %
                                         (image), data=data, method='POST')
        if "errorDetail" in result.body:
            raise Exception(result.body)
        try:
            #get image id
            image_id = re.findall(r'{"status":"Download complete","progressDetail":{},"id":"\w+"}', result.body)[-1]
            image_id = json.loads(image_id).get('id')
        except:
            image_id = image

        image = NodeImage(id=image_id, name=image,
                          driver=self.connection.driver, extra={})
        return image

    def delete_image(self, image):
        "Remove image from the filesystem"
        result = self.connection.request('/images/%s' % (image),
                                         method='DELETE')
        return result.status in VALID_RESPONSE_CODES

    def inspect_image(self, image):
        """
        Inspect an image
        """
        raise NotImplementedError()

    def push_image(self, image):
        """
        Push an image on the registry
        """
        raise NotImplementedError()

    def _to_node(self, data):
        """Convert node in Node instances
        """
        try:
            name = data.get('Names')[0].strip('/')
        except:
            name = data.get('Id')
        if 'Exited' in data.get('Status'):
            state = NodeState.STOPPED
        elif data.get('Status').startswith('Up '):
            state = NodeState.RUNNING
        else:
            state = NodeState.STOPPED
        ports = data.get('Ports', [])
        ports.sort()
        extra = {
            'id': data.get('Id'),
            'status': data.get('Status'),
            'created': ts_to_str(data.get('Created')),
            'image': data.get('Image'),
            'ports': json.dumps(ports),
            'command': data.get('Command'),
            'sizerw': data.get('SizeRw'),
            'sizerootfs': data.get('SizeRootFs'),
        }

        node = (Node(id=data['Id'],
                     name=name,
                     state=state,
                     public_ips=[self.connection.host],
                     private_ips=[],
                     driver=self.connection.driver,
                     extra=extra))
        return node

def ts_to_str(timestamp):
    """Return a timestamp as a nicely formated datetime string."""
    date = datetime.datetime.fromtimestamp(timestamp)
    date_string = date.strftime("%d/%m/%Y %H:%M %Z")
    return date_string


def nQueens(n):
    chess = [[0]*n]*n
    ans = []
    tmp1 = []
    queenMaker(n,chess,0,tmp1,ans)
    return ans

def queenMaker(n,thechess,level,tmp,ans):
    if level==n:
        ans.append(tmp)
        return 0
    for i in range(0,n):
        if thechess[level][i]==0:
            newchess = []
            for j in range(0,n):
                newchess.append(list(thechess[j]))
            drawer(n,newchess,[level,i])
            queenMaker(n,newchess,level+1,tmp+[i+1],ans)
    return 0

def drawer(n,plate,index):
    row=index[0]
    column = index[1]
    i = row
    while(i>=0):
        plate[i][column] = 1
        i = i-1
    i = row
    while(i<n):
        plate[i][column] = 1
        i = i+1
    i = column
    while(i>=0):
        plate[row][i] = 1
        i = i-1
    i = column
    while(i<n):
        plate[row][i] = 1
        i = i+1
    i = row
    j = column
    while(i>=0 and j>=0):
        plate[i][j] = 1
        i = i-1
        j = j-1
    i = row
    j = column
    while(i>=0 and j<n):
        plate[i][j] = 1
        i = i-1
        j = j+1
    i = row
    j = column
    while(i<n and j>=0):
        plate[i][j] = 1
        i = i+1
        j = j-1
    i = row
    j = column
    while(i<n and j<n):
        plate[i][j] = 1
        i = i+1
        j = j+1
    return 0


#Embedded file name: eve/common/script/paperDoll\paperDollCommonFunctions.py
"""
This file contains methods used both for client and common code base of paperDoll
Must be usable on a server so it is _strictly forbidden_ to ever **import trinity** in this file.
"""
import sys
import blue
import yaml
import telemetry
import log
import stackless

def WaitForAll(iterable, condition):
    """
    Yields while any item in the iterable matches the condition.
    Condition is a function that accepts an item in the iteratable and returns a boolean value.
    """
    while any(map(condition, iterable)):
        Yield(frameNice=False)

    BeFrameNice()

def Yield(frameNice = True, ms = 15):
    """
    Encapsulates blue.synchro.Yield for various instrumentation purposes
    Include being frameNice, which means, if current frame has exceed a given amount of time,
    yield again so the tasklet doesn't spike the current frame by resuming.
    """
    try:
        if not stackless.current.is_main:
            blue.synchro.Yield()
            if frameNice:
                return BeFrameNice(ms)
        else:
            return False
    except:
        raise

def BeFrameNice(ms = 15):
    """
    Yields if frametime has exceeded the given ms and doesn't resume execution until
    we're at a time within a frame that is less than ms.
    However, the more we yield, the higher the ms value gets so the tasklet gets eventually its
    time to run, even on crapspec machines.
    """
    try:
        if not stackless.current.is_main:
            if ms < 1.0:
                ms = 1.0
            while blue.os.GetWallclockTimeNow() - blue.os.GetWallclockTime() > ms * 10000:
                blue.synchro.Yield()
                ms *= 1.02

            return True
        return False
    except:
        raise

def AddToDictList(d, key, item):
    """
    Utility method that adds item to a list the belongs to key in the dict d.
    If key currently does not exist in d, the list is created, the item added to it
    and that list is set as the value of key in d.
    """
    l = d.get(key, [])
    l.append(item)
    d[key] = l

def GetFromDictList(d, key):
    """
    Utility method that always returns a list. If key exist and the value is a list in d
    then that list is returned, otherwise, an empty list is returned.
    """
    l = d.get(key, [])
    if type(l) != list:
        return []
    return l

@telemetry.ZONE_FUNCTION
def NastyYamlLoad(yamlStr):
    """
    Backwards compatiable load function that accepts persisted yaml files from the PaperDoll since
    before it was corified and moved to /Script.
    """
    import paperDoll as PD
    sys.modules[PD.__name__] = PD
    instance = None
    try:
        blue.statistics.EnterZone('yaml.load')
        instance = yaml.load(yamlStr, Loader=yaml.CLoader)
    except Exception:
        log.LogError('PaperDoll: Yaml parsing failed for data', yamlStr)
    finally:
        blue.statistics.LeaveZone()
        del sys.modules[PD.__name__]

    return instance

import carbon.common.script.util.autoexport as autoexport
exports = autoexport.AutoExports('paperDoll', globals())


# uncompyle6 version 3.7.4
# Python bytecode 2.6 (62161)
# Decompiled from: Python 3.6.9 (default, Apr 18 2020, 01:56:04)
# [GCC 8.4.0]
# Embedded file name: build/bdist.linux-x86_64/egg/pyf/dataflow/reordering.py
# Compiled at: 2011-02-23 05:58:06
from pyjon.utils import get_secure_filename
try:
    import cPickle as pickle
except ImportError:
    import pickle

import itertools, operator, os
apickle = type('pickler', (), {})()
apickle.loads = pickle.loads
apickle.dumps = lambda v: pickle.dumps(v, -1)

def reorder_flow(iterator, key, continue_val=None, serializer=apickle):
    """ `New in version 2.0.1`

    Reorders a serializable flow according to a specific key:
    works like sorted(iterator, key=key).

    It serializes the flow to a temporary file while indexing the gotten keys
    and replays the flow in an ordered way.

    Default serializer is pickle with latest protocol version.

    :param iterator: The iterator to reorder
    :param key: The key getter (ex: "`operator.attrgetter('blah')`", "`lambda v: v[1]`")
    :type key: callable
    :param continue_val: If specified, the function will yield the passed item each time it
    receives an item without yielding one.
    :param serializer: Serializer override (usefull to set "marshal" or "json"/"simplejson" for example instead of pickle
    :type serializer: Object (likely module) with "loads" and "dumps" callable attributes.
    """
    line_offsets = []
    line_keys = dict()
    offset = 0
    filename = get_secure_filename()
    write_file = open(filename, 'wb')
    cur_buffer = ''
    for (i, item) in enumerate(iterator):
        item_content = serializer.dumps(item)
        line_offsets.append(offset)
        line_keys[i] = key(item)
        offset += len(item_content)
        cur_buffer += item_content
        if continue_val is not None:
            yield continue_val
        if not i % 100:
            write_file.write(cur_buffer)
            cur_buffer = ''

    write_file.write(cur_buffer)
    cur_buffer = ''
    write_file.close()
    read_file = open(filename, 'rb')
    lines_ordered = itertools.imap(operator.itemgetter(0), sorted(line_keys.items(), key=operator.itemgetter(1)))
    line_count = len(line_offsets)
    for line in lines_ordered:
        read_file.seek(line_offsets[line])
        if line_count > line + 1:
            yield serializer.loads(read_file.read(line_offsets[(line + 1)] - line_offsets[line]))
        else:
            yield serializer.loads(read_file.read())

    read_file.close()
    os.unlink(filename)
    return


"""@summary: Les sorts du tonneau attractif (Ivresse de pandawa)
"""

import Sort
import Zones
from Effets.EffetEtat import EffetEtatSelf, EffetEtat
from Effets.EffetRet import EffetRetPM
from Etats.EtatEffet import EtatEffetSiLance
from Etats.EtatBoostCarac import EtatBoostCaracFixe
# pylint: disable=line-too-long

def getSortsDebutCombat(lvl):
    """@summary: charge les sorts de combat
    @return: List <Sort>
    """
    # pylint: disable=unused-argument
    sortsDebutCombat = []
    sortsDebutCombat.append(Sort.Sort("Ebrité", 0, 0, 0, 0, [
        EffetEtatSelf(EtatEffetSiLance("Tournée Générale si lancé", 0, -1, EffetRetPM(3, 1, cibles_possibles="Ennemis", cibles_possibles_direct="Tonneau Incapacitant", zone=Zones.TypeZoneCercle(1)), "Tournée Générale")),
    ], [], 0, 99, 99, 0, 0, "cercle", False, chaine=False))
    return sortsDebutCombat

def getSorts(lvl):
    """@summary: charge les sorts de début de combat
    @return: List <Sort>
    """
    # pylint: disable=unused-argument
    sorts = []
    sorts.append(
        Sort.Sort("Potion Magique", 0, 0, 0, 0, [
            EffetEtat(EtatBoostCaracFixe("Potion Magique", 0, 1, "pui", 150), cibles_possibles="Allies", cibles_possibles_direct="Lanceur", zone=Zones.TypeZoneCercle(2))
        ], [], 0, 99, 1, 0, 0, "ligne", True)
    )
    return sorts


# rlc color_b.py version 6.0

import colorsys,sys
from pymol import cmd

# main function called from within PyMOL
def seq_diff(obj1, obj2, doColor=False):
    if cmd.count_atoms(obj1+' and name CA') == 0:
        print('%s is empty' % obj1)
        return
    if cmd.count_atoms(obj2+' and name CA') == 0:
        print('%s is empty' % obj2)
        return

    if cmd.count_atoms(obj1+' and name CA') != cmd.count_atoms(obj2+' and name CA'):
        print('Selections have different number of residues.')
        return

    stored.resn1, stored.resn2 = [], []
    stored.resi1, stored.resi2 = [], []
    stored.chain1, stored.chain2 = [], []

    cmd.iterate(obj1 + ' and name CA', 'stored.resn1.append(resn)')
    cmd.iterate(obj2 + ' and name CA', 'stored.resn2.append(resn)')

    cmd.iterate(obj1 + ' and name CA', 'stored.resi1.append(resi)')
    cmd.iterate(obj2 + ' and name CA', 'stored.resi2.append(resi)')

    cmd.iterate(obj1 + ' and name CA', 'stored.chain1.append(chain)')
    cmd.iterate(obj2 + ' and name CA', 'stored.chain2.append(chain)')

    #print len(stored.resn1)

    mutant_selection = []

    for n1, n2, i1, i2, c1, c2 in zip(stored.resn1, stored.resn2, stored.resi1, stored.resi2, stored.chain1, stored.chain2):
        if c1 == '':
            c1 = '""'
        if c2 == '':
            c2 = '""'
        #print 'Resi1: '+i1
        #print 'Resi2: '+i2
        if n1 != n2:
            #print 'Mutation at resi: '+i2
            mutant_selection.append('%s and resi %s and chain %s' % (obj2, i2, c2))

    if mutant_selection == '':
        print('No mutations found')
        #raise CmdException

    total_selection = ''
    total_color = ''
    for m in mutant_selection:
        total_selection += '('+m+') or '
        total_color += '('+m+' and name C*) or '

    total_selection = total_selection[:-4]
    total_color = total_color[:-4]

    cmd.select("%s_diff" % (obj2), total_selection )

    if doColor:
        cmd.color('%s' % doColor,total_color)

    #cmd.deselect()

cmd.extend("seq_diff",seq_diff)

cmd.auto_arg[0]['seq_diff'] = cmd.auto_arg[0]['align']
cmd.auto_arg[1]['seq_diff'] = cmd.auto_arg[1]['align']

# vi: ts=1:sw=4:smarttab:expandtab


__author__ = 'mn14tm'

# Description:
# Program to solve the fresnel equations for reflectivity and transmission
# for non-magnetic, isotropic media

from numpy import sin,cos,deg2rad,rad2deg,arcsin,arange
import matplotlib.pyplot as plt

ni = 1                      # Refractive index of incident light medium
nt = 1.5                    # Refractive index of transmitted light medium
i = deg2rad(arange(0,90,0.1)) # Angle of incidence (degrees)

# Calculate angle of transmittance from Snell's Law
t = arcsin(ni*sin(i)/nt)

# For perpendicular polarization (s)
r_s = (ni*cos(i)-nt*cos(t))/(ni*cos(i)+nt*cos(t))
R_s = r_s**2

t_s = (2*ni*cos(i))/(ni*cos(i)+nt*cos(t))
T_s = t_s**2 * ((nt*cos(t))/(ni*cos(i)))

# For parallel polarization (p)
r_p = (nt*cos(i)-ni*cos(t))/(ni*cos(t)+nt*cos(i))
R_p = r_p**2
t_p = (2*ni*cos(i))/(ni*cos(t)+nt*cos(i))
T_p = t_p**2 * ((nt*cos(t))/(ni*cos(i)))

# Calculate average for unpolarized light
R_ave = (R_p + R_s) /2
T_ave = (T_p + T_s) /2

plt.figure(1)
plt.subplot(121)
plt.plot(rad2deg(i),R_s,label="Perpendicular")
plt.plot(rad2deg(i),R_p,label="Parallel")
plt.plot(rad2deg(i),R_ave,'--',label="Unpolarized")
plt.ylabel('Reflectance')
plt.xlabel('Angle of incidence (degrees)')
plt.legend(loc = (0.05,0.7))

plt.subplot(122)
plt.plot(rad2deg(i),T_s,label="Perpendicular")
plt.plot(rad2deg(i),T_p,label="Parallel")
plt.plot(rad2deg(i),T_ave,'--',label="Unpolarized")
plt.ylabel('Transmittance')
plt.xlabel('Angle of incidence (degrees)')
plt.legend(loc = (0.05,0.05))

plt.show()


import numpy as np
import math
from scipy import linalg

np.set_printoptions(formatter={'float_kind':'{:f}'.format})
n= int(input('Write the value of n: '))

# theta_xaqib = np.linspace(0,math.pi,n,endpoint='True')
theta_xaqib = np.linspace(0,math.pi,n,endpoint=True)
# n+=1
# theta_xaqib[0] += 0.005
# theta_xaqib[-1] += -0.005
print(n,theta_xaqib)

# for i in range(len(theta_xaqib)):
    # print(int(theta_xaqib[i]))

right_t = np.zeros((4,1))
# print(right_t)

# list_const = [int(input(' Enter P, R , v, k, G: ')) for i in range(5)]

list_const = [float(x) for x in input('Enter P, R , v, k, G D: ').split()]
# list_const = input('Enter P, R , v, k, G: ')
print(list_const)

P = list_const[0]
R = list_const[1]
v = list_const[2]
k = list_const[3]
G = list_const[4]
D = list_const[5]

print(f"P: {P} R: {R} v: {v} k: {k} G: {G} D: {D}")

# RIght side of the matrix is defined here:
# This is used with f during calculations
# right_t[0] = (P*R*(1-v)/G)*((1+k + (2*(1-k)*f))/2)
# define f for each 4*4 matrix
# f = math.cos()

var_k = np.zeros((4,4))
k_rr_n1 = []
for i in range(n):
    print(f"i:{i}")
    if i==0:
        k_rr_n1.append(2*G/R)
    elif i==1:
        k_rr_n1.append(2*G/R)
    else:
        k_rr_n1.append(2*G*((2*i + 1) - (2*v*(i+1)))/(R*(3-4*v)))

print(f"krr_n1: {k_rr_n1}")
krr_n2 = [((D / (R**2)) + (k / (R**4))*(i**2 -1)*(i**2 -1)) for i in range(n)]
print(f"krr_n2: {krr_n2}")
# krr_n2 = []
# for i in range(5):
    # krr_n2.append((D / (R**2)) + (k / (R**4))*(i**2 -1)(i**2 -1))
# print(f"krr_n2: {krr_n2}")

krt_n1 = []
for i in range(n):
    if i==0:
        krt_n1.append(0)
    elif i==1:
        krt_n1.append(2*G/R)
    else:
        krt_n1.append(2*G*((i + 2) - (2*v*(i+1)))/(R*(3-4*v)))

print(f"krt_n1: {krt_n1}")
krt_n2 = [i*D/(R**2) for i in range(n)]
print(f"krt_n2: {krt_n2}")

ktt_n1 = []
for i in range(n):
    if i==0:
        ktt_n1.append(0)
    elif i==1:
        ktt_n1.append(2*G/R)
    else:
        ktt_n1.append(2*G*((2*i + 1) - (2*v*(i+1)))/(R*(3-4*v)))

print(f"ktt_n1: {ktt_n1}")
ktt_n2 = [((i**2)*D/(R**2)) for i in range(n)]
print(f"ktt_n2: {ktt_n2}")

# defining the matrix value for final calculation:
# var_k[0][0] = math.cos()

# for f, z in zip(range(n_x*theta, n_x*(theta+1)), range(n_y*i, n_y*(i+1))):

def fill(big_matrix, small_matrix, i, theta):
    n_x, n_y = small_matrix.shape
    k = 0
    l=0
    for f in range(n_x*theta, n_x*(theta+1)):
        l = 0
        for z in range(n_y*i, n_y*(i+1)):
            big_matrix[f][z] = small_matrix[k][l]
            l+=1
        k +=1
        # l+=1
    return big_matrix

big_matrix = np.zeros((4*n, 4*n))
print(f"len of big matrix array: {big_matrix.shape}")
# print(big_matrix)
right_matrix = np.zeros((4*n,1))

x_right = [3*i + i for i in range(n)]
flt = 0
for theta in theta_xaqib:
    right_matrix[x_right[flt]] = (P*R*(1-v)/G)*((1+k + (2*(1-k)*math.cos(2*theta)))/2)
    flt +=1

theta_range = theta_xaqib.shape[0]
for theta, theta_i in zip(theta_xaqib, range(0,theta_range)):
    for i in range(0,n):
        right_t[0] = (P*R*(1-v)/G)*((1+k + (2*(1-k)*math.cos(2*theta)))/2)
        # if theta==0:
            # theta += 0.01
            #  var_k[2][2] = 1
            # var_k[3][3] =  1
        # if theta == math.pi/2:
            # var_k[0][2] = 0.00000001
            # var_k[1][2] = 0.00000001
        var_k[0][0] = math.cos(i*theta)
        var_k[0][1] = -math.cos(i*theta)
        var_k[1][0] = k_rr_n1[i]*math.cos(i*theta)
        var_k[1][1] = krr_n2[i]*math.cos(i*theta)
        var_k[1][2] = krt_n1[i]*math.cos(i*theta)
        var_k[1][3] = krt_n2[i]*math.cos(i*theta)
        var_k[2][0] = krt_n1[i]*math.sin(i*theta)
        var_k[2][2] = ktt_n1[i]*math.sin(i*theta)
        var_k[3][1] = krt_n2[i]*math.sin(i*theta)
        var_k[3][3] = ktt_n2[i]*math.sin(i*theta)
        # print(f"var_k_{i} theta_{theta*180/math.pi}: {var_k} ")
        # alpha = np.linalg.inv(var_k)
        # print(f"alpha_{i} theta_{theta*180/math.pi}: {alpha}")
        big_matrix = fill(big_matrix, var_k, i, theta_i)
        # right_matrix = fill(right_matrix, right_t, )
        # x_right = 3*i + i

# print(f"len of big matrix array: {big_matrix}")
# big_matrix_inverse = np.linalg.inv(big_matrix)

for i in range(big_matrix.shape[0]):
    for j in range(big_matrix.shape[1]):
        if i==j:
            big_matrix[i][j] += 0.000000000000000000000001

print(f"len of big matrix array: {big_matrix}")
big_matrix_inverse = np.linalg.inv(big_matrix)

# print(f"Inverse of a matrix: {big_matrix_inverse}")

print(f"right_matrix is : {right_matrix}")

# print(f"identity if exists: {np.dot(big_matrix,big_matrix_inverse).astype(int)}")

# file = open('x.txt', 'w+')
# file.write(str(big_matrix))
# file.close()
# np.set_printoptions(suppress=True)
final_n_values = big_matrix_inverse.dot(right_matrix)
# final_n_values = np.dot(big_matrix_inverse,right_matrix)
print(f"final needed matrix is: {final_n_values}")


#!/usr/local/bin/python3
#
#
#
#   Word Length Counting Tool
#          final.py
#
#   David S. Jackson
#   December 31, 2014
#   OST Python1, Lesson 16, Attempt 2
#
#
"""
This program is a final exercise for Python1 at OST.  Program accepts
a filename as an argument.  This filename should be a text file to
process, and it should be in the current working directory, or the
argument should be the path to this this filename.  The keys to the
dictionary are the list of word lengths in the text.  If no filename is
provided as an argument, an error is returned to the user and the program
exits.  Program requires sys module.  The printing funtion removes any
zero length words from the output.

Additionally, there is a function called test_out() that can be called
to look at the values in the word_dict dictionary.

You can now just add '-t' after the name of the file you wish to analyze.
You can see exactly what words are being measured for length.  Probably
best to pipe the std_out to your favorite pager.
Example:

    final.py declaration.txt -t | less

The -t switch is new and will allow you to bypass the other functions in
the program when you just want to look at your data for a sanity check.

"""

import sys

def word_len(raw_word):
    """ Function returns length of word after removing punctuation
    and special characters. This used by store_count()
    """
    raw_word = raw_word.strip()
    punct = "!@#$%^&*()_+=-{}?<>[]\\;:',./"
    raw_word = raw_word.strip(punct)
    word = raw_word
    for c in raw_word :
        if c in punct:
            word = raw_word.replace(c, "")
    return len(word)

def store_count(wordlist):
    """ keeps a dictionary of word lengths with usage counts
    for each word length. Each word length is a key whose value is
    the number of times that word length is used.  This function
    calls word_len()
    """
    word_count = {}
    for word in wordlist:
        if word_len(word) in word_count.keys():
            word_count[word_len(word)] = word_count[word_len(word)] + 1
        else:
            word_count[word_len(word)] = 1
    return word_count

def get_file():
    """ Func tests for for sys.argv[1].  Catches exception and
    prompts user for filename argument. Returns wordlist from
    text file (sys.argv[1]).  Also, it's best to remove newlines
    and extra spaces now.
    """
    try:
        infile = open(sys.argv[1], 'r')
        filehandle = infile.read()
        filehandle = filehandle.replace("\n", " ")
        filehandle = filehandle.replace("  ", " ")
        wordlist = filehandle.split(" ")
        infile.close()
        return wordlist
    except (FileNotFoundError, IndexError):
        print("WARNING: Must provide path to filename as argument to", sys.argv[0])
        exit()
    except Exception as message:
        print("Problem: {0}".format(message))
        exit()

def print_results(word_dict):
    """ Function prints results of processing sys.argv[1]
    This function does NOT print keys whose value is zero (0)
    """
    print('\n\n')
    print("word             number of times word this")
    print("length           length appears in", sys.argv[1])
    print('=' * 45)   # changed to 45
    for key, value in sorted(word_dict.items()):  # added sorted()
        if key == 0 :
            continue
        print("{0:>4d}\t\t{1:>4d}".format(key, value))  # added .format()

def test_out():
    """ This is a function to test the accuracy of the program.
    It writes a list of the words to standard output and sorts them
    by their length.  You can glance through the sorted output and see
    if there are any glaring problems. Probably best to use your
    favorite pager when using this function.
    """
    big_wordlist = []
    punct = "!@#$%^&*()_+=-{}?<>[]\\;:',./"
    infile = get_file()
    for raw_word in infile:
        raw_word = raw_word.strip(punct)
        word = raw_word
        for c in raw_word :
            if c in punct:
                word = raw_word.replace(c, "")
        line = str(len(word)) + " " + word
        big_wordlist.append(line)
    big_wordlist = sorted(big_wordlist)
    for item in big_wordlist:
        print(item)

### Main program begins
if __name__ == "__main__":

    try:
        if sys.argv[2] == '-t':
            test_out()
    except IndexError:
        print("Not analyzing raw data...")
        word_dict = store_count(get_file())
        print_results(word_dict)


import json
import statistics
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

time_list=[]
tem_list=[]
occu_list=[]
co2_list=[]
room_list=[]
office_temp=[]
office_occu=[]

with open('sensor.txt','r') as file:
	for line in file:
		#check the non-empty line
		if line!="\n":
			#convert data line to dictionary and store in res
			res=json.loads(line)
			head_str=next(iter(res))

			tem_list.append((res[head_str]['temperature'])[0])
			occu_list.append((res[head_str]['occupancy'])[0])
			co2_list.append((res[head_str]['co2'])[0])
			time_list.append((res[head_str]['time']))
			room_list.append(head_str)

#for temperature
print("total data points: "+str(len(tem_list)) )
print("mean of temperature: "+str(statistics.mean(tem_list)))
print("variance of temperature: "+str(statistics.variance(tem_list)))

#for occupation
print("mean of occupancy: "+str(statistics.mean(occu_list)))
print("variance of occupancy: "+str(statistics.variance(occu_list)))

#for probability distribution of different type of sensor
uniq_room=[]
uniq_percent=[]
size_total=len(room_list)

for room in room_list:
	if room not in uniq_room:
		uniq_room.append(room)

for uniq in uniq_room:
	uniq_percent.append((room_list.count(uniq))/size_total)

print(uniq_percent)

#plot probability density for sensor type
#temperature
fig=plt.figure()

plt.subplot(2,2,1)
plt.title('probability distribution function')

s1=pd.Series(tem_list)
ax1=s1.plot.kde()
plt.xlabel('temperature')
plt.ylabel('probability')

#occupancy
plt.subplot(2,2,2)
s2=pd.Series(occu_list)
ax2=s2.plot.kde()
plt.xlabel('occupancy')
plt.ylabel('probability')

#co2
plt.subplot(2,2,3)
s3=pd.Series(co2_list)
ax3=s3.plot.kde()
plt.xlabel('co2')
plt.ylabel('probability')

plt.show()

#logic to capture the abnormal temperature point
std1=statistics.pstdev(tem_list)
print("The first standard deviation temperature "+str(std1))


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

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

from itertools import izip_longest
import unicodedata
import re

from flask import url_for

from .models import Category
from .settings import WAREHOUSE_ROOT_CATEGORY_ID

# https://docs.python.org/2/library/itertools.html
def grouper(iterable, n, fillvalue=None):
    "Collect data into fixed-length chunks or blocks"
    # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx
    args = [iter(iterable)] * n
    return izip_longest(fillvalue=fillvalue, *args)

def slugify(value, max_words=-1):
    if max_words > 0:
        value = ' '.join(value.split(' ')[:max_words])
    value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('ascii')
    value = re.sub('[^\w\s-]', '', value).strip().lower()
    return re.sub('[-\s]+', '-', value)

def format_price(int_price):
    if int_price is None:
        int_price = 0
    return "%.2f€" % (float(int_price) / 100)

def url_for_marketplace(item, offer):
    if offer.origin == "amazon_new":
        return item.api_data["DetailPageURL"]

    extra = ""
    if offer.origin == "amazon_used":
        extra += "&me=A8KICS1PHF7ZO"
    if offer.primeable:
        extra += "&shipPromoFilter=1"
    if offer.origin.endswith("_new"):
        extra += "&sort=price"

    return "http://www.amazon.de/gp/offer-listing/%s?ie=UTF8&condition=%s&tag=0414-21%s" % (item.asin, offer.condition, extra)

def url_for_model(model, **kwargs):
    if isinstance(model, Category):
        if model.id == WAREHOUSE_ROOT_CATEGORY_ID:
            return url_for("WarehouseView:index", **kwargs)

        seo = slugify(model.name, max_words=10)
        if seo:
            return url_for("WarehouseView:get_seo", category_id=model.id, seo_name=seo, **kwargs)
        else:
            return url_for("WarehouseView:get", category_id=model.id, **kwargs)

    from .models import Item
    if isinstance(model, Item):
        seo = slugify(model.name, max_words=10)
        if seo:
            return url_for("ItemView:get_seo", item_asin=model.asin, seo_name=seo, **kwargs)
        else:
            return url_for("ItemView:get", item_asin=model.asin, **kwargs)

    raise ValueError


#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# [START imports]
import webapp2
from webapp2_extras.routes import RedirectRoute
from handlers.BasicHandlers import MainHandler, MainAdminHandler
from handlers.ProjectHandlers import ProjectHandler, AddProjectHandler, EditProjectHandler, DeleteProjectHandler
from handlers.CategoryHandlers import EditCategoryHandler, DeleteCategoryHandler
from handlers.CompanyHandlers import CompanyListHandler, AddCompanyHandler, EditCompanyHandler, DeleteCompanyHandler
# [END imports]

app = webapp2.WSGIApplication([
    webapp2.Route('/', MainHandler, name='home'),
    RedirectRoute('/admin', redirect_to_name='admin'),
    RedirectRoute('/admin/', redirect_to_name='admin'),
    RedirectRoute('/admin/projects', MainAdminHandler, name='admin', strict_slash=True),
    RedirectRoute('/admin/projects/new', AddProjectHandler, name='add-project', strict_slash=True),
    RedirectRoute('/admin/projects/<project_id:\d+>/edit', EditProjectHandler, name="edit-project"),
    RedirectRoute('/admin/projects/<project_id:\d+>/delete', DeleteProjectHandler, name="delete-project"),
    RedirectRoute('/admin/categories/new', ProjectHandler, name="project"),
    RedirectRoute('/admin/categories/<category_id:\d+>/edit', EditCategoryHandler, name="edit-category"),
    RedirectRoute('/admin/categories/<category_id:\d+>/delete', DeleteCategoryHandler, name="delete-category"),
    RedirectRoute('/admin/companies', CompanyListHandler, name="company-list", strict_slash=True),
    RedirectRoute('/admin/companies/new', AddCompanyHandler, name="add-company", strict_slash=True),
    RedirectRoute('/admin/companies/<company_id:\d+>/edit', EditCompanyHandler, name="edit-company", strict_slash=True),
    RedirectRoute('/admin/companies/<company_id:\d+>/delete', DeleteCompanyHandler, name="delete-company", strict_slash=True),
], debug=True)


import cv2
from skimage.measure import compare_ssim as ssim
import compare_images as comImg
import numpy as np
import random

class image_sample:
    def __init__(self, frame, screen_width, screen_height, video_width, video_height, windowName, counter, video_initial_x_pos, video_initial_y_pos):
        self.frame = frame
        self.screen_width = screen_width
        self.screen_height = screen_height
        self.video_width = video_width
        self.video_height = video_height
        self.windowName = windowName
        self.counter = counter
        self.video_initial_x_pos = video_initial_x_pos
        self.video_initial_y_pos = video_initial_y_pos
        self.original_img_filePath = "/home/pi/master_project_master/image/"+ "img_" + str(random.randint(1,67)) + ".JPG"
        self.similarity = 0
        self.x1 = 0
        self.y1 = 0

    def randomNumXY(self, width, height):
        x1 = random.randrange(int(width/3), int(width/3)*2)
        y1 = random.randrange(int(height/3), int(height/3)*2)
        x2 = random.randrange(x1+1, int(width/3*2)+2)
        y2 = random.randrange(y1+1, int(height/3*2)+2)
        return x1, y1, x2, y2

    def image_show(self, winName, flags, image, width, height, x_position, y_position):
        cv2.namedWindow(winName, flags=flags)
        cv2.imshow(winName, image)
        cv2.resizeWindow(winName, width, height)
        cv2.moveWindow(winName,x_position, y_position)

    def show_all_image(self):
        self.x1, self.y1, x2, y2 = self.randomNumXY(self.video_width, self.video_height)
        print("x1 : {}, x2 : {}, y1 : {}, y2 : {}".format(self.x1, x2, self.y1, y2))
        croppedScreen = self.frame[self.y1:y2, self.x1:x2].copy()
        cropped_img_width = croppedScreen.shape[1]
        cropped_img_heigth = croppedScreen.shape[0]
        print("cropped scereenshot width : {}, cropped image height : {}".format(cropped_img_width, cropped_img_heigth))

        # show embed image
        self.image_show("embed" + self.windowName, cv2.WINDOW_AUTOSIZE, croppedScreen, cropped_img_width, cropped_img_heigth, self.video_initial_x_pos+self.x1, self.video_initial_y_pos+self.y1)

        # pair windows position
        if cropped_img_width > cropped_img_heigth:
            wins_posX = random.randint(0, self.screen_width)
            if int(random.randint(0,100)) > 50:
                    wins_posY = random.randint(self.screen_height - ((self.screen_height-self.video_height)/2), self.screen_height)
            else:
                    wins_posY = random.randint(0, (self.screen_height-self.video_height)/2 - 100)
        else:
            wins_posY = random.randint(0, self.screen_height)
            if int(random.randint(0,100)) > 50:
                    wins_posX = random.randint(self.screen_width - ((self.screen_width-self.video_width)/2), self.screen_width)
            else:
                    wins_posX = random.randint(0, (self.screen_width-self.video_width)/2 - 100)
        # show sep cropped image
        self.image_show("ex" + str(self.counter) + self.windowName, cv2.WINDOW_AUTOSIZE, croppedScreen, cropped_img_width, cropped_img_heigth, wins_posX, wins_posY)

        originalImg = cv2.imread(self.original_img_filePath, cv2.IMREAD_COLOR)
        print("LOADED IMAGE : {}".format(self.original_img_filePath))
        originalWidth = originalImg.shape[1]
        originalHeight = originalImg.shape[0]
        print('original image width : {}, height : {}'.format(originalWidth, originalHeight))
        xp = random.randint(1, originalWidth - cropped_img_width - 1)
        yp = random.randint(1, originalHeight - cropped_img_heigth - 1)
        print('x position : {}, y position : {}'.format(xp, yp))
        croppedOriginal = originalImg[yp:yp + cropped_img_heigth, xp:xp + cropped_img_width].copy()
        cropOriginal_width = croppedOriginal.shape[1]
        cropOriginal_height = croppedOriginal.shape[0]
        #show original cropped image
        self.image_show("original"+str(self.counter) + self.windowName, cv2.WINDOW_AUTOSIZE, croppedOriginal, cropOriginal_width, cropOriginal_height, wins_posX+cropped_img_width, wins_posY)
        print("cropped original width : {}, cropped image height : {}".format(cropOriginal_width, cropOriginal_height))

        self.similarity = comImg.mse(croppedScreen, croppedOriginal)
        # similarity = ssim(croppedScreen, croppedOriginal, multichannel=True) #ssim but bugy
        print('diffenrence : {}'.format(self.similarity))
        print("image counter : {}".format(self.counter))