from uuid import UUID
from components.models import Component
from django.db.models import Sum
from inventory.models import UserInventory
from inventory.serializers import UserInventorySerializer
from modules.models import ModuleBomListItem
from rest_framework import status
from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
from django.shortcuts import get_object_or_404

class UserInventoryView(APIView):
    """
    Handles GET, POST, DELETE, and PATCH methods for the user's inventory.
    """

    permission_classes = [IsAuthenticated]

    def get(self, request):
        user = request.user
        inventory = UserInventory.objects.filter(user=user)
        serializer = UserInventorySerializer(inventory, many=True)
        return Response(serializer.data, status=status.HTTP_200_OK)

    def post(self, request, component_pk):
        user = request.user

        # Determine the editing mode from request
        edit_mode = request.data.get("editMode", True)

        # Filter the user inventory items by user and component_id
        user_inventory_items = UserInventory.objects.filter(
            user=user, component__id=component_pk
        )

        # If the user inventory item exists, update the quantity
        if user_inventory_items.exists():
            user_inventory_item = user_inventory_items.first()
            quantity = int(request.data.get("quantity", 0))
            if edit_mode:
                user_inventory_item.quantity = quantity
            else:
                user_inventory_item.quantity += quantity

            user_inventory_item.save()
            serializer = UserInventorySerializer(user_inventory_item)
            return Response(serializer.data, status=status.HTTP_200_OK)

        # If the user inventory item does not exist, create a new one
        quantity = int(request.data.get("quantity", 0))
        component = Component.objects.get(id=component_pk)

        user_inventory = UserInventory.objects.create(
            user=user, component=component, quantity=quantity
        )

        serializer = UserInventorySerializer(user_inventory)
        return Response(serializer.data, status=status.HTTP_201_CREATED)

    def delete(self, request, component_pk):
        user = request.user
        user_inventory_item = UserInventory.objects.filter(
            user=user, component__id=component_pk
        ).first()

        if not user_inventory_item:
            return Response(
                {"detail": "User inventory not found"}, status=status.HTTP_404_NOT_FOUND
            )

        user_inventory_item.delete()
        return Response(
            {"detail": "User inventory deleted successfully"},
            status=status.HTTP_204_NO_CONTENT,
        )

    def patch(self, request, component_pk):
        user = request.user
        user_inventory_item = UserInventory.objects.filter(
            user=user, component__id=component_pk
        ).first()

        if not user_inventory_item:
            return Response(
                {"detail": "User inventory not found"}, status=status.HTTP_404_NOT_FOUND
            )

        serializer = UserInventorySerializer(
            user_inventory_item, data=request.data, partial=True
        )

        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

@permission_classes([IsAuthenticated])
@api_view(["GET"])
def get_user_inventory_quantity(request, component_pk):
    inventory = UserInventory.objects.filter(
        component__id=component_pk, user=request.user
    )

    # Check if inventory exists
    if inventory.exists():
        # Access the first inventory object in the QuerySet
        # and retrieve the 'quantity' attribute
        quantity = inventory.first().quantity
        return Response({"quantity": quantity}, status=status.HTTP_200_OK)
    else:
        return Response({"quantity": 0}, status=status.HTTP_200_OK)

@permission_classes([IsAuthenticated])
@api_view(["GET"])
def get_user_inventory_quantities_for_bom_list_item(request, modulebomlistitem_pk):
    """
    Get sum of components in user inventory that fulfill a given bom list item
    """
    bom_list_item = ModuleBomListItem.objects.get(id=modulebomlistitem_pk)
    inventory = UserInventory.objects.filter(
        component__in=bom_list_item.components_options.all(), user=request.user
    )

    # Check if inventory exists
    if inventory.exists():
        # Use aggregate function to get the sum of 'quantity' attribute
        quantity_sum = inventory.aggregate(Sum("quantity")).get("quantity__sum")
        return Response({"quantity": quantity_sum}, status=status.HTTP_200_OK)
    else:
        return Response({"quantity": 0}, status=status.HTTP_200_OK)


"""Config Dictionary Dumpers."""

import json

from .__optional_imports import try_import
from .__typing import ConfigDict, FilePath

def json_dumper(config: ConfigDict, location: FilePath) -> None:
    """Dump config to JSON file.

    Args:
        config (ConfigDict): configuration
        location (FilePath): file to write
    """
    with open(location, "w", encoding="utf-8") as _file:
        json.dump(config, _file)

def yaml_dumper(config: ConfigDict, location: FilePath) -> None:
    """Dump config to YAML file.

    Args:
        config (ConfigDict): configuration
        location (FilePath): file to write

    Raises:
        ModuleNotFoundError: pyyaml is required
    """

    yaml = try_import("yaml")

    if yaml is None:  # pragma: no cover
        message = "Please install the pyyaml library."
        raise ModuleNotFoundError(message)

    with open(location, "w", encoding="utf-8") as _file:
        # NOTE: we must convert config from OrderedDict to dict because
        # pyyaml can't load OrderedDict for python <= 3.8
        yaml.dump(dict(config), _file)

def toml_dumper(config: ConfigDict, location: FilePath) -> None:
    """Dump config to TOML file.

    Args:
        config (ConfigDict): configuration
        location (FilePath): file to write

    Raises:
        ModuleNotFoundError: toml library is required for writing files
    """

    toml = try_import("toml")

    if toml is None:  # pragma: no cover
        message = "Please install the toml library to write TOML files."
        raise ModuleNotFoundError(message)

    with open(location, "w", encoding="utf-8") as _file:
        toml.dump(config, _file)  # type: ignore


""" Script that evaluates reaction coordinates using the SGOOP method.
Probabilites are calculated using MD trajectories. Transition rates are
found using the maximum caliber approach.
For unbiased simulations use rc_eval().
For biased simulations calculate unbiased probabilities and analyze then with sgoop().

The original method was published by Tiwary and Berne, PNAS 2016, 113, 2839.

Author: Zachary Smith                   zsmith7@terpmail.umd.edu
Original Algorithm: Pratyush Tiwary     ptiwary@umd.edu
Contributor: Pablo Bravo Collado        ptbravo@uc.cl"""

import numpy as np
import scipy.optimize as opt
import sgoop.analysis as analysis

# #####################################################################
# ########### Get probabilities along RC with KDE #####################
# #####################################################################

def md_prob(rc, md_traj, weights=None, rc_bins=20, kde_bw=None):
    """
    Calculate probability density along a given reaction coordinate.

    Calculate the value of the probability density function (for KDE) or probability
    mass function (for histogram) at discrete grid points along a given RC. For a
    biased simulation, frame weights should be supplied.

    Parameters
    ----------
    rc : array-like
    md_traj : array-like
    weights : array-like, None
    rc_bins : int, 20
    kde_bw : float, None

    Returns
    -------
    pdf : np.ndarray
    grid : np.ndarray

    Examples
    --------

    """
    # ensure rc and md_traj are numpy arrays before computation
    rc = np.array(rc)
    md_traj = np.array(md_traj)
    # calculate rc observable for each frame
    colvar_rc = np.sum(md_traj * rc, axis=1)

    if weights is not None:
        weights = np.array(weights)

    if kde_bw is not None:
        # evaluate pdf on a grid using KDE with Gaussian kernel
        grid = np.linspace(colvar_rc.min(), colvar_rc.max(), num=rc_bins)
        pdf = analysis.gaussian_density_estimation(colvar_rc, weights, grid, kde_bw)
        return pdf, grid

    # evaluate pdf using histograms
    pdf, bin_edges = analysis.histogram_density_estimation(colvar_rc, weights, rc_bins)
    # set grid points to center of bins
    bin_width = bin_edges[1] - bin_edges[0]
    grid = bin_edges[:-1] + bin_width

    return pdf, grid

# #####################################################################
# ###### Get binned RC value along unbiased traj for MaxCal ###########
# #####################################################################

def bin_max_cal(rc, md_traj, grid):
    """
    Calculate Reaction Coordinate bin index for each frame in max_cal_traj.

    Parameters
    ----------
    rc : np.ndarray
        Array of coefficients for one-dimensional reaction coordinate.
    md_traj : pd.DataFrame
        DataFrame storing COLVAR data from MaxCal trajectory.
    grid : np.ndarray
        Array of RC values at the center of each rc_bin.

    Returns
    ----------
    binned : np.ndarray

    """
    if rc is None or md_traj is None:
        return None

    # ensure rc and md_traj are ndarrays before computation
    rc = np.array(rc)
    md_traj = np.array(md_traj)
    # calculate rc observable for each frame
    colvar_rc = np.sum(md_traj * rc, axis=1)
    binned = analysis.find_closest_points(colvar_rc, grid)
    return binned

# #####################################################################
# ###### Calc transistion matrix from binned RC values from   #########
# ###### unbiased and probability from biased trajectory.     #########
# #####################################################################

def get_eigenvalues(binned_rc_traj, p, d, diffusivity=None):
    if diffusivity is None and binned_rc_traj is None:
        print("You must supply a MaxCal traj or diffusivity.")
        return

    n = diffusivity
    if binned_rc_traj is not None:
        # ensure binned traj is an ndarray before computation
        binned_rc_traj = np.array(binned_rc_traj)
        n = analysis.avg_neighbor_transitions(binned_rc_traj, d)

    with np.errstate(divide="ignore", invalid="ignore"):
        # ensure p is an ndarray before computation
        p = np.array(p)
        prob_matrix = analysis.probability_matrix(p, d)

    transition_matrix = n * prob_matrix
    eigenvalues = analysis.sorted_eigenvalues(transition_matrix)
    return eigenvalues

# ####################################################################
# ###### Evaluate a series of RCs or optimize from starting RC #######
# ####################################################################

def rc_eval(
    rc,
    probability_traj,
    sgoop_dict,
    weights=None,
    max_cal_traj=None,
    return_eigenvalues=False,
):
    # calculate prob for rc bins and binned rc value for MaxCal traj
    rc_bins = sgoop_dict.get("rc_bins")
    kde_bw = sgoop_dict.get("kde_bw")
    prob, grid = md_prob(rc, probability_traj, weights, rc_bins, kde_bw)
    # bin MaxCal trajectory. returns None if no trajectory is supplied.
    binned = bin_max_cal(rc, max_cal_traj, grid)
    # calculate spectral gap
    d = sgoop_dict.get("d")
    wells = sgoop_dict.get("wells")
    diffusivity = sgoop_dict.get("diffusivity")
    eigenvalues = get_eigenvalues(binned, prob, d, diffusivity)
    sg = analysis.spectral_gap(eigenvalues, wells)

    if return_eigenvalues:
        return sg, eigenvalues

    return sg

def optimize_rc(
    rc_0,
    probability_traj,
    sgoop_dict,
    weights=None,
    max_cal_traj=None,
    niter=50,
    annealing_temp=0.1,
    step_size=0.5,
):
    """
    Calculate optimal RC given an initial estimate for the coefficients
    and a Sgoop object containing a COLVAR file with CVs tracked over
    the course of a short unbiased simulation a COLVAR file with
    c(t) and CVs from a biased MetaD simulation.

    :param rc_0:
    :param single_sgoop:
    :param niter:
    :param annealing_temp:
    :return:
    """
    # pass trajectories and sgoop options through minimizer kwargs
    minimizer_kwargs = {
        "method": "BFGS",
        "options": {
            # "maxiter": 10
        },
        "args": (probability_traj, sgoop_dict, weights, max_cal_traj),
    }

    if max_cal_traj is None and sgoop_dict.get("diffusivity") is None:
        print(
            "A dynamical observable is required by the MaxCal framework. Please "
            "provide either a MaxCal trajectory or static diffusion constant."
        )
        return

    return opt.basinhopping(
        __opt_func,
        rc_0,
        niter=niter,
        T=annealing_temp,
        stepsize=step_size,
        minimizer_kwargs=minimizer_kwargs,
        disp=True,
        callback=__print_fun,
    )

def __opt_func(rc, metad_traj, sgoop_dict, weights, max_cal_traj):
    # normalize rc
    rc = rc / np.sqrt(np.sum(np.square(rc)))
    # calculate spectral gap for normalized rc
    sg = rc_eval(rc, metad_traj, sgoop_dict, weights, max_cal_traj)
    # return negative gap for minimization
    return -sg

def __print_fun(x, f, accepted):
    if accepted:
        rc = x / np.sqrt(np.sum(np.square(x)))
        print(f"RC with spectral gap {-f:} accepted.")
        print(", ".join([str(coeff) for coeff in rc]), "\n")
    else:
        print("")


import pandas as pd
import numpy as np
import torch
import utils
import config

import uncertainty_utilities
import pickle

def get_annotations_per_sample(params):
    annotations_per_vid = {}

    for annotator in range(1,18):
        try:
            data = utils.load_data(params, params.feature_set, params.emo_dim_set, params.normalize, params.label_preproc, params.norm_opts, params.segment_type, params.win_len, params.hop_len, save=params.cache, refresh=params.refresh, add_seg_id=params.add_seg_id, annotator=annotator)
        except Exception as e:
            print(f"Exception for annotator {annotator}: {e}")
            continue

        for partition in ["train", "devel", "test"]:
            metas = data[partition]["meta"]
            labels_ = data[partition]["label"]

            for emo_dim in range(labels_[0].shape[1]):
                labels = [l[:,emo_dim] for l in labels_]

                # NOTE iterate over samples and store them by their id
                for i, meta in enumerate(metas):

                    # NOTE meta list of shape (seq_len, 3); with 2nd dim: [vid_id,timestamp,segment_id]
                    vid_id = int(meta[0][0])

                    label_series = labels[i]

                    # NOTE only at the validation set for MuSe 2020 labels: for any reason, the raw annotations are always exactly by 3 timesteps longer than the fusioned annotations; therefore we have to cut it down, so that later the shapes of variances and subjectivites match
                    if partition == "devel" and not config.USE_2021_LABELS:
                        label_series = label_series[:-3]

                    # NOTE for training, samples get split up, so if we would not specify id, we would get multiple sub-series per sample and per annotator
                    if partition == "train":
                        first_timestamp = int(meta[0][1])
                        vid_id = f"{vid_id}_{first_timestamp}"
                    else:
                        vid_id = str(vid_id)

                    if vid_id not in annotations_per_vid.keys():
                        annotations_per_vid[vid_id] = {}
                    if emo_dim not in annotations_per_vid[vid_id].keys():
                        annotations_per_vid[vid_id][emo_dim] = []

                    annotations_per_vid[vid_id][emo_dim] += [label_series]

    return annotations_per_vid

def calculate_rolling_subjectivities(annotations_per_vid):
    subjectivities = {}
    for vid_id, emo_dims in annotations_per_vid.items():

        subjectivity_of_sample_all_emo_dims = []
        for emo_dim, annotations in emo_dims.items():

            subjectivity_of_sample = []
            for k, annotation_1 in enumerate(annotations):
                for j, annotation_2 in enumerate(annotations[k:]):
                    if k == j:
                        continue

                    # NOTE calculate rolling measuremt of subjectivity between each available annotation
                    rolling_window = 3
                    subjectivity = [
                        pd.Series(annotation_1[i - rolling_window : i]).corr(pd.Series(annotation_2[i - rolling_window : i]))
                            for i in range(rolling_window, len(annotation_1) + 1)
                        ]
                    subjectivity = [subjectivity[0]] * (rolling_window - 1) + subjectivity

                    # NOTE [0,0,0].corr([0,0,0]) = nan; therefore interpolate to fill nan
                    if np.isnan(subjectivity[0]):
                        subjectivity[0] = 0.
                    subjectivity = pd.Series(subjectivity).interpolate()

                    # NOTE maybe use rolling mean over subjectivity, that measurement becomes smoother
                    # rolling_mean_window = 3
                    # subjectivity = subjectivity.rolling(rolling_mean_window).mean()
                    # subjectivity[:rolling_mean_window-1] = subjectivity[rolling_mean_window-1]

                    subjectivity_of_sample += [subjectivity]

            assert len(subjectivity_of_sample) >= 1, f"too less annotations for sample {vid_id}"

            # NOTE calculate element-wise mean to get average subjectivity at each timestep
            subjectivity_of_sample = np.stack(subjectivity_of_sample).mean(axis=0)
            # NOTE convert to tensor and store to return
            subjectivity_of_sample = torch.Tensor(subjectivity_of_sample).float()

            # subjectivities[vid_id] = subjectivity_of_sample
            subjectivity_of_sample_all_emo_dims += [subjectivity_of_sample]

        # NOTE concatenate all dims of emotion (i.e. valence and arousal)
        subjectivity_of_sample_all_emo_dims = torch.Tensor(np.column_stack(subjectivity_of_sample_all_emo_dims))
        subjectivities[vid_id] = subjectivity_of_sample_all_emo_dims

    return subjectivities

def calculate_global_subjectivities(annotations_per_vid, window: int):
    subjectivities = {}
    max_len_global_subjectivities = None

    for vid_id, emo_dims in annotations_per_vid.items():

        subjectivity_of_sample_all_emo_dims = []
        for emo_dim, annotations in emo_dims.items():

            subjectivity_of_sample = []
            for k, annotation_1 in enumerate(annotations):
                for annotation_2 in annotations[k+1:]:

                    if window is None:
                        subjectivity = uncertainty_utilities.ccc_score(annotation_1, annotation_2)

                    else:
                        subjectivity = []
                        for i in range(0, len(annotation_1) + 1 - window, window):
                            if len(annotation_1[i : i + window]) < window:
                                continue
                            subjectivity += [uncertainty_utilities.ccc_score(annotation_1[i : i + window], annotation_2[i : i + window])]

                        if np.isnan(subjectivity[0]):
                            subjectivity[0] = 0.
                        subjectivity = pd.Series(subjectivity).interpolate().to_list()

                    subjectivity_of_sample += [subjectivity]

            if window is None:
                subjectivity_of_sample = np.mean(subjectivity_of_sample)
            else:
                subjectivity_of_sample = np.mean(subjectivity_of_sample, axis=0).tolist()

                if max_len_global_subjectivities is None or len(subjectivity_of_sample) > max_len_global_subjectivities:
                    max_len_global_subjectivities = len(subjectivity_of_sample)

            subjectivity_of_sample_all_emo_dims += [subjectivity_of_sample]

        if window is None:
            subjectivity_of_sample_all_emo_dims = torch.Tensor(subjectivity_of_sample_all_emo_dims)

        subjectivities[vid_id] = subjectivity_of_sample_all_emo_dims

    if not window is None:
        subjectivities_ = {}
        for vid_id, subjectivity_of_sample_all_emo_dims in subjectivities.items():
            subjectivity_of_sample_all_emo_dims_ = []
            for subjectivity_of_sample in subjectivity_of_sample_all_emo_dims:
                nans = [float("nan")] * (max_len_global_subjectivities - len(subjectivity_of_sample))
                subjectivity_of_sample_ = subjectivity_of_sample + nans
                subjectivity_of_sample_all_emo_dims_ += [subjectivity_of_sample_]
            subjectivities_[vid_id] = torch.Tensor(subjectivity_of_sample_all_emo_dims_)
        subjectivities = subjectivities_

    return subjectivities

def load_from_file(filename: str):
    with open(filename, "rb") as file:
        data = pickle.load(file)

    return data[0], data[1]

def save_to_file(short_term_subjectivities, global_subjectivities, filename: str):
    data = [short_term_subjectivities, global_subjectivities]
    with open(filename, "wb") as file:
        pickle.dump(data, file, protocol=pickle.HIGHEST_PROTOCOL)

def build_from_scratch(params, filename: str):
    print("Calculating subjectivities among annotators from sratch...")
    annotations_per_vid = get_annotations_per_sample(params)
    short_term = calculate_rolling_subjectivities(annotations_per_vid)
    globals = calculate_global_subjectivities(annotations_per_vid, params.global_uncertainty_window)
    print("Subjectivities calculated.")

    if params.save_subjectivity_to_file:
        save_to_file(short_term, globals, filename)
        print("Subjectivities saved to file.")

    return short_term, globals

def calculate_subjectivities(params):
    filename = config.DATA_FOLDER + "/subjectivities_among_annotations.pickle"

    if params.load_subjectivity_from_file:
        try:
            short_term, globals = load_from_file(filename)
            print("Subjectivities deserialized from file.")
        except:
            short_term, globals = build_from_scratch(params, filename)

    else:
        short_term, globals = build_from_scratch(params, filename)

    return short_term, globals


#!/usr/bin/env python
import glob
import multiprocessing.pool
import os
import tarfile
import urllib.request
import warnings
import stat
import torch

from setuptools import setup, find_packages, distutils
from setuptools.command.install import install
from distutils import log
from torch.utils.cpp_extension import BuildExtension
from torch.utils.cpp_extension import CppExtension, include_paths

def download_extract(url, dl_path):
    if not os.path.isfile(dl_path):
        # Already downloaded
        urllib.request.urlretrieve(url, dl_path)
    if dl_path.endswith(".tar.gz") and os.path.isdir(dl_path[:-len(".tar.gz")]):
        # Already extracted
        return
    tar = tarfile.open(dl_path)
    tar.extractall('third_party/')
    tar.close()

FILES_TO_MARK_EXECUTABLE = ["flac-linux-x86", "flac-linux-x86_64", "flac-mac", "flac-win32.exe"]

# Does gcc compile with this header and library?
def compile_test(header, library):
    dummy_path = os.path.join(os.path.dirname(__file__), "dummy")
    command = "bash -c \"g++ -include " + header + " -l" + library + " -x c++ - <<<'int main() {}' -o " + dummy_path \
              + " >/dev/null 2>/dev/null && rm " + dummy_path + " 2>/dev/null\""
    return os.system(command) == 0

compile_args = ['-O3', '-DKENLM_MAX_ORDER=6', '-std=c++14', '-fPIC']
ext_libs = []
if compile_test('zlib.h', 'z'):
    compile_args.append('-DHAVE_ZLIB')
    ext_libs.append('z')

if compile_test('bzlib.h', 'bz2'):
    compile_args.append('-DHAVE_BZLIB')
    ext_libs.append('bz2')

if compile_test('lzma.h', 'lzma'):
    compile_args.append('-DHAVE_XZLIB')
    ext_libs.append('lzma')

third_party_libs = ["kenlm", "openfst-1.6.7/src/include", "ThreadPool", "boost_1_67_0", "utf8"]
compile_args.extend(['-DINCLUDE_KENLM', '-DKENLM_MAX_ORDER=6'])
lib_sources = glob.glob('third_party/kenlm/util/*.cc') + glob.glob('third_party/kenlm/lm/*.cc') + glob.glob(
    'third_party/kenlm/util/double-conversion/*.cc') + glob.glob('third_party/openfst-1.6.7/src/lib/*.cc')
lib_sources = [fn for fn in lib_sources if not (fn.endswith('main.cc') or fn.endswith('test.cc'))]

third_party_includes = ["D:\\SpeechRecognition\\AutomaticSpeechRecognitionSystem\\neuralnet", "D:\\SpeechRecognition\\AutomaticSpeechRecognitionSystem\\ctcdecode"]
ctc_sources = glob.glob('ctcdecode/src/*.cpp')

extension = CppExtension(
    name='ctcdecode._ext.ctc_decode',
    package=True,
    with_cuda=False,
    sources=ctc_sources + lib_sources,
    include_dirs=third_party_includes + include_paths(),
    libraries=ext_libs,
    extra_compile_args=compile_args,
    language='c++'
)

# monkey-patch for parallel compilation
# See: https://stackoverflow.com/a/13176803
def parallelCCompile(self,
                     sources,
                     output_dir=None,
                     macros=None,
                     include_dirs=None,
                     debug=0,
                     extra_preargs=None,
                     extra_postargs=None,
                     depends=None):
    # those lines are copied from distutils.ccompiler.CCompiler directly
    macros, objects, extra_postargs, pp_opts, build = self._setup_compile(
        output_dir, macros, include_dirs, sources, depends, extra_postargs)
    cc_args = self._get_cc_args(pp_opts, debug, extra_preargs)

    # parallel code
    def _single_compile(obj):
        try:
            src, ext = build[obj]
        except KeyError:
            return
        self._compile(obj, src, ext, cc_args, extra_postargs, pp_opts)

    # convert to list, imap is evaluated on-demand
    thread_pool = multiprocessing.pool.ThreadPool(os.cpu_count())
    list(thread_pool.imap(_single_compile, objects))
    return objects

# hack compile to support parallel compiling
distutils.ccompiler.CCompiler.compile = parallelCCompile

class BuildExtension(install):
    def run(self):
        install.run(self)  # do the original install steps

        # mark the FLAC executables as executable by all users (this fixes occasional issues when file permissions get messed up)
        for output_path in self.get_outputs():
            if os.path.basename(output_path) in FILES_TO_MARK_EXECUTABLE:
                log.info("setting executable permissions on {}".format(output_path))
                stat_info = os.stat(output_path)
                os.chmod(
                    output_path,
                    stat_info.st_mode |
                    stat.S_IRUSR | stat.S_IXUSR |  # owner can read/execute
                    stat.S_IRGRP | stat.S_IXGRP |  # group can read/execute
                    stat.S_IROTH | stat.S_IXOTH  # everyone else can read/execute
                )

setup(
    name="CTC_SpeechRecognition",
    version="1.0.0",
    description="Automatic Speech Recognition System with PyTorch and CTC Decoder based on Paddle Paddle's implementation",
    url="https://github.com/DvelopedByIkshaan/CTC-SpeechRecognition",
    author="Ikshaan Gupta",
    author_email="ikshaan@gmail.com",
    # Exclude the build files.
    packages=["CTC_SpeechRecognition"],
    include_package_data=True,
    include_dirs=third_party_includes,
    cmdclass={"install": BuildExtension}
)


from io import BytesIO
import json
import logging

import click

from pywcmp.wcmp2.kpi import (
    WMOCoreMetadataProfileKeyPerformanceIndicators as wcmp_kpis2
)
from pywcmp.util import (get_cli_common_options, parse_wcmp, setup_logger,
                         urlopen_)

LOGGER = logging.getLogger(__name__)

@click.group()
def kpi():
    """key performance indicators"""
    pass

@click.command()
@click.pass_context
@get_cli_common_options
@click.argument('file_or_url')
@click.option('--summary', '-s', is_flag=True, default=False,
              help='Provide summary of KPI test results')
@click.option('--kpi', '-k', help='KPI to run, default is all')
def validate(ctx, file_or_url, summary, kpi, logfile, verbosity):
    """run key performance indicators"""

    setup_logger(verbosity, logfile)

    if file_or_url.startswith('http'):
        content = BytesIO(urlopen_(file_or_url).read())
    else:
        content = file_or_url

    click.echo(f'Validating {file_or_url}')

    try:
        data = parse_wcmp(content)
    except Exception as err:
        raise click.ClickException(err)

    cls = wcmp_kpis2

    kpis = cls(data)

    try:
        kpis_results = kpis.evaluate(kpi)
    except ValueError as err:
        raise click.UsageError(f'Invalid KPI {kpi}: {err}')

    if not summary or kpi is not None:
        click.echo(json.dumps(kpis_results, indent=4))
    else:
        click.echo(json.dumps(kpis_results['summary'], indent=4))

kpi.add_command(validate)


import pandas as pd
import numpy as np
import os
from itertools import product
from scipy.interpolate import interp1d
from datetime import datetime

import netCDF4 as nc
from geopy.distance import distance

from src.constants import MMC
from src.framework import define_equation_elements, define_state_elements

def load_poc_data():

    metadata = pd.read_csv('../../../data/geotraces/poc_conc.csv',
                           usecols=('GTNum', 'GTStn', 'CastType',
                                    'CorrectedMeanDepthm',
                                    'Latitudedegrees_north',
                                    'Longitudedegrees_east',
                                    'DateatMidcastGMTyyyymmdd'))

    # SPM_SPT_pM has NaN for intercal samples, useful for dropping later
    cols = ('SPM_SPT_ugL', 'POC_SPT_uM', 'POC_LPT_uM')

    values = pd.read_csv('../../../data/geotraces/poc_conc.csv', usecols=cols)
    errors = pd.read_csv('../../../data/geotraces/poc_sd.csv', usecols=cols)
    flags = pd.read_csv('../../../data/geotraces/poc_flag.csv', usecols=cols)

    data = merge_poc_data(metadata, values, errors, flags)
    data.dropna(inplace=True)
    data = data.loc[:, ~data.columns.str.startswith('SPM_SPT_ugL')]

    data = data[~data['station'].isin((1, 3, 18.3))]  # exclude stations 1, 18.3
    data = data[data['depth'] < 1000]  # don't need data below 1km

    return data

def merge_poc_data(metadata, values, errors, flags):

    rename_cols = {'GTStn': 'station', 'CastType': 'cast',
                   'CorrectedMeanDepthm': 'depth',
                   'POC_SPT_uM': 'POCS', 'POC_LPT_uM': 'POCL',
                   'Latitudedegrees_north': 'latitude',
                   'Longitudedegrees_east': 'longitude',
                   'DateatMidcastGMTyyyymmdd': 'datetime'}

    for df in (metadata, values, errors, flags):
        df.rename(columns=rename_cols, inplace=True)

    data = pd.merge(metadata, values, left_index=True, right_index=True)
    data = pd.merge(data, errors, left_index=True, right_index=True,
                    suffixes=(None, '_unc'))
    data = pd.merge(data, flags, left_index=True, right_index=True,
                    suffixes=(None, '_flag'))

    return data

def poc_by_station():

    df = load_poc_data()
    data = {}
    maxdepth = 600

    for s in df['station'].unique():
        raw = df[df['station'] == s].copy()
        raw.sort_values('depth', inplace=True, ignore_index=True)
        cleaned = clean_by_flags(raw)
        data[int(s)] = cleaned.loc[cleaned['depth'] < maxdepth]

    return data

def clean_by_flags(raw):

    cleaned = raw.copy()
    flags_to_clean = (3, 4)

    tracers = ('POCS', 'POCL')
    for ((i, row), t) in product(cleaned.iterrows(), tracers):
        if row[f'{t}_flag'] in flags_to_clean:
            poc = cleaned.at[i - 1, t], cleaned.at[i + 1, t]
            depth = cleaned.at[i - 1, 'depth'], cleaned.at[i + 1, 'depth']
            interp = interp1d(depth, poc)
            cleaned.at[i, t] = interp(row['depth'])
            cleaned.at[i, f'{t}_unc'] = cleaned.at[i, t]

    return cleaned

def load_nc_data(dir):

    datainfo = {'modis': {'ext': '.nc', 'dateidx': 3},
                'cbpm': {'ext': '.hdf', 'dateidx': 1}}

    path = f'../../../data/geotraces/{dir}'
    filenames = [f for f in os.listdir(path) if datainfo[dir]['ext'] in f]
    data = {}

    for f in filenames:
        date = f.split('.')[datainfo[dir]['dateidx']]
        if dir == 'cbpm':
            date = datetime.strptime(date, '%Y%j').strftime('%Y%m%d')
        data[date] = nc.Dataset(os.path.join(path, f))

    return data

def extract_nc_data(poc_data, dir):

    var_by_station = {}

    nc_data = load_nc_data(dir)
    nc_dates = [datetime.strptime(d,'%Y%m%d') for d in nc_data]

    if dir == 'cbpm':
        nc_lats = [90 - x*(1/12) - 1/24 for x in range(2160)]
        nc_lons = [x*(1/12) - 180 + 1/24 for x in range(4320)]

    for s in poc_data:

        df = poc_data[s].copy()
        row = df[df['cast'] == 'S'].iloc[0]

        date = datetime.strptime(row['datetime'], '%m/%d/%y %H:%M')
        station_coord = np.array((row['latitude'], row['longitude']))
        prev_nc_dates = [d for d in nc_dates if d <= date]
        nc_date = min(prev_nc_dates, key=lambda x: abs(x - date))
        nc_8day = nc_data[nc_date.strftime('%Y%m%d')]

        if dir == 'cbpm':
            var_name = 'npp'
            var_8day = nc_8day.variables[var_name]
        if dir == 'modis':
            var_name = 'Kd'
            var_8day = nc_8day.variables['MODISA_L3m_KD_8d_4km_2018_Kd_490'][0]
            nc_lats = list(nc_8day.variables['lat'][:])
            nc_lons = list(nc_8day.variables['lon'][:])

        close_nc_lats = [
            l for l in nc_lats if abs(station_coord[0] - l) < 1]
        close_nc_lons = [
            l for l in nc_lons if abs(station_coord[1] - l) < 1]
        nc_coords = list(product(close_nc_lats, close_nc_lons))
        distances = [distance(ncc, station_coord) for ncc in nc_coords]
        nc_coords_sorted = [
            x for _, x in sorted(zip(distances, nc_coords))]

        j = 0
        while True:
            nc_lat_index = nc_lats.index(nc_coords_sorted[j][0])
            nc_lon_index = nc_lons.index(nc_coords_sorted[j][1])
            station_var = var_8day[nc_lat_index, nc_lon_index]
            if station_var > -9999:
                break
            j += 1

        var_by_station[row['station']] = station_var

    return var_by_station

def load_mixed_layer_depths():

    mld_df = pd.read_csv('../../../data/geotraces/mld.csv')
    mld_dict = dict(zip(mld_df['station'], mld_df['depth']))

    return mld_dict

def load_Th_fluxes():

    df = pd.read_csv('../../../data/geotraces/sinkingflux_Th.csv',
                     usecols=('station', 'depth', 'flux'))

    return df

def get_median_POCS():

    poc = poc_by_station()
    data = pd.DataFrame(columns=['depth', 'POCS'])
    for  df in poc.values():
        data = pd.concat([data, df], join='inner', ignore_index=True)

    median = np.median(data['POCS'])

    return median

def get_station_Th_fluxes(grid, max_depth, station, flux_df):

    flux_layers = []
    flux_depths = []
    flux_vals = []
    s_df = flux_df.loc[(flux_df['station'] == station) & (flux_df['depth'] < max_depth)]
    for i, depth in enumerate(grid):
        nearby = s_df.iloc[(s_df['depth'] - depth).abs().argsort()[:1]].iloc[0]
        if nearby['flux'] > 0:
            flux_layers.append(i)
            flux_depths.append(nearby['depth'])
            flux_vals.append(nearby['flux'])
    fluxes = pd.DataFrame(list(zip(flux_layers, flux_depths, flux_vals)), columns=['layer', 'depth', 'flux'])

    return fluxes

def get_station_data(poc_data, params, ez_depths, flux_constraint=False):

    d = {s: {} for s in poc_data}
    mixed_layer_depths = load_mixed_layer_depths()
    if flux_constraint:
        max_depth = 620
        flux_df = load_Th_fluxes()

    for s in poc_data.keys():
        grid = tuple(poc_data[s]['depth'].values)
        layers = tuple(range(len(grid)))
        zg = min(grid, key=lambda x:abs(x - ez_depths[s]))  # grazing depth
        tracers = define_tracers(poc_data[s])
        d[s]['mld'] = mixed_layer_depths[s]
        d[s]['grid'] = grid
        d[s]['latitude'] = poc_data[s].iloc[0]['latitude']
        d[s]['longitude'] = poc_data[s].iloc[0]['longitude']
        d[s]['layers'] = layers
        d[s]['zg'] = zg
        d[s]['ezd'] = ez_depths[s]
        d[s]['umz_start'] = grid.index(zg) + 1
        d[s]['tracers'] = tracers
        if flux_constraint:
            d[s]['Th_fluxes'] = get_station_Th_fluxes(grid, max_depth, s, flux_df)
        else:
            d[s]['Th_fluxes'] = None
        d[s]['e_elements'] = define_equation_elements(tracers, layers, Th_fluxes=d[s]['Th_fluxes'])
        d[s]['s_elements'] = define_state_elements(tracers, params, layers, Th_fluxes=d[s]['Th_fluxes'])

    return d

def define_tracers(data):

    tracers = {'POCS': {}, 'POCL': {}}

    for t in tracers:
        tracers[t]['prior'] = data[t]
        tracers[t]['prior_e'] = data[f'{t}_unc']

    return tracers

def define_residuals(prior_error, gamma):

    residuals = {'POCS': {}, 'POCL': {}}

    for tracer in residuals:
        residuals[tracer]['prior'] = 0
        residuals[tracer]['prior_e'] = gamma * prior_error

    return residuals

def set_param_priors(params, Lp_prior, Po_prior, B3_prior, mc_params):

    def set_prior(param_name, prior, error):

        params[param_name]['prior'] = prior
        params[param_name]['prior_e'] = error

    set_prior('B2p', mc_params['B2p'], mc_params['B2p'])
    set_prior('Bm2', mc_params['Bm2'], mc_params['Bm2'])
    set_prior('Bm1s', mc_params['Bm1s'], mc_params['Bm1s'])
    set_prior('Bm1l', mc_params['Bm1l'], mc_params['Bm1l'])
    set_prior('ws', mc_params['ws'], mc_params['ws'])
    set_prior('wl', mc_params['wl'], mc_params['wl'])
    set_prior('Po', Po_prior, Po_prior*0.5)
    set_prior('Lp', Lp_prior, Lp_prior*0.5)
    set_prior('B3', B3_prior, B3_prior*0.5)
    set_prior('a', 0.3, 0.3*0.5)
    set_prior('zm', 500, 500*0.5)

def define_param_uniformity():

    param_uniformity = {}

    nonuniform_params = ('B2p', 'Bm2', 'Bm1s', 'Bm1l', 'ws', 'wl')
    uniform_params = ('Po', 'Lp', 'B3', 'a', 'zm')

    for p in nonuniform_params:
        param_uniformity[p] = {'dv': True}

    for p in uniform_params:
        param_uniformity[p] = {'dv': False}

    return param_uniformity

def get_Lp_priors(poc_data):

    Kd = extract_nc_data(poc_data, 'modis')
    Lp_priors = {station: 1/k for station, k in Kd.items()}

    return Lp_priors

def get_ez_depths(Lp_priors):

    depths = {station: l*np.log(100) for station, l in Lp_priors.items()}

    return depths

def get_Po_priors(poc_data, Lp_priors, npp_data):
    """Calculate Po priors at each station.

    Args:
        poc_data (_type_): _description_
        Lp_priors (_type_): _description_
        npp_data (_type_): _description_

    Returns:
        _type_: _description_

    Full equation for Po is Po = npp / [Lp * (1 - exp[-ez_depth / Lp])],
    but ez_depth = Lp * ln(100), so (1-exp[]) simplifies to 0.99.
    """
    Po_priors = {s: ((npp_data[s] / MMC)
                     / (Lp_priors[s] * 0.99)) for s in poc_data}

    return Po_priors

def get_B3_priors(npp_data):

    B3_priors = {}

    for s in npp_data:
        B3_priors[s] = 10**(-2.42 + 0.53*np.log10(npp_data[s]))

    return B3_priors


from __future__ import division, print_function

import os

import numpy as np
import pandas as pd
import seaborn as sns

from gabor_experiment.misc import FIGS_DIRS
from gabor_experiment.misc import REPS_DIRS
from gabor_experiment.misc import RESULTS_DIR
from gabor_experiment.misc import STIMULI_DIRS
from gabor_experiment.misc import STIMULI_POSTFIXES
from gabor_experiment.misc import TITLES
import matplotlib.pyplot as plt
from utils.misc import LAYER_NAMES

def create_and_save_figures(just_original=True):
    """Create and save figures."""
    sns.set()
    sns.set(style="whitegrid")
    sns.set(font_scale=1.6, style="ticks")

    for stimuli_dir, reps_dir, figs_dir, stimuli_postfix, title \
            in zip(STIMULI_DIRS, REPS_DIRS, FIGS_DIRS,
                   STIMULI_POSTFIXES, TITLES):
        # Filename to load/save results database from/to:
        results_filename = RESULTS_DIR + 'pca' + stimuli_postfix + '.csv'
        correlations_results_filename = RESULTS_DIR + \
            'correlations_pca' + stimuli_postfix + '.csv'

        try:
            df = pd.read_csv(correlations_results_filename)
        except FileNotFoundError:
            print('Please run pca_permutations.py to have all files required'
                  'to create the figures.')
            continue

        # df.to_csv(correlations_results_filename)

        sns.set_palette(sns.color_palette('muted'))
        colors = ["bright sky blue", "purple"]
        sns.set_palette(sns.xkcd_palette(colors))

        fig, ax = plt.subplots()
        fig.subplots_adjust(bottom=0.15)

        ax = df.plot.line(x='Layer', y=['Frequency',
                                        'Orientation'],
                          ax=ax, lw=3,  alpha=0.75,
                          legend=False)
        # ax.set_ylim(0.45,1)
        # plt.legend(loc=3)
        # y_min = 0.39
        # y_max = 1.005
        # plt.axis([-1, 26, y_min, y_max])
        #
        # sns.despine(offset=10, trim=True)
        # ax.set_xlabel('Network Layer', size=20)
        # ax.set_ylabel('Correlation Coefficient', size=20)
        mean_permuted_frequency = []
        mean_permuted_orientation = []
        for l, layer_name in enumerate(LAYER_NAMES):
            try:
                del permutation_df
            except NameError:
                None
            permutations_filename = (RESULTS_DIR + 'permutations_pca/'
                                     + 'permuted_correlations'
                                     + stimuli_postfix + '_layer_' + str(l)
                                     + '.csv')

    #         print(permutations_filename)

            try:
                permutation_df = pd.read_csv(permutations_filename,
                                             index_col=0)
            except FileNotFoundError:
                print('File missing:', permutations_filename)
                print('Please run pca_permutations.py to have all files'
                      ' required to create the figures.')
                continue

            mean_permuted_frequency.append(
                permutation_df['Permuted Frequency'].mean())
            mean_permuted_orientation.append(
                permutation_df['Permuted Orientation'].mean())

    #         print(mean_permuted_frequency)
    #         print(mean_permuted_orientation)
        ax.plot(mean_permuted_orientation, color='#aaaaaa', lw=3,
                linestyle='--', label='Mean Permuted Orientation')
        ax.plot(mean_permuted_frequency, color='#000000', lw=3,
                linestyle=':', label='Mean Permuted Frequency')
        plt.legend(loc=3, frameon=False)
        y_min = 0.1
        y_max = 1.005
    #     plt.axis([-1, 26, y_min, y_max])
        sns.despine(offset=10, trim=True)
        ax.set_xlabel('Network Layer', size=20)
        ax.set_ylabel('Correlation Coefficient', size=20)

        general_figs_dir = os.sep.join(figs_dir.split(os.sep)[:-2])
        general_figs_dir = os.sep.join(
            [general_figs_dir, figs_dir.split(os.sep)[-2]])

        general_figs_dir = os.sep.join(figs_dir.split(os.sep)[:-2])
        general_figs_dir = os.sep.join(
            [general_figs_dir, figs_dir.split(os.sep)[-2]])

        ax.tick_params(axis='both', which='both', top='off', right='off')

        fig.savefig(general_figs_dir + '_correlation.pdf', bbox_inches='tight')
        fig.savefig(general_figs_dir + '_correlation.png', bbox_inches='tight')
        # if 'original' in figs_dir:
        # print(df[['Frequency', 'Orientation']])
        # print(df)

        if just_original:
            print(general_figs_dir + '_correlation.pdf')
            return general_figs_dir + '_correlation.pdf'

if __name__ == '__main__':
    create_and_save_figures(just_original=False)


import pygame

pygame.init()

screen = pygame.display.set_mode((1080,720))
p1col = (255,122,122)
p2col = (0,0,255)
def num_to_coords(x,y):
    if x == 1 and y == 1:
        return 7
    if x == 1 and y == 2:
        return 4
    if x == 1 and y == 3:
        return 1
    if x == 2 and y == 1:
        return 8
    if x == 2 and y == 2:
        return 5
    if x == 2 and y == 3:
        return 2
    if x == 3 and y == 1:
        return 9
    if x == 3 and y == 2:
        return 6
    if x == 1 and y == 1:
        return 3
def num_to_x(num):
    if num == 1 or num == 4 or num == 7:
        return 1
    if num == 2 or num == 5 or num == 8:
        return 2
    if num == 3 or num == 6 or num == 9:
        return 3
def num_to_y(num):
    if num == 1 or num == 2 or num == 3:
        return 3
    if num == 4 or num == 5 or num == 6:
        return 2
    if num == 7 or num == 8 or num == 9:
        return 1
def make_map():
    font = pygame.font.Font('freesansbold.ttf',64)
    p1txt = font.render('PLAYER',True,(10,10,10))
    p2txt = font.render('COMPUTER',True,(10,10,10))
    pygame.draw.rect(screen,p1col,(0,0,540,120))
    pygame.draw.rect(screen,p2col,(540,0,540,120))
    screen.blit(p1txt,(10,10))
    screen.blit(p2txt,(680,10))
    pygame.draw.rect(screen,(122,122,122),(235,110,610,610))
    pygame.draw.rect(screen,(0,0,0),(245,120,590,590))
    n = 1
    while n <= 2:
        pygame.draw.rect(screen,(122,122,122),(245,110+n*200,600,10))
        pygame.draw.rect(screen,(122,122,122),(235+n*200,110,10,600))

        n+=1
def cross(num):
    n = 0
    while n <= 156:
        pygame.draw.circle(screen, p1col, ((num_to_x(num) - 1) * 200 + 262+n, ((num_to_y(num) - 1)) * 200 + 137+n), 12)
        pygame.draw.circle(screen, p1col, ((num_to_x(num) - 1) * 200 + 262+n, ((num_to_y(num) - 1)) * 200 + 293-n), 12)
        n += 1
def circle(num):
    pygame.draw.circle(screen, p2col, (340 + (num_to_x(num)-1)*200, 215 + (num_to_y(num)-1)*200), 85)
    pygame.draw.circle(screen, (0,0,0), (340 + (num_to_x(num)-1)*200, 215 + (num_to_y(num)-1)*200), 61)

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    pygame.draw.rect(screen, (0, 0, 0), (0, 0, 1080, 720))
    make_map()
    circle(6)
    cross(1)

    pygame.display.flip()

pygame.quit()


import xml.etree.ElementTree as ET
# use this file
xmlDoc = ET.parse("Files chapter 9/plants.xml")
# Find the root <catalog> in this case
root = xmlDoc.getroot()
count = 0

# Lets you look in the child elements of "plant"
for common in root.findall("plant"):
    if common[3].text == "SUN": # oefb - Ony show plant that are best in the sun
        count += 1
        name = common[0].text # .text to print the words inside
        botanical = common[1].text
        print("plant", str(count), ":", name, " (" + botanical + ")")


import geopandas as gpd
import re
from shapely import wkt
import folium
import streamlit as st
import streamlit_folium

try:
    uploaded_file = st.file_uploader("Please Upload GeoJSON or ZIP Shapefiles here")
    geofence_gdf = gpd.read_file(uploaded_file)
    geofence_gdf['str_geom'] = geofence_gdf.geometry.apply(lambda x: wkt.dumps(x))

    # CONVERT QGIS GEOMETRY TO GEOTOOLS COORDINATES
    for i, row in geofence_gdf.iterrows():
       # EXTRACTING THE COORDINATES FROM STR GEOM USING REGEX
       coordinates_match = re.search(r'POLYGON \(\((.*?)\)\)', row['str_geom'])
    if coordinates_match:
        coordinates_text = coordinates_match.group(1)
        coordinates = coordinates_text.split(', ')

          #REFORMATING THE COORDINATES ACCORDING GEOTOOLS
        reformatted_coordinates = []
        for coord in coordinates:
            x,y = coord.split(' ')
            reformatted_coordinates.append(f'{y}, {x}')

        #JOINING THE REFORMATTED COORDINATES WITH COMMAS
            result_string = ', '.join(reformatted_coordinates)
            geofence_gdf.at[i, 'geotools_coordinates'] = result_string
        else:
            st.write("invalid input string")

    output = st.text_input('File Save As', )
    csv=geofence_gdf.to_csv()
    st.download_button(
      label="Download data as CSV",
      data=csv,
      file_name = output+'.csv',
      mime='text/csv',
  )

except (TypeError, NameError, AttributeError):
  pass


import re, sys, logging, argparse, traceback, ntpath
import matplotlib.pyplot as plt
import json, os, glob, subprocess
import numpy as np
import pandas as pd
import importlib.machinery
from pprint import pformat
from src.utils.logging.logging_segue import create_logger
from src.utils.video.multilevel_video_factory import MultilevelVideoFactory
from src.consts.video_configs_consts import *
from src.consts.manifest_representation_consts import *
from src.consts.splitter_consts import *
from src.consts.augmenter_consts import *
from src.utils.video.level import Level
from src.utils.video_factory import Video, FullVideo, EXTENSION

def pmkdir(kdir):
    if not os.path.exists(kdir):
        os.makedirs(kdir)

def read_configs(configs):
    with open(configs, 'r') as fin:
        data = json.load(fin)
    return data

def empty(folder):
    for filename in os.listdir(folder):
        file_path = os.path.join(folder, filename)
        try:
            if os.path.isfile(file_path) or os.path.islink(file_path):
                os.unlink(file_path)
            elif os.path.isdir(file_path):
                shutil.rmtree(file_path)
        except Exception as e:
            print("Error in empyting log folder")
            sys.exit(-1)

class SimulationFileHandler:
    def __init__(self, args):
        ## parameter check

        assert os.path.exists(args.video_configs), "Video configuration file doesn't exist"
        print(args.segments_handler_configs)
        assert os.path.exists(args.segments_handler_configs), "Grouper configuration file doesn't exist"

        assert os.path.exists(args.segments_structure_in), "Video RAW segments do not exists"

        self.verbose = args.verbose

        self.logs_dir = args.logs_dir
        if os.path.exists(args.logs_dir):
            empty(args.logs_dir)
        pmkdir(args.logs_dir)

        log_file = os.path.join(args.logs_dir, 'make_simulation_file.log')
        self.logger = create_logger('Make Simulation File: Main', log_file, verbose=args.verbose)

        self.logger.info("Simulation file handler module initialized")
        self.logger.info("Logs file stored in {}".format(log_file))

        ## Outputs:
        self.simulation_file_out = args.simulation_file_out
        self.logger.info("Simulation file stored in {}".format(self.simulation_file_out))
        pmkdir(os.path.dirname(self.simulation_file_out))

        self.csv_std_segments_in = pd.read_csv(args.csv_std_segments_in, keep_default_na=False)
        try:
            self.csv_aug_segments_in = pd.read_csv(args.csv_aug_segments_in)
        except:
            self.logger.warning("aug chunks {} does not exist! Continuing without augmentation".format(args.csv_aug_segments_in))
            self.csv_aug_segments_in = None

        self.logger.info("Reference segments csv in: {}".format(args.csv_std_segments_in))
        self.logger.info("Augmented segments csv in: {}".format(args.csv_aug_segments_in))

        self.csv_std_segments_out = args.csv_std_segments_out
        self.logger.info("CSV of standard segments will be stored in {}".format(self.csv_std_segments_out))
        pmkdir(os.path.dirname(self.csv_std_segments_out))

        if args.csv_aug_segments_out:
            self.csv_aug_segments_out = args.csv_aug_segments_out
            self.logger.info("CSV of augmented segments will be stored in {}".format(self.csv_aug_segments_out))

        ## creation of figsdir
        pmkdir(args.figs_dir)
        self.figs_dir = args.figs_dir
        self.logger.info("Figs will be stored in {}".format(self.figs_dir))

        self.video_data = read_configs(args.video_configs)
        self.logger.info("Accounted video data stored = {}".format(args.video_configs))
        self.logger.debug("Accounted video data {}".format(self.video_data))

        self.segments_handler_data = read_configs(args.segments_handler_configs)
        self.logger.info("Accounted augmenter data stored = {}".format(self.segments_handler_data))
        self.logger.debug("Accounted augmenter data {}".format(self.segments_handler_data))

        self.simulation_file_data = read_configs(args.sim_file_configs)
        self.logger.info("Accounted simulation file data stored = {}".format(args.sim_file_configs))
        self.logger.debug("Accounted simulation file data {}".format(self.simulation_file_data))

        self.segments_structure_in = read_configs(args.segments_structure_in)
        self.logger.info("Accounted segments boundaries stored = {}".format(args.segments_structure_in))

        video_original_path = self.video_data[K_VIDEO_PATH]
        assert os.path.exists(video_original_path), "Original video does not exists"

        self.logger.debug("Creating raw video from {}".format(video_original_path))
        self.raw_video  = FullVideo(Video(video_original_path, self.logs_dir, verbose=self.verbose))

        self.logger.info("Raw video from {} created succesfully".format(video_original_path))
        self.logger.debug("Video {} has a total of {} frames for {} fps".format(self.raw_video.video().video_path(),
                                                                                self.raw_video.video().load_total_frames(),
                                                                                self.raw_video.video().load_fps()))
        self.resolutions = self.video_data[K_RESOLUTIONS].split()
        self.logger.debug("Original video resolution = {}".format(self.resolutions))

        self.rescaled_video_template = args.rescaled_video_template
        self.logger.debug("Rescaled video template is {}".format(self.rescaled_video_template))

        self.multires_video = None

        self.rescaled_videos = []
        for res in self.resolutions:
            video_path = self.rescaled_video_template.format(res)
            self.logger.debug("Looking for {}".format(video_path))
            assert os.path.exists(video_path), video_path
            self.rescaled_videos.append(FullVideo(Video(video_path, self.logs_dir, verbose=self.verbose)))
            self.logger.info("Video {} found".format(video_path))

        splitting_module = self.simulation_file_data[K_SPLITTING_MODULE].replace('/','.').replace('.py', '')
        splitting_class = self.simulation_file_data[K_SPLITTING_CLASS]
        self.logger.info("Splitting module = {}".format(splitting_module))
        self.logger.info("Splitting class = {}".format(splitting_class))

        try:
            splitting_module_args = self.simulation_file_data[K_SPLITTING_MODULE_ARGS]
            self.logger.debug("Splitting module args = {}".format(splitting_module_args))
        except:
            splitting_module_args = {}
            self.logger.warning("No splitting module argument has been passed")

        try:
            self.logger.debug("Retrieving splitting policy")
            SplittingPolicy = getattr(importlib.import_module(splitting_module), splitting_class)
            self.logger.info("Splitting policy retrieved correctly")
        except:
            self.logger.error("{} doesn't contain class name {}, or some errors are present in module".format(splitting_module, splitting_class))
            self.logger.exception("message")
            sys.exit(-1)

        try:
            self.logger.debug("Instantiating splitting policy")
            self.splitting_policy_std = SplittingPolicy(   self.raw_video,
                                                           self.rescaled_videos,
                                                           os.path.dirname(self.rescaled_video_template),
                                                           self.segments_structure_in,
                                                           splitting_module_args,
                                                           args.verbose,
                                                           args.logs_dir,
                                                           args.figs_dir)

            self.logger.info("Splitting policy instantiated correctly")
        except:
            self.logger.error("Something went wrong in the instantiation of the class")
            self.logger.exception("message")
            sys.exit(-1)

        self.augmented_videos = []
        self.splitting_policy_aug = None
        try:
            assert self.csv_aug_segments_in is not None
            augmenter_module_args = self.segments_handler_data[K_AUGMENTER_MODULE_ARGS]
            self.logger.debug("Augmenter module args = {}".format(augmenter_module_args))

            augmenter_encoding_name =  augmenter_module_args[K_AUGMENTER_ENCODING_NAME]
            self.logger.info("Looking for augmented segments in {}".format(augmenter_encoding_name))

            for res in self.resolutions:
                video_path = args.augmented_video_template.format(augmenter_encoding_name, res, EXTENSION)
                self.logger.debug("Looking for {}".format(video_path))
                if not os.path.exists(video_path):
                    self.logger.warning("{} does not exists. Skipping..".format(video_path))
                    continue

                self.augmented_videos_template = os.path.dirname(args.augmented_video_template.format(augmenter_encoding_name, '{}', EXTENSION))
                self.augmented_videos.append(FullVideo(Video(video_path, self.logs_dir, verbose=self.verbose)))
                self.logger.info("Video {} found".format(video_path))
            try:
                self.splitting_policy_aug = SplittingPolicy(   self.raw_video,
                                                               self.augmented_videos,
                                                               self.augmented_videos_template,
                                                               self.segments_structure_in,
                                                               splitting_module_args,
                                                               args.verbose,
                                                               args.logs_dir,
                                                               args.figs_dir)
            except:
                self.logger.exception("message")
                sys.exit(-1)
        except:
            self.augmenter_policy = None
            self.logger.info("No augmentation policy selected. Processing video unaugmented")

    def compute_splitting(self):
        self.logger.info("Starting video splitting and computation: std segments")
        self.splitting_policy_std.split_and_compute()
        self.logger.info("Video splitting computed succesfully")
        fact = MultilevelVideoFactory(self.logger, enable_logging=self.verbose)
        self.multires_video = fact.multilevel_video_from_full_videos(self.rescaled_videos)

        # check if there have been some removal #

        remove_map = {}
        self.logger.info("Checking if std segments present removal")

        for i, row in self.csv_std_segments_in.iterrows():
            for res in self.resolutions:
                if BITRATE_CSV_STD.format(res) not in row.keys() or row[BITRATE_CSV_STD.format(res)] == '':
                    if i not in remove_map.keys():
                        remove_map[i] = []
                    remove_map[i].append(self.multires_video.get_std_level(res, i))

        self.multires_video = self.multires_video.remove_levels(remove_map)
        if self.splitting_policy_aug:
            self.logger.info("Starting video splitting and computation: aug segments")
            self.splitting_policy_aug.split_and_compute()

    def compute_augmentation(self):

        assert self.multires_video

        if self.augmented_videos:
            self.logger.info("Adding augmented levels to the video")
            augmented_map = {}
            augmented_videos_map = {}

            for video in self.augmented_videos:
                augmented_videos_map[video.video().load_resolution()] = video

            for row_index, row in self.csv_aug_segments_in.iterrows():
                level = augmented_videos_map[ row[RESOLUTION_CSV]].load_segments()[row[INDEX_CSV]]
                level = Level([level], is_augmented=True)
                if row[INDEX_CSV] not in augmented_map.keys():
                    augmented_map[row[INDEX_CSV]] = []
                augmented_map[row[INDEX_CSV]].append(level)

            self.multires_video = self.multires_video.add_levels(augmented_map)

        else:
            self.logger.info("augmentation policy is None")

    def log_results(self):

        std_dataframe = self.multires_video.load_std_dataframe()
        aug_dataframe = self.multires_video.load_aug_dataframe()
        sim_data = self.multires_video.get_simulation_data()

        std_dataframe.to_csv(self.csv_std_segments_out)
        if aug_dataframe is not None:
            aug_dataframe.to_csv(self.csv_aug_segments_out)

        with open(self.simulation_file_out, 'w') as fout:
            json.dump(sim_data, fout)

if __name__=="__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument('--video_configs', help='video configurations', required=True)
    parser.add_argument('--segments_handler_configs', help='configurations of the rescaler and aug module', required=True)
    parser.add_argument('--sim_file_configs', help='configurations of the rescaler and aug module', required=True)
    parser.add_argument('--segments_structure_in', help='indicator for the splitting', required=True)
    parser.add_argument('--logs_dir', help='where the logs are gonna be stored', required=True)
    parser.add_argument('--figs_dir', help='where the figs are gonna be stored', required=True)
    parser.add_argument('--simulation_file_out', help='output: json file for the simulation', required=True)
    parser.add_argument('--csv_std_segments_in', help='output: recap csv for segments std', required=True)
    parser.add_argument('--csv_aug_segments_in', help='output: recap csv for segments aug')
    parser.add_argument('--csv_std_segments_out', help='output: recap csv for segments std', required=True)
    parser.add_argument('--csv_aug_segments_out', help='output: recap csv for segments aug')
    parser.add_argument('--rescaled_video_template', help='rescaled video template', required=True)
    parser.add_argument('--augmented_video_template', help='rescaled video template', required=True)
    parser.add_argument('--verbose', action="store_true")

    args = parser.parse_args()
    args.rescaled_video_template = args.rescaled_video_template.format('{}', EXTENSION)
    ra = SimulationFileHandler(args)
    ra.compute_splitting()
    ra.compute_augmentation()
    ra.log_results()


b = float(input())
a = b+0.0001
notas = [100, 50, 20, 10, 5, 2]
moedas = [1.00, 0.50, 0.25, 0.10, 0.05, 0.01]
print(f"NOTAS:")
for nota in notas:
    print(f"{int(a//nota)} nota(s) de R$ {nota}.00")
    a = a%nota
print(f"MOEDAS:")
for moeda in moedas:
    print(f"{int(a//moeda)} moeda(s) de R$ {moeda:.2f}")
    a = a%moeda


import bpy
import os
import subprocess
import platform

bl_info = {
    "name": "Open Blend Directory",
    "description": "Opens the directory where the currently opened .blend file is stored similarly to how JetBrains handles it. Cross Platform!",
    "author": "Tobin Cavanaugh",
    "version": (1, 0),
    "blender": (3, 3, 1),
    "location": "View3D > Tool Shelf > My Addon",
    "category": "System",
}

addon_keymaps = []

# Thanks to
# https://blender.stackexchange.com/questions/717/is-it-possible-to-print-to-the-report-window-in-the-info-view
# Manu Järvinen & brockmann for this solution
def ErrorMessageUnsaved(self, context):
    self.layout.label(text="You haven't saved your blender file yet! That's bad! Save it!")

def ErrorMessageOS(self, context):
    self.layout.label(text="I don't know this operating system! It should be easy to add support for though")

#Actual function for opening the blend directory
def open_blend_directory():

    #Get the save data path
    blend_file_path = bpy.data.filepath

    #Choose the correct OS, if its a valid save path
    #No guarantees that these work on non windows platforms
    #According to Lous Brandy & Boris Verkhoviskiy this should be correct
    # https://stackoverflow.com/questions/1854/how-to-identify-which-os-python-is-running-on
    if blend_file_path:
        if platform.system() == 'Windows':
            subprocess.Popen(f'explorer /select, "{bpy.data.filepath}"')
        elif platform.system() == 'Linux':
            subprocess.Popen(['xdg-open', os.path.dirname(blend_file_path)])
        elif platform.system() == 'Darwin':
            subprocess.Popen(['open', '-R', blend_file_path])
        else:
            bpy.context.window_manager.popup_menu(ErrorMessageOS, title="Error", icon='ERROR')
    else:
        bpy.context.window_manager.popup_menu(ErrorMessageUnsaved, title="Error", icon='ERROR')

def register():

    #Register our class
    bpy.utils.register_class(OpenBlendDirectoryOperator)
    wm = bpy.context.window_manager

    #I dont really understand whats going on here tbh

    #Get our new keymap
    km = wm.keyconfigs.addon.keymaps.new(name='Window', space_type='EMPTY')

    #Create the keymap? if you want to change the keybinds for alt + shift + r, do it here!!
    kmi = km.keymap_items.new('wm.open_blend_directory', 'R', 'PRESS', alt=True, shift=True)

    #Append to the keymaps
    addon_keymaps.append((km, kmi))

def unregister():

    #Unregister our class
    bpy.utils.unregister_class(OpenBlendDirectoryOperator)
    wm = bpy.context.window_manager

    #Remove all keymaps
    for km, kmi in addon_keymaps:
        km.keymap_items.remove(kmi)

    #Clear our locally saved keymaps
    addon_keymaps.clear()

#Define our operator class
class OpenBlendDirectoryOperator(bpy.types.Operator):

    bl_idname = "wm.open_blend_directory"
    bl_label = "Open Blend Directory"
    bl_options = {'REGISTER'}

    #On execution, open the directory
    def execute(self, context):
        open_blend_directory()
        return {'FINISHED'}

classes = [
    OpenBlendDirectoryOperator,
]

if __name__ == "__main__":
    register()


from pathlib import Path
import json
import requests as re

# looks up all .jl files in the data folder and downloads the pdf linked under mainFile/accessUrl
def download_pdfs():
    pathlist = Path("data/").glob("**/*.jl")
    for path in pathlist:
        jl_file = str(path)
        with open(jl_file, "r") as f:
            for paper in f:
                paper_json = json.loads(paper)
                if not paper_json.get("mainFile") or not paper_json.get("mainFile").get(
                    "accessUrl"
                ):  # manche von den jsons haben kein pdf als mainFile verlinkt – sollte man es da woanders suchen?
                    continue
                pdf_name = paper_json["mainFile"]["fileName"]
                pdf_path = Path(f"data/pdfs/{pdf_name}")
                if not pdf_path.is_file():
                    print("Processing " + f"data/pdfs/{pdf_name}")
                    if not pdf_name.startswith("1047735.pdf"):
                        pdf_url = paper_json["mainFile"]["accessUrl"]
                        response = re.get(pdf_url)
                        Path("data/pdfs").mkdir(parents=True, exist_ok=True)
                        with open(f"data/pdfs/{pdf_name}", "wb") as f:
                            print("Writing contents to " + f"data/pdfs/{pdf_name}")
                            f.write(response.content)
    print("finished!")

download_pdfs()


import numpy as np
import os, struct
from array import array as pyarray
from numpy import append, array, int8, uint8, zeros

def load_mnist(dataset="training", digits=np.arange(10), path="."):
    """
    Loads MNIST

	Note that you first need to download files
	http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz
	http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz
	http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz
	http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz
	and unpack them
    """

    if dataset == "training":
        fname_img = os.path.join(path, 'train-images-idx3-ubyte')
        fname_lbl = os.path.join(path, 'train-labels-idx1-ubyte')
    elif dataset == "testing":
        fname_img = os.path.join(path, 't10k-images-idx3-ubyte')
        fname_lbl = os.path.join(path, 't10k-labels-idx1-ubyte')
    else:
        raise ValueError("dataset must be 'testing' or 'training'")

    flbl = open(fname_lbl, 'rb')
    magic_nr, size = struct.unpack(">II", flbl.read(8))
    lbl = pyarray("b", flbl.read())
    flbl.close()

    fimg = open(fname_img, 'rb')
    magic_nr, size, rows, cols = struct.unpack(">IIII", fimg.read(16))
    img = pyarray("B", fimg.read())
    fimg.close()

    ind = [ k for k in range(size) if lbl[k] in digits ]
    N = len(ind)

    images = zeros((N, rows, cols), dtype=uint8)
    labels = zeros((N, 1), dtype=int8)
    for i in range(len(ind)):
        images[i] = array(img[ ind[i]*rows*cols : (ind[i]+1)*rows*cols ]).reshape((rows, cols))
        labels[i] = lbl[ind[i]]

    labels += 1
    labels  = labels.flatten()

    images  = images.astype(np.float16)
    images  = images.reshape(( images.shape[0], -1 )) # FLATTENING for plain DNN

    return images, labels

testing, lab_testing = load_mnist('testing')
trainin, lab_trainin = load_mnist('training')

mean = trainin.astype(np.float64).mean()
std  = trainin.astype(np.float64).std()
print(mean, std)

np.save	(	'mnist.trainin'
		,	trainin
		,	allow_pickle = False
		)
np.save	(	'mnist.testing'
		,	testing
		,	allow_pickle = False
		)

np.save	(	'mnist.lab_testing'
		,	lab_testing
		,	allow_pickle = False
		)
np.save	(	'mnist.lab_trainin'
		,	lab_trainin
		,	allow_pickle = False
		)


class Solution:
    def getPermutation(self, n: int, k: int) -> str:
        fact = 1
        numbers = []
        for i in range(1, n):
            fact *= i
            numbers.append(i)
        numbers.append(n)
        ans = ""
        k -= 1
        while True:
            ans += str(numbers[k // fact])
            numbers.pop(k // fact)
            if not numbers:
                break
            k %= fact
            fact //= len(numbers)
        return ans


import os
import sys
import numpy as np
import matplotlib.pyplot as plt
import pickle
import re

filePattern = re.compile('([^_]*)_([0-9]*).p');

algos = {}

# point this at the output directory
for root, subFolders, files in os.walk('./runtimes/'):
	for file in files:
		m = filePattern.match(file)
		if not m:
			continue
		algo = m.group(1)
		nprob = m.group(2)

		with open(os.path.join(root, file), "rb") as input:
			if nprob not in algos:
				algos[nprob] = {}
			data = pickle.load(input)
			algos[nprob][algo] = data

probNdx = [-1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, -1, 14, 15, 16, 17, -1, 19, 20, 21]
algoIdx = {
	'mine': 1,
	'pyOpt': 2
}

with open('../../octave/assign_to_matlab.m', 'w') as log:
	log.write('h = zeros(0,0,0);')

	for nprob in algos.keys():
		print(nprob)
		for algo in algos[nprob].keys():
			nfev = algos[nprob][algo]['nfev']
			fvals = algos[nprob][algo]['fvals']
			print('\t' + str(algo) + " " + str(nfev))
			for i in range(len(fvals)):
				log.write('h(' + str(i + 1) + ', ' + str(probNdx[int(nprob)]) + ', ' + str(algoIdx[algo]) + ') = ' + str(fvals[i][0]) + ';\n')
			xs = np.asarray(range(nfev))
			ys = np.zeros(nfev)
			for i in range(nfev):
				ys[i] = fvals[i][0]
			plt.plot(xs, ys, label=algo)

		plt.legend(loc='upper right')
		plt.savefig('plots/' + nprob + '_performance.png')
		plt.close()


import logging
import time
from collections import namedtuple
from urllib.robotparser import RobotFileParser

import requests
from bs4 import BeautifulSoup
from contexttimer import Timer
from dateutil import parser as date_parser
from lazy_property import LazyProperty as lazy_property

from articlix.crawler import USER_AGENT
from articlix.crawler.article import Article
from articlix.crawler.exception import FetchError
from articlix.crawler.url import Url

logger = logging.getLogger(__name__)

class Page:
    def __init__(self, url, headers, text):
        self.url = url
        self.head = headers
        self.text = text

    @lazy_property
    def date(self):
        date = self.head.get('Date', None)
        if date is not None:
            date = date_parser.parse(date)
        return date

    @lazy_property
    def last_modified(self):
        last_modified = self.head.get('Last-Modified', None)
        if last_modified is not None:
            last_modified = date_parser.parse(last_modified)
        return last_modified

    @lazy_property
    def allow_cache(self):
        return not self._have_perm('NOCACHE')

    @lazy_property
    def allow_follow(self):
        return not self._have_perm('NOFOLLOW')

    def links_gen(self):
        if not self.allow_follow:
            return

        for link_node in self.soup.find_all('a'):
            url_str = link_node.get('href')
            if url_str is None: continue
            url = Url(url_str)
            url = url if url.is_absolute else self.url + url
            if url.is_valid: yield url

    @lazy_property
    def soup(self):
        return BeautifulSoup(self.text, 'html.parser')

    def read(self):
        return Article(self)

    def _have_perm(self, perm):
        for tag in self.soup.find_all('ROBOTS', 'meta'):
            if perm in tag['content'].split(', '):
                return True
        return False

Response = namedtuple('Response', 'headers text elapsed ended')

def fetch_raw(url, method='GET', strict=True, timeout=3):
    try:
        with Timer() as t:
            r = requests.request(method, str(url),
                                 headers={'User-Agent': USER_AGENT},
                                 timeout=timeout)
            r.raise_for_status()
        return Response(r.headers, r.text, t.elapsed, t.end)
    except Exception as e:
        if strict:
            raise FetchError("Failed to get data") from e
    return None

class Fetcher:
    def __init__(self, delay=None, use_adaptive=True,
                 adaptive_scale=5, upper_bound=3):
        self.delay = delay
        self.use_adaptive = use_adaptive
        self.adaptive_scale = adaptive_scale
        self.upper_bound = upper_bound

        self._t = None
        self._last_fetched = None

    def __call__(self, url):
        self._cool_down()
        headers, text, self._t, self._last_fetched = fetch_raw(url)
        logging.info("Last fetched time is `%s`.", self._last_fetched)
        return Page(url, headers, text)

    def _cool_down(self):
        if self._last_fetched is None:
            return

        def cutb(t):
            return max(0, min(t, self.upper_bound))

        time_passed = time.time() - self._last_fetched

        if self.delay is not None:
            time.sleep(cutb(self.delay - time_passed))

        if self.delay is None and self.use_adaptive:
            time.sleep(cutb(self.adaptive_scale * self._t - time_passed))

class Site:
    DEFAULT_ROBOTS = 'User-Agent: *\nAllow: /\n'

    def __init__(self, url):
        self.url = url

    def allow_crawl(self, url):
        return self._robots.can_fetch(USER_AGENT, str(url))

    def fetch(self, url):
        return self._fetcher(url)

    @lazy_property
    def _robots(self):
        robots = RobotFileParser()
        r = fetch_raw(self.url.site + 'robots.txt', strict=False)
        if r is None:
            robots.parse(self.DEFAULT_ROBOTS.splitlines())
        else:
            robots.parse(r.text.splitlines())
        return robots

    @lazy_property
    def _fetcher(self):
        crawl_delay = self._crawl_delay
        if crawl_delay is None:
            request_rate = self._request_rate
            if request_rate is not None:
                crawl_delay = request_rate[1] / request_rate[0]
        return Fetcher(crawl_delay)

    @lazy_property
    def _crawl_delay(self):
        try:
            return self._robots.crawl_delay(USER_AGENT)
        except:
            return None

    @lazy_property
    def _request_rate(self):
        try:
            return self._robots.request_rate(USER_AGENT)
        except:
            return None


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

"""
@author: dj
@contact: dj@itmojun.com
@software: PyCharm
@file: main.py
@time: 2018/12/5 20:32
"""

import time
import urllib, json, random
from flask import Flask, request, abort
from werkzeug.contrib.fixers import ProxyFix
import redis
from wechatpy.utils import check_signature
from wechatpy.exceptions import InvalidSignatureException
from wechatpy import parse_message, create_reply

app = Flask(__name__)
app.wsgi_app = ProxyFix(app.wsgi_app)

pool = redis.ConnectionPool(host='localhost', port=6379, decode_responses=True)

def get_robot_reply(input_text):
    apiKeys = ('cc8c863cfa2b42ecb1e6ae9d4f2c5f36', '512a756fbb5b45b4902e53e09a89e86d', '263aa1a1b0a6470d84ad28415e7bee47', '2d7072e72426447585a3bfc5f72fdc38', 'f7954cb3d7b14134962d0a918185339a')

    data = {
        "reqType":0,
        "perception": {
            "inputText": {
                "text": input_text
            },
        },
        "userInfo": {
            "apiKey": random.choice(apiKeys),
            "userId": "339745"
        }
    }

    data = json.dumps(data, ensure_ascii=False).encode("utf-8")
    url = urllib.request.Request("http://openapi.tuling123.com/openapi/api/v2", data=data, method="POST")
    res = urllib.request.urlopen(url).read()
    return json.loads(res.decode("utf-8"))["results"][0]["values"]["text"]

@app.route('/wx', methods=['GET', 'POST'])
def weixin_handle():
    token = 'itmojun'  # 注意此处要与微信公众平台上填写的保持一致
    timestamp = request.args.get('timestamp', '')
    nonce = request.args.get('nonce', '')
    echo_str = request.args.get('echostr', '')
    signature = request.args.get('signature', '')

    try:
        check_signature(token, signature, timestamp, nonce)
    except InvalidSignatureException:
        # 处理异常情况或忽略
        abort(403)  # 后续代码不会被执行

    if request.method == 'GET':
        # 微信公众号后台设置界面提交服务器URL验证
        return echo_str
        # return reply, 200, {'Content-Type': 'text/plain; charset=utf-8'}
    else:
        # 微信官方服务器通过POST方式转发消息给我们自己的服务器
        msg = parse_message(request.data)

        r = redis.Redis(connection_pool=pool)
        redis_key = msg.source

        if msg.type == 'event':  # 事件消息
            if msg.event == 'subscribe':  # 关注事件
                subscribe_time = msg.create_time.strftime("%Y-%m-%d %H:%M:%S")
                ret = r.hmset(redis_key, {"state": 1, "subscribe_time": subscribe_time})
                if ret:
                    pass  # redis操作成功
                else:
                    pass  # redis操作失败

                reply = create_reply('Hello，小伙伴儿，欢迎关注IT魔君！一入IT深似海，从此再也不缺爱，IT魔君与你分享各种骚操作，带你装逼带你飞！', msg);
            elif msg.event == 'unsubscribe':  # 取消关注事件
                r.delete(redis_key)
                reply = create_reply('期待与你再次相遇！', msg)
            else:
                # 比如点击菜单事件等
                reply = create_reply('', msg)  # 回复空消息
        elif msg.type == 'text' or msg.type == 'voice':  # 文本消息或语音消息
            cur_msg_time = time.time()
            last_msg_time = r.hget(redis_key, 'last_msg_time')  # hget方法返回值为str值或者None
            if not last_msg_time:
                last_msg_time = 0
            ret = r.hset(redis_key, "last_msg_time", cur_msg_time)

            # 如果两次连续消息的时间间隔超过1小时，那么就直接设置为普通聊天模式
            if cur_msg_time - float(last_msg_time) > 3600:
                r.hmset(redis_key, {'state': 1})
                state = '1'

            if msg.type == 'voice':
                content = msg.recognition
                if content is None:
                    reply = create_reply('你的普通话不够标准，请再说一次或打字吧！', msg)
                    return reply.render()
            else:
                content = msg.content

            state = r.hget(redis_key, 'state')  # hget方法返回值为str值或者None
            if not state:
                r.hmset(redis_key, {'state': 1})
                state = '1'

            if state == '1':
                # 普通聊天模式
                if '急急如律令' in content:
                    if r.hexists(redis_key, 'pc_id'):
                        reply = '成功进入远控模式！(已绑定目标电脑ID为“%s”，如需更换绑定，请输入“绑定电脑”)' % r.hget(redis_key, 'pc_id')
                        ret = r.hset(redis_key, "state", 2)
                    else:
                        reply = '成功进入远控模式，请输入要绑定的目标电脑ID'
                        ret = r.hset(redis_key, "state", 21)
                # elif '小魔仙你好' in content:
                elif '@小魔仙' == content:
                        reply = '小魔仙驾到，我们开始快乐地尬聊吧~'
                        ret = r.hset(redis_key, "state", 3)
                else:
                        # reply = ''  # 回复空消息
                        reply = '君哥正在苦逼调bug，稍后给你回复！'
            elif state == '2':
                # 远控模式
                if '芝麻关门' in content:
                    reply = '已退出远控模式，进入与君哥尬聊的快乐模式！'
                    ret = r.hset(redis_key, "state", 1)
                elif '绑定电脑' in content:
                    reply = '目标电脑ID是啥？'
                    ret = r.hset(redis_key, "state", 21)
                else:
                    pc_id = r.hget(redis_key, 'pc_id')
                    cmd = content
                    ret = r.set(pc_id, cmd, ex=3)  # 过期时间为3秒
                    if ret:
                        reply = '控制成功！'
                    else:
                        reply = '控制失败！'
            elif state == '21':
                # 等待用户输入目标电脑ID模式
                pc_id = content
                ret = r.hmset(redis_key, {'state': 2, 'pc_id': pc_id})
                if ret:
                    reply = '绑定成功！'
                else:
                    reply = '绑定失败！'
            elif state == '3':
                # 自动聊天模式
                # if '小魔仙再见' in content:
                if '@君哥' == content:
                    reply = '小魔仙睡觉去了，现在君哥陪你尬聊！'
                    ret = r.hset(redis_key, "state", 1)
                else:
                    try:
                        reply = get_robot_reply(content)
                    except Exception as e:
                        reply = str(e)
            else:
                reply = ''  # 回复空消息

            reply = create_reply(reply, msg)
        else:  # 其他类型消息
            reply = create_reply('很抱歉，我暂时不能处理这种消息！', msg)

        return reply.render()

if __name__ == '__main__':
    app.run(debug=True)


'''
Created on May 6, 2019

@author: mac
'''

import os
import multiprocessing as mp
import time
import pandas as pd
from PathManager.StockPathManager import GetMergedFolder_Last

def _ReadAllMergedDailyData(fileName):
    stockID = fileName[fileName.rfind('/')+1:fileName.find('.')]
    df = pd.read_excel(fileName, index_col=None, encoding='utf_8_sig')
    print('Read file:', fileName, 'Done!')
    return {stockID: df}

def ReadAllMergedDailyData():
    folder = GetMergedFolder_Last()
    files = os.listdir(folder)
    dataFrames = []
    begin_time = time.time()
    pool = mp.Pool(mp.cpu_count()*2)
    for file_ in files:
        fullpath = os.path.join(folder, file_)
        if fullpath.find('.xlsx') == -1:
            continue
        dataFrames.append(pool.apply_async(
            _ReadAllMergedDailyData, (fullpath, )))
    pool.close()
    pool.join()
    endTime = time.time()
    print('ReadAllMergedDailyData cost:%s' % (endTime - begin_time))
    ret = {}
    for item in dataFrames:
        ret.update(item.get())
    endTime1 = time.time()
    print('ReadAllMergedDailyData to dict:%s' % (endTime1 - endTime))
    return ret

if __name__ == '__main__':
    ret = ReadAllMergedDailyData()
    for stockID in ret:
        print(stockID, ret[stockID])
        input()


import wx
from FlowPanel import *
import globals
import os.path
import os

class Finish(FlowPanel):
    def __init__(self, parent, stepNumber):
        self.go = False
        FlowPanel.__init__(self, parent, stepNumber=stepNumber)
        self.parent = parent
        self.parent.finishBtn = wx.Button(self,id=wx.ID_ANY,label="Finish")
        self.parent.interruptBtn = wx.Button(self,id=wx.ID_ANY,label="Interrupt")
        self.parent.interruptBtn.Disable()

        actions = wx.BoxSizer(wx.HORIZONTAL)

        # event for moving forward
        def go(evt):
            self.finished()
            # disable the button once pressed
            self.parent.exit()

        self.parent.finishBtn.Bind(wx.EVT_BUTTON, go)
        actions.Add(self.parent.finishBtn, 1)

        self.parent.interruptBtn.Bind(wx.EVT_BUTTON, go)
        actions.Add(self.parent.interruptBtn, 1)

        self.sizer.Add(actions, flag=wx.EXPAND|wx.RIGHT|wx.LEFT, border=10)

    def do(self):
        self.setStatus(STATUS_READY)
        self.finished()
        return True


import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import seaborn, random

from gridWorldEnvironment import GridWorld

# creating gridworld environment
gw = GridWorld(gamma = .9)

def state_action_value(env):
    q = dict()
    for state, action, next_state, reward in env.transitions:
        q[(state, action)] = np.random.normal()
    return q

def e_greedy(env, e, q, state):
    actions = env.actions
    action_values = []
    prob = []
    for action in actions:
        action_values.append(q[(state, action)])
    for i in range(len(action_values)):
        if i == np.argmax(action_values):
            prob.append(1 - e + e/len(action_values))
        else:
            prob.append(e/len(action_values))
    return actions, prob

def generate_e_greedy_policy(env, e, Q):
    pi = dict()
    for state in env.states:
        pi[state] = e_greedy(env, e, Q, state)
    return pi

def generate_random_policy(env):
    pi = dict()
    for state in env.states:
        actions = []
        prob = []
        for action in env.actions:
            actions.append(action)
            prob.append(0.25)
        pi[state] = (actions, prob)
    return pi

# function for tree backup algorithm
def avg_over_actions(pi, Q, state):
    actions, probs = pi[state]
    q_values = np.zeros(4)
    for s, a in Q.keys():
        if s == state:
            q_values[actions.index(a)] = Q[s,a]
    return np.dot(q_values, probs)

def n_step_tree_backup(env, epsilon, alpha, n, num_iter, learn_pi = True):
    Q = state_action_value(env)
    Q_, pi_, delta = dict(), dict(), dict()
    pi = generate_e_greedy_policy(env, epsilon, Q)

    for _ in range(num_iter):
        current_state = np.random.choice(env.states)
        action = np.random.choice(b[current_state][0], p = b[current_state][1])
        state_trace, action_trace, reward_trace  = [current_state], [action], [0]
        Q_[0] = Q[current_state, action]
        t, T = 0, 10000
        while True:
            if t < T:
                next_state, reward = env.state_transition(current_state, action)
                state_trace.append(next_state)
                reward_trace.append(reward)
                if next_state == 0:
                    T = t + 1
                    delta[t] = reward - Q_[t]
                else:
                    delta[t] = reward + env.gamma * avg_over_actions(pi, Q, next_state) - Q_[t]
                    action = np.random.choice(pi[next_state][0], p = pi[next_state][1])
                    action_trace.append(action)
                    Q_[t+1] = Q[next_state, action]
                    pi_[t+1] = pi[next_state][1][pi[next_state][0].index(action)]

            tau = t - n + 1
            if tau >= 0:
                Z = 1
                G = Q_[tau]
                for i in range(tau, min([tau + n -1, T-1])):
                    G += Z * delta[i]
                    Z *= env.gamma * pi_[i+1]
                Q[state_trace[tau], action_trace[tau]] += alpha * (G - Q[state_trace[tau], action_trace[tau]])
                if learn_pi:
                    pi[state_trace[tau]] = e_greedy(env, epsilon, Q, state_trace[tau])
            current_state = next_state
#             print(state_trace, action_trace, reward_trace)

            if tau == (T-1):
                break
            t += 1

    return pi, Q

pi, Q = n_step_tree_backup(gw, 0.2, 0.5, 1, 10000)

### RED = TERMINAL (0)
### GREEN = LEFT
### BLUE = UP
### PURPLE = RIGHT
### ORANGE = DOWN

show_policy(pi, gw)


import os
from pathlib import Path

featureName = input("Please enter Usecase\n")
ext = 'ts'
pathDomain = './src/domain/usecases/{0}'.format(
    featureName[0].lower()+featureName[1:])
pathRepository = './src/data/repository/{0}'.format(
    featureName[0].lower()+featureName[1:])
pathDataSource = './src/data/dataSource/{0}'.format(
    featureName[0].lower()+featureName[1:])

if not os.path.exists(pathDomain):   # create folders if not exists
    Path(pathDomain).mkdir(parents=True, exist_ok=True)

if not os.path.exists(pathRepository):   # create folders if not exists
    Path(pathRepository).mkdir(parents=True, exist_ok=True)

if not os.path.exists(pathDataSource):   # create folders if not exists
    Path(pathDataSource).mkdir(parents=True, exist_ok=True)

useCase = '{0}Usecase'.format(featureName)
useCaseImpl = '{0}UsecaseImpl'.format(featureName)

repository = '{0}Repository'.format(featureName)
repositoryImpl = '{0}RepositoryImpl'.format(featureName)

dataSource = '{0}DataSource'.format(featureName)
localDataSource = '{0}LocalDataSource'.format(featureName)
remoteDataSource = '{0}RemoteDataSource'.format(featureName)

f_useCase = os.path.join(pathDomain, '{0}.{1}'.format(useCase, ext))
f_useCaseImpl = os.path.join(pathDomain, '{0}.{1}'.format(useCaseImpl, ext))

f_dataSource = os.path.join(pathDataSource, '{0}.{1}'.format(dataSource, ext))
f_localDataSource = os.path.join(
    pathDataSource, '{0}.{1}'.format(localDataSource, ext))
f_remoteDataSource = os.path.join(
    pathDataSource, '{0}.{1}'.format(remoteDataSource, ext))

f_repository = os.path.join(pathRepository, '{0}.{1}'.format(repository, ext))
f_repositoryImpl = os.path.join(
    pathRepository, '{0}.{1}'.format(repositoryImpl, ext))

contentUsecase = ("export interface {0} {{}} " +
                  "\nexport class {1} implements {0} {{" +
                  "\nprivate repository: {2}" +
                  "\nconstructor(_repository: {2}) {{" +
                  "\n  this.repository = _repository" +
                  "\n}}" +
                  "\n}} ").format(
    useCase, useCaseImpl, repository)

contentRepository = ("export interface {0} {{}} " +
                     "\nexport class {1} implements {0} {{" +
                     "\n    private localDataSource: {2}" +
                     "\n    private remoteDataSource: {2}" +

                     "\nconstructor(_localDataSource: {2},_remoteDataSource:{2}) {{" +
                     "\n    this.localDataSource = _localDataSource" +
                     "\n    this.remoteDataSource = _remoteDataSource" +

                     "\n}}}}").format(
    repository, repositoryImpl, dataSource)

contentDataSource = "export interface {0} {{}}".format(
    dataSource)

contentLocalDataSource = "export class {0} implements {1} {{}}".format(
    localDataSource, dataSource)

contentRemoteDataSource = "export class {0} implements {1} {{}}".format(
    remoteDataSource, dataSource)

fileUseCase = open(f_useCase, "x")
fileUseCase.write(contentUsecase)
fileUseCase.close()

fileRepository = open(f_repository, "x")
fileRepository.write(contentRepository)
fileRepository.close()

fileDataSource = open(f_dataSource, "x")
fileDataSource.write(contentDataSource)
fileDataSource.close()

fileLocalDataSource = open(f_localDataSource, "x")
fileLocalDataSource.write(contentLocalDataSource)
fileLocalDataSource.close()

fileRemoteDataSource = open(f_remoteDataSource, "x")
fileRemoteDataSource.write(contentRemoteDataSource)
fileRemoteDataSource.close()


import cv2
import os

def save_frame_camera_key(device_num, dir_path, rfid, ext='jpg', delay=1, window_name='frame'):
    WIDTH = 1920
    HEIGHT = 1080
    FPS = 30
    cap = cv2.VideoCapture(device_num)
    cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'))
    # cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('Y','U','Y','V'))
    cap.set(cv2.CAP_PROP_FRAME_WIDTH, WIDTH)
    cap.set(cv2.CAP_PROP_FRAME_HEIGHT, HEIGHT)
    cap.set(cv2.CAP_PROP_FPS, FPS)

    if not cap.isOpened():
        return

    os.makedirs(dir_path, exist_ok=True)
    base_path = os.path.join(dir_path, rfid)

    while True:
        ret, frame = cap.read()
        cv2.imshow(window_name, frame)
        key = cv2.waitKey(delay) & 0xFF
        if key == ord('c'):
            cv2.imwrite('{}.{}'.format(base_path, ext), frame)
            return '{}.{}'.format(base_path, ext)
        elif key == ord('q'):
            break

    cv2.destroyWindow(window_name)

ret = save_frame_camera_key(1, 'data/temp', 'aaaaa')
print(ret)


from machine import SD
import os
import time

# VERSION 1
# Writing a file in /flash folder
file_path = '/flash/log'

try:
    os.listdir('/flash/log')
    print('/flash/log file already exists.')
except OSError:
    print('/flash/log file does not exist. Creating it ...')
    os.mkdir('/flash/log')

name = '/my_first_file.log'

# Writing
with open(file_path + name, 'w') as f:
    f.write('Testing write operations in a file.')

# Reading
with open(file_path + name, 'r') as f:
    print(f.readall())


import numpy as np
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, BatchNormalization, Activation
from keras.layers.convolutional import Conv2D, MaxPooling2D
from keras.constraints import maxnorm

def Init_Train_Test(outputFile, activation, x_train, y_train, x_test, y_test, seed, class_num, epochs):
    # model
    model = Sequential()

    model.add(Conv2D(32, (3, 3), input_shape=x_train.shape[1:], activation='relu', padding='same'))
    model.add(Dropout(0.2))
    model.add(BatchNormalization())

    model.add(Conv2D(64, (3, 3), padding='same', activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.2))
    model.add(BatchNormalization())

    model.add(Conv2D(64, (3, 3), padding='same', activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.2))
    model.add(BatchNormalization())

    model.add(Conv2D(128, (3, 3), padding='same', activation='relu'))
    model.add(Dropout(0.2))
    model.add(BatchNormalization())

    model.add(Conv2D(64, (3, 3), padding='same', activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.2))
    model.add(BatchNormalization())

    model.add(Conv2D(128, (3, 3), padding='same', activation='relu'))
    model.add(Dropout(0.2))
    model.add(BatchNormalization())

    model.add(Flatten())
    model.add(Dropout(0.2))

    model.add(Dense(256, kernel_constraint=maxnorm(3)))
    model.add(Activation('relu'))
    model.add(Dropout(0.2))
    model.add(BatchNormalization())

    model.add(Dense(128, kernel_constraint=maxnorm(3)))
    model.add(Activation('relu'))
    model.add(Dropout(0.2))
    model.add(BatchNormalization())

    model.add(Dense(class_num))
    model.add(Activation(activation))

    # train
    optimizer = 'adam'
    model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy'])
    np.random.seed(seed)
    model.fit(x_train, y_train, validation_data=(x_test, y_test), epochs=epochs, batch_size=64)

    # test
    scores = model.evaluate(x_test, y_test, verbose=0)
    print("Accuracy: %.2f%%" % (scores[1] * 100))

    # save model
    model.save(outputFile)


# -*- coding: utf-8 -*-
import unittest
from securetea.lib.log_monitor.server_log.detect.attacks.xss import CrossSite
from securetea.lib.log_monitor.server_log.server_logger import ServerLogger
from securetea.lib.osint.osint import OSINT

try:
    # if python 3.x.x
    from unittest.mock import patch
except ImportError:  # python 2.x.x
    from mock import patch

class TestCrossSite(unittest.TestCase):
    """
    Test class for SecureTea Server Log CrossSite Attack Detection.
    """

    def setUp(self):
        """
        Setup class for TestCrossSite.
        """
        # Initialize CrossSite object
        self.xss_obj = CrossSite(test=True)

        # Mock parsed log file data
        self.data = {
            "1.1.1.1":{
                    "count": 1,
                    "get": ["random_get"],
                    "unique_get": ["random_get"],
                    "ua": ["random_ua"],
                    "ep_time": [1560492302],
                    "status_code": [200]
            }
        }

    @patch("securetea.lib.log_monitor.server_log.detect.attacks.xss.write_mal_ip")
    @patch.object(OSINT, "perform_osint_scan")
    @patch("securetea.lib.log_monitor.server_log.detect.attacks.xss.utils")
    @patch.object(ServerLogger, "log")
    @patch.object(CrossSite, "regex_check")
    @patch.object(CrossSite, "payload_match")
    def test_detect_xss(self, mck_pm, mck_rc, mock_log, mck_utils, mck_osint, mck_wmip):
        """
        Test detect_xss.
        """
        mck_wmip.return_value = True
        mck_osint.return_value = True
        mck_utils.write_ip.return_value = True
        mck_utils.epoch_to_date.return_value = "random_date"

        # Case 1: No XSS attack
        mck_pm.return_value = False
        mck_rc.return_value = False
        self.assertFalse(mock_log.called)

        # Case 2: XSS attack
        mck_pm.return_value = True
        mck_rc.return_value = True
        self.xss_obj.detect_xss(self.data)
        mock_log.assert_called_with('Possible Cross Site Scripting (XSS) detected from: 1.1.1.1 on: random_date',
                                    logtype='warning')
        mck_wmip.assert_called_with("1.1.1.1")

    def test_regex_check(self):
        """
        Test regex_check.
        """
        # Case 1: Regex matches
        self.xss_obj.regex = [r"<script>"]
        res = self.xss_obj.regex_check(["/random/req=<script>"])
        self.assertTrue(res)

        # Case 2: No regex match
        res = self.xss_obj.regex_check(["/random/req/"])
        self.assertFalse(res)

    def test_payload_match(self):
        """
        Test payload_match.
        """
        # Case 1: Payload matches
        self.xss_obj.payloads = ["<script>"]
        res = self.xss_obj.payload_match(["/random/req=<script>"])
        self.assertTrue(res)

        # Case 2: Payload does not match
        res = self.xss_obj.payload_match(["/random/req"])
        self.assertFalse(res)