text:
'''
Reference: https://github.com/adsodemelk/PRST
'''
# -*- coding: utf-8 -*-
from __future__ import print_function, division
import copy

__all__ = ["rldecode", "rlencode", "units", "mcolon", "recursive_diff", "gridtools"]

import plot.velocity_src.gridtools

import numpy as np
import scipy.sparse
from scipy.sparse import csr_matrix
import scipy.sparse as sps

class Struct(dict):
    """
    MATLAB-struct-like object.

    Source: http://stackoverflow.com/questions/35988/

    """
    def __init__(self, **kwargs):
        super(Struct, self).__init__(**kwargs)
        self.__dict__ = self

def rlencode(A, axis=0):
    """
    Compute run length encoding of array A along axis.

    Synopsis:
        A, n = rlencode(A)
        A, n = rlencode(A, axis)

    Arguments:
        A (np.ndarray): Array to be encoded.
        axis (Optional[int]): Axis of A where run length encoding is done.
                              Default value: axis=0

    Example (default axis):
        >>> A = np.array([
        ...     [1, 2, 3, 4],
        ...     [1, 2, 3, 4],
        ...     [3, 4, 5, 6],
        ...     [3, 3, 3, 3],
        ...     [3, 3, 4, 5],
        ...     [3, 3, 4, 5]])
        >>> A, n = rlencode(A, 0)
        >>> print(A)
        [[1 2 3 4]
         [3 4 5 6]
         [3 3 3 3]
         [3 3 4 5]]
        >>> print(n)
        [2 1 1 2]

    Example (j-axis):
        >>> A = np.array([
        ...     [1,1,3,3,3,3],
        ...     [2,2,4,3,3,3],
        ...     [3,3,5,3,4,4],
        ...     [4,4,6,3,5,5]])
        >>> A, n = rlencode(A, 1)
        >>> print(A)
        [[1 3 3 3]
         [2 4 3 3]
         [3 5 3 4]
         [4 6 3 5]]
        >>> print(n)
        [2 1 1 2]
    """
    # Let the relevant axis be the first axis
    B = np.swapaxes(A, 0, axis)

    # Flatten axes that are normal to the encoding axis
    B = B.reshape([B.shape[0],-1])

    # Pick indices where the next index is different
    i = np.append(np.where(np.any(B[:-1] != B[1:], axis=1)), B.shape[0]-1)

    # Find the number of repetitions
    n = np.diff(np.insert(i, 0, -1))

    # Pick necessary slices of the encoding axis
    return A.take(i, axis=axis), n

def rldecode(A, n, axis=0):
    """
    Decompresses run length encoding of array A along axis.

    Synopsis:
        B = rldecode(A, n, axis)
        B = rldecode(A, n)        # axis assumed to be 0

    Arguments:
        A (np.ndarray): Encoded array
        n (np.ndarray): Repetition of each layer along an axis.
        axis (Optional[int]): Axis of A where run length decoding is done.

    Returns:
        Uncompressed matrix

    Example (1D-array) along default axis:
        >>> A = np.array([1,4,5])
        >>> n = np.array([4,2,1])
        >>> print(rldecode(A, n))
        [1 1 1 1 4 4 5]

    Example (2D-array) along j-axis:
        >>> A = np.array([
        ...     [1,3,3,3],
        ...     [2,4,3,3],
        ...     [3,5,3,4],
        ...     [4,6,3,5]])
        >>> n = np.array([2,1,1,2])
        >>> print(rldecode(A, n, axis=1))
        [[1 1 3 3 3 3]
         [2 2 4 3 3 3]
         [3 3 5 3 4 4]
         [4 4 6 3 5 5]]
    """
    assert n.size > 0, "Length array was empty."
    # repeat functions take 1d array
    if n.ndim != 1:
        assert n.ndim <= 2
        assert n.shape[0] == 1 or n.shape[1] == 1
        n = n.ravel()
    return A.repeat(n, axis=axis)

def mcolon(lo, hi, s=None):
    """
    Compute concatenated ranges.

    Synopsis:
        mcolon(lo, hi)
        mcolon(lo, hi, stride)

    Arguments:
        lo (ndarray):
            1d array of lower bounds
        hi (ndarray):
            1d array of upper bounds
        s (Optional[ndarray]):
            1d array of strides. Default = np.ones(lo.shape) (unit strides).

    Returns:
        np.r_[lo[0]:hi[0], ..., lo[-1]:hi[-1]]
        np.r_[lo[0]:hi[0]:s[0], ..., lo[-1]:hi[-1]:s[-1]]
        (The NumPy r_ index trick builds a concatenated array of ranges.)

    Example:
        >>> lo = np.array([0,0,0,0])
        >>> hi = np.array([2,3,4,5])
        >>> ind = mcolon(lo, hi)
        >>> np.array_equal(ind, np.array([0,1,0,1,2,0,1,2,3,0,1,2,3,4]))
        True
    """
    if s is None:
        ranges = [range(l,h) for (l,h) in zip(lo,hi)]
    else:
        ranges = [range(l,h,st) for (l,h,st) in zip(lo,hi,s)]
    return np.concatenate(ranges)

def recursive_diff(A, B, indent=0):
    """
    Shows which attributes differ between two objects. Recursive.

    Synopsis:
        recursive_diff(A, B)

    Example:
        >> from prst.gridprocessing import cartGrid
        >> G, V = cartGrid([3,3,3]), cartGrid([3,3,4])
        >> recursive_diff(G, V)
        ====== Recursive comparison ======
         gridType
           Equal, (list,list)
         cells
           facePos
             NOT EQUAL, (ndarray,ndarray)
           num
             NOT EQUAL, (int,int)
           indexMap
             NOT EQUAL, (ndarray,ndarray)
        ...

    """
    def pprint(*args, **kwargs):
        print(" "*indent, *args, **kwargs)

    if indent == 0:
        print()
        print("====== Recursive comparison ======")

    # For classes, try to get dict attribute
    try:
        A = A.__dict__
    except:
        pass
    try:
        B = B.__dict__
    except:
        pass
    if isinstance(A, dict) and isinstance(B, dict):
        # Descend into attributes which exist in both and are dicts. Print them first.
        pass
        inA = set(A.keys())
        inB = set(B.keys())
        notInA = inB - inA
        notInB = inA - inB
        inBoth = inA & inB
        # Print attributes only in A
        if notInA:
            pprint("A MISSING ATTRIBUTES:", notInA)
        # Print attributes only in B
        if notInB:
            pprint("B MISSING ATTRIBUTES:", notInB)
        # Recursively do the same with common attributes
        for attr in inBoth:
            pprint(attr)
            recursive_diff(A[attr], B[attr], indent+2)

    else:
        # Compare A, B for equality
        equal = False
        try:
            equal = None
            close = None
            if isinstance(A, np.ndarray) and isinstance(B, np.ndarray):
                equal = np.array_equal(A, B)
                close = np.allclose(A, B)
            else:
                equal = A == B
            if equal:
                pprint("Equal, ", end="")
            else:
                pprint("NOT EQUAL, ", end="")
                if close:
                    print("(BUT APPROXIMATELY EQUAL)", end="")
        except:
            pprint("NOT COMPARABLE, ", end="")

        print("("+A.__class__.__name__+","+B.__class__.__name__+")")

class ADI(object):
    """ADI: Automatic DIfferentiation

    Simple implementation of automatic differentiation for easy construction
    of Jacobian matrices.

    Synopsis:
        x = ADI(value, jacobian)

    Arguments:
        value(np.ndarray):
            The numerical value of the object. Must be a NumPy column array.
            Not compatible with matrices (neither np.matrix nor
            scipy.sparse.spmatrix).

        jacobian(list[scipy.sparse.csr_matrix]):
            The Jacobian of the object. Split into parts to improve
            performance.

    Comment:
        This class is typically instantiated for a set of variables using
        initVariablesADI, not by itself.

        Many methods found in `np.ndarray` are also implemented by ADI. Example:

            x, = initVariablesADI(np.array([[2, 3, 4]]).T)
            y = x.log()
            z = x.sum()

        Using "np." methods is not supported yet, e.g., `np.dot(A, x)` where x
        is an ADI object will not work as expected, and is not recommended. A
        compatability layer, `prst.utils.npad` is provided. `npad.dot(A, x)`
        will work correctly for any number of AD arguments, and uses `np.dot(A,
        x)` if neither arguments are AD objects. Future versions of NumPy
        (>0.12) will most likely deprecate `npad` with the __numpy_ufunc__
        functionality.

    See also:
        initVariablesADI
    """
    # Requires __numpy_ufunc__ for syntactical sugar. Hopefully will be added to NumPy 1.12...
    # https://github.com/numpy/numpy/issues/7519

    __array_priority__ = 10000
    ndim = 2

    def __init__(self, val, jac):
        self.val = val
        self.jac = jac
        if not isinstance(self.jac, list):
            self.jac = [self.jac,]

    def __repr__(self):
        jacstring = str([block.shape for block in self.jac])
        return "(val: {0}.T, jac block sizes: {1})".format(self.val.T, jacstring)

    def pprint(self, name=None):
        """
        Pretty-print full matrices with limited decimals.

        Example:

            import numpy as np
            from prst.utils import initVariablesADI

            x0 = np.array([[1,2,3,2,3]]).T
            x, = initVariablesADI(x0)
            y = x**2
            y.pprint()

        Output:

            ADI properties
                val: [[1 4 9 4 9]].T

                jac[0]  [[ 2.  0.  0.  0.  0.]
                         [ 0.  4.  0.  0.  0.]
                         [ 0.  0.  6.  0.  0.]
                         [ 0.  0.  0.  4.  0.]
                         [ 0.  0.  0.  0.  6.]]
        """
        namestr = ""
        if name:
            namestr = name + " "
        lines = [
            namestr + "ADI properties",
            "\tval: " + str(self.val.T) + ".T",
        ]
        for i, j in enumerate(self.jac):
            lines.append("\n\tjac[" + str(i) + "]" + "\t" + str(j.toarray()).replace("\n", "\n\t\t"))
        lines.append("")
        print("\n".join(lines))

    def copy(self):
        return copy.deepcopy(self)

    #def __len__(self):
        #raise NotImplementedError("Use shape[0]. See http://stackoverflow.com/questions/37529715/")

    @property
    def shape(self):
        return self.val.shape

    def __ge__(u, v):
        try:
            return u.val >= v.val
        except AttributeError:
            return u.val >= v

    def __gt__(u, v):
        try:
            return u.val > v.val
        except AttributeError:
            return u.val > v

    def __le__(u, v):
        try:
            return u.val <= v.val
        except AttributeError:
            return u.val <= v

    def __lt__(u, v):
        try:
            return u.val < v.val
        except AttributeError:
            return u.val < v

    def __pos__(u): # +u
        return u.copy()

    def __neg__(u): # -u
        return ADI(-u.val, [-j for j in u.jac])

    def __add__(u, v): # u + v
        if isinstance(v, ADI):
            if u.val.shape[0] == v.val.shape[0]:
                return ADI(u.val + v.val, [ju+jv for (ju,jv) in zip(u.jac, v.jac)])
            if v.val.shape[0] == 1:
                # Tile v.jac to same length as u.jac since sparse matrices
                # don't broadcast properly.
                # https://github.com/scipy/scipy/issues/2128
                vjac = [sps.bmat([[j]]*len(u.val)) for j in v.jac]
                retjac = [ju+jv for (ju,jv) in zip(u.jac, vjac)]
                return ADI(u.val+v.val, retjac)
            if u.val.shape[0] == 1:
                # Vice versa, this time tile u instead
                ujac = [sps.bmat([[j]]*len(v.val)) for j in u.jac]
                retjac = [ju+jv for (ju,jv) in zip(ujac, v.jac)]
                return ADI(u.val+v.val, retjac)
            raise ValueError("Dimension mismatch")
        # v isn't AD object
        v = np.atleast_2d(v)
        return ADI(u.val + v, copy.deepcopy(u.jac))

    def __radd__(v, u): # u + v
        return v.__add__(u)

    def __sub__(u, v):
        return u.__add__(-v)

    def __rsub__(v, u): # u - v
        return (-v).__add__(u)

    # mul
    def __mul__(u, v):
        """Hadamard product u*v."""
        if isinstance(v, ADI):
            if len(u.val) == len(v.val):
                # Note: scipy.sparse.diags has changed parameters between
                # versions 0.16x and 0.17x. This code is only tested on 0.16x.
                # TODO test code in SciPy 0.17x
                uJv = [sps.diags([u.val.flat],[0])*jv for jv in v.jac] # MATRIX multiplication
                vJu = [sps.diags([v.val.flat],[0])*ju for ju in u.jac] # MATRIX multiplication
                jac = [a+b for (a,b) in zip(uJv, vJu)]
                return ADI(u.val*v.val, jac)
            if len(v.val) == 1:
                # Fix dimensions and recurse
                vval = np.tile(v.val, (u.val.shape[0],1) )
                vjac = [sps.bmat([[j]]*len(u.val)) for j in v.jac]
                return u.__mul__(ADI(vval, vjac))
            if len(u.val) == 1:
                # Fix dimensions and recurse
                uval = np.tile(u.val, (v.val.shape[0],1) )
                ujac = [sps.bmat([[j]]*len(v.val)) for j in u.jac]
                return ADI(uval, ujac).__mul__(v)
            raise ValueError("Dimension mismatch")
        else:
            v = np.atleast_2d(v)
            if len(u.val) == 1:
                val = u.val * v
                jac = [sps.diags(v.flat,0)*sps.bmat([[j]]*len(v)) for j in u.jac]
                return ADI(val, jac)
            if len(v) == 1:
                return ADI(u.val*v, [v.flat[0]*ju for ju in u.jac])
            if len(u.val) == len(v):
                vJu = [sps.diags(v.flat, 0)*ju for ju in u.jac] # MATRIX multiplication
                return ADI(u.val*v, vJu)
            raise ValueError("Dimension mismatch")

    def __rmul__(v, u):
        # u * v = v * u
        return v.__mul__(u)

    def dot(u, A): # u x A
        return _dot(u, A)

    def __pow__(u, v):
        return u._pow(u, v)

    # This method is static so that it can be called with non-ADI u
    # E.g. when calculating 2**u, where u is ADI.
    @staticmethod
    def _pow(u, v):
        """Elementwise power, u**v."""
        if not isinstance(v, ADI): # u is AD, v is a scalar or vector
            v = np.atleast_2d(v)
            tmp = v*u.val**(v-1)
            uvJac = [_spdiag(tmp)*ju for ju in u.jac]
            return ADI(u.val**v, uvJac)
        elif not isinstance(u, ADI): # u is a scalar, v is AD
            u = np.atleast_2d(u)
            tmp = u**v.val*np.log(u)
            uvJac = [sps.diags(tmp.flat, 0)*jv for jv in v.jac]
            return ADI(u**v.val, uvJac)
        else: # u and v are ADI objects of same length
            if  len(u.val) != len(v.val):
                raise ValueError("Must be same length")
            # d(u^v)/dx = diag(u^v o (v / u))*
            # + diag(u^v o log(u))*J
            tmp1 = u.val**v.val * v.val/u.val
            tmp2 = u.val**v.val * np.log(u.val)
            uvJacPart1 = [sps.diags(tmp1.flat, 0)*ju for ju in u.jac]
            uvJacPart2 = [sps.diags(tmp2.flat, 0)*jv for jv in v.jac]
            uvJac = [a+b for (a,b) in zip(uvJacPart1, uvJacPart2)]
            return ADI(u.val**v.val, uvJac)

    def __rpow__(v, u):
        """u**v where u is not ADI."""
        return v._pow(u, v)

    def __div__(u, v):
        raise DeprecationWarning("Add 'from __future__ import division'.")

    def __truediv__(u, v):
        return u * v**(-1.0)

    def __rdiv__(v, u):
        raise DeprecationWarning("Add 'from __future__ import division'.")

    def __rtruediv__(v, u):
        return u * v**(-1.0)

    def __getitem__(u, s):
        """
        Slices the column array using NumPy syntax.

        Examples: (x is ADI object)

            x[(2,1),:]
            x[1]
            x[1,:]
            x[np.array([True,True,False])]
            x[np.array([False,False,False]),:]
            x[np.array([2,1,0]),:]
            x[np.array([2]),:]
            x[::-1]
        """
        val = np.atleast_2d(u.val[s])
        if val.shape[0] != 1 and val.shape[1] != 1:
            raise ValueError("Slice type not supported")
        if val.shape[1] != 1:
            val = val.T
        try:
            s = s[0]
        except TypeError:
            pass
        jac = [j[s,:] for j in u.jac]
        return ADI(val, jac)

    def __setitem__(u, s, v):
        """
        Sets values in ADI vector.

        If the right side is non-ADI, the corresponding Jacobian rows are set to zero.
        If the right side is ADI, the corresponding Jacobian rows are overwritten.
        """
        if isinstance(v, ADI):
            # This part is not so pretty, and could probably
            # be improved.
            if u.val[s].ndim <= 1:
                u.val[s] = v.val.ravel()
            elif u.val[s].ndim == 2:
                u.val[s] = v.val
            else:
                raise ValueError("This should never happen.")
            try:
                s = s[0]
            except TypeError:
                pass
            for i in range(len(u.jac)):
                u.jac[i][s] = v.jac[i]
        else:
            u.val[s] = v
            try:
                s = s[0]
            except TypeError:
                pass
            for i in range(len(u.jac)):
                u.jac[i][s] = 0

    def max(u):
        """Return the maximum element in the array."""
        i = np.argmax(u.val)
        return ADI(np.atleast_2d(u.val[i,:]), [j[i,:] for j in u.jac])

    def min(u):
        """Return the minimum element in the array."""
        i = np.argmin(u.val)
        return ADI(np.atleast_2d(u.val[i,:]), [j[i,:] for j in u.jac])

    def sum(u):
        """Return the sum of the array elements."""
        val = u.val.sum(keepdims=True)
        jac = [sps.csr_matrix(j.sum(axis=0)) for j in u.jac]
        return ADI(val, jac)

    def sin(u):
        """Return element-wise sine of array."""
        val = np.sin(u.val)
        cosval = np.cos(u.val)
        jac = [sps.diags(cosval.flat, 0)*j for j in u.jac]
        return ADI(val, jac)

    def cos(u):
        """Return element-wise cosine of array."""
        val = np.cos(u.val)
        msinval = -np.sin(u.val)
        jac = [sps.diags(msinval.flat, 0)*j for j in u.jac]
        return ADI(val, jac)

    def exp(u):
        val = np.exp(u.val)
        jac = [sps.diags(val.flat, 0)*j for j in u.jac]
        return ADI(val, jac)

    def log(u):
        val = np.log(u.val)
        m = sps.diags((1/u.val).flat, 0)
        jac = [m*j for j in u.jac]
        return ADI(val, jac)

    def sign(u):
        return np.sign(u.val)

    def abs(u):
        val = np.abs(u.val)
        sgn = np.sign(u.val)
        jac = [sps.diags(sgn.flat, 0)*j for j in u.jac]
        return ADI(val, jac)

    def __numpy_ufunc__(self, func, method, pos, inputs, **kwargs):
        """Placeholder method for future NumPy versions."""
        raise NotImplementedError("NumPy has finally added __numpy_ufunc__ support, but "
                                   "PRST has not added support yet.")

# NumPy binary ufunc wrappers
def _dot(u, v):
    """Matrix multiplication."""
    if isinstance(u, ADI) and isinstance(v, ADI):
        # u_ad, v_ad
        assert u.val.shape[0] == v.val.shape[0] == 1, "dot(ad,ad) only valid for 1x1 arguments"
        return u * v
    elif isinstance(u, ADI) and not isinstance(v, ADI):
        # u_ad, v
        v = np.atleast_2d(v)
        assert v.shape[0] == 1, "dot(ad,vec) only valid for 1x1 vec."
        return u*v
    elif not isinstance(u, ADI) and isinstance(v, ADI):
        # u, v_ad
        if not hasattr(u, "dot"):
            u = np.atleast_2d(u)
        u_sp = sps.csr_matrix(u)
        return ADI(u.dot(v.val), [u_sp*j for j in v.jac])
    else:
        # u, v
        if hasattr(u, "dot"):
            return u.dot(v)
        return np.dot(u, v)

def _tile(A, reps):
    if isinstance(A, ADI):
        if len(reps) != 2 or reps[1] != 1:
            raise TypeError("AD vectors can only be tiled vertically.")
        val = np.tile(A.val, reps)
        jac = [sps.bmat([[j]]*reps[0]) for j in A.jac]
        return ADI(val, jac)
    else:
        return np.tile(A, reps)

# Numpy unary ufunc wrappers
# The unary wrappers are all following the same formula, and can possibly be
# removed entirely by making `npad` more magic with __getattr__.
def _sign(u):
    if isinstance(u, ADI):
        return u.sign()
    else:
        return np.sign(u)

def _abs(u):
    """np.abs for AD array."""
    if isinstance(u, ADI):
        return u.abs()
    else:
        return np.abs(u)

def _exp(u):
    """np.exp for AD array."""
    if isinstance(u, ADI):
        return u.exp()
    else:
        return np.abs(u)

# NumPy n-ary functions

def _vstack(tup):
    """np.vstack for AD array."""
    vals = np.vstack((u.val for u in tup))
    jacs = []
    num_jacs = len(tup[0].jac)
    for j in range(num_jacs):
        jacs.append(sps.bmat([[u.jac[j]] for u in tup]))
    return ADI(vals, jacs)

def _concatenate(tup, axis):
    """np.concatenate for AD array."""
    if axis != 0:
        raise TypeError("ADI objects can only be concatenated vertically.")
    return _vstack(tup)

# Register ufunc wrappers so they can be easily imported.
npad = Struct()
# n-ary
npad.vstack = _vstack
npad.concatenate = _concatenate
# binary
npad.dot = _dot
npad.tile = _tile
# unary
npad.sign = _sign
npad.abs = _abs

def initVariablesADI(*variables):
    """
    Returns AD (automatic differentiation) variables.

    See `help(prst.utils.ADI)` for documentation.
    """
    # Convert all inputs to column arrays
    vals = list(variables)
    for i in range(len(vals)):
        vals[i] = np.atleast_2d(vals[i])
        if vals[i].shape[1] == 0:
            vals[i] = vals[i].reshape(-1,1)
        elif vals[i].shape[1] != 1:
            raise ValueError("AD variables must be column vectors.")

    numvals = np.array([len(val) for val in vals])
    n = len(vals)

    ret = [None]*n
    for i in range(n):
        nrows = numvals[i]
        # Set Jacobians wrt other variables to zero-matrices
        jac = [None]*n
        for j in np.r_[0:i, (i+1):n]:
            ncols = numvals[j]
            jac[j] = scipy.sparse.csr_matrix((nrows, ncols))

        # Set Jacobian of current variable wrt itself to the identity matrix.
        jac[i] = scipy.sparse.identity(nrows, format="csr")

        ret[i] = ADI(vals[i], jac)
    return ret

def _spdiag(val_column):
    """Improved version of scipy.sparse.diags."""
    if val_column.shape[0] == 0:
        return sps.csr_matrix((1,0))
    return sps.diags(val_column.flat, 0, format="csr")

max_stars_repo_name: zabaras/bayesmultiscale

max_issues_repo_name: zabaras/bayesmultiscale

max_forks_repo_name: zabaras/bayesmultiscale

reason: import numpy,import scipy,from scipy


text:
# """
# Created on Feb 05, 2020

# @author: esukei
# """

import numpy as np
import pytest

from heterogeneoushmm.heterogeneous import HeterogeneousHMM
from hmmlearn.utils import normalize
from test_gaussian_hmm import make_covar_matrix

class TestHeterogeneousHMM:
    """
    Test based on the example provided on:
        https://hmmlearn.readthedocs.io/en/latest/tutorial.html#training-hmm-parameters-and-inferring-the-hidden-states
    """

    covariance_type = 'diagonal'  # set by subclasses

    @pytest.fixture(autouse=True)
    def setup(self):
        self.prng = prng = np.random.RandomState(10)
        self.n_states = n_states = 3
        self.n_g_emissions = n_g_emissions = 2
        self.n_d_emissions = 1
        self.n_d_features = [2]
        self.pi = prng.rand(n_states)
        self.pi = self.pi / self.pi.sum()
        self.A = prng.rand(n_states, n_states)
        self.A /= np.tile(self.A.sum(axis=1)[:, np.newaxis], (1, n_states))
        self.B = np.asarray(
            [
                np.random.random((self.n_states, self.n_d_features[i]))
                for i in range(self.n_d_emissions)
            ]
        )
        for i in range(self.n_d_emissions):
            normalize(self.B[i], axis=1)
        self.means = prng.randint(-20, 20, (n_states, n_g_emissions))
        self.covars = make_covar_matrix(
            self.covariance_type, self.n_states, self.n_g_emissions, random_state=prng
        )

    def test_bad_init_type(self):
        with pytest.raises(ValueError):
            _ = HeterogeneousHMM(
                n_states=self.n_states,
                n_g_emissions=self.n_g_emissions,
                n_d_emissions=self.n_d_emissions,
                n_d_features=self.n_d_features,
                init_type="badinit_type",
            )

    def test_bad_covariance_type(self):
        with pytest.raises(ValueError):
            _ = HeterogeneousHMM(
                n_states=self.n_states,
                n_g_emissions=self.n_g_emissions,
                n_d_emissions=self.n_d_emissions,
                n_d_features=self.n_d_features,
                covariance_type="badcovariance_type",
            )

    def test_score_samples_and_decode(self):
        h = HeterogeneousHMM(
            n_states=self.n_states,
            n_g_emissions=self.n_g_emissions,
            n_d_emissions=self.n_d_emissions,
            n_d_features=self.n_d_features,
            covariance_type=self.covariance_type,
        )

        h.pi = self.pi
        h.A = self.A
        # Make sure the means are far apart so posteriors.argmax()
        # picks the actual component used to generate the observations.
        h.means = 20 * self.means
        h.covars = self.covars
        h.B = self.B

        stateidx = np.repeat(np.arange(self.n_states), 5)
        n_samples = len(stateidx)
        X_gauss = self.prng.randn(
            n_samples, h.n_g_emissions) + h.means[stateidx]
        X = []
        for idx, state in enumerate(stateidx):
            cat_sample = []
            for e in range(h.n_d_emissions):
                cdf = np.cumsum(h.B[e][state, :])
                cat_sample.append((cdf > np.random.rand()).argmax())
            X.append(np.concatenate([X_gauss[idx], cat_sample]))
        X = [np.asarray(X)]
        posteriors = h.predict_proba(X)[
            0
        ]  # because we only had one observation sequence, but it returns a list anyways

        assert posteriors.shape == (n_samples, self.n_states)
        assert np.allclose(posteriors.sum(axis=1), np.ones(n_samples))

        _, stateseq = h.decode(X)
        assert np.allclose(stateseq, stateidx)

    def test_sample(self, n_samples=1000, n_sequences=5):
        h = HeterogeneousHMM(
            n_states=self.n_states,
            n_g_emissions=self.n_g_emissions,
            n_d_emissions=self.n_d_emissions,
            n_d_features=self.n_d_features,
            covariance_type=self.covariance_type,
        )
        h.pi = self.pi
        h.A = self.A
        h.B = self.B
        # Make sure the means are far apart so posteriors.argmax()
        # n_emissionscks the actual component used to generate the observations.
        h.means = 20 * self.means
        h.covars = np.maximum(self.covars, 0.1)

        X, state_sequences = h.sample(
            n_sequences=n_sequences, n_samples=n_samples, return_states=True)

        assert np.all(X[i].ndim == 2 for i in range(n_sequences))
        assert np.all(
            len(X[i]) == len(state_sequences[i]) == n_samples
            for i in range(n_sequences)
        )
        assert np.all(
            len(np.unique(X[i])) == (h.n_g_emissions + h.n_d_emissions)
            for i in range(n_sequences)
        )

    def test_train(self, n_samples=100, n_sequences=30, tr_params="stmc"):
        h = HeterogeneousHMM(
            n_states=self.n_states,
            n_g_emissions=self.n_g_emissions,
            n_d_emissions=self.n_d_emissions,
            n_d_features=self.n_d_features,
            init_type='kmeans',
            covariance_type=self.covariance_type,
            tr_params=tr_params,
            verbose=True,
        )
        h.pi = self.pi
        h.A = self.A
        h.B = self.B
        h.means = 20 * self.means
        h.covars = np.maximum(self.covars, 0.1)

        # Generate observation sequences
        X = h.sample(n_sequences=n_sequences, n_samples=n_samples)

        # Mess up the parameters and see if we can re-learn them.
        h, log_likelihoods = h._train(
            X, n_iter=10, conv_thresh=0.01, return_log_likelihoods=True, n_processes=4
        )

        # we consider learning if the log_likelihood increases
        assert np.all(np.round(np.diff(log_likelihoods), 10) >= 0)

    def test_train_without_init(self, n_samples=100, n_sequences=30, tr_params="ste"):
        h = HeterogeneousHMM(
            n_states=self.n_states,
            n_g_emissions=self.n_g_emissions,
            n_d_emissions=self.n_d_emissions,
            n_d_features=self.n_d_features,
            covariance_type=self.covariance_type,
            tr_params=tr_params,
        )

        h.pi = self.pi
        h.A = self.A
        h.means = 20 * self.means
        h.covars = np.maximum(self.covars, 0.1)
        h.B = self.B

        # Generate observation sequences
        X = h.sample(n_sequences=n_sequences, n_samples=n_samples)

        h_tst = HeterogeneousHMM(
            n_states=self.n_states,
            n_g_emissions=self.n_g_emissions,
            n_d_emissions=self.n_d_emissions,
            n_d_features=self.n_d_features,
            covariance_type=self.covariance_type,
            tr_params=tr_params,
        )
        with pytest.raises(AttributeError):
            h_tst, _ = h_tst._train(
                X, n_iter=100, conv_thresh=0.01, return_log_likelihoods=True, no_init=True, n_processes=4
            )

    def test_train_sequences_of_different_length(self, tr_params="stmc"):
        h = HeterogeneousHMM(
            n_states=self.n_states,
            n_g_emissions=self.n_g_emissions,
            n_d_emissions=self.n_d_emissions,
            n_d_features=self.n_d_features,
            covariance_type=self.covariance_type,
        )
        h.A = self.A
        h.pi = self.pi
        h.B = self.B
        h.means = self.means
        h.covars = self.covars
        h.tr_params = tr_params

        # Generate observation sequences
        lengths = [30, 40, 50]
        X = [
            h.sample(n_sequences=1, n_samples=n_samples)[0] for n_samples in lengths
        ]

        h, log_likelihoods = h._train(
            X, n_iter=10, conv_thresh=0.01, return_log_likelihoods=True
        )

        # we consider learning if the log_likelihood increases (the first one is discarded, because sometimes it drops
        # after the first iteration and increases for the rest
        assert np.all(np.round(np.diff(log_likelihoods[1:]), 10) >= 0)

    def test_non_trainable_emission(self, n_samples=100, n_sequences=30, tr_params="ste"):
        h = HeterogeneousHMM(
            n_states=self.n_states,
            n_g_emissions=self.n_g_emissions,
            n_d_emissions=self.n_d_emissions,
            n_d_features=self.n_d_features,
            covariance_type=self.covariance_type,
        )
        h.A = self.A
        h.pi = self.pi
        h.B = self.B
        h.means = self.means
        h.covars = self.covars
        h.tr_params = tr_params

        # Generate observation sequences
        X = h.sample(n_sequences=n_sequences, n_samples=n_samples)

        h_tst = HeterogeneousHMM(
            n_states=self.n_states,
            n_g_emissions=self.n_g_emissions,
            n_d_emissions=self.n_d_emissions,
            n_d_features=self.n_d_features,
            covariance_type=self.covariance_type,
            nr_no_train_de=1,
        )

        # Set up the emission probabilities and see if we can re-learn them.
        B_fix = np.eye(self.n_states, self.n_d_features[-1])

        with pytest.raises(AttributeError):
            h_tst, log_likelihoods = h_tst._train(
                X, n_iter=100, conv_thresh=0.01, return_log_likelihoods=True, no_init=False
            )

            # we consider learning if the log_likelihood increases
            assert np.all(np.round(np.diff(log_likelihoods), 10) >= 0)

            # we want that the emissions haven't changed
            assert np.allclose(B_fix, h_tst.B[-1])

class TestHeterogeneousHMMWithSphericalCovars(TestHeterogeneousHMM):
    covariance_type = "spherical"

class TestHeterogeneousHMMWithDiagonalCovars(TestHeterogeneousHMM):
    covariance_type = "diagonal"

class TestHeterogeneousHMMWithTiedCovars(TestHeterogeneousHMM):
    covariance_type = "tied"

class TestHeterogeneousHMMWithFullCovars(TestHeterogeneousHMM):
    covariance_type = "full"

ext: py

max_stars_repo_name: tahaceritli/HeterogeneousHMM

max_issues_repo_name: tahaceritli/HeterogeneousHMM

max_forks_repo_name: tahaceritli/HeterogeneousHMM

reason: import numpy


text:
# -*- coding: utf-8 -*-
"""
Created on Wed Jul 14 15:31:56 2021

@author: Mike
"""

# -*- coding: utf-8 -*-
"""
Created on Mon Aug  3 12:19:42 2020

@author: Mike
"""

import numpy as np
import pandas as pd
from datetime import datetime

from tensorflow.keras.layers import Dense, Input, LSTM, Embedding, Dropout, Activation, Flatten#, CuDNNLSTM

#TF 2 compatibility
#import keras
#from tensorflow.compat.v1.keras.layers import CuDNNLSTM

from tensorflow.keras.layers import Bidirectional, GlobalMaxPool1D, BatchNormalization, Conv1D, GlobalMaxPooling1D, MaxPooling1D
from tensorflow.keras.models import Model, Sequential
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau, CSVLogger
from tensorflow.keras.models import model_from_json
from tensorflow.keras import metrics, regularizers
#from tensorflow.keras.utils import multi_gpu_model
from tensorflow.keras.optimizers import Nadam, Adam

from sklearn.metrics import mean_squared_error, mean_absolute_error
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
from sklearn.model_selection import train_test_split as split

import tensorflow.keras.backend as K
import tensorflow.keras.callbacks as Kc

# TF2: Mixed precision

#from tensorflow.keras.mixed_precision import experimental as mixed_precision
#mixed_precision.set_policy('mixed_float16')

# TF2: Disable eager execution
import tensorflow as tf
"""
tf.compat.v1.disable_eager_execution()
tf.get_logger().setLevel('INFO')
"""
import time
from datetime import datetime
from random import randrange

#from PPM_CODE.Eval_helpers import*
#from PPM_CODE.HPO_searchspace import *

def GenSimpleModel(data_objects, model_params):
    # Clear the TF graph
    K.clear_session()

    # Load the data to check dimensionality
    x_train, y_train = data_objects["x_train"], data_objects["y_train"]
    print("Input data shape:",x_train.shape)

    ############################################

    # Number of block layers
    BLOCK_LAYERS = model_params["BLOCK_LAYERS"]

    # Number of cells
    FULLY_CONNECTED = model_params["FULLY_CONNECTED"]

    #CNN related params
    DROPOUT_RATE = model_params["DROPOUT_RATE"]

    #####################################

    input_dim = (x_train.shape[1], x_train.shape[2])

    model = Sequential()

    if BLOCK_LAYERS == 1:
        model.add(LSTM(FULLY_CONNECTED,  implementation=2,
                             recurrent_dropout=DROPOUT_RATE,
                             input_shape=input_dim,
                             return_sequences=False))

    # LSTM with batchNorm and recurrent dropout
    if BLOCK_LAYERS == 2:
        model.add(LSTM(FULLY_CONNECTED,  implementation=2,
                             recurrent_dropout=DROPOUT_RATE,
                             input_shape=input_dim,
                             return_sequences=True))
        model.add(BatchNormalization())

        model.add(LSTM(FULLY_CONNECTED,  implementation=2,
                             recurrent_dropout=DROPOUT_RATE,
                             input_shape=input_dim,
                             return_sequences=False))

    # LSTM with batchNorm and recurrent dropout
    if BLOCK_LAYERS == 3:
        model.add(LSTM(FULLY_CONNECTED,  implementation=2,
                             recurrent_dropout=DROPOUT_RATE,
                             input_shape=input_dim,
                             return_sequences=True))
        model.add(BatchNormalization())

        model.add(LSTM(FULLY_CONNECTED,  implementation=2,
                             recurrent_dropout=DROPOUT_RATE,
                             input_shape=input_dim,
                             return_sequences=True))
        model.add(BatchNormalization())

        model.add(LSTM(FULLY_CONNECTED,  implementation=2,
                             recurrent_dropout=DROPOUT_RATE,
                             input_shape=input_dim,
                             return_sequences=False))

    # LSTM with batchNorm and recurrent dropout
    if BLOCK_LAYERS == 4:
        model.add(LSTM(FULLY_CONNECTED,  implementation=2,
                             recurrent_dropout=DROPOUT_RATE,
                             input_shape=input_dim,
                             return_sequences=True))
        model.add(BatchNormalization())

        model.add(LSTM(FULLY_CONNECTED,  implementation=2,
                             recurrent_dropout=DROPOUT_RATE,
                             input_shape=input_dim,
                             return_sequences=True))
        model.add(BatchNormalization())

        model.add(LSTM(FULLY_CONNECTED,  implementation=2,
                             recurrent_dropout=DROPOUT_RATE,
                             input_shape=input_dim,
                             return_sequences=True))
        model.add(BatchNormalization())

        model.add(LSTM(FULLY_CONNECTED,  implementation=2,
                             recurrent_dropout=DROPOUT_RATE,
                             input_shape=input_dim,
                             return_sequences=False))

    model.add(Dense(1)) #, dtype='float32' #Only the softmax is adviced to be float32

    print(model.summary())

    return model, 0


text:
import argparse
import os
import json
from os.path import join

import numpy

def process_arguments(args):
    args.checkpoint_path = join(args.checkpoint_path, args.name)
    args.prediction_path = join(args.prediction_path, args.name)
    args.n_layers = int(args.gnn.split(':')[1].split(',')[0])
    args.n_heads = int(args.gnn.split(':')[1].split(',')[1])
    args.max_query_len = 50
    args.max_doc_len = 512
    args.training_samples = numpy.random.uniform(1, 90449, [1])

def save_settings(args):
    os.makedirs(args.checkpoint_path, exist_ok=True)
    os.makedirs(args.prediction_path, exist_ok=True)
    json.dump(args.__dict__, open(join(args.checkpoint_path, "run_settings.json"), 'w'))

def set_config():
    epoch = int(numpy.random.uniform(1, 50, [1]))

    parser = argparse.ArgumentParser()
    data_path = 'output'

    # Required parameters
    parser.add_argument("--name", type=str, default='default')
    parser.add_argument("--prediction_path", type=str, default=join(data_path, 'submissions'))
    parser.add_argument("--checkpoint_path", type=str, default=join(data_path, 'checkpoints'))

    parser.add_argument("--ckpt_id", type=int, default=0)
    parser.add_argument("--bert_model", type=str, default='bert-base-uncased',
                        help='Currently only support bert-base-uncased and bert-large-uncased')

    # learning and log
    parser.add_argument("--epochs", type=int, default=epoch)
    parser.add_argument("--qat_epochs", type=int, default=5)
    parser.add_argument("--batch_size", type=int, default=1)
    parser.add_argument("--max_bert_size", type=int, default=8)
    parser.add_argument("--lr", type=float, default=2e-5)
    parser.add_argument('--decay', type=float, default=1.0)
    parser.add_argument('--early_stop_epoch', type=int, default=0)
    parser.add_argument("--verbose_step", default=50, type=int)
    parser.add_argument("--grad_accumulate_step", default=2, type=int)

    parser.add_argument('--q_update', action='store_true', help='Whether update query')
    parser.add_argument('--basicblock_trans', action='store_true', help='transformer version basicblock')
    parser.add_argument("--prediction_trans", action='store_true', help='transformer version prediction layer')
    parser.add_argument("--trans_drop", type=float, default=0.5)
    parser.add_argument("--trans_heads", type=int, default=3)

    # device
    parser.add_argument("--encoder_gpu", default='0,1', type=str, help="device to place bert encoder.")
    parser.add_argument("--model_gpu", default='2', type=str, help="device to place model.")
    parser.add_argument("--input_dim", type=int, default=768, help="bert-base=768, bert-large=1024")

    # bi attn
    parser.add_argument("--bi_attn_drop", type=float, default=0.3)
    parser.add_argument("--hidden_dim", type=int, default=300)

    # graph net
    parser.add_argument('--tok2ent', default='mean_max', type=str, help='{mean, mean_max}')
    parser.add_argument('--gnn', default='gat:2,2', type=str, help='gat:n_layer, n_head')
    parser.add_argument("--gnn_drop", type=float, default=0.5)
    parser.add_argument("--gat_attn_drop", type=float, default=0.5)
    parser.add_argument('--q_attn', action='store_true', help='whether use query attention in GAT')
    parser.add_argument("--lstm_drop", type=float, default=0.3)

    # loss
    parser.add_argument("--type_lambda", type=float, default=1)
    parser.add_argument("--sp_lambda", type=float, default=5)
    parser.add_argument('--bfs_clf', action='store_true', help='Add BCELoss on bfs mask')
    parser.add_argument('--bfs_lambda', type=float, default=1)
    parser.add_argument("--sp_threshold", type=float, default=0.5)

    args = parser.parse_args()

    process_arguments(args)
    save_settings(args)

    return args

max_stars_repo_name: ByrdOfAFeather/DFGN-pytorch

max_issues_repo_name: ByrdOfAFeather/DFGN-pytorch

max_forks_repo_name: ByrdOfAFeather/DFGN-pytorch


text:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns

def plot_kinase_heatmap(heatmap, filename, idx, col, kinase):
    """
    plot and save the heatmap of given Jaccard's Index matrix

    Parameters
    ----------
    heatmap: dataframe of Jaccard's Index matrix
    filename: str
        output filename
    idx: str
        predictor name of the kinases represented by the indexes
    col: str
        predictor name of the kinases represented by the columns
    kinase: str
        kinase type

    """
    with sns.axes_style("white"):
        if kinase == 'y':
            sns.set(font_scale = 3)
            y_labels = True # show all kinase labels
        elif kinase == 'st':
            sns.set(font_scale = 2)
            y_labels, x_labels = reset_label (heatmap, 's') # only show kinases fit the criteria

        if -1 in heatmap.values: #between predictors
            # plot all kinases predicted by either of the predictor
            # add titles for the indexes and columns
            heatmap.index.name = idx
            heatmap.columns.name = col
            # plot heatmap with sorted Jaccard's Index matrix
            sort_heatmap (heatmap, kinase)
            plt.savefig(filename + '.eps', format='eps', bbox_inches="tight")
            if kinase == 'y':
                sort_heatmap (heatmap, 'st') # print another heatmap only show kinases that fit the criteria

            # plot overlapped kinases predicted by both of the predictor
            masked_heatmap = heatmap.loc[~np.all(heatmap == -1, axis=1), ~np.all(heatmap == -1, axis=0)]
            masked_heatmap.index.name = idx
            masked_heatmap.columns.name = col
            sort_heatmap (masked_heatmap,  kinase)
            plt.savefig(filename + '_overlap.eps', format='eps', bbox_inches="tight")
            if kinase == 'y':
                sort_heatmap (masked_heatmap, 'st')
        else: # within predictor
            ax = sns.clustermap(heatmap,
                            xticklabels = False, yticklabels = y_labels,
                            cbar_kws={"orientation": "horizontal"}, cbar_pos=(0.1975, -0.05, 0.7175, 0.025),
                            tree_kws=dict(linewidths=2), figsize = (50,50), vmin=0, vmax=1)
            ax.ax_col_dendrogram.set_visible(False)
            plt.savefig(filename + '.eps', format='eps', bbox_inches="tight")

def sort_heatmap (df, kinase):
    """
    sort the kinases in decreasing order of the same-kinase Jaccard's Index
    plot the heatmap in the sorted order

    Parameters
    ----------
    df: dataframe
        Jaccard's Index matrix
    kinase: str
        kinase type

    Returns
    -------
    ax: ploted heatmap of given Jaccard's Index matrix
    """
    # sort the given Jaccard's Index matrix
    order = df.stack().sort_values(ascending=False).reset_index()
    order = order[order.iloc[:,0] == order.iloc[:,1]].reset_index(drop=True)
    order = order.iloc[:,0].tolist()
    df = df.reindex(order)
    df = df.reindex(columns = order)

    if kinase == 'y':
        x_label = True
        y_label = True
    elif kinase == 'st':
        y_label, x_label = reset_label (df, 'p') # only show kinases fit the criteria

    # plot the ordered Jaccard's Index matrix
    plt.figure(figsize = (30,30))
    ax = sns.heatmap(df, mask = df == -1,
                     xticklabels = x_label, yticklabels = y_label,
                     square = True, vmin = 0, vmax = 1)
    ax.tick_params(left=True, bottom=True)
    return ax

def reset_label (df, df_type):
    """
    for S/T kinases, only show label if the kinases:
        within predictor: have at lease one Jaccard's Index, other than the comparison with itself, higher than 0.49
        between predictors: have at lease one Jaccard's Index is at the top 1%

    Parameters
    ----------
    df: dataframe
    df_type: str
        comparison type. 'p' for between predictors (paired); 's' for within predictor (single)

    Returns
    -------
    new_idx: list of labels that would show on the y-axis
    new_col: list of labels that would show on the x-axis
    """

    new_idx = []
    new_col = []
    if df_type == 'p': #between predictors (paired)
        for i in df.columns:
            if ((np.percentile(df[i], 99) <= df.at[i,i]) & (df[i] < 1)).any():
                new_col.append(i)
            else:
                new_col.append('')
        for j in df.index:
            if ((np.percentile(df.loc[j], 99) <= df.at[j,j]) & (df.loc[j] < 1)).any():
                new_idx.append(j)
            else:
                new_idx.append('')
    elif df_type == 's': # within predictor (single)
        thresh = 0.4900
        for i in df.columns:
            if ((df[i] >= thresh) & (df[i] < 1)).any():
                new_col.append(i)
            else:
                new_col.append('')
        for j in df.index:
            if ((df.loc[j] >= thresh) & (df.loc[j] < 1)).any():
                new_idx.append(j)
            else:
                new_idx.append('')

    return new_idx, new_col

max_stars_repo_name: NaegleLab/KinPred

max_issues_repo_name: NaegleLab/KinPred

max_forks_repo_name: NaegleLab/KinPred


text:
import cv2
import numpy as np
import matplotlib.pyplot as plt

# histogram equalization
def hist_equal(img, z_max=255):
    height, width, = img.shape
    S = height * width  * 1.
    img_equal = img.copy()
    sum_h = 0.
    for i in range(1, 255):
        ind = np.where(img == i)
        sum_h += len(img[ind])
        z_prime = sum_h / S * z_max
        img_equal[ind] = z_prime
    img_equal = img_equal.astype(np.uint8)
    return img_equal

img = cv2.imread("images/gamma.PNG",0)

# histogram normalization
img_equal = hist_equal(img)

# Display histogram
# plt.savefig("out_his.png")
# Save result
cv2.imshow("img",img)
cv2.imshow("result", img_equal)
plt.hist(img.ravel(), bins=255, rwidth=0.8, range=(0, 255))
plt.hist(img_equal.ravel(), bins=255, rwidth=0.8, range=(0, 255))
plt.show()
# cv2.imwrite("img_equal.jpg", out)
cv2.waitKey(0)
cv2.destroyAllWindows()

max_stars_repo_name: muyia/DIP-python

max_issues_repo_name: muyia/DIP-python

max_forks_repo_name: muyia/DIP-python


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

import argparse
import cv2
import dlib
import numpy as np
import os
import random
import sys

from skimage import io

from .. import helper
from .. import data

class NaiveDlib:

    def __init__(self, faceMean, facePredictor):
        """Initialize the dlib-based alignment."""
        self.detector = dlib.get_frontal_face_detector()
        self.normMeanAlignPoints = loadMeanPoints(faceMean)
        self.predictor = dlib.shape_predictor(facePredictor)

    def getAllFaceBoundingBoxes(self, img):
        return self.detector(img, 1)

    def getLargestFaceBoundingBox(self, img):
        faces = self.detector(img, 1)
        if len(faces) > 0:
            return max(faces, key=lambda rect: rect.width() * rect.height())

    def align(self, img, bb):
        points = self.predictor(img, bb)
        return list(map(lambda p: (p.x, p.y), points.parts()))

    def alignImg(self, method, size, img, bb=None,
                 outputPrefix=None, outputDebug=False,
                 expandBox=False):
        if outputPrefix:
            helper.mkdirP(os.path.dirname(outputPrefix))

            def getName(tag=None):
                if tag is None:
                    return "{}.png".format(outputPrefix)
                else:
                    return "{}-{}.png".format(outputPrefix, tag)

        if bb is None:
            try:
                bb = self.getLargestFaceBoundingBox(img)
            except Exception as e:
                print("Warning: {}".format(e))
                # In rare cases, exceptions are thrown.
                return
            if bb is None:
                # Most failed detection attempts return here.
                return

        alignPoints = self.align(img, bb)
        meanAlignPoints = transformPoints(self.normMeanAlignPoints, bb, True)

        (xs, ys) = zip(*meanAlignPoints)
        tightBb = dlib.rectangle(left=min(xs), right=max(xs),
                                 top=min(ys), bottom=max(ys))

        if method != 'tightcrop':
            npAlignPoints = np.float32(alignPoints)
            npMeanAlignPoints = np.float32(meanAlignPoints)

        if method == 'tightcrop':
            warpedImg = img
        elif method == 'affine':
            ss = np.array([39, 42, 57])  # Eyes and tip of nose.
            npAlignPointsSS = npAlignPoints[ss]
            npMeanAlignPointsSS = npMeanAlignPoints[ss]
            H = cv2.getAffineTransform(npAlignPointsSS, npMeanAlignPointsSS)
            warpedImg = cv2.warpAffine(img, H, np.shape(img)[0:2])
        elif method == 'perspective':
            ss = np.array([39, 42, 48, 54])  # Eyes and corners of mouth.
            npAlignPointsSS = npAlignPoints[ss]
            npMeanAlignPointsSS = npMeanAlignPoints[ss]
            H = cv2.getPerspectiveTransform(
                npAlignPointsSS, npMeanAlignPointsSS)
            warpedImg = cv2.warpPerspective(img, H, np.shape(img)[0:2])
        elif method == 'homography':
            (H, mask) = cv2.findHomography(npAlignPoints, npMeanAlignPoints,
                                           method=cv2.LMEDS)
            warpedImg = cv2.warpPerspective(img, H, np.shape(img)[0:2])
        else:
            print("Error: method '{}' is unimplemented.".format(method))
            sys.exit(-1)

        if method == 'tightcrop':
            wAlignPoints = alignPoints
        else:
            wBb = self.getLargestFaceBoundingBox(warpedImg)
            if wBb is None:
                return
            wAlignPoints = self.align(warpedImg, wBb)
            wMeanAlignPoints = transformPoints(
                self.normMeanAlignPoints, wBb, True)

        if outputDebug:
            annotatedImg = annotate(img, bb, alignPoints, meanAlignPoints)
            io.imsave(getName("orig"), img)
            io.imsave(getName("annotated"), annotatedImg)

            if method != 'tightcrop':
                wAnnotatedImg = annotate(warpedImg, wBb,
                                         wAlignPoints, wMeanAlignPoints)
                io.imsave(getName("warped"), warpedImg)
                io.imsave(getName("warped-annotated"), wAnnotatedImg)

        if len(warpedImg.shape) != 3:
            print("  + Warning: Result does not have 3 dimensions.")
            return None

        (xs, ys) = zip(*wAlignPoints)
        xRange = max(xs) - min(xs)
        yRange = max(ys) - min(ys)
        if expandBox:
            (l, r, t, b) = (min(xs) - 0.20 * xRange, max(xs) + 0.20 * xRange,
                            min(ys) - 0.65 * yRange, max(ys) + 0.20 * yRange)
        else:
            (l, r, t, b) = (min(xs), max(xs), min(ys), max(ys))
        (w, h, _) = warpedImg.shape
        if 0 <= l <= w and 0 <= r <= w and 0 <= b <= h and 0 <= t <= h:
            cwImg = cv2.resize(warpedImg[t:b, l:r], (size, size))
            h, edges = np.histogram(cwImg.ravel(), 16, [0, 256])
            s = sum(h)
            if any(h > 0.65 * s):
                print("Warning: Image is likely a single color.")
                return
        else:
            print("Warning: Unable to align and crop to the "
                  "face's bounding box.")
            return

        if outputPrefix:
            io.imsave(getName(), cwImg)
        return cwImg

def transformPoints(points, bb, toImgCoords):
    if toImgCoords:
        def scale(p):
            (x, y) = p
            return (int((x * bb.width()) + bb.left()),
                    int((y * bb.height()) + bb.top()))
    else:
        def scale(p):
            (x, y) = p
            return (float(x - bb.left()) / bb.width(),
                    float(y - bb.top()) / bb.height())
    return list(map(scale, points))

def loadMeanPoints(modelFname):
    def parse(line):
        (x, y) = line.strip().split(",")
        return (float(x), float(y))
    with open(modelFname, 'r') as f:
        return [parse(line) for line in f]

def annotate(img, box, points=None, meanPoints=None):
    a = np.copy(img)
    bl = (box.left(), box.bottom())
    tr = (box.right(), box.top())
    cv2.rectangle(a, bl, tr, color=(153, 255, 204), thickness=3)
    for p in points:
        cv2.circle(a, center=p, radius=3, color=(102, 204, 255), thickness=-1)
    for p in meanPoints:
        cv2.circle(a, center=p, radius=3, color=(0, 0, 0), thickness=-1)
    return a


text:
#!/usr/bin/env python
from numpy import sqrt,log
from sys import argv

if len(argv) == 2:
    q = float(argv[1])
    c = 1.
    print log(1 + sqrt(q))/c
elif len(argv) == 3:
    q = float(argv[1])
    c = float(argv[2])
    print log(1 + sqrt(q))/c

max_stars_repo_name: ernmeel/sweeny

max_issues_repo_name: ernmeel/sweeny

reason: from numpy


text:
# !/usr/bin/env python3
# -*- coding: utf-8 -*-
# ******************************************************************************
# Copyright (c) 2018 Mejbah ul Alam, Justin Gottschlich, Abdullah Muzahid
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# ******************************************************************************
"""utils.py

This file contains some utility function for data access, analyze, and
visualization.
"""

from __future__ import division

import os
from typing import Tuple
import contextlib

import numpy as np
from git import Repo

def compareDataset(datasetArrayBase: np.ndarray, datasetArray: np.ndarray) -> int:
  """Compares the mean values between two datasets.

  Args:
      datasetArrayBase: Original data.
      datasetArray: New data.

  Returns:
      L2 norm of the differences between the input array means.

  """
  meanBase = np.mean(datasetArrayBase, axis=0)
  meanComp = np.mean(datasetArray, axis=0)

  diffVal = np.linalg.norm(meanBase - meanComp)
  return diffVal

def getPerfCounterData(counterId: str, dataDir: str, scale: float) -> Tuple[list, str]:
  """Get the performance counter from the 3rd column of the CSV data file.

  Args:
      counterId: The performance counter name / ID.
      dataDir: The parent folder of the performance counter data.
      scale: Scale factor post-normalization. Accessed via `cfg.training.scale_factor`.

  Returns:
      A list of normalized performance counter results.
      The counter's header.
  """
  filename = dataDir + "/event_" + counterId + "_perf_data.csv"
  perfCounter = []
  with open(filename, 'r') as fp:
    for linenumber, line in enumerate(fp):
      if linenumber == 2:
        headers = line.strip().split(",")  # last one is the counter, 1 and 2  is thd id and instcouunt , 0 is mark id
        datasetHeader = headers[-1]
      if linenumber > 2:
        perfCounters = line.strip().split(",")
        # mark = int(perfCounters[0])  # unused
        # threadCount = int(perfCounters[1])  # unused
        instructionCount = int(perfCounters[2])
        currCounter = int(perfCounters[3])
        normalizedCounter = (currCounter / instructionCount) * scale
        perfCounter.append(normalizedCounter)

  return perfCounter, datasetHeader

def getAutoperfDir(directory: str = None) -> str:
  """Retrieves the full path to the repository's .autoperf directory.

  Args:
      directory: Path within the .autoperf dir.

  Returns:
      str: Path to .autoperf
  """
  repo = Repo(os.getcwd(), search_parent_directories=True)
  if directory:
    return os.path.join(repo.working_tree_dir, '.autoperf', directory)
  return os.path.join(repo.working_tree_dir, '.autoperf')

@contextlib.contextmanager
def set_working_directory(directory: str) -> str:
  """Temporarily cd into the working directory. After context is closed (e.g. the
  function is exited), returns to the previous working directory.

  Args:
      directory: Path to change to.

  Yields:
      New working directory.
  """
  owd = os.getcwd()
  try:
    os.chdir(directory)
    yield directory
  finally:
    os.chdir(owd)


text:
# -*- coding: utf-8 -*-
"""
Estimate Relaxation from Band Powers

This example shows how to buffer, epoch, and transform EEG data from a single
electrode into values for each of the classic frequencies (e.g. alpha, beta, theta)
Furthermore, it shows how ratios of the band powers can be used to estimate
mental state for neurofeedback.

The neurofeedback protocols described here are inspired by
*Neurofeedback: A Comprehensive Review on System Design, Methodology and Clinical Applications* by Marzbani et. al

Adapted from https://github.com/NeuroTechX/bci-workshop
"""

import numpy as np  # Module that simplifies computations on matrices
import matplotlib.pyplot as plt  # Module used for plotting
import matplotlib.animation as animation
from pylsl import StreamInlet, resolve_byprop  # Module to receive EEG data
import utils  # Our own utility functions
from blinkFilter import filt
from scipy import signal
import pyautogui # for spacebar
import datetime
import MoveCursor as m
import time
import _thread
from mne.filter import create_filter
from scipy.signal import lfilter, lfilter_zi
# Handy little enum to make code more readable

#pyautogui.PAUSE = 0.01                      # Make sure there is no delay between consecutive values when the autogui library is used

class Band:
    Delta = 0
    Theta = 1
    Alpha = 2
    Beta = 3

class Buffers:
    timestamp_buffer = None
    eeg_buffer = {}
    filter_state = None

""" EXPERIMENTAL PARAMETERS """
# Modify these to change aspects of the signal processing

# Length of the EEG data buffer (in seconds)
# This buffer will hold last n seconds of data and be used for calculations
BUFFER_LENGTH = 5

# Length of the epochs used to compute the FFT (in seconds)
EPOCH_LENGTH = 0.2

# Amount of overlap between two consecutive epochs (in seconds)
OVERLAP_LENGTH = 0.1

# Amount to 'shift' the start of each next consecutive epoch
SHIFT_LENGTH = EPOCH_LENGTH - OVERLAP_LENGTH

# Index of the channel(s) (electrodes) to be used
# 0 = left ear, 1 = left forehead, 2 = right forehead, 3 = right ear
INDEX_CHANNEL_LEFT = [1]
INDEX_CHANNEL_RIGHT = [2]

def input_thread(flag):
    input()
    flag.append(True)

def get_average_blink(eye_index):
    eeg_buffer = np.zeros((int(fs * BUFFER_LENGTH), 1))
    filter_state = None
    eye = "left" if eye_index == INDEX_CHANNEL_LEFT else "right"
    print("Please blink your " + eye + " eye. Press enter when done.")
    maxMatches = []
    stop_flag = []
    _thread.start_new_thread(input_thread, (stop_flag,))
    i = 10 # number of times to run after user presses enter
    while i > 0:
        if stop_flag:
            i = i - 1
        eeg_data, timestamp = inlet.pull_chunk(
            timeout=1, max_samples=int(SHIFT_LENGTH * fs))

        # Only keep the channel we're interested in
        ch_data = np.array(eeg_data)[:, eye_index]

        # Update EEG buffer with the new data
        eeg_buffer, filter_state = utils.update_buffer(
            eeg_buffer, ch_data, notch=True,
            filter_state=filter_state)

        """ 3.2 COMPUTE BAND POWERS """
        # Get newest samples from the buffer
        data_epoch = utils.get_last_data(eeg_buffer,
                int(EPOCH_LENGTH * fs))
        matchFilt = signal.hilbert(filt)
        matches = signal.correlate(matchFilt,data_epoch[:,0])
        matchesAbs = np.abs(matches[:])
        maxMatches.append((np.max(matchesAbs)/1e5).astype(int))

    maxMatchesIdx = np.argmax(maxMatches)
    idx = maxMatchesIdx
    peakValues = []
    stop_flag = 1
    check_right_side = False
    while True:
        if idx > 0 and idx < len(maxMatches) - 1:
            idx = idx + (1 if stop_flag == 2 else -1)
            possPeak = maxMatches[idx]
            if maxMatches[maxMatchesIdx]*0.6 < possPeak:
                peakValues.append(possPeak)
            elif stop_flag == 2:
                break
            else:
                check_right_side = True
        elif stop_flag == 2:
            break
        else:
            check_right_side = True
        if check_right_side:
            idx = maxMatchesIdx
            stop_flag = 2
            check_right_side = False

    return peakValues

def calibrate(rep):
    maxMatches = np.zeros((2, rep))
    maxMatchesLength = np.zeros((2, rep))
    for eye in [0 , 1]: # left = 0, right = 1
        for i in range(rep):
            temp = get_average_blink(
                INDEX_CHANNEL_LEFT if eye == 0 else INDEX_CHANNEL_RIGHT)
            maxMatches[eye, i] = np.average(temp)
            maxMatchesLength[eye, i] = len(temp)

    return np.average(maxMatches[0, :]), \
            np.average(maxMatches[1, :]), \
            np.average(maxMatchesLength[0, :]), \
            np.average(maxMatchesLength[1, :])

def live_plot(i, buff, ax, channel, update=False):
    if update:
        eeg_data, timestamp = inlet.pull_chunk(
            timeout=1, max_samples=int(SHIFT_LENGTH * fs))

        ch_data_left = np.array(eeg_data)[:, INDEX_CHANNEL_LEFT]
        ch_data_right = np.array(eeg_data)[:, INDEX_CHANNEL_RIGHT]
        timestamp = np.reshape(timestamp, (len(timestamp), 1))

        buff.eeg_buffer[str(INDEX_CHANNEL_LEFT)], filter_state = utils.update_buffer(
            buff.eeg_buffer[str(INDEX_CHANNEL_LEFT)], ch_data_left)
        buff.eeg_buffer[str(INDEX_CHANNEL_RIGHT)], filter_state = utils.update_buffer(
            buff.eeg_buffer[str(INDEX_CHANNEL_RIGHT)], ch_data_right)
        buff.timestamp_buffer, _ = utils.update_buffer(
            buff.timestamp_buffer, timestamp)

    xs = (buff.timestamp_buffer)
    ys = (buff.eeg_buffer[str(channel)])

    xs = xs[-500:]
    ys = ys[-500:]

    ax.clear()
    ax.set_ylim(-3000, 3000)
    ax.plot(xs, ys)

    plt.subplots_adjust(bottom=0.30)

def plot():
    buff = Buffers()

    buff.eeg_buffer[str(INDEX_CHANNEL_LEFT)] = np.zeros((int(fs * BUFFER_LENGTH), 1))
    buff.eeg_buffer[str(INDEX_CHANNEL_RIGHT)] = np.zeros((int(fs * BUFFER_LENGTH), 1))
    buff.timestamp_buffer = np.zeros((int(fs * BUFFER_LENGTH), 1))
    buff.filter_state = None

    fig = plt.figure(1)
    ax = fig.add_subplot(1, 1, 1)

    fig2 = plt.figure(2)
    ax2 = fig2.add_subplot(1, 1, 1)

    animL = animation.FuncAnimation(fig, live_plot,
            fargs=(buff, ax, INDEX_CHANNEL_LEFT,True,), interval=1)
#    animR = animation.FuncAnimation(fig2, live_plot,
#           fargs=(buff, ax2, INDEX_CHANNEL_RIGHT,), interval=1)
    plt.show()

def on_timer(   ):
        """Add some data at the end of each signal (real-time signals)."""

        samples, timestamps = inlet.pull_chunk(timeout=0.0, max_samples=100)
        if timestamps:
            samples = np.array(samples)[:, ::-1]

            data = np.vstack([data, samples])
            data = data[-n_samples :]
            filt_samples, filt_state = lfilter(
                bf, af, samples, axis=0, zi=filt_state
            )
            data_f = np.vstack([data_f, filt_samples])
            data_f = data_f[-n_samples :]

            if filt:
                plot_data = data_f / scale
            elif not filt:
                plot_data = (data - data.mean(axis=0)) / scale

            sd = np.std(plot_data[-int(sfreq) :], axis=0)[::-1] * scale
            co = np.int32(np.tanh((sd - 30) / 15) * 5 + 5)

            for ii in range(n_chans):
                quality[ii].text = "%.2f" % (sd[ii])
                quality[ii].color = quality_colors[co[ii]]
                quality[ii].font_size = 3 + co[ii]

                names[ii].font_size = 3 + co[ii]
                names[ii].color = quality_colors[co[ii]]

            program["a_position"].set_data(plot_data.T.ravel().astype(np.float32))
            update()

if __name__ == "__main__":

    """ 1. CONNECT TO EEG STREAM """

    # Search for active LSL streams
    new_cursor = m.MoveCursor()
    new_cursor.draw(3)
    print('Looking for an EEG stream...')
    streams = resolve_byprop('type', 'EEG', timeout=2)
    if len(streams) == 0:
        raise RuntimeError('Can\'t find EEG stream.')

    # Set active EEG stream to inlet and apply time correction
    print("Start acquiring data")
    inlet = StreamInlet(streams[0], max_chunklen=12)
    eeg_time_correction = inlet.time_correction()

    # Get the stream info and description
    info = inlet.info()
    description = info.desc()

    # Get the sampling frequency
    # This is an important value that represents how many EEG data points are
    # collected in a second. This influences our frequency band calculation.
    # for the Muse 2016, this should always be 256
    fs = int(info.nominal_srate())

    """ 2. INITIALIZE BUFFERS """

    # Initialize raw EEG data buffer
    eeg_buffer_left = np.zeros((int(fs * BUFFER_LENGTH), 1))
    eeg_buffer_right = np.zeros((int(fs * BUFFER_LENGTH), 1))
    filter_state_left = None  # for use with the notch filter
    filter_state_right = None  # for use with the notch filter

    # plot()

    # Compute the number of epochs in "buffer_length"
    n_win_test = int(np.floor((BUFFER_LENGTH - EPOCH_LENGTH) /
                              SHIFT_LENGTH + 1))

    # Initialize the band power buffer (for plotting)
    # bands will be ordered: [delta, theta, alpha, beta]
    band_buffer = np.zeros((n_win_test, 4))

    """ 3. GET DATA """

    # The try/except structure allows to quit the while loop by aborting the
    # script with <Ctrl-C>
    print('Press Ctrl-C in the console to break the while loop.')
    oldTimeL = datetime.datetime.now() # initialize time delta
    oldTimeR = datetime.datetime.now() # initialize time delta

    """

    #calibrate
    left_thresh, right_thresh, left_delay, right_delay = calibrate(3)

    #normalize delay
    left_delay = left_delay * 40 + 100
    right_delay = right_delay * 30 + 70

    print("left_thresh: %d   right_thresh: %d" % (left_thresh, right_thresh))
    print("left_delay: %d   right_delay: %d" % (left_delay, right_delay))
    input()

    """

    n_samples = int(fs * 10) #10 is window size
    data_fLeft = np.zeros((n_samples, 1))
    data_fRight = np.zeros((n_samples, 1))
    af = [1.0]
    bf = create_filter(data_fLeft.T, fs, 3, 40.0, method = "fir") #do
    zi = lfilter_zi(bf, af)
    dataLeft = np.zeros((n_samples, 1))
    dataRight = np.zeros((n_samples, 1))
    filt_stateLeft = np.tile(zi, (1, 1)).transpose()
    filt_stateRight = np.tile(zi, (1, 1)).transpose()
    oldTimeL = datetime.datetime.now() # initialize time delta
    oldTimeR = datetime.datetime.now() # initialize time delta

    try:
        # The following loop acquires data, computes band powers, and calculates neurofeedback metrics based on those band powers
        while True:
            """Add some data at the end of each signal (real-time signals)."""
            samples, timestamps = inlet.pull_chunk(timeout=0.0, max_samples=100)
            new_cursor.check_direction()
            if timestamps:
                samplesLeft = np.array(samples)[:, INDEX_CHANNEL_LEFT]
                dataLeft = np.vstack([dataLeft, samplesLeft])
                dataLeft = dataLeft[-n_samples :]
                filt_samples, filt_stateLeft = lfilter(
                    bf, af, samplesLeft, axis=0, zi=filt_stateLeft)
                data_fLeft = np.vstack([data_fLeft, filt_samples])
                data_fLeft = data_fLeft[-n_samples :]
                plot_data = data_fLeft / 500

                sd = np.std(plot_data[-int(fs) :], axis=0)[::-1] * 500 # 500 = scale
                co = np.int32(np.tanh((sd - 30) / 15) * 5 + 5)

                if(co > 1):
                    #print("Left eye blink" + str(sd))
                    newTime = datetime.datetime.now()
                    if (newTime - oldTimeL).total_seconds()*1000 > 150:
                        new_cursor.movement = False
                        new_cursor.start_action("L")
                    oldTimeL = newTime

                # ----------------------

                samplesRight = np.array(samples)[:, INDEX_CHANNEL_RIGHT]
                dataRight = np.vstack([dataRight, samplesRight])
                dataRight = dataRight[-n_samples :]
                filt_samples, filt_stateRight = lfilter(
                    bf, af, samplesRight, axis=0, zi=filt_stateRight)
                data_fRight = np.vstack([data_fRight, filt_samples])
                data_fRight = data_fRight[-n_samples :]
                plot_data = data_fRight / 500

                sd = np.std(plot_data[-int(fs) :], axis=0)[::-1] * 500 # 500 = scale
                co = np.int32(np.tanh((sd - 30) / 15) * 5 + 5)

                if(co > 1):
                    #print("Right eye blink" + str(sd))
                    newTime = datetime.datetime.now()
                    if (newTime - oldTimeR).total_seconds()*1000 > 100:
                        new_cursor.movement = True if not new_cursor.movement else False
                        new_cursor.start_action("R")
                    oldTimeR = newTime

    except KeyboardInterrupt:
        print('Closing!')

max_stars_repo_name: synaptech-uw/docs

max_issues_repo_name: synaptech-uw/docs

max_forks_repo_name: synaptech-uw/docs

reason: import numpy,from scipy


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

import argparse
import logging
import os
import re

import numpy as np
import tensorflow as tf
from tensorflow import pywrap_tensorflow

def filter_optimizer(variables_name):
    optimizer_names = ["adam", "momentum", "adamw", "momentum", "lamb", 'accum']
    flag = False
    for name in optimizer_names:
        if name.lower() in variables_name.lower():
            flag = True
    return flag

def truncate_vocab(embeddings, required_vocab_size):
    vocab_size, _hidden_size = embeddings.shape
    if vocab_size != required_vocab_size:
        logging.info(f"Truncating embeddings from vocab size {vocab_size} to {required_vocab_size}.")
    return embeddings[:required_vocab_size, :]

def is_qkv_tensor(tensor_name):
    qkv_tensors = ["attention/self/key", "attention/self/query", "attention/self/value"]
    for n in qkv_tensors:
        if n in tensor_name:
            return True
    return False

def convert_google_ckpt_to_gc(
    ckpt_file,
    output_dir,
    num_embed_split,
    vocab_size,
    use_attention_bias,
    use_qkv_bias,
    use_cls_layer,
    use_qkv_split,
    dtype,
):

    saved_variables = []

    def add_variable(old_tensor, new_tensor):
        logging.info(f"{old_tensor} -> {new_tensor}")
        saved_variables.append(new_tensor)

    graph = tf.Graph()
    reader = pywrap_tensorflow.NewCheckpointReader(ckpt_file)
    var_to_shape_map = reader.get_variable_to_shape_map()
    with graph.as_default():
        sess = tf.compat.v1.Session()
        for tensor_name in sorted(var_to_shape_map):
            # Filter out the optimizer variables
            if filter_optimizer(tensor_name):
                continue
            if not use_cls_layer and "transform" in tensor_name:
                logging.info("Discarding dense layer before MLM loss.")
                continue
            if not use_attention_bias and "output/dense/bias" in tensor_name:
                logging.info("Discarding attention biases.")
                continue

            this_tensor_dtype = dtype
            if "cls/squad/" in tensor_name:
                # Keep SQuAD output dense layer weights as float32
                this_tensor_dtype = tf.float32
                tensor_value = tf.cast(reader.get_tensor(tensor_name), dtype=this_tensor_dtype)

            else:
                #  Cast all other tensors to required precision.
                tensor_value = tf.cast(
                    reader.get_tensor(tensor_name), dtype=this_tensor_dtype
                )

            if "word_embeddings" in tensor_name and num_embed_split > 1:
                # Split word_embeddings when num_split>1
                logging.info(
                    f"Splitting word embeddings info {num_embed_split} splits."
                )
                word_embeddings = truncate_vocab(tensor_value, vocab_size)
                hidden_size = np.shape(word_embeddings)[1]
                assert vocab_size % num_embed_split == 0
                size_per_slice = int(vocab_size / num_embed_split)
                for i in range(num_embed_split):
                    start_idx = i * size_per_slice
                    end_idx = (i + 1) * size_per_slice
                    we_pieces = tf.Variable(
                        word_embeddings[start_idx:end_idx, :],
                        shape=(size_per_slice, hidden_size),
                        name=f"bert/embeddings/s{i}/word_embeddings",
                    )
                    add_variable(tensor_name, we_pieces)

            #  Truncate word embeddings to  vocab_size
            elif "word_embeddings" in tensor_name:
                full_word_embeddings = tf.Variable(truncate_vocab(tensor_value, vocab_size), name=tensor_name)
                add_variable(tensor_name, full_word_embeddings)

            # Rename tensor
            elif "attention/output" in tensor_name:
                new_name = tensor_name.replace("attention/output", "attention/projection")
                proj = tf.Variable(tensor_value, name=new_name)
                add_variable(tensor_name, proj)

            elif is_qkv_tensor(tensor_name):
                # We will process self-attention parameters outside the loop
                continue

            else:
                others_var = tf.Variable(tensor_value, name=tensor_name)
                add_variable(tensor_name, others_var)

        # Concatenate or split QKV
        layer_re = re.compile('.*/layer_([0-9]+)/.*')
        matches = [layer_re.match(k) for k in var_to_shape_map.keys()]
        num_hidden_layers = max([int(m.group(1)) for m in matches if m is not None]) + 1

        logging.info("Concatenate query, key, value layers into one.")
        for i in range(num_hidden_layers):
            layer_name = f"bert/encoder/layer_{i}/attention/self"
            # Combine query,key,value to qkv_weight
            qkv_weight = []
            qkv_bias = []
            for name in ["query", "key", "value"]:
                weight_name = layer_name + f"/{name}/kernel"
                bias_name = layer_name + f"/{name}/bias"
                weight = tf.cast(reader.get_tensor(weight_name), dtype=dtype)
                bias = tf.cast(reader.get_tensor(bias_name), dtype=dtype)

                if use_qkv_split:
                    add_variable(weight_name, tf.Variable(weight, name=weight_name))
                else:
                    # Concatenate
                    qkv_weight.append(weight)

                # The QKV bias is always concantenated
                qkv_bias.append(bias)

            # import ipdb; ipdb.set_trace()
            if not use_qkv_split:
                qkv_weight = tf.concat(qkv_weight, axis=-1)
                qkv_w = tf.Variable(qkv_weight, shape=qkv_weight.shape, name=layer_name + "/kernel/qkv_weight")
                add_variable("qkv_weight", qkv_w)

            if use_qkv_bias:
                qkv_bias = tf.concat(qkv_bias, axis=0)
                qkv_b = tf.Variable(qkv_bias, shape=qkv_bias.shape, name=layer_name + "/qkv_bias")
                add_variable("qkv_bias", qkv_b)

        sess.run(tf.compat.v1.global_variables_initializer())
        saver = tf.compat.v1.train.Saver()
        _dir_name, ckpt_name = os.path.split(ckpt_file)
        output_file = os.path.join(output_dir, ckpt_name)
        saver.save(sess, output_file)

        num_params = np.sum([np.prod(v.shape) for v in saved_variables])
        print(f"Number of parameters saved: {num_params}")

def convert_gc_ckpt_to_google(
    ckpt_file, output_dir=None, include_qkv_bias=False, dtype=tf.float32
):
    graph = tf.Graph()
    dir_name, ckpt_name = os.path.split(os.path.abspath(ckpt_file))
    reader = pywrap_tensorflow.NewCheckpointReader(ckpt_file)
    var_to_shape_map = reader.get_variable_to_shape_map()
    with graph.as_default():
        sess = tf.Session()
        num_hidden_layers = 0
        word_embeddings = []
        new_variables = []
        keep_variables = []
        for tensor_name in var_to_shape_map:
            logging.info(f"Loading {tensor_name}")
            # Filter the optimizer variables
            if filter_optimizer(tensor_name):
                continue

            tensor_value = tf.cast(reader.get_tensor(tensor_name), dtype=dtype)
            if "word_embeddings" in tensor_name:
                word_embeddings.append(tensor_name)
            elif "attention" in tensor_name:
                layer_idx = int(tensor_name.split("/")[2].split("_")[-1])
                num_hidden_layers = max(layer_idx, num_hidden_layers)
                if "qkv_bias" in tensor_name and include_qkv_bias:
                    hidden_size = tensor_value.shape[0] // 3
                    query_bias = tensor_value[:hidden_size]
                    key_bias = tensor_value[hidden_size:2*hidden_size]
                    value_bias = tensor_value[2*hidden_size:]
                    qb = tf.Variable(
                        query_bias, name=tensor_name.replace("qkv_bias", "query/bias")
                    )
                    kb = tf.Variable(
                        key_bias, name=tensor_name.replace("qkv_bias", "key/bias")
                    )
                    vb = tf.Variable(
                        value_bias, name=tensor_name.replace("qkv_bias", "value/bias")
                    )
                    new_variables.extend([qb, kb, vb])
                # rename projection to output
                elif "projection" in tensor_name:
                    new_name = tensor_name.replace("projection", "output")

                    proj = tf.Variable(tensor_value, name=new_name)
                    new_variables.append(proj)
            else:
                var = tf.get_variable(tensor_name, shape=tensor_value.shape, dtype=dtype)
                keep_variables.append(var)

        # Combine split embeddings
        word_embeddings = np.sort(word_embeddings)
        embeddings_vals = [reader.get_tensor(k) for k in word_embeddings]
        unit_embeddings = np.vstack(embeddings_vals)
        logging.debug(f"Concated word_embeddings shape: {unit_embeddings.shape}")
        we = tf.Variable(
            unit_embeddings,
            dtype=dtype,
            shape=unit_embeddings.shape,
            name="bert/embeddings/word_embeddings",
        )
        new_variables.append(we)
        saved_variables = new_variables + keep_variables
        sess.run(tf.compat.v1.global_variables_initializer())
        saver = tf.compat.v1.train.Saver(var_list=saved_variables)
        output_file = os.path.join(output_dir, ckpt_name)
        saver.save(sess, output_file)
        print("Saved to :" + output_file)

if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description="Checkpoint conversion tool.",
        add_help=True,
        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
    )
    parser.add_argument("action",
                        type=str,
                        choices=["graphcore2google", "google2graphcore"],
                        help="""Type of conversion between models""")
    parser.add_argument("checkpoint_path",
                        type=str,
                        help="""Path to the existing checkpoint to convert.""")
    parser.add_argument(
        "--output_dir",
        type=str,
        default="converted_ckpt",
        help="""Path to the output directory where the converted model will be stored."""
    )
    # Model-specific arguments
    parser.add_argument(
        "--vocab_size",
        type=int,
        default=30400,
        help="""Vocabulary size of the converted model. The vocabulary list will be truncated if the initial model was trained with a larger vocabulary than vocab_size.""",
    )
    parser.add_argument(
        "--num_embeddings_splits",
        type=int,
        default=1,
        help="""Number word embeddings splits.""",
    )
    parser.add_argument(
        "--use_attention_bias",
        action="store_true",
        default=True,
        help="""Use attention bias in converted model.""",
    )
    parser.add_argument(
        "--use_qkv_bias",
        action="store_true",
        default=True,
        help="""Use biases in the QKV layer.""",
    )
    parser.add_argument(
        "--use_cls_layer",
        action="store_true",
        default=False,
        help="""Use a final dense layer.""",
    )
    parser.add_argument(
        "--use_qkv_split",
        action="store_true",
        default=True,
        help="""Split the QKV matrix into three matrices.""",
    )
    parser.add_argument(
        "--precision",
        type=int,
        choices=[16, 32],
        default=16,
        help="""Default floating-point precision of converted model""",
    )
    args = parser.parse_args()

    if not os.path.exists(args.output_dir):
        os.makedirs(args.output_dir)

    precision_type = None
    if args.precision == 16:
        precision_type = tf.float16
    elif args.precision == 32:
        precision_type = 32
    else:
        raise ValueError(f"Unsupported precision format {args.precision}")

    if args.action.lower() == "google2graphcore":
        convert_google_ckpt_to_gc(
            args.checkpoint_path,
            args.output_dir,
            num_embed_split=args.num_embeddings_splits,
            vocab_size=args.vocab_size,
            use_attention_bias=args.use_attention_bias,
            use_qkv_bias=args.use_qkv_bias,
            use_cls_layer=args.use_cls_layer,
            use_qkv_split=args.use_qkv_split,
            dtype=precision_type,
        )

    elif args.action.lower() == "graphcore2google":
        convert_gc_ckpt_to_google(
            args.checkpoint_path,
            args.output_dir,
            num_embed_split=args.num_embeddings_splits,
            vocab_size=args.vocab_size,
            use_attention_bias=args.use_attention_bias,
            use_qkv_bias=args.use_qkv_bias,
            use_cls_layer=args.use_cls_layer,
            dtype=precision_type,
        )


text:
r"""!\file
\ingroup meas
Measurement of logdet.
"""

import numpy as np
from pentinsula.h5utils import open_or_pass_file

import isle
from .measurement import Measurement, BufferSpec

class Logdet(Measurement):
    r"""!
    \ingroup meas
    Measure the log determinant of particles and holes.
    """

    def __init__(self, hfm, savePath, configSlice=slice(None, None, None), alpha=1):
        assert alpha in [0, 1]

        super().__init__(savePath,
                         (BufferSpec("particles", (), np.complex128, "particles"),
                          BufferSpec("holes", (), np.complex128, "holes")),
                         configSlice)

        self.hfm = hfm
        self.alpha = alpha

    def __call__(self, stage, itr):
        """!Record logdet."""
        if self.alpha == 1:
            self.nextItem("particles")[...] = isle.logdetM(self.hfm, stage.phi,
                                                           isle.Species.PARTICLE)
            self.nextItem("holes")[...] = isle.logdetM(self.hfm, stage.phi,
                                                       isle.Species.HOLE)
        else:
            # use dense, slow numpy routine to get stable result
            ld = np.linalg.slogdet(isle.Matrix(self.hfm.M(-1j*stage.phi, isle.Species.PARTICLE)))
            self.nextItem("particles")[...] = np.log(ld[0]) + ld[1]
            ld = np.linalg.slogdet(isle.Matrix(self.hfm.M(-1j*stage.phi, isle.Species.HOLE)))
            self.nextItem("holes")[...] = np.log(ld[0]) + ld[1]

    def setup(self, memoryAllowance, expectedNConfigs, file, maxBufferSize=None):
        res = super().setup(memoryAllowance, expectedNConfigs, file, maxBufferSize)
        with open_or_pass_file(file, None, "a") as h5f:
            h5f[self.savePath].attrs["alpha"] = self.alpha
        return res

max_stars_repo_name: chelseajohn/isle

max_issues_repo_name: chelseajohn/isle

max_forks_repo_name: chelseajohn/isle


text:
from h3 import h3
from math import radians, cos, sin, asin, sqrt , floor, pow
import numpy as np
import math
import csv
from folium import folium
from folium import features
from folium import map

print('ifd')
n = 0
while n < 21 :
	print('n=',n,'ifd=', 0.152 * pow(2,n))
	n += 1

print('edge length')
res = 0
while res < 16 :
	print('res=', res, 'edge length=', h3.edge_length(res, unit='km'), 'km')
	res += 1


text:
import pandas
import numpy
from sklearn import ensemble
from sklearn.model_selection import train_test_split

# Read in the breast cancer data.csv
data = pandas.read_csv("data.csv", header=0)

# Take a look at pandas dataframe format
# print(data.head())

# Data cleaning
mapping = {'M' : 0, 'B' : 1}
data['diagnosis'] = data['diagnosis'].map(mapping)

features = list(data.columns[1:31]) # Appending all the columns in feature vector
train_features, test_features, train_labels, test_labels = train_test_split(data[features], data['diagnosis'].values, test_size=0.20, random_state=10)

# Get the random forest classifier from the scikit library we imported
classifier = ensemble.RandomForestClassifier()

# Train your classifier with our training data split
trained_classifier = classifier.fit(train_features.values, train_labels)

# Let's try out our trained classifier
y_prediction = trained_classifier.predict(test_features.values)

# Print out the predictions vs the actual values
print(y_prediction)
print(test_labels)

num_correct_predictions = numpy.sum(y_prediction == test_labels)
num_test_samples = float(len(test_labels))

print ("ML Accuracy", num_correct_predictions / num_test_samples)

max_stars_repo_name: mlatmd/intro-to-ml

max_issues_repo_name: mlatmd/intro-to-ml

max_forks_repo_name: mlatmd/intro-to-ml


text:
import os
import cv2
import torch
import core.networks as networks
import core.loss as loss
import core.utils as utils
import numpy as np

from abc import ABC
from core.base_inpaint import BaseInpaint

class FacialInpaint(BaseInpaint, ABC):
    def __init__(self, hy):
        super(FacialInpaint, self).__init__(hy)
        # define gen net
        self.inpaint_gen = networks.define_inpaint_gen(hy, self.gpu_ids, self.init_type, self.init_gain)
        self.semantic_gan = networks.define_semantic_gan(hy, self.gpu_ids, self.init_type, self.init_gain)
        self.model_names.append('inpaint_gen')
        self.model_names.append('semantic_gan')

        self.inpaint_dis = networks.define_MultiscaleDis(hy, self.input_dim, self.mult_dis_para['output_nc'],
                                                         self.gpu_ids, self.init_type, self.init_gain)
        self.semantic_dis = networks.define_MultiscaleDis(hy, self.num_semantic_label, self.mult_dis_para['output_nc'],
                                                          self.gpu_ids, self.init_type, self.init_gain)
        self.model_names.append('inpaint_dis')
        self.model_names.append('semantic_dis')

        print('---------- Networks initialized -------------')
        self.print_network()

        if self.is_train:
            self.init_optimizer()

        if self.is_train:
            self.criterionGAN = loss.MultiScaleRelativisticAverageLoss()
            self.criterionL1 = torch.nn.L1Loss()
            self.criterionL2 = torch.nn.MSELoss()
            self.PerceptualLoss = loss.PerceptualLoss()
            self.StyleLoss = loss.StyleLoss()
            # segmap
            self.SegmapLoss = loss.MultiScaleRelativisticAverageLoss()

    def set_input(self, inputs):
        file_name, image_gt, mask, semantic_label, segmap_one_hot, edge = inputs

        self.file_name = file_name
        self.image_gt = image_gt.to(self.device)
        self.mask = mask.to(self.device)
        self.semantic_label = semantic_label.to(self.device)
        self.semantic_one_hot = segmap_one_hot.to(self.device)
        self.edge = edge.to(self.device)

        # target(just for Cross entropy Loss)
        self.target_semantic = self.semantic_label[:, 0, :, :]

        # mask 0 is hole
        self.inv_ex_mask = torch.add(torch.neg(self.mask.float()), 1).float()

        # input
        self.input_edge = self.edge * self.mask[:, 0:1, :, :]
        self.input_segmap = self.semantic_one_hot * self.inv_ex_mask[:, 0:1, :, :]
        # Do not set the mask regions as 0
        self.input_image = image_gt.to(self.device)
        self.input_image.narrow(1, 0, 1).masked_fill_(self.mask.narrow(1, 0, 1).bool(), 2 * 123.0 / 255.0 - 1.0)
        self.input_image.narrow(1, 1, 1).masked_fill_(self.mask.narrow(1, 0, 1).bool(), 2 * 104.0 / 255.0 - 1.0)
        self.input_image.narrow(1, 2, 1).masked_fill_(self.mask.narrow(1, 0, 1).bool(), 2 * 117.0 / 255.0 - 1.0)

    def forward(self, mode='semantic_net'):
        net_input = torch.cat([self.input_image, self.input_edge], dim=1)
        if mode == 'semantic_net':
            self.fake_semantic = self.semantic_gan(net_input)
        elif mode == 'inpaint_net':
            self.fake_image = self.inpaint_gen(net_input, self.fake_semantic.detach(), self.mask)
        else:
            raise NotImplementedError

    def backward_semantic_dis(self):
        real = self.semantic_one_hot
        fake = self.fake_semantic
        # gan loss
        pred_fake = self.semantic_dis(fake.detach())
        pred_real = self.semantic_dis(real)
        self.loss_sd_semantic = self.SegmapLoss(pred_real, pred_fake, True)
        # backward
        self.loss_sd_semantic.backward()

    def backward_semantic_gen(self):
        real = self.semantic_one_hot
        fake = self.fake_semantic
        # gan loss
        pred_fake = self.semantic_dis(fake)
        pred_real = self.semantic_dis(real)
        self.loss_sg_semantic = self.SegmapLoss(pred_real, pred_fake, False)
        # L1 loss
        self.loss_sg_semantic_L1 = self.criterionL1(fake, real)
        # total loss
        self.loss_sg = self.loss_sg_semantic * 0.1 + self.loss_sg_semantic_L1 * 100
        # backward
        self.loss_sg.backward()

    def backward_inpaint_dis(self):
        real = self.image_gt
        fake = self.fake_image
        # # gan loss
        pred_fake = self.inpaint_dis(fake.detach())
        pred_real = self.inpaint_dis(real)
        self.loss_id_gan = self.criterionGAN(pred_real, pred_fake, True)
        # backward
        self.loss_id_gan.backward()

    def backward_inpaint_gen(self):
        real = self.image_gt
        fake = self.fake_image

        # Reconstruction loss, style loss, L1 loss
        self.loss_ig_L1 = self.criterionL1(fake, real)
        self.loss_ig_perceptual = self.PerceptualLoss(fake, real)
        self.loss_ig_style = self.StyleLoss(fake, real)
        # gan loss
        pred_fake = self.inpaint_dis(fake)
        pred_real = self.inpaint_dis(real)
        self.loss_ig_gan = self.criterionGAN(pred_real, pred_fake, False)
        # total loss
        self.loss_ig = self.loss_ig_L1 * 1 + self.loss_ig_perceptual * 0.2 + self.loss_ig_style * 250 \
                       + self.loss_ig_gan
        # backward
        self.loss_ig.backward()

    def optimize_parameters(self, epoch):
        if self.is_train:
            self.forward(mode='semantic_net')
            self.optimize_semantic_net()
            if epoch > self.num_train_semantic_net:
                self.forward(mode='inpaint_net')
                self.optimize_inpaint_net()
        else:
            self.test()

    def optimize_semantic_net(self):
        # Optimize the SemanticExtractNet Dis
        self.set_requires_grad(self.semantic_gan, False)
        self.set_requires_grad(self.semantic_dis, True)
        self.optimizer_semantic_dis.zero_grad()
        self.backward_semantic_dis()
        self.optimizer_semantic_dis.step()

        # Optimize the SemanticExtractNet G
        self.set_requires_grad(self.semantic_gan, True)
        self.set_requires_grad(self.semantic_dis, False)
        self.optimizer_semantic_gan.zero_grad()
        self.backward_semantic_gen()
        self.optimizer_semantic_gan.step()

    def optimize_inpaint_net(self):
        # Optimize the Gen Dis
        self.set_requires_grad(self.inpaint_gen, False)
        self.set_requires_grad(self.inpaint_dis, True)
        self.optimizer_inpaint_dis.zero_grad()
        self.backward_inpaint_dis()
        self.optimizer_inpaint_dis.step()

        # Optimize the Gen G
        self.set_requires_grad(self.inpaint_gen, True)
        self.set_requires_grad(self.inpaint_dis, False)
        self.optimizer_inpaint_gen.zero_grad()
        self.backward_inpaint_gen()
        self.optimizer_inpaint_gen.step()

    def get_current_errors(self, epoch):
        loss_dict = {
            # discriminator
            'loss_sd_semantic': self.loss_sd_semantic.data,
            # Generator
            'loss_sg': self.loss_sg.data,
            'loss_sg_semantic': self.loss_sg_semantic.data,
            'loss_sg_semantic_L1': self.loss_sg_semantic_L1.data,
        }
        if epoch > self.num_train_semantic_net:
            # discriminator
            loss_dict['loss_id_gan'] = self.loss_id_gan.data
            # Generator
            loss_dict['loss_ig_L1'] = self.loss_ig_L1.data
            loss_dict['loss_ig_perceptual'] = self.loss_ig_perceptual.data
            loss_dict['loss_ig_style'] = self.loss_ig_style.data
            loss_dict['loss_ig_gan'] = self.loss_ig_gan.data
            loss_dict['loss_ig'] = self.loss_ig.data

        return loss_dict

    def get_current_visuals(self, epoch):
        input_image = (self.input_image.data.cpu() + 1) / 2.0
        gt_image = (self.image_gt.data.cpu() + 1) / 2.0
        mask = self.mask.data.cpu()

        pred_semantic = utils.tensor2label(self.fake_semantic, self.num_semantic_label)
        pred_semantic = pred_semantic.transpose(0, 3, 1, 2) / 255
        pred_semantic = torch.from_numpy(pred_semantic)

        semantic = utils.tensor2label(self.semantic_one_hot, self.num_semantic_label)
        semantic = semantic.transpose(0, 3, 1, 2) / 255
        semantic = torch.from_numpy(semantic)

        if epoch > self.num_train_semantic_net:
            fake_image = (self.fake_image.data.cpu() + 1) / 2.0
            return semantic, pred_semantic, input_image, gt_image, mask, fake_image
        else:
            return semantic, pred_semantic, input_image, gt_image, mask

    def test(self):
        self.forward(mode='semantic_net')
        self.forward(mode='inpaint_net')
        # save
        self.save_image('mask', self.mask.cpu().numpy())
        self.save_image('image_gt', self.image_gt.cpu().numpy())
        self.save_image('input', self.input_image.cpu().numpy())
        # semantic
        real_semantic = utils.tensor2label(self.semantic_one_hot.cpu(), self.num_semantic_label)
        fake_semantic = utils.tensor2label(self.fake_semantic.cpu(), self.num_semantic_label)
        self.save_image('real_semantic', real_semantic, is_hwc=True)
        self.save_image('fake_semantic', fake_semantic, is_hwc=True)

    def save_image(self, image_title, image_tensor, is_hwc=False, is_rgb=True):
        image_np = image_tensor.astype(np.uint8)
        if not is_hwc:
            image_np = image_np.transpose((0, 2, 3, 1)) * 255

        save_path = os.path.join(self.output_dir, image_title)
        if not os.path.exists(save_path):
            utils.mkdirs(save_path)

        if image_np.ndim == 4:
            for b in range(image_np.shape[0]):
                file_name = self.file_name[b]
                if is_rgb:
                    image_np = image_np[b][..., ::-1]
                file_path = os.path.join(save_path, file_name)
                cv2.imwrite(file_path, image_np)
        # Gray image
        elif image_tensor.ndim == 3:
            for b in range(image_np.shape[0]):
                file_name = self.file_name[b]
                if is_rgb:
                    image_np = image_np[..., ::-1]
                file_path = os.path.join(save_path, file_name)
                cv2.imwrite(file_path, image_np)