import chardet
import os
import math
from pathlib import Path
import nltk
nltk.download('stopwords')
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from collections import Counter

# Detect the encoding of a file
def find_encoding(fname):
    try:
        with open(fname, 'rb') as f:
            result = chardet.detect(f.read())
            charenc = result['encoding']
            return charenc
    except Exception:
        return 'utf-8'

# Data pre-processing: stopwords removal and removal of any leading numbers from the string
def preprocess(document_text):
    stopwords_set = set(stopwords.words('english'))
    terms = [term.lower() for term in document_text.strip().split()[1:] if term.lower() not in stopwords_set]
    return terms

# Read the input file, preprocess the data, build the vocabulary, and construct the inverted index.
def load_data():
    vocab = Counter()
    documents = []
    inverted_index = {}

    directory = Path('Qdatalc/')
    file_names = [
        directory / 'indexlc.txt',
        directory / 'Qindexlc.txt',
    ]

    data_directory = directory / 'data'
    file_names.extend(data_directory.rglob('*'))

    for filename in file_names:
        my_encoding = find_encoding(filename)

        with open(filename, 'r', encoding=my_encoding) as f:
            lines = f.readlines()

        for index, line in enumerate(lines):
            tokens = preprocess(line)
            documents.append(tokens)
            for token in tokens:
                vocab[token] += 1
                inverted_index.setdefault(token, []).append(index)

    vocab = dict(sorted(vocab.items(), key=lambda item: item[1], reverse=True))
    save_data(vocab, documents, inverted_index)

# Save the vocabulary, IDF values, documents, and inverted index into separate text files
def save_data(vocab, documents, inverted_index):
    output_dir = Path('tf-idf_lc')
    output_dir.mkdir(exist_ok=True)

    # Save the vocab in a text file
    with open(output_dir / 'vocab_lc.txt', 'w', encoding=find_encoding('tf-idf_lc/vocab_lc.txt')) as f:
        f.write('\n'.join(vocab.keys()))

    # Save the idf values in a text file
    with open(output_dir / 'idf-values_lc.txt', 'w', encoding=find_encoding('tf-idf_lc/idf-values_lc.txt')) as f:
        f.write('\n'.join(str(vocab[key]) for key in vocab.keys()))

    # Save the documents in a text file
    with open(output_dir / 'documents_lc.txt', 'w', encoding=find_encoding('tf-idf_lc/documents_lc.txt')) as f:
        f.write('\n'.join(' '.join(document) for document in documents))

    # Save the inverted index in a text file
    with open(output_dir / 'inverted-index_lc.txt', 'w', encoding=find_encoding('tf-idf_lc/inverted-index_lc.txt')) as f:
        for key, value in inverted_index.items():
            f.write(f"{key}\n")
            f.write(' '.join(str(doc_id) for doc_id in value))
            f.write('\n')

# Load the vocabulary and IDF values from the text files
def load_vocab():
    vocab = {}
    with open('tf-idf_lc/vocab_lc.txt', 'r', encoding='utf-8') as f:
        vocab_terms = f.readlines()
    with open('tf-idf_lc/idf-values_lc.txt', 'r', encoding='utf-8') as f:
        idf_values = f.readlines()

    for (term, idf_value) in zip(vocab_terms, idf_values):
        vocab[term.strip()] = int(idf_value.strip())
    return vocab

# Load the preprocessed documents from the text file
def load_documents():
    with open('tf-idf_lc/documents_lc.txt', 'r', encoding='utf-8') as f:
        documents = [document.strip().split() for document in f.readlines()]
    return documents

# Load the inverted index from a text file
def load_inverted_index():
    inverted_index = {}
    with open('tf-idf_lc/inverted-index_lc.txt', 'r', encoding='utf-8') as f:
        inverted_index_terms = f.readlines()

    for row_num in range(0, len(inverted_index_terms), 2):
        term = inverted_index_terms[row_num].strip()
        documents = inverted_index_terms[row_num + 1].strip().split()
        inverted_index[term] = documents
    return inverted_index

# Calculate term frequency
def get_tf_dictionary(term):
    tf_values = {}
    if term in inverted_index:
        for document in inverted_index[term]:
            if document not in tf_values:
                tf_values[document] = 1
            else:
                tf_values[document] += 1

    for document in tf_values:
        tf_values[document] /= len(documents[int(document)])

    return tf_values

# Calculate inverse document frequency
def get_idf_value(term):
    return math.log(len(documents) / vocab_idf_values[term])

# Extract heading part
def fetch_text_by_index(file_path, index):
    if index is None:
        return None  # Return None if line number is None

    with open(file_path, 'r', encoding=find_encoding(file_path)) as file:
        lines = file.readlines()
        line = lines[index]
        parts = line.strip().split('.', 1)
        return parts[1].strip()

# Extract URL of the corresponding heading
def fetch_data_by_line(file_path, line_number):
    with open(file_path, 'r', encoding=find_encoding(file_path)) as file:
        lines = file.readlines()
        return lines[line_number].strip()

# Calculate the relevance scores for the given query terms and retrieve the matching documents
def calculate_sorted_order_of_documents(query_terms):
    potential_documents = {}
    for term in query_terms:
        if term in vocab_idf_values and vocab_idf_values[term] != 0:
            tf_values_by_document = get_tf_dictionary(term)
            idf_value = get_idf_value(term)
            for document in tf_values_by_document:
                if document not in potential_documents:
                    potential_documents[document] = tf_values_by_document[document] * idf_value
                potential_documents[document] += tf_values_by_document[document] * idf_value

    if not potential_documents:
        return []
    else:
        for document in potential_documents:
            potential_documents[document] /= len(query_terms)

        potential_documents = dict(sorted(potential_documents.items(), key=lambda item: item[1], reverse=True))

        sorted_documents = []

        heading = 'Qdatalc/indexlc.txt'
        link = 'Qdatalc/Qindexlc.txt'

        for doc_index in potential_documents:
            index=int(doc_index)-1
            heading_text = fetch_text_by_index(heading, index)
            if heading_text is None:
                continue
            url_text = fetch_data_by_line(link, index)
            sorted_documents.append((doc_index, heading_text, url_text))

        return sorted_documents

# Load the data if not already loaded
def load_and_process_data():
    if not os.path.exists('tf-idf_lc/vocab_lc.txt') or not os.path.exists('tf-idf_lc/idf-values_lc.txt') or not os.path.exists(
            'tf-idf_lc/documents_lc.txt') or not os.path.exists('tf-idf_lc/inverted-index_lc.txt'):
        load_data()
    vocab_idf_values = load_vocab()
    documents = load_documents()
    inverted_index = load_inverted_index()
    return vocab_idf_values, documents, inverted_index

vocab_idf_values, documents, inverted_index = load_and_process_data()


import pandas as pd
import connect
from etl_sql_queries import sharesale_insert
from create_tables import drop_tables, create_tables

def process_data(cur, conn, filepath):
    df = pd.read_csv(filepath)
    print('Read df')
    df['ts'] = df.date.astype(str).str.cat(df.time.astype(str), sep=' ')
    print('converted times')

    share_data = df[['ts', 'price', 'size']].values
    print('processed share_data')
    i = 0
    for s in share_data:
        cur.execute(sharesale_insert, s)
        if i % 10000 == 0:
            print(i)
            conn.commit()
            break
            # print('  Inserting:', s)
        i += 1

    conn.commit()
    print('  -- Finished!')

def show_first_rows(cur, conn, table, n):
    cur.execute('SELECT * FROM {} LIMIT {}'.format(table, n))
    conn.commit()
    for row in cur.fetchall():
        print(row, row[1].timestamp())

if __name__ == '__main__':
    try:
        input('This will drop all tables, press Enter to continue.')
        cur, conn = connect.connect(autocommit=False)
        drop_tables(cur, conn)
        create_tables(cur, conn)
        process_data(cur, conn, './data/ticks.csv')
        show_first_rows(cur, conn, 'sharesales', 12)
    finally:
        conn.close()


'''Running a Voxel-wise linear model with multiple volumetric variables'''
from pyVS.pyVoxelStats.pyVoxelStatsLM import pyVoxelStatsLM

model_string = 'A_minc ~ e_val + C_d_minc + C(f)'
csv_file = 'VoxelStatsTestData/CSV/Data_MT.csv'
mask_file = 'VoxelStatsTestData/Masks/cerebellum_full_mask2.mnc'
voxel_variables = ['A_minc', 'C_d_minc']
subset_string = 'b_val > 1'
multi_variable_operations = ['A_minc*(-1)']
file_type = 'minc'

lm = pyVoxelStatsLM(file_type, model_string, csv_file, mask_file, voxel_variables, subset_string,
                    multi_variable_operations)
lm.set_no_parallel(False)
lm.enable_save_model()
lm.set_no_parallel(True)
#lm.set_up_cluster(clus_json='/home/sulantha/ipyparallel_json/ipcontroller-client.json', no_start=True)
lm.evaluate()
#lm.set_up_cluster(workers=4)
results = lm.res

## Pickle analysis if needed to write results later
import pickle
## Saving
pickle.dump(lm, 'VoxelStatsTestData/Pickles/LM.pkl')
## Loading Back
lm2 = pickle.load('VoxelStatsTestData/Pickles/LM.pkl')


from __future__ import annotations
from urllib.parse import urlencode

__all__ = ["ApiQuery", "create_boostest_url"]

class ApiQuery:
    """
    Query for the UK Coronavirus Data Dashboard API, expressed as a URL string through
    the :attr:`url_string` property.
    """
    url_prefix: str = "https://api.coronavirus.data.gov.uk/v2/data"
    query_keys: list[str] = "areaType metric format".split()

    def __init__(
        self,
        metric_keys: list[str],
        area_type: str = "overview",
        data_format: str = "csv",
    ):
        if 1 > len(metric_keys):
            raise ValueError("Minimum of 1 metric required for an API call")
        elif len(metric_keys) > 5:
            raise ValueError("Maximum of 5 metrics permitted for an API call")
        self.areaType: str = area_type
        self.metric: list[str] = metric_keys
        self.format: str = data_format

    @property
    def query_string(self):
        """
        Generate list of 2-tuples with duplicate keys (multiple metrics) as
        ``[("areaType", "overview"), ("metric", "..."), ("metric", "..."), ...]``
        then encode as a parameterised query string ("?areaType=overview&metric=...")
        """
        query: list[tuple[str, str]] = [
            (k, val)
            for k, v in [(key, getattr(self, key)) for key in self.query_keys]
            for val in (v if isinstance(v, list) else [v])
        ]
        return f"?{urlencode(query)}"

    @property
    def url_string(self):
        return f"{self.url_prefix}{self.query_string}"

def create_boostest_url():
    boostest_metrics = [
        "cumVaccinationFirstDoseUptakeByPublishDatePercentage",
        "cumVaccinationSecondDoseUptakeByPublishDatePercentage",
        "cumVaccinationThirdInjectionUptakeByPublishDatePercentage",
        "newPCRTestsByPublishDate",
        "newLFDTestsBySpecimenDate",
    ]
    api_url = ApiQuery(metric_keys=boostest_metrics).url_string
    return api_url


#!/usr/bin/python3
""" 0. Change comes from within """

def makeChange(coins, total):
    """Return a list of coins to make the total.

    Args:
        coins (list): A list of available coins.
        total (int): The total amount to make change for.

    Returns:
        int: the fewest number of coins needed to meet total.
    """
    # change count and current coin index
    change = 0
    index = 0
    # sort coins in descending order
    coins.sort(reverse=True)
    while total > 0:
        if index >= len(coins):
            return -1
        if coins[index] <= total:
            total -= coins[index]
            change += 1
        else:
            index += 1
    return change


from os import environ
from  random import randint
from  time import sleep
import logging as logger
from flask import Flask, jsonify
from flask_cors import CORS
from collections import defaultdict
import json

app = Flask(__name__)
CORS(app)
logger.debug("App starting")

@app.route("/")
def hello():
    return "
Congratulations!
 Your server is running."

@app.route('/get_raw', methods=['GET'])
def get_data():
    logger.debug("GET method returning data")
    with open("data/covariates.json") as json_file:
        raw = json.load(json_file)
        #if randint(0,99) == 1:
        #    sleep(200000)
        return jsonify(raw), 200

@app.route('/get_counts', methods=['GET'])
def get_count():
    logger.debug("GET method returning data")
    with open("data/covariates.json") as json_file:
        raw = json.load(json_file)
        drugs = raw['Drug']
        res = defaultdict(list)
        for key, val in sorted(drugs.items()):
            res[val].append(key)
        raw = list()
        for key, val in res.items():
            drug_count = dict()
            drug_count["Drug"] = key
            drug_count['Count'] = len(val)
            raw.append(drug_count)
        if randint(0,99) == 1:
            sleep(200000)
        return jsonify(raw), 200

if __name__ == "__main__":
    app.run(host="0.0.0.0",
            port=environ['TX_SERVER_PORT'])


import config
import tweepy
from janome.tokenizer import Tokenizer
from wordcloud import WordCloud

font_pass = "/System/Library/Fonts/ヒラギノ角ゴシック W1.ttc"

def api():
    auth = tweepy.OAuth1UserHandler(
        config.api_key,
        config.api_secret,
        config.consumer_key,
        config.consumer_secret
    )
    return tweepy.API(auth, wait_on_rate_limit=True, timeout=120)

class TLDesigner:
    def __init__(self):
        self.tweet_id = None
        self.text = ''
        self.tweet_timelines = []
        self.API = api()

    def find_tweet(self):
        f = open("tweet_id.txt", "r")
        self.tweet_id = f.readline()

    def get_tweet(self):
        tweet_timelines = self.API.home_timeline(since_id=self.tweet_id, )
        for tt in tweet_timelines:
            if "https" not in tt.text and "RT" not in tt.text and "@" not in tt.text:
                self.tweet_timelines.append(tt.text)

    def filter_tweet(self):
        t = Tokenizer()
        for tt in self.tweet_timelines:
            tts = [token.surface for token in t.tokenize(tt) if token.part_of_speech.startswith('名詞')]
            for ts in tts:
                if len(ts) != 1:
                    self.text += " " + ts
            print(self.text)

    def generation_image(self, txt):
        wc = WordCloud(font_path=font_pass).generate(txt)
        wc.to_file("word_cloud_result.png")
        self.API.update_status_with_media(status='image tweet from python', filename='word_cloud_result.png')

    def store_tweet(self):
        ts = self.API.home_timeline(count=1)
        tweet_id = ts[0].id
        f = open("tweet_id.txt", "w")
        f.write(str(tweet_id))
        f.close()

    def main(self):
        self.find_tweet()
        self.get_tweet()
        self.filter_tweet()
        self.generation_image(self.text)
        self.store_tweet()

test = TLDesigner()
test.main()


class yazılımcı():
    def __init__(self,isim,soyisim,numara,maaş,diller):
        self.isim = isim
        self.soyisim = soyisim
        self.numara = numara
        self.maaş = maaş
        self.diller = diller

    def bilgileriGöster(self):
        print("""
        Yazılımcının özellikleri
        İsim: {}
        Soyisim: {}
        Numara: {}
        Maaş: {}
        Diller: {}
        """.format(self.isim,self.soyisim,self.numara,self.maaş,self.diller))

    def zamYap(self,zamMiktarı):
        print("Zam yapılıyor...")
        self.maaş += zamMiktarı

    def dilEkle(self,yeniDil):
        print("Dil ekleniyor...")
        self.diller.append(yeniDil)

yazılımcı1 = yazılımcı("Mustafa","Coşkun",12345,3000,["Python","Java","C"])
yazılımcı2 = yazılımcı("Tayfun","Şahin",6789,3800,["Matlab","C++"])
yazılımcı1.bilgileriGöster()
yazılımcı2.bilgileriGöster()

yazılımcı1.dilEkle("Golang")
yazılımcı1.bilgileriGöster()
yazılımcı2.zamYap(500)
yazılımcı2.bilgileriGöster()

58.Nesne Tabanlı Programlama - Metodlar.py


import sys
import argparse
import re
import pymongo
import colorsys

from PIL import Image
from numpy import array, random, mean

import utils

def main():
    parser = argparse.ArgumentParser(description = 'Calculating HSV values from images and appending into file.')
    parser.add_argument('-f', help = 'The CSV summary file.')
    parser.add_argument('-e', help = 'The flag to keep common header of files.')
    parser.add_argument('-t', help = 'The temporary path for storing downloaded image.')
    parser.add_argument('-o', help = 'The output file.')

    args = parser.parse_args()
    output = read_data(args)

    if (args.o != None):
        utils.write_file(output, args.o)

def calculat_hsv_model(part_of_data, image_temp_dir):

    content = []
    for line in part_of_data:
        line_fields = line.rsplit(',')
        if (len(line_fields) == 1):
            line_fields = line.rsplit("\t")

        image_url = line_fields[0]
        utils.crawl_image_from_url(image_url, image_temp_dir + '/temp.jpg')
        im = Image.open(image_temp_dir + '/temp.jpg')
        pix = im.load()

        hue = []
        satuation = []
        value = []
        (image_width, image_height) = im.size
        for index_x in range(image_width):
            for index_y in range(image_height):

                pixel = pix[index_x, index_y]

                (red, green, blue, alpha) = (0, 0, 0, 0)

                if len(pixel) == 3:
                    (red, green, blue) = pixel
                elif len(pixel) == 4:
                    (red, green, blue, alpha) = pixel

                red /= float(255)
                green /= float(255)
                blue /= float(255)

                (h, s, v) = colorsys.rgb_to_hsv(red, green, blue)
                hue.append(h)
                satuation.append(s)
                value.append(v)
                #print(str(red) + " " + str(green) + " " + str(blue))
                #print(str(h) + " " + str(s) + " " + str(v))

        mean_hsu = mean(hue)
        mean_satuation = mean(satuation)
        mean_value = mean(value)

        line_fields.append(str(mean_hsu))
        line_fields.append(str(mean_satuation))
        line_fields.append(str(mean_value))

        content.append(",".join(line_fields))

    return content

def read_data(args):

    data_sources = []
    header = ''
    if (args.f != None):

        parts = []
        afile = args.f

        part_of_data = utils.load_file(afile)

        if args.e != None and args.e == 'y':
            if header == '':
                header = part_of_data[0]
            part_of_data = part_of_data[1:len(part_of_data)]

        part_of_data = calculat_hsv_model(part_of_data, args.t)

        parts.append(part_of_data)

        parts = [item for sublist in parts for item in sublist]
        data_sources = array(parts)

    output = []
    if header != '':
        output.append(header)
    for item in data_sources:
        output.append(item)

    return output

if __name__ == "__main__":
    main()


import sys

input = sys.stdin.readline

n = int(input())
switches = list(map(int, input().split()))

m = int(input())
for _ in range(m):
    sex, num = map(int, input().split())
    if sex==1:  # 남자일 때
        i = num-1
        while i < n:
            switches[i] = 0 if switches[i] else 1
            i+=num
    else:  # 여자일 때
        l = r = num-1
        while 0 <= l and r < n:
            if switches[l] == switches[r]:
                switches[l] = switches[r] = 0 if switches[l] else 1
                l, r = l-1, r+1
            else:
                break
ans = ""
for i in range(1,n+1):
    ans += f"{switches[i-1]} "
    if i%20==0:
        ans += "\n"
print(ans)


# from __future__ import absolute_import
# from __future__ import print_function
import os
import sys
import gym
import subprocess
from gym import spaces, logger
import numpy as np
import time

# we need to import python modules from the $SUMO_HOME/tools directory
if 'SUMO_HOME' in os.environ:
    tools = os.path.join(os.environ['SUMO_HOME'], 'tools')
    sys.path.append(tools)
else:
    sys.exit("please declare environment variable 'SUMO_HOME'")
from sumolib import checkBinary
# import traci

controlled_lights = [{'name': 'gneJ14', 'curr_phase': 0, 'num_phases': 6}]
uncontrolled_lights = [{'name': 'nw', 'curr_phase': 0, 'num_phases': 4},
                       {'name': 'se', 'curr_phase': 0, 'num_phases': 4},
                       {'name': 'sw', 'curr_phase': 0, 'num_phases': 4}]
important_roads = ['gneE16', 'gneE59', 'gneE13']
load_options = ["-c", "PoundSign/PoundSign.sumocfg", "-t", "--remote-port", "1337"]

class SumoEnv(gym.Env):
    def __init__(self, steps_per_episode, render):
        super(SumoEnv, self).__init__()
        # self.scenario_name = scenario_name
        self.steps_per_episode = steps_per_episode
        self.is_done = False
        self.current_step = 0

        self.reward_range = (-float('inf'), float('inf'))  # HARDCODE
        self.action_space = spaces.Discrete(5)  # HARDCODE
        self.observation_space = spaces.Box(low=0, high=float('inf'), shape=np.array([6]), dtype=np.float32)  # HARDCODE

        # Start sumo simulation
        self.noguiBinary = checkBinary('sumo')
        self.guiBinary = checkBinary('sumo-gui')
        self.current_binary = self.guiBinary if render else self.noguiBinary
        self.sumobin = subprocess.Popen([self.current_binary] + load_options)
        time.sleep(1)  # Make sure that the binary opens before moving on TODO: Find a better way to wait for the binary to open

        # TODO: Start traci client
        self.sumo = subprocess.Popen(['./sim_interface'], stdin=subprocess.PIPE, text=True, universal_newlines=True, stdout=subprocess.PIPE) # , stdout=subprocess.PIPE
        self._send_command('connect')
        assert(self.sumo.stdout.readline().strip() == '1')
        print('Connected')

    def reset(self):
        print('reset')
        # self._send_command('load')
        self.current_step = 0
        self.is_done = False

        for i in range(10):
            print('hi')
            self._send_command('step')
            print(self.sumo.stdout.readline().strip())
            time.sleep(1)

        return self._next_observation()

    def _send_command(self, command):
        self.sumo.stdin.write(command + '\n')
        self.sumo.stdin.flush()

    def _next_observation(self):
        obs = []
        # TODO: Get Data
        # wait_counts, road_counts = self._get_road_waiting_vehicle_count()
        # HARDCODE
        # for lane in important_roads:
        #     obs.append(road_counts[lane])
        #     obs.append(wait_counts[lane])

        return np.array(obs)

    def step(self, action):

        self._take_action(action)

        self._send_command('step')

        self.current_step += 1

        obs = self._next_observation()
        reward = self._get_reward()

        if self.is_done:
            logger.warn("You are calling 'step()' even though this environment has already returned done = True. "
                        "You should always call 'reset()' once you receive 'done = True' "
                        "-- any further steps are undefined behavior.")
            reward = 0.0

        if self.current_step + 1 == self.steps_per_episode:
            self.is_done = True

        return obs, reward, self.is_done, {}

    def _get_reward(self):
        # TODO: Get data
        # road_waiting_vehicles_dict, _ = self._get_road_waiting_vehicle_count()
        reward = 0.0

        # for (road_id, num_vehicles) in road_waiting_vehicles_dict.items():
        #     if road_id in important_roads:
        #         reward -= num_vehicles

        return reward

    def _take_action(self, action):
        if action != controlled_lights[0]['curr_phase']:
            controlled_lights[0]['curr_phase'] = action
            # TODO: Change tl phase in sumo
            # self._set_tl_phase(controlled_lights[0]['name'], action)

    # def _get_road_waiting_vehicle_count():
    #     wait_counts = {'gneE16': 0, 'gneE59': 0, 'gneE13': 0}
    #     road_counts = {'gneE16': 0, 'gneE59': 0, 'gneE13': 0}
    #     vehicles = traci.vehicle.getIDList()
    #     for v in vehicles:
    #         road = traci.vehicle.getRoadID(v)
    #         if road in wait_counts.keys():
    #             if traci.vehicle.getWaitingTime(v) > 0:
    #                 wait_counts[road] += 1
    #             road_counts[road] += 1
    #     return wait_counts, road_counts

    # def _set_tl_phase(intersection_id, phase_id):
    #     traci.trafficlight.setPhase(intersection_id, phase_id)

    def render(self, mode='human'):
        self.current_binary = self.guiBinary


# -*- coding: utf-8 -*-
"""
Created on Wed Apr  6 14:38:54 2016

@author: Coline, Régis, Arnaud
"""
#import os
#os.chdir('C:\\Users\\Ariane\\Projet_info')
import random as rd
import matplotlib
matplotlib.use('Qt4Agg')
import matplotlib.pyplot as plt
import time
from humain import Sylviculteur, Bucheron
from terrain import Terrain
from eau import Eau
from foret import Foret
from foret_morte import Foret_morte
from foret_malade import Foret_malade
from plaine import Plaine
import numpy as np
import math

def generation_hum(nb_humains,type_classe,taille_x,taille_y,field):
    """
    method which create a list containing the human objects. They are randomly distributed on the map, except on the water.
    :param taille_x/y: integer which referts to the size of the map
    """
    humains=[]
    for i in range(nb_humains):
        x=rd.randint(0,taille_x-1)
        y=rd.randint(0,taille_y-1)
        while field[x][y].nature==0:
            x=rd.randint(0,taille_x-1)
            y=rd.randint(0,taille_y-1)
        humains=humains+[type_classe(x,y)]
    return(humains)

def tour(field,sylviculteurs,bucherons):
    """
    Method which runs a round, like a simulation with probabilities calculated in each method "maj" belonging to every class.
    It updates the matrice which contains the objects (plaine ,eau , foret etc...)

    """
    #on met à jour tous les sylviculteurs
    for sylviculteur in sylviculteurs:
        sylviculteur.maj(field,4)
      #on met à jour tous les bucherons
    for bucheron in bucherons:
        bucheron.maj(field, 3)

    #on met à jour tous les objets du terrains
    for x in range(field.shape[0]):
        for y in range(field.shape[1]):
            field[x,y].maj(field)

    #La valeur des attributs nature_suivant détermine quelle classe s'applique à la case.
    for x in range(field.shape[0]):
        for y in range(field.shape[1]):
            if field[x,y].nature!=field[x,y].nature_suivant:
                if field[x,y].nature_suivant==1:
                    field[x,y]=Plaine(x,y,field[x,y].presence)
                if field[x,y].nature_suivant==2 and field[x,y].nature==1:
                    field[x,y]=Foret(x,y,0,False)
                if field[x,y].nature_suivant==2 and field[x,y].nature==4:
                    field[x,y]=Foret(x,y,field[x,y].taille,field[x,y].presence)
                if field[x,y].nature_suivant==4:
                    field[x,y]=Foret_malade(x,y,field[x,y].taille,field[x,y].presence)
                if field[x,y].nature_suivant==3:
                    field[x,y]=Foret_morte(x,y,field[x,y].taille,field[x,y].presence)
    #Nature_suivant reprend la valeur 9, qui sert de valeur tampon
    for x in range(field.shape[0]):
        for y in range(field.shape[1]):
            if field[x,y].nature_suivant!=9:
                field[x,y].nature=field[x,y].nature_suivant
                field[x,y].nature_suivant=9

def ihm(field,sylviculteurs,bucherons,tours):
    """
    return a graphic showing the shape of the simulation
    """
    ihm=np.zeros(field.shape)
    for x in range(field.shape[0]):
        for y in range(field.shape[1]):
            ihm[x,y]=field[x,y].couleur
    fig=plt.figure()
    plt.pcolormesh(ihm,cmap='brg')
    for sylviculteur in sylviculteurs:
        plt.plot(sylviculteur.y,sylviculteur.x,'o')
    for bucheron in bucherons:
        plt.plot(bucheron.y,bucheron.x,'*')
    return(fig)

def generation_terrain(taille_x,taille_y,nb_sylviculteurs,nb_bucherons):
    """
    Method which create a matrice with field objects (eau, plaine, etc...)
    and uses two lists created by the method generation_hum (sylviculteur and bucheron).
    To create a coherent field, it uses noise.
    The shapes created are smoothed by bilinear interpolation,
    then each case is discretised to make them correspond at each object
    """
    print('starting Map creation')
    start_gen=time.time()
    imgx = taille_x
    imgy = taille_y
    octaves = int(math.log(max(imgx, imgy), 2.0))
    persistence =0.8
    imgAr = [[0.0 for i in range(imgx)] for j in range(imgy)] # matrice intermédiaire
    totAmp = 0.0
    for k in range(octaves):
        freq = 2**k
        amp = persistence ** k
        totAmp += amp
        # création d'une matrice de taille n*m (*amplitude)
        n = freq + 1; m = freq + 1 # taille de la matrice
        ar = [[rd.random() * amp for i in range(n)] for j in range(m)]
        nx = imgx / (n - 1.0); ny = imgy / (m - 1.0)
        for ky in range(imgy):
            for kx in range(imgx):
                #création du bruit de valeur
                #avec une interpolation bilinéaire
                i = int(kx / nx); j = int(ky / ny)
                dx0 = kx - i * nx; dx1 = nx - dx0
                dy0 = ky - j * ny; dy1 = ny - dy0
                z = ar[j][i] * dx1 * dy1
                z += ar[j][i + 1] * dx0 * dy1
                z += ar[j + 1][i] * dx1 * dy0
                z += ar[j + 1][i + 1] * dx0 * dy0
                z /= nx * ny
                imgAr[ky][kx] += z # Rassemblement dans la matrice intermédiaire

    # on cherche les propriétés statistiques de la matrice
    somme=0
    ecart_carre=0
    for y in range(imgy):
        for x in range(imgx):
            somme=somme+imgAr[y][x]

    moyenne=somme/(imgx*imgy)

    for y in range(imgy):
        for x in range(imgx):
            ecart_carre=ecart_carre+(imgAr[y][x]-moyenne)**2

    ecart_type=np.sqrt(ecart_carre/(imgx*imgy))

    # on regroupe toutes les valeurs de la matrice pour n'avoir plus que 3 valeurs
    for y in range(imgy):
        for x in range(imgx):
            if imgAr[y][x]<moyenne-ecart_type:
                imgAr[y][x]=0
            if imgAr[y][x]>moyenne+ecart_type:
                imgAr[y][x]=1.6
            if imgAr[y][x]>=moyenne-ecart_type and imgAr[y][x]<=moyenne+ecart_type:
                imgAr[y][x]=2
    # Création matrice objet ( elle sera modifié par la suite)
    mat_obj=np.array([[Plaine(x,y,False) for x in range(imgx)]for y in range(imgy)])
    #Les 3 valeurs de la matrice intermédiaire sont associés aux 3 classes terrains de départ (Eau, Plaine, Forêt)
    for y in range(imgy):
        for x in range(imgx):
            if imgAr[y][x]==0:
                mat_obj[x][y]=Eau(x,y,False)
            if imgAr[y][x]==1.6:
                mat_obj[x][y]=Foret(x,y,0,False)
            if imgAr[y][x]==2:
                mat_obj[x][y]=Plaine(x,y,False)

    sylviculteurs=generation_hum(nb_sylviculteurs,Sylviculteur,taille_x,taille_y,mat_obj)
    bucherons=generation_hum(nb_bucherons,Bucheron,taille_x,taille_y,mat_obj)

    end_gen=time.time()
    print('Map created in ' +str(end_gen-start_gen)+'seconds')

    return(mat_obj,sylviculteurs,bucherons)

#
#def film(Map,nb_tours):
#    """
#    Méthode permettant de générer un film
#    """
#    fig=plt.figure()
#    plt.xlim(0,10)
#    plt.ylim(0,10)
#    FFMpegWriter = manimation.writers['ffmpeg']
#    metadata = dict(title='Movie Test', artist='Matplotlib',
#                    comment='Movie support!')
#    writer = FFMpegWriter(fps=15, metadata=metadata)
#    with writer.saving(fig, "wild.mp4", 100):
#        for i in range(nb_tours):
#            tour(Map, sylviculteurs, bucherons, nature_case)
#            ihm=np.zeros(Map.shape)
#            for x in range(Map.shape[0]):
#                for y in range(Map.shape[1]):
#                    ihm[x,y]=Map[x,y].nature
#            plt.pcolor(ihm, cmap='brg')
#            writer.grab_frame()
#
#def dessin(field,nb_tours):
#    """
#    Renvoie l'état de la simulation après nb_tours
#    """
#    tic=time.time()
#    ihm(field,sylviculteurs,bucherons,tour)
#    for i in range(nb_tours):
#        print('start Round '+str(i))
#        start_tour=time.time()
#        tour(field, sylviculteurs, bucherons)
#        end_tour=time.time()
#        print('End Round ' +str(i))
#        print('Round done in '+str(end_tour-start_tour)+' seconds')
#        if i>nb_tours-10:
#            ihm(field,sylviculteurs, bucherons, i)
#    ihm(field,sylviculteurs, bucherons, nb_tours)
#    toc=time.time()
#    print('la simulation a mis ' +str(toc-tic)+ ' secondes')

#Generation=generation_terrain(100,100,10,10)
#field=Generation[0]
#sylviculteurs=Generation[1]
#bucherons=Generation[2]
#dessin(field,10)


from setuptools import setup, find_packages
import os

tests_require = ['zope.testing', 'Products.PloneTestCase']

version = '0.2.0'

setup(name='collective.powertoken.workflow',
      version=version,
      description="Workflow add-ons for collective.powertoken support for Plone",
      long_description=open("README.txt").read() + "\n" +
                       open(os.path.join("docs", "HISTORY.txt")).read(),
      # Get more strings from
      # http://pypi.python.org/pypi?:action=list_classifiers
      classifiers=[
        "Framework :: Plone",
        "Framework :: Plone :: 3.3",
        "Framework :: Plone :: 4.0",
        "Framework :: Plone :: 4.1",
        "Framework :: Zope2",
        "Framework :: Zope3",
        "Development Status :: 4 - Beta",
        "Intended Audience :: Developers",
        "Programming Language :: Python",
        ],
      keywords='plone security token plonegov workflow',
      author='RedTurtle Technology',
      author_email='sviluppoplone@redturtle.it',
      url='http://plone.org/products/collective.powertoken.core',
      license='GPL',
      packages=find_packages(exclude=['ez_setup']),
      namespace_packages=['collective', 'collective.powertoken'],
      include_package_data=True,
      zip_safe=False,
      install_requires=[
          'setuptools',
          'collective.powertoken.core>=0.3.0',
      ],
      tests_require=tests_require,
      extras_require=dict(tests=tests_require),
      entry_points="""
      [z3c.autoinclude.plugin]
      target = plone
      """,
      )


import pygame
pygame.init()

# Constants
BG_COLOR = 'black'
FONT_COLOR = 'white'
FONT_SIZE = 20
TOPLEFT = (10,30)
FONT = None  # None for pygame default font
ANTIALIAS = True

# This method converts list of strings to a multi-line overlay,
# with rect at position topleft = (x,y) = TOPLEFT
# Must input as a list of strings even if single line

def debug(multistring_list, x=TOPLEFT[0], y=TOPLEFT[1], font_size=FONT_SIZE):
    f = pygame.font.Font(FONT, font_size)
    display_surf = pygame.display.get_surface()
    line_height = pygame.font.Font.get_linesize(f)

    for index, line in enumerate(multistring_list):
        debug_surf = f.render(str(line), ANTIALIAS, FONT_COLOR)
        position = (x, y + index*line_height)
        debug_rect = debug_surf.get_rect(topleft=position)
        pygame.draw.rect(display_surf, BG_COLOR, debug_rect)
        display_surf.blit(debug_surf, debug_rect)


mymap = [[0]*8 for i in range(8)]
visited = [0]*8
que = [0]*8
parent = [-1]*8
Distance = [0]*8

def BFS(here):
    global front, rear
    rear += 1
    que[rear] = here
    visited[here] = True

    while front != rear:
        front += 1
        here = que[front]
        print(here)  # 경로 출력

        for next in range(8):
            if mymap[here][next] and not visited[next]:
                visited[next] = True
                Distance[next] = Distance[here]+1
                parent[next] = here
                rear += 1
                que[rear] = next

distance[1]=0
BFS(1)


import sys
from collections import deque
from itertools import permutations

dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]

def bfs(start, end):
    targetX, targetY = end
    visited = [[0 for _ in range(W)] for _ in range(H)]

    q = deque()
    q.append((start, 0))

    while q:
        (x, y), depth = q.popleft()
        for i in range(4):
            nx = x + dx[i]
            ny = y + dy[i]
            if nx < 0 or nx >= H or ny < 0 or ny >= W:
                continue
            if room[nx][ny] == "x":
                continue
            if visited[nx][ny] == 1:
                continue
            if nx == targetX and ny == targetY:
                return depth + 1
            visited[nx][ny] = 1
            q.append(((nx,ny), depth+1))

    return -1

while True:
    W, H = map(int, sys.stdin.readline().split())

    if W == 0 and H == 0:
        break

    room = [list(input()) for _ in range(H)]
    dirties = []

    for i in range(H):
        for j in range(W):
            if room[i][j] == 'o':
                robot = (i,j)
            if room[i][j] == '*':
                dirties.append((i,j))

    numVertex = len(dirties) + 1
    adjArr = [[0 for _ in range(numVertex)] for _ in range(numVertex)]
    flag = False

    for i in range(len(dirties)):
        for j in range(i+1, len(dirties) + 1):
            if i == 0:
                dist = bfs(robot, dirties[j-1])
            else:
                dist = bfs(dirties[i-1], dirties[j-1])

            if dist == -1:
                flag = True
                break

            adjArr[i][j] = dist
            adjArr[j][i] = dist

    if flag:
        print(-1)
        break

    comb = list(permutations([i+1 for i in range(len(dirties))], len(dirties)))

    answer = float('inf')
    for i in range(len(comb)):
        path = comb[i]
        sum = 0
        start = 0
        for j in range(len(path)):
            sum += adjArr[start][path[j]]
            start = path[j]
        answer = min(answer, sum)
    print(answer)


# coding=utf-8
from collections import defaultdict
import json
import redis
import logging

from sqlalchemy import text

from dart.context.database import db_replica as db
from dart.context.locator import injectable
from dart.model.action import Action, ActionState
from dart.model.dataset import Dataset
from dart.model.datastore import Datastore
from dart.model.event import Event
from dart.model.graph import GraphEntity, Graph, Node, Edge, Relationship, GraphEntityIdentifier, SubGraph, EntityType, \
    SubGraphDefinition
from dart.model.orm import SubGraphDefinitionDao
from dart.model.subscription import Subscription
from dart.model.trigger import Trigger
from dart.model.workflow import Workflow
from dart.service.graph.sql_recursive import RECURSIVE_SQL
from dart.service.graph.sql_misc import ENTITY_IDENTIFIER_SQL, DATASTORE_ONE_OFFS_SQL, WORKFLOW_INSTANCE_SQL
from dart.service.graph.sub_graph import get_static_subgraphs_by_engine_name, \
    get_static_subgraphs_by_engine_name_all_engines_related_none
from dart.util.rand import random_id
from operator import is_not
from functools import partial
import ast

_logger = logging.getLogger(__name__)

@injectable
class GraphEntityService(object):
    def __init__(self, engine_service, datastore_service, action_service, dart_config):
        self._engine_service = engine_service
        self._datastore_service = datastore_service
        self._action_service = action_service

        self._dart_config = dart_config

        self.redis_expiration_ttl = 0
        self.redis_client = None
        if ('redis' in dart_config):
            redis_host = dart_config['redis']['host']
            redis_port = int(dart_config['redis']['port'])
            self.redis_expiration_ttl = int(dart_config['redis']['expire_seconds'])

            try:
                self.redis_client = None #redis.StrictRedis(host=redis_host, port=redis_port, db=0)
            except Exception as err:
                _logger.error("Redis: failed to create a redis client. err={0}".format(err))

    def get_sub_graphs(self, related_type, related_engine_name):
        engine = None
        if related_engine_name:
            engine = self._engine_service.get_engine_by_name(related_engine_name)

        # merging (dictionary of lists)'s is easier with defaultdict
        sub_graphs_by_engine_name = defaultdict(list)

        # static subgraphs for the specified engine (or no engine)
        static_sub_graphs_by_engine_name = get_static_subgraphs_by_engine_name(engine, related_type, self)
        sub_graphs_by_engine_name.update(static_sub_graphs_by_engine_name)

        # engine provided subgraphs from the database
        db_sub_graphs = self._get_db_subgraphs_by_engine_name(related_engine_name, related_type)
        for engine_name, sub_graphs in db_sub_graphs.iteritems():
            sub_graphs_by_engine_name[engine_name].extend(sub_graphs)

        # static sub_graphs that are related to None
        if related_type is None:
            engine_names = self._engine_service.all_engine_names()
            unrelated_sub_graphs = get_static_subgraphs_by_engine_name_all_engines_related_none(engine_names, self)
            for engine_name, sub_graphs in unrelated_sub_graphs.iteritems():
                sub_graphs_by_engine_name[engine_name].extend(sub_graphs)

        return sub_graphs_by_engine_name

    def _get_db_subgraphs_by_engine_name(self, related_engine_name, related_type):
        query = SubGraphDefinitionDao.query
        if related_engine_name:
            query = query.filter(SubGraphDefinitionDao.data['engine_name'].astext == related_engine_name)
        if related_type:
            query = query.filter(SubGraphDefinitionDao.data['related_type'].astext == related_type)
        else:
            query = query.filter(SubGraphDefinitionDao.data['related_type'] == 'null')

        sub_graph_map = {}
        for e in query.all():
            sg_def = e.to_model()
            assert isinstance(sg_def, SubGraphDefinition)
            entity_models = self.to_entity_models_with_randomized_ids(
                sg_def.data.actions +
                sg_def.data.datasets +
                sg_def.data.datastores +
                sg_def.data.events +
                sg_def.data.subscriptions +
                sg_def.data.triggers +
                sg_def.data.workflows
            )
            engine_name = sg_def.data.engine_name
            entity_type = sg_def.data.related_type
            sub_graph = SubGraph(
                name=sg_def.data.name,
                description=sg_def.data.description,
                related_type=entity_type,
                related_is_a=sg_def.data.related_is_a,
                graph=self.to_graph(engine_name, entity_models),
                entity_map=self.to_entity_map(entity_models),
                icon=sg_def.data.icon,
                md_icon=sg_def.data.md_icon,
            )
            if engine_name not in sub_graph_map:
                sub_graph_map[engine_name] = []
            sub_graph_map[engine_name].append(sub_graph)

        return sub_graph_map

    def to_graph(self, engine_name, entity_models):
        visited_nodes = set()
        visited_edges = set()
        nodes = []
        edges = []

        for entity in entity_models:
            if isinstance(entity, Dataset):
                self._add_node(nodes, visited_nodes, 'dataset', entity.id, entity.data.name, None, None)

            if isinstance(entity, Action):
                self._add_node(nodes, visited_nodes, 'action', entity.id, entity.data.name, entity.data.state, entity.data.action_type_name)
                if entity.data.args and 'dataset_id' in entity.data.args:
                    self._add_edge(edges, visited_edges, 'dataset', entity.data.args['dataset_id'], 'action', entity.id)
                if entity.data.args and 'subscription_id' in entity.data.args:
                    self._add_edge(edges, visited_edges, 'subscription', entity.data.args['subscription_id'], 'action', entity.id)
                if entity.data.workflow_id:
                    self._add_edge(edges, visited_edges, 'workflow', entity.data.workflow_id, 'action', entity.id)
                if entity.data.datastore_id:
                    self._add_edge(edges, visited_edges, 'datastore', entity.data.datastore_id, 'action', entity.id)

            if isinstance(entity, Workflow):
                self._add_node(nodes, visited_nodes, 'workflow', entity.id, entity.data.name, entity.data.state, None)
                if entity.data.datastore_id:
                    self._add_edge(edges, visited_edges, 'datastore', entity.data.datastore_id, 'workflow', entity.id)

            if isinstance(entity, Trigger):
                self._add_node(nodes, visited_nodes, 'trigger', entity.id, entity.data.name, entity.data.state, entity.data.trigger_type_name)
                if entity.data.workflow_ids:
                    for wf_id in entity.data.workflow_ids:
                        self._add_edge(edges, visited_edges, 'trigger', entity.id, 'workflow', wf_id)
                if entity.data.args and 'completed_trigger_ids' in entity.data.args:
                    for t_id in entity.data.args['completed_trigger_ids']:
                        self._add_edge(edges, visited_edges, 'trigger', t_id, 'trigger', entity.id)
                if entity.data.args and 'event_id' in entity.data.args:
                    self._add_edge(edges, visited_edges, 'event', entity.data.args['event_id'], 'trigger', entity.id)
                if entity.data.args and 'completed_workflow_id' in entity.data.args:
                    self._add_edge(edges, visited_edges, 'workflow', entity.data.args['completed_workflow_id'], 'trigger', entity.id)
                if entity.data.args and 'subscription_id' in entity.data.args:
                    self._add_edge(edges, visited_edges, 'subscription', entity.data.args['subscription_id'], 'trigger', entity.id)

            if isinstance(entity, Subscription):
                self._add_node(nodes, visited_nodes, 'subscription', entity.id, entity.data.name, entity.data.state, None)
                if entity.data.dataset_id:
                    self._add_edge(edges, visited_edges, 'dataset', entity.data.dataset_id, 'subscription', entity.id)

            if isinstance(entity, Event):
                self._add_node(nodes, visited_nodes, 'event', entity.id, entity.data.name, entity.data.state, None)

            if isinstance(entity, Datastore):
                self._add_node(nodes, visited_nodes, 'datastore', entity.id, entity.data.name, entity.data.state, engine_name)

        return Graph(nodes, edges)

    @staticmethod
    def get_entity_identifiers(search):
        statement = text(ENTITY_IDENTIFIER_SQL).bindparams(search=search)
        return [GraphEntityIdentifier(*r) for r in db.session.execute(statement)]

    def get_entity_graph(self, entity):
        """ :type entity: dart.model.graph.GraphEntity
            :rtype: dart.model.graph.Graph """

        key = "graph:{0}".format(entity.entity_id)
        cached_graph = self.redis_client.get(key) if self.redis_client else None  # String representation of each Graph record

        if cached_graph:
            json_records = json.loads(cached_graph)
            ### read state from database:
            # we need to update the current state of object in the graph
            get_state_sql = ["select '{0}' as type, '{1}' as id,'{2}' as name, data->>'state' as state, '{4}' as sub_type, '{5}' as related_type, '{6}' as related_id, '{7}' as related_is_a FROM {0} where id = '{1}'".format(*rec) for rec in json_records]

            # add union all between sql statements
            state_sql = " union all ".join(get_state_sql)

            # execute sql: (dart_depth was already removed, so we use r instead of r[0:-1])
            records = [(r) for r in db.session.execute(text(state_sql))]
        else:
            records = [
                (r[:-1])  # remove depth parameter
                for r in db.session.execute(text(RECURSIVE_SQL).bindparams(
                    entity_type=entity.entity_type,
                    entity_id=entity.entity_id,
                    name=entity.name,
                    state=entity.state,
                    sub_type=entity.sub_type
                ))
            ]
            string_records = json.dumps(records)
            if self.redis_client:
                self.redis_client.setex(key, self.redis_expiration_ttl, string_records)  # Keep this graph cached for 10 minutes

        # rebuilding graph from string
        entities = [GraphEntity(*r) for r in records]

        nodes, visited_nodes = [], set()
        edges, visited_edges = [], set()

        for e in entities:
            self._add_node(nodes, visited_nodes, e.entity_type, e.entity_id, e.name, e.state, e.sub_type)

            if e.related_id:
                if e.related_is_a == Relationship.CHILD:
                    src_type = e.entity_type
                    src_id = e.entity_id
                    dst_type = e.related_type
                    dst_id = e.related_id
                else:
                    src_type = e.related_type
                    src_id = e.related_id
                    dst_type = e.entity_type
                    dst_id = e.entity_id

                self._add_edge(edges, visited_edges, src_type, src_id, dst_type, dst_id)

        # now that we have the base graph, add in the most recent workflow_instances and their actions,
        # as well as recent datastore actions that are not associated with any workflows (if any)
        wf_ids, wf_sql = self._get_workflow_instance_sql(nodes)
        d_ids, d_sql = self._get_datastore_one_offs(nodes)

        sql_parts = []
        if wf_sql:
            sql_parts.append('(' + wf_sql + ')')
        if d_sql:
            sql_parts.append('(' + d_sql + ')')

        if len(sql_parts) == 0:
            return Graph(nodes, edges)

        sql = '\nUNION ALL\n'.join(sql_parts)
        statement = text(sql.format(
            wf_ids=', '.join(["'" + wf_id + "'" for wf_id in wf_ids]),
            d_ids=', '.join(["'" + d_id + "'" for d_id in d_ids])
        ))

        for r in db.session.execute(statement):
            if r[0] == 'workflow':
                entity_type, wf_id, wfi_id, wfi_state, a_id, a_name, a_state, a_sub_type = r

                name = 'workflow_instance'
                if wfi_state == 'RUNNING' and ('workflow_instance' + wfi_id) not in visited_nodes:
                    actions = self._action_service.find_actions(workflow_instance_id=wfi_id)
                    if None not in [a.data.avg_runtime for a in actions]:
                        wfi_progress = 0
                        wf_runtime = 0
                        for a in actions:
                            a_runtime = a.data.avg_runtime.total_seconds()
                            wf_runtime += a_runtime
                            if a.data.state == ActionState.COMPLETED or a.data.state == ActionState.FAILED:
                                wfi_progress += a_runtime
                            elif (a.data.state == ActionState.RUNNING or a.data.state == ActionState.FINISHING) and a.data.progress:
                                wfi_progress += a_runtime * a.data.progress
                        if wf_runtime:
                            wfi_progress /= wf_runtime
                            name += ' - {:.0%}'.format(wfi_progress)

                self._add_node(nodes, visited_nodes, 'workflow_instance', wfi_id, name, wfi_state, None)
                self._add_edge(edges, visited_edges, 'workflow', wf_id, 'workflow_instance', wfi_id)
                if a_id:
                    self._add_node(nodes, visited_nodes, 'action', a_id, a_name, a_state, a_sub_type)
                    self._add_edge(edges, visited_edges, 'workflow_instance', wfi_id, 'action', a_id)
            else:
                entity_type, d_id, skip1, skip2, a_id, a_name, a_state, a_sub_type = r
                self._add_node(nodes, visited_nodes, 'action', a_id, a_name, a_state, a_sub_type)
                self._add_edge(edges, visited_edges, 'datastore', d_id, 'action', a_id)

        self.add_MM_nodes(nodes=nodes, edges=edges, visited_edges=visited_edges)
        return Graph(nodes, edges)

    def add_MM_nodes(self, nodes, edges, visited_edges):
        ''' MasterMind migrated workflows will want to show parent-child relationships between action nodes.
            We will show parent-child relationships only if the parallization parents exist (new workflows).
        '''
        def str_2_list(list_str):
            ''' '[1,2,3]' => [1,2,3] '''
            try:
                x = ast.literal_eval(list_str)
                return x
            except Exception as err:
                _logger.warning(err)
                return []

        def to_int(str):
            ''' "2.0" => 2 '''
            return int(float(str))

        # The template actions have the original order_idx (the instance action increment them constantly).
        # Instance action have mappings to their template actions via data->>'workflow_action_id'.
        # We match each template action with its instance action, and each parent template action with its matching
        # parent instance action.  For each such opair we add an edge.
        # In order de-clutter the graph we also remove edges from workflow and workflow-action to nodes
        # (except the first ones)

        # create template_action_id_2_instance_action which helps get an instance action by its template action id.
        WFI_IDS_SQL = "select  data->>'workflow_action_id', id, data->>'workflow_instance_id', data->>'workflow_id' from action where data->>'state'::TEXT != 'TEMPLATE' and data->>'workflow_instance_id' in ({0})"
        workflow_instance_ids = list(filter(partial(is_not, None),[n.entity_id if n.entity_type=='workflow_instance' else None for n in nodes]))
        wfi_ids = ','.join("'{0}'".format(w) for w in workflow_instance_ids)
        template_action_id_2_instance_action = {}
        if wfi_ids:
            for instance_action in db.session.execute(WFI_IDS_SQL.format(wfi_ids)):
                template_action_id_2_instance_action[instance_action[0]] = instance_action

        # For each workflow, find its actions and if action has parents than add edges from parent to child.
        WF_IDS_SQL = "SELECT id, data->>'order_idx' as idx, data->>'parallelization_parents' AS parents FROM action WHERE data->>'workflow_id'='{0}' and data->>'state'::TEXT = 'TEMPLATE'"
        workflow_ids = list(filter(partial(is_not, None),[n.entity_id if n.entity_type=='workflow' else None for n in nodes]))
        order_idx_2_template_action = {}
        template_action_id_2_order_idx = {}
        for wf_id in workflow_ids:
            # populate the two dictionaries above for the template actions only
            for action_record in db.session.execute(WF_IDS_SQL.format(wf_id)):
                order_idx_2_template_action[to_int(action_record[1])] = action_record
                template_action_id_2_order_idx[action_record[0]] = to_int(action_record[1])

            # For every template action (which may have an associated instance action) add an edge to parent (if exists)
            for act in order_idx_2_template_action.values():
                action_parents_str = act[2]
                if action_parents_str:  # if has parents (as order_idx values
                    template_action_id = act[0]

                    matching_instance_workflow_id = None
                    matching_instance_workflow_id = None
                    if template_action_id_2_instance_action.get(template_action_id):
                        matching_instance_action_id = template_action_id_2_instance_action.get(template_action_id)[1]
                        matching_instance_workflow_id = template_action_id_2_instance_action.get(template_action_id)[2]

                    # At this point we know we have parents for the template actions. We will add edges for the template
                    # actions and their matching instance actions (and parents) in tandem using mappings above.
                    for parents_idx in str_2_list(action_parents_str):
                        parent = order_idx_2_template_action.get(to_int(parents_idx))
                        if parent:
                            # In order to make the graph less cluttered we keep the workflow node pointing to
                            # the first template action only.
                            self._remove_edge(edges, visited_edges, "workflow", wf_id, "action", template_action_id)
                            if matching_instance_workflow_id:
                                self._remove_edge(edges, visited_edges, "workflow_instance", matching_instance_workflow_id, "action", matching_instance_action_id)

                            parent_template_action_id = parent[0]
                            self._add_edge(edges, visited_edges, 'action', parent_template_action_id, 'action', template_action_id)
                            if template_action_id_2_instance_action.get(parent_template_action_id):
                                parent_instance_action_id = template_action_id_2_instance_action.get(parent_template_action_id)[1]
                                if matching_instance_action_id:
                                    self._add_edge(edges, visited_edges, 'action', parent_instance_action_id, 'action', matching_instance_action_id)

    @staticmethod
    def _get_datastore_one_offs(nodes):
        d_sql = ''
        d_ids = [n.entity_id for n in nodes if n.entity_type == 'datastore']
        if len(d_ids) > 0:
            d_sql = DATASTORE_ONE_OFFS_SQL
        return d_ids, d_sql

    @staticmethod
    def _get_workflow_instance_sql(nodes):
        wf_sql = ''
        wf_ids = [n.entity_id for n in nodes if n.entity_type == 'workflow']
        if len(wf_ids) > 0:
            wf_sql = WORKFLOW_INSTANCE_SQL
        return wf_ids, wf_sql

    @staticmethod
    def _add_node(nodes, visited_nodes, e_type, e_id, name, state, sub_type):
        node_id = e_type + '-' + e_id
        if node_id not in visited_nodes:
            visited_nodes.add(node_id)
            nodes.append(Node(e_type, e_id, name, state, sub_type))

    @staticmethod
    def _add_edge(edges, visited_edges, s_type, s_id, d_type, d_id):
        edge_id = '%s-%s %s-%s' % (s_type, s_id, d_type, d_id)
        if edge_id not in visited_edges:
            visited_edges.add(edge_id)
            edges.append(Edge(s_type, s_id, d_type, d_id))

    @staticmethod
    def _remove_edge(edges, visited_edges, s_type, s_id, d_type, d_id):
        edge_id = '%s-%s %s-%s' % (s_type, s_id, d_type, d_id)
        found_item = False
        if edge_id in visited_edges:
            for idx, e in enumerate(edges):
                if (e.source_type == s_type) and (e.source_id == s_id) and (e.destination_type == d_type) and (e.destination_id == d_id):
                    found_item = True
                    break

        if found_item:
            del edges[idx]

    @staticmethod
    def _get_entity_type(entity):
        if isinstance(entity, Dataset):
            return EntityType.dataset
        if isinstance(entity, Action):
            return EntityType.action
        if isinstance(entity, Workflow):
            return EntityType.workflow
        if isinstance(entity, Trigger):
            return EntityType.trigger
        if isinstance(entity, Subscription):
            return EntityType.subscription
        if isinstance(entity, Event):
            return EntityType.event
        if isinstance(entity, Datastore):
            return EntityType.datastore

    def to_entity_map(self, entity_models):
        return {self._get_entity_type(em) + '-' + em.id: em for em in entity_models}

    @staticmethod
    def to_entity_models_with_randomized_ids(entity_models):
        # To support editing multiple subgraphs concurrently, we need to randomize the ids.
        # This method takes advantage of JSON formatting (a bit hacky, but simple and fast).
        id_map = {}
        for e in entity_models:
            prefix = 'UNSAVED'
            if e.id.startswith('PARENT'):
                prefix = 'PARENT'
            if e.id.startswith('CHILD'):
                prefix = 'CHILD'
            id_map[e.id] = '%s-%s' % (prefix, random_id())

        results = []
        for em in entity_models:
            stringified_model = json.dumps(em.to_dict())
            for original_id, randomized_id in id_map.iteritems():
                stringified_model = stringified_model.replace('"%s"' % original_id, '"%s"' % randomized_id)
            cls = type(em)
            results.append(cls.from_dict(json.loads(stringified_model)))
        return results


from scapy import *

def monitor_ports(target,monitored_ports):
    p=IP(dst=target)/TCP(dport=monitored_ports, flags="S")
    ans,unans=sr(p, timeout=1)
    open_ports = []
    closed_ports = []
    for s,n in ans:
        if s[TCP].dport == r[TCP].sport:
            open_ports.append(str(s[TCP].dport))
        else:
            closed_ports.append(str(s[TCP].dport))


import cme, eurex, liffe
from datecalc import *

__all__ = ['Computus', 'business_day']

class Computus:
	"""Factory for creating objects responsible for exchange date calculations. Each object
	implements the following methods:
		obj.fut_last_trade(month, year)
		obj.fut_first_notice(month, year)
		obj.fut_delivery(month, year)
		obj.opt_expiry(month, year)
	and is created by calling the make('label') function below, where 'label' refers to some
	canonical set of rules and is one of cbt-bond, cbt-note, cme-ed, cme-ed-mc, cme-ff,
	cme-metal, cme-wti, cme-fx, cme-soft, eurex-bund, liffe-ebor, liffe-stg

	Computus is also responsible for dealing with exchange holiday calendars and passing them
	to the datecalc objects above. Default rrulesets are defined for cme, target, eurex, and
	bba, but note these don't include special holidays like jubilee's etc. You can override
	these default rrulesets in the make function.

	N.B. It gets CLZ2 wrong for some reason, unsure what the deal is there. Bit worrying."""

	def __init__(self):
		self.cme_cal = rruleset(cache=True)
		self.target_cal = rruleset(cache=True)
		self.eurex_cal = rruleset(cache=True)
		self.bba_cal = rruleset(cache=True)

		self.cme_cal.rrule(adj_new_year())
		self.cme_cal.rrule(martin_luther_king_day())
		self.cme_cal.rrule(presidents_day())
		self.cme_cal.rrule(good_friday())
		self.cme_cal.rrule(last_monday_may())	# memorial day
		self.cme_cal.rrule(independence_day())	# adjusted 4th July
		self.cme_cal.rrule(labour_day())
		self.cme_cal.rrule(thanksgiving())
		self.cme_cal.rrule(adj_xmas())

		self.eurex_cal.rrule(adj_new_year())
		self.eurex_cal.rrule(good_friday())
		self.eurex_cal.rrule(easter_monday())
		self.eurex_cal.rrule(may1st())
		self.eurex_cal.rrule(adj_xmas_box())

		self.bba_cal.rrule(adj_new_year())
		self.bba_cal.rrule(good_friday())
		self.bba_cal.rrule(easter_monday())
		self.bba_cal.rrule(first_monday_may())
		self.bba_cal.rrule(last_monday_may())
		self.bba_cal.rrule(last_monday_aug())
		self.bba_cal.rrule(adj_xmas_box())

	def make(self, label, holidays=None):
		"""Construct the desired computus."""

		if label == 'cbot-bond':
			return cme.TBond(holidays if holidays else self.cme_cal)

		elif label == 'cbot-note':
			return cme.TNote(holidays if holidays else self.cme_cal)

		elif label == 'cme-ed':
			return cme.EuroDollar(holidays if holidays else self.cme_cal)

		elif label == 'cme-ed-mc':
			return cme.EuroDollarMC(holidays if holidays else self.cme_cal)

		elif label == 'cme-ff':
			return cme.FedFunds(holidays if holidays else self.cme_cal)

		elif label == 'cme-metal':
			return cme.Metal(holidays if holidays else self.cme_cal)

		elif label == 'cme-wti':
			return cme.WTI(holidays if holidays else self.cme_cal)

		elif label == 'cme-fx':
			return cme.FX(holidays if holidays else self.cme_cal)

		elif label == 'cme-soft':
			return cme.Soft(holidays if holidays else self.cme_cal)

		elif label == 'eurex-bund':
			return eurex.Bund(holidays if holidays else self.eurex_cal)

		elif label == 'liffe-ebor':
			return liffe.Euribor(holidays if holidays else self.bba_cal)

		elif label == 'liffe-stg':
			return liffe.ShortSterling(holidays if holidays else self.bba_cal)

		else:
			raise ValueError(label)


import numpy as np
import re as re
from scipy import stats
import gnc
import netCDF4 as nc
import copy as pcopy
import pdb
import pb
import pandas as pa

def Dic_DataFrame_to_Excel(excel_file,dic_df,multisheet=False,keyname=True,na_rep='', cols=None, header=True, index=True, index_label=None):
    """
    Write a dictionary of pandas.DataFrame data to an excel file with keys as excel sheets.
    """
    def _df_member(obj):
        """
        check if this obj is DataFrame obj.
        """
        if isinstance(obj,pa.core.frame.DataFrame):
            return True
        else:
            raise TypeError('this is not a DataFrame object')
    for key,dfm in dic_df.items():
        _df_member(dfm)

    excel_wob=pa.ExcelWriter(excel_file)

    if multisheet==True:
        for key,dfm in dic_df.items():
            dfm.to_excel(excel_wob,sheet_name=key,na_rep=na_rep,cols=cols,header=header,index=index,index_label=index_label)
    elif multisheet==False:
        for key,dfm in dic_df.items():
            if keyname==True:
                excel_wob.writerow([],sheet_name='sheet1')
                excel_wob.writerow([key],sheet_name='sheet1')
            dfm.to_excel(excel_wob,sheet_name='sheet1',na_rep=na_rep,cols=cols,header=header,index=index,index_label=index_label)
    else:
        pass

    excel_wob.save()

def DataFrame_to_Excel(excel_file,DataFrame,na_rep='', cols=None, header=True, index=True, index_label=None):
    """
    Write a pandas DataFrame to excel file
    """
    tempdic={}
    tempdic['data']=DataFrame
    Dic_DataFrame_to_Excel(excel_file,tempdic,multisheet=False,keyname=False,na_rep=na_rep, cols=cols, header=header, index=index, index_label=index_label)

def blankspace2csv(blank_sep_file,csvfile):
    """
    Purpose: change a blankspace separated file to a comma separated file. Different columns in the blank space delimited file can have arbitrary number of blank spaces.
    """
    csv.register_dialect('whitespace',delimiter=' ',skipinitialspace=True)
    fob=open(blank_sep_file)
    csvob=csv.reader(fob,dialect='whitespace')
    fob2=file(csvfile,'w')
    csv_writer = csv.writer(fob2)
    for d in csvob:
        csv_writer.writerow(d)
    fob2.close()

def DataFrame_from_Dic_Key_as_Index(data,columns=None):
    """
    Purpose: to convert dictionary to pandas dataframe using dic.keys() as index and impose column names by setting columns=['var1','var2','var3']
    Note:
        1. data is a dictionary, columns is list of strings representing new dataframe column names.
        2. The original keys of input dictionary serve as index for new dataframe; len(columns) must be equal to len(data[key])
        3*. currently (2012/05/14) data[key]=(1D np.array -- or list -- or only one number) has been tested.
    Example:
        In [41]: print p2s2.keys()
        [1952, 1955, 1956, 1957, 1958, 1959, 1960, 1961, 1962, 1963, 1964, 1965, 1966, 1967, 1968, 1969, 1970, 1971, 1972, 1973, 1974, 1975, 1976, 1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006]
        In [42]: p2s2[1952]
        Out[42]: array([ 16.57142857,   2.        ,  42.        ])
        In [51]: df=mathex.DataFrame_fromdic(p2s2,columns=['mean','min','max'])
        In [52]: df.head()
        Out[52]:
              max       mean  min
              1952   42  16.571429    2
              1955   55  23.400000    5
              1956   35  16.714286    4
              1957   37  23.600000   11
              1958   71  39.666667   11
        In [53]: df.index
        Out[53]:
        Int64Index([1952, 1955, 1956, 1957, 1958, 1959, 1960, 1961, 1962, 1963, 1964,
               1965, 1966, 1967, 1968, 1969, 1970, 1971, 1972, 1973, 1974, 1975,
               1976, 1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985, 1987,
               1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
               1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006], dtype=int64)

    """
    temp=dict()
    if columns == None:
        raise ValueError('columns names must be provided!')
    else:
        for colname in columns:
            temp[colname]=[]
    for i in data.keys():
        if len(columns)==1:
            temp[colname].append(data[i])
        else:
            for j,colname in enumerate(columns):
                temp[colname].append(data[i][j])
    tempdf=pa.DataFrame(temp,index=data.keys())
    return tempdf

def csv_concat(outfile,infile_list,index_col=None):
    """
    Purpose: Used to concat several csv files with the same column names.
    """
    if not isinstance(infile_list,list):
        raise TypeError("infile_list must be provided as a list")
    else:
        df_list=[]
        for filename in infile_list:
            dft = pa.read_csv(filename,index_col=index_col)
            df_list.append(dft)
        df_all = pa.concat(df_list)
        df_all.to_csv(outfile)
        return df_all

def concat_files(outfile,infile_list,remove_first_line=True):
    """
    Concatenate files.

    Parameters:
    -----------
    remove_first_line: if you want to remove the first line starting from the second input file, then set remove_first_line as True, otherwise False
    """
    pass


# -*- coding: utf-8 -*-
"""
Created on Sun Oct 21 18:05:51 2018

@author: Tianqi Guo
"""

class Solution(object):

    def letterCombinations(self, digits):
        """
        :type digits: str
        :rtype: List[str]
        """

        # the characters corresponding to each number
        c = [['a','b','c'],
             ['d','e','f'],
             ['g','h','i'],
             ['j','k','l'],
             ['m','n','o'],
             ['p','q','r','s'],
             ['t','u','v'],
             ['w','x','y','z']]
        # deal with the situation of an empty list
        l = len(digits)
        if l == 0:
            return []
        # the queue that stores all combinations by previous digits
        s = [""]
        # iterate over all digits
        for i in range(l):
            # get the current digit
            num = int(digits[i])
            # the queue that stores all new combinations
            ss = []
            # iterate over all previous combinations
            while s:
                # for each previous combination
                sss = s.pop(0)
                # combine with the characters for current digit
                for j in range(len(c[num-2])):
                    # record all new combinations
                    ss.append(sss+c[num-2][j])
            # copy the new combinations to the queue
            s = ss
        return s

digits = "23"
test = Solution()
print(test.letterCombinations(digits))


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''
script to build a deep neural network using data from "submitScriptOrig.py"
output: graphic of the model ("DNN_model.png") and training & validation plots ("DNN_training.png")
'''

import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split, KFold
import sklearn.metrics
from sklearn.metrics import confusion_matrix, roc_auc_score, roc_curve, accuracy_score, precision_recall_curve, auc, f1_score
from tensorflow.keras import models, layers, utils, backend as K
import tensorflow as tf
import statistics

from plot_NN import *
#from submitScriptOrig import GET_DATA #Remark: Everything in submitScriptOrig.py that is not in a function will be executed!

def Recall(y_true, y_pred):
    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
    possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
    recall = true_positives / (possible_positives + K.epsilon())
    return recall

def Precision(y_true, y_pred):
    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
    predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
    precision = true_positives / (predicted_positives + K.epsilon())
    return precision

def F1(y_true, y_pred):
    precision = Precision(y_true, y_pred)
    recall = Recall(y_true, y_pred)
    return 2*((precision*recall)/(precision+recall+K.epsilon()))

def create_model(n_features):
    """
    Creates a simple (mostly) unoptimized neural network with three hidden and dropout layers.
    """
    model = models.Sequential(name="DeepNN", layers=[
        ### hidden layer 1
        layers.Dense(name="h1", input_dim=n_features,
                     units=int(round((n_features)/2)),
                     activation='relu'),
        layers.Dropout(name="drop1", rate=0.2),
        ### hidden layer 2
        layers.Dense(name="h2", units=int(round((n_features)/4)),
                     activation='relu'),
        layers.Dropout(name="drop2", rate=0.2),
        ### hidden layer 3
        layers.Dense(name="h3", units=int(round((n_features+10)/8)),
                     activation='relu'),
        layers.Dropout(name="drop3", rate=0.2),
        layers.Dense(name="output", units=1, activation='sigmoid')
    ])
    # compile the neural network
    model.compile(optimizer='SGD', loss='mean_absolute_error',
                  metrics=['accuracy',F1])#,Recall,Precision])
    return model

def train_DNN(fps, act):
    '''
    Train and validate the model.
    '''
    fpsIndList = [i for i in range(0,len(fps))]
    trainFpsInds, testFpsInds, y_train, y_val = train_test_split(fpsIndList, act, test_size=0.10, stratify=act)
    x_train = np.array([fps[x] for i,x in enumerate(trainFpsInds)])
    x_val = np.array([fps[x] for i,x in enumerate(testFpsInds)])
    n_features = x_train.shape[1]
    model = create_model(n_features)
    model.summary()
    #configuration
    bs = 32
    ep = 1000
    #plot
    utils.plot_model(model, to_file='DNN_model.png', show_shapes=True, show_layer_names=True)
    #### train model ####
    training = model.fit(x=x_train, y=np.array(y_train), batch_size=bs, epochs=ep, verbose=0, validation_data=(x_val, np.array(y_val)))
    #### results ####
    print("Training DNN")
    print("Training Accuracy: ", round(max(training.history['accuracy']), 3))
    print("Training F1: ", round(max(training.history['F1']), 3))
    print("Validation Accuracy: ", round(max(training.history['val_accuracy']), 3))
    print("Validation F1: ", round(max(training.history['val_F1']), 3))
    #### plot ####
    metrics = [k for k in training.history.keys() if ("loss" not in k) and ("val" not in k)]
    fig, ax = plt.subplots(nrows=1, ncols=2, sharey=True, figsize=(40,12))
    ## training
    ax[0].set(title="Training")
    ax11 = ax[0].twinx()
    ax[0].plot(training.history['loss'], color='black')
    ax[0].set_xlabel('Epochs')
    ax[0].set_ylabel('Loss', color='black')
    for metric in metrics:
        ax11.plot(training.history[metric], label=metric)
    ax11.set_ylabel("Score", color='steelblue')
    ax11.legend()
    ## validation
    ax[1].set(title="Validation")
    ax22 = ax[1].twinx()
    ax[1].plot(training.history['val_loss'], color='black')
    ax[1].set_xlabel('Epochs')
    ax[1].set_ylabel('Loss', color='black')
    for metric in metrics:
        ax22.plot(training.history['val_'+metric], label=metric)
    ax22.set_ylabel("Score", color="steelblue")
    plt.savefig('DNN_training.png', dpi=200)
    plt.close()
    return training.model

def test_DNN(model, fps, act):
    '''
    Test DNN using external data set.
    '''
    fps = np.array(fps)
    act = np.array(act)
    res = model.predict(fps)
    compare = pd.DataFrame({'is_TRUE':np.where(act==1, True, False), 'prediction':np.where(res[:, 0]>0.75, True, False), 'prediction_value':res.tolist()})
    tn, fp, fn, tp = confusion_matrix(compare['is_TRUE'], compare['prediction'], labels=[0, 1]).ravel()
    print("External Validation DNN")
    print('AUC:', round(roc_auc_score(act, res[:, 0]), 3))
    print('Accuracy:', round((tp+tn)/(tp+fp+tn+fn), 3))
    print('Sensitivity:', round(tp/(tp+fn), 3))
    print('False negative Rate:', round(fn/(tp+fn), 3))
    print('Specificity:', round(tn/(tn+fp), 3))
    print('False positive Rate:', round(fp/(tn+fp), 3))
    precision, recall, thresholds = precision_recall_curve(compare['is_TRUE'], compare['prediction'])
    area = sklearn.metrics.auc(recall, precision)
    f1 = f1_score(compare['is_TRUE'], compare['prediction'])
    print(f'F1: %0.3f' % f1)
    fpr, tpr, thresholds = roc_curve(compare['is_TRUE'], compare['prediction'])
    res = [round(roc_auc_score(act, res[:, 0]), 3), round((tp/(tp+fn) + tn/(tn+fp))/2, 3), round((tp+tn)/(tp+fp+tn+fn), 3), round(tp/(tp+fn), 3), round(tn/(tn+fp), 3), f1]
    #plt.figure()
    #plt.plot(fpr, tpr)
    #plt.title('ROC curve')
    #plt.xlabel('False Positive Rate')
    #plt.ylabel('True Positive Rate')
    #plt.savefig('DNN_externalValidation_ROCcurve.png', dpi=200)
    #plt.close()
    return res, compare

def k_fold_DNN(k, x, y):
    '''
    K-Fold cross validation for DNN.

    '''
    x = np.array(x)
    y = np.array(y)
    kfold = KFold(n_splits=k, shuffle=True, random_state=42)
    auc = []
    bacc =[]
    acc = []
    sens = []
    fnr = []
    spec = []
    fpr = []
    auprc = []
    f1 = []
    for train_index, test_index in kfold.split(x):
        kfold_x_train, kfold_x_test = x[train_index], x[test_index]
        kfold_y_train, kfold_y_test = y[train_index], y[test_index]
        model = create_model(x.shape[1])
        model.fit(kfold_x_train, kfold_y_train, batch_size=32, epochs=1000, verbose=0)
        y_pred = model.predict(kfold_x_test)
        y_pred = np.where(y_pred[:, 0]>0.75, 1, 0)
        kfold_y_test = tf.constant(kfold_y_test)
        accuracy = accuracy_score(kfold_y_test, y_pred)
        tn, fp, fn, tp = confusion_matrix(kfold_y_test, y_pred, labels=[0, 1]).ravel()
        precision, recall, thresholds = precision_recall_curve(kfold_y_test, y_pred)
        area = sklearn.metrics.auc(recall, precision)
        auc.append(roc_auc_score(kfold_y_test, y_pred))
        bacc.append((tp/(tp+fn) + tn/(tn+fp))/2)
        acc.append(accuracy)
        sens.append(tp/(tp+fn))
        fnr.append(fn/(tp+fn))
        spec.append(tn/(tn+fp))
        fpr.append(fp/(tn+fp))
        auprc.append(area)
        f1.append(f1_score(kfold_y_test, y_pred))
    kfold_res = pd.DataFrame(list(zip(auc, bacc, acc, sens, fnr, spec, fpr, auprc, f1)), columns=['AUC', 'Balanced_Accuracy', 'Accuracy', 'Sensitivity', 'FNR', 'Specificity', 'FPR', 'AUC_PR', 'F1'])
    print(k, 'Fold Cross Validation DNN')
    print('AUC:', round(statistics.mean(auc), 3))
    print('Accuracy:', round(statistics.mean(acc), 3))
    print('Sensitivity:', round(statistics.mean(sens), 3))
    print('False negative Rate:', round(statistics.mean(fnr), 3))
    print('Specificity:', round(statistics.mean(spec), 3))
    print('False positive Rate:', round(statistics.mean(fpr), 3))
    print('Area Under PR Curve(AP):', round(statistics.mean(auprc), 3))
    print('F1:', round(statistics.mean(f1), 3))
    return kfold_res

### run the model ###
#global dataSet
#global sampling
#dataSet = "AhR"
#sampling = "kMedoids2"
#trainFps, trainAct = GET_DATA(dataSet,sampling=sampling)
#train_DNN(trainFps, trainAct)


import requests
import json
import time
import urllib.request
from PIL import Image
import datetime
def todayAt (hr, minutes=0, sec=0, micros=0):
    now_time = datetime.datetime.now()
    return now_time.replace(hour=hr, minute=minutes, second=sec, microsecond=micros)
while datetime.datetime.now() < todayAt (16,10):
    r = requests.get('https://dog.ceo/api/breeds/image/random')
    if r.status_code == 200:
        try:
            pretty_json =json.dumps(r.json(),indent=2)
        except:
            print("not a json")
            exit()
        print(f'Pretty: {pretty_json}')
    else:
        print(f"error {r.status_code}")
    msg= r.json()
    urllib.request.urlretrieve(
      msg['message'],
       "gfg.png")
    """img = Image.open("gfg.png")
    img.show()
    img.close()"""
    with Image.open('gfg.png') as test_image:
        test_image.show()
        time.sleep(3)