text:
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt

import sys
sys.path.append("../../sd")
sys.path.append("../../sd_cartopy")

import os
import glob
from netCDF4 import Dataset
import numpy as np
from scipy.interpolate import interp1d, interp2d
from scipy import array
import datetime as dt

from pysolar.solar import get_altitude
import sza
import pandas as pd
from scipy.stats import median_absolute_deviation as MAD

def plot_freq_versus_height_contour_plots(fig, ax, freqs=np.linspace(5,14,101)*1e6, loc=[10,-100], date=dt.datetime(2015,5,5,22,11)):
    mdata = sza.from_pickle("data_DL.pkl")
    cent, cent0 = mdata["cent"], mdata["cent0"]
    #fig = plt.figure(dpi=150, figsize=(4,3))
    #ax = fig.add_subplot(111)
    i,j = sza._get_ij_(cent["latx"], cent["lonx"], loc[0], loc[1])
    alts = cent["alts"]
    nds = np.zeros((len(freqs), len(alts)))
    for _i, f in enumerate(freqs):
        c_indx = np.argmin(np.abs(sza.calculate_fp(cent["nex"][:,i,j])-f))
        n_diff = -1*(sza.calculate_eta(cent["nex"][:,i,j], cent["alts"], loc[0], loc[1], date, f) -\
                sza.calculate_eta(cent0["nex"][:,i,j], cent0["alts"], loc[0], loc[1], date, f))
        n_diff[c_indx:] = np.nan
        nds[_i,:] = 2*f*n_diff/3e8
        if _i==-1:
            print(n_diff)
            print("Freq: - ", f/1e6)
            break
    print(alts)
    xx, yy = np.meshgrid(freqs, alts)
    c = ax.pcolormesh(xx/1e6, yy, nds.T, cmap="Reds")
    ax.set_xlabel(r"$f_0$, MHz")
    ax.set_ylabel("Height, km")
    ax.set_ylim(100,300)
    ax.set_xlim(5,14)
    cb = fig.colorbar(c, ax=ax, shrink=0.8)
    cb.set_label(r"$V_x (ms^{-1})$")
    ax.text(0.01, 1.05, "Date:%s UT"%date.strftime("%Y-%m-%d %H:%M"), ha="left", va="center", transform=ax.transAxes)
    ax.text(0.1,0.9,"$\chi=%.1f^o$"%sza.get_SZA(loc[0], loc[1], date), ha="left", va="center", transform=ax.transAxes)
    return

def plot():
    def qu(m, a=0.9):
        return np.quantile(m, a)
    dint=0.1
    x = pd.read_csv("../op/finescale_simulate_total_A.csv")
    x = x[(np.abs(x.vT)>30) & (np.abs(x.vT)<300)]
    x.vF = x.vF + x.vFh
    x = x[["vD", "vE", "vF", "freq"]]
    x["freq"] = np.rint(x.freq/dint)*dint
    x = x.groupby(by="freq").agg([qu, MAD]).reset_index()
    fig, axes = plt.subplots(dpi=150, figsize=(8,3), nrows=1, ncols=2)
    ax = axes[0]
    ax.set_xlabel(r"$f_0$, (MHz)")
    ax.set_ylabel(r"$V_x$, $ms^{-1}$")
    ax.set_ylim(0,130)
    t = 0.7
    ct, lw, cs = 0.3, 0.3, 0.7
    ax.errorbar(x.freq, x.vD["qu"], yerr=t*x.vD["median_absolute_deviation"], xerr=dint/2, fmt="o", ecolor="k", color="r",
            capthick=ct, lw=lw, ms=2., capsize=cs, label=r"$V_D$")
    ax.errorbar(x.freq, x.vE["qu"], yerr=t*x.vE["median_absolute_deviation"], xerr=dint/2, fmt="o", ecolor="k", color="g",
            capthick=ct, lw=lw, ms=2., capsize=cs, label=r"$V_E$")
    ax.errorbar(x.freq, x.vF["qu"]-40, yerr=t*x.vF["median_absolute_deviation"], xerr=dint/2, fmt="o", ecolor="k", color="b",
            capthick=ct, lw=lw, ms=2., capsize=cs, label=r"$V_F$")
    ax.legend(loc=1)
    ax.set_xlim(10,18)
    plot_freq_versus_height_contour_plots(fig, axes[1])
    fig.subplots_adjust(wspace=0.3)
    fig.savefig("figures/freq_dist.png", bbox_inches="tight")
    return

if __name__ == "__main__":
    plot()
    os.system("rm -rf __pycache__")
    pass


text:
import pyautogui
import pydirectinput
import keyboard
import time
from PIL import ImageGrab
import numpy as np
import cv2

time.sleep(3)
found_diamonds = False
placing_torch = False
terminated = False
torches_placed = 0

lower_range_diamond = np.array([75, 80, 100])
upper_range_diamond = np.array([90, 255, 255])
lower_range_lava = np.array([7, 216, 193])
upper_range_lava = np.array([23, 255, 255])

def mining_forward():
    global terminated
    if not placing_torch:
        pydirectinput.keyDown("c")
        pydirectinput.mouseDown()
        pydirectinput.keyDown("w")
    if keyboard.is_pressed("u"):
        pydirectinput.keyUp("w")
        pydirectinput.mouseUp()
        terminated = True

def timer():
    torch_time = time.time()
    screenshot_time = time.time()
    while not found_diamonds and not terminated:
        mining_forward()
        if(time.time() > torch_time + 6):
            torch_time = time.time()
            place_torch()
        if(time.time() > screenshot_time + 0.4):
            screenshot_time = time.time()
            if not placing_torch:
                detect_color()

def place_torch():
    global placing_torch
    global torches_placed
    torches_placed += 1
    if torches_placed % 64 == 0:
        pydirectinput.press(str(torches_placed / 64))
        pydirectinput.press("f")
        pydirectinput.press("9")
    placing_torch = True
    pydirectinput.mouseUp()
    pydirectinput.moveRel(700, 400)
    pydirectinput.rightClick()
    pydirectinput.moveRel(-700, -400)
    placing_torch = False

def detect_color():
    im1 = pyautogui.screenshot("currentImg.png")
    im1 = cv2.imread("currentImg.png")
    hsv = cv2.cvtColor(cv2.UMat(im1), cv2.COLOR_BGR2HSV)
    mask_diamond = cv2.inRange(hsv, lower_range_diamond, upper_range_diamond)
    mask_lava = cv2.inRange(hsv, lower_range_lava, upper_range_lava)

    cv2.imwrite("HSV.png", hsv)
    cv2.imwrite("CurrentMaskDiamond.png", mask_diamond)
    cv2.imwrite("CurrentMaskLava.png", mask_lava)
    print("Blue pixels: " + str(cv2.countNonZero(mask_diamond)))
    print("Orange pixels: " + str(cv2.countNonZero(mask_lava)))

    if(int(cv2.countNonZero(mask_diamond)) > 5000):
        release_inputs()
        print("Found Diamonds")
        quit()
    if(int(cv2.countNonZero(mask_lava)) > 25000):
        release_inputs()
        pydirectinput.moveRel(0, -1000)
        pydirectinput.moveRel(0, -1000)
        pydirectinput.moveRel(0, -1000)
        pydirectinput.press("8")
        pydirectinput.rightClick()
        pydirectinput.press("c")
        print("Found Lava")
        quit()

def release_inputs():
    pydirectinput.keyUp("w")
    pydirectinput.mouseUp()

timer()
pydirectinput.keyUp("c")

ext: py

max_stars_repo_path: Minecraft Auto Miner/Miner.py

max_issues_repo_path: Minecraft Auto Miner/Miner.py

max_issues_repo_name: BarjiYT/OpenCV-Minecraft-Diamond-Bot

max_forks_repo_path: Minecraft Auto Miner/Miner.py

max_forks_repo_name: BarjiYT/OpenCV-Minecraft-Diamond-Bot

reason: import numpy


text:
# Authors: Mainak Jas <mainak.jas@telecom-paristech.fr>
#          Tom Dupre La Tour <tom.duprelatour@telecom-paristech.fr>
#          Umut Simsekli <umut.simsekli@telecom-paristech.fr>
#          Alexandre Gramfort <alexandre.gramfort@inria.fr>
#          Thomas Moreau <thomas.moreau@inria.fr>

import numpy as np

from . import cython_code
from .utils.lil import get_z_shape, is_list_of_lil
from .utils.optim import fista, power_iteration
from .utils.convolution import numpy_convolve_uv
from .utils.compute_constants import compute_ztz, compute_ztX
from .utils.dictionary import tukey_window

from .loss_and_gradient import compute_objective, compute_X_and_objective_multi
from .loss_and_gradient import gradient_uv, gradient_d

def squeeze_all_except_one(X, axis=0):
    squeeze_axis = tuple(set(range(X.ndim)) - set([axis]))
    return X.squeeze(axis=squeeze_axis)

def prox_uv(uv, uv_constraint='joint', n_channels=None, return_norm=False):
    if uv_constraint == 'joint':
        norm_uv = np.maximum(1, np.linalg.norm(uv, axis=1, keepdims=True))
        uv /= norm_uv

    elif uv_constraint == 'separate':
        assert n_channels is not None
        norm_u = np.maximum(1, np.linalg.norm(uv[:, :n_channels],
                                              axis=1, keepdims=True))
        norm_v = np.maximum(1, np.linalg.norm(uv[:, n_channels:],
                                              axis=1, keepdims=True))

        uv[:, :n_channels] /= norm_u
        uv[:, n_channels:] /= norm_v
        norm_uv = norm_u * norm_v
    else:
        raise ValueError('Unknown uv_constraint: %s.' % (uv_constraint, ))

    if return_norm:
        return uv, squeeze_all_except_one(norm_uv, axis=0)
    else:
        return uv

def prox_d(D, return_norm=False):
    norm_d = np.maximum(1, np.linalg.norm(D, axis=(1, 2), keepdims=True))
    D /= norm_d

    if return_norm:
        return D, squeeze_all_except_one(norm_d, axis=0)
    else:
        return D

def update_uv(X, z, uv_hat0, constants=None, b_hat_0=None, debug=False,
              max_iter=300, eps=None, solver_d='alternate', momentum=False,
              uv_constraint='separate', loss='l2', loss_params=dict(),
              verbose=0, window=False):
    """Learn d's in time domain.

    Parameters
    ----------
    X : array, shape (n_trials, n_channels, n_times)
        The data for sparse coding
    z : array, shape (n_trials, n_atoms, n_times - n_times_atom + 1)
        Can also be a list of n_trials LIL-sparse matrix of shape
            (n_atoms, n_times - n_times_atom + 1)
        The code for which to learn the atoms
    uv_hat0 : array, shape (n_atoms, n_channels + n_times_atom)
        The initial atoms.
    constants : dict or None
        Dictionary of constants to accelerate the computation of the gradients.
        It should only be given for loss='l2' and should contain ztz and ztX.
    b_hat_0 : array, shape (n_atoms * (n_channels + n_times_atom))
        Init eigen-vector vector used in power_iteration, used in warm start.
    debug : bool
        If True, return the cost at each iteration.
    momentum : bool
        If True, use an accelerated version of the proximal gradient descent.
    uv_constraint : str in {'joint', 'separate'}
        The kind of norm constraint on the atoms:
        If 'joint', the constraint is norm_2([u, v]) <= 1
        If 'separate', the constraint is norm_2(u) <= 1 and norm_2(v) <= 1
    solver_d : str in {'alternate', 'joint'}
        The type of solver to update d:
        If 'alternate', the solver alternates between u then v
        If 'joint', the solver jointly optimize uv with a line search
    loss : str in {'l2' | 'dtw' | 'whitening'}
        The data-fit
    loss_params : dict
        Parameters of the loss
    verbose : int
        Verbosity level.
    window : boolean
        If True, reparametrize the atoms with a temporal Tukey window.

    Returns
    -------
    uv_hat : array, shape (n_atoms, n_channels + n_times_atom)
        The atoms to learn from the data.
    """
    n_trials, n_atoms, n_times_valid = get_z_shape(z)
    _, n_channels, n_times = X.shape
    n_times_atom = uv_hat0.shape[1] - n_channels

    if window:
        tukey_window_ = tukey_window(n_times_atom)[None, :]
        uv_hat0 = uv_hat0.copy()
        uv_hat0[:, n_channels:] /= tukey_window_

    if solver_d == 'alternate':
        msg = "alternate solver should be used with separate constraints"
        assert uv_constraint == 'separate', msg

    if loss == 'l2' and constants is None:
        constants = _get_d_update_constants(X, z)

    def objective(uv):
        if window:
            uv = uv.copy()
            uv[:, n_channels:] *= tukey_window_
        if loss == 'l2':
            return compute_objective(D=uv, constants=constants)
        return compute_X_and_objective_multi(X, z, D_hat=uv, loss=loss,
                                             loss_params=loss_params,
                                             feasible_evaluation=True,
                                             uv_constraint=uv_constraint)

    if solver_d in ['joint', 'fista']:
        # use FISTA on joint [u, v], with an adaptive step size

        def grad(uv):
            if window:
                uv = uv.copy()
                uv[:, n_channels:] *= tukey_window_
            grad = gradient_uv(uv=uv, X=X, z=z, constants=constants, loss=loss,
                               loss_params=loss_params)
            if window:
                grad[:, n_channels:] *= tukey_window_
            return grad

        def prox(uv, step_size=None):
            if window:
                uv[:, n_channels:] *= tukey_window_
            uv = prox_uv(uv, uv_constraint=uv_constraint,
                         n_channels=n_channels)
            if window:
                uv[:, n_channels:] /= tukey_window_
            return uv

        uv_hat, pobj = fista(objective, grad, prox, None, uv_hat0, max_iter,
                             verbose=verbose, momentum=momentum, eps=eps,
                             adaptive_step_size=True, debug=debug,
                             name="Update uv")

    elif solver_d in ['alternate', 'alternate_adaptive']:
        # use FISTA on alternate u and v

        adaptive_step_size = (solver_d == 'alternate_adaptive')

        uv_hat = uv_hat0.copy()
        u_hat, v_hat = uv_hat[:, :n_channels], uv_hat[:, n_channels:]

        def prox_u(u, step_size=None):
            u /= np.maximum(1., np.linalg.norm(u, axis=1, keepdims=True))
            return u

        def prox_v(v, step_size=None):
            if window:
                v *= tukey_window_
            v /= np.maximum(1., np.linalg.norm(v, axis=1, keepdims=True))
            if window:
                v /= tukey_window_
            return v

        pobj = []
        for jj in range(1):
            # ---------------- update u

            def obj(u):
                uv = np.c_[u, v_hat]
                return objective(uv)

            def grad_u(u):
                if window:
                    uv = np.c_[u, v_hat * tukey_window_]
                else:
                    uv = np.c_[u, v_hat]
                grad_d = gradient_d(uv, X=X, z=z, constants=constants,
                                    loss=loss, loss_params=loss_params)
                return (grad_d * uv[:, None, n_channels:]).sum(axis=2)

            if adaptive_step_size:
                Lu = 1
            else:
                Lu = compute_lipschitz(uv_hat, constants, 'u', b_hat_0)
            assert Lu > 0

            u_hat, pobj_u = fista(obj, grad_u, prox_u, 0.99 / Lu, u_hat,
                                  max_iter, momentum=momentum, eps=eps,
                                  adaptive_step_size=adaptive_step_size,
                                  verbose=verbose, debug=debug,
                                  name="Update u")
            uv_hat = np.c_[u_hat, v_hat]
            if debug:
                pobj.extend(pobj_u)

            # ---------------- update v
            def obj(v):
                uv = np.c_[u_hat, v]
                return objective(uv)

            def grad_v(v):
                if window:
                    v = v * tukey_window_
                uv = np.c_[u_hat, v]
                grad_d = gradient_d(uv, X=X, z=z, constants=constants,
                                    loss=loss, loss_params=loss_params)
                grad_v = (grad_d * uv[:, :n_channels, None]).sum(axis=1)
                if window:
                    grad_v *= tukey_window_
                return grad_v

            if adaptive_step_size:
                Lv = 1
            else:
                Lv = compute_lipschitz(uv_hat, constants, 'v', b_hat_0)
            assert Lv > 0

            v_hat, pobj_v = fista(obj, grad_v, prox_v, 0.99 / Lv, v_hat,
                                  max_iter, momentum=momentum, eps=eps,
                                  adaptive_step_size=adaptive_step_size,
                                  verbose=verbose, debug=debug,
                                  name="Update v")
            uv_hat = np.c_[u_hat, v_hat]
            if debug:
                pobj.extend(pobj_v)

    else:
        raise ValueError('Unknown solver_d: %s' % (solver_d, ))

    if window:
        uv_hat[:, n_channels:] *= tukey_window_

    if debug:
        return uv_hat, pobj
    return uv_hat

def update_d(X, z, D_hat0, constants=None, b_hat_0=None, debug=False,
             max_iter=300, eps=None, solver_d='fista', momentum=False,
             uv_constraint='joint', loss='l2', loss_params=dict(), verbose=0,
             window=False):
    """Learn d's in time domain.

    Parameters
    ----------
    X : array, shape (n_trials, n_channels, n_times)
        The data for sparse coding
    z : array, shape (n_trials, n_atoms, n_times - n_times_atom + 1)
        Can also be a list of n_trials LIL-sparse matrix of shape
            (n_atoms, n_times - n_times_atom + 1)
        The code for which to learn the atoms
    D_hat0 : array, shape (n_atoms, n_channels, n_times_atom)
        The initial atoms.
    constants : dict or None
        Dictionary of constants to accelerate the computation of the gradients.
        It should only be given for loss='l2' and should contain ztz and ztX.
    b_hat_0 : array, shape (n_atoms * (n_channels + n_times_atom))
        Init eigen-vector vector used in power_iteration, used in warm start.
    debug : bool
        If True, return the cost at each iteration.
    momentum : bool
        If True, use an accelerated version of the proximal gradient descent.
    solver_d : str in {'fista'}
        The type of solver to update d:
        If 'fista', the solver optimize D with fista and line search
    loss : str in {'l2' | 'dtw' | 'whitening'}
        The data-fit
    loss_params : dict
        Parameters of the loss
    verbose : int
        Verbosity level.
    window : boolean
        If True, reparametrize the atoms with a temporal Tukey window.

    Returns
    -------
    D_hat : array, shape (n_atoms, n_channels, n_times_atom)
        The atoms to learn from the data.
    """
    n_trials, n_atoms, n_times_valid = get_z_shape(z)
    _, n_channels, n_times = X.shape
    n_atoms, n_channels, n_times_atom = D_hat0.shape

    if window:
        tukey_window_ = tukey_window(n_times_atom)[None, None, :]
        D_hat0 = D_hat0 / tukey_window_

    if loss == 'l2' and constants is None:
        constants = _get_d_update_constants(X, z)

    def objective(D, full=False):
        if window:
            D = D * tukey_window_
        if loss == 'l2':
            return compute_objective(D=D, constants=constants)
        return compute_X_and_objective_multi(X, z, D_hat=D, loss=loss,
                                             loss_params=loss_params)

    if True:  # only solver available here
        # use FISTA on joint [u, v], with an adaptive step size

        def grad(D):
            if window:
                D = D * tukey_window_
            grad = gradient_d(D=D, X=X, z=z, constants=constants, loss=loss,
                              loss_params=loss_params)
            if window:
                grad *= tukey_window_
            return grad

        def prox(D, step_size=None):
            if window:
                D *= tukey_window_
            D = prox_d(D)
            if window:
                D /= tukey_window_
            return D

        D_hat, pobj = fista(objective, grad, prox, None, D_hat0, max_iter,
                            verbose=verbose, momentum=momentum, eps=eps,
                            adaptive_step_size=True, debug=debug,
                            name="Update D")

    else:
        raise ValueError('Unknown solver_d: %s' % (solver_d, ))

    if window:
        D_hat = D_hat * tukey_window_

    if debug:
        return D_hat, pobj
    return D_hat

def _get_d_update_constants(X, z):
    n_trials, n_atoms, n_times_valid = get_z_shape(z)
    n_trials, n_channels, n_times = X.shape
    n_times_atom = n_times - n_times_valid + 1

    if is_list_of_lil(z):
        cython_code._assert_cython()
        ztX = cython_code._fast_compute_ztX(z, X)
        ztz = cython_code._fast_compute_ztz(z, n_times_atom)
    else:
        ztX = compute_ztX(z, X)
        ztz = compute_ztz(z, n_times_atom)

    constants = {}
    constants['ztX'] = ztX
    constants['ztz'] = ztz
    constants['n_channels'] = X.shape[1]
    constants['XtX'] = np.dot(X.ravel(), X.ravel())
    return constants

def compute_lipschitz(uv0, constants, variable, b_hat_0=None,
                      random_state=None):

    n_channels = constants['n_channels']
    u0, v0 = uv0[:, :n_channels], uv0[:, n_channels:]
    n_atoms = uv0.shape[0]
    n_times_atom = uv0.shape[1] - n_channels
    if b_hat_0 is None:
        b_hat_0 = np.random.randn(uv0.size)

    def op_Hu(u):
        u = np.reshape(u, (n_atoms, n_channels))
        uv = np.c_[u, v0]
        H_d = numpy_convolve_uv(constants['ztz'], uv)
        H_u = (H_d * uv[:, None, n_channels:]).sum(axis=2)
        return H_u.ravel()

    def op_Hv(v):
        v = np.reshape(v, (n_atoms, n_times_atom))
        uv = np.c_[u0, v]
        H_d = numpy_convolve_uv(constants['ztz'], uv)
        H_v = (H_d * uv[:, :n_channels, None]).sum(axis=1)
        return H_v.ravel()

    if variable == 'u':
        b_hat_u0 = b_hat_0.reshape(n_atoms, -1)[:, :n_channels].ravel()
        n_points = n_atoms * n_channels
        L = power_iteration(op_Hu, n_points, b_hat_0=b_hat_u0)
    elif variable == 'v':
        b_hat_v0 = b_hat_0.reshape(n_atoms, -1)[:, n_channels:].ravel()
        n_points = n_atoms * n_times_atom
        L = power_iteration(op_Hv, n_points, b_hat_0=b_hat_v0)
    return L

max_stars_repo_name: sophiaas/alphacsc

max_issues_repo_name: sophiaas/alphacsc

max_forks_repo_name: sophiaas/alphacsc


text:
r"""
Building molecular Hamiltonians
===============================

.. meta::
    :property="og:description": Learn how to build electronic Hamiltonians of molecules.

    :property="og:image": https://pennylane.ai/qml/_images/water_structure.png

.. related::
   tutorial_vqe Variational quantum eigensolver

*Author: PennyLane dev team. Last updated: 19 July 2021*

The ultimate goal of computational quantum chemistry is to unravel the
quantum effects that determine the structure and properties of molecules. Reaching
this goal is challenging since the characteristic energies associated with
these effects, e.g., the electronic correlation energy, are typically a tiny fraction
of the total energy of the molecule.

Accurate molecular properties can be computed from the wave function describing the
interacting electrons in a molecule. The **electronic** wave function
:math:`\Psi(r)` satisfies the `Schrödinger equation
<https://en.wikipedia.org/wiki/Schr%C3%B6dinger_equation>`_

.. math::
    H_e \Psi(r) = E \Psi(r),

where :math:`H_e` and :math:`E` denote the electronic Hamiltonian and the
total energy of the molecule, respectively. When solving the latter equation,
the nuclei of the molecule can be treated as point particles whose coordinates
are fixed [#BornOpp1927]_. In this approximation, both the total energy and
the electronic Hamiltonian depend parametrically on the nuclear coordinates.

In this tutorial, you will learn how to use PennyLane to build a
representation of the electronic Hamiltonian :math:`H_e` that can be used to perform
**quantum** simulations of molecules [#yudong2019]_. First, we show how to define
the structure of the molecule in terms of the symbols and the coordinates of
the atoms. Next, we describe how to solve the `Hartree-Fock
equations <https://en.wikipedia.org/wiki/Hartree%E2%80%93Fock_method>`_ for the target
molecule by interfacing with classical quantum chemistry packages. Then, we
outline how to decompose the fermionic Hamiltonian into a set of Pauli operators
whose expectation values can be measured in a quantum computer to estimate the total energy
of the molecule. Finally, we discuss some advanced features that can be used to simulate
more complicated systems.

Let's get started!

Defining the molecular structure
--------------------------------
In this example we construct the electronic Hamiltonian of the water molecule.

.. figure:: ../demonstrations/quantum_chemistry/water_structure.png
    :width: 30%
    :align: center

The structure of a molecule is defined by the symbols and the nuclear coordinates of
its constituent atoms. It can be specified using different `chemical file formats
<https://en.wikipedia.org/wiki/Chemical_file_format>`_. Within PennyLane, the molecular
structure is defined by providing a list with the atomic symbols and a one-dimensional
array with the nuclear coordinates in
`atomic units <https://en.wikipedia.org/wiki/Hartree_atomic_units>`_.
"""
import numpy as np

symbols = ["H", "O", "H"]
coordinates = np.array([-0.0399, -0.0038, 0.0, 1.5780, 0.8540, 0.0, 2.7909, -0.5159, 0.0])

##############################################################################
# The :func:`~.pennylane_qchem.qchem.read_structure` function can also be used to read the
# molecular geometry from an external file.

from pennylane import qchem

symbols, coordinates = qchem.read_structure("h2o.xyz")

##############################################################################
# The xyz format is supported out of the box. If
# `Open Babel <http://openbabel.org/wiki/Main_Page>`_ is installed, any
# format recognized by Open Babel is also supported by PennyLane.
#
# Solving the Hartree-Fock equations
# ----------------------------------
# The molecule's electronic Hamiltonian is commonly represented using the
# `second-quantization <https://en.wikipedia.org/wiki/Second_quantization>`_ formalism,
# which we will explore in more detail in the
# next section. To that aim, a basis of **single-particle** states needs to be chosen.
# In quantum chemistry these states are the
# `molecular orbitals <https://en.wikipedia.org/wiki/Molecular_orbital>`_
# which describe the wave function of a single electron in the molecule.
#
# Molecular orbitals are typically represented as a linear combination of **atomic orbitals**.
# The expansion coefficients in the atomic basis are calculated using the
# `Hartree-Fock (HF) method <https://en.wikipedia.org/wiki/Hartree%E2%80%93Fock_method>`_.
# In the HF approximation, each electron in the molecule is treated as an **independent**
# particle that moves under the influence of the Coulomb potential due to the nuclei, and a mean
# field generated by all other electrons [#pople1977]_. The optimized coefficients are precisely
# what we need to build the second-quantized Hamiltonian.
#
# We can call the function :func:`~.pennylane_qchem.qchem.meanfield` to perform
# the Hartree-Fock calculation using either the quantum chemistry package `PySCF
# <https://sunqm.github.io/pyscf/>`_, or `Psi4 <http://www.psicode.org/>`_. Here
# we use PySCF, which is the default option.

hf_file = qchem.meanfield(symbols, coordinates)

##############################################################################
# This function creates a local file that will later be used to compute the Hamiltonian.
#
# .. _hamiltonian:
#
# Building the Hamiltonian
# ------------------------
# In the second quantization formalism, the electronic wave function of the molecule
# is represented in the occupation number basis. For :math:`M` *spin* molecular
# orbitals, the elements of this basis are labelled as
# :math:`\vert n_0, n_1, \dots, n_{M-1} \rangle`, where :math:`n_i = 0,1`
# indicates the occupation of each orbital. In this representation, the electronic
# Hamiltonian is given by
#
# .. math::
#     H = \sum_{p,q} h_{pq} c_p^\dagger c_q +
#     \frac{1}{2} \sum_{p,q,r,s} h_{pqrs} c_p^\dagger c_q^\dagger c_r c_s,
#
# where :math:`c^\dagger` and :math:`c` are the electron creation
# and annihilation operators, respectively, and the coefficients
# :math:`h_{pq}` and :math:`h_{pqrs}` denote the one- and two-electron
# Coulomb integrals [#ref_integrals]_ evaluated using the Hartree-Fock
# orbitals.
#
# We can use the states of :math:`M` qubits to encode any element
# of the occupation number basis
#
# .. math::
#     \vert n_0, n_1, \dots, n_{M-1} \rangle \rightarrow \vert q_0 q_1 \cdots q_{M-1} \rangle.
#
# This implies that we need to map the fermionic operators onto operators
# that act on qubits. This can be done by using the
# `Jordan-Wigner <https://en.wikipedia.org/wiki/Jordan-Wigner_transformation>`_
# transformation [#seeley2012]_ which allows us to decompose the fermionic Hamiltonian
# into a linear combination of the tensor product of Pauli operators
#
# .. math::
#     H = \sum_j C_j \otimes_i \sigma_i^{(j)},
#
# where :math:`C_j` is a scalar coefficient and :math:`\sigma_i` represents an
# element of the Pauli group :math:`\{ I, X, Y, Z \}`.
#
# This fermionic-to-qubit transformation is done using
# the :func:`~.pennylane_qchem.qchem.decompose` function, which
# uses `OpenFermion <https://github.com/quantumlib/OpenFermion>`_
# to compute the electron integrals using the previously generated
# results of the mean field calculation. Then, it builds the fermionic Hamiltonian and
# maps it to the qubit representation.

qubit_hamiltonian = qchem.decompose(hf_file, mapping="jordan_wigner")
print("Qubit Hamiltonian of the water molecule")
print(qubit_hamiltonian)

##############################################################################
# It is often convenient to use the :func:`~.pennylane_qchem.qchem.molecular_hamiltonian`
# function which encapsulates all the steps explained above. It simplifies the process of building
# the electronic Hamiltonian to a single line of code. We just need to input the
# symbols and the nuclear coordinates of the molecule, as shown below:

H, qubits = qchem.molecular_hamiltonian(symbols, coordinates)
print("Number of qubits: {:}".format(qubits))
print("Qubit Hamiltonian")
print(H)

##############################################################################
# Additionally, if you have built your electronic Hamiltonian independently using
# `OpenFermion <https://github.com/quantumlib/OpenFermion>`_ tools, it can
# be readily converted to a PennyLane observable using the
# :func:`~.pennylane_qchem.qchem.convert_observable` function.
#
# Advanced features
# -----------------
# The :func:`~.pennylane_qchem.qchem.meanfield` function allows us to define additional keyword
# arguments to solve the Hartree-Fock equations of more complicated systems.
# The net charge of the molecule may be specified to simulate positively or negatively
# charged molecules. For a neutral system we choose

charge = 0

##############################################################################
# We can also specify the
# `spin multiplicity <https://en.wikipedia.org/wiki/Multiplicity_(chemistry)>`_. For the
# water molecule, which contains ten electrons, the `Slater determinant
# <https://en.wikipedia.org/wiki/Slater_determinant>`_ resulting from occupying the five
# lowest-energy orbitals with two *paired* electrons per orbital has spin multiplicity one.
# Alternatively, if we define an occupation where the first four orbitals are doubly occupied
# and the next two are singly occupied by *unpaired* electrons, the HF state will have
# multiplicity three.
#
# |
#
# .. figure:: ../demonstrations/quantum_chemistry/hf_references.png
#     :width: 50%
#     :align: center
#
# |
#
# For the neutral water molecule we have,

multiplicity = 1

##############################################################################
# As mentioned above, molecular orbitals are represented as a linear combination
# of atomic orbitals which are typically modeled as `Gaussian-type orbitals
# <https://en.wikipedia.org/wiki/Gaussian_orbital>`_. We can specify different types
# of `Gaussian atomic bases <https://www.basissetexchange.org/>`_. In this example we
# choose a `minimal basis set
# <https://en.wikipedia.org/wiki/Basis_set_(chemistry)#Minimal_basis_sets>`_.

basis_set = "sto-3g"

##############################################################################
# PennyLane also allows us to define an active space [#truhlar2018]_ to perform quantum
# simulations with a reduced number of qubits. This is done by classifying the molecular
# orbitals as core, active, and external orbitals:
#
# * Core orbitals are always occupied by two electrons.
# * Active orbitals can be occupied by zero, one, or two electrons.
# * The external orbitals are never occupied.
#
# Within this approximation, a certain number of **active electrons** are allowed to
# populate a finite set of **active orbitals**.
#
# .. figure:: ../demonstrations/quantum_chemistry/sketch_active_space.png
#     :width: 40%
#     :align: center
#
# .. note::
#     The number of active **spin-orbitals** determines the **number of qubits** required
#     to perform the quantum simulations.
#
# For the water molecule in a minimal basis set we have a total of ten electrons
# and seven molecular orbitals. In this example we define an symmetric active space with
# four electrons and four active orbitals using
# the :func:`~.pennylane_qchem.qchem.active_space` function:

electrons = 10
orbitals = 7
core, active = qchem.active_space(electrons, orbitals, active_electrons=4, active_orbitals=4)

##############################################################################
# Viewing the results:

print("List of core orbitals: {:}".format(core))
print("List of active orbitals: {:}".format(active))
print("Number of qubits: {:}".format(2 * len(active)))

##############################################################################
# Finally, we use the :func:`~.pennylane_qchem.qchem.molecular_hamiltonian` function to
# build the resulting Hamiltonian of the water molecule:

H, qubits = qchem.molecular_hamiltonian(
    symbols,
    coordinates,
    charge=charge,
    mult=multiplicity,
    basis=basis_set,
    active_electrons=4,
    active_orbitals=4,
)

print("Number of qubits required to perform quantum simulations: {:}".format(qubits))
print("Hamiltonian of the water molecule")
print(H)

##############################################################################
# In this case, since we have truncated the basis of molecular orbitals, the resulting
# observable is an approximation of the Hamiltonian generated in the
# section :ref:`hamiltonian`.
#
# You have completed the tutorial! Now, select your favorite molecule and build its electronic
# Hamiltonian.
# To see how simple it is to implement the VQE algorithm to compute the ground-state energy of
# your molecule using PennyLane, take a look at the tutorial :doc:`tutorial_vqe`.
#
# References
# ----------
#
# .. [#yudong2019]
#
#     Yudong Cao, Jonathan Romero, *et al.*, "Quantum Chemistry in the Age of Quantum Computing".
#     `Chem. Rev. 2019, 119, 19, 10856-10915.
#     <https://pubs.acs.org/doi/10.1021/acs.chemrev.8b00803>`_
#
# .. [#BornOpp1927]
#
#     M. Born, J.R. Oppenheimer, "Quantum Theory of the Molecules".
#     `Annalen der Physik 84, 457-484 (1927)
#     <https://onlinelibrary.wiley.com/doi/abs/10.1002/andp.19273892002>`_
#
# .. [#pople1977]
#
#     Rolf Seeger, John Pople. "Self‐consistent molecular orbital methods. XVIII. Constraints and
#     stability in Hartree–Fock theory". `Journal of Chemical Physics 66,
#     3045 (1977). <https://aip.scitation.org/doi/abs/10.1063/1.434318>`_
#
# .. [#ref_integrals]
#
#     J.T. Fermann, E.F. Valeev, "Fundamentals of Molecular Integrals Evaluation".
#     `arXiv:2007.12057 <https://arxiv.org/abs/2007.12057>`_
#
# .. [#seeley2012]
#
#     Jacob T. Seeley, Martin J. Richard, Peter J. Love. "The Bravyi-Kitaev transformation for
#     quantum computation of electronic structure". `Journal of Chemical Physics 137, 224109 (2012).
#     <https://aip.scitation.org/doi/abs/10.1063/1.4768229>`_
#
# .. [#truhlar2018]
#
#     J.J. Bao, S.S. Dong, L. Gagliardi, D.G. Truhlar. "Automatic Selection of an
#     Active Space for Calculating Electronic Excitation Spectra by MS-CASPT2 or MC-PDFT".
#     `Journal of Chemical Theory and Computation 14, 2017 (2018).
#     <https://pubs.acs.org/doi/abs/10.1021/acs.jctc.8b00032>`_


text:
"""
Functions for constructing graphs from surface meshes
"""

import numpy as np
from scipy import sparse

def _get_edges(faces):
    """
    Gets set of edges from `faces`

    Parameters
    ----------
    faces : (F, 3) array_like
        Set of indices creating triangular faces of a mesh

    Returns
    -------
    edges : (F*3, 2) array_like
        All edges in `faces`
    """

    faces = np.asarray(faces)
    edges = np.sort(faces[:, [0, 1, 1, 2, 2, 0]].reshape((-1, 2)), axis=1)

    return edges

def get_direct_edges(vertices, faces):
    """
    Gets (unique) direct edges and weights in mesh describes by inputs.

    Parameters
    ----------
    vertices : (N, 3) array_like
        Coordinates of `vertices` comprising mesh with `faces`
    faces : (F, 3) array_like
        Indices of `vertices` that compose triangular faces of mesh

    Returns
    -------
    edges : (E, 2) array_like
        Indices of `vertices` comprising direct edges (without duplicates)
    weights : (E, 1) array_like
        Distances between `edges`

    """
    edges = np.unique(_get_edges(faces), axis=0)
    weights = np.linalg.norm(np.diff(vertices[edges], axis=1), axis=-1)
    return edges, weights.squeeze()

def get_indirect_edges(vertices, faces):
    """
    Gets indirect edges and weights in mesh described by inputs

    Indirect edges are between two vertices that participate in faces sharing
    an edge

    Parameters
    ----------
    vertices : (N, 3) array_like
        Coordinates of `vertices` comprising mesh with `faces`
    faces : (F, 3) array_like
        Indices of `vertices` that compose triangular faces of mesh

    Returns
    -------
    edges : (E, 2) array_like
        Indices of `vertices` comprising indirect edges (without duplicates)
    weights : (E, 1) array_like
        Distances between `edges` on surface

    References
    ----------
    https://github.com/mikedh/trimesh (MIT licensed)

    """
    # first generate the list of edges for the provided faces and the
    # index for which face the edge is from (which is just the index of the
    # face repeated thrice, since each face generates three direct edges)
    edges = _get_edges(faces)
    edges_face = np.repeat(np.arange(len(faces)), 3)

    # every edge appears twice in a watertight surface, so we'll first get the
    # indices for each duplicate edge in `edges` (this should, assuming all
    # goes well, have rows equal to len(edges) // 2)
    order = np.lexsort(edges.T[::-1])
    edges_sorted = edges[order]
    dupe = np.any(edges_sorted[1:] != edges_sorted[:-1], axis=1)
    dupe_idx = np.append(0, np.nonzero(dupe)[0] + 1)
    start_ok = np.diff(np.concatenate((dupe_idx, [len(edges_sorted)]))) == 2
    groups = np.tile(dupe_idx[start_ok].reshape(-1, 1), 2)
    edge_groups = order[groups + np.arange(2)]

    # now, get the indices of the faces that participate in these duplicate
    # edges, as well as the edges themselves
    adjacency = edges_face[edge_groups]
    nondegenerate = adjacency[:, 0] != adjacency[:, 1]
    adjacency = np.sort(adjacency[nondegenerate], axis=1)
    adjacency_edges = edges[edge_groups[:, 0][nondegenerate]]

    # the non-shared vertex index is the same shape as adjacency, holding
    # vertex indices vs face indices
    indirect_edges = np.zeros(adjacency.shape, dtype=np.int32) - 1

    # loop through the two columns of adjacency
    for i, fid in enumerate(adjacency.T):
        # faces from the current column of adjacency
        face = faces[fid]
        # get index of vertex not included in shared edge
        unshared = np.logical_not(np.logical_or(
            face == adjacency_edges[:, 0].reshape(-1, 1),
            face == adjacency_edges[:, 1].reshape(-1, 1)))
        # each row should have one "uncontained" vertex; ignore degenerates
        row_ok = unshared.sum(axis=1) == 1
        unshared[~row_ok, :] = False
        indirect_edges[row_ok, i] = face[unshared]

    # get vertex coordinates of triangles pairs with shared edges, ordered
    # such that the non-shared vertex is always _last_ among the trio
    shared = np.sort(face[np.logical_not(unshared)].reshape(-1, 1, 2), axis=-1)
    shared = np.repeat(shared, 2, axis=1)
    triangles = np.concatenate((shared, indirect_edges[..., None]), axis=-1)
    # `A.shape`: (3, N, 2) corresponding to (xyz coords, edges, triangle pairs)
    A, B, V = vertices[triangles].transpose(2, 3, 0, 1)

    # calculate the xyz coordinates of the foot of each triangle, where the
    # base is the shared edge
    # that is, we're trying to calculate F in the equation `VF = VB - (w * BA)`
    # where `VF`, `VB`, and `BA` are vectors, and `w = (AB * VB) / (AB ** 2)`
    w = (np.sum((A - B) * (V - B), axis=0, keepdims=True)
         / np.sum((A - B) ** 2, axis=0, keepdims=True))
    feet = B - (w * (B - A))
    # calculate coordinates of midpoint b/w the feet of each pair of triangles
    midpoints = (np.sum(feet.transpose(1, 2, 0), axis=1) / 2)[:, None]
    # calculate Euclidean distance between non-shared vertices and midpoints
    # and add distances together for each pair of triangles
    norms = np.linalg.norm(vertices[indirect_edges] - midpoints, axis=-1)
    weights = np.sum(norms, axis=-1)

    # NOTE: weights won't be perfectly accurate for a small subset of triangle
    # pairs where either triangle has angle >90 along the shared edge. in these
    # the midpoint lies _outside_ the shared edge, so neighboring triangles
    # would need to be taken into account. that said, this occurs in only a
    # minority of cases and the difference tends to be in the ~0.001 mm range
    return indirect_edges, weights

def make_surf_graph(vertices, faces, mask=None):
    """
    Constructs adjacency graph from `surf`.

    Parameters
    ----------
    vertices : (N, 3) array_like
        Coordinates of `vertices` comprising mesh with `faces`
    faces : (F, 3) array_like
        Indices of `vertices` that compose triangular faces of mesh
    mask : (N,) array_like, optional (default None)
        Boolean mask indicating which vertices should be removed from generated
        graph. If not supplied, all vertices are used.

    Returns
    -------
    graph : scipy.sparse.csr_matrix
        Sparse matrix representing graph of `vertices` and `faces`

    Raises
    ------
    ValueError : inconsistent number of vertices in `mask` and `vertices`
    """

    if mask is not None and len(mask) != len(vertices):
        raise ValueError('Supplied `mask` array has different number of '
                         'vertices than supplied `vertices`.')

    # get all (direct + indirect) edges from surface
    direct_edges, direct_weights = get_direct_edges(vertices, faces)
    indirect_edges, indirect_weights = get_indirect_edges(vertices, faces)
    edges = np.row_stack((direct_edges, indirect_edges))
    weights = np.hstack((direct_weights, indirect_weights))

    # remove edges that include a vertex in `mask`
    if mask is not None:
        idx, = np.where(mask)
        mask = ~np.any(np.isin(edges, idx), axis=1)
        edges, weights = edges[mask], weights[mask]

    # construct our graph on which to calculate shortest paths
    return sparse.csr_matrix((np.squeeze(weights), (edges[:, 0], edges[:, 1])),
                             shape=(len(vertices), len(vertices)))

reason: import numpy,from scipy


text:
'''Testing helper_functions.py from NewLambdata package'''

import unittest
import numpy as np
import pandas as pd
from random import randint, sample
import helper_functions as thehelp

class TestNewDataFrameMethods(unittest.TestCase):
    '''Tests null_count Method'''
    def test_nullcount(self):
        test_df = thehelp.NewDataFrame(
                                        np.random.randint(
                                                        0,
                                                        100,
                                                        size=(100, 5)
                                                        ),
                                        columns=list('ABCDE')
                                        )
        self.assertEqual(test_df.nullcount(), 0)

    def test_split_dates(self):
        '''Tests split_dates method'''
        test_list = [
                    "1994-04-09",
                    "1996-12-16",
                    "1995-01-04",
                    "2012-02-07",
                    "2017-01-06",
                    "2013-01-26"
                    ]
        test_df = thehelp.NewDataFrame(test_list, columns=['date'])
        test_df.split_dates()
        self.assertEqual(test_df.shape, (6, 4))

if __name__ == '__main__':
    unittest.main()


text:
# -*- coding: utf-8 -*-

from ..regression import least_squares, irls
from ..testing import requires

@requires("numpy")
def test_irls():
    import numpy as np

    x = np.linspace(0, 100)
    y = np.exp(-x / 47)
    b, c, info = irls(x, np.log(y))
    assert abs(b[1] + 1 / 47) < 1e-5
    assert np.all(c < 1e-4)
    assert info["success"] is True
    assert info["niter"] < 3

@requires("numpy")
def test_least_squares():
    import numpy as np

    x, y, w = [0, 1, 2], [0, 1, 2], [1, 1, 1]
    beta, vcv, r2 = least_squares(x, y)
    assert np.allclose(beta, [0, 1])
    assert np.allclose(vcv, 0)
    assert np.allclose(r2, 1)

    wbeta, wvcv, wr2 = least_squares(x, y, w)
    assert np.allclose(wbeta, [0, 1])
    assert np.allclose(wvcv, 0)
    assert np.allclose(wr2, 1)

max_stars_repo_name: bertiewooster/chempy

max_issues_repo_name: bertiewooster/chempy

max_forks_repo_name: bertiewooster/chempy


text:
"""This module contains functions generally useful for building augmentations."""
import random

import cv2
import numpy as np
from sklearn.datasets import make_blobs

# Generate noise to edges of folding
def add_folding_noise(img, side, p=0.1):
    # side = flag to put more noise at certain side
    #   0  = left side
    #   1  = right side

    # get image dimension
    ysize, xsize = img.shape[:2]

    for y in range(ysize):
        for x in range(xsize):

            if side:  # more noise on right side
                p_score = (((x) / xsize) ** 3) * p  # non linear score with power
            else:  # more noise on left side
                p_score = (((xsize - x) / xsize) ** 3) * p  # non linear score with power

            if p_score > random.random():
                img[y, x] = 0
    return img

# Perspective transform based on 4 points
def four_point_transform(image, pts, dst, xs, ys):
    M = cv2.getPerspectiveTransform(pts, dst)
    if not isinstance(image, np.floating):
        image = image.astype("float")
    img_warped = cv2.warpPerspective(image, M, (xs, ys))
    # return the warped image
    return img_warped

# Transform left side of folding area
def warp_fold_left_side(
    img,
    ysize,
    fold_noise,
    fold_x,
    fold_width_one_side,
    fold_y_shift,
):

    img_fuse = img.copy()

    # 4 vectices of folding area
    xs = 0  # xleft
    xe = fold_width_one_side  # xright
    ys = 0  # ytop
    ye = ysize  # ybottom

    # before distortion
    top_left = [xs, ys]
    top_right = [xe, ys]
    bottom_left = [xs, ye]
    bottom_right = [xe, ye]

    # after distortion
    dtop_left = [xs, ys]
    dtop_right = [xe, ys + fold_y_shift]
    dbottom_left = [xs, ye]
    dbottom_right = [xe, ye + fold_y_shift]

    # image cropping points
    cxs = fold_x
    cxe = fold_x + fold_width_one_side
    cys = 0
    cye = ysize

    # points of folding area
    source_pts = np.array(
        [top_left, bottom_left, bottom_right, top_right],
        dtype=np.float32,
    )
    destination_pts = np.array(
        [dtop_left, dbottom_left, dbottom_right, dtop_right],
        dtype=np.float32,
    )

    # crop section of folding area
    img_crop = img[cys:cye, cxs:cxe]

    # get image dimension of cropped image
    if len(img_crop.shape) > 2:
        cysize, cxsize, cdim = img_crop.shape
    else:
        cysize, cxsize = img_crop.shape
        cdim = 2

    # warp folding area
    img_warped = four_point_transform(
        img_crop,
        source_pts,
        destination_pts,
        cxsize,
        cysize + fold_y_shift,
    )
    img_warped = add_folding_noise(img_warped, 1, fold_noise / 2)

    if cdim > 2:
        img_fuse[cys:cye, cxs:cxe, :] = img_warped[:-fold_y_shift, :, :]
    else:
        img_fuse[cys:cye, cxs:cxe] = img_warped[:-fold_y_shift, :]

    return img_fuse

# Transform right side of folding area
def warp_fold_right_side(
    img,
    ysize,
    fold_noise,
    fold_x,
    fold_width_one_side,
    fold_y_shift,
):

    img_fuse = img.copy()

    # 4 vectices of folding area
    xs = 0  # xleft
    xe = fold_width_one_side  # xright
    ys = 0  # ytop
    ye = ysize  # ybottom

    # before distortion
    top_left = [xs, ys]
    top_right = [xe, ys]
    bottom_left = [xs, ye]
    bottom_right = [xe, ye]

    # after distortion
    dtop_left = [xs, ys + (fold_y_shift)]
    dtop_right = [xe, ys]
    dbottom_left = [xs, ye + (fold_y_shift)]
    dbottom_right = [xe, ye]

    # image cropping points
    cxs = fold_x + fold_width_one_side
    cxe = fold_x + (fold_width_one_side * 2)
    cys = 0
    cye = ysize

    # points of folding area
    source_pts = np.array(
        [top_left, bottom_left, bottom_right, top_right],
        dtype=np.float32,
    )
    destination_pts = np.array(
        [dtop_left, dbottom_left, dbottom_right, dtop_right],
        dtype=np.float32,
    )

    # crop section of folding area
    img_crop = img[cys:cye, cxs:cxe]

    # get image dimension of cropped image
    if len(img_crop.shape) > 2:
        cysize, cxsize, cdim = img_crop.shape
    else:
        cysize, cxsize = img_crop.shape
        cdim = 2

    # warp folding area
    img_warped = four_point_transform(
        img_crop,
        source_pts,
        destination_pts,
        cxsize,
        cysize + fold_y_shift,
    )
    img_warped = add_folding_noise(img_warped, 0, fold_noise / 2)

    if cdim > 2:
        img_fuse[cys:cye, cxs:cxe, :] = img_warped[:-fold_y_shift, :, :]
    else:
        img_fuse[cys:cye, cxs:cxe] = img_warped[:-fold_y_shift, :]

    return img_fuse

def chaikin(points):
    """

    :param points: a list of more than 2 points, where each point is a tuple/array of len=2
    :type points: array
    """
    # appending the first point in path
    path = [points[0]]
    percent = 0.25
    for i in range(len(points) - 1):
        p0 = points[i]
        p1 = points[i + 1]
        # distance between x values of two subsequent points
        dx = p1[0] - p0[0]
        # distance between y values of two subsequent points
        dy = p1[1] - p0[1]
        # creating two new points having 25% and 75% distance from the previous point
        new_p0 = (p0[0] + dx * percent, p0[1] + dy * percent)
        new_p1 = (p0[0] + dx * (1 - percent), p0[1] + dy * (1 - percent))
        path.append(new_p0)
        path.append(new_p1)
    # appending last point in path list
    path.append(points[-1])
    return path

def smooth(points, iter):
    """

    :param points: a list of more than 2 points, where each point is a tuple/array of len=2
    :type points: array
    :param iter: number of times to apply chaikin algorithm
    :type iter: int
    :return:
    """
    for i in range(iter):
        points = chaikin(points)
    return points

def add_noise(image, intensity_range=(0.1, 0.2), color_range=(0, 224)):
    """Applies random noise to the input image.

    :param image: The image to noise.
    :type image: numpy.array
    :param intensity_range: Pair of bounds for intensity sample.
    :type intensity_range: tuple, optional
    :param color_range: Pair of bounds for 8-bit colors.
    :type color_range: tuple, optional
    """

    intensity = random.uniform(intensity_range[0], intensity_range[1])
    noise = lambda x: random.randint(color_range[0], color_range[1]) if (x == 0 and random.random() < intensity) else x
    add_noise = np.vectorize(noise)

    return add_noise(image)

def _create_blob(
    size_range=(10, 20),
    points_range=(5, 25),
    std_range=(10, 75),
    features_range=(15, 25),
    value_range=(180, 250),
):
    """Generates a Gaussian noise blob for placement in an image.
    To be used with _apply_blob()

    :param size_range: Pair of ints determining the range from which the
           diameter of a blob is sampled.
    :type size_range: tuple, optional
    :param points_range: Pair of ints determining the range from which the
           number of points in a blob is sampled.
    :type points_range: tuple, optional
    :param std_range: Pair of ints determining the range from which the
           standard deviation of the blob distribution is sampled.
    :type std_range: tuple, optional
    :param features_range: Pair of ints determining the range from which the
           number of features in the blob is sampled.
    :type features_range: tuple, optional
    :param value_range: Pair of ints determining the range from which the
           value of a point in the blob is sampled.
    :type value_range: tuple, optional
    """
    size = random.randint(size_range[0], size_range[1])
    std = random.randint(std_range[0], std_range[1]) / 100
    points = random.randint(points_range[0], points_range[1])
    features = random.randint(features_range[0], features_range[1])

    X, y = make_blobs(
        n_samples=points,
        cluster_std=[std],
        centers=[(0, 0)],
        n_features=features,
    )  # , random_state=1)
    X *= size // 4
    X += size // 2
    X = [[int(item) for item in items] for items in X]
    blob = np.full((size, size, 1), 0, dtype="uint8")

    for point in X:
        if point[0] < blob.shape[0] and point[1] < blob.shape[1] and point[0] > 0 and point[1] > 0:
            value = random.randint(value_range[0], value_range[1])
            blob[point[0], point[1]] = value

    return blob

def apply_blob(
    mask,
    size_range=(10, 20),
    points_range=(5, 25),
    std_range=(10, 75),
    features_range=(15, 25),
    value_range=(180, 250),
):
    """Places a Gaussian blob at a random location in the image.

    :param mask: The image to place the blob in.
    :type mask: numpy.array
    :param size_range: Pair of ints determining the range from which the
           diameter of a blob is sampled.
    :type size_range: tuple, optional
    :param points_range: Pair of ints determining the range from which the
           number of points in a blob is sampled.
    :type points_range: tuple, optional
    :param std_range: Pair of ints determining the range from which the
           standard deviation of the blob distribution is sampled.
    :type std_range: tuple, optional
    :param features_range: Pair of ints determining the range from which the
           number of features in the blob is sampled.
    :type features_range: tuple, optional
    :param value_range: Pair of ints determining the range from which the
           value of a point in the blob is sampled.
    :type value_range: tuple, optional
    """
    dim = min(
        mask.shape[0],
        mask.shape[1],
    )  # we don't want to generate blobs larger than the mask

    # temporary local variables, in case
    size = size_range
    std = std_range

    # make sure we don't generate a blob larger than the mask
    if 2 * (size_range[1] + std_range[1]) > dim:
        # don't make a radius that won't fit in our mask
        size = (1, dim // 2 - 1)
        # don't make a std.deviation that when added to radius, is larger than mask
        std = (0, dim // 2 - size[1])

    if size[1] < size[0]:
        return mask  # solve error where size[1] is smaller than size[0]

    blob = _create_blob(size, points_range, std, features_range, value_range)

    x_start = random.randint(0, mask.shape[1] - blob.shape[1])
    y_start = random.randint(0, mask.shape[0] - blob.shape[0])
    x_stop = x_start + blob.shape[1]
    y_stop = y_start + blob.shape[0]

    mask_chunk = mask[y_start:y_stop, x_start:x_stop]

    apply_chunk = np.vectorize(lambda x, y: max(x, y))

    mask_dim = len(mask.shape)  # mask channels
    if mask_dim > 2:  # colour image or > 3 channels
        for i in range(mask_dim):
            mask[y_start:y_stop, x_start:x_stop, i] = apply_chunk(
                mask_chunk[:, :, i],
                blob[:, :, 0],
            )
    else:  # single channel grayscale or binary image
        mask[y_start:y_stop, x_start:x_stop] = apply_chunk(
            mask_chunk[:, :],
            blob[:, :, 0],
        )

    return mask

def binary_threshold(
    img,
    enable_otsu=0,
    enable_simple=0,
    simple_method=cv2.THRESH_BINARY,
    thres=127,
    max_value=255,
    enable_adaptive=0,
    adaptive_method=cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
    block_size=21,
    C=10,
):
    """Converts img to grayscale and applies the given threshold type

    :param img: the image to binarize
    :type img: numpy.array

    :param enable_otsu: Flag to enable otsu binarization method
    :type enable_otsu: Int, optional

    :param enable_simple: Flag to enable simple binarization method
    :type enable_simple: Int, optional
    :param simple_method: Simple binarization method
    :type simple_method: cv2.THRESH_ value, optional
    :param thres: Thresholding value for simple binarization method
    :type thres: Int, optional
    :param max_value: Max value of image for simple binarization method
    :type max_value: Int, optional

    :param enable_adaptive: Flag to enable adaptive binarization method
    :type enable_adaptive: Int, optional
    :param adaptive_method: Adaptive binarization method
    :type adaptive_method: cv2.THRESH_ value, optional
    :param block_size: Size of a pixel neighborhood that is used to calculate
                        a threshold value for adaptive method
    :type block_size: Int, optional
    :param C: Constant subtracted from the mean or weighted mean, only for
                        adaptive method
    :type C: Int, optional
    """

    # otsu as default method
    if not enable_otsu and not enable_simple and not enable_adaptive:
        enable_otsu = 1

    # convert to grayscale
    if len(img.shape) > 2:
        grayscale = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    else:
        grayscale = img

    # otsu method:
    if enable_otsu:
        ret, thresholded = cv2.threshold(grayscale, 0, 255, cv2.THRESH_OTSU)
    # simple method
    elif enable_simple:
        simple_methods = [
            cv2.THRESH_BINARY,
            cv2.THRESH_BINARY_INV,
            cv2.THRESH_TRUNC,
            cv2.THRESH_TOZERO,
            cv2.THRESH_TOZERO_INV,
        ]
        if simple_method in simple_methods:
            ret, thresholded = cv2.threshold(grayscale, thres, max_value, simple_method)
        else:
            raise TypeError("Invalid thresholding method.")
    # adaptive method
    elif enable_adaptive:
        adaptive_methods = [cv2.ADAPTIVE_THRESH_MEAN_C, cv2.ADAPTIVE_THRESH_GAUSSIAN_C]
        if adaptive_method in adaptive_methods:
            thresholded = cv2.adaptiveThreshold(
                grayscale,
                255,
                adaptive_method,
                cv2.THRESH_BINARY,
                block_size,
                C,
            )
        else:
            raise TypeError("Invalid thresholding method.")

    return thresholded

def sobel(image):
    """Computes the gradient of the image intensity function.

    :param image: The image over which to create an edge mask.
    :type image: numpy.array
    """

    gradX = cv2.Sobel(image, ddepth=cv2.CV_32F, dx=1, dy=0, ksize=-1)
    gradY = cv2.Sobel(image, ddepth=cv2.CV_32F, dx=0, dy=1, ksize=-1)
    gradient = cv2.subtract(gradX, gradY)
    gradient = cv2.convertScaleAbs(gradient)
    return gradient

def make_white_transparent(img, ink_color=0):
    # Create the Ink Layer for the specified color.
    img_bgra = cv2.cvtColor(
        np.full((img.shape[0], img.shape[1], 3), ink_color, dtype="uint8"),
        cv2.COLOR_BGR2BGRA,
    )

    # Convert to grayscale if not already.
    if len(img.shape) > 2 and img.shape[2] > 1:
        img = cv2.cvtColor(img.astype(np.single), cv2.COLOR_BGR2GRAY)

    # Apply transparency mask based on grayscale.
    img_bgra[:, :, 3] = ~(img[:, :].astype(np.int64))
    return img_bgra

max_stars_repo_name: RyonSayer/augraphy

max_issues_repo_name: RyonSayer/augraphy

max_forks_repo_name: RyonSayer/augraphy


text:
import sympy

# Define
state_vector = x1, x2, e1, e2 = sympy.symbols('x1 x2 e1 e2')

# Define controller (in etc form)
u1 = -(x2+e2) - (x1+e1)**2*(x2+e2) - (x2+e2)**3

# Define dynamics
x1dot = -x1
x2dot = x1**2*x2 + x2**3 + u1
dynamics = [x1dot, x2dot, -x1dot, -x2dot]

# Triggering condition & other etc.
trigger = e1 ** 2 + e2 ** 2 - 0.01**2

# State space limits
state_space_limits = [[-2, 2], [-2, 2]]
grid_points_per_dim = [3,3]

import ETCetera.Abstractions as abstr

# Partition method manifold
traffic = abstr.TrafficModelNonlinearETC(dynamics, trigger, state_vector, homogeneity=2, state_space_limits=state_space_limits, grid_points_per_dim=grid_points_per_dim, manifolds_times=[0.002, 0.0028, 0.0038, 0.005, 0.0065, 0.0075], partition_method='manifold', heartbeat=0.021, order_approx=4)
regions, transitions = traffic.create_abstraction()
# Result: {}

# Access regions/transitions in alternative way
regions = traffic.regions
transitions = traffic.transitions

print(regions)
print(transitions)

# # Get symbolic expressions for the regions
region_descriptors = traffic.return_region_descriptors()

print(region_descriptors)

# Visualize results
traffic.visualize()

# To pickle the object:
traffic.export('traffic_etc', 'pickle')

# To save to a .json file:
traffic.export('traffic_etc', 'json')

# To save to a text file:
traffic.export('traffic_etc', 'txt')

max_stars_repo_name: ggleizer/ETCetera

max_issues_repo_name: ggleizer/ETCetera

max_forks_repo_name: ggleizer/ETCetera

reason: import sympy


text:
#!/usr/bin/env python
# coding: utf-8

# In[1]:

import json
import re
import time

import numpy as np
import tensorflow as tf
from sklearn.cross_validation import train_test_split
from sklearn.metrics import classification_report
from sklearn.model_selection import KFold
from tqdm import tqdm

from keras.utils import to_categorical

# In[2]:

def process_string(string):
    string = re.sub("[^A-Za-z0-9\-\/ ]+", " ", string).split()
    return [to_title(y.strip()) for y in string]

def to_title(string):
    if string.isupper():
        string = string.title()
    return string

# In[3]:

with open("pos-data-v3.json", "r") as fopen:
    dataset = json.load(fopen)

# In[4]:

texts, labels = [], []
for i in dataset:
    try:
        texts.append(process_string(i[0])[0].lower())
        labels.append(i[-1])
    except Exception as e:
        print(e, i)

# In[5]:

word2idx = {"PAD": 0, "NUM": 1, "UNK": 2}
tag2idx = {"PAD": 0}
char2idx = {"PAD": 0}
word_idx = 3
tag_idx = 1
char_idx = 1

def parse_XY(texts, labels):
    global word2idx, tag2idx, char2idx, word_idx, tag_idx, char_idx
    X, Y = [], []
    for no, text in enumerate(texts):
        text = to_title(text)
        tag = labels[no]
        for c in text:
            if c not in char2idx:
                char2idx[c] = char_idx
                char_idx += 1
        if tag not in tag2idx:
            tag2idx[tag] = tag_idx
            tag_idx += 1
        Y.append(tag2idx[tag])
        if text not in word2idx:
            word2idx[text] = word_idx
            word_idx += 1
        X.append(word2idx[text])
    return X, np.array(Y)

# In[6]:

X, Y = parse_XY(texts, labels)
idx2word = {idx: tag for tag, idx in word2idx.items()}
idx2tag = {i: w for w, i in tag2idx.items()}

# In[7]:

seq_len = 50

def iter_seq(x):
    return np.array([x[i : i + seq_len] for i in range(0, len(x) - seq_len, 1)])

def to_train_seq(*args):
    return [iter_seq(x) for x in args]

def generate_char_seq(batch):
    x = [[len(idx2word[i]) for i in k] for k in batch]
    maxlen = max([j for i in x for j in i])
    temp = np.zeros((batch.shape[0], batch.shape[1], maxlen), dtype=np.int32)
    for i in range(batch.shape[0]):
        for k in range(batch.shape[1]):
            for no, c in enumerate(idx2word[batch[i, k]]):
                temp[i, k, -1 - no] = char2idx[c]
    return temp

# In[8]:

X_seq, Y_seq = to_train_seq(X, Y)
X_char_seq = generate_char_seq(X_seq)
X_seq.shape

# In[9]:

with open("bahdanau-pos.json", "w") as fopen:
    fopen.write(
        json.dumps(
            {
                "idx2tag": idx2tag,
                "idx2word": idx2word,
                "word2idx": word2idx,
                "tag2idx": tag2idx,
                "char2idx": char2idx,
            }
        )
    )

# In[10]:

Y_seq_3d = [to_categorical(i, num_classes=len(tag2idx)) for i in Y_seq]

# In[11]:

train_X, test_X, train_Y, test_Y, train_char, test_char = train_test_split(
    X_seq, Y_seq_3d, X_char_seq, test_size=0.1
)

# In[12]:

class Model:
    def __init__(
        self,
        dim_word,
        dim_char,
        dropout,
        learning_rate,
        hidden_size_char,
        hidden_size_word,
        num_layers,
    ):
        def cells(size, reuse=False):
            return tf.contrib.rnn.DropoutWrapper(
                tf.nn.rnn_cell.LSTMCell(size, initializer=tf.orthogonal_initializer(), reuse=reuse),
                state_keep_prob=dropout,
                output_keep_prob=dropout,
            )

        def bahdanau(embedded, size):
            attention_mechanism = tf.contrib.seq2seq.BahdanauAttention(
                num_units=hidden_size_word, memory=embedded
            )
            return tf.contrib.seq2seq.AttentionWrapper(
                cell=cells(hidden_size_word),
                attention_mechanism=attention_mechanism,
                attention_layer_size=hidden_size_word,
            )

        self.word_ids = tf.placeholder(tf.int32, shape=[None, None])
        self.char_ids = tf.placeholder(tf.int32, shape=[None, None, None])
        self.labels = tf.placeholder(tf.int32, shape=[None, None, None])
        self.maxlen = tf.shape(self.word_ids)[1]
        self.lengths = tf.count_nonzero(self.word_ids, 1)

        self.word_embeddings = tf.Variable(
            tf.truncated_normal([len(word2idx), dim_word], stddev=1.0 / np.sqrt(dim_word))
        )
        self.char_embeddings = tf.Variable(
            tf.truncated_normal([len(char2idx), dim_char], stddev=1.0 / np.sqrt(dim_char))
        )

        word_embedded = tf.nn.embedding_lookup(self.word_embeddings, self.word_ids)
        char_embedded = tf.nn.embedding_lookup(self.char_embeddings, self.char_ids)
        s = tf.shape(char_embedded)
        char_embedded = tf.reshape(char_embedded, shape=[s[0] * s[1], s[-2], dim_char])

        for n in range(num_layers):
            (out_fw, out_bw), (state_fw, state_bw) = tf.nn.bidirectional_dynamic_rnn(
                cell_fw=cells(hidden_size_char),
                cell_bw=cells(hidden_size_char),
                inputs=char_embedded,
                dtype=tf.float32,
                scope="bidirectional_rnn_char_%d" % (n),
            )
            char_embedded = tf.concat((out_fw, out_bw), 2)
        output = tf.reshape(char_embedded[:, -1], shape=[s[0], s[1], 2 * hidden_size_char])
        word_embedded = tf.concat([word_embedded, output], axis=-1)

        for n in range(num_layers):
            (out_fw, out_bw), (state_fw, state_bw) = tf.nn.bidirectional_dynamic_rnn(
                cell_fw=bahdanau(word_embedded, hidden_size_word),
                cell_bw=bahdanau(word_embedded, hidden_size_word),
                inputs=word_embedded,
                dtype=tf.float32,
                scope="bidirectional_rnn_word_%d" % (n),
            )
            word_embedded = tf.concat((out_fw, out_bw), 2)

        logits = tf.layers.dense(word_embedded, len(idx2tag))
        y_t = tf.argmax(self.labels, 2)
        log_likelihood, transition_params = tf.contrib.crf.crf_log_likelihood(
            logits, y_t, self.lengths
        )
        self.cost = tf.reduce_mean(-log_likelihood)
        self.optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(self.cost)
        mask = tf.sequence_mask(self.lengths, maxlen=self.maxlen)
        self.tags_seq, tags_score = tf.contrib.crf.crf_decode(
            logits, transition_params, self.lengths
        )
        self.tags_seq = tf.identity(self.tags_seq, name="logits")

        y_t = tf.cast(y_t, tf.int32)
        self.prediction = tf.boolean_mask(self.tags_seq, mask)
        mask_label = tf.boolean_mask(y_t, mask)
        correct_pred = tf.equal(self.prediction, mask_label)
        correct_index = tf.cast(correct_pred, tf.float32)
        self.accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))

# In[13]:

tf.reset_default_graph()
sess = tf.InteractiveSession()

dim_word = 128
dim_char = 256
dropout = 0.8
learning_rate = 1e-3
hidden_size_char = 64
hidden_size_word = 64
num_layers = 2
batch_size = 32

model = Model(
    dim_word, dim_char, dropout, learning_rate, hidden_size_char, hidden_size_word, num_layers
)
sess.run(tf.global_variables_initializer())

# In[14]:

for e in range(2):
    lasttime = time.time()
    train_acc, train_loss, test_acc, test_loss = 0, 0, 0, 0
    pbar = tqdm(range(0, len(train_X), batch_size), desc="train minibatch loop")
    for i in pbar:
        batch_x = train_X[i : min(i + batch_size, train_X.shape[0])]
        batch_char = train_char[i : min(i + batch_size, train_X.shape[0])]
        batch_y = train_Y[i : min(i + batch_size, train_X.shape[0])]
        acc, cost, _ = sess.run(
            [model.accuracy, model.cost, model.optimizer],
            feed_dict={model.word_ids: batch_x, model.char_ids: batch_char, model.labels: batch_y},
        )
        assert not np.isnan(cost)
        train_loss += cost
        train_acc += acc
        pbar.set_postfix(cost=cost, accuracy=acc)

    pbar = tqdm(range(0, len(test_X), batch_size), desc="test minibatch loop")
    for i in pbar:
        batch_x = test_X[i : min(i + batch_size, test_X.shape[0])]
        batch_char = test_char[i : min(i + batch_size, test_X.shape[0])]
        batch_y = test_Y[i : min(i + batch_size, test_X.shape[0])]
        acc, cost = sess.run(
            [model.accuracy, model.cost],
            feed_dict={model.word_ids: batch_x, model.char_ids: batch_char, model.labels: batch_y},
        )
        assert not np.isnan(cost)
        test_loss += cost
        test_acc += acc
        pbar.set_postfix(cost=cost, accuracy=acc)

    train_loss /= len(train_X) / batch_size
    train_acc /= len(train_X) / batch_size
    test_loss /= len(test_X) / batch_size
    test_acc /= len(test_X) / batch_size

    print("time taken:", time.time() - lasttime)
    print(
        "epoch: %d, training loss: %f, training acc: %f, valid loss: %f, valid acc: %f\n"
        % (e, train_loss, train_acc, test_loss, test_acc)
    )

# In[15]:

def pred2label(pred):
    out = []
    for pred_i in pred:
        out_i = []
        for p in pred_i:
            out_i.append(idx2tag[p])
        out.append(out_i)
    return out

# In[16]:

real_Y, predict_Y = [], []

pbar = tqdm(range(0, len(test_X), batch_size), desc="validation minibatch loop")
for i in pbar:
    batch_x = test_X[i : min(i + batch_size, test_X.shape[0])]
    batch_char = test_char[i : min(i + batch_size, test_X.shape[0])]
    batch_y = test_Y[i : min(i + batch_size, test_X.shape[0])]
    predicted = pred2label(
        sess.run(model.tags_seq, feed_dict={model.word_ids: batch_x, model.char_ids: batch_char})
    )
    real = pred2label(np.argmax(batch_y, axis=2))
    predict_Y.extend(predicted)
    real_Y.extend(real)

# In[17]:

print(classification_report(np.array(real_Y).ravel(), np.array(predict_Y).ravel()))

# In[18]:

saver = tf.train.Saver(tf.trainable_variables())
saver.save(sess, "concat-bidirectional-bahdanau-pos/model.ckpt")

strings = ",".join(
    [
        n.name
        for n in tf.get_default_graph().as_graph_def().node
        if (
            "Variable" in n.op
            or "Placeholder" in n.name
            or "logits" in n.name
            or "alphas" in n.name
        )
        and "Adam" not in n.name
        and "beta" not in n.name
        and "OptimizeLoss" not in n.name
        and "Global_Step" not in n.name
    ]
)
strings.split(",")

# In[19]:

def freeze_graph(model_dir, output_node_names):

    if not tf.gfile.Exists(model_dir):
        raise AssertionError(
            "Export directory doesn't exists. Please specify an export " "directory: %s" % model_dir
        )

    checkpoint = tf.train.get_checkpoint_state(model_dir)
    input_checkpoint = checkpoint.model_checkpoint_path

    absolute_model_dir = "/".join(input_checkpoint.split("/")[:-1])
    output_graph = absolute_model_dir + "/frozen_model.pb"
    clear_devices = True
    with tf.Session(graph=tf.Graph()) as sess:
        saver = tf.train.import_meta_graph(input_checkpoint + ".meta", clear_devices=clear_devices)
        saver.restore(sess, input_checkpoint)
        output_graph_def = tf.graph_util.convert_variables_to_constants(
            sess, tf.get_default_graph().as_graph_def(), output_node_names.split(",")
        )
        with tf.gfile.GFile(output_graph, "wb") as f:
            f.write(output_graph_def.SerializeToString())
        print("%d ops in the final graph." % len(output_graph_def.node))

def load_graph(frozen_graph_filename):
    with tf.gfile.GFile(frozen_graph_filename, "rb") as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())
    with tf.Graph().as_default() as graph:
        tf.import_graph_def(graph_def)
    return graph

# In[20]:

freeze_graph("concat-bidirectional-bahdanau-pos", strings)

# In[21]:

g = load_graph("concat-bidirectional-bahdanau-pos/frozen_model.pb")

# In[22]:

string = "KUALA LUMPUR: Sempena sambutan Aidilfitri minggu depan, Perdana Menteri Tun Dr Mahathir Mohamad dan Menteri Pengangkutan Anthony Loke Siew Fook menitipkan pesanan khas kepada orang ramai yang mahu pulang ke kampung halaman masing-masing. Dalam video pendek terbitan Jabatan Keselamatan Jalan Raya (JKJR) itu, Dr Mahathir menasihati mereka supaya berhenti berehat dan tidur sebentar  sekiranya mengantuk ketika memandu."

# In[23]:

def char_str_idx(corpus, dic, UNK=0):
    maxlen = max([len(i) for i in corpus])
    X = np.zeros((len(corpus), maxlen))
    for i in range(len(corpus)):
        for no, k in enumerate(corpus[i][:maxlen][::-1]):
            val = dic[k] if k in dic else UNK
            X[i, -1 - no] = val
    return X

def generate_char_seq(batch, idx2word, char2idx):
    x = [[len(idx2word[i]) for i in k] for k in batch]
    maxlen = max([j for i in x for j in i])
    temp = np.zeros((batch.shape[0], batch.shape[1], maxlen), dtype=np.int32)
    for i in range(batch.shape[0]):
        for k in range(batch.shape[1]):
            for no, c in enumerate(idx2word[batch[i, k]].lower()):
                temp[i, k, -1 - no] = char2idx[c]
    return temp

sequence = process_string(string)
X_seq = char_str_idx([sequence], word2idx, 2)
X_char_seq = generate_char_seq(X_seq, idx2word, char2idx)

# In[24]:

word_ids = g.get_tensor_by_name("import/Placeholder:0")
char_ids = g.get_tensor_by_name("import/Placeholder_1:0")
tags_seq = g.get_tensor_by_name("import/logits:0")
test_sess = tf.InteractiveSession(graph=g)
predicted = test_sess.run(tags_seq, feed_dict={word_ids: X_seq, char_ids: X_char_seq})[0]

for i in range(len(predicted)):
    print(sequence[i], idx2tag[predicted[i]])

# In[ ]:

max_stars_repo_name: gitter-badger/mlmodels


text:
# Copyright (c) OpenMMLab. All rights reserved.
import mmcv
import numpy as np
import pytest
import torch

from mmdeploy.codebase import import_codebase
from mmdeploy.utils import Backend, Codebase
from mmdeploy.utils.test import (WrapFunction, backend_checker, check_backend,
                                 get_onnx_model, get_rewrite_outputs)

try:
    import_codebase(Codebase.MMROTATE)
except ImportError:
    pytest.skip(
        f'{Codebase.MMROTATE} is not installed.', allow_module_level=True)

@backend_checker(Backend.ONNXRUNTIME)
def test_multiclass_nms_rotated():
    from mmdeploy.codebase.mmrotate.core import multiclass_nms_rotated
    deploy_cfg = mmcv.Config(
        dict(
            onnx_config=dict(output_names=None, input_shape=None),
            backend_config=dict(
                type='onnxruntime',
                common_config=dict(
                    fp16_mode=False, max_workspace_size=1 << 20),
                model_inputs=[
                    dict(
                        input_shapes=dict(
                            boxes=dict(
                                min_shape=[1, 5, 5],
                                opt_shape=[1, 5, 5],
                                max_shape=[1, 5, 5]),
                            scores=dict(
                                min_shape=[1, 5, 8],
                                opt_shape=[1, 5, 8],
                                max_shape=[1, 5, 8])))
                ]),
            codebase_config=dict(
                type='mmrotate',
                task='RotatedDetection',
                post_processing=dict(
                    score_threshold=0.05,
                    iou_threshold=0.5,
                    pre_top_k=-1,
                    keep_top_k=10,
                ))))

    boxes = torch.rand(1, 5, 5)
    scores = torch.rand(1, 5, 8)
    keep_top_k = 10
    wrapped_func = WrapFunction(multiclass_nms_rotated, keep_top_k=keep_top_k)
    rewrite_outputs, _ = get_rewrite_outputs(
        wrapped_func,
        model_inputs={
            'boxes': boxes,
            'scores': scores
        },
        deploy_cfg=deploy_cfg)

    assert rewrite_outputs is not None, 'Got unexpected rewrite '\
        'outputs: {}'.format(rewrite_outputs)

@backend_checker(Backend.ONNXRUNTIME)
@pytest.mark.parametrize('pre_top_k', [-1, 1000])
def test_multiclass_nms_rotated_with_keep_top_k(pre_top_k):
    backend_type = 'onnxruntime'

    from mmdeploy.codebase.mmrotate.core import multiclass_nms_rotated
    keep_top_k = 15
    deploy_cfg = mmcv.Config(
        dict(
            onnx_config=dict(
                output_names=None,
                input_shape=None,
                dynamic_axes=dict(
                    boxes={
                        0: 'batch_size',
                        1: 'num_boxes'
                    },
                    scores={
                        0: 'batch_size',
                        1: 'num_boxes',
                        2: 'num_classes'
                    },
                ),
            ),
            backend_config=dict(type=backend_type),
            codebase_config=dict(
                type='mmrotate',
                task='RotatedDetection',
                post_processing=dict(
                    score_threshold=0.05,
                    iou_threshold=0.5,
                    pre_top_k=pre_top_k,
                    keep_top_k=keep_top_k,
                ))))

    num_classes = 5
    num_boxes = 2
    batch_size = 1
    export_boxes = torch.rand(batch_size, num_boxes, 5)
    export_scores = torch.ones(batch_size, num_boxes, num_classes)
    model_inputs = {'boxes': export_boxes, 'scores': export_scores}

    wrapped_func = WrapFunction(multiclass_nms_rotated, keep_top_k=keep_top_k)

    onnx_model_path = get_onnx_model(
        wrapped_func, model_inputs=model_inputs, deploy_cfg=deploy_cfg)

    num_boxes = 100
    test_boxes = torch.rand(batch_size, num_boxes, 5)
    test_scores = torch.ones(batch_size, num_boxes, num_classes)
    model_inputs = {'boxes': test_boxes, 'scores': test_scores}

    import mmdeploy.backend.onnxruntime as ort_apis
    backend_model = ort_apis.ORTWrapper(onnx_model_path, 'cuda:0', None)
    output = backend_model.forward(model_inputs)
    output = backend_model.output_to_list(output)
    dets = output[0]

    # Subtract 1 dim since we pad the tensors
    assert dets.shape[1] - 1 < keep_top_k, \
        'multiclass_nms_rotated returned more values than "keep_top_k"\n' \
        f'dets.shape: {dets.shape}\n' \
        f'keep_top_k: {keep_top_k}'

@pytest.mark.parametrize('backend_type', [Backend.ONNXRUNTIME])
@pytest.mark.parametrize('add_ctr_clamp', [True, False])
@pytest.mark.parametrize('max_shape,proj_xy,edge_swap',
                         [(None, False, False),
                          (torch.tensor([100, 200]), True, True)])
def test_delta_xywha_rbbox_coder_delta2bbox(backend_type: Backend,
                                            add_ctr_clamp: bool,
                                            max_shape: tuple, proj_xy: bool,
                                            edge_swap: bool):
    check_backend(backend_type)
    deploy_cfg = mmcv.Config(
        dict(
            onnx_config=dict(output_names=None, input_shape=None),
            backend_config=dict(type=backend_type.value, model_inputs=None),
            codebase_config=dict(type='mmrotate', task='RotatedDetection')))

    # wrap function to enable rewrite
    def delta2bbox(*args, **kwargs):
        import mmrotate
        return mmrotate.core.bbox.coder.delta_xywha_rbbox_coder.delta2bbox(
            *args, **kwargs)

    rois = torch.rand(5, 5)
    deltas = torch.rand(5, 5)
    original_outputs = delta2bbox(
        rois,
        deltas,
        max_shape=max_shape,
        add_ctr_clamp=add_ctr_clamp,
        proj_xy=proj_xy,
        edge_swap=edge_swap)

    # wrap function to nn.Module, enable torch.onnx.export
    wrapped_func = WrapFunction(
        delta2bbox,
        max_shape=max_shape,
        add_ctr_clamp=add_ctr_clamp,
        proj_xy=proj_xy,
        edge_swap=edge_swap)
    rewrite_outputs, is_backend_output = get_rewrite_outputs(
        wrapped_func,
        model_inputs={
            'rois': rois.unsqueeze(0),
            'deltas': deltas.unsqueeze(0)
        },
        deploy_cfg=deploy_cfg)

    if is_backend_output:
        model_output = original_outputs.squeeze().cpu().numpy()
        rewrite_output = rewrite_outputs[0].squeeze().cpu().numpy()
        assert np.allclose(
            model_output, rewrite_output, rtol=1e-03, atol=1e-05)
    else:
        assert rewrite_outputs is not None

@pytest.mark.parametrize('backend_type', [Backend.ONNXRUNTIME])
def test_delta_midpointoffset_rbbox_delta2bbox(backend_type: Backend):
    check_backend(backend_type)
    deploy_cfg = mmcv.Config(
        dict(
            onnx_config=dict(output_names=None, input_shape=None),
            backend_config=dict(type=backend_type.value, model_inputs=None),
            codebase_config=dict(type='mmrotate', task='RotatedDetection')))

    # wrap function to enable rewrite
    def delta2bbox(*args, **kwargs):
        import mmrotate
        return mmrotate.core.bbox.coder.delta_midpointoffset_rbbox_coder\
            .delta2bbox(*args, **kwargs)

    rois = torch.rand(5, 4)
    deltas = torch.rand(5, 6)
    original_outputs = delta2bbox(rois, deltas, version='le90')

    # wrap function to nn.Module, enable torch.onnx.export
    wrapped_func = WrapFunction(delta2bbox)
    rewrite_outputs, is_backend_output = get_rewrite_outputs(
        wrapped_func,
        model_inputs={
            'rois': rois.unsqueeze(0),
            'deltas': deltas.unsqueeze(0)
        },
        deploy_cfg=deploy_cfg)

    if is_backend_output:
        model_output = original_outputs.squeeze().cpu().numpy()
        rewrite_output = rewrite_outputs[0].squeeze().cpu().numpy()
        assert np.allclose(
            model_output[:, :4], rewrite_output[:, :4], rtol=1e-03, atol=1e-05)
    else:
        assert rewrite_outputs is not None

@backend_checker(Backend.ONNXRUNTIME)
def test_fake_multiclass_nms_rotated():
    from mmdeploy.codebase.mmrotate.core import fake_multiclass_nms_rotated
    deploy_cfg = mmcv.Config(
        dict(
            onnx_config=dict(output_names=None, input_shape=None),
            backend_config=dict(
                type='onnxruntime',
                common_config=dict(
                    fp16_mode=False, max_workspace_size=1 << 20),
                model_inputs=[
                    dict(
                        input_shapes=dict(
                            boxes=dict(
                                min_shape=[1, 5, 5],
                                opt_shape=[1, 5, 5],
                                max_shape=[1, 5, 5]),
                            scores=dict(
                                min_shape=[1, 5, 8],
                                opt_shape=[1, 5, 8],
                                max_shape=[1, 5, 8])))
                ]),
            codebase_config=dict(
                type='mmrotate',
                task='RotatedDetection',
                post_processing=dict(
                    score_threshold=0.05,
                    iou_threshold=0.5,
                    pre_top_k=-1,
                    keep_top_k=10,
                ))))

    boxes = torch.rand(1, 5, 5)
    scores = torch.rand(1, 5, 8)
    keep_top_k = 10
    wrapped_func = WrapFunction(
        fake_multiclass_nms_rotated, keep_top_k=keep_top_k)
    rewrite_outputs, _ = get_rewrite_outputs(
        wrapped_func,
        model_inputs={
            'boxes': boxes,
            'scores': scores
        },
        deploy_cfg=deploy_cfg)

    assert rewrite_outputs is not None, 'Got unexpected rewrite '\
        'outputs: {}'.format(rewrite_outputs)

max_stars_repo_name: grimoire/mmdeploy

max_issues_repo_name: grimoire/mmdeploy

max_forks_repo_name: grimoire/mmdeploy