"""
This file contains a set of pytest fixtures which are different gwcses for testing.
"""
import pytest

import numpy as np

import astropy.units as u
from astropy import coordinates as coord
from astropy.modeling import models
from astropy.time import Time

from .. import coordinate_frames as cf
from .. import wcs

# frames
detector_1d = cf.CoordinateFrame(name='detector', axes_order=(0,), naxes=1, axes_type="detector")
detector_2d = cf.Frame2D(name='detector', axes_order=(0, 1))
icrs_sky_frame = cf.CelestialFrame(reference_frame=coord.ICRS(),
                                   axes_order=(0, 1))

freq_frame = cf.SpectralFrame(name='freq', unit=u.Hz, axes_order=(2, ))
wave_frame = cf.SpectralFrame(name='wave', unit=u.m, axes_order=(2, ),
                              axes_names=('lambda', ))

# transforms
model_2d_shift = models.Shift(1) & models.Shift(2)
model_1d_scale = models.Scale(2)

@pytest.fixture
def gwcs_2d_spatial_shift():
    """
    A simple one step spatial WCS, in ICRS with a 1 and 2 px shift.
    """
    pipe = [(detector_2d, model_2d_shift), (icrs_sky_frame, None)]

    return wcs.WCS(pipe)

@pytest.fixture
def gwcs_1d_freq():
    return wcs.WCS([(detector_1d, model_1d_scale), (freq_frame, None)])

@pytest.fixture
def gwcs_3d_spatial_wave():
    comp1 = cf.CompositeFrame([icrs_sky_frame, wave_frame])
    m = model_2d_shift & model_1d_scale

    detector_frame = cf.CoordinateFrame(name="detector", naxes=3,
                                        axes_order=(0, 1, 2),
                                        axes_type=("pixel", "pixel", "pixel"),
                                        axes_names=("x", "y", "z"), unit=(u.pix, u.pix, u.pix))

    return wcs.WCS([(detector_frame, m),
                    (comp1, None)])

@pytest.fixture
def gwcs_2d_shift_scale():
    m1 = models.Shift(1) & models.Shift(2)
    m2 = models.Scale(5) & models.Scale(10)
    m3 = m1 | m2
    pipe = [(detector_2d, m3), (icrs_sky_frame, None)]
    return wcs.WCS(pipe)

@pytest.fixture
def gwcs_1d_freq_quantity():

    detector_1d = cf.CoordinateFrame(name='detector', axes_order=(0,), naxes=1, unit=u.pix, axes_type="detector")
    return wcs.WCS([(detector_1d, models.Multiply(1 * u.Hz / u.pix)), (freq_frame, None)])

@pytest.fixture
def gwcs_2d_shift_scale_quantity():
    m4 = models.Shift(1 * u.pix) & models.Shift(2 * u.pix)
    m5 = models.Scale(5 * u.deg)
    m6 = models.Scale(10 * u.deg)
    m5.input_units_equivalencies = {'x': u.pixel_scale(1 * u.deg / u.pix)}
    m6.input_units_equivalencies = {'x': u.pixel_scale(1 * u.deg / u.pix)}
    m5.inverse = models.Scale(1. / 5 * u.pix)
    m6.inverse = models.Scale(1. / 10 * u.pix)
    m5.inverse.input_units_equivalencies = {
        'x': u.pixel_scale(1 * u.pix / u.deg)
    }
    m6.inverse.input_units_equivalencies = {
        'x': u.pixel_scale(1 * u.pix / u.deg)
    }
    m7 = m5 & m6
    m8 = m4 | m7
    pipe2 = [(detector_2d, m8), (icrs_sky_frame, None)]
    return wcs.WCS(pipe2)

@pytest.fixture
def gwcs_3d_identity_units():
    """
    A simple 1-1 gwcs that converts from pixels to arcseconds
    """
    identity = (models.Multiply(1 * u.arcsec / u.pixel) &
                models.Multiply(1 * u.arcsec / u.pixel) &
                models.Multiply(1 * u.nm / u.pixel))
    sky_frame = cf.CelestialFrame(axes_order=(0, 1), name='icrs',
                                  reference_frame=coord.ICRS(),
                                  axes_names=("longitude", "latitude"))
    wave_frame = cf.SpectralFrame(axes_order=(2, ), unit=u.nm, axes_names=("wavelength",))

    frame = cf.CompositeFrame([sky_frame, wave_frame])

    detector_frame = cf.CoordinateFrame(name="detector", naxes=3,
                                        axes_order=(0, 1, 2),
                                        axes_type=("pixel", "pixel", "pixel"),
                                        axes_names=("x", "y", "z"), unit=(u.pix, u.pix, u.pix))

    return wcs.WCS(forward_transform=identity, output_frame=frame, input_frame=detector_frame)

@pytest.fixture
def gwcs_4d_identity_units():
    """
    A simple 1-1 gwcs that converts from pixels to arcseconds
    """
    identity = (models.Multiply(1*u.arcsec/u.pixel) & models.Multiply(1*u.arcsec/u.pixel) &
                models.Multiply(1*u.nm/u.pixel) & models.Multiply(1*u.nm/u.pixel))
    sky_frame = cf.CelestialFrame(axes_order=(0, 1), name='icrs',
                                  reference_frame=coord.ICRS())
    wave_frame = cf.SpectralFrame(axes_order=(2, ), unit=u.nm)
    time_frame = cf.TemporalFrame(axes_order=(3, ), unit=u.s,
                                  reference_frame=Time("2000-01-01T00:00:00"))

    frame = cf.CompositeFrame([sky_frame, wave_frame, time_frame])

    detector_frame = cf.CoordinateFrame(name="detector", naxes=4,
                                        axes_order=(0, 1, 2, 3),
                                        axes_type=("pixel", "pixel", "pixel", "pixel"),
                                        axes_names=("x", "y", "z", "s"), unit=(u.pix, u.pix, u.pix, u.pix))

    return wcs.WCS(forward_transform=identity, output_frame=frame, input_frame=detector_frame)

@pytest.fixture
def gwcs_simple_imaging_units():
    shift_by_crpix = models.Shift(-2048*u.pix) & models.Shift(-1024*u.pix)
    matrix = np.array([[1.290551569736E-05, 5.9525007864732E-06],
                       [5.0226382102765E-06 , -1.2644844123757E-05]])
    rotation = models.AffineTransformation2D(matrix * u.deg,
                                             translation=[0, 0] * u.deg)
    rotation.input_units_equivalencies = {"x": u.pixel_scale(1*u.deg/u.pix),
                                          "y": u.pixel_scale(1*u.deg/u.pix)}
    rotation.inverse = models.AffineTransformation2D(np.linalg.inv(matrix) * u.pix,
                                                     translation=[0, 0] * u.pix)
    rotation.inverse.input_units_equivalencies = {"x": u.pixel_scale(1*u.pix/u.deg),
                                                  "y": u.pixel_scale(1*u.pix/u.deg)}
    tan = models.Pix2Sky_TAN()
    celestial_rotation =  models.RotateNative2Celestial(5.63056810618*u.deg,
                                                        -72.05457184279*u.deg,
                                                        180*u.deg)
    det2sky = shift_by_crpix | rotation | tan | celestial_rotation
    det2sky.name = "linear_transform"

    detector_frame = cf.Frame2D(name="detector", axes_names=("x", "y"),
                                unit=(u.pix, u.pix))
    sky_frame = cf.CelestialFrame(reference_frame=coord.ICRS(), name='icrs',
                                  unit=(u.deg, u.deg))
    pipeline = [(detector_frame, det2sky),
                (sky_frame, None)
                ]
    return wcs.WCS(pipeline)

@pytest.fixture
def gwcs_3spectral_orders():
    comp1 = cf.CompositeFrame([icrs_sky_frame, wave_frame])
    detector_frame = cf.Frame2D(name="detector", axes_names=("x", "y"),
                                unit=(u.pix, u.pix))
    m = model_2d_shift & model_1d_scale

    return wcs.WCS([(detector_frame, m),
                    (comp1, None)])


from typing import Dict, Callable
import pyaml
import time
from jsonable import Jsonable

class ScrapeResult(Jsonable):
  """information for the result of a web scrape of listings(owners)"""

  def __init__(self) -> None:
    # size of the raw http response
    self.bytes = 0

    # how long the scrape took
    self.took = ''

    # total number of listings attempted to be scraped
    self.total = 0

    # actual number of scraped listings
    self.scraped = 0

    # listings that were cached and therefore werent attempted to be scraped
    self.cached = 0

    # how many listing scrapes failed
    self.failures = 0

    # the sum of the number of items for every scraped listing
    self.items = 0

    # summary of the types of items scraped
    self.itemTypes: Dict[str, int] = {}

    # summary of the types of the listings scraped
    self.ownerTypes: Dict[str, int] = {}

  def start(self) -> Callable:
    """starts a scrape result. this is used to calculate how long it took"""
    t0 = time.time()

    def finished() -> None:
      """sets the attributes on this class"""
      t1 = time.time()
      self.took = f'{(t1 - t0):.2f} seconds'
      self.total = self.scraped + self.cached + self.failures
      self.items = sum(self.itemTypes.values())

    # let the caller indicate when the scrape is finished
    return finished

  def pretty(self) -> str:
    """returns a pretty string representing this scrape result"""
    return f'took: {self.took}\n' + pyaml.dump({
      'scraped': self.scraped,
      'failures': self.failures,
      'cached': self.cached,
      'total': self.total,
      'ownerTypes': self.ownerTypes,
      'items': self.items,
      'itemTypes': self.itemTypes,
      'bytes': self.bytes,
    }, sort_dicts=False).strip()


from datetime import date
today = date.today()
from CRABClient.UserUtilities import config
config = config()

config.General.requestName = 'Chic_%s_2018A'%today
config.General.transferOutputs = True
config.General.transferLogs = True

config.JobType.pluginName = 'Analysis'
config.JobType.psetName = 'run-chic-miniaod_2018ABC.py'
config.JobType.outputFiles = ['rootuple.root']
config.JobType.sendExternalFolder = True
config.JobType.allowUndistributedCMSSW = True

config.Data.inputDataset = '/Charmonium/Run2018A-17Sep2018-v1/MINIAOD'
config.Data.inputDBS = 'global'
config.Data.splitting = 'FileBased'
config.Data.unitsPerJob = 5
config.Data.lumiMask = 'Cert_314472-325175_13TeV_Legacy2018_Collisions18_JSON_MuonPhys.txt'
config.Data.outLFNDirBase = '/store/user/xuyan/'
config.Data.publication = False
config.Data.outputDatasetTag  = 'Chic_%s_2018A'%today
config.Site.storageSite = 'T3_US_FNALLPC'


# -*- coding: utf-8 -*-
"""Provides skosify as command line client."""

from __future__ import print_function

from .skosify import Skosify, DEFAULT_NAMESPACES, DEFAULT_OPTIONS

import optparse
import logging
import sys

from rdflib import URIRef, Namespace, RDF, RDFS

try:
    import configparser
except ImportError:
    import ConfigParser as configparser

def get_option_parser(defaults):
    """Create and return an OptionParser with the given defaults."""
    # based on recipe from:
    # http://stackoverflow.com/questions/1880404/using-a-file-to-store-optparse-arguments

    # process command line parameters
    # e.g. skosify yso.owl -o yso-skos.rdf
    usage = "Usage: %prog [options] voc1 [voc2 ...]"
    parser = optparse.OptionParser(usage=usage)
    parser.set_defaults(**defaults)
    parser.add_option('-c', '--config', type='string',
                      help='Read default options '
                           'and transformation definitions '
                           'from the given configuration file.')
    parser.add_option('-o', '--output', type='string',
                      help='Output file name. Default is "-" (stdout).')
    parser.add_option('-D', '--debug', action="store_true",
                      help='Show debug output.')
    parser.add_option('-d', '--no-debug', dest="debug",
                      action="store_false", help='Hide debug output.')
    parser.add_option('-O', '--log', type='string',
                      help='Log file name. Default is to use standard error.')

    group = optparse.OptionGroup(parser, "Input and Output Options")
    group.add_option('-f', '--from-format', type='string',
                     help='Input format. '
                           'Default is to detect format '
                           'based on file extension. '
                           'Possible values: xml, n3, turtle, nt...')
    group.add_option('-F', '--to-format', type='string',
                     help='Output format. '
                           'Default is to detect format '
                           'based on file extension. '
                           'Possible values: xml, n3, turtle, nt...')
    group.add_option('--update-query', type='string',
                     help='SPARQL update query. '
                     'This query is executed against the input '
                     'data before processing it. '
                     'The value can be either the actual query, '
                     'or "@filename".')
    group.add_option('--construct-query', type='string',
                     help='SPARQL CONSTRUCT query. '
                     'This query is executed against the input '
                     'data and the result graph is used as the '
                     'actual input. '
                     'The value can be either the actual query, '
                     'or "@filename".')
    group.add_option('-I', '--infer', action="store_true",
                     help='Perform RDFS subclass/subproperty inference '
                          'before transforming input.')
    group.add_option('-i', '--no-infer', dest="infer", action="store_false",
                     help="Don't perform RDFS subclass/subproperty inference "
                          "before transforming input.")
    parser.add_option_group(group)

    group = optparse.OptionGroup(
        parser, "Concept Scheme and Labelling Options")
    group.add_option('-s', '--namespace', type='string',
                     help='Namespace of vocabulary '
                          '(usually optional; used to create a ConceptScheme)')
    group.add_option('-L', '--label', type='string',
                     help='Label/title for the vocabulary '
                          '(usually optional; used to label a ConceptScheme)')
    group.add_option('-l', '--default-language', type='string',
                     help='Language tag to set for labels '
                          'with no defined language.')
    group.add_option('-p', '--preflabel-policy', type='string',
                     help='Policy for handling multiple prefLabels '
                          'with the same language tag. '
                          'Possible values: shortest, longest, all.')
    group.add_option('--set-modified', dest="set_modified",
                     action="store_true",
                     help='Set modification date on the ConceptScheme')
    group.add_option('--no-set-modified', dest="set_modified",
                     action="store_false",
                     help="Don't set modification date on the ConceptScheme")
    parser.add_option_group(group)

    group = optparse.OptionGroup(parser, "Vocabulary Structure Options")
    group.add_option('-E', '--mark-top-concepts', action="store_true",
                     help='Mark top-level concepts in the hierarchy '
                          'as top concepts (entry points).')
    group.add_option('-e', '--no-mark-top-concepts',
                     dest="mark_top_concepts", action="store_false",
                     help="Don't mark top-level concepts in the hierarchy "
                          "as top concepts.")
    group.add_option('-N', '--narrower', action="store_true",
                     help='Include narrower/narrowerGeneric/narrowerPartitive '
                          'relationships in the output vocabulary.')
    group.add_option('-n', '--no-narrower',
                     dest="narrower", action="store_false",
                     help="Don't include "
                          "narrower/narrowerGeneric/narrowerPartitive "
                          "relationships in the output vocabulary.")
    group.add_option('-T', '--transitive', action="store_true",
                     help='Include transitive hierarchy relationships '
                          'in the output vocabulary.')
    group.add_option('-t', '--no-transitive',
                     dest="transitive", action="store_false",
                     help="Don't include transitive hierarchy relationships "
                          "in the output vocabulary.")
    group.add_option('-M', '--enrich-mappings', action="store_true",
                     help='Perform SKOS enrichments on mapping relationships.')
    group.add_option('-m', '--no-enrich-mappings', dest="enrich_mappings",
                     action="store_false",
                     help="Don't perform SKOS enrichments "
                          "on mapping relationships.")
    group.add_option('-A', '--aggregates', action="store_true",
                     help='Keep AggregateConcepts completely '
                          'in the output vocabulary.')
    group.add_option('-a', '--no-aggregates',
                     dest="aggregates", action="store_false",
                     help='Remove AggregateConcepts completely '
                          'from the output vocabulary.')
    group.add_option('-R', '--keep-related', action="store_true",
                     help="Keep skos:related relationships "
                          "within the same hierarchy.")
    group.add_option('-r', '--no-keep-related',
                     dest="keep_related", action="store_false",
                     help="Remove skos:related relationships "
                          "within the same hierarchy.")
    group.add_option('-B', '--break-cycles', action="store_true",
                     help="Break any cycles in the skos:broader hierarchy.")
    group.add_option('-b', '--no-break-cycles', dest="break_cycles",
                     action="store_false",
                     help="Don't break cycles in the skos:broader hierarchy.")
    group.add_option('--eliminate-redundancy', action="store_true",
                     help="Eliminate hierarchical redundancy in the "
                          "skos:broader hierarchy.")
    group.add_option('--no-eliminate-redundancy', dest="eliminate_redundancy",
                     action="store_false",
                     help="Don't eliminate hierarchical redundancy in the "
                          "skos:broader hierarchy.")
    parser.add_option_group(group)

    group = optparse.OptionGroup(parser, "Cleanup Options")
    group.add_option('--cleanup-classes', action="store_true",
                     help="Remove definitions of classes with no instances.")
    group.add_option('--no-cleanup-classes', dest='cleanup_classes',
                     action="store_false",
                     help="Don't remove definitions "
                          "of classes with no instances.")
    group.add_option('--cleanup-properties', action="store_true",
                     help="Remove definitions of properties "
                          "which have not been used.")
    group.add_option('--no-cleanup-properties', action="store_false",
                     dest='cleanup_properties',
                     help="Don't remove definitions of properties "
                          "which have not been used.")
    group.add_option('--cleanup-unreachable', action="store_true",
                     help="Remove triples which can not be reached "
                          "by a traversal from the main vocabulary graph.")
    group.add_option('--no-cleanup-unreachable', action="store_false",
                     dest='cleanup_unreachable',
                     help="Don't remove triples which can not be reached "
                          "by a traversal from the main vocabulary graph.")
    parser.add_option_group(group)

    return parser

def expand_curielike(namespaces, curie):
    """Expand a CURIE (or a CURIE-like string with a period instead of colon
    as separator) into URIRef. If the provided curie is not a CURIE, return it
    unchanged."""

    if curie == '':
        return None
    if sys.version < '3':  # Python 2 ConfigParser reads raw byte strings
        curie = curie.decode('UTF-8')  # ...make those into Unicode objects

    if curie.startswith('[') and curie.endswith(']'):
        # decode SafeCURIE
        curie = curie[1:-1]

    if ':' in curie:
        ns, localpart = curie.split(':', 1)
    elif '.' in curie:
        ns, localpart = curie.split('.', 1)
    else:
        return curie

    if ns in namespaces:
        return URIRef(namespaces[ns].term(localpart))
    else:
        logging.warning("Unknown namespace prefix %s", ns)
        return URIRef(curie)

def expand_mapping_target(namespaces, val):
    """Expand a mapping target, expressed as a comma-separated list of
    CURIE-like strings potentially prefixed with ^ to express inverse
    properties, into a list of (uri, inverse) tuples, where uri is a URIRef
    and inverse is a boolean."""

    vals = [v.strip() for v in val.split(',')]
    ret = []
    for v in vals:
        inverse = False
        if v.startswith('^'):
            inverse = True
            v = v[1:]
        ret.append((expand_curielike(namespaces, v), inverse))
    return ret

def write_rdf(rdf, filename, fmt):
    if filename == '-':
        out = sys.stdout
    else:
        out = open(filename, 'wb')

    if not fmt:
        # determine output format
        fmt = 'xml'  # default
        if filename.endswith('n3'):
            fmt = 'n3'
        if filename.endswith('nt'):
            fmt = 'nt'
        if filename.endswith('ttl'):
            fmt = 'turtle'

    logging.debug("Writing output file %s (format: %s)", filename, fmt)
    rdf.serialize(destination=out, format=fmt)

def main():
    """Read command line parameters and make a transform based on them."""

    skosify = Skosify()

    namespaces = DEFAULT_NAMESPACES
    typemap = {}
    literalmap = {}
    relationmap = {}

    defaults = DEFAULT_OPTIONS
    defaults['to_format'] = None
    defaults['output'] = '-'
    defaults['log'] = None
    defaults['debug'] = False

    options, remainingArgs = get_option_parser(defaults).parse_args()

    # configure logging
    logformat = '%(levelname)s: %(message)s'
    loglevel = logging.INFO
    if options.debug:
        loglevel = logging.DEBUG
    if options.log:
        logging.basicConfig(filename=options.log,
                            format=logformat, level=loglevel)
    else:  # logging messages go into stderr by default
        logging.basicConfig(format=logformat, level=loglevel)

    # read config file
    if options.config is not None:
        # read the supplied configuration file
        cfgparser = configparser.SafeConfigParser()
        # force case-sensitive handling of option names
        cfgparser.optionxform = str
        cfgparser.read(options.config)

        # parse namespaces from configuration file
        for prefix, uri in cfgparser.items('namespaces'):
            namespaces[prefix] = URIRef(uri)

        # parse types from configuration file
        for key, val in cfgparser.items('types'):
            typemap[expand_curielike(namespaces, key)] = \
                expand_mapping_target(namespaces, val)

        # parse literals from configuration file
        for key, val in cfgparser.items('literals'):
            literalmap[expand_curielike(namespaces, key)] = \
                expand_mapping_target(namespaces, val)

        # parse relations from configuration file
        for key, val in cfgparser.items('relations'):
            relationmap[expand_curielike(namespaces, key)] = \
                expand_mapping_target(namespaces, val)

        # parse options from configuration file
        for opt, val in cfgparser.items('options'):
            if opt not in defaults:
                logging.warning(
                    'Unknown option in configuration file: %s (ignored)', opt)
                continue
            if defaults[opt] in (True, False):  # is a Boolean option
                defaults[opt] = cfgparser.getboolean('options', opt)
            else:
                defaults[opt] = val

        # re-initialize and re-run OptionParser using defaults read from
        # configuration file
        options, remainingArgs = get_option_parser(defaults).parse_args()

    if remainingArgs:
        inputfiles = remainingArgs
    else:
        inputfiles = ['-']

    voc = skosify.skosify(inputfiles, namespaces, typemap,
                          literalmap, relationmap, options)

    write_rdf(voc, options.output, options.to_format)

if __name__ == '__main__':
    main()


import numpy as np
from keras.callbacks import Callback

from libs.utils.utils import decode_batch, accuracies

class VizCallback(Callback):
    """
    The Custom Callback created for printing the Accuracy and Letter Accuracy Metrics at the End of Each Epoch
    """

    def __init__(self, test_func, text_img_gen, is_train, acc_compute_batches):
        self.test_func = test_func
        self.text_img_gen = text_img_gen
        self.is_train = is_train                # Used to indicate whether the callback is called to for Train or Validation Data
        self.acc_batches = acc_compute_batches  # Number of Batches for which the metrics are computed typically equal to steps/epoch

    def show_accuracy_metrics(self, num_batches):
        """
        Calculates the accuracy and letter accuracy for each batch of inputs,
        and prints the avarage accuracy and letter accuracy across all the batches
        """
        accuracy = 0
        letter_accuracy = 0
        batches_cnt = num_batches
        while batches_cnt > 0:
            word_batch = next(self.text_img_gen)[0]   # Gets the next batch from the Data generator
            decoded_res = decode_batch(self.test_func, word_batch['img_input'])
            actual_res = word_batch['source_str']
            acc, let_acc = accuracies(actual_res, decoded_res)
            accuracy += acc
            letter_accuracy += let_acc
            batches_cnt -= 1
        accuracy = accuracy / num_batches
        letter_accuracy = letter_accuracy / num_batches
        if self.is_train:
            print("\nTrain Average Accuracy: \t", np.round(accuracy, 2), " %")
            print("Train Average Letter Accuracy: \t", np.round(letter_accuracy, 2), " %")
        else:
            print("Validation Average Accuracy: \t\t", np.round(accuracy, 2), " %")
            print("Validation Average Letter Accuracy: \t", np.round(letter_accuracy, 2), " %")

    def on_epoch_end(self, epoch, logs=None):
        self.show_accuracy_metrics(self.acc_batches)


# Copyright 2019 Canonical Ltd.
#
# 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.

import ast
from setuptools import setup, find_packages

def _read_me() -> str:
    with open("README.md", "rt", encoding="utf8") as fh:
        readme = fh.read()
    return readme

def _get_version() -> str:
    # ops.__init__ needs to pull in ops.charm to work around a circular
    # import so we can't import it here as that pulls in yaml which isn't
    # necessarily there yet.
    version = 'unknown'
    with open("ops/__init__.py", "rt", encoding="utf8") as fh:
        source = fh.read()
    code = ast.parse(source)
    for node in code.body:
        if isinstance(node, ast.Assign):
            targets = [i.id for i in node.targets]
            if '__version__' in targets:
                if isinstance(node.value, ast.Str):
                    # Python < 3.8
                    version = node.value.s
                else:
                    version = node.value.value
                break
    return version

setup(
    name="ops",
    version=_get_version(),
    description="The Python library behind great charms",
    long_description=_read_me(),
    long_description_content_type="text/markdown",
    license="Apache-2.0",
    url="https://github.com/canonical/operator",
    author="The Charmcraft team at Canonical Ltd.",
    author_email="charmcraft@lists.launchpad.net",
    packages=find_packages(include=('ops', 'ops.*')),
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: Apache Software License",
        "Development Status :: 4 - Beta",

        "Intended Audience :: Developers",
        "Intended Audience :: System Administrators",
        "Operating System :: MacOS :: MacOS X",
        "Operating System :: POSIX :: Linux",
        # include Windows once we're running tests there also
        # "Operating System :: Microsoft :: Windows",
    ],
    python_requires='>=3.5',
    install_requires=["PyYAML"],
)


from selenium import webdriver
from selenium.webdriver.common.by import By
import time

#Driver Intialization
driver=webdriver.Chrome(executable_path="D:\SeleniumPython\drivers\chromedriver.exe")
driver.maximize_window()

driver.get("http://demo.automationtesting.in/Frames.html") # Launch Browser

#Single Iframe
driver.switch_to.frame("singleframe")  #Switch to frame using id or name
driver.find_element(By.XPATH,"//div[@class='col-xs-6 col-xs-offset-5']//input").send_keys("Siva")

#Switch focus to browser
driver.switch_to.default_content()
time.sleep(3)
driver.find_element(By.LINK_TEXT,"Iframe with in an Iframe").click()

time.sleep(3)
frames=driver.find_elements_by_tag_name("iframe")
print(len(frames))
counter=0
for f in frames:
    print(f.get_attribute("src"))
    print(counter)
    counter=counter+1

# #Iframe with in an frame
driver.switch_to.frame(1)  #Switch to frame using index
driver.switch_to.frame(0)
driver.find_element(By.XPATH,"//div[@class='col-xs-6 col-xs-offset-5']//input").send_keys("Kumar")


import pandas as pd
import pymysql
import requests as rq
import datetime

db=pymysql.connect("localhost","ssafy","ssafy","forchannel",charset="utf8")
cursor = db.cursor()
CSV_PATH = 'C:/Users/multicampus/Desktop/crawling/jobkorea.csv'
csv_test = pd.read_csv(CSV_PATH)
sub=pd.DataFrame(csv_test, columns=['title','contents','sdate','edate'])
# sub=csv_test.data[['전시회명','전시장소','전시시작기간','전시끝기간','전시설명']]
data = [tuple(list(x)+[29,datetime.datetime.now(),datetime.datetime.now()]) for x in sub.values]
print(data)
query="""insert into schedules(title,contents,sdate,edate,ch_no,created_date,modified_date) values (%s,%s,%s,%s,%s,%s,%s)"""
cursor.executemany(query, data)
db.commit()


import configparser
import logging
import os

logging.basicConfig(level=logging.DEBUG)

class Config:
    def __init__(self):
        self.config = configparser.ConfigParser()
        ini_path = os.path.dirname(os.path.realpath(__file__))
        self.config.read(os.path.join(ini_path, "default.ini"))

    # simple pattern from https://wiki.python.org/moin/ConfigParserExamples
    def map(self, section):
        dict1 = {}
        options = self.config.options(section)
        for option in options:
            try:
                dict1[option] = self.config.get(section, option)
                if dict1[option] == -1:
                    logging.debug("skip: %s" % option)
            except:
                print("exception on %s!" % option)
                dict1[option] = None
        return dict1


""" Testing reading DICOM files

"""

import numpy as np

from .. import dicomreaders as didr

from .test_dicomwrappers import (EXPECTED_AFFINE,
                                 EXPECTED_PARAMS,
                                 DATA)

from nose.tools import assert_true, assert_false, \
     assert_equal, assert_raises

from numpy.testing import assert_array_equal, assert_array_almost_equal

from dipy.testing import parametric, IO_DATA_PATH

@parametric
def test_read_dwi():
    img = didr.mosaic_to_nii(DATA)
    arr = img.get_data()
    yield assert_equal(arr.shape, (128,128,48))
    yield assert_array_almost_equal(img.get_affine(), EXPECTED_AFFINE)

@parametric
def test_read_dwis():
    data, aff, bs, gs = didr.read_mosaic_dwi_dir(IO_DATA_PATH, '*.dcm.gz')
    yield assert_equal(data.ndim, 4)
    yield assert_array_almost_equal(aff, EXPECTED_AFFINE)
    yield assert_array_almost_equal(bs, (0, EXPECTED_PARAMS[0]))
    yield assert_array_almost_equal(gs,
                                    (np.zeros((3,)) + np.nan,
                                     EXPECTED_PARAMS[1]))
    yield assert_raises(IOError, didr.read_mosaic_dwi_dir, 'improbable')


import sublime
import sublime_plugin
import os
import sys
import tempfile
import shutil

# add PIL module
if sublime.platform() == "osx":
	package_file = os.path.normpath(os.path.abspath(__file__))
	package_path = os.path.dirname(package_file)
	lib_path =  os.path.join(package_path, "lib")
	if lib_path not in sys.path:
		sys.path.append(lib_path)
	from PIL import ImageGrab
	from PIL import ImageFile
	from PIL import Image

try:
	from MarkdownEditing.mdeutils import *
except ImportError:
	from mdeutils import *

class InsertImageFromClipboardCommand(MDETextCommand):
	"""docstring for InsertImagerFromClipboardCommand"""
	def load_settings(self):
		settings = sublime.load_settings('Markdown.sublime-settings')
		self.SAVE_METHOD = settings.get("mde.image.save.method")
		print(self.SAVE_METHOD)
		self.SAVE_BASEDIR = settings.get("mde.image.save.basedir")
		print(self.SAVE_BASEDIR)
		# current file dirname
		self.curdir = os.path.dirname(self.view.file_name())

	def run(self, edit):
		print("开始插入图片")
		self.load_settings()

		filepath = InsertImageFromClipboardCommand.save_image()
		if filepath:
			basename,url = self.getLocalUrl(filepath)
			self.view.run_command("insert_snippet", {"contents": "!["+basename+"](" + url + ")"})
		else:
			sublime.status_message("insert_snippet is error")

	@staticmethod
	def save_image():
		# 保存剪切板上的图片内容到临时文件
		m = ImageGrab.grabclipboard()
		if m :
			fd, tempFileName = tempfile.mkstemp(".png")
			try:
				m.save(tempFileName)
			except IOError as e:
				sublime.status_message("save image from clipboard is error")
				raise e
			return tempFileName

	def getLocalUrl(self,filePath):
		# 移动文件路径到制定目录，并且返回插入链接名和url
		if not filePath or not os.path.exists(filePath):
			return
		# filePath, ext
		dirname,ext=os.path.splitext(filePath)
		# 文件名，不含后缀
		basename=os.path.basename(dirname)

		basedir = os.path.expanduser(self.SAVE_BASEDIR) if self.SAVE_METHOD == "absoulte" else os.path.join(self.curdir,self.SAVE_BASEDIR)

		basedir = os.path.abspath(basedir)
		# 目标文件夹
		if not os.path.exists(basedir):
			os.makedirs(basedir)
		shutil.move(filePath,basedir)
		# 绝对路径运行
		realpath = basedir if self.SAVE_METHOD=="absoulte" else os.path.relpath(basedir,self.curdir)
		url = os.path.join(os.path.normpath(realpath),basename+""+ext)
		return basename,url


from django.conf import settings # import the settings file
def context(request):
    # return the value you want as a dictionnary. you may add multiple values in there.
    from .signals import render_started
    from django.apps import apps
    asenzor=apps.get_app_config("asenzor")
    render_started.send(sender=request)
    """
    BASE_URL=settings.BASE_URL
    BASE_URL_CLEAN=settings.BASE_URL_CLEAN

    if request.META['REMOTE_ADDR']!="127.0.0.1" and settings.DEBUG:
    	BASE_URL=settings.PROTOCOL+settings.PRIVATE_HOST#request.META['REMOTE_ADDR']+"/"
    if request.META['REMOTE_ADDR']!="127.0.0.1" and settings.DEBUG:
    	BASE_URL_CLEAN=settings.PRIVATE_HOST
    """
    warnings=""
    for elem in asenzor.warnings:
        if request.user.has_perm(elem[0]):
            warnings+=elem[1]
    print("iiiiiiiiiii")
    print(warnings)
    data={'BASE_URL': request.scheme+"://"+request.get_host()+settings.BASE_URL,
    'ASENZOR_URL': request.scheme+"://"+request.get_host()+settings.ASENZOR_URL,
    'MEDIA_URL': request.scheme+"://"+request.get_host()+settings.MEDIA_URL,
    "WARNINGS":warnings,
    }

    data.update(settings.MENUS)
    return data


import pickle
import numpy as np
import matplotlib as plt
import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import ImageGrid
from time import time
import itertools
from math import log10

np.set_printoptions(threshold=np.nan)
np.random.seed(400)

def plot_acc(acc, acc_val):
    plt.plot(acc)
    plt.plot(acc_val)
    plt.show()

def plot_cost(J_train, J_val, etas, lambdas):
    ###### PLOT COST ######
    train_labels = ['train_cost, (eta,lambdas): ('+str(x[0]) + ',' + str(x[1]) + ')' for x in itertools.product(etas,lambdas)]
    val_labels = ['val_cost:, (eta,lamdbas): ('+str(x[0]) + ',' + str(x[1]) + ')'  for x in list(itertools.product(etas,lambdas))]
    for i in range(J_train.shape[0]):
        plt.plot(J_train[i,:], label=train_labels[i])
        plt.plot(J_val[i,:], label=val_labels[i])
    plt.legend()
    plt.show()

class Params():
    def __init__(self, batch_size, n_epochs, _lambda, eta):
        self.batch_size = batch_size
        self.n_epochs = n_epochs
        self.eta = eta
        self._lambda = _lambda

class NeuronLayer():
    def __init__(self, n_in, n_out, type, init):
        if init == 'random':
            self.W = np.random.normal(0, 0.001, (n_in,n_out))
        elif init == 'He':
            self.W = np.random.normal(0, np.sqrt((2/n_out)), (n_in,n_out))
        self.b = np.zeros((n_in,1))
        self.grad_W = np.zeros((n_in,n_out))
        self.grad_b = np.zeros((n_in,1))
        self.type = type
        self.error = None
        self.velocity_W = 0
        self.velocity_b = 0

    def _feedforward(self,input):
        return self.W.dot(input) + self.b

    def _activation(self, ain):
        if self.type=='relu':
            return self.__relu(ain)
        if self.type=='softmax':
            return self.__softmax(ain)

    def __relu(self,ain):
        return np.maximum(0, ain)

    def __softmax(self, ain):
#         print(np.sum(np.exp(ain),axis=0))
        return np.exp(ain) / np.sum(np.exp(ain),axis=0)

    def estimate_params(self,ain):
        # OUTPUT: vectors of shape: (ain.shape[0],) (ain_shape[0],)
        return np.array(ain).mean(1), np.array(ain).var(1)

    def batchNorm(self, ain, mu, var, eps=1e-15):
        var_diag_scaled = np.diag(np.power((var+eps),-0.5))
#         print(var_diag_scaled.shape)

        ain_centered = ain - mu.reshape(-1,1)

        normed = var_diag_scaled.dot(ain_centered)
        return normed

class NeuralNetwork():
    def __init__(self):
        self.layers = []

    def add(self,layer):
        self.layers.append(layer)

    def computeAccuracy(self, X, Y):
        # finds the max probablity in each column and compares to the target labels.
        self.feedforward(X)
        pred = np.argmax(self.x[-1],axis=0)
        return np.sum(np.where(Y==pred,1,0))/X.shape[1]*100

    def compareGradient(self, X, Y, h=1e-5): # WEIGHT GRADIENT KLARAR 1e-3. men FILTER GRADIENT GÖR INTE DET.
        """
        http://peterroelants.github.io/posts/neural_network_implementation_part04/#Gradient-checking
        """
        # SAVE ALL DEVIATING BIAS GRADIENTS
        deviating_grads = []
        # Combine all parameter matrices in a list
        old_b = self.layers[-2].b.copy()
        # Combine all parameter gradients in a list
        grad_params = [self.layers[-2].grad_b.copy()]
        print(""" BIAS GRADIENT """)
        self.layers[-1].num_grad_b = np.zeros(self.layers[-2].grad_b.shape)
        for row in range(old_b.shape[0]):

                p_matrix_min = old_b.copy()
                p_matrix_min[row,0] -= h
                self.layers[-2].b = p_matrix_min
                c1 = self.computeCost(X, Y, _lambda=0)

                p_matrix_plus = old_b.copy()
                p_matrix_plus[row,0] += h
#                 print(self.layers[1].b - p_matrix_plus)
                self.layers[-2].b = p_matrix_plus
                c2 = self.computeCost(X, Y, _lambda = 0)

                grad_num = (c2 - c1) / (2 * h)
                print(c1, c2)
#                 print(grad_num, grad_params[0][row,0])

                # RESTORE OLD VALUES
#                 self.layers[1].b = old_b.copy()
#                 self.layers[1].grad_b = grad_params[0].copy()

                # Raise error if the numerical grade is not close to the backprop gradient
                if not np.isclose(grad_num, grad_params[0][row,0], atol=h):
                    print('Numerical gradient of {:.6f} is not close to {:.6f}!'.format(float(grad_num), float(grad_params[0][row,0])))
                    deviating_grads.append({'num':grad_num, 'analytical': grad_params[0][row,0], 'layer':'b'})
        print("num gradient errors in bias = {}".format(len(deviating_grads)/(row+1)))
#
        # SAVE ALL DEVIATING WEIGHT GRADIENTS
        deviating_grads = []
        # Combine all parameter matrices in a list
        old_W = self.layers[-2].W.copy()
        # Combine all parameter gradients in a list
        grad_params = [self.layers[-2].grad_W.copy()]
        print(""" WEIGHT GRADIENT """)
        self.layers[-2].num_grad_W = np.zeros(self.layers[-2].grad_W.shape)
        for row in range(old_W.shape[0]):
            for col in range(old_W.shape[1]):

                p_matrix_min = old_W.copy()
                p_matrix_min[row,col] -= h
                self.layers[-2].W = p_matrix_min
                c1 = self.computeCost(X, Y, 0)

                p_matrix_plus = old_W.copy()
                p_matrix_plus[row,col] += h
                self.layers[-2].W = p_matrix_plus
                c2 = self.computeCost(X, Y, 0)

                grad_num = (c2 - c1) / (2 * h)
#                 print(grad_num, grad_params[0][row,col])

                # RESTORE OLD VALUES
#                 self.layers[1].W = old_W.copy()
#                 self.layers[1].grad_W = grad_params[0].copy()

                # Raise error if the numerical grade is not close to the backprop gradient
                if not np.isclose(grad_num, grad_params[0][row,col], atol=h):
                    print('Numerical gradient of {:.6f} is not close to {:.6f}!'.format(float(grad_num), float(grad_params[0][row,col])))
                    deviating_grads.append({'num':grad_num, 'analytical': grad_params[0][row,col], 'layer':'W'})
            print("num gradient errors in row {}/{} = {}".format(row,old_W.shape[0], len(deviating_grads)/(row+1)))
        print('total errors is {}'.format(len(deviating_grads)))
#         print('No gradient errors found')

    def feedforward(self,input, test_time = None):
        self.eps = 1e-15
#         self.s.append(input) # add input to use in last backward pass
        self.s = [None] * len(self.layers)
        self.x = [None] * len(self.layers)
        self.batch_norms = [None] * len(self.layers)
#         self.input_mu, self.input_var = self.layers[-1].estimate_params(input)
#         normalized = self.layers[-1].batchNorm(input,self.input_mu,self.input_var)
#         self.input_norm = normalized
        if test_time == 'test_time':
#             print('test_time')
            self.mu = self.mu_avg
            self.var = self.var_avg
        else:
            self.mu = [None] * (len(self.layers)-1)
            self.var = [None] * (len(self.layers)-1)
        for i, layer in enumerate(self.layers):
            ain = layer._feedforward(input)
            self.s[i] = ain
            if i < len(self.layers)-1:
                if not test_time:
                    self.mu[i], self.var[i] = layer.estimate_params(ain)
                norm_out = layer.batchNorm(ain,self.mu[i],self.var[i])
                self.batch_norms[i] = norm_out
                aout = layer._activation(norm_out)
            else:
                # DO NOT NORMALIZE LAST LAYER OUTPUT
#                 print('hej')
                aout = layer._activation(ain)
            self.x[i] = aout
            input = aout

    def batchNormBackPass(self, G, s, mu, var):
#         print(G.shape,s.shape,mu.shape,var.shape)
        dj_dvar = np.zeros((G.shape[0]))
        dj_dmu = np.zeros((G.shape[0]))
        n = s.shape[1]
        for i in range(n):
            dj_dvar += G[:,i] * (1/np.cbrt(var+self.eps)) * (s[:,i] - mu)
            dj_dmu += G[:,i] * (1/np.sqrt(var+self.eps))
#         dj_dvar = np.sum(G * (1/np.cbrt(var+self.eps)), axis = 1, keepdims = True) * np.sum(s - mu, axis = 1, keepdims = True)
#         dj_dmu = np.sum(G * (1/np.sqrt(var+self.eps)), axis = 1)

        dj_dvar *= (-1/2)
        dj_dmu *= (-1)

        for i in range(n):
            a = G[:,i].dot(1/np.sqrt(var))
            b = (2/n) * dj_dvar * (s[:,i] - mu)
            c = (1/n) * dj_dmu
            G[:,i] = a + b + c
        return G

    def batchGradient(self,X,Y,GDparams):
        for layer in self.layers:
            layer.grad_W = np.zeros(layer.grad_W.shape)
            layer.grad_b = np.zeros(layer.grad_b.shape)
        n = X.shape[1]
        # ERROR LAST FIRST LAYER
        G = np.zeros(self.x[-2].shape)
        for i in range(n):
            g = -(Y[:,i] - self.x[-1][:,i]).reshape(1,-1) # 1x10
            xi = self.x[-2][:,i].reshape(-1,1) # 50x1
            s_hat = self.batch_norms[-2][:,i]
            # SET GRADIENTS
            self.layers[-1].grad_W += (np.dot(g.T, xi.T)/n) + 2*GDparams._lambda*self.layers[-1].W # 10x1 dot 1x50
            self.layers[-1].grad_b += (g.T/n)

            g = np.dot(g,self.layers[-1].W)
#             print(g.shape)
#             print(np.diag(s_hat).shape)
            G[:,i] = np.dot(g,np.diag(np.where(s_hat > 0, 1, 0)))
            # SCALE GRADIENTS
#       # KEEP ON BACKPROPAGATING THE ERROR THROUGH THE NETWORK
        for i in range(len(self.layers)-2,-1,-1):
            if i > 0:
#                 print('batch Norm backpass')
                G = self.batchNormBackPass(G, self.s[i], self.mu[i], self.var[i])
                # ISNERT BACKPROP HERE
#                 G = np.zeros(self.x[i-1].shape)
#                 print(self.layers[i].type)
                for j in range(n):
                    g = G[:,j].reshape(1,-1)
                    xi = self.x[i-1][:,j].reshape(-1,1) # 50x1
                    s_hat = self.batch_norms[i-1][:,j]
                    # SET GRADIENTS
                    self.layers[i].grad_W += (np.dot(g.T, xi.T)/n) + 2*GDparams._lambda*self.layers[i].W # 10x1 dot 1x50
                    self.layers[i].grad_b += (g.T/n)

                    g = np.dot(g,self.layers[i].W)
        #             print(g.shape)
        #             print(np.diag(s_hat).shape)
                    G[:,j] = np.dot(g,np.diag(np.where(s_hat > 0, 1, 0)))

            else:
                for j in range(n):
                    g = G[:,j].reshape(1,-1)
                    xj = X[:,j].reshape(-1,1) # 50x1
                    # SET GRADIENTS
                    self.layers[i].grad_W += (np.dot(g.T, xj.T)/n) #+ 2*GDparams._lambda*self.layers[j].W
                    self.layers[i].grad_b += (g.T/n)
#
#     def computeGradient(self, X, Y, GDparams):
#         for layer in self.layers:
#             layer.grad_W = 0
#             layer.grad_b = 0
#         self.error = -(Y -  self.activation_out[-1])
#         for i in range(len(self.layers)-1,-1,-1):
#             if i > 0:
#                 self.layers[i].grad_W = np.dot(self.error, self.activation_out[i-1].T)
#                 self.layers[i].grad_b = self.error.sum(axis=1).reshape(-1,1)
#                 self.error = np.dot(self.error.T, self.layers[i].W)
#                 # derivative
#                 self.error = np.where(self.activation_in[i-1] > 0, self.error.T, 0)
#             else: # LAST STEP. MULTIPLY BY INPUT
#                 self.layers[i].grad_W = np.dot(self.error, X.T)
#                 self.layers[i].grad_b = self.error.sum(axis=1).reshape(-1,1)
#
#             # NORMALIZE INPUT WEIGHTS AND BIAS
#             self.layers[i].grad_W *= (1/X.shape[1])
#             self.layers[i].grad_W +=  2 * GDparams._lambda * self.layers[i].W
#             self.layers[i].grad_b *= (1/X.shape[1])

    def computeCost(self, X, Y, _lambda, test_time = None):
        D = X.shape[1]
        if test_time == 'test_time':
            self.feedforward(X, 'test_time')
        else:
            self.feedforward(X)
        J = (1/D) * np.sum(self.l_cross(Y))  + _lambda * sum([np.sum(layer.W**2) for layer in self.layers])
        return J

    def l_cross(self, Y):
        idx = np.argmax(self.x[-1] * Y,axis = 0)
        cost_vec = np.zeros((1,idx.shape[0]))
        for i, p in enumerate(idx):
            cost_vec[0,i] = self.x[-1][p,i]
        return -np.log(cost_vec)

    def getMovingAvgs(self,j):
#         print(self.mu)
#         print(self.var)
        alpha = 0.99
        if j == 1:
            self.mu_avg = self.mu
            self.var_avg = self.var
        else:
            self.mu_avg = np.multiply(alpha, self.mu_avg) + np.multiply((1-alpha), self.mu)
            self.var_avg = np.multiply(alpha, self.var_avg) + np.multiply((1-alpha), self.var)

    def miniBatchGD(self, X, Y, GDparams, momentum):
        gamma = 0.9
        for j in range(1,int(X.shape[1]/GDparams.batch_size)+1):
            j_start = int((j-1)*GDparams.batch_size)
            j_end = int(j*GDparams.batch_size)
            Xs = X[:, j_start:j_end]
            Ys = Y[:, j_start:j_end]
            self.feedforward(Xs)
#             self.computeGradient(Xs, Ys, GDparams)
            self.batchGradient(Xs, Ys, GDparams)
#             self.compareGradient(Xs, Ys)
            # KEEP TRACK OF MOOVING AVERAGES OF MU AND VARIANCE
            self.getMovingAvgs(j)

            # UPDATE WEIGHTS
            if momentum:
                # USE MOMENTUM
                for layer in self.layers:
                    layer.velocity_W = gamma * layer.velocity_W + GDparams.eta * layer.grad_W
                    layer.velocity_b = gamma * layer.velocity_b + GDparams.eta * layer.grad_b
                    layer.W = layer.W - layer.velocity_W
                    layer.b = layer.b - layer.velocity_b
            else:
            # standard UPDATE WEIGHTS AND BIAS
                for layer in self.layers:
                    layer.W = layer.W - (GDparams.eta * layer.grad_W)
                    layer.b = layer.b - (GDparams.eta * layer.grad_b)

    def train(self, X_train, X_val, y_oh, Y_train, y_oh_val, Y_val, GDparams,init, momentum, check_gradients):
        J_train = np.zeros((GDparams.n_epochs,))
        J_val = np.zeros((GDparams.n_epochs,))
        acc = np.zeros(GDparams.n_epochs,)
        acc_val = np.zeros(GDparams.n_epochs,)
        best_acc = 0
        print('Start training... eta {}, lambda {}, init: {}'.format(GDparams.eta,GDparams._lambda,init))
        for epoch in range(GDparams.n_epochs):
            self.miniBatchGD(X_train, y_oh, GDparams, momentum)
            J_train[epoch] = self.computeCost(X_train, y_oh, GDparams._lambda, 'test_time')
            acc[epoch] = self.computeAccuracy(X_train,Y_train)

            J_val[epoch] = self.computeCost(X_val, y_oh_val, GDparams._lambda, 'test_time')
            acc_val[epoch] = self.computeAccuracy(X_val,Y_val)
            print('Epoch: ' + str(epoch) + '/' + str(GDparams.n_epochs) + ' Loss: ' + str(J_train[epoch]) + ' Acc: ' + str(acc[epoch]) + \
                                                                            ' Val-Loss: ' + str(J_val[epoch]) + ' Val-Acc: ' + str(acc_val[epoch]))
#             GDparams.eta = GDparams.eta * 0.95
            if check_gradients and epoch == 3:
                self.feedforward(X_train)
                self.batchGradient(X_train, y_oh, GDparams)
#                 self.computeGradient(X_train, y_oh, GDparams)
                self.compareGradient(X_train, y_oh)

        return J_train, J_val, acc, acc_val

def main():
    num_classes = 10
    num_hidden = 50
    features = 3072
    samples = 10000
    epochs = 10
    all_data = False
    momentum = True
    check_gradients = True

    import get_data as preprocess
    X_train,X_val,X_test,y_oh,y_oh_val,y_oh_test,Y_train,Y_val,Y_test = preprocess.get_data(all_data,features,samples,num_classes)

    ############# HYPER PARAMS ##############################
    etas = [0.02] # 0.02
    lambdas = [0.0] #[0.002]
    ##############################################################

    # SAVE ALL COSTS
    J_train = np.zeros((len(etas)*len(lambdas),epochs)) # cost vector for plotting
    J_val = np.zeros((len(etas)*len(lambdas),epochs)) # cost vector for plotting
    best_param_dict = {}

    init_list = ['random']

    for init in init_list:
        for i, hyperparam in enumerate(list(itertools.product(etas,lambdas))): # ~0.02 decent eta
            GDparams = Params(batch_size=100, n_epochs=epochs, eta=hyperparam[0], _lambda=hyperparam[1])
            NN = NeuralNetwork()
            layer0 = NeuronLayer(num_hidden,features,'relu', init)
            NN.add(layer0)
            layer1 = NeuronLayer(50,50,'relu', init)
            NN.add(layer1)
#             layer2 = NeuronLayer(30,50,'relu', init)
#             NN.add(layer2)
            layer3 = NeuronLayer(10,50,'softmax', init)
            NN.add(layer3)
            J_train[i], J_val[i], acc_vec, acc_val_vec = NN.train(X_train, X_val, y_oh, Y_train, y_oh_val, Y_val, GDparams,init,
                                                                                momentum,
                                                                                check_gradients)
            test_acc = NN.computeAccuracy(X_test,Y_test)
            print('test Acc: ' + str(test_acc) + ' eta: ' + str(hyperparam[0]) + ' lambda: ' + str(hyperparam[1]))
#         plt.plot(J_train, label = init)
#     plt.legend()
#     plt.show()
#         plot_cost(J_train, J_val, etas, lambdas)
#         plot_acc(acc_vec,acc_val_vec)

main()


from __future__ import unicode_literals
from django.shortcuts import render, render_to_response, redirect
from django.http import HttpResponse
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required
from django.core.paginator import Paginator
from django.core.paginator import EmptyPage
from django.core.paginator import PageNotAnInteger
from django.db.models import Q
import json, re, datetime, time
import monitorDataShow.config as config

# Create your views here.

def paginatorProcess(info, page):
    paginator = Paginator(info, 100)
    try:
        business_page = paginator.page(page)
    except PageNotAnInteger:
        business_page = paginator.page(1)
    except EmptyPage:
        business_page = paginator.page(paginator.num_pages)

    return business_page

class TimeFormat(object):
    def __init__(self):
        self.timeStr = datetime.datetime.now()

    def lastDay(self, days):
        return datetime.datetime.strftime(self.timeStr + datetime.timedelta(days=-days), '%Y-%m-%d')

    def ProcessFortTime(self, res):
        curr_month = datetime.datetime.strftime(self.timeStr, '%Y%m')
        if not res['start_time'] and not res['stop_time']:
            start_time = datetime.datetime.strftime(self.timeStr - datetime.timedelta(hours=3), '%Y-%m-%d %H:%M:00')
            stop_time = datetime.datetime.strftime(self.timeStr, '%Y-%m-%d %H:%M:00')
        else:
            start_time = datetime.datetime.strftime(datetime.datetime.strptime(res['start_time'], '%Y-%m-%dT%H:%M'),
                                                    '%Y-%m-%d %H:%M:00')
            stop_time = datetime.datetime.strftime(datetime.datetime.strptime(res['stop_time'], '%Y-%m-%dT%H:%M'),
                                                   '%Y-%m-%d %H:%M:00')
        return start_time, stop_time, curr_month

@login_required
def NginxAccessIP(request):
    page = request.GET.get('page')
    timeStr = TimeFormat().lastDay(1)
    selectSql = "select create_time, ip, count, region, city " \
                "from django_ipcount " \
                "where " \
                    "create_time=\'%s 00:00:00\' " \
                    "and ip!=\'200\' " \
                "ORDER BY -count;" % timeStr
    res = config.MysqlOp(project=config.nginxDBConfig).select(selectSql)
    businesses = paginatorProcess(res, page)
    return render(request, 'nginx_access_ip.html', {'business_info': businesses})

@login_required
def NginxAccessAPI(request):
    page = request.GET.get('page')
    timeStr = TimeFormat().lastDay(1)
    selectSql = "select create_time, dn, api, count " \
                "from django_apicount " \
                "where " \
                    "create_time=\'%s 00:00:00\' " \
                    "and api not IN ('/', '/admin/')" \
                "ORDER BY -count;" % timeStr
    res = config.MysqlOp(project=config.nginxDBConfig).select(selectSql)
    businesses = paginatorProcess(res, page)
    return render(request, 'nginx_access_api.html', {'business_info': businesses})

def getDataFromMysql(dn, res):
    start_time, stop_time, curr_month = TimeFormat().ProcessFortTime(res)
    selectSQL = "SELECT re.create_time, re.api, re.avg " \
                "FROM requesttime_%s as re INNER JOIN " \
                "(SELECT api, max(avg) as avg " \
                "FROM requesttime_%s " \
                "WHERE domain_name = \'%s\' " \
                "AND create_time >= \'%s\' " \
                "AND create_time <= \'%s\' " \
                "GROUP BY api " \
                "order by max(avg) desc limit 10) as a  on a.api = re.api " \
                "where domain_name = \'%s\' " \
                "AND create_time >= \'%s\' " \
                "AND create_time <= \'%s\' " \
                "ORDER BY re.create_time;" % (curr_month, curr_month, dn, start_time, stop_time, dn, start_time, stop_time)
    return config.MysqlOp(project=config.nginxDBConfig).select(selectSQL)

def processData(dataRes):
    SourceDataTimeList = list()
    SourceDataApiList = list()
    ResTimeList = list()
    ResApiList = list()
    ResRequestTimeList = list()

    for SourceTimeStr, SourceApiStr, _ in dataRes:
        if SourceTimeStr not in SourceDataTimeList:
            SourceDataTimeList.append(SourceTimeStr)
        if SourceApiStr not in SourceDataApiList:
            SourceDataApiList.append(SourceApiStr)

    DefaultList = [[x, y, 0] for x in SourceDataTimeList for y in SourceDataApiList]

    for data in dataRes:
        for result in DefaultList:
            index = DefaultList.index(result)
            if list(data[0:2]) == result[0:2]:
                DefaultList[index][-1] = data[-1]
                break

    for ResTimeStr, ResApiStr, ResrequestStr in DefaultList:
        if ResTimeStr.strftime('%Y-%m-%d %H:%M') not in ResTimeList:
            ResTimeList.append(ResTimeStr.strftime('%Y-%m-%d %H:%M'))
        if ResApiStr not in ResApiList:
            ResApiList.append(ResApiStr)
            ResRequestTimeList.append([ResrequestStr])
        else:
            ResRequestTimeList[ResApiList.index(ResApiStr)].append(ResrequestStr)

    return ResTimeList, ResApiList, ResRequestTimeList

@login_required
def NginxAccessRequestTime(request):
    if request.method == 'POST':
        res = json.loads(request.body)
        if res['name'] == 'dinghuoapi':
            dataRes = getDataFromMysql('dinghuo.api.huimin100.cn', res)
            dateList, apiList, requestList = processData(dataRes)
            data = json.dumps({"data": [dateList, apiList, requestList]})
            return HttpResponse(data, content_type='application/json')
        elif res['name'] == 'loginapi':
            dataRes = getDataFromMysql('login.api.huimin100.cn', res)
            dateList, apiList, requestList = processData(dataRes)
            data = json.dumps({"data": [dateList, apiList, requestList]})
            return HttpResponse(data, content_type='application/json')
        elif res['name'] == 'productapi':
            dataRes = getDataFromMysql('product.api.huimin100.cn', res)
            dateList, apiList, requestList = processData(dataRes)
            data = json.dumps({"data": [dateList, apiList, requestList]})
            return HttpResponse(data, content_type='application/json')
        elif res['name'] == 'pcshop':
            dataRes = getDataFromMysql('pcshop.huimin100.cn', res)
            dateList, apiList, requestList = processData(dataRes)
            data = json.dumps({"data": [dateList, apiList, requestList]})
            return HttpResponse(data, content_type='application/json')
        elif res['name'] == 'client':
            dataRes = getDataFromMysql('client.huimin100.cn', res)
            dateList, apiList, requestList = processData(dataRes)
            data = json.dumps({"data": [dateList, apiList, requestList]})
            return HttpResponse(data, content_type='application/json')
    return render(request, 'nginx_access_request.html')


from ..internals.cache.redis import delete_keys
from ..internals.database.database import get_cursor, get_conn, return_conn
from shutil import rmtree
from os.path import join
import config

def delete_post_cache_keys(service, artist_id, post_id):
    keys = [
        'post:' + service + ':' + str(artist_id) + ':' + str(post_id)
    ]

    delete_keys(keys)

def delete_all_post_cache_keys():
    keys = ['all_post_keys']

    delete_keys(keys)

def post_exists(service, artist_id, post_id):
    cursor = get_cursor()
    cursor.execute("SELECT * FROM posts WHERE id = %s AND \"user\" = %s AND service = %s", (post_id, artist_id, service,))
    return len(cursor.fetchall()) > 0

def remove_post_if_flagged_for_reimport(service, user, post):
    conn = get_conn()
    cursor = get_cursor()

    cursor.execute('SELECT * FROM booru_flags WHERE service = %s AND "user" = %s AND id = %s', (service, user, post))
    existing_flags = cursor.fetchall()
    if len(existing_flags) == 0:
        return

    cursor.execute('DELETE FROM booru_flags WHERE service = %s AND "user" = %s AND id = %s', (service, user, post))
    cursor.execute('DELETE FROM posts WHERE service = %s AND "user" = %s AND id = %s', (service, user, post))
    conn.commit()
    return_conn(conn)
    rmtree(join(
        config.download_path,
        'attachments',
        '' if service == 'patreon' else service,
        user,
        post
    ), ignore_errors=True)
    rmtree(join(
        config.download_path,
        'files',
        '' if service == 'patreon' else service,
        user,
        post
    ), ignore_errors=True)


# coding: utf-8

# # Take a look at channel 1
#
# * [Modis channel listing](https://modis.gsfc.nasa.gov/about/specifications.php)
#
# * Band 1 centered at 6.45 microns (red)
#
# * data acquired at 250 m resolution, resampled to 1 km for the MYD021KM file

# In[1]:

from a301utils.a301_readfile import download
import numpy as np
import h5py
import sys
import a301lib
from a301lib.geolocate import fast_hist, fast_avg, fast_count,make_plot
from a301lib.radiation import planckInvert
from matplotlib import pyplot as plt

filename = 'MYD021KM.A2016136.2015.006.2016138123353.h5'
download(filename)
h5_file=h5py.File(filename)

# In[2]:

filename='MYD03.A2016136.2015.006.2016138121537.h5'
download(filename)
geo_file = h5py.File(filename)
lon_data=geo_file['MODIS_Swath_Type_GEO']['Geolocation Fields']['Longitude'][...]
lat_data=geo_file['MODIS_Swath_Type_GEO']['Geolocation Fields']['Latitude'][...]
from a301lib.geolocate import find_corners
corners=find_corners(lat_data,lon_data)

# Here is the corresponding red,green,blue color composite for the granule.

# In[3]:

from IPython.display import Image
Image(url='http://clouds.eos.ubc.ca/~phil/courses/atsc301/downloads/aqua_136_2015.jpg',width=600)

# ### **Read the radiance data from MODIS_SWATH_Type_L1B/Data Fields/EV_1KM_RefSB**
#
# #### Both radiance and reflectance calibratiions are included

# In[4]:

get_ipython().magic('matplotlib inline')
index=0
my_name = 'EV_1KM_RefSB'
chan1=h5_file['MODIS_SWATH_Type_L1B']['Data Fields'][my_name][index,:,:]
scale=h5_file['MODIS_SWATH_Type_L1B']['Data Fields'][my_name].attrs['reflectance_scales'][...]
offset=h5_file['MODIS_SWATH_Type_L1B']['Data Fields'][my_name].attrs['reflectance_offsets'][...]
chan1_calibrated =(chan1 - offset[index])*scale[index]
plt.hist(chan1_calibrated.ravel())
ax=plt.gca()  #get current axis
_=ax.set(title='channel 1 reflectance for Ft. McMurray')

# Now call the planckInvert function imported at the top of the notebook to convert radiance to brightness temperature

# In[5]:

fig,ax = plt.subplots(1,1,figsize = (10,14))
CS=ax.imshow(chan1_calibrated)
cax=fig.colorbar(CS)
ax.set_title('Channel 1 reflectance, Ft. McMurray')
out=cax.ax.set_ylabel('Chan 1 reflectance$)')
out.set_rotation(270)
out.set_verticalalignment('bottom')

# In[6]:

lon_min= -144
lon_max = -92

lat_min = 47
lat_max = 70
binsize = 0.1

lon_hist = fast_hist(lon_data.ravel(),lon_min,lon_max,binsize=binsize)
lat_hist =  fast_hist(lat_data.ravel(),lat_min,lat_max,binsize=binsize)
gridded_image = fast_avg(lat_hist,lon_hist,chan1_calibrated.ravel())

# In[7]:

lat_centers=(lat_hist['edges_vec'][1:] + lat_hist['edges_vec'][:-1])/2.
lon_centers=(lon_hist['edges_vec'][1:] + lon_hist['edges_vec'][:-1])/2.
lon_array,lat_array=np.meshgrid(lon_centers,lat_centers)
print(lon_array.shape)
masked_reflects = np.ma.masked_invalid(gridded_image)

# In[8]:

from matplotlib import cm
from matplotlib.colors import Normalize

cmap=cm.YlGnBu_r  #see http://wiki.scipy.org/Cookbook/Matplotlib/Show_colormaps
cmap.set_over('w')
cmap.set_under('b',alpha=0.1)
cmap.set_bad('0.75') #75% grey
#
# set the range over which the pallette extends so I use
# use all my colors on data 10 and 40 degrees centigrade
#
vmin= 0.0
vmax= 0.4
the_norm=Normalize(vmin=vmin,vmax=vmax,clip=False)
fig,ax = plt.subplots(1,1,figsize=(14,18))
corners['ax'] = ax
corners['resolution']='l'
corners['projection']='lcc'
corners['urcrnrlon'] = -70.
corners['urcrnrlat'] = 65.
corners['llcrnrlat'] = 46.
corners['llcrnrlon'] = -140.
proj = make_plot(corners)
lat_centers=(lat_hist['edges_vec'][1:] + lat_hist['edges_vec'][:-1])/2.
lon_centers=(lon_hist['edges_vec'][1:] + lon_hist['edges_vec'][:-1])/2.
lon_array,lat_array=np.meshgrid(lon_centers,lat_centers)
#
# translate every lat,lon pair in the scene to x,y plotting coordinates
# for th Lambert projection
#
x,y=proj(lon_array,lat_array)
CS=proj.pcolormesh(x, y,masked_reflects, cmap=cmap, norm=the_norm)
CBar=proj.colorbar(CS, 'right', size='5%', pad='5%',extend='both')
CBar.set_label('Channel 1 reflectance',
               rotation=270,verticalalignment='bottom',size=18)
_=ax.set_title('Modis Channel 1, May 15, 2016 -- Fort McMurray',size=22)

# ### back to channel 31

# In[9]:

index31=10

my_name = 'EV_1KM_Emissive'
chan31=h5_file['MODIS_SWATH_Type_L1B']['Data Fields'][my_name][index31,:,:]
scale=h5_file['MODIS_SWATH_Type_L1B']['Data Fields']['EV_1KM_Emissive'].attrs['radiance_scales'][...]
offset=h5_file['MODIS_SWATH_Type_L1B']['Data Fields']['EV_1KM_Emissive'].attrs['radiance_offsets'][...]
chan31_calibrated =(chan31 - offset[index31])*scale[index31]

# In[10]:

wavel=11.e-6  #chan 31 central wavelength, meters
chan31_mks = chan31_calibrated*1.e6  #W/m^2/m/sr
Tbright = planckInvert(wavel,chan31_mks)
Tbright = Tbright - 273.15 #convert to Centigrade

# In[11]:

plt.hist(Tbright.ravel())
ax=plt.gca()  #get current axis
_=ax.set(title='channel 31 brightness temperatures for Ft. McMurray')

# ### now do a heatmap by binning temperatures and reflectivities in a 2D histogram
#
# #### put reflectivities on the rows and temperatures on the columns so hist is 30 x 40

# In[12]:

temp_min= -30
temp_max = 40
num_temp_bins=40

refl_min = 0
refl_max = 0.7
num_refl_bins=30

temp_hist = fast_hist(Tbright.ravel(),temp_min,temp_max,numbins=num_temp_bins)
refl_hist =  fast_hist(chan1_calibrated.ravel(),refl_min,refl_max,numbins=num_refl_bins)
heatmap = fast_count(refl_hist,temp_hist)

# In[13]:

_=plt.imshow(heatmap)

# In[14]:

plt.pcolormesh(heatmap)
_=plt.colorbar()

# In[15]:

with np.errstate(divide='ignore'): #use a context manager (with)
    log_counts=np.log10(heatmap)  #turn off divide by zero warning
masked_counts = np.ma.masked_invalid(log_counts)
plt.pcolormesh(masked_counts)
_=plt.colorbar()

# In[ ]:


import logging

from random import randint

from flask import Flask, render_template

from flask_ask import Ask, statement, question, session
import requests

app = Flask(__name__)

ask = Ask(app, "/")

logging.getLogger("flask_ask").setLevel(logging.DEBUG)

@ask.launch

def new_game():

    welcome_msg = render_template('welcome')

    return question(welcome_msg).reprompt(render_template('reprom'))

@ask.intent("SalesIntent",convert={'InvOne': str, 'InvTwo': str, 'csname':str},default={'InvOne': 'None','InvTwo': 'None','csname': 'None'})

def Sales(InvOne, InvTwo,csname):
    print(csname)
    final_sentence = ""
    if InvOne != 'None' and InvTwo != 'None' and csname == 'None':
        url = "http://122.179.136.81:1531/henkel/sale"
        params = {"SalOne": InvOne, "SalTwo": InvTwo}
        data = requests.get(url=url, params=params)
        data = data.json()
        final_sentence = data["Response"]
    elif InvOne == 'None' and InvTwo == 'None' and csname != 'None':
        url = "http://122.179.136.81:1531/henkel/customer"
        params = {"CusId": str(csname)}
        data = requests.get(url=url, params=params)
        data = data.json()
        final_sentence = data["Response"]
    else:
        final_sentence = "Kindly provide customer name"
    return question(final_sentence).reprompt(render_template('reprom'))

@ask.intent("InventoryIntent", convert={'InvOne': str, 'InvTwo': str})

def inventory(InvOne, InvTwo):
    final_sentence = ""
    url = "http://122.179.136.81:1531/henkel/inventory"
    params = {"InvOne":InvOne,"InvTwo":InvTwo}
    data = requests.get(url=url,params=params)
    data = data.json()
    final_sentence = data["Response"]
    return question(final_sentence).reprompt(render_template('reprom'))

@ask.intent("OpenOrdersIntent", convert={'InvOne': str, 'InvTwo': str,'InvThree':str},default={'InvThree': 'None'})

def order(InvOne, InvTwo,InvThree):
    final_sentence = ""
    url = "http://122.179.136.81:1531/henkel/order"
    params = {"OrdOne":InvOne,"OrdTwo":InvTwo,"OrdThree":InvThree}
    data = requests.get(url=url,params=params)
    data = data.json()
    final_sentence = data["Response"]
    return question(final_sentence).reprompt(render_template('reprom'))

@ask.intent("AMAZON.StopIntent")

def stop():
    final_sentence = "Alright then, Thank you for using insight bot. Bye."
    return statement(final_sentence)

@ask.intent("AMAZON.YesIntent")

def yes():
    final_sentence = "Okay. what else you want to know ?"
    return question(final_sentence).reprompt(render_template('reprom'))

@ask.intent("CustomerIntent",convert={'number': str},default={'number': 'None'})

def customer_val(csname):
    print(csname)
    final_sentence = ""
    url = "http://122.179.136.81:1531/henkel/customer"
    params = {"CusId": csname}
    data = requests.get(url=url, params=params)
    data = data.json()
    final_sentence = data["Response"]
    return question(final_sentence)

@ask.intent("AMAZON.NoIntent")

def no():
    final_sentence = "Alright then, Thank you for using insight bot. Bye."
    return question(final_sentence)

# @ask.intent("AMAZON.NoIntent")
#
# def no():
#     final_sentence = "Alright then, Thank you for using henkel bot. Bye Bye"
#     return statement(final_sentence)

if __name__ == '__main__':

    app.run(debug=True)


import json
from sys import argv
import time as dt
import urllib.request
import zipfile

data=[]
webpage = "http://data.itsfactory.fi/journeys/api/1/vehicle-activity"
count = 0
i = 0
while i < int(argv[3]):
    websource = urllib.request.urlopen(webpage)
    jsonData = json.loads(websource.read().decode())
    dt.sleep(int(argv[2]))
    i = i + int(argv[2])
    for key in jsonData:
        count = count + 1
        if count >= 3:
            value = jsonData[key]
            data.append(value)

with open(argv[1], "w", encoding="UTF-8") as outfile:
    json.dump(data, outfile, indent=2)

with zipfile.ZipFile ("busdata.zip","w") as myzip:
    myzip.write(argv[1])


from django.conf.urls import url

from . import views

app_name = "twitter"

urlpatterns = [
    url(regex=r"^$", view=views.HomeView.as_view(), name="home"),
    url(regex=r"^likes/$", view=views.FavoriteListView.as_view(), name="favorite_list"),
    url(
        regex=r"^(?P<screen_name>\w+)/$",
        view=views.UserDetailView.as_view(),
        name="user_detail",
    ),
    url(
        regex=r"^(?P<screen_name>\w+)/likes/$",
        view=views.AccountFavoriteListView.as_view(),
        name="account_favorite_list",
    ),
    url(
        regex=r"^(?P<screen_name>\w+)/(?P<twitter_id>\d+)/$",
        view=views.TweetDetailView.as_view(),
        name="tweet_detail",
    ),
]


from django.test import TestCase
from django.test.client import Client
from persons.models import Person as P

class PersonTestCase(TestCase):
    def setUp(self):
        P.objects.create(short_name='mwon', full_name='Michael Won')
        P.objects.create(short_name='jim12', full_name='James D. Costning',
                                email='jim12@jimbo.people.we.com')
        P.objects.create(short_name='world_master')

    def test_persons_count(self):
        self.assertEqual(P.objects.count(), 3)

    def test_persons_fields(self):
        persons = P.objects.all()
        correct_values = {
            'mwon' : {'str' : 'mwon (Michael Won)', 'email' : None},
            'jim12' : {'str' : 'jim12 (James D. Costning)',
                                'email' : 'jim12@jimbo.people.we.com'},
            'world_master' : {'str' : 'world_master', 'email' : None},
        }

        for p in persons:
            vals = correct_values[p.short_name]
            self.assertEqual(str(p), vals['str'])
            self.assertEqual(p.email, vals['email'])


# -*- coding: utf-8 -*-
"""
Created on Wed Dec 13 16:50:28 2017

@author: Sian JM Brooke
"""
import numpy as np
from sklearn import preprocessing
from scipy.stats import boxcox, gamma

### Log Transformation ###
var2log = "Twitter Gender" #Col name to transform
logcol = "Twitter Gender Log" #name for new col

tgen = wgh
tgen[var2log] = tgen[var2log].dropna().reset_index(drop=True)
tgen = tgen[(tgen[var2log] != 0)]

#tgen[logcol] = np.log(tgen[var2log])
#https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.log.html

#tgen[logcol].hist()
#print("Skew before:",tgen[var2log].skew(), "Skew After:", tgen[logcol].skew())

varpro = "Twitter Gender"
tgen2 = wgh
tgen3 = tgen2[varpro].dropna().reset_index(drop=True)

### Preprocessing data from sklearn
centscale = preprocessing.scale(tgen3)
dt = pd.DataFrame(data=centscale)
#print(dt.hist())

tbxcx = preprocessing.scale(boxcox(tgen3+1)[0])
dt2 = pd.DataFrame(data=tbxcx)
#print(dt2.hist())
#bxcx = boxcox(tgen3)

bnry = ["Male", "Female"]
forbinry = wgh[wgh.Gender.isin(bnry)].reset_index(drop=True)

forr = forbinry[["Twitter Gender", "Twitter Hostility",
            "Twitter Help Offering", "Twitter Help Seeking"]].copy()
forr = forr.fillna(0)
### Preprocessing data from sklearn
def prepro(i):
    centscale = preprocessing.scale(forr[i])
    forr[i] = centscale
    return
prepro(list(forr))


import requests
import sys
import csv
import re
from bs4 import BeautifulSoup
docterNameArray=[]
i=1
# fp=open("Plastic_Surgery_India.csv","w")
# url="https://find.plasticsurgery.org/country/india/?page=1"
fp=open("Plastic_Surgery_UAE.csv","w")

csvwriter = csv.writer(fp)
csvwriter.writerow(['Firstname','Lastname','degree','address','pincode','telephone'])
# print(len(data))
p = re.compile(r'^(\s+)?(Mr(\.)?|Mrs(\.)?)?(?P<FIRST_NAME>.+)(\s+)(?P<LAST_NAME>.+)$', re.IGNORECASE)

k=0
for k in range(0,5):

	url="https://find.plasticsurgery.org/?q=United%2BArab%2BAmirati&page=1"
	# print(url)
	result=requests.get(url)
	src = result.content
	soup = BeautifulSoup(src, "lxml")
	data=soup.find_all("div",{"class":"resultSet col-md-6 col-xs-12"})
	def getPlasticSurgeryData(data):
		for city in data:
			value=city.find_all("div",{"class":"col-xs-12"})
			for content in value:
				docterName=content.find("span",{"itemprop":"givenName"}).text
				address=content.find("p",{"itemprop":"address"}).text
				address=' '.join(address.split())
				telephone=re.sub('[^A-Za-z0-9]+', '',content.find("p",{"itemprop":"telephone"}).text)
				if(len(telephone)==12):
					telephone=telephone[2:len(telephone)]
				if(len(telephone)==13):
					telephone=telephone[2:len(telephone)]
				if(len(telephone)==14):
					telephone=telephone[2:len(telephone)]

				data=docterName.split(",")
				m = p.match(data[0])
				if(m != None):
					first_name = m.group('FIRST_NAME')
					last_name = m.group('LAST_NAME')
				degree=data[1]

				if(address!=None):
					zipcode = re.search(r'\d{6}(?:-\d{4})?(?=\D*$)', address)
					if(zipcode!=None):
						pincode=zipcode.group()
					else:
						pincode="-"

				if(len(telephone)>8):
					print(telephone)
					telephone='+'+telephone

				elif(len(telephone)<=8):
					telephone='+971'+telephone

				if(docterName not in docterNameArray):
					docterNameArray.append(docterName)
					csvwriter = csv.writer(fp)
					csvwriter.writerow([first_name,last_name,degree,address,pincode,telephone])

	getPlasticSurgeryData(data)
	i=1
	nextPageData=soup.find("div",{"class":"col-xs-12 results-pager"})
	while(nextPageData.find("a",{"rel":"next"}).has_attr('href')):
		i=i+1
		url="https://find.plasticsurgery.org/?q=United%2BArab%2BAmirati&page="+str(i)
		# print(url)
		result=requests.get(url)
		src = result.content
		soup = BeautifulSoup(src, "lxml")
		data=soup.find_all("div",{"class":"resultSet col-md-6 col-xs-12"})
		getPlasticSurgeryData(data)
		nextPageData=soup.find("div",{"class":"col-xs-12 results-pager"})

print(len(docterNameArray))


''' This program will use a set of existing messages stored in a database to develop a ML model
    based on how the messages are classified. Then the  model will be stored in a pickle file
    that should be specified in the arguments.

    Parameters:
    argument 1: Database file name
    arugment 2: Pickle file name

    Returns:
    GridSearchCV: trained model

'''

import sys
import pandas as pd
import numpy as np
from sqlalchemy import create_engine
import re
import nltk
from nltk.tokenize import word_tokenize, sent_tokenize
from nltk.stem import WordNetLemmatizer
from nltk.corpus import stopwords
from sklearn.pipeline import Pipeline
from sklearn.feature_extraction.text import CountVectorizer, TfidfTransformer
from sklearn.multioutput import MultiOutputClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from sklearn.model_selection import GridSearchCV
import pickle
nltk.download('punkt')
nltk.download('wordnet')
nltk.download('stopwords')

def load_data(database_filepath):
    ''' Loads data from the database and split messages and categoreis

    Parameters:
        database_filepath: The file path of the database to load dataframe

    Returns:
        X: dataframe of the messages
        Y: dataframe of the categories
        category_names: the names of the categories columns
    '''

    #Create an engine and load database
    engine = create_engine('sqlite:///{}'.format(database_filepath))
    df = pd.read_sql_table("MessagesTable2",engine)

    #X is the messages, Y is the categories
    X = df['message']
    Y = df.drop(['id','message','original','genre'],axis=1)
    category_names = Y.columns

    return X,Y,category_names

def tokenize(text):
    ''' takes a text  and normalize,lemmatize, and tokenize it.

    Parameters:
        text: the text string

    Returns:
        tokens: List of the tokenized lemmatize and normalized
    '''

    text = re.sub(r'[^a-zA-Z0-9]',' ',text.lower())
    stop_words = stopwords.words("english")
    tokens = word_tokenize(text)
    lemmatizer = WordNetLemmatizer()
    tokens = [lemmatizer.lemmatize(word).strip() for word in tokens if word not in stop_words]
    return tokens

def build_model():
    ''' this function builds the pipeline and gridsearchcv for the model

    Returns:
        cv: GridSearchCV using pipeline and parameters
    '''
    #build the pipeline
    pipeline = Pipeline([
        ('vect', CountVectorizer(tokenizer=tokenize)),
        ('tfidf',TfidfTransformer()),
        ('clf',MultiOutputClassifier(RandomForestClassifier()))
    ])

    # I tried multiple parameters for the GridSearchCV
    # to shorten the time some of those are commented out since GridSearch took forever to finish
    parameters = {
        'vect__max_df':(0.75,1),
#        'vect__max_features':(None,2000),
#         'vect__ngram_range': ((1, 1), (1, 2)),
#         'tfidf__use_idf': (True),
#         'tfidf__norm': ('l1', 'l2'),
#         'clf__n_jobs':(1,2),
#         'clf__estimator__n_estimators': [50, 100],
#         'clf__estimator__n_estimators': [75, 100],
#         'clf__estimator__min_samples_split': [2, 4],
#         'clf__estimator__criterion':('gini', 'entropy'),
    }

    cv = GridSearchCV(pipeline, param_grid=parameters)
    return cv

# method to convert the predected array to dataframe
def convert_to_df(Y_pred,columns):
    ''' convert an array to dataframe

    Parameters:
        Y_pred: predicted values in array format
        columns: names of the columns

    Returns:
        df: reutns a dataframe constructed from the predicted array
    '''
    df = pd.DataFrame(Y_pred)
    df.columns = columns
    return df

def evaluate_model(model, X_test, Y_test, category_names):
    '''evaluate the model and pring out the resutls of the classificaiton

    Parameters:
        model: the trained model
        X_test: the messages to classify
        Y_test: the actual results of the classificaiton
        category_names: the names of the categories
    '''
    Y_pred = model.predict(X_test)
    Y_pred_df = convert_to_df(Y_pred,category_names)
    for column in Y_test.columns:
        names= [column+"-0",column+"-1",column+"-2"]
        print(classification_report(np.array(Y_test[column]), np.array(Y_pred_df[column]),target_names=names))

def save_model(model, model_filepath):
    '''Save the model inot a pickle file
    Parametesr:
        model: The model to save
        model_filepath: the path to save the pickle file in and the name of the file
    '''
    pickle.dump(model, open(model_filepath, 'wb'))

def main():
    if len(sys.argv) == 3:
        database_filepath, model_filepath = sys.argv[1:]
        print('Loading data...\n    DATABASE: {}'.format(database_filepath))
        X, Y, category_names = load_data(database_filepath)
        X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2)

        print('Building model...')
        model = build_model()

        print('Training model...')
        model.fit(X_train, Y_train)

        print('Evaluating model...')
        evaluate_model(model, X_test, Y_test, category_names)

        print('Saving model...\n    MODEL: {}'.format(model_filepath))
        save_model(model, model_filepath)

        print('Trained model saved!')

    else:
        print('Please provide the filepath of the disaster messages database '\
              'as the first argument and the filepath of the pickle file to '\
              'save the model to as the second argument. \n\nExample: python '\
              'train_classifier.py ../data/DisasterResponse.db classifier.pkl')

if __name__ == '__main__':
    main()


import pickle as pk
import cv2
import dlib
from align import AlignDlib
from load_data import load_metadata
import numpy as np
from model import create_model
import time

def load_image(path):
    img = cv2.imread(path, 1)
    # OpenCV loads images with color channels
    # in BGR order. So we need to reverse them
    return img[...,::-1]

def dbEncodings_to_Vect(database):
    db_vect = []
    names = []
    for (name,db_enc) in database:
        names.append(name)
        db_vect.append(db_enc)
    db_vect = np.array(db_vect).T       # db_vect.shape = (128, len(db))
    return names, db_vect

#Initializing camera input
camera_port = 0
camera = cv2.VideoCapture(camera_port)

# Open database
database = pk.load(open("database.pkl", "rb"))
# Make encoding vectors
names, db_vect = dbEncodings_to_Vect(database)

# Initialize the OpenFace face alignment utility
alignment = AlignDlib('models/landmarks.dat')

#Create NN model
model = create_model()
model.load_weights('weights/nn4.small2.v1.h5')

# New time record :  0.41 on Asus A555L without GPU acceleration
bb = None # Setting the bounding_box as None

while True:
    start_time = time.time()

    # Reading image from webcam
    retval, image = camera.read()
    image = image[...,::-1] #Now in RGB
    image = cv2.resize(image, (300, 300))
    temp_image = image

    try :

        # 1. IMAGE --> EMBEDDING
        # Turn and crop the face
        image, bb = alignment.align(96, image, landmarkIndices=AlignDlib.OUTER_EYES_AND_NOSE) #image.shape = (96, 96, 3)
        #image = misc.imresize(image, (96, 96))

        # Scale RGB values to interval [0,1]
        image = (image / 255.).astype(np.float32)
        # Obtain the embedding vector for image
        encoded_image = model.predict(np.expand_dims(image, axis=0))[0]   # Time taken = 0.04

        # 2.EMBEDDING --> DISTANCES
        encoded_image = np.reshape(encoded_image, (128,1))
        # Computing Dictances
        dist_vect = np.sum(np.square(np.subtract(db_vect, encoded_image)), axis=0, dtype=np.float32, keepdims=True)
        dist_vect = np.squeeze(dist_vect)

        # 3.DISTANCES --> NAME
        result = dist_vect.min()
        if(result < 0.3):
            predict_name = names [ list(dist_vect).index(result) ]
            print(predict_name, result)
        else:
            predict_name = "Unknown"

    except:
        print ("Face not detected !")

    #Draw the bounding box
    if (bb is not None):
        cv2.rectangle(temp_image, (bb.left(), bb.top()) ,(bb.right(), bb.bottom()), (0,255,0),1)
        cv2.putText(temp_image, predict_name+" : "+str(result)+"%", (bb.left(),bb.top()-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,255,0))

    #Display the image with bounding box
    temp_image = temp_image[...,::-1] #Now in BGR
    cv2.imshow("Window", temp_image)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

    print("Time elapsed : ", time.time() - start_time)

camera.release()
cv2.destroyAllWindows()


import numpy as np
from tqdm.auto import tqdm

from globals import components_layer, nums_L0, datasets
from Sampler import Sampler

class SizeEstimatorSimulator(object):
    def __init__(self):
        pass

    @classmethod
    def get_error(cls, real_value, estimation):
        return 100 * abs(real_value - estimation) / real_value

    @classmethod
    def compute_size_for_dataset(cls,
                                 exp_id,
                                 dataset,
                                 l1_size_range,
                                 l0_method="greedy",
                                 l0_size=-1):
        print(f"Starting experiment #{exp_id}")
        title = dataset.title
        sampler = Sampler()
        sampler.init(title)
        sampler.generate_L0(l0_method, l0_size)
        high_subgraph = sampler.high_subgraph
        layer_manager = sampler.layer_manager
        size_estimator = sampler.size_estimator
        statistics = sampler.statistics
        ground_truth_avg_deg_down = statistics.avg_degrees()[1]

        layers = layer_manager.get_layers(components_layer[title])

        nodes_from_L1 = np.random.choice(high_subgraph.L1_list,
                                         min([len(high_subgraph.L1_list), max(l1_size_range)]),
                                         replace=False).tolist()
        L1_total_size = len(high_subgraph.L1_list)
        first_two_layers_size = len(high_subgraph.L0_list) + L1_total_size
        real_size = len(layers[-1]) + first_two_layers_size

        size_estimator.update_up(nodes_from_L1[:l1_size_range[0]])
        estimated_up_degs = [size_estimator.estimate_average_deg_up()]
        for i in tqdm(range(1, len(l1_size_range))):
            size_estimator.update_up(nodes_from_L1[l1_size_range[i-1]:l1_size_range[i]])
            estimated_up_degs.append(size_estimator.estimate_average_deg_up())

        sizes = [L1_total_size * deg_up / ground_truth_avg_deg_down for deg_up in estimated_up_degs]

        print("Done computing estimated sizes")
        return [cls.get_error(real_value=real_size, estimation=estimated_l2_size + first_two_layers_size) for estimated_l2_size in sizes]

    @classmethod
    def compute_size_for_dataset_l2_samples(cls,
                                            exp_id,
                                            dataset,
                                            l2_size_range,
                                            l0_method="greedy",
                                            l0_size=-1):
        print(f"Starting experiment #{exp_id}")
        title = dataset.title
        sampler = Sampler()
        sampler.init(title)
        sampler.generate_L0(l0_method, l0_size)
        high_subgraph = sampler.high_subgraph
        size_estimator = sampler.size_estimator
        statistics = sampler.statistics
        ground_truth_avg_deg_up = statistics.avg_degrees()[0]

        nodes, reachabilities = statistics.all_component_nodes_and_reachabilities()
        normed_reachabilities = np.array(reachabilities) / np.sum(reachabilities)

        nodes_from_comps = np.random.choice(nodes, max(l2_size_range), p=normed_reachabilities).tolist()

        first_two_layers_size = len(high_subgraph.L0_list) + len(high_subgraph.L1_list)
        real_size = len(nodes) + first_two_layers_size

        size_estimator.update_down(nodes_from_comps[:l2_size_range[0]])
        estimated_down_degs = [size_estimator.estimate_average_deg_down()]
        for i in tqdm(range(1, len(l2_size_range))):
            size_estimator.update_down(nodes_from_comps[l2_size_range[i - 1]:l2_size_range[i]])
            estimated_down_degs.append(size_estimator.estimate_average_deg_down())

        sizes = [len(high_subgraph.L1_list) * ground_truth_avg_deg_up / deg_down for deg_down in estimated_down_degs]

        print("Done computing estimated sizes")
        return [cls.get_error(real_value=real_size, estimation=estimated_l2_size + first_two_layers_size) for
                estimated_l2_size in sizes]

if __name__ == "__main__":
    x = SizeEstimatorSimulator.compute_size_for_dataset("Up", datasets[0], range(100, 1001, 100))
    print(x)
    x = SizeEstimatorSimulator.compute_size_for_dataset_l2_samples("Down", datasets[0], range(100, 1001, 100))
    print(x)


import numpy
import matplotlib.pyplot as plt
plt.switch_backend('agg')

import os

import clawpack.geoclaw.units as units
import clawpack.geoclaw.surge.storm as storm

from load_synthetic_storms import load_other_chaz_storms
from load_synthetic_storms import load_chaz_storms

from netCDF4 import Dataset

def ncdump(nc_fid, verb=True):
    '''
    ncdump outputs dimensions, variables and their attribute information.
    The information is similar to that of NCAR's ncdump utility.
    ncdump requires a valid instance of Dataset.

    Parameters
    ----------
    nc_fid : netCDF4.Dataset
        A netCDF4 dateset object
    verb : Boolean
        whether or not nc_attrs, nc_dims, and nc_vars are printed

    Returns
    -------
    nc_attrs : list
        A Python list of the NetCDF file global attributes
    nc_dims : list
        A Python list of the NetCDF file dimensions
    nc_vars : list
        A Python list of the NetCDF file variables
    '''
    def print_ncattr(key):
        """
        Prints the NetCDF file attributes for a given key

        Parameters
        ----------
        key : unicode
            a valid netCDF4.Dataset.variables key
        """
        try:
            print("\t\ttype:", repr(nc_fid.variables[key].dtype))
            for ncattr in nc_fid.variables[key].ncattrs():
                print('\t\t%s:' % ncattr,\
                      repr(nc_fid.variables[key].getncattr(ncattr)))
        except KeyError:
            print("\t\tWARNING: %s does not contain variable attributes" % key)

    # NetCDF global attributes
    nc_attrs = nc_fid.ncattrs()
    if verb:
        print("NetCDF Global Attributes:")
        for nc_attr in nc_attrs:
            print('\t%s:' % nc_attr, repr(nc_fid.getncattr(nc_attr)))
    nc_dims = [dim for dim in nc_fid.dimensions]  # list of nc dimensions
    # Dimension shape information.
    if verb:
        print("NetCDF dimension information:")
        for dim in nc_dims:
            print("\tName:", dim)
            print("\t\tsize:", len(nc_fid.dimensions[dim]))
            print_ncattr(dim)
    # Variable information.
    nc_vars = [var for var in nc_fid.variables]  # list of nc variables
    if verb:
        print("NetCDF variable information:")
        for var in nc_vars:
            if var not in nc_dims:
                print('\tName:', var)
                print("\t\tdimensions:", nc_fid.variables[var].dimensions)
                print("\t\tsize:", nc_fid.variables[var].size)
                print_ncattr(var)
    return nc_attrs, nc_dims, nc_vars

#nc_fid_chaz_ny_nc = Dataset("../data/synthetic-storm-data/LongIsland_chaz_test.nc", 'r')
#nc_attrs, nc_dims, nc_vars = ncdump(nc_fid_chaz_ny_nc)

#obs = [["Obs_ibtracks_knaff15_Mumbai.nc", "Obs NC", "other_chaz", "k", 2016-1879+1]]
data = [["LongIsland_chaz_test.nc", "Obs NC", "other_chaz", "k", 2016-1879+1]]

data_path = os.path.join("../data", "synthetic-storm-data", data[0][0])
storms = load_chaz_storms(path = data_path,
                mask_distance = None,
                mask_coordinate = None,
                mask_category = 4,
                categorization = "NHC")

test_storm = storms[0]

fig = plt.figure()
fig.set_figwidth(fig.get_figwidth() * 2)

title_font = {'fontname':'Arial', 'size':'12', 'color':'black', 'weight':'normal',
          'verticalalignment':'bottom'} # Bottom vertical alignment for more space
axis_font = {'fontname':'Arial', 'size':'12'}

axes = fig.add_subplot(2, 2, 1)
axes.plot(test_storm.max_wind_speed, test_storm.max_wind_radius, 'ro', label="Knaff Max Wind Radius")
axes.set_xlabel('Max Wind Speed (knots)', **axis_font)
axes.set_ylabel('Max Wind Radius (nmi)', **axis_font)
axes.legend()

axes = fig.add_subplot(2, 2, 2)
axes.plot(units.convert(test_storm.max_wind_speed, 'knots', 'm/s'),
units.convert(test_storm.max_wind_radius, 'nmi', 'km'), 'ro-', label="Knaff MWR")
axes.set_xlabel('Max Wind Speed (m/s)', **axis_font)
axes.set_ylabel('Max Wind Radius (km)', **axis_font)
axes.legend()

axes = fig.add_subplot(2, 2, 3)
axes.plot(range(0, test_storm.max_wind_speed.shape[0]),
test_storm.max_wind_speed, 'bo-', label="Max Wind Speed")
axes.set_xlabel('Obs Count', **axis_font)
axes.set_ylabel('Max Wind Speed (knots)', **axis_font)
axes.legend()

axes = fig.add_subplot(2, 2, 4)
axes.plot(range(0, test_storm.max_wind_radius.shape[0]),
test_storm.max_wind_radius, 'ro-', label="Knaff MWR")
axes.set_xlabel('Obs Count', **axis_font)
axes.set_ylabel('Max Wind Radius (nmi)', **axis_font)
axes.legend()

plt.yticks(fontsize=10)
plt.xticks(fontsize=10)
plt.tight_layout()
plt.savefig('MaxWindRadiusCheck.pdf')


import random
class KnightsTour():

    def __init__(self, board_axis):
        if len(board_axis) != 2:
            raise Exception('Error Baord Axis incorrect', board_axis)
        self.board_axis = board_axis
        self.m = board_axis[0]
        self.n = board_axis[1]
        self.board = None
        self.moves = {}

    def build_board(self):

        if self.board is None:
            self.board_hash = {}
            self.available_moves = {}
            self.board = " "
            self.board += "_" * 5 * self.m
            self.board += "\n"
            for y in range(1, self.m + 1):
                self.board += "|____"
                for x in range(1, self.n):
                    if self.board_hash.get((x,y)):
                        self.board += "|_xx_"
                    else:
                        self.board += "|____"

                self.board += "|\n"
        return self.board_hash
    def print_board(self):
        print(self.board)
    def run(self):
        board = self.build_board()
        possible_moves = self.get_all_available_moves()
        first_move = random.sample(self.available_moves.keys(), 1)[0]
        self.find_tour(first_move, 1)
        import pdb; pdb.set_trace()

    def get_all_available_moves(self):
        number_of_moves = self.m * self.n
        for y in range(1, self.m + 1):
            for x in range(1, self.n + 1):
                coordinates = (x, y)
                self.board_hash[coordinates] = False
                self.available_moves[coordinates] = self.find_all_moves(x,y)

    def find_all_moves(self, x, y):
        ## gets all valid moves for coordinates
        moves = [
            (x + 1, y + 2),
            (x + 1, y - 2),
            (x - 1, y + 2),
            (x - 1, y - 2),

            (x + 2, y + 1),
            (x + 2, y - 1),
            (x - 2, y + 1),
            (x - 2, y - 1),
        ]

        # less than 'n' and 'm' because we want to make sure its less than
        # the edges
        return [m for m in moves if(
                            m[0] > 0
                            and m[0] <= self.m
                            and m[1] <= self.n
                            and m[1] > 0
                            and not self.moves.get(m))]

    def find_tour(self, coordinates, i):
        moves = self.available_moves.get(coordinates)

        if len(moves) > 0:
            self.moves[coordinates] = i
            self.board_hash[coordinates] = True
            found_move = moves[0]
            self.get_all_available_moves()
            self.find_tour(found_move, i + 1)
        else:
            self.fix_euler(i)
        return self.moves

    def fix_euler(self, i):
        new_moves = self.find_max_moves()
        coords = new_moves[0]
        new_move = random.sample(new_moves[1], 1)[0]
        index = self.moves[coords]
        temp_moves = self.moves.copy()
        self.moves = {}
        self.moves[new_move] = index
        all_moves = [mv for mv in temp_moves.keys()]
        for y in range(0, len(temp_moves.keys())):
            new_i = index - 1
            if new_i > 1:
                new_c = all_moves[new_i]
                self.moves[new_c] = new_i
            else:
                another_temp = temp_moves.copy()
                for k,v in temp_moves.items():
                    another_temp[k] += 1
                self.moves.update(another_temp)

    def find_max_moves(self):
        moves = [m for m in self.moves.keys()]
        temp_array = []
        for move in moves:
            available_moves = self.find_all_moves(move[0], move[1])
            if len(available_moves) > 0:
                temp_array.append([move, available_moves])

        return random.sample(temp_array, 1)[0]

k = KnightsTour((5,5)).run()