from pathlib import WindowsPath
from typing import Counter
import numpy as np
from numpy.lib import utils
import Algo1
import predictCnn
import torch
import torchvision.transforms
import cv2 as cv2
import matplotlib.pyplot as plt
import coarseGrained
import cv2
import config
import utils
import time
from scipy.ndimage import gaussian_filter

def CNN(model, cropped_frames, Other_bounding_box, CONF_THRESHOLD, NMS_IOU_THRESH, ori_frame_shape):
    bboxes = []

    for idx in range(len(cropped_frames)):
        c_fra = cropped_frames[idx]

        input_size = 416

        #Rezising of the input frame to fit the model
        if (np.shape(c_fra)[0] < input_size) and (np.shape(c_fra)[1] < input_size):
            pil_frame, sqaure_shape = Algo1.size_up(c_fra, input_size)
        else:
            pil_frame = cv2.resize(c_fra, (416,416))
        pil_frame = pil_frame.astype(np.uint8)

        #Uncomment to show input for the model
        """cv2.imshow("Input frame", fra)
        k = cv2.waitKey(3) & 0xFF
        if k == 27:
            break
        q = input("Pres enter to continue, n for next model, q to exit")
        if (q == "q"):
            cv2.destroyAllWindows()
            exit()
        if (q == "n"):
            break"""

        ANCHORS = config.ANCHORS
        pil_fra_min = np.min(pil_frame)
        pil_fra_max = np.max(pil_frame)
        pil_fra = torch.from_numpy(np.array([pil_frame]))
        pil_fra = pil_fra.permute(0,3,1,2)
        pil_fra = pil_fra.float()

        pil_fra = (pil_fra - pil_fra_min) / (pil_fra_max - pil_fra_min)

        var = predictCnn.get_bboxes(
            pil_fra,
            model,
            NMS_IOU_THRESH,
            ANCHORS,
            CONF_THRESHOLD
        )

        #Uncomment to show preds on cropped frames
        #draw_boxes(var[0], pil_frame_crop)

        if np.shape(var) == (1,0):
            continue
        else:
            bboxes.append(Algo1.scale_bboxes(var[0], ori_frame_shape, Other_bounding_box[idx], sqaure_shape))

    return bboxes

def main():

    paths = ['test_frames/test_I_IL_01', 'test_frames/test_I_SI_01', 'test_frames/test_O_OP_01',
            'test_frames/test_O_CM_01', 'test_frames/test_O_OF_01']
    data_sets = []
    for p in paths:
        data_sets.append(utils.load_images_from_folder(p))

    paths_label = ['test_labels/label_I_IL_01', 'test_labels/label_I_SI_01', 'test_labels/label_O_OP_01',
                    'test_labels/label_O_CM_01', 'test_labels/label_O_OF_01']
    GTs = []
    for p in paths_label:
        GTs.append(utils.load_labels(p))

    gt_shows = []
    for p in paths_label:
        gt_shows.append(utils.load_labels_test(p))

    maps = []
    f_measures = []

    total_time = []
    Coarse_time = []
    Algo1_time = []
    CNN_time = []

    for i in range(len(data_sets)):
        data_set = data_sets[i]
        GT = GTs[i]
        gt_show = gt_shows[i]
        model = 'model_weights.h5'
        model = torch.load(model, map_location=torch.device('cuda'))
        model.eval()
        count = 0
        col_bboxes = []
        while (len(data_set)-1):
            t0 = time.time()
            print("While loop :", count ,"out of", len(data_set), end="\r")
            if(count == len(data_set) -1):
                break

            old_frame = data_set[count]
            new_frame = data_set[count+1]
            count += 1

            ori_frame_shape = np.shape(new_frame)
            #if ret == False:
            #   break
            # Block 1: Coarse grained
            gauss_sigma = 5
            old_frame = cv2.cvtColor(old_frame, cv2.COLOR_BGR2GRAY)
            old_frame = gaussian_filter(old_frame, sigma=gauss_sigma)
            c_grained, n_fram = coarseGrained.dense_optical_flow(cv2.calcOpticalFlowFarneback,
                        gauss_sigma, 5, new_frame, old_frame,
                        [0.5, 20, 50, 7, 20, 1.1, 0], True, True)

            #Show output of Coarse Grained
            """cv2.imshow("Original frame", c_grained)
            k = cv2.waitKey(3) & 0xFF
            if k == 27:
                break
            q = input("Pres enter to continue, n for next model, q to exit")
            if (q == "q"):
                cv2.destroyAllWindows()
                exit()
            if (q == "n"):
                break"""

            t1 = time.time()
            # Block 2: Algorithm 1
            pixel_thresh = 15
            Bounding_box = Algo1.Algorithm1(c_grained, pixel_thresh)
            #print("bbox",Bounding_box)

            Other_bounding_box = Bounding_box

            Other_bounding_box = []
            for i in range(len(Bounding_box)):
                if (Bounding_box[i][1][0]-Bounding_box[i][0][0])*(Bounding_box[i][1][1]-Bounding_box[i][0][1]) > 2000:
                    Other_bounding_box.append(Bounding_box[i])

            if len(Other_bounding_box) < 1:
                continue
            else:

                cropped_frames = utils.crop_pic(Other_bounding_box, new_frame)

                # Block 3: CNN object recognition
                NMS_IOU_THRESH = 0.45
                CONF_THRESHOLD = 0.05
                t2 = time.time()
                bboxes = CNN(model, cropped_frames, Other_bounding_box, CONF_THRESHOLD, NMS_IOU_THRESH, ori_frame_shape)

                #### Present results for each frame ####

                #Uncomment the following line to see the prediction on the frame
                utils.draw_boxes(bboxes, new_frame)

                #Uncomment the following line to see the label on the frame
                #utils.draw_boxes(gt_show[count - 1], new_frame)
                save_bbox = bboxes.copy()
                for box in save_bbox:
                    col_bboxes.append([count] + box)
                t3 = time.time()

            total_time.append(t3 - t0)
            Coarse_time.append(t1 - t0)
            Algo1_time.append(t2 - t1)
            CNN_time.append(t3 - t2)

        #Calculate mAP and f-measure

        Map, f_measure = utils.map_pred(col_bboxes, GT)
        Map = Map.cpu().detach().numpy()
        f_measure = f_measure.cpu().detach().numpy()

        f_measures.append(f_measure)
        maps.append(Map)
        print("mAP value:", Map)
        print("f-measure value:", f_measure)

    print("Average time per frame:")
    print(sum(total_time) / len(total_time))
    print("Avg. Coarse grained time:")
    print(sum(Coarse_time) / len(Coarse_time))
    print("Avg. Algo1 time:")
    print(sum(Algo1_time) / len(Algo1_time))
    print("Avg. CNN time:")
    print(sum(CNN_time) / len(CNN_time))

if __name__ == '__main__':
    main()


#!/usr/bin/env python3
import tkinter as tk
from tkinter import messagebox
from tkinter import filedialog

import xml.etree.ElementTree as ET

class GameState():
	"""Containing progress of a player - the state number and inventory."""
	def __init__(self):
		self.statenumber=1
		self.inventory=["Sword","Silver key","Plasma gun"]
		#self.inventory=[]

class MainWindow(tk.Frame):

	def __init__(self, master=None):
		"""Initializes the window, with all the widgets.
		Widgets are not visible outside of this method, but their communication variables are."""
		tk.Frame.__init__(self,master)
		self.grid()

		# Game state: statenumber, inventory
		self.gs = GameState()
		self.gs.statenumber=0

		loadGameButton=tk.Button(self, text="Load world", command=self.load_world_callback)
		loadGameButton.grid(row=0, column=0)

		loadProgressButton=tk.Button(self, text="Load progress", command=self.load_progress_callback)
		loadProgressButton.grid(row=0, column=1)

		saveProgressButton=tk.Button(self, text="Save progress", command=self.save_progress_callback)
		saveProgressButton.grid(row=0, column=2)

		showInventoryButton=tk.Button(self, text="Show inventory", command=self.show_inventory_callback)
		showInventoryButton.grid(row=0, column=3)

		showAboutButton=tk.Button(self, text="About", command=self.show_about_callback)
		showAboutButton.grid(row=0, column=4)

		showExitButton=tk.Button(self, text="Quit", command=self.quit)
		showExitButton.grid(row=0, column=5)

		self.stateDescriptionVar=tk.StringVar()
		stateDescription=tk.Label(self, width=60, height=8, textvariable=self.stateDescriptionVar)
		stateDescription.grid(row=1, column=0, columnspan=6)

		self.statePossibilitiesVar=tk.StringVar()
		statePossibilities=tk.Listbox(self, width=50, height=5, listvariable=self.statePossibilitiesVar)
		statePossibilities.grid(row=2, column=0, columnspan=5)

		chooseButton=tk.Button(self, text="Go", command=self.next_state_callback)
		chooseButton.grid(row=2, column=5)

		self.stateBarVar=tk.StringVar()
		self.stateBarVar.set("Insert copyright note here!")
		stateBar=tk.Label(self, textvariable=self.stateBarVar)
		stateBar.grid(row=3, column=0, columnspan=6)

	def load_world_callback(self):
		pathToFile=filedialog.askopenfilename(defaultextension=".xml", filetypes=[("Dungeon in XML",".xml")], title="Load world")
		if pathToFile!="":
			gs.statenumber=1
			gs.inventory=[]
			#parse XML
			self.xmlFile=ET.parse(pathToFile)
			#set current state

			self._load_state_data(1)

	def load_progress_callback(self):
		pathToFile=filedialog.askopenfilename(defaultextension=".dsw", filetypes=[("Saved game",".dsv")], title="Load progress")

	def save_progress_callback(self):
		pathToFile=filedialog.asksaveasfilename(defaultextension=".dsw", filetypes=[("Saved game",".dsv")], title="Save progress")

	def show_inventory_callback(self):
		inv_string=""
		for item in self.gs.inventory:
			inv_string=inv_string+item+"\n"
		messagebox.showinfo("Inventory",inv_string)

	def show_about_callback(self):
		messagebox.showinfo("About","Dungeons of XML, (C) Blah Blah 2016")

	def next_state_callback(self):
		pass

	"""Reads the state from XML, provides state description and possibilities"""
	def _load_state_data(self, sn):
		# iterate over the states to find the desired one
		for s in self.xmlFile:
			n=int(s.get("n"))
			if n==sn:
				#if this is the desired state
				desc=s.find("d").text
				self.stateDescriptionVar=desc

app = MainWindow()
app.master.title("Dungeons of XML")
app.mainloop()


import cv2, sys, numpy, os
import sqlite3
def Show_menu():
    print('1)Create Dataset')
    print('2)Train+Recognite+Attendance')
    print('3)Attendance Sheet')
    print('4)Exit')
def Create_Database():
    haar_file = 'haarcascade_frontalface_default.xml'
    datasets = 'datasets'  #All the faces data will be present this folder
    sub_data = input('Enter your ID ')    #This will creater folders in datasets with the face of people, so change it's name everytime for the new person.
    name = input('Enter your name ')
    con = sqlite3.connect('db_class.db')
    cur = con.cursor()
    sql = "INSERT INTO student (ID,Name) VALUES (?,?)"
    value=(sub_data,name)
    cur.execute(sql,value)
    con.commit()
    con.close()

    path = os.path.join(datasets, sub_data)
    if not os.path.isdir(path):
        os.mkdir(path)
    (width, height) = (130, 100)    # defining the size of images

    face_cascade = cv2.CascadeClassifier(haar_file)
    webcam = cv2.VideoCapture(0) #'0' is use for my webcam, if you've any other camera attached use '1' like this

    # The program loops until it has 30 images of the face.
    count = 1
    while count < 1001:
        (_, im) = webcam.read()
        gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
        faces = face_cascade.detectMultiScale(gray, 1.3, 4)
        for (x,y,w,h) in faces:
            cv2.rectangle(im,(x,y),(x+w,y+h),(255,0,0),2)
            face = gray[y:y + h, x:x + w]
            face_resize = cv2.resize(face, (width, height))
            cv2.imwrite('%s/%s.png' % (path,count), face_resize)
        count += 1
        cv2.imshow('OpenCV', im)
        key = cv2.waitKey(150)
        if key == 27:
            break
    print('Finish Process')
    cv2.waitKey(1)
    input('Press Enter to close window and camera.')
    cv2.destroyAllWindows()
    webcam.release()

def Face_rec():

    haar_file = 'haarcascade_frontalface_alt2.xml'#detect object
    datasets = 'datasets'
    week = input('Input week  ')
    con = sqlite3.connect('db_class.db')
    cur = con.cursor()
    print('Training...')
    print('Press Space Bar to close window and camera.')
    # Create a list of images and a list of corresponding names
    (images, labels, names, id) = ([], [], {}, 0)
    for (subdirs, dirs, files) in os.walk(datasets):
        for subdir in dirs:
            names[id] = subdir
            subjectpath = os.path.join(datasets, subdir)
            for filename in os.listdir(subjectpath):
                path = subjectpath + '/' + filename
                label = id
                images.append(cv2.imread(path, 0))
                labels.append(int(label))
            id += 1
    (width, height) = (130, 100)
    # Create a Numpy array from the two lists above
    (images, labels) = [numpy.array(lis) for lis in [images, labels]]
    model =  cv2.face.LBPHFaceRecognizer_create()
    model.train(images, labels)

    #use LBPHFace recognizer on camera frame
    face_cascade = cv2.CascadeClassifier(haar_file)
    webcam = cv2.VideoCapture(0)
    while True:
        (_, im) = webcam.read()
        gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
        faces = face_cascade.detectMultiScale(gray, 1.3, 5)
        for (x,y,w,h) in faces:
            cv2.rectangle(im,(x,y),(x+w,y+h),(255,0,0),2)
            face = gray[y:y + h, x:x + w]
            face_resize = cv2.resize(face, (width, height))
            #Try to recognize the face
            prediction = model.predict(face_resize)
            cv2.rectangle(im, (x, y), (x + w, y + h), (0, 255, 0), 3)
            if prediction[1]<70:
                cv2.putText(im,'%s - %.0f' % (names[prediction[0]],prediction[1]),(x-10, y-10), cv2.FONT_HERSHEY_PLAIN,1,(0, 255, 0))
                if week=='1':
                    sql = "UPDATE student SET Week1= '1' WHERE ID ='"+names[prediction[0]]+"'"
                    cur.execute(sql)
                    con.commit()
                if week=='2':
                    sql = "UPDATE student SET Week2= '1' WHERE ID ='"+names[prediction[0]]+"'"
                    cur.execute(sql)
                    con.commit()
                if week=='3':
                    sql = "UPDATE student SET Week3= '1' WHERE ID ='"+names[prediction[0]]+"'"
                    cur.execute(sql)
                    con.commit()
                if week=='4':
                    sql = "UPDATE student SET Week4= '1' WHERE ID ='"+names[prediction[0]]+"'"
                    cur.execute(sql)
                    con.commit()
                if week=='5':
                    sql = "UPDATE student SET Week5= '1' WHERE ID ='"+names[prediction[0]]+"'"
                    cur.execute(sql)
                    con.commit()
                if week=='6':
                    sql = "UPDATE student SET Week6= '1' WHERE ID ='"+names[prediction[0]]+"'"
                    cur.execute(sql)
                    con.commit()
                if week=='7':
                    sql = "UPDATE student SET Week7= '1' WHERE ID ='"+names[prediction[0]]+"'"
                    cur.execute(sql)
                    con.commit()
                if week=='8':
                    sql = "UPDATE student SET Week8= '1' WHERE ID ='"+names[prediction[0]]+"'"
                    cur.execute(sql)
                    con.commit()
            else:
                cv2.putText(im,'Unknown',(x-10, y-10), cv2.FONT_HERSHEY_PLAIN,1,(0, 255, 0))
        cv2.imshow('OpenCV', im)
        key = cv2.waitKey(1)
        if key == 27:
            break
    cv2.destroyAllWindows()
    webcam.release()

def Crete_datasheet():
    con = sqlite3.connect('db_class.db')
    with con:
        cur = con.cursor()
        cur.execute("SELECT * FROM student")
        rows = cur.fetchall()
        for row in rows:
            print(row)
while True:
    Show_menu()
    event=int(input('Enter your choose '))
    if event ==1:
        Create_Database()
    if event ==2:
        Face_rec()
    if event ==3:
        Crete_datasheet()
    if event==4:
        break
    else:
        Show_menu()
        event=int(input('Enter your choose again '))


from django.urls import path
from . import views
urlpatterns = [
   # artists
   path('', views.artist_list, name='artist_list'),
   path('artists/<int:pk>' , views.artist_detail , name = 'artist_detail' ),
   path('artists/new', views.artist_create, name='artist_create'), # new artists
   path('artists/<int:pk>/edit', views.artist_edit, name='artist_edit'), # edit artist
   path('artists/<int:pk>/delete', views.artist_delete, name='artist_delete'), #delete artist

   path('songs/', views.songs_list, name='song_list'),
   path('songs/<int:pk>' , views.song_detail , name = 'song_detail' ),
   path('songs/new', views.song_create, name='song_create'),
   path('songs/<int:pk>/edit', views.song_edit, name='song_edit'),
   path('songs/<int:pk>/delete', views.song_delete, name='song_delete'),

   path('home/' , views.home , name = "home" ) ,
   path('about/' , views.about) ,
   path('user/<int:id>' , views.show_one_user)
]


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('fb360', '0059_auto_20151023_1106'),
    ]

    operations = [
        migrations.AddField(
            model_name='qualitativeresponse',
            name='fb',
            field=models.ForeignKey(default=2, to='fb360.FB360', null=True),
            preserve_default=True,
        ),
        migrations.AddField(
            model_name='response',
            name='fb',
            field=models.ForeignKey(default=2, to='fb360.FB360', null=True),
            preserve_default=True,
        ),
    ]


import numpy as np
import h5py
import networkx as nx
import argparse
import itertools
import random
import pickle

import neurokernel.mpi_relaunch
import neurokernel.core_gpu as core

from neurokernel.LPU.InputProcessors.StepInputProcessor import StepInputProcessor
from neurokernel.LPU.InputProcessors.FileInputProcessor import FileInputProcessor
from neurokernel.tools.logging import setup_logger
from neurokernel.LPU.LPU import LPU

(comp_dict, conns) = LPU.lpu_parser('neuroballad_temp_graph.gexf.gz')
with open('run_parameters.pickle', 'rb') as f:
    run_parameters = pickle.load(f)
with open('record_parameters.pickle', 'rb') as f:
    record_parameters = pickle.load(f)
dur = 1.0
dt = 1e-4
dur = run_parameters[0]
dt = run_parameters[1]
fl_input_processor = FileInputProcessor('neuroballad_temp_model_input.h5')

from neurokernel.LPU.OutputProcessors.FileOutputProcessor import FileOutputProcessor
output_processor = FileOutputProcessor(record_parameters, 'neuroballad_temp_model_output.h5', sample_interval=1)

#Parse extra arguments
parser = argparse.ArgumentParser()
parser.add_argument('--debug', default=True,
                    dest='debug', action='store_true',
                    help='Write connectivity structures and inter-LPU routed data in debug folder')
parser.add_argument('-l', '--log', default='both', type=str,
                    help='Log output to screen [file, screen, both, or none; default:none]')
parser.add_argument('-r', '--time_sync', default=False, action='store_true',
                    help='Time data reception throughput [default: False]')
parser.add_argument('-g', '--gpu_dev', default=[0], type=int, nargs='+',
                    help='GPU device numbers [default: 0]')
parser.add_argument('-d', '--disconnect', default=False, action='store_true',
                    help='Run with disconnected LPUs [default: False]')
args = parser.parse_args()
file_name = None
screen = False
if args.log.lower() in ['file', 'both']:
    file_name = 'neurokernel.log'
if args.log.lower() in ['screen', 'both']:
    screen = True
logger = setup_logger(file_name=file_name, screen=screen)
man = core.Manager()

man.add(LPU, 'lpu', dt, comp_dict, conns,
        input_processors=[fl_input_processor],
        output_processors=[output_processor], device=args.gpu_dev[0],
        debug=True)

steps = int(dur/dt)
man.spawn()
man.start(steps = steps)
man.wait()


from lib.utils import util
from lib.base_controller import CommandHelp, ShellException

from .live_cluster_command_controller import LiveClusterCommandController

@CommandHelp(
    '"asinfo" provides raw access to the info protocol.',
    "  Options:",
    "    -v <command>   - The command to execute",
    "    -p <port>      - Port to use in the case of an XDR info command and XDR is",
    "                     not in asd",
    '    -l             - Replace semicolons ";" with newlines. If output does',
    '                     not contain semicolons "-l" will attempt to use',
    '                     colons ":" followed by commas ",".',
    "    --no_node_name - Force to display output without printing node names.",
)
class ASInfoController(LiveClusterCommandController):
    def __init__(self):
        self.modifiers = set(["with", "like"])

    @CommandHelp("Executes an info command.")
    def _do_default(self, line):
        mods = self.parse_modifiers(line)
        line = mods["line"]
        nodes = self.nodes

        value = None
        line_sep = False
        xdr = False
        show_node_name = True

        tline = line[:]

        try:
            while tline:
                word = tline.pop(0)
                if word == "-v":
                    value = tline.pop(0)
                elif word == "-l":
                    line_sep = True
                elif word == "-p":
                    port = tline.pop(0)
                    if port == "3004":  # ugly Hack
                        xdr = True
                elif word == "--no_node_name":
                    show_node_name = False
                else:
                    raise ShellException(
                        "Do not understand '%s' in '%s'" % (word, " ".join(line))
                    )
        except Exception:
            self.logger.warning(
                "Do not understand '%s' in '%s'" % (word, " ".join(line))
            )
            return
        if value is not None:
            value = value.translate(str.maketrans("", "", "'\""))

        if xdr:
            results = self.cluster.xdr_info(value, nodes=nodes)
        else:
            results = self.cluster.info(value, nodes=nodes)

        return util.Future(
            self.view.asinfo, results, line_sep, show_node_name, self.cluster, **mods
        )


from itertools import chain, combinations, compress, groupby, product
from math import comb
from typing import List, Optional

import numpy as np
from pymatgen.analysis.interface_reactions import InterfacialReactivity
from pymatgen.analysis.phase_diagram import GrandPotentialPhaseDiagram, PhaseDiagram
from pymatgen.core.composition import Element
from pymatgen.entries.computed_entries import ComputedEntry
from tqdm.auto import tqdm

from rxn_network.core import Enumerator, Reaction
from rxn_network.costs.calculators import Calculator, ChempotDistanceCalculator
from rxn_network.entries.entry_set import GibbsEntrySet
from rxn_network.enumerators.utils import (
    apply_calculators,
    filter_entries_by_chemsys,
    get_computed_rxn,
    get_entry_by_comp,
    get_open_computed_rxn,
    get_total_chemsys,
    group_by_chemsys,
    initialize_calculators,
    initialize_entry,
)
from rxn_network.reactions import ComputedReaction
from rxn_network.thermo.chempot_diagram import ChempotDiagram

class MinimizeGibbsEnumerator(Enumerator):
    """
    Enumerator for finding all reactions between two reactants that are predicted by
    thermodynamics; i.e., they appear when taking the convex hull along a straight
    line connecting any two phases in G-x phase space. Identity reactions are
    automatically excluded.
    """

    def __init__(
        self,
        precursors: Optional[List[str]] = None,
        target: Optional[str] = None,
        calculators: Optional[List[str]] = None,
    ):
        """
        Args:
            precursors: Optional formulas of precursors.
            target: Optional formula of target; only reactions which make this target
                will be enumerated.
            calculators: Optional list of Calculator object names; see calculators
                module for options (e.g., ["ChempotDistanceCalculator])
        """
        super().__init__(precursors, target, calculators)

    def enumerate(self, entries):
        """
        Calculate all possible reactions given a set of entries. If the enumerator
        was initialized with a target, only reactions to this target will be considered.

        Args:
            entries: the set of all entries to enumerate from

        Returns:
            List of unique computed reactions.
        """
        entries = GibbsEntrySet(entries)

        target = None
        if self.target:
            target = initialize_entry(self.target, entries)
            entries.add(target)
            target_elems = {str(e) for e in target.composition.elements}

        precursors = None
        if self.precursors:
            precursors = {initialize_entry(f, entries) for f in self.precursors}
            for p in precursors:
                entries.add(p)
            precursor_elems = {
                str(elem) for p in precursors for elem in p.composition.elements
            }

        if "ChempotDistanceCalculator" in self.calculators:
            entries = entries.filter_by_stability(e_above_hull=0.0)
            self.logger.info(
                "Filtering by stable entries due to use of 'ChempotDistanceCalculator'"
            )

        combos = list(combinations(entries, 2))
        combos_dict = group_by_chemsys(combos)

        rxns = []
        for chemsys, combos in tqdm(combos_dict.items()):
            elems = chemsys.split("-")
            if (
                (target and not target_elems.issubset(elems))
                or (precursors and not precursor_elems.issuperset(elems))
                or len(elems) >= 10
            ):
                continue

            chemsys_entries = filter_entries_by_chemsys(entries, chemsys)
            pd = PhaseDiagram(chemsys_entries)
            calculators = initialize_calculators(self.calculators, chemsys_entries)

            for e1, e2 in combos:
                if precursors and {e1, e2} != precursors:
                    continue
                predicted_rxns = self._react_interface(
                    e1.composition, e2.composition, pd, calculators=calculators
                )
                rxns.extend(predicted_rxns)

        return list(set(rxns))

    def estimate_num_reactions(self, entries: List[ComputedEntry]) -> int:
        """
        Estimate the upper bound of the number of possible reactions. This will
        correlate with the amount of time it takes to enumerate reactions.

        Args:
            entries: A list of all entries to consider

        Returns: The upper bound on the number of possible reactions
        """
        return comb(len(entries), 2)

    def _react_interface(
        self, r1, r2, pd, grand_pd=None, open_entry=None, calculators=None
    ):
        "Simple API for InterfacialReactivity module from pymatgen."
        if grand_pd:
            interface = InterfacialReactivity(
                r1,
                r2,
                grand_pd,
                norm=True,
                include_no_mixing_energy=False,
                pd_non_grand=pd,
                use_hull_energy=True,
            )
            chempots = grand_pd.chempots

        else:
            interface = InterfacialReactivity(
                r1,
                r2,
                pd,
                norm=False,
                include_no_mixing_energy=False,
                pd_non_grand=None,
                use_hull_energy=True,
            )

        rxns = []
        for _, _, _, rxn, _ in interface.get_kinks():
            if grand_pd:
                rxn = get_open_computed_rxn(rxn, pd.all_entries, chempots)
            else:
                rxn = get_computed_rxn(rxn, pd.all_entries)

            if rxn.is_identity or rxn.lowest_num_errors > 0:
                continue

            if self.target and self.target not in rxn.product_entries:
                continue

            rxn = apply_calculators(rxn, calculators)
            rxns.append(rxn)

        return rxns

class MinimizeGrandPotentialEnumerator(MinimizeGibbsEnumerator):
    """
    Enumerator for finding all reactions between two reactants and an open element
    that are predicted by thermo; i.e., they appear when taking the
    convex hull along a straight line connecting any two phases in Phi-x
    phase space. Identity reactions are excluded.
    """

    def __init__(
        self,
        open_elem: Element,
        mu: float,
        precursors: Optional[List[str]] = None,
        target: Optional[str] = None,
        calculators: Optional[str] = None,
    ):
        super().__init__(precursors=precursors, target=target, calculators=calculators)
        self.open_elem = Element(open_elem)
        self.mu = mu

    def enumerate(self, entries):
        entries = GibbsEntrySet(entries)

        target = None
        if self.target:
            target = initialize_entry(self.target, entries)
            entries.add(target)
            target_elems = {str(e) for e in target.composition.elements}

        precursors = None
        if self.precursors:
            precursors = {initialize_entry(f, entries) for f in self.precursors}
            for p in precursors:
                entries.add(p)
            precursor_elems = {
                str(elem) for p in precursors for elem in p.composition.elements
            }

        if "ChempotDistanceCalculator" in self.calculators:
            entries = entries.filter_by_stability(e_above_hull=0.0)
            self.logger.info(
                "Filtering by stable entries due to use of 'ChempotDistanceCalculator'"
            )

        open_entry = sorted(
            filter(lambda e: e.composition.elements == [self.open_elem], entries),
            key=lambda e: e.energy_per_atom,
        )[0]
        entries_no_open = GibbsEntrySet(entries)
        entries_no_open.remove(open_entry)
        combos = list(combinations(entries_no_open, 2))
        combos_dict = group_by_chemsys(combos, self.open_elem)

        rxns = []
        for chemsys, combos in tqdm(combos_dict.items()):
            elems = chemsys.split("-")
            if (
                (target and not target_elems.issubset(elems))
                or (precursors and not precursor_elems.issuperset(elems))
                or len(elems) >= 10
            ):
                continue

            chemsys_entries = filter_entries_by_chemsys(entries, chemsys)
            pd = PhaseDiagram(chemsys_entries)

            calculators = initialize_calculators(self.calculators, chemsys_entries)

            grand_pd = GrandPotentialPhaseDiagram(
                chemsys_entries, {self.open_elem: self.mu}
            )
            for e1, e2 in combos:
                if precursors and {e1, e2} != precursors:
                    continue
                predicted_rxns = self._react_interface(
                    e1.composition,
                    e2.composition,
                    pd,
                    grand_pd,
                    open_entry,
                    calculators,
                )
                rxns.extend(predicted_rxns)

        return list(set(rxns))


# -*- coding: utf-8 -*-
import argparse
import pandas as pd
import print_result

def count_unk(path, UNK_TAG = '<unk>'):
    """
    path: Path to a space-separated file
    for [NT-OTH-1] in tech report
    """
    unk, total = 0, 0
    with open(path,'r',encoding='utf8') as f:
        tokens = [token for line in f.readlines() for token in line.split()]
        total = len(tokens)
        unk = sum([1 for token in tokens if UNK_TAG in token])
    return unk,total

def print_title(name, **kwargs):
    print(f"# Analysis of {name}")
    for kw,arg in kwargs.items():
        print(f"{kw}={arg}")

def parse_seg_file(seg_path):
    """
    Returns:
    -----------
        linenos: list of int. ordered by seg; corresponding to metadata
        temps2sents: dict. (states) -> [([phrases], lineno_in_seg_txt)]
            Example:
            key: (114, 201, 207, 184, 149, 252, 122, 75)
            value: [(['Each', 'banana', 'costs $ <num>',
                '. How much', 'do <num>', 'bananas', 'cost', '? <eos>'], 0)]
        top_temps: list of temps2sents keys, sorted by frequency of samples
    """
    from template_extraction import group_by_template
    linenos = []
    with open(seg_path,'r',encoding='utf8') as f:
        lines = f.readlines()
        for line in lines:
            if line.startswith('D label_train(): corpus.train_mb2linenos='): # HACK Don't contain '|'
                linenos = [ int(strno) for strno in line.strip().split('=')[1].split()]
                break
    temps2sents = group_by_template(seg_path,0) # startlineno=0; returns a label-tup -> [(phrase-list, lineno), ...] map
    top_temps = sorted(list(temps2sents.keys()), key=lambda x: -len(temps2sents[x]))
    return linenos, temps2sents, top_temps

def re_sort_metadata(metadata_path, linenos, new_idxname):
    try:
        metadata = pd.read_csv(metadata_path, sep='\t', header=0)
    except FileNotFoundError:
        return None

    # Sort metadata by linenos s.t. the idx match: metadata and temps2sents and seg file
    # 1. Sort linenos
    #   FROM  linenos[seg_index] = corresponding index in metadata
    #   TO    linenos[metadata] = corresponding index in seg_index
    linenos = [b for a,b in sorted(zip(linenos, range(len(linenos))), key=lambda x: x[0])]
    # 2. Add metadata col
    metadata[new_idxname] = linenos
    metadata = metadata.sort_values(by=new_idxname)
    metadata = metadata.set_index(new_idxname)
    return metadata

def analyze_seg(data,metadata_path,seg_path, k, n, pure_path=''):
    """
    Give statistics according to given metadata and segmentation
    """
    print_title("segmentation", metadata_path=metadata_path,seg_path=seg_path,k=k,n=n)

    def specific_top_templates(temps2sents,metadata, attribute, metadata_colnames,
            pure = False):
        """
        Parameters:
        -----------
            attribute:  str
                e.g. 'solution type'
            pure:  bool
                Whether print templates that is 'Addition' only (or so). Must match attribute
        """
        if metadata is None:
            return None

        # sort solution types (stype, e.g. 'Addition') by their frequencies (counts)
        from collections import Counter
        stype_counts = Counter(metadata[attribute])
        stypes = [stype for stype, count in sorted(list(stype_counts.items()), key=lambda x: -x[1])]

        # empty dictionary: stype -> { (temps) -> [sample sents] }
        stype2templates = dict(zip(stypes, [dict() for i in range(len(stypes))]))

        # stype -> [pure top_temps]
        pure_templates = dict()

        for temp, sents in temps2sents.items():
            for (phrases,lineno_in_seg_txt) in sents:
                stype = metadata[attribute][lineno_in_seg_txt]
                if temp not in stype2templates[stype]:
                    stype2templates[stype][temp] = []
                stype2templates[stype][temp].append((phrases,lineno_in_seg_txt))
        for stype in stypes:
            if stype_counts[stype] < 5:     # HACK to avoiding printing stypes of little samples
                continue
            print(f"## {stype} ({stype_counts[stype]} samples)")
            stype_temps2sents = stype2templates[stype]
            top_temps = sorted(list(stype_temps2sents.keys()), key=lambda x: -len(stype_temps2sents[x]))

            # For correct statistics, use temps2sents instead of stype_temps2sents below
            filters = {attribute:[stype]} if pure else None
            printed = print_result.top_templates_from_train(top_temps,temps2sents,metadata,
                metadata_colnames=metadata_colnames, n_toptemps=k, n_samples=n, filters=filters)
            if pure:
                pure_templates[stype] = [top_temps[p] for p in printed]

        if pure:
            with open(pure_path,'w') as f:
                f.writelines([f"{attribute}|{s}|||{' '.join([ ','.join([str(tt) for tt in t]) for t in pure_templates[s]])}\n" for s in stypes if s in pure_templates.keys()])

    linenos, temps2sents, top_temps = parse_seg_file(seg_path)
    metadata = re_sort_metadata(metadata_path, linenos, new_idxname='seg_linenos')

    # Evaluate the quality of segmentation
    print("# Overall - top templates")
    print_result.top_templates_from_train(top_temps, temps2sents, metadata, metadata_colnames=[],n_toptemps=k, n_samples=n) #'solution type','source','question'

    #print("# Solution type - top templates")
    #specific_top_templates(temps2sents,metadata, 'solution type', metadata_colnames=['solution type','source'], pure=False)

    #print('# Dataset - top templates')
    #specific_top_templates(temps2sents,metadata, 'source', metadata_colnames=['solution type','source'], pure=False)

    if pure_path is not '':
        print('# Pure templates: specific solution type')
        specific_top_templates(temps2sents,metadata, 'solution type', metadata_colnames=['solution type','source'], pure=True)

def analyze_gen(data, metadata_path, gen_path, startlineno=0, seg_path=''):
    """
    Give statistics according to metadata & generation file
    Adapted from group_by_template(fi, startlineno) in template_extraction.py
    1. Statistics: How many samples use which template? Compare to training data distribution?
    """

    print_title("generation", data=data, metadata_path=metadata_path, gen_path=gen_path)

    _,seg_temps2sents,_ = parse_seg_file(seg_path) if seg_path!='' else None,{},None

    import re
    from collections import defaultdict
    seg_patt = re.compile('([^\|]+)\|(\d+)') # detects segments
    temps2sents = defaultdict(list)
    lineno = startlineno

    try:
        metadata = pd.read_csv(metadata_path, sep='\t', header=0)
        conditions = ['' for i in range(metadata.shape[0])] # metadata.shape == (#rows, #cols)
    except FileNotFoundError:
        metadata = None

    #metadata = None #HACK  # cuz the linenos aren't for #alltmplt gen

    with open(gen_path, 'r', encoding='utf-8') as f:
        for line in f:
            if line.startswith('__start'):
                state = None
                condition = []
                for token in line.split():
                    if token.startswith('__start'):
                        assert state == None
                        state = token[8:-2]
                    elif token.startswith('__end'):
                        assert state == token[6:-2]
                        if metadata is not None:
                            conditions[lineno] += f'{state}:{" ".join(condition)}  '
                        state = None
                        condition = []
                    else:
                        condition.append(token)
            elif '|||' in line:
                mwp, seqs = line.split('|||')
                seq = seg_patt.findall(seqs.strip())
                wordseq, labeseq = zip(*seq) # 2 tuples
                wordseq = [phrs.strip() for phrs in wordseq]
                labeseq = tuple(int(labe) for labe in labeseq)
                temps2sents[labeseq].append((wordseq, lineno))  #mwp,
                lineno += 1

    top_temps = sorted(list(temps2sents.keys()), key=lambda x: -len(temps2sents[x]))
    if metadata is not None:
        metadata['conditions'] = conditions
        print_result.top_templates_from_train(top_temps, temps2sents, metadata,
            metadata_colnames=['conditions'], n_toptemps=999, n_samples=99999, seg_temps2sents=seg_temps2sents, n_examples=2)
    else:
        print_result.top_templates_from_train(top_temps, temps2sents, None,
            metadata_colnames=[], n_toptemps=999, n_samples=99999, seg_temps2sents=seg_temps2sents, n_examples=2)

#: err print
def eprint(*args, **kwargs):
    #print(*args, file=sys.stderr, **kwargs)
    pass

def get_pure_toptemps(pure_path, some_types):
    pure_temps = [] # [(attr,stype,[temps])]    e.g. ('solution type','Addition',[(0,1,2),(3,4,5)] )
    with open(pure_path) as f:
        for line in f:
            info , temps = line.split('|||')
            attr, stype = info.split('|')   # 'solution type', 'Addition'
            temps = [tuple(int(i) for i in t.split(',')) for t in temps.split(' ')]
            if stype in some_types:
                pure_temps.extend(temps)
    return pure_temps

parser = argparse.ArgumentParser(description='')
parser.add_argument('-data', type=str, default='', help='path to data dir')
parser.add_argument('-tagged_fi', type=str, default='', help='path to tagged file i.e. segmentation')
parser.add_argument('-gen_fi', type=str, default='', help='path to generation file')
parser.add_argument('-pure', type=str, default='', help='path to pure_temps')

parser.add_argument('-a_seg', action='store_true', help='Analysis segmentation file')
parser.add_argument('-a_gen', action='store_true', help='Analyze generation file')

if __name__ == '__main__':
    args = parser.parse_args()
    print(args)

    #org_unk, org_ttl = count_unk('segs/seg-otherTrain-100-55-5-far-NER-no_test.txt')
    #new_unk, new_ttl = count_unk('segs/seg-otherTrain-100-55-5-far-NER-no_test-unk.txt')
    #print(org_unk,org_ttl,org_unk/org_ttl)
    #print(new_unk,new_ttl,new_unk/new_ttl)
    DATA = args.data
    SEG = args.tagged_fi
    GEN = args.gen_fi
    PURE = args.pure
    #DATA='/Users/shanglinghsu/mwp/Datasets/ai2-ilds-train-valid/ai2-ilds-train-valid-concated'
    #SEG = '/Users/shanglinghsu/mwp/ntg2/segs/seg-ai2-cmds-100-55-5-far-NER.txt'
    #GEN = '/Users/shanglinghsu/mwp/ntg2/gens/gen-ai2-ilds-100-55-5-far-NER-pure_add-sub.md'
    if args.a_seg:
        analyze_seg(data=DATA, metadata_path=DATA+'/metadata_train.tsv', seg_path=SEG,
            k=100, n=10, pure_path=PURE)
    if args.a_gen:
        analyze_gen(data=DATA, metadata_path=DATA+'/metadata_valid.tsv', gen_path=GEN, seg_path=SEG)

    #PURE = 'pure_temps.txt'
    #get_pure_toptemps(PURE,'Addition')


'''
Created on Jul 11, 2018

@author: root
'''

from PyQt5 import QtWidgets
from pydm import PyDMApplication

class EmaApp (object):
    def __init__(self):
        ''' Ema App'''
        from interface_i import Ui_MainWindow
        self.window_MW = QtWidgets.QMainWindow()
        self.ui_MW = Ui_MainWindow()
        self.ui_MW.setupUi(self.window_MW)
        self.window_MW.show()
        '''
        from interface_i import Ui_MainWindow
        self.window_MW = QtWidgets.QMainWindow()
        self.ui_MW = Ui_MainWindow()
        self.ui_MW.setupUi(self.window_MW)
        self.window_MW.show()

        self.__setFlowControl()
        '''
        from OceanOptics import OceanOptics
        self.Ui_MainWindow_Ocean = QtWidgets.QMainWindow()
        self.ui_ocean = OceanOptics()
        self.ui_ocean.setupUi(self.Ui_MainWindow_Ocean)
        self.ui_ocean.setFlowControl()
        #self.Ui_MainWindow_Ocean.show()

        self.__setFlowControl()

    def  __setFlowControl(self):

        self.ui_MW.PyDMPushButton_pressuresystem.clicked.connect(self.__openOceanOptics)

    def __openOceanOptics(self):
        print('__openOceanOptics')
        if(self.ui_MW.PyDMCheckbox.isChecked()):
            self.Ui_MainWindow_Ocean.show()
            print('__openOceanOptics')
            '''
            from OceanOptics import OceanOptics
            self.Ui_MainWindow_Ocean = QtWidgets.QMainWindow()
            self.ui_ocean = OceanOptics()
            self.ui_ocean.setupUi(self.Ui_MainWindow_Ocean)
            self.ui_ocean.setFlowControl()
            self.Ui_MainWindow_Ocean.show()
            '''
        else:
            print('Box OceanOptics: Not Checked')

if __name__ == '__main__':
    pass
    import sys
    from OceanOptics import OceanOptics
    app = PyDMApplication()
    '''

    Ui_MainWindow_Ocean = QtWidgets.QMainWindow()
    ui_ocean = OceanOptics()
    ui_ocean.setupUi(Ui_MainWindow_Ocean)
    ui_ocean.setFlowControl()
    Ui_MainWindow_Ocean.show()
    '''
    emapp = EmaApp()
    sys.exit(app.exec_())
    '''
    app = PyDMApplication()
    emapp = EmaApp()
    sys.exit(app.exec_())
    QtWidgets.QApplication.closeAllWindows()

    '''

{'Ui_MainWindow': 'interface_i.Ui_MainWindow', 'OceanOptics': 'OceanOptics.OceanOptics'}


import os
import yaml

def parse_config(filepath):
    if not os.path.exists(filepath):
        raise FileNotFoundError(f'Config file {filepath} does not exist.')

    # Not wrapping in a try/except so that we get the
    # yaml library to raise any issues on parsing
    with open(filepath, 'r') as f:
        config = yaml.safe_load(f)

    spatial = config.get('spatial', None)
    if spatial == None:
        raise KeyError('Config is missing the root "spatial" key.')
    for k in ['latitudinal_srid', 'dem']:
        if k not in spatial.keys():
            raise KeyError(f'Missing key: {k} in the spatial section of the config.')

    database = config.get('database', None)
    if database == None:
        raise KeyError('Config is missing the root "database" key.')

    redis = config.get('redis', None)
    if redis == None:
        raise KeyError('Config is missing the root "redis" key.')

    for k in ['type', 'username', 'password', 'host', 'pgbouncer_port', 'name']:
        if k not in database.keys():
            raise KeyError(f'Missing key: "{k}" in the database section of the config.')

    return config


import time
import pytest
from pytest import raises, mark
import pydle
from .fixtures import with_client
from .mocks import Mock

pydle.client.PING_TIMEOUT = 10

## Initialization.

@pytest.mark.asyncio
@with_client(invalid_kwarg=False)
def test_client_superfluous_arguments(server, client):
    assert client.logger.warning.called

## Connection.
@pytest.mark.asyncio
@with_client()
async def test_client_reconnect(server, client):
    await client.disconnect(expected=True)
    assert not client.connected

    await client.connect(reconnect=True)
    assert client.connected

@pytest.mark.asyncio
@mark.slow
@with_client()
async def test_client_unexpected_disconnect_reconnect(server, client):
    client._reconnect_delay = Mock(return_value=0)
    await client.disconnect(expected=False)
    assert client._reconnect_delay.called

    time.sleep(0.1)
    assert client.connected

@pytest.mark.asyncio
@with_client()
async def test_client_unexpected_reconnect_give_up(server, client):
    client.RECONNECT_ON_ERROR = False
    await client.disconnect(expected=False)
    assert not client.connected

@pytest.mark.asyncio
@mark.slow
@with_client()
async def test_client_unexpected_disconnect_reconnect_delay(server, client):
    client._reconnect_delay = Mock(return_value=1)
    await client.disconnect(expected=False)

    assert not client.connected
    time.sleep(1.1)
    assert client.connected

@pytest.mark.asyncio
@with_client()
def test_client_reconnect_delay_calculation(server, client):
    client.RECONNECT_DELAYED = False
    assert client._reconnect_delay() == 0

    client.RECONNECT_DELAYED = True
    for expected_delay in client.RECONNECT_DELAYS:
        delay = client._reconnect_delay()
        assert delay == expected_delay

        client._reconnect_attempts += 1

    assert client._reconnect_delay() == client.RECONNECT_DELAYS[-1]

@pytest.mark.asyncio
@with_client()
async def test_client_disconnect_on_connect(server, client):
    client.disconnect = Mock()

    await client.connect("mock://local", 1337)
    assert client.connected
    assert client.disconnect.called

@pytest.mark.asyncio
@with_client(connected=False)
async def test_client_connect_invalid_params(server, client):
    with raises(ValueError):
        await client.connect()
    with raises(ValueError):
        await client.connect(port=1337)

@pytest.mark.asyncio
@mark.slow
@with_client()
async def test_client_timeout(server, client):
    client.on_data_error = Mock()
    time.sleep(pydle.client.BasicClient.READ_TIMEOUT + 1)

    assert client.on_data_error.called
    assert isinstance(client.on_data_error.call_args[0][0], TimeoutError)

@pytest.mark.asyncio
@with_client(connected=False)
async def test_client_server_tag(server, client):
    assert client.server_tag is None

    await client.connect("Mock.local", 1337)
    assert client.server_tag == "mock"
    await client.disconnect()

    await client.connect("irc.mock.local", 1337)
    assert client.server_tag == "mock"
    await client.disconnect()

    await client.connect("mock", 1337)
    assert client.server_tag == "mock"
    await client.disconnect()

    await client.connect("127.0.0.1", 1337)
    assert client.server_tag == "127.0.0.1"

    client.network = "MockNet"
    assert client.server_tag == "mocknet"
    await client.disconnect()

## Messages.

@pytest.mark.asyncio
@with_client()
async def test_client_message(server, client):
    client.on_raw_install = Mock()
    await server.send("INSTALL", "gentoo")
    assert client.on_raw_install.called

    message = client.on_raw_install.call_args[0][0]
    assert isinstance(message, pydle.protocol.Message)
    assert message.command == "INSTALL"
    assert message.params == ("gentoo",)

@pytest.mark.asyncio
@with_client()
async def test_client_unknown(server, client):
    client.on_unknown = Mock()
    await server.send("INSTALL", "gentoo")
    assert client.on_unknown.called


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
import datetime
from django.utils.timezone import utc

class Migration(migrations.Migration):

    dependencies = [
        ('members', '0001_initial'),
    ]

    operations = [
        migrations.AlterModelOptions(
            name='areaofstudy',
            options={'verbose_name_plural': 'Program', 'verbose_name': 'Program'},
        ),
        migrations.AlterModelOptions(
            name='member',
            options={'verbose_name_plural': 'Medlemmar', 'verbose_name': 'Medlem'},
        ),
        migrations.AddField(
            model_name='member',
            name='member_since',
            field=models.DateTimeField(default=datetime.datetime(2016, 11, 22, 0, 26, 49, 355201, tzinfo=utc), auto_now=True),
            preserve_default=False,
        ),
        migrations.AlterField(
            model_name='member',
            name='email',
            field=models.EmailField(unique=True, max_length=255, verbose_name='E-postadress'),
        ),
    ]


import logging

from aiohttp import web
from servicelib.aiohttp.application_setup import ModuleCategory, app_module_setup
from servicelib.aiohttp.rest_routing import (
    iter_path_operations,
    map_handlers_with_operations,
)

from ..constants import APP_CONFIG_KEY, APP_OPENAPI_SPECS_KEY
from ..login.decorators import login_required
from .handlers_redirects import get_redirection_to_viewer
from .handlers_rest import rest_handler_functions

logger = logging.getLogger(__name__)

@app_module_setup(
    "simcore_service_webserver.studies_dispatcher", ModuleCategory.ADDON, logger=logger
)
def setup_studies_dispatcher(app: web.Application) -> bool:
    cfg = app[APP_CONFIG_KEY]["main"]

    # Redirects routes
    redirect_handler = get_redirection_to_viewer
    if not cfg["studies_access_enabled"]:
        redirect_handler = login_required(get_redirection_to_viewer)
        logger.warning(
            "'%s' config explicitly disables anonymous users from this feature",
            __name__,
        )
    app.router.add_routes(
        [web.get("/view", redirect_handler, name="get_redirection_to_viewer")]
    )

    # Rest-API routes: maps handlers with routes tags with "viewer" based on OAS operation_id
    specs = app[APP_OPENAPI_SPECS_KEY]
    rest_routes = map_handlers_with_operations(
        rest_handler_functions,
        filter(lambda op: "viewer" in op.tags, iter_path_operations(specs)),
        strict=True,
    )
    app.router.add_routes(rest_routes)

    return True


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

import requests
import pandas as pd
import numpy as np
import json

class StockModel():
    def __init__(self, date, price):
        self.date = date
        self.price = price

class StockData:

    def __init__(self, trade_code):
        self.code = trade_code

    def fetch(self, months_count, date_index = False):
        url = 'https://www.dsebd.org/php_graph/monthly_graph.php?inst=' + self.code + '&duration=' + str(months_count) + '&type=price'
        r = requests.get(url)
        r.text
        data = r.text
        #companyCode = data.split("Closing Price Graph of ",1)[1].split('\'')[0]
        d = data.split('\\n')
        del d[0]
        del d[-1]
        for i in range(0, len(d)):
            if(i == 0):
                d[i] = d[i].split('" +\r\n    "')[1]
            else:
                d[i] = d[i].split('"+"')[1]

        stock2dList = []
        for i in range(len(d)):
            arr = d[i].split(",")
            arr[1] = float(arr[1])
            stock2dList.append(arr)

        df = pd.DataFrame(stock2dList)
        df.columns =['Date','Price']

        print("---------------------------------")
        print("Trading Code: " + self.code)
        print("---------------------------------")
        print("Days: " + str(df.shape[0]))
        print("---------------------------------")
        if date_index:
            df = df.set_index(pd.to_datetime(df['Date']))
            del df['Date']
        return df

    def format_df_json(self, df, format_json = True):
        arr = np.array(df)
        stock_list = []
        for i in range(len(arr)):
            sm = StockModel(arr[i][0], arr[i][1])
            stock_list.append(sm.__dict__)
        if format_json:
            return json.dumps(stock_list)
        else:
            return stock_list


from baseband import mark5b
import numpy as np
from scipy.stats.stats import pearsonr
import astropy.units as u
import matplotlib.pyplot as plt
from astropy.time import Time
from pulsar.predictor import Polyco

with open('starttimes.txt','r') as f:
	start_times = f.read()
	start_times = start_times.split('\n')[:-1]

filename = 'close_pairs.txt'
with open(filename,'r') as f:
	GP_times = f.read()
	GP_times = GP_times.split('\n')[:-1]
	GP_times_utc1 = [entry.split()[0] for entry in GP_times]
	GP_times_utc2 = [entry.split()[1] for entry in GP_times]
	GP_times_unix1 = np.array([Time(t).unix-Time(start_times[0]).unix for t in GP_times_utc1])
	GP_times_unix2 = np.array([Time(t).unix-Time(start_times[0]).unix for t in GP_times_utc2])
	scan_nos1 = [entry.split()[2] for entry in GP_times]
	scan_nos2 = [entry.split()[3] for entry in GP_times]
	#S_Ns = [entry.split()[2] for entry in GP_times]

'''
with open('close_pairs.txt','w') as f:
   for b in close_GP_pairs:
		f.write('{} {} {} {} {}'.format(b[0],b[1],b[2],b[3],b[4])

#Getting close pairs from time strings

close_GP_pairs = np.zeros(shape = (0,7))
#def get_time_lag():
for i,ts1 in enumerate(GP_times_utc):
	for j in range(len(GP_times_utc)-i):
		ts2 = GP_times_utc[i+j]
		t_gp1 = Time(ts1)
		t_gp2 = Time(ts2)
		dt = t_gp2-t_gp1
		if abs(dt.sec)<10 and abs(dt.sec)>0.01:
			close_GP_pairs = np.append(close_GP_pairs,np.array([[ts1,ts2,scan_nos[i],scan_nos[i+j],dt.sec]]),0)
			print i,j

print close_GP_pairs.shape
'''

def find_phase(t_GPs,scan_no):
	t0 = Time(start_times[scan_no-1])
	t_GP = Time(t_GPs)
	psr_polyco = Polyco('polyco_new.dat')
	phase_pol = psr_polyco.phasepol(t0)
	phase = np.remainder(phase_pol(t_GP.mjd), 1)
	return phase

phases1 = []
phases2 = []
#def plot_phase_time():
for i,t1 in enumerate(GP_times_utc1):
	phases1.append(find_phase(t1,int(scan_nos1[i])))
	phases2.append(find_phase(GP_times_utc2[i],int(scan_nos2[i])))
phases1 = [phase1%0.8 if phase1>=0.8 else phase1+0.2 for phase1 in phases1]
phases2 = [phase2%0.8 if phase2>=0.8 else phase2+0.2 for phase2 in phases2]
phases1 = np.array(phases1)
phases2 = np.array(phases2)
phases1 = (phases1-1.0/30010*GP_times_unix1)%1.0
phases2 = (phases2-1.0/30010*GP_times_unix2)%1.0
plt.figure()
plt.plot(phases1,GP_times_unix1,'o')
plt.plot(phases2,GP_times_unix2,'x')
plt.show()

#plot_phase_time()

'''
# The function below is for adding scan numbers to a txt file with times but no scan numbers

def write_scan_no():
	with open('starttimes.txt','r') as f:
		times = f.read()
		times = times.split('\n')[:-1]
		print len(times)
	text_name = 'all7_sorted.txt'
	with open(text_name, 'r') as f:
		text = f.read()
		text_lines = text.split('\n')[:-1]
		print len(text_lines)

	scan_nos = ['0']*len(text_lines)
	for line_no,line in enumerate(text_lines):
		strings = line.split()
		t_GP = Time(strings[0])
		for i,start_time in enumerate(times):
			ts = Time(start_time)
			ts_next = Time(times[i+1])
			if t_GP.mjd >ts.mjd and t_GP.mjd <ts_next.mjd:
				scan_nos[line_no] = format(i+1,'02d')
				print i
				break
	print scan_nos

with open('all7_sorted1.txt','w') as f:
	for i in range(len(text_lines)):
		f.write('{} {}\n'.format(scan_nos[i],text_lines[i]))
'''


from __future__ import annotations
from dataclasses import dataclass
import logging
from typing import List

from typedflow.nodes import DumpNode

__all__ = ['Flow']

logger = logging.getLogger(__file__)

@dataclass
class Flow:
    dump_nodes: List[DumpNode]

    def validate(self) -> None:
        """
        not implemented because Python Generic doesn't offer
        any ways to access to the actual type
        """
        pass

    async def run(self,
                  validate: bool = True) -> None:
        if validate:
            self.validate()
        batch_id: int = 0
        while True:
            for node in self.dump_nodes:
                await node.run_and_dump(batch_id=batch_id)
            if all([node.finished for node in self.dump_nodes]):
                return


# uncompyle6 version 3.7.4
# Python bytecode 2.7 (62211)
# Decompiled from: Python 3.6.9 (default, Apr 18 2020, 01:56:04)
# [GCC 8.4.0]
# Embedded file name: C:\repositorios\web\django-microsip-base\django_microsip_base\django_microsip_base\settings\dev_plus.py
# Compiled at: 2019-09-30 12:05:46
from common import *
DEBUG = True
TEMPLATE_DEBUG = DEBUG
MODO_SERVIDOR = 'PRUEBAS'
MICROSIP_VERSION = '2017'
EXTRA_MODULES = ()
DJANGO_APPS += ('django_extensions', 'werkzeug_debugger_runserver')
from .common import get_microsip_extra_apps
MICROSIP_EXTRA_APPS, EXTRA_APPS = get_microsip_extra_apps(EXTRA_MODULES, is_dev=True)
INSTALLED_APPS = DJANGO_APPS + MICROSIP_MODULES + MICROSIP_EXTRA_APPS
ROOT_URLCONF = 'django_microsip_base.urls.dev'
STATICFILES_DIRS = (
 BASE_DIR + '/static/',)


#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Matrix Decomposition: latent factor model
"""

#==============================================================================
# Module metadata variables
#==============================================================================

#==============================================================================
# Imports
#==============================================================================

import logging
import sys
import copy
import numpy as np
from scipy.optimize import fmin, fmin_powell

from .recommenders import BaseEventNIScorePredictor
from pyrecsys.md import latent_factor

#==============================================================================
# Public symbols
#==============================================================================

__all__ = ['EventNIScorePredictor']

#==============================================================================
# Constants
#==============================================================================

#==============================================================================
# Module variables
#==============================================================================

#==============================================================================
# Classes
#==============================================================================

class EventNIScorePredictor(BaseEventNIScorePredictor):
    """
    Information neutralized pyrecsys.md.latent_factor.EventScorePredictor

    Parameters
    ----------
    C : float, optional
        regularization parameter (= :math:`\lambda`), default=1.0
    eta : float, optional
        parameter of information neutrality term (= :math:`\eta`),
        default=1.0
    k : int, optional
        the number of latent factors (= sizes of :math:`\mathbf{p}_u` or
        :math:`\mathbf{q}_i`), default=1

    Attributes
    ----------
    `mu_` : array_like
        grobal biases
    `bu_` : array_like
        users' biases
    `bi_` : array_like
        items' biases
    `p_` : array_like
        latent factors of users
    `q_` : array_like
        latent factors of items
    `n_trgvars_` : int
        the numbers of possible target variables
    `i_loss_` : float
        the loss value after initialization
    `f_loss_` : float
        the loss value after fitting
    """

    def __init__(self, C=1.0, eta=1.0, k=1):
        super(EventNIScorePredictor, self).__init__()

        self.C = np.float(C)
        self.eta = np.float(eta)
        self.k = np.int(k)
        self.coef_ = None
        self.mu_ = None
        self.bu_ = None
        self.bi_ = None
        self.p_ = None
        self.q_ = None
        self.n_trgvars_ = 0
        self.i_loss_ = np.inf
        self.f_loss_ = np.inf

    def _init_coef(self, orig_data, tev, tsc, n_objects, **kwargs):
        """
        Initialize model parameters

        Parameters
        ----------
        orig_data : pyrecsys.data.EventWithScoreData
            data before separated depending on vals of target vars
        tev : array, shape(n_events, 2)
            separated event data
        tsc : array, shape(n_events,)
            scores attached to separated events
        n_objects : array, shape(2,)
            vector of numbers of objects
        kwargs : keyword arguments
            parameters for optmizers
        """

        # constants
        n_users = n_objects[0]
        n_items = n_objects[1]
        k = self.k
        n_trgvars = self.n_trgvars_

        # define dtype for parameters
        self._dt = np.dtype([
            ('mu', np.float, (1,)),
            ('bu', np.float, (n_users,)),
            ('bi', np.float, (n_items,)),
            ('p', np.float, (n_users, k)),
            ('q', np.float, (n_items, k))
        ])

        # memory allocation
        dt_itemsize = 1 + n_users + n_items +\
                      n_users * k + n_items * k
        self.coef_ = np.zeros(dt_itemsize * n_trgvars, dtype=np.float)

        # set array's view
        self.mu_ = self.coef_.view(self._dt)['mu']
        self.bu_ = self.coef_.view(self._dt)['bu']
        self.bi_ = self.coef_.view(self._dt)['bi']
        self.p_ = self.coef_.view(self._dt)['p']
        self.q_ = self.coef_.view(self._dt)['q']

        # init model by normal recommenders
        data = copy.copy(orig_data)
        recommender = latent_factor.EventScorePredictor(C=self.C, k=k)
        for t in xrange(n_trgvars):
            data.event = tev[t]
            data.n_events = tev[t].shape[0]
            data.score = tsc[t]
            recommender.fit(data, **kwargs)

            self.mu_[t][:] = recommender.mu_
            self.bu_[t][:] = recommender.bu_[:n_users]
            self.bi_[t][:] = recommender.bi_[:n_items]
            self.p_[t][:, :] = recommender.p_[:n_users, :]
            self.q_[t][:, :] = recommender.q_[:n_items, :]

        # scale a regularization term by the number of parameters
        self._reg = self.C / np.float((dt_itemsize - 2 - 2 * k) * n_trgvars)

    def loss(self, coef, tev, tsc, n_objects, score_bins):
        """
        loss function to optimize

        main loss function: same as the pyrecsys.md.latent_factor.

        information neutrality term:

        To estimate distribution of estimated scores, we adopt a histogram
        model. Estimated scores are first discretized into bins according to
        `score_bins`. Distributions are derived from the frequencies of
        events in these bins.

        \sum_{u, i, t} \sum_{s \in bins} \
            I(bin(\hat{s}(u, i, t)) == s) log p[s | t] - log p[s]

        u, i, t are user_index, item_index, information neutral target var,
        respectively. \hat(s)(u, i, t) is estimated score, bin(s) is a
        function that returns index of the bin to which s belongs.
        Distribution p[y | t] is derived by generating histogram from a data
        set that is consists of events whose target values equal to t.
        p[s] = \sum_t \p[t] p[s | t].

        Parameters
        ----------
        coef : array_like, shape=(variable,)
            coefficients of this model
        tev : array_like, shape(n_events, 2), dtype=int
            user and item indexes
        tsc : array_like, shape(n_events,), dtype=float
            target scores
        n_objects : array_like, shape(2,), dtype=int
            numbers of users and items
        score_bins : array_like, shape=(variable,) dtype=float, optional
            thresholds to discretize scores.

        Returns
        -------
        loss : float
            value of loss function
        """
        # constants
        n_events = np.array([ev.shape[0] for ev in tev])
        n_users = n_objects[0]
        n_items = n_objects[1]
        k = self.k

        # set array's view
        mu = coef.view(self._dt)['mu']
        bu = coef.view(self._dt)['bu']
        bi = coef.view(self._dt)['bi']
        p = coef.view(self._dt)['p']
        q = coef.view(self._dt)['q']

        # basic stats
        esc = np.empty(self.n_trgvars_, dtype=object)
        pesct = np.empty((self.n_trgvars_, len(score_bins) - 1),
                         dtype=np.float)
        for t in xrange(self.n_trgvars_):
            ev = tev[t]
            esc[t] = mu[t][0] + bu[t][ev[:, 0]] + bi[t][ev[:, 1]] +\
                     np.sum(p[t][ev[:, 0], :] * q[t][ev[:, 1], :], axis=1)
            pesct[t, :] = np.histogram(esc[t], score_bins)[0]
        pesc = np.sum(pesct, axis=0) / np.sum(n_events)
        pesct = pesct / n_events[:, np.newaxis]

        # loss term and information neutrality term
        loss = 0.0
        for t in xrange(self.n_trgvars_):
            loss += np.sum((tsc[t] - esc[t]) ** 2)

        # information neutrality term
        in_term = 0.0
        for t in xrange(self.n_trgvars_):
            # NOTE: to avoid 0 in log
            pos = np.nonzero(pesct[t, :] > 0.0)[0]
            in_term += np.dot(pesct[t, pos],
                              np.log(pesct[t, pos]) - np.log(pesc[pos])) *\
                       n_events[t]

        # regularization term
        reg = 0.0
        for t in xrange(self.n_trgvars_):
            reg += np.sum(bu[t] ** 2) + np.sum(bi[t] ** 2) +\
                   np.sum(p[t] ** 2) + np.sum(q[t] ** 2)

        return loss / np.float(np.sum(n_events)) +\
               self.eta * in_term / np.float(np.sum(n_events)) +\
               self._reg * reg

    def fit(self, data, trg, n_trgvars=2,
            user_index=0, item_index=1, score_index=0,
            score_bins=None, **kwargs):
        """
        fitting model

        Parameters
        ----------
        data : :class:`pyrecsys.data.EventWithScoreData`
            data to fit
        trg : array_like, shape=(n_events,)
            a variable to be neutral in recommendation
        n_trgvars : int
            the numbers of possible target variables
        user_index : optional, int
            Index to specify the position of a user in an event vector.
            (default=0)
        item_index : optioanl, int
            Index to specify the position of a item in an event vector.
            (default=1)
        score_index : optional, int
            Ignored if score of data is a single criterion type. In a multi-
            criteria case, specify the position of the target score in a score
            vector. (default=0)
        score_bins : array_like, ndim=1
            a vector of thresholds to separate scores to bins
            default=[-np.inf, 1.5, 2.5, 3.5, 4.5, np.inf],
            for 1 to 5 rating scores
        kwargs : keyowrd arguments
            keyword arguments passed to optimizers
        """

        # set parameters
        self.n_trgvars_ = n_trgvars
        if score_bins is None:
            score_bins = np.array([-np.inf, 1.5, 2.5, 3.5, 4.5, np.inf])

        # get input data
        ev, sc, n_objects =\
        self._get_event_and_score(data,
            (user_index, item_index), score_index)

        # divide events and scores according to the corresponding target
        # variables
        tev = np.empty(self.n_trgvars_, dtype=np.object)
        tsc = np.empty(self.n_trgvars_, dtype=np.object)
        for t in xrange(self.n_trgvars_):
            tev[t] = ev[trg == t, :]
            tsc[t] = sc[trg == t]

        # initialize coefficients
        self._init_coef(data, tev, tsc, n_objects,
                        gtol=kwargs.pop('gtol', 1e-05),
                        maxiter=kwargs.get('maxiter', None),
                        disp=kwargs.get('disp', False))

        # check optimization parameters
        if not 'disp' in kwargs:
            kwargs['disp'] = False
        if 'gtol' in kwargs:
            del kwargs['gtol']

        # get final loss
        self.i_loss_ = self.loss(self.coef_, tev, tsc, n_objects, score_bins)

        # optimize model
        self.coef_[:] = fmin_powell(self.loss,
                                    self.coef_,
                                    args=(tev, tsc, n_objects, score_bins),
                                    **kwargs)

        # add parameters for unknown users and items
        self.bu_ = np.empty(self.n_trgvars_, dtype=object)
        self.bi_ = np.empty(self.n_trgvars_, dtype=object)
        self.p_ = np.empty(self.n_trgvars_, dtype=object)
        self.q_ = np.empty(self.n_trgvars_, dtype=object)
        for t in xrange(self.n_trgvars_):
            self.bu_[t] = np.r_[self.coef_.view(self._dt)['bu'][t], 0.0]
            self.bi_[t] = np.r_[self.coef_.view(self._dt)['bi'][t], 0.0]
            self.p_[t] = np.r_[self.coef_.view(self._dt)['p'][t],
                               np.zeros((1, self.k), dtype=np.float)]
            self.q_[t] = np.r_[self.coef_.view(self._dt)['q'][t],
                               np.zeros((1, self.k), dtype=np.float)]

        # get final loss
        self.f_loss_ = self.loss(self.coef_, tev, tsc, n_objects, score_bins)

        # clean up temporary instance variables
        del self.coef_
        del self._reg
        del self._dt

    def raw_predict(self, ev, trg):
        """
        predict score of given one event represented by internal ids

        Parameters
        ----------
        (user, item) : array_like
            a target user's and item's ids. unknwon objects assumed to be
            represented by n_object[event_otype]

        Returns
        -------
        ev : array_like, shape=(s_events,) or (variable, s_events)
            events for which scores are predicted
        trg : int or array_like, dtype=int
            target values to enhance information neutrality

        Raises
        ------
        TypeError
            shape of an input array is illegal
        """

        if ev.ndim == 1:
            return self.mu_[trg][0] +\
                   self.bu_[trg][ev[0]] + self.bi_[trg][ev[1]] +\
                   np.dot(self.p_[trg][ev[0]], self.q_[trg][ev[1]])
        elif ev.ndim == 2:
            return np.array([self.mu_[t][0] +\
                             self.bu_[t][ev[i, 0]] + self.bi_[t][ev[i, 1]] +\
                             np.dot(self.p_[t][ev[i, 0], :],
                                    self.q_[t][ev[i, 1], :])
                             for i, t in enumerate(trg)])
        else:
            raise TypeError

#==============================================================================
# Functions
#==============================================================================

#==============================================================================
# Module initialization
#==============================================================================

# init logging system ---------------------------------------------------------

logger = logging.getLogger('pyrecsys')
if not logger.handlers:
    logger.addHandler(logging.NullHandler)

#==============================================================================
# Test routine
#==============================================================================

def _test():
    """ test function for this module
    """

    # perform doctest
    import sys
    import doctest

    doctest.testmod()

    sys.exit(0)

# Check if this is call as command script -------------------------------------

if __name__ == '__main__':
    _test()


from __future__ import absolute_import, division, print_function, unicode_literals

import numpy as np
import pandas as pd

import tensorflow as tf

from tensorflow import feature_column
from tensorflow.keras import layers
from sklearn.model_selection import train_test_split

path_to_file = "./winequality-red.csv"
df = pd.read_csv(path_to_file, sep=",")
print(df.head(20))

train, test = train_test_split(df, test_size=0.2)
train, val   = train_test_split(train, test_size=0.2)

def df_to_dataset(df, shuffle=True, batch_size=32):
  df      = df.copy()
  labels  = df.pop('quality')
  ds      = tf.data.Dataset.from_tensor_slices((dict(df), labels))

  if shuffle:
    ds = ds.shuffle(buffer_size=len(df))

  ds = ds.batch(batch_size)
  return ds

batch_size  = 5
train_ds    = df_to_dataset(train, shuffle=True, batch_size=batch_size)
val_ds      = df_to_dataset(val, shuffle=True, batch_size=batch_size)
test_ds     = df_to_dataset(test, shuffle=True, batch_size=batch_size)

for feature_batch, label_batch in train_ds.take(1):
  print("dnesity feature", feature_batch['density'])

example_batch = next(iter(train_ds))[0]

def demo(feature_column):
  feature_layer = layers.DenseFeatures(feature_column)
  print(feature_layer(example_batch).numpy())

dioxide = feature_column.numeric_column('free_sulfur_dioxide')
demo(dioxide)

dioxide_buckets = feature_column.bucketized_column(dioxide, boundaries=[2, 6, 10, 14, 18, 24, 30, 50])

demo(dioxide_buckets)

#    fixed acidity  volatile acidity  citric acid  residual sugar  chlorides  free sulfur dioxide  total sulfur dioxide  density    pH  sulphates  alcohol  quality
#0             7.4             0.700         0.00             1.9      0.076                 11.0                  34.0   0.9978  3.51       0.56      9.4        5

feature_columns = []
for header in ['fixed_acidity', 'volatile_acidity', 'residual_sugar', 'chlorides', 'free_sulfur_dioxide', 'total_sulfur_dioxide', 'density', 'pH', 'sulphates', 'alcohol']:
  d = feature_column.numeric_column(header)
  print("****Demo Begins****", header)
  demo(d)
  feature_columns.append(d)

#feature_columns.append(dioxide_buckets)

feature_layer = tf.keras.layers.DenseFeatures(feature_columns)

print(test.head())

batch_size = 32
train_ds   = df_to_dataset(train, batch_size=batch_size)
val_ds     = df_to_dataset(val, shuffle=False, batch_size=batch_size)
test_ds    = df_to_dataset(test, shuffle=False, batch_size=batch_size)
print(test_ds.take(1))

model = tf.keras.Sequential([
  feature_layer,
  tf.keras.layers.Dense(128, activation='relu'),
  tf.keras.layers.Dense(128, activation='relu'),
  tf.keras.layers.Dense(1)
])

model.compile(optimizer='adam',
              loss=tf.keras.losses.BinaryCrossentropy(from_logits=True),
              )

model.fit(train_ds,
          validation_data=val_ds,
          epochs=5)

loss, accuracy = model.evaluate(test_ds)
#print("Accuracy--->", accuracy)


import os
import smtplib
#import mandrill

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
from email.utils import COMMASPACE,formatdate
from email import encoders

def send_mail(fro, to, subject, text, files=[]):
    #assert type(server) == dict
    assert type(to) == list
    assert type(files) == list

    msg = MIMEMultipart('alternative')

    msg['Subject'] = subject
    msg['From'] = 'CAP_TEAM@thomsonreuters.com' # Your from name and email address
    msg['To'] = ','.join(to)
    #print msg['To']
    msg['Date'] = formatdate(localtime=True)
    msg.attach(MIMEText(text, 'html', 'utf-8'))

    for file in files:
        data = open(file, 'rb')
        part = MIMEBase('application', 'octet-stream') #'octet-stream': binary data
        part.set_payload(data.read())
        data.close()
        encoders.encode_base64(part)
        part.add_header('Content-Disposition', 'attachment', filename=os.path.basename(file))
        msg.attach(part)

    s = smtplib.SMTP('10.80.81.132', 25)
    #s.login(username, password)
    s.sendmail(fro, to, msg.as_string())
    s.quit()

    ''' mandrill web api way
    try:
        mandrill_client = mandrill.Mandrill('ElZncQlS9DXqotj0TMjuJA')
        #to = ['chao.xie@thomsonreuters.com', 'yang.yang@thomsonreuters.com']
        raw_message = msg.as_string()
        #print raw_message
        result = mandrill_client.messages.send_raw(raw_message, from_email=None, from_name='Chao Xie', to=None, async=False, ip_pool='Main Pool', send_at=None, return_path_domain=None)

    except mandrill.Error, e:
        # Mandrill errors are thrown as exceptions
        print 'A mandrill error occurred: %s - %s' % (e.__class__, e)
        # A mandrill error occurred: <class 'mandrill.UnknownSubaccountError'> - No subaccount exists with the id 'customer-123'
        raise
    '''

def send_mail_ex(to, subject, content, fro='CAP_TEAM@thomsonreuters.com', cc=[], bcc=[], images=[], attaches=[]):
    msgRoot = MIMEMultipart('related')
    msgRoot['Subject'] = subject
    msgRoot['From'] = fro # Your from name and email address
    msgRoot['To'] = ','.join(to)
    msgRoot['Cc'] = ','.join(cc)
    msgRoot['Bcc'] = ','.join(bcc)

    # Add image html into body
    ind = 1
    for img in images:
        content += '

'
        ind += 1

    # Body
    msgText = MIMEText(content, 'html', 'utf-8')
    msgRoot.attach(msgText)

    # Image in body
    ind = 1
    for img in images:
        fp = open(img, 'rb')
        msgImage = MIMEImage(fp.read())
        fp.close()
        msgImage.add_header('Content-ID', '<image'+str(ind)+'>')
        msgRoot.attach(msgImage)
        ind += 1

    # Attachment
    for f in attaches:
        data = open(f, 'rb')
        part = MIMEBase('application', 'octet-stream') #'octet-stream': binary data
        part.set_payload(data.read())
        data.close()
        encoders.encode_base64(part)
        part.add_header('Content-Disposition', 'attachment', filename=os.path.basename(f))
        msgRoot.attach(part)

    s = smtplib.SMTP('10.80.81.132', 25)
    s.sendmail(fro, [to, ','.join(cc), ','.join(bcc)], msgRoot.as_string())
    s.quit()

if __name__ == "__main__":
    # cc = ['jiu.chen@thomsonreuters.com', 'liang.zhang1@thomsonreuters.com']
    to = ['jiu.chen@thomsonreuters.com']
    subject = "Python Email with Pictures"
    text1 = 'HTML content
baidu
'
    att = ['tickets-per-region.png']
    images = ['total-tickets.png', 'tickets-per-system.png']
    # send_mail(fro, to, subject, text, files)
    send_mail_ex(to, subject, text1, images=images, attaches=att)


import pygame
from pathfinder import Graph, AStar
from copy import copy
import player

# for position checking
MARGIN = 48

GRID_INCREMENT = player.GRID_INCREMENT
NODE_STEP = player.node_step

class AIHANDLER:
    def __init__(self, red_player, blue_player, health_pack, node_graph):
        self.red_player = red_player
        self.blue_player = blue_player
        self.health_pack = health_pack

        # the neural_net variable will test a primitive neural network, for now it is null
        self.neural_net = None
        self.score = 0

        # bits to determine what the player is doing
        self.CHASING = 2
        self.SHOOTING = 4
        self.FLEEING = 8
        self.RESTING = 16

        self.current_action = 2

        # bits to determine where player is located in regards to opponent
        self.red_is_left = 2
        self.red_is_right = 4
        self.red_is_up = 8
        self.red_is_down = 16

        self.current_position = 2

        self.node_graph = node_graph
        self.a_star = AStar(copy(self.node_graph))

        self.finding_path = False
        self.getting_coordinates = True

        self.subprogram = None

    def run_battle(self):

       if self.subprogram == "states":
            if self.current_action == self.RESTING:
                return
            # first we check if the agent is healthy, if not we run away
            if not self.red_player.is_player_healthy():
                self.current_action = self.FLEEING

            if self.current_action == self.CHASING:
                 self.determine_movement()
            # check the agent's current state and act accordingly
            elif self.current_action == self.SHOOTING:
                self.shoot_bullets()
                self.determine_movement()
            elif self.current_action == self.FLEEING:
                self.run_away()

                # print(self.current_action)
       elif self.subprogram == "pathfinding":
           self.determine_movement()

    def determine_movement(self):
        if self.subprogram == "pathfinding":
            # if our path list is currently empty do nothing
            if not self.a_star.final_path_list:
                self.finding_path = False
                return

            # find the next node in our path for the red_player to travel to
            next_node_coordinate = ((self.a_star.final_path_list[0].x), (self.a_star.final_path_list[0].y))
            # place the player's coordinates into variables for checking
            red_coordinate = ((self.red_player.rect.centerx), (self.red_player.rect.centery))

            # until we reach the node keep moving towards it
            if 2 <= abs(red_coordinate[0] - next_node_coordinate[0]) or 2 <= abs(red_coordinate[1] - next_node_coordinate[1]):
                self.red_player.move_to_next_node(next_node_coordinate)
            else:
                # then remove the current node so we can move toward the next node
                print("popping Node #: " + str(self.a_star.final_path_list[0].name))
                self.a_star.final_path_list.pop(0)

        if self.subprogram == "states":
            turning_around = False
            blue_coordinate = ((self.blue_player.rect.centerx), (self.blue_player.rect.centery))
            red_coordinate = ((self.red_player.rect.centerx), (self.red_player.rect.centery))

            # should we chase the player? if we are too far away then we will chase
            if (100 <= abs(blue_coordinate[0] - red_coordinate[0]) or 100 <= abs(blue_coordinate[0] - red_coordinate[0]))\
                and not self.finding_path:
                self.find_node(blue_coordinate[0], blue_coordinate[1])
                self.blue_player.previous_x = blue_coordinate[0]
                self.blue_player.previous_y = blue_coordinate[1]
                self.current_action = self.CHASING
            elif 300 <= abs(self.blue_player.previous_x - blue_coordinate[0]) or\
                100 <= abs(self.blue_player.previous_y - blue_coordinate[1]):
                self.change_course(blue_coordinate)
            elif 5 > abs(blue_coordinate[0] - red_coordinate[0]) or 5 > abs(blue_coordinate[1] - red_coordinate[1]):
                self.move_freely(blue_coordinate)
            else:
                self.current_action = self.SHOOTING

    def move_through_path(self):
        # if our path list is currently empty do nothing
        if not self.a_star.final_path_list:
            self.finding_path = False
            return

        # find the next node in our path for the red_player to travel to
        next_node_coordinate = ((self.a_star.final_path_list[0].x), (self.a_star.final_path_list[0].y))
        # place the player's coordinates into variables for checking
        red_coordinate = ((self.red_player.rect.centerx), (self.red_player.rect.centery))

        # until we reach the node keep moving towards it
        if 1 <= abs(red_coordinate[0] - next_node_coordinate[0]) or 1 <= abs(red_coordinate[1] - next_node_coordinate[1]):
            self.red_player.move_to_next_node(next_node_coordinate)
        else:
            # then remove the current node so we can move toward the next node
            if self.subprogram == "pathfinding":
                print("popping Node #: " + str(self.a_star.final_path_list[0].name))
            self.a_star.final_path_list.pop(0)

    def change_course(self, blue_coordinate):
        if self.a_star.final_path_list:
            self.a_star.start_node_index = self.find_node_index(self.a_star.final_path_list[0])
        del self.a_star.final_path_list
        self.find_node(blue_coordinate[0], blue_coordinate[1])
        self.blue_player.previous_x = blue_coordinate[0]
        self.blue_player.previous_y = blue_coordinate[1]

    def shoot_bullets(self):
        bullet_List = pygame.sprite.Group()

        red_coordinate = ((self.red_player.rect.centerx), (self.red_player.rect.centery))

        blue_coordinate = ((self.blue_player.rect.centerx), (self.blue_player.rect.centery))

        if red_coordinate[0] + MARGIN >= blue_coordinate[0] and red_coordinate[0] - MARGIN <= blue_coordinate[0]:
            # red player is above blue player
            if red_coordinate[1] < blue_coordinate[1] - MARGIN:
                self.current_position = self.red_is_up

            # red player is below blue player
            elif red_coordinate[1] > blue_coordinate[1] + MARGIN:
                self.current_position = self.red_is_down
        else:
            if red_coordinate[0] > blue_coordinate[0]:
                self.current_position = self.red_is_right
            elif red_coordinate[0] < blue_coordinate[0]:
                self.current_position = self.red_is_left

        if self.current_position == self.red_is_left:
            bullet_List.add(self.red_player.shoot_right())
        elif self.current_position == self.red_is_right:
            bullet_List.add(self.red_player.shoot_left())
        elif self.current_position == self.red_is_up:
            bullet_List.add(self.red_player.shoot_down())
        elif self.current_position == self.red_is_down:
            bullet_List.add(self.red_player.shoot_up())

        if self.finding_path:
            self.move_through_path()

        return bullet_List

    def run_away(self):

        health_pack_coordinate = (self.health_pack.rect.centerx, self.health_pack.rect.centery)
        red_coordinate = (self.red_player.rect.centerx, self.red_player.rect.centery)

        if not self.finding_path:
            self.find_node(health_pack_coordinate[0], health_pack_coordinate[1])

        if GRID_INCREMENT * NODE_STEP >= abs(red_coordinate[0] - health_pack_coordinate[0]) and \
           GRID_INCREMENT * NODE_STEP >= abs(red_coordinate[1] - health_pack_coordinate[1]):
            self.move_freely(health_pack_coordinate)
        elif self.finding_path:
            self.move_through_path()

        if self.red_player.rect.colliderect(self.health_pack):
            self.red_player.hit_points += 100
            self.red_player.set_healthy()
            print("picked up health pack")
            self.current_action = self.CHASING

    def move_freely(self, target):
        self.red_player.chase_object(target)

        return False

    def find_node(self, x_coordinate, y_coordinate):
        # we make a deep copy of our graph because we will be removing nodes when we run the A*
        # and we don't want it to change our original graph
        #self.a_star.unvisited = copy(self.node_graph)
        search_index = 0
        if x_coordinate > 550 and y_coordinate > 220:
            search_index = len(self.node_graph) / 2
        # for each node in our graph, we check the coordinates to see if it's "close enough" to our user coordinates
        for node in self.node_graph[search_index:]:
            if GRID_INCREMENT * NODE_STEP >= abs(node.x - x_coordinate) and GRID_INCREMENT * NODE_STEP >= abs(node.y - y_coordinate):
                print("matched coordinates with node# " + str(node.name))
                # use the matched node as the end node which we find the path to
                self.a_star.find_path(node)
                # our next start node will be our current end_node for the next time we find a path
                self.a_star.start_node_index = self.node_graph.index(node)
                self.finding_path = True
                self.getting_coordinates = False
                break

    def find_node_index(self, start_node):
        node_name = start_node.name
        for node in self.node_graph:
            if node.name == node_name:
                return self.node_graph.index(node)

    def getAction(self):
        new_action = input("ENTER COMMAND: ")

        if new_action.upper() == "CHASE":
            self.current_action = self.CHASING


import unittest
from unittest import mock

from unittest.mock import patch, MagicMock

from dataprep.bpepkg import merge
from dataprep.bpepkg.merge import MergeList, Merge

@patch('dataprep.bpepkg.merge.open')
class ReadMergesTest(unittest.TestCase):
    def test_simple(self, open_mock):
        open_mock.return_value = MagicMock(spec=['__enter__', '__exit__'])
        handle = open_mock.return_value.__enter__.return_value
        handle.__iter__.return_value = iter(['a b 67', 'b c 34', 'c d 94'])

        actual = merge.read_merges('file', 2)
        expected = MergeList().append(Merge(('a', 'b'), 67, 0)).append(Merge(('b', 'c'), 34, 1))

        self.assertEqual(expected, actual)

    def test_wrong_delimiter(self, open_mock):
        with self.assertRaises(ValueError):
            open_mock.return_value = MagicMock(spec=['__enter__', '__exit__'])
            handle = open_mock.return_value.__enter__.return_value
            handle.__iter__.return_value = iter(['a\tb\t67'])

            merge.read_merges('file')

@patch('dataprep.bpepkg.merge.open')
class WriteMergesTest(unittest.TestCase):
    def test_simple(self, open_mock):
        open_mock.return_value = MagicMock(spec=['__enter__', '__exit__'])
        handle = open_mock.return_value.__enter__.return_value

        merges = MergeList().append(Merge(('a', 'b'), 67, 0)).append(Merge(('b', 'c'), 34, 1))
        merge.dump_merges(merges, 'file')

        handle.write.assert_has_calls([
            mock.call('a b 67\n'),
            mock.call('b c 34\n')
        ])

class MergeTest(unittest.TestCase):
    def test_merge_as_key(self):
        merge1 = Merge(('a', 'b'), 34, 0)
        merge2 = Merge(('a', 'b'), 34, 0)
        map = {merge1: 3}

        self.assertEqual(3, map[merge2])

class MergeListTest(unittest.TestCase):
    def test_append_wrong_priority(self):
        with self.assertRaises(ValueError):
            merges = MergeList()
            merges.append(Merge(('a', 'b'), 34, 3))

    def test_get_priority(self):
        merges = MergeList()
        merges.append(Merge(('a', 'b'), 34, 0)).append(Merge(('b', 'c'), 44))
        actual = merges.get_priority(('b', 'c'))

        self.assertEqual(1, actual)

class MergeListInclusionTest(unittest.TestCase):
    def test_present(self):
        merges = MergeList()
        merges.append(Merge(('a', 'b'), 34, 0)).append(Merge(('b', 'c'), 44, 1))
        self.assertTrue(('a', 'b') in merges)

    def test_absent(self):
        merges = MergeList()
        merges.append(Merge(('a', 'b'), 34, 0)).append(Merge(('b', 'c'), 44, 1))
        self.assertFalse(('g', 'r') in merges)

class MergeListIndexTest(unittest.TestCase):
    def test_simple(self):
        merges = MergeList()
        merges.append(Merge(('a', 'b'), 34, 0)).append(Merge(('b', 'c'), 44))

        actual = merges[1]
        expected = (Merge(('b', 'c'), 44, 1))

        self.assertEqual(expected, actual)

    def test_out_of_range(self):
        with self.assertRaises(IndexError):
            merges = MergeList()
            merges.append(Merge(('a', 'b'), 34, 0)).append(Merge(('b', 'c'), 44, 1))

            actual = merges[2]

    def test_negative(self):
        merges = MergeList()
        merges.append(Merge(('a', 'b'), 34, 0)).append(Merge(('b', 'c'), 44))

        actual = merges[-1]
        expected = (Merge(('b', 'c'), 44, 1))

        self.assertEqual(expected, actual)

    def test_range_with_negative(self):
        merges = MergeList()
        merges.append(Merge(('a', 'b'), 34)).append(Merge(('b', 'c'), 44))

        actual = merges[0:-1]
        expected = [Merge(('a', 'b'), 34, 0)]

        self.assertEqual(expected, actual)

class MergeListPlusOperationTest(unittest.TestCase):
    def test_simple(self):
        merges1 = MergeList()
        merges1.append(Merge(('a', 'b'), 34, 0)).append(Merge(('b', 'c'), 44, 1))

        merges2 = MergeList()
        merges2.append(Merge(('c', 'd'), 64, 0)).append(Merge(('d', 'e'), 84, 1))

        expected = MergeList()
        expected.append(Merge(('a', 'b'), 34, 0)).append(Merge(('b', 'c'), 44, 1))\
            .append(Merge(('c', 'd'), 64, 2)).append(Merge(('d', 'e'), 84, 3))

        actual = merges1 + merges2

        self.assertEqual(expected, actual)

    def test_add_wrong_type(self):
        with self.assertRaises(TypeError):
            merges = MergeList()
            merges + [(('d', 'e'), 84, 1)]

class MergeListLenTest(unittest.TestCase):
    def test_simple(self):
        merges = MergeList()
        merges.append(Merge(('a', 'b'), 34, 0)).append(Merge(('b', 'c'), 44, 1))

        actual = len(merges)

        self.assertEqual(2, actual)

class MergeListIteratorTest(unittest.TestCase):
    def test_simple(self):
        merges = MergeList()
        merges.append(Merge(('a', 'b'), 34, 0)).append(Merge(('b', 'c'), 44, 1))

        for idx, merge in enumerate(merges):
            self.assertEqual(merges[idx], merge)

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


# import Image as Image
import PIL.Image as Image
import PIL.ImageDraw as ImageDraw
import os
import time

FILE_NAME = 'in.txt'
IMAGES_PATH = './image/'  # 正数图片集地址
IMAGE_SIZE = 36  # 每张小图片的大小
IMAGE_SAVE_PATH = './result/'  # 图片转换后的地址
IMAGE_RESIZE_SCALE = 2.5  # 放大倍率

arrOld = []

def read_file():
    # 读取文件
    with open(FILE_NAME) as f:
        for line in f.readlines():
            # 如果想修改分隔符，改这里
            arr = list(map(int, line.split(',')))
            arrOld.append(arr)

def create_image():

    # 为了蛋疼需求，把数组横过来
    # 建立一个横着的数组
    i = 0
    j = 0
    arr = [[0 for i in range(len(arrOld))] for j in range(len(arrOld[0]))]

    # 旋转数组
    for i in range(0, len(arrOld)):
        for j in range(0, len(arrOld[i])):
            arr[j][i] = arrOld[len(arrOld)-1-i][j]
        pass

    # 计算出总共需要多少格子
    colSize = 0
    for y in range(0, len(arr)):
        c = 0
        for x in range(0, len(arr[y])):
            count = getCountNumber(arr[y][x])
            colSize = max(count, colSize)
            c += count
    totolV = colSize * len(arr[0])

    # 创建一个新图
    to_image = Image.new('RGBA', (totolV * IMAGE_SIZE,
                                  len(arr) * IMAGE_SIZE))

    # 遍历方式有改动
    # 先是从Y轴往上便利
    for y in range(0, len(arr)):
        for x in range(0, len(arr[y])):
            number = arr[y][x]
            # 负数取正
            isPosi = number > 0
            if isPosi == False:
                number = -number
            c = 0  # 当前位
            while number != 0:
                num_img = number % 10
                from_image = convert2Image(num_img, c % 2 == 1, isPosi)
                if from_image != None:
                    # 这里坐标计算是试出来的，对就行，没仔细思考
                    to_image.paste(
                        from_image, (((x+1) * colSize - c - 1) * IMAGE_SIZE, y * IMAGE_SIZE))
                number = int(number / 10)
                c += 1

    # 画辅助线

    draw = ImageDraw.Draw(to_image)
    img_w, img_h = to_image.size
    for x in range(1, len(arr[0])):
        xoffset = x * colSize*IMAGE_SIZE
        draw.line((xoffset, 0, xoffset, img_h * IMAGE_SIZE), fill="blue")
    for y in range(1, len(arr)):
        draw.line((0, y*IMAGE_SIZE, img_w, y*IMAGE_SIZE), fill="blue")

    del draw

    # 如果想要图片变大，改这里
    to_image = to_image.resize((int(img_w * IMAGE_RESIZE_SCALE),
                                int(img_h * IMAGE_RESIZE_SCALE)))

    # 直接显示，不保存
    # to_image.show()
    # 生成一个本地文件
    out = time.strftime("%Y-%m-%d_%H-%M-%S", time.localtime()) + '.png'
    to_image.save(IMAGE_SAVE_PATH+out)  # 保存新图

def getCountNumber(num):
    # 计算数字一共几位
    c = 0
    while num != 0:
        num = int(num / 10)
        c += 1
    return c

def convert2Image(num, isEven, isPositive):
    # 读取算筹数字成图片
    # 一位数字，是否是偶数
    if num == 0:
        return None

    # 是否是竖着的数字
    isVert = 'Counting_rod_v'
    if isEven:
        isVert = 'Counting_rod_h'

    # 判断正负数，读取不同图片，这里不做图片颜色处理
    path = IMAGES_PATH + isVert + str(num)
    if isPositive:
        path += 'p'
    else:
        path += 'm'

    image = Image.open(path + '.png')

    # 生成返回图片，用于居中处理
    new_im = Image.new('RGBA', (IMAGE_SIZE, IMAGE_SIZE))
    img_w, img_h = image.size
    offset = (int((IMAGE_SIZE - img_w)/2), int((IMAGE_SIZE - img_h)/2))
    new_im.paste(image, offset)
    return new_im

read_file()
create_image()  # 调用函数