import sys
import os.path
import collections
import inspect
from pkgutil import iter_modules
import importlib.util as imputil
from glob import glob
from functools import partial
import itertools

from PyQt5 import QtGui, QtCore, QtWidgets

from . import state
APP = state.app
HW = state.hardware
CB = state.crossbar

from .Globals import styles, functions

ModDescriptor = collections.namedtuple('ModDescriptor', \
        ['module', 'name', 'display', 'toplevel', 'callback'])
"""
ModDescriptor describes everything the interface needs to create
programming panels. The fields of this named tuple are

* ``module``: actual module object
* ``name``: name of the module
* ``display``: display label for the tree
* ``toplevel``: toplevel widget for the toplevel tag; can be ``None``
* ``callback``: callback for data display; can be ``None``

Users typically don't need to make descriptors. They will be created
accordingly on program load.
"""

ModTag = collections.namedtuple('ModTag', ['tag', 'name', 'callback'])
"""
A module tag. Every module needs to provide a toplevel tag under a module-local
variable named ``tags``. For instance a module named `TestModule` may have
the following ``tags`` setup.

>>> tags = { 'top': ModTag('TM', 'TestModule', TestModule.showData) }

Additional subtags can be provided if functionality of the modules may be
split in multiple entities (see for example
`arc1pyqt.ProgPanels.MultiStateSeeker`). These can be added as ``subtags``
which is again a list of `ModTag`s. Of course ``subtags`` can be omitted
if no such functionality is wanted (probably for most cases).

>>> tags = {
>>>     'top': ModTag('TM', 'TestModule', None),
>>>     'subtags': [ ModTag('TSM', 'TestSubModule', TestModule.subModData) ]
>>> }

"""

def __registerTagsFromModule(module, kls=None):
    """
    Add all compatible tags for module in the global tag registry. Modules
    that actually want to display something in the GUI should also point the
    the toplevel widget that should be displayed through the ``kls`` variable.
    If ``kls`` is None then ``module`` is a hidden module (read-outs and
    pulses are such).
    """

    # top tag
    top = module.tags['top']
    if top.callback is not None:
        display = "%s*" % top.name
    else:
        display = top.name

    descriptor = ModDescriptor(module, top.name, display, kls, top.callback)
    APP.modules[top.tag] = descriptor

    if 'subtags' not in module.tags.keys():
        return

    for tag in module.tags['subtags']:
        if tag.callback is not None:
            display = "%s*" % tag.name
        else:
            display = tag.name
        descriptor = ModDescriptor(module, tag.name, display, None,\
                tag.callback)
        APP.modules[tag.tag] = descriptor

def discoverModules(paths, namespace="", force_register=False):
    """
    Discover modules under ``paths`` and load them into the global
    module list if possible. Argument ``namespace`` provides the
    module (either real or not) that these modules will be loaded
    under. If empty they are added to the toplevel module (not
    recommended).

    Compatible modules should

    * Have a set of tags associated with them under the ``tags``
      variable (see `arc1pyqt.modutils.ModTag`).
    * Have a main class with the same name as the name of the
      file (sans the .py extension, obviously). For instance
      TestModule.py should contain a TestModule class as a
      top level entry point.
    * Said class should inherit from `arc1pyqt.modutils.BaseProgPanel`.
    """

    for p in paths:
        for (finder, name, ispkg) in iter_modules([p]):
            loader = finder.find_module(name)
            try:
                if namespace != "":
                    mod_full = '%s.%s' % (namespace, name)
                else:
                    mod_full = name

                spec = imputil.spec_from_file_location(mod_full, loader.path)
                mod = imputil.module_from_spec(spec)
                spec.loader.exec_module(mod)

                if ispkg and force_register:
                    if mod_full not in sys.modules.keys():
                        sys.modules[mod_full] = mod

                # now check containing classes
                for _, kls in inspect.getmembers(mod, inspect.isclass):

                    # entry point should be a class named after the file
                    if kls.__name__ != name:
                        continue

                    # and subclass ``BaseProgPanel``
                    if not issubclass(kls, BaseProgPanel):
                        continue

                    # if we made it here, add the module to the modlist
                    # if it has the correct tag
                    if hasattr(mod, 'tags'):
                        __registerTagsFromModule(mod, kls)

                # if this is a package; descend into it to discover further modules
                if ispkg:
                    if namespace != "":
                        discoverModules([os.path.join(p, name)],
                            '%s.%s' % (namespace, name))
                    else:
                        discoverModules([os.path.join(p, name)], namespace)

            except Exception as exc:
                print("Exception encountered while importing module", exc)
                continue

def compile_ui(src, dst, force=False, from_imports=True, rc_suffix='_rc'):
    """
    Compile a Qt Designer UI file (`src`) into a loadable python file (`dst`).
    This function will only generate the file if `src` is newer than `dst`
    unless `force` is set to `True`. This is a convenient wrapper around
    ``PyQt5.uic`` and can be quite convenient when an external module uses
    a UI file. For instance the following snippet will ensure that UI file
    `sample.ui` always generates `sample.py` before it is imported into
    the main module.

    >>> from os.path import join, dirname
    >>> from arc1pyqt.modutils import compile_ui
    >>> # get directory of present module
    >>> THIS_DIR = dirname(__file__)
    >>> # and compile it
    >>> compile_ui(join(THIS_DIR, 'sample.ui'), join(THIS_DIR, 'sample.py'))
    >>> # module sample now exists
    >>> from .sample import *

    There are two additional options that mirror ``PyQt5.uic`` behaviour.
    The first is `from_imports` which is used to generate imports relative
    to the package that hosts the UI file. By default this is true as most
    resource will probably be package-local. The second is `rc_suffix` which
    will be appended to any resource modules used by the (see also
    ``compile_rc``) UI file. By default this again mirrors ``PyQt5.uic``
    behaviour and it is `_rc`, but can be override if so required.
    """

    import PyQt5.uic as uic

    if os.path.exists(dst) and not force:
        src_mtime = os.path.getmtime(src)
        dst_mtime = os.path.getmtime(dst)

        if dst_mtime >= src_mtime:
            return

    with open(dst, 'w', encoding='utf-8') as out:
        uic.compileUi(src, out, execute=False, indent=4,
            from_imports=from_imports, resource_suffix=rc_suffix)

def compile_rc(files, dst, force=False):
    """
    Compile a list of Qt Designer resource files (qrc) `files` to a python
    module (`dst`). Similar to ``compile_ui`` this is a convenient wrapper
    around ``PyQt5.pyrcc_main``. Target will not be overwritten if it's
    newer than all of the input qrc files unless `force` is set to True.
    Input argument (`files`) can either be a string (for a single qrc file)
    or a list of strings (for many qrc files). Note that if you are using
    qrc file with Qt Designer target module MUST end in `_rc` unless a
    different suffix has been specified when invoking ``compile_ui``.

    So if a UI file (`sample.ui`) is to be compiled into a python module
    (`sample.py`) and makes use of a resources files (`sample.rc`) the
    following snippet will ensure that these are in sync when the module
    is loaded (using the default options).

    >>> from os.path import join, dirname
    >>> from arc1pyqt.modutils import compile_ui, compile_rc
    >>> # get directory of present module
    >>> THIS_DIR = dirname(__file__)
    >>> # and compile it
    >>> compile_ui(join(THIS_DIR, 'sample.ui'), join(THIS_DIR, 'sample.py'))
    >>> compile_rc(join(THIS_DIR, 'sample.qrc'), join(THIS_DIR, 'sample_rc.py'))
    >>> # module sample now exists and its resources are loaded properly
    >>> from .sample import *
    """

    from collections.abc import Iterable
    import PyQt5.pyrcc_main as rcc

    # ensure `files` is a list
    if not isinstance(files, Iterable) or isinstance(files, str):
        files = [files]

    overwrite = False

    if os.path.exists(dst) and not force:
        dst_mtime = os.path.getmtime(dst)
        for f in files:
            src_mtime = os.path.getmtime(f)
            # check if rc file is newer than destination
            if src_mtime > dst_mtime:
                # if yes, destination must be recreated
                overwrite = True
                break
    else:
        overwrite = True

    if overwrite:
        rcc.processResourceFile(files, dst, False)

def makeDeviceList(isRange):
    """
    Generate a list of crosspoints to apply a function. Setting ``isRange`` to
    ``True`` will generate a device list containing only the devices selected
    in the main crossbar panel; ``False`` will return all the devices in the
    crossbar.
    """

    if isRange == False:
        (minW, maxW) = (1, HW.conf.words)
        (minB, maxB) = (1, HW.conf.bits)
    else:
        (minW, maxW) = CB.limits['words']
        (minB, maxB) = CB.limits['bits']

    all_devices = itertools.product(range(minW, maxW+1), range(minB, maxB+1))

    # Find how many SA devices are contained in the range
    if not CB.checkSA:
        return list(all_devices)
    else:
        return [cell for cell in all_devices if cell in CB.customArray]

class BaseProgPanel(QtWidgets.QWidget):

    def __init__(self, title, description="", short=False):
        super().__init__()
        self.title = title
        self.description = description
        self.short = short
        self.propertyWidgets = {}
        self.thread = None
        self._deferredUpdates = {}

    def execute(self, wrapper, entrypoint=None, deferredUpdate=False):
        """
        This function schedules a wrapper for execution taking care of the
        standard signals. The wrapped action (`wrapper`) will be passed
        along a thread which will call the `entrypoint` function of
        `wrapper`. If `entrypoint` is None the default `wrapper.run`
        entrypoint will be used. Argument `deferredUpdate` prevents the history
        tree from updating until the thread operation has finished. This can
        be useful in situations where multiple different devices are used or
        when a module uses many individual operations that would otherwise
        trigger a tree update (for instance hundreds of reads/pulses over
        ten different devices).
        """
        if (HW.ArC is None) or (self.thread is not None):
            return

        if entrypoint is None:
            entrypoint = wrapper.run

        self.threadWrapper = wrapper
        self.thread = QtCore.QThread()

        # When deferring tree updates store current point in history for the
        # whole crossbar. Once the operation is finished the history tree will
        # then be populated starting from this point in history
        if deferredUpdate:
            for (r, row) in enumerate(CB.history):
                for (c, col) in enumerate(row):
                    self._deferredUpdates['%d%d' % (r, c)] = (r, c, len(col))

        self.threadWrapper.moveToThread(self.thread)
        self.thread.started.connect(entrypoint)
        self.threadWrapper.finished.connect(self.thread.quit)
        #self.threadWrapper.sendData.connect(functions.updateHistory)
        #self.threadWrapper.highlight.connect(functions.cbAntenna.cast)
        #self.threadWrapper.displayData.connect(functions.displayUpdate.cast)
        if not deferredUpdate:
            self.threadWrapper.updateTree.connect(\
                functions.historyTreeAntenna.updateTree.emit)
        self.threadWrapper.disableInterface.connect(functions.interfaceAntenna.cast)
        self.thread.finished.connect(partial(self._onThreadFinished, deferredUpdate))
        self.thread.start()

    def _onThreadFinished(self, deferredUpdate=False):
        """ Clean up running threads and wake up the interface """
        if self.thread is None:
            return

        functions.interfaceAntenna.wakeUp()
        self.thread.wait()
        self.threadWrapper.deleteLater()
        self.threadWrapper = None
        self.thread = None

        # If updates were deferred do them now in batch
        if deferredUpdate:
            for (_, (w, b, idx)) in self._deferredUpdates.items():
                functions.historyTreeAntenna.updateTree_batch.emit(w, b, idx)
            self._deferredUpdates.clear()

    def registerPropertyWidget(self, wdg, name):
        """
        Register a widget owned by this panel as a save-able property. If `wdg`
        is one of ``QtWidgets.QLineEdit``, ``QtWidgets.QComboBox``,
        ``QtWidgets.QCheckBox``, ``QtWidgets.QSpinBox`` and
        ``QtWidgets.QDoubleSpinBox`` their values will reinstated when
        `BaseProgPanel.setPanelData` is called. Also see
        `BaseProgPanel.extractPanelData`.
        """
        self.propertyWidgets[name] = wdg
        wdg.setProperty("key", name)

    def extractPanelData(self):
        """
        Iterate through all widgets registered as save-able with
        `BaseProgPanel.registerPropertyWidget` and collect their current
        values. This function will automatically process common widgets:
        ``QtWidgets.QLineEdit``, ``QtWidgets.QComboBox``,
        ``QtWidgets.QCheckBox``, ``QtWidgets.QSpinBox`` and
        ``QtWidgets.QDoubleSpinBox``.  If further data needs to be saved child
        widgets must override this functions

        >>> def extractPanelData(self):
        >>>     # call superclass function
        >>>     data = super().extractPanelData()
        >>>     data["foo"] = "bar"
        >>>     # more data collection
        >>>     return data
        """

        items = {}

        for (_, item) in self.propertyWidgets.items():
            key = item.property("key")
            wdg = item
            if key is None:
                continue

            if isinstance(item, QtWidgets.QLineEdit):
                data = wdg.text()
            elif isinstance(item, QtWidgets.QComboBox):
                data = wdg.currentIndex()
            elif isinstance(item, QtWidgets.QCheckBox):
                data = wdg.checkState()
            elif isinstance(item, QtWidgets.QSpinBox) or \
                isinstance(item, QtWidgets.QDoubleSpinBox):
                data = wdg.value()
            else:
                data = None

            if data is not None:
                items[key] = data

        return items

    def setPanelData(self, data):
        """
        Restore widget values from the dict `data`. Field keys are the same as
        defined with `BaseProgPanel.registerPropertyWidget`.  This function
        will only process common widgets: ``QtWidgets.QLineEdit``,
        ``QtWidgets.QComboBox``, ``QtWidgets.QCheckBox``,
        ``QtWidgets.QSpinBox`` and ``QtWidgets.QDoubleSpinBox``. If further
        state is required to be recovered from `data` child panels should
        override this function

        >>> def setPanelData(self, data):
        >>>     super().setPanelData(data)
        >>>     do_smth_with(data["key"])
        """

        for (k, value) in data.items():
            wdg = self.propertyWidgets.get(k, None)
            if wdg is None:
                continue

            if isinstance(wdg, QtWidgets.QLineEdit):
                wdg.setText(value)
            elif isinstance(wdg, QtWidgets.QComboBox):
                wdg.setCurrentIndex(value)
            elif isinstance(wdg, QtWidgets.QCheckBox):
                wdg.setChecked(value)
            elif isinstance(wdg, QtWidgets.QSpinBox) or \
                isinstance(wdg, QtWidgets.QDoubleSpinBox):
                wdg.setValue(value)

    def makeControlButton(self, text, slot=None):
        btn = QtWidgets.QPushButton(text)
        btn.setStyleSheet(styles.btnStyle)
        if slot is not None:
            btn.clicked.connect(slot)
        return btn

class BaseThreadWrapper(QtCore.QObject):
    """
    Base class for runnable objects that takes care of allocating the
    signals and enabling/disabling the interface as needed. Subclasses
    should still emit the ``displayData`` or ``updateTree`` signals to
    force interface update. This is intentional because the way data are
    communicated to the interface is application dependent.

    Subclasses only need to decorate the target runner function with the
    ``BaseThreadWrapper.runner`` decorator. For instance

    >>> class ModuleThreadWrapper(BaseThreadWrapper):
    >>>     def __init__(self):
    >>>         super().__init__()
    >>>
    >>>     @BaseThreadWrapper.runner
    >>>     def do_stuff(self):
    >>>         do_some_number_crunching()

    The run function can then be connected to the ``start`` signal of a
    QThread as usual.
    """

    finished = QtCore.pyqtSignal()
    """ Process has finished """
    sendData = QtCore.pyqtSignal(int, int, float, float, float, str)
    """ Transfer data to be written to the session log """
    highlight = QtCore.pyqtSignal(int, int)
    """ Highlight a device in the crossbar view """
    displayData = QtCore.pyqtSignal()
    """ Force data display update """
    updateTree = QtCore.pyqtSignal(int, int)
    """ Force device tree update """
    disableInterface = QtCore.pyqtSignal(bool)
    """ Toggle interface interaction """

    def runner(func):
        """
        Decorator used to signify a runnable function within a custom measuring
        thread as described in `BaseThreadWrapper`.
        """
        def inner(self):
            self.disableInterface.emit(True)
            func(self)
            self.disableInterface.emit(False)
            self.finished.emit()
            self.displayData.emit()
        return inner

    def runner_no_display(func):
        def inner(self):
            self.disableInterface.emit(True)
            func(self)
            self.disableInterface.emit(False)
            self.finished.emit()
        return inner


import os
import pycurl
import urllib
import urlparse
from StringIO import StringIO

def _set_x509_creds(handle):
    """
    Set the certificate if available
    """
    if 'X509_USER_CERT' in os.environ:
        handle.setopt(pycurl.SSLCERT, os.environ['X509_USER_CERT'])
    if 'X509_USER_KEY' in os.environ:
        handle.setopt(pycurl.SSLKEY, os.environ['X509_USER_KEY'])

def _build_full_url(url, args):
    """
    Update url with the given query parameters that are passed by args
    """
    parsed = urlparse.urlparse(url)
    query = urlparse.parse_qs(parsed.query)
    for a in args:
        if args[a]:
            query[a] = args[a]
    query_str = urllib.urlencode(query)
    new_url = (parsed.scheme, parsed.netloc, parsed.path, parsed.params, query_str, parsed.fragment)
    return urlparse.urlunparse(new_url)

def get_url(url, **kwargs):
    """
    Get the content from an URL, and expand the extra arguments as query parameters
    """
    handle = pycurl.Curl()

    handle.setopt(pycurl.SSL_VERIFYPEER, False)
    _set_x509_creds(handle)

    buffer = StringIO()
    handle.setopt(pycurl.WRITEFUNCTION, buffer.write)

    handle.setopt(pycurl.FOLLOWLOCATION, True)
    handle.setopt(pycurl.URL, _build_full_url(url, kwargs))
    handle.perform()

    return buffer.getvalue()


from invargen.data.expression import *

MAX_EXPR_LENGTH = 20
MAX_EPISODE_LENGTH = 256

OPERATORS = [
    # Unary
    Abs, Sign, Log,
    # Binary
    Add, Sub, Mul, Div, Pow, Greater, Less
]

CONSTANTS = [int(i) for i in range(1, 1000)]

REWARD_PER_STEP = 0.


print('Digite os valores de a, b e c, para calcular as raízes da equação de 2° grau')
import math
a = float(input('a: '))
b = float(input('b: '))
c = float(input('c: '))
delta = b ** 2 - 4 * a * c
if a != 0:
    if delta < 0:
        print('Não existe raiz')
    elif delta == 0:
        raizdelta = -1 * b + (math.sqrt(delta)) / (2 * a)
        print(f'A raiz é: {raizdelta:.2f} e é Raiz única')
    elif delta >= 0:
        raizdelta1 = -1 * b + (math.sqrt(delta)) / (2 * a)
        raizdelta2 = -1 * b - (math.sqrt(delta)) / (2 * a)
        print(f'As raizes são: {raizdelta1:.2f} e {raizdelta2:.2f}')
else:
    print('Não é uma equação de segundo grau')


# In Class Lab 6
# Surface Brightness Profiles

# Load Modules
import numpy as np
import astropy.units as u

# import plotting modules
import matplotlib.pyplot as plt
import matplotlib

# my modules
from ReadFile import Read
from CenterOfMass import CenterOfMass
from MassProfile import MassProfile
from GalaxyMass import ComponentMass

# # Lab 6: Sersic Profiles
#
# In this lab we will use Homework 5 solutions to compute the mass profile of the Milky Way's bulge.
# We will turn the mass profile into a density profile and see if we can fit it reasonably well
# with a sersic profile.

# # Part A :
#
# Create a function called `sersicE` that returns the Sersic Profile in terms of the effective radius $R_e$ (i.e. the half light radius).
#
# $I(r) = I_e exp^{-7.67 ( (r/R_e)^{1/n} - 1)}$
#
# Where
#
# $ L = 7.2 I_e \pi R_e^2$
#
# and  $R_e$ is the half light radius.  We will assume a mass to light ratio for the stellar bulge of 1, so this is also the half mass radius.
#
# The function should take as input the radius, $R_e$, $n$ and the total stellar mass of the system.
#

def sersicE(r, re, n, mtot):
    """ Function that computes the Sersic Profile for an Elliptical System, assuming M/L ~ 1
    PARMETERS
    ---------
        r: `float`
            Distance from the center of the galaxy (kpc)
        re: `float`
            The Effective radius (2D radius that contains half the light) (kpc)
        n:  `float`
            sersic index
        mtot: `float`
            the total stellar mass (Msun)

    RETURNS
    -------
        I: `array of floats`
            the surface brightness profile of the elliptical in Lsun/kpc^2

    """

    # We are assuming M/L = 1, so the total luminosity is:
    lum = mtot

    # the effective surface brightness is
    Ie = lum/7.2/np.pi/re**2

    # The surface brightness
    I = Ie*np.exp(-7.67*((r/re)**(1.0/n)-1.0))

    return I

# # Part B
#
# a) Create an instance of the MassProfile Class for the M31. Store it as a variable `M31`.
#

# a)  Create a mass profile object for the Milky Way using solution to Homework 5
M31 = MassProfile("M31",0)

# b) Create an array of radii from 0.1 kpc to 30 kpc in increments of 0.1
#

# b) Array of radii
r = np.arange(0.1,30,0.1)

# c) Define a new array called `bulge_mass`, that uses the function `MassEnclosed` within MassProfile to compute the mass profile of the bulge.  Get rid of astropy units in `bulge_mass` by adding `.value`

# c) Use Mass Profile to return the Bulge Mass Profile at each value of R
# Recall that particle type 3 = bulge
bulge_mass = M31.massEnclosed(3,r).value  # Get rid of astropy units

# d) Compute the surface mass density profile for the simulated bulge and store it as an array called `bulge_I`. Assuming M/L ~ 1 this is also the surface brightness profile in Lsun/kpc^2

# d) Surface brightness profile
# Density of Light  Assume M ~ L
# I = L/4/pi/D^2
bulge_I = bulge_mass/4/np.pi/r**2

# # Part C
#
# Compute $R_e$, the half mass radius, for the bulge

# Determine the total mass of the bulge
bulge_total = ComponentMass("M31_000.txt",3)*1e12
print(f"{bulge_total:.2e}")

# Find the effective radius of the bulge, which encloses half of the mass
b_half = bulge_total/2.0
index = np.where((bulge_mass > b_half)) #  & (BulgeMass < Bhigh))

# Effective radius of the bulge
re_bulge = r[index][0]  # take first index where Bulge Mass > Blow
print(re_bulge)

# # Part D
#
# a) Plot the surface density profile of the simulated bulge
#
# b) Plot the Sersic profile, assuming a de Vaucouleurs Profile.
#
# c) If the profiles don't match, try changing either $R_e$ or $n$

# Plot the Bulge density profile vs
# the Sersic profile
####################################

fig = plt.figure(figsize=(8,8))
ax = plt.subplot(111)

#adjust tick label font size
label_size = 15
matplotlib.rcParams['xtick.labelsize'] = label_size
matplotlib.rcParams['ytick.labelsize'] = label_size

# plot the bulge mass density as a proxy for surface brighntess
plt.semilogy(r,bulge_I, color='black',linewidth=3, label='Simulated Bulge')

# YOU ADD HERE: Sersic fit to the surface brightness Sersic fit
# Sersic n = 4 - de Vaucouleurs
plt.semilogy(r,sersicE(r,re_bulge,4,bulge_total), color='red', linestyle="-.",linewidth=3, label='Sersic n=4')

# Try other profiles
# n=4 but increasing the effective radius
plt.semilogy(r,sersicE(r,20,4,bulge_total), color='orange', linestyle=":",linewidth=3, label='Sersic n=4, Re=20')

# keeping effective radius fixed, but changing n
plt.semilogy(r,sersicE(r,re_bulge,5.3,bulge_total), color='purple', linestyle="--",linewidth=3, label='Sersic n=5.3')

# Add axis labels
plt.xlabel('Radius (kpc)', fontsize=22)
plt.ylabel('Log(I)  $L_\odot/kpc^2$', fontsize=22)

# add a legend with some customizations.
legend = ax.legend(loc='upper right',fontsize='x-large')

plt.savefig('Lab6.png')


# coding=utf-8
import joblib
databasePath = 'snpdatabse.vcf'

f = open(databasePath)
lines = f.readlines()
# print('chr1\t' in lines[40])
print(len(lines))
chromosomes = [str(i) for i in list(range(1, 19))] + ['X', 'Y']
total = 0
for chr_ in chromosomes:
    chr_lines = [line for line in lines if len(line) > 0 and line[0] != '#' and ('chr'+chr_+'\t') in line]
    print(len(chr_lines))
    total += len(chr_lines)
    # 写入 .pkl 文件
    joblib.dump(chr_lines, 'database/Chr' + chr_ + '.pkl')

print(total)
f.close()


from setuptools import setup, find_packages

LONG_DESC = open("README.rst").read()

setup(
    name="calltest",
    use_scm_version={"version_scheme": "guess-next-dev", "local_scheme": "dirty-tag"},
    description="A distributed phone call test program",
    url="https://github.com/smurfix/calltest",
    long_description=LONG_DESC,
    author="Matthias Urlichs",
    author_email="matthias@urlichs.de",
    license="proprietary",
    packages=find_packages(),
    setup_requires=["setuptools_scm", "pytest_runner"],
    install_requires=[
        "asyncclick",
        "asyncari >= 0.8.3",
        "trio >= 0.11",
        "range_set >= 0.2",
        "attrs >= 18.2",
        "jsonschema >= 2.5",
        "pyyaml >= 3",
    ],
    tests_require=[
        "pytest",
        "pytest-trio",
        "flake8 >= 3.7"
    ],
    keywords=["async", "asterisk"],
    python_requires=">=3.7",
    classifiers=[
        "Development Status :: 4 - Beta",
        "Intended Audience :: Information Technology",
        "Framework :: AsyncIO",
        "Framework :: Trio",
        "Operating System :: POSIX :: Linux",
        "Operating System :: MacOS :: MacOS X",
        "Operating System :: Microsoft :: Windows",
        "Programming Language :: Python :: 3.7",
        "Programming Language :: Python :: 3.8",
        "Programming Language :: Python :: Implementation :: CPython",
        "Topic :: Communications :: Telephony",
        "Topic :: Software Development :: Testing",
        "License :: OSI Approved :: GNU General Public License v3 (GPLv3)",
    ],
    entry_points="""
    [console_scripts]
    calltest = calltest.command:cmd
    """,
    zip_safe=True,
)


from manager_rest.celery_client import celery_client as client

class WorkflowClient(object):

    @staticmethod
    def execute_workflow(name,
                         workflow,
                         blueprint_id,
                         deployment_id,
                         execution_id,
                         execution_parameters=None):
        task_name = workflow['operation']
        task_queue = '{}_workflows'.format(deployment_id)

        execution_parameters['__cloudify_context'] = {
            'workflow_id': name,
            'blueprint_id': blueprint_id,
            'deployment_id': deployment_id,
            'execution_id': execution_id
        }

        client().execute_task(task_name=task_name,
                              task_queue=task_queue,
                              task_id=execution_id,
                              kwargs=execution_parameters)

    @staticmethod
    def execute_system_workflow(deployment, wf_id, task_id, task_mapping,
                                execution_parameters=None):
        # task_id is not generated here since for system workflows,
        # the task id is equivalent to the execution id

        task_queue = 'cloudify.management'

        context = {
            'task_id': task_id,
            'task_name': task_mapping,
            'task_target': task_queue,
            'blueprint_id': deployment.blueprint_id,
            'deployment_id': deployment.id,
            'execution_id': task_id,
            'workflow_id': wf_id,
        }
        execution_parameters = execution_parameters or {}
        execution_parameters['__cloudify_context'] = context

        return client().execute_task(
            task_mapping,
            task_queue,
            task_id,
            kwargs=execution_parameters)

def workflow_client():
    return WorkflowClient()


# "C:\py venv\kivy_venv\scripts\activate"
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.core.window import Window
from kivy.lang import Builder
from kivy.animation import Animation

Builder.load_file('kv_design.kv')

class MyLayout(Widget):
    def ripple_effect(self, widget): # widget adalah self dari widget pemanggil
        # reset size
        widget.size_effect = [0,0]

        my_width = int(widget.width)
        my_height = int(widget.height)

        # fit the size
        if my_width > my_height:
            my_width = my_height
        else:
            my_height = my_width

        anim = Animation(
            opacity_effect = 1,
            duration = 0.01
        )
        anim &= Animation(
            opacity_effect = 1,
            size_effect = [my_width/1.5, my_height/1.5],
            duration = 0.2
        )
        anim += Animation(
            size_effect = [my_width*(1.2), my_height*(1.2)],
            opacity_effect = 0,
            duration = 0.2
        )
        anim.start(widget)

class MyApp(App):
    def build(self):
        return MyLayout()

if __name__ == '__main__':
    MyApp().run()

Playground/custom ripple button effect/main.py


import tensorflow as tf

if __name__ == '__main__':
    fashion_mnist = tf.keras.datasets.fashion_mnist.load_data()
    (x_train_full, y_train_full), (x_test, y_test) = fashion_mnist
    x_train, y_train = x_train_full[:-5000], y_train_full[:-5000]
    x_valid, y_valid = x_train_full[-5000:], y_train_full[-5000:]

    # now we scale the data to work better in neurons
    x_train, x_valid = x_train / 255, x_valid / 255

    class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'shirt', 'Sneaker', 'Bag',
                   'Ankle boot']

    tf.random.set_seed(42)
    model = tf.keras.Sequential()
    model.add(tf.keras.layers.InputLayer(input_shape=x_train.shape[1:]))
    # we have to flat the data, because the input neuron of the network cant be a matrix
    model.add(tf.keras.layers.Flatten())
    model.add(tf.keras.layers.Dense(300, activation='relu'))  # hidden layer
    model.add(tf.keras.layers.Dense(100, activation='relu'))  # hidden layer
    model.add(tf.keras.layers.Dense(10, activation='softmax'))  # output layer

    model.summary()

    model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

    history = model.fit(x_train, y_train, epochs=30, validation_data=(x_valid, y_valid))

    print('model evaluation: ', model.evaluate(x_test, y_test))


from codepack.interfaces.interface import Interface
import boto3
from botocore.config import Config
from botocore.client import BaseClient
from botocore.response import StreamingBody
from typing import Any, Union, Optional

class S3(Interface):
    def __init__(self, config: dict, *args: Any, **kwargs: Any) -> None:
        super().__init__(config)
        self.connect(*args, **kwargs)

    def connect(self, *args: Any, **kwargs: Any) -> BaseClient:
        _config = {k: v for k, v in self.config.items()}
        for k, v in kwargs.items():
            _config[k] = v
        if 'config' not in _config:
            tmp = Config(retries=dict(max_attempts=3))
            _config['config'] = tmp
            self.config['config'] = tmp
        self.session = boto3.client(*args, **_config)
        self._closed = False
        return self.session

    def list_objects(self, bucket: str, prefix: str = '', *args: Any, **kwargs: Any) -> list:
        ret = list()
        if self.session is None:
            return ret
        for page in self.session.get_paginator('list_objects').paginate(Bucket=bucket, Prefix=prefix, *args, **kwargs):
            ret += page.get('Contents', list())
        return ret

    def download(self, bucket: str, key: str,
                 streaming: bool = False, *args: Any, **kwargs: Any) -> Optional[Union[bytes, StreamingBody]]:
        try:
            obj = self.session.get_object(Bucket=bucket, Key=key, *args, **kwargs)
        except self.session.exceptions.NoSuchKey:
            return None
        body = obj.get('Body', None)
        if streaming:
            return body
        elif body:
            return body.read()
        else:
            return None

    def exist(self, bucket: str, key: str, *args: Any, **kwargs: Any) -> bool:
        try:
            obj = self.session.head_object(Bucket=bucket, Key=key, *args, **kwargs)
            return obj is not None
        except self.session.exceptions.ClientError:
            return False

    def delete(self, bucket: str, key: str, *args: Any, **kwargs: Any) -> dict:
        return self.session.delete_object(Bucket=bucket, Key=key, *args, **kwargs)

    def upload(self, bucket: str, key: str, data: Union[str, bytes], *args: Any, **kwargs: Any) -> dict:
        return self.session.put_object(Bucket=bucket, Key=key, Body=data, *args, **kwargs)

    def download_file(self, bucket: str, key: str, path: str, *args: Any, **kwargs: Any) -> None:
        self.session.download_file(Bucket=bucket, Key=key, Filename=path, *args, **kwargs)

    def upload_file(self, path: str, bucket: str, key: str, *args: Any, **kwargs: Any) -> None:
        self.session.upload_file(Filename=path, Bucket=bucket, Key=key, *args, **kwargs)

    def close(self) -> None:
        self._closed = True


from django.core.validators import RegexValidator
from rest_framework import serializers
from .models import *
from User.models import (
    User,
    Profile,
)

class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = (
            'first_name', 'last_name', 'email', 'phone_number'
        )

class AddressSerializer(serializers.ModelSerializer):
    class Meta:
        model = Address
        fields = (
            'province', 'city', 'address_detail', 'postal_code', 'receiver_name', 'customer', 'receiver_phone_number')

class CustomerSerializer(serializers.ModelSerializer):
    class Meta:
        model = Customer
        fields = '__all__'

class ProfileSerializer(serializers.HyperlinkedModelSerializer):
    # customer = CustomerSerializer(read_only=True, many=True)
    address = serializers.HyperlinkedRelatedField(view_name='user_addresses', many=True, read_only=True)

    class Meta:
        model = Profile
        fields = '__all__'
        extra_fields = ('address', 'information', 'wish_list', 'order_history')


import argparse
from sys import exit

from profanityfilter import ProfanityFilter

pf = ProfanityFilter()

def main():
    parser = argparse.ArgumentParser(description='Profanity filter console utility')
    group = parser.add_mutually_exclusive_group()
    group.add_argument('-t', '--text', dest='text', help='Test the given text for profanity')
    group.add_argument('-f', '--file', dest='path', help='Test the given file for profanity')
    parser.add_argument('-o', '--output', dest='output_file', help='Write the censored output to a file')
    parser.add_argument('--show', action='store_true', help='Print the censored text')

    args = parser.parse_args()

    if args.text and args.path:
        parser.print_help()
        exit()

    if args.text:
        text = args.text
    elif args.path:
        with open(args.path, 'r') as f:
            text = "".join(f.readlines())

    censored_text = pf.censor(text)

    if args.output_file:
        with open(args.output_file, 'w') as f:
            f.write(censored_text)
        print("Censored text written to output file at: " + args.output_file)

    if args.show:
        print("Censored text:\n")
        print(censored_text)

    if args.show or args.output_file:
        return

    if pf.is_clean(text):
        print("This text is clean.")
    else:
        print("This text is not clean!")

if __name__ == "__main__":
    main()


import unittest
from time import sleep
from PIL.Image import Image
from multisensor_pipeline import GraphPipeline
from multisensor_pipeline.dataframe import Topic
from multisensor_pipeline.modules import ListSink
from multisensor_pipeline.modules.video import WebcamSource

class WebcamTests(unittest.TestCase):
    def test_simple_webcam(self):
        self.simple_webcam()

    def test_simple_webcam_filter(self):
        topic = Topic(name="frame", dtype=Image)
        self.simple_webcam(topic=topic)

    def simple_webcam(self, topic=None):
        # (1) define the modules
        framerate = 15
        webcam_id = WebcamSource.available_webcams()[0]
        source = WebcamSource(webcam_id=webcam_id, framerate=framerate)
        sink = ListSink()

        # (2) add module to a pipeline...
        pipeline = GraphPipeline()
        pipeline.add_source(source)
        pipeline.add_sink(sink)

        # (3) ...and connect the modules
        pipeline.connect(source, sink, topics=topic)

        # Test
        pipeline.start()
        sleep(2)
        pipeline.stop()
        pipeline.join()

        # for i, img_frame in enumerate(sink.list):
        #     img_frame.data.save(f"test{i}.jpg")

        self.assertGreaterEqual(len(sink), 2*framerate - 1)
        self.assertLessEqual(len(sink), 2*framerate + 3)


import os, glob
import numpy, pandas

from scitbx.array_family import flex

from giant.structure.formatting import Labeller, ShortLabeller

from bamboo.html import png2base64src_maybe
from pandemic.html import PANDEMIC_HTML_ENV

# debugging flags
DEBUG = False

def format_summary(text, width=12, cls_tuple=['alert','info']):
    paragraphs = text.split('\n\n')
    output = []
    for p in paragraphs:
        lines = p.strip('>\n ').split('\n')
        if len(lines) > 1:
            title = lines.pop(0).strip('>\n ')
        else:
            title = None
        p_dict = {'width' : width,
                  'text'  : '
'.join(lines).replace('\t',' '),
                  'class' : cls_tuple}
        if title is not None:
            p_dict['title'] = title
        output.append(p_dict)
    return output

def create_overview_tab(parameterisation):
    p = parameterisation
    f = parameterisation.fitter
    fm = p.file_manager

    chain_ids = [c.id for c in p.blank_master_hierarchy().select(flex.bool(p.atom_mask.tolist()),copy_atoms=True).chains()]

    part_f = fm.get_file('level-partitions-by-chain-template')
    prof_f = fm.get_file('png-combined-profile-template')
    resd_f = fm.get_file('png-residual-profile-template')

    tab = {'id'             : 'overview',
           'short_name'     : 'Overview',
           'long_name'      : 'Hierarchical ADP Parameterisation Overview',
           'description'    : '',
           'contents'       : [{'width':8, 'text': '', 'image':png2base64src_maybe(fm.get_file('tracking_png'), print_on_missing=DEBUG)}] + \
                              [{'width':12, 'text': ' - '.join(['']*25), 'class':['text-center']}] + \
                              format_summary(f.summary(), width=6) + \
                              [{'width':12, 'text': ' - '.join(['']*25), 'class':['text-center']}] + \
                              numpy.concatenate(zip(*[[{'width':4, 'text': 'Partition Schematic',  'image':png2base64src_maybe(part_f.format(c), print_on_missing=DEBUG)} for c in chain_ids],
                                                      [{'width':4, 'text': 'TLS-level components', 'image':png2base64src_maybe(prof_f.format(c), print_on_missing=DEBUG)} for c in chain_ids],
                                                      [{'width':4, 'text': 'Residual component',   'image':png2base64src_maybe(resd_f.format(c), print_on_missing=DEBUG)} for c in chain_ids]
                                                     ])).tolist() + \
                              [{'width':12, 'text': ' - '.join(['']*25), 'class':['text-center']}] + \
                              numpy.concatenate([format_summary(l.summary(show=False), width=6) for l in f.levels+[f.residual]]).tolist()
          }

    return tab

def create_analysis_tab(parameterisation):
    p = parameterisation
    f = parameterisation.fitter
    fm = p.file_manager

    rall_f = fm.get_file('all_rvalues_v_resolution')
    rgap_f = fm.get_file('rgap_v_resolution')
    rfre_f = fm.get_file('rfree_v_resolution')
    rwor_f = fm.get_file('rwork_v_resolution')

    tab = {'id'             : 'statistics',
           'short_name'     : 'Results/Analysis',
           'long_name'      : 'Model Improvement Statistics from hierarchical TLS parameterisation',
           'description'    : '',
           'images'         : [{'width':6, 'path':png2base64src_maybe(rall_f, print_on_missing=DEBUG)},
                               {'width':6, 'path':png2base64src_maybe(rgap_f, print_on_missing=DEBUG)},
                               {'width':6, 'path':png2base64src_maybe(rfre_f, print_on_missing=DEBUG)},
                               {'width':6, 'path':png2base64src_maybe(rwor_f, print_on_missing=DEBUG)}],
           'plots'          : [{'div': 'variable-plots',
                                'json': p.tables.statistics.T.to_json(orient='split'),
                                'default_x' : 'High Resolution Limit',
                                'default_y' : 'R-free Change (Fitted-Input)'}],
           'table'          : p.tables.statistics.to_html(bold_rows=False, classes=['display nowrap'])\
                                        .replace('	','Dataset	')\
                                        .replace('border="1" ', ''),
          }

    return tab

def create_levels_tab(parameterisation):
    p = parameterisation
    f = parameterisation.fitter
    fm = parameterisation.file_manager

    chain_ids = [c.id for c in p.blank_master_hierarchy().select(flex.bool(p.atom_mask.tolist()),copy_atoms=True).chains()]

    tab = {'id'         : 'levels',
           'short_name' : 'ADP Summary',
           'long_name'  : 'Level-by-level TLS parameterisation',
           'description': 'Parameteristaion composed of {} levels.'.format(len(f.levels)),
           'tabs'       : [],
          }
    # -------------------------------->
    # Create overview sub-tab
    # -------------------------------->
    overview_tab = {'id'            : tab['id']+'overview',
                    'active'        : True,
                    'short_name'    : 'Overview',
                    'long_name'     : 'Overview of the parameterised hierarchical ADP model',
                    'description'   : '',
                    'panels'        : [],
                   }
    tab['tabs'].append(overview_tab)
    # Split the panels up by chain
    for c_id in chain_ids:
        # Split up the chains with divider panels
        prof_f = fm.get_file('png-combined-profile-template').format(c_id)
        resd_f = fm.get_file('png-residual-profile-template').format(c_id)
        panel = {'id'             : '
Levels for Chain {}
'.format(c_id),
                 'width'          : 12,
                 'show'           : True,
                 'table'          : None,
                 'objects'        : [{'width':6, 'text': 'TLS-level components', 'path':png2base64src_maybe(prof_f, print_on_missing=DEBUG)},
                                     {'width':6, 'text': 'Residual component',   'path':png2base64src_maybe(resd_f, print_on_missing=DEBUG)}],
                }
        overview_tab['panels'].append(panel)
        # Add images to the overview tab for each TLS level
        for i_level, (level_num, level_lab, level) in enumerate(f):
            chain_image = fm.get_file('pml-level-chain-template').format(level_num, c_id)
            stack_image = fm.get_file('png-tls-profile-template').format(level_num, c_id)
            aniso_image = fm.get_file('png-tls-anisotropy-template').format(level_num, c_id)
            panel = {'id'             : 'Level {} of {} ({})'.format(level_num, len(f.levels),level_lab),
                     'width'          : 4,
                     'show'           : True,
                     'table'          : None,
                     'objects'        : [{'width':12, 'text':'{} atoms.'.format('X')},
                                         {'width':12, 'path':png2base64src_maybe(chain_image, print_on_missing=DEBUG)},
                                         {'width':12, 'path':png2base64src_maybe(stack_image, print_on_missing=DEBUG)},
                                         {'width':12, 'path':png2base64src_maybe(aniso_image, print_on_missing=DEBUG)}],
                    }
            overview_tab['panels'].append(panel)
        # Format residual level
        chain_image = fm.get_file('pml-residual-chain-template').format(c_id)
        stack_image = fm.get_file('png-residual-profile-template').format(c_id)
        aniso_image = fm.get_file('png-residual-anisotropy-template').format(c_id)
        panel = {'id'             : 'Final Level (residual)',
                 'width'          : 4,
                 'show'           : True,
                 'table'          : None,
                 'objects'        : [{'width':12, 'text':'{} atoms.'.format('X')},
                                     {'width':12, 'path':png2base64src_maybe(chain_image, print_on_missing=DEBUG)},
                                     {'width':12, 'path':png2base64src_maybe(stack_image, print_on_missing=DEBUG)},
                                     {'width':12, 'path':png2base64src_maybe(aniso_image, print_on_missing=DEBUG)}],
                  }
        overview_tab['panels'].append(panel)
    # -------------------------------->
    # Create tab for each level
    # -------------------------------->
    for i_level, (level_num, level_lab, level) in enumerate(f):
        # Create dictionary for this tab and add to tab_list
        level_tab = {'id'         : tab['id']+'lvl{}'.format(level_num),
                     'short_name' : 'Level {}'.format(level_num),
                     'long_name'  : 'Level {} ({})'.format(level_num, level_lab),
                     'description': 'Level {} of {}. '.format(level_num, len(f.levels))+\
                                    'Composed of {} groups'.format(level.n_groups()),
                     'panels'       : [],
              }
        tab['tabs'].append(level_tab)
        # Add overview at the top of the tab
        for c_id in chain_ids:
            partn_image = fm.get_file('pml-level-partition-template').format(level_num, c_id)
            chain_image = fm.get_file('pml-level-chain-template').format(level_num, c_id)
            stack_image = fm.get_file('png-tls-profile-template').format(level_num, c_id)
            aniso_image = fm.get_file('png-tls-anisotropy-template').format(level_num, c_id)
            panel = {'id'             : 'Chain {}'.format(c_id),
                     'width'          : 12,
                     'show'           : True,
                     'table'          : None,
                     'objects'        : [{'width':12, 'text':'{} atoms.'.format('X')},
                                         {'width':6, 'path':png2base64src_maybe(partn_image, print_on_missing=DEBUG)},
                                         {'width':6, 'path':png2base64src_maybe(chain_image, print_on_missing=DEBUG)},
                                         {'width':6, 'path':png2base64src_maybe(stack_image, print_on_missing=DEBUG)},
                                         {'width':6, 'path':png2base64src_maybe(aniso_image, print_on_missing=DEBUG)}],
                    }
            level_tab['panels'].append(panel)
        # Read in the TLS models and amplitudes for this level
        tls_models     = pandas.read_csv(fm.get_file('csv-tls-mdl-template').format(level_num)).set_index(['group','model']).drop('Unnamed: 0', axis=1, errors='ignore')
        tls_amplitudes = pandas.read_csv(fm.get_file('csv-tls-amp-template').format(level_num)).set_index(['group','model','cpt']).drop('Unnamed: 0', axis=1, errors='ignore')
        # Extract groups for each level
        for i_group, (group_num, sel, group_fitter) in enumerate(level):
            # Extract TLS values for this group
            tls_vals = [tls_models.loc[(group_num, i_mode)] for i_mode in xrange(p.params.fitting.tls_models_per_tls_group)]
            # Skip if no TLS values
            if numpy.abs(tls_vals).sum() == 0.0:
                continue
            # Get images and format values
            scl_image = fm.get_file('pml-level-scaled-template').format(level_num, group_num)
            adp_image = fm.get_file('pml-level-group-template').format(level_num, group_num)
            amp_image = fm.get_file('png-tls-amp-dist-template').format(level_num, group_num)
            tls_mdl_strs = [('Mode {}:
' + \
                             '<samp>\n' + \
                             'T: {T11:>9.3f}, {T22:>9.3f}, {T33:>9.3f}, {T12:>9.3f}, {T13:>9.3f}, {T23:>9.3f},
' + \
                             'L: {L11:>9.3f}, {L22:>9.3f}, {L33:>9.3f}, {L12:>9.3f}, {L13:>9.3f}, {L23:>9.3f},
' + \
                             'S: {S11:>9.3f}, {S12:>9.3f}, {S13:>9.3f}, {S21:>9.3f}, {S22:>9.3f}, {S23:>9.3f}, {S31:>9.3f}, {S32:>9.3f}, {S33:>9.3f}' + \
                             '\n</samp>'
                            ).format(i_mode+1, **mode_vals.round(3)).replace(' ','&nbsp') if mode_vals.any() else 'Zero-value TLS values for mode {}'.format(i_mode+1) for i_mode, mode_vals in enumerate(tls_vals)]
            # Create panel dictionary
            panel = {'id'    : 'Group {} - {}'.format(group_num, p.levels[i_level][i_group]),
                     'width' : 12, #max(4,12//level.n_groups()),
                     'table' : None,
                     'objects': [{'width':12, 'text':'
'.join(['Number of atoms: {}'.format(sum(sel))])},
                                 {'width':4,  'text':'Shape of disorder (arbitrary scale)',     'path': png2base64src_maybe(scl_image, print_on_missing=DEBUG)},
                                 {'width':4,  'text':'Average size over all datasets',          'path': png2base64src_maybe(adp_image, print_on_missing=DEBUG)},
                                 {'width':4,  'text':'Amplitude Distribution',                  'path': png2base64src_maybe(amp_image, print_on_missing=DEBUG)}] + \
                                [{'width':12,'text':s} for s in tls_mdl_strs],
                    }
            level_tab['panels'].append(panel)
        # Make  the first panel open
        if len(level_tab['panels']) > 0:
            level_tab['panels'][0]['show'] = True
    # -------------------------------->
    # Create tab for residual level
    # -------------------------------->
    residual_tab = {'id'         : 'lvlres',
                    'short_name' : 'Residual',
                    'long_name'  : 'Final Level  (residual)',
                    'description': '',
                    'panels'     : [],
                   }
    tab['tabs'].append(residual_tab)
    # Get selection for fitted atoms
    atom_sel = flex.bool(p.atom_mask.tolist())
    # Create row for each residue
    for i_chain, c in enumerate(p.blank_master_hierarchy().select(atom_sel,copy_atoms=True).chains()):
        panel = {'id'    : 'Residual components for chain {}'.format(c.id),
                 'width' : 12,
                 'table' : None,
                 'objects': [],
                }
        residual_tab['panels'].append(panel)
        for i_rg, rg in enumerate(c.residue_groups()):
            short_label = ShortLabeller.format(rg)
            long_label  = Labeller.format(rg)
            adp_image = fm.get_file('pml-residual-group-template').format(short_label)
            panel['objects'].append({'width':4, 'text':long_label, 'path': png2base64src_maybe(adp_image, print_on_missing=DEBUG)})
        # Make  the first panel open
        residual_tab['panels'][0]['show'] = True

    return tab

def write_adp_summary(parameterisation, out_dir_tag='root'):
    """Write an overall, and a level-by-level summary of the parameterised ADPs"""

    # Get the html template
    template = PANDEMIC_HTML_ENV.get_template('adp_summary.html')
    # Create output file path
    out_file = parameterisation.file_manager.add_file(file_name='results.html', file_tag='results_html', dir_tag=out_dir_tag)

    # ===========================================================>
    # Construct the data object to populate the template
    output_data = {}
    output_data['header'] = 'PanDEMIC ADP Output Summary'
    output_data['title'] = 'PanDEMIC ADP Parameterisation'
    output_data['introduction'] = 'Hierarchical ADP parameterisation summary from pandemic.adp'
    # ===========================================================>
    # Construct the tabs
    output_data['tabs'] = []

    ##########################################################################################
    # Create overview tab
    ##########################################################################################
    output_data['tabs'].append(create_overview_tab(parameterisation=parameterisation))
    output_data['tabs'][-1]['active'] = True
    ##########################################################################################
    # Create statistics tab
    ##########################################################################################
    output_data['tabs'].append(create_analysis_tab(parameterisation=parameterisation))
    ##########################################################################################
    # Create adp-levels tab
    ##########################################################################################
    output_data['tabs'].append(create_levels_tab(parameterisation=parameterisation))
    ##########################################################################################
    # Write out and format
    ##########################################################################################
    with open(out_file, 'w') as out_html:
        out_html.write(template.render(output_data).encode( "utf-8" ))


import torch
import pandas as pd
import numpy as np
import torch.nn as nn
import pickle
from pathlib import Path
from sklearn import metrics as sk_metrics
from tqdm import tqdm
import torch.optim as optim
import copy
import matplotlib.pyplot as plt

file_extension = ".csv"
parent_name ="checkpoints/"
output_directory ="outputs/"

class Loader(torch.utils.data.Dataset):
    def __init__(self):
        super(Loader, self).__init__()
        self.dataset = ''

    def __len__(self):
        return len(self.dataset)

    def __getitem__(self, idx):
        row = self.dataset.iloc[idx]
        row = row.drop(labels={'label'})
        data = torch.from_numpy(np.array(row)).float()
        return data

class Train_Loader(Loader):
    def __init__(self):
        super(Train_Loader, self).__init__()
        self.dataset = pd.read_csv(
                       'data/random_train.csv',
                       index_col=False
                       )

def ae_train(model, X_loader, l_r = 1e-2, w_d = 1e-5, n_epochs = 1, batch_size = 32, save_errors = True):
    device = 'cuda' if torch.cuda.is_available() else 'cpu'
    model.to(device)
    optimizer = torch.optim.SGD(model.parameters(), lr=l_r, weight_decay=w_d)
    criterion = nn.MSELoss(reduction='mean')
    model.train(True)
    errors = []
    for epoch in tqdm(range(n_epochs)):
        epoch_loss = []
        for step, batch in enumerate(X_loader):
            x_in = batch.type(torch.float32)
            x_in = x_in.to(device)
            x_out = model(x_in)
            loss = criterion(x_out, x_in)
            model.zero_grad()
            loss.backward()
            optimizer.step()
            torch.autograd.set_detect_anomaly(True)
            epoch_loss.append(loss.item())
        errors.append(sum(epoch_loss)/len(epoch_loss))
        print("epoch {}: {}".format(epoch+1, sum(epoch_loss)/len(epoch_loss)))
    if save_errors:
        np.savetxt(output_directory+"_training_loss_"+model.name+file_extension, errors, delimiter=',')

def mlp_train(model, X_train, y_train, X_val, y_val, l_r = 1e-2, w_d = 1e-5, n_epochs = 1, batch_size = 32, save_errors = True):
    # training parameters
    X_train = torch.tensor(X_train, dtype=torch.float32)
    y_train = torch.tensor(y_train, dtype=torch.float32).reshape(-1, 1)
    X_val = torch.tensor(X_val, dtype=torch.float32)
    y_val = torch.tensor(y_val, dtype=torch.float32).reshape(-1, 1)
    batch_start = torch.arange(0, len(X_train), batch_size)

    # Hold the best model
    best_mse = np.inf   # init to infinity
    best_weights = None
    history = []

    loss_fn = nn.MSELoss()  # mean square error
    optimizer = optim.Adam(model.regressor.parameters(), lr=0.0001)

    # training loop
    for epoch in range(n_epochs):
        model.train()
        with tqdm(batch_start, unit="batch", mininterval=0, disable=True) as bar:
            bar.set_description(f"Epoch {epoch}")
            for start in bar:
                # take a batch
                X_batch = X_train[start:start+batch_size]
                y_batch = y_train[start:start+batch_size]
                # forward pass
                y_pred = model(X_batch)
                loss = loss_fn(y_pred, y_batch)
                # backward pass
                optimizer.zero_grad()
                loss.backward()
                # update weights
                optimizer.step()
                # print progress
                bar.set_postfix(mse=float(loss))
        # evaluate accuracy at end of each epoch
        model.eval()
        y_pred = model(X_val)
        mse = loss_fn(y_pred, y_val)
        mse = float(mse)
        history.append(mse)
        if mse < best_mse:
            best_mse = mse
            best_weights = copy.deepcopy(model.state_dict())

    # restore model and return best accuracy
    model.load_state_dict(best_weights)
    print("MSE: %.5f" % best_mse)
    print("RMSE: %.5f" % np.sqrt(best_mse))
    #plt.plot(history)
    #plt.show()

def forest_train(model, X_train, y_train, max_depth = 3, save_errors = True):
    # training parameters

    model.regressor.fit(X_train, y_train)

def model_eval(model, x):
    loss_fn = nn.MSELoss()
    model.eval()
    x_pred = model(x)
    loss_val = loss_fn(x, x_pred)
    return loss_val

def compute_metrics(val_score, y_val, thresh, pos_label=1):
    """
    This function compute metrics for a given threshold

    Parameters
    ----------
    test_score
    y_test
    thresh
    pos_label

    Returns
    -------

    """
    y_pred = (val_score >= thresh).astype(int)
    y_true = y_val.astype(int)

    accuracy = sk_metrics.accuracy_score(y_true, y_pred)
    precision, recall, f_score, _ = sk_metrics.precision_recall_fscore_support(
        y_true, y_pred, average='binary', pos_label=pos_label
    )
    avgpr = sk_metrics.average_precision_score(y_true, val_score)
    roc = sk_metrics.roc_auc_score(y_true, val_score)
    cm = sk_metrics.confusion_matrix(y_true, y_pred, labels=[1, 0])

    return accuracy, precision, recall, f_score, roc, avgpr, cm

def estimate_optimal_threshold(val_score, y_val, pos_label=1, nq=100):
    ratio = 100 * sum(y_val == 0) / len(y_val)
    print("ratio", ratio)
    print(f"Ratio of normal data:{ratio}")
    q = np.linspace(ratio - 5, min(ratio + 5, 100), nq)
    thresholds = np.percentile(val_score, q)

    result_search = []
    confusion_matrices = []
    f1 = np.zeros(shape=nq)
    r = np.zeros(shape=nq)
    p = np.zeros(shape=nq)
    auc = np.zeros(shape=nq)
    aupr = np.zeros(shape=nq)
    qis = np.zeros(shape=nq)

    for i, (thresh, qi) in enumerate(zip(thresholds, q)):
        # print(f"Threshold :{thresh:.3f}--> {qi:.3f}")
        # Prediction using the threshold value
        accuracy, precision, recall, f_score, roc, avgpr, cm = compute_metrics(val_score, y_val, thresh, pos_label)

        confusion_matrices.append(cm)
        result_search.append([accuracy, precision, recall, f_score])
        # print(f"qi:{qi:.3f} ==> p:{precision:.3f}  r:{recall:.3f}  f1:{f_score:.3f}")
        f1[i] = f_score
        r[i] = recall
        p[i] = precision
        auc[i] = roc
        aupr[i] = avgpr
        qis[i] = qi

    arm = np.argmax(f1)

    return {
        "Precision": p[arm],
        "Recall": r[arm],
        "F1-Score": f1[arm],
        "AUPR": aupr[arm],
        "AUROC": auc[arm],
        "Thresh_star": thresholds[arm],
        "Quantile_star": qis[arm]
    }

def save(model):
    Path(parent_name).mkdir(parents=True, exist_ok=True)
    with open(parent_name+model.name+".pickle", "wb") as fp:
        pickle.dump(model.state_dict(), fp)

def load(model):
    with open(parent_name+model.name+".pickle", "rb") as fp:
        model.load_state_dict(pickle.load(fp))


import speech_recognition as sr
#this is used to recognise the speech in the video
import moviepy.editor as mp
#this splits the videos in small cuts
from moviepy.video.io.ffmpeg_tools import ffmpeg_extract_subclip
#Extract the sound from a video file and save it in outputfile .

num_seconds_video= 88*60
#deciding the size of the video
print("The video is {} milliseconds".format(num_seconds_video))

l=list(range(0,num_seconds_video+1,60))
#using the range function to break the video in parts of
#60 seconds each.

diz={}
for i in range(len(l)-1):
  #breaking the video into sub parts
  #and storing them in the folder called chunks.
  #cut{}.mp4".format(i+1)----the files are named in this format
  #for example cut1.mp4,cut2.mp4-----cutn.mp4.
    ffmpeg_extract_subclip("videol.mp4", l[i]-2*(l[i]!=0), l[i+1], targetname="chunks/cut{}.mp4".format(i+1))
    clip = mp.VideoFileClip(r"chunks/cut{}.mp4".format(i+1))
    clip.audio.write_audiofile(r"converted/converted{}.wav".format(i+1))
    #converting the video subparts to audio files and writing them in the converted folder
    r = sr.Recognizer()
    audio = sr.AudioFile("converted/converted{}.wav".format(i+1))
    #speech recognition
    with audio as source:
      r.adjust_for_ambient_noise(source)
      #to check if there is any spoken phrase during silence or
      # if there is any spoken phase during background lines.
      audio_file = r.record(source)
    result = r.recognize_google(audio_file)
    diz['chunk{}'.format(i+1)]=result

    l_chunks=[diz['chunk{}'.format(i+1)] for i in range(len(diz))]
    text='\n'.join(l_chunks)

    #writing the recognized text from the converted audio..
    #in the text file
    #which is named as recognized.txt
    with open('recognized.txt',mode ='w') as file:
    #opening the file in write mode.
      file.write("Recognized Speech:")
    #writing the text in the file
      file.write("\n")
      file.write(text)
      print("Finally ready!")


# -*- coding: utf-8 -*-
"""
Created on Wed Apr 28 13:26:20 2021

@author: diego
"""

#import neccesary libraries
from progressbar import progressbar
import numpy as np
import random
import os
import tensorflow as tf # tensorflow 2.0 Google Deep learning release
import cv2 as cv
import imgaug.augmenters as iaa
import skimage
from skimage.morphology import disk
from skimage.filters import threshold_otsu, rank
from skimage.filters import try_all_threshold, threshold_minimum, threshold_mean
try:
    from . import hairReplacement
except:
    import hairReplacement
imageSize=128
seed=0
np.random.seed(seed) # fix random seed
tf.random.set_seed(seed)

kernel_size_for_edges=int(imageSize/10)#has to be odd

#allows the user to change adjust their desired processing method
######Takes a directory as input########
def adjustableProcessing(dataDir,labels,evenRatio=True, hairRemoval=True,hairRemovalMethod=3,borderRemoval=True,onlyBorderless=False,
                              segmentation=True,SegmentationMethod="otsu"):

    if borderRemoval==True and onlyBorderless==True:
        raise ValueError("Cannot have borderRemoval and onlyBorderless both set as True")
    randomIndices=[]
    counter=0
    melNum=0
    #declare array
    #imgArray=[]
    #colour arry
    imgArray=[]
            #get images and proccess them so the potential melanomas have more defined borders
    for file in progressbar(os.listdir(dataDir)):
        #get colour img
        colourImg=cv.resize(cv.imread(os.path.join(dataDir,file)), (300,300))#300 is the ideal size for hair removal method 2

        if hairRemoval==True:
            colourImg=hairReplacement.deleteHair(colourImg,method=hairRemovalMethod)

        colourImg= cv.resize(colourImg, (128,128))

        if borderRemoval==True:
            colourImg=BorderRemoval(colourImg)
        if onlyBorderless==True:
            if findBorders(colourImg)==True:#do this after hair removal so hairs do not count
            #remove labels and skip any following steps
                 labels=np.delete(labels, (counter), axis=0)
        #reshape img
        if segmentation==True:
            colourImg=Segmentation(colourImg,SegmentationMethod)

        #aka augmentation
        if evenRatio==True:
            #if the prediction is benign do not augment
            if labels[counter,1]==0:

                imgArray.append(np.float32(colourImg))
                counter=counter+1
            else:#duplicate and rotate twice and add to array
                #append original img
                imgArray.append(np.float32(colourImg))
                #append flipped and mirrored img
                imgArray.append(np.float32( cv.flip(colourImg, -1)))
                #append 90 deg. rotated img
                imgArray.append(np.float32(np.rot90(colourImg,axes=(0,1))))
                #append rotated and zoomed in img
                rotate=iaa.Affine(rotate=(-20, 20))
                rotatedImg=rotate.augment_image(colourImg)
                crop = iaa.Crop(percent=(0, 0.15)) # crop image (zoom in)
                imgArray.append(np.float32(crop.augment_image(rotatedImg)))
                #duplicate lables 3 times
                labels=np.insert(labels,counter,labels[counter],axis=0)
                labels=np.insert(labels,counter,labels[counter],axis=0)
                labels=np.insert(labels,counter,labels[counter],axis=0)

                counter=counter+4
                #keep track of the num of melanomas
                melNum=melNum+4
        else:
            imgArray.append(np.float32(colourImg))
    if evenRatio==True:
        #convert list to array
        imgArray=np.array(imgArray)
        nonMelIndex=np.where(labels[:,1]==0)
        nonMelIndex=np.asarray(nonMelIndex)
         #convert to 1d array and get the images with such index
        nonMelIndex=nonMelIndex[0,:]
        numOfExpendableImgs=(len(nonMelIndex)-melNum)
        print ("Number of non Mel. imgs: " + str(len(nonMelIndex)))

        print ("Number of non Mel. deleted to make a 1:1 ratio: " + str(numOfExpendableImgs))
        if numOfExpendableImgs >0:
            randomIndices=random.sample((nonMelIndex.tolist()),numOfExpendableImgs)# random (delete)
            #delete random non mel imgs
            labels=np.delete(labels, (randomIndices), axis=0)
            imgArray=np.delete(imgArray, (randomIndices), axis=0)
    else:
        #convert list to array
        imgArray=np.array(imgArray)
        labels=np.array(labels)

    return imgArray,labels,randomIndices#,imgArray

#allows the user to change adjust their desired processing method
######Takes an array of images as input########
def adjustableImageProcessing(imgs,labels,evenRatio=True, hairRemoval=True,hairRemovalMethod=3,hairLevels=None,
                              removalLowerBound=2,borderRemoval=True,
                              segmentation=True,imgSize=128,SegmentationMethod="otsu"):
    randomIndices=[]
    counter=0
    melNum=0
    noise_modes = ['gaussian', 'localvar', 'poisson', 'salt', 'pepper', 's&p', 'speckle']
    noise_mode = noise_modes[random.randint(0, 6)]
    #declare array
    #imgArray=[]
    #colour arry
    imgArray=[]
    imgCounter=0
        #get images and proccess them so the potential melanomas have more defined borders
    for img in progressbar(imgs):
        colourImg=img
        if hairRemoval==True:
            #only remove if there are hairs
            if hairLevels==None:

                colourImg=hairReplacement.deleteHair(colourImg,method=hairRemovalMethod)

            elif hairLevels[imgCounter]>removalLowerBound:#only delete hairs if image contains hairs
                colourImg=hairReplacement.deleteHair(colourImg,method=hairRemovalMethod)

        if borderRemoval==True:
            colourImg=BorderRemoval(colourImg)

        #reshape img
        colourImg= cv.resize(colourImg, (imgSize,imgSize))
        if segmentation==True:
            colourImg=Segmentation(colourImg,SegmentationMethod)
        #Augmentation
        if evenRatio==True:
            # if labels[counter,1]==0:
            #     imgArray.append(np.float32(colourImg))
            #     counter=counter+1
            # else:#duplicate and rotate twice and add to array
                #append og img
                imgArray.append(np.float32(colourImg))
                #append flipped and mirrored img
                #imgArray.append(np.float32( cv.flip(colourImg, -1)))
                #append 90 deg. rotated img cropped by 8%
                crop = iaa.Crop(percent=(0, 0.08)) # crop image
                croppedImg=crop.augment_image(colourImg)
                imgArray.append(np.float32(np.rot90(croppedImg,axes=(0,1))))
                #append rotated and zoomed in img
                # rotate=iaa.Affine(rotate=(-20, 20))
                # rotatedImg=rotate.augment_image(colourImg)
                # crop = iaa.Crop(percent=(0, 0.1)) # crop image
                # imgArray.append(np.float32(crop.augment_image(rotatedImg)))
                #add image with noise
                noisyImg = skimage.util.random_noise(colourImg, mode=noise_mode)
                rotate=iaa.Affine(rotate=(-180, 180))
                rotatedNoisyImg=rotate.augment_image(noisyImg)
                imgArray.append(np.float32(np.rot90(rotatedNoisyImg,axes=(0,1))))
                #duplicate groundtruth 3 times
                labels=np.insert(labels,counter,labels[counter],axis=0)
                labels=np.insert(labels,counter,labels[counter],axis=0)
                #labels=np.insert(labels,counter,labels[counter],axis=0)
                #labels=np.insert(labels,counter,labels[counter],axis=0)

                ###Change +3 if more images are added when augmented
                counter=counter+3
                #keep track of the num of melanomas
                melNum=melNum+3
        else:
            imgArray.append(np.float32(colourImg))
        imgCounter=imgCounter+1
    if evenRatio==True:
        #convert list to array
        imgArray=np.array(imgArray)
        melIndex=np.where(labels[:,1]==1)
        melIndex=np.asarray(melIndex)
         #convert to 1d array and get the images with such index
        melIndex=melIndex[0,:]

        nonMelIndex=np.where(labels[:,1]==0)
        nonMelIndex=np.asarray(nonMelIndex)
         #convert to 1d array and get the images with such index
        nonMelIndex=nonMelIndex[0,:]
        numOfExpendableImgs=(len(nonMelIndex)-len(melIndex))
        print("Number of melanoma pictures: %s" %len(melIndex))
        print ("Number of non Mel. imgs: " + str(len(nonMelIndex)))

        print ("Number of non Mel. deleted to make a 1:1 ratio: " + str(numOfExpendableImgs))
        if len(melIndex)<len(nonMelIndex):
            randomIndices=random.sample((nonMelIndex.tolist()),numOfExpendableImgs)# 6k random (delete)
        else:
            randomIndices=random.sample((melIndex.tolist()),numOfExpendableImgs*(-1))# 6k random (delete)

        #delete random non mel imgs
        labels=np.delete(labels, (randomIndices), axis=0)
        imgArray=np.delete(imgArray, (randomIndices), axis=0)
    else:
        #convert list to array
        imgArray=np.array(imgArray)
        labels=np.array(labels)

    return imgArray,labels,randomIndices#,imgArray
#use otsu to segment
def Segmentation(colourImg,method='otsu'):
        #black and white image
        colourImg=colourImg.astype('uint8')
        grayImg = cv.cvtColor(colourImg, cv.COLOR_BGR2GRAY)
        #otsu threshold removes backgroud and inter. like hair
        if method=='otsu':
            radius = 3
            selem = disk(radius)
            #apply otsu to img
            local_otsu = rank.otsu(grayImg, selem)
            threshold_global_otsu = threshold_otsu(grayImg)
            #use the global threshold to remove pixels that are not dark enough
            global_otsu = local_otsu >= threshold_global_otsu
            # mean_thresh = threshold_mean(grayImg)
            # #use the global threshold to remove pixels that are not dark enough
            # global_otsu = local_otsu >= mean_thresh
            #remove background from colour image
            colourImg[global_otsu[:,:]==True,:]=255
        else:
            print("No other methods avaliable at the moment")
        return colourImg

    #remove most part of the darkest borders which enhancest the segmenting process
def BorderRemoval(colourImg):
        blur = cv.GaussianBlur(colourImg, (5,5),2)#helps conserving dark values in lesions
        black=np.array([3,3,3])
        white=np.array([255,255,255])
        #conserve all the values lighter than rgb(3,3,3)
        mask = cv.inRange(blur, black, white)
        #trim edges
        kernel = cv.getStructuringElement(cv.MORPH_ELLIPSE, (int(kernel_size_for_edges),int(kernel_size_for_edges)))
        mask = cv.dilate(cv.bitwise_not(mask.astype('uint8')), kernel)
        mask =cv.bitwise_not(mask)

        if np.any(mask==0):

             #replace dark pixels with with pixels
             colourImg[mask[:,:]==0,:]=255
            # colourImg = cv.inpaint(colourImg, mask, inpaintRadius=25, flags=cv.INPAINT_TELEA)
        return colourImg
def findBorders(colourImg):
        blur = cv.GaussianBlur(colourImg, (5,5),2)#helps conserving small dark values in lesions
        black=np.array([3,3,3])
        white=np.array([255,255,255])
        #conserve all the values lighter than rgb(3,3,3)
        mask = cv.inRange(blur, black, white)
        #do not return an image if there are black values
        if np.any(mask==0):
            return True
        return False


import pygame
import random
from lerp import lerp
from dudepose import DudePose

pygame.init()

DISPLAY_WIDTH = 600
DISPLAY_HEIGHT = 400

BLACK = (0,0,0)
WHITE = (255,255,255)

gameDisplay = pygame.display.set_mode((DISPLAY_WIDTH,DISPLAY_HEIGHT))
pygame.display.set_caption('My Cool Graphics')
clock = pygame.time.Clock()

families = []

class Tempo:
  startTime = 0
  msPerBeat = 500
  beatsPerMeasure = 4

  def __init__(self, beatsPerMinute, beatsPerMeasure, delay=0):

    self.msPerBeat = 1/(beatsPerMinute/(60*1000))

    self.startTime = pygame.time.get_ticks() + (delay * self.msPerBeat)
    self.beatsPerMeasure = beatsPerMeasure

  def getBeat(self):
    beats = self.getTotalBeats()
    fraction = beats - int(beats)
    whole = int(beats)
    measured = whole % self.beatsPerMeasure

    return measured + fraction

  def getTotalBeats(self):
    now = pygame.time.get_ticks()
    deltaTime = now - self.startTime
    beats = deltaTime/self.msPerBeat

    return beats

  def drawTempo(self, gameDisplay, position):
    beat = self.getBeat()
    beat = int(beat)

    for i in range(self.beatsPerMeasure):
      if i <= beat:
        pygame.draw.circle(gameDisplay, (0, 255, 0), [position[0] + i * 20, position[1]], 5)

pygame.mixer.init()
pygame.mixer.music.load('Village-People-YMCA.mp3')
pygame.mixer.music.play()

# create the shared Tempo object.
TEMPO = Tempo(126,4,2)

class Dude:
  animationFinished = True

  def __init__(self, position, size):
    self.position = position
    self.size = size
    self.leftHanded = random.randrange(100) < 20
    # default the time to a random time so that all animations don't start at exactly
    # the same time
    self.time = pygame.time.get_ticks() + random.randrange(3000)
    self.frameCount = 0

  def getExtentsRect(self):
    return pygame.Rect([0, 0, 16*self.size, 16*self.size])

  def drawLine(self, gameDisplay, startingPoint, endingPoint, color = (0,0,0)):
    scale = self.size
    posAdjusted = (self.position[0], self.position[1] - (16*scale))

    pygame.draw.line(gameDisplay, color, (posAdjusted[0]+(startingPoint[0] * scale),posAdjusted[1]+(startingPoint[1] * scale)), (posAdjusted[0]+(endingPoint[0] * scale),posAdjusted[1]+(endingPoint[1] * scale)))

  def drawCircle(self, gameDisplay, centerPoint, radius, color = (0,0,0), lineWidth=1):
    scale = self.size
    posAdjusted = (self.position[0], self.position[1] - (16*scale))

    pygame.draw.circle(gameDisplay, color, [posAdjusted[0] + (centerPoint[0]*scale),posAdjusted[1] + (centerPoint[1]*scale)], radius*scale, lineWidth)

  def drawStanding(self, gameDisplay):

    # this was just to show you that we could possibly do this another way.
    # ignore for now
    # L8,4,8,12,2,2,8,6,14,2,8,6,8,12,4,14,8,1,12,14
    # C8,2,2

    # body
    self.drawLine(gameDisplay, (8,4), (8,12) )

    # left arm
    self.drawLine(gameDisplay, (2,2), (8,6))

    # right arm

    self.drawLine(gameDisplay, (14,2), (8,6))

    # left leg

    self.drawLine(gameDisplay, (8,12), (4,16))
    # right leg

    self.drawLine(gameDisplay, (8,12), (12,16))

    # draw head
    self.drawCircle(gameDisplay, (8,2), 2)

    # wouldn't it be nice to just be able to do this instead!
    # [[(8,12), (4,6)], [(8,12), (12,16)]]

  def drawTap(self, gameDisplay):

    # body
    self.drawLine(gameDisplay, (8,4), (8.5,12) )

    # left arm
    self.drawLine(gameDisplay, (3,3), (8,6))
    self.drawLine(gameDisplay, (2,3), (3,3))

    # right arm

    self.drawLine(gameDisplay, (14,2), (8,6))

    # left leg

    self.drawLine(gameDisplay, (8.5,12), (4,16))
    # right leg

    self.drawLine(gameDisplay, (8.5,12), (11,14))
    self.drawLine(gameDisplay, (11,14), (12,16))

    # draw head
    self.drawCircle(gameDisplay, (8,2), 2)

  def drawM(self, gameDisplay):

    # draw the M in YMCA

    # body
    self.drawLine(gameDisplay, (8,4), (8,12) )

    # left arm
    self.drawLine(gameDisplay, (4,6), (8,6))
    self.drawLine(gameDisplay, (4,6), (4,8))

    # right arm

    self.drawLine(gameDisplay, (12,6), (8,6))
    self.drawLine(gameDisplay, (12,6), (12,8))

    # left leg

    self.drawLine(gameDisplay, (8,12), (4,16))
    # right leg

    self.drawLine(gameDisplay, (8,12), (12,16))

    # draw head
    self.drawCircle(gameDisplay, (8,2), 2, (255,0,0))

  def drawC(self, gameDisplay):

    # draw the C in YMCA

    # body
    self.drawLine(gameDisplay, (8,4), (8,12) )

    # left arm
    self.drawLine(gameDisplay, (4,6), (8,6))
    self.drawLine(gameDisplay, (4,6), (4,0))
    self.drawLine(gameDisplay, (4,0), (12,0))

    # right arm

    self.drawLine(gameDisplay, (12,6), (8,6))

    # left leg

    self.drawLine(gameDisplay, (8,12), (4,16))
    # right leg

    self.drawLine(gameDisplay, (8,12), (12,16))

    # draw head
    self.drawCircle(gameDisplay, (8,2), 1.9, (0,255,0))

  def drawA(self, gameDisplay):

    # draw the A in YMCA
    # body
    self.drawLine(gameDisplay, (8,4), (8,12) )

    # left arm
    self.drawLine(gameDisplay, (4,6), (8,6))
    self.drawLine(gameDisplay, (4,6), (7,1))

    # right arm

    self.drawLine(gameDisplay, (12,6), (8,6))
    self.drawLine(gameDisplay, (12,6), (9,1))

    # left leg

    self.drawLine(gameDisplay, (8,12), (4,16))
    # right leg

    self.drawLine(gameDisplay, (8,12), (12,16))

    # draw head
    self.drawCircle(gameDisplay, (8,2), 2, (255,0,0))

  def animateYMCA(self, gameDisplay):

    beat = TEMPO.getBeat()
    totalBeats = TEMPO.getTotalBeats()

    if beat < 2:
      self.drawStanding(gameDisplay)
    elif beat < 3:
      self.drawM(gameDisplay)
    elif beat < 3.5:
      self.drawC(gameDisplay)
    else:
      self.drawA(gameDisplay)

    if totalBeats - self.startingBeat >= 4:
      # we're done with our animation, so return True
      return True

    # we're not done with our animation so return False
    return False

  def animateStanding(self, gameDisplay):
    # the standing animation will have the Dude tap their hand

    beat = TEMPO.getBeat()
    totalBeats = TEMPO.getTotalBeats()

    if beat - int(beat) < .2:
      self.drawTap(gameDisplay)
    else:
      self.drawStanding(gameDisplay)

    if totalBeats - self.startingBeat >= 4:
      # we're done with our animation, so return True
      return True

    return False

  def draw(self,gameDisplay):
    # pose = DudePose(headRadius=4)
    # pose.draw(gameDisplay, self.position, self.size)

    pose = DudePose()

    poseM = DudePose(lowerArmLeft=[[12,6],[12,8]],
                      upperArmLeft=[[8,6],[12,6]],
                      lowerArmRight=[[4,6],[4,8]],
                      upperArmRight=[[8,6],[4,6]])

    poseBigHead = DudePose(headRadius=3)

    poseRunning1 = DudePose(upperLegLeft=[[8,12], [8,14]],
                            lowerLegLeft=[[8,14], [8,16]],
                            upperLegRight=[[8,12],[10,14]],
                            lowerLegRight=[[10,14],[9,16]])

    # pose.draw(gameDisplay, self.position, self.size)

    # poseHalfWay = DudePose.lerp(pose, poseM, .25)

    # poseHalfWay.draw(gameDisplay, self.position, self.size)

    beat = TEMPO.getBeat()

    if beat - int(beat) < .5:
        headBeat = DudePose.lerp(pose, poseBigHead, (beat-int(beat))*2, pose, ["headRadius"])
    else:
        headBeat = DudePose.lerp(poseBigHead, pose, (beat-int(beat)-.5)*2, pose, ["headRadius"])

    if beat - int(beat) < 1:
        lerp = DudePose.lerp(pose, poseRunning1, (beat-int(beat)), headBeat, ["upperLegLeft", "lowerLegLeft", "upperLegRight", "lowerLegRight"])

    if beat < 1:
      lerp.draw(gameDisplay, self.position, self.size)
    elif beat < 1.5:
      lerpPose = DudePose.lerp(pose, poseM, (beat-1)*2, lerp, ["lowerArmLeft", "upperArmLeft", "lowerArmRight", "upperArmRight"])
      lerpPose.draw(gameDisplay, self.position, self.size)
    else:
      lerpPose = DudePose.lerp(pose, poseM, 1, lerp, ["lowerArmLeft", "upperArmLeft", "lowerArmRight", "upperArmRight"])
      lerpPose.draw(gameDisplay, self.position, self.size)

    # if self.animationFinished:
    #   animations = [self.animateStanding, self.animateYMCA, self.animateYMCA, self.animateYMCA]
    #   self.startingBeat = TEMPO.getTotalBeats()

    #   # alternate between the standing animation and the YMCA
    #   # animation

    #   self.animation = animations[self.frameCount % len(animations)]
    #   self.frameCount = self.frameCount + 1

    # if self.animation:
    #   self.animationFinished = self.animation(gameDisplay)

class Family:
  def __init__(self, position, size):
    self.position = position
    self.size = size

    self.members = []

    x = position[0]
    # 80% chance that we have a mom
    if random.randrange(100) < 80:
      mom = Dude((x, position[1]), 5 * size)
      self.members.append(mom)
      x = x + mom.getExtentsRect().width
    # 80% chance that we have a dad
    if random.randrange(100) < 80:
      dad = Dude((x, position[1]), 4 * size)
      self.members.append(dad)
      x = x + dad.getExtentsRect().width

    for i in range(random.randrange(4)):
      kid = Dude((x, position[1]), (random.randrange(3) + 1) * size)
      self.members.append(kid)
      x = x + kid.getExtentsRect().width

  def getExtentsRect(self):
    width = 0
    height = 0
    for i in self.members:
      memberRect = i.getExtentsRect()
      width = width + memberRect.width
      if memberRect.height > height:
        height = memberRect.height

    return pygame.Rect([0, 0, width, height])

  def draw(self, gameDisplay):
    for i in self.members:
      i.draw(gameDisplay)

def setup():
  gameDisplay.fill(WHITE)
  pygame.display.update()

  global families

  rows = 5
  for y in range(rows):
    columns = 7 * (y+1)
    # columns = 10
    if y % 2 == 1:
      xPos = 25
    else:
      xPos = 0
    for x in range(columns):
      family = Family((xPos,DISPLAY_HEIGHT - 200 * (y/rows)), 1 - y/rows)
      xPos = xPos + family.getExtentsRect().width
      families.append(family)

  # family = Family((100,100), 1)
  # families.append(family)

def graphicsLoop():
    gameExit = False

    while not gameExit:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                print("Goodbye.")
                quit()

        # erase the screen on each loop by filling the screen white
        gameDisplay.fill(WHITE)

        TEMPO.drawTempo(gameDisplay, (10,10))
        for i in families:
          i.draw(gameDisplay)

        # startingColor = (255,127,0)
        # endingColor = (0,127,255)

        # for i in range(100):
        #   pygame.draw.line(gameDisplay, lerp(startingColor, endingColor, i/100),[100 + i,0], [100+i, 100])

        pygame.display.update()

        # limit the refresh rate to 60 fps
        clock.tick(60)

print(lerp((0,0,0), (255,255,255), 2))

setup()
graphicsLoop()


class MinStack(object):
    """
        Min stack works just like regular stack except, it stores the current
        `minimum value` so far.
        Example: 4, 6, 3, 5, 2, 1 <- stack is added this way, 1 is at the top
                (4, 4, 3, 3, 2, 1)
                (1, 1)
                (2, 2)
                (5, 3)
                (3, 3)
                (6, 4)
                (4, 4)
    """
    def __init__(self):
        """
        Initialize data structure, store the val and min number
        """
        # stack => [(curr_val, min_val_so_far)]
        self.stack = []

    def push(self, x):
        """
        :type x: int
        :rtype: None
        """
        curr_min = self.getMin()
        if curr_min is None or curr_min > x:
            # update the current min
            curr_min = x
        # add the new element and store the curr min so far
        self.stack.append((x, curr_min))

    def pop(self):
        """
        :rtype: None
        """
        if self.stack:
            # pop the top of the element, the tuple
            self.stack.pop()

    def top(self):
        """
        :rtype: int
        """
        if self.stack:
            # access the top element, it is the 0th element in tuple
            return self.stack[-1][0]

    def getMin(self):
        """
        :rtype: int
        """
        if not self.stack:
            return None
        # the second item in the tuple is the current min val
        return self.stack[-1][1]

if __name__ == "__main__":
    stack_obj = MinStack()
    print(stack_obj.getMin())
    stack_obj.push(4)
    stack_obj.push(6)
    stack_obj.push(3)
    stack_obj.push(5)
    print(stack_obj.top())
    print(stack_obj.getMin())
    print(stack_obj.stack)


import tensorflow as tf
from tensorflow.keras.layers import Conv2D, BatchNormalization, GlobalAveragePooling2D, Dense
import tensorflow_datasets as tfds
import time
import json
import numpy as np
import argparse

parser = argparse.ArgumentParser()
parser.add_argument("--seed", "-s", type=int, default=42, help="Random seed")
args = parser.parse_args()
tf.random.set_seed(args.seed)

# ResNet Layers
class IdentityResidual(tf.keras.layers.Layer):
    def __init__(self, out_channels, stride):
        super().__init__()
        self.out_channels = out_channels
        self.stride = stride

    def build(self, input_shape):
        b, h, w, in_channels = input_shape
        self.in_channels = in_channels
        self.h = h // self.stride
        self.w = w // self.stride
        self.b = b
        self.c = self.out_channels - self.in_channels

    def call(self, input_tensor):
        # Downsample spatially
        x = input_tensor[:, ::self.stride, ::self.stride, :]
        # Create padding tensor for extra channels
        if self.out_channels != self.in_channels:
            pad = tf.zeros((self.b, self.h, self.w, self.c))
            # Append padding to the downsampled identity
            x = tf.concat((x, pad), axis=-1)
        return x

class ResNetV2Layer(tf.keras.Model):
    def __init__(self, channels, stride=1):
        super().__init__()
        conv_kwargs = {
            "padding": "same",
            "use_bias": False
        }
        self.stride = stride
        self.channels = channels
        self.relu = tf.nn.relu
        self.residual = IdentityResidual(channels, stride)
        self.conv1 = Conv2D(filters=channels, kernel_size=3, strides=self.stride, **conv_kwargs)
        self.bn1 = BatchNormalization()
        self.conv2 = Conv2D(filters=channels, kernel_size=3, **conv_kwargs)
        self.bn2 = BatchNormalization()

    def call(self, input_tensor, training=False):
        residual = self.residual(input_tensor)
        x = self.bn1(input_tensor, training=training)
        x = self.relu(x)
        x = self.conv1(x)
        x = self.bn2(x, training=training)
        x = self.relu(x)
        x = self.conv2(x)
        return x + residual

class TimingCallback(tf.keras.callbacks.Callback):
    def __init__(self, logs={}):
        self.logs = []

        self.start_list = []
        self.end_list = []
        self.difference_list = []

        self.start_train_time = 0.0
        self.end_train_time = 0.0

        self.start_batch_inference_list = []
        self.end_batch_inference_list = []
        self.difference_batch_inference_list = []

        self.final_training_loss = 0.0
        self.final_evaluation_accuracy = 0.0

    def on_train_begin(self, logs={}):
        self.start_train_time = time.time()

    def on_train_end(self, logs={}):
        self.end_train_time = time.time()

    def on_test_batch_begin(self, batch, logs={}):
        self.starttime = time.time()
        self.start_batch_inference_list.append(self.starttime)

    def on_test_batch_end(self, batch, logs={}):
        self.endtime = time.time()
        self.end_batch_inference_list.append(self.endtime)
        self.difference_batch_inference_list.append(
            self.end_batch_inference_list[-1] - self.start_batch_inference_list[-1])

    def on_epoch_begin(self, epoch, logs={}):
        self.starttime = time.time()
        self.start_list.append(self.starttime)

    def on_epoch_end(self, epoch, logs={}):
        self.final_training_loss += logs["loss"]
        self.final_evaluation_accuracy += logs["sparse_categorical_accuracy"]
        self.endtime = time.time()
        self.end_list.append(self.endtime)
        self.difference_list.append(self.end_list[-1] - self.start_list[-1])

def get_datasets(batch_size):
    """
    Creates train, validation, and test datasets.
    Applies data normalization to all datasets and augmentation to training only.
    """
    train_ds, val_ds, test_ds = tfds.load(
        "cifar10",
        split=["train[:90%]", "train[90%:]", "test"],
        as_supervised=True,
        shuffle_files=True,
    )

    mean = [0.485, 0.456, 0.406]
    std = [0.229, 0.224, 0.225]
    var = [x ** 2 for x in std]

    augment_pipeline = tf.keras.Sequential([
        tf.keras.layers.Rescaling(scale=1./255),
        tf.keras.layers.Normalization(mean=mean, variance=var),
        tf.keras.layers.ZeroPadding2D(padding=(4, 4)),
        tf.keras.layers.RandomFlip(mode="horizontal"),
        tf.keras.layers.RandomCrop(height=32, width=32)
    ])

    evaluate_pipeline = tf.keras.Sequential([
        tf.keras.layers.Rescaling(scale=1./255),
        tf.keras.layers.Normalization(mean=mean, variance=var),
    ])

    augment_pipeline.compile()
    evaluate_pipeline.compile()

    AUTOTUNE = tf.data.AUTOTUNE

    train_ds = train_ds.cache().shuffle(1000).batch(batch_size, drop_remainder=True)
    train_ds = train_ds.map(lambda x, y: (augment_pipeline(x, training=True), y)).prefetch(AUTOTUNE)

    val_ds = val_ds.cache().shuffle(1000).batch(batch_size, drop_remainder=True)
    val_ds = val_ds.map(lambda x, y: (evaluate_pipeline(x, training=False), y)).prefetch(AUTOTUNE)

    test_ds = test_ds.cache().shuffle(1000).batch(batch_size, drop_remainder=True)
    test_ds = test_ds.map(lambda x, y: (evaluate_pipeline(x, training=False), y)).prefetch(AUTOTUNE)

    return train_ds, val_ds, test_ds

def get_compiled_model(batch_size):
    ResNetV2Model = tf.keras.Sequential([
        Conv2D(filters=16, kernel_size=3, padding="same", use_bias=False, data_format="channels_last"),
        ResNetV2Layer(16),
        ResNetV2Layer(16),
        ResNetV2Layer(16),
        ResNetV2Layer(32, stride=2),
        ResNetV2Layer(32),
        ResNetV2Layer(32),
        ResNetV2Layer(64, stride=2),
        ResNetV2Layer(64),
        ResNetV2Layer(64),
        GlobalAveragePooling2D(),
        Dense(10)
    ])

    # Model needs dummy input to build
    inputs = tf.random.normal((batch_size, 32, 32, 3))
    z = ResNetV2Model(inputs)

    ResNetV2Model.compile(
        optimizer=tf.keras.optimizers.Adam(learning_rate=1e-3),
        loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
        metrics=[
            tf.keras.metrics.SparseCategoricalAccuracy(),
            tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
        ]
    )
    return ResNetV2Model

def main():
    BATCH_SIZE = 128
    train_ds, val_ds, test_ds = get_datasets(BATCH_SIZE)

    model = get_compiled_model(BATCH_SIZE)

    cb = TimingCallback()

    # Fitting the model
    history = model.fit(train_ds, epochs=10, validation_data=val_ds, callbacks=[cb])

    # Evaluate the model
    test_metrics = model.evaluate(test_ds)

    total_training_time = cb.end_train_time - cb.start_train_time
    average_epoch_training_time = np.mean(cb.difference_list)
    average_batch_inference_time = np.mean(cb.difference_batch_inference_list) * 1000
    final_eval_accuracy = history.history["val_sparse_categorical_accuracy"][-1]
    final_train_loss = history.history["loss"][-1]

    metrics = {
        "model_name": "ResNetV2-20",
        "framework_name": "TensorFlow",
        "dataset": "MNIST Digits",
        "task": "classification",
        "final_training_loss": final_train_loss,
        "final_evaluation_accuracy": final_eval_accuracy,
        "final_test_accuracy": test_metrics[1],  # Metrics is list of [loss, acc, ce]
        "total_training_time": total_training_time,  # in seconds
        "average_epoch_training_time": average_epoch_training_time,  # in seconds
        "average_batch_inference_time": average_batch_inference_time  # in milliseconds
    }

    for key, value in metrics.items():
        print(f'{key} : {value}')

    date_str = time.strftime("%Y-%m-%d-%H%M%S")
    with open(f"./output/m2-tensorflow-cnn-{date_str}.json", "w") as outfile:
        json.dump(metrics, outfile)

if __name__ == "__main__":
    main()


import sys
sys.stdin = open("jip.txt", "r")

T = int(input())
for tc in range(1, T+1):
    N ,M = map(int,input().split())
    arr =[list(map(int,input().split())) for _ in range(N)]
    dr = [-1,1,0,0]
    dc = [0,0,-1,1]
    lst = []
    for n in range(N):
        for m in range(M):
            sum = 0
            sum += arr[n][m]
            for k in range(4):
                new_r =n+dr[k]
                new_c = m+dc[k]
                if 0 <= new_r <N and  0<= new_c <M:
                    sum += arr[new_r][new_c]
        lst.append(sum)
    mmax = 0
    for j in range(len(lst)):
        if mmax < lst[j]:
            mmax = lst[j]
    print(f'#{tc} {mmax}')


#! /usr/bin/env python3

import matplotlib.pyplot as plt
import json
from sys import argv

def get_execution_time(file):
    init_time = -1

    relevant_info = []

    def d_time(curr):
        return curr - init_time

    with open(file, 'r') as f:
        for line in f:
            data = json.loads(line)

            if init_time < 0:
                init_time = data['time']

            if d_time(data['time']) / (10 ** 9) > 575:

                if (data['info']['cpu_percent']) > 20:
                    relevant_info.append({
                        'time': d_time(data['time']),
                        'cpu': data['info']['cpu_percent']
                    })

    return relevant_info

def main2():
    a = get_execution_time("/home/thomas/Documents/BGP_perf/1decplug/out.res.perf04")

    time = [i['time'] / (10 ** 9) for i in a]
    cpu = [i['cpu'] for i in a]

    plt.plot(time, cpu, label="Recvd and processed prefixes (ubpf disabled)")
    plt.grid()
    plt.show()

def main():
    exec_time = []

    for i in range(1, len(argv)):
        a = get_execution_time(argv[i])
        file = argv[i].split('/')

        time = [i['time'] / (10 ** 9) for i in a]
        cpu = [i['cpu'] for i in a]

        exec_time.append({
            'file': file[len(file) - 1],
            'time': a[len(a) - 1]['time'] - a[0]['time'],
        })

        # plt.plot(time, cpu, label="Recvd and processed prefixes (ubpf disabled)")
        # plt.grid()
        # plt.show()

    for a in exec_time:
        print(a)

if __name__ == '__main__':
    # main2()
    main()


import socket
class Server():

    def __init__(self,label):

        self.label=label
        self.act()

    def act(self):
        s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)

        host = socket.gethostname()
        port = 60000

        s.bind((host,port))

        while True:
            print ("Waiting for client...")
            self.label.setText("Waiting for client...")
            data,addr = s.recvfrom(1024)
            print ("Received Messages:",data," from",addr)
            self.label.setText("Received Messages:",data," from",addr)