import os
import logging

import anchore_image_db
import controller
from anchore import anchore_utils
from anchore.util import scripting
from anchore.util import contexts

class Analyzer(object):
    _logger = logging.getLogger(__name__)

    def __init__(self, anchore_config, imagelist, allimages, force, args=None):
        self._logger.debug("analyzer initialization: begin")

        self.config = anchore_config
        self.allimages = allimages
        self.force = force
        self.anchore_datadir = self.config['image_data_store']

        self.dockerfile = None
        try:
            self.dockerfile = args['dockerfile']
        except:
            pass

        self.skipgates = False
        try:
            self.skipgates = args['skipgates']
        except:
            pass

        try:
            if 'isbase' in args and args['isbase']:
                usertype = 'base'
            elif 'anchorebase' in args and args['anchorebase']:
                usertype = 'anchorebase'
            else:
                usertype = None
        except:
            usertype = None

        self._logger.debug("init input processed, loading input images: " + str(imagelist))

        self.images = anchore_utils.image_context_add(imagelist, allimages, docker_cli=contexts['docker_cli'], dockerfile=self.dockerfile, anchore_datadir=self.anchore_datadir, tmproot=self.config['tmpdir'], anchore_db=contexts['anchore_db'], usertype=usertype, must_load_all=True)

        self._logger.debug("loaded input images, checking that all input images have been loaded " + str(self.images))

        self.anchoreDB = contexts['anchore_db']

        self._logger.debug("analyzer initialization: end")

    def get_images(self):
        return(self.images)

    def run_analyzers(self, image):
        self._logger.debug("running analyzers on image: " + str(image.meta['imagename']) + ": begin")

        imagename = image.meta['imagename']
        outputdir = image.anchore_imagedir
        shortid = image.meta['shortId']
        analyzerdir = '/'.join([self.config["scripts_dir"], "analyzers"])

        if not self.force and os.path.exists(outputdir + "/analyzers.done"):
            self._logger.debug("image already analyzed and --force was not specified, nothing to do")
            self._logger.info(image.meta['shortId'] + ": analyzed.")

            return (True)

        self._logger.info(image.meta['shortId'] + ": analyzing ...")

        if not os.path.exists(outputdir):
            self._logger.debug("outputdir '" + str(outputdir) + "'not found, creating")
            os.makedirs(outputdir)

        self._logger.debug("unpacking image")
        imagedir = image.unpack()
        self._logger.debug("finished unpacking image to directory: " + str(imagedir))

        self._logger.debug("running all analyzers on image")
        path_overrides = ['/'.join([self.config['user_scripts_dir'], 'analyzers'])]
        if self.config['extra_scripts_dir']:
            path_overrides = path_overrides + ['/'.join([self.config['extra_scripts_dir'], 'analyzers'])]

        results = scripting.ScriptSetExecutor(path=analyzerdir, path_overrides=path_overrides).execute(capture_output=True, fail_fast=True, cmdline=' '.join([imagename, self.config['image_data_store'], outputdir, imagedir]))
        self._logger.debug("analyzers done running" + str(len(results)))

        success = True
        for r in results:
            (cmd, retcode, output) = r
            if retcode:
                # something failed
                self._logger.error("FAILED")
                self._logger.error("\tCMD: " + cmd)
                self._logger.error("\tEXITCODE: " + str(retcode))
                self._logger.error("\tOUTPUT: " + output)
                success = False
            else:
                self._logger.debug("")
                self._logger.debug("\tCMD: " + cmd)
                self._logger.debug("\tEXITCODE: " + str(retcode))
                self._logger.debug("\tOUTPUT: " + output)
                self._logger.debug("")

        self._logger.debug("analyzer commands all finished with successful exit codes")
        if success:
            self._logger.debug("generating analysis report from analyzer outputs and saving")
            report = self.generate_analysis_report(image)
            self.anchoreDB.save_analysis_report(image.meta['imageId'], report)

            self._logger.debug("saving image information with updated analysis data")
            image.save_image()

            anchore_utils.touch_file(outputdir + "/analyzers.done")

            self._logger.info(image.meta['shortId'] + ": analyzed.")

        self._logger.debug("running analyzers on image: " + str(image.meta['imagename']) + ": end")
        return (success)

    def generate_analysis_report(self, image):
        # this routine reads the results of image analysis and generates a formatted report
        report = {}

        analysisdir = image.anchore_imagedir + "/analyzer_output/"
        for d in os.listdir(analysisdir):
            if d not in report:
                report[d] = {}

            moduledir = analysisdir + "/" + d
            for o in os.listdir(moduledir):
                datafile = moduledir + "/" + o
                if o not in report[d]:
                    report[d][o] = list()
                report[d][o] = anchore_utils.read_plainfile_tolist(datafile)
        return (report)

    def generate_compare_report(self, image):
        # this routine reads the results of image compare and generates a formatted report
        report = {}

        rootdir = image.anchore_imagedir + "/compare_output/"
        for b in os.listdir(rootdir):
            if b not in report:
                report[b] = {}

            comparedir = rootdir + "/" + b + "/"
            for d in os.listdir(comparedir):
                if d == 'differs.done':
                    continue

                if d not in report[b]:
                    report[b][d] = {}

                moduledir = comparedir + "/" + d
                for o in os.listdir(moduledir):
                    datafile = moduledir + "/" + o

                    if o not in report[b][d]:
                        report[b][d][o] = list()
                    report[b][d][o] = anchore_utils.read_plainfile_tolist(datafile)

        return (report)

    def run_differs(self, image, baseimage):
        self._logger.debug("comparison of " + str(image.meta['imagename']) + " to " + str(image.meta['imagename']) + ": begin")
        shortida = image.meta['shortId']
        shortidb = baseimage.meta['shortId']

        if not image.is_analyzed():
            self._logger.error("cannot compare image " + shortida + " - need to analyze first.")
            return (False)

        if not baseimage.is_analyzed():
            self._logger.error("cannot compare image " + shortidb + " - need to analyze first")
            return (False)

        outputdir = image.anchore_imagedir

        if not self.force and os.path.exists(
                                        outputdir + "/compare_output/" + baseimage.meta['imageId'] + "/differs.done"):
            self._logger.debug("images already compared and --force not specified, nothing to do")
            self._logger.info(shortida + " to " + shortidb + ": compared.")
            return (True)

        self._logger.info(shortida + " to " + shortidb + ": comparing ...")

        if not os.path.exists(outputdir):
            self._logger.debug("output directory '" + str(outputdir) + "' does not exist, creating")
            os.makedirs(outputdir)

        thedir = outputdir + "/compare_output/" + baseimage.meta['imageId'] + "/"
        if not os.path.exists(thedir):
            self._logger.debug("output directory '" + str(thedir) + "' does not exist, creating")
            os.makedirs(thedir)

        compares = anchore_utils.diff_images(image, baseimage)
        for azkey in compares.keys():
            for aokey in compares[azkey].keys():
                outputdict = compares[azkey][aokey]
                thedir = outputdir + "/compare_output/" + baseimage.meta['imageId'] + "/" + azkey + "/"
                if not os.path.exists(thedir):
                    os.makedirs(thedir)

                thefile = thedir + "/" + aokey
                anchore_utils.write_kvfile_fromdict(thefile, outputdict)

        self._logger.debug("all comparisons completed")

        anchore_utils.touch_file(outputdir + "/compare_output/" + baseimage.meta['imageId'] + "/differs.done")

        self._logger.info(shortida + " to " + shortidb + ": compared.")

        self._logger.debug("comparison of " + str(image.meta['imagename']) + " to " + str(image.meta['imagename']) + ": end")
        return (True)

    def run(self):
        self._logger.debug("main image analysis on images: " + str(self.images) + ": begin")
        # analyze image and all of its family members
        success = True
        toanalyze = {}
        comparehash = {}
        linkhash = {}

        # calculate all images to be analyzed
        for imageId in self.images:
            coreimage = self.allimages[imageId]

            toanalyze[coreimage.meta['imageId']] = coreimage

            base = False
            lastimage = coreimage
            for i in coreimage.anchore_familytree:
                image = self.allimages[i]
                toanalyze[image.meta['imageId']] = image

                if (image.meta['shortId'] != coreimage.meta['shortId'] and not image.is_intermediate()):
                    comparehash[coreimage.meta['shortId'] + image.meta['shortId']] = [coreimage, image]
                    comparehash[lastimage.meta['shortId'] + image.meta['shortId']] = [lastimage, image]
                    if not base and image.is_base():
                        base = image
                    lastimage = image

            if base:
                linkhash[image.meta['imageId']] = base.meta['imageId']

        # execute analyzers
        self._logger.debug("images to be analyzed: " + str(toanalyze.keys()))
        for imageId in toanalyze.keys():
            image = toanalyze[imageId]
            success = self.run_analyzers(image)
            if not success:
                self._logger.error("analyzer failed to run on image " + str(image.meta['imagename']) + ", skipping the rest")
                break

        if not success:
            self._logger.error("analyzers failed to run on one or more images.")
            return (False)

        # execute differs
        for k in comparehash.keys():
            a = comparehash[k][0]
            b = comparehash[k][1]
            if a.meta['imageId'] != b.meta['imageId']:
                self.run_differs(a, b)

            self._logger.debug("generating and saving comparison report")
            report = self.generate_compare_report(a)
            self.anchoreDB.save_compare_report(a.meta['imageId'], report)

        for k in linkhash.keys():
            image = self.allimages[k]
            base = self.allimages[linkhash[k]]
            if image.meta['imageId'] != base.meta['imageId']:
                self._logger.debug("found image (" + image.meta['imageId'] + ") base (" + base.meta[
                    'imageId'] + "), creating softlink 'base'")
                dpath = image.anchore_imagedir + "/compare_output/base"
                if os.path.exists(dpath):
                    os.remove(dpath)
                os.symlink(image.anchore_imagedir + "/compare_output/" + base.meta['imageId'], dpath)

        if not self.skipgates:
            # execute gates
            self._logger.debug("running gates post-analysis: begin")
            for imageId in toanalyze.keys():
                c = controller.Controller(anchore_config=self.config, imagelist=[imageId], allimages=self.allimages).run_gates(refresh=True)
            self._logger.debug("running gates post-analysis: end")

        self._logger.debug("main image analysis on images: " + str(self.images) + ": end")
        return (success)


import sqlite3
db = None

def connect_and_get_cursor():
    global db
    db = sqlite3.connect('db/mobile.sqlite')
    cursor = db.cursor()
    return cursor

def print_all_table(cursor):
    cursor.execute('''select * from users''')
    result = cursor.fetchall()
    for x in result:
        print(x)

cursor = connect_and_get_cursor()

def get_record(identifier, value, table, cursor):
    sql_string = 'select * from {} where {} = {}'.format(table,identifier, value)
    cursor.execute(sql_string)
    result= cursor.fetchone()
    return result

def delete_record(id, table, cursor):
    # check record exists
    if get_record("id", id, table, cursor) is not None:
        sql_string= "delete from {} where id = {}".format(table, id)
        print(sql_string)
        cursor.execute(sql_string)
        db.commit()
        print("Record with id {} has been deleted ".format(id))
    else:
        print("Record with id {} could not be found".format(id))
delete_record(2,"users", cursor)

print_all_table(cursor)

db.close()


import matplotlib.pyplot as plt
import numpy

def timeseries_plot(data, legends, title, linewidth=0.5, save_as_file=None, show=False):
    plt.clf()
    cmap = plt.get_cmap("tab10")
    for i in range(0, len(data)):
        for j in range(0,len(data[i])):
            if j==0:
                plt.plot(data[i][j], color=cmap(i), label=legends[i], linewidth=linewidth)
            else:
                plt.plot(data[i][j], color=cmap(i), label='__nolegend__', linewidth=linewidth)
    plt.title(title)
    plt.legend()
    if save_as_file is not None:
        plt.savefig(save_as_file + ".png")
    if show:
        plt.show()

    plt.close('all')

def histogram_plot(data, legends, title, plot_mean=True, bins=80, save_as_file=None, show=False):
    plt.clf()
    bins = numpy.linspace(numpy.amin(data), numpy.amax(data), bins)
    cmap = plt.get_cmap("tab10")
    for i in range(0, len(data)):
        plt.hist(data[i], bins, color=cmap(i), edgecolor='k', alpha=0.5, label=legends[i])
        if plot_mean:
            plt.axvline(numpy.mean(data[i]), color=cmap(i), linestyle='dashed', linewidth=2)
    plt.title(title)
    plt.legend()
    if save_as_file is not None:
        plt.savefig(save_as_file + ".png")
    if show:
        plt.show()

    plt.close('all')

def barchart_plot(data, legends, title, errors=None, save_as_file=None, show=False):
    plt.clf()

    no_sets = len(data)
    width = 0.8/no_sets
    ind = numpy.arange(len(data[0]))

    fig, ax = plt.subplots()

    for i in range(0, len(data)):
        if errors is not None:
            ax.bar(ind - i * width, data[i], width, label=legends[i], yerr=errors[i], ecolor='grey', error_kw=dict(lw=1))
        else:
            ax.bar(ind - i * width, data[i], width, label=legends[i])

    plt.title(title)
    plt.legend()

    ax.set_xticks(ind - width*(no_sets-1) / 2)
    ax.set_xticklabels(ind)

    if save_as_file is not None:
        plt.savefig(save_as_file + ".png")
    if show:
        plt.show()

    plt.close(fig)


# -*- coding: utf-8 -*-
"""
/dms/photo/views_show.py

.. zeigt den Inhalt eines Photos an
         Django content Management System

Hans Rauch
hans.rauch@gmx.net

Die Programme des dms-Systems koennen frei genutzt und den spezifischen
Beduerfnissen entsprechend angepasst werden.

0.01  29.10.2007  Beginn der Arbeit
0.02  31.10.2007  Anzeige des Bildes
"""

from django.shortcuts   import render_to_response
from django.template.loader import get_template
from django.template import Context

from django.utils.translation import ugettext as _

from dms.utils          import show_link
from dms.utils_form     import get_item_vars_show
from dms.views_comment  import item_comment
from dms.file.utils     import get_file_url
from dms.gallery.utils  import get_exibition_url

from dms_ext.extension    import * # dms-Funktionen ueberschreiben

# -----------------------------------------------------
def photo_show(request,item_container):
  """ zeigt den Inhalt eines Photos """

  def get_photo_name_middle(item_container):
    """ ..liefert die Namen der normalen Bilder """
    file_name = get_file_url(item_container)
    ext_pos = file_name.rfind('.')
    return file_name[:ext_pos] + '_middle' + file_name[ext_pos:]

  app_name = 'photo'
  parent = item_container.get_parent()
  if parent.item.has_comments:
    comments = item_comment(request, item_container=item_container)
  else:
    comments = ''
  vars = get_item_vars_show(request, item_container, app_name)
  vars['comments'] = comments
  vars['image_url'] = get_photo_name_middle(item_container)
  vars['image_url_big'] = show_link(get_file_url(item_container), _(u'Originalphoto'), True)
  tItem = get_template('app/photo/show_photo.html')
  vars['full_name'] = item_container.item.string_1
  vars['email'] = item_container.item.string_2
  vars['exibition_url'] = get_exibition_url(item_container)
  vars['name'] = item_container.item.name
  vars['text_more'] = tItem.render(Context(vars))
  vars['text'] = ''
  vars['image_url'] = ''
  return render_to_response ( 'base-full-width.html', vars )


CSV_COLUMNS = [
    'id', 'td',
    'yaw', 'pitch', 'roll',
    'ax', 'ay', 'az',
    'gx', 'gy', 'gz',
    'qx', 'qy', 'qz', 'qw'
]

SUBJECTS = [
    'albert',
    'canon_12_5',
    'chen',
    'daniel',
    'isa_12_5',
    'joanne',
    'jq_12_6',
    'kelly_11_7',
    'kevin_11_7',
    'ruocheng',
    'russell_11_20_stand',
    'russell_random_12_7',
    'solomon',
    'yiheng_11_30',
    'yiheng_12_5',
    'yongxu_11_30',
    'zhaoye',
    'wenzhou_12_5',
    'haobin_11_22',
    'janet',
    'russell_11_7',
]

LABELS = 'abcdefghijklmnopqrstuvwxyz'
CALI_NAME = 'calibration'

NUM_OF_INTERP_POINTS = 100
THETA_RANGE = 5

SPLIT_MODE_CLASSIC = 0
SPLIT_MODE_BY_SUBJECT = 1


from django.urls import path

from . import views

app_name = 'sitebase'
urlpatterns = [
    path('', views.IndexView.as_view(), name='index'),
    path('news/', views.news, name='news'),
    path('news/<int:topic_id>/', views.newsdetail, name='newsdetail'),
    path('contact/', views.ContactView.as_view(), name='contact'),
    path('contact/done', views.ContactDoneView.as_view(), name='contactdone'),
]


#!/usr/bin/env python3
# coding:utf-8

import datetime
import logging

logging.basicConfig(filename='test_logging.log', level=logging.INFO,
                    format='%(asctime)s [%(levelname)s] %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
logging.debug('this is debug logs')
logging.info('this is info logs')
logging.warning('this is warning logs')
logging.error('this is error logs')
logging.critical("this is critical logs")
now = datetime.datetime.now()
now_format = now.strftime("%Y-%m-%d %H:%M:%S")
print(now_format)


# https://projecteuler.net/problem=387
from euler import is_prime

def sum_digits(n):
    s = 0
    for i in str(n):
        s += int(i)

    return s

def is_harshad(n):
    if n % sum_digits(n) == 0:
        return True
    else:
        return False

rhn = []
index = 0
for i in range(10, 100):
    if is_harshad(i):
        rhn.append(i)

# set this to the upper limit (10**exponent) exponent
exponent = 14

digits = 3
target = exponent - 1
while digits <= target:
    loop = rhn.copy()[index:]
    index = len(rhn)
    for i in loop:
        for j in range(10):
            n = i*10 + j
            if is_harshad(n):
                rhn.append(n)

    digits += 1

shn = []
for n in rhn:
    if is_prime(n // sum_digits(n)):
        shn.append(n)

s = 0
for n in shn:
    for j in [1, 3, 7, 9]:
        r = n*10 + j
        if is_prime(r):
            s += r
            print(r)

print('sum of harshard primes below 10^' + str(exponent) + ' is ' + str(s))


#!/bin/python3

def word_ladder(start_word, end_word, dictionary_file='words5.dict'):
    '''
    Returns a list satisfying the following properties:

    1. the first element is `start_word`
    2. the last element is `end_word`
    3. elements at index i and i+1 are `_adjacent`
    4. all elements are entries in the `dictionary_file` file

    For example, running the command
    ```
    word_ladder('stone','money')
    ```
    may give the output
    ```
    ['stone', 'shone', 'phone', 'phony', 'peony', 'penny', 'benny', 'bonny', 'boney', 'money']
    ```
    but the possible outputs are not unique,
    so you may also get the output
    ```
    ['stone', 'shone', 'shote', 'shots', 'soots', 'hoots', 'hooty', 'hooey', 'honey', 'money']
    ```
    (We cannot use doctests here because the outputs are not unique.)

    Whenever it is impossible to generate a word ladder between the two words,
    the function returns `None`.
    '''

    from collections import deque
    from copy import deepcopy

    dic = open(dictionary_file)
    dictionary = dic.read().split("\n")

    s = []                                          #create an empty stack
    s.append(start_word)                            #add the start_word to the stack

    q = deque()                                     #create a queue
    q.append(s)                                     #push the stack onto the queue

    if start_word == end_word:                      #if start_word is the end_word
        return s                                    #return the stack

    while len(q) > 0:                                       #if the there still are stack on the queue
        topstack = q.pop()                                  #take the right stack as topstack

        newdic=deepcopy(dictionary)

        for word in newdic:                             #for each word in the dictionary
            if _adjacent(word,topstack[-1]) is True:       #if it is the adjacent word with the first term from the right side of the stak
                copystack = deepcopy(topstack)              #make a copy of the stack
                copystack.append(word)                      #add that word to the copied stack

                if word == end_word:                        #if this word is the end_word
                    return copystack                        #return the rest of the stack

                q.appendleft(copystack)                     #add this stack to the left of the queue
                dictionary.remove(word)                     #remove the word from dictionary
    return None

def verify_word_ladder(ladder):
    '''
    Returns True if each entry of the input list is adjacent to its neighbors;
    otherwise returns False.
    '''

    if len(ladder) == 0:
        return False
    for i in range(len(ladder)-1):
        if not _adjacent(ladder[i],ladder[i+1]):
            return False
    return True

def _adjacent(word1, word2):
    '''
    Returns True if the input words differ by only a single character;
    returns False otherwise.

    >>> _adjacent('phone','phony')
    True
    >>> _adjacent('stone','money')
    False
    '''

    if len(word1) != len(word2):
        return False
    x=0
    for i in range(len(word1)):
        if word1[i] != word2[i]:
            x = x + 1
    if x==1:
        return True
    else:
        return False


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Sep 28 09:07:37 2018

@author: chakyu
"""

#---- Start Program ----

sentence = input('Please enter a sentence of your choice: ')

splitsentence = sentence.split(' ')

#number of names in the sentence
lastword = splitsentence[-1]

#print the number of words
print('The last word in the sentence is \'%s\'.' % lastword)

#---- End Program ----


import re

def sentence_splitter(file_name):
    with open(file_name, 'r') as f:
        file_content = f.read()

	# We first remove the newlines that were already there
    sentences = re.sub(r'\n', '', file_content)

	# Now we add a newline after each period only if that period is not
	# preceded by 'Mr', 'Mrs' or 'Dr' and is followed by a space and an
	# uppercase letter
    sentences = re.sub(r'(?<!Mr)(?<!Mrs)(?<!Dr)\.\s([A-Z])', r'.\n\1', sentences)

 	# Then we do the same after every '!'
    sentences = re.sub(r'!\s', '!\n', sentences)

 	# Finally, we do the same after every '?'
    sentences = re.sub(r'\?\s', '?\n', sentences)

    print(sentences)

#test
sentence_splitter('splitter.txt')


#
# Copyright 2020 - Swiss Data Science Center (SDSC)
# A partnership between École Polytechnique Fédérale de Lausanne (EPFL) and
# Eidgenössische Technische Hochschule Zürich (ETHZ).
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Renku service worker."""
import os
from contextlib import contextmanager

import sentry_sdk
from rq import Worker
from sentry_sdk.integrations.rq import RqIntegration

from renku.core.errors import ConfigurationError, UsageError
from renku.ui.service.cache.config import REDIS_NAMESPACE
from renku.ui.service.config import SENTRY_ENABLED, SENTRY_SAMPLERATE
from renku.ui.service.jobs.queues import QUEUES, WorkerQueues
from renku.ui.service.logger import DEPLOYMENT_LOG_LEVEL, worker_log

if SENTRY_ENABLED:
    sentry_sdk.init(
        dsn=os.getenv("SENTRY_DSN"),
        environment=os.getenv("SENTRY_ENV"),
        traces_sample_rate=float(SENTRY_SAMPLERATE),
        integrations=[RqIntegration()],
    )

@contextmanager
def worker(queue_list):
    """Creates worker object."""

    def build_worker():
        """Build worker."""
        # NOTE: logging configuration has been moved to `.work(logging_level=)`
        worker_log.info(f"worker log level set to {DEPLOYMENT_LOG_LEVEL}")

        rq_worker = Worker(queue_list, connection=WorkerQueues.connection, log_job_description=False)
        worker_log.info("worker created")

        return rq_worker

    yield build_worker()

def check_queues(queue_list):
    """Check if listening on specified queues is possible."""
    for queue in queue_list:
        if queue and queue not in QUEUES:
            err_msg = "Invalid queue name: {}\n\nValid queue names: \n{}".format(queue, "\n".join(QUEUES))
            raise UsageError(err_msg)

def start_worker(queue_list):
    """Start worker."""
    q = [f"{REDIS_NAMESPACE}.{q.strip()}" for q in queue_list if q.strip()]
    check_queues(q)

    worker_log.info(f"working on queues: {q}")

    with worker(q) as rq_worker:
        worker_log.info("running worker")
        rq_worker.work(logging_level=DEPLOYMENT_LOG_LEVEL)

if __name__ == "__main__":
    queues = os.getenv("RENKU_SVC_WORKER_QUEUES")
    worker_log.info(f"working on queues: {queues}")

    if not queues:
        raise ConfigurationError(
            "Worker queues not specified. Please, set RENKU_SVC_WORKER_QUEUES environment variable."
        )

    start_worker([f"{REDIS_NAMESPACE}.{queue_name.strip()}" for queue_name in queues.strip().split(",")])


# coding=utf-8
from fractions import Fraction

from binary_tree import Tree
from model.constants import Constants
from model.expression import Expression
from model.stack import Stack

class Answer:
    def __init__(self):
        self.wronum = 0
    @staticmethod
    def answer_one(set_expression):
        wnum = 0
        if set_expression:
            for exp in set_expression:
                flag = Answer.answer_two(exp)
                if flag == 1:
                    set_expression.remove(exp)
                    wnum = wnum +1
        return wnum
    @staticmethod
    def answer_two(expression):
        an = Answer()
        stack = Stack()
        result = Fraction(0, 1)
        # testsuff = "1 1 chu 0 chu 1'1/5 chu 2 +".split(" ")
        suffix = Tree.infix_to_suffix(expression.get_expression())
        str = " ".join(suffix)
        # print(str)
        if not suffix:
            return
        is_num = None
        for item in suffix:
            try:
                is_num = True
                result = Answer.get_fraction(item)
            except Exception:
                is_num = False
            if is_num:
                stack.push(result)
            else:
                flag = {
                    '+': 1,
                    '-': 2,
                    'x': 3,
                    '÷': 4,
                }.get(item, 5)
                if flag == 1:
                    a = stack.pop()
                    b = stack.pop()
                    stack.push(a + b)
                elif flag == 2:
                    a = stack.pop()
                    b = stack.pop()
                    stack.push(b - a)
                elif flag == 3:
                    a = stack.pop()
                    b = stack.pop()
                    stack.push(a * b)
                elif flag == 4:
                    a = stack.pop()
                    b = stack.pop()
                    if a == 0:
                        print("you 0")
                        an.wronum = 1
                        break
                    stack.push(b / a)
                elif flag == 5:
                    pass
        expression.set_fraction(stack.peek())
        expression.set_value(stack.peek())
        return an.wronum

    @staticmethod
    def get_fraction(item):
        result = 0
        if item.find(Constants.VIRGULE) > 0:
            attach = 0
            right = ""
            if item.find(Constants.SINGLE_QUOTE) > 0:
                parts = item.split(Constants.SINGLE_QUOTE)
                attach = int(parts[0])
                right = parts[1]
            else:
                right = item

            parts = right.split(Constants.VIRGULE)
            return Fraction(attach * int(parts[1]) + int(parts[0]), int(parts[1]))
        else:
            result = int(item);
            return Fraction(result, 1)

if __name__ == '__main__':
    ans = Answer()
    exp = Expression("1 + 2 - 5")
    ans.answer_two(exp)
    print(exp.tostring())


from django.conf.urls import url

urlpatterns = [
    url(r'^$', 'booking.views.index',
        name='booking_index'),
    url(r'^profile$', 'booking.views.profile',
        name='booking_profile'),
    url(r'^add_traveler$', 'booking.views.add_traveler',
        name='booking_add_traveler'),
    url(r'^search_flight$', 'booking.views.search_flight',
        name='booking_search_flight'),
    url(r'^book/(?P<flight_id>\d+)$', 'booking.views.book',
        name='booking_book'),
    url(r'^confirm_booking/(?P<flight_id>\d+)$',
        'booking.views.confirm_booking',
        name='booking_confirm_booking'),

]


import os

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from scipy import stats
import seaborn as sns
import tensorflow as tf

import helpers

def train_model(logfc_train_set, logfc_test_set, train_set, test_set, inference_func, params, logdir=None):

    if logdir is not None:
        if not os.path.isdir(logdir):
            os.makedirs(logdir)

    # reset and build the neural network
    tf.reset_default_graph()
    sess = tf.Session(config=tf.ConfigProto(intra_op_parallelism_threads=24))

    # if indicated, create directories to save the model
    if logdir is not None:
        SAVE_PATH = os.path.join(logdir, 'saved')
        VAR_PATH = os.path.join(logdir, 'vars')

        if not os.path.isdir(SAVE_PATH):
            os.makedirs(SAVE_PATH)

        if not os.path.isdir(VAR_PATH):
            os.makedirs(VAR_PATH)

    # create placeholders for data
    x = tf.placeholder(tf.float32, shape=[None, 16 * params['MIRLEN'] * params['SEQLEN']], name='x')
    y = tf.placeholder(tf.float32, shape=[None, params['OUT_NODES']], name='y')
    x_image = tf.reshape(x, [-1, 4*params['MIRLEN'], 4*params['SEQLEN'], 1])
    keep_prob = tf.placeholder(tf.float32, name='keep_prob')

    # do inference step
    inference_results = inference_func(x_image, y, keep_prob, params)
    train_step, loss, prediction, var_dict, train_step_logfc, loss_logfc, prediction_logfc = inference_results

    merged = tf.summary.merge_all()
    saver = tf.train.Saver()
    var_saver = tf.train.Saver(var_dict)

    # get kd test data
    test_labels = test_set[['log_kd']].values
    test_features = np.zeros((len(test_set), 16 * params['MIRLEN'] * params['SEQLEN']))
    test_colors = list(test_set['color'])
    for i, (seq, mirseq) in enumerate(zip(test_set['seq'], test_set['mirseq'])):
        mirseq = mirseq[-params['MIRLEN']:]
        test_features[i,:] = helpers.make_square(mirseq, seq).flatten()

    # set up kd train data
    if params['TRAIN_KD']:
        train_set_temp = train_set.copy()
        train_set_temp['keep'] = [int(np.random.random() > 0.75) if x == 'grey' else 1 for x in train_set_temp['color']]
        train_set_temp = train_set_temp[train_set_temp['keep'] == 1]

    # get repression test data
    logfc_test_set_features = np.zeros((len(logfc_test_set), 16*params['MIRLEN']*params['SEQLEN']))
    logfc_test_set_colors = []
    logfc_test_set_labels = logfc_test_set[['logFC']].values

    for i, (mirseq, seq) in enumerate(zip(logfc_test_set['mirseq'], logfc_test_set['seq'])):
        mirseq = mirseq[-params['MIRLEN']:]
        logfc_test_set_features[i, :] = helpers.make_square(mirseq, seq).flatten()
        sitem8 = helpers.complementaryT(mirseq[-8:-1])
        logfc_test_set_colors.append(helpers.get_color_old(sitem8, seq))

    logfc_test_set['color'] = logfc_test_set_colors

    # set up repression train data
    if params['TRAIN_LOGFC']:
        logfc_train_set_temp = logfc_train_set.copy()

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

    # if indicated, restore from previous model
    if params['RESTORE_FROM'] is not None:
        latest = tf.train.latest_checkpoint(params['RESTORE_FROM'])
        print('Restoring from {}'.format(latest))
        var_saver.restore(sess, latest)

    # train epochs and record performance
    sample_counter = 0
    batch_counter = 0
    epoch_counter = 0
    num_batches = []

    train_losses = []
    test_losses = []

    logfc_train_losses = []
    logfc_test_losses = []

    # must train on at least one dataset
    if not (params['TRAIN_KD'] or params['TRAIN_LOGFC']):
        raise ValueError('Either TRAIN_KD or TRAIN_LOGFC must be True.')

    while epoch_counter < params['MAX_EPOCH']:
        # train on kd data
        if params['TRAIN_KD']:

            # reset data if we finish an epoch
            if len(train_set_temp) < params['BATCH_SIZE']:
                train_set_temp = train_set.copy()
                train_set_temp['keep'] = [int(np.random.random() > 0.75) if x == 'grey' else 1 for x in train_set_temp['color']]
                train_set_temp = train_set_temp[train_set_temp['keep'] == 1]
                epoch_counter += 1

            # get batch
            subset = np.random.choice(list(train_set_temp.index), size=params['BATCH_SIZE'], replace=False)
            train_group = train_set_temp.loc[subset]
            train_set_temp = train_set_temp.drop(subset)

            train_labels = train_group[['log_kd']].values
            train_colors = list(train_group['color'].values)
            train_features = np.zeros((len(train_colors), 16 * params['MIRLEN'] * params['SEQLEN']))
            for i, row in enumerate(train_group.iterrows()):
                mirseq = row[1]['mirseq'][-params['MIRLEN']:]
                train_features[i, :] = helpers.make_square(mirseq, row[1]['seq']).flatten()

            # train step
            _ = sess.run(train_step, feed_dict={x: train_features,
                                                keep_prob: params['KEEP_PROB_TRAIN'],
                                                y: train_labels})
        # train on logfc data
        if params['TRAIN_LOGFC']:

            # reset data if we finish an epoch
            if len(logfc_train_set_temp) < params['LOGFC_BATCH_SIZE']:
                logfc_train_set_temp = logfc_train_set.copy()

            # get batch
            subset = list(np.random.choice(list(logfc_train_set_temp.index), size=params['LOGFC_BATCH_SIZE'], replace=False))
            logfc_train_set_group = logfc_train_set_temp.loc[subset]
            logfc_train_set_temp = logfc_train_set_temp.drop(subset)

            logfc_train_set_batch_labels = logfc_train_set_group[['logFC']].values
            logfc_train_set_batch_colors = list(logfc_train_set_group['color'].values)
            logfc_train_set_batch_features = np.zeros((len(logfc_train_set_batch_colors), 16 * params['MIRLEN'] * params['SEQLEN']))

            for i, row in enumerate(logfc_train_set_group.iterrows()):
                mirseq = row[1]['mirseq'][-params['MIRLEN']:]
                logfc_train_set_batch_features[i, :] = helpers.make_square(mirseq, row[1]['seq']).flatten()

            # train step
            _ = sess.run(train_step_logfc, feed_dict={x: logfc_train_set_batch_features,
                                                      keep_prob: params['KEEP_PROB_TRAIN'],
                                                      y: logfc_train_set_batch_labels})

        # increment batch counter
        batch_counter += 1

        # every few batches, record the loss
        if batch_counter%params['REPORT_INT'] == 0:

            test_loss, test_prediction = sess.run([loss, prediction], feed_dict={x: test_features,
                                                                                keep_prob: 1.0,
                                                                                y: test_labels})

            logfc_test_loss, logfc_test_set_prediction = sess.run([loss_logfc, prediction_logfc],
                                                                   feed_dict={x: logfc_test_set_features,
                                                                               keep_prob: 1.0,
                                                                               y: logfc_test_set_labels
                                                                               })

            logfc_test_losses.append(logfc_test_loss)
            test_losses.append(test_loss)
            num_batches.append(batch_counter)

            # if indicated, plot the loss and performance and save the model
            if logdir is not None:

                # save the model
                saver.save(sess, os.path.join(SAVE_PATH, 'model'), global_step=batch_counter)
                var_saver.save(sess, os.path.join(VAR_PATH, 'model'))

                # Plot kd loss over time
                fig = plt.figure()
                plt.plot(num_batches, test_losses)
                plt.xlabel('Num batches')
                plt.ylabel('Kd loss')
                plt.savefig(os.path.join(logdir, 'test_loss.pdf'))
                plt.close()

                # Plot repression loss over time
                fig = plt.figure()
                plt.plot(num_batches, logfc_test_losses)
                plt.xlabel('Num batches')
                plt.ylabel('Regression loss')
                plt.savefig(os.path.join(logdir, 'logfc_test_loss.pdf'))
                plt.close()

                # plot kd test predictions
                helpers.graph_predicted_v_actual(params['NCOLS'], params['OUT_NODES'], test_prediction, test_labels,
                                                test_colors, os.path.join(logdir, 'scatter_test.pdf'),
                                                log_scale=params['LOG_SCALE'])

                # plot regression test predictions
                helpers.graph_predicted_v_actual(params['NCOLS'], params['OUT_NODES'], logfc_test_set_prediction,
                                                np.repeat(logfc_test_set_labels, params['OUT_NODES'], axis=1),
                                                logfc_test_set_colors, os.path.join(logdir, 'scatter_test_logfc.pdf'),
                                                log_scale=params['LOG_SCALE'])

                if params['TRAIN_KD']:
                    train_loss, train_prediction = sess.run([loss, prediction], feed_dict={x: train_features,
                                                                                keep_prob: params['KEEP_PROB_TRAIN'],
                                                                                y: train_labels})

                    helpers.graph_predicted_v_actual(params['NCOLS'], params['OUT_NODES'], train_prediction, train_labels,
                                                train_colors, os.path.join(logdir, 'scatter_train.pdf'),
                                                log_scale=params['LOG_SCALE'])

                if params['TRAIN_LOGFC']:
                    logfc_train_loss, logfc_train_batch_prediction = sess.run([loss_logfc, prediction_logfc],
                                                                              feed_dict={x: logfc_train_set_batch_features,
                                                                                keep_prob: 1.0,
                                                                                y: logfc_train_set_batch_labels})

                    helpers.graph_predicted_v_actual(params['NCOLS'], params['OUT_NODES'], logfc_train_batch_prediction,
                                                    np.repeat(logfc_train_set_batch_labels, params['OUT_NODES'], axis=1),
                                                    logfc_train_set_batch_colors, os.path.join(logdir, 'scatter_train_logfc.pdf'),
                                                    log_scale=params['LOG_SCALE'])

                conv_weights = tf.get_collection('weight', scope='conv4x4')
                conv_weights = sess.run(conv_weights)[0]
                xlabels = ['U','A','G','C']
                ylabels = ['A','U','C','G']
                helpers.graph_convolutions(conv_weights, xlabels, ylabels, os.path.join(logdir, 'convolution1.pdf'))

                # conv_weights = tf.get_collection('weight', scope='convlayer2')
                # conv_weights = sess.run(conv_weights)[0]
                # helpers.graph_convolutions(conv_weights, None, None, os.path.join(logdir, 'convolution2.pdf'))

                # conv_weights = tf.get_collection('weight', scope='convlayer3')
                # conv_weights = sess.run(conv_weights)[0]
                # helpers.graph_convolutions(conv_weights, None, None, os.path.join(logdir, 'convolution3.pdf'))

                # conv_weights = tf.get_collection('weight', scope='fullyconnected')
                # conv_weights = sess.run(conv_weights)[0]
                # dim = conv_weights.shape
                # fig = plt.figure(figsize=(dim[1], dim[0]))

                # sns.heatmap(conv_weights, cmap=plt.cm.bwr)
                # plt.savefig(os.path.join(logdir, 'convolution_fullyconnected.pdf'))
                # plt.close()

            # do early stopping if test loss stops going down
            if len(logfc_test_losses) >= 20:
                if np.mean(logfc_test_losses[-10:]) >= np.mean(logfc_test_losses[-20:]):
                    sess.close()
                    return test_losses, logfc_test_losses, epoch_counter

    sess.close()
    return test_losses, logfc_test_losses, epoch_counter


import click
import requests

API_URL = "https://{language}.wikipedia.org/api/rest_v1/page/random/summary"

def random_page(language="en"):
  url = API_URL.format(language=language)

  try:
    with requests.get(url) as response:
      response.raise_for_status()
      return response.json()
  except requests.RequestException as error:
    message = str(error)
    raise click.ClickException(message)


'''
Evaluation for superpixel data in keras
'''

from resnet_keras import *
import numpy as np
from efficientnet_keras import *
from sklearn.metrics import f1_score
from sklearn.metrics import precision_score
import keras
import h5py
import sys
import h5py
import os
import sys

################################################################################

def evaluate_in_train_keras(model_name,model,num_epoch):
	output_directory='/home/pbu/Desktop/ganesh-samarth-fire-detection-copy'

	h5f = h5py.File("{}data_test.h5".format(output_directory), 'r')
	final_test_accuracy=list()
	final_f_score=list()
	final_precision=list()
	final_tpr=list()
	final_fnr=list()
	test_X = h5f['X']
	test_Y = h5f['Y']
	input_shape=[224,224,3]
	ypred=model.predict(test_X)
	print(ypred.shape)
	ypred=np.array(ypred)
	ypred_list=list(ypred[:,0])
	test_Y_act=list(test_Y[:,0])
	ypred_act=list()
	for k in ypred_list:
		if k>0.5:
			ypred_act.append(1)
		else:
			ypred_act.append(0)
	tpr=0
	p=0
	f=0
	fnr=0
	for j in range (0,len(ypred_act)):
		if ypred_act[j]==1 and test_Y_act[j]==1:
			tpr+=1
			p+=1
		if ypred_act[j]==1 and test_Y_act[j]==0:
			f+=1
		if ypred_act[j]==0 and test_Y_act[j]==1:
			p+=1
		if ypred_act[j]==0 and test_Y_act[j]==0:
			fnr+=1
			f+=1

	f_score=f1_score(list(test_Y[:,0]),ypred_act)
	pres=precision_score(list(test_Y[:,0]),ypred_act)

	final_f_score.append(f_score)
	final_precision.append(pres)
	final_tpr.append(tpr/p)
	final_fnr.append(fnr/f)
	final_test_accuracy.append((tpr+fnr)/(p+f))

	if num_epoch==0:
		acc_thresh=sum(final_test_accuracy)/len(final_test_accuracy)
		with open('accuracy_thresh.txt','w') as myfile:
			myfile.write(str(acc_thresh)+':'+sys.argv[1]+'_keras.h5'+':'+sys.argv[1])
			model.save('models_weights_with_eval/'+sys.argv[1]+'/'+sys.argv[1]+'_keras.h5')
			string_check=sys.argv[1]+'_'+'keras.h5'

			for item in os.listdir('models_weights_with_eval/'+sys.argv[1]):

				if string_check in item:

					if os.path.isdir('/home/pbu/fire-detection-cnn/models_best/'+sys.argv[1])==False:
						os.mkdir('/home/pbu/fire-detection-cnn/models_best/'+sys.argv[1])

					os.system('cp /home/pbu/fire-detection-cnn/models_weights_with_eval/'+sys.argv[1]+'/'+item+' '+'/home/pbu/fire-detection-cnn/models_best/'+sys.argv[1]+'/')

			myfile.close()

	else:

		with open('accuracy_thresh.txt','r') as myfile2:
				data=myfile2.read()
				print(data.split(':'))
				acc_thresh=float(data.split(':')[0])
				save_path=data.split(':')[1]
				model_name=data.split(':')[2]
				myfile2.close()

		if sum(final_test_accuracy)/len(final_test_accuracy)>acc_thresh:
			acc_thresh=sum(final_test_accuracy)/len(final_test_accuracy)
			best_model=model
			print(save_path)
			with open('accuracy_thresh.txt','w') as myfile3:
				myfile3.write(str(acc_thresh)+':'+save_path+':'+model_name)
			myfile3.close()
			best_model.save('models_weights_with_eval/'+model_name+'/'+save_path)
			print('saved model after epoch number:'+str(num_epoch))
			string_check=save_path
			for item in os.listdir('models_weights_with_eval/'+model_name):
				if string_check in item:
					os.system('cp /home/pbu/fire-detection-cnn/models_weights_with_eval/'+model_name+'/'+item+' '+'/home/pbu/fire-detection-cnn/models_best/'+model_name+'/')

	print('Test accuracy: '+ str(sum(final_test_accuracy)/len(final_test_accuracy)))
	print('Test f1_score: '+str(sum(final_f_score)/len(final_f_score)))
	print('Test precision_score: '+str(sum(final_precision)/len(final_precision)))
	print('Test tpr: '+ str(sum(final_tpr)/len(final_tpr)))
	print('Test fnr: '+str(sum(final_fnr)/len(final_fnr)))

	h5f.close()


#! /usr/bin/env python

"""
SYNOPSIS

    ./pak-viz.py --help

    ./pak-viz.py ExampleBuild my-dest-dir

    ./pak-viz.py ExampleBuild my-dest-dir --unreal_pak U:/bin/UnrealPak.exe

DESCRIPTION

Accepts an input and output directory.

Within the input directory, searches for an `.obb` or `.apk` file. In the case of the latter, searches
for an .obb as `assets/.obb.png` and process if found.

An `.pak` file is sought in the `.obb` 's `/ProjectName/Content/Paks/`, and if found, is processed through
`UnrealPak.exe`, and then a JSON tree is created of the directory structure, and added to a
d3 sunburst visualisation, which is then saved to the specified destination directory as `pak-viz.html`

"""

import argparse
import json
import os
import pathlib
import re
import shutil
import subprocess
import sys
import tempfile
import zipfile

EXTRACTED_OBB_DIR = tempfile.mkdtemp()
EXTRACTED_PAK_DIR = tempfile.mkdtemp()
EXTRACTED_APK_DIR = tempfile.mkdtemp()

HTML_FILENAME = 'pak-viz.html'
HTML_DATA_PLACEHOLDER = 'const root = {};'
HTML_BUILD_DIRECTORY_PLACEHOLDER = 'Build Directory:
'
HTML_TOTAL_BUILD_SIZE_PLACEHOLDER = ''

parser = argparse.ArgumentParser(
    description="Visualise the filesystem of a UE4 pak file")

parser.add_argument("input", type=str, help="Directory of the build to scan")

parser.add_argument("output", type=str,
                    help="Destination directory for the generated file")

parser.add_argument("--unreal_pak", type=str, default="UnrealPak.exe",
                    help="Path to UnrealPak.exe - defaults to just the filename")

args = parser.parse_args()

input_location = os.path.abspath(args.input)
output_dir = os.path.abspath(args.output)
html_dest_path = output_dir + os.sep + HTML_FILENAME

def main():
    if (not(os.path.isdir(input_location))):
        print('Supplied input directory not found: ' + input_location)
        sys.exit(1)

    if (not(os.path.isdir(output_dir))):
        os.mkdir(output_dir)

    obb_file_path = get_obb_file_path(input_location)

    if obb_file_path: # Android build
        with zipfile.ZipFile(obb_file_path, 'r') as zipObj:
            zipObj.extractall(EXTRACTED_OBB_DIR)
        pak_path = find_pak_path(EXTRACTED_OBB_DIR)
    else: # Windows build
        pak_path = find_pak_path(input_location)

    if not(pak_path):
        print("Unable to find an apk, obb or windows pak file\n")
        sys.exit(-9)

    subprocess.run([args.unreal_pak, pak_path, '-extract', EXTRACTED_PAK_DIR])

    theTree = tree(EXTRACTED_PAK_DIR)

    create_file(theTree)

    shutil.copyfile('rewind_banner.png', output_dir + os.sep + 'rewind_banner.png')

    print('\nFinished after writing ' + html_dest_path)

def tree(dir):
    match = re.search('([^/\\\\]+)[/\\\\]*$', dir)
    label = match.group(0)

    if dir == EXTRACTED_PAK_DIR:
        label = 'Package'

    branch = {
        "name": label,
        "children": []
    }

    for file in os.listdir(dir):
        path = dir + os.sep + file
        if os.path.isdir(path):
            branch["children"].append(tree(path))
        else:
            branch["children"].append(
                {"name": file, "size": os.path.getsize(path)})

    return branch

def create_file(theTree):
    with open(HTML_FILENAME) as html_file:
        html = html_file.read()

    html = html.replace(HTML_DATA_PLACEHOLDER, "const root = " +
                        json.dumps(theTree) + ";")

    html = html.replace(HTML_BUILD_DIRECTORY_PLACEHOLDER,
        'Build Directory: ' + input_location +  '
')

    if os.path.isfile(html_dest_path):
        os.remove(html_dest_path)

    with open(html_dest_path, 'w') as combined_file:
        combined_file.write(html)

def get_obb_path_from_apk(apk_path):
    with zipfile.ZipFile(apk_path, 'r') as zipObj:
        zipObj.extractall(EXTRACTED_APK_DIR)
    obb_path = EXTRACTED_APK_DIR + os.sep + 'assets' + os.sep + 'main.obb.png'
    if not(os.path.isfile(obb_path)):
        sys.stderr.write(
            'Did not find assets/.obb.png in ' + apk_path + '\n')
        obb_path = ''
    return obb_path

def get_obb_file_path(input_path):
    obb_file_path = ''

    print('searching ' + input_path)

    if os.path.isfile(input_path):
        obb_file_path = find_input_file(input_path)
    else:
        for element in os.listdir(input_path):
            print('- ' + element)
            full_element_path = input_path + os.sep + element
            if os.path.isfile(full_element_path):
                obb_file_path = find_input_file(full_element_path)
            else:
                obb_file_path = get_obb_file_path(full_element_path)

            if obb_file_path:
                break

    return obb_file_path

def find_input_file(path):
    obb_file_path = ''
    if os.path.isfile(path):
        if re.search('\\.apk$', path):
            obb_file_path = get_obb_path_from_apk(path)
            if obb_file_path:
                print('Extracting .obb.png from apk', path)
        elif re.search('\\.obb$', path):
            obb_file_path = path
            print('Found an obb outside of an apk', path)

    return obb_file_path

def find_pak_path(obb_dir):
    pak_path = ''
    for root, dirs, files in os.walk(obb_dir):
        for file in files:
            if file.endswith(".pak"):
                pak_path = os.path.join(root, file)

    if pak_path:
        print('Using pak file', pak_path)

    return pak_path

main()


import sys
from statistics import mean

def main():
    assert len(sys.argv) > 1, "No input file path specified."

    input_file_path = sys.argv[1]
    print(f"Processing input file: {input_file_path}")

    with open (input_file_path, "r") as f:
        for inputs in f:
            inputs = inputs.strip("\n")
            inputs_ting = inputs.split(",")
            minah = []
            for x in inputs_ting:
                minah.append(float(x))
                average = mean(minah)
            print(f"{average}")

if __name__ == "__main__":
    main()


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

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    url(r'^stats/$', 'stats.views.stats_index', name='index'),
    url(r'^stats/(.*)$', 'stats.views.stats_home'),
    url(r'^stats_markdown/(.*)$', 'stats.views.stats_markdown'),
    url(r'^game/(.*)$', 'stats.views.stats_game'),
    url(r'^$', 'mjapp.views.home'),
    url(r'^api/new_game/([^/]+)/([^/]+)', 'stats.views.api_new_game'),
    url(r'^api/new_game/([^/]+)', 'stats.views.api_new_game'),
)


"""API viewsets for the groups app."""

from django.db.models import Prefetch
from django.utils.decorators import method_decorator
from django.views.decorators.cache import cache_control

from rest_framework.parsers import MultiPartParser
from rest_framework.viewsets import ModelViewSet

from api.v2.mixins import METHODS, CORSMixin
from api.v2.schema import OpenAPISchema

from . import models, serializers

@method_decorator(cache_control(public=True, max_age=7200), 'dispatch')
class GroupViewSet(CORSMixin, ModelViewSet):
    """
    API endpoints for groups.

    * list: List groups.
    * read: View a certain group.
    * create: Create a new group.
    * patch: Edit the given group.
    * delete: Delete the given group.
    """
    schema = OpenAPISchema(tags=('groups',))
    queryset = models.Group.objects.prefetch_related(
        Prefetch('roles', queryset=(
            models.Role.objects.only(
                'member__name', 'role', 'group_id'
            ).select_related('member')
        ))
    )
    serializer_class = serializers.GroupSerializer
    parser_classes = (MultiPartParser,)
    http_method_names = METHODS

@method_decorator(cache_control(public=True, max_age=10800), 'dispatch')
class MemberViewSet(CORSMixin, ModelViewSet):
    """
    API endpoints for members.

    * list: List members.
    * read: View a certain member.
    * create: Create a new member.
    * patch: Edit the given member.
    * delete: Delete the given member.
    """
    schema = OpenAPISchema(tags=('members',))
    queryset = models.Member.objects.prefetch_related(
        Prefetch('roles', queryset=(
            models.Role.objects.only('role', 'group_id')
        ))
    )
    serializer_class = serializers.MemberSerializer
    parser_classes = (MultiPartParser,)
    http_method_names = METHODS

__all__ = ['GroupViewSet', 'MemberViewSet']


from django.shortcuts import render
import requests
import xml.etree.ElementTree
from lxml import html, etree

# func to finding the elements before a slice and return slice before it
def before(value, a):
    # Find first part and return slice before it.
    pos_a = value.find(a)
    if pos_a == -1: return ""
    return value[0:pos_a]

def ActivityDateType(request):
    return render(request, 'xmlapp/ActivityDateType.html')

def xmllist(request):
    xmlObj = []

    # scraping the file names from the repo
    url = ("https://github.com/IATI/IATI-Codelists/tree/version-2.03/xml")

    r = requests.get(url)
    data = r.text

    # xmlCount = data.count('.xml" id')
    nameList = []
    xmlFileNames = []

    dataSplits = data.split('open" title="')

    # delete first generated data split as it doesn't contain an xml list.
    del dataSplits[0]

    # trim trailing text from each in dataSplits and appending into nameList
    for i in dataSplits:
        try:
            nameList.append(before(i, '.xml" id='))
        except TypeError:
            nameList.append('no string found')

    # making a list the file names
    for name in nameList:
        xmlFileNames.append(name + ''.join('.xml'))

    ##### loop each file name to create urls and running them through the parser.
    for xmlFileName in xmlFileNames:
        xmlurl = ("https://raw.githubusercontent.com/IATI/IATI-Codelists/version-2.03/xml/" + ''.join(xmlFileName))

        # testurl = ("https://raw.githubusercontent.com/IATI/IATI-Codelists/version-2.03/xml/DocumentCategory.xml")

        # response = requests.get(xmlurl)
        response = requests.get(testurl)
        root = etree.fromstring(response.content)

        metaData = root.find("./metadata")
        codeListName = metaData.findtext("./name/narrative")
        codeListDesc = metaData.findtext("./description/narrative")

        codeListItems = root.find("./codelist-items")
        codeListItem = codeListItems.find("./codelist-item")

        ########

        # iterate over all item
        for codeListItem in codeListItems.findall('codelist-item'):

            # access element - name
            codeListItemName = codeListItem.findtext('./name/narrative')
            # print('list item : ', codeListItemName)

            # access element - description
            codeListItemDesc = codeListItem.findtext(
                './description/narrative')
            # print('item description : ', codeListItemDesc)

        myObj = {
            'codeListName': codeListName,
            'codeListDesc': codeListDesc,
            'codeListItemName': codeListItemName,
            'codeListItemDesc': codeListItemDesc,
            }

        xmlObj.append(myObj)

    return render(request, 'xmlapp/xmllist.html')

def index(request):
    return render(request, 'xmlapp/index.html')


import numpy as np
import cv2

# 读图
img = cv2.imread('../../Picture/water.jpg', 1)

# 展示图
cv2.imshow('src', img)

# 获取图片信息
imgInfo = img.shape  # (170, 361, 3) 170*361矩阵 [宽度, 高度, 图片的颜色组成方式]
height = imgInfo[0]  # 矩阵的行数代表图片的高度
width = imgInfo[1]   # 矩阵的列数代表图片的宽度

# 设置一个偏移矩阵 两行三列数据  含义:沿着x轴向右移动了100，沿着y轴向下移动了0
matShift = np.float32([[1, 0, 100], [0, 1, 0]])  # 2*3

# 目标图片warpAffine，完成矩阵的映射。
# 参数1: 原图片 参数2: 移位矩阵 参数3: 当前图片的高宽
dst = cv2.warpAffine(img, matShift, (width, height))  # 1 data 2 matshift 3 info
cv2.imshow('dst', dst)

# 源码实现图片移位
dstt = np.zeros(imgInfo, np.uint8)
for i in range(0, height):
    for j in range(0, width - 100):
        dstt[i, j + 100] = img[i, j]
cv2.imshow("dstt", dstt)

print("img.shape:", img.shape)
print("dst.shape:", dst.shape)
print("dstt.shape:", dstt.shape)

cv2.waitKey(0)


from PySide2.QtCore import QCoreApplication
from PySide2.QtGui import QKeySequence, QClipboard
from PySide2.QtWidgets import QMenu

from node_launcher.constants import BITCOIN_CLI_COMMANDS, LNCLI_COMMANDS
from node_launcher.gui.system_tray_widgets import (
    AdvancedWidget,
    BitcoindOutputWidget,
    ConsoleDialog,
    LndOutputWidget,
    SettingsTabDialog
)
from node_launcher.gui.utilities import reveal
from node_launcher.node_set import NodeSet

class Menu(QMenu):
    def __init__(self, node_set: NodeSet, system_tray):
        super().__init__()
        self.node_set = node_set
        self.system_tray = system_tray

        self.bitcoind_status_action = self.addAction('bitcoind off')
        self.bitcoind_status_action.setEnabled(False)

        # bitcoin console
        self.bitcoin_cli_widget = ConsoleDialog(
            title='bitcoin-cli',
            program=self.node_set.bitcoin.software.bitcoin_cli,
            args=self.node_set.bitcoin.args,
            commands=BITCOIN_CLI_COMMANDS
        )
        self.bitcoin_console_action = self.addAction('Open Bitcoin Console')
        self.bitcoin_console_action.triggered.connect(
            self.bitcoin_cli_widget.show
        )

        # bitcoind output
        self.bitcoind_output_widget = BitcoindOutputWidget(
            node_set=self.node_set,
            system_tray=self.system_tray
        )
        self.bitcoind_output_action = self.addAction('See Bitcoin Output')
        self.bitcoind_output_action.triggered.connect(
            self.bitcoind_output_widget.show
        )

        self.addSeparator()

        self.lnd_status_action = self.addAction('lnd off')
        self.lnd_status_action.setEnabled(False)

        # lnd console

        self.lncli_widget = ConsoleDialog(
            title='lncli',
            program=self.node_set.lnd.software.lncli,
            args=self.node_set.lnd.lncli_arguments(),
            commands=LNCLI_COMMANDS
        )
        self.lnd_console_action = self.addAction('Open LND Console')
        self.lnd_console_action.triggered.connect(
            self.lncli_widget.show
        )

        # lnd output
        self.lnd_output_widget = LndOutputWidget(
            node_set=self.node_set,
            system_tray=self.system_tray
        )
        self.lnd_output_action = self.addAction('See LND Output')
        self.lnd_output_action.triggered.connect(
            self.lnd_output_widget.show
        )

        self.addSeparator()

        # Joule

        self.joule_status_action = self.addAction('Joule Browser Extension')
        self.joule_status_action.setEnabled(False)
        self.joule_url_action = self.addAction('Copy Node URL (REST)')
        self.joule_macaroons_action = self.addAction('Show Macaroons')

        self.joule_url_action.triggered.connect(
            lambda: QClipboard().setText(self.node_set.lnd.rest_url)
        )

        self.joule_macaroons_action.triggered.connect(
            lambda: reveal(self.node_set.lnd.macaroon_path)
        )

        self.addSeparator()

        # settings

        self.settings_action = self.addAction('&Settings')
        self.settings_action.setShortcut(QKeySequence.Preferences)
        self.settings_tab = SettingsTabDialog(node_set=self.node_set)
        self.settings_action.triggered.connect(self.settings_tab.show)

        # advanced

        self.advanced_widget = AdvancedWidget(node_set=self.node_set)
        self.advanced_action = self.addAction('Advanced...')
        self.advanced_action.triggered.connect(self.advanced_widget.show)

        self.addSeparator()

        # quit
        self.quit_action = self.addAction('Quit')

        self.quit_action.triggered.connect(
            lambda _: QCoreApplication.exit(0)
        )


VOWELS = ['a', 'i', 'y', 'e', 'o', 'u']
CONS = ['b', 'k', 'x', 'z', 'n', 'h', 'd', 'c', 'w', 'g', 'p', 'v', 'j', 'q', 't', 's', 'r', 'l', 'm', 'f']

def tongues(code):
    encoded = ""
    for sym in code:
        if sym.lower() in VOWELS:
            encoded += encode(sym, VOWELS, 3)
        elif sym.lower() in CONS:
            encoded += encode(sym, CONS, 10)
        else:
            encoded += sym
    return encoded

def encode(symbol: "string", state: "VOWEL, CONS", delay):
    encoded = ''
    index = state.index(symbol.lower())
    encoded_sym = state[(index - delay) % len(state)]
    if symbol.isupper():
        encoded = encoded_sym.upper()
    else:
        encoded = encoded_sym
    return encoded

print(tongues('Ita dotf ni dyca nsaw ecc.'))


import sys

N, M = list(map(int, sys.stdin.readline().split()))

lst = [[0] * (N + 1)]

for _ in range(N):
    ls = [0] + [int(x) for x in sys.stdin.readline().split()]
    lst.append(ls)

for i in range(1, N + 1):
    for j in range(1, N):
        lst[i][j + 1] += lst[i][j]
for j in range(1, N + 1):
    for i in range(1, N):
        lst[i + 1][j] += lst[i][j]

for _ in range(M):
    x1, y1, x2, y2 = map(int, sys.stdin.readline().split())
    print(lst[x2][y2] - lst[x1 - 1][y2] - lst[x2][y1 - 1] + lst[x1 - 1][y1 - 1])


# -*- coding: utf-8 -*-
"""
Created on Sun May 17 15:03:08 2020

@author: JAE6KOR
"""

import os
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
from scipy import stats

upvotes_data_loc = r'D:\Seaborn tutorial\Predict number of upvotes'
train_data = pd.read_csv(os.path.join(upvotes_data_loc, 'train_data.csv'))

#df = train_data.iloc[:1000]
df = train_data.copy()
#%%

"data pre-processing"
summary = df.describe()

"missing data summary"
summary_na= [df[x].isna().sum() for x in df.columns] # we don't have any nan values in this data
#%%
"find correlation matrix"
df_corr = df.corr()

"generating heatmap using df_corr"
sns.heatmap(df_corr) # we don't see any var-pair haivng > 0.5 correlation, hence we'll be directly going for model building
#%%
"splitting train-test data"
from sklearn.model_selection import train_test_split

"Converting categorical str to continuous form usig dummy variables"
df1 = pd.concat([df.loc[:, df.columns!='Tag'], pd.get_dummies(df[['Tag']])], axis=1)

input_features = df1.drop(columns=['ID','Username', 'Upvotes']).values
target = df1['Upvotes'].values

X_train, X_val, y_train, y_val = train_test_split(input_features, target, random_state=10)

"ML models"
# we'll try 6 different models to get best model with best hyper-parameters
# will use gridsearchcv o randomizedsearchcv for parameter optimization

from sklearn.preprocessing import PolynomialFeatures
from sklearn.neighbors import KNeighborsRegressor
from sklearn.ensemble import RandomForestRegressor
from sklearn.linear_model import LassoLars
from xgboost import XGBRegressor
from sklearn.svm import SVR

poly5 = PolynomialFeatures(5)

llr = LassoLars()
knn = KNeighborsRegressor()
rfr = RandomForestRegressor()
xgbr = XGBRegressor()
svr = SVR()

from sklearn.model_selection import GridSearchCV, RandomizedSearchCV
from sklearn.metrics import mean_squared_error as mse

models = [llr, knn, svr, rfr, xgbr]
grid_params = {llr: {'alpha':[0,0.005,0.01,0.02,0.05,0.1,0.2,0.5,1,2,5,10]},
                 knn:{'n_neighbors':[1,3,5,11,15,17,20,30,40,50,70],'weights':['uniform','distance'],'metric':['euclidean','manhattan']},
                 svr:{'C': [0.1, 1, 10, 100, 1000],'epsilon': [0.0001, 0.0005, 0.001, 0.005, 0.01, 0.05, 0.1, 0.5, 1, 5, 10],
                         'gamma': [1, 0.1, 0.01, 0.001, 0.0001],'kernel': ['linear', 'poly', 'rbf', 'sigmoid']},
                  rfr: {'n_estimators': [int(x) for x in np.linspace(start = 10, stop = 200, num = 10)],# Number of trees in random forest
                                                 'max_features': ['auto', 'sqrt',None],# Number of features to consider at every split
                                                 'max_depth': [int(x) for x in np.linspace(10, 110, num = 11)]+[None],# Maximum number of levels in tree
                                                 'min_samples_split': [2, 5, 10],# Minimum number of samples required to split a node
                                                 'min_samples_leaf': [1, 2, 4],# Minimum number of samples required at each leaf node
                                                 'bootstrap': [True, False]},# Method of selecting samples for training each tree
                 xgbr:{'eta':[0.1,0.3,0.5,0.7],'gamma':[0,10,100],'max_depth':[1,3,5,9,11]}}

#%%
import time
scoring='neg_mean_squared_error'
cv_results_list = []
best_params_list = []
best_cv_rmse_list = []
val_rmse_list = []
train_time_list = []
for model in models[-1:]:
    num_iter = 1
    st = time.time()
#    model = GridSearchCV(knn, grid_params[knn], n_jobs=-1, scoring=scoring) # not performing gridsearchcv as it takes lot time
    total_param_combinations = np.prod([len(grid_params[model][par]) for par in list(grid_params[model].keys())])

    model = RandomizedSearchCV(estimator=model, n_iter=num_iter, cv=3, scoring=scoring,n_jobs=-1, param_distributions=grid_params[model], refit=True)

    if model==llr:
        X_train1 = poly5.fit_transform(X_train)
        model.fit(X_train1, y_train)
    else:
        model.fit(X_train, y_train)

    cv_results_list+=[model.cv_results_]

    best_cv_rmse1 = np.sqrt(-max(model.cv_results_['mean_test_score']))
    best_cv_rmse_list+=[best_cv_rmse1]

    best_params_list+=[model.best_params_]

    train_time1 = str(round(time.time()-st,2))+' seconds'
    train_time_list+=[train_time1]
    if model==llr:
        X_val1 = poly5.transform(X_val)
        y_pred = model.predict(X_val1)
    else:
        y_pred = model.predict(X_val)

    rmse1 = np.sqrt(mse(y_val,y_pred))

    val_rmse_list+=[rmse1]

    print('total_cv_time:',train_time1)
    print('avg_cv_rmse:',best_cv_rmse1)

    print('val_rmse:', rmse1)

#%%


#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Mon Jul  2 15:07:25 2018

@author: thomas
"""
#%%
# Loading packages
from scipy.optimize import curve_fit
import numpy as np
import cPickle as pickle
import matplotlib.pyplot as plt
import os
import re

#%%
plt.close("all")
folder = "data/single_DOM_hits_grid_1/"
files = os.listdir(folder)
#files = [folder+file for file in files]
files = [folder+file for file in files if "single" in file]

#%%
dps = 0.025
dpa = 0.005
Ps = np.arange(dps, 0.5+dps, dps)
Pa = np.arange(dpa, 0.1+dpa, dpa)

#Ps = np.arange(dps, 0.5, dps)
#Pa = np.arange(dps, 0.5, dps)

grid = np.zeros((Ps.shape[0], Pa.shape[0], 10))
for k in range(10):
    for i, ps in enumerate(Ps):
        for j, pa in enumerate(Pa):
    #        print [file for file in files if "pa_{}_ps_{}.p".format(pa, ps) in file][0]
            fname = [file for file in files if "pa_{}_ps_{}.p".format(pa, ps) in file][0]
            hits = np.loadtxt(fname).T
            mu = hits[:,k].mean(axis=0)
            tot = mu.sum()
            grid[i, j, k] = tot

#%%
fig, axes = plt.subplots(2, 5)

axes = axes.flatten()
for k in range(10):
    ax = axes[k]
    ax.imshow(grid[:,:,k])
    plt.sca(ax)
    plt.xticks(range(len(Pa)),Pa.round(4), size=8, rotation="vertical")
    plt.yticks(range(len(Ps)),Ps.round(4), size=8, rotation="horizontal")

#%%
#plt.figure()
#legend = []
#color_list = plt.cm.tab20c(np.linspace(0, 1, len(files)))
#for i, fname in enumerate(files):
#    hits = np.loadtxt(fname).T
#
#    P_absorb = float(re.findall(r"(?<=pa\_)0.\d*", fname)[0])
#    P_scatter = float(re.findall(r"(?<=ps\_)0.\d*", fname)[0])
#
#    mu = hits.mean(axis=0)
#    sig = hits.std(ddof=1, axis=0)
#
#    if len(hits.shape)>1:
#        N_doms = len(mu)
#    else:
#         N_doms = 1
#
#    c = color_list[i]
#    plt.errorbar(range(N_doms), mu, sig, fmt="-o", color=c)
##    plt.plot(range(10), mu, color=c)
#    plt.grid()
#    legend.append("pa_{}_ps_{}".format(P_absorb, P_scatter))
#
#plt.xlabel("DOM Number")
#plt.ylabel("Photon Hits")
#plt.legend(legend, prop={"size":12})


import numpy as np
import matplotlib.pyplot as plt

from teaming import logger
X=[i*100/(10000*16) for i in range(101)]
#frqs=[1,10,100,1000,10000]
#frqs=[1,2,3,4,6,8,12,16]
frqs=[1,2,3,4,5,6,7,8]
letter="qq"
data=[]
err=[]

log = logger.logger()
log.load("tests/evo121-0.pkl")

p=log.pull("position")
t=log.pull("types")
tst=log.pull("test")
print(tst)

tst=np.array(tst)
Tst=np.average(tst,axis=1)
tst=tst[-1]
print(tst)

poi=log.pull("poi")[0]
print(poi)

idx=3
nagents=5
pos=p

for idx1 in range(50):
    plt.ion()
    plt.clf()
    plt.subplot(1,2,1)
    vals=np.array([0.1,0.1,0.5,0.3,0.0,0.0])*200+10
    plt.scatter(poi[:,0],poi[:,1],s=vals)
    typ=t[0][idx]
    print(typ)
    for i in range(nagents):
        data=[]
        for j in range(len(pos)):
            p=pos[j][idx]
            data.append(p[i])
        data=np.array(data).T
        x,y=data
        tt=typ[i]
        color=['k','r','b','m','y'][tt]
        plt.plot(x,y,color)

    plt.title(str(idx)+':'+str(tst[idx]))

    plt.subplot(1,2,2)
    plt.plot(Tst)
    plt.pause(1.0)
plt.show()


import ROOT
from ROOT import gSystem
import root_numpy as rnp
import numpy as np
import os
import pickle
import rootpy.plotting
from rootpy.io import root_open

from dependencies import root_dependencies
root_dependencies()

# Energy bins function

#The function gets only one parameter: the filename
def energy_bins(filename):
    """
    Defines energy bins and calculates the number of events per bin for simulated and triggered events
    extracted from an input ROOT file; selects the minimum number of hits in the simulated events and
    the minimum number of hit DOMs per event.

    Parameters:
    -----------
    filename : str
        The single ROOT filename to process with extension .root

    Returns:
    --------
    MC_events_per_bin : np.ndarray
        number of simulated events per bin, as returned by `np.histogram`
    trig_events_per_bin : np.ndarray
        number of triggered events per bin, as returned by `np.histogram`
    sel_hits_events_per_bin : np.ndarray
            number of selected events per bin (nhits > min), as returned by `np.histogram`
    sel_doms_events_per_bin : np.ndarray
        number of selected events per bin (nDOMs > min), as returned by `np.histogram`
    energy_bins : np.ndarray
        energy bins boundaries, returned by `np.histogram`

    """
    # usual data extraction from the input file and conversion of the content of arrays from float64 to int64
    triggered_events_idxs = rnp.root2array(filename, treename="KM3NET_EVENT", branches="KM3NET_EVENT.getCounter()")
    triggered_events_idxs = triggered_events_idxs.astype(np.int64)
    #energy extraction from the MONTECARLO tree
    montecarlo_energies = rnp.root2array(filename, treename="MONTECARLO", branches="neutrino_.E_")

    #energies of the triggered events
    MC_triggered_energies = montecarlo_energies[triggered_events_idxs] # Fancy indexing here !-)

    #number of hits per event extraction from the input ROOT file
    montecarlo_numberofhits = rnp.root2array(filename, treename="MONTECARLO", branches="@HitList_.size()")
    montecarlo_numberofhits = montecarlo_numberofhits.astype(np.int64)
    # selection of the energies with number of hits > 5
    selection = 5
    sel_hits_energies = np.array([en for i, en in enumerate(montecarlo_energies) if montecarlo_numberofhits[i]>selection])

    #import the tree montecarlo eventnumber, to define the histogram and calculate the number of hit doms
    eventnumber = rnp.root2array(filename, treename='MONTECARLO', branches='eventNumber_')
    eventnumber = eventnumber.astype(np.int64)
    max_evn = np.int(np.max(eventnumber))
    ndoms = 2070

    #open the file with rootpy and get the tree 'MONTECARLO'
    root_file = root_open(filename)
    rpy_tree = root_file.MONTECARLO

    #create the histogram with number of doms and eventnumber
    h2d = rootpy.plotting.Hist2D(np.int(np.max(eventnumber)), np.min(eventnumber), np.max(eventnumber), ndoms, 0, ndoms, type="F")
    ret = rpy_tree.Draw('eventNumber_:HitList_.pm_id_/31', hist=h2d, create_hist=False)

    #extraction of the number of DOMs hit from the histogram. the for loop on the x axis in done on the eventnumber array, while the one on y is done on the doms
    count_doms = np.zeros(eventnumber.shape)
    for i, x in enumerate(eventnumber):
        count = 0
        ix = np.int(x)
        for y in range(ndoms):
            if h2d.GetBinContent(ix,y) > 0:
                count+=1
        if count > 0:
            count_doms[i] = count

    #select the events with number of doms > 5
    selected_idx = np.where(count_doms>selection)[0]

    #get the energies of the selected events
    sel_doms_energies = montecarlo_energies[selected_idx]

    #number of bins for the efficiency calculation
    nbins = 12
    #parameters definition
    #conversion of the energies to log
    log_minE = 2 #np.log10(np.int(np.min(montecarlo_energies)))
    log_maxE = 8 #np.log10(np.int(np.max(montecarlo_energies)))

    log_MC_en = np.log10(montecarlo_energies)
    log_trig_en = np.log10(MC_triggered_energies)
    log_sel_hits_en = np.log10(sel_hits_energies)
    log_sel_doms_en = np.log10(sel_doms_energies)
    #binned log energies calculation with np.histogram
    MC_events_per_bin, _ = np.histogram(log_MC_en, bins=nbins, range=(log_minE, log_maxE))
    trig_events_per_bin, energy_bins = np.histogram(log_trig_en, bins=nbins, range=(log_minE,log_maxE))
    sel_hits_events_per_bin, energy_bins = np.histogram(log_sel_hits_en, bins=nbins, range=(log_minE,log_maxE))
    sel_doms_events_per_bin, energy_bins = np.histogram(log_sel_doms_en, bins=nbins, range=(log_minE,log_maxE))

    return MC_events_per_bin, trig_events_per_bin, sel_hits_events_per_bin, sel_doms_events_per_bin, energy_bins


"""Middleware for adding CORS headers for API calls."""

import typing

import aiohttp.web
from asyncpg import UniqueViolationError

from .db import DBConn

AiohttpHandler = typing.Callable[
    [aiohttp.web.Request],
    typing.Coroutine[
        typing.Awaitable,
        typing.Any,
        aiohttp.web.Response
    ]
]

@aiohttp.web.middleware
async def add_cors(
        request: aiohttp.web.Request,
        handler: AiohttpHandler
) -> aiohttp.web.Response:
    """Add CORS header for API responses."""
    resp = await handler(request)
    if "origin" in request.headers.keys():
        resp.headers["Access-Control-Allow-Origin"] = request.headers["origin"]
    return resp

@aiohttp.web.middleware
async def check_db_conn(
        request: aiohttp.web.Request,
        handler: AiohttpHandler
) -> aiohttp.web.Response:
    """Check if an established database connection exists."""
    if request.path == "/health":
        return await handler(request)

    if (
            isinstance(request.app["db_conn"], DBConn)
            and request.app["db_conn"].conn is None
    ):
        raise aiohttp.web.HTTPServiceUnavailable(
            reason="No database connection."
        )
    return await handler(request)

@aiohttp.web.middleware
async def catch_uniqueness_error(
        request: aiohttp.web.Request,
        handler: AiohttpHandler
) -> aiohttp.web.Response:
    """Catch excepetion arising from a non-unique primary key."""
    try:
        return await handler(request)
    except UniqueViolationError:
        raise aiohttp.web.HTTPConflict(
            reason="Duplicate entries are not allowed."
        )


import os
import pickle
from datetime import *

import platform
import numpy as np
import tensorflow as tf
from util.load_as import Loader, INFO_LOG

from Config import Config
from util.model_saver import DynGCN_saver, DynGCN_loader
from util.LearningRateUpdater import LearningRateUpdater
from model.dynGCN_3hop import DynGCN
# from model.dynGCN import DynGCN
from util.evalutate import F1score
import time

def run(session, config, model, loader, verbose=False):
    total_cost = 0.

    num_ = 0.

    auc = 0.
    f1_score = F1score(model.batch_size)
    prediction_l = [0.] * model.batch_size
    prediction_n_l = [0.] * model.batch_size
    t_auc = 0.
    t_num = 0.

    def _add_list(x, y):
        for idx in range(len(x)):
            x[idx] += y[idx]
        return x

    time_consume = 0.
    feature_h0 = loader.last_embeddings()
    adj_now, delta_adj = loader.adj()
    for batch in loader.generate_batch_data(batchsize=model.batch_size, mode=model.mode):

        batch_id, batch_num, nodelist1, nodelist2, negative_list = batch

        if model.mode == "Train":
            feed = {
                model.input_x: nodelist1,
                model.input_y: nodelist2,
                model.adj_now: adj_now,
                model.delta_adj: delta_adj,
                model.feature_h0:feature_h0,
                model.negative_sample: negative_list
            }
            out = [model.cost, model.optimizer, model.auc_result,
                   model.auc_opt
                   # , model.prediction, model.prediction_n,
                   # model.test1, model.test2
                   ]

            output = session.run(out, feed)
            cost, _, auc, _ = output #, prediction, prediction_n, test1, test2

            # prediction_l = _add_list(prediction_l, prediction)
            # prediction_n_l = _add_list(prediction_n_l, prediction_n)

        if model.mode == "Valid":
            # print "nodelist1", np.asarray(nodelist1 * 2).shape
            # print np.asarray(nodelist2 + negative_list).shape
            # print np.asarray([1] * (model.batch_size / 2) + [0] * (model.batch_size /2)).shape

            feed = {
                model.input_x: np.asarray(nodelist1 * 2),
                model.input_y: np.asarray(nodelist2 + negative_list),
                model.adj_now: adj_now,
                model.delta_adj: delta_adj,
                model.feature_h0:feature_h0,
                model.label_xy: np.asarray([1] * (model.batch_size / 2) + [0] * (model.batch_size /2))
            }

            out = [model.cost, model.optimizer, model.auc_result,
                   model.auc_opt, model.prediction]
            begin_time = time.time()
            output = session.run(out, feed)
            time_consume += time.time() - begin_time
            # print output
            cost, _, auc, _, prediction = output

            for idx in range(len(prediction) / 2):
                if prediction[idx] > prediction[idx + len(prediction) / 2]:
                    t_auc += 1
                t_num += 1
            # print prediction
        # print "TEST",prediction
        if model.mode == "Train":
            auc = 0.
            total_cost += cost
        else:
            f1_score.add_f1(
                np.asarray([1] * (model.batch_size / 2) + [0] * (model.batch_size / 2)), prediction
            )
            cost = 0.
            total_cost += cost

        num_ += 1.
        if verbose and batch_id % int(batch_num / 5.) == 1 and model.mode == "Valid":
            INFO_LOG("{}/{}, cost: {}, auc: {}, f1_score: {}".format(
                batch_id, batch_num, total_cost / num_,
                auc, f1_score.return_f1_score()
            ),
            True
            )
    if num_ == 0:
        INFO_LOG("===failed graph===" + str(loader.present_graph), True)
    # if model.mode == "Train":
    #     print("prediction_l", [x / batch_num for x in prediction_l])
    #     print("prediction_l_n", [x / batch_num for x in prediction_n_l])
    # else:
    #     print("valid prediction", f1_score.return_predict_mean())
    if not model.mode == "Train":
        # print "auc", t_auc / t_num
        auc = t_auc / t_num

    return total_cost / num_, {"auc": auc, "f1_score": f1_score.return_f1_score(), "time_consume": time_consume}

def main(_):

    loader = Loader(flag="as")
    config = Config(loader, flag="as")

    if platform.system() == 'Linux':
        gpuid = config.gpu
        os.environ["CUDA_VISIBLE_DEVICES"] = '{}'.format(gpuid)
        device = '/gpu:' + str(gpuid)
    else:
        device = '/cpu:0'

    lr_updater = LearningRateUpdater(config.learning_rate, config.decay, config.decay_epoch)

    i = 0
    graph = tf.Graph()
    with graph.as_default():
        trainm = DynGCN(config, device, loader, "Train")
        testm = DynGCN(config, device, loader, "Valid")

    session_config = tf.ConfigProto(allow_soft_placement=True, log_device_placement=False)
    session_config.gpu_options.allow_growth = True

    with tf.Session(graph=graph, config=session_config) as session:
        # print "!!!!!!!!!!!"
        session.run(tf.global_variables_initializer())
        # print "*********"
        # trainm.load_last_time_embedding(loader.present_graph, session)

        # CTR_GNN_loader(session, config)
        best_f1_score = 0.
        best_auc_score = 0.
        best_epoch = 0
        time_consume_t = 0.
        sum_time_consume = 0.
        for epoch in range(config.epoch_num):
            if epoch % 1 == 0 and epoch != 0:
                loader.change_2_next_graph_date()
            trainm.update_lr(session, lr_updater.get_lr())
            # session.run(tf.local_variables_initializer())
            print("which graph now?", loader.present_graph)

            cost, eavluation_result = run(session, config, trainm, loader, verbose=False)
            INFO_LOG("Epoch %d  Train " % epoch + str(eavluation_result), epoch % 1 == 0)
            INFO_LOG("Epoch %d Train costs %.3f" %
                     (epoch, cost), epoch % 100 == 0)
            session.run(tf.local_variables_initializer())
            if epoch % 20 != 0:
                continue
            cost, eavluation_result = run(session, config, testm, loader, verbose=False)
            INFO_LOG("Epoch %d  Valid " % epoch + str(eavluation_result), epoch % 1 == 0)
            INFO_LOG("Epoch %d Valid cost %.3f" % (epoch, cost), epoch % 1 == 0)
            # #
            auc = eavluation_result['auc']
            f1_score = eavluation_result["f1_score"]["micro_f1_score"]
            lr_updater.update(f1_score, epoch)

            if best_f1_score < f1_score:
                best_f1_score = f1_score
                best_epoch = epoch
                DynGCN_saver(session, config, best_f1_score, best_epoch, "as_3hop")

                INFO_LOG("*** best f1_score now is %.5f in %d epoch" % (best_f1_score, best_epoch), True)
                INFO_LOG("BEST Epoch %d  Valid " % epoch + str(eavluation_result), True)

            if best_auc_score < auc:
                best_auc_score = auc
                INFO_LOG("*** best auc now is %.5f in %d epoch" % (best_auc_score, epoch), True)

            INFO_LOG("*** best f1_score now is %.4f in %d epoch" % (best_f1_score, best_epoch), True)
            INFO_LOG("*** best AUC now is %.4f in %d epoch" % (best_auc_score, best_epoch), True)

            # time_consume_t += 1.
            # sum_time_consume += eavluation_result["time_consume"]
            # print("TIME CONSUME *** ", sum_time_consume/ time_consume_t)

if __name__ == '__main__':
    tf.app.run()


#! /usr/bin/env python
# 36-birthday-plots
#
# This exercise is Part 4 of 4 of the birthday data exercise series. The other exercises are: Part 1,
# Part 2, and Part 3.
# In the previous exercise we counted how many birthdays there are in each month in our dictionary of birthdays.
#
# In this exercise, use the bokeh Python library to plot a histogram of which months the scientists have birthdays in!
# Because it would take a long time for you to input the months of various scientists, you can use my scientist
# birthday JSON file. Just parse out the months (if you don’t know how, I suggest looking at the previous exercise or
# its solution) and draw your histogram.

from bokeh.plotting import figure, output_file, show
from bokeh.models import ColumnDataSource, Range1d
import json
import calendar

def read_birthdays(file):
    with open(file, "r") as data:
        birthdays = json.load(data)
    return birthdays

def count_all_months(birthdays):
    count_ordered_birthdays = {}
    for month in range(1, 13):
        for name in birthdays:
            number_current_month = int(birthdays[name].split("/")[1])
            name_current_month = calendar.month_name[number_current_month]
            if number_current_month == month:
                if name_current_month in count_ordered_birthdays:
                    count_ordered_birthdays[name_current_month] += 1
                else:
                    count_ordered_birthdays[name_current_month] = 1
    return count_ordered_birthdays

def main():
    file = "birthdays.json"
    birthdays = read_birthdays(file)
    count_months = count_all_months(birthdays)

    x = list(count_months.keys())
    y = list(count_months.values())

    source = ColumnDataSource(dict(x=[calendar.month_name[i] for i in range(1, 13)]))

    output_file("bars.html")
    p = figure(title='Amount of Birthdays of Scientists by Months', plot_width=800, plot_height=400,
               x_range=source.data["x"], y_range=Range1d(0, 3))
    p.vbar(x, width=0.5, bottom=0, top=y, color="firebrick")

    p.yaxis[0].axis_label = "Birthdays"

    show(p)

if __name__ == '__main__':
    main()