text:
from pdb import set_trace

import numpy as np
import torch

class LReturn:
    def __init__(self, **kwargs):
        eps = kwargs.get('eps', 1e-4)
        rho = -np.log(eps) / kwargs.get('T', 5.0)

        self.lam = kwargs.get('lam', 0.85)
        self.gam = np.exp(-rho * kwargs.get('dt', 1.0/125.0))
        self.N = np.ceil(np.log(eps / (1. - self.lam)) /
                         np.log(self.lam)).astype(int)
        self.n_timesteps = int(kwargs.get('T', 5.0) /
                               kwargs.get('dt', 1.0/125.0))

        """
        A self.N = 3 example would be:
                     ｜1  1  1  ｜
        return_mat = ｜0  g  g  ｜
                     ｜0  0  g^2｜
        l_return_vec = [(1-λ), (1-λ)λ, λ^2]
        """
        return_mat = torch.zeros(self.N, self.N)
        return_mat_value = torch.zeros(self.N, 1)
        l_return_vec = torch.zeros(self.N, 1)
        for i in range(self.N):
            return_mat[i, i:] = self.gam**i
            return_mat_value[i] = self.gam**(i+1)
            l_return_vec[i] = self.lam**i * (1 - self.lam)
        l_return_vec[-1] = self.lam**(self.N-1)

        if kwargs.get('cuda', False):
            self.return_mat = return_mat.cuda()
            self.return_mat_value = return_mat_value.cuda()
            self.l_return_vec = l_return_vec.cuda()
        else:
            self.return_mat = return_mat
            self.return_mat_value = return_mat_value
            self.l_return_vec = l_return_vec

    def get_returns(self, rewards, values):
        l_returns = []
        for i in range(self.n_timesteps):
            horizon = np.min([self.N, self.n_timesteps-i])
            i_returns = rewards[:, 0, i:i+horizon] @ \
                self.return_mat[:horizon, :horizon]
            i_returns += values[:, 0, i:i+horizon] @ \
                self.return_mat_value[:horizon, :]
            i_l_returns = i_returns @ self.l_return_vec[:horizon, :]
            l_returns.append(i_l_returns)
        l_returns = torch.cat(l_returns, dim=1).view(-1, self.n_timesteps, 1)

        return l_returns

class ReplayBuffer:
    def __init__(self, **kwargs):
        self.state_buf = []
        self.reward_buf = []
        self.value_buf = []

        self.l_return = LReturn(**kwargs)

    def add(self, state=None, reward=None, value=None):
        """
        No need to save:
        (1) the value function of the initial state;
        (2) the terminal state.
        """
        if state is not None:
            self.state_buf.append(state)
        if reward is not None:
            self.reward_buf.append(reward)
        if value is not None:
            self.value_buf.append(value)

    def calculate_l_return(self):
        """
                 The state should contain: x0, x1, ..., xN-1
               The rewards should contain: r1, r2, ..., rN
        The value function should contain: V1, V2, ..., VN
        ----------------------------------------------------
        where N = T / dt
        """
        rewards = torch.cat(self.reward_buf, dim=2)
        values = torch.cat(self.value_buf, dim=2)

        # get returns
        l_returns = self.l_return.get_returns(rewards, values)

        return l_returns

    def sample(self):
        states = torch.cat(self.state_buf, dim=2)
        states_dim = states.shape
        states = states.view(states_dim[0], states_dim[-1], -1)
        l_returns = self.calculate_l_return()

        n_states = states.shape[-1]
        states = states.view(-1, n_states)
        l_returns = l_returns.view(-1, 1)

        # reset all buffers
        self.state_buf = []
        self.reward_buf = []
        self.value_buf = []

        return states, l_returns

def main():
    N = 4

    return_mat = torch.zeros(N, N)
    for i in range(N):
        return_mat[i, i:] = 2.0**i

    return_mat_target = torch.tensor([[1.0, 1.0, 1.0, 1.0],
                                      [0.0, 2.0, 2.0, 2.0],
                                      [0.0, 0.0, 4.0, 4.0],
                                      [0.0, 0.0, 0.0, 8.0]])

    return_mat_err = torch.sum(return_mat_target - return_mat).item()
    assert return_mat_err <= 1e-9

    lam = 1.0
    N = 4

    l_return_vec = torch.zeros(N, 1)
    for i in range(N):
        l_return_vec[i] = lam**i * (1 - lam)
    l_return_vec[-1] = lam**(N-1)

    l_return_vec_target = torch.tensor([[0.0], [0.0], [0.0], [1.0]])
    l_return_vec_err = torch.sum(l_return_vec - l_return_vec_target).item()
    assert l_return_vec_err <= 1e-9

    lam = 0.0
    N = 5

    l_return_vec = torch.zeros(N, 1)
    for i in range(N):
        l_return_vec[i] = lam**i * (1 - lam)
    l_return_vec[-1] = lam**(N-1)

    l_return_vec_target = torch.tensor([[1.0], [0.0], [0.0], [0.0], [0.0]])
    l_return_vec_err = torch.sum(l_return_vec - l_return_vec_target).item()
    assert l_return_vec_err <= 1e-9

    lam = 0.5
    N = 5

    l_return_vec = torch.zeros(N, 1)
    for i in range(N):
        l_return_vec[i] = lam**i * (1 - lam)
    l_return_vec[-1] = lam**(N-1)

    l_return_vec_target = torch.tensor(
        [[0.5], [0.25], [0.125], [0.0625], [0.0625]])
    l_return_vec_err = torch.sum(l_return_vec - l_return_vec_target).item()
    assert l_return_vec_err <= 1e-9

if __name__ == "__main__":
    main()


text:
import json
import logging
import os
import re
import sys

import numpy
import tensorflow as tf
from tensorflow.python.framework import ops
#import tensorflow.contrib.slim as slim #tensorflow.contrib.framework ???

# ModuleNotFoundError is new in 3.6; older versions will throw SystemError
if sys.version_info < (3, 6):
    ModuleNotFoundError = SystemError

try:
    from .exponential_smoothing import ExponentialSmoothing
    from . import training_progress
except (ModuleNotFoundError, ImportError) as e:
    from exponential_smoothing import ExponentialSmoothing
    import training_progress

def init_or_restore_variables(config, sess, ensemble_scope=None, train=False):
    """Initializes all variables or restores variables from a checkpoint.

    Prior to calling this function, the model (or models if using an ensemble)
    should already have been created. If a model uses exponential smoothing,
    then the _smooth versions of its variables should also have been created
    (by constructing an ExponentialSmoothing object). This function will then
    initialize the variables or restore them from a checkpoint (if one can be
    found).

    When using an ensemble, this function should be called once for each
    model, with each call using model-specific config and ensemble_scope
    arguments.

    Args:
      config: Namespace object specifying the config for the current model.
      sess: a TensorFlow session.
      ensemble_scope: a tf.variable_scope for the current model if ensembling.
      train: Boolean specifying that the model is being used for training.

    Returns:
      If train is True, returns a pair (saver, progress) where saver is a
      tf.train.Saver and progress is a training_progress.TrainingProgress
      object. Otherwise, just returns the saver.
    """

    accum_regex = re.compile('^accum\d+$')

    def is_excluded_variable(name):
        # Exclude gradient accumulation variables.
        if accum_regex.match(name):
            return True
        if name == 'accumulated_loss':
            return True
        return False

    variables = ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES)

    # Construct a mapping between saved variable names and names in the current
    # scope. There are two reasons why names might be different:
    #
    #   1. This model is part of an ensemble, in which case a model-specific
    #       name scope will be active.
    #
    #   2. The saved model is from an old version of Nematus (before deep model
    #        support was added) and uses a different variable naming scheme
    #        for the GRUs.

    var_map = {}
    for v in variables:
        name = v.name.split(':')[0]
        if ensemble_scope == None:
            saved_name = name
        elif v.name.startswith(ensemble_scope.name + "/"):
            saved_name = name[len(ensemble_scope.name)+1:]
            # The ensemble scope is repeated for Adam variables. See
            # https://github.com/tensorflow/tensorflow/issues/8120
            if saved_name.startswith(ensemble_scope.name + "/"):
                saved_name = saved_name[len(ensemble_scope.name)+1:]
        else: # v belongs to a different model in the ensemble.
            continue
        if is_excluded_variable(saved_name):
            continue
        if config.model_version == 0.1:
            # Backwards compatibility with the old variable naming scheme.
            saved_name = _revert_variable_name(saved_name, 0.1)
        var_map[saved_name] = v
    saver = tf.compat.v1.train.Saver(var_map, max_to_keep=None)

    # compute reload model filename
    reload_filename = None
    if config.reload == 'latest_checkpoint':
        checkpoint_dir = os.path.dirname(config.saveto)
        reload_filename = tf.train.latest_checkpoint(checkpoint_dir)
        if reload_filename != None:
            if (os.path.basename(reload_filename).rsplit('-', 1)[0] !=
                os.path.basename(config.saveto)):
                logging.error("Mismatching model filename found in the same directory while reloading from the latest checkpoint")
                sys.exit(1)
            logging.info('Latest checkpoint found in directory ' + os.path.abspath(checkpoint_dir))
    elif config.reload != None:
        reload_filename = config.reload
    if (reload_filename == None) and (config.prior_model != None):
        logging.info('Initializing model parameters from prior')
        reload_filename = config.prior_model

    # initialize or reload training progress
    if train:
        progress = training_progress.TrainingProgress()
        progress.bad_counter = 0
        progress.uidx = 0
        progress.eidx = 0
        progress.estop = False
        progress.history_errs = []
        progress.valid_script_scores = []
        if reload_filename and config.reload_training_progress:
            path = reload_filename + '.progress.json'
            if os.path.exists(path):
                logging.info('Reloading training progress')
                progress.load_from_json(path)
                if (progress.estop == True or
                    progress.eidx > config.max_epochs or
                    progress.uidx >= config.finish_after):
                    logging.warning('Training is already complete. Disable reloading of training progress (--no_reload_training_progress) or remove or modify progress file (%s) to train anyway.' % path)
                    sys.exit(0)

    # load prior model
    if train and config.prior_model != None:
        load_prior(config, sess, saver)

    init_op = tf.compat.v1.global_variables_initializer()

    # initialize or restore model
    if reload_filename == None:
        logging.info('Initializing model parameters from scratch...')
        sess.run(init_op)
    else:
        logging.info('Loading model parameters from file ' + os.path.abspath(reload_filename))
        if train:
            # Initialize all variables before restoring from the checkpoint.
            # This is to allow for variables that are not saved to the
            # checkpoint. Currently that is just the gradient accumulation
            # variables, which are unusual in that they persist across multiple
            # sessions during training (and therefore need to be variables) but
            # are regularly reset to zero.
            sess.run(init_op)
        saver.restore(sess, os.path.abspath(reload_filename))
    logging.info('Done')

    if train:
        return saver, progress
    else:
        return saver

def load_prior(config, sess, saver):
     logging.info('Loading prior model parameters from file ' + os.path.abspath(config.prior_model))
     saver.restore(sess, os.path.abspath(config.prior_model))

     # fill prior variables with the loaded values
     prior_variables = tf.compat.v1.get_collection_ref('prior_variables')
     prior_variables_dict = dict([(v.name, v) for v in prior_variables])
     assign_tensors = []
     with tf.compat.v1.variable_scope('prior'):
         for v in tf.compat.v1.trainable_variables():
             prior_name = 'loss/prior/'+v.name
             prior_variable = prior_variables_dict[prior_name]
             assign_tensors.append(prior_variable.assign(v))
     tf.compat.v1.variables_initializer(prior_variables)
     sess.run(assign_tensors)

# for backwards compatibility with old models
def _revert_variable_name(name, old_version):
    assert old_version == 0.1
    if name.endswith("/Adam"):
        prefix = name[:-len("/Adam")]
        return _revert_variable_name(prefix, old_version) + "/Adam"
    if name.endswith("/Adam_1"):
        prefix = name[:-len("/Adam_1")]
        return _revert_variable_name(prefix, old_version) + "/Adam_1"
    if "forward-stack/level0/gru0" in name:
        return name.replace("forward-stack/level0/gru0", "forwardEncoder")
    if "backward-stack/level0/gru0" in name:
        return name.replace("backward-stack/level0/gru0", "backwardEncoder")
    if "decoder/base/gru0" in name:
        return name.replace("decoder/base/gru0", "decoder")
    if "decoder/base/attention" in name:
        return name.replace("decoder/base/attention", "decoder")
    if "decoder/base/gru1" in name:
        tmp = name.replace("decoder/base/gru1", "decoder")
        if tmp.endswith("/new_mean"):
            return tmp.replace("/new_mean", "_1/new_mean")
        elif tmp.endswith("/new_std"):
            return tmp.replace("/new_std", "_1/new_std")
        else:
            return tmp + "_1"
    if "decoder/embedding" in name:
        return name.replace("decoder/embedding", "decoder/y_embeddings_layer")
    return name

ext: py

max_stars_repo_name: DLPerf/nematus

max_issues_repo_name: DLPerf/nematus

max_forks_repo_name: DLPerf/nematus

reason: import numpy


text:
import numpy as _np

import internal_field as internal_field_cython
import current_disk as current_disk_cython

import params
import transform

_ph = params

def internal_field(r, theta, phi):
    '''Calculates internal magnetic field of the Jupiter.

    Parameters
    ----------
    r : 1-D array
        Spherical coordinates r (Jupiter radii) of the sample points in right-handed System III (1965)
    theta : 1-D array
        Spherical coordinates theta (rad) of the sample points in right-handed System III (1965)
    phi : 1-D array
        Spherical coordinates phi (rad) of the sample points in right-handed System III (1965)

    Returns
    -------
    ndarray
        Magnetic field in sample points in nT in spherical right-handed System III (1965)

    '''
    n = _ph.internal_field.g.shape[0]
    return internal_field_cython.internal_field_1darray(
        r * (params.RJ / _ph.internal_field.a), theta, phi, _ph.internal_field.g, _ph.internal_field.h, _ph.schmidt_norm_coeffs[:n, :n + 1])

def PCD_field(rho, phi, z, x):
    '''Calculates magnetic field of the Jupiter's current disk according to the PCD model.

    Note:
        Magnetic equatorial cylindrical coordinate system: rho and phi in the magnetic equator, z parallel to the dipole axis.

    Parameters
    ----------
    rho : 1-D array
        Cylindrical coordinates rho (Jupiter radii) of the sample points in magnetic equatorial coordinate system.
    phi : 1-D array
        Cylindrical coordinates phi (rad) of the sample points in magnetic equatorial coordinate system.
    z : 1-D array
        Cylindrical coordinates z (Jupiter radii) of the sample points in magnetic equatorial coordinate system.
    x : 1-D array
        Coordinates x (Jupiter radii) of the sample points along the Jupiter-Sun axis.

    Returns
    -------
    ndarray
        Magnetic field in sample points in nT in cylindrical magnetic equatorial coordinate system

    '''
    res = current_disk_cython.PCD_field_1darray(
        rho, z, _ph.PCD.I0, _ph.PCD.D, _ph.PCD.Rin, _ph.PCD.Rout, _ph.PCD.R1, _ph.PCD.R2, z_cs(rho, phi, x), dzcs_drho(rho, phi, x))
    return _np.array([res[0], _np.zeros(res.shape[1]), res[1]])

def lines(starts, ds, N, B_func):
    '''Calculates field lines of the field determined by callable B_func.

    Parameters
    ----------
    starts : ndarray
        Cartesian coordinates of the start points of lines
    ds : 1-D array
        Spatial step of field line calculation for each line
    N : int
        Maximal number of seps
    B_func : callable
        Calculates cartesian components of the magnetic field. Input: cartesian coordinates

    Returns
    -------
    positions : ndarray
        cartesian coordinates of points on the field lines

    end_ind : ndarray
        indices of end points of the lines in the array of positions

    '''
    positions = _np.zeros((3, N, starts.shape[1]))
    positions[:, 0, :] = starts
    end_ind = _np.zeros(starts.shape[1], dtype=_np.int)
    end_mask = _np.arange(starts.shape[1], dtype=_np.int)
    for i in range(1, N):
        B = B_func(positions[:, i-1, end_mask])
        positions[:, i, end_mask] = positions[:, i-1, end_mask] + \
            ds[end_mask] * B / _np.sqrt(B[0] ** 2 + B[1] ** 2 + B[2] ** 2)
        mag = _np.sqrt(positions[0, i, :] ** 2 +
                       positions[1, i, :] ** 2 + positions[2, i, :] ** 2)
        end_ind[end_mask] = i
        end_mask = _np.where(mag > 1)[0]
        if (end_mask.size == 0):
            break

    return (positions, end_ind)

def SIII2M(vec):
    '''Converts vectors from System III (1965) to magnetic equatorial system.

    Note: vectors should be in cartesian coordinates.
    '''
    return _np.dot(_ph.SIII2M_mat, vec)

def M2SIII(vec):
    '''Converts vectors from magnetic equatorial system to System III (1965).

    Note: vectors should be in cartesian coordinates
    '''
    return _np.dot(_ph.M2SIII_mat, vec)

def dzcs_drho(rho, phi, x):
    ''' Calculates partial derivative by rho of z_cs. '''
    tan = _np.tan(_ph.dipole_tilt)
    return (tan * (_np.cosh(x / _ph.curvature.x0)) ** (-2) * _np.cos(phi - _delta(rho))
            - rho * tan * _ph.curvature.x0 / x * _np.tanh(x / _ph.curvature.x0) * _np.sin(phi - _delta(
                rho)) * _ph.curvature.omega_j / _ph.curvature.nu0 * _np.tanh(rho / _ph.curvature.rho0)
            - tan * _np.cos(phi - _delta(rho)))

def z_cs(rho, phi, x):
    ''' Calcualtes z coordinate of the current sheet center in magnetic coordinates.

    For more info see Khurana 1997 (https://doi.org/10.1029/97JA00563)
    '''
    return rho * _np.tan(_ph.dipole_tilt) * (_ph.curvature.x0 / x * _np.tanh(x / _ph.curvature.x0) * _np.cos(phi - _delta(rho))
                                             - _np.cos(phi - _np.pi))

def _delta(rho):
    return _np.pi - _ph.curvature.omega_j * _ph.curvature.rho0 / _ph.curvature.nu0 * _np.log(_np.cosh(rho / _ph.curvature.rho0))

def _totalB(M_cart):
    sun_long = 0
    SIII_cart = M2SIII(M_cart)
    SIII_sph = transform.coords_cart2sph(SIII_cart)
    M_cyl = transform.coords_cart2cyl(M_cart)
    x_jso = M_cyl[0] * _np.tan(_ph.dipole_tilt) * _np.cos(M_cyl[2] - sun_long)
    B_int_M_cart = SIII2M(transform.vectors_sph2cart(
        internal_field(SIII_sph[0], SIII_sph[1], SIII_sph[2]), SIII_sph))
    B_md_M_cart = transform.vectors_cyl2cart(
        PCD_field(M_cyl[0], M_cyl[1], M_cyl[2], x_jso), M_cyl)
    return B_int_M_cart + B_md_M_cart

max_stars_repo_name: gasgiant/jfield

max_issues_repo_name: gasgiant/jfield

max_forks_repo_name: gasgiant/jfield


text:
r"""
Intro to QAOA
=========================

.. meta::
    :property="og:description": Learn how to implement QAOA with PennyLane
    :property="og:image": https://pennylane.ai/qml/_images/qaoa_layer.png

.. related::

   tutorial_qaoa_maxcut QAOA for MaxCut

*Author: Jack Ceroni. Posted: 18 Nov 2020. Last updated: 11 Jan 2021.*

The Quantum Approximate Optimization Algorithm (QAOA) is a widely-studied
method for solving combinatorial optimization problems on NISQ devices.
The applications of QAOA are broad and far-reaching, and the performance
of the algorithm is of great interest to the quantum computing research community.

.. figure:: ../demonstrations/qaoa_module/qaoa_circuit.png
    :align: center
    :width: 90%

The goal of this tutorial is to introduce the basic concepts of QAOA and
to guide you through PennyLane's built-in QAOA
functionality. You will learn how to use time evolution to establish a
connection between Hamiltonians and quantum circuits, and how to layer
these circuits to create more powerful algorithms. These simple ingredients,
together with the ability to optimize quantum circuits, are the building blocks of QAOA. By focusing
on the fundamentals, PennyLane provides general and flexible capabilities that can be tailored and
refined to implement QAOA for a wide variety of problems. In the last part of the tutorial, you will
learn how to bring these pieces together and deploy a complete QAOA workflow to solve the
minimum vertex cover problem. Let's get started! 🎉

Circuits and Hamiltonians
-------------------------

When considering quantum circuits, it is often convenient to define them by a
series of quantum gates. But there are many instances where
it is useful to think of a quantum circuit in terms of a
`Hamiltonian <https://en.wikipedia.org/wiki/Hamiltonian_(quantum_mechanics)>`__.
Indeed, gates are physically implemented by performing time evolution under a carefully engineered
Hamiltonian. These transformations are described by the time evolution operator,
which is a unitary defined as:"""

######################################################################
#  .. math:: U(H, \ t) \ = \ e^{-i H t / \hbar}.
#
# The time evolution operator is determined completely in terms of a Hamiltonian
# :math:`H` and a scalar :math:`t` representing time. In fact, any unitary
# :math:`U` can be written in the form :math:`e^{i \gamma H}`, where :math:`\gamma` is a scalar
# and :math:`H` is a Hermitian operator,
# interpreted as a Hamiltonian. Thus, time evolution establishes a connection that allows us to
# describe quantum circuits in terms of Hamiltonians. 🤯
#
# In general, implementing a quantum circuit that exactly exponentiates a Hamiltonian
# with many non-commuting terms, i.e., a Hamiltonian of the form:
#
# .. math:: H \ = \ H_1 \ + \ H_2 \ + \ H_3 \ + \ \cdots \ + \ H_N,
#
# is very challenging. Instead, we can use the
# `Trotter-Suzuki <https://en.wikipedia.org/wiki/Lie_product_formula>`__ decomposition formula
#
# .. math:: e^{A \ + \ B} \ \approx \ \Big(e^{A/n} e^{B/n}\Big)^{n},
#
# to implement an *approximate* time-evolution unitary:
#
# .. math:: U(H, t, n) \ = \ \displaystyle\prod_{j \ = \ 1}^{n}
#           \displaystyle\prod_{k} e^{-i H_k t / n} \ \ \ \ \ \ \ \ \ \ H \
#           = \ \displaystyle\sum_{k} H_k,
#
# where :math:`U` approaches :math:`e^{-i H t}` as :math:`n`
# becomes larger.
#
# .. figure:: ../demonstrations/qaoa_module/ham_circuit.png
#     :align: center
#     :width: 70%
#
# In PennyLane, this is implemented using the :func:`~.pennylane.templates.ApproxTimeEvolution` template.
# For example, let's say we have the following Hamiltonian:

import pennylane as qml

H = qml.Hamiltonian(
    [1, 1, 0.5],
    [qml.PauliX(0), qml.PauliZ(1), qml.PauliX(0) @ qml.PauliX(1)]
)
print(H)

######################################################################
#
# We can implement the approximate time-evolution operator corresponding to this
# Hamiltonian:

dev = qml.device('default.qubit', wires=2)

t = 1
n = 2

@qml.qnode(dev)
def circuit():
    qml.ApproxTimeEvolution(H, t, n)
    return [qml.expval(qml.PauliZ(i)) for i in range(2)]

print(qml.draw(circuit, expansion_strategy='device')())

######################################################################
# Layering circuits
# -----------------
#
# Think of all the times you have copied a text or image, then pasted it repeatedly to create
# many duplicates. This is also a useful feature when designing quantum algorithms!
# The idea of repetition is ubiquitous in quantum computing:
# from amplitude amplification in `Grover’s algorithm
# <https://en.wikipedia.org/wiki/Grover%27s_algorithm>`__
# to layers in `quantum neural networks
# <https://journals.aps.org/prresearch/abstract/10.1103/PhysRevResearch.1.033063>`__
# and `Hamiltonian simulation
# <https://en.wikipedia.org/wiki/Hamiltonian_simulation>`__, repeated application
# of a circuit is a central tool in quantum algorithms.
#
#
# .. figure:: ../demonstrations/qaoa_module/repeat.png
#     :align: center
#     :width: 100%
#
#
# Circuit repetition is implemented in PennyLane using the :func:`~.pennylane.layer` function. This method
# allows us to take a function containing either quantum operations, a template, or even a single
# quantum gate, and repeatedly apply it to a set of wires.
#
# .. figure:: ../demonstrations/qaoa_module/qml_layer.png
#     :align: center
#     :width: 90%
#
# To create a larger circuit consisting of many repetitions, we pass the circuit to be
# repeated as an argument and specify the number of repetitions. For example, let's
# say that we want to layer the following circuit three times:

def circ(theta):
    qml.RX(theta, wires=0)
    qml.Hadamard(wires=1)
    qml.CNOT(wires=[0, 1])

@qml.qnode(dev)
def circuit(param):
    circ(param)
    return [qml.expval(qml.PauliZ(i)) for i in range(2)]

print(qml.draw(circuit)(0.5))

######################################################################
#
# We simply pass this function into the :func:`~.pennylane.layer` function:
#

@qml.qnode(dev)
def circuit(params, **kwargs):
    qml.layer(circ, 3, params)
    return [qml.expval(qml.PauliZ(i)) for i in range(2)]

print(qml.draw(circuit)([0.3, 0.4, 0.5]))

######################################################################
#
# We have learned how time evolution can be used to create circuits from Hamiltonians,
# and how these can be layered to create longer circuits. We are now ready to
# explore QAOA.
#

######################################################################
# QAOA
# ----
#
# The quantum approximate optimization algorithm (QAOA) is a general technique that can be used
# to find approximate solutions to combinatorial optimization problems, in particular problems
# that can be cast as searching for an optimal bitstring. QAOA consists of the following
# steps:
#
# 1. Define a *cost Hamiltonian* :math:`H_C` such that its ground state
#    encodes the solution to the optimization problem.
#
# 2. Define a *mixer Hamiltonian* :math:`H_M`.
#
# 3. Construct the circuits :math:`e^{-i \gamma H_C}` and :math:`e^{-i\alpha H_M}`. We call
#    these the *cost* and *mixer layers*, respectively.
#
# 4. Choose a parameter :math:`n\geq 1` and build the circuit
#
#    .. math:: U(\boldsymbol\gamma, \ \boldsymbol\alpha) \ = \ e^{-i \alpha_n H_M}
#              e^{-i \gamma_n H_C} \ ... \ e^{-i \alpha_1 H_M} e^{-i \gamma_1 H_C},
#
#    consisting of repeated application of the cost and mixer layers.
#
# 5. Prepare an initial state, apply :math:`U(\boldsymbol\gamma,\boldsymbol\alpha)`,
#    and use classical techniques to optimize the parameters.
#
# 6. After the circuit has been optimized, measurements of the output state reveal
#    approximate solutions to the optimization problem.
#
# In summary, the starting point of QAOA is the specification of cost and mixer Hamiltonians.
# We then use time evolution and layering to create a variational circuit and optimize its
# parameters. The algorithm concludes by sampling from the circuit to get an approximate solution to
# the optimization problem. Let's see it in action! 🚀
#

######################################################################
# Minimum Vertex Cover with QAOA
# ------------------------------
#
# Our goal is to find the `minimum vertex
# cover <https://en.wikipedia.org/wiki/Vertex_cover>`__ of a graph:
# a collection of vertices such that
# each edge in the graph contains at least one of the vertices in the cover. Hence,
# these vertices "cover" all the edges 👍.
# We wish to find the vertex cover that has the
# smallest possible number of vertices.
#
# Vertex covers can be represented by a bit string
# where each bit denotes whether the corresponding vertex is present in the cover. For example,
# the bit string 01010 represents a cover consisting of the second and fourth vertex in a graph with five vertices.
#
# .. figure:: ../demonstrations/qaoa_module/minvc.png
#     :align: center
#     :width: 90%
#
# To implement QAOA with PennyLane, we first import the necessary dependencies:
#

from pennylane import qaoa
from pennylane import numpy as np
from matplotlib import pyplot as plt
import networkx as nx

######################################################################
#
# We also define the four-vertex graph for which we
# want to find the minimum vertex cover:

edges = [(0, 1), (1, 2), (2, 0), (2, 3)]
graph = nx.Graph(edges)

nx.draw(graph, with_labels=True)
plt.show()

######################################################################
#
# There are two minimum vertex covers of this graph: the vertices 0 and 2,
# and the vertices 1 and 2. These can be respectively represented by the bit strings 1010 and
# 0110. The goal of the algorithm is to sample these bit strings with high probability.
#
# The PennyLane QAOA module has a collection of built-in optimization
# problems, including minimum vertex cover. For each problem, you can retrieve the cost Hamiltonian
# as well as a recommended mixer Hamiltonian. This
# makes it straightforward to obtain the Hamiltonians for specific problems while still
# permitting the flexibility to make other choices, for example by adding constraints or
# experimenting with different mixers.
#
# In our case, the cost
# Hamiltonian has two ground states, :math:`|1010\rangle` and :math:`|0110\rangle`, coinciding
# with the solutions of the problem. The mixer Hamiltonian is the simple, non-commuting sum of Pauli-X
# operations on each node of the graph:

cost_h, mixer_h = qaoa.min_vertex_cover(graph, constrained=False)

print("Cost Hamiltonian", cost_h)
print("Mixer Hamiltonian", mixer_h)

######################################################################
#
# A single layer of QAOA consists of time evolution under these
# Hamiltonians:
#
# .. figure:: ../demonstrations/qaoa_module/layer.png
#     :align: center
#     :width: 90%
#
# While it is possible to use :func:`~.pennylane.templates.ApproxTimeEvolution`, the QAOA module allows you to
# build the cost and mixer layers directly using the functions :func:`~.pennylane.qaoa.cost_layer` and
# :func:`~.pennylane.qaoa.mixer_layer`, which take as input the respective Hamiltonian and variational parameters:

def qaoa_layer(gamma, alpha):
    qaoa.cost_layer(gamma, cost_h)
    qaoa.mixer_layer(alpha, mixer_h)

######################################################################
#
# We are now ready to build the full variational circuit. The number of wires is equal to
# the number of vertices of the graph. We initialize the state to an even superposition over
# all basis states.
# For this example, we employ a circuit consisting of two QAOA layers:

wires = range(4)
depth = 2

def circuit(params, **kwargs):
    for w in wires:
        qml.Hadamard(wires=w)
    qml.layer(qaoa_layer, depth, params[0], params[1])

######################################################################
#
# Note that :func:`~.pennylane.layer` allows us to pass variational parameters
# ``params[0]`` and ``params[1]`` into each layer of the circuit. That's it! The last
# step is PennyLane's specialty: optimizing the circuit parameters.
#
# The cost function is the expectation value of :math:`H_C`, which we want to minimize. We
# use the function :func:`~.pennylane.expval` which returns the
# expectation value of the Hamiltonian with respect to the circuit's output state.
# We also define the device on which the simulation is performed. We use the
# PennyLane-Qulacs plugin to run the circuit on the Qulacs simulator:
#

dev = qml.device("qulacs.simulator", wires=wires)

@qml.qnode(dev)
def cost_function(params):
    circuit(params)
    return qml.expval(cost_h)

######################################################################
#
# Finally, we optimize the cost function using the built-in
# :func:`~.pennylane.GradientDescentOptimizer`. We perform optimization for seventy steps and initialize the
# parameters:

optimizer = qml.GradientDescentOptimizer()
steps = 70
params = np.array([[0.5, 0.5], [0.5, 0.5]], requires_grad=True)

######################################################################
#
# Notice that we set each of the initial parameters to :math:`0.5`. For demonstration purposes,
# we chose initial parameters that we know work fairly well, and don't get stuck in any local minima.
#
# The choice of initial parameters for a variational circuit is usually a difficult problem,
# so we won't linger on it too much in this tutorial, but it is important to note that
# finding an initial set of parameters that work well for a few toy problems often yields good results
# for more complex instances of the algorithm as well.
#
# Now, we can optimize the circuit:
#

for i in range(steps):
    params = optimizer.step(cost_function, params)

print("Optimal Parameters")
print(params)

######################################################################
#
# With the optimal parameters, we can now reconstruct the probability
# landscape. We redefine the
# full QAOA circuit with the optimal parameters, but this time we
# return the probabilities of measuring each bitstring:
#

@qml.qnode(dev)
def probability_circuit(gamma, alpha):
    circuit([gamma, alpha])
    return qml.probs(wires=wires)

probs = probability_circuit(params[0], params[1])

######################################################################
#
# Finally, we can display a bar graph showing the probability of
# measuring each bitstring:

plt.style.use("seaborn")
plt.bar(range(2 ** len(wires)), probs)
plt.show()

######################################################################
#
# The states
# :math:`|6\rangle \ = \ |0110\rangle` and
# :math:`|10\rangle \ = \ |1010\rangle` have the highest probabilities of
# being measured, just as expected!
#
# .. figure:: ../demonstrations/qaoa_module/graph.png
#     :align: center
#     :width: 90%
#

######################################################################
# Customizing QAOA
# ----------------
#
# QAOA is not one-size-fits-all when it comes to solving optimization problems. In many cases,
# cost and mixer Hamiltonians will be very specific to one scenario, and not necessarily
# fit within the structure of the pre-defined problems in the :func:`~.pennylane.qaoa` submodule. Luckily,
# one of the core principles behind the entire PennyLane library is customizability, and this principle hold true for
# QAOA submodule as well!
#
# The QAOA workflow above gave us two optimal solutions: :math:`|6\rangle = |0110\rangle`
# and :math:`|10\rangle = |1010\rangle`. What if we add a constraint
# that made one of these solutions "better" than the other? Let's imagine that we are interested in
# solutions that minimize the original cost function,
# *but also colour the first and third vertices* :math:`1`. A constraint of this form will
# favour :math:`|10\rangle`, making it the only true ground state.
#
# It is easy to introduce constraints of this form in PennyLane.
# We can use the :func:`~.pennylane.qaoa.edge_driver` cost
# Hamiltonian to "reward" cases in which the first and last vertices of the graph
# are :math:`0`:

reward_h = qaoa.edge_driver(nx.Graph([(0, 2)]), ['11'])

######################################################################
#
# We then weigh and add the constraining term
# to the original minimum vertex cover Hamiltonian:

new_cost_h = cost_h + 2 * reward_h

######################################################################
#
# Notice that PennyLane allows for simple addition and multiplication of
# Hamiltonian objects using inline arithmetic operations ➕ ➖ ✖➗! Finally, we can
# use this new cost Hamiltonian to define a new QAOA workflow:

def qaoa_layer(gamma, alpha):
    qaoa.cost_layer(gamma, new_cost_h)
    qaoa.mixer_layer(alpha, mixer_h)

def circuit(params, **kwargs):
    for w in wires:
        qml.Hadamard(wires=w)
    qml.layer(qaoa_layer, depth, params[0], params[1])

@qml.qnode(dev)
def cost_function(params):
    circuit(params)
    return qml.expval(new_cost_h)

params = np.array([[0.5, 0.5], [0.5, 0.5]], requires_grad=True)

for i in range(steps):
    params = optimizer.step(cost_function, params)

print("Optimal Parameters")
print(params)

######################################################################
#
# We then reconstruct the probability landscape with the optimal parameters:
#

@qml.qnode(dev)
def probability_circuit(gamma, alpha):
    circuit([gamma, alpha])
    return qml.probs(wires=wires)

probs = probability_circuit(params[0], params[1])

plt.style.use("seaborn")
plt.bar(range(2 ** len(wires)), probs)
plt.show()

######################################################################
#
# Just as we expected, the :math:`|10\rangle` state is now favoured
# over :math:`|6\rangle`!
#

######################################################################
# Conclusion
# ----------
#
# You have learned how to use the PennyLane QAOA functionality, while
# also surveying some of the fundamental features that make the QAOA module simple and
# flexible. Now, it's your turn to experiment with QAOA! If you need some inspiration for how to get
# started:
#
# - Experiment with different optimizers and different devices. Which ones work the best?
# - Play around with some of the other built-in cost and mixer Hamiltonians.
# - Try making your own custom constraining terms. Is QAOA properly amplifying some bitstrings over others?
#
# .. figure:: ../demonstrations/qaoa_module/qaoa_circuit.png
#     :align: center
#     :width: 90%
#

reason: import numpy,import networkx


text:
import cv2
import math
import time
import numpy as np
from . import util
from .config_reader import config_reader
from scipy.ndimage.filters import gaussian_filter
from tensorflow.keras.models import load_model
import tensorflow as tf
import code
import copy
import scipy.ndimage as sn
from PIL import Image
from tqdm import tqdm
from .model_simulated_RGB101 import get_testing_model_resnet101
from .human_seg.human_seg_gt import human_seg_combine_argmax

right_part_idx = [2, 3, 4,  8,  9, 10, 14, 16]
left_part_idx =  [5, 6, 7, 11, 12, 13, 15, 17]
human_part = [0,1,2,4,3,6,5,8,7,10,9,12,11,14,13]
human_ori_part = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14]
seg_num = 15 # current model supports 15 parts only

# find connection in the specified sequence, center 29 is in the position 15
limbSeq = [[2, 3], [2, 6], [3, 4], [4, 5], [6, 7], [7, 8], [2, 9], [9, 10], \
           [10, 11], [2, 12], [12, 13], [13, 14], [2, 1], [1, 15], [15, 17], \
           [1, 16], [16, 18], [3, 17], [6, 18]]

# the middle joints heatmap correpondence
mapIdx = [[31, 32], [39, 40], [33, 34], [35, 36], [41, 42], [43, 44], [19, 20], [21, 22], \
          [23, 24], [25, 26], [27, 28], [29, 30], [47, 48], [49, 50], [53, 54], [51, 52], \
          [55, 56], [37, 38], [45, 46]]

# visualize
colors = [[255, 0, 0], [255, 85, 0], [255, 170, 0], [255, 255, 0], [170, 255, 0], [85, 255, 0],
          [0, 255, 0], \
          [0, 255, 85], [0, 255, 170], [0, 255, 255], [0, 170, 255], [0, 85, 255], [0, 0, 255],
          [85, 0, 255], \
          [170, 0, 255], [255, 0, 255], [255, 0, 170], [255, 0, 85]]

ori_paf_idx = [12, 13, 20, 21, 14, 15, 16, 17, 22, 23, 24, 25, 0, 1, 2, 3, \
                4, 5, 6, 7, 8, 9, 10, 11, 28, 29, 30, 31, 34,35, 32, 33, 36, 37, 18, 19, 26, 27]
flip_paf_idx = [20, 21, 12, 13, 22, 23,  24, 25, 14, 15, 16, 17, 6, 7, 8, 9, \
                10, 11, 0, 1, 2, 3, 4, 5, 28, 29, 32, 33, 36, 37, 30, 31, 34,35,26, 27,18, 19]
x_paf_idx = [20, 12, 22, 24, 14, 16, 6, 8, \
                10, 0, 2, 4, 28, 32, 36, 30, 34,26,18]

def recover_flipping_output(oriImg, heatmap_ori_size, paf_ori_size, part_ori_size):

    heatmap_ori_size = heatmap_ori_size[:, ::-1, :]
    heatmap_flip_size = np.zeros((oriImg.shape[0], oriImg.shape[1], 19))
    heatmap_flip_size[:,:,left_part_idx] = heatmap_ori_size[:,:,right_part_idx]
    heatmap_flip_size[:,:,right_part_idx] = heatmap_ori_size[:,:,left_part_idx]
    heatmap_flip_size[:,:,0:2] = heatmap_ori_size[:,:,0:2]

    paf_ori_size = paf_ori_size[:, ::-1, :]
    paf_flip_size = np.zeros((oriImg.shape[0], oriImg.shape[1], 38))
    paf_flip_size[:,:,ori_paf_idx] = paf_ori_size[:,:,flip_paf_idx]
    paf_flip_size[:,:,x_paf_idx] = paf_flip_size[:,:,x_paf_idx]*-1

    part_ori_size = part_ori_size[:, ::-1, :]
    part_flip_size = np.zeros((oriImg.shape[0], oriImg.shape[1], 15))
    part_flip_size[:,:,human_ori_part] = part_ori_size[:,:,human_part]
    return heatmap_flip_size, paf_flip_size, part_flip_size

def recover_flipping_output2(oriImg, part_ori_size):

    part_ori_size = part_ori_size[:, ::-1, :]
    part_flip_size = np.zeros((oriImg.shape[0], oriImg.shape[1], 15))
    part_flip_size[:,:,human_ori_part] = part_ori_size[:,:,human_part]
    return part_flip_size

def part_thresholding(seg_argmax):
    background = 0.6
    head = 0.5
    torso = 0.8

    rightfoot = 0.55
    leftfoot = 0.55
    leftthigh = 0.55
    rightthigh = 0.55
    leftshank = 0.55
    rightshank = 0.55
    rightupperarm = 0.55
    leftupperarm = 0.55
    rightforearm = 0.55
    leftforearm = 0.55
    lefthand = 0.55
    righthand = 0.55

    part_th = [background, head, torso, leftupperarm ,rightupperarm, leftforearm, rightforearm, lefthand, righthand, leftthigh, rightthigh, leftshank, rightshank, leftfoot, rightfoot]
    th_mask = np.zeros(seg_argmax.shape)
    for indx in range(15):
        part_prediction = (seg_argmax==indx)
        part_prediction = part_prediction*part_th[indx]
        th_mask += part_prediction

    return th_mask

def process (oriImg, flipImg, params, model_params, model, Img):

    input_scale = 1.0
    multiplier = [x * model_params['boxsize'] / oriImg.shape[0] for x in params['scale_search']]

    heatmap_avg = np.zeros((oriImg.shape[0], oriImg.shape[1], 19))
    paf_avg = np.zeros((oriImg.shape[0], oriImg.shape[1], 38))
    seg_avg = np.zeros((oriImg.shape[0], oriImg.shape[1], 15))

    segmap_scale1 = np.zeros((oriImg.shape[0], oriImg.shape[1], seg_num))
    segmap_scale2 = np.zeros((oriImg.shape[0], oriImg.shape[1], seg_num))
    segmap_scale3 = np.zeros((oriImg.shape[0], oriImg.shape[1], seg_num))
    segmap_scale4 = np.zeros((oriImg.shape[0], oriImg.shape[1], seg_num))

    segmap_scale5 = np.zeros((oriImg.shape[0], oriImg.shape[1], seg_num))
    segmap_scale6 = np.zeros((oriImg.shape[0], oriImg.shape[1], seg_num))
    segmap_scale7 = np.zeros((oriImg.shape[0], oriImg.shape[1], seg_num))
    segmap_scale8 = np.zeros((oriImg.shape[0], oriImg.shape[1], seg_num))

    for m in range(len(multiplier)):
        scale = multiplier[m]*input_scale
        imageToTest = cv2.resize(oriImg, (0, 0), fx=scale, fy=scale, interpolation=cv2.INTER_CUBIC)

        pad = [ 0,
                0,
                (imageToTest.shape[0] - model_params['stride']) % model_params['stride'],
                (imageToTest.shape[1] - model_params['stride']) % model_params['stride']
              ]

        imageToTest_padded = np.pad(imageToTest, ((0, pad[2]), (0, pad[3]), (0, 0)), mode='constant', constant_values=((0, 0), (0, 0), (0, 0)))

        input_img = imageToTest_padded[np.newaxis, ...]

        # print( "\tActual size fed into NN: ", input_img.shape)

        output_blobs = model.predict(input_img)

        heatmap = np.squeeze(output_blobs[1])  # output 1 is heatmaps
        heatmap = cv2.resize(heatmap, (0, 0), fx=model_params['stride'], fy=model_params['stride'],
                             interpolation=cv2.INTER_CUBIC)
        heatmap = heatmap[:imageToTest_padded.shape[0] - pad[2], :imageToTest_padded.shape[1] - pad[3],
                  :]
        heatmap = cv2.resize(heatmap, (oriImg.shape[1], oriImg.shape[0]), interpolation=cv2.INTER_CUBIC)

        paf = np.squeeze(output_blobs[0])  # output 0 is PAFs
        paf = cv2.resize(paf, (0, 0), fx=model_params['stride'], fy=model_params['stride'],
                         interpolation=cv2.INTER_CUBIC)
        paf = paf[:imageToTest_padded.shape[0] - pad[2], :imageToTest_padded.shape[1] - pad[3], :]
        paf = cv2.resize(paf, (oriImg.shape[1], oriImg.shape[0]), interpolation=cv2.INTER_CUBIC)

        seg = np.squeeze(output_blobs[2])
        seg = cv2.resize(seg, (0, 0), fx=model_params['stride'], fy=model_params['stride'],
                             interpolation=cv2.INTER_CUBIC)
        seg = seg[:imageToTest_padded.shape[0] - pad[2], :imageToTest_padded.shape[1] - pad[3], :]
        seg = cv2.resize(seg, (oriImg.shape[1], oriImg.shape[0]), interpolation=cv2.INTER_CUBIC)

        if m==0:
            segmap_scale1 = seg
        elif m==1:
            segmap_scale2 = seg
        elif m==2:
            segmap_scale3 = seg
        elif m==3:
            segmap_scale4 = seg

    # flipping
    for m in range(len(multiplier)):
        scale = multiplier[m]
        imageToTest = cv2.resize(flipImg, (0, 0), fx=scale, fy=scale, interpolation=cv2.INTER_CUBIC)
        pad = [ 0,
                0,
                (imageToTest.shape[0] - model_params['stride']) % model_params['stride'],
                (imageToTest.shape[1] - model_params['stride']) % model_params['stride']
              ]

        imageToTest_padded = np.pad(imageToTest, ((0, pad[2]), (0, pad[3]), (0, 0)), mode='constant', constant_values=((0, 0), (0, 0), (0, 0)))
        input_img = imageToTest_padded[np.newaxis, ...]
        # print( "\tActual size fed into NN: ", input_img.shape)
        output_blobs = model.predict(input_img)

        # extract outputs, resize, and remove padding
        heatmap = np.squeeze(output_blobs[1])  # output 1 is heatmaps
        heatmap = cv2.resize(heatmap, (0, 0), fx=model_params['stride'], fy=model_params['stride'],interpolation=cv2.INTER_CUBIC)
        heatmap = heatmap[:imageToTest_padded.shape[0] - pad[2], :imageToTest_padded.shape[1] - pad[3], :]
        heatmap = cv2.resize(heatmap, (oriImg.shape[1], oriImg.shape[0]), interpolation=cv2.INTER_CUBIC)

        paf = np.squeeze(output_blobs[0])  # output 0 is PAFs
        paf = cv2.resize(paf, (0, 0), fx=model_params['stride'], fy=model_params['stride'],interpolation=cv2.INTER_CUBIC)
        paf = paf[:imageToTest_padded.shape[0] - pad[2], :imageToTest_padded.shape[1] - pad[3], :]
        paf = cv2.resize(paf, (oriImg.shape[1], oriImg.shape[0]), interpolation=cv2.INTER_CUBIC)

        seg = np.squeeze(output_blobs[2])
        seg = cv2.resize(seg, (0, 0), fx=model_params['stride'], fy=model_params['stride'],
                             interpolation=cv2.INTER_CUBIC)
        seg = seg[:imageToTest_padded.shape[0] - pad[2], :imageToTest_padded.shape[1] - pad[3], :]
        seg = cv2.resize(seg, (oriImg.shape[1], oriImg.shape[0]), interpolation=cv2.INTER_CUBIC)

        heatmap_recover, paf_recover, seg_recover = recover_flipping_output(oriImg, heatmap, paf, seg)

        heatmap_avg = heatmap_avg + heatmap_recover
        paf_avg = paf_avg + paf_recover

        if m==0:
            segmap_scale5 = seg_recover
        elif m==1:
            segmap_scale6 = seg_recover
        elif m==2:
            segmap_scale7 = seg_recover
        elif m==3:
            segmap_scale8 = seg_recover

    heatmap_avg = heatmap_avg / (len(multiplier)*2)
    paf_avg = paf_avg / (len(multiplier)*2)

    segmap_a = np.maximum(segmap_scale1,segmap_scale2)
    segmap_b = np.maximum(segmap_scale4,segmap_scale3)
    segmap_c = np.maximum(segmap_scale5,segmap_scale6)
    segmap_d = np.maximum(segmap_scale7,segmap_scale8)
    seg_ori = np.maximum(segmap_a, segmap_b)
    seg_flip = np.maximum(segmap_c, segmap_d)
    seg_avg = np.maximum(seg_ori, seg_flip)

    all_peaks = []
    peak_counter = 0

    for part in range(18):
        map_ori = heatmap_avg[:, :, part]
        map = gaussian_filter(map_ori, sigma=3)

        map_left = np.zeros(map.shape)
        map_left[1:, :] = map[:-1, :]
        map_right = np.zeros(map.shape)
        map_right[:-1, :] = map[1:, :]
        map_up = np.zeros(map.shape)
        map_up[:, 1:] = map[:, :-1]
        map_down = np.zeros(map.shape)
        map_down[:, :-1] = map[:, 1:]

        peaks_binary = np.logical_and.reduce(
            (map >= map_left, map >= map_right, map >= map_up, map >= map_down, map > params['thre1']))
        peaks = list(zip(np.nonzero(peaks_binary)[1], np.nonzero(peaks_binary)[0]))  # note reverse
        peaks_with_score = [x + (map_ori[x[1], x[0]],) for x in peaks]
        id = range(peak_counter, peak_counter + len(peaks))
        peaks_with_score_and_id = [peaks_with_score[i] + (id[i],) for i in range(len(id))]

        all_peaks.append(peaks_with_score_and_id)
        peak_counter += len(peaks)

    connection_all = []
    special_k = []
    mid_num = 10

    for k in range(len(mapIdx)):
        score_mid = paf_avg[:, :, [x - 19 for x in mapIdx[k]]]
        candA = all_peaks[limbSeq[k][0] - 1]
        candB = all_peaks[limbSeq[k][1] - 1]
        nA = len(candA)
        nB = len(candB)
        indexA, indexB = limbSeq[k]
        if (nA != 0 and nB != 0):
            connection_candidate = []
            for i in range(nA):
                for j in range(nB):
                    vec = np.subtract(candB[j][:2], candA[i][:2])
                    norm = math.sqrt(vec[0] * vec[0] + vec[1] * vec[1])
                    # failure case when 2 body parts overlaps
                    if norm == 0:
                        continue
                    vec = np.divide(vec, norm)

                    startend = list(zip(np.linspace(candA[i][0], candB[j][0], num=mid_num), \
                                   np.linspace(candA[i][1], candB[j][1], num=mid_num)))

                    vec_x = np.array(
                        [score_mid[int(round(startend[I][1])), int(round(startend[I][0])), 0] \
                         for I in range(len(startend))])
                    vec_y = np.array(
                        [score_mid[int(round(startend[I][1])), int(round(startend[I][0])), 1] \
                         for I in range(len(startend))])

                    score_midpts = np.multiply(vec_x, vec[0]) + np.multiply(vec_y, vec[1])
                    score_with_dist_prior = sum(score_midpts) / len(score_midpts) + min(
                        0.5 * oriImg.shape[0] / norm - 1, 0)
                    criterion1 = len(np.nonzero(score_midpts > params['thre2'])[0]) > 0.8 * len(
                        score_midpts)
                    criterion2 = score_with_dist_prior > 0
                    if criterion1 and criterion2:
                        connection_candidate.append([i, j, score_with_dist_prior,
                                                     score_with_dist_prior + candA[i][2] + candB[j][2]])

            connection_candidate = sorted(connection_candidate, key=lambda x: x[2], reverse=True)
            connection = np.zeros((0, 5))
            for c in range(len(connection_candidate)):
                i, j, s = connection_candidate[c][0:3]
                if (i not in connection[:, 3] and j not in connection[:, 4]):
                    connection = np.vstack([connection, [candA[i][3], candB[j][3], s, i, j]])
                    if (len(connection) >= min(nA, nB)):
                        break

            connection_all.append(connection)
        else:
            special_k.append(k)
            connection_all.append([])

    # last number in each row is the total parts number of that person
    # the second last number in each row is the score of the overall configuration
    subset = -1 * np.ones((0, 20))
    candidate = np.array([item for sublist in all_peaks for item in sublist])

    for k in range(len(mapIdx)):
        if k not in special_k:
            partAs = connection_all[k][:, 0]
            partBs = connection_all[k][:, 1]
            indexA, indexB = np.array(limbSeq[k]) - 1

            for i in range(len(connection_all[k])):  # = 1:size(temp,1)
                found = 0
                subset_idx = [-1, -1]
                for j in range(len(subset)):  # 1:size(subset,1):
                    if subset[j][indexA] == partAs[i] or subset[j][indexB] == partBs[i]:
                        subset_idx[found] = j
                        found += 1

                if found == 1:
                    j = subset_idx[0]
                    if (subset[j][indexB] != partBs[i]):
                        subset[j][indexB] = partBs[i]
                        subset[j][-1] += 1
                        subset[j][-2] += candidate[partBs[i].astype(int), 2] + connection_all[k][i][2]
                elif found == 2:  # if found 2 and disjoint, merge them
                    j1, j2 = subset_idx
                    membership = ((subset[j1] >= 0).astype(int) + (subset[j2] >= 0).astype(int))[:-2]
                    if len(np.nonzero(membership == 2)[0]) == 0:  # merge
                        subset[j1][:-2] += (subset[j2][:-2] + 1)
                        subset[j1][-2:] += subset[j2][-2:]
                        subset[j1][-2] += connection_all[k][i][2]
                        subset = np.delete(subset, j2, 0)
                    else:  # as like found == 1
                        subset[j1][indexB] = partBs[i]
                        subset[j1][-1] += 1
                        subset[j1][-2] += candidate[partBs[i].astype(int), 2] + connection_all[k][i][2]
                # if find no partA in the subset, create a new subset
                elif not found and k < 17:
                    row = -1 * np.ones(20)
                    row[indexA] = partAs[i]
                    row[indexB] = partBs[i]
                    row[-1] = 2
                    row[-2] = sum(candidate[connection_all[k][i, :2].astype(int), 2]) + \
                              connection_all[k][i][2]
                    subset = np.vstack([subset, row])

    # delete some rows of subset which has few parts occur
    deleteIdx = []
    for i in range(len(subset)):
        if subset[i][-1] < 4 or subset[i][-2] / subset[i][-1] < 0.4:
            deleteIdx.append(i)
    subset = np.delete(subset, deleteIdx, axis=0)

    # canvas = cv2.imread(input_image)
    '''
    for i in range(18):
        for j in range(len(all_peaks[i])):
            cv2.circle(canvas, all_peaks[i][j][0:2], 4, colors[i], thickness=-1)
    '''
    stickwidth = 4

    for i in range(17):
        for n in range(len(subset)):
            index = subset[n][np.array(limbSeq[i]) - 1]
            if -1 in index:
                continue
            cur_canvas = Img.copy()
            Y = candidate[index.astype(int), 0]
            X = candidate[index.astype(int), 1]
            mX = np.mean(X)
            mY = np.mean(Y)
            length = ((X[0] - X[1]) ** 2 + (Y[0] - Y[1]) ** 2) ** 0.5
            angle = math.degrees(math.atan2(X[0] - X[1], Y[0] - Y[1]))
            polygon = cv2.ellipse2Poly((int(mY), int(mX)), (int(length / 2), stickwidth), int(angle), 0,
                                       360, 1)
            cv2.fillConvexPoly(Img, polygon, colors[i])
            canvas = cv2.addWeighted(Img, 0.3, cur_canvas, 0.7, 0)
    people = []
    subset_int = subset.astype(int)
    for i in range(len(subset_int)):
        people.append([])
        for j in range(18):
            if subset_int[i][j] == -1:
                people[-1].append([-1, -1])
            else:
                people[-1].append([candidate[subset_int[i][j], 0], candidate[subset_int[i][j], 1]])

    # code.interact(local=locals())
    return canvas, heatmap_avg, paf_avg, people, seg_avg

def run_image(input_path:str, output_path:str):

    print('-' * 20)
    print('Downloading Weights File...\nPlease Wait...')
    print('-' * 20)
    weights_path = tf.keras.utils.get_file('cdcl.h5',
              					'https://github.com/Adeel-Intizar/Xtreme-Vision/releases/download/1.0/cdcl_model.h5',
                  			cache_subdir = 'weights/', cache_dir = 'xtreme_vision')

    print('Processing Started...')
    # load model
    model = get_testing_model_resnet101()
    model.load_weights(weights_path)
    params, model_params = config_reader()

    scale_list = [1.0]
    params['scale_search'] = scale_list

    if input_path.endswith(".png") or input_path.endswith(".jpg"):

        Img = cv2.imread(input_path)
        flipImg = cv2.flip(Img, 1)
        oriImg = (Img / 256.0) - 0.5
        flipImg = (flipImg / 256.0) - 0.5

        canvas, heatmap, paf, people, seg = process(oriImg, flipImg, params, model_params, model, Img)
        print('Done... Processing is finished.')
        cv2.imwrite(output_path, canvas)
        # seg_argmax = np.argmax(seg, axis=-1)
        # seg_max = np.max(seg, axis=-1)
        # th_mask = part_thresholding(seg_argmax)
        # seg_max_thres = (seg_max > 0.1).astype(np.uint8)
        # seg_argmax *= seg_max_thres
        # seg_canvas = human_seg_combine_argmax(seg_argmax)
        # # cur_canvas = cv2.imread(args.input_folder+'/'+filename)
        # canvas = cv2.addWeighted(seg_canvas, 0.6, Img, 0.4, 0)
        # # filename = '%s/%s.jpg'%(args.output_folder,'seg_'+filename)
        # cv2.imwrite('seg-' + output_path, canvas)

def run_video(input_path:str, output_path:str, fps:int = 25):

    print('-' * 20)
    print('Downloading Weights File...\nPlease Wait...')
    print('-' * 20)
    weights_path = tf.keras.utils.get_file('cdcl.h5',
              					'https://github.com/Adeel-Intizar/Xtreme-Vision/releases/download/1.0/cdcl_model.h5',
                  			cache_subdir = 'weights/', cache_dir = 'xtreme_vision')

    # load model
    model = get_testing_model_resnet101()
    model.load_weights(weights_path)
    params, model_params = config_reader()
    params['scale_search'] = [1.0]

    if not (input_path.endswith(".mp4") or input_path.endswith(".avi")):
        raise RuntimeError ('Invalid video format, Valid formats are mp4 and avi.')
    else:

        out = None
        cap = cv2.VideoCapture(input_path)
        length = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
        print(f'\nThere are {length} Frames in this video')
        print('-' * 20)
        print('Detecting Objects in the Video... Please Wait...')
        print('-' * 20)

        while(cap.isOpened()):
            retreive, frame = cap.read()
            if not retreive:
                break

            flipImg = cv2.flip(frame, 1)
            oriImg = (frame / 256.0) - 0.5
            flipImg = (flipImg / 256.0) - 0.5

            canvas, heatmap, paf, people, seg = process(oriImg, flipImg, params, model_params, model, frame)

            if out is None:
                fourcc = cv2.VideoWriter_fourcc(*'DIVX')
                out = cv2.VideoWriter(output_path, fourcc, fps, (frame.shape[1], frame.shape[0]))

            out.write(canvas)
        print('Done. Processing has been Finished... Please Check Output Video.')
        out.release()
        cap.release()

max_stars_repo_name: Adeel-Intizar/Xtreme-Vision

max_issues_repo_name: Adeel-Intizar/Xtreme-Vision

max_forks_repo_name: Adeel-Intizar/Xtreme-Vision

reason: import numpy,import scipy,from scipy


text:
import pickle
import traceback
import glob
import os
import numpy as np
import modules.common_params.common_headless as c
import modules.memory_classes.memory_headless as m
import modules.queues.queue_headless as q
import modules.organisms.organism_headless as o
import math
import json
from fungera_headless import FungeraHeadless
import sys

sys.modules['modules.memory'] = m
sys.modules['modules.queue'] = q
sys.modules['modules.organism'] = o
sys.modules['modules.common'] = c

import numpy as np
import cv2
from matplotlib import pyplot as plt

img = cv2.imread('messi.jpg')
font = cv2.FONT_HERSHEY_COMPLEX_SMALL
font_color = (255, 255, 255)
thick = 1
font_size = 0.9

class SnapshotMap:
    def __init__(self, cells=np.array([40, 40]), padding=10, cellsize=np.array([20, 20])):
        size = np.multiply(cells, cellsize)
        size = size + padding * 2
        self.image = np.zeros((*size, 3), np.uint8)
        self.padding = padding
        self.grid = cells
        self.cellsize = cellsize

    def _create_grid(self):
        color = (255, 0, 0)
        thickness = 2

        for i in range(self.grid[0]):
            for j in range(self.grid[1]):
                start = np.array((i * self.cellsize[0], j * self.cellsize[1])) + self.padding
                end = start + self.cellsize

                self.image = cv2.rectangle(self.image, start, end, 10000, 1)

    def show(self):
        plt.imshow(self.image, cmap='Greys')
        plt.show()

    def _putchar(self, coords, char):
        coords = np.multiply(self.cellsize, np.array(coords) + 1)
        offset = np.array([10, -10])
        coords[0] -= self.padding
        coords[1] += self.padding
        coords = coords + offset

        self.image = cv2.putText(self.image, char, coords, font, .5, (255, 255, 255), thick, cv2.LINE_AA)

    def frommatrix(self, matrix):
        for i in range(len(matrix)):
            for j in range(len(matrix[0])):
                self._putchar((i, j), matrix[j][i])

    def draw_bounds(self, start, end):

        start = np.multiply(np.array(start), self.cellsize) + self.padding
        end = np.multiply(np.array(end), self.cellsize) + self.padding
        self.image = cv2.rectangle(self.image, start[::-1], end[::-1], (0, 100, 0), -1)
        self.image = cv2.rectangle(self.image, start[::-1], end[::-1], (255, 255, 255), 1)

def get_organism_commands(start, size, memory):
    return memory.memory_map[
           start[0]: start[0] + size[0],
           start[1]: start[1] + size[1],
           ]

dir = 'test'

if __name__ == '__main__':
    with open(c.config['snapshot_to_load'], 'rb') as f:
        state = pickle.load(f)
        queue = state['queue']
        memory = state['memory']

        snapshot = SnapshotMap(cells=np.array([memory.memory_map.shape[1], m.memory.memory_map.shape[0]]))

        # print(snapshot.image)
        # snapshot.show()

        for i, organism in enumerate(queue.organisms):
            commands = snapshot.draw_bounds(organism.start, organism.start + organism.size)
        snapshot.frommatrix(memory.memory_map)
        cv2.imwrite(c.config['output_file'], snapshot.image)

    # m.memory.update(refresh=True)


text:
import sys
import math
import numpy as np

def stripped_lines(filename):
    with open(filename) as f:
        for line in f.readlines():
            yield line.strip()

def parse(line):
    return line[0], int(line[1:])

def parse_file(filename):
    return [parse(line) for line in stripped_lines(filename)]

def rotate(action, amount, vector):
    rads = amount * math.pi / 180.0
    if action == 'R':
        rads = -rads
    rot = np.array([
        [math.cos(rads), -math.sin(rads)],
        [math.sin(rads), math.cos(rads)],
    ])
    return np.dot(rot, vector)

def simulate1(values):
    facing = np.array([1, 0])
    xpos = 0
    ypos = 0
    for action, amount in values:
        if action == 'N':
            ypos += amount
        elif action == 'S':
            ypos -= amount
        elif action == 'E':
            xpos += amount
        elif action == 'W':
            xpos -= amount
        elif action == 'L' or action == 'R':
            facing = rotate(action, amount, facing)
        elif action == 'F':
            xpos += amount * facing[0]
            ypos += amount * facing[1]
    return xpos, ypos

def simulate2(values):
    boat_x = 0
    boat_y = 0
    way_x = 10
    way_y = 1
    for action, amount in values:
        if action == 'N':
            way_y += amount
        elif action == 'S':
            way_y -= amount
        elif action == 'E':
            way_x += amount
        elif action == 'W':
            way_x -= amount
        elif action == 'L' or action == 'R':
            way = rotate(action, amount, np.array([way_x, way_y]))
            way_x = way[0]
            way_y = way[1]
        elif action == 'F':
            boat_x += amount * way_x
            boat_y += amount * way_y
    return boat_x, boat_y

def main(args):
    inputs = parse_file(args[1])
    xpos, ypos = simulate1(inputs)
    p1_ans = int(round(abs(xpos) + abs(ypos)))
    print(f'part one: {p1_ans}')
    xpos, ypos = simulate2(inputs)
    p2_ans = int(round(abs(xpos) + abs(ypos)))
    print(f'part two: {p2_ans}')
    # part one: 1631
    # part two: 58606

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


text:
# -*- coding: utf-8 -*
import os
import numpy as np
import pickle
import matplotlib
# matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
import tensorflow as tf
import gym
from gym import wrappers
from collections import deque
import itertools

from Config import DQfDConfig
from DQfD import DQfD
from DDQN import DDQN
from expert import value_iteration as expert_VI
from canonical_plot import plot

def trans_state(observation):
    moved_dict={'True':1,'False':0}
    s1=np.array([moved_dict[str(observation['moved'])]])
    s2=observation['scent']
    s3=observation['vision']
    s3=s3.ravel()
    state=np.append(s1,s2)
    state=np.append(state,s3)
    return state

# ===================================================== #
#                    train with DQfD                    #
# ===================================================== #
def run_DQfD(index, env, file_demo, file_name):
    with open(file_demo+'demo.p', 'rb') as f:
        demo_transitions = pickle.load(f)
        demo_transitions = deque(itertools.islice(demo_transitions, 0, DQfDConfig.demo_buffer_size))
        assert len(demo_transitions) == DQfDConfig.demo_buffer_size
    with tf.variable_scope('DQfD_' + str(index)):
        agent = DQfD(env, DQfDConfig(), demo_transitions=demo_transitions)

    agent.pre_train()  # use the demo data to pre-train network

    REWARDS, REWARD100, episode, replay_full_episode = [], [], 0, None
    reward100, n_step_reward, state = 0, None, env.reset()
    state = trans_state(state)
    t_q = deque(maxlen=DQfDConfig.trajectory_n)
    for steps in range(DQfDConfig.episode):
        action = agent.egreedy_action(state)  # e-greedy action for train
        next_state, reward, done, _ = env.step(action)
        next_state = trans_state(next_state)
        reward100 += reward
        REWARDS.append(reward)
        t_q.append([state, action, reward, next_state, done, 0.0])

        # record the earliest reward for the sub-sequence
        if len(t_q) < t_q.maxlen:
            reward_to_sub = 0.
        else:
            reward_to_sub = t_q[0][2]
            if n_step_reward is None:  # only compute once when t_q first filled
                n_step_reward = sum([t[2]*DQfDConfig.GAMMA**i for i, t in enumerate(t_q)])
            else:
                n_step_reward = (n_step_reward - reward_to_sub) / DQfDConfig.GAMMA
                n_step_reward += reward*DQfDConfig.GAMMA**(DQfDConfig.trajectory_n-1)

            t_q[0].extend([n_step_reward, next_state, done, t_q.maxlen])  # actual_n is max_len here
            update_eps=True if (steps+1)%DQfDConfig.eps_gap==0 else False
            agent.perceive(t_q[0], update_eps=update_eps)  # perceive when a transition is completed
            if (steps+1)%DQfDConfig.UPDATE_ESTIMATE_NET==0:
                agent.train_Q_network(update=False)  # train along with generation
            replay_full_episode = replay_full_episode or episode

        state = next_state

        if (steps+1) % DQfDConfig.UPDATE_TARGET_NET == 0:
            if agent.replay_memory.full(): agent.sess.run(agent.update_target_net)

        if (steps+1) % DQfDConfig.eps_gap == 0:
            episode += 1
            if replay_full_episode is not None:
                print("episode: {}  trained-episode: {}  reward100: {}  memory length: {}  epsilon: {}"
                      .format(episode, episode-replay_full_episode, reward100, len(agent.replay_memory), agent.epsilon))
            REWARD100.append(reward100)
            reward100 = 0

        if (steps+1) % (DQfDConfig.eps_gap*100) == 0:
            with open(file_name+'REWARD100.p', 'wb') as f: pickle.dump(REWARD100, f, protocol=2)
            with open(file_name+'REWARD100.txt', 'wb') as f: f.write(str(REWARD100))
            with open(file_name+'REWARDS.p', 'wb') as f: pickle.dump(REWARDS, f, protocol=2)
            with open(file_name+'REWARDS.txt', 'wb') as f: f.write(str(REWARDS))
            plot(1, REWARDS, file_name)

    with open(file_name+'REWARD100.p', 'wb') as f: pickle.dump(REWARD100, f, protocol=2)
    with open(file_name+'REWARD100.txt', 'wb') as f: f.write(str(REWARD100))
    with open(file_name+'REWARDS.p', 'wb') as f: pickle.dump(REWARDS, f, protocol=2)
    with open(file_name+'REWARDS.txt', 'wb') as f: f.write(str(REWARDS))
    plot(1, REWARDS, file_name)

# ===================================================== #
#              generate demonstration data              #
# ===================================================== #
# extend [n_step_reward, n_step_away_state] for transitions in demo
def set_n_step(container, n):
    t_list = list(container)
    # accumulated reward of first (trajectory_n-1) transitions
    n_step_reward = sum([t[2] * DQfDConfig.GAMMA**i for i, t in enumerate(t_list[0:min(len(t_list), n) - 1])])
    for begin in range(len(t_list)):
        end = min(len(t_list) - 1, begin + DQfDConfig.trajectory_n - 1)
        n_step_reward += t_list[end][2]*DQfDConfig.GAMMA**(end-begin)
        # extend[n_reward, n_next_s, n_done, actual_n]
        t_list[begin].extend([n_step_reward, t_list[end][3], t_list[end][4], end-begin+1])
        n_step_reward = (n_step_reward - t_list[begin][2])/DQfDConfig.GAMMA
    return t_list

def get_demo_greedy(env, file_name):
    demo_buffer = deque()
    demo = []
    demo_sas = []
    REWARDS, REWARD100, reward100 = [], [], 0
    state = env.reset()
    vision = state['vision']
    n_tongs = env._agent._items[1]  # number of tongs carried
    q_value = value_iteration(vision, n_tongs)
    action = np.argmax(q_value[5, 5, 0] + 1e-2 * np.random.rand(3))  # add noise to break ties
    state = trans_state(state)
    print(DQfDConfig.demo_buffer_size)
    for steps in range(DQfDConfig.demo_buffer_size):
        next_state, reward, done, _ = env.step(action)
        reward100 += reward
        REWARDS.append(reward)
        vision = next_state['vision']
        n_tongs = env._agent._items[1]
        q_value = expert_VI(vision, n_tongs)
        action = np.argmax(q_value[5, 5, 0] + 1e-2 * np.random.rand(3))
        next_state = trans_state(next_state)
        demo.append([state, action, reward, next_state, done, 1.0])
        state = next_state

        if (steps+1)%DQfDConfig.eps_gap == 0:
            print("demo - steps: {}  reward100: {}".format(steps, reward100))
            REWARD100.append(reward100)
            reward100 = 0

        if (steps+1)%(DQfDConfig.eps_gap*10) == 0:
            with open(file_name+'REWARD100.p', 'wb') as f: pickle.dump(REWARD100, f, protocol=2)
            with open(file_name+'REWARD100.txt', 'wb') as f: f.write(str(REWARD100))
            with open(file_name+'REWARDS.p', 'wb') as f: pickle.dump(REWARDS, f, protocol=2)
            with open(file_name+'REWARDS.txt', 'wb') as f: f.write(str(REWARDS))
            plot(1, REWARDS, file_name)

    with open(file_name+'REWARD100.p', 'wb') as f: pickle.dump(REWARD100, f, protocol=2)
    with open(file_name+'REWARD100.txt', 'wb') as f: f.write(str(REWARD100))
    with open(file_name+'REWARDS.p', 'wb') as f: pickle.dump(REWARDS, f, protocol=2)
    with open(file_name+'REWARDS.txt', 'wb') as f: f.write(str(REWARDS))
    plot(1, REWARDS, file_name)

    demo = set_n_step(demo, DQfDConfig.trajectory_n)
    demo_buffer.extend(demo)
    with open(file_name+'demo.txt', "w") as file: file.write(str(demo_buffer))
    with open(file_name+'demo.p', 'wb') as f: pickle.dump(demo_buffer, f, protocol=2)

# ===================================================== #
#                          main                         #
# ===================================================== #
if __name__ == '__main__':
    folder = './results_greedy/'
    if not os.path.exists(folder): os.makedirs(folder)

    env = gym.make(DQfDConfig.ENV_NAME)

    # ----------- get demo REWARD100 by DDQN --------------- #
    # file_demo = folder + 'dqfd_demoDDQN_'
    # get_demo_ddqn(env, file_demo) # get demo REWARD100 by DDQN
    # -------- get demo REWARD100 by "greedy expert" ------- #
    file_demo = folder + 'dqfd_s_'
    # get_demo_greedy(env, file_demo)

    file_name = folder + 'dqfd_s_trial' + str(DQfDConfig.trial_num) + '_'
    run_DQfD(DQfDConfig.trial_num, env, file_demo, file_name)

    env.close()


text:
# -*- coding: utf-8 -*-
# 抽样函数及其傅里叶变换

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib
import mpl_toolkits.axisartist as axisartist
font = {'family':'SimHei'}
matplotlib.rc('font',**font)
matplotlib.rcParams['axes.unicode_minus']=False
fig = plt.figure()
# 第一个图
plt.subplot(1,2,1)# 画布分为一列两行，取第一个
matplotlib.rcParams['axes.unicode_minus']=False
fig = plt.figure()
# 第一个图
plt.subplot(1,2,1)
fig = plt.figure(figsize=(6,6))
ax = axisartist.Subplot(fig, 111)
fig.add_axes(ax)
ax.axis[:].set_visible(False)
ax.axis["x"] = ax.new_floating_axis(0,0)
ax.axis["x"].set_axisline_style("->", size = 1.0)
ax.axis["y"] = ax.new_floating_axis(1,0)
ax.axis["y"].set_axisline_style("-|>", size = 1.0)
ax.axis["x"].set_axis_direction("top")
ax.axis["y"].set_axis_direction("right")
t = np.arange(-16,16,0.1)
y = np.sin(t)/t
plt.title(r'$Sa(t)=\frac{\sin(t)}{t}$',fontsize=14)
plt.text(0,-0.06,'O',fontdict={'size':16})
plt.text(16.5,-0.06,'t',fontdict={'size':14})
plt.xticks([])
plt.yticks([])
plt.plot(t,y,'b')
plt.show()

fig = plt.figure(figsize=(6,6))
ax = axisartist.Subplot(fig, 111)
fig.add_axes(ax)
ax.axis[:].set_visible(False)
ax.axis["x"] = ax.new_floating_axis(0,0)
ax.axis["x"].set_axisline_style("->", size = 1.0)
ax.axis["y"] = ax.new_floating_axis(1,0)
ax.axis["y"].set_axisline_style("-|>", size = 1.0)
ax.axis["x"].set_axis_direction("top")
ax.axis["y"].set_axis_direction("right")
t = np.arange(-11,11,0.1)
y = np.sin(t)/t
plt.title(r'$Sa(t)=\frac{\sin(t)}{t}$',fontsize=14)
plt.text(0,-0.06,'O',fontdict={'size':16})
plt.text(11,-2,r'$\omega_0$',fontdict={'size':14})
plt.plot(t,np.fft.fft(y),'b')# 傅里叶变换
plt.show()


text:
import numpy as np

def groupify_array(arr):
    # Input: Pyarrow/Numpy array
    # Output:
    #   - 1. Unique values
    #   - 2. Count per unique
    #   - 3. Sort index
    #   - 4. Begin index per unique
    dic, counts = np.unique(arr, return_counts=True)
    sort_idx = np.argsort(arr)
    return dic, counts, sort_idx, [0] + np.cumsum(counts)[:-1].tolist()

def combine_column(table, name):
    return table.column(name).combine_chunks()

def _hash(val):
    # EDGE CASE FOR https://github.com/mabel-dev/opteryx/issues/98
    # hashing NULL doesn't result in the same value each time
    try:
        if all(np.isnan(v) for v in val):
            return 0
    except TypeError:  # np.isnan fails on strings
        pass
    return hash(val)

def columns_to_array(table, columns):
    """modified for Opteryx"""
    columns = [columns] if isinstance(columns, str) else list(set(columns))
    if len(columns) == 1:
        # FIX https://github.com/mabel-dev/opteryx/issues/98
        # hashing NULL doesn't result in the same value each time
        return combine_column(table, columns[0]).to_numpy(zero_copy_only=False)
    values = [c.to_numpy() for c in table.select(columns).itercolumns()]
    return np.array(list(map(_hash, zip(*values))))

max_stars_repo_name: mabel-dev/mabel-sql

max_issues_repo_name: mabel-dev/mabel-sql

max_forks_repo_name: mabel-dev/mabel-sql


text:
import cv2
import numpy as np

def getOpticalFlow(video):
    """Calculate dense optical flow of input video
    Args:
        video: the input video with shape of [frames,height,width,channel]. dtype=np.array
    Returns:
        flows_x: the optical flow at x-axis, with the shape of [frames,height,width,channel]
        flows_y: the optical flow at y-axis, with the shape of [frames,height,width,channel]
    """
    # initialize the list of optical flows
    gray_video = []
    # print(len(video))
    for i in video:
        img = cv2.cvtColor(i, cv2.COLOR_RGB2GRAY)

        gray_video.append(np.reshape(img, (224, 224, 1)))

    flows = []
    for i in range(63):
        # calculate optical flow between each pair of frames
        flow = cv2.calcOpticalFlowFarneback(gray_video[i], gray_video[i + 1], None, 0.5, 3, 15, 3, 5, 1.2,
                                            cv2.OPTFLOW_FARNEBACK_GAUSSIAN)
        # subtract the mean in order to eliminate the movement of camera
        flow[..., 0] -= np.mean(flow[..., 0])
        flow[..., 1] -= np.mean(flow[..., 1])
        # normalize each component in optical flow
        flow[..., 0] = cv2.normalize(flow[..., 0], None, 0, 255, cv2.NORM_MINMAX)
        flow[..., 1] = cv2.normalize(flow[..., 1], None, 0, 255, cv2.NORM_MINMAX)
        # Add into list
        flows.append(flow)

    # Padding the last frame as empty array
    flows.append(np.zeros((224, 224, 2)))

    return np.array(flows, dtype=np.float32)

def normalize(data):
    mean = np.mean(data)
    std = np.std(data)
    return (data - mean) / std

def process_frames(frames_64):
    frames = np.array(frames_64, dtype=np.uint8)
    # Get the optical flow of video
    flows = getOpticalFlow(frames)

    result = np.zeros((len(flows), 224, 224, 5))
    result[..., :3] = frames
    result[..., 3:] = flows

    result[..., :3] = normalize(result[..., :3])
    result[..., 3:] = normalize(result[..., 3:])

    return np.reshape(result, [1, 64, 224, 224, 5])


text:
import numpy as np
import scipy.stats
from ChunkBasedLearner.load_mwus import load_mwus as load_cbl_mwus
from PredictionBasedSegmenter.load_mwus import load_mwus as load_pbs_mwus

def print_mwu_info(mwu_type):

    for corpus_name in ['NA', 'BE']:

        if mwu_type == 'CBL':
            mwu_counter = load_cbl_mwus(corpus_name)
        elif mwu_type == 'PBS':
            mwu_counter = load_pbs_mwus(corpus_name)
        else:
            raise Exception('mwu_type must be one of: CBL, PBS')

        mwus = list(mwu_counter.keys())
        mwu_lengths = [len(i) for i in mwus]
        nr_mwu_types = len(mwus)
        nr_mwu_tokens = sum([mwu_counter[i] for i in mwus])

        print(corpus_name, mwu_type)
        print('nr MWU tokens: %s' % nr_mwu_tokens)
        print('nr MWU types: %s' % nr_mwu_types)
        print('median MWU length: %s (IQR: %s)' % (np.median(mwu_lengths), scipy.stats.iqr(mwu_lengths)))
        print()

if __name__ == '__main__':
    print_mwu_info(mwu_type='CBL')
    print_mwu_info(mwu_type='PBS')

reason: import numpy,import scipy