# pylint: disable=W0614,W0401,W0611,W0622,C0103,E0401,E0402
import logging
import sys
from itertools import repeat, starmap
from pathlib import Path

import pandas as pd

if __name__ == "__main__":
    from config import config
    from deconvolution_models.fit_models import Fitter, InitializeModels
    from export.exporter import Exporter
    from indexer.indexer import MakeRamanFilesIndex
    from processing.spectrum_constructor import (
        SpectrumDataCollection,
        SpectrumDataLoader,
    )

    # from processing.cleaner import SpectrumCleaner
    from processing.spectrum_template import SpectrumTemplate

else:
    from .. import __package_name__
    from ..cli.cli import RUN_MODES
    from ..config import config
    from ..config.filepaths import get_directory_paths_for_run_mode
    from ..deconvolution_models.fit_models import Fitter, InitializeModels
    from ..export.exporter import Exporter
    from ..indexer.indexer import MakeRamanFilesIndex
    from ..processing.spectrum_constructor import (
        SpectrumDataCollection,
        SpectrumDataLoader,
    )

    # from processing.cleaner import SpectrumCleaner
    from ..processing.spectrum_template import SpectrumTemplate

logger = logging.getLogger(__package_name__)
# from raman_fitting.indexer.indexer import OrganizeRamanFiles
# from raman_fitting.processing.spectrum_constructor import SpectrumDataLoader, SpectrumDataCollection

class prdError(Exception):
    """Base error raised by pyramdeconv."""

class MainDelegatorError(prdError):
    """Raised when a method in the main delegator fails."""

class MainDelegator:
    # TODO Add flexible input handling for the cli, such a path to dir, or list of files
    #  or create index when no kwargs are given.
    """
    Main delegator for the processing of files containing Raman spectra.

    Input parameters is DataFrame of index
    Creates plots and files in the config RESULTS directory.
    """

    # DEFAULT_RUN_MODE = {'run_mode' : 'normal'}
    # _kwargs = {}

    def __init__(self, run_mode="normal", **kwargs):

        # print(f'{self} kwargs:', kwargs)
        self.kwargs = kwargs
        self._cqnm = __class__.__qualname__

        self.run_mode = run_mode
        if run_mode not in RUN_MODES:
            logger.warning(
                f"{self}\n\twarning run_mode choice {run_mode} not in\n\t{RUN_MODES}"
            )

        self.dest_dirs = get_directory_paths_for_run_mode(run_mode=run_mode)
        self.RESULTS_DIR = self.dest_dirs["RESULTS_DIR"]
        self.DATASET_DIR = self.dest_dirs["DATASET_DIR"]
        self.INDEX_FILE = self.dest_dirs["INDEX_FILE"]

        self.spectrum = SpectrumTemplate()
        # self.index = RamanIndex

        self.run_delegator(**self.kwargs)

    def index_delegator(self, **kwargs):

        RF_index = MakeRamanFilesIndex(
            run_mode=self.run_mode,
            RESULTS_DIR=self.RESULTS_DIR,
            DATASET_DIR=self.DATASET_DIR,
            INDEX_FILE=self.INDEX_FILE,
            **kwargs,
        )
        logger.info(f"{self} index prepared with len {len(RF_index)}")
        return RF_index

    def run_delegator(self, **kwargs):
        # TODO remove self.set_models() removed InitModels
        self._failed_samples = []
        self.export_collect = []

        # assert type(self.index) == type(pd.DataFrame())
        if self.run_mode in ("normal", "DEBUG", "make_index", "make_examples"):
            RF_indexer = self.index_delegator(**kwargs)
            self.index = RF_indexer.index_selection

            if self.index.empty:
                logger.warning(f"{self._cqnm} index selection empty")

            if self.run_mode == "make_index":
                logger.warning(
                    f"{self._cqnm} Debug run mode {self}. Index loaded {RF_indexer}"
                )
                sys.exit(0)

            models = self.initialize_models()
            self.kwargs.update({"models": models})
            logger.info(
                f"\n{self._cqnm} models initialized for run mode ({self.run_mode}):\n\n{repr(models)}"
            )

            if self.run_mode in ("normal", "make_examples"):
                if not self.index.empty:
                    logger.info(f"{self._cqnm}. starting run generator.")
                    self._run_gen(**self.kwargs)
                else:
                    pass
                # info raman loop finished because index is empty
            elif self.run_mode == "DEBUG":
                logger.info(f"Debug run mode {self}. Models initialized {models}")

                try:
                    # self._run_gen() # TODO add extra test runs in tests dir
                    pass
                except Exception as e:
                    raise MainDelegatorError(
                        "The debug run failed. " f" on {self} because {e}"
                    )
                # raise('Error in DEBUG run: ', e)
            else:
                logger.warning(f"Debug run mode {self.run_mode} not recognized")
            # TODO get testing from index and run
        else:
            logger.warning(f'Debug run mode "{self.run_mode}" not recognized not in ')
            # warning run mode not recognized

    def initialize_models(self):
        try:
            return InitializeModels()
        except Exception as e:
            raise MainDelegatorError(
                "The initialization of models failed. " f" on {self} with excp: {e}"
            )
            return None

    def sample_group_gen(self):
        """Generator for Sample Groups, yields the name of group and group of the index SampleGroup"""
        for grpnm, sGrp_grp in self.index.groupby(
            self.spectrum.grp_names.sGrp_cols[0]
        ):  # Loop over SampleGroups
            yield grpnm, sGrp_grp

    def _sID_gen(self, grpnm, sGrp_grp):
        """Generator for SampleIDs, yields the name of group, name of SampleID and group of the index of the SampleID"""
        for nm, sID_grp in sGrp_grp.groupby(
            list(self.spectrum.grp_names.sGrp_cols[1:])
        ):  # Loop over SampleIDs within SampleGroup
            yield (grpnm, nm, sID_grp)

    def _run_gen(self, **kwargs):
        # sort of coordinator coroutine, can implement still deque
        _mygen = self._generator(**kwargs)
        logger.info(f"{self._cqnm} _run_gen starting: {kwargs}")
        _count = 0
        while True:

            try:
                next(_mygen)
                _count += 1
            except StopIteration:
                logger.info(
                    f"{self._cqnm} _run_gen StopIteration after {_count } steps"
                )
                # print('StopIteration for mygen')
                break
            finally:
                Exporter(self.export_collect)  # clean up and export

    def _generator(self, *args, **kwargs):
        _sgrpgen = self.sample_group_gen()
        for grpnm, sGrp_grp in _sgrpgen:
            _sID_gen = self._sID_gen(grpnm, sGrp_grp)
            logger.info(f"{self._cqnm} _generator starting group: {grpnm}")
            try:

                yield from self.simple_process_sample_wrapper(_sID_gen, **kwargs)
                # starmap(process_sample_wrapper, args_for_starmap )
                # args_for_starmap = zip(repeat(self.process_sample), _sID_gen, repeat(kwargs))

            except GeneratorExit:
                logger.warning(f"{self._cqnm} _generator closed.")
                return None
            except Exception as e:
                logger.warning(f"{self._cqnm} _generator exception: {e}")

    def coordinator(self):
        pass

    def simple_process_sample_wrapper(self, _gen, **kwargs):

        for sID_args in _gen:
            logger.info(
                f"{self._cqnm} starting simple process_sample_wrapper args:\n\t - {_gen}\n\t - {kwargs.keys()}"
            )
            exp_sample = None
            try:
                logger.debug(
                    f"{self._cqnm} simple process_sample_wrapper trying:\n\t - {sID_args}"
                )
                exp_sample = self.process_sample(*sID_args, **kwargs)
                self.export_collect.append(exp_sample)
            except StopIteration:
                logger.info(
                    f"{self._cqnm} _gen StopIteration for simple process wrapper "
                )
            except Exception as e:
                logger.warning(
                    f"{self._cqnm} simple process_sample_wrapper exception on call process sample: {e}"
                )
                self._failed_samples.append((e, sID_args, kwargs))

    def _process_sample_wrapper(self, fn, *args, **kwargs):
        logger.warning(
            f"{self._cqnm} process_sample_wrapper args:\n\t - {fn}\n\t - {args}\n\t - {kwargs.keys()}"
        )
        exp_sample = None
        try:
            exp_sample = fn(self, *args, **kwargs)
            self.export_collect.append(exp_sample)
        except Exception as e:
            logger.warning(
                f"{self._cqnm} process_sample_wrapper exception on call {fn}: {e}"
            )
            self._failed_samples.append((e, args, kwargs))

    def test_positions(
        self, sGrp_grp, nm, grp_cols=["FileStem", "SamplePos", "FilePath"]
    ):
        if sGrp_grp.FileStem.nunique() != sGrp_grp.SamplePos.nunique():
            logger.warning(
                f"{sGrp_grp[grp_cols]} Unique files and positions not matching for {nm}"
            )
        return sGrp_grp.groupby(grp_cols), grp_cols

    def process_sample(self, *args, **kwargs):
        """
        Loops over individual Sample positions (files) from a SampleID and performs the
        fitting, plotting and exporting.
        """
        logger.info(
            f"{self._cqnm} process_sample called:\n\t - {args}\n\t - {kwargs.keys()}"
        )
        # self = args[0]
        # args = args[]
        grpnm, nm, sID_grp = args
        models = kwargs.get("models", None)

        sGr, (sID, sDate) = grpnm, nm
        sGr_out = dict(zip(self.spectrum.grp_names.sGrp_cols, (grpnm,) + nm))
        export_info_out = add_make_sample_group_destdirs(sID_grp)
        sample_pos_grp, sPos_cols = self.test_positions(
            sID_grp, nm, list(self.spectrum.grp_names.sPos_cols)
        )

        sample_spectra = []
        for meannm, meangrp in sample_pos_grp:
            logger.info(f"{self._cqnm} process sample mean loop file: {meannm}.")
            sPos_out = dict(zip(self.spectrum.grp_names.sPos_cols, meannm))
            _spectrum_position_info_kwargs = {**sGr_out, **export_info_out, **sPos_out}
            spectrum_data = SpectrumDataLoader(
                file=meannm[-1], run_kwargs=_spectrum_position_info_kwargs, ovv=meangrp
            )
            # spectrum_data.plot_raw()
            sample_spectra.append(spectrum_data)
        spectra_collection = SpectrumDataCollection(sample_spectra)
        ft = Fitter(spectra_collection, RamanModels=models)
        rex = Exporter(ft)
        return rex

    def __repr__(self):
        return f'Maindelegator: run_mode = {self.run_mode}, {", ".join([f"{k} = {str(val)}" for k,val in self.kwargs.items()])}'

def add_make_sample_group_destdirs(sample_grp: pd.DataFrame):

    dest_grp_dir = Path(
        sample_grp.DestDir.unique()[0]
    )  # takes one destination directory from Sample Groups
    dest_fit_plots = dest_grp_dir.joinpath("Fitting_Plots")
    dest_fit_comps = dest_grp_dir.joinpath("Fitting_Components")
    dest_fit_comps.mkdir(parents=True, exist_ok=True)

    dest_raw_data_dir = dest_grp_dir.joinpath("Raw_Data")
    dest_raw_data_dir.mkdir(parents=True, exist_ok=True)

    export_info = {
        "DestGrpDir": dest_grp_dir,
        "DestFittingPlots": dest_fit_plots,
        "DestFittingComps": dest_fit_comps,
        "DestRaw": dest_raw_data_dir,
    }
    return export_info

def process_sample_wrapper(fn, *args, **kwargs):
    def wrapper(*args, **kwargs):
        logger.debug(
            f"process_sample_wrapper args:\n\t - {fn}\n\t - {args}\n\t - {kwargs.keys()}"
        )
        exp_sample = None
        try:
            exp_sample = fn(*args, **kwargs)
            # self.export_collect.append(exp_sample)
        except Exception as e:
            logger.error(
                f"process_sample_wrapper process_sample_wrapper exception on call {fn}: {e}"
            )
            # self._failed_samples.append((e, args, kwargs))
            exp_sample = (e, args, kwargs)
        finally:
            return exp_sample

def make_examples():
    _main_run = MainDelegator(run_mode="make_examples")


# Prompt: https://leetcode.com/problems/lucky-numbers-in-a-matrix/
# Runtime: 104 ms, faster than 73.71% of Python online submissions for Lucky Numbers in a Matrix.
# Memory Usage: 13.8 MB, less than 27.89% of Python online submissions for Lucky Numbers in a Matrix.

class Solution(object):
    def luckyNumbers (self, matrix):
        """
        :type matrix: List[List[int]]
        :rtype: List[int]
        """
        minRows = []
        luckyNumbers = []
        for row in matrix:
            minRows.append(min(row))

        maxCols = []
        for i in range(len(matrix[0])):
            mx = matrix[0][i]
            for j in range(len(matrix)):
                if matrix[j][i] > mx:
                    mx = matrix[j][i]
            maxCols.append(mx)

        for i in range(len(matrix)):
            for j in range(len(matrix[0])):
                if minRows[i] == matrix[i][j] and maxCols[j] == matrix[i][j]:
                    luckyNumbers.append(matrix[i][j])

        return luckyNumbers

0. Easy/1380. Lucky Numbers in a Matrix/lucky_numbers_matrix.py


"""
Copyright 2017 Neural Networks and Deep Learning lab, MIPT

Licensed inder the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

import spacy
import re

from deeppavlov.core.models.inferable import Inferable
from deeppavlov.core.common.registry import register

@register('spacy_tokenizer')
class SpacyTokenizer(Inferable):

    def __init__(self, save_path=None, **kwargs):
        self.NLP = spacy.load('en')
        super().__init__(save_path=save_path, **kwargs)

    def _tokenize(self, text, **kwargs):
        """Tokenize with spacy, placing service words as individual tokens."""
        return [t.text for t in self.NLP.tokenizer(text)]

    def _detokenize(self, tokens):
        """
        Detokenizing a text undoes the tokenizing operation, restoring
        punctuation and spaces to the places that people expect them to be.
        Ideally, `detokenize(tokenize(text))` should be identical to `text`,
        except for line breaks.
        """
        text = ' '.join(tokens)
        step0 = text.replace('. . .',  '...')
        step1 = step0.replace("`` ", '"').replace(" ''", '"')
        step2 = step1.replace(" ( ", " (").replace(" ) ", ") ")
        step3 = re.sub(r' ([.,:;?!%]+)([ \'"`])', r"\1\2", step2)
        step4 = re.sub(r' ([.,:;?!%]+)$', r"\1", step3)
        step5 = step4.replace(" '", "'").replace(" n't", "n't")\
            .replace(" nt", "nt").replace("can not", "cannot")
        step6 = step5.replace(" ` ", " '")
        return step6.strip()

    def infer(self, x):
        if isinstance(x, str):
            return self._tokenize(x)
        if isinstance(x, list):
            return self._detokenize(x)
        raise TypeError("SpacyTokenize.infer() not implemented for `{}`"\
                        .format(type(x)))


# bot.py
import os

import discord
from discord.ext import tasks, commands

from dotenv import load_dotenv

load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
GUILD_ID = int(os.getenv('DISCORD_GUILD'))

class SmashBotSpain(commands.Bot):
    VERSION =  "v1.1"

    def __init__(self, command_prefix, intents):
        super().__init__(command_prefix=command_prefix, intents=intents)
        self.help_command = None

    async def on_ready(self):
        self.guild = self.get_guild(GUILD_ID)

        print(
            f'{client.user} is connected to the following guild:\n'
            f'{self.guild.name}(id: {self.guild.id})'
        )

        matchmaking = self.get_cog('Matchmaking')
        flairing = self.get_cog('Flairing')
        await matchmaking.setup_matchmaking(guild=self.guild)
        await flairing.setup_flairing(guild=self.guild)

intents = discord.Intents.default()  # All but the two privileged ones
intents.members = True

client = SmashBotSpain(command_prefix=["."], intents=intents)
client.load_extension("extensions.member_nickname")
client.load_extension("cogs.Matchmaking")
client.load_extension("cogs.HelpCommands")
client.load_extension("cogs.Flairing")

client.run(TOKEN)


#===============================================================================
# Copyright 2012 Jake Ross
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#===============================================================================

#============= enthought library imports =======================
from traits.api import Float, Enum
from traitsui.api import View, Item, VGroup, HGroup, spring, \
    Spring, Label, UItem
from apptools.preferences.ui.preferences_page import PreferencesPage
from src.constants import PLUSMINUS

#============= standard library imports ========================
#============= local library imports  ==========================

class ConstantsPreferencesPage(PreferencesPage):
    name = 'Constants'
    preferences_path = 'pychron.experiment.constants'
    Ar40_Ar36_atm = Float(295.5)
    Ar40_Ar36_atm_error = Float(0)
    Ar40_Ar38_atm = Float(1575)
    Ar40_Ar38_atm_error = Float(2)
    lambda_e = Float(5.81e-11)
    lambda_e_error = Float(0)
    lambda_b = Float(4.962e-10)
    lambda_b_error = Float(0)
    lambda_Cl36 = Float(6.308e-9)
    lambda_Cl36_error = Float(0)
    lambda_Ar37 = Float(0.01975)
    lambda_Ar37_error = Float(0)
    lambda_Ar39 = Float(7.068e-6)
    lambda_Ar39_error = Float(0)
    Ar37_Ar39_mode = Enum('Normal', 'Fixed')
    Ar37_Ar39 = Float(0.01)
    Ar37_Ar39_error = Float(0.01)

    #===========================================================================
    # spectrometer
    #===========================================================================
    abundant_sensitivity = Float(0)
    sensitivity = Float(0)
    ic_factor = Float(1.0)
    ic_factor_error = Float(0.0)

    def traits_view(self):
        ratios = VGroup(
                        HGroup(Spring(springy=False, width=125),
                               Label('Value'), Spring(springy=False, width=55),
                               Label(u'{}1s'.format(PLUSMINUS))),
                        HGroup(Item('Ar40_Ar36_atm', label='(40Ar/36Ar)atm'),
                               Item('Ar40_Ar36_atm_error', show_label=False)),
                        HGroup(Item('Ar40_Ar38_atm', label='(40Ar/38Ar)atm'),
                               Item('Ar40_Ar38_atm_error', show_label=False)),

                        HGroup(
                               Item('Ar37_Ar39_mode', label='(37Ar/39Ar)K'),
                               Item('Ar37_Ar39', show_label=False),
                               Item('Ar37_Ar39_error', show_label=False)),

                        show_border=True,
                        label='Ratios'
                        )
        decay = VGroup(
                        HGroup(Spring(springy=False, width=125),
                               Label('Value'), Spring(springy=False, width=55),
                               Label(u'{}1s'.format(PLUSMINUS))),

                        HGroup(
                                VGroup(
                                       spring,
                                       HGroup(spring, Label('Ar40K epsilon/yr')),
                                       spring,
                                       HGroup(spring, Label('Ar40K beta/yr')),
                                       spring,
                                       HGroup(spring, Label('Cl36/d')),
                                       spring,
                                       HGroup(spring, Label('Ar37/d')),
                                       spring,
                                       HGroup(spring, Label('Ar39/d')),
                                       ),
                                VGroup(
                                       HGroup(Item('lambda_e',),
                                              Item('lambda_e_error'), show_labels=False),
                                       HGroup(Item('lambda_b'),
                                              Item('lambda_b_error'), show_labels=False),
                                       HGroup(Item('lambda_Cl36'),
                                              Item('lambda_Cl36_error'), show_labels=False),
                                       HGroup(Item('lambda_Ar37'),
                                              Item('lambda_Ar37_error'), show_labels=False),
                                       HGroup(Item('lambda_Ar39'),
                                              Item('lambda_Ar39_error'), show_labels=False)
                                       )
                               ),
                        show_border=True,
                        label='Decay'
                        )
        spectrometer = VGroup(
                            Item('abundant_sensitivity'),
                            Item('sensitivity',
                                 tooltip='Nominal spectrometer sensitivity saved with analysis'
                                 ),
                            HGroup(Spring(springy=False, width=125),
                               Label('Value'), Spring(springy=False, width=55),
                               Label(u'{}1s'.format(PLUSMINUS))),
                            HGroup(
                                Item('ic_factor',
                                     tooltip='Default intercalibration factor (H1/CDD) saved with analysis'
                                     ),
                                UItem('ic_factor_error')
                                   ),
                            label='Spectrometer', show_border=True)
        v = View(ratios, decay, spectrometer)
        return v
#============= EOF =============================================


from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import pyautogui as py

class whatsappBot:
    def __init__(self,victim,msg,num):
        self.victim=victim
        self.msg=msg
        self.num=num
        self.bot=webdriver.Firefox()

    def login(self):
        bot=self.bot
        bot.get('https://web.whatsapp.com/')
        pause=input('Press Anything To Continue!')
        time.sleep(2)

        vic=bot.find_element_by_xpath('/html/body/div[1]/div/div/div[3]/div/div[1]/div/label/div/div[2]')
        print('hello')
        vic.send_keys(self.victim)
        vic.send_keys(Keys.ENTER)
        time.sleep(2)
        elem=bot.find_element_by_xpath('/html/body/div[1]/div/div/div[4]/div/footer/div[1]/div[2]/div/div[2]')

        elem.send_keys('Script Time.Chill.And Not Sorry. #B')
        elem.send_keys(Keys.ENTER)

        y=self.msg
        for i in range(0,self.num):
            elem.send_keys(y)
            elem.send_keys(Keys.ENTER)
            if(i%100==0):
                print(i)

print('Author-@b\n')

victim=input('Enter The Contact (Exact) : ')
msg=input('Enter Your Message : ')
num=int(input('Enter The Number Of Times You Want To Send: '))
print('Login Now And Enjoy.#B')

plan=whatsappBot(victim,msg,num)

plan.login()


#!/usr/bin/env python3

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

t = np.linspace(-1, 1, 1000)

x = 1-(t**2)
y = 2*t
z = 1+(t**2)

x_2d = x/z
y_2d = y/z
z_2d = np.ones_like(y_2d)

x_3d = x
y_3d = y
z_3d = z

x_plot = np.concatenate([x_2d, x_3d])
y_plot = np.concatenate([y_2d, y_3d])
z_plot = np.concatenate([z_2d, z_3d])

fig = plt.figure(); ax = fig.add_subplot(1, 1, 1, projection='3d');
ax.set_xlabel('x'); ax.set_ylabel('y'); ax.set_zlabel('z')
ax.scatter(x_plot, y_plot, z_plot, c='b', marker='o')

plt.show(block=True)


"""
  Capstone Project.  Code to run on a LAPTOP (NOT the robot).
  Displays the Graphical User Interface (GUI) and communicates with the robot.

  Authors:  Your professors (for the framework)
    and Joseph Law.
  Winter term, 2018-2019.
"""

import mqtt_remote_method_calls as com
import tkinter
from tkinter import ttk
import shared_gui

def main():
    """
    This code, which must run on a LAPTOP:
      1. Constructs a GUI for my part of the Capstone Project.
      2. Communicates via MQTT with the code that runs on the EV3 robot.
    """
    # -------------------------------------------------------------------------
    # Construct and connect the MQTT Client:
    # -------------------------------------------------------------------------
    mqtt_sender = com.MqttClient()
    mqtt_sender.connect_to_ev3()

    # -------------------------------------------------------------------------
    # The root TK object for the GUI:
    # -------------------------------------------------------------------------
    root = tkinter.Tk()
    root.title("CSSE 120 Capstone Project")

    # -------------------------------------------------------------------------
    # The main frame, upon which the other frames are placed.
    # -------------------------------------------------------------------------
    main_frame = ttk.Frame(root,padding=10,borderwidth=5,relief="groove")
    main_frame.grid()

    # -------------------------------------------------------------------------
    # Sub-frames for the shared GUI that the team developed:
    # -------------------------------------------------------------------------
    #teleop_frame, arm_frame, control_frame, drive_system_frame,sound_frame,proximity_frame,camera_frame = get_shared_frames(main_frame,mqtt_sender)

    # -------------------------------------------------------------------------
    # Frames that are particular to my individual contributions to the project.
    # -------------------------------------------------------------------------
    # TODO: Implement and call get_my_frames(...)
    find_package_frame,deliver_package_frame, steal_package = get_my_frames(main_frame,mqtt_sender)

    # -------------------------------------------------------------------------
    # Grid the frames.
    # -------------------------------------------------------------------------
    #grid_frames(teleop_frame, arm_frame, control_frame, drive_system_frame,sound_frame,proximity_frame,camera_frame)
    grid_my_frames(find_package_frame,deliver_package_frame,steal_package)

    # -------------------------------------------------------------------------
    # Using the keyboard to move the robot
    # -------------------------------------------------------------------------
    root.bind_all('<Key-a>', lambda event: go_left(mqtt_sender))
    root.bind_all('<Key-d>', lambda event: go_right(mqtt_sender))
    root.bind_all('<Key-s>', lambda event: go_backward(mqtt_sender))
    root.bind_all('<Key-w>', lambda event: go_forward(mqtt_sender))
    root.bind_all('<Key-c>', lambda event: calibrate_claw(mqtt_sender))
    root.bind_all('<Key-e>', lambda event: raise_claw(mqtt_sender))
    root.bind_all('<Key-q>', lambda event: lower_claw(mqtt_sender))
    root.bind_all('<Key-space>', lambda event: stop_robot(mqtt_sender))
    root.bind_all('<Key-h>', lambda event: say_hello(mqtt_sender))
    root.bind_all('<Key-g>', lambda event: say_goodbye(mqtt_sender))
    root.bind_all('<Key-l>', lambda event: laugh(mqtt_sender))

    # -------------------------------------------------------------------------
    # The event loop:
    # -------------------------------------------------------------------------
    root.mainloop()

#PHASE 1 AND 2
def get_shared_frames(main_frame, mqtt_sender):
    teleop = shared_gui.get_teleoperation_frame(main_frame,mqtt_sender)
    arm = shared_gui.get_arm_frame(main_frame,mqtt_sender)
    control = shared_gui.get_control_frame(main_frame,mqtt_sender)
    drive_system = shared_gui.get_drive_system(main_frame,mqtt_sender)
    sound_frame = shared_gui.get_sound_system(main_frame,mqtt_sender)
    myframe_proximity = get_myframe_proximity(main_frame,mqtt_sender)
    myframe_camera = get_myframe_camera(main_frame,mqtt_sender)
    return teleop, arm, control,drive_system,sound_frame,myframe_proximity, myframe_camera

def grid_frames(teleop_frame, arm_frame, control_frame,drive_system_frame,sound_frame,proximity_frame,camera_frame):
    teleop_frame.grid(row=0,column=0)
    arm_frame.grid(row=1,column=0)
    control_frame.grid(row=2,column=0)
    drive_system_frame.grid(row=0,column=1)
    sound_frame.grid(row=1,column=1)
    proximity_frame.grid(row=2,column=1)
    camera_frame.grid(row=3,column=0)

def get_myframe_proximity(window,mqtt_sender):
    # Construct the frame to return:
    frame = ttk.Frame(window, padding=10, borderwidth=5, relief="ridge")
    frame.grid()

    # Construct the widgets on the frame:
    frame_label_proximity = ttk.Label(frame,text="Proximity Sensor")
    initial_beep_rate_label = ttk.Label(frame,text="Initial Beep Rate")
    beep_rate_increase_label = ttk.Label(frame,text="Beep Rate Increase")
    initial_beep_rate_entry = ttk.Entry(frame,width=8)
    beep_rate_increase_entry = ttk.Entry(frame,width=8)
    drive_and_beep_button = ttk.Button(frame,text="Drive and Beep")

    # Grid the widgets:
    frame_label_proximity.grid(row=0,column=1)
    initial_beep_rate_label.grid(row=1,column=0)
    initial_beep_rate_entry.grid(row=2,column=0)
    beep_rate_increase_label.grid(row=1,column=2)
    beep_rate_increase_entry.grid(row=2,column=2)
    drive_and_beep_button.grid(row=2,column=1)

    # Set the Button callbacks:
    drive_and_beep_button["command"] = lambda: handle_proximity(initial_beep_rate_entry, beep_rate_increase_entry, mqtt_sender)

    return frame
def get_myframe_camera(window,mqtt_sender):
    # Construct the frame to return:
    frame = ttk.Frame(window, padding=10, borderwidth=5, relief="ridge")
    frame.grid()

    # Construct the widgets on the frame:
    frame_label = ttk.Label(frame,text="Finding Object Through Spinning")
    direction_label = ttk.Label(frame,text="CW or CCW")
    direction_entry = ttk.Entry(frame,width=8)
    speed_label = ttk.Label(frame,text="Speed")
    speed_entry = ttk.Entry(frame,width=8)
    find_object_button = ttk.Button(frame,text="Find Object")

    # Grid the widgets:
    frame_label.grid(row=0,column=1)
    direction_label.grid(row=1,column=0)
    direction_entry.grid(row=2,column=0)
    speed_label.grid(row=1,column=2)
    speed_entry.grid(row=2,column=2)
    find_object_button.grid(row=2,column=1)
    find_object_button["command"] = lambda: handle_spin_to_find_object(direction_entry, speed_entry, mqtt_sender)

    return frame

# -----------------------------------------------------------------------------
# Handle Functions
# -----------------------------------------------------------------------------
def handle_proximity(initial_beep_rate_entry,beep_rate_increase_entry,mqtt_sender):
    print("Proximity Beeping and Booping (Initial rate - Increase): (",initial_beep_rate_entry,"-",beep_rate_increase_entry,")")
    mqtt_sender.send_message("m1_proximity",[initial_beep_rate_entry.get(),beep_rate_increase_entry.get()])

def handle_spin_to_find_object(direction_entry,speed_entry,mqtt_sender):
    print("Spinning to find object",direction_entry,speed_entry)
    mqtt_sender.send_message("m1_camera",[direction_entry.get(),speed_entry.get(),1,2])

# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# PHASE 3
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------

# -----------------------------------------------------------------------------
# Frames
# -----------------------------------------------------------------------------
def find_package_frame(window, mqtt_sender):
    # Construct the frame to return:
    frame = ttk.Frame(window, padding=10, borderwidth=5, relief="ridge")
    frame.grid()

    # Construct the widgets on the frame:
    frame_label = ttk.Label(frame, text="Package Finder")
    speed_label = ttk.Label(frame,text="Speed")
    speed_entry = ttk.Entry(frame,width=8)
    button = ttk.Button(frame,text="Find package!")

    frame_label.grid(row=0,column=0)
    speed_label.grid(row=1,column=0)
    speed_entry.grid(row=2,column=0)
    button.grid(row=3,column=0)

    #Lambda command
    button["command"] = lambda: handle_find_package(speed_entry, mqtt_sender)

    return frame

def package_delivery_frame(window,mqtt_sender):
    # Construct the frame to return:
    frame = ttk.Frame(window, padding=10, borderwidth=5, relief="ridge")
    frame.grid()

    # Construct the widgets on the frame:
    frame_label = ttk.Label(frame, text="Package Delivery")
    house_choice_label = ttk.Label(frame,text="Who's house would you like to deliver to?")
    greeting_label = ttk.Label(frame,text="Greeting:")
    greeting_entry = ttk.Entry(frame,width=18)
    goodbye_label = ttk.Label(frame,text="Goodbye:")
    goodbye_entry = ttk.Entry(frame,width=18)
    deliver_button = ttk.Button(frame,text="Deliver package!")

    #Setting up the Radio Frame
    radio_frame = ttk.Frame(frame, borderwidth=10, relief='groove')
    house1 = ttk.Radiobutton(radio_frame, text='John',
                             value=3)
    house2 = ttk.Radiobutton(radio_frame, text='Joe',
                             value=4)
    house3 = ttk.Radiobutton(radio_frame, text='Brad',
                             value=5)
    house4 = ttk.Radiobutton(radio_frame, text='Cooper',
                             value=2)
    house1.grid(row=0,column=0)
    house2.grid(row=0, column=1)
    house3.grid(row=0, column=2)
    house4.grid(row=0, column=3)
    radio_observer = tkinter.IntVar()
    for radio in [house1, house2, house3, house4]:
        radio['variable'] = radio_observer

    #Gridding everything
    frame_label.grid(row=0,column=1)
    house_choice_label.grid(row=1,column=1)
    radio_frame.grid(row=2,column=1)
    greeting_label.grid(row=1,column=0)
    greeting_entry.grid(row=2,column=0)
    goodbye_label.grid(row=1,column=2)
    goodbye_entry.grid(row=2,column=2)
    deliver_button.grid(row=3,column=1)

    #Lambda command
    deliver_button["command"] = lambda: handle_delivery(radio_observer,greeting_entry,goodbye_entry,mqtt_sender)
    return frame

def steal_package_frame(window,mqtt_sender):
    # Construct the frame to return:
    frame = ttk.Frame(window, padding=10, borderwidth=5, relief="ridge")
    frame.grid()

    # Construct the widgets on the frame:
    frame_label = ttk.Label(frame, text="Package Stealer")
    house_choice_label = ttk.Label(frame, text="Who's house would you steal from?")
    laugh_label = ttk.Label(frame,text="Pick your evil laugh:")
    steal_button = ttk.Button(frame,text="Steal package!")
    # Setting up the Radio Frame for house choice
    radio_frame = ttk.Frame(frame, borderwidth=10, relief='groove')
    house1 = ttk.Radiobutton(radio_frame, text='John',
                             value=3)
    house2 = ttk.Radiobutton(radio_frame, text='Joe',
                             value=4)
    house3 = ttk.Radiobutton(radio_frame, text='Brad',
                             value=5)
    house4 = ttk.Radiobutton(radio_frame, text='Cooper',
                             value=2)
    house1.grid(row=0, column=0)
    house2.grid(row=0, column=1)
    house3.grid(row=0, column=2)
    house4.grid(row=0, column=3)
    radio_observer = tkinter.IntVar()
    for radio in [house1, house2, house3, house4]:
        radio['variable'] = radio_observer
    # Setting up the Radio Frame for laugh choice
    radio_frame2 = ttk.Frame(frame, borderwidth=10, relief='groove')
    laugh1 = ttk.Radiobutton(radio_frame2, text='Haha',
                             value='hahahahahahahahaha')
    laugh2 = ttk.Radiobutton(radio_frame2, text='Bwaha',
                             value='bawahahahahahahaha')
    laugh3 = ttk.Radiobutton(radio_frame2, text='Bwahahe',
                             value='bwahahahehehehehehe')
    laugh4 = ttk.Radiobutton(radio_frame2, text="Bwahaheho",
                             value='bwahahahehehehohoho')
    laugh1.grid(row=0, column=0)
    laugh2.grid(row=0, column=1)
    laugh3.grid(row=0, column=2)
    laugh4.grid(row=0, column=3)
    radio_observer2 = tkinter.StringVar()
    for radio in [laugh1, laugh2, laugh3, laugh4]:
        radio['variable'] = radio_observer2

    # Gridding everything
    frame_label.grid(row=0, column=1)
    house_choice_label.grid(row=1,column=0)
    laugh_label.grid(row=1,column=2)
    radio_frame.grid(row=2,column=0)
    radio_frame2.grid(row=2,column=2)
    steal_button.grid(row=2,column=1)

    steal_button["command"] = lambda: handle_theft(radio_observer, radio_observer2, mqtt_sender)

    return frame

# -----------------------------------------------------------------------------
# Handle Functions
# -----------------------------------------------------------------------------
def handle_find_package(speed_entry,mqtt_sender):
    print("Finding package",speed_entry.get())
    mqtt_sender.send_message("m1_find_package",[speed_entry.get()])

def handle_delivery(color,greeting_entry,goodbye_entry,mqtt_sender):
    print("Delivering package!",color.get(),greeting_entry.get,goodbye_entry.get())
    mqtt_sender.send_message("m1_full_delivery",[color.get(),greeting_entry.get(),goodbye_entry.get()])

def handle_theft(color,laugh_entry,mqtt_sender):
    print("Stealing package!",color.get(),laugh_entry.get())
    mqtt_sender.send_message("m1_theft",[color.get(),laugh_entry.get()])

def go_forward(mqtt_sender):
    print("forward")
    mqtt_sender.send_message("forward",[50,50])

def go_backward(mqtt_sender):
    print("backward")
    mqtt_sender.send_message("forward",[-50,-50])

def go_right(mqtt_sender):
    print("right")
    mqtt_sender.send_message("forward",[50,1])
def go_left(mqtt_sender):
    print("left")
    mqtt_sender.send_message("forward",[1,50])
def raise_claw(mqtt_sender):
    print("Raise claw!")
    mqtt_sender.send_message("raise_arm")
def lower_claw(mqtt_sender):
    print("Lower Arm")
    mqtt_sender.send_message("lower_arm")
def stop_robot(mqtt_sender):
    print("Stop")
    mqtt_sender.send_message("stop")
def calibrate_claw(mqtt_sender):
    print("Calibrate Arm")
    mqtt_sender.send_message("calibrate_arm")
def say_hello(mqtt_sender):
    print("Say Hello")
    mqtt_sender.send_message("sound_speak",["Hello"])
def say_goodbye(mqtt_sender):
    print("Say goodbye")
    mqtt_sender.send_message("sound_speak",["Goodbye"])
def laugh(mqtt_sender):
    print("Laugh")
    mqtt_sender.send_message("sound_speak",["Hahahahahahahahaha"])
# -----------------------------------------------------------------------------
# Get and Grid My Frames
# -----------------------------------------------------------------------------
def get_my_frames(main_frame, mqtt_sender):
    find_package = find_package_frame(main_frame,mqtt_sender)
    deliver_package = package_delivery_frame(main_frame,mqtt_sender)
    steal_package = steal_package_frame(main_frame,mqtt_sender)

    return find_package, deliver_package,steal_package

def grid_my_frames(find_package_frame,deliver_package_frame,steal_package_frame):
    find_package_frame.grid(row=0,column=0)
    deliver_package_frame.grid(row=1,column=0)
    steal_package_frame.grid(row=2,column=0)

# -----------------------------------------------------------------------------
# Calls  main  to start the ball rolling.
# -----------------------------------------------------------------------------
main()


import msgpack

import imageio

import os
import sys
import re
import argparse

import numpy as np
import cv2
import time

import json

import multiprocessing as mp
import random

import logging

import tensorflow as tf

from msgpack_dataset_creator import MsgPackWriter

from google.protobuf.json_format import MessageToJson

def parse_args():
    parser = argparse.ArgumentParser(description='')

    parser.add_argument('-v', '--verbose', action='store_true', help='verbose output')
    parser.add_argument('-p', '--path', help='verbose output')
    parser.add_argument('-o', '--output', help='path to dir of images')
    parser.add_argument('-m', '--mapping')
    parser.add_argument('--process', default=8, type=int)

    parser.add_argument('-s', '--shuffle', action='store_true', default=True, help='verbose output')

    args = parser.parse_args()
    return args

def read_records(args):
    record = args['record']
    mapping = args['mapping']
    data_dict_list = []
    for example in tf.data.TFRecordDataset(record):
        data_dict = {}
        for key, value in tf.train.Example.FromString(example.numpy()).features.feature.items():
            values = []
            for v in value.float_list.value:
                values.append(v)
            for v in value.bytes_list.value:
                values.append(v)
            for v in value.int64_list.value:
                values.append(v)

            if len(values) == 1:
                values = values[0]
            if key in mapping:
                data_dict[mapping[key]] = values
            else:
                data_dict[key] = values
        data_dict_list.append(data_dict)
    return data_dict_list

def main():
    args = parse_args()

    if args.mapping is not None:
        mapping = json.loads(args.mapping)
    else:
        mapping = {}
    #record_re = re.compile()
    records = [os.path.join(args.path, x) for x in os.listdir(args.path)]

    with mp.Pool(args.process) as pool:
        with MsgPackWriter(args.output) as f:
            start = time.time()
            records = [{'record': x, 'mapping': mapping} for x in records]
            count = 0
            for i, x in enumerate(pool.imap(read_records, records)):
                for example in x:
                    if example is None:
                        continue
                    #print(i)
                    f.write(example)

                    # f.write(json.dumps({'key': x['path'], 'path': x['path'], 'index': i}) + '\n')
                    if count % 1000 == 0:
                        # txn.commit()
                        end = time.time()
                        print(f'{count}: {1000/(end - start):.2f} image/s')
                        start = end
                    count += 1

    return 0

if __name__ == '__main__':
    sys.exit(main())


from tqdm import trange
import tensorflow as tf
import numpy as np
import os
from glob import glob
from random import shuffle
from data_loader import get_image
import data_saver

from encoder_reduce import Encoder
from generator import Generator
from utils import logger

def sample_z(batch_size, latent_size):
    return np.float32(np.random.normal(size=[batch_size, latent_size]))

class VAE(object):
    def __init__(self, args):
        self._log_step = args.log_step
        self._batch_size = args.batch_size
        self._image_size = args.image_size
        self._label_size = args.label_size
        self._latent_size = args.latent_size
        self._epochs = args.epoch

        self._augment_size = self._image_size + (30 if self._image_size == 256 else 15)
        self._image_shape = [self._image_size, self._image_size, 3]

        self.is_train = tf.placeholder(tf.bool, name='is_train')
        self.global_step = tf.contrib.framework.get_or_create_global_step(graph=None)

        self.x_input = tf.placeholder(tf.float32, [self._batch_size] + self._image_shape, 'x_input')

        G = Generator('Generator', self.is_train, norm='layer', activation='leaky', image_shape=self._image_shape,
                      bias=True)
        E = Encoder('Encoder', self.is_train, latent_size=self._latent_size, norm='layer', activation='leaky',
                    bias=True, image_shape=self._image_shape)

        mu, stddev = E(self.x_input)

        eps = tf.random_normal(tf.shape(mu), 0, 1, dtype=tf.float32)
        z = mu + stddev * eps
        # for test
        # z = tf.random_normal([self._batch_size] + [self._latent_size])

        self.x_out = G(z)

        self.z_gen = tf.random_normal([self._batch_size] + [self._latent_size])
        self.x_gen = G(self.z_gen)

        loss_recon = self.loss_recon = tf.reduce_mean(tf.reduce_sum(tf.abs(self.x_out - self.x_input), axis=[1, 2, 3]))
        kl_div = 0.5 * tf.reduce_sum(tf.square(mu) + tf.square(stddev) - tf.log(1e-8 + tf.square(stddev)) - 1, axis=1)
        loss_kl = self.loss_kl = tf.reduce_mean(kl_div) * 3
        loss = loss_recon + loss_kl

        lr = 3e-4

        self.opt = tf.train.AdamOptimizer(lr, beta1=0.5).minimize(loss, global_step=self.global_step)

        tf.summary.scalar('loss/recon', self.loss_recon)
        tf.summary.scalar('loss/kl', self.loss_kl)
        tf.summary.image('x/original', self.x_input)
        tf.summary.image('G/recon', self.x_out)
        tf.summary.histogram('E/z', z)

        self.summary_op = tf.summary.merge_all()

    def train(self, sess, summary_writer, data):
        logger.info('Start training.')
        x_train_files = sorted(glob('./datasets/images/*.jpg'))

        data_size = len(x_train_files)
        logger.info('   {} images from data'.format(data_size))

        num_batch = data_size // self._batch_size

        initial_step = sess.run(self.global_step)
        num_global_step = self._epochs * num_batch

        t = trange(initial_step, num_global_step,
                   total=num_global_step, initial=initial_step)

        for step in t:
            iteration = step % num_batch
            if iteration == 0:
                shuffle(x_train_files)

            x_batch_train_files = x_train_files[iteration*self._batch_size:(iteration+1)*self._batch_size]
            x_batch_train = np.stack([get_image(batch_file, self._image_size, crop_size=136) for batch_file in x_batch_train_files])
            x_batch_train = np.reshape(x_batch_train, [-1] + self._image_shape)

            sess.run(self.opt, feed_dict={self.x_input: x_batch_train,
                                            self.is_train: True})

            fetches = [self.loss_kl, self.loss_recon]

            if step % self._log_step == 0:
                fetches += [self.summary_op]
                t.set_description('\n')
                fetched = sess.run(fetches, feed_dict={self.x_input: x_batch_train,
                                                       self.is_train: False
                                                       })

            if step % self._log_step == 0:
                summary_writer.add_summary(fetched[-1], step)
                summary_writer.flush()
                t.set_description('loss: kl({:.3f}) recon({:.3f})'.format(fetched[0], fetched[1]))

    def test(self, sess, base_dir, data):
        # reconstruction & generation
        x_test_files = sorted(glob('./datasets/test/*.jpg'))
        x_test_files = x_test_files[:self._batch_size]
        x_test = np.stack([get_image(batch_file, self._image_size, crop_size=136) for batch_file in x_test_files])
        x_test = np.reshape(x_test, [-1] + self._image_shape)
        img_frame_dim = int(np.floor(np.sqrt(self._batch_size)))
        samples = sess.run(self.x_out, feed_dict={self.x_input: x_test, self.is_train: False})
        samples = np.reshape(samples, [-1] + self._image_shape)
        gen = sess.run(self.x_gen, feed_dict={self.is_train: False})
        gen = np.reshape(gen, [-1] + self._image_shape)
        data_saver.save_images(samples[:img_frame_dim**2, :, :, :], [img_frame_dim, img_frame_dim], os.path.join(base_dir, 'VAE_celebA_recon.jpg'))
        data_saver.save_images(x_test[:img_frame_dim**2, :, :, :], [img_frame_dim, img_frame_dim], os.path.join(base_dir, 'VAE_celebA_orig.jpg'))
        data_saver.save_images(gen[:img_frame_dim**2, :, :, :], [img_frame_dim, img_frame_dim], os.path.join(base_dir, 'VAE_celebA_gen.jpg'))


import os
import argparse
import pandas as pd
import numpy as np
import keras.backend as K
from keras.models import Model
from keras.models import load_model
from keras.layers import Dense
from keras.layers import Input
from keras.layers import BatchNormalization
from keras.layers import TimeDistributed
from keras.layers import SimpleRNN
from keras.layers import GRU
from keras.layers import Lambda
from keras.layers import concatenate
from keras.optimizers import Adam
from keras.callbacks import ModelCheckpoint
from keras.callbacks import CSVLogger
from src.data.category_idx import map_categories
from src.model.multi_memmap_iterator import MultiMemmapIterator
from src.model.resnet50_vecs import create_images_df

LOAD_MODEL = 'model.h5'
SNAPSHOT_MODEL = 'model.h5'
LOG_FILE = 'training.log'
PREDICTIONS_FILE = 'single_predictions.csv'
VALID_PREDICTIONS_FILE = 'valid_single_predictions.csv'
MAX_PREDICTIONS_AT_TIME = 50000

def train_data(memmap_path, memmap_len, bcolz_prod_info, sample_prod_info, train_split, category_idx, batch_size,
               shuffle=None, batch_seed=123, max_images=2, only_single=False, use_img_idx=False, include_singles=True):
    images_df = create_images_df(bcolz_prod_info, False)
    bcolz_prod_info['category_idx'] = map_categories(category_idx, bcolz_prod_info['category_id'])
    bcolz_prod_info = bcolz_prod_info.merge(train_split, on='product_id', how='left')
    images_df = images_df.merge(bcolz_prod_info, on='product_id', how='left')[
        ['product_id', 'category_idx', 'img_idx', 'num_imgs', 'train']]
    if shuffle:
        np.random.seed(shuffle)
        perm = np.random.permutation(images_df.shape[0])
        images_df = images_df.reindex(perm)
        images_df.reset_index(drop=True, inplace=True)
    images_df = images_df[images_df.product_id.isin(sample_prod_info.product_id)]
    train_df = images_df[images_df['train']]
    valid_df = images_df[~images_df['train']]
    num_classes = np.unique(images_df['category_idx']).size

    train_it = MultiMemmapIterator(memmap_path=memmap_path,
                                   memmap_shape=(memmap_len, 2048),
                                   images_df=train_df,
                                   num_classes=num_classes,
                                   seed=batch_seed,
                                   batch_size=batch_size,
                                   only_single=only_single,
                                   include_singles=include_singles,
                                   max_images=max_images,
                                   pool_wrokers=4,
                                   shuffle=True,
                                   use_side_input=use_img_idx)
    valid_mul_it = MultiMemmapIterator(memmap_path=memmap_path,
                                       memmap_shape=(memmap_len, 2048),
                                       images_df=valid_df,
                                       num_classes=num_classes,
                                       seed=batch_seed,
                                       batch_size=batch_size,
                                       shuffle=False,
                                       only_single=False,
                                       include_singles=False,
                                       max_images=4,
                                       pool_wrokers=1,
                                       use_side_input=use_img_idx)
    valid_sngl_it = MultiMemmapIterator(memmap_path=memmap_path,
                                        memmap_shape=(memmap_len, 2048),
                                        images_df=valid_df,
                                        num_classes=num_classes,
                                        seed=batch_seed,
                                        batch_size=batch_size,
                                        shuffle=False,
                                        only_single=True,
                                        include_singles=True,
                                        max_images=4,
                                        pool_wrokers=1,
                                        use_side_input=use_img_idx)
    return train_it, valid_mul_it, valid_sngl_it, num_classes

def fit_model(train_it, valid_mul_it, valid_sngl_it, num_classes, models_dir, lr=0.001, batch_size=64, epochs=1, mode=0,
              seed=125):
    model_file = os.path.join(models_dir, LOAD_MODEL)
    if os.path.exists(model_file):
        model = load_model(model_file)
    else:
        if mode == 0:
            inp1 = Input((None, 2048))
            inp2 = Input((None, 8))
            x = concatenate([inp1, inp2])
            x = TimeDistributed(Dense(4096, activation='relu'))(x)
            x = BatchNormalization(axis=-1)(x)
            x = TimeDistributed(Dense(4096, activation='relu'))(x)
            x = BatchNormalization(axis=-1)(x)
            x = Lambda(lambda x: K.sum(x, axis=-2), output_shape=(4096,))(x)
            x = Dense(num_classes, activation='softmax')(x)
            model = Model([inp1, inp2], x)
        elif mode == 1:
            inp1 = Input((None, 2048))
            inp2 = Input((None, 8))
            x = concatenate([inp1, inp2])
            x = TimeDistributed(Dense(4096, activation='relu'))(x)
            x = BatchNormalization(axis=-1)(x)
            x = SimpleRNN(4096, activation='relu', recurrent_initializer='identity')(x)
            x = BatchNormalization(axis=-1)(x)
            x = Dense(num_classes, activation='softmax')(x)
            model = Model([inp1, inp2], x)
        elif mode == 2:
            inp1 = Input((None, 2048))
            inp2 = Input((None, 8))
            x = concatenate([inp1, inp2])
            x = TimeDistributed(Dense(4096, activation='relu'))(x)
            x = BatchNormalization(axis=-1)(x)
            x = TimeDistributed(Dense(4096, activation='relu'))(x)
            x = BatchNormalization(axis=-1)(x)
            x = GRU(100, activation='relu')(x)
            x = BatchNormalization(axis=-1)(x)
            x = Dense(num_classes, activation='softmax')(x)
            model = Model([inp1, inp2], x)
        elif mode == 3:
            inp1 = Input((None, 2048))
            inp1_com = Lambda(lambda x: K.max(x, axis=-2), output_shape=(2048,))(inp1)
            x = Dense(4096, activation='relu')(inp1_com)
            x = BatchNormalization(axis=-1)(x)
            x = Dense(4096, activation='relu')(x)
            x = BatchNormalization(axis=-1)(x)
            x = Dense(num_classes, activation='softmax')(x)
            model = Model(inp1, x)

    model.compile(optimizer=Adam(lr=lr), loss='sparse_categorical_crossentropy',
                  metrics=['sparse_categorical_accuracy'])

    np.random.seed(seed)
    checkpointer = ModelCheckpoint(filepath=os.path.join(models_dir, SNAPSHOT_MODEL))
    csv_logger = CSVLogger(os.path.join(models_dir, LOG_FILE), append=True)
    model.fit_generator(train_it,
                        steps_per_epoch=train_it.samples / batch_size,
                        validation_data=valid_mul_it,
                        validation_steps=valid_mul_it.samples / batch_size,
                        epochs=epochs,
                        callbacks=[checkpointer, csv_logger],
                        max_queue_size=10,
                        use_multiprocessing=False)

    with open(os.path.join(models_dir, LOG_FILE), "a") as file:
        file.write('Multi {}\n'.format(model.evaluate_generator(valid_mul_it, steps=valid_mul_it.samples / batch_size)))
        file.write(
            'Single {}\n'.format(model.evaluate_generator(valid_sngl_it, steps=valid_sngl_it.samples / batch_size)))

def predict(memmap_path, memmap_len, prod_info, sample_prod_info, models_dir, batch_size=200,
            shuffle=None, top_k=10, use_img_idx=True):
    model_file = os.path.join(models_dir, LOAD_MODEL)
    if os.path.exists(model_file):
        model = load_model(model_file)
    else:
        raise ValueError("Model doesn't exist")
    images_df = create_images_df(prod_info, False)
    images_df = images_df.merge(prod_info, on='product_id', how='left')[
        ['product_id', 'img_idx', 'num_imgs']]
    if shuffle:
        np.random.seed(shuffle)
        perm = np.random.permutation(images_df.shape[0])
        images_df = images_df.reindex(perm)
        images_df.reset_index(drop=True, inplace=True)
    if sample_prod_info is not None:
        images_df = images_df[images_df.product_id.isin(sample_prod_info.product_id)]
    images_df.sort_values('product_id', inplace=True)
    dfs = []
    offset = 0
    while offset < images_df.shape[0]:
        end_idx = min(images_df.shape[0], offset + MAX_PREDICTIONS_AT_TIME - 5)
        while end_idx < images_df.shape[0]:
            if images_df.iloc[end_idx - 1].product_id == images_df.iloc[end_idx].product_id:
                end_idx += 1
            else:
                break
        it = MultiMemmapIterator(memmap_path=memmap_path,
                            memmap_shape=(memmap_len, 2048),
                            images_df=images_df[offset:end_idx],
                            batch_size=batch_size,
                            pool_wrokers=1,
                            only_single=False,
                            include_singles=False,
                            max_images=4,
                            shuffle=False,
                            use_side_input=use_img_idx)

        preds = model.predict_generator(it, it.samples / batch_size,
                                        verbose=1, max_queue_size=10)
        it.terminate()
        del it
        chunk = []
        for i, product_id in enumerate(images_df[offset:end_idx].product_id.unique()):
            top_k_preds = np.argpartition(preds[i], -top_k)[-top_k:]
            for pred_idx in range(top_k):
                chunk.append((product_id, 0, top_k_preds[pred_idx], preds[i, top_k_preds[pred_idx]]))

        chunk_df = pd.DataFrame(chunk, columns=['product_id', 'img_idx', 'category_idx', 'prob'])
        dfs.append(chunk_df)
        offset = end_idx
        del preds
        del chunk
    return pd.concat(dfs)

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--fit', action='store_true', dest='is_fit')
    parser.add_argument('--predict', action='store_true', dest='is_predict')
    parser.add_argument('--predict_valid', action='store_true', dest='is_predict_valid')
    parser.add_argument('--bcolz_root', required=True, help='VGG16 vecs bcolz root path')
    parser.add_argument('--bcolz_prod_info_csv', required=True,
                        help='Path to prod info csv with which VGG16 were generated')
    parser.add_argument('--sample_prod_info_csv', required=True, help='Path to sample prod info csv')
    parser.add_argument('--category_idx_csv', required=True, help='Path to categories to index mapping csv')
    parser.add_argument('--train_split_csv', required=True, help='Train split csv')
    parser.add_argument('--models_dir', required=True, help='Output directory for models snapshots')
    parser.add_argument('--lr', type=float, default=0.001, required=False, help='Learning rate')
    parser.add_argument('--batch_size', type=int, default=64, required=False, help='Batch size')
    parser.add_argument('--epochs', type=int, default=1, required=False, help='Number of epochs')
    parser.add_argument('--only_first_image', dest='only_first_image', action='store_true',
                        help="Include only first image from each product")
    parser.add_argument('--shuffle', type=int, default=None, required=False,
                        help='If products should be shuffled, provide seed')
    parser.set_defaults(only_first_image=False)
    parser.add_argument('--mode', type=int, default=0, required=False, help='Mode')
    parser.add_argument('--batch_seed', type=int, default=123, required=False, help='Batch seed')
    parser.add_argument('--dont_use_img_idx', action='store_false', dest='use_img_idx')
    parser.set_defaults(use_img_idx=True)
    parser.add_argument('--memmap_len', type=int, required=True, help='Number of rows in memmap')
    parser.set_defaults(two_outs=False)
    parser.add_argument('--max_images', type=int, default=2, required=False, help='Max images in train record')
    parser.add_argument('--only_single', action='store_true', dest='only_single')
    parser.set_defaults(only_single=False)
    parser.add_argument('--dont_include_singles', action='store_false', dest='include_singles')
    parser.set_defaults(include_singles=True)

    args = parser.parse_args()
    if not os.path.isdir(args.models_dir):
        os.mkdir(args.models_dir)

    bcolz_prod_info = pd.read_csv(args.bcolz_prod_info_csv)
    sample_prod_info = pd.read_csv(args.sample_prod_info_csv)
    train_split = pd.read_csv(args.train_split_csv)
    category_idx = pd.read_csv(args.category_idx_csv)

    if args.is_fit:
        train_it, valid_mul_it, valid_sngl_it, num_classes = train_data(args.bcolz_root,
                                                                        args.memmap_len,
                                                                        bcolz_prod_info,
                                                                        sample_prod_info,
                                                                        train_split,
                                                                        category_idx,
                                                                        args.batch_size,
                                                                        args.shuffle,
                                                                        args.batch_seed,
                                                                        args.max_images,
                                                                        args.only_single,
                                                                        args.use_img_idx,
                                                                        args.include_singles)
        fit_model(train_it, valid_mul_it, valid_sngl_it, num_classes, args.models_dir, args.lr, args.batch_size,
                  args.epochs,
                  args.mode,
                  args.batch_seed)
        train_it.terminate()
        valid_mul_it.terminate()
        valid_sngl_it.terminate()
    elif args.is_predict:
        out_df = predict(args.bcolz_root, args.memmap_len, bcolz_prod_info, sample_prod_info, args.models_dir,
                         use_img_idx=args.use_img_idx)
        out_df.to_csv(os.path.join(args.models_dir, PREDICTIONS_FILE), index=False)
    elif args.is_predict_valid:
        only_valids = bcolz_prod_info[
            bcolz_prod_info.product_id.isin(train_split[train_split.train == False].product_id)]
        out_df = predict(args.bcolz_root, args.memmap_len, bcolz_prod_info, only_valids, args.models_dir,
                         shuffle=args.shuffle, use_img_idx=args.use_img_idx)
        out_df.to_csv(os.path.join(args.models_dir, VALID_PREDICTIONS_FILE), index=False)


from typing import List

# Definition for a binary tree node.
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution:
    def lowestCommonAncestor(
        self, root: "TreeNode", p: "TreeNode", q: "TreeNode"
    ) -> "TreeNode":
        if root in (None, p, q):
            return root
        left = self.lowestCommonAncestor(root.left, p, q)
        right = self.lowestCommonAncestor(root.right, p, q)

        if not left and not right:
            return None
        if left and right:
            return root
        return right if not left else right
        # if not root:
        #     return None
        # res, _ = self.recursive(root, p, q)
        # return res

    def recursive1(
        self, root: "TreeNode", p: "TreeNode", q: "TreeNode"
    ) -> ("TreeNode", List["TreeNode"]):
        if not root:
            return None, []

        left, l_data = self.recursive(root.left, p, q)
        right, r_data = self.recursive(root.right, p, q)

        if left:
            return left, []
        if right:
            return right, []

        l_data.extend(r_data)
        if root.val == p.val or root.val == q.val:
            l_data.append(root)

        if len(l_data) == 2:
            return root, []

        return None, l_data

def main():
    nums = [0, 1, 2, 3, 4, 5, 6, 7]
    # nums = [0, 1, -1, 3, -1, -1, -1, 7]
    # nums = [0, 1, 2, 3, -1, 4, 6, -1, -1, -1, -1, 5, -1, 7]
    root = makeTree(nums, 1)
    sol = Solution()
    res = sol.lowestCommonAncestor(root, TreeNode(4), TreeNode(5))
    print(res.val)

def makeTree(nums: List[int], pos: int) -> TreeNode:
    if pos >= len(nums) or nums[pos] < 0:
        return None
    node = TreeNode(nums[pos])
    node.left = makeTree(nums, pos * 2)
    node.right = makeTree(nums, pos * 2 + 1)
    return node

main()

leetcode/old_session/236. Lowest Common Ancestor of a Binary Tree.py


#
# Copyright (c) 2017-2018 Minato Sato
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
#

import numpy as np
import os

w2i = {}
i2w = {}

c2i = {}
i2c = {}

w2i['pad'] = 0
i2w[0] = 'pad'

c2i[' '] = 0
i2c[0] = ' '

word_length = 20

def load_data(filename):
    global w2i, i2w
    global c2i, i2c
    with open(filename) as f:
        lines = f.read().replace('\n', '<eos>')
        for char in set(lines):
            if char not in c2i:
                c2i[char] = len(c2i)
            if c2i[char] not in i2c:
                i2c[c2i[char]] = char

        words = lines.strip().split()
    dataset = np.ndarray((len(words), ), dtype=np.int32)

    for i, word in enumerate(words):
        if word not in w2i:
            w2i[word] = len(w2i)
        if w2i[word] not in i2w:
            i2w[w2i[word]] = word
        dataset[i] = w2i[word]

    sentences = []
    sentence = []
    for index in dataset:
        if i2w[index] != '<eos>':
            sentence.append(index)
        else:
            sentence.append(index)
            sentences.append(sentence)
            sentence = []
    return sentences

def wordseq2charseq(data):
    global word_length
    data = np.repeat(np.expand_dims(data, axis=2), word_length, axis=2)
    data[:, :, 1:] = 0
    for i in range(data.shape[0]):
        for j in range(data.shape[1]):
            word = data[i][j][0]
            for k, char in enumerate(i2w[word]):
                data[i][j][k] = c2i[char]
    return data

def with_padding(sequences, padding_type='post', max_sequence_length=None):
    if max_sequence_length is None:
        max_sequence_length = max(map(lambda x: len(x), sequences))
    else:
        assert type(max_sequence_length) == int, 'max_sequence_length must be an integer.'
        assert max_sequence_length > 0, 'max_sequence_length must be a positive integer.'

    def _with_padding(sequence):
        sequence = sequence[:max_sequence_length]
        sequence_length = len(sequence)
        pad_length = max_sequence_length - sequence_length
        if padding_type == 'post':
            return sequence + [0] * pad_length
        elif padding_type == 'pre':
            return [0] * pad_length + sequence
        else:
            raise Exception('padding type error. padding type must be "post" or "pre"')

    return np.array(list(map(_with_padding, sequences)), dtype=np.int32)

ptb_url = 'https://raw.githubusercontent.com/wojzaremba/lstm/master/data/ptb.{0}.txt'
types = ['train', 'valid', 'test']
ptb_urls = map(lambda x: ptb_url.format(x), types)
os.makedirs('./ptb/', exist_ok=True)
for _url, _type in zip(ptb_urls, types):
    if not os.path.exists('./ptb/' + _type + '.txt'):
        os.system('wget -O ' + './ptb/' + _type + '.txt ' + _url)


def file_name(path):
	import os
	filelist = os.listdir(path)
	timelist = list(map(lambda x: os.stat(path + x).st_mtime, filelist))
	filename = filelist[timelist.index(max(timelist))]
	return path + filename

def load_file(filename):
	f = open(filename)
	kmer = f.readline().rstrip()
	dnas = f.readline().rstrip().split(' ')
	return kmer, dnas

def ham_d_s(kmer, dnas):
	ret = 0
	for dna in dnas:
		ret += ham_d(kmer, dna)
	return ret

def ham_d(kmer, dna):
	mind = len(dna)
	for i in range(len(dna) - len(kmer) + 1):
		part = dna[i: i + len(kmer)]
		cnt = 0
		for x, y in zip(part, kmer):
			cnt = cnt + 1 if x != y else cnt
		mind = cnt if cnt < mind else mind
	return mind

def main():
	path = 'D:/Browser Download/'
	filename = file_name(path)
	kmer, dnas = load_file(filename)
	print(ham_d_s(kmer, dnas))

main()


#Zak9-14
import turtle
t = turtle.Turtle()
wn = turtle.Screen()
wn.setworldcoordinates(-500, -500, 500, 500)
file = open("mystery.txt", "r")
for aline in file:
    items = aline.split()
    if items[0] == "UP":
        t.up()
    elif items[0] == "DOWN":
        t.down()
    else:
        t.goto(int(items[0]), int(items[1]))


import os
import pandas as pd

SPlist = pd.read_csv('Sp500listdata.csv',',')
def cleaning(object):
	object.replace("'","")

for stock in SPlist:
	stock = cleaning(stock)

def filecreater(list):
	for i in list:
		open(('Data/' + str(i).strip("'") + '.csv').replace("'",""),'a')
filecreater(SPlist)
#print(SPlist)


from PyQt4.QtGui import *
from PyQt4.QtCore import *
import keyword
from conf import python

#QBrush(QColor)

class PythonHighlighter(QSyntaxHighlighter):
	def __init__(self, parent=None):
		super(PythonHighlighter, self).__init__(parent)
		self.keywords = []
		self.RULES = []
		self.shema = "light"
		self.load_python_keyords()
		self.__createRules()

	def __createRules(self):
		keywordformat = QTextCharFormat()
		kbrush = QBrush(QColor(python.shemas[self.shema]["keyword"]))
		keywordformat.setForeground(kbrush)
		keywordformat.setFontWeight(QFont.Bold)
		for key in self.keywords:
			self.RULES.append((QRegExp(key), keywordformat))

		fromImportFormat = QTextCharFormat()
		fromImportFormat.setForeground(Qt.green)
		self.RULES.append((QRegExp("^(?=from) .* (?=import)"), fromImportFormat))

		integerFormat = QTextCharFormat()
		ibrush = QBrush(QColor(python.shemas[self.shema]["integer"]))
		integerFormat.setForeground(ibrush)
		self.RULES.append((QRegExp("\\b[0-9]+\\b"), integerFormat))

		stringDoubleCuoted = QTextCharFormat()
		sdcbrush = QBrush(QColor(python.shemas[self.shema]["string"]))
		stringDoubleCuoted.setForeground(sdcbrush)
		self.RULES.append((QRegExp("\".*\""), stringDoubleCuoted))

		stringSingleCuoted = QTextCharFormat()

		stringSingleCuoted.setForeground(sdcbrush)
		stringSingleCuoted.setFontItalic(True)
		self.RULES.append((QRegExp("\'.*\'"), stringSingleCuoted))

		functionFormat = QTextCharFormat()
		fbrush = QBrush(QColor(python.shemas[self.shema]["method"]))
		functionFormat.setForeground(fbrush)
		self.RULES.append((QRegExp("\\b[A-Za-z0-9_]+(?=\\()"), functionFormat))

		self.multiLineCommentFormat = QTextCharFormat()
		self.multiLineCommentFormat.setForeground(Qt.red)
		self.commentMultiline = QRegExp('"""[^\']*')

		simpleLineComment = QTextCharFormat()
		slbrush = QBrush(QColor(python.shemas[self.shema]["comment"]))
		simpleLineComment.setForeground(slbrush)
		#TODO: Agregar una regla aqui para que no lo coja dentro de los comentarios
		self.RULES.append((QRegExp("(#).*[^\n]"), simpleLineComment))

	def highlightBlock(self, text):
		for regex, format in self.RULES:
			i = regex.indexIn(text)
			while i >= 0:
				lenght = regex.matchedLength()
				self.setFormat(i, lenght, format)
				i = regex.indexIn(text, i+lenght)
		self.setCurrentBlockState(0)
		# startIndex = 0
		# if self.previousBlockState() != 1:
		# 	startIndex = self.commentMultiline.indexIn(text)

		# while startIndex >= 0:
		# 	endIndex = self.commentMultiline.indexIn(text, startIndex+3)

		# 	if endIndex == -1:
		# 		self.setCurrentBlockState(1)
		# 		commentLenght = len(text) -startIndex
		# 	else:
		# 		commentLenght = endIndex - startIndex + self.commentMultiline.matchedLength()

		# 	self.setFormat(startIndex, commentLenght, self.multiLineCommentFormat)
		# 	startIndex = self.commentMultiline.indexIn(text, startIndex+commentLenght)

	def load_python_keyords(self):
		"""this method load all the python keywords"""
		keys = keyword.kwlist
		for key in keys:
			self.keywords.append("\\b{0}\\b".format(key))

	def add_rule(self, rule): #Rule es una tupla (Expresion, formato)
		self.RULES.append(rule)

	def setShema(self, shema):
		self.shema = shema
		self.RULES = []
		self.load_python_keyords()
		self.__createRules()


"""

"""

from typing import List, Sequence

from docutils import nodes
from docutils.parsers.rst import directives
from sphinx.application import Sphinx

from sphinx_needs.directives.utils import (
    no_needs_found_paragraph,
    used_filter_paragraph,
)
from sphinx_needs.filter_common import FilterBase, process_filters
from sphinx_needs.layout import create_need
from sphinx_needs.utils import unwrap

class Needextract(nodes.General, nodes.Element):
    pass

class NeedextractDirective(FilterBase):
    """
    Directive to filter needs and present them as normal needs with given layout and style.
    """

    option_spec = {
        "layout": directives.unchanged_required,
        "style": directives.unchanged_required,
        "show_filters": directives.flag,
    }
    # Update the options_spec with values defined in the FilterBase class
    option_spec.update(FilterBase.base_option_spec)

    def run(self) -> Sequence[nodes.Node]:
        env = self.state.document.settings.env
        if not hasattr(env, "need_all_needextracts"):
            env.need_all_needextracts = {}

        # be sure, global var is available. If not, create it
        if not hasattr(env, "needs_all_needs"):
            env.needs_all_needs = {}

        targetid = "needextract-{docname}-{id}".format(docname=env.docname, id=env.new_serialno("needextract"))
        targetnode = nodes.target("", "", ids=[targetid])

        # Add the need and all needed information
        env.need_all_needextracts[targetid] = {
            "docname": env.docname,
            "lineno": self.lineno,
            "target_node": targetnode,
            "env": env,
            "export_id": self.options.get("export_id", ""),
            "layout": self.options.get("layout"),
            "style": self.options.get("style"),
            "show_filters": "show_filters" in self.options,
        }
        env.need_all_needextracts[targetid].update(self.collect_filter_attributes())

        return [targetnode, Needextract("")]

def process_needextract(app: Sphinx, doctree: nodes.document, fromdocname: str) -> None:
    """
    Replace all needextract nodes with a list of the collected needs.
    """
    env = unwrap(app.env)

    for node in doctree.findall(Needextract):
        if not app.config.needs_include_needs:
            # Ok, this is really dirty.
            # If we replace a node, docutils checks, if it will not lose any attributes.
            # But this is here the case, because we are using the attribute "ids" of a node.
            # However, I do not understand, why losing an attribute is such a big deal, so we delete everything
            # before docutils claims about it.
            for att in ("ids", "names", "classes", "dupnames"):
                node[att] = []
            node.replace_self([])
            continue

        id = node.attributes["ids"][0]
        current_needextract = env.need_all_needextracts[id]
        all_needs = env.needs_all_needs
        content: List[nodes.Element] = []
        all_needs = list(all_needs.values())
        found_needs = process_filters(app, all_needs, current_needextract)

        for need_info in found_needs:
            need_extract = create_need(
                need_info["id"],
                app,
                layout=current_needextract["layout"],
                style=current_needextract["style"],
                docname=current_needextract["docname"],
            )

            # Add lineno to node
            need_extract.line = current_needextract["lineno"]

            content.append(need_extract)

        if len(content) == 0:
            content.append(no_needs_found_paragraph())

        if current_needextract["show_filters"]:
            content.append(used_filter_paragraph(current_needextract))

        node.replace_self(content)


'''
这只是纯api的一个测试
'''
import CRFPP
import sys

try:
    # -v 3: access deep information like alpha,beta,prob
    # -nN: enable nbest output. N should be >= 2
    tagger = CRFPP.Tagger("-m ../../trainSource/2/model -v 3 -n 2")

    # clear internal context
    tagger.clear()

    # add context
    tagger.add("北 None 京")
    tagger.add("京 北 人")
    tagger.add("人 京 都")
    tagger.add("都 人 是")
    tagger.add("是 都 活")
    tagger.add("活 是 雷")
    tagger.add("雷 活 峰")
    tagger.add("峰 雷 None")

    print ("column size: " , tagger.xsize())
    print ("token size: " , tagger.size())
    print ("tag size: " , tagger.ysize())

    print ("tagset information:")
    ysize = tagger.ysize()
    for i in range(0, ysize-1):
        print ("tag " , i , " " , tagger.yname(i))

    # parse and change internal stated as 'parsed'
    tagger.parse()

    print ("conditional prob=" , tagger.prob(), " log(Z)=" , tagger.Z())

    size = tagger.size()
    xsize = tagger.xsize()
    for i in range(0, (size - 1)):
        for j in range(0, (xsize-1)):
            print (tagger.x(i, j) , " 2222 ",)
        print (tagger.y2(i) , "\t",)
        print ("Details")
        for j in range(0, (ysize-1)):
            print (tagger.yname(j) , "/prob=" , tagger.prob(i,j),"/alpha=" , tagger.alpha(i, j),"/beta=" , tagger.beta(i, j),)
        print()

    print ("nbest outputs:")
    for n in range(0, 9):
        if (not tagger.next()):
            continue
        print ("nbest n=" , n , "\tconditional prob=" , tagger.prob())
        # you can access any information using tagger.y()...

    print ("Done")

except RuntimeError as e:
    print ("RuntimeError: ", e,)


# Rename _icon and _type options to allow overriding them in options
def _show(title=None, message=None, _icon=None, _type=None, **options):
    if _icon and "icon" not in options:    options["icon"] = _icon
    if _type and "type" not in options:    options["type"] = _type
    if title:   options["title"] = title
    if message: options["message"] = message
    res = Message(**options).show()
    # In some Tcl installations, yes/no is converted into a boolean.
    if isinstance(res, bool):
        if res:
            return YES
        return NO
    # In others we get a Tcl_Obj.
    return str(res)


__all__ = ('CircularLayout', )
import math
from kivy.clock import Clock
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.widget import Widget

class CircularLayout (FloatLayout):
    def __init__(self, **kvargs):
        # The max widgets that the layout can hold, I think it does not have a purpose but I havent checked
        self._max_widgets = 0
        # Stores which widget is exactly to the right, even after rotation
        self._first_widget = -1
        # Kivy docs explain timers
        self._trigger_layout = Clock.create_trigger(self._do_layout, -1)
        super (FloatLayout, self).__init__(**kvargs)
        # Any changes to these attributes will trigger a method call to _do_layout at the earliest time possible
        self.bind (children = self._trigger_layout,
                   pos = self._trigger_layout,
                   pos_hint = self._trigger_layout,
                   size_hint = self._trigger_layout,
                   size = self._trigger_layout)

    def _do_layout (self, *largs):
        if len(self.children) == 0:
            return
        # see above
        self._max_widgets = num_widgets = len(self.children)
        # stores in local variables for efficient access
        temp_center_x, temp_center_y = self.center
        children = self.children[:]

        max_width = max_height = 0
        # looks for the max possible diagonal that can occur by finding max possible width and height
        for c in children:
            csx, csy = c.size
            if csx > max_width:
                max_width = csx
            if csy > max_height:
                max_height = csy
        max_diagonal = math.sqrt (max_width*max_width + max_height*max_height)

        # Some math to calculate minimum radius required to a widget given widget radius (max_diagonal/2)
        # and number of widgets. Math works I believe
        layout_radius = math.ceil (max_diagonal/2/math.sqrt(2*(1-math.cos(math.pi/len(children)))))

        # Iterates at each angle starting with the first widget set by the variable _first_widget
        # and does some work to find the widget's position
        theta = 0
        theta_increment = 2*math.pi/len(self.children)
        i = self._first_widget

        # No do/while loop in python so implemented as an infinite loop with a break
        # Basically, the first widget can be anywhere in the list and each widget in the list must be drawn in order
        # from that first widget including the ones behind it in terms of index
        # So, iterates with modulo operations
        while True:
            new_center = temp_center_x + math.cos (theta)*layout_radius, temp_center_y + math.sin (theta)*layout_radius
            self.reposition_child (children[i], center=new_center)
            theta += theta_increment
            i = (i - 1)%num_widgets
            if i == self._first_widget:
                break

    def do_rotation (self, steps=1):
        # Some modulo operations used to rotate the first child position
        num_widgets = len(self.children)
        if num_widgets == 0:
            return
        steps %= num_widgets
        self._first_widget = (self._first_widget - steps)%num_widgets
        self._do_layout ()

    def get_child (self, index=0):
        return self.children[(self._first_widget + index)%len(self.children)]

    def add_widget (self, widget, index=0):
        # Binds the new widget callback so that the layout is rearranged when a new widget is added
        self._first_widget += 1
        widget.bind (
            size = self._trigger_layout,
            size_hint = self._trigger_layout,
            pos = self._trigger_layout,
            pos_hint = self._trigger_layout)
        return super (FloatLayout, self).add_widget (widget, index)

    def remove_widget (self, widget):
        # Binds the new widget callback so that the layout is rearranged when a new widget is added
        self._first_widget -= 1
        widget.unbind(
            size = self._trigger_layout,
            size_hint = self._trigger_layout,
            pos = self._trigger_layout,
            pos_hint = self._trigger_layout)
        return super(Layout, self).remove_widget(widget)


from datetime import datetime

# Project information
project = 'PyAnsys Developers Guide'
copyright = f'{datetime.now().year}, ANSYS'
author = 'ANSYS, Inc.'
release = version = '0.1.dev0'

html_logo = 'https://docs.pyansys.com/_static/pyansys-logo-black-cropped.png'
html_theme = 'pyansys_sphinx_theme'

html_theme_options = {
    "github_url": "https://github.com/pyansys/about",
    "show_prev_next": False
}

# Sphinx extensions
extensions = [
    'sphinx.ext.todo',
    'sphinx.ext.autodoc',
    'sphinx.ext.napoleon',
    'sphinx.ext.autosummary',
]

# The suffix(es) of source filenames.
source_suffix = '.rst'

# The master toctree document.
master_doc = 'index'

master_doc = 'index'

latex_elements = {}

# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title,
#  author, documentclass [howto, manual, or own class]).
latex_documents = [
    (master_doc,
     f'pyansys_dev_guide_v{version}.tex',
     "PyAnsys Developer's Guide",
     author,
     'manual'),
]

# # -- Options for manual page output ------------------------------------------

# # One entry per manual page. List of tuples
# # (source start file, name, description, authors, manual section).
# man_pages = [
#     (master_doc, 'ansys.mapdl.core', 'ansys.mapdl.core Documentation',
#      [author], 1)
# ]

# # -- Options for Texinfo output ----------------------------------------------

# # Grouping the document tree into Texinfo files. List of tuples
# # (source start file, target name, title, author,
# #  dir menu entry, description, category)
# texinfo_documents = [
#     (master_doc, 'ansys.mapdl.core', 'ansys.mapdl.core Documentation',
#      author, 'ansys.mapdl.core', 'Pythonic interface to MAPDL using gRPC',
#      'Engineering Software'),
# ]


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Sep 24 21:01:10 2020

@author: alejandrosoto
"""

import pandas as pd
import numpy as np

Jan=pd.read_csv('Jan_S13_B.csv', sep=',', decimal=',', encoding='latin1', error_bad_lines=False)
Feb=pd.read_csv('Feb_S13_B.csv', sep=',', decimal=',', encoding='latin1', error_bad_lines=False)
Mar=pd.read_csv('Mar_S13_B.csv', sep=',', decimal=',', encoding='latin1', error_bad_lines=False)
Apr=pd.read_csv('Apr_S13_B.csv', sep=',', decimal=',', encoding='latin1', error_bad_lines=False)
May=pd.read_csv('May_S13_B.csv', sep=',', decimal=',', encoding='latin1', error_bad_lines=False)
Jun=pd.read_csv('Jun_S13_B.csv', sep=',', decimal=',', encoding='latin1', error_bad_lines=False)
Jul=pd.read_csv('Jul_S13_B.csv', sep=',', decimal=',', encoding='latin1', error_bad_lines=False)
Aug=pd.read_csv('Aug_S13_B.csv', sep=',', decimal=',', encoding='latin1', error_bad_lines=False)
Sep=pd.read_csv('Sep_S13_B.csv', sep=',', decimal=',', encoding='latin1', error_bad_lines=False)
Oct=pd.read_csv('Oct_S13_B.csv', sep=',', decimal=',', encoding='latin1', error_bad_lines=False)
Nov=pd.read_csv('Nov_S13_B.csv', sep=',', decimal=',', encoding='latin1', error_bad_lines=False)
Dec=pd.read_csv('Dec_S13_B.csv', sep=',', decimal=',', encoding='latin1', error_bad_lines=False)
p=pd.concat([Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec],ignore_index=True)
p['min'] = np.arange(len(p))
p_split = np.array_split(p, 8760)
for i in range (0,8760,1):
    p_split[i]['0'] = p_split[i]['0'].astype(float)
    globals()['string%s' % i]=p_split[i]['0'].mean()
e = []
for i in range (0,8760,1):
    e.append({'power_hourly': globals()['string%s' % i]})
pd.DataFrame(e)
df = pd.DataFrame(np.array([e]).T)
df.columns =['power']
df['power'] = df['power'].astype(str)
df[['gar','power']] = df.power.str.split(":",expand=True,)
df['power'] = df['power'].str.replace(r'}', '')
df['power'] = df['power'].astype(float)
df=df.rename(columns={'power': '1'})
df.index = np.arange(1, len(df)+1)
del df['gar']
df.to_csv('hourly_S13_B.csv')


from __future__ import unicode_literals, print_function, division
import logging
import os
import veil_component
from .installer import composite_installer

LOGGER = logging.getLogger(__name__)

@composite_installer
def component_resource(name):
    component_name = name
    module_loader = veil_component.find_module_loader_without_import(component_name)
    if not module_loader:
        raise Exception('component {} not found'.format(name))
    module_path = module_loader.get_filename()
    module_dir = os.path.dirname(module_path)
    installer_path = os.path.join(module_dir, 'INSTALLER')
    resources = []
    veil_component.scan_component(name)
    for dependent_component_name in veil_component.get_dependent_component_names(component_name,
        includes_children=True):
        resources.append(component_resource(name=dependent_component_name))
    resources.append(installer_resource(name=installer_path))
    return resources

@composite_installer
def installer_resource(name):
    installer_path = name
    resources = []
    if os.path.exists(installer_path):
        try:
            with open(installer_path) as f:
                lines = f.readlines()
            for line in lines:
                line = line.strip()
                if not line:
                    continue
                resources.append(parse_resource(line))
        except:
            LOGGER.error('failed to parse: %(installer_path)s', {'installer_path': installer_path})
            raise
    return resources

def parse_resource(line):
    if '?' not in line:
        return line, {}
    installer_name, installer_args = line.split('?')
    if not installer_args:
        return installer_name, {}
    installer_args = [arg if '=' in arg else 'name={}'.format(arg) for arg in installer_args.split('&')]
    installer_args = dict([(arg.split('=', 1)[0].strip(), arg.split('=', 1)[1].strip()) for arg in installer_args])
    return installer_name, installer_args


# -*- coding:utf-8 -*-
# edit by fuzongfei

from django.contrib.auth.decorators import login_required
from django.urls import path, re_path

from mstats.views import RenderMySQLUserView, MySQLUserView, MysqlUserManagerView, RBackupTaskView, BackupTaskView, \
    BackupTaskDetailView, BackupTaskPreviewView, BackupTaskPreviewListView, GetBackupDiskUsedView, \
    RSchemaMonitorTaskView, SchemaMonitorTaskView, MySQLQueryView, RMySQLQueryView

urlpatterns = [
    path(r'r_mysql_user_manager/', login_required(RenderMySQLUserView.as_view()), name='p_r_mysql_user_manager'),
    path(r'mysql_user/', login_required(MySQLUserView.as_view()), name='p_mysql_user'),
    path(r'mysql_user_manager/', login_required(MysqlUserManagerView.as_view()), name='p_mysql_user_manager'),
    # 监控表结构定时任务
    path(r'rperiodic_task/', login_required(RSchemaMonitorTaskView.as_view()), name='p_rschema_monitor_task'),
    path(r'periodic_task/', login_required(SchemaMonitorTaskView.as_view()), name='p_schema_monitor_task'),
    # 监控备份定时任务
    path(r'rbackup_task/', login_required(RBackupTaskView.as_view()), name='p_rbackup_task'),
    path(r'backup_task/', login_required(BackupTaskView.as_view()), name='p_backup_task'),
    # 获取备份任务信息
    path(r'backup_task_detail/', login_required(BackupTaskDetailView.as_view()), name='p_backup_task_detail'),
    re_path(r'backup_task_preview/(?P<id>\d+)/', login_required(BackupTaskPreviewView.as_view())),
    re_path(r'backup_task_preview_list', login_required(BackupTaskPreviewListView.as_view()),
            name='p_backup_task_preview_list'),
    re_path(r'get_backup_disk_used', login_required(GetBackupDiskUsedView.as_view()),
            name='p_get_backup_disk_used'),
    path(r'rquery/', login_required(RMySQLQueryView.as_view()), name='p_rquery'),
    path(r'query/', login_required(MySQLQueryView.as_view()), name='p_query')
]