import tensorflow as tf
from tensorflow.python.tools.freeze_graph import freeze_graph
from core import simple_mnist_cnn
from core import MNISTBatch
import math
import argparse
import os

def calculate_learning_rate(step=0, lr_max=1e-6, lr_min=1e-10, lr_decay=2e4):
    return lr_min + (lr_max - lr_min) * math.exp(-step / lr_decay)

def print_header():
    print('{}|{}|{}|{}|{}'.format('epoch', 'train loss','test loss',
        'train accuracy', 'test accuracy'))

def print_progress(epoch, train_loss, test_loss, train_accuracy, test_accuracy):
    print('{:03d}{:2}|{:4.4f}{:4}|{:4.4f}{:3}|{:.4f}{:8}|{:.3f}{:11} \
        '.format(epoch, '', train_loss, '', test_loss, '', train_accuracy, '',
        test_accuracy, ''))

def parse_args():
    parser = argparse.ArgumentParser(description='Traing Script for MNIST')
    parser.add_argument('--save', dest='save_dir', type=str, default='/tmp/mnist')
    parser.add_argument('--epochs', dest='epochs', type=int, default=600)
    parser.add_argument('--steps', dest='steps', type=int, default=100)
    parser.add_argument('--train-batch', dest='train_batch', type=int, default=100)
    parser.add_argument('--test-batch', dest='test_batch', type=int, default=1000)
    parser.add_argument('--lr-max', dest='lr_max', type=float, default=1e-6)
    parser.add_argument('--lr-min', dest='lr_min', type=float, default=1e-10)
    parser.add_argument('--lr-decay', dest='lr_decay', type=float, default=2e4)
    parser.add_argument('--checkpoint', dest='checkpoint', type=int, default=100)
    return parser.parse_args()

def main():
    args = parse_args()
    train_dir = os.path.join(args.save_dir, 'train')
    test_dir = os.path.join(args.save_dir, 'test')
    ckpt_dir = os.path.join(args.save_dir, 'ckpt')
    freeze_dir = os.path.join(args.save_dir, 'freeze')

    os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' # Do not clutter training progress.

    tf.reset_default_graph()

    learning_rate = tf.placeholder(tf.float32)

    input_tensor, logits, labels = simple_mnist_cnn()

    mnist = MNISTBatch()

    saver = tf.train.Saver()

    with tf.name_scope('output'):
        softmax = tf.nn.softmax(logits, name='softmax')

    with tf.name_scope('cross_entropy'):
        cross_entropy = tf.reduce_mean(tf.losses.sparse_softmax_cross_entropy(logits=logits, labels=labels))
        tf.summary.scalar('cross_entropy', cross_entropy)

    with tf.name_scope('accuracy'):
        correct_prediction = tf.equal(tf.argmax(logits, 1), labels)
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
        tf.summary.scalar('accuracy', accuracy)

    with tf.name_scope('train'):
        train_step = tf.train.AdamOptimizer(learning_rate).minimize(cross_entropy)

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        train_writer = tf.summary.FileWriter(train_dir)
        test_writer = tf.summary.FileWriter(test_dir)

        train_writer.add_graph(sess.graph)
        summary = tf.summary.merge_all()

        test_tensor, test_labels = mnist.test(batch_size=args.test_batch)

        print_header()
        for epoch in range(args.epochs):
            train_tensor, train_labels = mnist.train(batch_size=args.train_batch)

            [train_loss, train_accuracy, train_summary] = sess.run([cross_entropy, accuracy, summary],
                feed_dict={input_tensor: train_tensor, labels: train_labels})

            [test_loss, test_accuracy, test_summary] = sess.run([cross_entropy, accuracy, summary],
                feed_dict={input_tensor: test_tensor, labels: test_labels})

            train_writer.add_summary(train_summary, epoch)
            test_writer.add_summary(test_summary, epoch)
            print_progress(epoch, train_loss, test_loss, train_accuracy, test_accuracy)

            for step in range(args.steps):
                global_step = step + epoch * args.steps
                lr = calculate_learning_rate(global_step, args.lr_max, args.lr_min, args.lr_decay)
                sess.run([train_step], feed_dict={input_tensor: train_tensor, labels: train_labels, learning_rate: lr})

            if epoch % args.checkpoint is 0:
                saver.save(sess, os.path.join(ckpt_dir, 'model_' + str(epoch) + '.ckpt'))

        ckpt_file = saver.save(sess, os.path.join(ckpt_dir, 'model'), global_step=global_step)

        tf.train.write_graph(sess.graph.as_graph_def(), freeze_dir, 'model.pb', as_text=False)

        freeze_graph(os.path.join(freeze_dir, 'model.pb'), '', True, os.path.join(ckpt_dir, ckpt_file),
            'output/softmax', 'save/restore_all', 'save/Const:0', os.path.join(freeze_dir, 'frozenmodel.pb'), True, '')

if __name__ == '__main__':
    main()


import mnist
import warnings
from time import gmtime, strftime
from timeit import default_timer as timer

warnings.filterwarnings("ignore")

def Train_Network(n_network, n):

    iterations = n

    print("Preparing Training Data Set.")
    training_images = [x.reshape(1, x.shape[0] * x.shape[1]).tolist()[0] for x in mnist.train_images()]
    print("Preparing Training Data Set Completed.\n")

    print("Training Start.")
    training_labels = mnist.train_labels()

    answers = [[0 for _ in range(10)] for _ in range(len(training_labels))]

    for x in range(len(answers)):
        answers[x][training_labels[x]] = 1

    training_start = timer()

    for y in range(iterations):

        iteration_start = timer()

        for x in range(len(training_images)):
            n_network.train(training_images[x], answers[x])

        iteration_end = timer()

        print("{0:.1f}".format((y + 1) / iterations * 100) + "% Completed. Elapsed Time:",
              "{0:.1f}".format(iteration_end - iteration_start) + "s. Remaining Time ~",
              "{0:.1f}".format((iterations - y - 1) * (iteration_end - iteration_start)) + "s.")

    training_end = timer()
    print("Training End. Elapsed time: " + strftime("%Hh, %Mm, %Ss.", gmtime(training_end - training_start)) + "\n")

def Test_Network(n_network):
    print("Preparing Test Data Set.")
    test_images = [x.reshape(1, x.shape[0] * x.shape[1]).tolist()[0] for x in mnist.test_images()]
    print("Preparing Test Data Set Completed.\n")
    print("Testing Start.")

    test_labels = mnist.test_labels()

    successes = 0

    for x in range(len(test_images)):
        output = n_network.feedforward(test_images[x])

        pos = 0
        max_probability = 0
        for z in range(10):
            if output[z] > max_probability:
                max_probability = output[z]
                pos = z

        if pos == test_labels[x]:
            successes += 1

    print("Testing End.\n")
    print("Successes : {0:.2f}%".format(successes / len(test_labels) * 100))

def Test_Network_With_Image(n_network, image):
    import png

    print("Gathering Image Metadata.\n")
    reader = png.Reader(filename=image)
    w, h, pixels, metadata = reader.read_flat()
    pixels = [pixels[x] for x in [x * 4 for x in range(w * h)]]
    output = n_network.feedforward(pixels)

    position = 0
    max_probability = 0

    for x in range(len(output)):
        if output[x] > max_probability:
            max_probability = output[x]
            position = x

    print("Prediction: {0}".format(position) + " Certainty: {0:.2f}".format(output[position] * 100) + "%")


#!/usr/local/bin/python3

import os
import glob
import imp

def GetGeneratorList():
    # Get list of generators
    genList = []
    path = "./Generators/"
    for infile in glob.glob(os.path.join(path, "*.py")):
        infile = os.path.splitext(os.path.basename(infile))[0]
        if not infile.startswith("_"):
            genList.append(infile)

    print("+=-- List --=+")
    for gen in genList:
        print(gen)
    print("+=-----------=+\n")
    return genList

def OnRoll(userRequest,numRequested):
    genFile = "./Generators/" + userRequest.strip() + ".py"
    numRolls = int(numRequested)

    x = imp.load_source("generator", genFile)
    z = x.generator()

    results = []
    for _ in range(numRolls):
        results.append(z.start())
    results.sort()

    print("+=-- Rolls --=+")
    for result in results:
        print(result)
    print("+=-----------=+\n")

if __name__ == "__main__":
    tableList=GetGeneratorList()
    userRequest = input("Which table to roll?: ")
    numRequested = input("And how many do you want?: ")
    OnRoll(userRequest,numRequested)


# -*- Elementary Web Scraping -*-

import time
from urllib.robotparser import RobotFileParser
import requests
import bs4
from bs4 import BeautifulSoup
import pandas as pd

#In the first web scraper, I extracted the plot summary table from the Smithsonian Tropical Research Institute webpage
#and converted the table to DataFrame for analysis:

#Parse webpage information:
target_url='http://www.ctfs.si.edu/plots/summary/'
robot_url='http://www.si.edu/robots.txt'
rfp=RobotFileParser(url=robot_url)
rfp.read()
if not rfp.can_fetch('*', target_url):
    raise RuntimeError('Website disallows')
req=requests.get(target_url)
req.raise_for_status() #raise an error if request was not ok
soup=bs4.BeautifulSoup(req.text,'lxml')

#To extract the plot summary table, first inspect elements of column headers and extract column headers for DataFrame:
column_headers=[td.getText() for td in soup.findAll('td',class_='tgrid_label')]

#Then extract the table rows and corresponding data in each cell of the row:
data_rows=soup.findAll('tr')[6:-1]
table_data=[[td.getText() for td in data_rows[i].findAll('td')]
            for i in range(len(data_rows))]

#Use pandas to convert table data to data frame for analysis:
df1=pd.DataFrame(table_data,columns=column_headers)

#Get rid of null rows:
df1=df1[df1['Plot Size'].notnull()]
df1

#In the second web scraper, I extracted from Craiglist 100 auto sale prices (>= $5,000) in Central NJ.

#Parse webpage information:
target_url='http://cnj.craigslist.org/search/cto?srchType=T&min_price=5000'
robot_url='http://cnj.craigslist.org/robots.txt'
rfp=RobotFileParser(url=robot_url)
rfp.read()
if not rfp.can_fetch('*', target_url):
    raise RuntimeError('Website disallows')
req=requests.get(target_url)
req.raise_for_status() #raise an error if request was not ok
soup=bs4.BeautifulSoup(req.text,'lxml')

#Extract the price tag information:
price_found=soup.select('span.price')
#Craiglist has two same price tags for each auto item, and we only need to look at one.
price_found=price_found[1::2]
prices=[]
for i in range(len(price_found)):
    prices.append(price_found[i].string)
values=prices

#Extract the auto information (model,year,etc.) of the auto on sale:
info_found=soup.select('a.hdrlnk')
info=[]
for i in range(len(info_found)):
    info.append(info_found[i].string)
keys=info

#Convert the above extracted information to DataFrame:
df2=pd.DataFrame({'Auto Information':keys,'Listed Price':values})
df2

#In the third scraper, I followed the class example and built a scraper base class
#with subclasses to extract futures prices from Bloomberg website.

class FuturesScraper(object):

    def __init__(self,futures):
        self.data = {}
        self.futures = futures

    def scrape(self):
        futures_prices = self.get_futures_prices()
        print(self.futures)
        print(futures_prices)
        for ticker, price in zip(self.futures,futures_prices):
            self.data[ticker]=price
        print('Done!')

    @staticmethod
    def get_futures_price(symbol):
        target_url = 'http://www.bloomberg.com/quote/'+ symbol
        robot_url= 'http://www.bloomberg.com/robots.txt'
        rfp=RobotFileParser(url=robot_url)
        rfp.read()
        if not rfp.can_fetch('*',target_url):
            raise RuntimeError('Bloomberg disallows')
        req=requests.get(target_url)
        req.raise_for_status()
        soup=bs4.BeautifulSoup(req.text,'lxml')
        found=soup.find('div',class_='price')
        if not found:
            raise RuntimeError('Could not find price in soup - changed?')
        return found.string

    def get_futures_prices(self):
        prices=[]
        for symbol in self.futures:
            time.sleep(1)
            prices.append(self.get_futures_price(symbol))
        return prices
americas_futures = 'DM1:IND,ES1:IND,NQ1:IND,IS1:IND,SCT1:IND,PT1:IND,BZ1:IND,LB1:IND'.split(',')
americas_futures

fs=FuturesScraper(americas_futures)
fs.scrape()
print(fs.data)


#!/usr/bin/env python

import json
import os

file_paths = [
    "/usr/lib/code/product.json",
    "/usr/share/codium/resources/app/product.json"
]

fileChanged = False

for path in file_paths:
    print('Checking for file: {}'.format(path))
    if os.path.isfile(path):
        with open(path, "r") as f:
            config = json.load(f)
            config["extensionsGallery"]["serviceUrl"] = "https://marketplace.visualstudio.com/_apis/public/gallery"
            config["extensionsGallery"]["cacheUrl"] = "https://vscode.blob.core.windows.net/gallery/index"
            config["extensionsGallery"]["itemUrl"] = "https://marketplace.visualstudio.com/items"

        with open(path, "w") as f:
            json.dump(config, f, indent=2)

        print('  Changed marketplace in {}'.format(path))
        fileChanged = True

if not fileChanged:
    print('Warning: No marketplaces could be found to be changed')
    exit(1)


#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
@Author      : xueqiang.liu
@contact     : xqliu@dragondropinn.com
@Date        : 2019/6/24
@Description :fastq文件质控
'''

import os
import time
import sys
from profile import Profile,timefly

var_path = Profile()

@timefly
def QC(prefix, threads):
    path = 'DataQC'
    os.makedirs(path,exist_ok=True)
    fastq = [ i for i in os.listdir() if prefix in i]
    fastq1 = ''.join([i for i in fastq if '1.f' in i])
    fastq2 = ''.join([i for i in fastq if '2.f' in i])
    os.system("{fastp} -w {th} --in1 {f1} --out1 {p}/{T}_R1.clean.fastq.gz "
              "--in2 {f2} --out2 {p}/{T}_R2.clean.fastq.gz "
              "--low_complexity_filter --correction --length_required=70 "
              "--html {p}/{T}.QCReport.html --json {p}/{T}.json --report_title {p}/{T}.QCReport "
              " >{p}/{T}.fastp.log 2>&1".format(T=prefix, p=path,f1=fastq1, f2=fastq2, th=threads,**var_path))
    os.system("python {summary4fastp} {p}/{T}.json > {p}/{T}.QCsummary.xls ".format(T=prefix,p=path,**var_path))

if __name__  == '__main__':
    if len(sys.argv) < 2:
        print('\nusage:  python {} [prefix] [threads]\n'.format(sys.argv[0]))
        sys.exit(1)
    prefix = sys.argv[1]
    threads = sys.argv[2]
    QC(prefix, threads)


import sys
import random
import math
import itertools
from fractions import Fraction

from scipy.spatial import Delaunay

sys.path.append("../../../")
from dan.lib.helper import *
from solid import *
from solid.utils import *

class LayerStructure:
    def get_layers(self, shrink_offset=0):
        layers = []
        for h in range(self.height):
            layer = []
            for a in range(self.sections):
                angle, dist = self.calc(h, a)
                dist -= shrink_offset
                layer.append(
                    Vec3(math.cos(angle) * dist, math.sin(angle) * dist, h * 300)
                )
            layers.append(layer)
        return layers

class RandomMeshStructure:
    def torus_dist2(self, p1, p2, w, h):
        return (
            min(abs(p1.x - p2.x), w - abs(p1.x - p2.x)) ** 2
            + min(abs(p1.y - p2.y), h - abs(p1.y - p2.y)) ** 2
        )

    def get_object(self, shrink_offset=0, seed=None):
        if not seed:
            seed = self.seed
        if not seed:
            seed = 0
        random.seed(seed)

        width = self.avg_rad * 2 * math.pi
        points = []

        # Put points along top and bottom
        edge_count = int(math.floor(width / self.min_point_dist))
        edge_width = width / edge_count
        for i in range(edge_count):
            points.append(Vec3(edge_width * i, 0))
            points.append(Vec3(edge_width * i, self.height))

        edge_points = [i for i in range(len(points))]

        # Fill the area with points
        while True:
            found = False
            for i in xrange(1000):
                point = Vec3(random.uniform(0, width), random.uniform(0, self.height))
                collides = False
                for p in points:
                    if (
                        self.torus_dist2(point, p, width, self.height)
                        < self.min_point_dist ** 2
                    ):
                        collides = True
                        break

                if not collides:
                    points.append(point)
                    found = True
                    break

            if not found:
                break

        # Bin the points into veritcal columns
        dsects_count = 4
        dsects_inc = width / dsects_count
        dsects = [[] for i in range(dsects_count)]
        for p in points:
            slot = int(p.x / dsects_inc)
            dsects[slot].append(p)

        # Create the Delaunay areas
        dareas = []
        ref_lists = [{} for i in range(dsects_count)]
        for i in range(dsects_count):
            if i - 1 < 0:
                bindex = i - 1 + dsects_count
                before = dsects[bindex]
                before = [Vec3(p.x - width, p.y) for p in before]
            else:
                bindex = i - 1
                before = dsects[i - 1]

            if i + 1 >= dsects_count:
                aindex = i + 1 - dsects_count
                after = dsects[aindex]
                after = [Vec3(p.x + width, p.y) for p in after]
            else:
                aindex = i + 1
                after = dsects[i + 1]

            for j in range(len(before)):
                ref_lists[i][j] = (bindex, j, True)

            for j in range(len(dsects[i])):
                ref_lists[i][j + len(before)] = (i, j, False)

            for j in range(len(after)):
                ref_lists[i][j + len(dsects[i]) + len(before)] = (aindex, j, True)

            dareas.append(before + dsects[i] + after)

        # Calculate Tris
        tris = []
        for i in range(dsects_count):
            tris.append(
                Delaunay([p.to_list()[:2] for p in dareas[i]], qhull_options="QJ")
            )

        def find_index(i, p):
            if p in ref_lists[i]:
                l, li, fake = ref_lists[i][p]
                point = dsects[l][li]

            for cpi in range(len(points)):
                if point is points[cpi]:
                    return cpi, fake

        # Make list of real tris
        real_tris = []
        for i in range(dsects_count):
            for t in tris[i].simplices:
                a, af = find_index(i, t[0])
                b, bf = find_index(i, t[1])
                c, cf = find_index(i, t[2])
                if af and bf and cf:
                    continue
                if a in edge_points and b in edge_points and c in edge_points:
                    continue
                real_tris.append([c, b, a])

        # Remove duplicates
        real_tris.sort()
        real_tris = list(k for k, _ in itertools.groupby(real_tris))
        real_tris = filter(lambda x: None not in x, real_tris)

        def realp(point):
            dist = self.calc(point.x / width, point.y / self.height) - shrink_offset
            a = point.x / width * math.pi * 2
            return Vec3(math.cos(a) * dist, math.sin(a) * dist, point.y)

        real_points = [realp(p) for p in points]

        real_points.append(Vec3(0, 0, 0))
        real_points.append(Vec3(0, 0, self.height))

        for i in range(edge_count):
            real_tris.append(
                [len(real_points) - 2, i * 2, (i * 2 + 2) % (edge_count * 2)]
            )

            real_tris.append(
                [(i * 2 + 3) % (edge_count * 2), i * 2 + 1, len(real_points) - 1]
            )

        return polyhedron(points=[p.to_list() for p in real_points], faces=real_tris)

class WallPiece:
    def get_object(self):
        points = list(self.points)  # Shallow Copy
        tris = Delaunay([p.to_list()[:2] for p in points], qhull_options="Qt")
        tris = [[t[0], t[1], t[2]] for t in tris.simplices]
        tris = [list(reversed(l)) for l in tris]

        edge_count = {}
        for tri in tris:
            for i in range(3):
                a = tri[i]
                b = tri[(i + 1) % 3]
                key = (a, b)
                edge = edge_count.get(key)
                if edge == None:
                    key = (b, a)
                    edge = edge_count.get(key)

                if edge == None:
                    edge_count[key] = 1
                else:
                    edge_count[key] = edge_count[key] + 1

        edges = []
        for key, value in edge_count.iteritems():
            if value == 1:
                edges.append(key[0])
                edges.append(key[1])

        edges = list(set(edges))
        avg_point = reduce(lambda x, i: x + points[i], edges, Vec3())
        avg_point /= len(edges)
        avg_point.z = 0

        def get_angle(a):
            x = a.x - avg_point.x
            y = a.y - avg_point.y
            return atan2(y, x)

        def sort_fun(a, b):
            return 1 if get_angle(points[a]) < get_angle(points[b]) else -1

        edges.sort(sort_fun)

        new_points = []
        for p in edges:
            np = points[p].copy()
            np.z = 0
            new_points.append(np)

        for i in range(len(new_points)):
            t1 = [edges[i], len(points) + i, len(points) + ((i + 1) % len(new_points))]
            t2 = [
                edges[i],
                len(points) + ((i + 1) % len(new_points)),
                edges[(i + 1) % len(new_points)],
            ]
            tris.append(t1)
            tris.append(t2)

        points += new_points

        points.append(avg_point)
        for i in range(len(new_points)):
            tris.append(
                list(
                    reversed(
                        [
                            len(points) - 1,
                            len(points) - 1 - len(new_points) + i,
                            len(points)
                            - 1
                            - len(new_points)
                            + ((i + 1) % len(new_points)),
                        ]
                    )
                )
            )

        # return union() ( [translate( points[v].to_list() ) ( linear_extrude( 10 ) ( text( str(i), size=100, valign="center", halign="center" ) ) ) for i, v in enumerate( edges ) ])
        return polyhedron(points=[p.to_list() for p in points], faces=tris)

    def get_list(self):
        sorted_points = sorted(self.points, lambda a, b: cmp(a.y, b.y))
        sorted_points = filter(lambda x: x.z != 0, sorted_points)
        out = ""
        in_in_mm = 0.0393701
        win = self.width * in_in_mm
        hin = self.height * in_in_mm
        for i in xrange(len(sorted_points)):
            p = sorted_points[i].copy()
            height_in_mm = p.z
            p *= in_in_mm

            def convert_to_fraction(v):
                main = int(v * 16) / 16
                fraction = Fraction(int(v * 16) / 16.0 - main)
                return str(main) + " " + str(fraction)

            top_left = ", ".join([convert_to_fraction(v) for v in p.to_list()[:-1]])
            bottom_right = Vec3(win - p.x, hin - p.y, p.z)
            bottom_right = ", ".join(
                [convert_to_fraction(v) for v in bottom_right.to_list()[:-1]]
            )

            line = [i] + [top_left, bottom_right, convert_to_fraction(p.z)]
            line = [str(e) for e in line]
            out += " || ".join(line) + "\n"
        return out


#!/usr/bin/env python3.6
from skimage.measure import label, regionprops

print('hi')

import torch.nn.functional as F
import matplotlib.pyplot as plt
from pathlib import Path
from PIL import Image
import numpy as np
import torch
from torch.autograd import Variable
import sys
import os
import csv

sys.path.append('/home/eljurros/spare-workplace/surface-loss-master')
from utils import dice_coef, dice_batch, save_images, tqdm_, haussdorf, probs2one_hot, class2one_hot, numpy_haussdorf
root = '/media/eljurros/Transcend/Decathlone/benchmark/isles/FOLD_1'
net_path = '/media/eljurros/Transcend/Decathlone/benchmark/isles/FOLD_1/results/eroded_distance/best2.pkl'
net = torch.load(net_path, map_location=torch.device('cpu'))
#print(net)
fieldnames = ['SLICE_ID', 'dice','haus',  'c_error']
n_classes = 4
#assert os.path.exists(os.path.join(net_path.split(os.path.basename(net_path))[0], 'predictions'))== False
print('started this stupid work')
exp_path = net_path.split('/best2.pkl')[0]
name =os.path.basename(exp_path)
folder_path = Path(exp_path, 'CSV_RESULTS')

folder_path.mkdir(parents=True, exist_ok=True)
file_path = os.path.join(exp_path, name)
fold_clean_H1 = open(os.path.join(folder_path, '{}_clean.csv'.format(name)), "w")
fold_all_H1 = open(os.path.join(folder_path, '{}_all.csv'.format(name)), "w")

fold_all_H1.write(f"file, dice, haussdorf,connecterror \n")
fold_clean_H1.write(f"file, dice, haussdorf,connecterror \n")

path=os.path.join(net_path.split(os.path.basename(net_path))[0])

pred_path = Path(path,'predictions')
gt_path = Path(path,'gt')

pred_path.mkdir(parents=True, exist_ok=True)
gt_path.mkdir(parents=True, exist_ok=True)

for _,_,files in os.walk(os.path.join(root, 'img')):

    print('walking into', os.path.join(root, 'img'))
    for file in files:
        print(file)
        image = np.array(Image.open(os.path.join(root,'img', file)))
        gt = np.array(Image.open(os.path.join(root,'gt', file)))
        if len(np.unique(gt)) >0:
            #print('infering {} of shape {} and classes {}, max {} and min {} '.format( file, image.shape, np.unique(gt), image.max(), image.min()))
            image = image.reshape(-1, 5, 256, 256)/255.00
            image = torch.tensor(image, dtype=torch.float)
            image = Variable(image, requires_grad=True)
            pred = net(image)
            pred = F.softmax(pred, dim=1).to('cpu')
            predicted_output = probs2one_hot(pred.detach())
            np.save(os.path.join(path, 'predictions', '{}'.format(file)), pred.to('cpu').detach().numpy())
            dice = dice_coef(predicted_output.to('cpu'), class2one_hot(torch.tensor(gt).to('cpu'), n_classes))
            hauss = haussdorf(predicted_output,class2one_hot(torch.tensor(gt).to('cpu'), n_classes))
            plt.imsave(os.path.join(path, 'predictions', '{}.png'.format(file.split('.npy')[0])), np.argmax(predicted_output,1)[0])
            plt.imsave(os.path.join(path, 'gt', '{}.png'.format(file.split('.npy')[0])), gt)

            pred_label = len(np.unique(label(np.array(pred.argmax(axis = 1).detach().numpy()))))
            gt_label = len(np.unique(label(gt)))
            pred_label = len(np.unique(label(predicted_output[0][1])))
            gt_label = len(np.unique(label(class2one_hot(torch.tensor(gt), n_classes)[0][2])))
            error = np.abs(pred_label - gt_label)

            print(f"{file}, {np.float(dice[0][2])}, {np.float(hauss[0][2])},{np.float(error)} \n")

            fold_all_H1.write(f"{file}, {np.float(dice[0][2])}, {np.float(hauss[0][2])},{np.float(error)} \n")

            if len(np.unique(gt)) == 2:
                fold_clean_H1.write(f"{file}, {np.float(dice[0][2])}, {np.float(hauss[0][2])},{np.float(error)} \n")
                fold_clean_H1.flush()
            #folders.write("hi")
            fold_all_H1.flush()


str = input()
indexlist = list(eval(input()))
notes = []
#notes for pairs which can change with each other
lists = []
#lists for noting which has emerged
for i in range(len(indexlist)):
    if not (indexlist[i][0] in lists and indexlist[i][1] in lists):
        notes.append(indexlist[i])
        lists.append(indexlist[i][0])
        lists.append(indexlist[i][1])
    else:
        for k in range(len(notes)):
            templist = list(notes[k])
            for j in range(len(templist)):
                if templist[j]==indexlist[i][0] :
                    templist.append(indexlist[i][1])
                    lists.append(indexlist[i][1])
                elif templist[j]==indexlist[i][1]:
                    templist.append(indexlist[i][0])
                    lists.append(indexlist[i][0])
            notes[k] = templist
strs = list(str)
for i in range(len(notes)):
    index = list(notes[i])
    letters = list()
    for j in range(len(index)):
        letters.append(strs[index[j]])
    letters.sort()
    index.sort()
    for j in range(len(index)):
        strs[index[j]]=letters[j]
str = ' '.join(strs)
print(str)


'''
License:
  "NASA Open Source Agreement 1.3"

Description:
  Utility module for ESPA project.
  This is a shared module to hold simple utility functions.

History:
  Original implementation by David V. Hill, USGS/EROS
  Updated Jan/2014 by Ron Dilley, USGS/EROS
'''

import os
import sys
import datetime
import commands
import random
import re
from email.mime.text import MIMEText
from smtplib import SMTP

# local objects and methods
import settings

# ============================================================================
#def scenecache_is_alive(url='http://edclpdsftp.cr.usgs.gov:50000/RPC2'):
#    """Determine if the specified url has an http server
#    that accepts POST calls
#
#    Keyword args:
#    url -- The url of the server to check
#
#    Return:
#    True -- If the contacted server is alive and accepts POST calls
#    False -- If the server does not accept POST calls or the
#             server could not be contacted
#    """
#
#    try:
#        return urllib2.urlopen(url, data="").getcode() == 200
#    except Exception, e:
#        if settings.DEBUG:
#            print("Scene cache could not be contacted")
#            print(e)
#        return False

# ============================================================================
#def scenecache_client():
#    """Return an xmlrpc proxy to the caller for the scene cache
#
#    Returns -- An xmlrpclib ServerProxy object
#    """
#    url = 'http://edclpdsftp.cr.usgs.gov:50000/RPC2'
#    # url = os.environ['ESPA_SCENECACHE_URL']
#    if scenecache_is_alive(url):
#        return xmlrpclib.ServerProxy(url)
#    else:
#        msg = "Could not contact scene_cache at %s" % url
#        raise RuntimeError(msg)

# ============================================================================
def date_from_doy(year, doy):
    '''Returns a python date object given a year and day of year'''

    d = datetime.datetime(int(year), 1, 1) + datetime.timedelta(int(doy) - 1)

    if int(d.year) != int(year):
        raise Exception("doy [%s] must fall within the specified year [%s]" %
                        (doy, year))
    else:
        return d

# ============================================================================
def is_number(s):
    '''Determines if a string value is a float or int.

    Keyword args:
    s -- A string possibly containing a float or int

    Return:
    True if s is a float or int
    False if s is not a float or int
    '''
    try:
        float(s)
        return True
    except ValueError:
        return False

# ============================================================================
def execute_cmd(cmd):
    '''
    Description:
      Execute a command line and return the terminal output or raise an
      exception

    Returns:
        output - The stdout and/or stderr from the executed command.
    '''

    output = ''

    (status, output) = commands.getstatusoutput(cmd)

    if status < 0:
        message = "Application terminated by signal [%s]" % cmd
        if len(output) > 0:
            message = ' Stdout/Stderr is: '.join([message, output])
        raise Exception(message)

    if status != 0:
        message = "Application failed to execute [%s]" % cmd
        if len(output) > 0:
            message = ' Stdout/Stderr is: '.join([message, output])
        raise Exception(message)

    if os.WEXITSTATUS(status) != 0:
        message = "Application [%s] returned error code [%d]" \
                  % (cmd, os.WEXITSTATUS(status))
        if len(output) > 0:
            message = ' Stdout/Stderr is: '.join([message, output])
        raise Exception(message)

    return output

# ============================================================================
def strip_zeros(value):
    '''
    Description:
      Removes all leading zeros from a string
    '''

    while value.startswith('0'):
        value = value[1:len(value)]
    return value

# ============================================================================
def get_cache_hostname():
    '''
    Description:
      Poor mans load balancer for accessing the online cache over the private
      network
    '''

    host_list = settings.ESPA_CACHE_HOST_LIST

    def check_host_status(hostname):
        cmd = "ping -q -c 1 %s" % hostname

        try:
            execute_cmd(cmd)
        except Exception:
            return -1
        return 0

    def get_hostname():
        hostname = random.choice(host_list)
        if check_host_status(hostname) == 0:
            return hostname
        else:
            for x in host_list:
                if x == hostname:
                    host_list.remove(x)
            if len(host_list) > 0:
                return get_hostname()
            else:
                raise Exception("No online cache hosts available...")

    return get_hostname()

def send_email(recipient, subject, body):
    '''Sends an email to a receipient on the behalf of espa'''

    if not validate_email(recipient):
        raise TypeError("Invalid email address provided:%s" % recipient)

    msg = MIMEText(body)
    msg['Subject'] = subject
    msg['To'] = recipient
    msg['From'] = settings.ESPA_EMAIL_ADDRESS
    s = SMTP(host=settings.ESPA_EMAIL_SERVER)
    s.sendmail(msg['From'], msg['To'], msg.as_string())
    s.quit()

    return True

def validate_email(email):
    '''Compares incoming email address against regular expression to make sure
    its at least formatted like an email

    Keyword args:
    email -- String to validate as an email address

    Return:
    True if the string is a properly formatted email address
    False if not
    '''
    #pattern = '^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$'
    #some clown used a single quote in his email address... sigh.
    email = email.replace("'", "\'")
    pattern = r'^[A-Za-z0-9._%+-\\\']+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$'
    return re.match(pattern, email.strip())

def tar_files(tarred_full_path, file_list, gzip=False):
    '''
    Description:
      Create a tar ball (*.tar) of the specified file(s).
      OR
      Create a tar.gz ball (*.tar.gz) of the specified file(s).
    '''

    flags = '-cf'
    target = '%s.tar' % tarred_full_path

    # If zipping was chosen, change the flags and the target name
    if gzip:
        flags = '-czf'
        target = '%s.tar.gz' % tarred_full_path

    cmd = ['tar', flags, target]
    cmd.extend(file_list)
    cmd = ' '.join(cmd)

    output = ''
    try:
        output = execute_cmd(cmd)
    except Exception:
        msg = "Error encountered tar'ing file(s): Stdout/Stderr:"
        if len(output) > 0:
            msg = ' '.join([msg, output])
        else:
            msg = ' '.join([msg, "NO STDOUT/STDERR"])
        # Raise and retain the callstack
        raise Exception(msg)

    return target

def gzip_files(file_list):
    '''
    Description:
      Create a gzip for each of the specified file(s).
    '''

    # Force the gzip file to overwrite any previously existing attempt
    cmd = ['gzip', '--force']
    cmd.extend(file_list)
    cmd = ' '.join(cmd)

    output = ''
    try:
        output = execute_cmd(cmd)
    except Exception:
        msg = "Error encountered compressing file(s): Stdout/Stderr:"
        if len(output) > 0:
            msg = ' '.join([msg, output])
        else:
            msg = ' '.join([msg, "NO STDOUT/STDERR"])
        # Raise and retain the callstack
        raise Exception(msg)

def checksum_local_file(filename):
    '''
    Description:
      Create a checksum for the specified file.
    '''

    cmd = ' '.join(['cksum', filename])

    cksum_result = ''
    try:
        cksum_result = execute_cmd(cmd)
    except Exception:
        msg = "Error encountered generating checksum: Stdout/Stderr:"
        if len(cksum_result) > 0:
            msg = ' '.join([msg, cksum_result])
        else:
            msg = ' '.join([msg, "NO STDOUT/STDERR"])
        # Raise and retain the callstack
        raise Exception(msg)

    return cksum_result


from prometheus_client import start_http_server, Gauge
import sys
import subprocess
import time

port_check_metric = Gauge('port_check','Check if port is working',["port","host"])

targets = eval(open("/opt/prometheus-exporters/port_exporter/config.json").read())

if __name__ == "__main__":
    start_http_server(9001)

    while True:
        for host, ports in targets.items():
            for port in ports:
                cmd="nc -vz " + host + " " + port +  " 2>&1 | grep Connected > /dev/null && echo 1 || echo 0"
                result=subprocess.check_output(cmd, shell=True, universal_newlines=True)
                result=result.strip()
                #print('Result is : ')
                #print(result)

                if result == "1" :
                    port_check_metric.labels(port=port,host=host).set(1)
                else:
                    port_check_metric.labels(port=port,host=host).set(0)

        time.sleep(120)


# -*- coding: utf-8 -*-
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
# from modules.db_util import *
# from modules.update_db import *
from selenium import webdriver
from bs4 import BeautifulSoup
import pandas as pd
import urllib.request as req
import sys
import re
# import chromedriver_binary
import pathlib
current_dir = pathlib.Path(__file__).resolve().parent
sys.path.append(str(current_dir) + '/../')

class Illusie300:
    baseurl = "http://mb.palemo.com/brand.php?brand=11&id=illusie300"
    _result_df = pd.DataFrame(columns=['store_name', 'address'])

    @staticmethod
    def _AppendItemToDataFrame(storeName: str, storeAddress: str):
        """
        店舗名と住所を受け取り、クラス変数のDataFrameに追加する

        Parameters
        -----
        storeName : str
            店舗名
        storeAddress : str
            住所
        """
        tmp_se = pd.Series([storeName, storeAddress],
                           index=Illusie300._result_df.columns)
        Illusie300._result_df = Illusie300._result_df.append(
            tmp_se, ignore_index=True)

    @staticmethod
    def _OpenURLWithSelenium(url: str):
        """
        URLをSeleniumで開く

        Parameter
        -----
        url : str
            開きたいページのURL

        Return
        -----
        browser : WebDriver
            ページを開いた後のWebDriverオブジェクト
        """
        browser = webdriver.Chrome()
        browser.get(url)
        return browser

    @staticmethod
    def _OpenURLWithBeautifulSoup(url: str):
        """
        URLをBeautifulSoup(パーサーはhtml.parser)で開く

        Parameter
        -----
        url : str
            開きたいページのURL

        Return
        -----
        soup : BeautifulSoup
            ページを開いた後のBeautifulSoupオブジェクト
        """
        res = req.urlopen(url)
        soup = BeautifulSoup(res, "html.parser")
        return soup

    @staticmethod
    def _OpenPagesourceWithBeautifulSoup(page_source):
        """
        WebDriver.pagesourceをBeautifulSoup(パーサーはhtml.parser)で開く

        Parameter
        -----
        page_source
            開きたいページのページソース

        Return
        -----
        soup : BeautifulSoup
            ページを開いた後のBeautifulSoupオブジェクト
        """
        soup = BeautifulSoup(page_source, "html.parser")
        return soup

    @staticmethod
    def getStoreInfo():
        page = Illusie300._OpenURLWithBeautifulSoup(Illusie300.baseurl)

        stores = page.find("div", {"id": "ShopList"}).find(
            "div", {"class": "ContBox"}).find_all("tr")

        for store in stores:
            storeInfo = store.find_all("td")
            storeName = storeInfo[0].find("a").text
            storeAddress = storeInfo[1].text

            storeName = "illusie300 " + storeName
            storeAddress = re.sub(".*【 MAP 】", "", storeAddress)

            Illusie300._AppendItemToDataFrame(storeName, storeAddress)

        return Illusie300._result_df

if __name__ == "__main__":
    df = Illusie300.getStoreInfo()
    # brand_id = UpdateDB.getBrandId(os.path.basename(__file__))
    # df['brand_id'] = brand_id
    # new_brand_df = df.rename(columns={0: 'store_name', 1: 'address'})
    # conn_pg = DBUtil.getConnect()
    # UpdateDB.execUpdateStandby(conn_pg, brand_id, new_brand_df)


"""Service to retrieve news articles from GDELT news tracker."""
import requests
import pandas as pd
from newspaper import Article, ArticleException

import training_scripts.domain.sampler as sampler

EXCEPTION_CAUSING_URLS = [
    'https://www.newsweek.com/',
    'https://www.forbes.com',
    'https://www.malaysiasun.com/',
]

GDELT_LATEST_UPDATE_URL = 'http://data.gdeltproject.org/gdeltv2/lastupdate.txt'

GDELT_MASTER_LIST_URL = 'http://data.gdeltproject.org/gdeltv2/masterfilelist.txt'

GDELT_COLUMNS = [
    'GlobalEventID',
    'Day',
    'MonthYear',
    'Year',
    'FractionDate',
    'Actor1Code',
    'Actor1Name',
    'Actor1CountryCode',
    'Actor1KnownGroupCode',
    'Actor1EthnicCode',
    'Actor1Religion1Code',
    'Actor1Religion2Code',
    'Actor1Type1Code',
    'Actor1Type2Code',
    'Actor1Type3Code',
    'Actor2Code',
    'Actor2Name',
    'Actor2CountryCode',
    'Actor2KnownGroupCode',
    'Actor2EthnicCode',
    'Actor2Religion1Code',
    'Actor2Religion2Code',
    'Actor2Type1Code',
    'Actor2Type2Code',
    'Actor2Type3Code',
    'IsRootEvent',
    'EventCode',
    'EventBaseCode',
    'EventRootCode',
    'QuadClass',
    'GoldsteinScale',
    'NumMentions',
    'NumSources',
    'NumArticles',
    'AvgTone',
    'Actor1Geo_Type',
    'Actor1Geo_Fullname',
    'Actor1Geo_CountryCode',
    'Actor1Geo_ADM1Code',
    'Actor1Geo_ADM2Code',
    'Actor1Geo_Lat',
    'Actor1Geo_Long',
    'Actor1Geo_FeatureID',
    'Actor2Geo_Type',
    'Actor2Geo_Fullname',
    'Actor2Geo_CountryCode',
    'Actor2Geo_ADM1Code',
    'Actor2Geo_ADM2Code',
    'Actor2Geo_Lat',
    'Actor2Geo_Long',
    'Actor2Geo_FeatureID',
    'Action2Geo_Type',
    'Action2Geo_Fullname',
    'Action2Geo_CountryCode',
    'Action2Geo_ADM1Code',
    'Action2Geo_ADM2Code',
    'Action2Geo_Lat',
    'Action2Geo_Long',
    'Action2Geo_FeatureID',
    'DATEADDED',
    'SOURCEURL',
]

def scrape_gdelt_dataset(url,
                         balance_data,
                         sample_size,
                         blacklisted_domains=EXCEPTION_CAUSING_URLS):
    """Scrape articles linked GDELT dataset and write them to files"""
    df_gdelt = _format_gdelt_dataframe(url, balance_data, sample_size)
    for _, row in df_gdelt.iterrows():
        try:
            article_dict = _build_article_dict(row, blacklisted_domains)
            yield article_dict
        except ArticleException as exception:
            print(repr(exception))

def scrape_latest_gdelt_dataset(balance_data,
                                sample_size,
                                blacklisted_domains=EXCEPTION_CAUSING_URLS):
    """Scrape latest GDELT dataset"""
    latest_dataset_url = _get_latest_gdelt_dataset_url()
    for result in scrape_gdelt_dataset(latest_dataset_url, balance_data,
                                       sample_size, blacklisted_domains):
        yield result

def scrape_latest_gdelt_datasets(n_datasets,
                                balance_data,
                                sample_size):
    """Scrape latest GDELT datasets"""
    for dataset_url in _get_number_of_gdelt_dataset_urls(n_datasets):
        for result in scrape_gdelt_dataset(dataset_url, balance_data, sample_size):
            yield result

def _get_latest_gdelt_dataset_url():
    """Get URL for latest GDELT dataset"""
    information_on_latest_dataset = requests.get(GDELT_LATEST_UPDATE_URL).text
    line_with_latest_dataset, *_ = information_on_latest_dataset.splitlines()
    *_, latest_dataset_url = line_with_latest_dataset.split()
    return latest_dataset_url

def _get_number_of_gdelt_dataset_urls(n_datasets):
    """Get URL for latest n GDELT datasets"""
    gdelt_master_list = requests.get(GDELT_MASTER_LIST_URL).text
    gdelt_master_list = gdelt_master_list.splitlines()
    gdelt_datasets = [
        l for l in gdelt_master_list if l.endswith('.export.CSV.zip')
    ]
    for dataset in gdelt_datasets[-n_datasets:]:
        *_, dataset_url = dataset.split()
        yield dataset_url

def _format_gdelt_dataframe(url, balance_data, sample_size):
    """Create dataframe from URL containing zipped CSV of GDELT data"""
    df_gdelt = pd.read_csv(url, names=GDELT_COLUMNS, delimiter='\t')
    df_gdelt = df_gdelt.sample(frac=sample_size)
    df_gdelt = df_gdelt.drop_duplicates(subset=['SOURCEURL'])
    if balance_data:
        df_gdelt = sampler.sample_dataframe(df_gdelt, 'Actor1Geo_CountryCode')
    return df_gdelt

def _extract_article_contents(url):
    """Get title and text from URL"""
    article = Article(url)
    article.download()
    article.parse()
    article_content = {
        'TITLE': article.title,
        'TEXT': article.text,
        'METADATA': article.meta_data,
    }
    return article_content

def _build_article_dict(row, blacklisted_domains):
    """Create article dictionary from dataframe row"""
    article_dict = {}
    article_dict['GDELT'] = dict(row.dropna())
    url = article_dict['GDELT']['SOURCEURL']
    if any(url.startswith(u) for u in blacklisted_domains):
        return article_dict
    article_dict['ARTICLE'] = _extract_article_contents(url)
    return article_dict


from __future__ import division
from __future__ import print_function

import time
import tensorflow as tf

from gcn.utils import *
from gcn.models import GCN, MLP

# Set random seed
seed = 123
np.random.seed(seed)
tf.set_random_seed(seed)

# Settings
flags = tf.app.flags
FLAGS = flags.FLAGS
flags.DEFINE_string('dataset', 'cora', 'Dataset string.')  # 'cora', 'citeseer', 'pubmed'
flags.DEFINE_string('model', 'gcn', 'Model string.')  # 'gcn', 'gcn_cheby', 'dense'
flags.DEFINE_float('learning_rate', 0.01, 'Initial learning rate.')
flags.DEFINE_integer('epochs', 2000, 'Number of epochs to train.')
flags.DEFINE_integer('hidden1', 16, 'Number of units in hidden layer 1.')
flags.DEFINE_float('dropout', 0.5, 'Dropout rate (1 - keep probability).')
flags.DEFINE_float('weight_decay', 5e-4, 'Weight for L2 loss on embedding matrix.')
flags.DEFINE_integer('early_stopping', 10, 'Tolerance for early stopping (# of epochs).')
flags.DEFINE_integer('max_degree', 3, 'Maximum Chebyshev polynomial degree.')

# Load data
# adj, features, y_train, y_val, y_test, train_mask, val_mask, test_mask = load_data(FLAGS.dataset)
dataset_str = FLAGS.dataset
names = ['x', 'y', 'tx', 'ty', 'allx', 'ally', 'graph']
objects = []
for i in range(len(names)):
    with open("data/ind.{}.{}".format(dataset_str, names[i]), 'rb') as f:
        if sys.version_info > (3, 0):
            objects.append(pkl.load(f, encoding='latin1'))
        else:
            objects.append(pkl.load(f))

x, y, tx, ty, allx, ally, graph = tuple(objects)
test_idx_reorder = parse_index_file("data/ind.{}.test.index".format(dataset_str))
test_idx_range = np.sort(test_idx_reorder)

if dataset_str == 'citeseer':
    # Fix citeseer dataset (there are some isolated nodes in the graph)
    # Find isolated nodes, add them as zero-vecs into the right position
    test_idx_range_full = range(min(test_idx_reorder), max(test_idx_reorder)+1)
    tx_extended = sp.lil_matrix((len(test_idx_range_full), x.shape[1]))
    tx_extended[test_idx_range-min(test_idx_range), :] = tx
    tx = tx_extended
    ty_extended = np.zeros((len(test_idx_range_full), y.shape[1]))
    ty_extended[test_idx_range-min(test_idx_range), :] = ty
    ty = ty_extended

features = sp.vstack((allx, tx)).tolil()
features[test_idx_reorder, :] = features[test_idx_range, :]
adj = nx.adjacency_matrix(nx.from_dict_of_lists(graph))

labels = np.vstack((ally, ty))
labels[test_idx_reorder, :] = labels[test_idx_range, :]

idx_test = test_idx_range.tolist()
idx_train = range(len(y))
idx_val = range(len(y), len(y)+500)

train_mask = sample_mask(idx_train, labels.shape[0])
val_mask = sample_mask(idx_val, labels.shape[0])
test_mask = sample_mask(idx_test, labels.shape[0])

y_train = np.zeros(labels.shape)
y_val = np.zeros(labels.shape)
y_test = np.zeros(labels.shape)
y_train[train_mask, :] = labels[train_mask, :]
y_val[val_mask, :] = labels[val_mask, :]
y_test[test_mask, :] = labels[test_mask, :]
import pdb; pdb.set_trace()

# Some preprocessing
features = preprocess_features(features)
if FLAGS.model == 'gcn':
    support = [preprocess_adj(adj)]
    num_supports = 1
    model_func = GCN
elif FLAGS.model == 'gcn_cheby':
    support = chebyshev_polynomials(adj, FLAGS.max_degree)
    num_supports = 1 + FLAGS.max_degree
    model_func = GCN
elif FLAGS.model == 'dense':
    support = [preprocess_adj(adj)]  # Not used
    num_supports = 1
    model_func = MLP
else:
    raise ValueError('Invalid argument for model: ' + str(FLAGS.model))

# Define placeholders
placeholders = {
    'support': [tf.sparse_placeholder(tf.float32) for _ in range(num_supports)],
    'features': tf.sparse_placeholder(tf.float32, shape=tf.constant(features[2], dtype=tf.int64)),
    'labels': tf.placeholder(tf.float32, shape=(None, y_train.shape[1])),
    'labels_mask': tf.placeholder(tf.int32),
    'dropout': tf.placeholder_with_default(0., shape=()),
    'num_features_nonzero': tf.placeholder(tf.int32)  # helper variable for sparse dropout
}

# Create model
model = model_func(placeholders, input_dim=features[2][1], logging=True)

# Initialize session
sess = tf.Session()

# Define model evaluation function
def evaluate(features, support, labels, mask, placeholders):
    t_test = time.time()
    feed_dict_val = construct_feed_dict(features, support, labels, mask, placeholders)
    outs_val = sess.run([model.loss, model.accuracy], feed_dict=feed_dict_val)
    return outs_val[0], outs_val[1], (time.time() - t_test)

# Init variables
sess.run(tf.global_variables_initializer())

cost_val = []

# Train model
for epoch in range(FLAGS.epochs):

    t = time.time()
    # Construct feed dictionary
    feed_dict = construct_feed_dict(features, support, y_train, train_mask, placeholders)
    feed_dict.update({placeholders['dropout']: FLAGS.dropout})

    # Training step
    outs = sess.run([model.opt_op, model.loss, model.accuracy], feed_dict=feed_dict)

    # Validation
    cost, acc, duration = evaluate(features, support, y_val, val_mask, placeholders)
    cost_val.append(cost)

    # Print results
    print("Epoch:", '%04d' % (epoch + 1), "train_loss=", "{:.5f}".format(outs[1]),
          "train_acc=", "{:.5f}".format(outs[2]), "val_loss=", "{:.5f}".format(cost),
          "val_acc=", "{:.5f}".format(acc), "time=", "{:.5f}".format(time.time() - t))

    if epoch > FLAGS.early_stopping and cost_val[-1] > np.mean(cost_val[-(FLAGS.early_stopping+1):-1]):
        print("Early stopping...")
        break

print("Optimization Finished!")

# Testing
test_cost, test_acc, test_duration = evaluate(features, support, y_test, test_mask, placeholders)
print("Test set results:", "cost=", "{:.5f}".format(test_cost),
      "accuracy=", "{:.5f}".format(test_acc), "time=", "{:.5f}".format(test_duration))


#!/usr/bin/env python
# encoding: utf-8

import sys
import json
import sqlite3
import os
from os.path import join, dirname, expanduser

class Product(object):

    def __init__(self):
        dirname = join(expanduser('~'), '.revbot')
        if not os.path.isdir(dirname):
            os.makedirs(dirname)
        db_path = join(dirname, 'app.db')
        self.conn = sqlite3.connect(db_path, check_same_thread=False)
        self.conn.row_factory = sqlite3.Row
        c = self.conn.cursor()
        table1 = """CREATE TABLE IF NOT EXISTS products (
                    id INTEGER PRIMARY KEY,
                    manufacturer STRING DEFAULT '',
                    part_number STRING DEFAULT '',
                    uploaded INTEGER DEFAULT 0,
                    data TEXT)"""
        table2 = """CREATE TABLE IF NOT EXISTS failed_products (
                    id INTEGER PRIMARY KEY,
                    manufacturer STRING DEFAULT '',
                    part_number STRING DEFAULT '',
                    uploaded INTEGER DEFAULT 0,
                    data TEXT)"""
        c.execute(table1)
        c.execute(table2)

    def count_all(self, criteria={}):
        where = []
        for key in criteria:
            if type(criteria[key]) == str and criteria[key].lower() == 'null':
                where.append("%s is NULL" % key)
            else:
                where.append("%s='%s'" % (key, str(criteria[key]).replace("'", "''")))
        sql = """SELECT COUNT(id) AS total
                 FROM products
                 WHERE %s""" % (' AND '.join(where) if len(where) else '1')
        c = self.conn.cursor()
        c.execute(sql)
        return c.fetchone()['total']

    def find_all(self, criteria={}, limit=0, offset=0):
        where = []
        for key in criteria:
            if type(criteria[key]) == str and criteria[key].lower() == 'null':
                where.append("%s is NULL" % key)
            else:
                where.append("%s='%s'" % (key, str(criteria[key]).replace("'", "''")))
        sql = """SELECT id, manufacturer, part_number, data
                 FROM products
                 WHERE %s
                 ORDER BY id """ % ( " AND ".join(where) if len(where) else '1 ')
        if limit and offset:
            sql += "LIMIT %d OFFSET %d" % (limit, offset)
        c = self.conn.cursor()
        c.execute(sql)
        return c.fetchone() if limit == 1 else c.fetchall()

    def find_one(self, criteria={}):
        return self.find_all(criteria, limit=1)

    def save(self, dict1):
        prod = self.find_one({'manufacturer': dict1.get('manufacturer'),
                              'part_number': dict1.get('part_number')})
        if not prod:
            return self.insert(dict1)
        else:
            return self.update(prod['id'], {'data': json.dumps(dict1)})

    def save_failed_product(self, product_id):
        row = self.find_one({'id': product_id})
        try:
            c = self.conn.cursor()
            c.execute("""DELETE FROM failed_products
                         WHERE manufacturer='%s' AND part_number='%s'""" % ( \
                         row['manufacturer'],
                         row['part_number']))
            c.execute("""INSERT INTO failed_products (manufacturer, part_number, uploaded, data)
                         SELECT manufacturer, part_number, uploaded, data
                         FROM products WHERE id='%s'""" % product_id)
            self.conn.commit()
        except Exception as e:
            print('%s\nSQL: %s' % e)
            raise(e)

    def insert(self, dict1):
        manufacturer = dict1.get('manufacturer').replace("'", "''")
        part_number = dict1.get('part_number').replace("'", "''")
        data = json.dumps(dict1).replace("'", "''")
        sql = """INSERT INTO products (id, manufacturer, part_number, data)
                 VALUES (null, '%s', '%s', '%s')""" % (
                 manufacturer, part_number, data)
        try:
            c = self.conn.cursor()
            c.execute(sql)
            self.conn.commit()
            return c.lastrowid
        except Exception as e:
            print('%s\nSQL: %s' % (e, sql))
            raise(e)

    def update(self, product_id, values):
        cols = ["%s='%s'" % (key, values[key].replace("'", "''")) for key in values]
        sql = """UPDATE products
                 SET %s
                 WHERE id='%s'""" % (",".join(cols), product_id)
        try:
            c = self.conn.cursor()
            c.execute(sql)
            self.conn.commit()
            return product_id
        except Exception as e:
            print('%s\nSQL: %s' % (e, sql))
            raise(e)

    def delete(self, criteria={}):
        where = []
        for key in criteria:
            if type(criteria[key]) == str and criteria[key].lower() == 'null':
                where.append("%s is NULL" % key)
            else:
                where.append("%s='%s'" % (key, str(criteria[key]).replace("'", "''")))
        sql = """DELETE FROM products
                 WHERE %s""" % (' AND '.join(where) if len(where) else '1')
        try:
            c = self.conn.cursor()
            c.execute(sql)
            self.conn.commit()
        except Exception as e:
            print('%s\nSQL: %s' % (e, sql))
            raise(e)

if __name__ == "__main__":
    product = Product()
    for i in range(10):
        data = {"name": "product %d" % (i+1),
                "manufacturer": "ACL",
                "part_number": "6789%d" % i,
                "price": 12.5,
                "category": "bearings"}
        product.save(data)
    row = product.find_one({'id': '1'})
    print('%s, %s' % (row['id'], row['data']))
    rows = product.find_all()
    for row in rows:
        print('%s, %s' % (row['id'], row['data']))


class Solution:
    def minSetSize(self, arr: List[int]) -> int:
        cnt = Counter(arr)
        frequencies = list(cnt.values())
        frequencies.sort()

        ans, removed, half = 0, 0, len(arr) // 2
        while removed < half:
            ans += 1
            removed += frequencies.pop()
        return ans


import numpy as np
import pandas as pd

from sklearn.svm import SVR
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import RBF, Matern, RationalQuadratic

from aiaa_utils import get_dataset, test_interpolation
from aiaa_utils import plot_loo, plot_comparison_loo, split_output_input, plot_changing_error

def test_loo(inputs, outputs, model, speeds_test, num_samples, speeds=[]):
    # GET DATASET
    dataset = get_dataset(num_samples=num_samples, speeds=speeds)
    # EXECUTE TEST SEQUENCE
    df_mape = all_df_test_pred = pd.DataFrame([], columns=outputs)
    for speed in speeds_test:
        df_train, df_test = split_output_input(dataset, speeds_test=[speed])
        df_test_pred, list_mape = test_interpolation(inputs, outputs, model, df_train, df_test)
        df_mape = df_mape.append(pd.DataFrame([list_mape], columns=outputs))
        all_df_test_pred = pd.concat([all_df_test_pred, df_test_pred], sort=False)

    return df_mape, all_df_test_pred

############## MODELS ##############
svr = SVR(kernel='rbf', C=200, gamma="scale", epsilon=1e-10)
svr2 = SVR(kernel='rbf', C=200, epsilon=1e-10)
svr3 = SVR(kernel='rbf', C=20, gamma="scale", epsilon=1e-10)
svr4 = SVR(kernel='rbf', C=20, epsilon=1e-10)

gpr_rbf = GaussianProcessRegressor(kernel=RBF(length_scale_bounds=(3, 10)))
gpr_matern = GaussianProcessRegressor(kernel=Matern(length_scale_bounds=(3, 10), nu=5, length_scale=0.5))
gpr_quad = GaussianProcessRegressor(kernel=RationalQuadratic(length_scale_bounds=(3, 10), alpha_bounds=(3, 10)))

############## TRAINING SPECIFICATIONS ##############
outputs = ['Pressure_ratio', 'Polytropic_efficiency']
inputs = ['speeds_scaled', 'mass_flow_scaled']
speeds = np.arange(10000, 100000, 10000)

############## TEST SIMPLE LOO ##############
# num_samples = 25
# models = [svr, gpr_matern]
# list_results_models = []
# for model in models:
#     mape_results, list_df_test_pred = test_loo(inputs, outputs, model, speeds, num_samples)
#     print('MAPE mean: ', np.mean(mape_results.values))
#     list_results_models.append(mape_results)
# dict_legend = {'title': 'Models', 'tested_features': ['SVR', 'GPR']}
# plot_loo(list_results_models, speeds, outputs, dict_legend)

############## TEST KERNELS ##############
# num_samples = 25
# kernels = [gpr_rbf] #, gpr_matern
# speed = 30000
# list_results_kernels = []
# for kernel in kernels:
#     mape_results, list_df_test_pred = test_loo(inputs, outputs, kernel, speeds, num_samples)
#     print('MAPE mean: ', np.mean(mape_results.values))
#     list_results_kernels.append(list_df_test_pred)
#
# dict_legend = {'title': '', 'tested_features': ['True Values', 'RBF', 'Matern']}
# plot_comparison_loo(list_results_kernels, speed, outputs, dict_legend)

############## TEST SVR FEATURES ##############
num_samples = 25
speed = 30000
models = [svr, svr2, svr3, svr4]
list_results_models, list_results_structure = [], []
for model in models:
    mape_results, list_df_test_pred = test_loo(inputs, outputs, model, speeds, num_samples)
    print('MAPE mean: ', np.mean(mape_results.values))
    list_results_models.append(mape_results)
    list_results_structure.append(list_df_test_pred)
dict_legend = {'title': '', 'tested_features': ['C=200, scale', 'C=200, 1/n_features',
                                                'C=20, scale', 'C=20, 1/n_features']}
plot_loo(list_results_models, speeds, outputs, dict_legend)
dict_legend = {'title': '', 'tested_features': ['True Values', 'C=200, scale', 'C=200, 1/n_features',
                                                'C=20, scale', 'C=20, 1/n_features']}
plot_comparison_loo(list_results_structure, speed, outputs, dict_legend)

############## TEST NUM SAMPLES ##############
# list_num_samples = [5, 15, 20, 25]
# speed = 50000
# speeds = [20000, 30000, 40000, 50000, 60000, 70000]
# list_results_num_samples, list_mape_results = [], []
# for num_samples in list_num_samples:
#     mape_results, list_df_test_pred = test_loo(inputs, outputs, svr, speeds, num_samples, speeds=speeds)
#     list_results_num_samples.append(list_df_test_pred)
#     list_mape_results.append(mape_results)
#
# dict_legend = {'title': '', 'tested_features': ['True Values', '25', '5']}
# # plot_comparison_loo(list_results_num_samples, speed, outputs, dict_legend)
# plot_changing_error(list_mape_results, list_num_samples, outputs)

############## TEST NUM TRAINING ##############
# speed = [50000]
# all_speeds = [10000, 20000, 30000, 40000, 50000, 60000, 70000, 80000, 90000]
# num_iterations = [1, 2, 3, 4, 5, 6]
# num_samples = 25
# list_results_num_samples, list_mape_results = [], []
# for num_iteration in num_iterations:
#     if num_iteration % 2:
#         all_speeds = all_speeds[:-1]
#     else:
#         all_speeds = all_speeds[1:]
#     print('SPEEDS', all_speeds)
#     mape_results, list_df_test_pred = test_loo(inputs, outputs, svr, speed, num_samples, speeds=all_speeds)
#     list_results_num_samples.append(list_df_test_pred)
#     list_mape_results.append(mape_results)
#
# dict_legend = {'title': '', 'tested_features': ['True Values', '25', '5']}
# plot_changing_error(list_mape_results, num_iterations, outputs)

############## TEST COMPARISON MODELS ##############
# num_samples = 25
# list_models = [svr, gpr_matern]
# speed = 30000
# list_results_models = []
# for model in list_models:
#     _, list_df_test_pred = test_loo(inputs, outputs, model, speeds, num_samples)
#     list_results_models.append(list_df_test_pred)
#
# dict_legend = {'title': '', 'tested_features': ['True Values', 'SVR', 'GPR']}
# plot_comparison_loo(list_results_models, speed, outputs, dict_legend)


# String Stuff
s1 = "hello world"
s2 = 'hello world'
s3 = "Hello World"
s4 = 'hello world!'

s5 = s1+s2
print(s5)

s1='some new string'

print(s1)
print(s5) # Notice that s5 is not changed when s1 is

# strings are immutable - you can't change them once you
# create them - you have to assign a new one

s6 = s2[0:5] # make a new string with the first 5 chars from s5
s7 = s2[:5] # same as above
s8 = s2[6:] # from 6 to the end
len(s2) # gives you the number of characters in s2

twowords = "hello world"
space_location = twowords.find(' ')
print(twowords, space_location)
first = twowords[:space_location]
print(first)
second = twowords[space_location+1:]
print(second)


from django.conf.urls import patterns, include, url
from django.contrib import admin

admin.autodiscover()

import views
urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'TimetableWeb.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),

    url(r'^admin/', include(admin.site.urls)),
    url(r'^accounts/register/$', views.registration),
    url(r'^accounts/login/$', 'django.contrib.auth.views.login', { "template_name": "accounts/login.html" }),
    #url(r'^$', views.main),
    url(r'^accounts/logout/$', 'django.contrib.auth.views.logout_then_login' ),
    url(r'^accounts/profile/$', views.later),
    url(r'^$', views.main),
)
urlpatterns += patterns('event.views',
    url(r'^add_event/$', 'add_event'),
    url(r'^day/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/(?P<day>[0-9]{2})/$', 'day_events'),
    url(r'^event/(?P<cur_id>\d+)/$', 'active_event'),
)


#!python
#cython: language_level=3
import numpy as np
from collections import defaultdict
from . import srg

def lower_upper_bound(A: np.array, b: np.array, bounds: np.array) -> None:
    for row, lower_upper_bound in zip(A, b):
        nonzeros = np.nonzero(row)[0]
        # print(row, lower_upper_bound, nonzeros)
        # [0 0 0 0 1] 1 [4]
        # [0 0 1 1 0] 1 [2 3]
        # [1 0 1 0 0] 0 [0 2]
        # [1 1 0 0 0] 1 [0 1]
        for loc in nonzeros:
            bounds[loc] = min(bounds[loc], lower_upper_bound)

def zero_bound_loc(bounds: np.array) -> np.array:
    return np.where(bounds == 0)[0]

def one_element_row_locs(A: np.array) -> set:
    '''
    example input:

    [0 0 0 0 1] 1 [4]
    [0 0 1 1 0] 1 [2 3]
    [1 0 1 0 0] 0 [0 2]
    [1 1 0 0 0] 1 [0 1]

    defaultdict(<class 'list'>, {0: [4], 1: [2, 3], 2: [0, 2], 3: [0, 1]})

    return {(0,4)}

    '''
    nz = defaultdict(list)
    for rnz, cnz in zip(*A.nonzero()):
        nz[rnz].append(cnz)

    # reduce rcs so that columns are unique. Corresponding rows dont matter
    # {(0, 1), (3, 0), (1, 1), (4, 0)} => { 1:1, 0:4 }
    crs = {clist[0]:r for r, clist in nz.items() if len(clist) == 1}

    return {(r, c) for c, r in crs.items()}

if __name__ == '__main__':
    def test1():
        A = srg.array([[0, 0, 0, 0, 1],
                       [0, 0, 1, 1, 0],
                       [1, 0, 1, 0, 0],
                       [1, 1, 0, 0, 0]])
        b = srg.array([1, 1, 0, 1])
        bound = srg.array([1, 1, 1, 1, 1])

        print('original upper bounds', bound)
        lower_upper_bound(A, b, bound)
        print('new lower upper bounds', bound)
        assert np.array_equal(bound, [0, 1, 0, 1, 1])

        rcs = one_element_row_locs(A)
        print('one element rows (r,c)=', rcs)
        assert np.array_equal(rcs.pop(), (0, 4))

    A = srg.array([[0, 1], [0, 1], [0, 0], [1, 0], [1, 0], [0, 0]])
    b = srg.array([4, 4, 0, 4, 4, 0])
    bound = srg.array([4, 4])

    print('original upper bounds', bound)
    lower_upper_bound(A, b, bound)
    print('new lower upper bounds', bound)

    rcs = one_element_row_locs(A)
    print('one element rows (r,c)=', rcs)


#!/usr/bin/env python
#-*- coding:utf-8 -*-

import os

from collections import defaultdict

from .lowlevel import load_any, unload_any, filter_extensions, VALID_BODY_KERNEL_TYPES
from .._helpers import ignored, cleanpath, iterable_path, TimeWindows

__all__ = ['Kernel']

class Kernel(object):
    '''A loaded kernel file.

    .. WARNING:: The constructor should never be used directly. Use one of the
                 methods in the *See also* section.

    Parameters
    ----------
    path: str
        Relative or absolute path to the kernel file/directory.
    kernel_type: {'sp', 'c', 'pc', 'f', 'ls', 'sc'}
        Used to identify what information the kernel is holding and how to load
        it.
    force_reload: bool, optional
        Reload already loaded kernel files.
        By default an error is raised if a kernel file is already loaded.

    Raises
    ------
    ValueError
        If the kernel file is already loaded.

    Attributes
    ----------
    *classattribute* LOADED: set of Kernel
        All loaded kernels.
    *classattribute* TIMEWINDOWS_POS: dict of int -> list
        Maps a list of start-end-tuples for all known time windows of its
        position to the id of a Body.
    *classattribute* TIMEWINDOWS_ROT: dict of int -> list
        Maps a list of start-end-tuples for all known time windows of its
        rotation to the id of a Body.
    path: str
        Absolute path to the kernel file.
    name: str
        Filename of the kernel file.
    type: {'sp', 'c', 'pc', 'f', 'ls', 'sc'}
        Identifier for kernel loading mechanism.
    info: {'pos', 'rot', 'none'}
        What kind of spatial information the kernel contains.
    ids: set of int
        The IDs of all bodies about which the kernel has information.

    See also
    --------
    load: Load multiple files.
    load_single: More controllable way to load single files.
    unload: Unload kernels.
    '''
    LOADED = set()
    TIMEWINDOWS_POS = defaultdict(TimeWindows)
    TIMEWINDOWS_ROT = defaultdict(TimeWindows)

    def __new__(cls, path, kernel_type, force_reload=False):
        path = cleanpath(path)
        hit = {hash(k): k for k in cls.LOADED}.get(hash(path))
        if hit:
            if not force_reload:
                msg = "Kernel already loaded: {}"
                raise ValueError(msg.format(path))
            else:
                hit._unload()
                return hit
        else:
            return object.__new__(cls)

    def __init__(self, path, kernel_type, force_reload=False):
        self.path = cleanpath(path)
        self.name = os.path.basename(self.path)
        self.type = kernel_type
        # Load the kernel and handle time windows
        self.info, self._intervals = load_any(path, self.type)
        self.ids = frozenset(self._intervals.keys())
        with ignored(AttributeError):
            windows = self._timewindows()
            for key, vals in self._intervals.iteritems():
                windows[key] += vals
        # Make self available for unloading
        self.__class__.LOADED.add(self)

    def _unload(self):
        self.__class__.LOADED.remove(self)
        with ignored(AttributeError):
            windows = self._timewindows()
            for key, vals in self._intervals.iteritems():
                windows[key] -= vals
                if not windows[key]:
                    del windows[key]
        unload_any(self)

    def _timewindows(self):
        windows = 'TIMEWINDOWS_' + self.info.upper()
        return getattr(self.__class__, windows)

    def __hash__(self):
        return hash(self.path)

    def __str__(self):
        return 'Kernel {} ({})'.format(self.name, self.type)

    def __repr__(self):
        return '{}({}, {})'.format(self.__class__.__name__, self.path,
            self.type)

    @classmethod
    def load(cls, path='.', recursive=True, followlinks=False, force_reload=False):
        '''Load a kernel file or all kernel files in a directory tree.

        Refer to `spiceminer.load` for full documentation.

        See also
        --------
        spiceminer.load_single: More controllable way to load single files.
        spiceminer.unload: Unload kernels.
        '''
        path = cleanpath(path)
        # Find and sort kernel files.
        body_kernels = set()
        misc_kernels = set()
        for dir_path, _, fnames in iterable_path(path, recursive, followlinks):
            for name in fnames:
                with ignored(ValueError):
                    kernel_type = filter_extensions(name)
                    filepath = os.path.join(dir_path, name)
                    # Split kernels into those containing bodies and others
                    if kernel_type in VALID_BODY_KERNEL_TYPES:
                        body_kernels.add((filepath, kernel_type))
                    else:
                        misc_kernels.add((filepath, kernel_type))
        # Make sure, we actually found at least one valid file.
        if not (body_kernels or misc_kernels):
            msg = 'No valid files found on path'
            raise IOError(2, msg, path)
        # Load collected kernel files (misc first to avoid missing leapseconds).
        loaded = set()
        for path, extension in misc_kernels:
            k = cls(path, extension, force_reload)
            loaded.add(k)
        for path, extension in body_kernels:
            k = cls(path, extension, force_reload)
            loaded.add(k)
        return loaded

    @classmethod
    def load_single(cls, path, extension=None, force_reload=False):
        '''Load a single kernel file. Allows non-standart extensions.

        Refer to `spiceminer.load_single` for full documentation.

        See also
        --------
        spiceminer.load: Load multiple files.
        spiceminer.unload: Unload kernels.
        '''
        if extension is not None:
            kernel_type = filter_extensions('.' + extension)
        else:
            kernel_type = filter_extensions(path)
        k = cls(path, kernel_type, force_reload)
        return k

    @classmethod
    def unload(cls, path='.', recursive=True, followlinks=False):
        '''Unload a kernel file or all kernel files in a directory tree.

        Refer to `spiceminer.unload` for full documentation.

        See also
        --------
        spiceminer.load: Load multiple files.
        spiceminer.load_single: More controllable way to load single files.
        '''
        path = cleanpath(path)
        if not os.path.exists(path):
            msg = 'unload() expected a valid file or directory path, got {}'
            raise ValueError(msg.format(path))
        # Find and sort kernel files.
        hashes = set()
        for dir_path, _, fnames in iterable_path(path, recursive, followlinks):
            for name in fnames:
                # Ignore extension to be able to unload all kernels
                hashes.add(hash(os.path.join(dir_path, name)))
        # Unload collected kernel files.
        unloaded = {k for k in cls.LOADED if hash(k) in hashes}
        for k in unloaded:
            k._unload()
        return unloaded


#!/usr/bin/env python
#
# LSST Data Management System
#
# Copyright 2008-2016  AURA/LSST.
#
# This product includes software developed by the
# LSST Project (http://www.lsst.org/).
#
# This program 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.
#
# This program 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 LSST License Statement and
# the GNU General Public License along with this program.  If not,
# see <https://www.lsstcorp.org/LegalNotices/>.
#
import unittest
from collections import namedtuple

import lsst.utils.tests
from lsst.afw.image import Filter
from lsst.obs.hsc import HscMapper

class CameraTestCase(lsst.utils.tests.TestCase):

    def setUp(self):
        self.mapper = HscMapper(root=".")
        self.camera = self.mapper.camera

    def tearDown(self):
        del self.camera
        del self.mapper

    def testName(self):
        self.assertEqual(self.camera.getName(), "HSC")

    def testNumCcds(self):
        self.assertEqual(len(list(self.camera.getIdIter())), 112)

    def testCcdSize(self):
        for ccd in self.camera:
            self.assertEqual(ccd.getBBox().getWidth(), 2048)
            self.assertEqual(ccd.getBBox().getHeight(), 4176)

    def testFilters(self):
        # Check that the mapper has defined some standard filters.
        # Note that this list is not intended to be comprehensive -- we
        # anticipate that more filters can be added without causing the test
        # to break -- but captures the standard HSC broad-band filters.
        FilterName = namedtuple("FilterName", ["alias", "canonical"])
        filterNames = (
            FilterName(alias="HSC-G", canonical="g"),
            FilterName(alias="HSC-R", canonical="r"),
            FilterName(alias="HSC-I", canonical="i"),
            FilterName(alias="HSC-Z", canonical="z"),
            FilterName(alias="HSC-Y", canonical="y"),
            FilterName(alias="NONE", canonical="UNRECOGNISED")
        )

        for filterName in filterNames:
            self.assertIn(filterName.alias, self.mapper.filters)
            self.assertEqual(Filter(filterName.alias).getCanonicalName(), filterName.canonical)

class TestMemory(lsst.utils.tests.MemoryTestCase):
    def setUp(self):
        HscMapper.clearCache()
        lsst.utils.tests.MemoryTestCase.setUp(self)

def setup_module(module):
    lsst.utils.tests.init()

if __name__ == "__main__":
    lsst.utils.tests.init()
    unittest.main()


from app import app, memcache
import flask

@app.route('/fibonachi', methods=('GET', ))
def fibonachi():
    required_args = ['from', 'to']
    try:
        f, t = [int(flask.request.args[x]) for x in required_args]
    except (KeyError, TypeError):
        flask.abort(400)

    if f > t or t < 0 or f < 0:
        flask.abort(400)

    res = []
    offset = 10
    fk = f - f % offset
    tk = t - t % offset + offset
    start = fk
    ref1, ref2 = None, None  # The last 2 numbers in previous block in order to calculate ones in current block

    # Try to figure out whether previous block exists
    # If not, check whole sequence from 0
    if start > 0:
        prev_cached = memcache.client.get(str(start - offset))
        if prev_cached:
            ref1, ref2 = [int(x) for x in prev_cached.decode().split(',')][-2:]
        else:
            start = 0

    for k in range(start, tk, offset):
        cached = memcache.client.get(str(k))
        seq = None

        if not cached:
            seq = list(get_fibonacci(ref1, ref2, offset))
            ref1, ref2 = seq[-2:]
            memcache.client.set(str(k), ','.join(str(x) for x in seq))
        else:
            seq = [int(x) for x in cached.decode().split(',')]
            ref1, ref2 = seq[-2:]

        # print(seq)
        if k >= fk:
            res.extend(seq or [int(x) for x in cached.decode().split(',')])

    res = [int(x) for x in res[f % offset:-offset + (t % offset)]]
    return flask.jsonify(res)

def get_fibonacci(ref1, ref2, count):
    """
        Return Fibonacci sequence of numbers starting next after ref2. I.e. the first item will be ref1+ref2
        If both ref1 and ref2 are None then returns the seqence from start: 0, 1, 1, 2,...
    """
    p, q = ref1, ref2
    c = count

    if ref1 is None and ref2 is None:
        p, q = 0, 1
        yield p
        yield q
        c -= 2
    elif p > q:
        raise ValueError('ref1 more than ref2')

    while c > 0:
        s = p + q
        p = q
        q = s
        yield s
        c -= 1


'''Utility functions for dealing with locations and distances'''
import itertools
import pandas as pd
import numpy as np
import geopy.distance as geodist
import datetime
import maps
import requests

def extract_locations(data):
    '''Get list of (lat, lng) tuples from dataframe'''
    recs = data[['Latitude', 'Longitude']].to_records(index=False)
    return [list(x) for x in recs]

def distance_matrix(row_locs, col_locs, row_names, col_names):
    '''
    Get straight line distances between locations, along with a cutoff value
        for each row which can be used to define "nearby" locations for that
        row, relative to the closest location
        row_locs, col_locs -- lists of (lat, lng) tuples
        row_names, col_names -- indices identifying locations
    '''
    dist_vals = [geodist.distance(l1, l2).miles
                 for l1, l2 in itertools.product(row_locs, col_locs)]
    dist_vals = np.reshape(dist_vals, (len(row_locs), len(col_locs)))
    out = pd.DataFrame(dist_vals, columns=col_names, index=row_names)
    out['Min_dist'] = out.min(axis=1)
    out['Cutoff'] = out.Min_dist.apply(lambda m: max(m * 1.5, m + 30))
    out = out.drop(columns='Min_dist')
    return out

def _to_gmap_coor(coors):
    return '|'.join([','.join([str(x) for x in y]) for y in coors])

def get_request(api_request):
    return requests.get(api_request).json()

def get_depart_time():
    ''' Return peak traffic time at 5:30PM in a weekday
        that is in the future, to be used as an argument for
        Google Maps Distance Matrix API departure_time'''
    today = datetime.datetime.today()
    tmrw = today + datetime.timedelta(days=1)
    while tmrw.weekday() >= 5: # saturday is 5, sunday is 6
        tmrw += datetime.timedelta(days=1)
    peak_hour = datetime.time(17,30,0) # 5:30PM
    depart_time = datetime.datetime.combine(tmrw,peak_hour)
    return int(depart_time.timestamp())

def gmap_distance_matrix(origins,destinations,mode='driving-traffic'):
    if mode=='driving-traffic':
        mode = 'driving'
        depart_time = get_depart_time()
    else:
        depart_time = None
    api_template = 'https://maps.googleapis.com/maps/api/distancematrix/json?origins={}&destinations={}'
    origins,destinations = _to_gmap_coor(origins),_to_gmap_coor(destinations)
    api_call = api_template.format(origins,destinations)
    if depart_time: api_call += "&departure_time={}".format(depart_time)
    api_call += "&key={}".format(maps.get_key())
    return get_request(api_call)


import threading
from multiprocessing import Queue

class Producer(threading.Thread):
    def __init__(self, queue, fRead):
        threading.Thread.__init__(self)
        self.queue = queue
        self.file = fRead

    def run(self):
        for item in self.file :
            self.queue.put(item)
        self.queue.put("finish")


"""CMPT 370 Group 5 Project: NAME (Nearly Analogous Music Engine)
    Credits: Michael Coquet
             Elizabeth Reid
             Ben Camplin
             Laurence Craig Garcia
             Sean Warren
"""
from . import Genius_Api_Manager

class Lyrics(object):
    """A class which obtains a song's lyrics and can return
        all necessary information.
    """
    def __init__(self, song_name, song_artist):
        """ Initializes the lyrics objects by getting information
            from the lyrics api manager
            song_name: name of the song
            song_artist: artist associated with the song
        """
        # Call the genius_api_manager and get lyrics
        lyrics_object = Genius_Api_Manager(song_name, song_artist)

        self.__song_lyrics = lyrics_object.search_for_lyrics()
        self.__song_name = song_name
        self.__song_artist = song_artist
        self.__num_words = 0
        self.__num_chorus = 0
        self.__num_verse = 0
        self.__variability_score = 0

    def get_lyrics(self):
        """Get song lyrics and return as a string"""

        #If Song is instrumental it has no lyrics but the API
        #Still returns a string so it must be changed
        if(self.__song_lyrics == "Instrumental" or self.__song_lyrics == "No lyrics for this song were found"):
            self.__song_lyrics = None

        return self.__song_lyrics

    def get_song_artist(self):
        return self.__song_artist

    def get_num_words(self):
        """Calculates the number of words in the song's lyrics and
            ignores any irrelevant headers
        """
        number_words = 0

        words = self.get_lyrics().split()
        for word in words:
            if("[" not in word and "]" not in word):
                number_words += 1

        return number_words

    def get_num_verse(self):
        """Calculates the total number of verses in the song"""

        number_verses = 0

        words = self.get_lyrics().split()
        for word in words:
            if("[Verse" in word):
                number_verses += 1

        return number_verses

    def get_num_chorus(self):
        """Calculates the total number of choruses in the song"""

        number_chorus = 0

        words = self.get_lyrics().split()
        for word in words:
            if("[Chorus]" in word):
                number_chorus += 1

        return number_chorus

    def get_variability(self):
        """Calculates variability, whatever that means"""
        return 0

    def __analyze_lyrics(self):
        """Analyze the song, I did all the analyzing in the
            respective get functions instead of here by accident,
            will refactor later
        """
        return 0


from flask_login import UserMixin

class User(UserMixin):
	def __init__(self):
		self.name = "TEST USER"
		self.section = [7]
		self.id = "2018fall_ksf"
		self.active = True
		self.isAdmin = False
		self.isStudent = False
		self.isStaff = False
		self.attendance = {}

	def create_student_user_from_db(db_result):
		usr = User()
		usr.id = db_result["id"]
		usr.section = db_result["section_num"]
		usr.name = db_result["first_name"] + " " + db_result["last_name"]
		usr.username = db_result["username"]
		usr.isStudent = True
		return usr

	def is_active(self):
		# Here you should write whatever the code is
		# that checks the database if your user is active
		return self.active

	def is_anonymous(self):
		return False

	def is_authenticated(self):
		return True

	def get_student_demo():
		usr = User()
		usr.name = "Student User"
		usr.id = "2018fall_student"
		usr.isStudent = True
		return usr

	def get_admin_demo():
		usr = User()
		usr.name = "Admin"
		usr.id = "2018fall_admin"
		usr.isAdmin = True
		return usr


"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Harvard CS124 - Spring 2018
Philippe Noël - Pset 3 - Crypto
Python version of Crypto
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
from collections import defaultdict, deque
# global dictionnaries to contain all of our sets
# parent is like the parents set, in some sense
# it contains the parent of each node and that's it
parent = dict()
rank = dict()
# this array will contain the bandwiths for each query from 1 to t
bandwiths = []
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""" Subroutine code for merge subroutine of MergeSort algorithm """
def merge(lst1, lst2):
    # save lengths of lists for runtime
    lst1_len = len(lst1)
    lst2_len = len(lst2)
    # initialize output list and iteration indices
    oupt_lst = []
    index1, index2 = 0, 0
    # merging loop for non-empty lists
    while index1 != lst1_len and index2 != lst2_len:
        if lst1[index1] > lst2[index2]:
            oupt_lst.append(lst1[index1])
            index1 += 1
        else:
            oupt_lst.append(lst2[index2])
            index2 += 1
    # subcase if one list is now empty
    if index1 == lst1_len:
        oupt_lst += lst2[index2:]
    else:
        oupt_lst += lst1[index1:]
    return oupt_lst
""" Subroutine code for MergeSort algorithm """
def MergeSort(lst):
    # save length of list for runtime
    lst_len = len(lst)
    # base case
    if lst_len == 0 or lst_len == 1:
        return lst
    else:
        # recursive call
        mid = int(lst_len / 2)
        first_half = MergeSort(lst[:mid])
        second_half = MergeSort(lst[mid:])
        return merge(first_half, second_half)
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""" Subroutine code for Kruskal's makeset """
def makeset(vertex):
    # add our new vertex as a singleton value inside our set
    # and set its rank to 0
    parent[vertex] = vertex
    rank[vertex] = 0
""" Subroutine code for Kruskal's set find """
def find(vertex):
    # if our vertex is not the parent, recurse down the subtree
    if parent[vertex] != vertex:
        parent[vertex] = find(parent[vertex])
    # return p(x)
    return parent[vertex]
""" Subroutine code for Kruskal's set union """
def union(vertex1, vertex2, servers, users):
    # vertex1 and vertex2 are required to be roots
    root1 = find(vertex1)
    root2 = find(vertex2)
    # if rank of root1 > rank of root 2, we swap the two roots
    # since root2 has higher rank, so we should attach the smallest
    # tree to it
    if rank[root1] > rank[root2]:
        # switch the roots
        tmp = root2
        root2 = root1
        root1 = tmp
        # switch the users count
        tmp1 = users[root2]
        users[root2] = users[root1]
        users[root1] = tmp1
        # switch the servers count
        tmp2 = servers[root2]
        servers[root2] = servers[root1]
        servers[root1] = tmp2
    # if the rank are the same, make them as one root and increment rank
    if rank[root1] == rank[root2]:
        rank[root2] += 1
    # update the tree
    parent[root1] = root2
    # update users
    users[root2] += users[root1]
    # update servers
    servers[root2] += servers[root1]
    # return the root of the new connected component
    return root2
""" Subroutine code for Kruskal's algorithm """
def Kruskal(graph, weights, servers, users):
    # making all the sets is done when reading input
    # each weight in the weights list represent one edge
    # they are already sorted in decreasing order
    for w in weights:
        # get the current edge we are looking at
        edge = graph[w].popleft()
        # if not single node, add edge to our tree and merge the sets
        if find(edge[0]) != find(edge[1]):
            # pass along servers and users as when we will merge elements,
            # we check if they can provide an allocation and if so allocate
            # pass in weight since it will be the bandwith
            connected_comp_root = union(edge[0], edge[1], servers, users)
            # get number of servers and users in this new connected comp
            num_servers = servers[connected_comp_root]
            num_users = users[connected_comp_root]
            # iterate over all the users in the component
            for user in range(num_users):
                # if there is still space in servers
                if num_servers > 0:
                    # append a new bottleneck bandwith
                    bandwiths.append(w)
                    # decrease capacity available by 1
                    num_servers -= 1
                    # remove the user just assigned
                    users[connected_comp_root] -= 1
            # once done looping over the users, set the number of servers
            # in the component to the remaining number of servers for when
            # we will merge the components
            servers[connected_comp_root] = num_servers
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""" Subroutine code for Crypto algorithm """
def crypto():
    # global var for more easily keeping track of bandwith cuz lazy
    global bandwiths
    # read n (vertices) and m (edges)
    n, m = map(int, input().split())
    # weights list -- will contain duplicate, should be ok
    weights = []
    # initialize the graph as an adjacency list, though about making adjacency
    # matrix, but it will be very inefficient since most of it would be empty
    # weights as keys and edges list as value
    graph = defaultdict(deque)
    # get all the edges and their weights
    for edge in range(m):
        # get the edge and its weight
        u, v, w = map(int, input().split())
        # add weight to our weight array that will be sorted
        weights.append(w)
        # fill in the vertices V_sets
        # this is the MAKESET operation from Kruskal
        # we do it here to save a linear loop later
        makeset(u)
        makeset(v)
        # add edge and its weight to our graph
        graph[w].append((u, v))
    # get k (servers) and t (users)
    k, t = map(int, input().split()) # these are kinda useless since Python
    # get the arrays that contain the servers locations, their capacities,
    # and the users locations
    servs_locations = list(map(int, input().split()))
    servs_capacities = list(map(int, input().split()))
    users_locations = list(map(int, input().split()))
    # generate a dictionary that contains the number of spots available in
    # each connected component (key is the root of the component)
    servers = dict()
    for index in range(k):
        servers[servs_locations[index]] = servs_capacities[index]
    # generate a dictionary that contains the number of users in each
    # connected component (key is the root of the component)
    users = dict()
    for index in range(t):
        users[users_locations[index]] = 1
    # fill the rest of both dictionnaries with 0 values since no user nor serv
    for index in range(n):
        if servers.get(index + 1) == None:
            servers[index + 1] = 0
        if users.get(index + 1) == None:
            users[index + 1] = 0
    # get Q (number of queries) and the Q queries
    Q = int(input()) # useless value since we're using Python
    queries = list(map(int, input().split()))
    # sort the weights in decreasing order (we are building a Max Span Tree)
    # this is an inverted MergeSort algorithm
    sorted_weights = MergeSort(weights)
    # Modified Kruskal algorithm that builds a maximum spanning tree while
    # assigning users to servers and storing it in the bandwiths array
    Kruskal(graph, sorted_weights, servers, users)
    # iterate over all the queries and take the bandwith computed
    # previously and return it. We take - 1 since array is indexed 0
    for query in queries:
        print(bandwiths[query - 1])

""" Function call for testing """
crypto()


#!/usr/bin/env python
# -*- coding: utf-8 -*- #
from __future__ import unicode_literals

AUTHOR = 'LANCIS'
SITENAME = 'LANCIS-IE'
SITEURL = ''

PATH = 'content'

TIMEZONE = 'America/Mexico_City'

DEFAULT_LANG = 'es'

# Feed generation is usually not desired when developing
FEED_ALL_ATOM = None
CATEGORY_FEED_ATOM = None
TRANSLATION_FEED_ATOM = None
AUTHOR_FEED_ATOM = None
AUTHOR_FEED_RSS = None

DEFAULT_PAGINATION = 12

RELATIVE_URLS = True

THEME = 'theme'

STATIC_PATHS = ['images', 'docs', 'assets',]

SUMMARY_MAX_LENGTH = 50


from django.http import Http404
from django.shortcuts import render, redirect
from django.template import TemplateDoesNotExist

from ladders.logic.util import get_ladder_or_404, get_watcher

def handle_form_and_redirect_to_ladder(request, ladder_id, form_class, template, context=None, form_name='form'):
    if not context:
        context = {}
    ladder = get_ladder_or_404(pk=ladder_id)
    if request.POST:
        form = form_class(ladder, request.POST)
        if form.is_valid():
            form.save()
            return redirect(ladder)
    else:
        form = form_class(ladder)
    context[form_name] = form
    return view_with_ladder(request, ladder, template, context=context)

def handle_form(request, ladder_id, form_class, template, context=None, form_name='form'):
    if not context:
        context = {}
    ladder = get_ladder_or_404(pk=ladder_id)
    if request.POST:
        form = form_class(ladder, request.POST)
        if form.is_valid():
            form.save()
            form.success = "Form submitted successfully"
    else:
        form = form_class(ladder)
    context[form_name] = form
    return view_with_ladder(request, ladder, template, context=context)

def view_with_ladder(request, ladder, template, context=None):
    if not context:
        context = {}
    context.update({'ladder': ladder, 'watcher': get_watcher(request.user, ladder)})
    try:
        return render(request, template, context)
    except TemplateDoesNotExist:
        raise Http404("Template does not exist: %s" % template)